Back to index

cell-binutils  2.17cvs20070401
ldlang.c
Go to the documentation of this file.
00001 /* Linker command language support.
00002    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
00003    2001, 2002, 2003, 2004, 2005, 2006, 2007
00004    Free Software Foundation, Inc.
00005 
00006    This file is part of GLD, the Gnu Linker.
00007 
00008    GLD is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2, or (at your option)
00011    any later version.
00012 
00013    GLD is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with GLD; see the file COPYING.  If not, write to the Free
00020    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
00021    02110-1301, USA.  */
00022 
00023 #include "bfd.h"
00024 #include "sysdep.h"
00025 #include "libiberty.h"
00026 #include "safe-ctype.h"
00027 #include "obstack.h"
00028 #include "bfdlink.h"
00029 
00030 #include "ld.h"
00031 #include "ldmain.h"
00032 #include "ldexp.h"
00033 #include "ldlang.h"
00034 #include <ldgram.h>
00035 #include "ldlex.h"
00036 #include "ldmisc.h"
00037 #include "ldctor.h"
00038 #include "ldfile.h"
00039 #include "ldemul.h"
00040 #include "fnmatch.h"
00041 #include "demangle.h"
00042 #include "hashtab.h"
00043 
00044 #ifndef offsetof
00045 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
00046 #endif
00047 
00048 /* Locals variables.  */
00049 static struct obstack stat_obstack;
00050 static struct obstack map_obstack;
00051 
00052 #define obstack_chunk_alloc xmalloc
00053 #define obstack_chunk_free free
00054 static const char *startup_file;
00055 static bfd_boolean placed_commons = FALSE;
00056 static bfd_boolean stripped_excluded_sections = FALSE;
00057 static lang_output_section_statement_type *default_common_section;
00058 static bfd_boolean map_option_f;
00059 static bfd_vma print_dot;
00060 static lang_input_statement_type *first_file;
00061 static const char *current_target;
00062 static const char *output_target;
00063 static lang_statement_list_type statement_list;
00064 static struct bfd_hash_table lang_definedness_table;
00065 
00066 /* Forward declarations.  */
00067 static void exp_init_os (etree_type *);
00068 static void init_map_userdata (bfd *, asection *, void *);
00069 static lang_input_statement_type *lookup_name (const char *);
00070 static struct bfd_hash_entry *lang_definedness_newfunc
00071  (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
00072 static void insert_undefined (const char *);
00073 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
00074 static void print_statement (lang_statement_union_type *,
00075                           lang_output_section_statement_type *);
00076 static void print_statement_list (lang_statement_union_type *,
00077                               lang_output_section_statement_type *);
00078 static void print_statements (void);
00079 static void print_input_section (asection *);
00080 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
00081 static void lang_record_phdrs (void);
00082 static void lang_do_version_exports_section (void);
00083 static void lang_finalize_version_expr_head
00084   (struct bfd_elf_version_expr_head *);
00085 
00086 /* Exported variables.  */
00087 lang_output_section_statement_type *abs_output_section;
00088 lang_statement_list_type lang_output_section_statement;
00089 lang_statement_list_type *stat_ptr = &statement_list;
00090 lang_statement_list_type file_chain = { NULL, NULL };
00091 lang_statement_list_type input_file_chain;
00092 struct bfd_sym_chain entry_symbol = { NULL, NULL };
00093 static const char *entry_symbol_default = "start";
00094 const char *entry_section = ".text";
00095 bfd_boolean entry_from_cmdline;
00096 bfd_boolean lang_has_input_file = FALSE;
00097 bfd_boolean had_output_filename = FALSE;
00098 bfd_boolean lang_float_flag = FALSE;
00099 bfd_boolean delete_output_file_on_failure = FALSE;
00100 struct lang_phdr *lang_phdr_list;
00101 struct lang_nocrossrefs *nocrossref_list;
00102 static struct unique_sections *unique_section_list;
00103 static bfd_boolean ldlang_sysrooted_script = FALSE;
00104 
00105  /* Functions that traverse the linker script and might evaluate
00106     DEFINED() need to increment this.  */
00107 int lang_statement_iteration = 0;
00108 
00109 etree_type *base; /* Relocation base - or null */
00110 
00111 /* Return TRUE if the PATTERN argument is a wildcard pattern.
00112    Although backslashes are treated specially if a pattern contains
00113    wildcards, we do not consider the mere presence of a backslash to
00114    be enough to cause the pattern to be treated as a wildcard.
00115    That lets us handle DOS filenames more naturally.  */
00116 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
00117 
00118 #define new_stat(x, y) \
00119   (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
00120 
00121 #define outside_section_address(q) \
00122   ((q)->output_offset + (q)->output_section->vma)
00123 
00124 #define outside_symbol_address(q) \
00125   ((q)->value + outside_section_address (q->section))
00126 
00127 #define SECTION_NAME_MAP_LENGTH (16)
00128 
00129 void *
00130 stat_alloc (size_t size)
00131 {
00132   return obstack_alloc (&stat_obstack, size);
00133 }
00134 
00135 bfd_boolean
00136 unique_section_p (const asection *sec)
00137 {
00138   struct unique_sections *unam;
00139   const char *secnam;
00140 
00141   if (link_info.relocatable
00142       && sec->owner != NULL
00143       && bfd_is_group_section (sec->owner, sec))
00144     return TRUE;
00145 
00146   secnam = sec->name;
00147   for (unam = unique_section_list; unam; unam = unam->next)
00148     if (wildcardp (unam->name)
00149        ? fnmatch (unam->name, secnam, 0) == 0
00150        : strcmp (unam->name, secnam) == 0)
00151       {
00152        return TRUE;
00153       }
00154 
00155   return FALSE;
00156 }
00157 
00158 /* Generic traversal routines for finding matching sections.  */
00159 
00160 /* Try processing a section against a wildcard.  This just calls
00161    the callback unless the filename exclusion list is present
00162    and excludes the file.  It's hardly ever present so this
00163    function is very fast.  */
00164 
00165 static void
00166 walk_wild_consider_section (lang_wild_statement_type *ptr,
00167                          lang_input_statement_type *file,
00168                          asection *s,
00169                          struct wildcard_list *sec,
00170                          callback_t callback,
00171                          void *data)
00172 {
00173   bfd_boolean skip = FALSE;
00174   struct name_list *list_tmp;
00175 
00176   /* Don't process sections from files which were
00177      excluded.  */
00178   for (list_tmp = sec->spec.exclude_name_list;
00179        list_tmp;
00180        list_tmp = list_tmp->next)
00181     {
00182       bfd_boolean is_wildcard = wildcardp (list_tmp->name);
00183       if (is_wildcard)
00184        skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
00185       else
00186        skip = strcmp (list_tmp->name, file->filename) == 0;
00187 
00188       /* If this file is part of an archive, and the archive is
00189         excluded, exclude this file.  */
00190       if (! skip && file->the_bfd != NULL
00191          && file->the_bfd->my_archive != NULL
00192          && file->the_bfd->my_archive->filename != NULL)
00193        {
00194          if (is_wildcard)
00195            skip = fnmatch (list_tmp->name,
00196                          file->the_bfd->my_archive->filename,
00197                          0) == 0;
00198          else
00199            skip = strcmp (list_tmp->name,
00200                         file->the_bfd->my_archive->filename) == 0;
00201        }
00202 
00203       if (skip)
00204        break;
00205     }
00206 
00207   if (!skip)
00208     (*callback) (ptr, sec, s, file, data);
00209 }
00210 
00211 /* Lowest common denominator routine that can handle everything correctly,
00212    but slowly.  */
00213 
00214 static void
00215 walk_wild_section_general (lang_wild_statement_type *ptr,
00216                         lang_input_statement_type *file,
00217                         callback_t callback,
00218                         void *data)
00219 {
00220   asection *s;
00221   struct wildcard_list *sec;
00222 
00223   for (s = file->the_bfd->sections; s != NULL; s = s->next)
00224     {
00225       sec = ptr->section_list;
00226       if (sec == NULL)
00227        (*callback) (ptr, sec, s, file, data);
00228 
00229       while (sec != NULL)
00230        {
00231          bfd_boolean skip = FALSE;
00232 
00233          if (sec->spec.name != NULL)
00234            {
00235              const char *sname = bfd_get_section_name (file->the_bfd, s);
00236 
00237              if (wildcardp (sec->spec.name))
00238               skip = fnmatch (sec->spec.name, sname, 0) != 0;
00239              else
00240               skip = strcmp (sec->spec.name, sname) != 0;
00241            }
00242 
00243          if (!skip)
00244            walk_wild_consider_section (ptr, file, s, sec, callback, data);
00245 
00246          sec = sec->next;
00247        }
00248     }
00249 }
00250 
00251 /* Routines to find a single section given its name.  If there's more
00252    than one section with that name, we report that.  */
00253 
00254 typedef struct
00255 {
00256   asection *found_section;
00257   bfd_boolean multiple_sections_found;
00258 } section_iterator_callback_data;
00259 
00260 static bfd_boolean
00261 section_iterator_callback (bfd *bfd ATTRIBUTE_UNUSED, asection *s, void *data)
00262 {
00263   section_iterator_callback_data *d = data;
00264 
00265   if (d->found_section != NULL)
00266     {
00267       d->multiple_sections_found = TRUE;
00268       return TRUE;
00269     }
00270 
00271   d->found_section = s;
00272   return FALSE;
00273 }
00274 
00275 static asection *
00276 find_section (lang_input_statement_type *file,
00277              struct wildcard_list *sec,
00278              bfd_boolean *multiple_sections_found)
00279 {
00280   section_iterator_callback_data cb_data = { NULL, FALSE };
00281 
00282   bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
00283                            section_iterator_callback, &cb_data);
00284   *multiple_sections_found = cb_data.multiple_sections_found;
00285   return cb_data.found_section;
00286 }
00287 
00288 /* Code for handling simple wildcards without going through fnmatch,
00289    which can be expensive because of charset translations etc.  */
00290 
00291 /* A simple wild is a literal string followed by a single '*',
00292    where the literal part is at least 4 characters long.  */
00293 
00294 static bfd_boolean
00295 is_simple_wild (const char *name)
00296 {
00297   size_t len = strcspn (name, "*?[");
00298   return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
00299 }
00300 
00301 static bfd_boolean
00302 match_simple_wild (const char *pattern, const char *name)
00303 {
00304   /* The first four characters of the pattern are guaranteed valid
00305      non-wildcard characters.  So we can go faster.  */
00306   if (pattern[0] != name[0] || pattern[1] != name[1]
00307       || pattern[2] != name[2] || pattern[3] != name[3])
00308     return FALSE;
00309 
00310   pattern += 4;
00311   name += 4;
00312   while (*pattern != '*')
00313     if (*name++ != *pattern++)
00314       return FALSE;
00315 
00316   return TRUE;
00317 }
00318 
00319 /* Compare sections ASEC and BSEC according to SORT.  */
00320 
00321 static int
00322 compare_section (sort_type sort, asection *asec, asection *bsec)
00323 {
00324   int ret;
00325 
00326   switch (sort)
00327     {
00328     default:
00329       abort ();
00330 
00331     case by_alignment_name:
00332       ret = (bfd_section_alignment (bsec->owner, bsec)
00333             - bfd_section_alignment (asec->owner, asec));
00334       if (ret)
00335        break;
00336       /* Fall through.  */
00337 
00338     case by_name:
00339       ret = strcmp (bfd_get_section_name (asec->owner, asec),
00340                   bfd_get_section_name (bsec->owner, bsec));
00341       break;
00342 
00343     case by_name_alignment:
00344       ret = strcmp (bfd_get_section_name (asec->owner, asec),
00345                   bfd_get_section_name (bsec->owner, bsec));
00346       if (ret)
00347        break;
00348       /* Fall through.  */
00349 
00350     case by_alignment:
00351       ret = (bfd_section_alignment (bsec->owner, bsec)
00352             - bfd_section_alignment (asec->owner, asec));
00353       break;
00354     }
00355 
00356   return ret;
00357 }
00358 
00359 /* Build a Binary Search Tree to sort sections, unlike insertion sort
00360    used in wild_sort(). BST is considerably faster if the number of
00361    of sections are large.  */
00362 
00363 static lang_section_bst_type **
00364 wild_sort_fast (lang_wild_statement_type *wild,
00365               struct wildcard_list *sec,
00366               lang_input_statement_type *file ATTRIBUTE_UNUSED,
00367               asection *section)
00368 {
00369   lang_section_bst_type **tree;
00370 
00371   tree = &wild->tree;
00372   if (!wild->filenames_sorted
00373       && (sec == NULL || sec->spec.sorted == none))
00374     {
00375       /* Append at the right end of tree.  */
00376       while (*tree)
00377        tree = &((*tree)->right);
00378       return tree;
00379     }
00380 
00381   while (*tree)
00382     {
00383       /* Find the correct node to append this section.  */
00384       if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
00385        tree = &((*tree)->left);
00386       else
00387        tree = &((*tree)->right);
00388     }
00389 
00390   return tree;
00391 }
00392 
00393 /* Use wild_sort_fast to build a BST to sort sections.  */
00394 
00395 static void
00396 output_section_callback_fast (lang_wild_statement_type *ptr,
00397                            struct wildcard_list *sec,
00398                            asection *section,
00399                            lang_input_statement_type *file,
00400                            void *output ATTRIBUTE_UNUSED)
00401 {
00402   lang_section_bst_type *node;
00403   lang_section_bst_type **tree;
00404 
00405   if (unique_section_p (section))
00406     return;
00407 
00408   node = xmalloc (sizeof (lang_section_bst_type));
00409   node->left = 0;
00410   node->right = 0;
00411   node->section = section;
00412 
00413   tree = wild_sort_fast (ptr, sec, file, section);
00414   if (tree != NULL)
00415     *tree = node;
00416 }
00417 
00418 /* Convert a sorted sections' BST back to list form.  */
00419 
00420 static void
00421 output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
00422                                   lang_section_bst_type *tree,
00423                                   void *output)
00424 {
00425   if (tree->left)
00426     output_section_callback_tree_to_list (ptr, tree->left, output);
00427 
00428   lang_add_section (&ptr->children, tree->section,
00429                   (lang_output_section_statement_type *) output);
00430 
00431   if (tree->right)
00432     output_section_callback_tree_to_list (ptr, tree->right, output);
00433 
00434   free (tree);
00435 }
00436 
00437 /* Specialized, optimized routines for handling different kinds of
00438    wildcards */
00439 
00440 static void
00441 walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
00442                             lang_input_statement_type *file,
00443                             callback_t callback,
00444                             void *data)
00445 {
00446   /* We can just do a hash lookup for the section with the right name.
00447      But if that lookup discovers more than one section with the name
00448      (should be rare), we fall back to the general algorithm because
00449      we would otherwise have to sort the sections to make sure they
00450      get processed in the bfd's order.  */
00451   bfd_boolean multiple_sections_found;
00452   struct wildcard_list *sec0 = ptr->handler_data[0];
00453   asection *s0 = find_section (file, sec0, &multiple_sections_found);
00454 
00455   if (multiple_sections_found)
00456     walk_wild_section_general (ptr, file, callback, data);
00457   else if (s0)
00458     walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
00459 }
00460 
00461 static void
00462 walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
00463                             lang_input_statement_type *file,
00464                             callback_t callback,
00465                             void *data)
00466 {
00467   asection *s;
00468   struct wildcard_list *wildsec0 = ptr->handler_data[0];
00469 
00470   for (s = file->the_bfd->sections; s != NULL; s = s->next)
00471     {
00472       const char *sname = bfd_get_section_name (file->the_bfd, s);
00473       bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
00474 
00475       if (!skip)
00476        walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
00477     }
00478 }
00479 
00480 static void
00481 walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
00482                             lang_input_statement_type *file,
00483                             callback_t callback,
00484                             void *data)
00485 {
00486   asection *s;
00487   struct wildcard_list *sec0 = ptr->handler_data[0];
00488   struct wildcard_list *wildsec1 = ptr->handler_data[1];
00489   bfd_boolean multiple_sections_found;
00490   asection *s0 = find_section (file, sec0, &multiple_sections_found);
00491 
00492   if (multiple_sections_found)
00493     {
00494       walk_wild_section_general (ptr, file, callback, data);
00495       return;
00496     }
00497 
00498   /* Note that if the section was not found, s0 is NULL and
00499      we'll simply never succeed the s == s0 test below.  */
00500   for (s = file->the_bfd->sections; s != NULL; s = s->next)
00501     {
00502       /* Recall that in this code path, a section cannot satisfy more
00503         than one spec, so if s == s0 then it cannot match
00504         wildspec1.  */
00505       if (s == s0)
00506        walk_wild_consider_section (ptr, file, s, sec0, callback, data);
00507       else
00508        {
00509          const char *sname = bfd_get_section_name (file->the_bfd, s);
00510          bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
00511 
00512          if (!skip)
00513            walk_wild_consider_section (ptr, file, s, wildsec1, callback,
00514                                    data);
00515        }
00516     }
00517 }
00518 
00519 static void
00520 walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
00521                             lang_input_statement_type *file,
00522                             callback_t callback,
00523                             void *data)
00524 {
00525   asection *s;
00526   struct wildcard_list *sec0 = ptr->handler_data[0];
00527   struct wildcard_list *wildsec1 = ptr->handler_data[1];
00528   struct wildcard_list *wildsec2 = ptr->handler_data[2];
00529   bfd_boolean multiple_sections_found;
00530   asection *s0 = find_section (file, sec0, &multiple_sections_found);
00531 
00532   if (multiple_sections_found)
00533     {
00534       walk_wild_section_general (ptr, file, callback, data);
00535       return;
00536     }
00537 
00538   for (s = file->the_bfd->sections; s != NULL; s = s->next)
00539     {
00540       if (s == s0)
00541        walk_wild_consider_section (ptr, file, s, sec0, callback, data);
00542       else
00543        {
00544          const char *sname = bfd_get_section_name (file->the_bfd, s);
00545          bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
00546 
00547          if (!skip)
00548            walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
00549          else
00550            {
00551              skip = !match_simple_wild (wildsec2->spec.name, sname);
00552              if (!skip)
00553               walk_wild_consider_section (ptr, file, s, wildsec2, callback,
00554                                        data);
00555            }
00556        }
00557     }
00558 }
00559 
00560 static void
00561 walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
00562                             lang_input_statement_type *file,
00563                             callback_t callback,
00564                             void *data)
00565 {
00566   asection *s;
00567   struct wildcard_list *sec0 = ptr->handler_data[0];
00568   struct wildcard_list *sec1 = ptr->handler_data[1];
00569   struct wildcard_list *wildsec2 = ptr->handler_data[2];
00570   struct wildcard_list *wildsec3 = ptr->handler_data[3];
00571   bfd_boolean multiple_sections_found;
00572   asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
00573 
00574   if (multiple_sections_found)
00575     {
00576       walk_wild_section_general (ptr, file, callback, data);
00577       return;
00578     }
00579 
00580   s1 = find_section (file, sec1, &multiple_sections_found);
00581   if (multiple_sections_found)
00582     {
00583       walk_wild_section_general (ptr, file, callback, data);
00584       return;
00585     }
00586 
00587   for (s = file->the_bfd->sections; s != NULL; s = s->next)
00588     {
00589       if (s == s0)
00590        walk_wild_consider_section (ptr, file, s, sec0, callback, data);
00591       else
00592        if (s == s1)
00593          walk_wild_consider_section (ptr, file, s, sec1, callback, data);
00594        else
00595          {
00596            const char *sname = bfd_get_section_name (file->the_bfd, s);
00597            bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
00598                                              sname);
00599 
00600            if (!skip)
00601              walk_wild_consider_section (ptr, file, s, wildsec2, callback,
00602                                      data);
00603            else
00604              {
00605               skip = !match_simple_wild (wildsec3->spec.name, sname);
00606               if (!skip)
00607                 walk_wild_consider_section (ptr, file, s, wildsec3,
00608                                          callback, data);
00609              }
00610          }
00611     }
00612 }
00613 
00614 static void
00615 walk_wild_section (lang_wild_statement_type *ptr,
00616                  lang_input_statement_type *file,
00617                  callback_t callback,
00618                  void *data)
00619 {
00620   if (file->just_syms_flag)
00621     return;
00622 
00623   (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
00624 }
00625 
00626 /* Returns TRUE when name1 is a wildcard spec that might match
00627    something name2 can match.  We're conservative: we return FALSE
00628    only if the prefixes of name1 and name2 are different up to the
00629    first wildcard character.  */
00630 
00631 static bfd_boolean
00632 wild_spec_can_overlap (const char *name1, const char *name2)
00633 {
00634   size_t prefix1_len = strcspn (name1, "?*[");
00635   size_t prefix2_len = strcspn (name2, "?*[");
00636   size_t min_prefix_len;
00637 
00638   /* Note that if there is no wildcard character, then we treat the
00639      terminating 0 as part of the prefix.  Thus ".text" won't match
00640      ".text." or ".text.*", for example.  */
00641   if (name1[prefix1_len] == '\0')
00642     prefix1_len++;
00643   if (name2[prefix2_len] == '\0')
00644     prefix2_len++;
00645 
00646   min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
00647 
00648   return memcmp (name1, name2, min_prefix_len) == 0;
00649 }
00650 
00651 /* Select specialized code to handle various kinds of wildcard
00652    statements.  */
00653 
00654 static void
00655 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
00656 {
00657   int sec_count = 0;
00658   int wild_name_count = 0;
00659   struct wildcard_list *sec;
00660   int signature;
00661   int data_counter;
00662 
00663   ptr->walk_wild_section_handler = walk_wild_section_general;
00664   ptr->handler_data[0] = NULL;
00665   ptr->handler_data[1] = NULL;
00666   ptr->handler_data[2] = NULL;
00667   ptr->handler_data[3] = NULL;
00668   ptr->tree = NULL;
00669 
00670   /* Count how many wildcard_specs there are, and how many of those
00671      actually use wildcards in the name.  Also, bail out if any of the
00672      wildcard names are NULL. (Can this actually happen?
00673      walk_wild_section used to test for it.)  And bail out if any
00674      of the wildcards are more complex than a simple string
00675      ending in a single '*'.  */
00676   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
00677     {
00678       ++sec_count;
00679       if (sec->spec.name == NULL)
00680        return;
00681       if (wildcardp (sec->spec.name))
00682        {
00683          ++wild_name_count;
00684          if (!is_simple_wild (sec->spec.name))
00685            return;
00686        }
00687     }
00688 
00689   /* The zero-spec case would be easy to optimize but it doesn't
00690      happen in practice.  Likewise, more than 4 specs doesn't
00691      happen in practice.  */
00692   if (sec_count == 0 || sec_count > 4)
00693     return;
00694 
00695   /* Check that no two specs can match the same section.  */
00696   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
00697     {
00698       struct wildcard_list *sec2;
00699       for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
00700        {
00701          if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
00702            return;
00703        }
00704     }
00705 
00706   signature = (sec_count << 8) + wild_name_count;
00707   switch (signature)
00708     {
00709     case 0x0100:
00710       ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
00711       break;
00712     case 0x0101:
00713       ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
00714       break;
00715     case 0x0201:
00716       ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
00717       break;
00718     case 0x0302:
00719       ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
00720       break;
00721     case 0x0402:
00722       ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
00723       break;
00724     default:
00725       return;
00726     }
00727 
00728   /* Now fill the data array with pointers to the specs, first the
00729      specs with non-wildcard names, then the specs with wildcard
00730      names.  It's OK to process the specs in different order from the
00731      given order, because we've already determined that no section
00732      will match more than one spec.  */
00733   data_counter = 0;
00734   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
00735     if (!wildcardp (sec->spec.name))
00736       ptr->handler_data[data_counter++] = sec;
00737   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
00738     if (wildcardp (sec->spec.name))
00739       ptr->handler_data[data_counter++] = sec;
00740 }
00741 
00742 /* Handle a wild statement for a single file F.  */
00743 
00744 static void
00745 walk_wild_file (lang_wild_statement_type *s,
00746               lang_input_statement_type *f,
00747               callback_t callback,
00748               void *data)
00749 {
00750   if (f->the_bfd == NULL
00751       || ! bfd_check_format (f->the_bfd, bfd_archive))
00752     walk_wild_section (s, f, callback, data);
00753   else
00754     {
00755       bfd *member;
00756 
00757       /* This is an archive file.  We must map each member of the
00758         archive separately.  */
00759       member = bfd_openr_next_archived_file (f->the_bfd, NULL);
00760       while (member != NULL)
00761        {
00762          /* When lookup_name is called, it will call the add_symbols
00763             entry point for the archive.  For each element of the
00764             archive which is included, BFD will call ldlang_add_file,
00765             which will set the usrdata field of the member to the
00766             lang_input_statement.  */
00767          if (member->usrdata != NULL)
00768            {
00769              walk_wild_section (s, member->usrdata, callback, data);
00770            }
00771 
00772          member = bfd_openr_next_archived_file (f->the_bfd, member);
00773        }
00774     }
00775 }
00776 
00777 static void
00778 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
00779 {
00780   const char *file_spec = s->filename;
00781 
00782   if (file_spec == NULL)
00783     {
00784       /* Perform the iteration over all files in the list.  */
00785       LANG_FOR_EACH_INPUT_STATEMENT (f)
00786        {
00787          walk_wild_file (s, f, callback, data);
00788        }
00789     }
00790   else if (wildcardp (file_spec))
00791     {
00792       LANG_FOR_EACH_INPUT_STATEMENT (f)
00793        {
00794          if (fnmatch (file_spec, f->filename, 0) == 0)
00795            walk_wild_file (s, f, callback, data);
00796        }
00797     }
00798   else
00799     {
00800       lang_input_statement_type *f;
00801 
00802       /* Perform the iteration over a single file.  */
00803       f = lookup_name (file_spec);
00804       if (f)
00805        walk_wild_file (s, f, callback, data);
00806     }
00807 }
00808 
00809 /* lang_for_each_statement walks the parse tree and calls the provided
00810    function for each node.  */
00811 
00812 static void
00813 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
00814                             lang_statement_union_type *s)
00815 {
00816   for (; s != NULL; s = s->header.next)
00817     {
00818       func (s);
00819 
00820       switch (s->header.type)
00821        {
00822        case lang_constructors_statement_enum:
00823          lang_for_each_statement_worker (func, constructor_list.head);
00824          break;
00825        case lang_output_section_statement_enum:
00826          lang_for_each_statement_worker
00827            (func, s->output_section_statement.children.head);
00828          break;
00829        case lang_wild_statement_enum:
00830          lang_for_each_statement_worker (func,
00831                                      s->wild_statement.children.head);
00832          break;
00833        case lang_group_statement_enum:
00834          lang_for_each_statement_worker (func,
00835                                      s->group_statement.children.head);
00836          break;
00837        case lang_data_statement_enum:
00838        case lang_reloc_statement_enum:
00839        case lang_object_symbols_statement_enum:
00840        case lang_output_statement_enum:
00841        case lang_target_statement_enum:
00842        case lang_input_section_enum:
00843        case lang_input_statement_enum:
00844        case lang_assignment_statement_enum:
00845        case lang_padding_statement_enum:
00846        case lang_address_statement_enum:
00847        case lang_fill_statement_enum:
00848          break;
00849        default:
00850          FAIL ();
00851          break;
00852        }
00853     }
00854 }
00855 
00856 void
00857 lang_for_each_statement (void (*func) (lang_statement_union_type *))
00858 {
00859   lang_for_each_statement_worker (func, statement_list.head);
00860 }
00861 
00862 /*----------------------------------------------------------------------*/
00863 
00864 void
00865 lang_list_init (lang_statement_list_type *list)
00866 {
00867   list->head = NULL;
00868   list->tail = &list->head;
00869 }
00870 
00871 /* Build a new statement node for the parse tree.  */
00872 
00873 static lang_statement_union_type *
00874 new_statement (enum statement_enum type,
00875               size_t size,
00876               lang_statement_list_type *list)
00877 {
00878   lang_statement_union_type *new;
00879 
00880   new = stat_alloc (size);
00881   new->header.type = type;
00882   new->header.next = NULL;
00883   lang_statement_append (list, new, &new->header.next);
00884   return new;
00885 }
00886 
00887 /* Build a new input file node for the language.  There are several
00888    ways in which we treat an input file, eg, we only look at symbols,
00889    or prefix it with a -l etc.
00890 
00891    We can be supplied with requests for input files more than once;
00892    they may, for example be split over several lines like foo.o(.text)
00893    foo.o(.data) etc, so when asked for a file we check that we haven't
00894    got it already so we don't duplicate the bfd.  */
00895 
00896 static lang_input_statement_type *
00897 new_afile (const char *name,
00898           lang_input_file_enum_type file_type,
00899           const char *target,
00900           bfd_boolean add_to_list)
00901 {
00902   lang_input_statement_type *p;
00903 
00904   if (add_to_list)
00905     p = new_stat (lang_input_statement, stat_ptr);
00906   else
00907     {
00908       p = stat_alloc (sizeof (lang_input_statement_type));
00909       p->header.type = lang_input_statement_enum;
00910       p->header.next = NULL;
00911     }
00912 
00913   lang_has_input_file = TRUE;
00914   p->target = target;
00915   p->sysrooted = FALSE;
00916 
00917   if (file_type == lang_input_file_is_l_enum
00918       && name[0] == ':' && name[1] != '\0')
00919     {
00920       file_type = lang_input_file_is_search_file_enum;
00921       name = name + 1;
00922     }
00923 
00924   switch (file_type)
00925     {
00926     case lang_input_file_is_symbols_only_enum:
00927       p->filename = name;
00928       p->is_archive = FALSE;
00929       p->real = TRUE;
00930       p->local_sym_name = name;
00931       p->just_syms_flag = TRUE;
00932       p->search_dirs_flag = FALSE;
00933       break;
00934     case lang_input_file_is_fake_enum:
00935       p->filename = name;
00936       p->is_archive = FALSE;
00937       p->real = FALSE;
00938       p->local_sym_name = name;
00939       p->just_syms_flag = FALSE;
00940       p->search_dirs_flag = FALSE;
00941       break;
00942     case lang_input_file_is_l_enum:
00943       p->is_archive = TRUE;
00944       p->filename = name;
00945       p->real = TRUE;
00946       p->local_sym_name = concat ("-l", name, NULL);
00947       p->just_syms_flag = FALSE;
00948       p->search_dirs_flag = TRUE;
00949       break;
00950     case lang_input_file_is_marker_enum:
00951       p->filename = name;
00952       p->is_archive = FALSE;
00953       p->real = FALSE;
00954       p->local_sym_name = name;
00955       p->just_syms_flag = FALSE;
00956       p->search_dirs_flag = TRUE;
00957       break;
00958     case lang_input_file_is_search_file_enum:
00959       p->sysrooted = ldlang_sysrooted_script;
00960       p->filename = name;
00961       p->is_archive = FALSE;
00962       p->real = TRUE;
00963       p->local_sym_name = name;
00964       p->just_syms_flag = FALSE;
00965       p->search_dirs_flag = TRUE;
00966       break;
00967     case lang_input_file_is_file_enum:
00968       p->filename = name;
00969       p->is_archive = FALSE;
00970       p->real = TRUE;
00971       p->local_sym_name = name;
00972       p->just_syms_flag = FALSE;
00973       p->search_dirs_flag = FALSE;
00974       break;
00975     default:
00976       FAIL ();
00977     }
00978   p->the_bfd = NULL;
00979   p->asymbols = NULL;
00980   p->next_real_file = NULL;
00981   p->next = NULL;
00982   p->symbol_count = 0;
00983   p->dynamic = config.dynamic_link;
00984   p->add_needed = add_needed;
00985   p->as_needed = as_needed;
00986   p->whole_archive = whole_archive;
00987   p->loaded = FALSE;
00988   lang_statement_append (&input_file_chain,
00989                       (lang_statement_union_type *) p,
00990                       &p->next_real_file);
00991   return p;
00992 }
00993 
00994 lang_input_statement_type *
00995 lang_add_input_file (const char *name,
00996                    lang_input_file_enum_type file_type,
00997                    const char *target)
00998 {
00999   return new_afile (name, file_type, target, TRUE);
01000 }
01001 
01002 struct out_section_hash_entry
01003 {
01004   struct bfd_hash_entry root;
01005   lang_statement_union_type s;
01006 };
01007 
01008 /* The hash table.  */
01009 
01010 static struct bfd_hash_table output_section_statement_table;
01011 
01012 /* Support routines for the hash table used by lang_output_section_find,
01013    initialize the table, fill in an entry and remove the table.  */
01014 
01015 static struct bfd_hash_entry *
01016 output_section_statement_newfunc (struct bfd_hash_entry *entry,
01017                               struct bfd_hash_table *table,
01018                               const char *string)
01019 {
01020   lang_output_section_statement_type **nextp;
01021   struct out_section_hash_entry *ret;
01022 
01023   if (entry == NULL)
01024     {
01025       entry = bfd_hash_allocate (table, sizeof (*ret));
01026       if (entry == NULL)
01027        return entry;
01028     }
01029 
01030   entry = bfd_hash_newfunc (entry, table, string);
01031   if (entry == NULL)
01032     return entry;
01033 
01034   ret = (struct out_section_hash_entry *) entry;
01035   memset (&ret->s, 0, sizeof (ret->s));
01036   ret->s.header.type = lang_output_section_statement_enum;
01037   ret->s.output_section_statement.subsection_alignment = -1;
01038   ret->s.output_section_statement.section_alignment = -1;
01039   ret->s.output_section_statement.block_value = 1;
01040   lang_list_init (&ret->s.output_section_statement.children);
01041   lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
01042 
01043   /* For every output section statement added to the list, except the
01044      first one, lang_output_section_statement.tail points to the "next"
01045      field of the last element of the list.  */
01046   if (lang_output_section_statement.head != NULL)
01047     ret->s.output_section_statement.prev
01048       = ((lang_output_section_statement_type *)
01049         ((char *) lang_output_section_statement.tail
01050          - offsetof (lang_output_section_statement_type, next)));
01051 
01052   /* GCC's strict aliasing rules prevent us from just casting the
01053      address, so we store the pointer in a variable and cast that
01054      instead.  */
01055   nextp = &ret->s.output_section_statement.next;
01056   lang_statement_append (&lang_output_section_statement,
01057                       &ret->s,
01058                       (lang_statement_union_type **) nextp);
01059   return &ret->root;
01060 }
01061 
01062 static void
01063 output_section_statement_table_init (void)
01064 {
01065   if (!bfd_hash_table_init_n (&output_section_statement_table,
01066                            output_section_statement_newfunc,
01067                            sizeof (struct out_section_hash_entry),
01068                            61))
01069     einfo (_("%P%F: can not create hash table: %E\n"));
01070 }
01071 
01072 static void
01073 output_section_statement_table_free (void)
01074 {
01075   bfd_hash_table_free (&output_section_statement_table);
01076 }
01077 
01078 /* Build enough state so that the parser can build its tree.  */
01079 
01080 void
01081 lang_init (void)
01082 {
01083   obstack_begin (&stat_obstack, 1000);
01084 
01085   stat_ptr = &statement_list;
01086 
01087   output_section_statement_table_init ();
01088 
01089   lang_list_init (stat_ptr);
01090 
01091   lang_list_init (&input_file_chain);
01092   lang_list_init (&lang_output_section_statement);
01093   lang_list_init (&file_chain);
01094   first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
01095                                 NULL);
01096   abs_output_section =
01097     lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
01098 
01099   abs_output_section->bfd_section = bfd_abs_section_ptr;
01100 
01101   /* The value "3" is ad-hoc, somewhat related to the expected number of
01102      DEFINED expressions in a linker script.  For most default linker
01103      scripts, there are none.  Why a hash table then?  Well, it's somewhat
01104      simpler to re-use working machinery than using a linked list in terms
01105      of code-complexity here in ld, besides the initialization which just
01106      looks like other code here.  */
01107   if (!bfd_hash_table_init_n (&lang_definedness_table,
01108                            lang_definedness_newfunc,
01109                            sizeof (struct lang_definedness_hash_entry),
01110                            3))
01111     einfo (_("%P%F: can not create hash table: %E\n"));
01112 }
01113 
01114 void
01115 lang_finish (void)
01116 {
01117   output_section_statement_table_free ();
01118 }
01119 
01120 /*----------------------------------------------------------------------
01121   A region is an area of memory declared with the
01122   MEMORY {  name:org=exp, len=exp ... }
01123   syntax.
01124 
01125   We maintain a list of all the regions here.
01126 
01127   If no regions are specified in the script, then the default is used
01128   which is created when looked up to be the entire data space.
01129 
01130   If create is true we are creating a region inside a MEMORY block.
01131   In this case it is probably an error to create a region that has
01132   already been created.  If we are not inside a MEMORY block it is
01133   dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
01134   and so we issue a warning.  */
01135 
01136 static lang_memory_region_type *lang_memory_region_list;
01137 static lang_memory_region_type **lang_memory_region_list_tail
01138   = &lang_memory_region_list;
01139 
01140 lang_memory_region_type *
01141 lang_memory_region_lookup (const char *const name, bfd_boolean create)
01142 {
01143   lang_memory_region_type *p;
01144   lang_memory_region_type *new;
01145 
01146   /* NAME is NULL for LMA memspecs if no region was specified.  */
01147   if (name == NULL)
01148     return NULL;
01149 
01150   for (p = lang_memory_region_list; p != NULL; p = p->next)
01151     if (strcmp (p->name, name) == 0)
01152       {
01153        if (create)
01154          einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
01155                name);
01156        return p;
01157       }
01158 
01159   if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
01160     einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
01161 
01162   new = stat_alloc (sizeof (lang_memory_region_type));
01163 
01164   new->name = xstrdup (name);
01165   new->next = NULL;
01166   new->origin = 0;
01167   new->length = ~(bfd_size_type) 0;
01168   new->current = 0;
01169   new->last_os = NULL;
01170   new->flags = 0;
01171   new->not_flags = 0;
01172   new->had_full_message = FALSE;
01173 
01174   *lang_memory_region_list_tail = new;
01175   lang_memory_region_list_tail = &new->next;
01176 
01177   return new;
01178 }
01179 
01180 static lang_memory_region_type *
01181 lang_memory_default (asection *section)
01182 {
01183   lang_memory_region_type *p;
01184 
01185   flagword sec_flags = section->flags;
01186 
01187   /* Override SEC_DATA to mean a writable section.  */
01188   if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
01189     sec_flags |= SEC_DATA;
01190 
01191   for (p = lang_memory_region_list; p != NULL; p = p->next)
01192     {
01193       if ((p->flags & sec_flags) != 0
01194          && (p->not_flags & sec_flags) == 0)
01195        {
01196          return p;
01197        }
01198     }
01199   return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
01200 }
01201 
01202 lang_output_section_statement_type *
01203 lang_output_section_find (const char *const name)
01204 {
01205   struct out_section_hash_entry *entry;
01206   unsigned long hash;
01207 
01208   entry = ((struct out_section_hash_entry *)
01209           bfd_hash_lookup (&output_section_statement_table, name,
01210                          FALSE, FALSE));
01211   if (entry == NULL)
01212     return NULL;
01213 
01214   hash = entry->root.hash;
01215   do
01216     {
01217       if (entry->s.output_section_statement.constraint != -1)
01218        return &entry->s.output_section_statement;
01219       entry = (struct out_section_hash_entry *) entry->root.next;
01220     }
01221   while (entry != NULL
01222         && entry->root.hash == hash
01223         && strcmp (name, entry->s.output_section_statement.name) == 0);
01224 
01225   return NULL;
01226 }
01227 
01228 static lang_output_section_statement_type *
01229 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
01230 {
01231   struct out_section_hash_entry *entry;
01232   struct out_section_hash_entry *last_ent;
01233   unsigned long hash;
01234 
01235   entry = ((struct out_section_hash_entry *)
01236           bfd_hash_lookup (&output_section_statement_table, name,
01237                          TRUE, FALSE));
01238   if (entry == NULL)
01239     {
01240       einfo (_("%P%F: failed creating section `%s': %E\n"), name);
01241       return NULL;
01242     }
01243 
01244   if (entry->s.output_section_statement.name != NULL)
01245     {
01246       /* We have a section of this name, but it might not have the correct
01247         constraint.  */
01248       hash = entry->root.hash;
01249       do
01250        {
01251          if (entry->s.output_section_statement.constraint != -1
01252              && (constraint == 0
01253                 || (constraint == entry->s.output_section_statement.constraint
01254                     && constraint != SPECIAL)))
01255            return &entry->s.output_section_statement;
01256          last_ent = entry;
01257          entry = (struct out_section_hash_entry *) entry->root.next;
01258        }
01259       while (entry != NULL
01260             && entry->root.hash == hash
01261             && strcmp (name, entry->s.output_section_statement.name) == 0);
01262 
01263       entry
01264        = ((struct out_section_hash_entry *)
01265           output_section_statement_newfunc (NULL,
01266                                         &output_section_statement_table,
01267                                         name));
01268       if (entry == NULL)
01269        {
01270          einfo (_("%P%F: failed creating section `%s': %E\n"), name);
01271          return NULL;
01272        }
01273       entry->root = last_ent->root;
01274       last_ent->root.next = &entry->root;
01275     }
01276 
01277   entry->s.output_section_statement.name = name;
01278   entry->s.output_section_statement.constraint = constraint;
01279   return &entry->s.output_section_statement;
01280 }
01281 
01282 lang_output_section_statement_type *
01283 lang_output_section_statement_lookup (const char *const name)
01284 {
01285   return lang_output_section_statement_lookup_1 (name, 0);
01286 }
01287 
01288 /* A variant of lang_output_section_find used by place_orphan.
01289    Returns the output statement that should precede a new output
01290    statement for SEC.  If an exact match is found on certain flags,
01291    sets *EXACT too.  */
01292 
01293 lang_output_section_statement_type *
01294 lang_output_section_find_by_flags (const asection *sec,
01295                                lang_output_section_statement_type **exact,
01296                                lang_match_sec_type_func match_type)
01297 {
01298   lang_output_section_statement_type *first, *look, *found;
01299   flagword flags;
01300 
01301   /* We know the first statement on this list is *ABS*.  May as well
01302      skip it.  */
01303   first = &lang_output_section_statement.head->output_section_statement;
01304   first = first->next;
01305 
01306   /* First try for an exact match.  */
01307   found = NULL;
01308   for (look = first; look; look = look->next)
01309     {
01310       flags = look->flags;
01311       if (look->bfd_section != NULL)
01312        {
01313          flags = look->bfd_section->flags;
01314          if (match_type && !match_type (output_bfd, look->bfd_section,
01315                                     sec->owner, sec))
01316            continue;
01317        }
01318       flags ^= sec->flags;
01319       if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
01320                    | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
01321        found = look;
01322     }
01323   if (found != NULL)
01324     {
01325       if (exact != NULL)
01326        *exact = found;
01327       return found;
01328     }
01329 
01330   if (sec->flags & SEC_CODE)
01331     {
01332       /* Try for a rw code section.  */
01333       for (look = first; look; look = look->next)
01334        {
01335          flags = look->flags;
01336          if (look->bfd_section != NULL)
01337            {
01338              flags = look->bfd_section->flags;
01339              if (match_type && !match_type (output_bfd, look->bfd_section,
01340                                         sec->owner, sec))
01341               continue;
01342            }
01343          flags ^= sec->flags;
01344          if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
01345                       | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
01346            found = look;
01347        }
01348     }
01349   else if (sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL))
01350     {
01351       /* .rodata can go after .text, .sdata2 after .rodata.  */
01352       for (look = first; look; look = look->next)
01353        {
01354          flags = look->flags;
01355          if (look->bfd_section != NULL)
01356            {
01357              flags = look->bfd_section->flags;
01358              if (match_type && !match_type (output_bfd, look->bfd_section,
01359                                         sec->owner, sec))
01360               continue;
01361            }
01362          flags ^= sec->flags;
01363          if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
01364                       | SEC_READONLY))
01365              && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
01366            found = look;
01367        }
01368     }
01369   else if (sec->flags & SEC_SMALL_DATA)
01370     {
01371       /* .sdata goes after .data, .sbss after .sdata.  */
01372       for (look = first; look; look = look->next)
01373        {
01374          flags = look->flags;
01375          if (look->bfd_section != NULL)
01376            {
01377              flags = look->bfd_section->flags;
01378              if (match_type && !match_type (output_bfd, look->bfd_section,
01379                                         sec->owner, sec))
01380               continue;
01381            }
01382          flags ^= sec->flags;
01383          if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
01384                       | SEC_THREAD_LOCAL))
01385              || ((look->flags & SEC_SMALL_DATA)
01386                 && !(sec->flags & SEC_HAS_CONTENTS)))
01387            found = look;
01388        }
01389     }
01390   else if (sec->flags & SEC_HAS_CONTENTS)
01391     {
01392       /* .data goes after .rodata.  */
01393       for (look = first; look; look = look->next)
01394        {
01395          flags = look->flags;
01396          if (look->bfd_section != NULL)
01397            {
01398              flags = look->bfd_section->flags;
01399              if (match_type && !match_type (output_bfd, look->bfd_section,
01400                                         sec->owner, sec))
01401               continue;
01402            }
01403          flags ^= sec->flags;
01404          if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
01405                       | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
01406            found = look;
01407        }
01408     }
01409   else
01410     {
01411       /* .bss goes last.  */
01412       for (look = first; look; look = look->next)
01413        {
01414          flags = look->flags;
01415          if (look->bfd_section != NULL)
01416            {
01417              flags = look->bfd_section->flags;
01418              if (match_type && !match_type (output_bfd, look->bfd_section,
01419                                         sec->owner, sec))
01420               continue;
01421            }
01422          flags ^= sec->flags;
01423          if (!(flags & SEC_ALLOC))
01424            found = look;
01425        }
01426     }
01427 
01428   if (found || !match_type)
01429     return found;
01430 
01431   return lang_output_section_find_by_flags (sec, NULL, NULL);
01432 }
01433 
01434 /* Find the last output section before given output statement.
01435    Used by place_orphan.  */
01436 
01437 static asection *
01438 output_prev_sec_find (lang_output_section_statement_type *os)
01439 {
01440   lang_output_section_statement_type *lookup;
01441 
01442   for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
01443     {
01444       if (lookup->constraint == -1)
01445        continue;
01446 
01447       if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
01448        return lookup->bfd_section;
01449     }
01450 
01451   return NULL;
01452 }
01453 
01454 lang_output_section_statement_type *
01455 lang_insert_orphan (asection *s,
01456                   const char *secname,
01457                   lang_output_section_statement_type *after,
01458                   struct orphan_save *place,
01459                   etree_type *address,
01460                   lang_statement_list_type *add_child)
01461 {
01462   lang_statement_list_type *old;
01463   lang_statement_list_type add;
01464   const char *ps;
01465   lang_output_section_statement_type *os;
01466   lang_output_section_statement_type **os_tail;
01467 
01468   /* Start building a list of statements for this section.
01469      First save the current statement pointer.  */
01470   old = stat_ptr;
01471 
01472   /* If we have found an appropriate place for the output section
01473      statements for this orphan, add them to our own private list,
01474      inserting them later into the global statement list.  */
01475   if (after != NULL)
01476     {
01477       stat_ptr = &add;
01478       lang_list_init (stat_ptr);
01479     }
01480 
01481   ps = NULL;
01482   if (config.build_constructors)
01483     {
01484       /* If the name of the section is representable in C, then create
01485         symbols to mark the start and the end of the section.  */
01486       for (ps = secname; *ps != '\0'; ps++)
01487        if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
01488          break;
01489       if (*ps == '\0')
01490        {
01491          char *symname;
01492          etree_type *e_align;
01493 
01494          symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
01495          symname[0] = bfd_get_symbol_leading_char (output_bfd);
01496          sprintf (symname + (symname[0] != 0), "__start_%s", secname);
01497          e_align = exp_unop (ALIGN_K,
01498                            exp_intop ((bfd_vma) 1 << s->alignment_power));
01499          lang_add_assignment (exp_assop ('=', ".", e_align));
01500          lang_add_assignment (exp_provide (symname,
01501                                        exp_nameop (NAME, "."),
01502                                        FALSE));
01503        }
01504     }
01505 
01506   if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
01507     address = exp_intop (0);
01508 
01509   os_tail = ((lang_output_section_statement_type **)
01510             lang_output_section_statement.tail);
01511   os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
01512                                        NULL, 0);
01513 
01514   if (add_child == NULL)
01515     add_child = &os->children;
01516   lang_add_section (add_child, s, os);
01517 
01518   lang_leave_output_section_statement (0, "*default*", NULL, NULL);
01519 
01520   if (config.build_constructors && *ps == '\0')
01521     {
01522       char *symname;
01523 
01524       /* lang_leave_ouput_section_statement resets stat_ptr.
01525         Put stat_ptr back where we want it.  */
01526       if (after != NULL)
01527        stat_ptr = &add;
01528 
01529       symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
01530       symname[0] = bfd_get_symbol_leading_char (output_bfd);
01531       sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
01532       lang_add_assignment (exp_provide (symname,
01533                                    exp_nameop (NAME, "."),
01534                                    FALSE));
01535     }
01536 
01537   /* Restore the global list pointer.  */
01538   if (after != NULL)
01539     stat_ptr = old;
01540 
01541   if (after != NULL && os->bfd_section != NULL)
01542     {
01543       asection *snew, *as;
01544 
01545       snew = os->bfd_section;
01546 
01547       /* Shuffle the bfd section list to make the output file look
01548         neater.  This is really only cosmetic.  */
01549       if (place->section == NULL
01550          && after != (&lang_output_section_statement.head
01551                      ->output_section_statement))
01552        {
01553          asection *bfd_section = after->bfd_section;
01554 
01555          /* If the output statement hasn't been used to place any input
01556             sections (and thus doesn't have an output bfd_section),
01557             look for the closest prior output statement having an
01558             output section.  */
01559          if (bfd_section == NULL)
01560            bfd_section = output_prev_sec_find (after);
01561 
01562          if (bfd_section != NULL && bfd_section != snew)
01563            place->section = &bfd_section->next;
01564        }
01565 
01566       if (place->section == NULL)
01567        place->section = &output_bfd->sections;
01568 
01569       as = *place->section;
01570 
01571       if (!as)
01572        {
01573          /* Put the section at the end of the list.  */
01574 
01575          /* Unlink the section.  */
01576          bfd_section_list_remove (output_bfd, snew);
01577 
01578          /* Now tack it back on in the right place.  */
01579          bfd_section_list_append (output_bfd, snew);
01580        }
01581       else if (as != snew && as->prev != snew)
01582        {
01583          /* Unlink the section.  */
01584          bfd_section_list_remove (output_bfd, snew);
01585 
01586          /* Now tack it back on in the right place.  */
01587          bfd_section_list_insert_before (output_bfd, as, snew);
01588        }
01589 
01590       /* Save the end of this list.  Further ophans of this type will
01591         follow the one we've just added.  */
01592       place->section = &snew->next;
01593 
01594       /* The following is non-cosmetic.  We try to put the output
01595         statements in some sort of reasonable order here, because they
01596         determine the final load addresses of the orphan sections.
01597         In addition, placing output statements in the wrong order may
01598         require extra segments.  For instance, given a typical
01599         situation of all read-only sections placed in one segment and
01600         following that a segment containing all the read-write
01601         sections, we wouldn't want to place an orphan read/write
01602         section before or amongst the read-only ones.  */
01603       if (add.head != NULL)
01604        {
01605          lang_output_section_statement_type *newly_added_os;
01606 
01607          if (place->stmt == NULL)
01608            {
01609              lang_statement_union_type **where;
01610              lang_statement_union_type **assign = NULL;
01611              bfd_boolean ignore_first;
01612 
01613              /* Look for a suitable place for the new statement list.
01614                The idea is to skip over anything that might be inside
01615                a SECTIONS {} statement in a script, before we find
01616                another output_section_statement.  Assignments to "dot"
01617                before an output section statement are assumed to
01618                belong to it.  An exception to this rule is made for
01619                the first assignment to dot, otherwise we might put an
01620                orphan before . = . + SIZEOF_HEADERS or similar
01621                assignments that set the initial address.  */
01622 
01623              ignore_first = after == (&lang_output_section_statement.head
01624                                    ->output_section_statement);
01625              for (where = &after->header.next;
01626                  *where != NULL;
01627                  where = &(*where)->header.next)
01628               {
01629                 switch ((*where)->header.type)
01630                   {
01631                   case lang_assignment_statement_enum:
01632                     if (assign == NULL)
01633                      {
01634                        lang_assignment_statement_type *ass;
01635                        ass = &(*where)->assignment_statement;
01636                        if (ass->exp->type.node_class != etree_assert
01637                            && ass->exp->assign.dst[0] == '.'
01638                            && ass->exp->assign.dst[1] == 0
01639                            && !ignore_first)
01640                          assign = where;
01641                      }
01642                     ignore_first = FALSE;
01643                     continue;
01644                   case lang_wild_statement_enum:
01645                   case lang_input_section_enum:
01646                   case lang_object_symbols_statement_enum:
01647                   case lang_fill_statement_enum:
01648                   case lang_data_statement_enum:
01649                   case lang_reloc_statement_enum:
01650                   case lang_padding_statement_enum:
01651                   case lang_constructors_statement_enum:
01652                     assign = NULL;
01653                     continue;
01654                   case lang_output_section_statement_enum:
01655                     if (assign != NULL)
01656                      where = assign;
01657                   case lang_input_statement_enum:
01658                   case lang_address_statement_enum:
01659                   case lang_target_statement_enum:
01660                   case lang_output_statement_enum:
01661                   case lang_group_statement_enum:
01662                   case lang_afile_asection_pair_statement_enum:
01663                     break;
01664                   }
01665                 break;
01666               }
01667 
01668              *add.tail = *where;
01669              *where = add.head;
01670 
01671              place->os_tail = &after->next;
01672            }
01673          else
01674            {
01675              /* Put it after the last orphan statement we added.  */
01676              *add.tail = *place->stmt;
01677              *place->stmt = add.head;
01678            }
01679 
01680          /* Fix the global list pointer if we happened to tack our
01681             new list at the tail.  */
01682          if (*old->tail == add.head)
01683            old->tail = add.tail;
01684 
01685          /* Save the end of this list.  */
01686          place->stmt = add.tail;
01687 
01688          /* Do the same for the list of output section statements.  */
01689          newly_added_os = *os_tail;
01690          *os_tail = NULL;
01691          newly_added_os->prev = (lang_output_section_statement_type *)
01692            ((char *) place->os_tail
01693             - offsetof (lang_output_section_statement_type, next));
01694          newly_added_os->next = *place->os_tail;
01695          if (newly_added_os->next != NULL)
01696            newly_added_os->next->prev = newly_added_os;
01697          *place->os_tail = newly_added_os;
01698          place->os_tail = &newly_added_os->next;
01699 
01700          /* Fixing the global list pointer here is a little different.
01701             We added to the list in lang_enter_output_section_statement,
01702             trimmed off the new output_section_statment above when
01703             assigning *os_tail = NULL, but possibly added it back in
01704             the same place when assigning *place->os_tail.  */
01705          if (*os_tail == NULL)
01706            lang_output_section_statement.tail
01707              = (lang_statement_union_type **) os_tail;
01708        }
01709     }
01710   return os;
01711 }
01712 
01713 static void
01714 lang_map_flags (flagword flag)
01715 {
01716   if (flag & SEC_ALLOC)
01717     minfo ("a");
01718 
01719   if (flag & SEC_CODE)
01720     minfo ("x");
01721 
01722   if (flag & SEC_READONLY)
01723     minfo ("r");
01724 
01725   if (flag & SEC_DATA)
01726     minfo ("w");
01727 
01728   if (flag & SEC_LOAD)
01729     minfo ("l");
01730 }
01731 
01732 void
01733 lang_map (void)
01734 {
01735   lang_memory_region_type *m;
01736   bfd_boolean dis_header_printed = FALSE;
01737   bfd *p;
01738 
01739   LANG_FOR_EACH_INPUT_STATEMENT (file)
01740     {
01741       asection *s;
01742 
01743       if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
01744          || file->just_syms_flag)
01745        continue;
01746 
01747       for (s = file->the_bfd->sections; s != NULL; s = s->next)
01748        if ((s->output_section == NULL
01749             || s->output_section->owner != output_bfd)
01750            && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
01751          {
01752            if (! dis_header_printed)
01753              {
01754               fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
01755               dis_header_printed = TRUE;
01756              }
01757 
01758            print_input_section (s);
01759          }
01760     }
01761 
01762   minfo (_("\nMemory Configuration\n\n"));
01763   fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
01764           _("Name"), _("Origin"), _("Length"), _("Attributes"));
01765 
01766   for (m = lang_memory_region_list; m != NULL; m = m->next)
01767     {
01768       char buf[100];
01769       int len;
01770 
01771       fprintf (config.map_file, "%-16s ", m->name);
01772 
01773       sprintf_vma (buf, m->origin);
01774       minfo ("0x%s ", buf);
01775       len = strlen (buf);
01776       while (len < 16)
01777        {
01778          print_space ();
01779          ++len;
01780        }
01781 
01782       minfo ("0x%V", m->length);
01783       if (m->flags || m->not_flags)
01784        {
01785 #ifndef BFD64
01786          minfo ("        ");
01787 #endif
01788          if (m->flags)
01789            {
01790              print_space ();
01791              lang_map_flags (m->flags);
01792            }
01793 
01794          if (m->not_flags)
01795            {
01796              minfo (" !");
01797              lang_map_flags (m->not_flags);
01798            }
01799        }
01800 
01801       print_nl ();
01802     }
01803 
01804   fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
01805 
01806   if (! link_info.reduce_memory_overheads)
01807     {
01808       obstack_begin (&map_obstack, 1000);
01809       for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
01810        bfd_map_over_sections (p, init_map_userdata, 0);
01811       bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
01812     }
01813   print_statements ();
01814 }
01815 
01816 static void
01817 init_map_userdata (abfd, sec, data)
01818      bfd *abfd ATTRIBUTE_UNUSED;
01819      asection *sec;
01820      void *data ATTRIBUTE_UNUSED;
01821 {
01822   fat_section_userdata_type *new_data
01823     = ((fat_section_userdata_type *) (stat_alloc
01824                                   (sizeof (fat_section_userdata_type))));
01825 
01826   ASSERT (get_userdata (sec) == NULL);
01827   get_userdata (sec) = new_data;
01828   new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
01829 }
01830 
01831 static bfd_boolean
01832 sort_def_symbol (hash_entry, info)
01833      struct bfd_link_hash_entry *hash_entry;
01834      void *info ATTRIBUTE_UNUSED;
01835 {
01836   if (hash_entry->type == bfd_link_hash_defined
01837       || hash_entry->type == bfd_link_hash_defweak)
01838     {
01839       struct fat_user_section_struct *ud;
01840       struct map_symbol_def *def;
01841 
01842       ud = get_userdata (hash_entry->u.def.section);
01843       if  (! ud)
01844        {
01845          /* ??? What do we have to do to initialize this beforehand?  */
01846          /* The first time we get here is bfd_abs_section...  */
01847          init_map_userdata (0, hash_entry->u.def.section, 0);
01848          ud = get_userdata (hash_entry->u.def.section);
01849        }
01850       else if  (!ud->map_symbol_def_tail)
01851        ud->map_symbol_def_tail = &ud->map_symbol_def_head;
01852 
01853       def = obstack_alloc (&map_obstack, sizeof *def);
01854       def->entry = hash_entry;
01855       *(ud->map_symbol_def_tail) = def;
01856       ud->map_symbol_def_tail = &def->next;
01857     }
01858   return TRUE;
01859 }
01860 
01861 /* Initialize an output section.  */
01862 
01863 static void
01864 init_os (lang_output_section_statement_type *s, asection *isec,
01865         flagword flags)
01866 {
01867   if (s->bfd_section != NULL)
01868     return;
01869 
01870   if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
01871     einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
01872 
01873   s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
01874   if (s->bfd_section == NULL)
01875     s->bfd_section = bfd_make_section_with_flags (output_bfd, s->name,
01876                                             flags);
01877   if (s->bfd_section == NULL)
01878     {
01879       einfo (_("%P%F: output format %s cannot represent section called %s\n"),
01880             output_bfd->xvec->name, s->name);
01881     }
01882   s->bfd_section->output_section = s->bfd_section;
01883   s->bfd_section->output_offset = 0;
01884 
01885   if (!link_info.reduce_memory_overheads)
01886     {
01887       fat_section_userdata_type *new
01888        = stat_alloc (sizeof (fat_section_userdata_type));
01889       memset (new, 0, sizeof (fat_section_userdata_type));
01890       get_userdata (s->bfd_section) = new;
01891     }
01892 
01893   /* If there is a base address, make sure that any sections it might
01894      mention are initialized.  */
01895   if (s->addr_tree != NULL)
01896     exp_init_os (s->addr_tree);
01897 
01898   if (s->load_base != NULL)
01899     exp_init_os (s->load_base);
01900 
01901   /* If supplied an alignment, set it.  */
01902   if (s->section_alignment != -1)
01903     s->bfd_section->alignment_power = s->section_alignment;
01904 
01905   if (isec)
01906     bfd_init_private_section_data (isec->owner, isec,
01907                                output_bfd, s->bfd_section,
01908                                &link_info);
01909 }
01910 
01911 /* Make sure that all output sections mentioned in an expression are
01912    initialized.  */
01913 
01914 static void
01915 exp_init_os (etree_type *exp)
01916 {
01917   switch (exp->type.node_class)
01918     {
01919     case etree_assign:
01920     case etree_provide:
01921       exp_init_os (exp->assign.src);
01922       break;
01923 
01924     case etree_binary:
01925       exp_init_os (exp->binary.lhs);
01926       exp_init_os (exp->binary.rhs);
01927       break;
01928 
01929     case etree_trinary:
01930       exp_init_os (exp->trinary.cond);
01931       exp_init_os (exp->trinary.lhs);
01932       exp_init_os (exp->trinary.rhs);
01933       break;
01934 
01935     case etree_assert:
01936       exp_init_os (exp->assert_s.child);
01937       break;
01938 
01939     case etree_unary:
01940       exp_init_os (exp->unary.child);
01941       break;
01942 
01943     case etree_name:
01944       switch (exp->type.node_code)
01945        {
01946        case ADDR:
01947        case LOADADDR:
01948        case SIZEOF:
01949          {
01950            lang_output_section_statement_type *os;
01951 
01952            os = lang_output_section_find (exp->name.name);
01953            if (os != NULL && os->bfd_section == NULL)
01954              init_os (os, NULL, 0);
01955          }
01956        }
01957       break;
01958 
01959     default:
01960       break;
01961     }
01962 }
01963 
01964 static void
01965 section_already_linked (bfd *abfd, asection *sec, void *data)
01966 {
01967   lang_input_statement_type *entry = data;
01968 
01969   /* If we are only reading symbols from this object, then we want to
01970      discard all sections.  */
01971   if (entry->just_syms_flag)
01972     {
01973       bfd_link_just_syms (abfd, sec, &link_info);
01974       return;
01975     }
01976 
01977   if (!(abfd->flags & DYNAMIC))
01978     bfd_section_already_linked (abfd, sec, &link_info);
01979 }
01980 
01981 /* The wild routines.
01982 
01983    These expand statements like *(.text) and foo.o to a list of
01984    explicit actions, like foo.o(.text), bar.o(.text) and
01985    foo.o(.text, .data).  */
01986 
01987 /* Add SECTION to the output section OUTPUT.  Do this by creating a
01988    lang_input_section statement which is placed at PTR.  FILE is the
01989    input file which holds SECTION.  */
01990 
01991 void
01992 lang_add_section (lang_statement_list_type *ptr,
01993                 asection *section,
01994                 lang_output_section_statement_type *output)
01995 {
01996   flagword flags = section->flags;
01997   bfd_boolean discard;
01998 
01999   /* Discard sections marked with SEC_EXCLUDE.  */
02000   discard = (flags & SEC_EXCLUDE) != 0;
02001 
02002   /* Discard input sections which are assigned to a section named
02003      DISCARD_SECTION_NAME.  */
02004   if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
02005     discard = TRUE;
02006 
02007   /* Discard debugging sections if we are stripping debugging
02008      information.  */
02009   if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
02010       && (flags & SEC_DEBUGGING) != 0)
02011     discard = TRUE;
02012 
02013   if (discard)
02014     {
02015       if (section->output_section == NULL)
02016        {
02017          /* This prevents future calls from assigning this section.  */
02018          section->output_section = bfd_abs_section_ptr;
02019        }
02020       return;
02021     }
02022 
02023   if (section->output_section == NULL)
02024     {
02025       bfd_boolean first;
02026       lang_input_section_type *new;
02027       flagword flags;
02028 
02029       flags = section->flags;
02030 
02031       /* We don't copy the SEC_NEVER_LOAD flag from an input section
02032         to an output section, because we want to be able to include a
02033         SEC_NEVER_LOAD section in the middle of an otherwise loaded
02034         section (I don't know why we want to do this, but we do).
02035         build_link_order in ldwrite.c handles this case by turning
02036         the embedded SEC_NEVER_LOAD section into a fill.  */
02037 
02038       flags &= ~ SEC_NEVER_LOAD;
02039 
02040       switch (output->sectype)
02041        {
02042        case normal_section:
02043          break;
02044        case noalloc_section:
02045          flags &= ~SEC_ALLOC;
02046          break;
02047        case noload_section:
02048          flags &= ~SEC_LOAD;
02049          flags |= SEC_NEVER_LOAD;
02050          break;
02051        }
02052 
02053       if (output->bfd_section == NULL)
02054        init_os (output, section, flags);
02055 
02056       first = ! output->bfd_section->linker_has_input;
02057       output->bfd_section->linker_has_input = 1;
02058 
02059       if (!link_info.relocatable
02060          && !stripped_excluded_sections)
02061        {
02062          asection *s = output->bfd_section->map_tail.s;
02063          output->bfd_section->map_tail.s = section;
02064          section->map_head.s = NULL;
02065          section->map_tail.s = s;
02066          if (s != NULL)
02067            s->map_head.s = section;
02068          else
02069            output->bfd_section->map_head.s = section;
02070        }
02071 
02072       /* Add a section reference to the list.  */
02073       new = new_stat (lang_input_section, ptr);
02074 
02075       new->section = section;
02076       section->output_section = output->bfd_section;
02077 
02078       /* If final link, don't copy the SEC_LINK_ONCE flags, they've
02079         already been processed.  One reason to do this is that on pe
02080         format targets, .text$foo sections go into .text and it's odd
02081         to see .text with SEC_LINK_ONCE set.  */
02082 
02083       if (! link_info.relocatable)
02084        flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
02085 
02086       /* If this is not the first input section, and the SEC_READONLY
02087         flag is not currently set, then don't set it just because the
02088         input section has it set.  */
02089 
02090       if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
02091        flags &= ~ SEC_READONLY;
02092 
02093       /* Keep SEC_MERGE and SEC_STRINGS only if they are the same.  */
02094       if (! first
02095          && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
02096              != (flags & (SEC_MERGE | SEC_STRINGS))
02097              || ((flags & SEC_MERGE)
02098                 && output->bfd_section->entsize != section->entsize)))
02099        {
02100          output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
02101          flags &= ~ (SEC_MERGE | SEC_STRINGS);
02102        }
02103 
02104       output->bfd_section->flags |= flags;
02105 
02106       if (flags & SEC_MERGE)
02107        output->bfd_section->entsize = section->entsize;
02108 
02109       /* If SEC_READONLY is not set in the input section, then clear
02110         it from the output section.  */
02111       if ((section->flags & SEC_READONLY) == 0)
02112        output->bfd_section->flags &= ~SEC_READONLY;
02113 
02114       /* Copy over SEC_SMALL_DATA.  */
02115       if (section->flags & SEC_SMALL_DATA)
02116        output->bfd_section->flags |= SEC_SMALL_DATA;
02117 
02118       if (section->alignment_power > output->bfd_section->alignment_power)
02119        output->bfd_section->alignment_power = section->alignment_power;
02120 
02121       if (bfd_get_arch (section->owner) == bfd_arch_tic54x
02122          && (section->flags & SEC_TIC54X_BLOCK) != 0)
02123        {
02124          output->bfd_section->flags |= SEC_TIC54X_BLOCK;
02125          /* FIXME: This value should really be obtained from the bfd...  */
02126          output->block_value = 128;
02127        }
02128     }
02129 }
02130 
02131 /* Handle wildcard sorting.  This returns the lang_input_section which
02132    should follow the one we are going to create for SECTION and FILE,
02133    based on the sorting requirements of WILD.  It returns NULL if the
02134    new section should just go at the end of the current list.  */
02135 
02136 static lang_statement_union_type *
02137 wild_sort (lang_wild_statement_type *wild,
02138           struct wildcard_list *sec,
02139           lang_input_statement_type *file,
02140           asection *section)
02141 {
02142   const char *section_name;
02143   lang_statement_union_type *l;
02144 
02145   if (!wild->filenames_sorted
02146       && (sec == NULL || sec->spec.sorted == none))
02147     return NULL;
02148 
02149   section_name = bfd_get_section_name (file->the_bfd, section);
02150   for (l = wild->children.head; l != NULL; l = l->header.next)
02151     {
02152       lang_input_section_type *ls;
02153 
02154       if (l->header.type != lang_input_section_enum)
02155        continue;
02156       ls = &l->input_section;
02157 
02158       /* Sorting by filename takes precedence over sorting by section
02159         name.  */
02160 
02161       if (wild->filenames_sorted)
02162        {
02163          const char *fn, *ln;
02164          bfd_boolean fa, la;
02165          int i;
02166 
02167          /* The PE support for the .idata section as generated by
02168             dlltool assumes that files will be sorted by the name of
02169             the archive and then the name of the file within the
02170             archive.  */
02171 
02172          if (file->the_bfd != NULL
02173              && bfd_my_archive (file->the_bfd) != NULL)
02174            {
02175              fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
02176              fa = TRUE;
02177            }
02178          else
02179            {
02180              fn = file->filename;
02181              fa = FALSE;
02182            }
02183 
02184          if (bfd_my_archive (ls->section->owner) != NULL)
02185            {
02186              ln = bfd_get_filename (bfd_my_archive (ls->section->owner));
02187              la = TRUE;
02188            }
02189          else
02190            {
02191              ln = ls->section->owner->filename;
02192              la = FALSE;
02193            }
02194 
02195          i = strcmp (fn, ln);
02196          if (i > 0)
02197            continue;
02198          else if (i < 0)
02199            break;
02200 
02201          if (fa || la)
02202            {
02203              if (fa)
02204               fn = file->filename;
02205              if (la)
02206               ln = ls->section->owner->filename;
02207 
02208              i = strcmp (fn, ln);
02209              if (i > 0)
02210               continue;
02211              else if (i < 0)
02212               break;
02213            }
02214        }
02215 
02216       /* Here either the files are not sorted by name, or we are
02217         looking at the sections for this file.  */
02218 
02219       if (sec != NULL && sec->spec.sorted != none)
02220        if (compare_section (sec->spec.sorted, section, ls->section) < 0)
02221          break;
02222     }
02223 
02224   return l;
02225 }
02226 
02227 /* Expand a wild statement for a particular FILE.  SECTION may be
02228    NULL, in which case it is a wild card.  */
02229 
02230 static void
02231 output_section_callback (lang_wild_statement_type *ptr,
02232                       struct wildcard_list *sec,
02233                       asection *section,
02234                       lang_input_statement_type *file,
02235                       void *output)
02236 {
02237   lang_statement_union_type *before;
02238 
02239   /* Exclude sections that match UNIQUE_SECTION_LIST.  */
02240   if (unique_section_p (section))
02241     return;
02242 
02243   before = wild_sort (ptr, sec, file, section);
02244 
02245   /* Here BEFORE points to the lang_input_section which
02246      should follow the one we are about to add.  If BEFORE
02247      is NULL, then the section should just go at the end
02248      of the current list.  */
02249 
02250   if (before == NULL)
02251     lang_add_section (&ptr->children, section,
02252                     (lang_output_section_statement_type *) output);
02253   else
02254     {
02255       lang_statement_list_type list;
02256       lang_statement_union_type **pp;
02257 
02258       lang_list_init (&list);
02259       lang_add_section (&list, section,
02260                      (lang_output_section_statement_type *) output);
02261 
02262       /* If we are discarding the section, LIST.HEAD will
02263         be NULL.  */
02264       if (list.head != NULL)
02265        {
02266          ASSERT (list.head->header.next == NULL);
02267 
02268          for (pp = &ptr->children.head;
02269               *pp != before;
02270               pp = &(*pp)->header.next)
02271            ASSERT (*pp != NULL);
02272 
02273          list.head->header.next = *pp;
02274          *pp = list.head;
02275        }
02276     }
02277 }
02278 
02279 /* Check if all sections in a wild statement for a particular FILE
02280    are readonly.  */
02281 
02282 static void
02283 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
02284                      struct wildcard_list *sec ATTRIBUTE_UNUSED,
02285                      asection *section,
02286                      lang_input_statement_type *file ATTRIBUTE_UNUSED,
02287                      void *data)
02288 {
02289   /* Exclude sections that match UNIQUE_SECTION_LIST.  */
02290   if (unique_section_p (section))
02291     return;
02292 
02293   if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
02294     ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
02295 }
02296 
02297 /* This is passed a file name which must have been seen already and
02298    added to the statement tree.  We will see if it has been opened
02299    already and had its symbols read.  If not then we'll read it.  */
02300 
02301 static lang_input_statement_type *
02302 lookup_name (const char *name)
02303 {
02304   lang_input_statement_type *search;
02305 
02306   for (search = (lang_input_statement_type *) input_file_chain.head;
02307        search != NULL;
02308        search = (lang_input_statement_type *) search->next_real_file)
02309     {
02310       /* Use the local_sym_name as the name of the file that has
02311         already been loaded as filename might have been transformed
02312         via the search directory lookup mechanism.  */
02313       const char *filename = search->local_sym_name;
02314 
02315       if (filename != NULL
02316          && strcmp (filename, name) == 0)
02317        break;
02318     }
02319 
02320   if (search == NULL)
02321     search = new_afile (name, lang_input_file_is_search_file_enum,
02322                      default_target, FALSE);
02323 
02324   /* If we have already added this file, or this file is not real
02325      don't add this file.  */
02326   if (search->loaded || !search->real)
02327     return search;
02328 
02329   if (! load_symbols (search, NULL))
02330     return NULL;
02331 
02332   return search;
02333 }
02334 
02335 /* Save LIST as a list of libraries whose symbols should not be exported.  */
02336 
02337 struct excluded_lib
02338 {
02339   char *name;
02340   struct excluded_lib *next;
02341 };
02342 static struct excluded_lib *excluded_libs;
02343 
02344 void
02345 add_excluded_libs (const char *list)
02346 {
02347   const char *p = list, *end;
02348 
02349   while (*p != '\0')
02350     {
02351       struct excluded_lib *entry;
02352       end = strpbrk (p, ",:");
02353       if (end == NULL)
02354        end = p + strlen (p);
02355       entry = xmalloc (sizeof (*entry));
02356       entry->next = excluded_libs;
02357       entry->name = xmalloc (end - p + 1);
02358       memcpy (entry->name, p, end - p);
02359       entry->name[end - p] = '\0';
02360       excluded_libs = entry;
02361       if (*end == '\0')
02362        break;
02363       p = end + 1;
02364     }
02365 }
02366 
02367 static void
02368 check_excluded_libs (bfd *abfd)
02369 {
02370   struct excluded_lib *lib = excluded_libs;
02371 
02372   while (lib)
02373     {
02374       int len = strlen (lib->name);
02375       const char *filename = lbasename (abfd->filename);
02376 
02377       if (strcmp (lib->name, "ALL") == 0)
02378        {
02379          abfd->no_export = TRUE;
02380          return;
02381        }
02382 
02383       if (strncmp (lib->name, filename, len) == 0
02384          && (filename[len] == '\0'
02385              || (filename[len] == '.' && filename[len + 1] == 'a'
02386                 && filename[len + 2] == '\0')))
02387        {
02388          abfd->no_export = TRUE;
02389          return;
02390        }
02391 
02392       lib = lib->next;
02393     }
02394 }
02395 
02396 /* Get the symbols for an input file.  */
02397 
02398 bfd_boolean
02399 load_symbols (lang_input_statement_type *entry,
02400              lang_statement_list_type *place)
02401 {
02402   char **matching;
02403 
02404   if (entry->loaded)
02405     return TRUE;
02406 
02407   ldfile_open_file (entry);
02408 
02409   if (! bfd_check_format (entry->the_bfd, bfd_archive)
02410       && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
02411     {
02412       bfd_error_type err;
02413       lang_statement_list_type *hold;
02414       bfd_boolean bad_load = TRUE;
02415       bfd_boolean save_ldlang_sysrooted_script;
02416       bfd_boolean save_as_needed, save_add_needed;
02417 
02418       err = bfd_get_error ();
02419 
02420       /* See if the emulation has some special knowledge.  */
02421       if (ldemul_unrecognized_file (entry))
02422        return TRUE;
02423 
02424       if (err == bfd_error_file_ambiguously_recognized)
02425        {
02426          char **p;
02427 
02428          einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
02429          einfo (_("%B: matching formats:"), entry->the_bfd);
02430          for (p = matching; *p != NULL; p++)
02431            einfo (" %s", *p);
02432          einfo ("%F\n");
02433        }
02434       else if (err != bfd_error_file_not_recognized
02435               || place == NULL)
02436          einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
02437       else
02438        bad_load = FALSE;
02439 
02440       bfd_close (entry->the_bfd);
02441       entry->the_bfd = NULL;
02442 
02443       /* Try to interpret the file as a linker script.  */
02444       ldfile_open_command_file (entry->filename);
02445 
02446       hold = stat_ptr;
02447       stat_ptr = place;
02448       save_ldlang_sysrooted_script = ldlang_sysrooted_script;
02449       ldlang_sysrooted_script = entry->sysrooted;
02450       save_as_needed = as_needed;
02451       as_needed = entry->as_needed;
02452       save_add_needed = add_needed;
02453       add_needed = entry->add_needed;
02454 
02455       ldfile_assumed_script = TRUE;
02456       parser_input = input_script;
02457       /* We want to use the same -Bdynamic/-Bstatic as the one for
02458         ENTRY.  */
02459       config.dynamic_link = entry->dynamic;
02460       yyparse ();
02461       ldfile_assumed_script = FALSE;
02462 
02463       ldlang_sysrooted_script = save_ldlang_sysrooted_script;
02464       as_needed = save_as_needed;
02465       add_needed = save_add_needed;
02466       stat_ptr = hold;
02467 
02468       return ! bad_load;
02469     }
02470 
02471   if (ldemul_recognized_file (entry))
02472     return TRUE;
02473 
02474   /* We don't call ldlang_add_file for an archive.  Instead, the
02475      add_symbols entry point will call ldlang_add_file, via the
02476      add_archive_element callback, for each element of the archive
02477      which is used.  */
02478   switch (bfd_get_format (entry->the_bfd))
02479     {
02480     default:
02481       break;
02482 
02483     case bfd_object:
02484       ldlang_add_file (entry);
02485       if (trace_files || trace_file_tries)
02486        info_msg ("%I\n", entry);
02487       break;
02488 
02489     case bfd_archive:
02490       check_excluded_libs (entry->the_bfd);
02491 
02492       if (entry->whole_archive)
02493        {
02494          bfd *member = NULL;
02495          bfd_boolean loaded = TRUE;
02496 
02497          for (;;)
02498            {
02499              member = bfd_openr_next_archived_file (entry->the_bfd, member);
02500 
02501              if (member == NULL)
02502               break;
02503 
02504              if (! bfd_check_format (member, bfd_object))
02505               {
02506                 einfo (_("%F%B: member %B in archive is not an object\n"),
02507                       entry->the_bfd, member);
02508                 loaded = FALSE;
02509               }
02510 
02511              if (! ((*link_info.callbacks->add_archive_element)
02512                    (&link_info, member, "--whole-archive")))
02513               abort ();
02514 
02515              if (! bfd_link_add_symbols (member, &link_info))
02516               {
02517                 einfo (_("%F%B: could not read symbols: %E\n"), member);
02518                 loaded = FALSE;
02519               }
02520            }
02521 
02522          entry->loaded = loaded;
02523          return loaded;
02524        }
02525       break;
02526     }
02527 
02528   if (bfd_link_add_symbols (entry->the_bfd, &link_info))
02529     entry->loaded = TRUE;
02530   else
02531     einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
02532 
02533   return entry->loaded;
02534 }
02535 
02536 /* Handle a wild statement.  S->FILENAME or S->SECTION_LIST or both
02537    may be NULL, indicating that it is a wildcard.  Separate
02538    lang_input_section statements are created for each part of the
02539    expansion; they are added after the wild statement S.  OUTPUT is
02540    the output section.  */
02541 
02542 static void
02543 wild (lang_wild_statement_type *s,
02544       const char *target ATTRIBUTE_UNUSED,
02545       lang_output_section_statement_type *output)
02546 {
02547   struct wildcard_list *sec;
02548 
02549   if (s->handler_data[0]
02550       && s->handler_data[0]->spec.sorted == by_name
02551       && !s->filenames_sorted)
02552     {
02553       lang_section_bst_type *tree;
02554 
02555       walk_wild (s, output_section_callback_fast, output);
02556 
02557       tree = s->tree;
02558       if (tree)
02559        {
02560          output_section_callback_tree_to_list (s, tree, output);
02561          s->tree = NULL;
02562        }
02563     }
02564   else
02565     walk_wild (s, output_section_callback, output);
02566 
02567   if (default_common_section == NULL)
02568     for (sec = s->section_list; sec != NULL; sec = sec->next)
02569       if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
02570        {
02571          /* Remember the section that common is going to in case we
02572             later get something which doesn't know where to put it.  */
02573          default_common_section = output;
02574          break;
02575        }
02576 }
02577 
02578 /* Return TRUE iff target is the sought target.  */
02579 
02580 static int
02581 get_target (const bfd_target *target, void *data)
02582 {
02583   const char *sought = data;
02584 
02585   return strcmp (target->name, sought) == 0;
02586 }
02587 
02588 /* Like strcpy() but convert to lower case as well.  */
02589 
02590 static void
02591 stricpy (char *dest, char *src)
02592 {
02593   char c;
02594 
02595   while ((c = *src++) != 0)
02596     *dest++ = TOLOWER (c);
02597 
02598   *dest = 0;
02599 }
02600 
02601 /* Remove the first occurrence of needle (if any) in haystack
02602    from haystack.  */
02603 
02604 static void
02605 strcut (char *haystack, char *needle)
02606 {
02607   haystack = strstr (haystack, needle);
02608 
02609   if (haystack)
02610     {
02611       char *src;
02612 
02613       for (src = haystack + strlen (needle); *src;)
02614        *haystack++ = *src++;
02615 
02616       *haystack = 0;
02617     }
02618 }
02619 
02620 /* Compare two target format name strings.
02621    Return a value indicating how "similar" they are.  */
02622 
02623 static int
02624 name_compare (char *first, char *second)
02625 {
02626   char *copy1;
02627   char *copy2;
02628   int result;
02629 
02630   copy1 = xmalloc (strlen (first) + 1);
02631   copy2 = xmalloc (strlen (second) + 1);
02632 
02633   /* Convert the names to lower case.  */
02634   stricpy (copy1, first);
02635   stricpy (copy2, second);
02636 
02637   /* Remove size and endian strings from the name.  */
02638   strcut (copy1, "big");
02639   strcut (copy1, "little");
02640   strcut (copy2, "big");
02641   strcut (copy2, "little");
02642 
02643   /* Return a value based on how many characters match,
02644      starting from the beginning.   If both strings are
02645      the same then return 10 * their length.  */
02646   for (result = 0; copy1[result] == copy2[result]; result++)
02647     if (copy1[result] == 0)
02648       {
02649        result *= 10;
02650        break;
02651       }
02652 
02653   free (copy1);
02654   free (copy2);
02655 
02656   return result;
02657 }
02658 
02659 /* Set by closest_target_match() below.  */
02660 static const bfd_target *winner;
02661 
02662 /* Scan all the valid bfd targets looking for one that has the endianness
02663    requirement that was specified on the command line, and is the nearest
02664    match to the original output target.  */
02665 
02666 static int
02667 closest_target_match (const bfd_target *target, void *data)
02668 {
02669   const bfd_target *original = data;
02670 
02671   if (command_line.endian == ENDIAN_BIG
02672       && target->byteorder != BFD_ENDIAN_BIG)
02673     return 0;
02674 
02675   if (command_line.endian == ENDIAN_LITTLE
02676       && target->byteorder != BFD_ENDIAN_LITTLE)
02677     return 0;
02678 
02679   /* Must be the same flavour.  */
02680   if (target->flavour != original->flavour)
02681     return 0;
02682 
02683   /* If we have not found a potential winner yet, then record this one.  */
02684   if (winner == NULL)
02685     {
02686       winner = target;
02687       return 0;
02688     }
02689 
02690   /* Oh dear, we now have two potential candidates for a successful match.
02691      Compare their names and choose the better one.  */
02692   if (name_compare (target->name, original->name)
02693       > name_compare (winner->name, original->name))
02694     winner = target;
02695 
02696   /* Keep on searching until wqe have checked them all.  */
02697   return 0;
02698 }
02699 
02700 /* Return the BFD target format of the first input file.  */
02701 
02702 static char *
02703 get_first_input_target (void)
02704 {
02705   char *target = NULL;
02706 
02707   LANG_FOR_EACH_INPUT_STATEMENT (s)
02708     {
02709       if (s->header.type == lang_input_statement_enum
02710          && s->real)
02711        {
02712          ldfile_open_file (s);
02713 
02714          if (s->the_bfd != NULL
02715              && bfd_check_format (s->the_bfd, bfd_object))
02716            {
02717              target = bfd_get_target (s->the_bfd);
02718 
02719              if (target != NULL)
02720               break;
02721            }
02722        }
02723     }
02724 
02725   return target;
02726 }
02727 
02728 const char *
02729 lang_get_output_target (void)
02730 {
02731   const char *target;
02732 
02733   /* Has the user told us which output format to use?  */
02734   if (output_target != NULL)
02735     return output_target;
02736 
02737   /* No - has the current target been set to something other than
02738      the default?  */
02739   if (current_target != default_target)
02740     return current_target;
02741 
02742   /* No - can we determine the format of the first input file?  */
02743   target = get_first_input_target ();
02744   if (target != NULL)
02745     return target;
02746 
02747   /* Failed - use the default output target.  */
02748   return default_target;
02749 }
02750 
02751 /* Open the output file.  */
02752 
02753 static bfd *
02754 open_output (const char *name)
02755 {
02756   bfd *output;
02757 
02758   output_target = lang_get_output_target ();
02759 
02760   /* Has the user requested a particular endianness on the command
02761      line?  */
02762   if (command_line.endian != ENDIAN_UNSET)
02763     {
02764       const bfd_target *target;
02765       enum bfd_endian desired_endian;
02766 
02767       /* Get the chosen target.  */
02768       target = bfd_search_for_target (get_target, (void *) output_target);
02769 
02770       /* If the target is not supported, we cannot do anything.  */
02771       if (target != NULL)
02772        {
02773          if (command_line.endian == ENDIAN_BIG)
02774            desired_endian = BFD_ENDIAN_BIG;
02775          else
02776            desired_endian = BFD_ENDIAN_LITTLE;
02777 
02778          /* See if the target has the wrong endianness.  This should
02779             not happen if the linker script has provided big and
02780             little endian alternatives, but some scrips don't do
02781             this.  */
02782          if (target->byteorder != desired_endian)
02783            {
02784              /* If it does, then see if the target provides
02785                an alternative with the correct endianness.  */
02786              if (target->alternative_target != NULL
02787                 && (target->alternative_target->byteorder == desired_endian))
02788               output_target = target->alternative_target->name;
02789              else
02790               {
02791                 /* Try to find a target as similar as possible to
02792                    the default target, but which has the desired
02793                    endian characteristic.  */
02794                 bfd_search_for_target (closest_target_match,
02795                                     (void *) target);
02796 
02797                 /* Oh dear - we could not find any targets that
02798                    satisfy our requirements.  */
02799                 if (winner == NULL)
02800                   einfo (_("%P: warning: could not find any targets"
02801                           " that match endianness requirement\n"));
02802                 else
02803                   output_target = winner->name;
02804               }
02805            }
02806        }
02807     }
02808 
02809   output = bfd_openw (name, output_target);
02810 
02811   if (output == NULL)
02812     {
02813       if (bfd_get_error () == bfd_error_invalid_target)
02814        einfo (_("%P%F: target %s not found\n"), output_target);
02815 
02816       einfo (_("%P%F: cannot open output file %s: %E\n"), name);
02817     }
02818 
02819   delete_output_file_on_failure = TRUE;
02820 
02821   if (! bfd_set_format (output, bfd_object))
02822     einfo (_("%P%F:%s: can not make object file: %E\n"), name);
02823   if (! bfd_set_arch_mach (output,
02824                         ldfile_output_architecture,
02825                         ldfile_output_machine))
02826     einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
02827 
02828   link_info.hash = bfd_link_hash_table_create (output);
02829   if (link_info.hash == NULL)
02830     einfo (_("%P%F: can not create hash table: %E\n"));
02831 
02832   bfd_set_gp_size (output, g_switch_value);
02833   return output;
02834 }
02835 
02836 static void
02837 ldlang_open_output (lang_statement_union_type *statement)
02838 {
02839   switch (statement->header.type)
02840     {
02841     case lang_output_statement_enum:
02842       ASSERT (output_bfd == NULL);
02843       output_bfd = open_output (statement->output_statement.name);
02844       ldemul_set_output_arch ();
02845       if (config.magic_demand_paged && !link_info.relocatable)
02846        output_bfd->flags |= D_PAGED;
02847       else
02848        output_bfd->flags &= ~D_PAGED;
02849       if (config.text_read_only)
02850        output_bfd->flags |= WP_TEXT;
02851       else
02852        output_bfd->flags &= ~WP_TEXT;
02853       if (link_info.traditional_format)
02854        output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
02855       else
02856        output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
02857       break;
02858 
02859     case lang_target_statement_enum:
02860       current_target = statement->target_statement.target;
02861       break;
02862     default:
02863       break;
02864     }
02865 }
02866 
02867 /* Convert between addresses in bytes and sizes in octets.
02868    For currently supported targets, octets_per_byte is always a power
02869    of two, so we can use shifts.  */
02870 #define TO_ADDR(X) ((X) >> opb_shift)
02871 #define TO_SIZE(X) ((X) << opb_shift)
02872 
02873 /* Support the above.  */
02874 static unsigned int opb_shift = 0;
02875 
02876 static void
02877 init_opb (void)
02878 {
02879   unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
02880                                          ldfile_output_machine);
02881   opb_shift = 0;
02882   if (x > 1)
02883     while ((x & 1) == 0)
02884       {
02885        x >>= 1;
02886        ++opb_shift;
02887       }
02888   ASSERT (x == 1);
02889 }
02890 
02891 /* Open all the input files.  */
02892 
02893 static void
02894 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
02895 {
02896   for (; s != NULL; s = s->header.next)
02897     {
02898       switch (s->header.type)
02899        {
02900        case lang_constructors_statement_enum:
02901          open_input_bfds (constructor_list.head, force);
02902          break;
02903        case lang_output_section_statement_enum:
02904          open_input_bfds (s->output_section_statement.children.head, force);
02905          break;
02906        case lang_wild_statement_enum:
02907          /* Maybe we should load the file's symbols.  */
02908          if (s->wild_statement.filename
02909              && ! wildcardp (s->wild_statement.filename))
02910            lookup_name (s->wild_statement.filename);
02911          open_input_bfds (s->wild_statement.children.head, force);
02912          break;
02913        case lang_group_statement_enum:
02914          {
02915            struct bfd_link_hash_entry *undefs;
02916 
02917            /* We must continually search the entries in the group
02918               until no new symbols are added to the list of undefined
02919               symbols.  */
02920 
02921            do
02922              {
02923               undefs = link_info.hash->undefs_tail;
02924               open_input_bfds (s->group_statement.children.head, TRUE);
02925              }
02926            while (undefs != link_info.hash->undefs_tail);
02927          }
02928          break;
02929        case lang_target_statement_enum:
02930          current_target = s->target_statement.target;
02931          break;
02932        case lang_input_statement_enum:
02933          if (s->input_statement.real)
02934            {
02935              lang_statement_list_type add;
02936 
02937              s->input_statement.target = current_target;
02938 
02939              /* If we are being called from within a group, and this
02940                is an archive which has already been searched, then
02941                force it to be researched unless the whole archive
02942                has been loaded already.  */
02943              if (force
02944                 && !s->input_statement.whole_archive
02945                 && s->input_statement.loaded
02946                 && bfd_check_format (s->input_statement.the_bfd,
02947                                    bfd_archive))
02948               s->input_statement.loaded = FALSE;
02949 
02950              lang_list_init (&add);
02951 
02952              if (! load_symbols (&s->input_statement, &add))
02953               config.make_executable = FALSE;
02954 
02955              if (add.head != NULL)
02956               {
02957                 *add.tail = s->header.next;
02958                 s->header.next = add.head;
02959               }
02960            }
02961          break;
02962        default:
02963          break;
02964        }
02965     }
02966 }
02967 
02968 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions.  */
02969 
02970 void
02971 lang_track_definedness (const char *name)
02972 {
02973   if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
02974     einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
02975 }
02976 
02977 /* New-function for the definedness hash table.  */
02978 
02979 static struct bfd_hash_entry *
02980 lang_definedness_newfunc (struct bfd_hash_entry *entry,
02981                        struct bfd_hash_table *table ATTRIBUTE_UNUSED,
02982                        const char *name ATTRIBUTE_UNUSED)
02983 {
02984   struct lang_definedness_hash_entry *ret
02985     = (struct lang_definedness_hash_entry *) entry;
02986 
02987   if (ret == NULL)
02988     ret = (struct lang_definedness_hash_entry *)
02989       bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
02990 
02991   if (ret == NULL)
02992     einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
02993 
02994   ret->iteration = -1;
02995   return &ret->root;
02996 }
02997 
02998 /* Return the iteration when the definition of NAME was last updated.  A
02999    value of -1 means that the symbol is not defined in the linker script
03000    or the command line, but may be defined in the linker symbol table.  */
03001 
03002 int
03003 lang_symbol_definition_iteration (const char *name)
03004 {
03005   struct lang_definedness_hash_entry *defentry
03006     = (struct lang_definedness_hash_entry *)
03007     bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
03008 
03009   /* We've already created this one on the presence of DEFINED in the
03010      script, so it can't be NULL unless something is borked elsewhere in
03011      the code.  */
03012   if (defentry == NULL)
03013     FAIL ();
03014 
03015   return defentry->iteration;
03016 }
03017 
03018 /* Update the definedness state of NAME.  */
03019 
03020 void
03021 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
03022 {
03023   struct lang_definedness_hash_entry *defentry
03024     = (struct lang_definedness_hash_entry *)
03025     bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
03026 
03027   /* We don't keep track of symbols not tested with DEFINED.  */
03028   if (defentry == NULL)
03029     return;
03030 
03031   /* If the symbol was already defined, and not from an earlier statement
03032      iteration, don't update the definedness iteration, because that'd
03033      make the symbol seem defined in the linker script at this point, and
03034      it wasn't; it was defined in some object.  If we do anyway, DEFINED
03035      would start to yield false before this point and the construct "sym =
03036      DEFINED (sym) ? sym : X;" would change sym to X despite being defined
03037      in an object.  */
03038   if (h->type != bfd_link_hash_undefined
03039       && h->type != bfd_link_hash_common
03040       && h->type != bfd_link_hash_new
03041       && defentry->iteration == -1)
03042     return;
03043 
03044   defentry->iteration = lang_statement_iteration;
03045 }
03046 
03047 /* Add the supplied name to the symbol table as an undefined reference.
03048    This is a two step process as the symbol table doesn't even exist at
03049    the time the ld command line is processed.  First we put the name
03050    on a list, then, once the output file has been opened, transfer the
03051    name to the symbol table.  */
03052 
03053 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
03054 
03055 #define ldlang_undef_chain_list_head entry_symbol.next
03056 
03057 void
03058 ldlang_add_undef (const char *const name)
03059 {
03060   ldlang_undef_chain_list_type *new =
03061     stat_alloc (sizeof (ldlang_undef_chain_list_type));
03062 
03063   new->next = ldlang_undef_chain_list_head;
03064   ldlang_undef_chain_list_head = new;
03065 
03066   new->name = xstrdup (name);
03067 
03068   if (output_bfd != NULL)
03069     insert_undefined (new->name);
03070 }
03071 
03072 /* Insert NAME as undefined in the symbol table.  */
03073 
03074 static void
03075 insert_undefined (const char *name)
03076 {
03077   struct bfd_link_hash_entry *h;
03078 
03079   h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
03080   if (h == NULL)
03081     einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
03082   if (h->type == bfd_link_hash_new)
03083     {
03084       h->type = bfd_link_hash_undefined;
03085       h->u.undef.abfd = NULL;
03086       bfd_link_add_undef (link_info.hash, h);
03087     }
03088 }
03089 
03090 /* Run through the list of undefineds created above and place them
03091    into the linker hash table as undefined symbols belonging to the
03092    script file.  */
03093 
03094 static void
03095 lang_place_undefineds (void)
03096 {
03097   ldlang_undef_chain_list_type *ptr;
03098 
03099   for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
03100     insert_undefined (ptr->name);
03101 }
03102 
03103 /* Check for all readonly or some readwrite sections.  */
03104 
03105 static void
03106 check_input_sections
03107   (lang_statement_union_type *s,
03108    lang_output_section_statement_type *output_section_statement)
03109 {
03110   for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
03111     {
03112       switch (s->header.type)
03113       {
03114       case lang_wild_statement_enum:
03115        walk_wild (&s->wild_statement, check_section_callback,
03116                  output_section_statement);
03117        if (! output_section_statement->all_input_readonly)
03118          return;
03119        break;
03120       case lang_constructors_statement_enum:
03121        check_input_sections (constructor_list.head,
03122                            output_section_statement);
03123        if (! output_section_statement->all_input_readonly)
03124          return;
03125        break;
03126       case lang_group_statement_enum:
03127        check_input_sections (s->group_statement.children.head,
03128                            output_section_statement);
03129        if (! output_section_statement->all_input_readonly)
03130          return;
03131        break;
03132       default:
03133        break;
03134       }
03135     }
03136 }
03137 
03138 /* Update wildcard statements if needed.  */
03139 
03140 static void
03141 update_wild_statements (lang_statement_union_type *s)
03142 {
03143   struct wildcard_list *sec;
03144 
03145   switch (sort_section)
03146     {
03147     default:
03148       FAIL ();
03149 
03150     case none:
03151       break;
03152 
03153     case by_name:
03154     case by_alignment:
03155       for (; s != NULL; s = s->header.next)
03156        {
03157          switch (s->header.type)
03158            {
03159            default:
03160              break;
03161 
03162            case lang_wild_statement_enum:
03163              sec = s->wild_statement.section_list;
03164              for (sec = s->wild_statement.section_list; sec != NULL;
03165                  sec = sec->next)
03166               {
03167                 switch (sec->spec.sorted)
03168                   {
03169                   case none:
03170                     sec->spec.sorted = sort_section;
03171                     break;
03172                   case by_name:
03173                     if (sort_section == by_alignment)
03174                      sec->spec.sorted = by_name_alignment;
03175                     break;
03176                   case by_alignment:
03177                     if (sort_section == by_name)
03178                      sec->spec.sorted = by_alignment_name;
03179                     break;
03180                   default:
03181                     break;
03182                   }
03183               }
03184              break;
03185 
03186            case lang_constructors_statement_enum:
03187              update_wild_statements (constructor_list.head);
03188              break;
03189 
03190            case lang_output_section_statement_enum:
03191              update_wild_statements
03192               (s->output_section_statement.children.head);
03193              break;
03194 
03195            case lang_group_statement_enum:
03196              update_wild_statements (s->group_statement.children.head);
03197              break;
03198            }
03199        }
03200       break;
03201     }
03202 }
03203 
03204 /* Open input files and attach to output sections.  */
03205 
03206 static void
03207 map_input_to_output_sections
03208   (lang_statement_union_type *s, const char *target,
03209    lang_output_section_statement_type *os)
03210 {
03211   flagword flags;
03212 
03213   for (; s != NULL; s = s->header.next)
03214     {
03215       switch (s->header.type)
03216        {
03217        case lang_wild_statement_enum:
03218          wild (&s->wild_statement, target, os);
03219          break;
03220        case lang_constructors_statement_enum:
03221          map_input_to_output_sections (constructor_list.head,
03222                                    target,
03223                                    os);
03224          break;
03225        case lang_output_section_statement_enum:
03226          if (s->output_section_statement.constraint)
03227            {
03228              if (s->output_section_statement.constraint != ONLY_IF_RW
03229                 && s->output_section_statement.constraint != ONLY_IF_RO)
03230               break;
03231              s->output_section_statement.all_input_readonly = TRUE;
03232              check_input_sections (s->output_section_statement.children.head,
03233                                 &s->output_section_statement);
03234              if ((s->output_section_statement.all_input_readonly
03235                  && s->output_section_statement.constraint == ONLY_IF_RW)
03236                 || (!s->output_section_statement.all_input_readonly
03237                     && s->output_section_statement.constraint == ONLY_IF_RO))
03238               {
03239                 s->output_section_statement.constraint = -1;
03240                 break;
03241               }
03242            }
03243 
03244          map_input_to_output_sections (s->output_section_statement.children.head,
03245                                    target,
03246                                    &s->output_section_statement);
03247          break;
03248        case lang_output_statement_enum:
03249          break;
03250        case lang_target_statement_enum:
03251          target = s->target_statement.target;
03252          break;
03253        case lang_group_statement_enum:
03254          map_input_to_output_sections (s->group_statement.children.head,
03255                                    target,
03256                                    os);
03257          break;
03258        case lang_data_statement_enum:
03259          /* Make sure that any sections mentioned in the expression
03260             are initialized.  */
03261          exp_init_os (s->data_statement.exp);
03262          flags = SEC_HAS_CONTENTS;
03263          /* The output section gets contents, and then we inspect for
03264             any flags set in the input script which override any ALLOC.  */
03265          if (!(os->flags & SEC_NEVER_LOAD))
03266            flags |= SEC_ALLOC | SEC_LOAD;
03267          if (os->bfd_section == NULL)
03268            init_os (os, NULL, flags);
03269          else
03270            os->bfd_section->flags |= flags;
03271          break;
03272        case lang_input_section_enum:
03273          break;
03274        case lang_fill_statement_enum:
03275        case lang_object_symbols_statement_enum:
03276        case lang_reloc_statement_enum:
03277        case lang_padding_statement_enum:
03278        case lang_input_statement_enum:
03279          if (os != NULL && os->bfd_section == NULL)
03280            init_os (os, NULL, 0);
03281          break;
03282        case lang_assignment_statement_enum:
03283          if (os != NULL && os->bfd_section == NULL)
03284            init_os (os, NULL, 0);
03285 
03286          /* Make sure that any sections mentioned in the assignment
03287             are initialized.  */
03288          exp_init_os (s->assignment_statement.exp);
03289          break;
03290        case lang_afile_asection_pair_statement_enum:
03291          FAIL ();
03292          break;
03293        case lang_address_statement_enum:
03294          /* Mark the specified section with the supplied address.
03295 
03296             If this section was actually a segment marker, then the
03297             directive is ignored if the linker script explicitly
03298             processed the segment marker.  Originally, the linker
03299             treated segment directives (like -Ttext on the
03300             command-line) as section directives.  We honor the
03301             section directive semantics for backwards compatibilty;
03302             linker scripts that do not specifically check for
03303             SEGMENT_START automatically get the old semantics.  */
03304          if (!s->address_statement.segment
03305              || !s->address_statement.segment->used)
03306            {
03307              lang_output_section_statement_type *aos
03308               = (lang_output_section_statement_lookup
03309                  (s->address_statement.section_name));
03310 
03311              if (aos->bfd_section == NULL)
03312               init_os (aos, NULL, 0);
03313              aos->addr_tree = s->address_statement.address;
03314            }
03315          break;
03316        }
03317     }
03318 }
03319 
03320 /* An output section might have been removed after its statement was
03321    added.  For example, ldemul_before_allocation can remove dynamic
03322    sections if they turn out to be not needed.  Clean them up here.  */
03323 
03324 void
03325 strip_excluded_output_sections (void)
03326 {
03327   lang_output_section_statement_type *os;
03328 
03329   /* Run lang_size_sections (if not already done).  */
03330   if (expld.phase != lang_mark_phase_enum)
03331     {
03332       expld.phase = lang_mark_phase_enum;
03333       expld.dataseg.phase = exp_dataseg_none;
03334       one_lang_size_sections_pass (NULL, FALSE);
03335       lang_reset_memory_regions ();
03336     }
03337 
03338   for (os = &lang_output_section_statement.head->output_section_statement;
03339        os != NULL;
03340        os = os->next)
03341     {
03342       asection *output_section;
03343       bfd_boolean exclude;
03344 
03345       if (os->constraint == -1)
03346        continue;
03347 
03348       output_section = os->bfd_section;
03349       if (output_section == NULL)
03350        continue;
03351 
03352       exclude = (output_section->rawsize == 0
03353                && (output_section->flags & SEC_KEEP) == 0
03354                && !bfd_section_removed_from_list (output_bfd,
03355                                               output_section));
03356 
03357       /* Some sections have not yet been sized, notably .gnu.version,
03358         .dynsym, .dynstr and .hash.  These all have SEC_LINKER_CREATED
03359         input sections, so don't drop output sections that have such
03360         input sections unless they are also marked SEC_EXCLUDE.  */
03361       if (exclude && output_section->map_head.s != NULL)
03362        {
03363          asection *s;
03364 
03365          for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
03366            if ((s->flags & SEC_LINKER_CREATED) != 0
03367               && (s->flags & SEC_EXCLUDE) == 0)
03368              {
03369               exclude = FALSE;
03370               break;
03371              }
03372        }
03373 
03374       /* TODO: Don't just junk map_head.s, turn them into link_orders.  */
03375       output_section->map_head.link_order = NULL;
03376       output_section->map_tail.link_order = NULL;
03377 
03378       if (exclude)
03379        {
03380          /* We don't set bfd_section to NULL since bfd_section of the
03381             removed output section statement may still be used.  */
03382          if (!os->section_relative_symbol)
03383            os->ignored = TRUE;
03384          output_section->flags |= SEC_EXCLUDE;
03385          bfd_section_list_remove (output_bfd, output_section);
03386          output_bfd->section_count--;
03387        }
03388     }
03389 
03390   /* Stop future calls to lang_add_section from messing with map_head
03391      and map_tail link_order fields.  */
03392   stripped_excluded_sections = TRUE;
03393 }
03394 
03395 static void
03396 print_output_section_statement
03397   (lang_output_section_statement_type *output_section_statement)
03398 {
03399   asection *section = output_section_statement->bfd_section;
03400   int len;
03401 
03402   if (output_section_statement != abs_output_section)
03403     {
03404       minfo ("\n%s", output_section_statement->name);
03405 
03406       if (section != NULL)
03407        {
03408          print_dot = section->vma;
03409 
03410          len = strlen (output_section_statement->name);
03411          if (len >= SECTION_NAME_MAP_LENGTH - 1)
03412            {
03413              print_nl ();
03414              len = 0;
03415            }
03416          while (len < SECTION_NAME_MAP_LENGTH)
03417            {
03418              print_space ();
03419              ++len;
03420            }
03421 
03422          minfo ("0x%V %W", section->vma, section->size);
03423 
03424          if (section->vma != section->lma)
03425            minfo (_(" load address 0x%V"), section->lma);
03426        }
03427 
03428       print_nl ();
03429     }
03430 
03431   print_statement_list (output_section_statement->children.head,
03432                      output_section_statement);
03433 }
03434 
03435 /* Scan for the use of the destination in the right hand side
03436    of an expression.  In such cases we will not compute the
03437    correct expression, since the value of DST that is used on
03438    the right hand side will be its final value, not its value
03439    just before this expression is evaluated.  */
03440 
03441 static bfd_boolean
03442 scan_for_self_assignment (const char * dst, etree_type * rhs)
03443 {
03444   if (rhs == NULL || dst == NULL)
03445     return FALSE;
03446 
03447   switch (rhs->type.node_class)
03448     {
03449     case etree_binary:
03450       return scan_for_self_assignment (dst, rhs->binary.lhs)
03451        ||   scan_for_self_assignment (dst, rhs->binary.rhs);
03452 
03453     case etree_trinary:
03454       return scan_for_self_assignment (dst, rhs->trinary.lhs)
03455        ||   scan_for_self_assignment (dst, rhs->trinary.rhs);
03456 
03457     case etree_assign:
03458     case etree_provided:
03459     case etree_provide:
03460       if (strcmp (dst, rhs->assign.dst) == 0)
03461        return TRUE;
03462       return scan_for_self_assignment (dst, rhs->assign.src);
03463 
03464     case etree_unary:
03465       return scan_for_self_assignment (dst, rhs->unary.child);
03466 
03467     case etree_value:
03468       if (rhs->value.str)
03469        return strcmp (dst, rhs->value.str) == 0;
03470       return FALSE;
03471 
03472     case etree_name:
03473       if (rhs->name.name)
03474        return strcmp (dst, rhs->name.name) == 0;
03475       return FALSE;
03476 
03477     default:
03478       break;
03479     }
03480 
03481   return FALSE;
03482 }
03483 
03484 
03485 static void
03486 print_assignment (lang_assignment_statement_type *assignment,
03487                 lang_output_section_statement_type *output_section)
03488 {
03489   unsigned int i;
03490   bfd_boolean is_dot;
03491   bfd_boolean computation_is_valid = TRUE;
03492   etree_type *tree;
03493 
03494   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
03495     print_space ();
03496 
03497   if (assignment->exp->type.node_class == etree_assert)
03498     {
03499       is_dot = FALSE;
03500       tree = assignment->exp->assert_s.child;
03501       computation_is_valid = TRUE;
03502     }
03503   else
03504     {
03505       const char *dst = assignment->exp->assign.dst;
03506 
03507       is_dot = (dst[0] == '.' && dst[1] == 0);
03508       tree = assignment->exp->assign.src;
03509       computation_is_valid = is_dot || (scan_for_self_assignment (dst, tree) == FALSE);
03510     }
03511 
03512   exp_fold_tree (tree, output_section->bfd_section, &print_dot);
03513   if (expld.result.valid_p)
03514     {
03515       bfd_vma value;
03516 
03517       if (computation_is_valid)
03518        {
03519          value = expld.result.value;
03520 
03521          if (expld.result.section)
03522            value += expld.result.section->vma;
03523 
03524          minfo ("0x%V", value);
03525          if (is_dot)
03526            print_dot = value;
03527        }
03528       else
03529        {
03530          struct bfd_link_hash_entry *h;
03531 
03532          h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
03533                                 FALSE, FALSE, TRUE);
03534          if (h)
03535            {
03536              value = h->u.def.value;
03537 
03538              if (expld.result.section)
03539              value += expld.result.section->vma;
03540 
03541              minfo ("[0x%V]", value);
03542            }
03543          else
03544            minfo ("[unresolved]");
03545        }
03546     }
03547   else
03548     {
03549       minfo ("*undef*   ");
03550 #ifdef BFD64
03551       minfo ("        ");
03552 #endif
03553     }
03554 
03555   minfo ("                ");
03556   exp_print_tree (assignment->exp);
03557   print_nl ();
03558 }
03559 
03560 static void
03561 print_input_statement (lang_input_statement_type *statm)
03562 {
03563   if (statm->filename != NULL)
03564     {
03565       fprintf (config.map_file, "LOAD %s\n", statm->filename);
03566     }
03567 }
03568 
03569 /* Print all symbols defined in a particular section.  This is called
03570    via bfd_link_hash_traverse, or by print_all_symbols.  */
03571 
03572 static bfd_boolean
03573 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
03574 {
03575   asection *sec = ptr;
03576 
03577   if ((hash_entry->type == bfd_link_hash_defined
03578        || hash_entry->type == bfd_link_hash_defweak)
03579       && sec == hash_entry->u.def.section)
03580     {
03581       int i;
03582 
03583       for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
03584        print_space ();
03585       minfo ("0x%V   ",
03586             (hash_entry->u.def.value
03587              + hash_entry->u.def.section->output_offset
03588              + hash_entry->u.def.section->output_section->vma));
03589 
03590       minfo ("             %T\n", hash_entry->root.string);
03591     }
03592 
03593   return TRUE;
03594 }
03595 
03596 static void
03597 print_all_symbols (asection *sec)
03598 {
03599   struct fat_user_section_struct *ud = get_userdata (sec);
03600   struct map_symbol_def *def;
03601 
03602   if (!ud)
03603     return;
03604 
03605   *ud->map_symbol_def_tail = 0;
03606   for (def = ud->map_symbol_def_head; def; def = def->next)
03607     print_one_symbol (def->entry, sec);
03608 }
03609 
03610 /* Print information about an input section to the map file.  */
03611 
03612 static void
03613 print_input_section (asection *i)
03614 {
03615   bfd_size_type size = i->size;
03616   int len;
03617   bfd_vma addr;
03618 
03619   init_opb ();
03620 
03621   print_space ();
03622   minfo ("%s", i->name);
03623 
03624   len = 1 + strlen (i->name);
03625   if (len >= SECTION_NAME_MAP_LENGTH - 1)
03626     {
03627       print_nl ();
03628       len = 0;
03629     }
03630   while (len < SECTION_NAME_MAP_LENGTH)
03631     {
03632       print_space ();
03633       ++len;
03634     }
03635 
03636   if (i->output_section != NULL && i->output_section->owner == output_bfd)
03637     addr = i->output_section->vma + i->output_offset;
03638   else
03639     {
03640       addr = print_dot;
03641       size = 0;
03642     }
03643 
03644   minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
03645 
03646   if (size != i->rawsize && i->rawsize != 0)
03647     {
03648       len = SECTION_NAME_MAP_LENGTH + 3;
03649 #ifdef BFD64
03650       len += 16;
03651 #else
03652       len += 8;
03653 #endif
03654       while (len > 0)
03655        {
03656          print_space ();
03657          --len;
03658        }
03659 
03660       minfo (_("%W (size before relaxing)\n"), i->rawsize);
03661     }
03662 
03663   if (i->output_section != NULL && i->output_section->owner == output_bfd)
03664     {
03665       if (link_info.reduce_memory_overheads)
03666        bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
03667       else
03668        print_all_symbols (i);
03669 
03670       print_dot = addr + TO_ADDR (size);
03671     }
03672 }
03673 
03674 static void
03675 print_fill_statement (lang_fill_statement_type *fill)
03676 {
03677   size_t size;
03678   unsigned char *p;
03679   fputs (" FILL mask 0x", config.map_file);
03680   for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
03681     fprintf (config.map_file, "%02x", *p);
03682   fputs ("\n", config.map_file);
03683 }
03684 
03685 static void
03686 print_data_statement (lang_data_statement_type *data)
03687 {
03688   int i;
03689   bfd_vma addr;
03690   bfd_size_type size;
03691   const char *name;
03692 
03693   init_opb ();
03694   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
03695     print_space ();
03696 
03697   addr = data->output_offset;
03698   if (data->output_section != NULL)
03699     addr += data->output_section->vma;
03700 
03701   switch (data->type)
03702     {
03703     default:
03704       abort ();
03705     case BYTE:
03706       size = BYTE_SIZE;
03707       name = "BYTE";
03708       break;
03709     case SHORT:
03710       size = SHORT_SIZE;
03711       name = "SHORT";
03712       break;
03713     case LONG:
03714       size = LONG_SIZE;
03715       name = "LONG";
03716       break;
03717     case QUAD:
03718       size = QUAD_SIZE;
03719       name = "QUAD";
03720       break;
03721     case SQUAD:
03722       size = QUAD_SIZE;
03723       name = "SQUAD";
03724       break;
03725     }
03726 
03727   minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
03728 
03729   if (data->exp->type.node_class != etree_value)
03730     {
03731       print_space ();
03732       exp_print_tree (data->exp);
03733     }
03734 
03735   print_nl ();
03736 
03737   print_dot = addr + TO_ADDR (size);
03738 }
03739 
03740 /* Print an address statement.  These are generated by options like
03741    -Ttext.  */
03742 
03743 static void
03744 print_address_statement (lang_address_statement_type *address)
03745 {
03746   minfo (_("Address of section %s set to "), address->section_name);
03747   exp_print_tree (address->address);
03748   print_nl ();
03749 }
03750 
03751 /* Print a reloc statement.  */
03752 
03753 static void
03754 print_reloc_statement (lang_reloc_statement_type *reloc)
03755 {
03756   int i;
03757   bfd_vma addr;
03758   bfd_size_type size;
03759 
03760   init_opb ();
03761   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
03762     print_space ();
03763 
03764   addr = reloc->output_offset;
03765   if (reloc->output_section != NULL)
03766     addr += reloc->output_section->vma;
03767 
03768   size = bfd_get_reloc_size (reloc->howto);
03769 
03770   minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
03771 
03772   if (reloc->name != NULL)
03773     minfo ("%s+", reloc->name);
03774   else
03775     minfo ("%s+", reloc->section->name);
03776 
03777   exp_print_tree (reloc->addend_exp);
03778 
03779   print_nl ();
03780 
03781   print_dot = addr + TO_ADDR (size);
03782 }
03783 
03784 static void
03785 print_padding_statement (lang_padding_statement_type *s)
03786 {
03787   int len;
03788   bfd_vma addr;
03789 
03790   init_opb ();
03791   minfo (" *fill*");
03792 
03793   len = sizeof " *fill*" - 1;
03794   while (len < SECTION_NAME_MAP_LENGTH)
03795     {
03796       print_space ();
03797       ++len;
03798     }
03799 
03800   addr = s->output_offset;
03801   if (s->output_section != NULL)
03802     addr += s->output_section->vma;
03803   minfo ("0x%V %W ", addr, (bfd_vma) s->size);
03804 
03805   if (s->fill->size != 0)
03806     {
03807       size_t size;
03808       unsigned char *p;
03809       for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
03810        fprintf (config.map_file, "%02x", *p);
03811     }
03812 
03813   print_nl ();
03814 
03815   print_dot = addr + TO_ADDR (s->size);
03816 }
03817 
03818 static void
03819 print_wild_statement (lang_wild_statement_type *w,
03820                     lang_output_section_statement_type *os)
03821 {
03822   struct wildcard_list *sec;
03823 
03824   print_space ();
03825 
03826   if (w->filenames_sorted)
03827     minfo ("SORT(");
03828   if (w->filename != NULL)
03829     minfo ("%s", w->filename);
03830   else
03831     minfo ("*");
03832   if (w->filenames_sorted)
03833     minfo (")");
03834 
03835   minfo ("(");
03836   for (sec = w->section_list; sec; sec = sec->next)
03837     {
03838       if (sec->spec.sorted)
03839        minfo ("SORT(");
03840       if (sec->spec.exclude_name_list != NULL)
03841        {
03842          name_list *tmp;
03843          minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
03844          for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
03845            minfo (" %s", tmp->name);
03846          minfo (") ");
03847        }
03848       if (sec->spec.name != NULL)
03849        minfo ("%s", sec->spec.name);
03850       else
03851        minfo ("*");
03852       if (sec->spec.sorted)
03853        minfo (")");
03854       if (sec->next)
03855        minfo (" ");
03856     }
03857   minfo (")");
03858 
03859   print_nl ();
03860 
03861   print_statement_list (w->children.head, os);
03862 }
03863 
03864 /* Print a group statement.  */
03865 
03866 static void
03867 print_group (lang_group_statement_type *s,
03868             lang_output_section_statement_type *os)
03869 {
03870   fprintf (config.map_file, "START GROUP\n");
03871   print_statement_list (s->children.head, os);
03872   fprintf (config.map_file, "END GROUP\n");
03873 }
03874 
03875 /* Print the list of statements in S.
03876    This can be called for any statement type.  */
03877 
03878 static void
03879 print_statement_list (lang_statement_union_type *s,
03880                     lang_output_section_statement_type *os)
03881 {
03882   while (s != NULL)
03883     {
03884       print_statement (s, os);
03885       s = s->header.next;
03886     }
03887 }
03888 
03889 /* Print the first statement in statement list S.
03890    This can be called for any statement type.  */
03891 
03892 static void
03893 print_statement (lang_statement_union_type *s,
03894                lang_output_section_statement_type *os)
03895 {
03896   switch (s->header.type)
03897     {
03898     default:
03899       fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
03900       FAIL ();
03901       break;
03902     case lang_constructors_statement_enum:
03903       if (constructor_list.head != NULL)
03904        {
03905          if (constructors_sorted)
03906            minfo (" SORT (CONSTRUCTORS)\n");
03907          else
03908            minfo (" CONSTRUCTORS\n");
03909          print_statement_list (constructor_list.head, os);
03910        }
03911       break;
03912     case lang_wild_statement_enum:
03913       print_wild_statement (&s->wild_statement, os);
03914       break;
03915     case lang_address_statement_enum:
03916       print_address_statement (&s->address_statement);
03917       break;
03918     case lang_object_symbols_statement_enum:
03919       minfo (" CREATE_OBJECT_SYMBOLS\n");
03920       break;
03921     case lang_fill_statement_enum:
03922       print_fill_statement (&s->fill_statement);
03923       break;
03924     case lang_data_statement_enum:
03925       print_data_statement (&s->data_statement);
03926       break;
03927     case lang_reloc_statement_enum:
03928       print_reloc_statement (&s->reloc_statement);
03929       break;
03930     case lang_input_section_enum:
03931       print_input_section (s->input_section.section);
03932       break;
03933     case lang_padding_statement_enum:
03934       print_padding_statement (&s->padding_statement);
03935       break;
03936     case lang_output_section_statement_enum:
03937       print_output_section_statement (&s->output_section_statement);
03938       break;
03939     case lang_assignment_statement_enum:
03940       print_assignment (&s->assignment_statement, os);
03941       break;
03942     case lang_target_statement_enum:
03943       fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
03944       break;
03945     case lang_output_statement_enum:
03946       minfo ("OUTPUT(%s", s->output_statement.name);
03947       if (output_target != NULL)
03948        minfo (" %s", output_target);
03949       minfo (")\n");
03950       break;
03951     case lang_input_statement_enum:
03952       print_input_statement (&s->input_statement);
03953       break;
03954     case lang_group_statement_enum:
03955       print_group (&s->group_statement, os);
03956       break;
03957     case lang_afile_asection_pair_statement_enum:
03958       FAIL ();
03959       break;
03960     }
03961 }
03962 
03963 static void
03964 print_statements (void)
03965 {
03966   print_statement_list (statement_list.head, abs_output_section);
03967 }
03968 
03969 /* Print the first N statements in statement list S to STDERR.
03970    If N == 0, nothing is printed.
03971    If N < 0, the entire list is printed.
03972    Intended to be called from GDB.  */
03973 
03974 void
03975 dprint_statement (lang_statement_union_type *s, int n)
03976 {
03977   FILE *map_save = config.map_file;
03978 
03979   config.map_file = stderr;
03980 
03981   if (n < 0)
03982     print_statement_list (s, abs_output_section);
03983   else
03984     {
03985       while (s && --n >= 0)
03986        {
03987          print_statement (s, abs_output_section);
03988          s = s->header.next;
03989        }
03990     }
03991 
03992   config.map_file = map_save;
03993 }
03994 
03995 static void
03996 insert_pad (lang_statement_union_type **ptr,
03997            fill_type *fill,
03998            unsigned int alignment_needed,
03999            asection *output_section,
04000            bfd_vma dot)
04001 {
04002   static fill_type zero_fill = { 1, { 0 } };
04003   lang_statement_union_type *pad = NULL;
04004 
04005   if (ptr != &statement_list.head)
04006     pad = ((lang_statement_union_type *)
04007           ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
04008   if (pad != NULL
04009       && pad->header.type == lang_padding_statement_enum
04010       && pad->padding_statement.output_section == output_section)
04011     {
04012       /* Use the existing pad statement.  */
04013     }
04014   else if ((pad = *ptr) != NULL
04015       && pad->header.type == lang_padding_statement_enum
04016       && pad->padding_statement.output_section == output_section)
04017     {
04018       /* Use the existing pad statement.  */
04019     }
04020   else
04021     {
04022       /* Make a new padding statement, linked into existing chain.  */
04023       pad = stat_alloc (sizeof (lang_padding_statement_type));
04024       pad->header.next = *ptr;
04025       *ptr = pad;
04026       pad->header.type = lang_padding_statement_enum;
04027       pad->padding_statement.output_section = output_section;
04028       if (fill == NULL)
04029        fill = &zero_fill;
04030       pad->padding_statement.fill = fill;
04031     }
04032   pad->padding_statement.output_offset = dot - output_section->vma;
04033   pad->padding_statement.size = alignment_needed;
04034   output_section->size += alignment_needed;
04035 }
04036 
04037 /* Work out how much this section will move the dot point.  */
04038 
04039 static bfd_vma
04040 size_input_section
04041   (lang_statement_union_type **this_ptr,
04042    lang_output_section_statement_type *output_section_statement,
04043    fill_type *fill,
04044    bfd_vma dot)
04045 {
04046   lang_input_section_type *is = &((*this_ptr)->input_section);
04047   asection *i = is->section;
04048 
04049   if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
04050       && (i->flags & SEC_EXCLUDE) == 0)
04051     {
04052       unsigned int alignment_needed;
04053       asection *o;
04054 
04055       /* Align this section first to the input sections requirement,
04056         then to the output section's requirement.  If this alignment
04057         is greater than any seen before, then record it too.  Perform
04058         the alignment by inserting a magic 'padding' statement.  */
04059 
04060       if (output_section_statement->subsection_alignment != -1)
04061        i->alignment_power = output_section_statement->subsection_alignment;
04062 
04063       o = output_section_statement->bfd_section;
04064       if (o->alignment_power < i->alignment_power)
04065        o->alignment_power = i->alignment_power;
04066 
04067       alignment_needed = align_power (dot, i->alignment_power) - dot;
04068 
04069       if (alignment_needed != 0)
04070        {
04071          insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
04072          dot += alignment_needed;
04073        }
04074 
04075       /* Remember where in the output section this input section goes.  */
04076 
04077       i->output_offset = dot - o->vma;
04078 
04079       /* Mark how big the output section must be to contain this now.  */
04080       dot += TO_ADDR (i->size);
04081       o->size = TO_SIZE (dot - o->vma);
04082     }
04083   else
04084     {
04085       i->output_offset = i->vma - output_section_statement->bfd_section->vma;
04086     }
04087 
04088   return dot;
04089 }
04090 
04091 static int
04092 sort_sections_by_lma (const void *arg1, const void *arg2)
04093 {
04094   const asection *sec1 = *(const asection **) arg1;
04095   const asection *sec2 = *(const asection **) arg2;
04096 
04097   if (bfd_section_lma (sec1->owner, sec1)
04098       < bfd_section_lma (sec2->owner, sec2))
04099     return -1;
04100   else if (bfd_section_lma (sec1->owner, sec1)
04101           > bfd_section_lma (sec2->owner, sec2))
04102     return 1;
04103 
04104   return 0;
04105 }
04106 
04107 #define IGNORE_SECTION(s) \
04108   ((s->flags & SEC_NEVER_LOAD) != 0                            \
04109    || (s->flags & SEC_ALLOC) == 0                       \
04110    || ((s->flags & SEC_THREAD_LOCAL) != 0               \
04111        && (s->flags & SEC_LOAD) == 0))
04112 
04113 /* Check to see if any allocated sections overlap with other allocated
04114    sections.  This can happen if a linker script specifies the output
04115    section addresses of the two sections.  */
04116 
04117 static void
04118 lang_check_section_addresses (void)
04119 {
04120   asection *s, *os;
04121   asection **sections, **spp;
04122   unsigned int count;
04123   bfd_vma s_start;
04124   bfd_vma s_end;
04125   bfd_vma os_start;
04126   bfd_vma os_end;
04127   bfd_size_type amt;
04128 
04129   if (bfd_count_sections (output_bfd) <= 1)
04130     return;
04131 
04132   amt = bfd_count_sections (output_bfd) * sizeof (asection *);
04133   sections = xmalloc (amt);
04134 
04135   /* Scan all sections in the output list.  */
04136   count = 0;
04137   for (s = output_bfd->sections; s != NULL; s = s->next)
04138     {
04139       /* Only consider loadable sections with real contents.  */
04140       if (IGNORE_SECTION (s) || s->size == 0)
04141        continue;
04142 
04143       sections[count] = s;
04144       count++;
04145     }
04146 
04147   if (count <= 1)
04148     return;
04149 
04150   qsort (sections, (size_t) count, sizeof (asection *),
04151         sort_sections_by_lma);
04152 
04153   spp = sections;
04154   s = *spp++;
04155   s_start = bfd_section_lma (output_bfd, s);
04156   s_end = s_start + TO_ADDR (s->size) - 1;
04157   for (count--; count; count--)
04158     {
04159       /* We must check the sections' LMA addresses not their VMA
04160         addresses because overlay sections can have overlapping VMAs
04161         but they must have distinct LMAs.  */
04162       os = s;
04163       os_start = s_start;
04164       os_end = s_end;
04165       s = *spp++;
04166       s_start = bfd_section_lma (output_bfd, s);
04167       s_end = s_start + TO_ADDR (s->size) - 1;
04168 
04169       /* Look for an overlap.  */
04170       if (s_end >= os_start && s_start <= os_end)
04171        einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
04172               s->name, s_start, s_end, os->name, os_start, os_end);
04173     }
04174 
04175   free (sections);
04176 }
04177 
04178 /* Make sure the new address is within the region.  We explicitly permit the
04179    current address to be at the exact end of the region when the address is
04180    non-zero, in case the region is at the end of addressable memory and the
04181    calculation wraps around.  */
04182 
04183 static void
04184 os_region_check (lang_output_section_statement_type *os,
04185                lang_memory_region_type *region,
04186                etree_type *tree,
04187                bfd_vma base)
04188 {
04189   if ((region->current < region->origin
04190        || (region->current - region->origin > region->length))
04191       && ((region->current != region->origin + region->length)
04192          || base == 0))
04193     {
04194       if (tree != NULL)
04195        {
04196          einfo (_("%X%P: address 0x%v of %B section %s"
04197                  " is not within region %s\n"),
04198                region->current,
04199                os->bfd_section->owner,
04200                os->bfd_section->name,
04201                region->name);
04202        }
04203       else
04204        {
04205          einfo (_("%X%P: region %s is full (%B section %s)\n"),
04206                region->name,
04207                os->bfd_section->owner,
04208                os->bfd_section->name);
04209        }
04210       /* Reset the region pointer.  */
04211       region->current = region->origin;
04212     }
04213 }
04214 
04215 /* Set the sizes for all the output sections.  */
04216 
04217 static bfd_vma
04218 lang_size_sections_1
04219   (lang_statement_union_type *s,
04220    lang_output_section_statement_type *output_section_statement,
04221    lang_statement_union_type **prev,
04222    fill_type *fill,
04223    bfd_vma dot,
04224    bfd_boolean *relax,
04225    bfd_boolean check_regions)
04226 {
04227   /* Size up the sections from their constituent parts.  */
04228   for (; s != NULL; s = s->header.next)
04229     {
04230       switch (s->header.type)
04231        {
04232        case lang_output_section_statement_enum:
04233          {
04234            bfd_vma newdot, after;
04235            lang_output_section_statement_type *os;
04236            lang_memory_region_type *r;
04237 
04238            os = &s->output_section_statement;
04239            if (os->addr_tree != NULL)
04240              {
04241               os->processed_vma = FALSE;
04242               exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
04243 
04244               if (!expld.result.valid_p
04245                   && expld.phase != lang_mark_phase_enum)
04246                 einfo (_("%F%S: non constant or forward reference"
04247                         " address expression for section %s\n"),
04248                       os->name);
04249 
04250               dot = expld.result.value + expld.result.section->vma;
04251              }
04252 
04253            if (os->bfd_section == NULL)
04254              /* This section was removed or never actually created.  */
04255              break;
04256 
04257            /* If this is a COFF shared library section, use the size and
04258               address from the input section.  FIXME: This is COFF
04259               specific; it would be cleaner if there were some other way
04260               to do this, but nothing simple comes to mind.  */
04261            if ((bfd_get_flavour (output_bfd) == bfd_target_ecoff_flavour
04262                || bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
04263               && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
04264              {
04265               asection *input;
04266 
04267               if (os->children.head == NULL
04268                   || os->children.head->header.next != NULL
04269                   || (os->children.head->header.type
04270                      != lang_input_section_enum))
04271                 einfo (_("%P%X: Internal error on COFF shared library"
04272                         " section %s\n"), os->name);
04273 
04274               input = os->children.head->input_section.section;
04275               bfd_set_section_vma (os->bfd_section->owner,
04276                                  os->bfd_section,
04277                                  bfd_section_vma (input->owner, input));
04278               os->bfd_section->size = input->size;
04279               break;
04280              }
04281 
04282            newdot = dot;
04283            if (bfd_is_abs_section (os->bfd_section))
04284              {
04285               /* No matter what happens, an abs section starts at zero.  */
04286               ASSERT (os->bfd_section->vma == 0);
04287              }
04288            else
04289              {
04290               int align;
04291 
04292               if (os->addr_tree == NULL)
04293                 {
04294                   /* No address specified for this section, get one
04295                      from the region specification.  */
04296                   if (os->region == NULL
04297                      || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
04298                          && os->region->name[0] == '*'
04299                          && strcmp (os->region->name,
04300                                    DEFAULT_MEMORY_REGION) == 0))
04301                     {
04302                      os->region = lang_memory_default (os->bfd_section);
04303                     }
04304 
04305                   /* If a loadable section is using the default memory
04306                      region, and some non default memory regions were
04307                      defined, issue an error message.  */
04308                   if (!os->ignored
04309                      && !IGNORE_SECTION (os->bfd_section)
04310                      && ! link_info.relocatable
04311                      && check_regions
04312                      && strcmp (os->region->name,
04313                                DEFAULT_MEMORY_REGION) == 0
04314                      && lang_memory_region_list != NULL
04315                      && (strcmp (lang_memory_region_list->name,
04316                                 DEFAULT_MEMORY_REGION) != 0
04317                          || lang_memory_region_list->next != NULL)
04318                      && expld.phase != lang_mark_phase_enum)
04319                     {
04320                      /* By default this is an error rather than just a
04321                         warning because if we allocate the section to the
04322                         default memory region we can end up creating an
04323                         excessively large binary, or even seg faulting when
04324                         attempting to perform a negative seek.  See
04325                         sources.redhat.com/ml/binutils/2003-04/msg00423.html
04326                         for an example of this.  This behaviour can be
04327                         overridden by the using the --no-check-sections
04328                         switch.  */
04329                      if (command_line.check_section_addresses)
04330                        einfo (_("%P%F: error: no memory region specified"
04331                                " for loadable section `%s'\n"),
04332                              bfd_get_section_name (output_bfd,
04333                                                  os->bfd_section));
04334                      else
04335                        einfo (_("%P: warning: no memory region specified"
04336                                " for loadable section `%s'\n"),
04337                              bfd_get_section_name (output_bfd,
04338                                                  os->bfd_section));
04339                     }
04340 
04341                   newdot = os->region->current;
04342                   align = os->bfd_section->alignment_power;
04343                 }
04344               else
04345                 align = os->section_alignment;
04346 
04347               /* Align to what the section needs.  */
04348               if (align > 0)
04349                 {
04350                   bfd_vma savedot = newdot;
04351                   newdot = align_power (newdot, align);
04352 
04353                   if (newdot != savedot
04354                      && (config.warn_section_align
04355                          || os->addr_tree != NULL)
04356                      && expld.phase != lang_mark_phase_enum)
04357                     einfo (_("%P: warning: changing start of section"
04358                             " %s by %lu bytes\n"),
04359                           os->name, (unsigned long) (newdot - savedot));
04360                 }
04361 
04362               bfd_set_section_vma (0, os->bfd_section, newdot);
04363 
04364               os->bfd_section->output_offset = 0;
04365              }
04366 
04367            lang_size_sections_1 (os->children.head, os, &os->children.head,
04368                               os->fill, newdot, relax, check_regions);
04369 
04370            os->processed_vma = TRUE;
04371 
04372            if (bfd_is_abs_section (os->bfd_section) || os->ignored)
04373              /* Except for some special linker created sections,
04374                no output section should change from zero size
04375                after strip_excluded_output_sections.  A non-zero
04376                size on an ignored section indicates that some
04377                input section was not sized early enough.  */
04378              ASSERT (os->bfd_section->size == 0);
04379            else
04380              {
04381               dot = os->bfd_section->vma;
04382 
04383               /* Put the section within the requested block size, or
04384                  align at the block boundary.  */
04385               after = ((dot
04386                        + TO_ADDR (os->bfd_section->size)
04387                        + os->block_value - 1)
04388                       & - (bfd_vma) os->block_value);
04389 
04390               os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
04391              }
04392 
04393            /* Set section lma.  */
04394            r = os->region;
04395            if (r == NULL)
04396              r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
04397 
04398            if (os->load_base)
04399              {
04400               bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
04401               os->bfd_section->lma = lma;
04402              }
04403            else if (os->region != NULL
04404                    && os->lma_region != NULL
04405                    && os->lma_region != os->region)
04406              {
04407               bfd_vma lma = os->lma_region->current;
04408 
04409               if (os->section_alignment != -1)
04410                 lma = align_power (lma, os->section_alignment);
04411               os->bfd_section->lma = lma;
04412              }
04413            else if (r->last_os != NULL
04414                    && (os->bfd_section->flags & SEC_ALLOC) != 0)
04415              {
04416               bfd_vma lma;
04417               asection *last;
04418 
04419               last = r->last_os->output_section_statement.bfd_section;
04420 
04421               /* A backwards move of dot should be accompanied by
04422                  an explicit assignment to the section LMA (ie.
04423                  os->load_base set) because backwards moves can
04424                  create overlapping LMAs.  */
04425               if (dot < last->vma
04426                   && os->bfd_section->size != 0
04427                   && dot + os->bfd_section->size <= last->vma)
04428                 {
04429                   /* If dot moved backwards then leave lma equal to
04430                      vma.  This is the old default lma, which might
04431                      just happen to work when the backwards move is
04432                      sufficiently large.  Nag if this changes anything,
04433                      so people can fix their linker scripts.  */
04434 
04435                   if (last->vma != last->lma)
04436                     einfo (_("%P: warning: dot moved backwards before `%s'\n"),
04437                           os->name);
04438                 }
04439               else
04440                 {
04441                   /* If the current vma overlaps the previous section,
04442                      then set the current lma to that at the end of
04443                      the previous section.  The previous section was
04444                      probably an overlay.  */
04445                   if ((dot >= last->vma
04446                       && dot < last->vma + last->size)
04447                      || (last->vma >= dot
04448                          && last->vma < dot + os->bfd_section->size))
04449                     lma = last->lma + last->size;
04450 
04451                   /* Otherwise, keep the same lma to vma relationship
04452                      as the previous section.  */
04453                   else
04454                     lma = dot + last->lma - last->vma;
04455 
04456                   if (os->section_alignment != -1)
04457                     lma = align_power (lma, os->section_alignment);
04458                   os->bfd_section->lma = lma;
04459                 }
04460              }
04461            os->processed_lma = TRUE;
04462 
04463            if (bfd_is_abs_section (os->bfd_section) || os->ignored)
04464              break;
04465 
04466            /* Keep track of normal sections using the default
04467               lma region.  We use this to set the lma for
04468               following sections.  Overlays or other linker
04469               script assignment to lma might mean that the
04470               default lma == vma is incorrect.
04471               To avoid warnings about dot moving backwards when using
04472               -Ttext, don't start tracking sections until we find one
04473               of non-zero size or with lma set differently to vma.  */
04474            if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
04475                || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0)
04476               && (os->bfd_section->flags & SEC_ALLOC) != 0
04477               && (os->bfd_section->size != 0
04478                   || (r->last_os == NULL
04479                      && os->bfd_section->vma != os->bfd_section->lma)
04480                   || (r->last_os != NULL
04481                      && dot >= (r->last_os->output_section_statement
04482                                .bfd_section->vma)))
04483               && os->lma_region == NULL
04484               && !link_info.relocatable)
04485              r->last_os = s;
04486 
04487            /* .tbss sections effectively have zero size.  */
04488            if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
04489               || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
04490               || link_info.relocatable)
04491              dot += TO_ADDR (os->bfd_section->size);
04492 
04493            if (os->update_dot_tree != 0)
04494              exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
04495 
04496            /* Update dot in the region ?
04497               We only do this if the section is going to be allocated,
04498               since unallocated sections do not contribute to the region's
04499               overall size in memory.
04500 
04501               If the SEC_NEVER_LOAD bit is not set, it will affect the
04502               addresses of sections after it. We have to update
04503               dot.  */
04504            if (os->region != NULL
04505               && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
04506                   || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
04507              {
04508               os->region->current = dot;
04509 
04510               if (check_regions)
04511                 /* Make sure the new address is within the region.  */
04512                 os_region_check (os, os->region, os->addr_tree,
04513                                os->bfd_section->vma);
04514 
04515               if (os->lma_region != NULL && os->lma_region != os->region)
04516                 {
04517                   os->lma_region->current
04518                     = os->bfd_section->lma + TO_ADDR (os->bfd_section->size);
04519 
04520                   if (check_regions)
04521                     os_region_check (os, os->lma_region, NULL,
04522                                    os->bfd_section->lma);
04523                 }
04524              }
04525          }
04526          break;
04527 
04528        case lang_constructors_statement_enum:
04529          dot = lang_size_sections_1 (constructor_list.head,
04530                                   output_section_statement,
04531                                   &s->wild_statement.children.head,
04532                                   fill, dot, relax, check_regions);
04533          break;
04534 
04535        case lang_data_statement_enum:
04536          {
04537            unsigned int size = 0;
04538 
04539            s->data_statement.output_offset =
04540              dot - output_section_statement->bfd_section->vma;
04541            s->data_statement.output_section =
04542              output_section_statement->bfd_section;
04543 
04544            /* We might refer to provided symbols in the expression, and
04545               need to mark them as needed.  */
04546            exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
04547 
04548            switch (s->data_statement.type)
04549              {
04550              default:
04551               abort ();
04552              case QUAD:
04553              case SQUAD:
04554               size = QUAD_SIZE;
04555               break;
04556              case LONG:
04557               size = LONG_SIZE;
04558               break;
04559              case SHORT:
04560               size = SHORT_SIZE;
04561               break;
04562              case BYTE:
04563               size = BYTE_SIZE;
04564               break;
04565              }
04566            if (size < TO_SIZE ((unsigned) 1))
04567              size = TO_SIZE ((unsigned) 1);
04568            dot += TO_ADDR (size);
04569            output_section_statement->bfd_section->size += size;
04570          }
04571          break;
04572 
04573        case lang_reloc_statement_enum:
04574          {
04575            int size;
04576 
04577            s->reloc_statement.output_offset =
04578              dot - output_section_statement->bfd_section->vma;
04579            s->reloc_statement.output_section =
04580              output_section_statement->bfd_section;
04581            size = bfd_get_reloc_size (s->reloc_statement.howto);
04582            dot += TO_ADDR (size);
04583            output_section_statement->bfd_section->size += size;
04584          }
04585          break;
04586 
04587        case lang_wild_statement_enum:
04588          dot = lang_size_sections_1 (s->wild_statement.children.head,
04589                                   output_section_statement,
04590                                   &s->wild_statement.children.head,
04591                                   fill, dot, relax, check_regions);
04592          break;
04593 
04594        case lang_object_symbols_statement_enum:
04595          link_info.create_object_symbols_section =
04596            output_section_statement->bfd_section;
04597          break;
04598 
04599        case lang_output_statement_enum:
04600        case lang_target_statement_enum:
04601          break;
04602 
04603        case lang_input_section_enum:
04604          {
04605            asection *i;
04606 
04607            i = (*prev)->input_section.section;
04608            if (relax)
04609              {
04610               bfd_boolean again;
04611 
04612               if (! bfd_relax_section (i->owner, i, &link_info, &again))
04613                 einfo (_("%P%F: can't relax section: %E\n"));
04614               if (again)
04615                 *relax = TRUE;
04616              }
04617            dot = size_input_section (prev, output_section_statement,
04618                                   output_section_statement->fill, dot);
04619          }
04620          break;
04621 
04622        case lang_input_statement_enum:
04623          break;
04624 
04625        case lang_fill_statement_enum:
04626          s->fill_statement.output_section =
04627            output_section_statement->bfd_section;
04628 
04629          fill = s->fill_statement.fill;
04630          break;
04631 
04632        case lang_assignment_statement_enum:
04633          {
04634            bfd_vma newdot = dot;
04635            etree_type *tree = s->assignment_statement.exp;
04636 
04637            exp_fold_tree (tree,
04638                         output_section_statement->bfd_section,
04639                         &newdot);
04640 
04641            /* This symbol is relative to this section.  */
04642            if ((tree->type.node_class == etree_provided
04643                || tree->type.node_class == etree_assign)
04644               && (tree->assign.dst [0] != '.'
04645                   || tree->assign.dst [1] != '\0'))
04646              output_section_statement->section_relative_symbol = 1;
04647 
04648            if (!output_section_statement->ignored)
04649              {
04650               if (output_section_statement == abs_output_section)
04651                 {
04652                   /* If we don't have an output section, then just adjust
04653                      the default memory address.  */
04654                   lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
04655                                           FALSE)->current = newdot;
04656                 }
04657               else if (newdot != dot)
04658                 {
04659                   /* Insert a pad after this statement.  We can't
04660                      put the pad before when relaxing, in case the
04661                      assignment references dot.  */
04662                   insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
04663                             output_section_statement->bfd_section, dot);
04664 
04665                   /* Don't neuter the pad below when relaxing.  */
04666                   s = s->header.next;
04667 
04668                   /* If dot is advanced, this implies that the section
04669                      should have space allocated to it, unless the
04670                      user has explicitly stated that the section
04671                      should never be loaded.  */
04672                   if (!(output_section_statement->flags
04673                        & (SEC_NEVER_LOAD | SEC_ALLOC)))
04674                     output_section_statement->bfd_section->flags |= SEC_ALLOC;
04675                 }
04676               dot = newdot;
04677              }
04678          }
04679          break;
04680 
04681        case lang_padding_statement_enum:
04682          /* If this is the first time lang_size_sections is called,
04683             we won't have any padding statements.  If this is the
04684             second or later passes when relaxing, we should allow
04685             padding to shrink.  If padding is needed on this pass, it
04686             will be added back in.  */
04687          s->padding_statement.size = 0;
04688 
04689          /* Make sure output_offset is valid.  If relaxation shrinks
04690             the section and this pad isn't needed, it's possible to
04691             have output_offset larger than the final size of the
04692             section.  bfd_set_section_contents will complain even for
04693             a pad size of zero.  */
04694          s->padding_statement.output_offset
04695            = dot - output_section_statement->bfd_section->vma;
04696          break;
04697 
04698        case lang_group_statement_enum:
04699          dot = lang_size_sections_1 (s->group_statement.children.head,
04700                                   output_section_statement,
04701                                   &s->group_statement.children.head,
04702                                   fill, dot, relax, check_regions);
04703          break;
04704 
04705        default:
04706          FAIL ();
04707          break;
04708 
04709          /* We can only get here when relaxing is turned on.  */
04710        case lang_address_statement_enum:
04711          break;
04712        }
04713       prev = &s->header.next;
04714     }
04715   return dot;
04716 }
04717 
04718 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
04719    The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
04720    CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
04721    segments.  We are allowed an opportunity to override this decision.  */
04722 
04723 bfd_boolean
04724 ldlang_override_segment_assignment (struct bfd_link_info * info ATTRIBUTE_UNUSED,
04725                                 bfd * abfd ATTRIBUTE_UNUSED,
04726                                 asection * current_section,
04727                                 asection * previous_section,
04728                                 bfd_boolean new_segment)
04729 {
04730   lang_output_section_statement_type * cur;
04731   lang_output_section_statement_type * prev;
04732 
04733   /* The checks below are only necessary when the BFD library has decided
04734      that the two sections ought to be placed into the same segment.  */
04735   if (new_segment)
04736     return TRUE;
04737 
04738   /* Paranoia checks.  */
04739   if (current_section == NULL || previous_section == NULL)
04740     return new_segment;
04741 
04742   /* Find the memory regions associated with the two sections.
04743      We call lang_output_section_find() here rather than scanning the list
04744      of output sections looking for a matching section pointer because if
04745      we have a large number of sections then a hash lookup is faster.  */
04746   cur  = lang_output_section_find (current_section->name);
04747   prev = lang_output_section_find (previous_section->name);
04748 
04749   /* More paranoia.  */
04750   if (cur == NULL || prev == NULL)
04751     return new_segment;
04752 
04753   /* If the regions are different then force the sections to live in
04754      different segments.  See the email thread starting at the following
04755      URL for the reasons why this is necessary:
04756      http://sourceware.org/ml/binutils/2007-02/msg00216.html  */
04757   return cur->region != prev->region;
04758 }
04759 
04760 void
04761 one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
04762 {
04763   lang_statement_iteration++;
04764   lang_size_sections_1 (statement_list.head, abs_output_section,
04765                      &statement_list.head, 0, 0, relax, check_regions);
04766 }
04767 
04768 void
04769 lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
04770 {
04771   expld.phase = lang_allocating_phase_enum;
04772   expld.dataseg.phase = exp_dataseg_none;
04773 
04774   one_lang_size_sections_pass (relax, check_regions);
04775   if (expld.dataseg.phase == exp_dataseg_end_seen
04776       && link_info.relro && expld.dataseg.relro_end)
04777     {
04778       /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
04779         to put expld.dataseg.relro on a (common) page boundary.  */
04780       bfd_vma old_min_base, relro_end, maxpage;
04781 
04782       expld.dataseg.phase = exp_dataseg_relro_adjust;
04783       old_min_base = expld.dataseg.min_base;
04784       maxpage = expld.dataseg.maxpagesize;
04785       expld.dataseg.base += (-expld.dataseg.relro_end
04786                           & (expld.dataseg.pagesize - 1));
04787       /* Compute the expected PT_GNU_RELRO segment end.  */
04788       relro_end = (expld.dataseg.relro_end + expld.dataseg.pagesize - 1)
04789                 & ~(expld.dataseg.pagesize - 1);
04790       if (old_min_base + maxpage < expld.dataseg.base)
04791        {
04792          expld.dataseg.base -= maxpage;
04793          relro_end -= maxpage;
04794        }
04795       lang_reset_memory_regions ();
04796       one_lang_size_sections_pass (relax, check_regions);
04797       if (expld.dataseg.relro_end > relro_end)
04798        {
04799          /* The alignment of sections between DATA_SEGMENT_ALIGN
04800             and DATA_SEGMENT_RELRO_END caused huge padding to be
04801             inserted at DATA_SEGMENT_RELRO_END.  Try some other base.  */
04802          asection *sec;
04803          unsigned int max_alignment_power = 0;
04804 
04805          /* Find maximum alignment power of sections between
04806             DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END.  */
04807          for (sec = output_bfd->sections; sec; sec = sec->next)
04808            if (sec->vma >= expld.dataseg.base
04809               && sec->vma < expld.dataseg.relro_end
04810               && sec->alignment_power > max_alignment_power)
04811              max_alignment_power = sec->alignment_power;
04812 
04813          if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize)
04814            {
04815              if (expld.dataseg.base - (1 << max_alignment_power)
04816                 < old_min_base)
04817               expld.dataseg.base += expld.dataseg.pagesize;
04818              expld.dataseg.base -= (1 << max_alignment_power);
04819              lang_reset_memory_regions ();
04820              one_lang_size_sections_pass (relax, check_regions);
04821            }
04822        }
04823       link_info.relro_start = expld.dataseg.base;
04824       link_info.relro_end = expld.dataseg.relro_end;
04825     }
04826   else if (expld.dataseg.phase == exp_dataseg_end_seen)
04827     {
04828       /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
04829         a page could be saved in the data segment.  */
04830       bfd_vma first, last;
04831 
04832       first = -expld.dataseg.base & (expld.dataseg.pagesize - 1);
04833       last = expld.dataseg.end & (expld.dataseg.pagesize - 1);
04834       if (first && last
04835          && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1))
04836              != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1)))
04837          && first + last <= expld.dataseg.pagesize)
04838        {
04839          expld.dataseg.phase = exp_dataseg_adjust;
04840          lang_reset_memory_regions ();
04841          one_lang_size_sections_pass (relax, check_regions);
04842        }
04843     }
04844 
04845   expld.phase = lang_final_phase_enum;
04846 }
04847 
04848 /* Worker function for lang_do_assignments.  Recursiveness goes here.  */
04849 
04850 static bfd_vma
04851 lang_do_assignments_1 (lang_statement_union_type *s,
04852                      lang_output_section_statement_type *current_os,
04853                      fill_type *fill,
04854                      bfd_vma dot)
04855 {
04856   for (; s != NULL; s = s->header.next)
04857     {
04858       switch (s->header.type)
04859        {
04860        case lang_constructors_statement_enum:
04861          dot = lang_do_assignments_1 (constructor_list.head,
04862                                    current_os, fill, dot);
04863          break;
04864 
04865        case lang_output_section_statement_enum:
04866          {
04867            lang_output_section_statement_type *os;
04868 
04869            os = &(s->output_section_statement);
04870            if (os->bfd_section != NULL && !os->ignored)
04871              {
04872               dot = os->bfd_section->vma;
04873 
04874               lang_do_assignments_1 (os->children.head, os, os->fill, dot);
04875 
04876               /* .tbss sections effectively have zero size.  */
04877               if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
04878                   || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
04879                   || link_info.relocatable)
04880                 dot += TO_ADDR (os->bfd_section->size);
04881              }
04882          }
04883          break;
04884 
04885        case lang_wild_statement_enum:
04886 
04887          dot = lang_do_assignments_1 (s->wild_statement.children.head,
04888                                    current_os, fill, dot);
04889          break;
04890 
04891        case lang_object_symbols_statement_enum:
04892        case lang_output_statement_enum:
04893        case lang_target_statement_enum:
04894          break;
04895 
04896        case lang_data_statement_enum:
04897          exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
04898          if (expld.result.valid_p)
04899            s->data_statement.value = (expld.result.value
04900                                    + expld.result.section->vma);
04901          else
04902            einfo (_("%F%P: invalid data statement\n"));
04903          {
04904            unsigned int size;
04905            switch (s->data_statement.type)
04906              {
04907              default:
04908               abort ();
04909              case QUAD:
04910              case SQUAD:
04911               size = QUAD_SIZE;
04912               break;
04913              case LONG:
04914               size = LONG_SIZE;
04915               break;
04916              case SHORT:
04917               size = SHORT_SIZE;
04918               break;
04919              case BYTE:
04920               size = BYTE_SIZE;
04921               break;
04922              }
04923            if (size < TO_SIZE ((unsigned) 1))
04924              size = TO_SIZE ((unsigned) 1);
04925            dot += TO_ADDR (size);
04926          }
04927          break;
04928 
04929        case lang_reloc_statement_enum:
04930          exp_fold_tree (s->reloc_statement.addend_exp,
04931                       bfd_abs_section_ptr, &dot);
04932          if (expld.result.valid_p)
04933            s->reloc_statement.addend_value = expld.result.value;
04934          else
04935            einfo (_("%F%P: invalid reloc statement\n"));
04936          dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
04937          break;
04938 
04939        case lang_input_section_enum:
04940          {
04941            asection *in = s->input_section.section;
04942 
04943            if ((in->flags & SEC_EXCLUDE) == 0)
04944              dot += TO_ADDR (in->size);
04945          }
04946          break;
04947 
04948        case lang_input_statement_enum:
04949          break;
04950 
04951        case lang_fill_statement_enum:
04952          fill = s->fill_statement.fill;
04953          break;
04954 
04955        case lang_assignment_statement_enum:
04956          exp_fold_tree (s->assignment_statement.exp,
04957                       current_os->bfd_section,
04958                       &dot);
04959          break;
04960 
04961        case lang_padding_statement_enum:
04962          dot += TO_ADDR (s->padding_statement.size);
04963          break;
04964 
04965        case lang_group_statement_enum:
04966          dot = lang_do_assignments_1 (s->group_statement.children.head,
04967                                    current_os, fill, dot);
04968          break;
04969 
04970        default:
04971          FAIL ();
04972          break;
04973 
04974        case lang_address_statement_enum:
04975          break;
04976        }
04977     }
04978   return dot;
04979 }
04980 
04981 void
04982 lang_do_assignments (void)
04983 {
04984   lang_statement_iteration++;
04985   lang_do_assignments_1 (statement_list.head, abs_output_section, NULL, 0);
04986 }
04987 
04988 /* Fix any .startof. or .sizeof. symbols.  When the assemblers see the
04989    operator .startof. (section_name), it produces an undefined symbol
04990    .startof.section_name.  Similarly, when it sees
04991    .sizeof. (section_name), it produces an undefined symbol
04992    .sizeof.section_name.  For all the output sections, we look for
04993    such symbols, and set them to the correct value.  */
04994 
04995 static void
04996 lang_set_startof (void)
04997 {
04998   asection *s;
04999 
05000   if (link_info.relocatable)
05001     return;
05002 
05003   for (s = output_bfd->sections; s != NULL; s = s->next)
05004     {
05005       const char *secname;
05006       char *buf;
05007       struct bfd_link_hash_entry *h;
05008 
05009       secname = bfd_get_section_name (output_bfd, s);
05010       buf = xmalloc (10 + strlen (secname));
05011 
05012       sprintf (buf, ".startof.%s", secname);
05013       h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
05014       if (h != NULL && h->type == bfd_link_hash_undefined)
05015        {
05016          h->type = bfd_link_hash_defined;
05017          h->u.def.value = bfd_get_section_vma (output_bfd, s);
05018          h->u.def.section = bfd_abs_section_ptr;
05019        }
05020 
05021       sprintf (buf, ".sizeof.%s", secname);
05022       h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
05023       if (h != NULL && h->type == bfd_link_hash_undefined)
05024        {
05025          h->type = bfd_link_hash_defined;
05026          h->u.def.value = TO_ADDR (s->size);
05027          h->u.def.section = bfd_abs_section_ptr;
05028        }
05029 
05030       free (buf);
05031     }
05032 }
05033 
05034 static void
05035 lang_end (void)
05036 {
05037   struct bfd_link_hash_entry *h;
05038   bfd_boolean warn;
05039 
05040   if (link_info.relocatable || link_info.shared)
05041     warn = FALSE;
05042   else
05043     warn = TRUE;
05044 
05045   if (entry_symbol.name == NULL)
05046     {
05047       /* No entry has been specified.  Look for the default entry, but
05048         don't warn if we don't find it.  */
05049       entry_symbol.name = entry_symbol_default;
05050       warn = FALSE;
05051     }
05052 
05053   h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
05054                          FALSE, FALSE, TRUE);
05055   if (h != NULL
05056       && (h->type == bfd_link_hash_defined
05057          || h->type == bfd_link_hash_defweak)
05058       && h->u.def.section->output_section != NULL)
05059     {
05060       bfd_vma val;
05061 
05062       val = (h->u.def.value
05063             + bfd_get_section_vma (output_bfd,
05064                                 h->u.def.section->output_section)
05065             + h->u.def.section->output_offset);
05066       if (! bfd_set_start_address (output_bfd, val))
05067        einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
05068     }
05069   else
05070     {
05071       bfd_vma val;
05072       const char *send;
05073 
05074       /* We couldn't find the entry symbol.  Try parsing it as a
05075         number.  */
05076       val = bfd_scan_vma (entry_symbol.name, &send, 0);
05077       if (*send == '\0')
05078        {
05079          if (! bfd_set_start_address (output_bfd, val))
05080            einfo (_("%P%F: can't set start address\n"));
05081        }
05082       else
05083        {
05084          asection *ts;
05085 
05086          /* Can't find the entry symbol, and it's not a number.  Use
05087             the first address in the text section.  */
05088          ts = bfd_get_section_by_name (output_bfd, entry_section);
05089          if (ts != NULL)
05090            {
05091              if (warn)
05092               einfo (_("%P: warning: cannot find entry symbol %s;"
05093                       " defaulting to %V\n"),
05094                      entry_symbol.name,
05095                      bfd_get_section_vma (output_bfd, ts));
05096              if (! bfd_set_start_address (output_bfd,
05097                                       bfd_get_section_vma (output_bfd,
05098                                                         ts)))
05099               einfo (_("%P%F: can't set start address\n"));
05100            }
05101          else
05102            {
05103              if (warn)
05104               einfo (_("%P: warning: cannot find entry symbol %s;"
05105                       " not setting start address\n"),
05106                      entry_symbol.name);
05107            }
05108        }
05109     }
05110 
05111   /* Don't bfd_hash_table_free (&lang_definedness_table);
05112      map file output may result in a call of lang_track_definedness.  */
05113 }
05114 
05115 /* This is a small function used when we want to ignore errors from
05116    BFD.  */
05117 
05118 static void
05119 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
05120 {
05121   /* Don't do anything.  */
05122 }
05123 
05124 /* Check that the architecture of all the input files is compatible
05125    with the output file.  Also call the backend to let it do any
05126    other checking that is needed.  */
05127 
05128 static void
05129 lang_check (void)
05130 {
05131   lang_statement_union_type *file;
05132   bfd *input_bfd;
05133   const bfd_arch_info_type *compatible;
05134 
05135   for (file = file_chain.head; file != NULL; file = file->input_statement.next)
05136     {
05137       input_bfd = file->input_statement.the_bfd;
05138       compatible
05139        = bfd_arch_get_compatible (input_bfd, output_bfd,
05140                                command_line.accept_unknown_input_arch);
05141 
05142       /* In general it is not possible to perform a relocatable
05143         link between differing object formats when the input
05144         file has relocations, because the relocations in the
05145         input format may not have equivalent representations in
05146         the output format (and besides BFD does not translate
05147         relocs for other link purposes than a final link).  */
05148       if ((link_info.relocatable || link_info.emitrelocations)
05149          && (compatible == NULL
05150              || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
05151          && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
05152        {
05153          einfo (_("%P%F: Relocatable linking with relocations from"
05154                  " format %s (%B) to format %s (%B) is not supported\n"),
05155                bfd_get_target (input_bfd), input_bfd,
05156                bfd_get_target (output_bfd), output_bfd);
05157          /* einfo with %F exits.  */
05158        }
05159 
05160       if (compatible == NULL)
05161        {
05162          if (command_line.warn_mismatch)
05163            einfo (_("%P: warning: %s architecture of input file `%B'"
05164                    " is incompatible with %s output\n"),
05165                  bfd_printable_name (input_bfd), input_bfd,
05166                  bfd_printable_name (output_bfd));
05167        }
05168       else if (bfd_count_sections (input_bfd))
05169        {
05170          /* If the input bfd has no contents, it shouldn't set the
05171             private data of the output bfd.  */
05172 
05173          bfd_error_handler_type pfn = NULL;
05174 
05175          /* If we aren't supposed to warn about mismatched input
05176             files, temporarily set the BFD error handler to a
05177             function which will do nothing.  We still want to call
05178             bfd_merge_private_bfd_data, since it may set up
05179             information which is needed in the output file.  */
05180          if (! command_line.warn_mismatch)
05181            pfn = bfd_set_error_handler (ignore_bfd_errors);
05182          if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
05183            {
05184              if (command_line.warn_mismatch)
05185               einfo (_("%P%X: failed to merge target specific data"
05186                       " of file %B\n"), input_bfd);
05187            }
05188          if (! command_line.warn_mismatch)
05189            bfd_set_error_handler (pfn);
05190        }
05191     }
05192 }
05193 
05194 /* Look through all the global common symbols and attach them to the
05195    correct section.  The -sort-common command line switch may be used
05196    to roughly sort the entries by size.  */
05197 
05198 static void
05199 lang_common (void)
05200 {
05201   if (command_line.inhibit_common_definition)
05202     return;
05203   if (link_info.relocatable
05204       && ! command_line.force_common_definition)
05205     return;
05206 
05207   if (! config.sort_common)
05208     bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
05209   else
05210     {
05211       int power;
05212 
05213       for (power = 4; power >= 0; power--)
05214        bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
05215     }
05216 }
05217 
05218 /* Place one common symbol in the correct section.  */
05219 
05220 static bfd_boolean
05221 lang_one_common (struct bfd_link_hash_entry *h, void *info)
05222 {
05223   unsigned int power_of_two;
05224   bfd_vma size;
05225   asection *section;
05226 
05227   if (h->type != bfd_link_hash_common)
05228     return TRUE;
05229 
05230   size = h->u.c.size;
05231   power_of_two = h->u.c.p->alignment_power;
05232 
05233   if (config.sort_common
05234       && power_of_two < (unsigned int) *(int *) info)
05235     return TRUE;
05236 
05237   section = h->u.c.p->section;
05238 
05239   /* Increase the size of the section to align the common sym.  */
05240   section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
05241   section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
05242 
05243   /* Adjust the alignment if necessary.  */
05244   if (power_of_two > section->alignment_power)
05245     section->alignment_power = power_of_two;
05246 
05247   /* Change the symbol from common to defined.  */
05248   h->type = bfd_link_hash_defined;
05249   h->u.def.section = section;
05250   h->u.def.value = section->size;
05251 
05252   /* Increase the size of the section.  */
05253   section->size += size;
05254 
05255   /* Make sure the section is allocated in memory, and make sure that
05256      it is no longer a common section.  */
05257   section->flags |= SEC_ALLOC;
05258   section->flags &= ~SEC_IS_COMMON;
05259 
05260   if (config.map_file != NULL)
05261     {
05262       static bfd_boolean header_printed;
05263       int len;
05264       char *name;
05265       char buf[50];
05266 
05267       if (! header_printed)
05268        {
05269          minfo (_("\nAllocating common symbols\n"));
05270          minfo (_("Common symbol       size              file\n\n"));
05271          header_printed = TRUE;
05272        }
05273 
05274       name = demangle (h->root.string);
05275       minfo ("%s", name);
05276       len = strlen (name);
05277       free (name);
05278 
05279       if (len >= 19)
05280        {
05281          print_nl ();
05282          len = 0;
05283        }
05284       while (len < 20)
05285        {
05286          print_space ();
05287          ++len;
05288        }
05289 
05290       minfo ("0x");
05291       if (size <= 0xffffffff)
05292        sprintf (buf, "%lx", (unsigned long) size);
05293       else
05294        sprintf_vma (buf, size);
05295       minfo ("%s", buf);
05296       len = strlen (buf);
05297 
05298       while (len < 16)
05299        {
05300          print_space ();
05301          ++len;
05302        }
05303 
05304       minfo ("%B\n", section->owner);
05305     }
05306 
05307   return TRUE;
05308 }
05309 
05310 /* Run through the input files and ensure that every input section has
05311    somewhere to go.  If one is found without a destination then create
05312    an input request and place it into the statement tree.  */
05313 
05314 static void
05315 lang_place_orphans (void)
05316 {
05317   LANG_FOR_EACH_INPUT_STATEMENT (file)
05318     {
05319       asection *s;
05320 
05321       for (s = file->the_bfd->sections; s != NULL; s = s->next)
05322        {
05323          if (s->output_section == NULL)
05324            {
05325              /* This section of the file is not attached, root
05326                around for a sensible place for it to go.  */
05327 
05328              if (file->just_syms_flag)
05329               bfd_link_just_syms (file->the_bfd, s, &link_info);
05330              else if ((s->flags & SEC_EXCLUDE) != 0)
05331               s->output_section = bfd_abs_section_ptr;
05332              else if (strcmp (s->name, "COMMON") == 0)
05333               {
05334                 /* This is a lonely common section which must have
05335                    come from an archive.  We attach to the section
05336                    with the wildcard.  */
05337                 if (! link_info.relocatable
05338                     || command_line.force_common_definition)
05339                   {
05340                     if (default_common_section == NULL)
05341                      {
05342                        default_common_section =
05343                          lang_output_section_statement_lookup (".bss");
05344 
05345                      }
05346                     lang_add_section (&default_common_section->children, s,
05347                                    default_common_section);
05348                   }
05349               }
05350              else if (ldemul_place_orphan (s))
05351               ;
05352              else
05353               {
05354                 lang_output_section_statement_type *os;
05355 
05356                 os = lang_output_section_statement_lookup (s->name);
05357                 lang_add_section (&os->children, s, os);
05358               }
05359            }
05360        }
05361     }
05362 }
05363 
05364 void
05365 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
05366 {
05367   flagword *ptr_flags;
05368 
05369   ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
05370   while (*flags)
05371     {
05372       switch (*flags)
05373        {
05374        case 'A': case 'a':
05375          *ptr_flags |= SEC_ALLOC;
05376          break;
05377 
05378        case 'R': case 'r':
05379          *ptr_flags |= SEC_READONLY;
05380          break;
05381 
05382        case 'W': case 'w':
05383          *ptr_flags |= SEC_DATA;
05384          break;
05385 
05386        case 'X': case 'x':
05387          *ptr_flags |= SEC_CODE;
05388          break;
05389 
05390        case 'L': case 'l':
05391        case 'I': case 'i':
05392          *ptr_flags |= SEC_LOAD;
05393          break;
05394 
05395        default:
05396          einfo (_("%P%F: invalid syntax in flags\n"));
05397          break;
05398        }
05399       flags++;
05400     }
05401 }
05402 
05403 /* Call a function on each input file.  This function will be called
05404    on an archive, but not on the elements.  */
05405 
05406 void
05407 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
05408 {
05409   lang_input_statement_type *f;
05410 
05411   for (f = (lang_input_statement_type *) input_file_chain.head;
05412        f != NULL;
05413        f = (lang_input_statement_type *) f->next_real_file)
05414     func (f);
05415 }
05416 
05417 /* Call a function on each file.  The function will be called on all
05418    the elements of an archive which are included in the link, but will
05419    not be called on the archive file itself.  */
05420 
05421 void
05422 lang_for_each_file (void (*func) (lang_input_statement_type *))
05423 {
05424   LANG_FOR_EACH_INPUT_STATEMENT (f)
05425     {
05426       func (f);
05427     }
05428 }
05429 
05430 void
05431 ldlang_add_file (lang_input_statement_type *entry)
05432 {
05433   bfd **pp;
05434 
05435   lang_statement_append (&file_chain,
05436                       (lang_statement_union_type *) entry,
05437                       &entry->next);
05438 
05439   /* The BFD linker needs to have a list of all input BFDs involved in
05440      a link.  */
05441   ASSERT (entry->the_bfd->link_next == NULL);
05442   ASSERT (entry->the_bfd != output_bfd);
05443   for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
05444     ;
05445   *pp = entry->the_bfd;
05446   entry->the_bfd->usrdata = entry;
05447   bfd_set_gp_size (entry->the_bfd, g_switch_value);
05448 
05449   /* Look through the sections and check for any which should not be
05450      included in the link.  We need to do this now, so that we can
05451      notice when the backend linker tries to report multiple
05452      definition errors for symbols which are in sections we aren't
05453      going to link.  FIXME: It might be better to entirely ignore
05454      symbols which are defined in sections which are going to be
05455      discarded.  This would require modifying the backend linker for
05456      each backend which might set the SEC_LINK_ONCE flag.  If we do
05457      this, we should probably handle SEC_EXCLUDE in the same way.  */
05458 
05459   bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
05460 }
05461 
05462 void
05463 lang_add_output (const char *name, int from_script)
05464 {
05465   /* Make -o on command line override OUTPUT in script.  */
05466   if (!had_output_filename || !from_script)
05467     {
05468       output_filename = name;
05469       had_output_filename = TRUE;
05470     }
05471 }
05472 
05473 static lang_output_section_statement_type *current_section;
05474 
05475 static int
05476 topower (int x)
05477 {
05478   unsigned int i = 1;
05479   int l;
05480 
05481   if (x < 0)
05482     return -1;
05483 
05484   for (l = 0; l < 32; l++)
05485     {
05486       if (i >= (unsigned int) x)
05487        return l;
05488       i <<= 1;
05489     }
05490 
05491   return 0;
05492 }
05493 
05494 lang_output_section_statement_type *
05495 lang_enter_output_section_statement (const char *output_section_statement_name,
05496                                  etree_type *address_exp,
05497                                  enum section_type sectype,
05498                                  etree_type *align,
05499                                  etree_type *subalign,
05500                                  etree_type *ebase,
05501                                  int constraint)
05502 {
05503   lang_output_section_statement_type *os;
05504 
05505    os = lang_output_section_statement_lookup_1 (output_section_statement_name,
05506                                           constraint);
05507    current_section = os;
05508 
05509   /* Make next things chain into subchain of this.  */
05510 
05511   if (os->addr_tree == NULL)
05512     {
05513       os->addr_tree = address_exp;
05514     }
05515   os->sectype = sectype;
05516   if (sectype != noload_section)
05517     os->flags = SEC_NO_FLAGS;
05518   else
05519     os->flags = SEC_NEVER_LOAD;
05520   os->block_value = 1;
05521   stat_ptr = &os->children;
05522 
05523   os->subsection_alignment =
05524     topower (exp_get_value_int (subalign, -1, "subsection alignment"));
05525   os->section_alignment =
05526     topower (exp_get_value_int (align, -1, "section alignment"));
05527 
05528   os->load_base = ebase;
05529   return os;
05530 }
05531 
05532 void
05533 lang_final (void)
05534 {
05535   lang_output_statement_type *new;
05536 
05537   new = new_stat (lang_output_statement, stat_ptr);
05538   new->name = output_filename;
05539 }
05540 
05541 /* Reset the current counters in the regions.  */
05542 
05543 void
05544 lang_reset_memory_regions (void)
05545 {
05546   lang_memory_region_type *p = lang_memory_region_list;
05547   asection *o;
05548   lang_output_section_statement_type *os;
05549 
05550   for (p = lang_memory_region_list; p != NULL; p = p->next)
05551     {
05552       p->current = p->origin;
05553       p->last_os = NULL;
05554     }
05555 
05556   for (os = &lang_output_section_statement.head->output_section_statement;
05557        os != NULL;
05558        os = os->next)
05559     {
05560       os->processed_vma = FALSE;
05561       os->processed_lma = FALSE;
05562     }
05563 
05564   for (o = output_bfd->sections; o != NULL; o = o->next)
05565     {
05566       /* Save the last size for possible use by bfd_relax_section.  */
05567       o->rawsize = o->size;
05568       o->size = 0;
05569     }
05570 }
05571 
05572 /* Worker for lang_gc_sections_1.  */
05573 
05574 static void
05575 gc_section_callback (lang_wild_statement_type *ptr,
05576                    struct wildcard_list *sec ATTRIBUTE_UNUSED,
05577                    asection *section,
05578                    lang_input_statement_type *file ATTRIBUTE_UNUSED,
05579                    void *data ATTRIBUTE_UNUSED)
05580 {
05581   /* If the wild pattern was marked KEEP, the member sections
05582      should be as well.  */
05583   if (ptr->keep_sections)
05584     section->flags |= SEC_KEEP;
05585 }
05586 
05587 /* Iterate over sections marking them against GC.  */
05588 
05589 static void
05590 lang_gc_sections_1 (lang_statement_union_type *s)
05591 {
05592   for (; s != NULL; s = s->header.next)
05593     {
05594       switch (s->header.type)
05595        {
05596        case lang_wild_statement_enum:
05597          walk_wild (&s->wild_statement, gc_section_callback, NULL);
05598          break;
05599        case lang_constructors_statement_enum:
05600          lang_gc_sections_1 (constructor_list.head);
05601          break;
05602        case lang_output_section_statement_enum:
05603          lang_gc_sections_1 (s->output_section_statement.children.head);
05604          break;
05605        case lang_group_statement_enum:
05606          lang_gc_sections_1 (s->group_statement.children.head);
05607          break;
05608        default:
05609          break;
05610        }
05611     }
05612 }
05613 
05614 static void
05615 lang_gc_sections (void)
05616 {
05617   struct bfd_link_hash_entry *h;
05618   ldlang_undef_chain_list_type *ulist;
05619 
05620   /* Keep all sections so marked in the link script.  */
05621 
05622   lang_gc_sections_1 (statement_list.head);
05623 
05624   /* Keep all sections containing symbols undefined on the command-line,
05625      and the section containing the entry symbol.  */
05626 
05627   for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
05628     {
05629       h = bfd_link_hash_lookup (link_info.hash, ulist->name,
05630                             FALSE, FALSE, FALSE);
05631 
05632       if (h != NULL
05633          && (h->type == bfd_link_hash_defined
05634              || h->type == bfd_link_hash_defweak)
05635          && ! bfd_is_abs_section (h->u.def.section))
05636        {
05637          h->u.def.section->flags |= SEC_KEEP;
05638        }
05639     }
05640 
05641   /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
05642      the special case of debug info.  (See bfd/stabs.c)
05643      Twiddle the flag here, to simplify later linker code.  */
05644   if (link_info.relocatable)
05645     {
05646       LANG_FOR_EACH_INPUT_STATEMENT (f)
05647        {
05648          asection *sec;
05649          for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
05650            if ((sec->flags & SEC_DEBUGGING) == 0)
05651              sec->flags &= ~SEC_EXCLUDE;
05652        }
05653     }
05654 
05655   if (link_info.gc_sections)
05656     bfd_gc_sections (output_bfd, &link_info);
05657 }
05658 
05659 /* Relax all sections until bfd_relax_section gives up.  */
05660 
05661 static void
05662 relax_sections (void)
05663 {
05664   /* Keep relaxing until bfd_relax_section gives up.  */
05665   bfd_boolean relax_again;
05666 
05667   do
05668     {
05669       relax_again = FALSE;
05670 
05671       /* Note: pe-dll.c does something like this also.  If you find
05672         you need to change this code, you probably need to change
05673         pe-dll.c also.  DJ  */
05674 
05675       /* Do all the assignments with our current guesses as to
05676         section sizes.  */
05677       lang_do_assignments ();
05678 
05679       /* We must do this after lang_do_assignments, because it uses
05680         size.  */
05681       lang_reset_memory_regions ();
05682 
05683       /* Perform another relax pass - this time we know where the
05684         globals are, so can make a better guess.  */
05685       lang_size_sections (&relax_again, FALSE);
05686     }
05687   while (relax_again);
05688 }
05689 
05690 void
05691 lang_process (void)
05692 {
05693   /* Finalize dynamic list.  */
05694   if (link_info.dynamic_list)
05695     lang_finalize_version_expr_head (&link_info.dynamic_list->head);
05696 
05697   current_target = default_target;
05698 
05699   /* Open the output file.  */
05700   lang_for_each_statement (ldlang_open_output);
05701   init_opb ();
05702 
05703   ldemul_create_output_section_statements ();
05704 
05705   /* Add to the hash table all undefineds on the command line.  */
05706   lang_place_undefineds ();
05707 
05708   if (!bfd_section_already_linked_table_init ())
05709     einfo (_("%P%F: Failed to create hash table\n"));
05710 
05711   /* Create a bfd for each input file.  */
05712   current_target = default_target;
05713   open_input_bfds (statement_list.head, FALSE);
05714 
05715   link_info.gc_sym_list = &entry_symbol;
05716   if (entry_symbol.name == NULL)
05717     link_info.gc_sym_list = ldlang_undef_chain_list_head;
05718 
05719   ldemul_after_open ();
05720 
05721   bfd_section_already_linked_table_free ();
05722 
05723   /* Make sure that we're not mixing architectures.  We call this
05724      after all the input files have been opened, but before we do any
05725      other processing, so that any operations merge_private_bfd_data
05726      does on the output file will be known during the rest of the
05727      link.  */
05728   lang_check ();
05729 
05730   /* Handle .exports instead of a version script if we're told to do so.  */
05731   if (command_line.version_exports_section)
05732     lang_do_version_exports_section ();
05733 
05734   /* Build all sets based on the information gathered from the input
05735      files.  */
05736   ldctor_build_sets ();
05737 
05738   /* Remove unreferenced sections if asked to.  */
05739   lang_gc_sections ();
05740 
05741   /* Size up the common data.  */
05742   lang_common ();
05743 
05744   /* Update wild statements.  */
05745   update_wild_statements (statement_list.head);
05746 
05747   /* Run through the contours of the script and attach input sections
05748      to the correct output sections.  */
05749   map_input_to_output_sections (statement_list.head, NULL, NULL);
05750 
05751   /* Find any sections not attached explicitly and handle them.  */
05752   lang_place_orphans ();
05753 
05754   if (! link_info.relocatable)
05755     {
05756       asection *found;
05757 
05758       /* Merge SEC_MERGE sections.  This has to be done after GC of
05759         sections, so that GCed sections are not merged, but before
05760         assigning dynamic symbols, since removing whole input sections
05761         is hard then.  */
05762       bfd_merge_sections (output_bfd, &link_info);
05763 
05764       /* Look for a text section and set the readonly attribute in it.  */
05765       found = bfd_get_section_by_name (output_bfd, ".text");
05766 
05767       if (found != NULL)
05768        {
05769          if (config.text_read_only)
05770            found->flags |= SEC_READONLY;
05771          else
05772            found->flags &= ~SEC_READONLY;
05773        }
05774     }
05775 
05776   /* Do anything special before sizing sections.  This is where ELF
05777      and other back-ends size dynamic sections.  */
05778   ldemul_before_allocation ();
05779 
05780   /* We must record the program headers before we try to fix the
05781      section positions, since they will affect SIZEOF_HEADERS.  */
05782   lang_record_phdrs ();
05783 
05784   /* Size up the sections.  */
05785   lang_size_sections (NULL, !command_line.relax);
05786 
05787   /* Now run around and relax if we can.  */
05788   if (command_line.relax)
05789     {
05790       /* We may need more than one relaxation pass.  */
05791       int i = link_info.relax_pass;
05792 
05793       /* The backend can use it to determine the current pass.  */
05794       link_info.relax_pass = 0;
05795 
05796       while (i--)
05797        {
05798          relax_sections ();
05799          link_info.relax_pass++;
05800        }
05801 
05802       /* Final extra sizing to report errors.  */
05803       lang_do_assignments ();
05804       lang_reset_memory_regions ();
05805       lang_size_sections (NULL, TRUE);
05806     }
05807 
05808   /* See if anything special should be done now we know how big
05809      everything is.  */
05810   ldemul_after_allocation ();
05811 
05812   /* Fix any .startof. or .sizeof. symbols.  */
05813   lang_set_startof ();
05814 
05815   /* Do all the assignments, now that we know the final resting places
05816      of all the symbols.  */
05817 
05818   lang_do_assignments ();
05819 
05820   ldemul_finish ();
05821 
05822   /* Make sure that the section addresses make sense.  */
05823   if (! link_info.relocatable
05824       && command_line.check_section_addresses)
05825     lang_check_section_addresses ();
05826 
05827   lang_end ();
05828 }
05829 
05830 /* EXPORTED TO YACC */
05831 
05832 void
05833 lang_add_wild (struct wildcard_spec *filespec,
05834               struct wildcard_list *section_list,
05835               bfd_boolean keep_sections)
05836 {
05837   struct wildcard_list *curr, *next;
05838   lang_wild_statement_type *new;
05839 
05840   /* Reverse the list as the parser puts it back to front.  */
05841   for (curr = section_list, section_list = NULL;
05842        curr != NULL;
05843        section_list = curr, curr = next)
05844     {
05845       if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
05846        placed_commons = TRUE;
05847 
05848       next = curr->next;
05849       curr->next = section_list;
05850     }
05851 
05852   if (filespec != NULL && filespec->name != NULL)
05853     {
05854       if (strcmp (filespec->name, "*") == 0)
05855        filespec->name = NULL;
05856       else if (! wildcardp (filespec->name))
05857        lang_has_input_file = TRUE;
05858     }
05859 
05860   new = new_stat (lang_wild_statement, stat_ptr);
05861   new->filename = NULL;
05862   new->filenames_sorted = FALSE;
05863   if (filespec != NULL)
05864     {
05865       new->filename = filespec->name;
05866       new->filenames_sorted = filespec->sorted == by_name;
05867     }
05868   new->section_list = section_list;
05869   new->keep_sections = keep_sections;
05870   lang_list_init (&new->children);
05871   analyze_walk_wild_section_handler (new);
05872 }
05873 
05874 void
05875 lang_section_start (const char *name, etree_type *address,
05876                   const segment_type *segment)
05877 {
05878   lang_address_statement_type *ad;
05879 
05880   ad = new_stat (lang_address_statement, stat_ptr);
05881   ad->section_name = name;
05882   ad->address = address;
05883   ad->segment = segment;
05884 }
05885 
05886 /* Set the start symbol to NAME.  CMDLINE is nonzero if this is called
05887    because of a -e argument on the command line, or zero if this is
05888    called by ENTRY in a linker script.  Command line arguments take
05889    precedence.  */
05890 
05891 void
05892 lang_add_entry (const char *name, bfd_boolean cmdline)
05893 {
05894   if (entry_symbol.name == NULL
05895       || cmdline
05896       || ! entry_from_cmdline)
05897     {
05898       entry_symbol.name = name;
05899       entry_from_cmdline = cmdline;
05900     }
05901 }
05902 
05903 /* Set the default start symbol to NAME.  .em files should use this,
05904    not lang_add_entry, to override the use of "start" if neither the
05905    linker script nor the command line specifies an entry point.  NAME
05906    must be permanently allocated.  */
05907 void
05908 lang_default_entry (const char *name)
05909 {
05910   entry_symbol_default = name;
05911 }
05912 
05913 void
05914 lang_add_target (const char *name)
05915 {
05916   lang_target_statement_type *new;
05917 
05918   new = new_stat (lang_target_statement, stat_ptr);
05919   new->target = name;
05920 }
05921 
05922 void
05923 lang_add_map (const char *name)
05924 {
05925   while (*name)
05926     {
05927       switch (*name)
05928        {
05929        case 'F':
05930          map_option_f = TRUE;
05931          break;
05932        }
05933       name++;
05934     }
05935 }
05936 
05937 void
05938 lang_add_fill (fill_type *fill)
05939 {
05940   lang_fill_statement_type *new;
05941 
05942   new = new_stat (lang_fill_statement, stat_ptr);
05943   new->fill = fill;
05944 }
05945 
05946 void
05947 lang_add_data (int type, union etree_union *exp)
05948 {
05949   lang_data_statement_type *new;
05950 
05951   new = new_stat (lang_data_statement, stat_ptr);
05952   new->exp = exp;
05953   new->type = type;
05954 }
05955 
05956 /* Create a new reloc statement.  RELOC is the BFD relocation type to
05957    generate.  HOWTO is the corresponding howto structure (we could
05958    look this up, but the caller has already done so).  SECTION is the
05959    section to generate a reloc against, or NAME is the name of the
05960    symbol to generate a reloc against.  Exactly one of SECTION and
05961    NAME must be NULL.  ADDEND is an expression for the addend.  */
05962 
05963 void
05964 lang_add_reloc (bfd_reloc_code_real_type reloc,
05965               reloc_howto_type *howto,
05966               asection *section,
05967               const char *name,
05968               union etree_union *addend)
05969 {
05970   lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
05971 
05972   p->reloc = reloc;
05973   p->howto = howto;
05974   p->section = section;
05975   p->name = name;
05976   p->addend_exp = addend;
05977 
05978   p->addend_value = 0;
05979   p->output_section = NULL;
05980   p->output_offset = 0;
05981 }
05982 
05983 lang_assignment_statement_type *
05984 lang_add_assignment (etree_type *exp)
05985 {
05986   lang_assignment_statement_type *new;
05987 
05988   new = new_stat (lang_assignment_statement, stat_ptr);
05989   new->exp = exp;
05990   return new;
05991 }
05992 
05993 void
05994 lang_add_attribute (enum statement_enum attribute)
05995 {
05996   new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
05997 }
05998 
05999 void
06000 lang_startup (const char *name)
06001 {
06002   if (startup_file != NULL)
06003     {
06004       einfo (_("%P%F: multiple STARTUP files\n"));
06005     }
06006   first_file->filename = name;
06007   first_file->local_sym_name = name;
06008   first_file->real = TRUE;
06009 
06010   startup_file = name;
06011 }
06012 
06013 void
06014 lang_float (bfd_boolean maybe)
06015 {
06016   lang_float_flag = maybe;
06017 }
06018 
06019 
06020 /* Work out the load- and run-time regions from a script statement, and
06021    store them in *LMA_REGION and *REGION respectively.
06022 
06023    MEMSPEC is the name of the run-time region, or the value of
06024    DEFAULT_MEMORY_REGION if the statement didn't specify one.
06025    LMA_MEMSPEC is the name of the load-time region, or null if the
06026    statement didn't specify one.HAVE_LMA_P is TRUE if the statement
06027    had an explicit load address.
06028 
06029    It is an error to specify both a load region and a load address.  */
06030 
06031 static void
06032 lang_get_regions (lang_memory_region_type **region,
06033                 lang_memory_region_type **lma_region,
06034                 const char *memspec,
06035                 const char *lma_memspec,
06036                 bfd_boolean have_lma,
06037                 bfd_boolean have_vma)
06038 {
06039   *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
06040 
06041   /* If no runtime region or VMA has been specified, but the load region
06042      has been specified, then use the load region for the runtime region
06043      as well.  */
06044   if (lma_memspec != NULL
06045       && ! have_vma
06046       && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
06047     *region = *lma_region;
06048   else
06049     *region = lang_memory_region_lookup (memspec, FALSE);
06050 
06051   if (have_lma && lma_memspec != 0)
06052     einfo (_("%X%P:%S: section has both a load address and a load region\n"));
06053 }
06054 
06055 void
06056 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
06057                                  lang_output_section_phdr_list *phdrs,
06058                                  const char *lma_memspec)
06059 {
06060   lang_get_regions (&current_section->region,
06061                   &current_section->lma_region,
06062                   memspec, lma_memspec,
06063                   current_section->load_base != NULL,
06064                   current_section->addr_tree != NULL);
06065   current_section->fill = fill;
06066   current_section->phdrs = phdrs;
06067   stat_ptr = &statement_list;
06068 }
06069 
06070 /* Create an absolute symbol with the given name with the value of the
06071    address of first byte of the section named.
06072 
06073    If the symbol already exists, then do nothing.  */
06074 
06075 void
06076 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
06077 {
06078   struct bfd_link_hash_entry *h;
06079 
06080   h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
06081   if (h == NULL)
06082     einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
06083 
06084   if (h->type == bfd_link_hash_new
06085       || h->type == bfd_link_hash_undefined)
06086     {
06087       asection *sec;
06088 
06089       h->type = bfd_link_hash_defined;
06090 
06091       sec = bfd_get_section_by_name (output_bfd, secname);
06092       if (sec == NULL)
06093        h->u.def.value = 0;
06094       else
06095        h->u.def.value = bfd_get_section_vma (output_bfd, sec);
06096 
06097       h->u.def.section = bfd_abs_section_ptr;
06098     }
06099 }
06100 
06101 /* Create an absolute symbol with the given name with the value of the
06102    address of the first byte after the end of the section named.
06103 
06104    If the symbol already exists, then do nothing.  */
06105 
06106 void
06107 lang_abs_symbol_at_end_of (const char *secname, const char *name)
06108 {
06109   struct bfd_link_hash_entry *h;
06110 
06111   h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
06112   if (h == NULL)
06113     einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
06114 
06115   if (h->type == bfd_link_hash_new
06116       || h->type == bfd_link_hash_undefined)
06117     {
06118       asection *sec;
06119 
06120       h->type = bfd_link_hash_defined;
06121 
06122       sec = bfd_get_section_by_name (output_bfd, secname);
06123       if (sec == NULL)
06124        h->u.def.value = 0;
06125       else
06126        h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
06127                        + TO_ADDR (sec->size));
06128 
06129       h->u.def.section = bfd_abs_section_ptr;
06130     }
06131 }
06132 
06133 void
06134 lang_statement_append (lang_statement_list_type *list,
06135                      lang_statement_union_type *element,
06136                      lang_statement_union_type **field)
06137 {
06138   *(list->tail) = element;
06139   list->tail = field;
06140 }
06141 
06142 /* Set the output format type.  -oformat overrides scripts.  */
06143 
06144 void
06145 lang_add_output_format (const char *format,
06146                      const char *big,
06147                      const char *little,
06148                      int from_script)
06149 {
06150   if (output_target == NULL || !from_script)
06151     {
06152       if (command_line.endian == ENDIAN_BIG
06153          && big != NULL)
06154        format = big;
06155       else if (command_line.endian == ENDIAN_LITTLE
06156               && little != NULL)
06157        format = little;
06158 
06159       output_target = format;
06160     }
06161 }
06162 
06163 /* Enter a group.  This creates a new lang_group_statement, and sets
06164    stat_ptr to build new statements within the group.  */
06165 
06166 void
06167 lang_enter_group (void)
06168 {
06169   lang_group_statement_type *g;
06170 
06171   g = new_stat (lang_group_statement, stat_ptr);
06172   lang_list_init (&g->children);
06173   stat_ptr = &g->children;
06174 }
06175 
06176 /* Leave a group.  This just resets stat_ptr to start writing to the
06177    regular list of statements again.  Note that this will not work if
06178    groups can occur inside anything else which can adjust stat_ptr,
06179    but currently they can't.  */
06180 
06181 void
06182 lang_leave_group (void)
06183 {
06184   stat_ptr = &statement_list;
06185 }
06186 
06187 /* Add a new program header.  This is called for each entry in a PHDRS
06188    command in a linker script.  */
06189 
06190 void
06191 lang_new_phdr (const char *name,
06192               etree_type *type,
06193               bfd_boolean filehdr,
06194               bfd_boolean phdrs,
06195               etree_type *at,
06196               etree_type *flags)
06197 {
06198   struct lang_phdr *n, **pp;
06199 
06200   n = stat_alloc (sizeof (struct lang_phdr));
06201   n->next = NULL;
06202   n->name = name;
06203   n->type = exp_get_value_int (type, 0, "program header type");
06204   n->filehdr = filehdr;
06205   n->phdrs = phdrs;
06206   n->at = at;
06207   n->flags = flags;
06208 
06209   for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
06210     ;
06211   *pp = n;
06212 }
06213 
06214 /* Record the program header information in the output BFD.  FIXME: We
06215    should not be calling an ELF specific function here.  */
06216 
06217 static void
06218 lang_record_phdrs (void)
06219 {
06220   unsigned int alc;
06221   asection **secs;
06222   lang_output_section_phdr_list *last;
06223   struct lang_phdr *l;
06224   lang_output_section_statement_type *os;
06225 
06226   alc = 10;
06227   secs = xmalloc (alc * sizeof (asection *));
06228   last = NULL;
06229 
06230   for (l = lang_phdr_list; l != NULL; l = l->next)
06231     {
06232       unsigned int c;
06233       flagword flags;
06234       bfd_vma at;
06235 
06236       c = 0;
06237       for (os = &lang_output_section_statement.head->output_section_statement;
06238           os != NULL;
06239           os = os->next)
06240        {
06241          lang_output_section_phdr_list *pl;
06242 
06243          if (os->constraint == -1)
06244            continue;
06245 
06246          pl = os->phdrs;
06247          if (pl != NULL)
06248            last = pl;
06249          else
06250            {
06251              if (os->sectype == noload_section
06252                 || os->bfd_section == NULL
06253                 || (os->bfd_section->flags & SEC_ALLOC) == 0)
06254               continue;
06255 
06256              if (last)
06257               pl = last;
06258              else
06259               {
06260                 lang_output_section_statement_type * tmp_os;
06261 
06262                 /* If we have not run across a section with a program
06263                    header assigned to it yet, then scan forwards to find
06264                    one.  This prevents inconsistencies in the linker's
06265                    behaviour when a script has specified just a single
06266                    header and there are sections in that script which are
06267                    not assigned to it, and which occur before the first
06268                    use of that header. See here for more details:
06269                    http://sourceware.org/ml/binutils/2007-02/msg00291.html  */
06270                 for (tmp_os = os; tmp_os; tmp_os = tmp_os->next)
06271                   if (tmp_os->phdrs)
06272                     break;
06273                 pl = tmp_os->phdrs;
06274               }
06275            }
06276 
06277          if (os->bfd_section == NULL)
06278            continue;
06279 
06280          for (; pl != NULL; pl = pl->next)
06281            {
06282              if (strcmp (pl->name, l->name) == 0)
06283               {
06284                 if (c >= alc)
06285                   {
06286                     alc *= 2;
06287                     secs = xrealloc (secs, alc * sizeof (asection *));
06288                   }
06289                 secs[c] = os->bfd_section;
06290                 ++c;
06291                 pl->used = TRUE;
06292               }
06293            }
06294        }
06295 
06296       if (l->flags == NULL)
06297        flags = 0;
06298       else
06299        flags = exp_get_vma (l->flags, 0, "phdr flags");
06300 
06301       if (l->at == NULL)
06302        at = 0;
06303       else
06304        at = exp_get_vma (l->at, 0, "phdr load address");
06305 
06306       if (! bfd_record_phdr (output_bfd, l->type,
06307                           l->flags != NULL, flags, l->at != NULL,
06308                           at, l->filehdr, l->phdrs, c, secs))
06309        einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
06310     }
06311 
06312   free (secs);
06313 
06314   /* Make sure all the phdr assignments succeeded.  */
06315   for (os = &lang_output_section_statement.head->output_section_statement;
06316        os != NULL;
06317        os = os->next)
06318     {
06319       lang_output_section_phdr_list *pl;
06320 
06321       if (os->constraint == -1
06322          || os->bfd_section == NULL)
06323        continue;
06324 
06325       for (pl = os->phdrs;
06326           pl != NULL;
06327           pl = pl->next)
06328        if (! pl->used && strcmp (pl->name, "NONE") != 0)
06329          einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
06330                os->name, pl->name);
06331     }
06332 }
06333 
06334 /* Record a list of sections which may not be cross referenced.  */
06335 
06336 void
06337 lang_add_nocrossref (lang_nocrossref_type *l)
06338 {
06339   struct lang_nocrossrefs *n;
06340 
06341   n = xmalloc (sizeof *n);
06342   n->next = nocrossref_list;
06343   n->list = l;
06344   nocrossref_list = n;
06345 
06346   /* Set notice_all so that we get informed about all symbols.  */
06347   link_info.notice_all = TRUE;
06348 }
06349 
06350 /* Overlay handling.  We handle overlays with some static variables.  */
06351 
06352 /* The overlay virtual address.  */
06353 static etree_type *overlay_vma;
06354 /* And subsection alignment.  */
06355 static etree_type *overlay_subalign;
06356 
06357 /* An expression for the maximum section size seen so far.  */
06358 static etree_type *overlay_max;
06359 
06360 /* A list of all the sections in this overlay.  */
06361 
06362 struct overlay_list {
06363   struct overlay_list *next;
06364   lang_output_section_statement_type *os;
06365 };
06366 
06367 static struct overlay_list *overlay_list;
06368 
06369 /* Start handling an overlay.  */
06370 
06371 void
06372 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
06373 {
06374   /* The grammar should prevent nested overlays from occurring.  */
06375   ASSERT (overlay_vma == NULL
06376          && overlay_subalign == NULL
06377          && overlay_max == NULL);
06378 
06379   overlay_vma = vma_expr;
06380   overlay_subalign = subalign;
06381 }
06382 
06383 /* Start a section in an overlay.  We handle this by calling
06384    lang_enter_output_section_statement with the correct VMA.
06385    lang_leave_overlay sets up the LMA and memory regions.  */
06386 
06387 void
06388 lang_enter_overlay_section (const char *name)
06389 {
06390   struct overlay_list *n;
06391   etree_type *size;
06392 
06393   lang_enter_output_section_statement (name, overlay_vma, normal_section,
06394                                    0, overlay_subalign, 0, 0);
06395 
06396   /* If this is the first section, then base the VMA of future
06397      sections on this one.  This will work correctly even if `.' is
06398      used in the addresses.  */
06399   if (overlay_list == NULL)
06400     overlay_vma = exp_nameop (ADDR, name);
06401 
06402   /* Remember the section.  */
06403   n = xmalloc (sizeof *n);
06404   n->os = current_section;
06405   n->next = overlay_list;
06406   overlay_list = n;
06407 
06408   size = exp_nameop (SIZEOF, name);
06409 
06410   /* Arrange to work out the maximum section end address.  */
06411   if (overlay_max == NULL)
06412     overlay_max = size;
06413   else
06414     overlay_max = exp_binop (MAX_K, overlay_max, size);
06415 }
06416 
06417 /* Finish a section in an overlay.  There isn't any special to do
06418    here.  */
06419 
06420 void
06421 lang_leave_overlay_section (fill_type *fill,
06422                          lang_output_section_phdr_list *phdrs)
06423 {
06424   const char *name;
06425   char *clean, *s2;
06426   const char *s1;
06427   char *buf;
06428 
06429   name = current_section->name;
06430 
06431   /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
06432      region and that no load-time region has been specified.  It doesn't
06433      really matter what we say here, since lang_leave_overlay will
06434      override it.  */
06435   lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
06436 
06437   /* Define the magic symbols.  */
06438 
06439   clean = xmalloc (strlen (name) + 1);
06440   s2 = clean;
06441   for (s1 = name; *s1 != '\0'; s1++)
06442     if (ISALNUM (*s1) || *s1 == '_')
06443       *s2++ = *s1;
06444   *s2 = '\0';
06445 
06446   buf = xmalloc (strlen (clean) + sizeof "__load_start_");
06447   sprintf (buf, "__load_start_%s", clean);
06448   lang_add_assignment (exp_provide (buf,
06449                                 exp_nameop (LOADADDR, name),
06450                                 FALSE));
06451 
06452   buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
06453   sprintf (buf, "__load_stop_%s", clean);
06454   lang_add_assignment (exp_provide (buf,
06455                                 exp_binop ('+',
06456                                           exp_nameop (LOADADDR, name),
06457                                           exp_nameop (SIZEOF, name)),
06458                                 FALSE));
06459 
06460   free (clean);
06461 }
06462 
06463 /* Finish an overlay.  If there are any overlay wide settings, this
06464    looks through all the sections in the overlay and sets them.  */
06465 
06466 void
06467 lang_leave_overlay (etree_type *lma_expr,
06468                   int nocrossrefs,
06469                   fill_type *fill,
06470                   const char *memspec,
06471                   lang_output_section_phdr_list *phdrs,
06472                   const char *lma_memspec)
06473 {
06474   lang_memory_region_type *region;
06475   lang_memory_region_type *lma_region;
06476   struct overlay_list *l;
06477   lang_nocrossref_type *nocrossref;
06478 
06479   lang_get_regions (&region, &lma_region,
06480                   memspec, lma_memspec,
06481                   lma_expr != NULL, FALSE);
06482 
06483   nocrossref = NULL;
06484 
06485   /* After setting the size of the last section, set '.' to end of the
06486      overlay region.  */
06487   if (overlay_list != NULL)
06488     overlay_list->os->update_dot_tree
06489       = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
06490 
06491   l = overlay_list;
06492   while (l != NULL)
06493     {
06494       struct overlay_list *next;
06495 
06496       if (fill != NULL && l->os->fill == NULL)
06497        l->os->fill = fill;
06498 
06499       l->os->region = region;
06500       l->os->lma_region = lma_region;
06501 
06502       /* The first section has the load address specified in the
06503         OVERLAY statement.  The rest are worked out from that.
06504         The base address is not needed (and should be null) if
06505         an LMA region was specified.  */
06506       if (l->next == 0)
06507        l->os->load_base = lma_expr;
06508       if (phdrs != NULL && l->os->phdrs == NULL)
06509        l->os->phdrs = phdrs;
06510 
06511       if (nocrossrefs)
06512        {
06513          lang_nocrossref_type *nc;
06514 
06515          nc = xmalloc (sizeof *nc);
06516          nc->name = l->os->name;
06517          nc->next = nocrossref;
06518          nocrossref = nc;
06519        }
06520 
06521       next = l->next;
06522       free (l);
06523       l = next;
06524     }
06525 
06526   if (nocrossref != NULL)
06527     lang_add_nocrossref (nocrossref);
06528 
06529   overlay_vma = NULL;
06530   overlay_list = NULL;
06531   overlay_max = NULL;
06532 }
06533 
06534 /* Version handling.  This is only useful for ELF.  */
06535 
06536 /* This global variable holds the version tree that we build.  */
06537 
06538 struct bfd_elf_version_tree *lang_elf_version_info;
06539 
06540 /* If PREV is NULL, return first version pattern matching particular symbol.
06541    If PREV is non-NULL, return first version pattern matching particular
06542    symbol after PREV (previously returned by lang_vers_match).  */
06543 
06544 static struct bfd_elf_version_expr *
06545 lang_vers_match (struct bfd_elf_version_expr_head *head,
06546                struct bfd_elf_version_expr *prev,
06547                const char *sym)
06548 {
06549   const char *cxx_sym = sym;
06550   const char *java_sym = sym;
06551   struct bfd_elf_version_expr *expr = NULL;
06552 
06553   if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
06554     {
06555       cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
06556       if (!cxx_sym)
06557        cxx_sym = sym;
06558     }
06559   if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
06560     {
06561       java_sym = cplus_demangle (sym, DMGL_JAVA);
06562       if (!java_sym)
06563        java_sym = sym;
06564     }
06565 
06566   if (head->htab && (prev == NULL || prev->symbol))
06567     {
06568       struct bfd_elf_version_expr e;
06569 
06570       switch (prev ? prev->mask : 0)
06571        {
06572          case 0:
06573            if (head->mask & BFD_ELF_VERSION_C_TYPE)
06574              {
06575               e.symbol = sym;
06576               expr = htab_find (head->htab, &e);
06577               while (expr && strcmp (expr->symbol, sym) == 0)
06578                 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
06579                   goto out_ret;
06580                 else
06581                   expr = expr->next;
06582              }
06583            /* Fallthrough */
06584          case BFD_ELF_VERSION_C_TYPE:
06585            if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
06586              {
06587               e.symbol = cxx_sym;
06588               expr = htab_find (head->htab, &e);
06589               while (expr && strcmp (expr->symbol, cxx_sym) == 0)
06590                 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
06591                   goto out_ret;
06592                 else
06593                   expr = expr->next;
06594              }
06595            /* Fallthrough */
06596          case BFD_ELF_VERSION_CXX_TYPE:
06597            if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
06598              {
06599               e.symbol = java_sym;
06600               expr = htab_find (head->htab, &e);
06601               while (expr && strcmp (expr->symbol, java_sym) == 0)
06602                 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
06603                   goto out_ret;
06604                 else
06605                   expr = expr->next;
06606              }
06607            /* Fallthrough */
06608          default:
06609            break;
06610        }
06611     }
06612 
06613   /* Finally, try the wildcards.  */
06614   if (prev == NULL || prev->symbol)
06615     expr = head->remaining;
06616   else
06617     expr = prev->next;
06618   for (; expr; expr = expr->next)
06619     {
06620       const char *s;
06621 
06622       if (!expr->pattern)
06623        continue;
06624 
06625       if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
06626        break;
06627 
06628       if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
06629        s = java_sym;
06630       else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
06631        s = cxx_sym;
06632       else
06633        s = sym;
06634       if (fnmatch (expr->pattern, s, 0) == 0)
06635        break;
06636     }
06637 
06638 out_ret:
06639   if (cxx_sym != sym)
06640     free ((char *) cxx_sym);
06641   if (java_sym != sym)
06642     free ((char *) java_sym);
06643   return expr;
06644 }
06645 
06646 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
06647    return a string pointing to the symbol name.  */
06648 
06649 static const char *
06650 realsymbol (const char *pattern)
06651 {
06652   const char *p;
06653   bfd_boolean changed = FALSE, backslash = FALSE;
06654   char *s, *symbol = xmalloc (strlen (pattern) + 1);
06655 
06656   for (p = pattern, s = symbol; *p != '\0'; ++p)
06657     {
06658       /* It is a glob pattern only if there is no preceding
06659         backslash.  */
06660       if (! backslash && (*p == '?' || *p == '*' || *p == '['))
06661        {
06662          free (symbol);
06663          return NULL;
06664        }
06665 
06666       if (backslash)
06667        {
06668          /* Remove the preceding backslash.  */
06669          *(s - 1) = *p;
06670          changed = TRUE;
06671        }
06672       else
06673        *s++ = *p;
06674 
06675       backslash = *p == '\\';
06676     }
06677 
06678   if (changed)
06679     {
06680       *s = '\0';
06681       return symbol;
06682     }
06683   else
06684     {
06685       free (symbol);
06686       return pattern;
06687     }
06688 }
06689 
06690 /* This is called for each variable name or match expression.  NEW is
06691    the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
06692    pattern to be matched against symbol names.  */
06693 
06694 struct bfd_elf_version_expr *
06695 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
06696                      const char *new,
06697                      const char *lang,
06698                      bfd_boolean literal_p)
06699 {
06700   struct bfd_elf_version_expr *ret;
06701 
06702   ret = xmalloc (sizeof *ret);
06703   ret->next = orig;
06704   ret->pattern = literal_p ? NULL : new;
06705   ret->symver = 0;
06706   ret->script = 0;
06707   ret->symbol = literal_p ? new : realsymbol (new);
06708 
06709   if (lang == NULL || strcasecmp (lang, "C") == 0)
06710     ret->mask = BFD_ELF_VERSION_C_TYPE;
06711   else if (strcasecmp (lang, "C++") == 0)
06712     ret->mask = BFD_ELF_VERSION_CXX_TYPE;
06713   else if (strcasecmp (lang, "Java") == 0)
06714     ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
06715   else
06716     {
06717       einfo (_("%X%P: unknown language `%s' in version information\n"),
06718             lang);
06719       ret->mask = BFD_ELF_VERSION_C_TYPE;
06720     }
06721 
06722   return ldemul_new_vers_pattern (ret);
06723 }
06724 
06725 /* This is called for each set of variable names and match
06726    expressions.  */
06727 
06728 struct bfd_elf_version_tree *
06729 lang_new_vers_node (struct bfd_elf_version_expr *globals,
06730                   struct bfd_elf_version_expr *locals)
06731 {
06732   struct bfd_elf_version_tree *ret;
06733 
06734   ret = xcalloc (1, sizeof *ret);
06735   ret->globals.list = globals;
06736   ret->locals.list = locals;
06737   ret->match = lang_vers_match;
06738   ret->name_indx = (unsigned int) -1;
06739   return ret;
06740 }
06741 
06742 /* This static variable keeps track of version indices.  */
06743 
06744 static int version_index;
06745 
06746 static hashval_t
06747 version_expr_head_hash (const void *p)
06748 {
06749   const struct bfd_elf_version_expr *e = p;
06750 
06751   return htab_hash_string (e->symbol);
06752 }
06753 
06754 static int
06755 version_expr_head_eq (const void *p1, const void *p2)
06756 {
06757   const struct bfd_elf_version_expr *e1 = p1;
06758   const struct bfd_elf_version_expr *e2 = p2;
06759 
06760   return strcmp (e1->symbol, e2->symbol) == 0;
06761 }
06762 
06763 static void
06764 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
06765 {
06766   size_t count = 0;
06767   struct bfd_elf_version_expr *e, *next;
06768   struct bfd_elf_version_expr **list_loc, **remaining_loc;
06769 
06770   for (e = head->list; e; e = e->next)
06771     {
06772       if (e->symbol)
06773        count++;
06774       head->mask |= e->mask;
06775     }
06776 
06777   if (count)
06778     {
06779       head->htab = htab_create (count * 2, version_expr_head_hash,
06780                             version_expr_head_eq, NULL);
06781       list_loc = &head->list;
06782       remaining_loc = &head->remaining;
06783       for (e = head->list; e; e = next)
06784        {
06785          next = e->next;
06786          if (!e->symbol)
06787            {
06788              *remaining_loc = e;
06789              remaining_loc = &e->next;
06790            }
06791          else
06792            {
06793              void **loc = htab_find_slot (head->htab, e, INSERT);
06794 
06795              if (*loc)
06796               {
06797                 struct bfd_elf_version_expr *e1, *last;
06798 
06799                 e1 = *loc;
06800                 last = NULL;
06801                 do
06802                   {
06803                     if (e1->mask == e->mask)
06804                      {
06805                        last = NULL;
06806                        break;
06807                      }
06808                     last = e1;
06809                     e1 = e1->next;
06810                   }
06811                 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
06812 
06813                 if (last == NULL)
06814                   {
06815                     /* This is a duplicate.  */
06816                     /* FIXME: Memory leak.  Sometimes pattern is not
06817                       xmalloced alone, but in larger chunk of memory.  */
06818                     /* free (e->symbol); */
06819                     free (e);
06820                   }
06821                 else
06822                   {
06823                     e->next = last->next;
06824                     last->next = e;
06825                   }
06826               }
06827              else
06828               {
06829                 *loc = e;
06830                 *list_loc = e;
06831                 list_loc = &e->next;
06832               }
06833            }
06834        }
06835       *remaining_loc = NULL;
06836       *list_loc = head->remaining;
06837     }
06838   else
06839     head->remaining = head->list;
06840 }
06841 
06842 /* This is called when we know the name and dependencies of the
06843    version.  */
06844 
06845 void
06846 lang_register_vers_node (const char *name,
06847                       struct bfd_elf_version_tree *version,
06848                       struct bfd_elf_version_deps *deps)
06849 {
06850   struct bfd_elf_version_tree *t, **pp;
06851   struct bfd_elf_version_expr *e1;
06852 
06853   if (name == NULL)
06854     name = "";
06855 
06856   if ((name[0] == '\0' && lang_elf_version_info != NULL)
06857       || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
06858     {
06859       einfo (_("%X%P: anonymous version tag cannot be combined"
06860               " with other version tags\n"));
06861       free (version);
06862       return;
06863     }
06864 
06865   /* Make sure this node has a unique name.  */
06866   for (t = lang_elf_version_info; t != NULL; t = t->next)
06867     if (strcmp (t->name, name) == 0)
06868       einfo (_("%X%P: duplicate version tag `%s'\n"), name);
06869 
06870   lang_finalize_version_expr_head (&version->globals);
06871   lang_finalize_version_expr_head (&version->locals);
06872 
06873   /* Check the global and local match names, and make sure there
06874      aren't any duplicates.  */
06875 
06876   for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
06877     {
06878       for (t = lang_elf_version_info; t != NULL; t = t->next)
06879        {
06880          struct bfd_elf_version_expr *e2;
06881 
06882          if (t->locals.htab && e1->symbol)
06883            {
06884              e2 = htab_find (t->locals.htab, e1);
06885              while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
06886               {
06887                 if (e1->mask == e2->mask)
06888                   einfo (_("%X%P: duplicate expression `%s'"
06889                           " in version information\n"), e1->symbol);
06890                 e2 = e2->next;
06891               }
06892            }
06893          else if (!e1->symbol)
06894            for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
06895              if (strcmp (e1->pattern, e2->pattern) == 0
06896                 && e1->mask == e2->mask)
06897               einfo (_("%X%P: duplicate expression `%s'"
06898                       " in version information\n"), e1->pattern);
06899        }
06900     }
06901 
06902   for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
06903     {
06904       for (t = lang_elf_version_info; t != NULL; t = t->next)
06905        {
06906          struct bfd_elf_version_expr *e2;
06907 
06908          if (t->globals.htab && e1->symbol)
06909            {
06910              e2 = htab_find (t->globals.htab, e1);
06911              while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
06912               {
06913                 if (e1->mask == e2->mask)
06914                   einfo (_("%X%P: duplicate expression `%s'"
06915                           " in version information\n"),
06916                         e1->symbol);
06917                 e2 = e2->next;
06918               }
06919            }
06920          else if (!e1->symbol)
06921            for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
06922              if (strcmp (e1->pattern, e2->pattern) == 0
06923                 && e1->mask == e2->mask)
06924               einfo (_("%X%P: duplicate expression `%s'"
06925                       " in version information\n"), e1->pattern);
06926        }
06927     }
06928 
06929   version->deps = deps;
06930   version->name = name;
06931   if (name[0] != '\0')
06932     {
06933       ++version_index;
06934       version->vernum = version_index;
06935     }
06936   else
06937     version->vernum = 0;
06938 
06939   for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
06940     ;
06941   *pp = version;
06942 }
06943 
06944 /* This is called when we see a version dependency.  */
06945 
06946 struct bfd_elf_version_deps *
06947 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
06948 {
06949   struct bfd_elf_version_deps *ret;
06950   struct bfd_elf_version_tree *t;
06951 
06952   ret = xmalloc (sizeof *ret);
06953   ret->next = list;
06954 
06955   for (t = lang_elf_version_info; t != NULL; t = t->next)
06956     {
06957       if (strcmp (t->name, name) == 0)
06958        {
06959          ret->version_needed = t;
06960          return ret;
06961        }
06962     }
06963 
06964   einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
06965 
06966   return ret;
06967 }
06968 
06969 static void
06970 lang_do_version_exports_section (void)
06971 {
06972   struct bfd_elf_version_expr *greg = NULL, *lreg;
06973 
06974   LANG_FOR_EACH_INPUT_STATEMENT (is)
06975     {
06976       asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
06977       char *contents, *p;
06978       bfd_size_type len;
06979 
06980       if (sec == NULL)
06981        continue;
06982 
06983       len = sec->size;
06984       contents = xmalloc (len);
06985       if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
06986        einfo (_("%X%P: unable to read .exports section contents\n"), sec);
06987 
06988       p = contents;
06989       while (p < contents + len)
06990        {
06991          greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
06992          p = strchr (p, '\0') + 1;
06993        }
06994 
06995       /* Do not free the contents, as we used them creating the regex.  */
06996 
06997       /* Do not include this section in the link.  */
06998       sec->flags |= SEC_EXCLUDE | SEC_KEEP;
06999     }
07000 
07001   lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
07002   lang_register_vers_node (command_line.version_exports_section,
07003                         lang_new_vers_node (greg, lreg), NULL);
07004 }
07005 
07006 void
07007 lang_add_unique (const char *name)
07008 {
07009   struct unique_sections *ent;
07010 
07011   for (ent = unique_section_list; ent; ent = ent->next)
07012     if (strcmp (ent->name, name) == 0)
07013       return;
07014 
07015   ent = xmalloc (sizeof *ent);
07016   ent->name = xstrdup (name);
07017   ent->next = unique_section_list;
07018   unique_section_list = ent;
07019 }
07020 
07021 /* Append the list of dynamic symbols to the existing one.  */
07022 
07023 void
07024 lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic)
07025 {
07026   if (link_info.dynamic_list)
07027     {
07028       struct bfd_elf_version_expr *tail;
07029       for (tail = dynamic; tail->next != NULL; tail = tail->next)
07030        ;
07031       tail->next = link_info.dynamic_list->head.list;
07032       link_info.dynamic_list->head.list = dynamic;
07033     }
07034   else
07035     {
07036       struct bfd_elf_dynamic_list *d;
07037 
07038       d = xcalloc (1, sizeof *d);
07039       d->head.list = dynamic;
07040       d->match = lang_vers_match;
07041       link_info.dynamic_list = d;
07042     }
07043 }
07044 
07045 /* Append the list of C++ typeinfo dynamic symbols to the existing
07046    one.  */
07047 
07048 void
07049 lang_append_dynamic_list_cpp_typeinfo (void)
07050 {
07051   const char * symbols [] =
07052     {
07053       "typeinfo name for*",
07054       "typeinfo for*"
07055     };
07056   struct bfd_elf_version_expr *dynamic = NULL;
07057   unsigned int i;
07058 
07059   for (i = 0; i < ARRAY_SIZE (symbols); i++)
07060     dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
07061                                  FALSE);
07062 
07063   lang_append_dynamic_list (dynamic);
07064 }
07065 
07066 /* Append the list of C++ operator new and delete dynamic symbols to the
07067    existing one.  */
07068 
07069 void
07070 lang_append_dynamic_list_cpp_new (void)
07071 {
07072   const char * symbols [] =
07073     {
07074       "operator new*",
07075       "operator delete*"
07076     };
07077   struct bfd_elf_version_expr *dynamic = NULL;
07078   unsigned int i;
07079 
07080   for (i = 0; i < ARRAY_SIZE (symbols); i++)
07081     dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
07082                                  FALSE);
07083 
07084   lang_append_dynamic_list (dynamic);
07085 }