Home | History | Annotate | Download | only in ld
      1 /* Linker command language support.
      2    Copyright (C) 1991-2014 Free Software Foundation, Inc.
      3 
      4    This file is part of the GNU Binutils.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program; if not, write to the Free Software
     18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     19    MA 02110-1301, USA.  */
     20 
     21 #include "sysdep.h"
     22 #include "bfd.h"
     23 #include "libiberty.h"
     24 #include "filenames.h"
     25 #include "safe-ctype.h"
     26 #include "obstack.h"
     27 #include "bfdlink.h"
     28 
     29 #include "ld.h"
     30 #include "ldmain.h"
     31 #include "ldexp.h"
     32 #include "ldlang.h"
     33 #include <ldgram.h>
     34 #include "ldlex.h"
     35 #include "ldmisc.h"
     36 #include "ldctor.h"
     37 #include "ldfile.h"
     38 #include "ldemul.h"
     39 #include "fnmatch.h"
     40 #include "demangle.h"
     41 #include "hashtab.h"
     42 #include "libbfd.h"
     43 #include "elf-bfd.h"
     44 #ifdef ENABLE_PLUGINS
     45 #include "plugin.h"
     46 #endif /* ENABLE_PLUGINS */
     47 
     48 #ifndef offsetof
     49 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
     50 #endif
     51 
     52 /* Locals variables.  */
     53 static struct obstack stat_obstack;
     54 static struct obstack map_obstack;
     55 
     56 #define obstack_chunk_alloc xmalloc
     57 #define obstack_chunk_free free
     58 static const char *entry_symbol_default = "start";
     59 static bfd_boolean placed_commons = FALSE;
     60 static bfd_boolean map_head_is_link_order = FALSE;
     61 static lang_output_section_statement_type *default_common_section;
     62 static bfd_boolean map_option_f;
     63 static bfd_vma print_dot;
     64 static lang_input_statement_type *first_file;
     65 static const char *current_target;
     66 static lang_statement_list_type statement_list;
     67 static struct bfd_hash_table lang_definedness_table;
     68 static lang_statement_list_type *stat_save[10];
     69 static lang_statement_list_type **stat_save_ptr = &stat_save[0];
     70 static struct unique_sections *unique_section_list;
     71 static struct asneeded_minfo *asneeded_list_head;
     72 
     73 /* Forward declarations.  */
     74 static void exp_init_os (etree_type *);
     75 static lang_input_statement_type *lookup_name (const char *);
     76 static struct bfd_hash_entry *lang_definedness_newfunc
     77  (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
     78 static void insert_undefined (const char *);
     79 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
     80 static void print_statement (lang_statement_union_type *,
     81 			     lang_output_section_statement_type *);
     82 static void print_statement_list (lang_statement_union_type *,
     83 				  lang_output_section_statement_type *);
     84 static void print_statements (void);
     85 static void print_input_section (asection *, bfd_boolean);
     86 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
     87 static void lang_record_phdrs (void);
     88 static void lang_do_version_exports_section (void);
     89 static void lang_finalize_version_expr_head
     90   (struct bfd_elf_version_expr_head *);
     91 
     92 /* Exported variables.  */
     93 const char *output_target;
     94 lang_output_section_statement_type *abs_output_section;
     95 lang_statement_list_type lang_output_section_statement;
     96 lang_statement_list_type *stat_ptr = &statement_list;
     97 lang_statement_list_type file_chain = { NULL, NULL };
     98 lang_statement_list_type input_file_chain;
     99 struct bfd_sym_chain entry_symbol = { NULL, NULL };
    100 const char *entry_section = ".text";
    101 struct lang_input_statement_flags input_flags;
    102 bfd_boolean entry_from_cmdline;
    103 bfd_boolean undef_from_cmdline;
    104 bfd_boolean lang_has_input_file = FALSE;
    105 bfd_boolean had_output_filename = FALSE;
    106 bfd_boolean lang_float_flag = FALSE;
    107 bfd_boolean delete_output_file_on_failure = FALSE;
    108 struct lang_phdr *lang_phdr_list;
    109 struct lang_nocrossrefs *nocrossref_list;
    110 struct asneeded_minfo **asneeded_list_tail;
    111 
    112  /* Functions that traverse the linker script and might evaluate
    113     DEFINED() need to increment this at the start of the traversal.  */
    114 int lang_statement_iteration = 0;
    115 
    116 /* Return TRUE if the PATTERN argument is a wildcard pattern.
    117    Although backslashes are treated specially if a pattern contains
    118    wildcards, we do not consider the mere presence of a backslash to
    119    be enough to cause the pattern to be treated as a wildcard.
    120    That lets us handle DOS filenames more naturally.  */
    121 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
    122 
    123 #define new_stat(x, y) \
    124   (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
    125 
    126 #define outside_section_address(q) \
    127   ((q)->output_offset + (q)->output_section->vma)
    128 
    129 #define outside_symbol_address(q) \
    130   ((q)->value + outside_section_address (q->section))
    131 
    132 #define SECTION_NAME_MAP_LENGTH (16)
    133 
    134 void *
    135 stat_alloc (size_t size)
    136 {
    137   return obstack_alloc (&stat_obstack, size);
    138 }
    139 
    140 static int
    141 name_match (const char *pattern, const char *name)
    142 {
    143   if (wildcardp (pattern))
    144     return fnmatch (pattern, name, 0);
    145   return strcmp (pattern, name);
    146 }
    147 
    148 /* If PATTERN is of the form archive:file, return a pointer to the
    149    separator.  If not, return NULL.  */
    150 
    151 static char *
    152 archive_path (const char *pattern)
    153 {
    154   char *p = NULL;
    155 
    156   if (link_info.path_separator == 0)
    157     return p;
    158 
    159   p = strchr (pattern, link_info.path_separator);
    160 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
    161   if (p == NULL || link_info.path_separator != ':')
    162     return p;
    163 
    164   /* Assume a match on the second char is part of drive specifier,
    165      as in "c:\silly.dos".  */
    166   if (p == pattern + 1 && ISALPHA (*pattern))
    167     p = strchr (p + 1, link_info.path_separator);
    168 #endif
    169   return p;
    170 }
    171 
    172 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
    173    return whether F matches FILE_SPEC.  */
    174 
    175 static bfd_boolean
    176 input_statement_is_archive_path (const char *file_spec, char *sep,
    177 				 lang_input_statement_type *f)
    178 {
    179   bfd_boolean match = FALSE;
    180 
    181   if ((*(sep + 1) == 0
    182        || name_match (sep + 1, f->filename) == 0)
    183       && ((sep != file_spec)
    184 	  == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL)))
    185     {
    186       match = TRUE;
    187 
    188       if (sep != file_spec)
    189 	{
    190 	  const char *aname = f->the_bfd->my_archive->filename;
    191 	  *sep = 0;
    192 	  match = name_match (file_spec, aname) == 0;
    193 	  *sep = link_info.path_separator;
    194 	}
    195     }
    196   return match;
    197 }
    198 
    199 static bfd_boolean
    200 unique_section_p (const asection *sec,
    201 		  const lang_output_section_statement_type *os)
    202 {
    203   struct unique_sections *unam;
    204   const char *secnam;
    205 
    206   if (link_info.relocatable
    207       && sec->owner != NULL
    208       && bfd_is_group_section (sec->owner, sec))
    209     return !(os != NULL
    210 	     && strcmp (os->name, DISCARD_SECTION_NAME) == 0);
    211 
    212   secnam = sec->name;
    213   for (unam = unique_section_list; unam; unam = unam->next)
    214     if (name_match (unam->name, secnam) == 0)
    215       return TRUE;
    216 
    217   return FALSE;
    218 }
    219 
    220 /* Generic traversal routines for finding matching sections.  */
    221 
    222 /* Try processing a section against a wildcard.  This just calls
    223    the callback unless the filename exclusion list is present
    224    and excludes the file.  It's hardly ever present so this
    225    function is very fast.  */
    226 
    227 static void
    228 walk_wild_consider_section (lang_wild_statement_type *ptr,
    229 			    lang_input_statement_type *file,
    230 			    asection *s,
    231 			    struct wildcard_list *sec,
    232 			    callback_t callback,
    233 			    void *data)
    234 {
    235   struct name_list *list_tmp;
    236 
    237   /* Don't process sections from files which were excluded.  */
    238   for (list_tmp = sec->spec.exclude_name_list;
    239        list_tmp;
    240        list_tmp = list_tmp->next)
    241     {
    242       char *p = archive_path (list_tmp->name);
    243 
    244       if (p != NULL)
    245 	{
    246 	  if (input_statement_is_archive_path (list_tmp->name, p, file))
    247 	    return;
    248 	}
    249 
    250       else if (name_match (list_tmp->name, file->filename) == 0)
    251 	return;
    252 
    253       /* FIXME: Perhaps remove the following at some stage?  Matching
    254 	 unadorned archives like this was never documented and has
    255 	 been superceded by the archive:path syntax.  */
    256       else if (file->the_bfd != NULL
    257 	       && file->the_bfd->my_archive != NULL
    258 	       && name_match (list_tmp->name,
    259 			      file->the_bfd->my_archive->filename) == 0)
    260 	return;
    261     }
    262 
    263   (*callback) (ptr, sec, s, ptr->section_flag_list, file, data);
    264 }
    265 
    266 /* Lowest common denominator routine that can handle everything correctly,
    267    but slowly.  */
    268 
    269 static void
    270 walk_wild_section_general (lang_wild_statement_type *ptr,
    271 			   lang_input_statement_type *file,
    272 			   callback_t callback,
    273 			   void *data)
    274 {
    275   asection *s;
    276   struct wildcard_list *sec;
    277 
    278   for (s = file->the_bfd->sections; s != NULL; s = s->next)
    279     {
    280       sec = ptr->section_list;
    281       if (sec == NULL)
    282 	(*callback) (ptr, sec, s, ptr->section_flag_list, file, data);
    283 
    284       while (sec != NULL)
    285 	{
    286 	  bfd_boolean skip = FALSE;
    287 
    288 	  if (sec->spec.name != NULL)
    289 	    {
    290 	      const char *sname = bfd_get_section_name (file->the_bfd, s);
    291 
    292 	      skip = name_match (sec->spec.name, sname) != 0;
    293 	    }
    294 
    295 	  if (!skip)
    296 	    walk_wild_consider_section (ptr, file, s, sec, callback, data);
    297 
    298 	  sec = sec->next;
    299 	}
    300     }
    301 }
    302 
    303 /* Routines to find a single section given its name.  If there's more
    304    than one section with that name, we report that.  */
    305 
    306 typedef struct
    307 {
    308   asection *found_section;
    309   bfd_boolean multiple_sections_found;
    310 } section_iterator_callback_data;
    311 
    312 static bfd_boolean
    313 section_iterator_callback (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *data)
    314 {
    315   section_iterator_callback_data *d = (section_iterator_callback_data *) data;
    316 
    317   if (d->found_section != NULL)
    318     {
    319       d->multiple_sections_found = TRUE;
    320       return TRUE;
    321     }
    322 
    323   d->found_section = s;
    324   return FALSE;
    325 }
    326 
    327 static asection *
    328 find_section (lang_input_statement_type *file,
    329 	      struct wildcard_list *sec,
    330 	      bfd_boolean *multiple_sections_found)
    331 {
    332   section_iterator_callback_data cb_data = { NULL, FALSE };
    333 
    334   bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
    335 			      section_iterator_callback, &cb_data);
    336   *multiple_sections_found = cb_data.multiple_sections_found;
    337   return cb_data.found_section;
    338 }
    339 
    340 /* Code for handling simple wildcards without going through fnmatch,
    341    which can be expensive because of charset translations etc.  */
    342 
    343 /* A simple wild is a literal string followed by a single '*',
    344    where the literal part is at least 4 characters long.  */
    345 
    346 static bfd_boolean
    347 is_simple_wild (const char *name)
    348 {
    349   size_t len = strcspn (name, "*?[");
    350   return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
    351 }
    352 
    353 static bfd_boolean
    354 match_simple_wild (const char *pattern, const char *name)
    355 {
    356   /* The first four characters of the pattern are guaranteed valid
    357      non-wildcard characters.  So we can go faster.  */
    358   if (pattern[0] != name[0] || pattern[1] != name[1]
    359       || pattern[2] != name[2] || pattern[3] != name[3])
    360     return FALSE;
    361 
    362   pattern += 4;
    363   name += 4;
    364   while (*pattern != '*')
    365     if (*name++ != *pattern++)
    366       return FALSE;
    367 
    368   return TRUE;
    369 }
    370 
    371 /* Return the numerical value of the init_priority attribute from
    372    section name NAME.  */
    373 
    374 static unsigned long
    375 get_init_priority (const char *name)
    376 {
    377   char *end;
    378   unsigned long init_priority;
    379 
    380   /* GCC uses the following section names for the init_priority
    381      attribute with numerical values 101 and 65535 inclusive. A
    382      lower value means a higher priority.
    383 
    384      1: .init_array.NNNN/.fini_array.NNNN: Where NNNN is the
    385 	decimal numerical value of the init_priority attribute.
    386 	The order of execution in .init_array is forward and
    387 	.fini_array is backward.
    388      2: .ctors.NNNN/.dtors.NNNN: Where NNNN is 65535 minus the
    389 	decimal numerical value of the init_priority attribute.
    390 	The order of execution in .ctors is backward and .dtors
    391 	is forward.
    392    */
    393   if (strncmp (name, ".init_array.", 12) == 0
    394       || strncmp (name, ".fini_array.", 12) == 0)
    395     {
    396       init_priority = strtoul (name + 12, &end, 10);
    397       return *end ? 0 : init_priority;
    398     }
    399   else if (strncmp (name, ".ctors.", 7) == 0
    400 	   || strncmp (name, ".dtors.", 7) == 0)
    401     {
    402       init_priority = strtoul (name + 7, &end, 10);
    403       return *end ? 0 : 65535 - init_priority;
    404     }
    405 
    406   return 0;
    407 }
    408 
    409 /* Compare sections ASEC and BSEC according to SORT.  */
    410 
    411 static int
    412 compare_section (sort_type sort, asection *asec, asection *bsec)
    413 {
    414   int ret;
    415   unsigned long ainit_priority, binit_priority;
    416 
    417   switch (sort)
    418     {
    419     default:
    420       abort ();
    421 
    422     case by_init_priority:
    423       ainit_priority
    424 	= get_init_priority (bfd_get_section_name (asec->owner, asec));
    425       binit_priority
    426 	= get_init_priority (bfd_get_section_name (bsec->owner, bsec));
    427       if (ainit_priority == 0 || binit_priority == 0)
    428 	goto sort_by_name;
    429       ret = ainit_priority - binit_priority;
    430       if (ret)
    431 	break;
    432       else
    433 	goto sort_by_name;
    434 
    435     case by_alignment_name:
    436       ret = (bfd_section_alignment (bsec->owner, bsec)
    437 	     - bfd_section_alignment (asec->owner, asec));
    438       if (ret)
    439 	break;
    440       /* Fall through.  */
    441 
    442     case by_name:
    443 sort_by_name:
    444       ret = strcmp (bfd_get_section_name (asec->owner, asec),
    445 		    bfd_get_section_name (bsec->owner, bsec));
    446       break;
    447 
    448     case by_name_alignment:
    449       ret = strcmp (bfd_get_section_name (asec->owner, asec),
    450 		    bfd_get_section_name (bsec->owner, bsec));
    451       if (ret)
    452 	break;
    453       /* Fall through.  */
    454 
    455     case by_alignment:
    456       ret = (bfd_section_alignment (bsec->owner, bsec)
    457 	     - bfd_section_alignment (asec->owner, asec));
    458       break;
    459     }
    460 
    461   return ret;
    462 }
    463 
    464 /* Build a Binary Search Tree to sort sections, unlike insertion sort
    465    used in wild_sort(). BST is considerably faster if the number of
    466    of sections are large.  */
    467 
    468 static lang_section_bst_type **
    469 wild_sort_fast (lang_wild_statement_type *wild,
    470 		struct wildcard_list *sec,
    471 		lang_input_statement_type *file ATTRIBUTE_UNUSED,
    472 		asection *section)
    473 {
    474   lang_section_bst_type **tree;
    475 
    476   tree = &wild->tree;
    477   if (!wild->filenames_sorted
    478       && (sec == NULL || sec->spec.sorted == none))
    479     {
    480       /* Append at the right end of tree.  */
    481       while (*tree)
    482 	tree = &((*tree)->right);
    483       return tree;
    484     }
    485 
    486   while (*tree)
    487     {
    488       /* Find the correct node to append this section.  */
    489       if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
    490 	tree = &((*tree)->left);
    491       else
    492 	tree = &((*tree)->right);
    493     }
    494 
    495   return tree;
    496 }
    497 
    498 /* Use wild_sort_fast to build a BST to sort sections.  */
    499 
    500 static void
    501 output_section_callback_fast (lang_wild_statement_type *ptr,
    502 			      struct wildcard_list *sec,
    503 			      asection *section,
    504 			      struct flag_info *sflag_list ATTRIBUTE_UNUSED,
    505 			      lang_input_statement_type *file,
    506 			      void *output)
    507 {
    508   lang_section_bst_type *node;
    509   lang_section_bst_type **tree;
    510   lang_output_section_statement_type *os;
    511 
    512   os = (lang_output_section_statement_type *) output;
    513 
    514   if (unique_section_p (section, os))
    515     return;
    516 
    517   node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type));
    518   node->left = 0;
    519   node->right = 0;
    520   node->section = section;
    521 
    522   tree = wild_sort_fast (ptr, sec, file, section);
    523   if (tree != NULL)
    524     *tree = node;
    525 }
    526 
    527 /* Convert a sorted sections' BST back to list form.  */
    528 
    529 static void
    530 output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
    531 				      lang_section_bst_type *tree,
    532 				      void *output)
    533 {
    534   if (tree->left)
    535     output_section_callback_tree_to_list (ptr, tree->left, output);
    536 
    537   lang_add_section (&ptr->children, tree->section, NULL,
    538 		    (lang_output_section_statement_type *) output);
    539 
    540   if (tree->right)
    541     output_section_callback_tree_to_list (ptr, tree->right, output);
    542 
    543   free (tree);
    544 }
    545 
    546 /* Specialized, optimized routines for handling different kinds of
    547    wildcards */
    548 
    549 static void
    550 walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
    551 				lang_input_statement_type *file,
    552 				callback_t callback,
    553 				void *data)
    554 {
    555   /* We can just do a hash lookup for the section with the right name.
    556      But if that lookup discovers more than one section with the name
    557      (should be rare), we fall back to the general algorithm because
    558      we would otherwise have to sort the sections to make sure they
    559      get processed in the bfd's order.  */
    560   bfd_boolean multiple_sections_found;
    561   struct wildcard_list *sec0 = ptr->handler_data[0];
    562   asection *s0 = find_section (file, sec0, &multiple_sections_found);
    563 
    564   if (multiple_sections_found)
    565     walk_wild_section_general (ptr, file, callback, data);
    566   else if (s0)
    567     walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
    568 }
    569 
    570 static void
    571 walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
    572 				lang_input_statement_type *file,
    573 				callback_t callback,
    574 				void *data)
    575 {
    576   asection *s;
    577   struct wildcard_list *wildsec0 = ptr->handler_data[0];
    578 
    579   for (s = file->the_bfd->sections; s != NULL; s = s->next)
    580     {
    581       const char *sname = bfd_get_section_name (file->the_bfd, s);
    582       bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
    583 
    584       if (!skip)
    585 	walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
    586     }
    587 }
    588 
    589 static void
    590 walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
    591 				lang_input_statement_type *file,
    592 				callback_t callback,
    593 				void *data)
    594 {
    595   asection *s;
    596   struct wildcard_list *sec0 = ptr->handler_data[0];
    597   struct wildcard_list *wildsec1 = ptr->handler_data[1];
    598   bfd_boolean multiple_sections_found;
    599   asection *s0 = find_section (file, sec0, &multiple_sections_found);
    600 
    601   if (multiple_sections_found)
    602     {
    603       walk_wild_section_general (ptr, file, callback, data);
    604       return;
    605     }
    606 
    607   /* Note that if the section was not found, s0 is NULL and
    608      we'll simply never succeed the s == s0 test below.  */
    609   for (s = file->the_bfd->sections; s != NULL; s = s->next)
    610     {
    611       /* Recall that in this code path, a section cannot satisfy more
    612 	 than one spec, so if s == s0 then it cannot match
    613 	 wildspec1.  */
    614       if (s == s0)
    615 	walk_wild_consider_section (ptr, file, s, sec0, callback, data);
    616       else
    617 	{
    618 	  const char *sname = bfd_get_section_name (file->the_bfd, s);
    619 	  bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
    620 
    621 	  if (!skip)
    622 	    walk_wild_consider_section (ptr, file, s, wildsec1, callback,
    623 					data);
    624 	}
    625     }
    626 }
    627 
    628 static void
    629 walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
    630 				lang_input_statement_type *file,
    631 				callback_t callback,
    632 				void *data)
    633 {
    634   asection *s;
    635   struct wildcard_list *sec0 = ptr->handler_data[0];
    636   struct wildcard_list *wildsec1 = ptr->handler_data[1];
    637   struct wildcard_list *wildsec2 = ptr->handler_data[2];
    638   bfd_boolean multiple_sections_found;
    639   asection *s0 = find_section (file, sec0, &multiple_sections_found);
    640 
    641   if (multiple_sections_found)
    642     {
    643       walk_wild_section_general (ptr, file, callback, data);
    644       return;
    645     }
    646 
    647   for (s = file->the_bfd->sections; s != NULL; s = s->next)
    648     {
    649       if (s == s0)
    650 	walk_wild_consider_section (ptr, file, s, sec0, callback, data);
    651       else
    652 	{
    653 	  const char *sname = bfd_get_section_name (file->the_bfd, s);
    654 	  bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
    655 
    656 	  if (!skip)
    657 	    walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
    658 	  else
    659 	    {
    660 	      skip = !match_simple_wild (wildsec2->spec.name, sname);
    661 	      if (!skip)
    662 		walk_wild_consider_section (ptr, file, s, wildsec2, callback,
    663 					    data);
    664 	    }
    665 	}
    666     }
    667 }
    668 
    669 static void
    670 walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
    671 				lang_input_statement_type *file,
    672 				callback_t callback,
    673 				void *data)
    674 {
    675   asection *s;
    676   struct wildcard_list *sec0 = ptr->handler_data[0];
    677   struct wildcard_list *sec1 = ptr->handler_data[1];
    678   struct wildcard_list *wildsec2 = ptr->handler_data[2];
    679   struct wildcard_list *wildsec3 = ptr->handler_data[3];
    680   bfd_boolean multiple_sections_found;
    681   asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
    682 
    683   if (multiple_sections_found)
    684     {
    685       walk_wild_section_general (ptr, file, callback, data);
    686       return;
    687     }
    688 
    689   s1 = find_section (file, sec1, &multiple_sections_found);
    690   if (multiple_sections_found)
    691     {
    692       walk_wild_section_general (ptr, file, callback, data);
    693       return;
    694     }
    695 
    696   for (s = file->the_bfd->sections; s != NULL; s = s->next)
    697     {
    698       if (s == s0)
    699 	walk_wild_consider_section (ptr, file, s, sec0, callback, data);
    700       else
    701 	if (s == s1)
    702 	  walk_wild_consider_section (ptr, file, s, sec1, callback, data);
    703 	else
    704 	  {
    705 	    const char *sname = bfd_get_section_name (file->the_bfd, s);
    706 	    bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
    707 						   sname);
    708 
    709 	    if (!skip)
    710 	      walk_wild_consider_section (ptr, file, s, wildsec2, callback,
    711 					  data);
    712 	    else
    713 	      {
    714 		skip = !match_simple_wild (wildsec3->spec.name, sname);
    715 		if (!skip)
    716 		  walk_wild_consider_section (ptr, file, s, wildsec3,
    717 					      callback, data);
    718 	      }
    719 	  }
    720     }
    721 }
    722 
    723 static void
    724 walk_wild_section (lang_wild_statement_type *ptr,
    725 		   lang_input_statement_type *file,
    726 		   callback_t callback,
    727 		   void *data)
    728 {
    729   if (file->flags.just_syms)
    730     return;
    731 
    732   (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
    733 }
    734 
    735 /* Returns TRUE when name1 is a wildcard spec that might match
    736    something name2 can match.  We're conservative: we return FALSE
    737    only if the prefixes of name1 and name2 are different up to the
    738    first wildcard character.  */
    739 
    740 static bfd_boolean
    741 wild_spec_can_overlap (const char *name1, const char *name2)
    742 {
    743   size_t prefix1_len = strcspn (name1, "?*[");
    744   size_t prefix2_len = strcspn (name2, "?*[");
    745   size_t min_prefix_len;
    746 
    747   /* Note that if there is no wildcard character, then we treat the
    748      terminating 0 as part of the prefix.  Thus ".text" won't match
    749      ".text." or ".text.*", for example.  */
    750   if (name1[prefix1_len] == '\0')
    751     prefix1_len++;
    752   if (name2[prefix2_len] == '\0')
    753     prefix2_len++;
    754 
    755   min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
    756 
    757   return memcmp (name1, name2, min_prefix_len) == 0;
    758 }
    759 
    760 /* Select specialized code to handle various kinds of wildcard
    761    statements.  */
    762 
    763 static void
    764 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
    765 {
    766   int sec_count = 0;
    767   int wild_name_count = 0;
    768   struct wildcard_list *sec;
    769   int signature;
    770   int data_counter;
    771 
    772   ptr->walk_wild_section_handler = walk_wild_section_general;
    773   ptr->handler_data[0] = NULL;
    774   ptr->handler_data[1] = NULL;
    775   ptr->handler_data[2] = NULL;
    776   ptr->handler_data[3] = NULL;
    777   ptr->tree = NULL;
    778 
    779   /* Count how many wildcard_specs there are, and how many of those
    780      actually use wildcards in the name.  Also, bail out if any of the
    781      wildcard names are NULL. (Can this actually happen?
    782      walk_wild_section used to test for it.)  And bail out if any
    783      of the wildcards are more complex than a simple string
    784      ending in a single '*'.  */
    785   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
    786     {
    787       ++sec_count;
    788       if (sec->spec.name == NULL)
    789 	return;
    790       if (wildcardp (sec->spec.name))
    791 	{
    792 	  ++wild_name_count;
    793 	  if (!is_simple_wild (sec->spec.name))
    794 	    return;
    795 	}
    796     }
    797 
    798   /* The zero-spec case would be easy to optimize but it doesn't
    799      happen in practice.  Likewise, more than 4 specs doesn't
    800      happen in practice.  */
    801   if (sec_count == 0 || sec_count > 4)
    802     return;
    803 
    804   /* Check that no two specs can match the same section.  */
    805   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
    806     {
    807       struct wildcard_list *sec2;
    808       for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
    809 	{
    810 	  if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
    811 	    return;
    812 	}
    813     }
    814 
    815   signature = (sec_count << 8) + wild_name_count;
    816   switch (signature)
    817     {
    818     case 0x0100:
    819       ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
    820       break;
    821     case 0x0101:
    822       ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
    823       break;
    824     case 0x0201:
    825       ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
    826       break;
    827     case 0x0302:
    828       ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
    829       break;
    830     case 0x0402:
    831       ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
    832       break;
    833     default:
    834       return;
    835     }
    836 
    837   /* Now fill the data array with pointers to the specs, first the
    838      specs with non-wildcard names, then the specs with wildcard
    839      names.  It's OK to process the specs in different order from the
    840      given order, because we've already determined that no section
    841      will match more than one spec.  */
    842   data_counter = 0;
    843   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
    844     if (!wildcardp (sec->spec.name))
    845       ptr->handler_data[data_counter++] = sec;
    846   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
    847     if (wildcardp (sec->spec.name))
    848       ptr->handler_data[data_counter++] = sec;
    849 }
    850 
    851 /* Handle a wild statement for a single file F.  */
    852 
    853 static void
    854 walk_wild_file (lang_wild_statement_type *s,
    855 		lang_input_statement_type *f,
    856 		callback_t callback,
    857 		void *data)
    858 {
    859   if (f->the_bfd == NULL
    860       || ! bfd_check_format (f->the_bfd, bfd_archive))
    861     walk_wild_section (s, f, callback, data);
    862   else
    863     {
    864       bfd *member;
    865 
    866       /* This is an archive file.  We must map each member of the
    867 	 archive separately.  */
    868       member = bfd_openr_next_archived_file (f->the_bfd, NULL);
    869       while (member != NULL)
    870 	{
    871 	  /* When lookup_name is called, it will call the add_symbols
    872 	     entry point for the archive.  For each element of the
    873 	     archive which is included, BFD will call ldlang_add_file,
    874 	     which will set the usrdata field of the member to the
    875 	     lang_input_statement.  */
    876 	  if (member->usrdata != NULL)
    877 	    {
    878 	      walk_wild_section (s,
    879 				 (lang_input_statement_type *) member->usrdata,
    880 				 callback, data);
    881 	    }
    882 
    883 	  member = bfd_openr_next_archived_file (f->the_bfd, member);
    884 	}
    885     }
    886 }
    887 
    888 static void
    889 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
    890 {
    891   const char *file_spec = s->filename;
    892   char *p;
    893 
    894   if (file_spec == NULL)
    895     {
    896       /* Perform the iteration over all files in the list.  */
    897       LANG_FOR_EACH_INPUT_STATEMENT (f)
    898 	{
    899 	  walk_wild_file (s, f, callback, data);
    900 	}
    901     }
    902   else if ((p = archive_path (file_spec)) != NULL)
    903     {
    904       LANG_FOR_EACH_INPUT_STATEMENT (f)
    905 	{
    906 	  if (input_statement_is_archive_path (file_spec, p, f))
    907 	    walk_wild_file (s, f, callback, data);
    908 	}
    909     }
    910   else if (wildcardp (file_spec))
    911     {
    912       LANG_FOR_EACH_INPUT_STATEMENT (f)
    913 	{
    914 	  if (fnmatch (file_spec, f->filename, 0) == 0)
    915 	    walk_wild_file (s, f, callback, data);
    916 	}
    917     }
    918   else
    919     {
    920       lang_input_statement_type *f;
    921 
    922       /* Perform the iteration over a single file.  */
    923       f = lookup_name (file_spec);
    924       if (f)
    925 	walk_wild_file (s, f, callback, data);
    926     }
    927 }
    928 
    929 /* lang_for_each_statement walks the parse tree and calls the provided
    930    function for each node, except those inside output section statements
    931    with constraint set to -1.  */
    932 
    933 void
    934 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
    935 				lang_statement_union_type *s)
    936 {
    937   for (; s != NULL; s = s->header.next)
    938     {
    939       func (s);
    940 
    941       switch (s->header.type)
    942 	{
    943 	case lang_constructors_statement_enum:
    944 	  lang_for_each_statement_worker (func, constructor_list.head);
    945 	  break;
    946 	case lang_output_section_statement_enum:
    947 	  if (s->output_section_statement.constraint != -1)
    948 	    lang_for_each_statement_worker
    949 	      (func, s->output_section_statement.children.head);
    950 	  break;
    951 	case lang_wild_statement_enum:
    952 	  lang_for_each_statement_worker (func,
    953 					  s->wild_statement.children.head);
    954 	  break;
    955 	case lang_group_statement_enum:
    956 	  lang_for_each_statement_worker (func,
    957 					  s->group_statement.children.head);
    958 	  break;
    959 	case lang_data_statement_enum:
    960 	case lang_reloc_statement_enum:
    961 	case lang_object_symbols_statement_enum:
    962 	case lang_output_statement_enum:
    963 	case lang_target_statement_enum:
    964 	case lang_input_section_enum:
    965 	case lang_input_statement_enum:
    966 	case lang_assignment_statement_enum:
    967 	case lang_padding_statement_enum:
    968 	case lang_address_statement_enum:
    969 	case lang_fill_statement_enum:
    970 	case lang_insert_statement_enum:
    971 	  break;
    972 	default:
    973 	  FAIL ();
    974 	  break;
    975 	}
    976     }
    977 }
    978 
    979 void
    980 lang_for_each_statement (void (*func) (lang_statement_union_type *))
    981 {
    982   lang_for_each_statement_worker (func, statement_list.head);
    983 }
    984 
    985 /*----------------------------------------------------------------------*/
    986 
    987 void
    988 lang_list_init (lang_statement_list_type *list)
    989 {
    990   list->head = NULL;
    991   list->tail = &list->head;
    992 }
    993 
    994 void
    995 push_stat_ptr (lang_statement_list_type *new_ptr)
    996 {
    997   if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0]))
    998     abort ();
    999   *stat_save_ptr++ = stat_ptr;
   1000   stat_ptr = new_ptr;
   1001 }
   1002 
   1003 void
   1004 pop_stat_ptr (void)
   1005 {
   1006   if (stat_save_ptr <= stat_save)
   1007     abort ();
   1008   stat_ptr = *--stat_save_ptr;
   1009 }
   1010 
   1011 /* Build a new statement node for the parse tree.  */
   1012 
   1013 static lang_statement_union_type *
   1014 new_statement (enum statement_enum type,
   1015 	       size_t size,
   1016 	       lang_statement_list_type *list)
   1017 {
   1018   lang_statement_union_type *new_stmt;
   1019 
   1020   new_stmt = (lang_statement_union_type *) stat_alloc (size);
   1021   new_stmt->header.type = type;
   1022   new_stmt->header.next = NULL;
   1023   lang_statement_append (list, new_stmt, &new_stmt->header.next);
   1024   return new_stmt;
   1025 }
   1026 
   1027 /* Build a new input file node for the language.  There are several
   1028    ways in which we treat an input file, eg, we only look at symbols,
   1029    or prefix it with a -l etc.
   1030 
   1031    We can be supplied with requests for input files more than once;
   1032    they may, for example be split over several lines like foo.o(.text)
   1033    foo.o(.data) etc, so when asked for a file we check that we haven't
   1034    got it already so we don't duplicate the bfd.  */
   1035 
   1036 static lang_input_statement_type *
   1037 new_afile (const char *name,
   1038 	   lang_input_file_enum_type file_type,
   1039 	   const char *target,
   1040 	   bfd_boolean add_to_list)
   1041 {
   1042   lang_input_statement_type *p;
   1043 
   1044   lang_has_input_file = TRUE;
   1045 
   1046   if (add_to_list)
   1047     p = (lang_input_statement_type *) new_stat (lang_input_statement, stat_ptr);
   1048   else
   1049     {
   1050       p = (lang_input_statement_type *)
   1051 	  stat_alloc (sizeof (lang_input_statement_type));
   1052       p->header.type = lang_input_statement_enum;
   1053       p->header.next = NULL;
   1054     }
   1055 
   1056   memset (&p->the_bfd, 0,
   1057 	  sizeof (*p) - offsetof (lang_input_statement_type, the_bfd));
   1058   p->target = target;
   1059   p->flags.dynamic = input_flags.dynamic;
   1060   p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic;
   1061   p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular;
   1062   p->flags.whole_archive = input_flags.whole_archive;
   1063   p->flags.sysrooted = input_flags.sysrooted;
   1064 
   1065   switch (file_type)
   1066     {
   1067     case lang_input_file_is_symbols_only_enum:
   1068       p->filename = name;
   1069       p->local_sym_name = name;
   1070       p->flags.real = TRUE;
   1071       p->flags.just_syms = TRUE;
   1072       break;
   1073     case lang_input_file_is_fake_enum:
   1074       p->filename = name;
   1075       p->local_sym_name = name;
   1076       break;
   1077     case lang_input_file_is_l_enum:
   1078       if (name[0] == ':' && name[1] != '\0')
   1079         {
   1080           p->filename = name + 1;
   1081           p->flags.full_name_provided = TRUE;
   1082         }
   1083       else
   1084         p->filename = name;
   1085       p->local_sym_name = concat ("-l", name, (const char *) NULL);
   1086       p->flags.maybe_archive = TRUE;
   1087       p->flags.real = TRUE;
   1088       p->flags.search_dirs = TRUE;
   1089       break;
   1090     case lang_input_file_is_marker_enum:
   1091       p->filename = name;
   1092       p->local_sym_name = name;
   1093       p->flags.search_dirs = TRUE;
   1094       break;
   1095     case lang_input_file_is_search_file_enum:
   1096       p->filename = name;
   1097       p->local_sym_name = name;
   1098       p->flags.real = TRUE;
   1099       p->flags.search_dirs = TRUE;
   1100       break;
   1101     case lang_input_file_is_file_enum:
   1102       p->filename = name;
   1103       p->local_sym_name = name;
   1104       p->flags.real = TRUE;
   1105       break;
   1106     default:
   1107       FAIL ();
   1108     }
   1109 
   1110   lang_statement_append (&input_file_chain,
   1111 			 (lang_statement_union_type *) p,
   1112 			 &p->next_real_file);
   1113   return p;
   1114 }
   1115 
   1116 lang_input_statement_type *
   1117 lang_add_input_file (const char *name,
   1118 		     lang_input_file_enum_type file_type,
   1119 		     const char *target)
   1120 {
   1121   if (name != NULL && *name == '=')
   1122     {
   1123       lang_input_statement_type *ret;
   1124       char *sysrooted_name
   1125 	= concat (ld_sysroot, name + 1, (const char *) NULL);
   1126 
   1127       /* We've now forcibly prepended the sysroot, making the input
   1128 	 file independent of the context.  Therefore, temporarily
   1129 	 force a non-sysrooted context for this statement, so it won't
   1130 	 get the sysroot prepended again when opened.  (N.B. if it's a
   1131 	 script, any child nodes with input files starting with "/"
   1132 	 will be handled as "sysrooted" as they'll be found to be
   1133 	 within the sysroot subdirectory.)  */
   1134       unsigned int outer_sysrooted = input_flags.sysrooted;
   1135       input_flags.sysrooted = 0;
   1136       ret = new_afile (sysrooted_name, file_type, target, TRUE);
   1137       input_flags.sysrooted = outer_sysrooted;
   1138       return ret;
   1139     }
   1140 
   1141   return new_afile (name, file_type, target, TRUE);
   1142 }
   1143 
   1144 struct out_section_hash_entry
   1145 {
   1146   struct bfd_hash_entry root;
   1147   lang_statement_union_type s;
   1148 };
   1149 
   1150 /* The hash table.  */
   1151 
   1152 static struct bfd_hash_table output_section_statement_table;
   1153 
   1154 /* Support routines for the hash table used by lang_output_section_find,
   1155    initialize the table, fill in an entry and remove the table.  */
   1156 
   1157 static struct bfd_hash_entry *
   1158 output_section_statement_newfunc (struct bfd_hash_entry *entry,
   1159 				  struct bfd_hash_table *table,
   1160 				  const char *string)
   1161 {
   1162   lang_output_section_statement_type **nextp;
   1163   struct out_section_hash_entry *ret;
   1164 
   1165   if (entry == NULL)
   1166     {
   1167       entry = (struct bfd_hash_entry *) bfd_hash_allocate (table,
   1168 							   sizeof (*ret));
   1169       if (entry == NULL)
   1170 	return entry;
   1171     }
   1172 
   1173   entry = bfd_hash_newfunc (entry, table, string);
   1174   if (entry == NULL)
   1175     return entry;
   1176 
   1177   ret = (struct out_section_hash_entry *) entry;
   1178   memset (&ret->s, 0, sizeof (ret->s));
   1179   ret->s.header.type = lang_output_section_statement_enum;
   1180   ret->s.output_section_statement.subsection_alignment = -1;
   1181   ret->s.output_section_statement.section_alignment = -1;
   1182   ret->s.output_section_statement.block_value = 1;
   1183   lang_list_init (&ret->s.output_section_statement.children);
   1184   lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
   1185 
   1186   /* For every output section statement added to the list, except the
   1187      first one, lang_output_section_statement.tail points to the "next"
   1188      field of the last element of the list.  */
   1189   if (lang_output_section_statement.head != NULL)
   1190     ret->s.output_section_statement.prev
   1191       = ((lang_output_section_statement_type *)
   1192 	 ((char *) lang_output_section_statement.tail
   1193 	  - offsetof (lang_output_section_statement_type, next)));
   1194 
   1195   /* GCC's strict aliasing rules prevent us from just casting the
   1196      address, so we store the pointer in a variable and cast that
   1197      instead.  */
   1198   nextp = &ret->s.output_section_statement.next;
   1199   lang_statement_append (&lang_output_section_statement,
   1200 			 &ret->s,
   1201 			 (lang_statement_union_type **) nextp);
   1202   return &ret->root;
   1203 }
   1204 
   1205 static void
   1206 output_section_statement_table_init (void)
   1207 {
   1208   if (!bfd_hash_table_init_n (&output_section_statement_table,
   1209 			      output_section_statement_newfunc,
   1210 			      sizeof (struct out_section_hash_entry),
   1211 			      61))
   1212     einfo (_("%P%F: can not create hash table: %E\n"));
   1213 }
   1214 
   1215 static void
   1216 output_section_statement_table_free (void)
   1217 {
   1218   bfd_hash_table_free (&output_section_statement_table);
   1219 }
   1220 
   1221 /* Build enough state so that the parser can build its tree.  */
   1222 
   1223 void
   1224 lang_init (void)
   1225 {
   1226   obstack_begin (&stat_obstack, 1000);
   1227 
   1228   stat_ptr = &statement_list;
   1229 
   1230   output_section_statement_table_init ();
   1231 
   1232   lang_list_init (stat_ptr);
   1233 
   1234   lang_list_init (&input_file_chain);
   1235   lang_list_init (&lang_output_section_statement);
   1236   lang_list_init (&file_chain);
   1237   first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
   1238 				    NULL);
   1239   abs_output_section =
   1240     lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, TRUE);
   1241 
   1242   abs_output_section->bfd_section = bfd_abs_section_ptr;
   1243 
   1244   /* The value "13" is ad-hoc, somewhat related to the expected number of
   1245      assignments in a linker script.  */
   1246   if (!bfd_hash_table_init_n (&lang_definedness_table,
   1247 			      lang_definedness_newfunc,
   1248 			      sizeof (struct lang_definedness_hash_entry),
   1249 			      13))
   1250     einfo (_("%P%F: can not create hash table: %E\n"));
   1251 
   1252   asneeded_list_head = NULL;
   1253   asneeded_list_tail = &asneeded_list_head;
   1254 }
   1255 
   1256 void
   1257 lang_finish (void)
   1258 {
   1259   bfd_hash_table_free (&lang_definedness_table);
   1260   output_section_statement_table_free ();
   1261 }
   1262 
   1263 /*----------------------------------------------------------------------
   1264   A region is an area of memory declared with the
   1265   MEMORY {  name:org=exp, len=exp ... }
   1266   syntax.
   1267 
   1268   We maintain a list of all the regions here.
   1269 
   1270   If no regions are specified in the script, then the default is used
   1271   which is created when looked up to be the entire data space.
   1272 
   1273   If create is true we are creating a region inside a MEMORY block.
   1274   In this case it is probably an error to create a region that has
   1275   already been created.  If we are not inside a MEMORY block it is
   1276   dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
   1277   and so we issue a warning.
   1278 
   1279   Each region has at least one name.  The first name is either
   1280   DEFAULT_MEMORY_REGION or the name given in the MEMORY block.  You can add
   1281   alias names to an existing region within a script with
   1282   REGION_ALIAS (alias, region_name).  Each name corresponds to at most one
   1283   region.  */
   1284 
   1285 static lang_memory_region_type *lang_memory_region_list;
   1286 static lang_memory_region_type **lang_memory_region_list_tail
   1287   = &lang_memory_region_list;
   1288 
   1289 lang_memory_region_type *
   1290 lang_memory_region_lookup (const char *const name, bfd_boolean create)
   1291 {
   1292   lang_memory_region_name *n;
   1293   lang_memory_region_type *r;
   1294   lang_memory_region_type *new_region;
   1295 
   1296   /* NAME is NULL for LMA memspecs if no region was specified.  */
   1297   if (name == NULL)
   1298     return NULL;
   1299 
   1300   for (r = lang_memory_region_list; r != NULL; r = r->next)
   1301     for (n = &r->name_list; n != NULL; n = n->next)
   1302       if (strcmp (n->name, name) == 0)
   1303 	{
   1304 	  if (create)
   1305 	    einfo (_("%P:%S: warning: redeclaration of memory region `%s'\n"),
   1306 		   NULL, name);
   1307 	  return r;
   1308 	}
   1309 
   1310   if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
   1311     einfo (_("%P:%S: warning: memory region `%s' not declared\n"),
   1312 	   NULL, name);
   1313 
   1314   new_region = (lang_memory_region_type *)
   1315       stat_alloc (sizeof (lang_memory_region_type));
   1316 
   1317   new_region->name_list.name = xstrdup (name);
   1318   new_region->name_list.next = NULL;
   1319   new_region->next = NULL;
   1320   new_region->origin = 0;
   1321   new_region->length = ~(bfd_size_type) 0;
   1322   new_region->current = 0;
   1323   new_region->last_os = NULL;
   1324   new_region->flags = 0;
   1325   new_region->not_flags = 0;
   1326   new_region->had_full_message = FALSE;
   1327 
   1328   *lang_memory_region_list_tail = new_region;
   1329   lang_memory_region_list_tail = &new_region->next;
   1330 
   1331   return new_region;
   1332 }
   1333 
   1334 void
   1335 lang_memory_region_alias (const char * alias, const char * region_name)
   1336 {
   1337   lang_memory_region_name * n;
   1338   lang_memory_region_type * r;
   1339   lang_memory_region_type * region;
   1340 
   1341   /* The default region must be unique.  This ensures that it is not necessary
   1342      to iterate through the name list if someone wants the check if a region is
   1343      the default memory region.  */
   1344   if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0
   1345       || strcmp (alias, DEFAULT_MEMORY_REGION) == 0)
   1346     einfo (_("%F%P:%S: error: alias for default memory region\n"), NULL);
   1347 
   1348   /* Look for the target region and check if the alias is not already
   1349      in use.  */
   1350   region = NULL;
   1351   for (r = lang_memory_region_list; r != NULL; r = r->next)
   1352     for (n = &r->name_list; n != NULL; n = n->next)
   1353       {
   1354 	if (region == NULL && strcmp (n->name, region_name) == 0)
   1355 	  region = r;
   1356 	if (strcmp (n->name, alias) == 0)
   1357 	  einfo (_("%F%P:%S: error: redefinition of memory region "
   1358 		   "alias `%s'\n"),
   1359 		 NULL, alias);
   1360       }
   1361 
   1362   /* Check if the target region exists.  */
   1363   if (region == NULL)
   1364     einfo (_("%F%P:%S: error: memory region `%s' "
   1365 	     "for alias `%s' does not exist\n"),
   1366 	   NULL, region_name, alias);
   1367 
   1368   /* Add alias to region name list.  */
   1369   n = (lang_memory_region_name *) stat_alloc (sizeof (lang_memory_region_name));
   1370   n->name = xstrdup (alias);
   1371   n->next = region->name_list.next;
   1372   region->name_list.next = n;
   1373 }
   1374 
   1375 static lang_memory_region_type *
   1376 lang_memory_default (asection * section)
   1377 {
   1378   lang_memory_region_type *p;
   1379 
   1380   flagword sec_flags = section->flags;
   1381 
   1382   /* Override SEC_DATA to mean a writable section.  */
   1383   if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
   1384     sec_flags |= SEC_DATA;
   1385 
   1386   for (p = lang_memory_region_list; p != NULL; p = p->next)
   1387     {
   1388       if ((p->flags & sec_flags) != 0
   1389 	  && (p->not_flags & sec_flags) == 0)
   1390 	{
   1391 	  return p;
   1392 	}
   1393     }
   1394   return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
   1395 }
   1396 
   1397 /* Get the output section statement directly from the userdata.  */
   1398 
   1399 lang_output_section_statement_type *
   1400 lang_output_section_get (const asection *output_section)
   1401 {
   1402   return get_userdata (output_section);
   1403 }
   1404 
   1405 /* Find or create an output_section_statement with the given NAME.
   1406    If CONSTRAINT is non-zero match one with that constraint, otherwise
   1407    match any non-negative constraint.  If CREATE, always make a
   1408    new output_section_statement for SPECIAL CONSTRAINT.  */
   1409 
   1410 lang_output_section_statement_type *
   1411 lang_output_section_statement_lookup (const char *name,
   1412 				      int constraint,
   1413 				      bfd_boolean create)
   1414 {
   1415   struct out_section_hash_entry *entry;
   1416 
   1417   entry = ((struct out_section_hash_entry *)
   1418 	   bfd_hash_lookup (&output_section_statement_table, name,
   1419 			    create, FALSE));
   1420   if (entry == NULL)
   1421     {
   1422       if (create)
   1423 	einfo (_("%P%F: failed creating section `%s': %E\n"), name);
   1424       return NULL;
   1425     }
   1426 
   1427   if (entry->s.output_section_statement.name != NULL)
   1428     {
   1429       /* We have a section of this name, but it might not have the correct
   1430 	 constraint.  */
   1431       struct out_section_hash_entry *last_ent;
   1432 
   1433       name = entry->s.output_section_statement.name;
   1434       if (create && constraint == SPECIAL)
   1435 	/* Not traversing to the end reverses the order of the second
   1436 	   and subsequent SPECIAL sections in the hash table chain,
   1437 	   but that shouldn't matter.  */
   1438 	last_ent = entry;
   1439       else
   1440 	do
   1441 	  {
   1442 	    if (constraint == entry->s.output_section_statement.constraint
   1443 		|| (constraint == 0
   1444 		    && entry->s.output_section_statement.constraint >= 0))
   1445 	      return &entry->s.output_section_statement;
   1446 	    last_ent = entry;
   1447 	    entry = (struct out_section_hash_entry *) entry->root.next;
   1448 	  }
   1449 	while (entry != NULL
   1450 	       && name == entry->s.output_section_statement.name);
   1451 
   1452       if (!create)
   1453 	return NULL;
   1454 
   1455       entry
   1456 	= ((struct out_section_hash_entry *)
   1457 	   output_section_statement_newfunc (NULL,
   1458 					     &output_section_statement_table,
   1459 					     name));
   1460       if (entry == NULL)
   1461 	{
   1462 	  einfo (_("%P%F: failed creating section `%s': %E\n"), name);
   1463 	  return NULL;
   1464 	}
   1465       entry->root = last_ent->root;
   1466       last_ent->root.next = &entry->root;
   1467     }
   1468 
   1469   entry->s.output_section_statement.name = name;
   1470   entry->s.output_section_statement.constraint = constraint;
   1471   return &entry->s.output_section_statement;
   1472 }
   1473 
   1474 /* Find the next output_section_statement with the same name as OS.
   1475    If CONSTRAINT is non-zero, find one with that constraint otherwise
   1476    match any non-negative constraint.  */
   1477 
   1478 lang_output_section_statement_type *
   1479 next_matching_output_section_statement (lang_output_section_statement_type *os,
   1480 					int constraint)
   1481 {
   1482   /* All output_section_statements are actually part of a
   1483      struct out_section_hash_entry.  */
   1484   struct out_section_hash_entry *entry = (struct out_section_hash_entry *)
   1485     ((char *) os
   1486      - offsetof (struct out_section_hash_entry, s.output_section_statement));
   1487   const char *name = os->name;
   1488 
   1489   ASSERT (name == entry->root.string);
   1490   do
   1491     {
   1492       entry = (struct out_section_hash_entry *) entry->root.next;
   1493       if (entry == NULL
   1494 	  || name != entry->s.output_section_statement.name)
   1495 	return NULL;
   1496     }
   1497   while (constraint != entry->s.output_section_statement.constraint
   1498 	 && (constraint != 0
   1499 	     || entry->s.output_section_statement.constraint < 0));
   1500 
   1501   return &entry->s.output_section_statement;
   1502 }
   1503 
   1504 /* A variant of lang_output_section_find used by place_orphan.
   1505    Returns the output statement that should precede a new output
   1506    statement for SEC.  If an exact match is found on certain flags,
   1507    sets *EXACT too.  */
   1508 
   1509 lang_output_section_statement_type *
   1510 lang_output_section_find_by_flags (const asection *sec,
   1511 				   lang_output_section_statement_type **exact,
   1512 				   lang_match_sec_type_func match_type)
   1513 {
   1514   lang_output_section_statement_type *first, *look, *found;
   1515   flagword look_flags, sec_flags, differ;
   1516 
   1517   /* We know the first statement on this list is *ABS*.  May as well
   1518      skip it.  */
   1519   first = &lang_output_section_statement.head->output_section_statement;
   1520   first = first->next;
   1521 
   1522   /* First try for an exact match.  */
   1523   sec_flags = sec->flags;
   1524   found = NULL;
   1525   for (look = first; look; look = look->next)
   1526     {
   1527       look_flags = look->flags;
   1528       if (look->bfd_section != NULL)
   1529 	{
   1530 	  look_flags = look->bfd_section->flags;
   1531 	  if (match_type && !match_type (link_info.output_bfd,
   1532 					 look->bfd_section,
   1533 					 sec->owner, sec))
   1534 	    continue;
   1535 	}
   1536       differ = look_flags ^ sec_flags;
   1537       if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
   1538 		      | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
   1539 	found = look;
   1540     }
   1541   if (found != NULL)
   1542     {
   1543       if (exact != NULL)
   1544 	*exact = found;
   1545       return found;
   1546     }
   1547 
   1548   if ((sec_flags & SEC_CODE) != 0
   1549       && (sec_flags & SEC_ALLOC) != 0)
   1550     {
   1551       /* Try for a rw code section.  */
   1552       for (look = first; look; look = look->next)
   1553 	{
   1554 	  look_flags = look->flags;
   1555 	  if (look->bfd_section != NULL)
   1556 	    {
   1557 	      look_flags = look->bfd_section->flags;
   1558 	      if (match_type && !match_type (link_info.output_bfd,
   1559 					     look->bfd_section,
   1560 					     sec->owner, sec))
   1561 		continue;
   1562 	    }
   1563 	  differ = look_flags ^ sec_flags;
   1564 	  if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
   1565 			  | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
   1566 	    found = look;
   1567 	}
   1568     }
   1569   else if ((sec_flags & SEC_READONLY) != 0
   1570 	   && (sec_flags & SEC_ALLOC) != 0)
   1571     {
   1572       /* .rodata can go after .text, .sdata2 after .rodata.  */
   1573       for (look = first; look; look = look->next)
   1574 	{
   1575 	  look_flags = look->flags;
   1576 	  if (look->bfd_section != NULL)
   1577 	    {
   1578 	      look_flags = look->bfd_section->flags;
   1579 	      if (match_type && !match_type (link_info.output_bfd,
   1580 					     look->bfd_section,
   1581 					     sec->owner, sec))
   1582 		continue;
   1583 	    }
   1584 	  differ = look_flags ^ sec_flags;
   1585 	  if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
   1586 			  | SEC_READONLY | SEC_SMALL_DATA))
   1587 	      || (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
   1588 			      | SEC_READONLY))
   1589 		  && !(look_flags & SEC_SMALL_DATA)))
   1590 	    found = look;
   1591 	}
   1592     }
   1593   else if ((sec_flags & SEC_THREAD_LOCAL) != 0
   1594 	   && (sec_flags & SEC_ALLOC) != 0)
   1595     {
   1596       /* .tdata can go after .data, .tbss after .tdata.  Treat .tbss
   1597 	 as if it were a loaded section, and don't use match_type.  */
   1598       bfd_boolean seen_thread_local = FALSE;
   1599 
   1600       match_type = NULL;
   1601       for (look = first; look; look = look->next)
   1602 	{
   1603 	  look_flags = look->flags;
   1604 	  if (look->bfd_section != NULL)
   1605 	    look_flags = look->bfd_section->flags;
   1606 
   1607 	  differ = look_flags ^ (sec_flags | SEC_LOAD | SEC_HAS_CONTENTS);
   1608 	  if (!(differ & (SEC_THREAD_LOCAL | SEC_ALLOC)))
   1609 	    {
   1610 	      /* .tdata and .tbss must be adjacent and in that order.  */
   1611 	      if (!(look_flags & SEC_LOAD)
   1612 		  && (sec_flags & SEC_LOAD))
   1613 		/* ..so if we're at a .tbss section and we're placing
   1614 		   a .tdata section stop looking and return the
   1615 		   previous section.  */
   1616 		break;
   1617 	      found = look;
   1618 	      seen_thread_local = TRUE;
   1619 	    }
   1620 	  else if (seen_thread_local)
   1621 	    break;
   1622 	  else if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD)))
   1623 	    found = look;
   1624 	}
   1625     }
   1626   else if ((sec_flags & SEC_SMALL_DATA) != 0
   1627 	   && (sec_flags & SEC_ALLOC) != 0)
   1628     {
   1629       /* .sdata goes after .data, .sbss after .sdata.  */
   1630       for (look = first; look; look = look->next)
   1631 	{
   1632 	  look_flags = look->flags;
   1633 	  if (look->bfd_section != NULL)
   1634 	    {
   1635 	      look_flags = look->bfd_section->flags;
   1636 	      if (match_type && !match_type (link_info.output_bfd,
   1637 					     look->bfd_section,
   1638 					     sec->owner, sec))
   1639 		continue;
   1640 	    }
   1641 	  differ = look_flags ^ sec_flags;
   1642 	  if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
   1643 			  | SEC_THREAD_LOCAL))
   1644 	      || ((look_flags & SEC_SMALL_DATA)
   1645 		  && !(sec_flags & SEC_HAS_CONTENTS)))
   1646 	    found = look;
   1647 	}
   1648     }
   1649   else if ((sec_flags & SEC_HAS_CONTENTS) != 0
   1650 	   && (sec_flags & SEC_ALLOC) != 0)
   1651     {
   1652       /* .data goes after .rodata.  */
   1653       for (look = first; look; look = look->next)
   1654 	{
   1655 	  look_flags = look->flags;
   1656 	  if (look->bfd_section != NULL)
   1657 	    {
   1658 	      look_flags = look->bfd_section->flags;
   1659 	      if (match_type && !match_type (link_info.output_bfd,
   1660 					     look->bfd_section,
   1661 					     sec->owner, sec))
   1662 		continue;
   1663 	    }
   1664 	  differ = look_flags ^ sec_flags;
   1665 	  if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
   1666 			  | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
   1667 	    found = look;
   1668 	}
   1669     }
   1670   else if ((sec_flags & SEC_ALLOC) != 0)
   1671     {
   1672       /* .bss goes after any other alloc section.  */
   1673       for (look = first; look; look = look->next)
   1674 	{
   1675 	  look_flags = look->flags;
   1676 	  if (look->bfd_section != NULL)
   1677 	    {
   1678 	      look_flags = look->bfd_section->flags;
   1679 	      if (match_type && !match_type (link_info.output_bfd,
   1680 					     look->bfd_section,
   1681 					     sec->owner, sec))
   1682 		continue;
   1683 	    }
   1684 	  differ = look_flags ^ sec_flags;
   1685 	  if (!(differ & SEC_ALLOC))
   1686 	    found = look;
   1687 	}
   1688     }
   1689   else
   1690     {
   1691       /* non-alloc go last.  */
   1692       for (look = first; look; look = look->next)
   1693 	{
   1694 	  look_flags = look->flags;
   1695 	  if (look->bfd_section != NULL)
   1696 	    look_flags = look->bfd_section->flags;
   1697 	  differ = look_flags ^ sec_flags;
   1698 	  if (!(differ & SEC_DEBUGGING))
   1699 	    found = look;
   1700 	}
   1701       return found;
   1702     }
   1703 
   1704   if (found || !match_type)
   1705     return found;
   1706 
   1707   return lang_output_section_find_by_flags (sec, NULL, NULL);
   1708 }
   1709 
   1710 /* Find the last output section before given output statement.
   1711    Used by place_orphan.  */
   1712 
   1713 static asection *
   1714 output_prev_sec_find (lang_output_section_statement_type *os)
   1715 {
   1716   lang_output_section_statement_type *lookup;
   1717 
   1718   for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
   1719     {
   1720       if (lookup->constraint < 0)
   1721 	continue;
   1722 
   1723       if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
   1724 	return lookup->bfd_section;
   1725     }
   1726 
   1727   return NULL;
   1728 }
   1729 
   1730 /* Look for a suitable place for a new output section statement.  The
   1731    idea is to skip over anything that might be inside a SECTIONS {}
   1732    statement in a script, before we find another output section
   1733    statement.  Assignments to "dot" before an output section statement
   1734    are assumed to belong to it, except in two cases;  The first
   1735    assignment to dot, and assignments before non-alloc sections.
   1736    Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
   1737    similar assignments that set the initial address, or we might
   1738    insert non-alloc note sections among assignments setting end of
   1739    image symbols.  */
   1740 
   1741 static lang_statement_union_type **
   1742 insert_os_after (lang_output_section_statement_type *after)
   1743 {
   1744   lang_statement_union_type **where;
   1745   lang_statement_union_type **assign = NULL;
   1746   bfd_boolean ignore_first;
   1747 
   1748   ignore_first
   1749     = after == &lang_output_section_statement.head->output_section_statement;
   1750 
   1751   for (where = &after->header.next;
   1752        *where != NULL;
   1753        where = &(*where)->header.next)
   1754     {
   1755       switch ((*where)->header.type)
   1756 	{
   1757 	case lang_assignment_statement_enum:
   1758 	  if (assign == NULL)
   1759 	    {
   1760 	      lang_assignment_statement_type *ass;
   1761 
   1762 	      ass = &(*where)->assignment_statement;
   1763 	      if (ass->exp->type.node_class != etree_assert
   1764 		  && ass->exp->assign.dst[0] == '.'
   1765 		  && ass->exp->assign.dst[1] == 0
   1766 		  && !ignore_first)
   1767 		assign = where;
   1768 	    }
   1769 	  ignore_first = FALSE;
   1770 	  continue;
   1771 	case lang_wild_statement_enum:
   1772 	case lang_input_section_enum:
   1773 	case lang_object_symbols_statement_enum:
   1774 	case lang_fill_statement_enum:
   1775 	case lang_data_statement_enum:
   1776 	case lang_reloc_statement_enum:
   1777 	case lang_padding_statement_enum:
   1778 	case lang_constructors_statement_enum:
   1779 	  assign = NULL;
   1780 	  continue;
   1781 	case lang_output_section_statement_enum:
   1782 	  if (assign != NULL)
   1783 	    {
   1784 	      asection *s = (*where)->output_section_statement.bfd_section;
   1785 
   1786 	      if (s == NULL
   1787 		  || s->map_head.s == NULL
   1788 		  || (s->flags & SEC_ALLOC) != 0)
   1789 		where = assign;
   1790 	    }
   1791 	  break;
   1792 	case lang_input_statement_enum:
   1793 	case lang_address_statement_enum:
   1794 	case lang_target_statement_enum:
   1795 	case lang_output_statement_enum:
   1796 	case lang_group_statement_enum:
   1797 	case lang_insert_statement_enum:
   1798 	  continue;
   1799 	}
   1800       break;
   1801     }
   1802 
   1803   return where;
   1804 }
   1805 
   1806 lang_output_section_statement_type *
   1807 lang_insert_orphan (asection *s,
   1808 		    const char *secname,
   1809 		    int constraint,
   1810 		    lang_output_section_statement_type *after,
   1811 		    struct orphan_save *place,
   1812 		    etree_type *address,
   1813 		    lang_statement_list_type *add_child)
   1814 {
   1815   lang_statement_list_type add;
   1816   const char *ps;
   1817   lang_output_section_statement_type *os;
   1818   lang_output_section_statement_type **os_tail;
   1819 
   1820   /* If we have found an appropriate place for the output section
   1821      statements for this orphan, add them to our own private list,
   1822      inserting them later into the global statement list.  */
   1823   if (after != NULL)
   1824     {
   1825       lang_list_init (&add);
   1826       push_stat_ptr (&add);
   1827     }
   1828 
   1829   if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
   1830     address = exp_intop (0);
   1831 
   1832   os_tail = ((lang_output_section_statement_type **)
   1833 	     lang_output_section_statement.tail);
   1834   os = lang_enter_output_section_statement (secname, address, normal_section,
   1835 					    NULL, NULL, NULL, constraint, 0);
   1836 
   1837   ps = NULL;
   1838   if (config.build_constructors && *os_tail == os)
   1839     {
   1840       /* If the name of the section is representable in C, then create
   1841 	 symbols to mark the start and the end of the section.  */
   1842       for (ps = secname; *ps != '\0'; ps++)
   1843 	if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
   1844 	  break;
   1845       if (*ps == '\0')
   1846 	{
   1847 	  char *symname;
   1848 
   1849 	  symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
   1850 	  symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
   1851 	  sprintf (symname + (symname[0] != 0), "__start_%s", secname);
   1852 	  lang_add_assignment (exp_provide (symname,
   1853 					    exp_nameop (NAME, "."),
   1854 					    FALSE));
   1855 	}
   1856     }
   1857 
   1858   if (add_child == NULL)
   1859     add_child = &os->children;
   1860   lang_add_section (add_child, s, NULL, os);
   1861 
   1862   if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0)
   1863     {
   1864       const char *region = (after->region
   1865 			    ? after->region->name_list.name
   1866 			    : DEFAULT_MEMORY_REGION);
   1867       const char *lma_region = (after->lma_region
   1868 				? after->lma_region->name_list.name
   1869 				: NULL);
   1870       lang_leave_output_section_statement (NULL, region, after->phdrs,
   1871 					   lma_region);
   1872     }
   1873   else
   1874     lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL,
   1875 					 NULL);
   1876 
   1877   if (ps != NULL && *ps == '\0')
   1878     {
   1879       char *symname;
   1880 
   1881       symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
   1882       symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
   1883       sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
   1884       lang_add_assignment (exp_provide (symname,
   1885 					exp_nameop (NAME, "."),
   1886 					FALSE));
   1887     }
   1888 
   1889   /* Restore the global list pointer.  */
   1890   if (after != NULL)
   1891     pop_stat_ptr ();
   1892 
   1893   if (after != NULL && os->bfd_section != NULL)
   1894     {
   1895       asection *snew, *as;
   1896 
   1897       snew = os->bfd_section;
   1898 
   1899       /* Shuffle the bfd section list to make the output file look
   1900 	 neater.  This is really only cosmetic.  */
   1901       if (place->section == NULL
   1902 	  && after != (&lang_output_section_statement.head
   1903 		       ->output_section_statement))
   1904 	{
   1905 	  asection *bfd_section = after->bfd_section;
   1906 
   1907 	  /* If the output statement hasn't been used to place any input
   1908 	     sections (and thus doesn't have an output bfd_section),
   1909 	     look for the closest prior output statement having an
   1910 	     output section.  */
   1911 	  if (bfd_section == NULL)
   1912 	    bfd_section = output_prev_sec_find (after);
   1913 
   1914 	  if (bfd_section != NULL && bfd_section != snew)
   1915 	    place->section = &bfd_section->next;
   1916 	}
   1917 
   1918       if (place->section == NULL)
   1919 	place->section = &link_info.output_bfd->sections;
   1920 
   1921       as = *place->section;
   1922 
   1923       if (!as)
   1924 	{
   1925 	  /* Put the section at the end of the list.  */
   1926 
   1927 	  /* Unlink the section.  */
   1928 	  bfd_section_list_remove (link_info.output_bfd, snew);
   1929 
   1930 	  /* Now tack it back on in the right place.  */
   1931 	  bfd_section_list_append (link_info.output_bfd, snew);
   1932 	}
   1933       else if (as != snew && as->prev != snew)
   1934 	{
   1935 	  /* Unlink the section.  */
   1936 	  bfd_section_list_remove (link_info.output_bfd, snew);
   1937 
   1938 	  /* Now tack it back on in the right place.  */
   1939 	  bfd_section_list_insert_before (link_info.output_bfd, as, snew);
   1940 	}
   1941 
   1942       /* Save the end of this list.  Further ophans of this type will
   1943 	 follow the one we've just added.  */
   1944       place->section = &snew->next;
   1945 
   1946       /* The following is non-cosmetic.  We try to put the output
   1947 	 statements in some sort of reasonable order here, because they
   1948 	 determine the final load addresses of the orphan sections.
   1949 	 In addition, placing output statements in the wrong order may
   1950 	 require extra segments.  For instance, given a typical
   1951 	 situation of all read-only sections placed in one segment and
   1952 	 following that a segment containing all the read-write
   1953 	 sections, we wouldn't want to place an orphan read/write
   1954 	 section before or amongst the read-only ones.  */
   1955       if (add.head != NULL)
   1956 	{
   1957 	  lang_output_section_statement_type *newly_added_os;
   1958 
   1959 	  if (place->stmt == NULL)
   1960 	    {
   1961 	      lang_statement_union_type **where = insert_os_after (after);
   1962 
   1963 	      *add.tail = *where;
   1964 	      *where = add.head;
   1965 
   1966 	      place->os_tail = &after->next;
   1967 	    }
   1968 	  else
   1969 	    {
   1970 	      /* Put it after the last orphan statement we added.  */
   1971 	      *add.tail = *place->stmt;
   1972 	      *place->stmt = add.head;
   1973 	    }
   1974 
   1975 	  /* Fix the global list pointer if we happened to tack our
   1976 	     new list at the tail.  */
   1977 	  if (*stat_ptr->tail == add.head)
   1978 	    stat_ptr->tail = add.tail;
   1979 
   1980 	  /* Save the end of this list.  */
   1981 	  place->stmt = add.tail;
   1982 
   1983 	  /* Do the same for the list of output section statements.  */
   1984 	  newly_added_os = *os_tail;
   1985 	  *os_tail = NULL;
   1986 	  newly_added_os->prev = (lang_output_section_statement_type *)
   1987 	    ((char *) place->os_tail
   1988 	     - offsetof (lang_output_section_statement_type, next));
   1989 	  newly_added_os->next = *place->os_tail;
   1990 	  if (newly_added_os->next != NULL)
   1991 	    newly_added_os->next->prev = newly_added_os;
   1992 	  *place->os_tail = newly_added_os;
   1993 	  place->os_tail = &newly_added_os->next;
   1994 
   1995 	  /* Fixing the global list pointer here is a little different.
   1996 	     We added to the list in lang_enter_output_section_statement,
   1997 	     trimmed off the new output_section_statment above when
   1998 	     assigning *os_tail = NULL, but possibly added it back in
   1999 	     the same place when assigning *place->os_tail.  */
   2000 	  if (*os_tail == NULL)
   2001 	    lang_output_section_statement.tail
   2002 	      = (lang_statement_union_type **) os_tail;
   2003 	}
   2004     }
   2005   return os;
   2006 }
   2007 
   2008 static void
   2009 lang_print_asneeded (void)
   2010 {
   2011   struct asneeded_minfo *m;
   2012   char buf[100];
   2013 
   2014   if (asneeded_list_head == NULL)
   2015     return;
   2016 
   2017   sprintf (buf, _("\nAs-needed library included "
   2018 		  "to satisfy reference by file (symbol)\n\n"));
   2019   minfo ("%s", buf);
   2020 
   2021   for (m = asneeded_list_head; m != NULL; m = m->next)
   2022     {
   2023       size_t len;
   2024 
   2025       minfo ("%s", m->soname);
   2026       len = strlen (m->soname);
   2027 
   2028       if (len >= 29)
   2029 	{
   2030 	  print_nl ();
   2031 	  len = 0;
   2032 	}
   2033       while (len < 30)
   2034 	{
   2035 	  print_space ();
   2036 	  ++len;
   2037 	}
   2038 
   2039       if (m->ref != NULL)
   2040 	minfo ("%B ", m->ref);
   2041       minfo ("(%T)\n", m->name);
   2042     }
   2043 }
   2044 
   2045 static void
   2046 lang_map_flags (flagword flag)
   2047 {
   2048   if (flag & SEC_ALLOC)
   2049     minfo ("a");
   2050 
   2051   if (flag & SEC_CODE)
   2052     minfo ("x");
   2053 
   2054   if (flag & SEC_READONLY)
   2055     minfo ("r");
   2056 
   2057   if (flag & SEC_DATA)
   2058     minfo ("w");
   2059 
   2060   if (flag & SEC_LOAD)
   2061     minfo ("l");
   2062 }
   2063 
   2064 void
   2065 lang_map (void)
   2066 {
   2067   lang_memory_region_type *m;
   2068   bfd_boolean dis_header_printed = FALSE;
   2069 
   2070   LANG_FOR_EACH_INPUT_STATEMENT (file)
   2071     {
   2072       asection *s;
   2073 
   2074       if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
   2075 	  || file->flags.just_syms)
   2076 	continue;
   2077 
   2078       for (s = file->the_bfd->sections; s != NULL; s = s->next)
   2079 	if ((s->output_section == NULL
   2080 	     || s->output_section->owner != link_info.output_bfd)
   2081 	    && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
   2082 	  {
   2083 	    if (! dis_header_printed)
   2084 	      {
   2085 		fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
   2086 		dis_header_printed = TRUE;
   2087 	      }
   2088 
   2089 	    print_input_section (s, TRUE);
   2090 	  }
   2091     }
   2092 
   2093   minfo (_("\nMemory Configuration\n\n"));
   2094   fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
   2095 	   _("Name"), _("Origin"), _("Length"), _("Attributes"));
   2096 
   2097   for (m = lang_memory_region_list; m != NULL; m = m->next)
   2098     {
   2099       char buf[100];
   2100       int len;
   2101 
   2102       fprintf (config.map_file, "%-16s ", m->name_list.name);
   2103 
   2104       sprintf_vma (buf, m->origin);
   2105       minfo ("0x%s ", buf);
   2106       len = strlen (buf);
   2107       while (len < 16)
   2108 	{
   2109 	  print_space ();
   2110 	  ++len;
   2111 	}
   2112 
   2113       minfo ("0x%V", m->length);
   2114       if (m->flags || m->not_flags)
   2115 	{
   2116 #ifndef BFD64
   2117 	  minfo ("        ");
   2118 #endif
   2119 	  if (m->flags)
   2120 	    {
   2121 	      print_space ();
   2122 	      lang_map_flags (m->flags);
   2123 	    }
   2124 
   2125 	  if (m->not_flags)
   2126 	    {
   2127 	      minfo (" !");
   2128 	      lang_map_flags (m->not_flags);
   2129 	    }
   2130 	}
   2131 
   2132       print_nl ();
   2133     }
   2134 
   2135   fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
   2136 
   2137   if (! link_info.reduce_memory_overheads)
   2138     {
   2139       obstack_begin (&map_obstack, 1000);
   2140       bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
   2141     }
   2142   lang_statement_iteration++;
   2143   print_statements ();
   2144 
   2145   ldemul_extra_map_file_text (link_info.output_bfd, &link_info, config.map_file);
   2146 }
   2147 
   2148 static bfd_boolean
   2149 sort_def_symbol (struct bfd_link_hash_entry *hash_entry,
   2150 		 void *info ATTRIBUTE_UNUSED)
   2151 {
   2152   if ((hash_entry->type == bfd_link_hash_defined
   2153        || hash_entry->type == bfd_link_hash_defweak)
   2154       && hash_entry->u.def.section->owner != link_info.output_bfd
   2155       && hash_entry->u.def.section->owner != NULL)
   2156     {
   2157       input_section_userdata_type *ud;
   2158       struct map_symbol_def *def;
   2159 
   2160       ud = ((input_section_userdata_type *)
   2161 	    get_userdata (hash_entry->u.def.section));
   2162       if (!ud)
   2163 	{
   2164 	  ud = (input_section_userdata_type *) stat_alloc (sizeof (*ud));
   2165 	  get_userdata (hash_entry->u.def.section) = ud;
   2166 	  ud->map_symbol_def_tail = &ud->map_symbol_def_head;
   2167 	  ud->map_symbol_def_count = 0;
   2168 	}
   2169       else if (!ud->map_symbol_def_tail)
   2170 	ud->map_symbol_def_tail = &ud->map_symbol_def_head;
   2171 
   2172       def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def);
   2173       def->entry = hash_entry;
   2174       *(ud->map_symbol_def_tail) = def;
   2175       ud->map_symbol_def_tail = &def->next;
   2176       ud->map_symbol_def_count++;
   2177     }
   2178   return TRUE;
   2179 }
   2180 
   2181 /* Initialize an output section.  */
   2182 
   2183 static void
   2184 init_os (lang_output_section_statement_type *s, flagword flags)
   2185 {
   2186   if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
   2187     einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
   2188 
   2189   if (s->constraint != SPECIAL)
   2190     s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name);
   2191   if (s->bfd_section == NULL)
   2192     s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd,
   2193 							 s->name, flags);
   2194   if (s->bfd_section == NULL)
   2195     {
   2196       einfo (_("%P%F: output format %s cannot represent section called %s\n"),
   2197 	     link_info.output_bfd->xvec->name, s->name);
   2198     }
   2199   s->bfd_section->output_section = s->bfd_section;
   2200   s->bfd_section->output_offset = 0;
   2201 
   2202   /* Set the userdata of the output section to the output section
   2203      statement to avoid lookup.  */
   2204   get_userdata (s->bfd_section) = s;
   2205 
   2206   /* If there is a base address, make sure that any sections it might
   2207      mention are initialized.  */
   2208   if (s->addr_tree != NULL)
   2209     exp_init_os (s->addr_tree);
   2210 
   2211   if (s->load_base != NULL)
   2212     exp_init_os (s->load_base);
   2213 
   2214   /* If supplied an alignment, set it.  */
   2215   if (s->section_alignment != -1)
   2216     s->bfd_section->alignment_power = s->section_alignment;
   2217 }
   2218 
   2219 /* Make sure that all output sections mentioned in an expression are
   2220    initialized.  */
   2221 
   2222 static void
   2223 exp_init_os (etree_type *exp)
   2224 {
   2225   switch (exp->type.node_class)
   2226     {
   2227     case etree_assign:
   2228     case etree_provide:
   2229       exp_init_os (exp->assign.src);
   2230       break;
   2231 
   2232     case etree_binary:
   2233       exp_init_os (exp->binary.lhs);
   2234       exp_init_os (exp->binary.rhs);
   2235       break;
   2236 
   2237     case etree_trinary:
   2238       exp_init_os (exp->trinary.cond);
   2239       exp_init_os (exp->trinary.lhs);
   2240       exp_init_os (exp->trinary.rhs);
   2241       break;
   2242 
   2243     case etree_assert:
   2244       exp_init_os (exp->assert_s.child);
   2245       break;
   2246 
   2247     case etree_unary:
   2248       exp_init_os (exp->unary.child);
   2249       break;
   2250 
   2251     case etree_name:
   2252       switch (exp->type.node_code)
   2253 	{
   2254 	case ADDR:
   2255 	case LOADADDR:
   2256 	case SIZEOF:
   2257 	  {
   2258 	    lang_output_section_statement_type *os;
   2259 
   2260 	    os = lang_output_section_find (exp->name.name);
   2261 	    if (os != NULL && os->bfd_section == NULL)
   2262 	      init_os (os, 0);
   2263 	  }
   2264 	}
   2265       break;
   2266 
   2267     default:
   2268       break;
   2269     }
   2270 }
   2271 
   2272 static void
   2274 section_already_linked (bfd *abfd, asection *sec, void *data)
   2275 {
   2276   lang_input_statement_type *entry = (lang_input_statement_type *) data;
   2277 
   2278   /* If we are only reading symbols from this object, then we want to
   2279      discard all sections.  */
   2280   if (entry->flags.just_syms)
   2281     {
   2282       bfd_link_just_syms (abfd, sec, &link_info);
   2283       return;
   2284     }
   2285 
   2286   if (!(abfd->flags & DYNAMIC))
   2287     bfd_section_already_linked (abfd, sec, &link_info);
   2288 }
   2289 
   2290 /* The wild routines.
   2292 
   2293    These expand statements like *(.text) and foo.o to a list of
   2294    explicit actions, like foo.o(.text), bar.o(.text) and
   2295    foo.o(.text, .data).  */
   2296 
   2297 /* Add SECTION to the output section OUTPUT.  Do this by creating a
   2298    lang_input_section statement which is placed at PTR.  */
   2299 
   2300 void
   2301 lang_add_section (lang_statement_list_type *ptr,
   2302 		  asection *section,
   2303 		  struct flag_info *sflag_info,
   2304 		  lang_output_section_statement_type *output)
   2305 {
   2306   flagword flags = section->flags;
   2307 
   2308   bfd_boolean discard;
   2309   lang_input_section_type *new_section;
   2310   bfd *abfd = link_info.output_bfd;
   2311 
   2312   /* Discard sections marked with SEC_EXCLUDE.  */
   2313   discard = (flags & SEC_EXCLUDE) != 0;
   2314 
   2315   /* Discard input sections which are assigned to a section named
   2316      DISCARD_SECTION_NAME.  */
   2317   if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
   2318     discard = TRUE;
   2319 
   2320   /* Discard debugging sections if we are stripping debugging
   2321      information.  */
   2322   if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
   2323       && (flags & SEC_DEBUGGING) != 0)
   2324     discard = TRUE;
   2325 
   2326   if (discard)
   2327     {
   2328       if (section->output_section == NULL)
   2329 	{
   2330 	  /* This prevents future calls from assigning this section.  */
   2331 	  section->output_section = bfd_abs_section_ptr;
   2332 	}
   2333       return;
   2334     }
   2335 
   2336   if (sflag_info)
   2337     {
   2338       bfd_boolean keep;
   2339 
   2340       keep = bfd_lookup_section_flags (&link_info, sflag_info, section);
   2341       if (!keep)
   2342 	return;
   2343     }
   2344 
   2345   if (section->output_section != NULL)
   2346     return;
   2347 
   2348   /* We don't copy the SEC_NEVER_LOAD flag from an input section
   2349      to an output section, because we want to be able to include a
   2350      SEC_NEVER_LOAD section in the middle of an otherwise loaded
   2351      section (I don't know why we want to do this, but we do).
   2352      build_link_order in ldwrite.c handles this case by turning
   2353      the embedded SEC_NEVER_LOAD section into a fill.  */
   2354   flags &= ~ SEC_NEVER_LOAD;
   2355 
   2356   /* If final link, don't copy the SEC_LINK_ONCE flags, they've
   2357      already been processed.  One reason to do this is that on pe
   2358      format targets, .text$foo sections go into .text and it's odd
   2359      to see .text with SEC_LINK_ONCE set.  */
   2360 
   2361   if (!link_info.relocatable)
   2362     flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC);
   2363 
   2364   switch (output->sectype)
   2365     {
   2366     case normal_section:
   2367     case overlay_section:
   2368       break;
   2369     case noalloc_section:
   2370       flags &= ~SEC_ALLOC;
   2371       break;
   2372     case noload_section:
   2373       flags &= ~SEC_LOAD;
   2374       flags |= SEC_NEVER_LOAD;
   2375       /* Unfortunately GNU ld has managed to evolve two different
   2376 	 meanings to NOLOAD in scripts.  ELF gets a .bss style noload,
   2377 	 alloc, no contents section.  All others get a noload, noalloc
   2378 	 section.  */
   2379       if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
   2380 	flags &= ~SEC_HAS_CONTENTS;
   2381       else
   2382 	flags &= ~SEC_ALLOC;
   2383       break;
   2384     }
   2385 
   2386   if (output->bfd_section == NULL)
   2387     init_os (output, flags);
   2388 
   2389   /* If SEC_READONLY is not set in the input section, then clear
   2390      it from the output section.  */
   2391   output->bfd_section->flags &= flags | ~SEC_READONLY;
   2392 
   2393   if (output->bfd_section->linker_has_input)
   2394     {
   2395       /* Only set SEC_READONLY flag on the first input section.  */
   2396       flags &= ~ SEC_READONLY;
   2397 
   2398       /* Keep SEC_MERGE and SEC_STRINGS only if they are the same.  */
   2399       if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
   2400 	  != (flags & (SEC_MERGE | SEC_STRINGS))
   2401 	  || ((flags & SEC_MERGE) != 0
   2402 	      && output->bfd_section->entsize != section->entsize))
   2403 	{
   2404 	  output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
   2405 	  flags &= ~ (SEC_MERGE | SEC_STRINGS);
   2406 	}
   2407     }
   2408   output->bfd_section->flags |= flags;
   2409 
   2410   if (!output->bfd_section->linker_has_input)
   2411     {
   2412       output->bfd_section->linker_has_input = 1;
   2413       /* This must happen after flags have been updated.  The output
   2414 	 section may have been created before we saw its first input
   2415 	 section, eg. for a data statement.  */
   2416       bfd_init_private_section_data (section->owner, section,
   2417 				     link_info.output_bfd,
   2418 				     output->bfd_section,
   2419 				     &link_info);
   2420       if ((flags & SEC_MERGE) != 0)
   2421 	output->bfd_section->entsize = section->entsize;
   2422     }
   2423 
   2424   if ((flags & SEC_TIC54X_BLOCK) != 0
   2425       && bfd_get_arch (section->owner) == bfd_arch_tic54x)
   2426     {
   2427       /* FIXME: This value should really be obtained from the bfd...  */
   2428       output->block_value = 128;
   2429     }
   2430 
   2431   if (section->alignment_power > output->bfd_section->alignment_power)
   2432     output->bfd_section->alignment_power = section->alignment_power;
   2433 
   2434   section->output_section = output->bfd_section;
   2435 
   2436   if (!map_head_is_link_order)
   2437     {
   2438       asection *s = output->bfd_section->map_tail.s;
   2439       output->bfd_section->map_tail.s = section;
   2440       section->map_head.s = NULL;
   2441       section->map_tail.s = s;
   2442       if (s != NULL)
   2443 	s->map_head.s = section;
   2444       else
   2445 	output->bfd_section->map_head.s = section;
   2446     }
   2447 
   2448   /* Add a section reference to the list.  */
   2449   new_section = new_stat (lang_input_section, ptr);
   2450   new_section->section = section;
   2451 }
   2452 
   2453 /* Handle wildcard sorting.  This returns the lang_input_section which
   2454    should follow the one we are going to create for SECTION and FILE,
   2455    based on the sorting requirements of WILD.  It returns NULL if the
   2456    new section should just go at the end of the current list.  */
   2457 
   2458 static lang_statement_union_type *
   2459 wild_sort (lang_wild_statement_type *wild,
   2460 	   struct wildcard_list *sec,
   2461 	   lang_input_statement_type *file,
   2462 	   asection *section)
   2463 {
   2464   lang_statement_union_type *l;
   2465 
   2466   if (!wild->filenames_sorted
   2467       && (sec == NULL || sec->spec.sorted == none))
   2468     return NULL;
   2469 
   2470   for (l = wild->children.head; l != NULL; l = l->header.next)
   2471     {
   2472       lang_input_section_type *ls;
   2473 
   2474       if (l->header.type != lang_input_section_enum)
   2475 	continue;
   2476       ls = &l->input_section;
   2477 
   2478       /* Sorting by filename takes precedence over sorting by section
   2479 	 name.  */
   2480 
   2481       if (wild->filenames_sorted)
   2482 	{
   2483 	  const char *fn, *ln;
   2484 	  bfd_boolean fa, la;
   2485 	  int i;
   2486 
   2487 	  /* The PE support for the .idata section as generated by
   2488 	     dlltool assumes that files will be sorted by the name of
   2489 	     the archive and then the name of the file within the
   2490 	     archive.  */
   2491 
   2492 	  if (file->the_bfd != NULL
   2493 	      && bfd_my_archive (file->the_bfd) != NULL)
   2494 	    {
   2495 	      fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
   2496 	      fa = TRUE;
   2497 	    }
   2498 	  else
   2499 	    {
   2500 	      fn = file->filename;
   2501 	      fa = FALSE;
   2502 	    }
   2503 
   2504 	  if (bfd_my_archive (ls->section->owner) != NULL)
   2505 	    {
   2506 	      ln = bfd_get_filename (bfd_my_archive (ls->section->owner));
   2507 	      la = TRUE;
   2508 	    }
   2509 	  else
   2510 	    {
   2511 	      ln = ls->section->owner->filename;
   2512 	      la = FALSE;
   2513 	    }
   2514 
   2515 	  i = filename_cmp (fn, ln);
   2516 	  if (i > 0)
   2517 	    continue;
   2518 	  else if (i < 0)
   2519 	    break;
   2520 
   2521 	  if (fa || la)
   2522 	    {
   2523 	      if (fa)
   2524 		fn = file->filename;
   2525 	      if (la)
   2526 		ln = ls->section->owner->filename;
   2527 
   2528 	      i = filename_cmp (fn, ln);
   2529 	      if (i > 0)
   2530 		continue;
   2531 	      else if (i < 0)
   2532 		break;
   2533 	    }
   2534 	}
   2535 
   2536       /* Here either the files are not sorted by name, or we are
   2537 	 looking at the sections for this file.  */
   2538 
   2539       if (sec != NULL
   2540 	  && sec->spec.sorted != none
   2541 	  && sec->spec.sorted != by_none)
   2542 	if (compare_section (sec->spec.sorted, section, ls->section) < 0)
   2543 	  break;
   2544     }
   2545 
   2546   return l;
   2547 }
   2548 
   2549 /* Expand a wild statement for a particular FILE.  SECTION may be
   2550    NULL, in which case it is a wild card.  */
   2551 
   2552 static void
   2553 output_section_callback (lang_wild_statement_type *ptr,
   2554 			 struct wildcard_list *sec,
   2555 			 asection *section,
   2556 			 struct flag_info *sflag_info,
   2557 			 lang_input_statement_type *file,
   2558 			 void *output)
   2559 {
   2560   lang_statement_union_type *before;
   2561   lang_output_section_statement_type *os;
   2562 
   2563   os = (lang_output_section_statement_type *) output;
   2564 
   2565   /* Exclude sections that match UNIQUE_SECTION_LIST.  */
   2566   if (unique_section_p (section, os))
   2567     return;
   2568 
   2569   before = wild_sort (ptr, sec, file, section);
   2570 
   2571   /* Here BEFORE points to the lang_input_section which
   2572      should follow the one we are about to add.  If BEFORE
   2573      is NULL, then the section should just go at the end
   2574      of the current list.  */
   2575 
   2576   if (before == NULL)
   2577     lang_add_section (&ptr->children, section, sflag_info, os);
   2578   else
   2579     {
   2580       lang_statement_list_type list;
   2581       lang_statement_union_type **pp;
   2582 
   2583       lang_list_init (&list);
   2584       lang_add_section (&list, section, sflag_info, os);
   2585 
   2586       /* If we are discarding the section, LIST.HEAD will
   2587 	 be NULL.  */
   2588       if (list.head != NULL)
   2589 	{
   2590 	  ASSERT (list.head->header.next == NULL);
   2591 
   2592 	  for (pp = &ptr->children.head;
   2593 	       *pp != before;
   2594 	       pp = &(*pp)->header.next)
   2595 	    ASSERT (*pp != NULL);
   2596 
   2597 	  list.head->header.next = *pp;
   2598 	  *pp = list.head;
   2599 	}
   2600     }
   2601 }
   2602 
   2603 /* Check if all sections in a wild statement for a particular FILE
   2604    are readonly.  */
   2605 
   2606 static void
   2607 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
   2608 			struct wildcard_list *sec ATTRIBUTE_UNUSED,
   2609 			asection *section,
   2610 			struct flag_info *sflag_info ATTRIBUTE_UNUSED,
   2611 			lang_input_statement_type *file ATTRIBUTE_UNUSED,
   2612 			void *output)
   2613 {
   2614   lang_output_section_statement_type *os;
   2615 
   2616   os = (lang_output_section_statement_type *) output;
   2617 
   2618   /* Exclude sections that match UNIQUE_SECTION_LIST.  */
   2619   if (unique_section_p (section, os))
   2620     return;
   2621 
   2622   if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
   2623     os->all_input_readonly = FALSE;
   2624 }
   2625 
   2626 /* This is passed a file name which must have been seen already and
   2627    added to the statement tree.  We will see if it has been opened
   2628    already and had its symbols read.  If not then we'll read it.  */
   2629 
   2630 static lang_input_statement_type *
   2631 lookup_name (const char *name)
   2632 {
   2633   lang_input_statement_type *search;
   2634 
   2635   for (search = (lang_input_statement_type *) input_file_chain.head;
   2636        search != NULL;
   2637        search = (lang_input_statement_type *) search->next_real_file)
   2638     {
   2639       /* Use the local_sym_name as the name of the file that has
   2640 	 already been loaded as filename might have been transformed
   2641 	 via the search directory lookup mechanism.  */
   2642       const char *filename = search->local_sym_name;
   2643 
   2644       if (filename != NULL
   2645 	  && filename_cmp (filename, name) == 0)
   2646 	break;
   2647     }
   2648 
   2649   if (search == NULL)
   2650     search = new_afile (name, lang_input_file_is_search_file_enum,
   2651 			default_target, FALSE);
   2652 
   2653   /* If we have already added this file, or this file is not real
   2654      don't add this file.  */
   2655   if (search->flags.loaded || !search->flags.real)
   2656     return search;
   2657 
   2658   if (! load_symbols (search, NULL))
   2659     return NULL;
   2660 
   2661   return search;
   2662 }
   2663 
   2664 /* Save LIST as a list of libraries whose symbols should not be exported.  */
   2665 
   2666 struct excluded_lib
   2667 {
   2668   char *name;
   2669   struct excluded_lib *next;
   2670 };
   2671 static struct excluded_lib *excluded_libs;
   2672 
   2673 void
   2674 add_excluded_libs (const char *list)
   2675 {
   2676   const char *p = list, *end;
   2677 
   2678   while (*p != '\0')
   2679     {
   2680       struct excluded_lib *entry;
   2681       end = strpbrk (p, ",:");
   2682       if (end == NULL)
   2683 	end = p + strlen (p);
   2684       entry = (struct excluded_lib *) xmalloc (sizeof (*entry));
   2685       entry->next = excluded_libs;
   2686       entry->name = (char *) xmalloc (end - p + 1);
   2687       memcpy (entry->name, p, end - p);
   2688       entry->name[end - p] = '\0';
   2689       excluded_libs = entry;
   2690       if (*end == '\0')
   2691 	break;
   2692       p = end + 1;
   2693     }
   2694 }
   2695 
   2696 static void
   2697 check_excluded_libs (bfd *abfd)
   2698 {
   2699   struct excluded_lib *lib = excluded_libs;
   2700 
   2701   while (lib)
   2702     {
   2703       int len = strlen (lib->name);
   2704       const char *filename = lbasename (abfd->filename);
   2705 
   2706       if (strcmp (lib->name, "ALL") == 0)
   2707 	{
   2708 	  abfd->no_export = TRUE;
   2709 	  return;
   2710 	}
   2711 
   2712       if (filename_ncmp (lib->name, filename, len) == 0
   2713 	  && (filename[len] == '\0'
   2714 	      || (filename[len] == '.' && filename[len + 1] == 'a'
   2715 		  && filename[len + 2] == '\0')))
   2716 	{
   2717 	  abfd->no_export = TRUE;
   2718 	  return;
   2719 	}
   2720 
   2721       lib = lib->next;
   2722     }
   2723 }
   2724 
   2725 /* Get the symbols for an input file.  */
   2726 
   2727 bfd_boolean
   2728 load_symbols (lang_input_statement_type *entry,
   2729 	      lang_statement_list_type *place)
   2730 {
   2731   char **matching;
   2732 
   2733   if (entry->flags.loaded)
   2734     return TRUE;
   2735 
   2736   ldfile_open_file (entry);
   2737 
   2738   /* Do not process further if the file was missing.  */
   2739   if (entry->flags.missing_file)
   2740     return TRUE;
   2741 
   2742   if (! bfd_check_format (entry->the_bfd, bfd_archive)
   2743       && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
   2744     {
   2745       bfd_error_type err;
   2746       struct lang_input_statement_flags save_flags;
   2747       extern FILE *yyin;
   2748 
   2749       err = bfd_get_error ();
   2750 
   2751       /* See if the emulation has some special knowledge.  */
   2752       if (ldemul_unrecognized_file (entry))
   2753 	return TRUE;
   2754 
   2755       if (err == bfd_error_file_ambiguously_recognized)
   2756 	{
   2757 	  char **p;
   2758 
   2759 	  einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
   2760 	  einfo (_("%B: matching formats:"), entry->the_bfd);
   2761 	  for (p = matching; *p != NULL; p++)
   2762 	    einfo (" %s", *p);
   2763 	  einfo ("%F\n");
   2764 	}
   2765       else if (err != bfd_error_file_not_recognized
   2766 	       || place == NULL)
   2767 	einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
   2768 
   2769       bfd_close (entry->the_bfd);
   2770       entry->the_bfd = NULL;
   2771 
   2772       /* Try to interpret the file as a linker script.  */
   2773       save_flags = input_flags;
   2774       ldfile_open_command_file (entry->filename);
   2775 
   2776       push_stat_ptr (place);
   2777       input_flags.add_DT_NEEDED_for_regular
   2778 	= entry->flags.add_DT_NEEDED_for_regular;
   2779       input_flags.add_DT_NEEDED_for_dynamic
   2780 	= entry->flags.add_DT_NEEDED_for_dynamic;
   2781       input_flags.whole_archive = entry->flags.whole_archive;
   2782       input_flags.dynamic = entry->flags.dynamic;
   2783 
   2784       ldfile_assumed_script = TRUE;
   2785       parser_input = input_script;
   2786       yyparse ();
   2787       ldfile_assumed_script = FALSE;
   2788 
   2789       /* missing_file is sticky.  sysrooted will already have been
   2790 	 restored when seeing EOF in yyparse, but no harm to restore
   2791 	 again.  */
   2792       save_flags.missing_file |= input_flags.missing_file;
   2793       input_flags = save_flags;
   2794       pop_stat_ptr ();
   2795       fclose (yyin);
   2796       yyin = NULL;
   2797       entry->flags.loaded = TRUE;
   2798 
   2799       return TRUE;
   2800     }
   2801 
   2802   if (ldemul_recognized_file (entry))
   2803     return TRUE;
   2804 
   2805   /* We don't call ldlang_add_file for an archive.  Instead, the
   2806      add_symbols entry point will call ldlang_add_file, via the
   2807      add_archive_element callback, for each element of the archive
   2808      which is used.  */
   2809   switch (bfd_get_format (entry->the_bfd))
   2810     {
   2811     default:
   2812       break;
   2813 
   2814     case bfd_object:
   2815       if (!entry->flags.reload)
   2816 	ldlang_add_file (entry);
   2817       if (trace_files || verbose)
   2818 	info_msg ("%I\n", entry);
   2819       break;
   2820 
   2821     case bfd_archive:
   2822       check_excluded_libs (entry->the_bfd);
   2823 
   2824       if (entry->flags.whole_archive)
   2825 	{
   2826 	  bfd *member = NULL;
   2827 	  bfd_boolean loaded = TRUE;
   2828 
   2829 	  for (;;)
   2830 	    {
   2831 	      bfd *subsbfd;
   2832 	      member = bfd_openr_next_archived_file (entry->the_bfd, member);
   2833 
   2834 	      if (member == NULL)
   2835 		break;
   2836 
   2837 	      if (! bfd_check_format (member, bfd_object))
   2838 		{
   2839 		  einfo (_("%F%B: member %B in archive is not an object\n"),
   2840 			 entry->the_bfd, member);
   2841 		  loaded = FALSE;
   2842 		}
   2843 
   2844 	      subsbfd = member;
   2845 	      if (!(*link_info.callbacks
   2846 		    ->add_archive_element) (&link_info, member,
   2847 					    "--whole-archive", &subsbfd))
   2848 		abort ();
   2849 
   2850 	      /* Potentially, the add_archive_element hook may have set a
   2851 		 substitute BFD for us.  */
   2852 	      if (!bfd_link_add_symbols (subsbfd, &link_info))
   2853 		{
   2854 		  einfo (_("%F%B: error adding symbols: %E\n"), member);
   2855 		  loaded = FALSE;
   2856 		}
   2857 	    }
   2858 
   2859 	  entry->flags.loaded = loaded;
   2860 	  return loaded;
   2861 	}
   2862       break;
   2863     }
   2864 
   2865   if (bfd_link_add_symbols (entry->the_bfd, &link_info))
   2866     entry->flags.loaded = TRUE;
   2867   else
   2868     einfo (_("%F%B: error adding symbols: %E\n"), entry->the_bfd);
   2869 
   2870   return entry->flags.loaded;
   2871 }
   2872 
   2873 /* Handle a wild statement.  S->FILENAME or S->SECTION_LIST or both
   2874    may be NULL, indicating that it is a wildcard.  Separate
   2875    lang_input_section statements are created for each part of the
   2876    expansion; they are added after the wild statement S.  OUTPUT is
   2877    the output section.  */
   2878 
   2879 static void
   2880 wild (lang_wild_statement_type *s,
   2881       const char *target ATTRIBUTE_UNUSED,
   2882       lang_output_section_statement_type *output)
   2883 {
   2884   struct wildcard_list *sec;
   2885 
   2886   if (s->handler_data[0]
   2887       && s->handler_data[0]->spec.sorted == by_name
   2888       && !s->filenames_sorted)
   2889     {
   2890       lang_section_bst_type *tree;
   2891 
   2892       walk_wild (s, output_section_callback_fast, output);
   2893 
   2894       tree = s->tree;
   2895       if (tree)
   2896 	{
   2897 	  output_section_callback_tree_to_list (s, tree, output);
   2898 	  s->tree = NULL;
   2899 	}
   2900     }
   2901   else
   2902     walk_wild (s, output_section_callback, output);
   2903 
   2904   if (default_common_section == NULL)
   2905     for (sec = s->section_list; sec != NULL; sec = sec->next)
   2906       if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
   2907 	{
   2908 	  /* Remember the section that common is going to in case we
   2909 	     later get something which doesn't know where to put it.  */
   2910 	  default_common_section = output;
   2911 	  break;
   2912 	}
   2913 }
   2914 
   2915 /* Return TRUE iff target is the sought target.  */
   2916 
   2917 static int
   2918 get_target (const bfd_target *target, void *data)
   2919 {
   2920   const char *sought = (const char *) data;
   2921 
   2922   return strcmp (target->name, sought) == 0;
   2923 }
   2924 
   2925 /* Like strcpy() but convert to lower case as well.  */
   2926 
   2927 static void
   2928 stricpy (char *dest, char *src)
   2929 {
   2930   char c;
   2931 
   2932   while ((c = *src++) != 0)
   2933     *dest++ = TOLOWER (c);
   2934 
   2935   *dest = 0;
   2936 }
   2937 
   2938 /* Remove the first occurrence of needle (if any) in haystack
   2939    from haystack.  */
   2940 
   2941 static void
   2942 strcut (char *haystack, char *needle)
   2943 {
   2944   haystack = strstr (haystack, needle);
   2945 
   2946   if (haystack)
   2947     {
   2948       char *src;
   2949 
   2950       for (src = haystack + strlen (needle); *src;)
   2951 	*haystack++ = *src++;
   2952 
   2953       *haystack = 0;
   2954     }
   2955 }
   2956 
   2957 /* Compare two target format name strings.
   2958    Return a value indicating how "similar" they are.  */
   2959 
   2960 static int
   2961 name_compare (char *first, char *second)
   2962 {
   2963   char *copy1;
   2964   char *copy2;
   2965   int result;
   2966 
   2967   copy1 = (char *) xmalloc (strlen (first) + 1);
   2968   copy2 = (char *) xmalloc (strlen (second) + 1);
   2969 
   2970   /* Convert the names to lower case.  */
   2971   stricpy (copy1, first);
   2972   stricpy (copy2, second);
   2973 
   2974   /* Remove size and endian strings from the name.  */
   2975   strcut (copy1, "big");
   2976   strcut (copy1, "little");
   2977   strcut (copy2, "big");
   2978   strcut (copy2, "little");
   2979 
   2980   /* Return a value based on how many characters match,
   2981      starting from the beginning.   If both strings are
   2982      the same then return 10 * their length.  */
   2983   for (result = 0; copy1[result] == copy2[result]; result++)
   2984     if (copy1[result] == 0)
   2985       {
   2986 	result *= 10;
   2987 	break;
   2988       }
   2989 
   2990   free (copy1);
   2991   free (copy2);
   2992 
   2993   return result;
   2994 }
   2995 
   2996 /* Set by closest_target_match() below.  */
   2997 static const bfd_target *winner;
   2998 
   2999 /* Scan all the valid bfd targets looking for one that has the endianness
   3000    requirement that was specified on the command line, and is the nearest
   3001    match to the original output target.  */
   3002 
   3003 static int
   3004 closest_target_match (const bfd_target *target, void *data)
   3005 {
   3006   const bfd_target *original = (const bfd_target *) data;
   3007 
   3008   if (command_line.endian == ENDIAN_BIG
   3009       && target->byteorder != BFD_ENDIAN_BIG)
   3010     return 0;
   3011 
   3012   if (command_line.endian == ENDIAN_LITTLE
   3013       && target->byteorder != BFD_ENDIAN_LITTLE)
   3014     return 0;
   3015 
   3016   /* Must be the same flavour.  */
   3017   if (target->flavour != original->flavour)
   3018     return 0;
   3019 
   3020   /* Ignore generic big and little endian elf vectors.  */
   3021   if (strcmp (target->name, "elf32-big") == 0
   3022       || strcmp (target->name, "elf64-big") == 0
   3023       || strcmp (target->name, "elf32-little") == 0
   3024       || strcmp (target->name, "elf64-little") == 0)
   3025     return 0;
   3026 
   3027   /* If we have not found a potential winner yet, then record this one.  */
   3028   if (winner == NULL)
   3029     {
   3030       winner = target;
   3031       return 0;
   3032     }
   3033 
   3034   /* Oh dear, we now have two potential candidates for a successful match.
   3035      Compare their names and choose the better one.  */
   3036   if (name_compare (target->name, original->name)
   3037       > name_compare (winner->name, original->name))
   3038     winner = target;
   3039 
   3040   /* Keep on searching until wqe have checked them all.  */
   3041   return 0;
   3042 }
   3043 
   3044 /* Return the BFD target format of the first input file.  */
   3045 
   3046 static char *
   3047 get_first_input_target (void)
   3048 {
   3049   char *target = NULL;
   3050 
   3051   LANG_FOR_EACH_INPUT_STATEMENT (s)
   3052     {
   3053       if (s->header.type == lang_input_statement_enum
   3054 	  && s->flags.real)
   3055 	{
   3056 	  ldfile_open_file (s);
   3057 
   3058 	  if (s->the_bfd != NULL
   3059 	      && bfd_check_format (s->the_bfd, bfd_object))
   3060 	    {
   3061 	      target = bfd_get_target (s->the_bfd);
   3062 
   3063 	      if (target != NULL)
   3064 		break;
   3065 	    }
   3066 	}
   3067     }
   3068 
   3069   return target;
   3070 }
   3071 
   3072 const char *
   3073 lang_get_output_target (void)
   3074 {
   3075   const char *target;
   3076 
   3077   /* Has the user told us which output format to use?  */
   3078   if (output_target != NULL)
   3079     return output_target;
   3080 
   3081   /* No - has the current target been set to something other than
   3082      the default?  */
   3083   if (current_target != default_target && current_target != NULL)
   3084     return current_target;
   3085 
   3086   /* No - can we determine the format of the first input file?  */
   3087   target = get_first_input_target ();
   3088   if (target != NULL)
   3089     return target;
   3090 
   3091   /* Failed - use the default output target.  */
   3092   return default_target;
   3093 }
   3094 
   3095 /* Open the output file.  */
   3096 
   3097 static void
   3098 open_output (const char *name)
   3099 {
   3100   output_target = lang_get_output_target ();
   3101 
   3102   /* Has the user requested a particular endianness on the command
   3103      line?  */
   3104   if (command_line.endian != ENDIAN_UNSET)
   3105     {
   3106       const bfd_target *target;
   3107       enum bfd_endian desired_endian;
   3108 
   3109       /* Get the chosen target.  */
   3110       target = bfd_search_for_target (get_target, (void *) output_target);
   3111 
   3112       /* If the target is not supported, we cannot do anything.  */
   3113       if (target != NULL)
   3114 	{
   3115 	  if (command_line.endian == ENDIAN_BIG)
   3116 	    desired_endian = BFD_ENDIAN_BIG;
   3117 	  else
   3118 	    desired_endian = BFD_ENDIAN_LITTLE;
   3119 
   3120 	  /* See if the target has the wrong endianness.  This should
   3121 	     not happen if the linker script has provided big and
   3122 	     little endian alternatives, but some scrips don't do
   3123 	     this.  */
   3124 	  if (target->byteorder != desired_endian)
   3125 	    {
   3126 	      /* If it does, then see if the target provides
   3127 		 an alternative with the correct endianness.  */
   3128 	      if (target->alternative_target != NULL
   3129 		  && (target->alternative_target->byteorder == desired_endian))
   3130 		output_target = target->alternative_target->name;
   3131 	      else
   3132 		{
   3133 		  /* Try to find a target as similar as possible to
   3134 		     the default target, but which has the desired
   3135 		     endian characteristic.  */
   3136 		  bfd_search_for_target (closest_target_match,
   3137 					 (void *) target);
   3138 
   3139 		  /* Oh dear - we could not find any targets that
   3140 		     satisfy our requirements.  */
   3141 		  if (winner == NULL)
   3142 		    einfo (_("%P: warning: could not find any targets"
   3143 			     " that match endianness requirement\n"));
   3144 		  else
   3145 		    output_target = winner->name;
   3146 		}
   3147 	    }
   3148 	}
   3149     }
   3150 
   3151   link_info.output_bfd = bfd_openw (name, output_target);
   3152 
   3153   if (link_info.output_bfd == NULL)
   3154     {
   3155       if (bfd_get_error () == bfd_error_invalid_target)
   3156 	einfo (_("%P%F: target %s not found\n"), output_target);
   3157 
   3158       einfo (_("%P%F: cannot open output file %s: %E\n"), name);
   3159     }
   3160 
   3161   delete_output_file_on_failure = TRUE;
   3162 
   3163   if (! bfd_set_format (link_info.output_bfd, bfd_object))
   3164     einfo (_("%P%F:%s: can not make object file: %E\n"), name);
   3165   if (! bfd_set_arch_mach (link_info.output_bfd,
   3166 			   ldfile_output_architecture,
   3167 			   ldfile_output_machine))
   3168     einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
   3169 
   3170   link_info.hash = bfd_link_hash_table_create (link_info.output_bfd);
   3171   if (link_info.hash == NULL)
   3172     einfo (_("%P%F: can not create hash table: %E\n"));
   3173 
   3174   bfd_set_gp_size (link_info.output_bfd, g_switch_value);
   3175 }
   3176 
   3177 static void
   3178 ldlang_open_output (lang_statement_union_type *statement)
   3179 {
   3180   switch (statement->header.type)
   3181     {
   3182     case lang_output_statement_enum:
   3183       ASSERT (link_info.output_bfd == NULL);
   3184       open_output (statement->output_statement.name);
   3185       ldemul_set_output_arch ();
   3186       if (config.magic_demand_paged && !link_info.relocatable)
   3187 	link_info.output_bfd->flags |= D_PAGED;
   3188       else
   3189 	link_info.output_bfd->flags &= ~D_PAGED;
   3190       if (config.text_read_only)
   3191 	link_info.output_bfd->flags |= WP_TEXT;
   3192       else
   3193 	link_info.output_bfd->flags &= ~WP_TEXT;
   3194       if (link_info.traditional_format)
   3195 	link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
   3196       else
   3197 	link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
   3198       break;
   3199 
   3200     case lang_target_statement_enum:
   3201       current_target = statement->target_statement.target;
   3202       break;
   3203     default:
   3204       break;
   3205     }
   3206 }
   3207 
   3208 /* Convert between addresses in bytes and sizes in octets.
   3209    For currently supported targets, octets_per_byte is always a power
   3210    of two, so we can use shifts.  */
   3211 #define TO_ADDR(X) ((X) >> opb_shift)
   3212 #define TO_SIZE(X) ((X) << opb_shift)
   3213 
   3214 /* Suppo