Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* Linker command language support.
  2.    Copyright 1991-2013 Free Software Foundation, Inc.
  3.  
  4.    This file is part of the GNU Binutils.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  19.    MA 02110-1301, USA.  */
  20.  
  21. #include "sysdep.h"
  22. #include "bfd.h"
  23. #include "libiberty.h"
  24. #include "filenames.h"
  25. #include "safe-ctype.h"
  26. #include "obstack.h"
  27. #include "bfdlink.h"
  28.  
  29. #include "ld.h"
  30. #include "ldmain.h"
  31. #include "ldexp.h"
  32. #include "ldlang.h"
  33. #include <ldgram.h>
  34. #include "ldlex.h"
  35. #include "ldmisc.h"
  36. #include "ldctor.h"
  37. #include "ldfile.h"
  38. #include "ldemul.h"
  39. #include "fnmatch.h"
  40. #include "demangle.h"
  41. #include "hashtab.h"
  42. #include "libbfd.h"
  43. #ifdef ENABLE_PLUGINS
  44. #include "plugin.h"
  45. #endif /* ENABLE_PLUGINS */
  46.  
  47. #include <limits.h>
  48.  
  49.  
  50. #ifndef offsetof
  51. #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
  52. #endif
  53.  
  54. /* Locals variables.  */
  55. static struct obstack stat_obstack;
  56. static struct obstack map_obstack;
  57.  
  58. #define obstack_chunk_alloc xmalloc
  59. #define obstack_chunk_free free
  60. static const char *entry_symbol_default = "start";
  61. static bfd_boolean placed_commons = FALSE;
  62. static bfd_boolean stripped_excluded_sections = FALSE;
  63. static lang_output_section_statement_type *default_common_section;
  64. static bfd_boolean map_option_f;
  65. static bfd_vma print_dot;
  66. static lang_input_statement_type *first_file;
  67. static const char *current_target;
  68. static lang_statement_list_type statement_list;
  69. static struct bfd_hash_table lang_definedness_table;
  70. static lang_statement_list_type *stat_save[10];
  71. static lang_statement_list_type **stat_save_ptr = &stat_save[0];
  72. static struct unique_sections *unique_section_list;
  73.  
  74. /* Forward declarations.  */
  75. static void exp_init_os (etree_type *);
  76. static void init_map_userdata (bfd *, asection *, void *);
  77. static lang_input_statement_type *lookup_name (const char *);
  78. static struct bfd_hash_entry *lang_definedness_newfunc
  79.  (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
  80. static void insert_undefined (const char *);
  81. static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
  82. static void print_statement (lang_statement_union_type *,
  83.                              lang_output_section_statement_type *);
  84. static void print_statement_list (lang_statement_union_type *,
  85.                                   lang_output_section_statement_type *);
  86. static void print_statements (void);
  87. static void print_input_section (asection *, bfd_boolean);
  88. static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
  89. static void lang_record_phdrs (void);
  90. static void lang_do_version_exports_section (void);
  91. static void lang_finalize_version_expr_head
  92.   (struct bfd_elf_version_expr_head *);
  93.  
  94. /* Exported variables.  */
  95. const char *output_target;
  96. lang_output_section_statement_type *abs_output_section;
  97. lang_statement_list_type lang_output_section_statement;
  98. lang_statement_list_type *stat_ptr = &statement_list;
  99. lang_statement_list_type file_chain = { NULL, NULL };
  100. lang_statement_list_type input_file_chain;
  101. struct bfd_sym_chain entry_symbol = { NULL, NULL };
  102. const char *entry_section = ".text";
  103. struct lang_input_statement_flags input_flags;
  104. bfd_boolean entry_from_cmdline;
  105. bfd_boolean undef_from_cmdline;
  106. bfd_boolean lang_has_input_file = FALSE;
  107. bfd_boolean had_output_filename = FALSE;
  108. bfd_boolean lang_float_flag = FALSE;
  109. bfd_boolean delete_output_file_on_failure = FALSE;
  110. struct lang_phdr *lang_phdr_list;
  111. struct lang_nocrossrefs *nocrossref_list;
  112.  
  113.  /* Functions that traverse the linker script and might evaluate
  114.     DEFINED() need to increment this.  */
  115. int lang_statement_iteration = 0;
  116.  
  117. etree_type *base; /* Relocation base - or null */
  118.  
  119. /* Return TRUE if the PATTERN argument is a wildcard pattern.
  120.    Although backslashes are treated specially if a pattern contains
  121.    wildcards, we do not consider the mere presence of a backslash to
  122.    be enough to cause the pattern to be treated as a wildcard.
  123.    That lets us handle DOS filenames more naturally.  */
  124. #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
  125.  
  126. #define new_stat(x, y) \
  127.   (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
  128.  
  129. #define outside_section_address(q) \
  130.   ((q)->output_offset + (q)->output_section->vma)
  131.  
  132. #define outside_symbol_address(q) \
  133.   ((q)->value + outside_section_address (q->section))
  134.  
  135. #define SECTION_NAME_MAP_LENGTH (16)
  136.  
  137. void *
  138. stat_alloc (size_t size)
  139. {
  140.   return obstack_alloc (&stat_obstack, size);
  141. }
  142.  
  143. static int
  144. name_match (const char *pattern, const char *name)
  145. {
  146.   if (wildcardp (pattern))
  147.     return fnmatch (pattern, name, 0);
  148.   return strcmp (pattern, name);
  149. }
  150.  
  151. /* If PATTERN is of the form archive:file, return a pointer to the
  152.    separator.  If not, return NULL.  */
  153.  
  154. static char *
  155. archive_path (const char *pattern)
  156. {
  157.   char *p = NULL;
  158.  
  159.   if (link_info.path_separator == 0)
  160.     return p;
  161.  
  162.   p = strchr (pattern, link_info.path_separator);
  163. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  164.   if (p == NULL || link_info.path_separator != ':')
  165.     return p;
  166.  
  167.   /* Assume a match on the second char is part of drive specifier,
  168.      as in "c:\silly.dos".  */
  169.   if (p == pattern + 1 && ISALPHA (*pattern))
  170.     p = strchr (p + 1, link_info.path_separator);
  171. #endif
  172.   return p;
  173. }
  174.  
  175. /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
  176.    return whether F matches FILE_SPEC.  */
  177.  
  178. static bfd_boolean
  179. input_statement_is_archive_path (const char *file_spec, char *sep,
  180.                                  lang_input_statement_type *f)
  181. {
  182.   bfd_boolean match = FALSE;
  183.  
  184.   if ((*(sep + 1) == 0
  185.        || name_match (sep + 1, f->filename) == 0)
  186.       && ((sep != file_spec)
  187.           == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL)))
  188.     {
  189.       match = TRUE;
  190.  
  191.       if (sep != file_spec)
  192.         {
  193.           const char *aname = f->the_bfd->my_archive->filename;
  194.           *sep = 0;
  195.           match = name_match (file_spec, aname) == 0;
  196.           *sep = link_info.path_separator;
  197.         }
  198.     }
  199.   return match;
  200. }
  201.  
  202. static bfd_boolean
  203. unique_section_p (const asection *sec,
  204.                   const lang_output_section_statement_type *os)
  205. {
  206.   struct unique_sections *unam;
  207.   const char *secnam;
  208.  
  209.   if (link_info.relocatable
  210.       && sec->owner != NULL
  211.       && bfd_is_group_section (sec->owner, sec))
  212.     return !(os != NULL
  213.              && strcmp (os->name, DISCARD_SECTION_NAME) == 0);
  214.  
  215.   secnam = sec->name;
  216.   for (unam = unique_section_list; unam; unam = unam->next)
  217.     if (name_match (unam->name, secnam) == 0)
  218.       return TRUE;
  219.  
  220.   return FALSE;
  221. }
  222.  
  223. /* Generic traversal routines for finding matching sections.  */
  224.  
  225. /* Try processing a section against a wildcard.  This just calls
  226.    the callback unless the filename exclusion list is present
  227.    and excludes the file.  It's hardly ever present so this
  228.    function is very fast.  */
  229.  
  230. static void
  231. walk_wild_consider_section (lang_wild_statement_type *ptr,
  232.                             lang_input_statement_type *file,
  233.                             asection *s,
  234.                             struct wildcard_list *sec,
  235.                             callback_t callback,
  236.                             void *data)
  237. {
  238.   struct name_list *list_tmp;
  239.  
  240.   /* Don't process sections from files which were excluded.  */
  241.   for (list_tmp = sec->spec.exclude_name_list;
  242.        list_tmp;
  243.        list_tmp = list_tmp->next)
  244.     {
  245.       char *p = archive_path (list_tmp->name);
  246.  
  247.       if (p != NULL)
  248.         {
  249.           if (input_statement_is_archive_path (list_tmp->name, p, file))
  250.             return;
  251.         }
  252.  
  253.       else if (name_match (list_tmp->name, file->filename) == 0)
  254.         return;
  255.  
  256.       /* FIXME: Perhaps remove the following at some stage?  Matching
  257.          unadorned archives like this was never documented and has
  258.          been superceded by the archive:path syntax.  */
  259.       else if (file->the_bfd != NULL
  260.                && file->the_bfd->my_archive != NULL
  261.                && name_match (list_tmp->name,
  262.                               file->the_bfd->my_archive->filename) == 0)
  263.         return;
  264.     }
  265.  
  266.   (*callback) (ptr, sec, s, ptr->section_flag_list, file, data);
  267. }
  268.  
  269. /* Lowest common denominator routine that can handle everything correctly,
  270.    but slowly.  */
  271.  
  272. static void
  273. walk_wild_section_general (lang_wild_statement_type *ptr,
  274.                            lang_input_statement_type *file,
  275.                            callback_t callback,
  276.                            void *data)
  277. {
  278.   asection *s;
  279.   struct wildcard_list *sec;
  280.  
  281.   for (s = file->the_bfd->sections; s != NULL; s = s->next)
  282.     {
  283.       sec = ptr->section_list;
  284.       if (sec == NULL)
  285.         (*callback) (ptr, sec, s, ptr->section_flag_list, file, data);
  286.  
  287.       while (sec != NULL)
  288.         {
  289.           bfd_boolean skip = FALSE;
  290.  
  291.           if (sec->spec.name != NULL)
  292.             {
  293.               const char *sname = bfd_get_section_name (file->the_bfd, s);
  294.  
  295.               skip = name_match (sec->spec.name, sname) != 0;
  296.             }
  297.  
  298.           if (!skip)
  299.             walk_wild_consider_section (ptr, file, s, sec, callback, data);
  300.  
  301.           sec = sec->next;
  302.         }
  303.     }
  304. }
  305.  
  306. /* Routines to find a single section given its name.  If there's more
  307.    than one section with that name, we report that.  */
  308.  
  309. typedef struct
  310. {
  311.   asection *found_section;
  312.   bfd_boolean multiple_sections_found;
  313. } section_iterator_callback_data;
  314.  
  315. static bfd_boolean
  316. section_iterator_callback (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *data)
  317. {
  318.   section_iterator_callback_data *d = (section_iterator_callback_data *) data;
  319.  
  320.   if (d->found_section != NULL)
  321.     {
  322.       d->multiple_sections_found = TRUE;
  323.       return TRUE;
  324.     }
  325.  
  326.   d->found_section = s;
  327.   return FALSE;
  328. }
  329.  
  330. static asection *
  331. find_section (lang_input_statement_type *file,
  332.               struct wildcard_list *sec,
  333.               bfd_boolean *multiple_sections_found)
  334. {
  335.   section_iterator_callback_data cb_data = { NULL, FALSE };
  336.  
  337.   bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
  338.                               section_iterator_callback, &cb_data);
  339.   *multiple_sections_found = cb_data.multiple_sections_found;
  340.   return cb_data.found_section;
  341. }
  342.  
  343. /* Code for handling simple wildcards without going through fnmatch,
  344.    which can be expensive because of charset translations etc.  */
  345.  
  346. /* A simple wild is a literal string followed by a single '*',
  347.    where the literal part is at least 4 characters long.  */
  348.  
  349. static bfd_boolean
  350. is_simple_wild (const char *name)
  351. {
  352.   size_t len = strcspn (name, "*?[");
  353.   return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
  354. }
  355.  
  356. static bfd_boolean
  357. match_simple_wild (const char *pattern, const char *name)
  358. {
  359.   /* The first four characters of the pattern are guaranteed valid
  360.      non-wildcard characters.  So we can go faster.  */
  361.   if (pattern[0] != name[0] || pattern[1] != name[1]
  362.       || pattern[2] != name[2] || pattern[3] != name[3])
  363.     return FALSE;
  364.  
  365.   pattern += 4;
  366.   name += 4;
  367.   while (*pattern != '*')
  368.     if (*name++ != *pattern++)
  369.       return FALSE;
  370.  
  371.   return TRUE;
  372. }
  373.  
  374. /* Return the numerical value of the init_priority attribute from
  375.    section name NAME.  */
  376.  
  377. static unsigned long
  378. get_init_priority (const char *name)
  379. {
  380.   char *end;
  381.   unsigned long init_priority;
  382.  
  383.   /* GCC uses the following section names for the init_priority
  384.      attribute with numerical values 101 and 65535 inclusive. A
  385.      lower value means a higher priority.
  386.  
  387.      1: .init_array.NNNN/.fini_array.NNNN: Where NNNN is the
  388.         decimal numerical value of the init_priority attribute.
  389.         The order of execution in .init_array is forward and
  390.         .fini_array is backward.
  391.      2: .ctors.NNNN/.dtors.NNNN: Where NNNN is 65535 minus the
  392.         decimal numerical value of the init_priority attribute.
  393.         The order of execution in .ctors is backward and .dtors
  394.         is forward.
  395.    */
  396.   if (strncmp (name, ".init_array.", 12) == 0
  397.       || strncmp (name, ".fini_array.", 12) == 0)
  398.     {
  399.       init_priority = strtoul (name + 12, &end, 10);
  400.       return *end ? 0 : init_priority;
  401.     }
  402.   else if (strncmp (name, ".ctors.", 7) == 0
  403.            || strncmp (name, ".dtors.", 7) == 0)
  404.     {
  405.       init_priority = strtoul (name + 7, &end, 10);
  406.       return *end ? 0 : 65535 - init_priority;
  407.     }
  408.  
  409.   return 0;
  410. }
  411.  
  412. /* Compare sections ASEC and BSEC according to SORT.  */
  413.  
  414. static int
  415. compare_section (sort_type sort, asection *asec, asection *bsec)
  416. {
  417.   int ret;
  418.   unsigned long ainit_priority, binit_priority;
  419.  
  420.   switch (sort)
  421.     {
  422.     default:
  423.       abort ();
  424.  
  425.     case by_init_priority:
  426.       ainit_priority
  427.         = get_init_priority (bfd_get_section_name (asec->owner, asec));
  428.       binit_priority
  429.         = get_init_priority (bfd_get_section_name (bsec->owner, bsec));
  430.       if (ainit_priority == 0 || binit_priority == 0)
  431.         goto sort_by_name;
  432.       ret = ainit_priority - binit_priority;
  433.       if (ret)
  434.         break;
  435.       else
  436.         goto sort_by_name;
  437.  
  438.     case by_alignment_name:
  439.       ret = (bfd_section_alignment (bsec->owner, bsec)
  440.              - bfd_section_alignment (asec->owner, asec));
  441.       if (ret)
  442.         break;
  443.       /* Fall through.  */
  444.  
  445.     case by_name:
  446. sort_by_name:
  447.       ret = strcmp (bfd_get_section_name (asec->owner, asec),
  448.                     bfd_get_section_name (bsec->owner, bsec));
  449.       break;
  450.  
  451.     case by_name_alignment:
  452.       ret = strcmp (bfd_get_section_name (asec->owner, asec),
  453.                     bfd_get_section_name (bsec->owner, bsec));
  454.       if (ret)
  455.         break;
  456.       /* Fall through.  */
  457.  
  458.     case by_alignment:
  459.       ret = (bfd_section_alignment (bsec->owner, bsec)
  460.              - bfd_section_alignment (asec->owner, asec));
  461.       break;
  462.     }
  463.  
  464.   return ret;
  465. }
  466.  
  467. /* Build a Binary Search Tree to sort sections, unlike insertion sort
  468.    used in wild_sort(). BST is considerably faster if the number of
  469.    of sections are large.  */
  470.  
  471. static lang_section_bst_type **
  472. wild_sort_fast (lang_wild_statement_type *wild,
  473.                 struct wildcard_list *sec,
  474.                 lang_input_statement_type *file ATTRIBUTE_UNUSED,
  475.                 asection *section)
  476. {
  477.   lang_section_bst_type **tree;
  478.  
  479.   tree = &wild->tree;
  480.   if (!wild->filenames_sorted
  481.       && (sec == NULL || sec->spec.sorted == none))
  482.     {
  483.       /* Append at the right end of tree.  */
  484.       while (*tree)
  485.         tree = &((*tree)->right);
  486.       return tree;
  487.     }
  488.  
  489.   while (*tree)
  490.     {
  491.       /* Find the correct node to append this section.  */
  492.       if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
  493.         tree = &((*tree)->left);
  494.       else
  495.         tree = &((*tree)->right);
  496.     }
  497.  
  498.   return tree;
  499. }
  500.  
  501. /* Use wild_sort_fast to build a BST to sort sections.  */
  502.  
  503. static void
  504. output_section_callback_fast (lang_wild_statement_type *ptr,
  505.                               struct wildcard_list *sec,
  506.                               asection *section,
  507.                               struct flag_info *sflag_list ATTRIBUTE_UNUSED,
  508.                               lang_input_statement_type *file,
  509.                               void *output)
  510. {
  511.   lang_section_bst_type *node;
  512.   lang_section_bst_type **tree;
  513.   lang_output_section_statement_type *os;
  514.  
  515.   os = (lang_output_section_statement_type *) output;
  516.  
  517.   if (unique_section_p (section, os))
  518.     return;
  519.  
  520.   node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type));
  521.   node->left = 0;
  522.   node->right = 0;
  523.   node->section = section;
  524.  
  525.   tree = wild_sort_fast (ptr, sec, file, section);
  526.   if (tree != NULL)
  527.     *tree = node;
  528. }
  529.  
  530. /* Convert a sorted sections' BST back to list form.  */
  531.  
  532. static void
  533. output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
  534.                                       lang_section_bst_type *tree,
  535.                                       void *output)
  536. {
  537.   if (tree->left)
  538.     output_section_callback_tree_to_list (ptr, tree->left, output);
  539.  
  540.   lang_add_section (&ptr->children, tree->section, NULL,
  541.                     (lang_output_section_statement_type *) output);
  542.  
  543.   if (tree->right)
  544.     output_section_callback_tree_to_list (ptr, tree->right, output);
  545.  
  546.   free (tree);
  547. }
  548.  
  549. /* Specialized, optimized routines for handling different kinds of
  550.    wildcards */
  551.  
  552. static void
  553. walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
  554.                                 lang_input_statement_type *file,
  555.                                 callback_t callback,
  556.                                 void *data)
  557. {
  558.   /* We can just do a hash lookup for the section with the right name.
  559.      But if that lookup discovers more than one section with the name
  560.      (should be rare), we fall back to the general algorithm because
  561.      we would otherwise have to sort the sections to make sure they
  562.      get processed in the bfd's order.  */
  563.   bfd_boolean multiple_sections_found;
  564.   struct wildcard_list *sec0 = ptr->handler_data[0];
  565.   asection *s0 = find_section (file, sec0, &multiple_sections_found);
  566.  
  567.   if (multiple_sections_found)
  568.     walk_wild_section_general (ptr, file, callback, data);
  569.   else if (s0)
  570.     walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
  571. }
  572.  
  573. static void
  574. walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
  575.                                 lang_input_statement_type *file,
  576.                                 callback_t callback,
  577.                                 void *data)
  578. {
  579.   asection *s;
  580.   struct wildcard_list *wildsec0 = ptr->handler_data[0];
  581.  
  582.   for (s = file->the_bfd->sections; s != NULL; s = s->next)
  583.     {
  584.       const char *sname = bfd_get_section_name (file->the_bfd, s);
  585.       bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
  586.  
  587.       if (!skip)
  588.         walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
  589.     }
  590. }
  591.  
  592. static void
  593. walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
  594.                                 lang_input_statement_type *file,
  595.                                 callback_t callback,
  596.                                 void *data)
  597. {
  598.   asection *s;
  599.   struct wildcard_list *sec0 = ptr->handler_data[0];
  600.   struct wildcard_list *wildsec1 = ptr->handler_data[1];
  601.   bfd_boolean multiple_sections_found;
  602.   asection *s0 = find_section (file, sec0, &multiple_sections_found);
  603.  
  604.   if (multiple_sections_found)
  605.     {
  606.       walk_wild_section_general (ptr, file, callback, data);
  607.       return;
  608.     }
  609.  
  610.   /* Note that if the section was not found, s0 is NULL and
  611.      we'll simply never succeed the s == s0 test below.  */
  612.   for (s = file->the_bfd->sections; s != NULL; s = s->next)
  613.     {
  614.       /* Recall that in this code path, a section cannot satisfy more
  615.          than one spec, so if s == s0 then it cannot match
  616.          wildspec1.  */
  617.       if (s == s0)
  618.         walk_wild_consider_section (ptr, file, s, sec0, callback, data);
  619.       else
  620.         {
  621.           const char *sname = bfd_get_section_name (file->the_bfd, s);
  622.           bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
  623.  
  624.           if (!skip)
  625.             walk_wild_consider_section (ptr, file, s, wildsec1, callback,
  626.                                         data);
  627.         }
  628.     }
  629. }
  630.  
  631. static void
  632. walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
  633.                                 lang_input_statement_type *file,
  634.                                 callback_t callback,
  635.                                 void *data)
  636. {
  637.   asection *s;
  638.   struct wildcard_list *sec0 = ptr->handler_data[0];
  639.   struct wildcard_list *wildsec1 = ptr->handler_data[1];
  640.   struct wildcard_list *wildsec2 = ptr->handler_data[2];
  641.   bfd_boolean multiple_sections_found;
  642.   asection *s0 = find_section (file, sec0, &multiple_sections_found);
  643.  
  644.   if (multiple_sections_found)
  645.     {
  646.       walk_wild_section_general (ptr, file, callback, data);
  647.       return;
  648.     }
  649.  
  650.   for (s = file->the_bfd->sections; s != NULL; s = s->next)
  651.     {
  652.       if (s == s0)
  653.         walk_wild_consider_section (ptr, file, s, sec0, callback, data);
  654.       else
  655.         {
  656.           const char *sname = bfd_get_section_name (file->the_bfd, s);
  657.           bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
  658.  
  659.           if (!skip)
  660.             walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
  661.           else
  662.             {
  663.               skip = !match_simple_wild (wildsec2->spec.name, sname);
  664.               if (!skip)
  665.                 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
  666.                                             data);
  667.             }
  668.         }
  669.     }
  670. }
  671.  
  672. static void
  673. walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
  674.                                 lang_input_statement_type *file,
  675.                                 callback_t callback,
  676.                                 void *data)
  677. {
  678.   asection *s;
  679.   struct wildcard_list *sec0 = ptr->handler_data[0];
  680.   struct wildcard_list *sec1 = ptr->handler_data[1];
  681.   struct wildcard_list *wildsec2 = ptr->handler_data[2];
  682.   struct wildcard_list *wildsec3 = ptr->handler_data[3];
  683.   bfd_boolean multiple_sections_found;
  684.   asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
  685.  
  686.   if (multiple_sections_found)
  687.     {
  688.       walk_wild_section_general (ptr, file, callback, data);
  689.       return;
  690.     }
  691.  
  692.   s1 = find_section (file, sec1, &multiple_sections_found);
  693.   if (multiple_sections_found)
  694.     {
  695.       walk_wild_section_general (ptr, file, callback, data);
  696.       return;
  697.     }
  698.  
  699.   for (s = file->the_bfd->sections; s != NULL; s = s->next)
  700.     {
  701.       if (s == s0)
  702.         walk_wild_consider_section (ptr, file, s, sec0, callback, data);
  703.       else
  704.         if (s == s1)
  705.           walk_wild_consider_section (ptr, file, s, sec1, callback, data);
  706.         else
  707.           {
  708.             const char *sname = bfd_get_section_name (file->the_bfd, s);
  709.             bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
  710.                                                    sname);
  711.  
  712.             if (!skip)
  713.               walk_wild_consider_section (ptr, file, s, wildsec2, callback,
  714.                                           data);
  715.             else
  716.               {
  717.                 skip = !match_simple_wild (wildsec3->spec.name, sname);
  718.                 if (!skip)
  719.                   walk_wild_consider_section (ptr, file, s, wildsec3,
  720.                                               callback, data);
  721.               }
  722.           }
  723.     }
  724. }
  725.  
  726. static void
  727. walk_wild_section (lang_wild_statement_type *ptr,
  728.                    lang_input_statement_type *file,
  729.                    callback_t callback,
  730.                    void *data)
  731. {
  732.   if (file->flags.just_syms)
  733.     return;
  734.  
  735.   (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
  736. }
  737.  
  738. /* Returns TRUE when name1 is a wildcard spec that might match
  739.    something name2 can match.  We're conservative: we return FALSE
  740.    only if the prefixes of name1 and name2 are different up to the
  741.    first wildcard character.  */
  742.  
  743. static bfd_boolean
  744. wild_spec_can_overlap (const char *name1, const char *name2)
  745. {
  746.   size_t prefix1_len = strcspn (name1, "?*[");
  747.   size_t prefix2_len = strcspn (name2, "?*[");
  748.   size_t min_prefix_len;
  749.  
  750.   /* Note that if there is no wildcard character, then we treat the
  751.      terminating 0 as part of the prefix.  Thus ".text" won't match
  752.      ".text." or ".text.*", for example.  */
  753.   if (name1[prefix1_len] == '\0')
  754.     prefix1_len++;
  755.   if (name2[prefix2_len] == '\0')
  756.     prefix2_len++;
  757.  
  758.   min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
  759.  
  760.   return memcmp (name1, name2, min_prefix_len) == 0;
  761. }
  762.  
  763. /* Select specialized code to handle various kinds of wildcard
  764.    statements.  */
  765.  
  766. static void
  767. analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
  768. {
  769.   int sec_count = 0;
  770.   int wild_name_count = 0;
  771.   struct wildcard_list *sec;
  772.   int signature;
  773.   int data_counter;
  774.  
  775.   ptr->walk_wild_section_handler = walk_wild_section_general;
  776.   ptr->handler_data[0] = NULL;
  777.   ptr->handler_data[1] = NULL;
  778.   ptr->handler_data[2] = NULL;
  779.   ptr->handler_data[3] = NULL;
  780.   ptr->tree = NULL;
  781.  
  782.   /* Count how many wildcard_specs there are, and how many of those
  783.      actually use wildcards in the name.  Also, bail out if any of the
  784.      wildcard names are NULL. (Can this actually happen?
  785.      walk_wild_section used to test for it.)  And bail out if any
  786.      of the wildcards are more complex than a simple string
  787.      ending in a single '*'.  */
  788.   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
  789.     {
  790.       ++sec_count;
  791.       if (sec->spec.name == NULL)
  792.         return;
  793.       if (wildcardp (sec->spec.name))
  794.         {
  795.           ++wild_name_count;
  796.           if (!is_simple_wild (sec->spec.name))
  797.             return;
  798.         }
  799.     }
  800.  
  801.   /* The zero-spec case would be easy to optimize but it doesn't
  802.      happen in practice.  Likewise, more than 4 specs doesn't
  803.      happen in practice.  */
  804.   if (sec_count == 0 || sec_count > 4)
  805.     return;
  806.  
  807.   /* Check that no two specs can match the same section.  */
  808.   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
  809.     {
  810.       struct wildcard_list *sec2;
  811.       for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
  812.         {
  813.           if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
  814.             return;
  815.         }
  816.     }
  817.  
  818.   signature = (sec_count << 8) + wild_name_count;
  819.   switch (signature)
  820.     {
  821.     case 0x0100:
  822.       ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
  823.       break;
  824.     case 0x0101:
  825.       ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
  826.       break;
  827.     case 0x0201:
  828.       ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
  829.       break;
  830.     case 0x0302:
  831.       ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
  832.       break;
  833.     case 0x0402:
  834.       ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
  835.       break;
  836.     default:
  837.       return;
  838.     }
  839.  
  840.   /* Now fill the data array with pointers to the specs, first the
  841.      specs with non-wildcard names, then the specs with wildcard
  842.      names.  It's OK to process the specs in different order from the
  843.      given order, because we've already determined that no section
  844.      will match more than one spec.  */
  845.   data_counter = 0;
  846.   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
  847.     if (!wildcardp (sec->spec.name))
  848.       ptr->handler_data[data_counter++] = sec;
  849.   for (sec = ptr->section_list; sec != NULL; sec = sec->next)
  850.     if (wildcardp (sec->spec.name))
  851.       ptr->handler_data[data_counter++] = sec;
  852. }
  853.  
  854. /* Handle a wild statement for a single file F.  */
  855.  
  856. static void
  857. walk_wild_file (lang_wild_statement_type *s,
  858.                 lang_input_statement_type *f,
  859.                 callback_t callback,
  860.                 void *data)
  861. {
  862.   if (f->the_bfd == NULL
  863.       || ! bfd_check_format (f->the_bfd, bfd_archive))
  864.     walk_wild_section (s, f, callback, data);
  865.   else
  866.     {
  867.       bfd *member;
  868.  
  869.       /* This is an archive file.  We must map each member of the
  870.          archive separately.  */
  871.       member = bfd_openr_next_archived_file (f->the_bfd, NULL);
  872.       while (member != NULL)
  873.         {
  874.           /* When lookup_name is called, it will call the add_symbols
  875.              entry point for the archive.  For each element of the
  876.              archive which is included, BFD will call ldlang_add_file,
  877.              which will set the usrdata field of the member to the
  878.              lang_input_statement.  */
  879.           if (member->usrdata != NULL)
  880.             {
  881.               walk_wild_section (s,
  882.                                  (lang_input_statement_type *) member->usrdata,
  883.                                  callback, data);
  884.             }
  885.  
  886.           member = bfd_openr_next_archived_file (f->the_bfd, member);
  887.         }
  888.     }
  889. }
  890.  
  891. static void
  892. walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
  893. {
  894.   const char *file_spec = s->filename;
  895.   char *p;
  896.  
  897.   if (file_spec == NULL)
  898.     {
  899.       /* Perform the iteration over all files in the list.  */
  900.       LANG_FOR_EACH_INPUT_STATEMENT (f)
  901.         {
  902.           walk_wild_file (s, f, callback, data);
  903.         }
  904.     }
  905.   else if ((p = archive_path (file_spec)) != NULL)
  906.     {
  907.       LANG_FOR_EACH_INPUT_STATEMENT (f)
  908.         {
  909.           if (input_statement_is_archive_path (file_spec, p, f))
  910.             walk_wild_file (s, f, callback, data);
  911.         }
  912.     }
  913.   else if (wildcardp (file_spec))
  914.     {
  915.       LANG_FOR_EACH_INPUT_STATEMENT (f)
  916.         {
  917.           if (fnmatch (file_spec, f->filename, 0) == 0)
  918.             walk_wild_file (s, f, callback, data);
  919.         }
  920.     }
  921.   else
  922.     {
  923.       lang_input_statement_type *f;
  924.  
  925.       /* Perform the iteration over a single file.  */
  926.       f = lookup_name (file_spec);
  927.       if (f)
  928.         walk_wild_file (s, f, callback, data);
  929.     }
  930. }
  931.  
  932. /* lang_for_each_statement walks the parse tree and calls the provided
  933.    function for each node, except those inside output section statements
  934.    with constraint set to -1.  */
  935.  
  936. void
  937. lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
  938.                                 lang_statement_union_type *s)
  939. {
  940.   for (; s != NULL; s = s->header.next)
  941.     {
  942.       func (s);
  943.  
  944.       switch (s->header.type)
  945.         {
  946.         case lang_constructors_statement_enum:
  947.           lang_for_each_statement_worker (func, constructor_list.head);
  948.           break;
  949.         case lang_output_section_statement_enum:
  950.           if (s->output_section_statement.constraint != -1)
  951.             lang_for_each_statement_worker
  952.               (func, s->output_section_statement.children.head);
  953.           break;
  954.         case lang_wild_statement_enum:
  955.           lang_for_each_statement_worker (func,
  956.                                           s->wild_statement.children.head);
  957.           break;
  958.         case lang_group_statement_enum:
  959.           lang_for_each_statement_worker (func,
  960.                                           s->group_statement.children.head);
  961.           break;
  962.         case lang_data_statement_enum:
  963.         case lang_reloc_statement_enum:
  964.         case lang_object_symbols_statement_enum:
  965.         case lang_output_statement_enum:
  966.         case lang_target_statement_enum:
  967.         case lang_input_section_enum:
  968.         case lang_input_statement_enum:
  969.         case lang_assignment_statement_enum:
  970.         case lang_padding_statement_enum:
  971.         case lang_address_statement_enum:
  972.         case lang_fill_statement_enum:
  973.         case lang_insert_statement_enum:
  974.           break;
  975.         default:
  976.           FAIL ();
  977.           break;
  978.         }
  979.     }
  980. }
  981.  
  982. void
  983. lang_for_each_statement (void (*func) (lang_statement_union_type *))
  984. {
  985.   lang_for_each_statement_worker (func, statement_list.head);
  986. }
  987.  
  988. /*----------------------------------------------------------------------*/
  989.  
  990. void
  991. lang_list_init (lang_statement_list_type *list)
  992. {
  993.   list->head = NULL;
  994.   list->tail = &list->head;
  995. }
  996.  
  997. void
  998. push_stat_ptr (lang_statement_list_type *new_ptr)
  999. {
  1000.   if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0]))
  1001.     abort ();
  1002.   *stat_save_ptr++ = stat_ptr;
  1003.   stat_ptr = new_ptr;
  1004. }
  1005.  
  1006. void
  1007. pop_stat_ptr (void)
  1008. {
  1009.   if (stat_save_ptr <= stat_save)
  1010.     abort ();
  1011.   stat_ptr = *--stat_save_ptr;
  1012. }
  1013.  
  1014. /* Build a new statement node for the parse tree.  */
  1015.  
  1016. static lang_statement_union_type *
  1017. new_statement (enum statement_enum type,
  1018.                size_t size,
  1019.                lang_statement_list_type *list)
  1020. {
  1021.   lang_statement_union_type *new_stmt;
  1022.  
  1023.   new_stmt = (lang_statement_union_type *) stat_alloc (size);
  1024.   new_stmt->header.type = type;
  1025.   new_stmt->header.next = NULL;
  1026.   lang_statement_append (list, new_stmt, &new_stmt->header.next);
  1027.   return new_stmt;
  1028. }
  1029.  
  1030. /* Build a new input file node for the language.  There are several
  1031.    ways in which we treat an input file, eg, we only look at symbols,
  1032.    or prefix it with a -l etc.
  1033.  
  1034.    We can be supplied with requests for input files more than once;
  1035.    they may, for example be split over several lines like foo.o(.text)
  1036.    foo.o(.data) etc, so when asked for a file we check that we haven't
  1037.    got it already so we don't duplicate the bfd.  */
  1038.  
  1039. static lang_input_statement_type *
  1040. new_afile (const char *name,
  1041.            lang_input_file_enum_type file_type,
  1042.            const char *target,
  1043.            bfd_boolean add_to_list)
  1044. {
  1045.   lang_input_statement_type *p;
  1046.  
  1047.   lang_has_input_file = TRUE;
  1048.  
  1049.   if (add_to_list)
  1050.     p = (lang_input_statement_type *) new_stat (lang_input_statement, stat_ptr);
  1051.   else
  1052.     {
  1053.       p = (lang_input_statement_type *)
  1054.           stat_alloc (sizeof (lang_input_statement_type));
  1055.       p->header.type = lang_input_statement_enum;
  1056.       p->header.next = NULL;
  1057.     }
  1058.  
  1059.   memset (&p->the_bfd, 0,
  1060.           sizeof (*p) - offsetof (lang_input_statement_type, the_bfd));
  1061.   p->target = target;
  1062.   p->flags.dynamic = input_flags.dynamic;
  1063.   p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic;
  1064.   p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular;
  1065.   p->flags.whole_archive = input_flags.whole_archive;
  1066.   p->flags.sysrooted = input_flags.sysrooted;
  1067.  
  1068.   if (file_type == lang_input_file_is_l_enum
  1069.       && name[0] == ':' && name[1] != '\0')
  1070.     {
  1071.       file_type = lang_input_file_is_search_file_enum;
  1072.       name = name + 1;
  1073.     }
  1074.  
  1075.   switch (file_type)
  1076.     {
  1077.     case lang_input_file_is_symbols_only_enum:
  1078.       p->filename = name;
  1079.       p->local_sym_name = name;
  1080.       p->flags.real = TRUE;
  1081.       p->flags.just_syms = TRUE;
  1082.       break;
  1083.     case lang_input_file_is_fake_enum:
  1084.       p->filename = name;
  1085.       p->local_sym_name = name;
  1086.       break;
  1087.     case lang_input_file_is_l_enum:
  1088.       p->filename = name;
  1089.       p->local_sym_name = concat ("-l", name, (const char *) NULL);
  1090.       p->flags.maybe_archive = TRUE;
  1091.       p->flags.real = TRUE;
  1092.       p->flags.search_dirs = TRUE;
  1093.       break;
  1094.     case lang_input_file_is_marker_enum:
  1095.       p->filename = name;
  1096.       p->local_sym_name = name;
  1097.       p->flags.search_dirs = TRUE;
  1098.       break;
  1099.     case lang_input_file_is_search_file_enum:
  1100.       p->filename = name;
  1101.       p->local_sym_name = name;
  1102.       p->flags.real = TRUE;
  1103.       p->flags.search_dirs = TRUE;
  1104.       break;
  1105.     case lang_input_file_is_file_enum:
  1106.       p->filename = name;
  1107.       p->local_sym_name = name;
  1108.       p->flags.real = TRUE;
  1109.       break;
  1110.     default:
  1111.       FAIL ();
  1112.     }
  1113.  
  1114.   lang_statement_append (&input_file_chain,
  1115.                          (lang_statement_union_type *) p,
  1116.                          &p->next_real_file);
  1117.   return p;
  1118. }
  1119.  
  1120. lang_input_statement_type *
  1121. lang_add_input_file (const char *name,
  1122.                      lang_input_file_enum_type file_type,
  1123.                      const char *target)
  1124. {
  1125.   return new_afile (name, file_type, target, TRUE);
  1126. }
  1127.  
  1128. struct out_section_hash_entry
  1129. {
  1130.   struct bfd_hash_entry root;
  1131.   lang_statement_union_type s;
  1132. };
  1133.  
  1134. /* The hash table.  */
  1135.  
  1136. static struct bfd_hash_table output_section_statement_table;
  1137.  
  1138. /* Support routines for the hash table used by lang_output_section_find,
  1139.    initialize the table, fill in an entry and remove the table.  */
  1140.  
  1141. static struct bfd_hash_entry *
  1142. output_section_statement_newfunc (struct bfd_hash_entry *entry,
  1143.                                   struct bfd_hash_table *table,
  1144.                                   const char *string)
  1145. {
  1146.   lang_output_section_statement_type **nextp;
  1147.   struct out_section_hash_entry *ret;
  1148.  
  1149.   if (entry == NULL)
  1150.     {
  1151.       entry = (struct bfd_hash_entry *) bfd_hash_allocate (table,
  1152.                                                            sizeof (*ret));
  1153.       if (entry == NULL)
  1154.         return entry;
  1155.     }
  1156.  
  1157.   entry = bfd_hash_newfunc (entry, table, string);
  1158.   if (entry == NULL)
  1159.     return entry;
  1160.  
  1161.   ret = (struct out_section_hash_entry *) entry;
  1162.   memset (&ret->s, 0, sizeof (ret->s));
  1163.   ret->s.header.type = lang_output_section_statement_enum;
  1164.   ret->s.output_section_statement.subsection_alignment = -1;
  1165.   ret->s.output_section_statement.section_alignment = -1;
  1166.   ret->s.output_section_statement.block_value = 1;
  1167.   lang_list_init (&ret->s.output_section_statement.children);
  1168.   lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
  1169.  
  1170.   /* For every output section statement added to the list, except the
  1171.      first one, lang_output_section_statement.tail points to the "next"
  1172.      field of the last element of the list.  */
  1173.   if (lang_output_section_statement.head != NULL)
  1174.     ret->s.output_section_statement.prev
  1175.       = ((lang_output_section_statement_type *)
  1176.          ((char *) lang_output_section_statement.tail
  1177.           - offsetof (lang_output_section_statement_type, next)));
  1178.  
  1179.   /* GCC's strict aliasing rules prevent us from just casting the
  1180.      address, so we store the pointer in a variable and cast that
  1181.      instead.  */
  1182.   nextp = &ret->s.output_section_statement.next;
  1183.   lang_statement_append (&lang_output_section_statement,
  1184.                          &ret->s,
  1185.                          (lang_statement_union_type **) nextp);
  1186.   return &ret->root;
  1187. }
  1188.  
  1189. static void
  1190. output_section_statement_table_init (void)
  1191. {
  1192.   if (!bfd_hash_table_init_n (&output_section_statement_table,
  1193.                               output_section_statement_newfunc,
  1194.                               sizeof (struct out_section_hash_entry),
  1195.                               61))
  1196.     einfo (_("%P%F: can not create hash table: %E\n"));
  1197. }
  1198.  
  1199. static void
  1200. output_section_statement_table_free (void)
  1201. {
  1202.   bfd_hash_table_free (&output_section_statement_table);
  1203. }
  1204.  
  1205. /* Build enough state so that the parser can build its tree.  */
  1206.  
  1207. void
  1208. lang_init (void)
  1209. {
  1210.   obstack_begin (&stat_obstack, 1000);
  1211.  
  1212.   stat_ptr = &statement_list;
  1213.  
  1214.   output_section_statement_table_init ();
  1215.  
  1216.   lang_list_init (stat_ptr);
  1217.  
  1218.   lang_list_init (&input_file_chain);
  1219.   lang_list_init (&lang_output_section_statement);
  1220.   lang_list_init (&file_chain);
  1221.   first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
  1222.                                     NULL);
  1223.   abs_output_section =
  1224.     lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, TRUE);
  1225.  
  1226.   abs_output_section->bfd_section = bfd_abs_section_ptr;
  1227.  
  1228.   /* The value "3" is ad-hoc, somewhat related to the expected number of
  1229.      DEFINED expressions in a linker script.  For most default linker
  1230.      scripts, there are none.  Why a hash table then?  Well, it's somewhat
  1231.      simpler to re-use working machinery than using a linked list in terms
  1232.      of code-complexity here in ld, besides the initialization which just
  1233.      looks like other code here.  */
  1234.   if (!bfd_hash_table_init_n (&lang_definedness_table,
  1235.                               lang_definedness_newfunc,
  1236.                               sizeof (struct lang_definedness_hash_entry),
  1237.                               3))
  1238.     einfo (_("%P%F: can not create hash table: %E\n"));
  1239. }
  1240.  
  1241. void
  1242. lang_finish (void)
  1243. {
  1244.   bfd_link_hash_table_free (link_info.output_bfd, link_info.hash);
  1245.   bfd_hash_table_free (&lang_definedness_table);
  1246.   output_section_statement_table_free ();
  1247. }
  1248.  
  1249. /*----------------------------------------------------------------------
  1250.   A region is an area of memory declared with the
  1251.   MEMORY {  name:org=exp, len=exp ... }
  1252.   syntax.
  1253.  
  1254.   We maintain a list of all the regions here.
  1255.  
  1256.   If no regions are specified in the script, then the default is used
  1257.   which is created when looked up to be the entire data space.
  1258.  
  1259.   If create is true we are creating a region inside a MEMORY block.
  1260.   In this case it is probably an error to create a region that has
  1261.   already been created.  If we are not inside a MEMORY block it is
  1262.   dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
  1263.   and so we issue a warning.
  1264.  
  1265.   Each region has at least one name.  The first name is either
  1266.   DEFAULT_MEMORY_REGION or the name given in the MEMORY block.  You can add
  1267.   alias names to an existing region within a script with
  1268.   REGION_ALIAS (alias, region_name).  Each name corresponds to at most one
  1269.   region.  */
  1270.  
  1271. static lang_memory_region_type *lang_memory_region_list;
  1272. static lang_memory_region_type **lang_memory_region_list_tail
  1273.   = &lang_memory_region_list;
  1274.  
  1275. lang_memory_region_type *
  1276. lang_memory_region_lookup (const char *const name, bfd_boolean create)
  1277. {
  1278.   lang_memory_region_name *n;
  1279.   lang_memory_region_type *r;
  1280.   lang_memory_region_type *new_region;
  1281.  
  1282.   /* NAME is NULL for LMA memspecs if no region was specified.  */
  1283.   if (name == NULL)
  1284.     return NULL;
  1285.  
  1286.   for (r = lang_memory_region_list; r != NULL; r = r->next)
  1287.     for (n = &r->name_list; n != NULL; n = n->next)
  1288.       if (strcmp (n->name, name) == 0)
  1289.         {
  1290.           if (create)
  1291.             einfo (_("%P:%S: warning: redeclaration of memory region `%s'\n"),
  1292.                    NULL, name);
  1293.           return r;
  1294.         }
  1295.  
  1296.   if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
  1297.     einfo (_("%P:%S: warning: memory region `%s' not declared\n"),
  1298.            NULL, name);
  1299.  
  1300.   new_region = (lang_memory_region_type *)
  1301.       stat_alloc (sizeof (lang_memory_region_type));
  1302.  
  1303.   new_region->name_list.name = xstrdup (name);
  1304.   new_region->name_list.next = NULL;
  1305.   new_region->next = NULL;
  1306.   new_region->origin = 0;
  1307.   new_region->length = ~(bfd_size_type) 0;
  1308.   new_region->current = 0;
  1309.   new_region->last_os = NULL;
  1310.   new_region->flags = 0;
  1311.   new_region->not_flags = 0;
  1312.   new_region->had_full_message = FALSE;
  1313.  
  1314.   *lang_memory_region_list_tail = new_region;
  1315.   lang_memory_region_list_tail = &new_region->next;
  1316.  
  1317.   return new_region;
  1318. }
  1319.  
  1320. void
  1321. lang_memory_region_alias (const char * alias, const char * region_name)
  1322. {
  1323.   lang_memory_region_name * n;
  1324.   lang_memory_region_type * r;
  1325.   lang_memory_region_type * region;
  1326.  
  1327.   /* The default region must be unique.  This ensures that it is not necessary
  1328.      to iterate through the name list if someone wants the check if a region is
  1329.      the default memory region.  */
  1330.   if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0
  1331.       || strcmp (alias, DEFAULT_MEMORY_REGION) == 0)
  1332.     einfo (_("%F%P:%S: error: alias for default memory region\n"), NULL);
  1333.  
  1334.   /* Look for the target region and check if the alias is not already
  1335.      in use.  */
  1336.   region = NULL;
  1337.   for (r = lang_memory_region_list; r != NULL; r = r->next)
  1338.     for (n = &r->name_list; n != NULL; n = n->next)
  1339.       {
  1340.         if (region == NULL && strcmp (n->name, region_name) == 0)
  1341.           region = r;
  1342.         if (strcmp (n->name, alias) == 0)
  1343.           einfo (_("%F%P:%S: error: redefinition of memory region "
  1344.                    "alias `%s'\n"),
  1345.                  NULL, alias);
  1346.       }
  1347.  
  1348.   /* Check if the target region exists.  */
  1349.   if (region == NULL)
  1350.     einfo (_("%F%P:%S: error: memory region `%s' "
  1351.              "for alias `%s' does not exist\n"),
  1352.            NULL, region_name, alias);
  1353.  
  1354.   /* Add alias to region name list.  */
  1355.   n = (lang_memory_region_name *) stat_alloc (sizeof (lang_memory_region_name));
  1356.   n->name = xstrdup (alias);
  1357.   n->next = region->name_list.next;
  1358.   region->name_list.next = n;
  1359. }
  1360.  
  1361. static lang_memory_region_type *
  1362. lang_memory_default (asection * section)
  1363. {
  1364.   lang_memory_region_type *p;
  1365.  
  1366.   flagword sec_flags = section->flags;
  1367.  
  1368.   /* Override SEC_DATA to mean a writable section.  */
  1369.   if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
  1370.     sec_flags |= SEC_DATA;
  1371.  
  1372.   for (p = lang_memory_region_list; p != NULL; p = p->next)
  1373.     {
  1374.       if ((p->flags & sec_flags) != 0
  1375.           && (p->not_flags & sec_flags) == 0)
  1376.         {
  1377.           return p;
  1378.         }
  1379.     }
  1380.   return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
  1381. }
  1382.  
  1383. /* Find or create an output_section_statement with the given NAME.
  1384.    If CONSTRAINT is non-zero match one with that constraint, otherwise
  1385.    match any non-negative constraint.  If CREATE, always make a
  1386.    new output_section_statement for SPECIAL CONSTRAINT.  */
  1387.  
  1388. lang_output_section_statement_type *
  1389. lang_output_section_statement_lookup (const char *name,
  1390.                                       int constraint,
  1391.                                       bfd_boolean create)
  1392. {
  1393.   struct out_section_hash_entry *entry;
  1394.  
  1395.   entry = ((struct out_section_hash_entry *)
  1396.            bfd_hash_lookup (&output_section_statement_table, name,
  1397.                             create, FALSE));
  1398.   if (entry == NULL)
  1399.     {
  1400.       if (create)
  1401.         einfo (_("%P%F: failed creating section `%s': %E\n"), name);
  1402.       return NULL;
  1403.     }
  1404.  
  1405.   if (entry->s.output_section_statement.name != NULL)
  1406.     {
  1407.       /* We have a section of this name, but it might not have the correct
  1408.          constraint.  */
  1409.       struct out_section_hash_entry *last_ent;
  1410.  
  1411.       name = entry->s.output_section_statement.name;
  1412.       if (create && constraint == SPECIAL)
  1413.         /* Not traversing to the end reverses the order of the second
  1414.            and subsequent SPECIAL sections in the hash table chain,
  1415.            but that shouldn't matter.  */
  1416.         last_ent = entry;
  1417.       else
  1418.         do
  1419.           {
  1420.             if (constraint == entry->s.output_section_statement.constraint
  1421.                 || (constraint == 0
  1422.                     && entry->s.output_section_statement.constraint >= 0))
  1423.               return &entry->s.output_section_statement;
  1424.             last_ent = entry;
  1425.             entry = (struct out_section_hash_entry *) entry->root.next;
  1426.           }
  1427.         while (entry != NULL
  1428.                && name == entry->s.output_section_statement.name);
  1429.  
  1430.       if (!create)
  1431.         return NULL;
  1432.  
  1433.       entry
  1434.         = ((struct out_section_hash_entry *)
  1435.            output_section_statement_newfunc (NULL,
  1436.                                              &output_section_statement_table,
  1437.                                              name));
  1438.       if (entry == NULL)
  1439.         {
  1440.           einfo (_("%P%F: failed creating section `%s': %E\n"), name);
  1441.           return NULL;
  1442.         }
  1443.       entry->root = last_ent->root;
  1444.       last_ent->root.next = &entry->root;
  1445.     }
  1446.  
  1447.   entry->s.output_section_statement.name = name;
  1448.   entry->s.output_section_statement.constraint = constraint;
  1449.   return &entry->s.output_section_statement;
  1450. }
  1451.  
  1452. /* Find the next output_section_statement with the same name as OS.
  1453.    If CONSTRAINT is non-zero, find one with that constraint otherwise
  1454.    match any non-negative constraint.  */
  1455.  
  1456. lang_output_section_statement_type *
  1457. next_matching_output_section_statement (lang_output_section_statement_type *os,
  1458.                                         int constraint)
  1459. {
  1460.   /* All output_section_statements are actually part of a
  1461.      struct out_section_hash_entry.  */
  1462.   struct out_section_hash_entry *entry = (struct out_section_hash_entry *)
  1463.     ((char *) os
  1464.      - offsetof (struct out_section_hash_entry, s.output_section_statement));
  1465.   const char *name = os->name;
  1466.  
  1467.   ASSERT (name == entry->root.string);
  1468.   do
  1469.     {
  1470.       entry = (struct out_section_hash_entry *) entry->root.next;
  1471.       if (entry == NULL
  1472.           || name != entry->s.output_section_statement.name)
  1473.         return NULL;
  1474.     }
  1475.   while (constraint != entry->s.output_section_statement.constraint
  1476.          && (constraint != 0
  1477.              || entry->s.output_section_statement.constraint < 0));
  1478.  
  1479.   return &entry->s.output_section_statement;
  1480. }
  1481.  
  1482. /* A variant of lang_output_section_find used by place_orphan.
  1483.    Returns the output statement that should precede a new output
  1484.    statement for SEC.  If an exact match is found on certain flags,
  1485.    sets *EXACT too.  */
  1486.  
  1487. lang_output_section_statement_type *
  1488. lang_output_section_find_by_flags (const asection *sec,
  1489.                                    lang_output_section_statement_type **exact,
  1490.                                    lang_match_sec_type_func match_type)
  1491. {
  1492.   lang_output_section_statement_type *first, *look, *found;
  1493.   flagword flags;
  1494.  
  1495.   /* We know the first statement on this list is *ABS*.  May as well
  1496.      skip it.  */
  1497.   first = &lang_output_section_statement.head->output_section_statement;
  1498.   first = first->next;
  1499.  
  1500.   /* First try for an exact match.  */
  1501.   found = NULL;
  1502.   for (look = first; look; look = look->next)
  1503.     {
  1504.       flags = look->flags;
  1505.       if (look->bfd_section != NULL)
  1506.         {
  1507.           flags = look->bfd_section->flags;
  1508.           if (match_type && !match_type (link_info.output_bfd,
  1509.                                          look->bfd_section,
  1510.                                          sec->owner, sec))
  1511.             continue;
  1512.         }
  1513.       flags ^= sec->flags;
  1514.       if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
  1515.                      | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
  1516.         found = look;
  1517.     }
  1518.   if (found != NULL)
  1519.     {
  1520.       if (exact != NULL)
  1521.         *exact = found;
  1522.       return found;
  1523.     }
  1524.  
  1525.   if ((sec->flags & SEC_CODE) != 0
  1526.       && (sec->flags & SEC_ALLOC) != 0)
  1527.     {
  1528.       /* Try for a rw code section.  */
  1529.       for (look = first; look; look = look->next)
  1530.         {
  1531.           flags = look->flags;
  1532.           if (look->bfd_section != NULL)
  1533.             {
  1534.               flags = look->bfd_section->flags;
  1535.               if (match_type && !match_type (link_info.output_bfd,
  1536.                                              look->bfd_section,
  1537.                                              sec->owner, sec))
  1538.                 continue;
  1539.             }
  1540.           flags ^= sec->flags;
  1541.           if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
  1542.                          | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
  1543.             found = look;
  1544.         }
  1545.     }
  1546.   else if ((sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL)) != 0
  1547.            && (sec->flags & SEC_ALLOC) != 0)
  1548.     {
  1549.       /* .rodata can go after .text, .sdata2 after .rodata.  */
  1550.       for (look = first; look; look = look->next)
  1551.         {
  1552.           flags = look->flags;
  1553.           if (look->bfd_section != NULL)
  1554.             {
  1555.               flags = look->bfd_section->flags;
  1556.               if (match_type && !match_type (link_info.output_bfd,
  1557.                                              look->bfd_section,
  1558.                                              sec->owner, sec))
  1559.                 continue;
  1560.             }
  1561.           flags ^= sec->flags;
  1562.           if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
  1563.                          | SEC_READONLY | SEC_SMALL_DATA))
  1564.               || (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
  1565.                              | SEC_READONLY))
  1566.                   && !(look->flags & SEC_SMALL_DATA))
  1567.               || (!(flags & (SEC_THREAD_LOCAL | SEC_ALLOC))
  1568.                   && (look->flags & SEC_THREAD_LOCAL)
  1569.                   && (!(flags & SEC_LOAD)
  1570.                       || (look->flags & SEC_LOAD))))
  1571.             found = look;
  1572.         }
  1573.     }
  1574.   else if ((sec->flags & SEC_SMALL_DATA) != 0
  1575.            && (sec->flags & SEC_ALLOC) != 0)
  1576.     {
  1577.       /* .sdata goes after .data, .sbss after .sdata.  */
  1578.       for (look = first; look; look = look->next)
  1579.         {
  1580.           flags = look->flags;
  1581.           if (look->bfd_section != NULL)
  1582.             {
  1583.               flags = look->bfd_section->flags;
  1584.               if (match_type && !match_type (link_info.output_bfd,
  1585.                                              look->bfd_section,
  1586.                                              sec->owner, sec))
  1587.                 continue;
  1588.             }
  1589.           flags ^= sec->flags;
  1590.           if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
  1591.                          | SEC_THREAD_LOCAL))
  1592.               || ((look->flags & SEC_SMALL_DATA)
  1593.                   && !(sec->flags & SEC_HAS_CONTENTS)))
  1594.             found = look;
  1595.         }
  1596.     }
  1597.   else if ((sec->flags & SEC_HAS_CONTENTS) != 0
  1598.            && (sec->flags & SEC_ALLOC) != 0)
  1599.     {
  1600.       /* .data goes after .rodata.  */
  1601.       for (look = first; look; look = look->next)
  1602.         {
  1603.           flags = look->flags;
  1604.           if (look->bfd_section != NULL)
  1605.             {
  1606.               flags = look->bfd_section->flags;
  1607.               if (match_type && !match_type (link_info.output_bfd,
  1608.                                              look->bfd_section,
  1609.                                              sec->owner, sec))
  1610.                 continue;
  1611.             }
  1612.           flags ^= sec->flags;
  1613.           if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
  1614.                          | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
  1615.             found = look;
  1616.         }
  1617.     }
  1618.   else if ((sec->flags & SEC_ALLOC) != 0)
  1619.     {
  1620.       /* .bss goes after any other alloc section.  */
  1621.       for (look = first; look; look = look->next)
  1622.         {
  1623.           flags = look->flags;
  1624.           if (look->bfd_section != NULL)
  1625.             {
  1626.               flags = look->bfd_section->flags;
  1627.               if (match_type && !match_type (link_info.output_bfd,
  1628.                                              look->bfd_section,
  1629.                                              sec->owner, sec))
  1630.                 continue;
  1631.             }
  1632.           flags ^= sec->flags;
  1633.           if (!(flags & SEC_ALLOC))
  1634.             found = look;
  1635.         }
  1636.     }
  1637.   else
  1638.     {
  1639.       /* non-alloc go last.  */
  1640.       for (look = first; look; look = look->next)
  1641.         {
  1642.           flags = look->flags;
  1643.           if (look->bfd_section != NULL)
  1644.             flags = look->bfd_section->flags;
  1645.           flags ^= sec->flags;
  1646.           if (!(flags & SEC_DEBUGGING))
  1647.             found = look;
  1648.         }
  1649.       return found;
  1650.     }
  1651.  
  1652.   if (found || !match_type)
  1653.     return found;
  1654.  
  1655.   return lang_output_section_find_by_flags (sec, NULL, NULL);
  1656. }
  1657.  
  1658. /* Find the last output section before given output statement.
  1659.    Used by place_orphan.  */
  1660.  
  1661. static asection *
  1662. output_prev_sec_find (lang_output_section_statement_type *os)
  1663. {
  1664.   lang_output_section_statement_type *lookup;
  1665.  
  1666.   for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
  1667.     {
  1668.       if (lookup->constraint < 0)
  1669.         continue;
  1670.  
  1671.       if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
  1672.         return lookup->bfd_section;
  1673.     }
  1674.  
  1675.   return NULL;
  1676. }
  1677.  
  1678. /* Look for a suitable place for a new output section statement.  The
  1679.    idea is to skip over anything that might be inside a SECTIONS {}
  1680.    statement in a script, before we find another output section
  1681.    statement.  Assignments to "dot" before an output section statement
  1682.    are assumed to belong to it, except in two cases;  The first
  1683.    assignment to dot, and assignments before non-alloc sections.
  1684.    Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
  1685.    similar assignments that set the initial address, or we might
  1686.    insert non-alloc note sections among assignments setting end of
  1687.    image symbols.  */
  1688.  
  1689. static lang_statement_union_type **
  1690. insert_os_after (lang_output_section_statement_type *after)
  1691. {
  1692.   lang_statement_union_type **where;
  1693.   lang_statement_union_type **assign = NULL;
  1694.   bfd_boolean ignore_first;
  1695.  
  1696.   ignore_first
  1697.     = after == &lang_output_section_statement.head->output_section_statement;
  1698.  
  1699.   for (where = &after->header.next;
  1700.        *where != NULL;
  1701.        where = &(*where)->header.next)
  1702.     {
  1703.       switch ((*where)->header.type)
  1704.         {
  1705.         case lang_assignment_statement_enum:
  1706.           if (assign == NULL)
  1707.             {
  1708.               lang_assignment_statement_type *ass;
  1709.  
  1710.               ass = &(*where)->assignment_statement;
  1711.               if (ass->exp->type.node_class != etree_assert
  1712.                   && ass->exp->assign.dst[0] == '.'
  1713.                   && ass->exp->assign.dst[1] == 0
  1714.                   && !ignore_first)
  1715.                 assign = where;
  1716.             }
  1717.           ignore_first = FALSE;
  1718.           continue;
  1719.         case lang_wild_statement_enum:
  1720.         case lang_input_section_enum:
  1721.         case lang_object_symbols_statement_enum:
  1722.         case lang_fill_statement_enum:
  1723.         case lang_data_statement_enum:
  1724.         case lang_reloc_statement_enum:
  1725.         case lang_padding_statement_enum:
  1726.         case lang_constructors_statement_enum:
  1727.           assign = NULL;
  1728.           continue;
  1729.         case lang_output_section_statement_enum:
  1730.           if (assign != NULL)
  1731.             {
  1732.               asection *s = (*where)->output_section_statement.bfd_section;
  1733.  
  1734.               if (s == NULL
  1735.                   || s->map_head.s == NULL
  1736.                   || (s->flags & SEC_ALLOC) != 0)
  1737.                 where = assign;
  1738.             }
  1739.           break;
  1740.         case lang_input_statement_enum:
  1741.         case lang_address_statement_enum:
  1742.         case lang_target_statement_enum:
  1743.         case lang_output_statement_enum:
  1744.         case lang_group_statement_enum:
  1745.         case lang_insert_statement_enum:
  1746.           continue;
  1747.         }
  1748.       break;
  1749.     }
  1750.  
  1751.   return where;
  1752. }
  1753.  
  1754. lang_output_section_statement_type *
  1755. lang_insert_orphan (asection *s,
  1756.                     const char *secname,
  1757.                     int constraint,
  1758.                     lang_output_section_statement_type *after,
  1759.                     struct orphan_save *place,
  1760.                     etree_type *address,
  1761.                     lang_statement_list_type *add_child)
  1762. {
  1763.   lang_statement_list_type add;
  1764.   const char *ps;
  1765.   lang_output_section_statement_type *os;
  1766.   lang_output_section_statement_type **os_tail;
  1767.  
  1768.   /* If we have found an appropriate place for the output section
  1769.      statements for this orphan, add them to our own private list,
  1770.      inserting them later into the global statement list.  */
  1771.   if (after != NULL)
  1772.     {
  1773.       lang_list_init (&add);
  1774.       push_stat_ptr (&add);
  1775.     }
  1776.  
  1777.   if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
  1778.     address = exp_intop (0);
  1779.  
  1780.   os_tail = ((lang_output_section_statement_type **)
  1781.              lang_output_section_statement.tail);
  1782.   os = lang_enter_output_section_statement (secname, address, normal_section,
  1783.                                             NULL, NULL, NULL, constraint, 0);
  1784.  
  1785.   ps = NULL;
  1786.   if (config.build_constructors && *os_tail == os)
  1787.     {
  1788.       /* If the name of the section is representable in C, then create
  1789.          symbols to mark the start and the end of the section.  */
  1790.       for (ps = secname; *ps != '\0'; ps++)
  1791.         if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
  1792.           break;
  1793.       if (*ps == '\0')
  1794.         {
  1795.           char *symname;
  1796.  
  1797.           symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
  1798.           symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
  1799.           sprintf (symname + (symname[0] != 0), "__start_%s", secname);
  1800.           lang_add_assignment (exp_provide (symname,
  1801.                                             exp_nameop (NAME, "."),
  1802.                                             FALSE));
  1803.         }
  1804.     }
  1805.  
  1806.   if (add_child == NULL)
  1807.     add_child = &os->children;
  1808.   lang_add_section (add_child, s, NULL, os);
  1809.  
  1810.   if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0)
  1811.     {
  1812.       const char *region = (after->region
  1813.                             ? after->region->name_list.name
  1814.                             : DEFAULT_MEMORY_REGION);
  1815.       const char *lma_region = (after->lma_region
  1816.                                 ? after->lma_region->name_list.name
  1817.                                 : NULL);
  1818.       lang_leave_output_section_statement (NULL, region, after->phdrs,
  1819.                                            lma_region);
  1820.     }
  1821.   else
  1822.     lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL,
  1823.                                          NULL);
  1824.  
  1825.   if (ps != NULL && *ps == '\0')
  1826.     {
  1827.       char *symname;
  1828.  
  1829.       symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
  1830.       symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
  1831.       sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
  1832.       lang_add_assignment (exp_provide (symname,
  1833.                                         exp_nameop (NAME, "."),
  1834.                                         FALSE));
  1835.     }
  1836.  
  1837.   /* Restore the global list pointer.  */
  1838.   if (after != NULL)
  1839.     pop_stat_ptr ();
  1840.  
  1841.   if (after != NULL && os->bfd_section != NULL)
  1842.     {
  1843.       asection *snew, *as;
  1844.  
  1845.       snew = os->bfd_section;
  1846.  
  1847.       /* Shuffle the bfd section list to make the output file look
  1848.          neater.  This is really only cosmetic.  */
  1849.       if (place->section == NULL
  1850.           && after != (&lang_output_section_statement.head
  1851.                        ->output_section_statement))
  1852.         {
  1853.           asection *bfd_section = after->bfd_section;
  1854.  
  1855.           /* If the output statement hasn't been used to place any input
  1856.              sections (and thus doesn't have an output bfd_section),
  1857.              look for the closest prior output statement having an
  1858.              output section.  */
  1859.           if (bfd_section == NULL)
  1860.             bfd_section = output_prev_sec_find (after);
  1861.  
  1862.           if (bfd_section != NULL && bfd_section != snew)
  1863.             place->section = &bfd_section->next;
  1864.         }
  1865.  
  1866.       if (place->section == NULL)
  1867.         place->section = &link_info.output_bfd->sections;
  1868.  
  1869.       as = *place->section;
  1870.  
  1871.       if (!as)
  1872.         {
  1873.           /* Put the section at the end of the list.  */
  1874.  
  1875.           /* Unlink the section.  */
  1876.           bfd_section_list_remove (link_info.output_bfd, snew);
  1877.  
  1878.           /* Now tack it back on in the right place.  */
  1879.           bfd_section_list_append (link_info.output_bfd, snew);
  1880.         }
  1881.       else if (as != snew && as->prev != snew)
  1882.         {
  1883.           /* Unlink the section.  */
  1884.           bfd_section_list_remove (link_info.output_bfd, snew);
  1885.  
  1886.           /* Now tack it back on in the right place.  */
  1887.           bfd_section_list_insert_before (link_info.output_bfd, as, snew);
  1888.         }
  1889.  
  1890.       /* Save the end of this list.  Further ophans of this type will
  1891.          follow the one we've just added.  */
  1892.       place->section = &snew->next;
  1893.  
  1894.       /* The following is non-cosmetic.  We try to put the output
  1895.          statements in some sort of reasonable order here, because they
  1896.          determine the final load addresses of the orphan sections.
  1897.          In addition, placing output statements in the wrong order may
  1898.          require extra segments.  For instance, given a typical
  1899.          situation of all read-only sections placed in one segment and
  1900.          following that a segment containing all the read-write
  1901.          sections, we wouldn't want to place an orphan read/write
  1902.          section before or amongst the read-only ones.  */
  1903.       if (add.head != NULL)
  1904.         {
  1905.           lang_output_section_statement_type *newly_added_os;
  1906.  
  1907.           if (place->stmt == NULL)
  1908.             {
  1909.               lang_statement_union_type **where = insert_os_after (after);
  1910.  
  1911.               *add.tail = *where;
  1912.               *where = add.head;
  1913.  
  1914.               place->os_tail = &after->next;
  1915.             }
  1916.           else
  1917.             {
  1918.               /* Put it after the last orphan statement we added.  */
  1919.               *add.tail = *place->stmt;
  1920.               *place->stmt = add.head;
  1921.             }
  1922.  
  1923.           /* Fix the global list pointer if we happened to tack our
  1924.              new list at the tail.  */
  1925.           if (*stat_ptr->tail == add.head)
  1926.             stat_ptr->tail = add.tail;
  1927.  
  1928.           /* Save the end of this list.  */
  1929.           place->stmt = add.tail;
  1930.  
  1931.           /* Do the same for the list of output section statements.  */
  1932.           newly_added_os = *os_tail;
  1933.           *os_tail = NULL;
  1934.           newly_added_os->prev = (lang_output_section_statement_type *)
  1935.             ((char *) place->os_tail
  1936.              - offsetof (lang_output_section_statement_type, next));
  1937.           newly_added_os->next = *place->os_tail;
  1938.           if (newly_added_os->next != NULL)
  1939.             newly_added_os->next->prev = newly_added_os;
  1940.           *place->os_tail = newly_added_os;
  1941.           place->os_tail = &newly_added_os->next;
  1942.  
  1943.           /* Fixing the global list pointer here is a little different.
  1944.              We added to the list in lang_enter_output_section_statement,
  1945.              trimmed off the new output_section_statment above when
  1946.              assigning *os_tail = NULL, but possibly added it back in
  1947.              the same place when assigning *place->os_tail.  */
  1948.           if (*os_tail == NULL)
  1949.             lang_output_section_statement.tail
  1950.               = (lang_statement_union_type **) os_tail;
  1951.         }
  1952.     }
  1953.   return os;
  1954. }
  1955.  
  1956. static void
  1957. lang_map_flags (flagword flag)
  1958. {
  1959.   if (flag & SEC_ALLOC)
  1960.     minfo ("a");
  1961.  
  1962.   if (flag & SEC_CODE)
  1963.     minfo ("x");
  1964.  
  1965.   if (flag & SEC_READONLY)
  1966.     minfo ("r");
  1967.  
  1968.   if (flag & SEC_DATA)
  1969.     minfo ("w");
  1970.  
  1971.   if (flag & SEC_LOAD)
  1972.     minfo ("l");
  1973. }
  1974.  
  1975. void
  1976. lang_map (void)
  1977. {
  1978.   lang_memory_region_type *m;
  1979.   bfd_boolean dis_header_printed = FALSE;
  1980.   bfd *p;
  1981.  
  1982.   LANG_FOR_EACH_INPUT_STATEMENT (file)
  1983.     {
  1984.       asection *s;
  1985.  
  1986.       if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
  1987.           || file->flags.just_syms)
  1988.         continue;
  1989.  
  1990.       for (s = file->the_bfd->sections; s != NULL; s = s->next)
  1991.         if ((s->output_section == NULL
  1992.              || s->output_section->owner != link_info.output_bfd)
  1993.             && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
  1994.           {
  1995.             if (! dis_header_printed)
  1996.               {
  1997.                 fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
  1998.                 dis_header_printed = TRUE;
  1999.               }
  2000.  
  2001.             print_input_section (s, TRUE);
  2002.           }
  2003.     }
  2004.  
  2005.   minfo (_("\nMemory Configuration\n\n"));
  2006.   fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
  2007.            _("Name"), _("Origin"), _("Length"), _("Attributes"));
  2008.  
  2009.   for (m = lang_memory_region_list; m != NULL; m = m->next)
  2010.     {
  2011.       char buf[100];
  2012.       int len;
  2013.  
  2014.       fprintf (config.map_file, "%-16s ", m->name_list.name);
  2015.  
  2016.       sprintf_vma (buf, m->origin);
  2017.       minfo ("0x%s ", buf);
  2018.       len = strlen (buf);
  2019.       while (len < 16)
  2020.         {
  2021.           print_space ();
  2022.           ++len;
  2023.         }
  2024.  
  2025.       minfo ("0x%V", m->length);
  2026.       if (m->flags || m->not_flags)
  2027.         {
  2028. #ifndef BFD64
  2029.           minfo ("        ");
  2030. #endif
  2031.           if (m->flags)
  2032.             {
  2033.               print_space ();
  2034.               lang_map_flags (m->flags);
  2035.             }
  2036.  
  2037.           if (m->not_flags)
  2038.             {
  2039.               minfo (" !");
  2040.               lang_map_flags (m->not_flags);
  2041.             }
  2042.         }
  2043.  
  2044.       print_nl ();
  2045.     }
  2046.  
  2047.   fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
  2048.  
  2049.   if (! link_info.reduce_memory_overheads)
  2050.     {
  2051.       obstack_begin (&map_obstack, 1000);
  2052.       for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
  2053.         bfd_map_over_sections (p, init_map_userdata, 0);
  2054.       bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
  2055.     }
  2056.   lang_statement_iteration ++;
  2057.   print_statements ();
  2058. }
  2059.  
  2060. static void
  2061. init_map_userdata (bfd *abfd ATTRIBUTE_UNUSED,
  2062.                    asection *sec,
  2063.                    void *data ATTRIBUTE_UNUSED)
  2064. {
  2065.   fat_section_userdata_type *new_data
  2066.     = ((fat_section_userdata_type *) (stat_alloc
  2067.                                       (sizeof (fat_section_userdata_type))));
  2068.  
  2069.   ASSERT (get_userdata (sec) == NULL);
  2070.   get_userdata (sec) = new_data;
  2071.   new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
  2072.   new_data->map_symbol_def_count = 0;
  2073. }
  2074.  
  2075. static bfd_boolean
  2076. sort_def_symbol (struct bfd_link_hash_entry *hash_entry,
  2077.                  void *info ATTRIBUTE_UNUSED)
  2078. {
  2079.   if (hash_entry->type == bfd_link_hash_defined
  2080.       || hash_entry->type == bfd_link_hash_defweak)
  2081.     {
  2082.       struct fat_user_section_struct *ud;
  2083.       struct map_symbol_def *def;
  2084.  
  2085.       ud = (struct fat_user_section_struct *)
  2086.           get_userdata (hash_entry->u.def.section);
  2087.       if  (! ud)
  2088.         {
  2089.           /* ??? What do we have to do to initialize this beforehand?  */
  2090.           /* The first time we get here is bfd_abs_section...  */
  2091.           init_map_userdata (0, hash_entry->u.def.section, 0);
  2092.           ud = (struct fat_user_section_struct *)
  2093.               get_userdata (hash_entry->u.def.section);
  2094.         }
  2095.       else if  (!ud->map_symbol_def_tail)
  2096.         ud->map_symbol_def_tail = &ud->map_symbol_def_head;
  2097.  
  2098.       def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def);
  2099.       def->entry = hash_entry;
  2100.       *(ud->map_symbol_def_tail) = def;
  2101.       ud->map_symbol_def_tail = &def->next;
  2102.       ud->map_symbol_def_count++;
  2103.     }
  2104.   return TRUE;
  2105. }
  2106.  
  2107. /* Initialize an output section.  */
  2108.  
  2109. static void
  2110. init_os (lang_output_section_statement_type *s, flagword flags)
  2111. {
  2112.   if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
  2113.     einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
  2114.  
  2115.   if (s->constraint != SPECIAL)
  2116.     s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name);
  2117.   if (s->bfd_section == NULL)
  2118.     s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd,
  2119.                                                          s->name, flags);
  2120.   if (s->bfd_section == NULL)
  2121.     {
  2122.       einfo (_("%P%F: output format %s cannot represent section called %s\n"),
  2123.              link_info.output_bfd->xvec->name, s->name);
  2124.     }
  2125.   s->bfd_section->output_section = s->bfd_section;
  2126.   s->bfd_section->output_offset = 0;
  2127.  
  2128.   if (!link_info.reduce_memory_overheads)
  2129.     {
  2130.       fat_section_userdata_type *new_userdata = (fat_section_userdata_type *)
  2131.         stat_alloc (sizeof (fat_section_userdata_type));
  2132.       memset (new_userdata, 0, sizeof (fat_section_userdata_type));
  2133.       get_userdata (s->bfd_section) = new_userdata;
  2134.     }
  2135.  
  2136.   /* If there is a base address, make sure that any sections it might
  2137.      mention are initialized.  */
  2138.   if (s->addr_tree != NULL)
  2139.     exp_init_os (s->addr_tree);
  2140.  
  2141.   if (s->load_base != NULL)
  2142.     exp_init_os (s->load_base);
  2143.  
  2144.   /* If supplied an alignment, set it.  */
  2145.   if (s->section_alignment != -1)
  2146.     s->bfd_section->alignment_power = s->section_alignment;
  2147. }
  2148.  
  2149. /* Make sure that all output sections mentioned in an expression are
  2150.    initialized.  */
  2151.  
  2152. static void
  2153. exp_init_os (etree_type *exp)
  2154. {
  2155.   switch (exp->type.node_class)
  2156.     {
  2157.     case etree_assign:
  2158.     case etree_provide:
  2159.       exp_init_os (exp->assign.src);
  2160.       break;
  2161.  
  2162.     case etree_binary:
  2163.       exp_init_os (exp->binary.lhs);
  2164.       exp_init_os (exp->binary.rhs);
  2165.       break;
  2166.  
  2167.     case etree_trinary:
  2168.       exp_init_os (exp->trinary.cond);
  2169.       exp_init_os (exp->trinary.lhs);
  2170.       exp_init_os (exp->trinary.rhs);
  2171.       break;
  2172.  
  2173.     case etree_assert:
  2174.       exp_init_os (exp->assert_s.child);
  2175.       break;
  2176.  
  2177.     case etree_unary:
  2178.       exp_init_os (exp->unary.child);
  2179.       break;
  2180.  
  2181.     case etree_name:
  2182.       switch (exp->type.node_code)
  2183.         {
  2184.         case ADDR:
  2185.         case LOADADDR:
  2186.         case SIZEOF:
  2187.           {
  2188.             lang_output_section_statement_type *os;
  2189.  
  2190.             os = lang_output_section_find (exp->name.name);
  2191.             if (os != NULL && os->bfd_section == NULL)
  2192.               init_os (os, 0);
  2193.           }
  2194.         }
  2195.       break;
  2196.  
  2197.     default:
  2198.       break;
  2199.     }
  2200. }
  2201. static void
  2202. section_already_linked (bfd *abfd, asection *sec, void *data)
  2203. {
  2204.   lang_input_statement_type *entry = (lang_input_statement_type *) data;
  2205.  
  2206.   /* If we are only reading symbols from this object, then we want to
  2207.      discard all sections.  */
  2208.   if (entry->flags.just_syms)
  2209.     {
  2210.       bfd_link_just_syms (abfd, sec, &link_info);
  2211.       return;
  2212.     }
  2213.  
  2214.   if (!(abfd->flags & DYNAMIC))
  2215.     bfd_section_already_linked (abfd, sec, &link_info);
  2216. }
  2217. /* The wild routines.
  2218.  
  2219.    These expand statements like *(.text) and foo.o to a list of
  2220.    explicit actions, like foo.o(.text), bar.o(.text) and
  2221.    foo.o(.text, .data).  */
  2222.  
  2223. /* Add SECTION to the output section OUTPUT.  Do this by creating a
  2224.    lang_input_section statement which is placed at PTR.  */
  2225.  
  2226. void
  2227. lang_add_section (lang_statement_list_type *ptr,
  2228.                   asection *section,
  2229.                   struct flag_info *sflag_info,
  2230.                   lang_output_section_statement_type *output)
  2231. {
  2232.   flagword flags = section->flags;
  2233.  
  2234.   bfd_boolean discard;
  2235.   lang_input_section_type *new_section;
  2236.   bfd *abfd = link_info.output_bfd;
  2237.  
  2238.   /* Discard sections marked with SEC_EXCLUDE.  */
  2239.   discard = (flags & SEC_EXCLUDE) != 0;
  2240.  
  2241.   /* Discard input sections which are assigned to a section named
  2242.      DISCARD_SECTION_NAME.  */
  2243.   if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
  2244.     discard = TRUE;
  2245.  
  2246.   /* Discard debugging sections if we are stripping debugging
  2247.      information.  */
  2248.   if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
  2249.       && (flags & SEC_DEBUGGING) != 0)
  2250.     discard = TRUE;
  2251.  
  2252.   if (discard)
  2253.     {
  2254.       if (section->output_section == NULL)
  2255.         {
  2256.           /* This prevents future calls from assigning this section.  */
  2257.           section->output_section = bfd_abs_section_ptr;
  2258.         }
  2259.       return;
  2260.     }
  2261.  
  2262.   if (sflag_info)
  2263.     {
  2264.       bfd_boolean keep;
  2265.  
  2266.       keep = bfd_lookup_section_flags (&link_info, sflag_info, section);
  2267.       if (!keep)
  2268.         return;
  2269.     }
  2270.  
  2271.   if (section->output_section != NULL)
  2272.     return;
  2273.  
  2274.   /* We don't copy the SEC_NEVER_LOAD flag from an input section
  2275.      to an output section, because we want to be able to include a
  2276.      SEC_NEVER_LOAD section in the middle of an otherwise loaded
  2277.      section (I don't know why we want to do this, but we do).
  2278.      build_link_order in ldwrite.c handles this case by turning
  2279.      the embedded SEC_NEVER_LOAD section into a fill.  */
  2280.   flags &= ~ SEC_NEVER_LOAD;
  2281.  
  2282.   /* If final link, don't copy the SEC_LINK_ONCE flags, they've
  2283.      already been processed.  One reason to do this is that on pe
  2284.      format targets, .text$foo sections go into .text and it's odd
  2285.      to see .text with SEC_LINK_ONCE set.  */
  2286.  
  2287.   if (!link_info.relocatable)
  2288.     flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC);
  2289.  
  2290.   switch (output->sectype)
  2291.     {
  2292.     case normal_section:
  2293.     case overlay_section:
  2294.       break;
  2295.     case noalloc_section:
  2296.       flags &= ~SEC_ALLOC;
  2297.       break;
  2298.     case noload_section:
  2299.       flags &= ~SEC_LOAD;
  2300.       flags |= SEC_NEVER_LOAD;
  2301.       /* Unfortunately GNU ld has managed to evolve two different
  2302.          meanings to NOLOAD in scripts.  ELF gets a .bss style noload,
  2303.          alloc, no contents section.  All others get a noload, noalloc
  2304.          section.  */
  2305.       if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
  2306.         flags &= ~SEC_HAS_CONTENTS;
  2307.       else
  2308.         flags &= ~SEC_ALLOC;
  2309.       break;
  2310.     }
  2311.  
  2312.   if (output->bfd_section == NULL)
  2313.     init_os (output, flags);
  2314.  
  2315.   /* If SEC_READONLY is not set in the input section, then clear
  2316.      it from the output section.  */
  2317.   output->bfd_section->flags &= flags | ~SEC_READONLY;
  2318.  
  2319.   if (output->bfd_section->linker_has_input)
  2320.     {
  2321.       /* Only set SEC_READONLY flag on the first input section.  */
  2322.       flags &= ~ SEC_READONLY;
  2323.  
  2324.       /* Keep SEC_MERGE and SEC_STRINGS only if they are the same.  */
  2325.       if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
  2326.           != (flags & (SEC_MERGE | SEC_STRINGS))
  2327.           || ((flags & SEC_MERGE) != 0
  2328.               && output->bfd_section->entsize != section->entsize))
  2329.         {
  2330.           output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
  2331.           flags &= ~ (SEC_MERGE | SEC_STRINGS);
  2332.         }
  2333.     }
  2334.   output->bfd_section->flags |= flags;
  2335.  
  2336.   if (!output->bfd_section->linker_has_input)
  2337.     {
  2338.       output->bfd_section->linker_has_input = 1;
  2339.       /* This must happen after flags have been updated.  The output
  2340.          section may have been created before we saw its first input
  2341.          section, eg. for a data statement.  */
  2342.       bfd_init_private_section_data (section->owner, section,
  2343.                                      link_info.output_bfd,
  2344.                                      output->bfd_section,
  2345.                                      &link_info);
  2346.       if ((flags & SEC_MERGE) != 0)
  2347.         output->bfd_section->entsize = section->entsize;
  2348.     }
  2349.  
  2350.   if ((flags & SEC_TIC54X_BLOCK) != 0
  2351.       && bfd_get_arch (section->owner) == bfd_arch_tic54x)
  2352.     {
  2353.       /* FIXME: This value should really be obtained from the bfd...  */
  2354.       output->block_value = 128;
  2355.     }
  2356.  
  2357.   if (section->alignment_power > output->bfd_section->alignment_power)
  2358.     output->bfd_section->alignment_power = section->alignment_power;
  2359.  
  2360.   section->output_section = output->bfd_section;
  2361.  
  2362.   if (!link_info.relocatable
  2363.       && !stripped_excluded_sections)
  2364.     {
  2365.       asection *s = output->bfd_section->map_tail.s;
  2366.       output->bfd_section->map_tail.s = section;
  2367.       section->map_head.s = NULL;
  2368.       section->map_tail.s = s;
  2369.       if (s != NULL)
  2370.         s->map_head.s = section;
  2371.       else
  2372.         output->bfd_section->map_head.s = section;
  2373.     }
  2374.  
  2375.   /* Add a section reference to the list.  */
  2376.   new_section = new_stat (lang_input_section, ptr);
  2377.   new_section->section = section;
  2378. }
  2379.  
  2380. /* Handle wildcard sorting.  This returns the lang_input_section which
  2381.    should follow the one we are going to create for SECTION and FILE,
  2382.    based on the sorting requirements of WILD.  It returns NULL if the
  2383.    new section should just go at the end of the current list.  */
  2384.  
  2385. static lang_statement_union_type *
  2386. wild_sort (lang_wild_statement_type *wild,
  2387.            struct wildcard_list *sec,
  2388.            lang_input_statement_type *file,
  2389.            asection *section)
  2390. {
  2391.   lang_statement_union_type *l;
  2392.  
  2393.   if (!wild->filenames_sorted
  2394.       && (sec == NULL || sec->spec.sorted == none))
  2395.     return NULL;
  2396.  
  2397.   for (l = wild->children.head; l != NULL; l = l->header.next)
  2398.     {
  2399.       lang_input_section_type *ls;
  2400.  
  2401.       if (l->header.type != lang_input_section_enum)
  2402.         continue;
  2403.       ls = &l->input_section;
  2404.  
  2405.       /* Sorting by filename takes precedence over sorting by section
  2406.          name.  */
  2407.  
  2408.       if (wild->filenames_sorted)
  2409.         {
  2410.           const char *fn, *ln;
  2411.           bfd_boolean fa, la;
  2412.           int i;
  2413.  
  2414.           /* The PE support for the .idata section as generated by
  2415.              dlltool assumes that files will be sorted by the name of
  2416.              the archive and then the name of the file within the
  2417.              archive.  */
  2418.  
  2419.           if (file->the_bfd != NULL
  2420.               && bfd_my_archive (file->the_bfd) != NULL)
  2421.             {
  2422.               fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
  2423.               fa = TRUE;
  2424.             }
  2425.           else
  2426.             {
  2427.               fn = file->filename;
  2428.               fa = FALSE;
  2429.             }
  2430.  
  2431.           if (bfd_my_archive (ls->section->owner) != NULL)
  2432.             {
  2433.               ln = bfd_get_filename (bfd_my_archive (ls->section->owner));
  2434.               la = TRUE;
  2435.             }
  2436.           else
  2437.             {
  2438.               ln = ls->section->owner->filename;
  2439.               la = FALSE;
  2440.             }
  2441.  
  2442.           i = filename_cmp (fn, ln);
  2443.           if (i > 0)
  2444.             continue;
  2445.           else if (i < 0)
  2446.             break;
  2447.  
  2448.           if (fa || la)
  2449.             {
  2450.               if (fa)
  2451.                 fn = file->filename;
  2452.               if (la)
  2453.                 ln = ls->section->owner->filename;
  2454.  
  2455.               i = filename_cmp (fn, ln);
  2456.               if (i > 0)
  2457.                 continue;
  2458.               else if (i < 0)
  2459.                 break;
  2460.             }
  2461.         }
  2462.  
  2463.       /* Here either the files are not sorted by name, or we are
  2464.          looking at the sections for this file.  */
  2465.  
  2466.       if (sec != NULL
  2467.           && sec->spec.sorted != none
  2468.           && sec->spec.sorted != by_none)
  2469.         if (compare_section (sec->spec.sorted, section, ls->section) < 0)
  2470.           break;
  2471.     }
  2472.  
  2473.   return l;
  2474. }
  2475.  
  2476. /* Expand a wild statement for a particular FILE.  SECTION may be
  2477.    NULL, in which case it is a wild card.  */
  2478.  
  2479. static void
  2480. output_section_callback (lang_wild_statement_type *ptr,
  2481.                          struct wildcard_list *sec,
  2482.                          asection *section,
  2483.                          struct flag_info *sflag_info,
  2484.                          lang_input_statement_type *file,
  2485.                          void *output)
  2486. {
  2487.   lang_statement_union_type *before;
  2488.   lang_output_section_statement_type *os;
  2489.  
  2490.   os = (lang_output_section_statement_type *) output;
  2491.  
  2492.   /* Exclude sections that match UNIQUE_SECTION_LIST.  */
  2493.   if (unique_section_p (section, os))
  2494.     return;
  2495.  
  2496.   before = wild_sort (ptr, sec, file, section);
  2497.  
  2498.   /* Here BEFORE points to the lang_input_section which
  2499.      should follow the one we are about to add.  If BEFORE
  2500.      is NULL, then the section should just go at the end
  2501.      of the current list.  */
  2502.  
  2503.   if (before == NULL)
  2504.     lang_add_section (&ptr->children, section, sflag_info, os);
  2505.   else
  2506.     {
  2507.       lang_statement_list_type list;
  2508.       lang_statement_union_type **pp;
  2509.  
  2510.       lang_list_init (&list);
  2511.       lang_add_section (&list, section, sflag_info, os);
  2512.  
  2513.       /* If we are discarding the section, LIST.HEAD will
  2514.          be NULL.  */
  2515.       if (list.head != NULL)
  2516.         {
  2517.           ASSERT (list.head->header.next == NULL);
  2518.  
  2519.           for (pp = &ptr->children.head;
  2520.                *pp != before;
  2521.                pp = &(*pp)->header.next)
  2522.             ASSERT (*pp != NULL);
  2523.  
  2524.           list.head->header.next = *pp;
  2525.           *pp = list.head;
  2526.         }
  2527.     }
  2528. }
  2529.  
  2530. /* Check if all sections in a wild statement for a particular FILE
  2531.    are readonly.  */
  2532.  
  2533. static void
  2534. check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
  2535.                         struct wildcard_list *sec ATTRIBUTE_UNUSED,
  2536.                         asection *section,
  2537.                         struct flag_info *sflag_info ATTRIBUTE_UNUSED,
  2538.                         lang_input_statement_type *file ATTRIBUTE_UNUSED,
  2539.                         void *output)
  2540. {
  2541.   lang_output_section_statement_type *os;
  2542.  
  2543.   os = (lang_output_section_statement_type *) output;
  2544.  
  2545.   /* Exclude sections that match UNIQUE_SECTION_LIST.  */
  2546.   if (unique_section_p (section, os))
  2547.     return;
  2548.  
  2549.   if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
  2550.     os->all_input_readonly = FALSE;
  2551. }
  2552.  
  2553. /* This is passed a file name which must have been seen already and
  2554.    added to the statement tree.  We will see if it has been opened
  2555.    already and had its symbols read.  If not then we'll read it.  */
  2556.  
  2557. static lang_input_statement_type *
  2558. lookup_name (const char *name)
  2559. {
  2560.   lang_input_statement_type *search;
  2561.  
  2562.   for (search = (lang_input_statement_type *) input_file_chain.head;
  2563.        search != NULL;
  2564.        search = (lang_input_statement_type *) search->next_real_file)
  2565.     {
  2566.       /* Use the local_sym_name as the name of the file that has
  2567.          already been loaded as filename might have been transformed
  2568.          via the search directory lookup mechanism.  */
  2569.       const char *filename = search->local_sym_name;
  2570.  
  2571.       if (filename != NULL
  2572.           && filename_cmp (filename, name) == 0)
  2573.         break;
  2574.     }
  2575.  
  2576.   if (search == NULL)
  2577.     search = new_afile (name, lang_input_file_is_search_file_enum,
  2578.                         default_target, FALSE);
  2579.  
  2580.   /* If we have already added this file, or this file is not real
  2581.      don't add this file.  */
  2582.   if (search->flags.loaded || !search->flags.real)
  2583.     return search;
  2584.  
  2585.   if (! load_symbols (search, NULL))
  2586.     return NULL;
  2587.  
  2588.   return search;
  2589. }
  2590.  
  2591. /* Save LIST as a list of libraries whose symbols should not be exported.  */
  2592.  
  2593. struct excluded_lib
  2594. {
  2595.   char *name;
  2596.   struct excluded_lib *next;
  2597. };
  2598. static struct excluded_lib *excluded_libs;
  2599.  
  2600. void
  2601. add_excluded_libs (const char *list)
  2602. {
  2603.   const char *p = list, *end;
  2604.  
  2605.   while (*p != '\0')
  2606.     {
  2607.       struct excluded_lib *entry;
  2608.       end = strpbrk (p, ",:");
  2609.       if (end == NULL)
  2610.         end = p + strlen (p);
  2611.       entry = (struct excluded_lib *) xmalloc (sizeof (*entry));
  2612.       entry->next = excluded_libs;
  2613.       entry->name = (char *) xmalloc (end - p + 1);
  2614.       memcpy (entry->name, p, end - p);
  2615.       entry->name[end - p] = '\0';
  2616.       excluded_libs = entry;
  2617.       if (*end == '\0')
  2618.         break;
  2619.       p = end + 1;
  2620.     }
  2621. }
  2622.  
  2623. static void
  2624. check_excluded_libs (bfd *abfd)
  2625. {
  2626.   struct excluded_lib *lib = excluded_libs;
  2627.  
  2628.   while (lib)
  2629.     {
  2630.       int len = strlen (lib->name);
  2631.       const char *filename = lbasename (abfd->filename);
  2632.  
  2633.       if (strcmp (lib->name, "ALL") == 0)
  2634.         {
  2635.           abfd->no_export = TRUE;
  2636.           return;
  2637.         }
  2638.  
  2639.       if (filename_ncmp (lib->name, filename, len) == 0
  2640.           && (filename[len] == '\0'
  2641.               || (filename[len] == '.' && filename[len + 1] == 'a'
  2642.                   && filename[len + 2] == '\0')))
  2643.         {
  2644.           abfd->no_export = TRUE;
  2645.           return;
  2646.         }
  2647.  
  2648.       lib = lib->next;
  2649.     }
  2650. }
  2651.  
  2652. /* Get the symbols for an input file.  */
  2653.  
  2654. bfd_boolean
  2655. load_symbols (lang_input_statement_type *entry,
  2656.               lang_statement_list_type *place)
  2657. {
  2658.   char **matching;
  2659.  
  2660.   if (entry->flags.loaded)
  2661.     return TRUE;
  2662.  
  2663.   ldfile_open_file (entry);
  2664.  
  2665.   /* Do not process further if the file was missing.  */
  2666.   if (entry->flags.missing_file)
  2667.     return TRUE;
  2668.  
  2669.   if (! bfd_check_format (entry->the_bfd, bfd_archive)
  2670.       && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
  2671.     {
  2672.       bfd_error_type err;
  2673.       struct lang_input_statement_flags save_flags;
  2674.       extern FILE *yyin;
  2675.  
  2676.       err = bfd_get_error ();
  2677.  
  2678.       /* See if the emulation has some special knowledge.  */
  2679.       if (ldemul_unrecognized_file (entry))
  2680.         return TRUE;
  2681.  
  2682.       if (err == bfd_error_file_ambiguously_recognized)
  2683.         {
  2684.           char **p;
  2685.  
  2686.           einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
  2687.           einfo (_("%B: matching formats:"), entry->the_bfd);
  2688.           for (p = matching; *p != NULL; p++)
  2689.             einfo (" %s", *p);
  2690.           einfo ("%F\n");
  2691.         }
  2692.       else if (err != bfd_error_file_not_recognized
  2693.                || place == NULL)
  2694.         einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
  2695.  
  2696.       bfd_close (entry->the_bfd);
  2697.       entry->the_bfd = NULL;
  2698.  
  2699.       /* Try to interpret the file as a linker script.  */
  2700.       save_flags = input_flags;
  2701.       ldfile_open_command_file (entry->filename);
  2702.  
  2703.       push_stat_ptr (place);
  2704.       input_flags.add_DT_NEEDED_for_regular
  2705.         = entry->flags.add_DT_NEEDED_for_regular;
  2706.       input_flags.add_DT_NEEDED_for_dynamic
  2707.         = entry->flags.add_DT_NEEDED_for_dynamic;
  2708.       input_flags.whole_archive = entry->flags.whole_archive;
  2709.       input_flags.dynamic = entry->flags.dynamic;
  2710.  
  2711.       ldfile_assumed_script = TRUE;
  2712.       parser_input = input_script;
  2713.       yyparse ();
  2714.       ldfile_assumed_script = FALSE;
  2715.  
  2716.       /* missing_file is sticky.  sysrooted will already have been
  2717.          restored when seeing EOF in yyparse, but no harm to restore
  2718.          again.  */
  2719.       save_flags.missing_file |= input_flags.missing_file;
  2720.       input_flags = save_flags;
  2721.       pop_stat_ptr ();
  2722.       fclose (yyin);
  2723.       yyin = NULL;
  2724.       entry->flags.loaded = TRUE;
  2725.  
  2726.       return TRUE;
  2727.     }
  2728.  
  2729.   if (ldemul_recognized_file (entry))
  2730.     return TRUE;
  2731.  
  2732.   /* We don't call ldlang_add_file for an archive.  Instead, the
  2733.      add_symbols entry point will call ldlang_add_file, via the
  2734.      add_archive_element callback, for each element of the archive
  2735.      which is used.  */
  2736.   switch (bfd_get_format (entry->the_bfd))
  2737.     {
  2738.     default:
  2739.       break;
  2740.  
  2741.     case bfd_object:
  2742. #ifdef ENABLE_PLUGINS
  2743.       if (!entry->flags.reload)
  2744. #endif
  2745.         ldlang_add_file (entry);
  2746.       if (trace_files || verbose)
  2747.         info_msg ("%I\n", entry);
  2748.       break;
  2749.  
  2750.     case bfd_archive:
  2751.       check_excluded_libs (entry->the_bfd);
  2752.  
  2753.       if (entry->flags.whole_archive)
  2754.         {
  2755.           bfd *member = NULL;
  2756.           bfd_boolean loaded = TRUE;
  2757.  
  2758.           for (;;)
  2759.             {
  2760.               bfd *subsbfd;
  2761.               member = bfd_openr_next_archived_file (entry->the_bfd, member);
  2762.  
  2763.               if (member == NULL)
  2764.                 break;
  2765.  
  2766.               if (! bfd_check_format (member, bfd_object))
  2767.                 {
  2768.                   einfo (_("%F%B: member %B in archive is not an object\n"),
  2769.                          entry->the_bfd, member);
  2770.                   loaded = FALSE;
  2771.                 }
  2772.  
  2773.               subsbfd = member;
  2774.               if (!(*link_info.callbacks
  2775.                     ->add_archive_element) (&link_info, member,
  2776.                                             "--whole-archive", &subsbfd))
  2777.                 abort ();
  2778.  
  2779.               /* Potentially, the add_archive_element hook may have set a
  2780.                  substitute BFD for us.  */
  2781.               if (!bfd_link_add_symbols (subsbfd, &link_info))
  2782.                 {
  2783.                   einfo (_("%F%B: error adding symbols: %E\n"), member);
  2784.                   loaded = FALSE;
  2785.                 }
  2786.             }
  2787.  
  2788.           entry->flags.loaded = loaded;
  2789.           return loaded;
  2790.         }
  2791.       break;
  2792.     }
  2793.  
  2794.   if (bfd_link_add_symbols (entry->the_bfd, &link_info))
  2795.     entry->flags.loaded = TRUE;
  2796.   else
  2797.     einfo (_("%F%B: error adding symbols: %E\n"), entry->the_bfd);
  2798.  
  2799.   return entry->flags.loaded;
  2800. }
  2801.  
  2802. /* Handle a wild statement.  S->FILENAME or S->SECTION_LIST or both
  2803.    may be NULL, indicating that it is a wildcard.  Separate
  2804.    lang_input_section statements are created for each part of the
  2805.    expansion; they are added after the wild statement S.  OUTPUT is
  2806.    the output section.  */
  2807.  
  2808. static void
  2809. wild (lang_wild_statement_type *s,
  2810.       const char *target ATTRIBUTE_UNUSED,
  2811.       lang_output_section_statement_type *output)
  2812. {
  2813.   struct wildcard_list *sec;
  2814.  
  2815.   if (s->handler_data[0]
  2816.       && s->handler_data[0]->spec.sorted == by_name
  2817.       && !s->filenames_sorted)
  2818.     {
  2819.       lang_section_bst_type *tree;
  2820.  
  2821.       walk_wild (s, output_section_callback_fast, output);
  2822.  
  2823.       tree = s->tree;
  2824.       if (tree)
  2825.         {
  2826.           output_section_callback_tree_to_list (s, tree, output);
  2827.           s->tree = NULL;
  2828.         }
  2829.     }
  2830.   else
  2831.     walk_wild (s, output_section_callback, output);
  2832.  
  2833.   if (default_common_section == NULL)
  2834.     for (sec = s->section_list; sec != NULL; sec = sec->next)
  2835.       if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
  2836.         {
  2837.           /* Remember the section that common is going to in case we
  2838.              later get something which doesn't know where to put it.  */
  2839.           default_common_section = output;
  2840.           break;
  2841.         }
  2842. }
  2843.  
  2844. /* Return TRUE iff target is the sought target.  */
  2845.  
  2846. static int
  2847. get_target (const bfd_target *target, void *data)
  2848. {
  2849.   const char *sought = (const char *) data;
  2850.  
  2851.   return strcmp (target->name, sought) == 0;
  2852. }
  2853.  
  2854. /* Like strcpy() but convert to lower case as well.  */
  2855.  
  2856. static void
  2857. stricpy (char *dest, char *src)
  2858. {
  2859.   char c;
  2860.  
  2861.   while ((c = *src++) != 0)
  2862.     *dest++ = TOLOWER (c);
  2863.  
  2864.   *dest = 0;
  2865. }
  2866.  
  2867. /* Remove the first occurrence of needle (if any) in haystack
  2868.    from haystack.  */
  2869.  
  2870. static void
  2871. strcut (char *haystack, char *needle)
  2872. {
  2873.   haystack = strstr (haystack, needle);
  2874.  
  2875.   if (haystack)
  2876.     {
  2877.       char *src;
  2878.  
  2879.       for (src = haystack + strlen (needle); *src;)
  2880.         *haystack++ = *src++;
  2881.  
  2882.       *haystack = 0;
  2883.     }
  2884. }
  2885.  
  2886. /* Compare two target format name strings.
  2887.    Return a value indicating how "similar" they are.  */
  2888.  
  2889. static int
  2890. name_compare (char *first, char *second)
  2891. {
  2892.   char *copy1;
  2893.   char *copy2;
  2894.   int result;
  2895.  
  2896.   copy1 = (char *) xmalloc (strlen (first) + 1);
  2897.   copy2 = (char *) xmalloc (strlen (second) + 1);
  2898.  
  2899.   /* Convert the names to lower case.  */
  2900.   stricpy (copy1, first);
  2901.   stricpy (copy2, second);
  2902.  
  2903.   /* Remove size and endian strings from the name.  */
  2904.   strcut (copy1, "big");
  2905.   strcut (copy1, "little");
  2906.   strcut (copy2, "big");
  2907.   strcut (copy2, "little");
  2908.  
  2909.   /* Return a value based on how many characters match,
  2910.      starting from the beginning.   If both strings are
  2911.      the same then return 10 * their length.  */
  2912.   for (result = 0; copy1[result] == copy2[result]; result++)
  2913.     if (copy1[result] == 0)
  2914.       {
  2915.         result *= 10;
  2916.         break;
  2917.       }
  2918.  
  2919.   free (copy1);
  2920.   free (copy2);
  2921.  
  2922.   return result;
  2923. }
  2924.  
  2925. /* Set by closest_target_match() below.  */
  2926. static const bfd_target *winner;
  2927.  
  2928. /* Scan all the valid bfd targets looking for one that has the endianness
  2929.    requirement that was specified on the command line, and is the nearest
  2930.    match to the original output target.  */
  2931.  
  2932. static int
  2933. closest_target_match (const bfd_target *target, void *data)
  2934. {
  2935.   const bfd_target *original = (const bfd_target *) data;
  2936.  
  2937.   if (command_line.endian == ENDIAN_BIG
  2938.       && target->byteorder != BFD_ENDIAN_BIG)
  2939.     return 0;
  2940.  
  2941.   if (command_line.endian == ENDIAN_LITTLE
  2942.       && target->byteorder != BFD_ENDIAN_LITTLE)
  2943.     return 0;
  2944.  
  2945.   /* Must be the same flavour.  */
  2946.   if (target->flavour != original->flavour)
  2947.     return 0;
  2948.  
  2949.   /* Ignore generic big and little endian elf vectors.  */
  2950.   if (strcmp (target->name, "elf32-big") == 0
  2951.       || strcmp (target->name, "elf64-big") == 0
  2952.       || strcmp (target->name, "elf32-little") == 0
  2953.       || strcmp (target->name, "elf64-little") == 0)
  2954.     return 0;
  2955.  
  2956.   /* If we have not found a potential winner yet, then record this one.  */
  2957.   if (winner == NULL)
  2958.     {
  2959.       winner = target;
  2960.       return 0;
  2961.     }
  2962.  
  2963.   /* Oh dear, we now have two potential candidates for a successful match.
  2964.      Compare their names and choose the better one.  */
  2965.   if (name_compare (target->name, original->name)
  2966.       > name_compare (winner->name, original->name))
  2967.     winner = target;
  2968.  
  2969.   /* Keep on searching until wqe have checked them all.  */
  2970.   return 0;
  2971. }
  2972.  
  2973. /* Return the BFD target format of the first input file.  */
  2974.  
  2975. static char *
  2976. get_first_input_target (void)
  2977. {
  2978.   char *target = NULL;
  2979.  
  2980.   LANG_FOR_EACH_INPUT_STATEMENT (s)
  2981.     {
  2982.       if (s->header.type == lang_input_statement_enum
  2983.           && s->flags.real)
  2984.         {
  2985.           ldfile_open_file (s);
  2986.  
  2987.           if (s->the_bfd != NULL
  2988.               && bfd_check_format (s->the_bfd, bfd_object))
  2989.             {
  2990.               target = bfd_get_target (s->the_bfd);
  2991.  
  2992.               if (target != NULL)
  2993.                 break;
  2994.             }
  2995.         }
  2996.     }
  2997.  
  2998.   return target;
  2999. }
  3000.  
  3001. const char *
  3002. lang_get_output_target (void)
  3003. {
  3004.   const char *target;
  3005.  
  3006.   /* Has the user told us which output format to use?  */
  3007.   if (output_target != NULL)
  3008.     return output_target;
  3009.  
  3010.   /* No - has the current target been set to something other than
  3011.      the default?  */
  3012.   if (current_target != default_target && current_target != NULL)
  3013.     return current_target;
  3014.  
  3015.   /* No - can we determine the format of the first input file?  */
  3016.   target = get_first_input_target ();
  3017.   if (target != NULL)
  3018.     return target;
  3019.  
  3020.   /* Failed - use the default output target.  */
  3021.   return default_target;
  3022. }
  3023.  
  3024. /* Open the output file.  */
  3025.  
  3026. static void
  3027. open_output (const char *name)
  3028. {
  3029.   output_target = lang_get_output_target ();
  3030.  
  3031.   /* Has the user requested a particular endianness on the command
  3032.      line?  */
  3033.   if (command_line.endian != ENDIAN_UNSET)
  3034.     {
  3035.       const bfd_target *target;
  3036.       enum bfd_endian desired_endian;
  3037.  
  3038.       /* Get the chosen target.  */
  3039.       target = bfd_search_for_target (get_target, (void *) output_target);
  3040.  
  3041.       /* If the target is not supported, we cannot do anything.  */
  3042.       if (target != NULL)
  3043.         {
  3044.           if (command_line.endian == ENDIAN_BIG)
  3045.             desired_endian = BFD_ENDIAN_BIG;
  3046.           else
  3047.             desired_endian = BFD_ENDIAN_LITTLE;
  3048.  
  3049.           /* See if the target has the wrong endianness.  This should
  3050.              not happen if the linker script has provided big and
  3051.              little endian alternatives, but some scrips don't do
  3052.              this.  */
  3053.           if (target->byteorder != desired_endian)
  3054.             {
  3055.               /* If it does, then see if the target provides
  3056.                  an alternative with the correct endianness.  */
  3057.               if (target->alternative_target != NULL
  3058.                   && (target->alternative_target->byteorder == desired_endian))
  3059.                 output_target = target->alternative_target->name;
  3060.               else
  3061.                 {
  3062.                   /* Try to find a target as similar as possible to
  3063.                      the default target, but which has the desired
  3064.                      endian characteristic.  */
  3065.                   bfd_search_for_target (closest_target_match,
  3066.                                          (void *) target);
  3067.  
  3068.                   /* Oh dear - we could not find any targets that
  3069.                      satisfy our requirements.  */
  3070.                   if (winner == NULL)
  3071.                     einfo (_("%P: warning: could not find any targets"
  3072.                              " that match endianness requirement\n"));
  3073.                   else
  3074.                     output_target = winner->name;
  3075.                 }
  3076.             }
  3077.         }
  3078.     }
  3079.  
  3080.   link_info.output_bfd = bfd_openw (name, output_target);
  3081.  
  3082.   if (link_info.output_bfd == NULL)
  3083.     {
  3084.       if (bfd_get_error () == bfd_error_invalid_target)
  3085.         einfo (_("%P%F: target %s not found\n"), output_target);
  3086.  
  3087.       einfo (_("%P%F: cannot open output file %s: %E\n"), name);
  3088.     }
  3089.  
  3090.   delete_output_file_on_failure = TRUE;
  3091.  
  3092.   if (! bfd_set_format (link_info.output_bfd, bfd_object))
  3093.     einfo (_("%P%F:%s: can not make object file: %E\n"), name);
  3094.   if (! bfd_set_arch_mach (link_info.output_bfd,
  3095.                            ldfile_output_architecture,
  3096.                            ldfile_output_machine))
  3097.     einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
  3098.  
  3099.   link_info.hash = bfd_link_hash_table_create (link_info.output_bfd);
  3100.   if (link_info.hash == NULL)
  3101.     einfo (_("%P%F: can not create hash table: %E\n"));
  3102.  
  3103.   bfd_set_gp_size (link_info.output_bfd, g_switch_value);
  3104. }
  3105.  
  3106. static void
  3107. ldlang_open_output (lang_statement_union_type *statement)
  3108. {
  3109.   switch (statement->header.type)
  3110.     {
  3111.     case lang_output_statement_enum:
  3112.       ASSERT (link_info.output_bfd == NULL);
  3113.       open_output (statement->output_statement.name);
  3114.       ldemul_set_output_arch ();
  3115.       if (config.magic_demand_paged && !link_info.relocatable)
  3116.         link_info.output_bfd->flags |= D_PAGED;
  3117.       else
  3118.         link_info.output_bfd->flags &= ~D_PAGED;
  3119.       if (config.text_read_only)
  3120.         link_info.output_bfd->flags |= WP_TEXT;
  3121.       else
  3122.         link_info.output_bfd->flags &= ~WP_TEXT;
  3123.       if (link_info.traditional_format)
  3124.         link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
  3125.       else
  3126.         link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
  3127.       break;
  3128.  
  3129.     case lang_target_statement_enum:
  3130.       current_target = statement->target_statement.target;
  3131.       break;
  3132.     default:
  3133.       break;
  3134.     }
  3135. }
  3136.  
  3137. /* Convert between addresses in bytes and sizes in octets.
  3138.    For currently supported targets, octets_per_byte is always a power
  3139.    of two, so we can use shifts.  */
  3140. #define TO_ADDR(X) ((X) >> opb_shift)
  3141. #define TO_SIZE(X) ((X) << opb_shift)
  3142.  
  3143. /* Support the above.  */
  3144. static unsigned int opb_shift = 0;
  3145.  
  3146. static void
  3147. init_opb (void)
  3148. {
  3149.   unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
  3150.                                               ldfile_output_machine);
  3151.   opb_shift = 0;
  3152.   if (x > 1)
  3153.     while ((x & 1) == 0)
  3154.       {
  3155.         x >>= 1;
  3156.         ++opb_shift;
  3157.       }
  3158.   ASSERT (x == 1);
  3159. }
  3160.  
  3161. /* Open all the input files.  */
  3162.  
  3163. enum open_bfd_mode
  3164.   {
  3165.     OPEN_BFD_NORMAL = 0,
  3166.     OPEN_BFD_FORCE = 1,
  3167.     OPEN_BFD_RESCAN = 2
  3168.   };
  3169. #ifdef ENABLE_PLUGINS
  3170. static lang_input_statement_type *plugin_insert = NULL;
  3171. #endif
  3172.  
  3173. static void
  3174. open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode)
  3175. {
  3176.   for (; s != NULL; s = s->header.next)
  3177.     {
  3178.       switch (s->header.type)
  3179.         {
  3180.         case lang_constructors_statement_enum:
  3181.           open_input_bfds (constructor_list.head, mode);
  3182.           break;
  3183.         case lang_output_section_statement_enum:
  3184.           open_input_bfds (s->output_section_statement.children.head, mode);
  3185.           break;
  3186.         case lang_wild_statement_enum:
  3187.           /* Maybe we should load the file's symbols.  */
  3188.           if ((mode & OPEN_BFD_RESCAN) == 0
  3189.               && s->wild_statement.filename
  3190.               && !wildcardp (s->wild_statement.filename)
  3191.               && !archive_path (s->wild_statement.filename))
  3192.             lookup_name (s->wild_statement.filename);
  3193.           open_input_bfds (s->wild_statement.children.head, mode);
  3194.           break;
  3195.         case lang_group_statement_enum:
  3196.           {
  3197.             struct bfd_link_hash_entry *undefs;
  3198.  
  3199.             /* We must continually search the entries in the group
  3200.                until no new symbols are added to the list of undefined
  3201.                symbols.  */
  3202.  
  3203.             do
  3204.               {
  3205.                 undefs = link_info.hash->undefs_tail;
  3206.                 open_input_bfds (s->group_statement.children.head,
  3207.                                  mode | OPEN_BFD_FORCE);
  3208.               }
  3209.             while (undefs != link_info.hash->undefs_tail);
  3210.           }
  3211.           break;
  3212.         case lang_target_statement_enum:
  3213.           current_target = s->target_statement.target;
  3214.           break;
  3215.         case lang_input_statement_enum:
  3216.           if (s->input_statement.flags.real)
  3217.             {
  3218.               lang_statement_union_type **os_tail;
  3219.               lang_statement_list_type add;
  3220.  
  3221.               s->input_statement.target = current_target;
  3222.  
  3223.               /* If we are being called from within a group, and this
  3224.                  is an archive which has already been searched, then
  3225.                  force it to be researched unless the whole archive
  3226.                  has been loaded already.  Do the same for a rescan.  */
  3227.               if (mode != OPEN_BFD_NORMAL
  3228. #ifdef ENABLE_PLUGINS
  3229.                   && ((mode & OPEN_BFD_RESCAN) == 0
  3230.                       || plugin_insert == NULL)
  3231. #endif
  3232.                   && !s->input_statement.flags.whole_archive
  3233.                   && s->input_statement.flags.loaded
  3234.                   && s->input_statement.the_bfd != NULL
  3235.                   && bfd_check_format (s->input_statement.the_bfd,
  3236.                                        bfd_archive))
  3237.                 s->input_statement.flags.loaded = FALSE;
  3238. #ifdef ENABLE_PLUGINS
  3239.               /* When rescanning, reload --as-needed shared libs.  */
  3240.               else if ((mode & OPEN_BFD_RESCAN) != 0
  3241.                        && plugin_insert == NULL
  3242.                        && s->input_statement.flags.loaded
  3243.                        && s->input_statement.flags.add_DT_NEEDED_for_regular
  3244.                        && s->input_statement.the_bfd != NULL
  3245.                        && ((s->input_statement.the_bfd->flags) & DYNAMIC) != 0
  3246.                        && plugin_should_reload (s->input_statement.the_bfd))
  3247.                 {
  3248.                   s->input_statement.flags.loaded = FALSE;
  3249.                   s->input_statement.flags.reload = TRUE;
  3250.                 }
  3251. #endif
  3252.  
  3253.               os_tail = lang_output_section_statement.tail;
  3254.               lang_list_init (&add);
  3255.  
  3256.               if (! load_symbols (&s->input_statement, &add))
  3257.                 config.make_executable = FALSE;
  3258.  
  3259.               if (add.head != NULL)
  3260.                 {
  3261.                   /* If this was a script with output sections then
  3262.                      tack any added statements on to the end of the
  3263.                      list.  This avoids having to reorder the output
  3264.                      section statement list.  Very likely the user
  3265.                      forgot -T, and whatever we do here will not meet
  3266.                      naive user expectations.  */
  3267.                   if (os_tail != lang_output_section_statement.tail)
  3268.                     {
  3269.                       einfo (_("%P: warning: %s contains output sections;"
  3270.                                " did you forget -T?\n"),
  3271.                              s->input_statement.filename);
  3272.                       *stat_ptr->tail = add.head;
  3273.                       stat_ptr->tail = add.tail;
  3274.                     }
  3275.                   else
  3276.                     {
  3277.                       *add.tail = s->header.next;
  3278.                       s->header.next = add.head;
  3279.                     }
  3280.                 }
  3281.             }
  3282. #ifdef ENABLE_PLUGINS
  3283.           /* If we have found the point at which a plugin added new
  3284.              files, clear plugin_insert to enable archive rescan.  */
  3285.           if (&s->input_statement == plugin_insert)
  3286.             plugin_insert = NULL;
  3287. #endif
  3288.           break;
  3289.         case lang_assignment_statement_enum:
  3290.           if (s->assignment_statement.exp->assign.defsym)
  3291.             /* This is from a --defsym on the command line.  */
  3292.             exp_fold_tree_no_dot (s->assignment_statement.exp);
  3293.           break;
  3294.         default:
  3295.           break;
  3296.         }
  3297.     }
  3298.  
  3299.   /* Exit if any of the files were missing.  */
  3300.   if (input_flags.missing_file)
  3301.     einfo ("%F");
  3302. }
  3303.  
  3304. /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions.  */
  3305.  
  3306. void
  3307. lang_track_definedness (const char *name)
  3308. {
  3309.   if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
  3310.     einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
  3311. }
  3312.  
  3313. /* New-function for the definedness hash table.  */
  3314.  
  3315. static struct bfd_hash_entry *
  3316. lang_definedness_newfunc (struct bfd_hash_entry *entry,
  3317.                           struct bfd_hash_table *table ATTRIBUTE_UNUSED,
  3318.                           const char *name ATTRIBUTE_UNUSED)
  3319. {
  3320.   struct lang_definedness_hash_entry *ret
  3321.     = (struct lang_definedness_hash_entry *) entry;
  3322.  
  3323.   if (ret == NULL)
  3324.     ret = (struct lang_definedness_hash_entry *)
  3325.       bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
  3326.  
  3327.   if (ret == NULL)
  3328.     einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
  3329.  
  3330.   ret->iteration = -1;
  3331.   return &ret->root;
  3332. }
  3333.  
  3334. /* Return the iteration when the definition of NAME was last updated.  A
  3335.    value of -1 means that the symbol is not defined in the linker script
  3336.    or the command line, but may be defined in the linker symbol table.  */
  3337.  
  3338. int
  3339. lang_symbol_definition_iteration (const char *name)
  3340. {
  3341.   struct lang_definedness_hash_entry *defentry
  3342.     = (struct lang_definedness_hash_entry *)
  3343.     bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
  3344.  
  3345.   /* We've already created this one on the presence of DEFINED in the
  3346.      script, so it can't be NULL unless something is borked elsewhere in
  3347.      the code.  */
  3348.   if (defentry == NULL)
  3349.     FAIL ();
  3350.  
  3351.   return defentry->iteration;
  3352. }
  3353.  
  3354. /* Update the definedness state of NAME.  */
  3355.  
  3356. void
  3357. lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
  3358. {
  3359.   struct lang_definedness_hash_entry *defentry
  3360.     = (struct lang_definedness_hash_entry *)
  3361.     bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
  3362.  
  3363.   /* We don't keep track of symbols not tested with DEFINED.  */
  3364.   if (defentry == NULL)
  3365.     return;
  3366.  
  3367.   /* If the symbol was already defined, and not from an earlier statement
  3368.      iteration, don't update the definedness iteration, because that'd
  3369.      make the symbol seem defined in the linker script at this point, and
  3370.      it wasn't; it was defined in some object.  If we do anyway, DEFINED
  3371.      would start to yield false before this point and the construct "sym =
  3372.      DEFINED (sym) ? sym : X;" would change sym to X despite being defined
  3373.      in an object.  */
  3374.   if (h->type != bfd_link_hash_undefined
  3375.       && h->type != bfd_link_hash_common
  3376.       && h->type != bfd_link_hash_new
  3377.       && defentry->iteration == -1)
  3378.     return;
  3379.  
  3380.   defentry->iteration = lang_statement_iteration;
  3381. }
  3382.  
  3383. /* Add the supplied name to the symbol table as an undefined reference.
  3384.    This is a two step process as the symbol table doesn't even exist at
  3385.    the time the ld command line is processed.  First we put the name
  3386.    on a list, then, once the output file has been opened, transfer the
  3387.    name to the symbol table.  */
  3388.  
  3389. typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
  3390.  
  3391. #define ldlang_undef_chain_list_head entry_symbol.next
  3392.  
  3393. void
  3394. ldlang_add_undef (const char *const name, bfd_boolean cmdline)
  3395. {
  3396.   ldlang_undef_chain_list_type *new_undef;
  3397.  
  3398.   undef_from_cmdline = undef_from_cmdline || cmdline;
  3399.   new_undef = (ldlang_undef_chain_list_type *) stat_alloc (sizeof (*new_undef));
  3400.   new_undef->next = ldlang_undef_chain_list_head;
  3401.   ldlang_undef_chain_list_head = new_undef;
  3402.  
  3403.   new_undef->name = xstrdup (name);
  3404.  
  3405.   if (link_info.output_bfd != NULL)
  3406.     insert_undefined (new_undef->name);
  3407. }
  3408.  
  3409. /* Insert NAME as undefined in the symbol table.  */
  3410.  
  3411. static void
  3412. insert_undefined (const char *name)
  3413. {
  3414.   struct bfd_link_hash_entry *h;
  3415.  
  3416.   h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
  3417.   if (h == NULL)
  3418.     einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
  3419.   if (h->type == bfd_link_hash_new)
  3420.     {
  3421.       h->type = bfd_link_hash_undefined;
  3422.       h->u.undef.abfd = NULL;
  3423.       bfd_link_add_undef (link_info.hash, h);
  3424.     }
  3425. }
  3426.  
  3427. /* Run through the list of undefineds created above and place them
  3428.    into the linker hash table as undefined symbols belonging to the
  3429.    script file.  */
  3430.  
  3431. static void
  3432. lang_place_undefineds (void)
  3433. {
  3434.   ldlang_undef_chain_list_type *ptr;
  3435.  
  3436.   for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
  3437.     insert_undefined (ptr->name);
  3438. }
  3439.  
  3440. /* Check for all readonly or some readwrite sections.  */
  3441.  
  3442. static void
  3443. check_input_sections
  3444.   (lang_statement_union_type *s,
  3445.    lang_output_section_statement_type *output_section_statement)
  3446. {
  3447.   for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
  3448.     {
  3449.       switch (s->header.type)
  3450.         {
  3451.         case lang_wild_statement_enum:
  3452.           walk_wild (&s->wild_statement, check_section_callback,
  3453.                      output_section_statement);
  3454.           if (! output_section_statement->all_input_readonly)
  3455.             return;
  3456.           break;
  3457.         case lang_constructors_statement_enum:
  3458.           check_input_sections (constructor_list.head,
  3459.                                 output_section_statement);
  3460.           if (! output_section_statement->all_input_readonly)
  3461.             return;
  3462.           break;
  3463.         case lang_group_statement_enum:
  3464.           check_input_sections (s->group_statement.children.head,
  3465.                                 output_section_statement);
  3466.           if (! output_section_statement->all_input_readonly)
  3467.             return;
  3468.           break;
  3469.         default:
  3470.           break;
  3471.         }
  3472.     }
  3473. }
  3474.  
  3475. /* Update wildcard statements if needed.  */
  3476.  
  3477. static void
  3478. update_wild_statements (lang_statement_union_type *s)
  3479. {
  3480.   struct wildcard_list *sec;
  3481.  
  3482.   switch (sort_section)
  3483.     {
  3484.     default:
  3485.       FAIL ();
  3486.  
  3487.     case none:
  3488.       break;
  3489.  
  3490.     case by_name:
  3491.     case by_alignment:
  3492.       for (; s != NULL; s = s->header.next)
  3493.         {
  3494.           switch (s->header.type)
  3495.             {
  3496.             default:
  3497.               break;
  3498.  
  3499.             case lang_wild_statement_enum:
  3500.               for (sec = s->wild_statement.section_list; sec != NULL;
  3501.                    sec = sec->next)
  3502.                 {
  3503.                   switch (sec->spec.sorted)
  3504.                     {
  3505.                     case none:
  3506.                       sec->spec.sorted = sort_section;
  3507.                       break;
  3508.                     case by_name:
  3509.                       if (sort_section == by_alignment)
  3510.                         sec->spec.sorted = by_name_alignment;
  3511.                       break;
  3512.                     case by_alignment:
  3513.                       if (sort_section == by_name)
  3514.                         sec->spec.sorted = by_alignment_name;
  3515.                       break;
  3516.                     default:
  3517.                       break;
  3518.                     }
  3519.                 }
  3520.               break;
  3521.  
  3522.             case lang_constructors_statement_enum:
  3523.               update_wild_statements (constructor_list.head);
  3524.               break;
  3525.  
  3526.             case lang_output_section_statement_enum:
  3527.               /* Don't sort .init/.fini sections.  */
  3528.               if (strcmp (s->output_section_statement.name, ".init") != 0
  3529.                   && strcmp (s->output_section_statement.name, ".fini") != 0)
  3530.                 update_wild_statements
  3531.                   (s->output_section_statement.children.head);
  3532.               break;
  3533.  
  3534.             case lang_group_statement_enum:
  3535.               update_wild_statements (s->group_statement.children.head);
  3536.               break;
  3537.             }
  3538.         }
  3539.       break;
  3540.     }
  3541. }
  3542.  
  3543. /* Open input files and attach to output sections.  */
  3544.  
  3545. static void
  3546. map_input_to_output_sections
  3547.   (lang_statement_union_type *s, const char *target,
  3548.    lang_output_section_statement_type *os)
  3549. {
  3550.   for (; s != NULL; s = s->header.next)
  3551.     {
  3552.       lang_output_section_statement_type *tos;
  3553.       flagword flags;
  3554.  
  3555.       switch (s->header.type)
  3556.         {
  3557.         case lang_wild_statement_enum:
  3558.           wild (&s->wild_statement, target, os);
  3559.           break;
  3560.         case lang_constructors_statement_enum:
  3561.           map_input_to_output_sections (constructor_list.head,
  3562.                                         target,
  3563.                                         os);
  3564.           break;
  3565.         case lang_output_section_statement_enum:
  3566.           tos = &s->output_section_statement;
  3567.           if (tos->constraint != 0)
  3568.             {
  3569.               if (tos->constraint != ONLY_IF_RW
  3570.                   && tos->constraint != ONLY_IF_RO)
  3571.                 break;
  3572.               tos->all_input_readonly = TRUE;
  3573.               check_input_sections (tos->children.head, tos);
  3574.               if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO))
  3575.                 {
  3576.                   tos->constraint = -1;
  3577.                   break;
  3578.                 }
  3579.             }
  3580.           map_input_to_output_sections (tos->children.head,
  3581.                                         target,
  3582.                                         tos);
  3583.           break;
  3584.         case lang_output_statement_enum:
  3585.           break;
  3586.         case lang_target_statement_enum:
  3587.           target = s->target_statement.target;
  3588.           break;
  3589.         case lang_group_statement_enum:
  3590.           map_input_to_output_sections (s->group_statement.children.head,
  3591.                                         target,
  3592.                                         os);
  3593.           break;
  3594.         case lang_data_statement_enum:
  3595.           /* Make sure that any sections mentioned in the expression
  3596.              are initialized.  */
  3597.           exp_init_os (s->data_statement.exp);
  3598.           /* The output section gets CONTENTS, ALLOC and LOAD, but
  3599.              these may be overridden by the script.  */
  3600.           flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD;
  3601.           switch (os->sectype)
  3602.             {
  3603.             case normal_section:
  3604.             case overlay_section:
  3605.               break;
  3606.             case noalloc_section:
  3607.               flags = SEC_HAS_CONTENTS;
  3608.               break;
  3609.             case noload_section:
  3610.               if (bfd_get_flavour (link_info.output_bfd)
  3611.                   == bfd_target_elf_flavour)
  3612.                 flags = SEC_NEVER_LOAD | SEC_ALLOC;
  3613.               else
  3614.                 flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS;
  3615.               break;
  3616.             }
  3617.           if (os->bfd_section == NULL)
  3618.             init_os (os, flags);
  3619.           else
  3620.             os->bfd_section->flags |= flags;
  3621.           break;
  3622.         case lang_input_section_enum:
  3623.           break;
  3624.         case lang_fill_statement_enum:
  3625.         case lang_object_symbols_statement_enum:
  3626.         case lang_reloc_statement_enum:
  3627.         case lang_padding_statement_enum:
  3628.         case lang_input_statement_enum:
  3629.           if (os != NULL && os->bfd_section == NULL)
  3630.             init_os (os, 0);
  3631.           break;
  3632.         case lang_assignment_statement_enum:
  3633.           if (os != NULL && os->bfd_section == NULL)
  3634.             init_os (os, 0);
  3635.  
  3636.           /* Make sure that any sections mentioned in the assignment
  3637.              are initialized.  */
  3638.           exp_init_os (s->assignment_statement.exp);
  3639.           break;
  3640.         case lang_address_statement_enum:
  3641.           /* Mark the specified section with the supplied address.
  3642.              If this section was actually a segment marker, then the
  3643.              directive is ignored if the linker script explicitly
  3644.              processed the segment marker.  Originally, the linker
  3645.              treated segment directives (like -Ttext on the
  3646.              command-line) as section directives.  We honor the
  3647.              section directive semantics for backwards compatibilty;
  3648.              linker scripts that do not specifically check for
  3649.              SEGMENT_START automatically get the old semantics.  */
  3650.           if (!s->address_statement.segment
  3651.               || !s->address_statement.segment->used)
  3652.             {
  3653.               const char *name = s->address_statement.section_name;
  3654.  
  3655.               /* Create the output section statement here so that
  3656.                  orphans with a set address will be placed after other
  3657.                  script sections.  If we let the orphan placement code
  3658.                  place them in amongst other sections then the address
  3659.                  will affect following script sections, which is
  3660.                  likely to surprise naive users.  */
  3661.               tos = lang_output_section_statement_lookup (name, 0, TRUE);
  3662.               tos->addr_tree = s->address_statement.address;
  3663.               if (tos->bfd_section == NULL)
  3664.                 init_os (tos, 0);
  3665.             }
  3666.           break;
  3667.         case lang_insert_statement_enum:
  3668.           break;
  3669.         }
  3670.     }
  3671. }
  3672.  
  3673. /* An insert statement snips out all the linker statements from the
  3674.    start of the list and places them after the output section
  3675.    statement specified by the insert.  This operation is complicated
  3676.    by the fact that we keep a doubly linked list of output section
  3677.    statements as well as the singly linked list of all statements.  */
  3678.  
  3679. static void
  3680. process_insert_statements (void)
  3681. {
  3682.   lang_statement_union_type **s;
  3683.   lang_output_section_statement_type *first_os = NULL;
  3684.   lang_output_section_statement_type *last_os = NULL;
  3685.   lang_output_section_statement_type *os;
  3686.  
  3687.   /* "start of list" is actually the statement immediately after
  3688.      the special abs_section output statement, so that it isn't
  3689.      reordered.  */
  3690.   s = &lang_output_section_statement.head;
  3691.   while (*(s = &(*s)->header.next) != NULL)
  3692.     {
  3693.       if ((*s)->header.type == lang_output_section_statement_enum)
  3694.         {
  3695.           /* Keep pointers to the first and last output section
  3696.              statement in the sequence we may be about to move.  */
  3697.           os = &(*s)->output_section_statement;
  3698.  
  3699.           ASSERT (last_os == NULL || last_os->next == os);
  3700.           last_os = os;
  3701.  
  3702.           /* Set constraint negative so that lang_output_section_find
  3703.              won't match this output section statement.  At this
  3704.              stage in linking constraint has values in the range
  3705.              [-1, ONLY_IN_RW].  */
  3706.           last_os->constraint = -2 - last_os->constraint;
  3707.           if (first_os == NULL)
  3708.             first_os = last_os;
  3709.         }
  3710.       else if ((*s)->header.type == lang_insert_statement_enum)
  3711.         {
  3712.           lang_insert_statement_type *i = &(*s)->insert_statement;
  3713.           lang_output_section_statement_type *where;
  3714.           lang_statement_union_type **ptr;
  3715.           lang_statement_union_type *first;
  3716.  
  3717.           where = lang_output_section_find (i->where);
  3718.           if (where != NULL && i->is_before)
  3719.             {
  3720.               do
  3721.                 where = where->prev;
  3722.               while (where != NULL && where->constraint < 0);
  3723.             }
  3724.           if (where == NULL)
  3725.             {
  3726.               einfo (_("%F%P: %s not found for insert\n"), i->where);
  3727.               return;
  3728.             }
  3729.  
  3730.           /* Deal with reordering the output section statement list.  */
  3731.           if (last_os != NULL)
  3732.             {
  3733.               asection *first_sec, *last_sec;
  3734.               struct lang_output_section_statement_struct **next;
  3735.  
  3736.               /* Snip out the output sections we are moving.  */
  3737.               first_os->prev->next = last_os->next;
  3738.               if (last_os->next == NULL)
  3739.                 {
  3740.                   next = &first_os->prev->next;
  3741.                   lang_output_section_statement.tail
  3742.                     = (lang_statement_union_type **) next;
  3743.                 }
  3744.               else
  3745.                 last_os->next->prev = first_os->prev;
  3746.               /* Add them in at the new position.  */
  3747.               last_os->next = where->next;
  3748.               if (where->next == NULL)
  3749.                 {
  3750.                   next = &last_os->next;
  3751.                   lang_output_section_statement.tail
  3752.                     = (lang_statement_union_type **) next;
  3753.                 }
  3754.               else
  3755.                 where->next->prev = last_os;
  3756.               first_os->prev = where;
  3757.               where->next = first_os;
  3758.  
  3759.               /* Move the bfd sections in the same way.  */
  3760.               first_sec = NULL;
  3761.               last_sec = NULL;
  3762.               for (os = first_os; os != NULL; os = os->next)
  3763.                 {
  3764.                   os->constraint = -2 - os->constraint;
  3765.                   if (os->bfd_section != NULL
  3766.                       && os->bfd_section->owner != NULL)
  3767.                     {
  3768.                       last_sec = os->bfd_section;
  3769.                       if (first_sec == NULL)
  3770.                         first_sec = last_sec;
  3771.                     }
  3772.                   if (os == last_os)
  3773.                     break;
  3774.                 }
  3775.               if (last_sec != NULL)
  3776.                 {
  3777.                   asection *sec = where->bfd_section;
  3778.                   if (sec == NULL)
  3779.                     sec = output_prev_sec_find (where);
  3780.  
  3781.                   /* The place we want to insert must come after the
  3782.                      sections we are moving.  So if we find no
  3783.                      section or if the section is the same as our
  3784.                      last section, then no move is needed.  */
  3785.                   if (sec != NULL && sec != last_sec)
  3786.                     {
  3787.                       /* Trim them off.  */
  3788.                       if (first_sec->prev != NULL)
  3789.                         first_sec->prev->next = last_sec->next;
  3790.                       else
  3791.                         link_info.output_bfd->sections = last_sec->next;
  3792.                       if (last_sec->next != NULL)
  3793.                         last_sec->next->prev = first_sec->prev;
  3794.                       else
  3795.                         link_info.output_bfd->section_last = first_sec->prev;
  3796.                       /* Add back.  */
  3797.                       last_sec->next = sec->next;
  3798.                       if (sec->next != NULL)
  3799.                         sec->next->prev = last_sec;
  3800.                       else
  3801.                         link_info.output_bfd->section_last = last_sec;
  3802.                       first_sec->prev = sec;
  3803.                       sec->next = first_sec;
  3804.                     }
  3805.                 }
  3806.  
  3807.               first_os = NULL;
  3808.               last_os = NULL;
  3809.             }
  3810.  
  3811.           ptr = insert_os_after (where);
  3812.           /* Snip everything after the abs_section output statement we
  3813.              know is at the start of the list, up to and including
  3814.              the insert statement we are currently processing.  */
  3815.           first = lang_output_section_statement.head->header.next;
  3816.           lang_output_section_statement.head->header.next = (*s)->header.next;
  3817.           /* Add them back where they belong.  */
  3818.           *s = *ptr;
  3819.           if (*s == NULL)
  3820.             statement_list.tail = s;
  3821.           *ptr = first;
  3822.           s = &lang_output_section_statement.head;
  3823.         }
  3824.     }
  3825.  
  3826.   /* Undo constraint twiddling.  */
  3827.   for (os = first_os; os != NULL; os = os->next)
  3828.     {
  3829.       os->constraint = -2 - os->constraint;
  3830.       if (os == last_os)
  3831.         break;
  3832.     }
  3833. }
  3834.  
  3835. /* An output section might have been removed after its statement was
  3836.    added.  For example, ldemul_before_allocation can remove dynamic
  3837.    sections if they turn out to be not needed.  Clean them up here.  */
  3838.  
  3839. void
  3840. strip_excluded_output_sections (void)
  3841. {
  3842.   lang_output_section_statement_type *os;
  3843.  
  3844.   /* Run lang_size_sections (if not already done).  */
  3845.   if (expld.phase != lang_mark_phase_enum)
  3846.     {
  3847.       expld.phase = lang_mark_phase_enum;
  3848.       expld.dataseg.phase = exp_dataseg_none;
  3849.       one_lang_size_sections_pass (NULL, FALSE);
  3850.       lang_reset_memory_regions ();
  3851.     }
  3852.  
  3853.   for (os = &lang_output_section_statement.head->output_section_statement;
  3854.        os != NULL;
  3855.        os = os->next)
  3856.     {
  3857.       asection *output_section;
  3858.       bfd_boolean exclude;
  3859.  
  3860.       if (os->constraint < 0)
  3861.         continue;
  3862.  
  3863.       output_section = os->bfd_section;
  3864.       if (output_section == NULL)
  3865.         continue;
  3866.  
  3867.       exclude = (output_section->rawsize == 0
  3868.                  && (output_section->flags & SEC_KEEP) == 0
  3869.                  && !bfd_section_removed_from_list (link_info.output_bfd,
  3870.                                                     output_section));
  3871.  
  3872.       /* Some sections have not yet been sized, notably .gnu.version,
  3873.          .dynsym, .dynstr and .hash.  These all have SEC_LINKER_CREATED
  3874.          input sections, so don't drop output sections that have such
  3875.          input sections unless they are also marked SEC_EXCLUDE.  */
  3876.       if (exclude && output_section->map_head.s != NULL)
  3877.         {
  3878.           asection *s;
  3879.  
  3880.           for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
  3881.             if ((s->flags & SEC_EXCLUDE) == 0
  3882.                 && ((s->flags & SEC_LINKER_CREATED) != 0
  3883.                     || link_info.emitrelocations))
  3884.               {
  3885.                 exclude = FALSE;
  3886.                 break;
  3887.               }
  3888.         }
  3889.  
  3890.       /* TODO: Don't just junk map_head.s, turn them into link_orders.  */
  3891.       output_section->map_head.link_order = NULL;
  3892.       output_section->map_tail.link_order = NULL;
  3893.  
  3894.       if (exclude)
  3895.         {
  3896.           /* We don't set bfd_section to NULL since bfd_section of the
  3897.              removed output section statement may still be used.  */
  3898.           if (!os->update_dot)
  3899.             os->ignored = TRUE;
  3900.           output_section->flags |= SEC_EXCLUDE;
  3901.           bfd_section_list_remove (link_info.output_bfd, output_section);
  3902.           link_info.output_bfd->section_count--;
  3903.         }
  3904.     }
  3905.  
  3906.   /* Stop future calls to lang_add_section from messing with map_head
  3907.      and map_tail link_order fields.  */
  3908.   stripped_excluded_sections = TRUE;
  3909. }
  3910.  
  3911. static void
  3912. print_output_section_statement
  3913.   (lang_output_section_statement_type *output_section_statement)
  3914. {
  3915.   asection *section = output_section_statement->bfd_section;
  3916.   int len;
  3917.  
  3918.   if (output_section_statement != abs_output_section)
  3919.     {
  3920.       minfo ("\n%s", output_section_statement->name);
  3921.  
  3922.       if (section != NULL)
  3923.         {
  3924.           print_dot = section->vma;
  3925.  
  3926.           len = strlen (output_section_statement->name);
  3927.           if (len >= SECTION_NAME_MAP_LENGTH - 1)
  3928.             {
  3929.               print_nl ();
  3930.               len = 0;
  3931.             }
  3932.           while (len < SECTION_NAME_MAP_LENGTH)
  3933.             {
  3934.               print_space ();
  3935.               ++len;
  3936.             }
  3937.  
  3938.           minfo ("0x%V %W", section->vma, section->size);
  3939.  
  3940.           if (section->vma != section->lma)
  3941.             minfo (_(" load address 0x%V"), section->lma);
  3942.  
  3943.           if (output_section_statement->update_dot_tree != NULL)
  3944.             exp_fold_tree (output_section_statement->update_dot_tree,
  3945.                            bfd_abs_section_ptr, &print_dot);
  3946.         }
  3947.  
  3948.       print_nl ();
  3949.     }
  3950.  
  3951.   print_statement_list (output_section_statement->children.head,
  3952.                         output_section_statement);
  3953. }
  3954.  
  3955. static void
  3956. print_assignment (lang_assignment_statement_type *assignment,
  3957.                   lang_output_section_statement_type *output_section)
  3958. {
  3959.   unsigned int i;
  3960.   bfd_boolean is_dot;
  3961.   etree_type *tree;
  3962.   asection *osec;
  3963.  
  3964.   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  3965.     print_space ();
  3966.  
  3967.   if (assignment->exp->type.node_class == etree_assert)
  3968.     {
  3969.       is_dot = FALSE;
  3970.       tree = assignment->exp->assert_s.child;
  3971.     }
  3972.   else
  3973.     {
  3974.       const char *dst = assignment->exp->assign.dst;
  3975.  
  3976.       is_dot = (dst[0] == '.' && dst[1] == 0);
  3977.       expld.assign_name = dst;
  3978.       tree = assignment->exp->assign.src;
  3979.     }
  3980.  
  3981.   osec = output_section->bfd_section;
  3982.   if (osec == NULL)
  3983.     osec = bfd_abs_section_ptr;
  3984.   exp_fold_tree (tree, osec, &print_dot);
  3985.   if (expld.result.valid_p)
  3986.     {
  3987.       bfd_vma value;
  3988.  
  3989.       if (assignment->exp->type.node_class == etree_assert
  3990.           || is_dot
  3991.           || expld.assign_name != NULL)
  3992.         {
  3993.           value = expld.result.value;
  3994.  
  3995.           if (expld.result.section != NULL)
  3996.             value += expld.result.section->vma;
  3997.  
  3998.           minfo ("0x%V", value);
  3999.           if (is_dot)
  4000.             print_dot = value;
  4001.         }
  4002.       else
  4003.         {
  4004.           struct bfd_link_hash_entry *h;
  4005.  
  4006.           h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
  4007.                                     FALSE, FALSE, TRUE);
  4008.           if (h)
  4009.             {
  4010.               value = h->u.def.value;
  4011.               value += h->u.def.section->output_section->vma;
  4012.               value += h->u.def.section->output_offset;
  4013.  
  4014.               minfo ("[0x%V]", value);
  4015.             }
  4016.           else
  4017.             minfo ("[unresolved]");
  4018.         }
  4019.     }
  4020.   else
  4021.     {
  4022.       minfo ("*undef*   ");
  4023. #ifdef BFD64
  4024.       minfo ("        ");
  4025. #endif
  4026.     }
  4027.   expld.assign_name = NULL;
  4028.  
  4029.   minfo ("                ");
  4030.   exp_print_tree (assignment->exp);
  4031.   print_nl ();
  4032. }
  4033.  
  4034. static void
  4035. print_input_statement (lang_input_statement_type *statm)
  4036. {
  4037.   if (statm->filename != NULL
  4038.       && (statm->the_bfd == NULL
  4039.           || (statm->the_bfd->flags & BFD_LINKER_CREATED) == 0))
  4040.     fprintf (config.map_file, "LOAD %s\n", statm->filename);
  4041. }
  4042.  
  4043. /* Print all symbols defined in a particular section.  This is called
  4044.    via bfd_link_hash_traverse, or by print_all_symbols.  */
  4045.  
  4046. static bfd_boolean
  4047. print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
  4048. {
  4049.   asection *sec = (asection *) ptr;
  4050.  
  4051.   if ((hash_entry->type == bfd_link_hash_defined
  4052.        || hash_entry->type == bfd_link_hash_defweak)
  4053.       && sec == hash_entry->u.def.section)
  4054.     {
  4055.       int i;
  4056.  
  4057.       for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  4058.         print_space ();
  4059.       minfo ("0x%V   ",
  4060.              (hash_entry->u.def.value
  4061.               + hash_entry->u.def.section->output_offset
  4062.               + hash_entry->u.def.section->output_section->vma));
  4063.  
  4064.       minfo ("             %T\n", hash_entry->root.string);
  4065.     }
  4066.  
  4067.   return TRUE;
  4068. }
  4069.  
  4070. static int
  4071. hash_entry_addr_cmp (const void *a, const void *b)
  4072. {
  4073.   const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a;
  4074.   const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b;
  4075.  
  4076.   if (l->u.def.value < r->u.def.value)
  4077.     return -1;
  4078.   else if (l->u.def.value > r->u.def.value)
  4079.     return 1;
  4080.   else
  4081.     return 0;
  4082. }
  4083.  
  4084. static void
  4085. print_all_symbols (asection *sec)
  4086. {
  4087.   struct fat_user_section_struct *ud =
  4088.       (struct fat_user_section_struct *) get_userdata (sec);
  4089.   struct map_symbol_def *def;
  4090.   struct bfd_link_hash_entry **entries;
  4091.   unsigned int i;
  4092.  
  4093.   if (!ud)
  4094.     return;
  4095.  
  4096.   *ud->map_symbol_def_tail = 0;
  4097.  
  4098.   /* Sort the symbols by address.  */
  4099.   entries = (struct bfd_link_hash_entry **)
  4100.       obstack_alloc (&map_obstack, ud->map_symbol_def_count * sizeof (*entries));
  4101.  
  4102.   for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++)
  4103.     entries[i] = def->entry;
  4104.  
  4105.   qsort (entries, ud->map_symbol_def_count, sizeof (*entries),
  4106.          hash_entry_addr_cmp);
  4107.  
  4108.   /* Print the symbols.  */
  4109.   for (i = 0; i < ud->map_symbol_def_count; i++)
  4110.     print_one_symbol (entries[i], sec);
  4111.  
  4112.   obstack_free (&map_obstack, entries);
  4113. }
  4114.  
  4115. /* Print information about an input section to the map file.  */
  4116.  
  4117. static void
  4118. print_input_section (asection *i, bfd_boolean is_discarded)
  4119. {
  4120.   bfd_size_type size = i->size;
  4121.   int len;
  4122.   bfd_vma addr;
  4123.  
  4124.   init_opb ();
  4125.  
  4126.   print_space ();
  4127.   minfo ("%s", i->name);
  4128.  
  4129.   len = 1 + strlen (i->name);
  4130.   if (len >= SECTION_NAME_MAP_LENGTH - 1)
  4131.     {
  4132.       print_nl ();
  4133.       len = 0;
  4134.     }
  4135.   while (len < SECTION_NAME_MAP_LENGTH)
  4136.     {
  4137.       print_space ();
  4138.       ++len;
  4139.     }
  4140.  
  4141.   if (i->output_section != NULL
  4142.       && i->output_section->owner == link_info.output_bfd)
  4143.     addr = i->output_section->vma + i->output_offset;
  4144.   else
  4145.     {
  4146.       addr = print_dot;
  4147.       if (!is_discarded)
  4148.         size = 0;
  4149.     }
  4150.  
  4151.   minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
  4152.  
  4153.   if (size != i->rawsize && i->rawsize != 0)
  4154.     {
  4155.       len = SECTION_NAME_MAP_LENGTH + 3;
  4156. #ifdef BFD64
  4157.       len += 16;
  4158. #else
  4159.       len += 8;
  4160. #endif
  4161.       while (len > 0)
  4162.         {
  4163.           print_space ();
  4164.           --len;
  4165.         }
  4166.  
  4167.       minfo (_("%W (size before relaxing)\n"), i->rawsize);
  4168.     }
  4169.  
  4170.   if (i->output_section != NULL
  4171.       && i->output_section->owner == link_info.output_bfd)
  4172.     {
  4173.       if (link_info.reduce_memory_overheads)
  4174.         bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
  4175.       else
  4176.         print_all_symbols (i);
  4177.  
  4178.       /* Update print_dot, but make sure that we do not move it
  4179.          backwards - this could happen if we have overlays and a
  4180.          later overlay is shorter than an earier one.  */
  4181.       if (addr + TO_ADDR (size) > print_dot)
  4182.         print_dot = addr + TO_ADDR (size);
  4183.     }
  4184. }
  4185.  
  4186. static void
  4187. print_fill_statement (lang_fill_statement_type *fill)
  4188. {
  4189.   size_t size;
  4190.   unsigned char *p;
  4191.   fputs (" FILL mask 0x", config.map_file);
  4192.   for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
  4193.     fprintf (config.map_file, "%02x", *p);
  4194.   fputs ("\n", config.map_file);
  4195. }
  4196.  
  4197. static void
  4198. print_data_statement (lang_data_statement_type *data)
  4199. {
  4200.   int i;
  4201.   bfd_vma addr;
  4202.   bfd_size_type size;
  4203.   const char *name;
  4204.  
  4205.   init_opb ();
  4206.   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  4207.     print_space ();
  4208.  
  4209.   addr = data->output_offset;
  4210.   if (data->output_section != NULL)
  4211.     addr += data->output_section->vma;
  4212.  
  4213.   switch (data->type)
  4214.     {
  4215.     default:
  4216.       abort ();
  4217.     case BYTE:
  4218.       size = BYTE_SIZE;
  4219.       name = "BYTE";
  4220.       break;
  4221.     case SHORT:
  4222.       size = SHORT_SIZE;
  4223.       name = "SHORT";
  4224.       break;
  4225.     case LONG:
  4226.       size = LONG_SIZE;
  4227.       name = "LONG";
  4228.       break;
  4229.     case QUAD:
  4230.       size = QUAD_SIZE;
  4231.       name = "QUAD";
  4232.       break;
  4233.     case SQUAD:
  4234.       size = QUAD_SIZE;
  4235.       name = "SQUAD";
  4236.       break;
  4237.     }
  4238.  
  4239.   minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
  4240.  
  4241.   if (data->exp->type.node_class != etree_value)
  4242.     {
  4243.       print_space ();
  4244.       exp_print_tree (data->exp);
  4245.     }
  4246.  
  4247.   print_nl ();
  4248.  
  4249.   print_dot = addr + TO_ADDR (size);
  4250. }
  4251.  
  4252. /* Print an address statement.  These are generated by options like
  4253.    -Ttext.  */
  4254.  
  4255. static void
  4256. print_address_statement (lang_address_statement_type *address)
  4257. {
  4258.   minfo (_("Address of section %s set to "), address->section_name);
  4259.   exp_print_tree (address->address);
  4260.   print_nl ();
  4261. }
  4262.  
  4263. /* Print a reloc statement.  */
  4264.  
  4265. static void
  4266. print_reloc_statement (lang_reloc_statement_type *reloc)
  4267. {
  4268.   int i;
  4269.   bfd_vma addr;
  4270.   bfd_size_type size;
  4271.  
  4272.   init_opb ();
  4273.   for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  4274.     print_space ();
  4275.  
  4276.   addr = reloc->output_offset;
  4277.   if (reloc->output_section != NULL)
  4278.     addr += reloc->output_section->vma;
  4279.  
  4280.   size = bfd_get_reloc_size (reloc->howto);
  4281.  
  4282.   minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
  4283.  
  4284.   if (reloc->name != NULL)
  4285.     minfo ("%s+", reloc->name);
  4286.   else
  4287.     minfo ("%s+", reloc->section->name);
  4288.  
  4289.   exp_print_tree (reloc->addend_exp);
  4290.  
  4291.   print_nl ();
  4292.  
  4293.   print_dot = addr + TO_ADDR (size);
  4294. }
  4295.  
  4296. static void
  4297. print_padding_statement (lang_padding_statement_type *s)
  4298. {
  4299.   int len;
  4300.   bfd_vma addr;
  4301.  
  4302.   init_opb ();
  4303.   minfo (" *fill*");
  4304.  
  4305.   len = sizeof " *fill*" - 1;
  4306.   while (len < SECTION_NAME_MAP_LENGTH)
  4307.     {
  4308.       print_space ();
  4309.       ++len;
  4310.     }
  4311.  
  4312.   addr = s->output_offset;
  4313.   if (s->output_section != NULL)
  4314.     addr += s->output_section->vma;
  4315.   minfo ("0x%V %W ", addr, (bfd_vma) s->size);
  4316.  
  4317.   if (s->fill->size != 0)
  4318.     {
  4319.       size_t size;
  4320.       unsigned char *p;
  4321.       for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
  4322.         fprintf (config.map_file, "%02x", *p);
  4323.     }
  4324.  
  4325.   print_nl ();
  4326.  
  4327.   print_dot = addr + TO_ADDR (s->size);
  4328. }
  4329.  
  4330. static void
  4331. print_wild_statement (lang_wild_statement_type *w,
  4332.                       lang_output_section_statement_type *os)
  4333. {
  4334.   struct wildcard_list *sec;
  4335.  
  4336.   print_space ();
  4337.  
  4338.   if (w->filenames_sorted)
  4339.     minfo ("SORT(");
  4340.   if (w->filename != NULL)
  4341.     minfo ("%s", w->filename);
  4342.   else
  4343.     minfo ("*");
  4344.   if (w->filenames_sorted)
  4345.     minfo (")");
  4346.  
  4347.   minfo ("(");
  4348.   for (sec = w->section_list; sec; sec = sec->next)
  4349.     {
  4350.       if (sec->spec.sorted)
  4351.         minfo ("SORT(");
  4352.       if (sec->spec.exclude_name_list != NULL)
  4353.         {
  4354.           name_list *tmp;
  4355.           minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
  4356.           for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
  4357.             minfo (" %s", tmp->name);
  4358.           minfo (") ");
  4359.         }
  4360.       if (sec->spec.name != NULL)
  4361.         minfo ("%s", sec->spec.name);
  4362.       else
  4363.         minfo ("*");
  4364.       if (sec->spec.sorted)
  4365.         minfo (")");
  4366.       if (sec->next)
  4367.         minfo (" ");
  4368.     }
  4369.   minfo (")");
  4370.  
  4371.   print_nl ();
  4372.  
  4373.   print_statement_list (w->children.head, os);
  4374. }
  4375.  
  4376. /* Print a group statement.  */
  4377.  
  4378. static void
  4379. print_group (lang_group_statement_type *s,
  4380.              lang_output_section_statement_type *os)
  4381. {
  4382.   fprintf (config.map_file, "START GROUP\n");
  4383.   print_statement_list (s->children.head, os);
  4384.   fprintf (config.map_file, "END GROUP\n");
  4385. }
  4386.  
  4387. /* Print the list of statements in S.
  4388.    This can be called for any statement type.  */
  4389.  
  4390. static void
  4391. print_statement_list (lang_statement_union_type *s,
  4392.                       lang_output_section_statement_type *os)
  4393. {
  4394.   while (s != NULL)
  4395.     {
  4396.       print_statement (s, os);
  4397.       s = s->header.next;
  4398.     }
  4399. }
  4400.  
  4401. /* Print the first statement in statement list S.
  4402.    This can be called for any statement type.  */
  4403.  
  4404. static void
  4405. print_statement (lang_statement_union_type *s,
  4406.                  lang_output_section_statement_type *os)
  4407. {
  4408.   switch (s->header.type)
  4409.     {
  4410.     default:
  4411.       fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
  4412.       FAIL ();
  4413.       break;
  4414.     case lang_constructors_statement_enum:
  4415.       if (constructor_list.head != NULL)
  4416.         {
  4417.           if (constructors_sorted)
  4418.             minfo (" SORT (CONSTRUCTORS)\n");
  4419.           else
  4420.             minfo (" CONSTRUCTORS\n");
  4421.           print_statement_list (constructor_list.head, os);
  4422.         }
  4423.       break;
  4424.     case lang_wild_statement_enum:
  4425.       print_wild_statement (&s->wild_statement, os);
  4426.       break;
  4427.     case lang_address_statement_enum:
  4428.       print_address_statement (&s->address_statement);
  4429.       break;
  4430.     case lang_object_symbols_statement_enum:
  4431.       minfo (" CREATE_OBJECT_SYMBOLS\n");
  4432.       break;
  4433.     case lang_fill_statement_enum:
  4434.       print_fill_statement (&s->fill_statement);
  4435.       break;
  4436.     case lang_data_statement_enum:
  4437.       print_data_statement (&s->data_statement);
  4438.       break;
  4439.     case lang_reloc_statement_enum:
  4440.       print_reloc_statement (&s->reloc_statement);
  4441.       break;
  4442.     case lang_input_section_enum:
  4443.       print_input_section (s->input_section.section, FALSE);
  4444.       break;
  4445.     case lang_padding_statement_enum:
  4446.       print_padding_statement (&s->padding_statement);
  4447.       break;
  4448.     case lang_output_section_statement_enum:
  4449.       print_output_section_statement (&s->output_section_statement);
  4450.       break;
  4451.     case lang_assignment_statement_enum:
  4452.       print_assignment (&s->assignment_statement, os);
  4453.       break;
  4454.     case lang_target_statement_enum:
  4455.       fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
  4456.       break;
  4457.     case lang_output_statement_enum:
  4458.       minfo ("OUTPUT(%s", s->output_statement.name);
  4459.       if (output_target != NULL)
  4460.         minfo (" %s", output_target);
  4461.       minfo (")\n");
  4462.       break;
  4463.     case lang_input_statement_enum:
  4464.       print_input_statement (&s->input_statement);
  4465.       break;
  4466.     case lang_group_statement_enum:
  4467.       print_group (&s->group_statement, os);
  4468.       break;
  4469.     case lang_insert_statement_enum:
  4470.       minfo ("INSERT %s %s\n",
  4471.              s->insert_statement.is_before ? "BEFORE" : "AFTER",
  4472.              s->insert_statement.where);
  4473.       break;
  4474.     }
  4475. }
  4476.  
  4477. static void
  4478. print_statements (void)
  4479. {
  4480.   print_statement_list (statement_list.head, abs_output_section);
  4481. }
  4482.  
  4483. /* Print the first N statements in statement list S to STDERR.
  4484.    If N == 0, nothing is printed.
  4485.    If N < 0, the entire list is printed.
  4486.    Intended to be called from GDB.  */
  4487.  
  4488. void
  4489. dprint_statement (lang_statement_union_type *s, int n)
  4490. {
  4491.   FILE *map_save = config.map_file;
  4492.  
  4493.   config.map_file = stderr;
  4494.  
  4495.   if (n < 0)
  4496.     print_statement_list (s, abs_output_section);
  4497.   else
  4498.     {
  4499.       while (s && --n >= 0)
  4500.         {
  4501.           print_statement (s, abs_output_section);
  4502.           s = s->header.next;
  4503.         }
  4504.     }
  4505.  
  4506.   config.map_file = map_save;
  4507. }
  4508.  
  4509. static void
  4510. insert_pad (lang_statement_union_type **ptr,
  4511.             fill_type *fill,
  4512.             bfd_size_type alignment_needed,
  4513.             asection *output_section,
  4514.             bfd_vma dot)
  4515. {
  4516.   static fill_type zero_fill;
  4517.   lang_statement_union_type *pad = NULL;
  4518.  
  4519.   if (ptr != &statement_list.head)
  4520.     pad = ((lang_statement_union_type *)
  4521.            ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
  4522.   if (pad != NULL
  4523.       && pad->header.type == lang_padding_statement_enum
  4524.       && pad->padding_statement.output_section == output_section)
  4525.     {
  4526.       /* Use the existing pad statement.  */
  4527.     }
  4528.   else if ((pad = *ptr) != NULL
  4529.            && pad->header.type == lang_padding_statement_enum
  4530.            && pad->padding_statement.output_section == output_section)
  4531.     {
  4532.       /* Use the existing pad statement.  */
  4533.     }
  4534.   else
  4535.     {
  4536.       /* Make a new padding statement, linked into existing chain.  */
  4537.       pad = (lang_statement_union_type *)
  4538.           stat_alloc (sizeof (lang_padding_statement_type));
  4539.       pad->header.next = *ptr;
  4540.       *ptr = pad;
  4541.       pad->header.type = lang_padding_statement_enum;
  4542.       pad->padding_statement.output_section = output_section;
  4543.       if (fill == NULL)
  4544.         fill = &zero_fill;
  4545.       pad->padding_statement.fill = fill;
  4546.     }
  4547.   pad->padding_statement.output_offset = dot - output_section->vma;
  4548.   pad->padding_statement.size = alignment_needed;
  4549.   output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed)
  4550.                                   - output_section->vma);
  4551. }
  4552.  
  4553. /* Work out how much this section will move the dot point.  */
  4554.  
  4555. static bfd_vma
  4556. size_input_section
  4557.   (lang_statement_union_type **this_ptr,
  4558.    lang_output_section_statement_type *output_section_statement,
  4559.    fill_type *fill,
  4560.    bfd_vma dot)
  4561. {
  4562.   lang_input_section_type *is = &((*this_ptr)->input_section);
  4563.   asection *i = is->section;
  4564.  
  4565.   if (i->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
  4566.       && (i->flags & SEC_EXCLUDE) == 0)
  4567.     {
  4568.       bfd_size_type alignment_needed;
  4569.       asection *o;
  4570.  
  4571.       /* Align this section first to the input sections requirement,
  4572.          then to the output section's requirement.  If this alignment
  4573.          is greater than any seen before, then record it too.  Perform
  4574.          the alignment by inserting a magic 'padding' statement.  */
  4575.  
  4576.       if (output_section_statement->subsection_alignment != -1)
  4577.         i->alignment_power = output_section_statement->subsection_alignment;
  4578.  
  4579.       o = output_section_statement->bfd_section;
  4580.       if (o->alignment_power < i->alignment_power)
  4581.         o->alignment_power = i->alignment_power;
  4582.  
  4583.       alignment_needed = align_power (dot, i->alignment_power) - dot;
  4584.  
  4585.       if (alignment_needed != 0)
  4586.         {
  4587.           insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
  4588.           dot += alignment_needed;
  4589.         }
  4590.  
  4591.       /* Remember where in the output section this input section goes.  */
  4592.  
  4593.       i->output_offset = dot - o->vma;
  4594.  
  4595.       /* Mark how big the output section must be to contain this now.  */
  4596.       dot += TO_ADDR (i->size);
  4597.       o->size = TO_SIZE (dot - o->vma);
  4598.     }
  4599.   else
  4600.     {
  4601.       i->output_offset = i->vma - output_section_statement->bfd_section->vma;
  4602.     }
  4603.  
  4604.   return dot;
  4605. }
  4606.  
  4607. static int
  4608. sort_sections_by_lma (const void *arg1, const void *arg2)
  4609. {
  4610.   const asection *sec1 = *(const asection **) arg1;
  4611.   const asection *sec2 = *(const asection **) arg2;
  4612.  
  4613.   if (bfd_section_lma (sec1->owner, sec1)
  4614.       < bfd_section_lma (sec2->owner, sec2))
  4615.     return -1;
  4616.   else if (bfd_section_lma (sec1->owner, sec1)
  4617.            > bfd_section_lma (sec2->owner, sec2))
  4618.     return 1;
  4619.   else if (sec1->id < sec2->id)
  4620.     return -1;
  4621.   else if (sec1->id > sec2->id)
  4622.     return 1;
  4623.  
  4624.   return 0;
  4625. }
  4626.  
  4627. #define IGNORE_SECTION(s) \
  4628.   ((s->flags & SEC_ALLOC) == 0                          \
  4629.    || ((s->flags & SEC_THREAD_LOCAL) != 0               \
  4630.         && (s->flags & SEC_LOAD) == 0))
  4631.  
  4632. /* Check to see if any allocated sections overlap with other allocated
  4633.    sections.  This can happen if a linker script specifies the output
  4634.    section addresses of the two sections.  Also check whether any memory
  4635.    region has overflowed.  */
  4636.  
  4637. static void
  4638. lang_check_section_addresses (void)
  4639. {
  4640.   asection *s, *p;
  4641.   asection **sections, **spp;
  4642.   unsigned int count;
  4643.   bfd_vma s_start;
  4644.   bfd_vma s_end;
  4645.   bfd_vma p_start;
  4646.   bfd_vma p_end;
  4647.   bfd_size_type amt;
  4648.   lang_memory_region_type *m;
  4649.  
  4650.   if (bfd_count_sections (link_info.output_bfd) <= 1)
  4651.     return;
  4652.  
  4653.   amt = bfd_count_sections (link_info.output_bfd) * sizeof (asection *);
  4654.   sections = (asection **) xmalloc (amt);
  4655.  
  4656.   /* Scan all sections in the output list.  */
  4657.   count = 0;
  4658.   for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
  4659.     {
  4660.       /* Only consider loadable sections with real contents.  */
  4661.       if (!(s->flags & SEC_LOAD)
  4662.           || !(s->flags & SEC_ALLOC)
  4663.           || s->size == 0)
  4664.         continue;
  4665.  
  4666.       sections[count] = s;
  4667.       count++;
  4668.     }
  4669.  
  4670.   if (count <= 1)
  4671.     return;
  4672.  
  4673.   qsort (sections, (size_t) count, sizeof (asection *),
  4674.          sort_sections_by_lma);
  4675.  
  4676.   spp = sections;
  4677.   s = *spp++;
  4678.   s_start = s->lma;
  4679.   s_end = s_start + TO_ADDR (s->size) - 1;
  4680.   for (count--; count; count--)
  4681.     {
  4682.       /* We must check the sections' LMA addresses not their VMA
  4683.          addresses because overlay sections can have overlapping VMAs
  4684.          but they must have distinct LMAs.  */
  4685.       p = s;
  4686.       p_start = s_start;
  4687.       p_end = s_end;
  4688.       s = *spp++;
  4689.       s_start = s->lma;
  4690.       s_end = s_start + TO_ADDR (s->size) - 1;
  4691.  
  4692.       /* Look for an overlap.  We have sorted sections by lma, so we
  4693.          know that s_start >= p_start.  Besides the obvious case of
  4694.          overlap when the current section starts before the previous
  4695.          one ends, we also must have overlap if the previous section
  4696.          wraps around the address space.  */
  4697.       if (s_start <= p_end
  4698.           || p_end < p_start)
  4699.         einfo (_("%X%P: section %s loaded at [%V,%V] overlaps section %s loaded at [%V,%V]\n"),
  4700.                s->name, s_start, s_end, p->name, p_start, p_end);
  4701.     }
  4702.  
  4703.   free (sections);
  4704.  
  4705.   /* If any memory region has overflowed, report by how much.
  4706.      We do not issue this diagnostic for regions that had sections
  4707.      explicitly placed outside their bounds; os_region_check's
  4708.      diagnostics are adequate for that case.
  4709.  
  4710.      FIXME: It is conceivable that m->current - (m->origin + m->length)
  4711.      might overflow a 32-bit integer.  There is, alas, no way to print
  4712.      a bfd_vma quantity in decimal.  */
  4713.   for (m = lang_memory_region_list; m; m = m->next)
  4714.     if (m->had_full_message)
  4715.       einfo (_("%X%P: region `%s' overflowed by %ld bytes\n"),
  4716.              m->name_list.name, (long)(m->current - (m->origin + m->length)));
  4717.  
  4718. }
  4719.  
  4720. /* Make sure the new address is within the region.  We explicitly permit the
  4721.    current address to be at the exact end of the region when the address is
  4722.    non-zero, in case the region is at the end of addressable memory and the
  4723.    calculation wraps around.  */
  4724.  
  4725. static void
  4726. os_region_check (lang_output_section_statement_type *os,
  4727.                  lang_memory_region_type *region,
  4728.                  etree_type *tree,
  4729.                  bfd_vma rbase)
  4730. {
  4731.   if ((region->current < region->origin
  4732.        || (region->current - region->origin > region->length))
  4733.       && ((region->current != region->origin + region->length)
  4734.           || rbase == 0))
  4735.     {
  4736.       if (tree != NULL)
  4737.         {
  4738.           einfo (_("%X%P: address 0x%v of %B section `%s'"
  4739.                    " is not within region `%s'\n"),
  4740.                  region->current,
  4741.                  os->bfd_section->owner,
  4742.                  os->bfd_section->name,
  4743.                  region->name_list.name);
  4744.         }
  4745.       else if (!region->had_full_message)
  4746.         {
  4747.           region->had_full_message = TRUE;
  4748.  
  4749.           einfo (_("%X%P: %B section `%s' will not fit in region `%s'\n"),
  4750.                  os->bfd_section->owner,
  4751.                  os->bfd_section->name,
  4752.                  region->name_list.name);
  4753.         }
  4754.     }
  4755. }
  4756.  
  4757. /* Set the sizes for all the output sections.  */
  4758.  
  4759. static bfd_vma
  4760. lang_size_sections_1
  4761.   (lang_statement_union_type **prev,
  4762.    lang_output_section_statement_type *output_section_statement,
  4763.    fill_type *fill,
  4764.    bfd_vma dot,
  4765.    bfd_boolean *relax,
  4766.    bfd_boolean check_regions)
  4767. {
  4768.   lang_statement_union_type *s;
  4769.  
  4770.   /* Size up the sections from their constituent parts.  */
  4771.   for (s = *prev; s != NULL; s = s->header.next)
  4772.     {
  4773.       switch (s->header.type)
  4774.         {
  4775.         case lang_output_section_statement_enum:
  4776.           {
  4777.             bfd_vma newdot, after;
  4778.             lang_output_section_statement_type *os;
  4779.             lang_memory_region_type *r;
  4780.             int section_alignment = 0;
  4781.  
  4782.             os = &s->output_section_statement;
  4783.             if (os->constraint == -1)
  4784.               break;
  4785.  
  4786.             /* FIXME: We shouldn't need to zero section vmas for ld -r
  4787.                here, in lang_insert_orphan, or in the default linker scripts.
  4788.                This is covering for coff backend linker bugs.  See PR6945.  */
  4789.             if (os->addr_tree == NULL
  4790.                 && link_info.relocatable
  4791.                 && (bfd_get_flavour (link_info.output_bfd)
  4792.                     == bfd_target_coff_flavour))
  4793.               os->addr_tree = exp_intop (0);
  4794.             if (os->addr_tree != NULL)
  4795.               {
  4796.                 os->processed_vma = FALSE;
  4797.                 exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
  4798.  
  4799.                 if (expld.result.valid_p)
  4800.                   {
  4801.                     dot = expld.result.value;
  4802.                     if (expld.result.section != NULL)
  4803.                       dot += expld.result.section->vma;
  4804.                   }
  4805.                 else if (expld.phase != lang_mark_phase_enum)
  4806.                   einfo (_("%F%S: non constant or forward reference"
  4807.                            " address expression for section %s\n"),
  4808.                          os->addr_tree, os->name);
  4809.               }
  4810.  
  4811.             if (os->bfd_section == NULL)
  4812.               /* This section was removed or never actually created.  */
  4813.               break;
  4814.  
  4815.             /* If this is a COFF shared library section, use the size and
  4816.                address from the input section.  FIXME: This is COFF
  4817.                specific; it would be cleaner if there were some other way
  4818.                to do this, but nothing simple comes to mind.  */
  4819.             if (((bfd_get_flavour (link_info.output_bfd)
  4820.                   == bfd_target_ecoff_flavour)
  4821.                  || (bfd_get_flavour (link_info.output_bfd)
  4822.                      == bfd_target_coff_flavour))
  4823.                 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
  4824.               {
  4825.                 asection *input;
  4826.  
  4827.                 if (os->children.head == NULL
  4828.                     || os->children.head->header.next != NULL
  4829.                     || (os->children.head->header.type
  4830.                         != lang_input_section_enum))
  4831.                   einfo (_("%P%X: Internal error on COFF shared library"
  4832.                            " section %s\n"), os->name);
  4833.  
  4834.                 input = os->children.head->input_section.section;
  4835.                 bfd_set_section_vma (os->bfd_section->owner,
  4836.                                      os->bfd_section,
  4837.                                      bfd_section_vma (input->owner, input));
  4838.                 os->bfd_section->size = input->size;
  4839.                 break;
  4840.               }
  4841.  
  4842.             newdot = dot;
  4843.             if (bfd_is_abs_section (os->bfd_section))
  4844.               {
  4845.                 /* No matter what happens, an abs section starts at zero.  */
  4846.                 ASSERT (os->bfd_section->vma == 0);
  4847.               }
  4848.             else
  4849.               {
  4850.                 if (os->addr_tree == NULL)
  4851.                   {
  4852.                     /* No address specified for this section, get one
  4853.                        from the region specification.  */
  4854.                     if (os->region == NULL
  4855.                         || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
  4856.                             && os->region->name_list.name[0] == '*'
  4857.                             && strcmp (os->region->name_list.name,
  4858.                                        DEFAULT_MEMORY_REGION) == 0))
  4859.                       {
  4860.                         os->region = lang_memory_default (os->bfd_section);
  4861.                       }
  4862.  
  4863.                     /* If a loadable section is using the default memory
  4864.                        region, and some non default memory regions were
  4865.                        defined, issue an error message.  */
  4866.                     if (!os->ignored
  4867.                         && !IGNORE_SECTION (os->bfd_section)
  4868.                         && ! link_info.relocatable
  4869.                         && check_regions
  4870.                         && strcmp (os->region->name_list.name,
  4871.                                    DEFAULT_MEMORY_REGION) == 0
  4872.                         && lang_memory_region_list != NULL
  4873.                         && (strcmp (lang_memory_region_list->name_list.name,
  4874.                                     DEFAULT_MEMORY_REGION) != 0
  4875.                             || lang_memory_region_list->next != NULL)
  4876.                         && expld.phase != lang_mark_phase_enum)
  4877.                       {
  4878.                         /* By default this is an error rather than just a
  4879.                            warning because if we allocate the section to the
  4880.                            default memory region we can end up creating an
  4881.                            excessively large binary, or even seg faulting when
  4882.                            attempting to perform a negative seek.  See
  4883.                            sources.redhat.com/ml/binutils/2003-04/msg00423.html
  4884.                            for an example of this.  This behaviour can be
  4885.                            overridden by the using the --no-check-sections
  4886.                            switch.  */
  4887.                         if (command_line.check_section_addresses)
  4888.                           einfo (_("%P%F: error: no memory region specified"
  4889.                                    " for loadable section `%s'\n"),
  4890.                                  bfd_get_section_name (link_info.output_bfd,
  4891.                                                        os->bfd_section));
  4892.                         else
  4893.                           einfo (_("%P: warning: no memory region specified"
  4894.                                    " for loadable section `%s'\n"),
  4895.                                  bfd_get_section_name (link_info.output_bfd,
  4896.                                                        os->bfd_section));
  4897.                       }
  4898.  
  4899.                     newdot = os->region->current;
  4900.                     section_alignment = os->bfd_section->alignment_power;
  4901.                   }
  4902.                 else
  4903.                   section_alignment = os->section_alignment;
  4904.  
  4905.                 /* Align to what the section needs.  */
  4906.                 if (section_alignment > 0)
  4907.                   {
  4908.                     bfd_vma savedot = newdot;
  4909.                     newdot = align_power (newdot, section_alignment);
  4910.  
  4911.                     if (newdot != savedot
  4912.                         && (config.warn_section_align
  4913.                             || os->addr_tree != NULL)
  4914.                         && expld.phase != lang_mark_phase_enum)
  4915.                       einfo (_("%P: warning: changing start of section"
  4916.                                " %s by %lu bytes\n"),
  4917.                              os->name, (unsigned long) (newdot - savedot));
  4918.                   }
  4919.  
  4920.                 bfd_set_section_vma (0, os->bfd_section, newdot);
  4921.  
  4922.                 os->bfd_section->output_offset = 0;
  4923.               }
  4924.  
  4925.             lang_size_sections_1 (&os->children.head, os,
  4926.                                   os->fill, newdot, relax, check_regions);
  4927.  
  4928.             os->processed_vma = TRUE;
  4929.  
  4930.             if (bfd_is_abs_section (os->bfd_section) || os->ignored)
  4931.               /* Except for some special linker created sections,
  4932.                  no output section should change from zero size
  4933.                  after strip_excluded_output_sections.  A non-zero
  4934.                  size on an ignored section indicates that some
  4935.                  input section was not sized early enough.  */
  4936.               ASSERT (os->bfd_section->size == 0);
  4937.             else
  4938.               {
  4939.                 dot = os->bfd_section->vma;
  4940.  
  4941.                 /* Put the section within the requested block size, or
  4942.                    align at the block boundary.  */
  4943.                 after = ((dot
  4944.                           + TO_ADDR (os->bfd_section->size)
  4945.                           + os->block_value - 1)
  4946.                          & - (bfd_vma) os->block_value);
  4947.  
  4948.                 os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
  4949.               }
  4950.  
  4951.             /* Set section lma.  */
  4952.             r = os->region;
  4953.             if (r == NULL)
  4954.               r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
  4955.  
  4956.             if (os->load_base)
  4957.               {
  4958.                 bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
  4959.                 os->bfd_section->lma = lma;
  4960.               }
  4961.             else if (os->lma_region != NULL)
  4962.               {
  4963.                 bfd_vma lma = os->lma_region->current;
  4964.  
  4965.                 /* When LMA_REGION is the same as REGION, align the LMA
  4966.                    as we did for the VMA, possibly including alignment
  4967.                    from the bfd section.  If a different region, then
  4968.                    only align according to the value in the output
  4969.                    statement unless specified otherwise.  */
  4970.                 if (os->lma_region != os->region && !os->align_lma_with_input)
  4971.                   section_alignment = os->section_alignment;
  4972.                 if (section_alignment > 0)
  4973.                   lma = align_power (lma, section_alignment);
  4974.                 os->bfd_section->lma = lma;
  4975.               }
  4976.             else if (r->last_os != NULL
  4977.                      && (os->bfd_section->flags & SEC_ALLOC) != 0)
  4978.               {
  4979.                 bfd_vma lma;
  4980.                 asection *last;
  4981.  
  4982.                 last = r->last_os->output_section_statement.bfd_section;
  4983.  
  4984.                 /* A backwards move of dot should be accompanied by
  4985.                    an explicit assignment to the section LMA (ie.
  4986.                    os->load_base set) because backwards moves can
  4987.                    create overlapping LMAs.  */
  4988.                 if (dot < last->vma
  4989.                     && os->bfd_section->size != 0
  4990.                     && dot + os->bfd_section->size <= last->vma)
  4991.                   {
  4992.                     /* If dot moved backwards then leave lma equal to
  4993.                        vma.  This is the old default lma, which might
  4994.                        just happen to work when the backwards move is
  4995.                        sufficiently large.  Nag if this changes anything,
  4996.                        so people can fix their linker scripts.  */
  4997.  
  4998.                     if (last->vma != last->lma)
  4999.                       einfo (_("%P: warning: dot moved backwards before `%s'\n"),
  5000.                              os->name);
  5001.                   }
  5002.                 else
  5003.                   {
  5004.                     /* If this is an overlay, set the current lma to that
  5005.                        at the end of the previous section.  */
  5006.                     if (os->sectype == overlay_section)
  5007.                       lma = last->lma + last->size;
  5008.  
  5009.                     /* Otherwise, keep the same lma to vma relationship
  5010.                        as the previous section.  */
  5011.                     else
  5012.                       lma = dot + last->lma - last->vma;
  5013.  
  5014.                     if (section_alignment > 0)
  5015.                       lma = align_power (lma, section_alignment);
  5016.                     os->bfd_section->lma = lma;
  5017.                   }
  5018.               }
  5019.             os->processed_lma = TRUE;
  5020.  
  5021.             if (bfd_is_abs_section (os->bfd_section) || os->ignored)
  5022.               break;
  5023.  
  5024.             /* Keep track of normal sections using the default
  5025.                lma region.  We use this to set the lma for
  5026.                following sections.  Overlays or other linker
  5027.                script assignment to lma might mean that the
  5028.                default lma == vma is incorrect.
  5029.                To avoid warnings about dot moving backwards when using
  5030.                -Ttext, don't start tracking sections until we find one
  5031.                of non-zero size or with lma set differently to vma.  */
  5032.             if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
  5033.                  || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0)
  5034.                 && (os->bfd_section->flags & SEC_ALLOC) != 0
  5035.                 && (os->bfd_section->size != 0
  5036.                     || (r->last_os == NULL
  5037.                         && os->bfd_section->vma != os->bfd_section->lma)
  5038.                     || (r->last_os != NULL
  5039.                         && dot >= (r->last_os->output_section_statement
  5040.                                    .bfd_section->vma)))
  5041.                 && os->lma_region == NULL
  5042.                 && !link_info.relocatable)
  5043.               r->last_os = s;
  5044.  
  5045.             /* .tbss sections effectively have zero size.  */
  5046.             if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
  5047.                 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
  5048.                 || link_info.relocatable)
  5049.               dot += TO_ADDR (os->bfd_section->size);
  5050.  
  5051.             if (os->update_dot_tree != 0)
  5052.               exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
  5053.  
  5054.             /* Update dot in the region ?
  5055.                We only do this if the section is going to be allocated,
  5056.                since unallocated sections do not contribute to the region's
  5057.                overall size in memory.  */
  5058.             if (os->region != NULL
  5059.                 && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)))
  5060.               {
  5061.                 os->region->current = dot;
  5062.  
  5063.                 if (check_regions)
  5064.                   /* Make sure the new address is within the region.  */
  5065.                   os_region_check (os, os->region, os->addr_tree,
  5066.                                    os->bfd_section->vma);
  5067.  
  5068.                 if (os->lma_region != NULL && os->lma_region != os->region
  5069.                     && (os->bfd_section->flags & SEC_LOAD))
  5070.                   {
  5071.                     os->lma_region->current
  5072.                       = os->bfd_section->lma + TO_ADDR (os->bfd_section->size);
  5073.  
  5074.                     if (check_regions)
  5075.                       os_region_check (os, os->lma_region, NULL,
  5076.                                        os->bfd_section->lma);
  5077.                   }
  5078.               }
  5079.           }
  5080.           break;
  5081.  
  5082.         case lang_constructors_statement_enum:
  5083.           dot = lang_size_sections_1 (&constructor_list.head,
  5084.                                       output_section_statement,
  5085.                                       fill, dot, relax, check_regions);
  5086.           break;
  5087.  
  5088.         case lang_data_statement_enum:
  5089.           {
  5090.             unsigned int size = 0;
  5091.  
  5092.             s->data_statement.output_offset =
  5093.               dot - output_section_statement->bfd_section->vma;
  5094.             s->data_statement.output_section =
  5095.               output_section_statement->bfd_section;
  5096.  
  5097.             /* We might refer to provided symbols in the expression, and
  5098.                need to mark them as needed.  */
  5099.             exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
  5100.  
  5101.             switch (s->data_statement.type)
  5102.               {
  5103.               default:
  5104.                 abort ();
  5105.               case QUAD:
  5106.               case SQUAD:
  5107.                 size = QUAD_SIZE;
  5108.                 break;
  5109.               case LONG:
  5110.                 size = LONG_SIZE;
  5111.                 break;
  5112.               case SHORT:
  5113.                 size = SHORT_SIZE;
  5114.                 break;
  5115.               case BYTE:
  5116.                 size = BYTE_SIZE;
  5117.                 break;
  5118.               }
  5119.             if (size < TO_SIZE ((unsigned) 1))
  5120.               size = TO_SIZE ((unsigned) 1);
  5121.             dot += TO_ADDR (size);
  5122.             output_section_statement->bfd_section->size
  5123.               = TO_SIZE (dot - output_section_statement->bfd_section->vma);
  5124.  
  5125.           }
  5126.           break;
  5127.  
  5128.         case lang_reloc_statement_enum:
  5129.           {
  5130.             int size;
  5131.  
  5132.             s->reloc_statement.output_offset =
  5133.               dot - output_section_statement->bfd_section->vma;
  5134.             s->reloc_statement.output_section =
  5135.               output_section_statement->bfd_section;
  5136.             size = bfd_get_reloc_size (s->reloc_statement.howto);
  5137.             dot += TO_ADDR (size);
  5138.             output_section_statement->bfd_section->size
  5139.               = TO_SIZE (dot - output_section_statement->bfd_section->vma);
  5140.           }
  5141.           break;
  5142.  
  5143.         case lang_wild_statement_enum:
  5144.           dot = lang_size_sections_1 (&s->wild_statement.children.head,
  5145.                                       output_section_statement,
  5146.                                       fill, dot, relax, check_regions);
  5147.           break;
  5148.  
  5149.         case lang_object_symbols_statement_enum:
  5150.           link_info.create_object_symbols_section =
  5151.             output_section_statement->bfd_section;
  5152.           break;
  5153.  
  5154.         case lang_output_statement_enum:
  5155.         case lang_target_statement_enum:
  5156.           break;
  5157.  
  5158.         case lang_input_section_enum:
  5159.           {
  5160.             asection *i;
  5161.  
  5162.             i = s->input_section.section;
  5163.             if (relax)
  5164.               {
  5165.                 bfd_boolean again;
  5166.  
  5167.                 if (! bfd_relax_section (i->owner, i, &link_info, &again))
  5168.                   einfo (_("%P%F: can't relax section: %E\n"));
  5169.                 if (again)
  5170.                   *relax = TRUE;
  5171.               }
  5172.             dot = size_input_section (prev, output_section_statement,
  5173.                                       output_section_statement->fill, dot);
  5174.           }
  5175.           break;
  5176.  
  5177.         case lang_input_statement_enum:
  5178.           break;
  5179.  
  5180.         case lang_fill_statement_enum:
  5181.           s->fill_statement.output_section =
  5182.             output_section_statement->bfd_section;
  5183.  
  5184.           fill = s->fill_statement.fill;
  5185.           break;
  5186.  
  5187.         case lang_assignment_statement_enum:
  5188.           {
  5189.             bfd_vma newdot = dot;
  5190.             etree_type *tree = s->assignment_statement.exp;
  5191.  
  5192.             expld.dataseg.relro = exp_dataseg_relro_none;
  5193.  
  5194.             exp_fold_tree (tree,
  5195.                            output_section_statement->bfd_section,
  5196.                            &newdot);
  5197.  
  5198.             if (expld.dataseg.relro == exp_dataseg_relro_start)
  5199.               {
  5200.                 if (!expld.dataseg.relro_start_stat)
  5201.                   expld.dataseg.relro_start_stat = s;
  5202.                 else
  5203.                   {
  5204.                     ASSERT (expld.dataseg.relro_start_stat == s);
  5205.                   }
  5206.               }
  5207.             else if (expld.dataseg.relro == exp_dataseg_relro_end)
  5208.               {
  5209.                 if (!expld.dataseg.relro_end_stat)
  5210.                   expld.dataseg.relro_end_stat = s;
  5211.                 else
  5212.                   {
  5213.                     ASSERT (expld.dataseg.relro_end_stat == s);
  5214.                   }
  5215.               }
  5216.             expld.dataseg.relro = exp_dataseg_relro_none;
  5217.  
  5218.             /* This symbol may be relative to this section.  */
  5219.             if ((tree->type.node_class == etree_provided
  5220.                  || tree->type.node_class == etree_assign)
  5221.                 && (tree->assign.dst [0] != '.'
  5222.                     || tree->assign.dst [1] != '\0'))
  5223.               output_section_statement->update_dot = 1;
  5224.  
  5225.             if (!output_section_statement->ignored)
  5226.               {
  5227.                 if (output_section_statement == abs_output_section)
  5228.                   {
  5229.                     /* If we don't have an output section, then just adjust
  5230.                        the default memory address.  */
  5231.                     lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
  5232.                                                FALSE)->current = newdot;
  5233.                   }
  5234.                 else if (newdot != dot)
  5235.                   {
  5236.                     /* Insert a pad after this statement.  We can't
  5237.                        put the pad before when relaxing, in case the
  5238.                        assignment references dot.  */
  5239.                     insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
  5240.                                 output_section_statement->bfd_section, dot);
  5241.  
  5242.                     /* Don't neuter the pad below when relaxing.  */
  5243.                     s = s->header.next;
  5244.  
  5245.                     /* If dot is advanced, this implies that the section
  5246.                        should have space allocated to it, unless the
  5247.                        user has explicitly stated that the section
  5248.                        should not be allocated.  */
  5249.                     if (output_section_statement->sectype != noalloc_section
  5250.                         && (output_section_statement->sectype != noload_section
  5251.                             || (bfd_get_flavour (link_info.output_bfd)
  5252.                                 == bfd_target_elf_flavour)))
  5253.                       output_section_statement->bfd_section->flags |= SEC_ALLOC;
  5254.                   }
  5255.                 dot = newdot;
  5256.               }
  5257.           }
  5258.           break;
  5259.  
  5260.         case lang_padding_statement_enum:
  5261.           /* If this is the first time lang_size_sections is called,
  5262.              we won't have any padding statements.  If this is the
  5263.              second or later passes when relaxing, we should allow
  5264.              padding to shrink.  If padding is needed on this pass, it
  5265.              will be added back in.  */
  5266.           s->padding_statement.size = 0;
  5267.  
  5268.           /* Make sure output_offset is valid.  If relaxation shrinks
  5269.              the section and this pad isn't needed, it's possible to
  5270.              have output_offset larger than the final size of the
  5271.              section.  bfd_set_section_contents will complain even for
  5272.              a pad size of zero.  */
  5273.           s->padding_statement.output_offset
  5274.             = dot - output_section_statement->bfd_section->vma;
  5275.           break;
  5276.  
  5277.         case lang_group_statement_enum:
  5278.           dot = lang_size_sections_1 (&s->group_statement.children.head,
  5279.                                       output_section_statement,
  5280.                                       fill, dot, relax, check_regions);
  5281.           break;
  5282.  
  5283.         case lang_insert_statement_enum:
  5284.           break;
  5285.  
  5286.           /* We can only get here when relaxing is turned on.  */
  5287.         case lang_address_statement_enum:
  5288.           break;
  5289.  
  5290.         default:
  5291.           FAIL ();
  5292.           break;
  5293.         }
  5294.       prev = &s->header.next;
  5295.     }
  5296.   return dot;
  5297. }
  5298.  
  5299. /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
  5300.    The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
  5301.    CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
  5302.    segments.  We are allowed an opportunity to override this decision.  */
  5303.  
  5304. bfd_boolean
  5305. ldlang_override_segment_assignment (struct bfd_link_info * info ATTRIBUTE_UNUSED,
  5306.                                     bfd * abfd ATTRIBUTE_UNUSED,
  5307.                                     asection * current_section,
  5308.                                     asection * previous_section,
  5309.                                     bfd_boolean new_segment)
  5310. {
  5311.   lang_output_section_statement_type * cur;
  5312.   lang_output_section_statement_type * prev;
  5313.  
  5314.   /* The checks below are only necessary when the BFD library has decided
  5315.      that the two sections ought to be placed into the same segment.  */
  5316.   if (new_segment)
  5317.     return TRUE;
  5318.  
  5319.   /* Paranoia checks.  */
  5320.   if (current_section == NULL || previous_section == NULL)
  5321.     return new_segment;
  5322.  
  5323.   /* If this flag is set, the target never wants code and non-code
  5324.      sections comingled in the same segment.  */
  5325.   if (config.separate_code
  5326.       && ((current_section->flags ^ previous_section->flags) & SEC_CODE))
  5327.     return TRUE;
  5328.  
  5329.   /* Find the memory regions associated with the two sections.
  5330.      We call lang_output_section_find() here rather than scanning the list
  5331.      of output sections looking for a matching section pointer because if
  5332.      we have a large number of sections then a hash lookup is faster.  */
  5333.   cur  = lang_output_section_find (current_section->name);
  5334.   prev = lang_output_section_find (previous_section->name);
  5335.  
  5336.   /* More paranoia.  */
  5337.   if (cur == NULL || prev == NULL)
  5338.     return new_segment;
  5339.  
  5340.   /* If the regions are different then force the sections to live in
  5341.      different segments.  See the email thread starting at the following
  5342.      URL for the reasons why this is necessary:
  5343.      http://sourceware.org/ml/binutils/2007-02/msg00216.html  */
  5344.   return cur->region != prev->region;
  5345. }
  5346.  
  5347. void
  5348. one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
  5349. {
  5350.   lang_statement_iteration++;
  5351.   lang_size_sections_1 (&statement_list.head, abs_output_section,
  5352.                         0, 0, relax, check_regions);
  5353. }
  5354.  
  5355. void
  5356. lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
  5357. {
  5358.   expld.phase = lang_allocating_phase_enum;
  5359.   expld.dataseg.phase = exp_dataseg_none;
  5360.  
  5361.   one_lang_size_sections_pass (relax, check_regions);
  5362.   if (expld.dataseg.phase == exp_dataseg_end_seen
  5363.       && link_info.relro && expld.dataseg.relro_end)
  5364.     {
  5365.       /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
  5366.          to put expld.dataseg.relro on a (common) page boundary.  */
  5367.       bfd_vma min_base, old_base, relro_end, maxpage;
  5368.  
  5369.       expld.dataseg.phase = exp_dataseg_relro_adjust;
  5370.       maxpage = expld.dataseg.maxpagesize;
  5371.       /* MIN_BASE is the absolute minimum address we are allowed to start the
  5372.          read-write segment (byte before will be mapped read-only).  */
  5373.       min_base = (expld.dataseg.min_base + maxpage - 1) & ~(maxpage - 1);
  5374.       /* OLD_BASE is the address for a feasible minimum address which will
  5375.          still not cause a data overlap inside MAXPAGE causing file offset skip
  5376.          by MAXPAGE.  */
  5377.       old_base = expld.dataseg.base;
  5378.       expld.dataseg.base += (-expld.dataseg.relro_end
  5379.                              & (expld.dataseg.pagesize - 1));
  5380.       /* Compute the expected PT_GNU_RELRO segment end.  */
  5381.       relro_end = ((expld.dataseg.relro_end + expld.dataseg.pagesize - 1)
  5382.                    & ~(expld.dataseg.pagesize - 1));
  5383.       if (min_base + maxpage < expld.dataseg.base)
  5384.         {
  5385.           expld.dataseg.base -= maxpage;
  5386.           relro_end -= maxpage;
  5387.         }
  5388.       lang_reset_memory_regions ();
  5389.       one_lang_size_sections_pass (relax, check_regions);
  5390.       if (expld.dataseg.relro_end > relro_end)
  5391.         {
  5392.           /* The alignment of sections between DATA_SEGMENT_ALIGN
  5393.              and DATA_SEGMENT_RELRO_END caused huge padding to be
  5394.              inserted at DATA_SEGMENT_RELRO_END.  Try to start a bit lower so
  5395.              that the section alignments will fit in.  */
  5396.           asection *sec;
  5397.           unsigned int max_alignment_power = 0;
  5398.  
  5399.           /* Find maximum alignment power of sections between
  5400.              DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END.  */
  5401.           for (sec = link_info.output_bfd->sections; sec; sec = sec->next)
  5402.             if (sec->vma >= expld.dataseg.base
  5403.                 && sec->vma < expld.dataseg.relro_end
  5404.                 && sec->alignment_power > max_alignment_power)
  5405.               max_alignment_power = sec->alignment_power;
  5406.  
  5407.           if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize)
  5408.             {
  5409.               if (expld.dataseg.base - (1 << max_alignment_power) < old_base)
  5410.                 expld.dataseg.base += expld.dataseg.pagesize;
  5411.               expld.dataseg.base -= (1 << max_alignment_power);
  5412.               lang_reset_memory_regions ();
  5413.               one_lang_size_sections_pass (relax, check_regions);
  5414.             }
  5415.         }
  5416.       link_info.relro_start = expld.dataseg.base;
  5417.       link_info.relro_end = expld.dataseg.relro_end;
  5418.     }
  5419.   else if (expld.dataseg.phase == exp_dataseg_end_seen)
  5420.     {
  5421.       /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
  5422.          a page could be saved in the data segment.  */
  5423.       bfd_vma first, last;
  5424.  
  5425.       first = -expld.dataseg.base & (expld.dataseg.pagesize - 1);
  5426.       last = expld.dataseg.end & (expld.dataseg.pagesize - 1);
  5427.       if (first && last
  5428.           && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1))
  5429.               != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1)))
  5430.           && first + last <= expld.dataseg.pagesize)
  5431.         {
  5432.           expld.dataseg.phase = exp_dataseg_adjust;
  5433.           lang_reset_memory_regions ();
  5434.           one_lang_size_sections_pass (relax, check_regions);
  5435.         }
  5436.       else
  5437.         expld.dataseg.phase = exp_dataseg_done;
  5438.     }
  5439.   else
  5440.     expld.dataseg.phase = exp_dataseg_done;
  5441. }
  5442.  
  5443. static lang_output_section_statement_type *current_section;
  5444. static lang_assignment_statement_type *current_assign;
  5445. static bfd_boolean prefer_next_section;
  5446.  
  5447. /* Worker function for lang_do_assignments.  Recursiveness goes here.  */
  5448.  
  5449. static bfd_vma
  5450. lang_do_assignments_1 (lang_statement_union_type *s,
  5451.                        lang_output_section_statement_type *current_os,
  5452.                        fill_type *fill,
  5453.                        bfd_vma dot,
  5454.                        bfd_boolean *found_end)
  5455. {
  5456.   for (; s != NULL; s = s->header.next)
  5457.     {
  5458.       switch (s->header.type)
  5459.         {
  5460.         case lang_constructors_statement_enum:
  5461.           dot = lang_do_assignments_1 (constructor_list.head,
  5462.                                        current_os, fill, dot, found_end);
  5463.           break;
  5464.  
  5465.         case lang_output_section_statement_enum:
  5466.           {
  5467.             lang_output_section_statement_type *os;
  5468.  
  5469.             os = &(s->output_section_statement);
  5470.             os->after_end = *found_end;
  5471.             if (os->bfd_section != NULL && !os->ignored)
  5472.               {
  5473.                 if ((os->bfd_section->flags & SEC_ALLOC) != 0)
  5474.                   {
  5475.                     current_section = os;
  5476.                     prefer_next_section = FALSE;
  5477.                   }
  5478.                 dot = os->bfd_section->vma;
  5479.  
  5480.                 lang_do_assignments_1 (os->children.head,
  5481.                                        os, os->fill, dot, found_end);
  5482.  
  5483.                 /* .tbss sections effectively have zero size.  */
  5484.                 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
  5485.                     || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
  5486.                     || link_info.relocatable)
  5487.                   dot += TO_ADDR (os->bfd_section->size);
  5488.  
  5489.                 if (os->update_dot_tree != NULL)
  5490.                   exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
  5491.               }
  5492.           }
  5493.           break;
  5494.  
  5495.         case lang_wild_statement_enum:
  5496.  
  5497.           dot = lang_do_assignments_1 (s->wild_statement.children.head,
  5498.                                        current_os, fill, dot, found_end);
  5499.           break;
  5500.  
  5501.         case lang_object_symbols_statement_enum:
  5502.         case lang_output_statement_enum:
  5503.         case lang_target_statement_enum:
  5504.           break;
  5505.  
  5506.         case lang_data_statement_enum:
  5507.           exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
  5508.           if (expld.result.valid_p)
  5509.             {
  5510.               s->data_statement.value = expld.result.value;
  5511.               if (expld.result.section != NULL)
  5512.                 s->data_statement.value += expld.result.section->vma;
  5513.             }
  5514.           else
  5515.             einfo (_("%F%P: invalid data statement\n"));
  5516.           {
  5517.             unsigned int size;
  5518.             switch (s->data_statement.type)
  5519.               {
  5520.               default:
  5521.                 abort ();
  5522.               case QUAD:
  5523.               case SQUAD:
  5524.                 size = QUAD_SIZE;
  5525.                 break;
  5526.               case LONG:
  5527.                 size = LONG_SIZE;
  5528.                 break;
  5529.               case SHORT:
  5530.                 size = SHORT_SIZE;
  5531.                 break;
  5532.               case BYTE:
  5533.                 size = BYTE_SIZE;
  5534.                 break;
  5535.               }
  5536.             if (size < TO_SIZE ((unsigned) 1))
  5537.               size = TO_SIZE ((unsigned) 1);
  5538.             dot += TO_ADDR (size);
  5539.           }
  5540.           break;
  5541.  
  5542.         case lang_reloc_statement_enum:
  5543.           exp_fold_tree (s->reloc_statement.addend_exp,
  5544.                          bfd_abs_section_ptr, &dot);
  5545.           if (expld.result.valid_p)
  5546.             s->reloc_statement.addend_value = expld.result.value;
  5547.           else
  5548.             einfo (_("%F%P: invalid reloc statement\n"));
  5549.           dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
  5550.           break;
  5551.  
  5552.         case lang_input_section_enum:
  5553.           {
  5554.             asection *in = s->input_section.section;
  5555.  
  5556.             if ((in->flags & SEC_EXCLUDE) == 0)
  5557.               dot += TO_ADDR (in->size);
  5558.           }
  5559.           break;
  5560.  
  5561.         case lang_input_statement_enum:
  5562.           break;
  5563.  
  5564.         case lang_fill_statement_enum:
  5565.           fill = s->fill_statement.fill;
  5566.           break;
  5567.  
  5568.         case lang_assignment_statement_enum:
  5569.           current_assign = &s->assignment_statement;
  5570.           if (current_assign->exp->type.node_class != etree_assert)
  5571.             {
  5572.               const char *p = current_assign->exp->assign.dst;
  5573.  
  5574.               if (current_os == abs_output_section && p[0] == '.' && p[1] == 0)
  5575.                 prefer_next_section = TRUE;
  5576.  
  5577.               while (*p == '_')
  5578.                 ++p;
  5579.               if (strcmp (p, "end") == 0)
  5580.                 *found_end = TRUE;
  5581.             }
  5582.           exp_fold_tree (s->assignment_statement.exp,
  5583.                          current_os->bfd_section,
  5584.                          &dot);
  5585.           break;
  5586.  
  5587.         case lang_padding_statement_enum:
  5588.           dot += TO_ADDR (s->padding_statement.size);
  5589.           break;
  5590.  
  5591.         case lang_group_statement_enum:
  5592.           dot = lang_do_assignments_1 (s->group_statement.children.head,
  5593.                                        current_os, fill, dot, found_end);
  5594.           break;
  5595.  
  5596.         case lang_insert_statement_enum:
  5597.           break;
  5598.  
  5599.         case lang_address_statement_enum:
  5600.           break;
  5601.  
  5602.         default:
  5603.           FAIL ();
  5604.           break;
  5605.         }
  5606.     }
  5607.   return dot;
  5608. }
  5609.  
  5610. void
  5611. lang_do_assignments (lang_phase_type phase)
  5612. {
  5613.   bfd_boolean found_end = FALSE;
  5614.  
  5615.   current_section = NULL;
  5616.   prefer_next_section = FALSE;
  5617.   expld.phase = phase;
  5618.   lang_statement_iteration++;
  5619.   lang_do_assignments_1 (statement_list.head,
  5620.                          abs_output_section, NULL, 0, &found_end);
  5621. }
  5622.  
  5623. /* For an assignment statement outside of an output section statement,
  5624.    choose the best of neighbouring output sections to use for values
  5625.    of "dot".  */
  5626.  
  5627. asection *
  5628. section_for_dot (void)
  5629. {
  5630.   asection *s;
  5631.  
  5632.   /* Assignments belong to the previous output section, unless there
  5633.      has been an assignment to "dot", in which case following
  5634.      assignments belong to the next output section.  (The assumption
  5635.      is that an assignment to "dot" is setting up the address for the
  5636.      next output section.)  Except that past the assignment to "_end"
  5637.      we always associate with the previous section.  This exception is
  5638.      for targets like SH that define an alloc .stack or other
  5639.      weirdness after non-alloc sections.  */
  5640.   if (current_section == NULL || prefer_next_section)
  5641.     {
  5642.       lang_statement_union_type *stmt;
  5643.       lang_output_section_statement_type *os;
  5644.  
  5645.       for (stmt = (lang_statement_union_type *) current_assign;
  5646.            stmt != NULL;
  5647.            stmt = stmt->header.next)
  5648.         if (stmt->header.type == lang_output_section_statement_enum)
  5649.           break;
  5650.  
  5651.       os = &stmt->output_section_statement;
  5652.       while (os != NULL
  5653.              && !os->after_end
  5654.              && (os->bfd_section == NULL
  5655.                  || (os->bfd_section->flags & SEC_EXCLUDE) != 0
  5656.                  || bfd_section_removed_from_list (link_info.output_bfd,
  5657.                                                    os->bfd_section)))
  5658.         os = os->next;
  5659.  
  5660.       if (current_section == NULL || os == NULL || !os->after_end)
  5661.         {
  5662.           if (os != NULL)
  5663.             s = os->bfd_section;
  5664.           else
  5665.             s = link_info.output_bfd->section_last;
  5666.           while (s != NULL
  5667.                  && ((s->flags & SEC_ALLOC) == 0
  5668.                      || (s->flags & SEC_THREAD_LOCAL) != 0))
  5669.             s = s->prev;
  5670.           if (s != NULL)
  5671.             return s;
  5672.  
  5673.           return bfd_abs_section_ptr;
  5674.         }
  5675.     }
  5676.  
  5677.   s = current_section->bfd_section;
  5678.  
  5679.   /* The section may have been stripped.  */
  5680.   while (s != NULL
  5681.          && ((s->flags & SEC_EXCLUDE) != 0
  5682.              || (s->flags & SEC_ALLOC) == 0
  5683.              || (s->flags & SEC_THREAD_LOCAL) != 0
  5684.              || bfd_section_removed_from_list (link_info.output_bfd, s)))
  5685.     s = s->prev;
  5686.   if (s == NULL)
  5687.     s = link_info.output_bfd->sections;
  5688.   while (s != NULL
  5689.          && ((s->flags & SEC_ALLOC) == 0
  5690.              || (s->flags & SEC_THREAD_LOCAL) != 0))
  5691.     s = s->next;
  5692.   if (s != NULL)
  5693.     return s;
  5694.  
  5695.   return bfd_abs_section_ptr;
  5696. }
  5697.  
  5698. /* Fix any .startof. or .sizeof. symbols.  When the assemblers see the
  5699.    operator .startof. (section_name), it produces an undefined symbol
  5700.    .startof.section_name.  Similarly, when it sees
  5701.    .sizeof. (section_name), it produces an undefined symbol
  5702.    .sizeof.section_name.  For all the output sections, we look for
  5703.    such symbols, and set them to the correct value.  */
  5704.  
  5705. static void
  5706. lang_set_startof (void)
  5707. {
  5708.   asection *s;
  5709.  
  5710.   if (link_info.relocatable)
  5711.     return;
  5712.  
  5713.   for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
  5714.     {
  5715.       const char *secname;
  5716.       char *buf;
  5717.       struct bfd_link_hash_entry *h;
  5718.  
  5719.       secname = bfd_get_section_name (link_info.output_bfd, s);
  5720.       buf = (char *) xmalloc (10 + strlen (secname));
  5721.  
  5722.       sprintf (buf, ".startof.%s", secname);
  5723.       h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
  5724.       if (h != NULL && h->type == bfd_link_hash_undefined)
  5725.         {
  5726.           h->type = bfd_link_hash_defined;
  5727.           h->u.def.value = 0;
  5728.           h->u.def.section = s;
  5729.         }
  5730.  
  5731.       sprintf (buf, ".sizeof.%s", secname);
  5732.       h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
  5733.       if (h != NULL && h->type == bfd_link_hash_undefined)
  5734.         {
  5735.           h->type = bfd_link_hash_defined;
  5736.           h->u.def.value = TO_ADDR (s->size);
  5737.           h->u.def.section = bfd_abs_section_ptr;
  5738.         }
  5739.  
  5740.       free (buf);
  5741.     }
  5742. }
  5743.  
  5744. static void
  5745. lang_end (void)
  5746. {
  5747.   struct bfd_link_hash_entry *h;
  5748.   bfd_boolean warn;
  5749.  
  5750.   if ((link_info.relocatable && !link_info.gc_sections)
  5751.       || (link_info.shared && !link_info.executable))
  5752.     warn = entry_from_cmdline;
  5753.   else
  5754.     warn = TRUE;
  5755.  
  5756.   /* Force the user to specify a root when generating a relocatable with
  5757.      --gc-sections.  */
  5758.   if (link_info.gc_sections && link_info.relocatable
  5759.       && !(entry_from_cmdline || undef_from_cmdline))
  5760.     einfo (_("%P%F: gc-sections requires either an entry or "
  5761.              "an undefined symbol\n"));
  5762.  
  5763.   if (entry_symbol.name == NULL)
  5764.     {
  5765.       /* No entry has been specified.  Look for the default entry, but
  5766.          don't warn if we don't find it.  */
  5767.       entry_symbol.name = entry_symbol_default;
  5768.       warn = FALSE;
  5769.     }
  5770.  
  5771.   h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
  5772.                             FALSE, FALSE, TRUE);
  5773.   if (h != NULL
  5774.       && (h->type == bfd_link_hash_defined
  5775.           || h->type == bfd_link_hash_defweak)
  5776.       && h->u.def.section->output_section != NULL)
  5777.     {
  5778.       bfd_vma val;
  5779.  
  5780.       val = (h->u.def.value
  5781.              + bfd_get_section_vma (link_info.output_bfd,
  5782.                                     h->u.def.section->output_section)
  5783.              + h->u.def.section->output_offset);
  5784.       if (! bfd_set_start_address (link_info.output_bfd, val))
  5785.         einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
  5786.     }
  5787.   else
  5788.     {
  5789.       bfd_vma val;
  5790.       const char *send;
  5791.  
  5792.       /* We couldn't find the entry symbol.  Try parsing it as a
  5793.          number.  */
  5794.       val = bfd_scan_vma (entry_symbol.name, &send, 0);
  5795.       if (*send == '\0')
  5796.         {
  5797.           if (! bfd_set_start_address (link_info.output_bfd, val))
  5798.             einfo (_("%P%F: can't set start address\n"));
  5799.         }
  5800.       else
  5801.         {
  5802.           asection *ts;
  5803.  
  5804.           /* Can't find the entry symbol, and it's not a number.  Use
  5805.              the first address in the text section.  */
  5806.           ts = bfd_get_section_by_name (link_info.output_bfd, entry_section);
  5807.           if (ts != NULL)
  5808.             {
  5809.               if (warn)
  5810.                 einfo (_("%P: warning: cannot find entry symbol %s;"
  5811.                          " defaulting to %V\n"),
  5812.                        entry_symbol.name,
  5813.                        bfd_get_section_vma (link_info.output_bfd, ts));
  5814.               if (!(bfd_set_start_address
  5815.                     (link_info.output_bfd,
  5816.                      bfd_get_section_vma (link_info.output_bfd, ts))))
  5817.                 einfo (_("%P%F: can't set start address\n"));
  5818.             }
  5819.           else
  5820.             {
  5821.               if (warn)
  5822.                 einfo (_("%P: warning: cannot find entry symbol %s;"
  5823.                          " not setting start address\n"),
  5824.                        entry_symbol.name);
  5825.             }
  5826.         }
  5827.     }
  5828. }
  5829.  
  5830. /* This is a small function used when we want to ignore errors from
  5831.    BFD.  */
  5832.  
  5833. static void
  5834. ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
  5835. {
  5836.   /* Don't do anything.  */
  5837. }
  5838.  
  5839. /* Check that the architecture of all the input files is compatible
  5840.    with the output file.  Also call the backend to let it do any
  5841.    other checking that is needed.  */
  5842.  
  5843. static void
  5844. lang_check (void)
  5845. {
  5846.   lang_statement_union_type *file;
  5847.   bfd *input_bfd;
  5848.   const bfd_arch_info_type *compatible;
  5849.  
  5850.   for (file = file_chain.head; file != NULL; file = file->input_statement.next)
  5851.     {
  5852. #ifdef ENABLE_PLUGINS
  5853.       /* Don't check format of files claimed by plugin.  */
  5854.       if (file->input_statement.flags.claimed)
  5855.         continue;
  5856. #endif /* ENABLE_PLUGINS */
  5857.       input_bfd = file->input_statement.the_bfd;
  5858.       compatible
  5859.         = bfd_arch_get_compatible (input_bfd, link_info.output_bfd,
  5860.                                    command_line.accept_unknown_input_arch);
  5861.  
  5862.       /* In general it is not possible to perform a relocatable
  5863.          link between differing object formats when the input
  5864.          file has relocations, because the relocations in the
  5865.          input format may not have equivalent representations in
  5866.          the output format (and besides BFD does not translate
  5867.          relocs for other link purposes than a final link).  */
  5868.       if ((link_info.relocatable || link_info.emitrelocations)
  5869.           && (compatible == NULL
  5870.               || (bfd_get_flavour (input_bfd)
  5871.                   != bfd_get_flavour (link_info.output_bfd)))
  5872.           && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
  5873.         {
  5874.           einfo (_("%P%F: Relocatable linking with relocations from"
  5875.                    " format %s (%B) to format %s (%B) is not supported\n"),
  5876.                  bfd_get_target (input_bfd), input_bfd,
  5877.                  bfd_get_target (link_info.output_bfd), link_info.output_bfd);
  5878.           /* einfo with %F exits.  */
  5879.         }
  5880.  
  5881.       if (compatible == NULL)
  5882.         {
  5883.           if (command_line.warn_mismatch)
  5884.             einfo (_("%P%X: %s architecture of input file `%B'"
  5885.                      " is incompatible with %s output\n"),
  5886.                    bfd_printable_name (input_bfd), input_bfd,
  5887.                    bfd_printable_name (link_info.output_bfd));
  5888.         }
  5889.       else if (bfd_count_sections (input_bfd))
  5890.         {
  5891.           /* If the input bfd has no contents, it shouldn't set the
  5892.              private data of the output bfd.  */
  5893.  
  5894.           bfd_error_handler_type pfn = NULL;
  5895.  
  5896.           /* If we aren't supposed to warn about mismatched input
  5897.              files, temporarily set the BFD error handler to a
  5898.              function which will do nothing.  We still want to call
  5899.              bfd_merge_private_bfd_data, since it may set up
  5900.              information which is needed in the output file.  */
  5901.           if (! command_line.warn_mismatch)
  5902.             pfn = bfd_set_error_handler (ignore_bfd_errors);
  5903.           if (! bfd_merge_private_bfd_data (input_bfd, link_info.output_bfd))
  5904.             {
  5905.               if (command_line.warn_mismatch)
  5906.                 einfo (_("%P%X: failed to merge target specific data"
  5907.                          " of file %B\n"), input_bfd);
  5908.             }
  5909.           if (! command_line.warn_mismatch)
  5910.             bfd_set_error_handler (pfn);
  5911.         }
  5912.     }
  5913. }
  5914.  
  5915. /* Look through all the global common symbols and attach them to the
  5916.    correct section.  The -sort-common command line switch may be used
  5917.    to roughly sort the entries by alignment.  */
  5918.  
  5919. static void
  5920. lang_common (void)
  5921. {
  5922.   if (command_line.inhibit_common_definition)
  5923.     return;
  5924.   if (link_info.relocatable
  5925.       && ! command_line.force_common_definition)
  5926.     return;
  5927.  
  5928.   if (! config.sort_common)
  5929.     bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
  5930.   else
  5931.     {
  5932.       unsigned int power;
  5933.  
  5934.       if (config.sort_common == sort_descending)
  5935.         {
  5936.           for (power = 4; power > 0; power--)
  5937.             bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
  5938.  
  5939.           power = 0;
  5940.           bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
  5941.         }
  5942.       else
  5943.         {
  5944.           for (power = 0; power <= 4; power++)
  5945.             bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
  5946.  
  5947.           power = UINT_MAX;
  5948.           bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
  5949.         }
  5950.     }
  5951. }
  5952.  
  5953. /* Place one common symbol in the correct section.  */
  5954.  
  5955. static bfd_boolean
  5956. lang_one_common (struct bfd_link_hash_entry *h, void *info)
  5957. {
  5958.   unsigned int power_of_two;
  5959.   bfd_vma size;
  5960.   asection *section;
  5961.  
  5962.   if (h->type != bfd_link_hash_common)
  5963.     return TRUE;
  5964.  
  5965.   size = h->u.c.size;
  5966.   power_of_two = h->u.c.p->alignment_power;
  5967.  
  5968.   if (config.sort_common == sort_descending
  5969.       && power_of_two < *(unsigned int *) info)
  5970.     return TRUE;
  5971.   else if (config.sort_common == sort_ascending
  5972.            && power_of_two > *(unsigned int *) info)
  5973.     return TRUE;
  5974.  
  5975.   section = h->u.c.p->section;
  5976.   if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h))
  5977.     einfo (_("%P%F: Could not define common symbol `%T': %E\n"),
  5978.            h->root.string);
  5979.  
  5980.   if (config.map_file != NULL)
  5981.     {
  5982.       static bfd_boolean header_printed;
  5983.       int len;
  5984.       char *name;
  5985.       char buf[50];
  5986.  
  5987.       if (! header_printed)
  5988.         {
  5989.           minfo (_("\nAllocating common symbols\n"));
  5990.           minfo (_("Common symbol       size              file\n\n"));
  5991.           header_printed = TRUE;
  5992.         }
  5993.  
  5994.       name = bfd_demangle (link_info.output_bfd, h->root.string,
  5995.                            DMGL_ANSI | DMGL_PARAMS);
  5996.       if (name == NULL)
  5997.         {
  5998.           minfo ("%s", h->root.string);
  5999.           len = strlen (h->root.string);
  6000.         }
  6001.       else
  6002.         {
  6003.           minfo ("%s", name);
  6004.           len = strlen (name);
  6005.           free (name);
  6006.         }
  6007.  
  6008.       if (len >= 19)
  6009.         {
  6010.           print_nl ();
  6011.           len = 0;
  6012.         }
  6013.       while (len < 20)
  6014.         {
  6015.           print_space ();
  6016.           ++len;
  6017.         }
  6018.  
  6019.       minfo ("0x");
  6020.       if (size <= 0xffffffff)
  6021.         sprintf (buf, "%lx", (unsigned long) size);
  6022.       else
  6023.         sprintf_vma (buf, size);
  6024.       minfo ("%s", buf);
  6025.       len = strlen (buf);
  6026.  
  6027.       while (len < 16)
  6028.         {
  6029.           print_space ();
  6030.           ++len;
  6031.         }
  6032.  
  6033.       minfo ("%B\n", section->owner);
  6034.     }
  6035.  
  6036.   return TRUE;
  6037. }
  6038.  
  6039. /* Run through the input files and ensure that every input section has
  6040.    somewhere to go.  If one is found without a destination then create
  6041.    an input request and place it into the statement tree.  */
  6042.  
  6043. static void
  6044. lang_place_orphans (void)
  6045. {
  6046.   LANG_FOR_EACH_INPUT_STATEMENT (file)
  6047.     {
  6048.       asection *s;
  6049.  
  6050.       for (s = file->the_bfd->sections; s != NULL; s = s->next)
  6051.         {
  6052.           if (s->output_section == NULL)
  6053.             {
  6054.               /* This section of the file is not attached, root
  6055.                  around for a sensible place for it to go.  */
  6056.  
  6057.               if (file->flags.just_syms)
  6058.                 bfd_link_just_syms (file->the_bfd, s, &link_info);
  6059.               else if ((s->flags & SEC_EXCLUDE) != 0)
  6060.                 s->output_section = bfd_abs_section_ptr;
  6061.               else if (strcmp (s->name, "COMMON") == 0)
  6062.                 {
  6063.                   /* This is a lonely common section which must have
  6064.                      come from an archive.  We attach to the section
  6065.                      with the wildcard.  */
  6066.                   if (! link_info.relocatable
  6067.                       || command_line.force_common_definition)
  6068.                     {
  6069.                       if (default_common_section == NULL)
  6070.                         default_common_section
  6071.                           = lang_output_section_statement_lookup (".bss", 0,
  6072.                                                                   TRUE);
  6073.                       lang_add_section (&default_common_section->children, s,
  6074.                                         NULL, default_common_section);
  6075.                     }
  6076.                 }
  6077.               else
  6078.                 {
  6079.                   const char *name = s->name;
  6080.                   int constraint = 0;
  6081.  
  6082.                   if (config.unique_orphan_sections
  6083.                       || unique_section_p (s, NULL))
  6084.                     constraint = SPECIAL;
  6085.  
  6086.                   if (!ldemul_place_orphan (s, name, constraint))
  6087.                     {
  6088.                       lang_output_section_statement_type *os;
  6089.                       os = lang_output_section_statement_lookup (name,
  6090.                                                                  constraint,
  6091.                                                                  TRUE);
  6092.                       if (os->addr_tree == NULL
  6093.                           && (link_info.relocatable
  6094.                               || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0))
  6095.                         os->addr_tree = exp_intop (0);
  6096.                       lang_add_section (&os->children, s, NULL, os);
  6097.                     }
  6098.                 }
  6099.             }
  6100.         }
  6101.     }
  6102. }
  6103.  
  6104. void
  6105. lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
  6106. {
  6107.   flagword *ptr_flags;
  6108.  
  6109.   ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
  6110.   while (*flags)
  6111.     {
  6112.       switch (*flags)
  6113.         {
  6114.         case 'A': case 'a':
  6115.           *ptr_flags |= SEC_ALLOC;
  6116.           break;
  6117.  
  6118.         case 'R': case 'r':
  6119.           *ptr_flags |= SEC_READONLY;
  6120.           break;
  6121.  
  6122.         case 'W': case 'w':
  6123.           *ptr_flags |= SEC_DATA;
  6124.           break;
  6125.  
  6126.         case 'X': case 'x':
  6127.           *ptr_flags |= SEC_CODE;
  6128.           break;
  6129.  
  6130.         case 'L': case 'l':
  6131.         case 'I': case 'i':
  6132.           *ptr_flags |= SEC_LOAD;
  6133.           break;
  6134.  
  6135.         default:
  6136.           einfo (_("%P%F: invalid syntax in flags\n"));
  6137.           break;
  6138.         }
  6139.       flags++;
  6140.     }
  6141. }
  6142.  
  6143. /* Call a function on each input file.  This function will be called
  6144.    on an archive, but not on the elements.  */
  6145.  
  6146. void
  6147. lang_for_each_input_file (void (*func) (lang_input_statement_type *))
  6148. {
  6149.   lang_input_statement_type *f;
  6150.  
  6151.   for (f = (lang_input_statement_type *) input_file_chain.head;
  6152.        f != NULL;
  6153.        f = (lang_input_statement_type *) f->next_real_file)
  6154.     func (f);
  6155. }
  6156.  
  6157. /* Call a function on each file.  The function will be called on all
  6158.    the elements of an archive which are included in the link, but will
  6159.    not be called on the archive file itself.  */
  6160.  
  6161. void
  6162. lang_for_each_file (void (*func) (lang_input_statement_type *))
  6163. {
  6164.   LANG_FOR_EACH_INPUT_STATEMENT (f)
  6165.     {
  6166.       func (f);
  6167.     }
  6168. }
  6169.  
  6170. void
  6171. ldlang_add_file (lang_input_statement_type *entry)
  6172. {
  6173.   lang_statement_append (&file_chain,
  6174.                          (lang_statement_union_type *) entry,
  6175.                          &entry->next);
  6176.  
  6177.   /* The BFD linker needs to have a list of all input BFDs involved in
  6178.      a link.  */
  6179.   ASSERT (entry->the_bfd->link_next == NULL);
  6180.   ASSERT (entry->the_bfd != link_info.output_bfd);
  6181.  
  6182.   *link_info.input_bfds_tail = entry->the_bfd;
  6183.   link_info.input_bfds_tail = &entry->the_bfd->link_next;
  6184.   entry->the_bfd->usrdata = entry;
  6185.   bfd_set_gp_size (entry->the_bfd, g_switch_value);
  6186.  
  6187.   /* Look through the sections and check for any which should not be
  6188.      included in the link.  We need to do this now, so that we can
  6189.      notice when the backend linker tries to report multiple
  6190.      definition errors for symbols which are in sections we aren't
  6191.      going to link.  FIXME: It might be better to entirely ignore
  6192.      symbols which are defined in sections which are going to be
  6193.      discarded.  This would require modifying the backend linker for
  6194.      each backend which might set the SEC_LINK_ONCE flag.  If we do
  6195.      this, we should probably handle SEC_EXCLUDE in the same way.  */
  6196.  
  6197.   bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
  6198. }
  6199.  
  6200. void
  6201. lang_add_output (const char *name, int from_script)
  6202. {
  6203.   /* Make -o on command line override OUTPUT in script.  */
  6204.   if (!had_output_filename || !from_script)
  6205.     {
  6206.       output_filename = name;
  6207.       had_output_filename = TRUE;
  6208.     }
  6209. }
  6210.  
  6211. static int
  6212. topower (int x)
  6213. {
  6214.   unsigned int i = 1;
  6215.   int l;
  6216.  
  6217.   if (x < 0)
  6218.     return -1;
  6219.  
  6220.   for (l = 0; l < 32; l++)
  6221.     {
  6222.       if (i >= (unsigned int) x)
  6223.         return l;
  6224.       i <<= 1;
  6225.     }
  6226.  
  6227.   return 0;
  6228. }
  6229.  
  6230. lang_output_section_statement_type *
  6231. lang_enter_output_section_statement (const char *output_section_statement_name,
  6232.                                      etree_type *address_exp,
  6233.                                      enum section_type sectype,
  6234.                                      etree_type *align,
  6235.                                      etree_type *subalign,
  6236.                                      etree_type *ebase,
  6237.                                      int constraint,
  6238.                                      int align_with_input)
  6239. {
  6240.   lang_output_section_statement_type *os;
  6241.  
  6242.   os = lang_output_section_statement_lookup (output_section_statement_name,
  6243.                                              constraint, TRUE);
  6244.   current_section = os;
  6245.  
  6246.   if (os->addr_tree == NULL)
  6247.     {
  6248.       os->addr_tree = address_exp;
  6249.     }
  6250.   os->sectype = sectype;
  6251.   if (sectype != noload_section)
  6252.     os->flags = SEC_NO_FLAGS;
  6253.   else
  6254.     os->flags = SEC_NEVER_LOAD;
  6255.   os->block_value = 1;
  6256.  
  6257.   /* Make next things chain into subchain of this.  */
  6258.   push_stat_ptr (&os->children);
  6259.  
  6260.   os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT;
  6261.   if (os->align_lma_with_input && align != NULL)
  6262.     einfo (_("%F%P:%S: error: align with input and explicit align specified\n"), NULL);
  6263.  
  6264.   os->subsection_alignment =
  6265.     topower (exp_get_value_int (subalign, -1, "subsection alignment"));
  6266.   os->section_alignment =
  6267.     topower (exp_get_value_int (align, -1, "section alignment"));
  6268.  
  6269.   os->load_base = ebase;
  6270.   return os;
  6271. }
  6272.  
  6273. void
  6274. lang_final (void)
  6275. {
  6276.   lang_output_statement_type *new_stmt;
  6277.  
  6278.   new_stmt = new_stat (lang_output_statement, stat_ptr);
  6279.   new_stmt->name = output_filename;
  6280.  
  6281. }
  6282.  
  6283. /* Reset the current counters in the regions.  */
  6284.  
  6285. void
  6286. lang_reset_memory_regions (void)
  6287. {
  6288.   lang_memory_region_type *p = lang_memory_region_list;
  6289.   asection *o;
  6290.   lang_output_section_statement_type *os;
  6291.  
  6292.   for (p = lang_memory_region_list; p != NULL; p = p->next)
  6293.     {
  6294.       p->current = p->origin;
  6295.       p->last_os = NULL;
  6296.     }
  6297.  
  6298.   for (os = &lang_output_section_statement.head->output_section_statement;
  6299.        os != NULL;
  6300.        os = os->next)
  6301.     {
  6302.       os->processed_vma = FALSE;
  6303.       os->processed_lma = FALSE;
  6304.     }
  6305.  
  6306.   for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
  6307.     {
  6308.       /* Save the last size for possible use by bfd_relax_section.  */
  6309.       o->rawsize = o->size;
  6310.       o->size = 0;
  6311.     }
  6312. }
  6313.  
  6314. /* Worker for lang_gc_sections_1.  */
  6315.  
  6316. static void
  6317. gc_section_callback (lang_wild_statement_type *ptr,
  6318.                      struct wildcard_list *sec ATTRIBUTE_UNUSED,
  6319.                      asection *section,
  6320.                      struct flag_info *sflag_info ATTRIBUTE_UNUSED,
  6321.                      lang_input_statement_type *file ATTRIBUTE_UNUSED,
  6322.                      void *data ATTRIBUTE_UNUSED)
  6323. {
  6324.   /* If the wild pattern was marked KEEP, the member sections
  6325.      should be as well.  */
  6326.   if (ptr->keep_sections)
  6327.     section->flags |= SEC_KEEP;
  6328. }
  6329.  
  6330. /* Iterate over sections marking them against GC.  */
  6331.  
  6332. static void
  6333. lang_gc_sections_1 (lang_statement_union_type *s)
  6334. {
  6335.   for (; s != NULL; s = s->header.next)
  6336.     {
  6337.       switch (s->header.type)
  6338.         {
  6339.         case lang_wild_statement_enum:
  6340.           walk_wild (&s->wild_statement, gc_section_callback, NULL);
  6341.           break;
  6342.         case lang_constructors_statement_enum:
  6343.           lang_gc_sections_1 (constructor_list.head);
  6344.           break;
  6345.         case lang_output_section_statement_enum:
  6346.           lang_gc_sections_1 (s->output_section_statement.children.head);
  6347.           break;
  6348.         case lang_group_statement_enum:
  6349.           lang_gc_sections_1 (s->group_statement.children.head);
  6350.           break;
  6351.         default:
  6352.           break;
  6353.         }
  6354.     }
  6355. }
  6356.  
  6357. static void
  6358. lang_gc_sections (void)
  6359. {
  6360.   /* Keep all sections so marked in the link script.  */
  6361.  
  6362.   lang_gc_sections_1 (statement_list.head);
  6363.  
  6364.   /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
  6365.      the special case of debug info.  (See bfd/stabs.c)
  6366.      Twiddle the flag here, to simplify later linker code.  */
  6367.   if (link_info.relocatable)
  6368.     {
  6369.       LANG_FOR_EACH_INPUT_STATEMENT (f)
  6370.         {
  6371.           asection *sec;
  6372. #ifdef ENABLE_PLUGINS
  6373.           if (f->flags.claimed)
  6374.             continue;
  6375. #endif
  6376.           for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
  6377.             if ((sec->flags & SEC_DEBUGGING) == 0)
  6378.               sec->flags &= ~SEC_EXCLUDE;
  6379.         }
  6380.     }
  6381.  
  6382.   if (link_info.gc_sections)
  6383.     bfd_gc_sections (link_info.output_bfd, &link_info);
  6384. }
  6385.  
  6386. /* Worker for lang_find_relro_sections_1.  */
  6387.  
  6388. static void
  6389. find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
  6390.                              struct wildcard_list *sec ATTRIBUTE_UNUSED,
  6391.                              asection *section,
  6392.                              struct flag_info *sflag_info ATTRIBUTE_UNUSED,
  6393.                              lang_input_statement_type *file ATTRIBUTE_UNUSED,
  6394.                              void *data)
  6395. {
  6396.   /* Discarded, excluded and ignored sections effectively have zero
  6397.      size.  */
  6398.   if (section->output_section != NULL
  6399.       && section->output_section->owner == link_info.output_bfd
  6400.       && (section->output_section->flags & SEC_EXCLUDE) == 0
  6401.       && !IGNORE_SECTION (section)
  6402.       && section->size != 0)
  6403.     {
  6404.       bfd_boolean *has_relro_section = (bfd_boolean *) data;
  6405.       *has_relro_section = TRUE;
  6406.     }
  6407. }
  6408.  
  6409. /* Iterate over sections for relro sections.  */
  6410.  
  6411. static void
  6412. lang_find_relro_sections_1 (lang_statement_union_type *s,
  6413.                             bfd_boolean *has_relro_section)
  6414. {
  6415.   if (*has_relro_section)
  6416.     return;
  6417.  
  6418.   for (; s != NULL; s = s->header.next)
  6419.     {
  6420.       if (s == expld.dataseg.relro_end_stat)
  6421.         break;
  6422.  
  6423.       switch (s->header.type)
  6424.         {
  6425.         case lang_wild_statement_enum:
  6426.           walk_wild (&s->wild_statement,
  6427.                      find_relro_section_callback,
  6428.                      has_relro_section);
  6429.           break;
  6430.         case lang_constructors_statement_enum:
  6431.           lang_find_relro_sections_1 (constructor_list.head,
  6432.                                       has_relro_section);
  6433.           break;
  6434.         case lang_output_section_statement_enum:
  6435.           lang_find_relro_sections_1 (s->output_section_statement.children.head,
  6436.                                       has_relro_section);
  6437.           break;
  6438.         case lang_group_statement_enum:
  6439.           lang_find_relro_sections_1 (s->group_statement.children.head,
  6440.                                       has_relro_section);
  6441.           break;
  6442.         default:
  6443.           break;
  6444.         }
  6445.     }
  6446. }
  6447.  
  6448. static void
  6449. lang_find_relro_sections (void)
  6450. {
  6451.   bfd_boolean has_relro_section = FALSE;
  6452.  
  6453.   /* Check all sections in the link script.  */
  6454.  
  6455.   lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
  6456.                               &has_relro_section);
  6457.  
  6458.   if (!has_relro_section)
  6459.     link_info.relro = FALSE;
  6460. }
  6461.  
  6462. /* Relax all sections until bfd_relax_section gives up.  */
  6463.  
  6464. void
  6465. lang_relax_sections (bfd_boolean need_layout)
  6466. {
  6467.   if (RELAXATION_ENABLED)
  6468.     {
  6469.       /* We may need more than one relaxation pass.  */
  6470.       int i = link_info.relax_pass;
  6471.  
  6472.       /* The backend can use it to determine the current pass.  */
  6473.       link_info.relax_pass = 0;
  6474.  
  6475.       while (i--)
  6476.         {
  6477.           /* Keep relaxing until bfd_relax_section gives up.  */
  6478.           bfd_boolean relax_again;
  6479.  
  6480.           link_info.relax_trip = -1;
  6481.           do
  6482.             {
  6483.               link_info.relax_trip++;
  6484.  
  6485.               /* Note: pe-dll.c does something like this also.  If you find
  6486.                  you need to change this code, you probably need to change
  6487.                  pe-dll.c also.  DJ  */
  6488.  
  6489.               /* Do all the assignments with our current guesses as to
  6490.                  section sizes.  */
  6491.               lang_do_assignments (lang_assigning_phase_enum);
  6492.  
  6493.               /* We must do this after lang_do_assignments, because it uses
  6494.                  size.  */
  6495.               lang_reset_memory_regions ();
  6496.  
  6497.               /* Perform another relax pass - this time we know where the
  6498.                  globals are, so can make a better guess.  */
  6499.               relax_again = FALSE;
  6500.               lang_size_sections (&relax_again, FALSE);
  6501.             }
  6502.           while (relax_again);
  6503.  
  6504.           link_info.relax_pass++;
  6505.         }
  6506.       need_layout = TRUE;
  6507.     }
  6508.  
  6509.   if (need_layout)
  6510.     {
  6511.       /* Final extra sizing to report errors.  */
  6512.       lang_do_assignments (lang_assigning_phase_enum);
  6513.       lang_reset_memory_regions ();
  6514.       lang_size_sections (NULL, TRUE);
  6515.     }
  6516. }
  6517.  
  6518. #ifdef ENABLE_PLUGINS
  6519. /* Find the insert point for the plugin's replacement files.  We
  6520.    place them after the first claimed real object file, or if the
  6521.    first claimed object is an archive member, after the last real
  6522.    object file immediately preceding the archive.  In the event
  6523.    no objects have been claimed at all, we return the first dummy
  6524.    object file on the list as the insert point; that works, but
  6525.    the callee must be careful when relinking the file_chain as it
  6526.    is not actually on that chain, only the statement_list and the
  6527.    input_file list; in that case, the replacement files must be
  6528.    inserted at the head of the file_chain.  */
  6529.  
  6530. static lang_input_statement_type *
  6531. find_replacements_insert_point (void)
  6532. {
  6533.   lang_input_statement_type *claim1, *lastobject;
  6534.   lastobject = &input_file_chain.head->input_statement;
  6535.   for (claim1 = &file_chain.head->input_statement;
  6536.        claim1 != NULL;
  6537.        claim1 = &claim1->next->input_statement)
  6538.     {
  6539.       if (claim1->flags.claimed)
  6540.         return claim1->flags.claim_archive ? lastobject : claim1;
  6541.       /* Update lastobject if this is a real object file.  */
  6542.       if (claim1->the_bfd && (claim1->the_bfd->my_archive == NULL))
  6543.         lastobject = claim1;
  6544.     }
  6545.   /* No files were claimed by the plugin.  Choose the last object
  6546.      file found on the list (maybe the first, dummy entry) as the
  6547.      insert point.  */
  6548.   return lastobject;
  6549. }
  6550.  
  6551. /* Insert SRCLIST into DESTLIST after given element by chaining
  6552.    on FIELD as the next-pointer.  (Counterintuitively does not need
  6553.    a pointer to the actual after-node itself, just its chain field.)  */
  6554.  
  6555. static void
  6556. lang_list_insert_after (lang_statement_list_type *destlist,
  6557.                         lang_statement_list_type *srclist,
  6558.                         lang_statement_union_type **field)
  6559. {
  6560.   *(srclist->tail) = *field;
  6561.   *field = srclist->head;
  6562.   if (destlist->tail == field)
  6563.     destlist->tail = srclist->tail;
  6564. }
  6565.  
  6566. /* Detach new nodes added to DESTLIST since the time ORIGLIST
  6567.    was taken as a copy of it and leave them in ORIGLIST.  */
  6568.  
  6569. static void
  6570. lang_list_remove_tail (lang_statement_list_type *destlist,
  6571.                        lang_statement_list_type *origlist)
  6572. {
  6573.   union lang_statement_union **savetail;
  6574.   /* Check that ORIGLIST really is an earlier state of DESTLIST.  */
  6575.   ASSERT (origlist->head == destlist->head);
  6576.   savetail = origlist->tail;
  6577.   origlist->head = *(savetail);
  6578.   origlist->tail = destlist->tail;
  6579.   destlist->tail = savetail;
  6580.   *savetail = NULL;
  6581. }
  6582. #endif /* ENABLE_PLUGINS */
  6583.  
  6584. void
  6585. lang_process (void)
  6586. {
  6587.   /* Finalize dynamic list.  */
  6588.   if (link_info.dynamic_list)
  6589.     lang_finalize_version_expr_head (&link_info.dynamic_list->head);
  6590.  
  6591.   current_target = default_target;
  6592.  
  6593.   /* Open the output file.  */
  6594.   lang_for_each_statement (ldlang_open_output);
  6595.   init_opb ();
  6596.  
  6597.   ldemul_create_output_section_statements ();
  6598.  
  6599.   /* Add to the hash table all undefineds on the command line.  */
  6600.   lang_place_undefineds ();
  6601.  
  6602.   if (!bfd_section_already_linked_table_init ())
  6603.     einfo (_("%P%F: Failed to create hash table\n"));
  6604.  
  6605.   /* Create a bfd for each input file.  */
  6606.   current_target = default_target;
  6607.   open_input_bfds (statement_list.head, OPEN_BFD_NORMAL);
  6608.  
  6609. #ifdef ENABLE_PLUGINS
  6610.   if (plugin_active_plugins_p ())
  6611.     {
  6612.       lang_statement_list_type added;
  6613.       lang_statement_list_type files, inputfiles;
  6614.  
  6615.       /* Now all files are read, let the plugin(s) decide if there
  6616.          are any more to be added to the link before we call the
  6617.          emulation's after_open hook.  We create a private list of
  6618.          input statements for this purpose, which we will eventually
  6619.          insert into the global statment list after the first claimed
  6620.          file.  */
  6621.       added = *stat_ptr;
  6622.       /* We need to manipulate all three chains in synchrony.  */
  6623.       files = file_chain;
  6624.       inputfiles = input_file_chain;
  6625.       if (plugin_call_all_symbols_read ())
  6626.         einfo (_("%P%F: %s: plugin reported error after all symbols read\n"),
  6627.                plugin_error_plugin ());
  6628.       /* Open any newly added files, updating the file chains.  */
  6629.       link_info.loading_lto_outputs = TRUE;
  6630.       open_input_bfds (*added.tail, OPEN_BFD_NORMAL);
  6631.       /* Restore the global list pointer now they have all been added.  */
  6632.       lang_list_remove_tail (stat_ptr, &added);
  6633.       /* And detach the fresh ends of the file lists.  */
  6634.       lang_list_remove_tail (&file_chain, &files);
  6635.       lang_list_remove_tail (&input_file_chain, &inputfiles);
  6636.       /* Were any new files added?  */
  6637.       if (added.head != NULL)
  6638.         {
  6639.           /* If so, we will insert them into the statement list immediately
  6640.              after the first input file that was claimed by the plugin.  */
  6641.           plugin_insert = find_replacements_insert_point ();
  6642.           /* If a plugin adds input files without having claimed any, we
  6643.              don't really have a good idea where to place them.  Just putting
  6644.              them at the start or end of the list is liable to leave them
  6645.              outside the crtbegin...crtend range.  */
  6646.           ASSERT (plugin_insert != NULL);
  6647.           /* Splice the new statement list into the old one.  */
  6648.           lang_list_insert_after (stat_ptr, &added,
  6649.                                   &plugin_insert->header.next);
  6650.           /* Likewise for the file chains.  */
  6651.           lang_list_insert_after (&input_file_chain, &inputfiles,
  6652.                                   &plugin_insert->next_real_file);
  6653.           /* We must be careful when relinking file_chain; we may need to
  6654.              insert the new files at the head of the list if the insert
  6655.              point chosen is the dummy first input file.  */
  6656.           if (plugin_insert->filename)
  6657.             lang_list_insert_after (&file_chain, &files, &plugin_insert->next);
  6658.           else
  6659.             lang_list_insert_after (&file_chain, &files, &file_chain.head);
  6660.  
  6661.           /* Rescan archives in case new undefined symbols have appeared.  */
  6662.           open_input_bfds (statement_list.head, OPEN_BFD_RESCAN);
  6663.         }
  6664.     }
  6665. #endif /* ENABLE_PLUGINS */
  6666.  
  6667.   link_info.gc_sym_list = &entry_symbol;
  6668.   if (entry_symbol.name == NULL)
  6669.     link_info.gc_sym_list = ldlang_undef_chain_list_head;
  6670.  
  6671.   ldemul_after_open ();
  6672.  
  6673.   bfd_section_already_linked_table_free ();
  6674.  
  6675.   /* Make sure that we're not mixing architectures.  We call this
  6676.      after all the input files have been opened, but before we do any
  6677.      other processing, so that any operations merge_private_bfd_data
  6678.      does on the output file will be known during the rest of the
  6679.      link.  */
  6680.   lang_check ();
  6681.  
  6682.   /* Handle .exports instead of a version script if we're told to do so.  */
  6683.   if (command_line.version_exports_section)
  6684.     lang_do_version_exports_section ();
  6685.  
  6686.   /* Build all sets based on the information gathered from the input
  6687.      files.  */
  6688.   ldctor_build_sets ();
  6689.  
  6690.   /* PR 13683: We must rerun the assignments prior to running garbage
  6691.      collection in order to make sure that all symbol aliases are resolved.  */
  6692.   lang_do_assignments (lang_mark_phase_enum);
  6693.   expld.phase = lang_first_phase_enum;
  6694.  
  6695.   /* Remove unreferenced sections if asked to.  */
  6696.   lang_gc_sections ();
  6697.  
  6698.   /* Size up the common data.  */
  6699.   lang_common ();
  6700.  
  6701.   /* Update wild statements.  */
  6702.   update_wild_statements (statement_list.head);
  6703.  
  6704.   /* Run through the contours of the script and attach input sections
  6705.      to the correct output sections.  */
  6706.   lang_statement_iteration++;
  6707.   map_input_to_output_sections (statement_list.head, NULL, NULL);
  6708.  
  6709.   process_insert_statements ();
  6710.  
  6711.   /* Find any sections not attached explicitly and handle them.  */
  6712.   lang_place_orphans ();
  6713.  
  6714.   if (! link_info.relocatable)
  6715.     {
  6716.       asection *found;
  6717.  
  6718.       /* Merge SEC_MERGE sections.  This has to be done after GC of
  6719.          sections, so that GCed sections are not merged, but before
  6720.          assigning dynamic symbols, since removing whole input sections
  6721.          is hard then.  */
  6722.       bfd_merge_sections (link_info.output_bfd, &link_info);
  6723.  
  6724.       /* Look for a text section and set the readonly attribute in it.  */
  6725.       found = bfd_get_section_by_name (link_info.output_bfd, ".text");
  6726.  
  6727.       if (found != NULL)
  6728.         {
  6729.           if (config.text_read_only)
  6730.             found->flags |= SEC_READONLY;
  6731.           else
  6732.             found->flags &= ~SEC_READONLY;
  6733.         }
  6734.     }
  6735.  
  6736.   /* Do anything special before sizing sections.  This is where ELF
  6737.      and other back-ends size dynamic sections.  */
  6738.   ldemul_before_allocation ();
  6739.  
  6740.   /* We must record the program headers before we try to fix the
  6741.      section positions, since they will affect SIZEOF_HEADERS.  */
  6742.   lang_record_phdrs ();
  6743.  
  6744.   /* Check relro sections.  */
  6745.   if (link_info.relro && ! link_info.relocatable)
  6746.     lang_find_relro_sections ();
  6747.  
  6748.   /* Size up the sections.  */
  6749.   lang_size_sections (NULL, ! RELAXATION_ENABLED);
  6750.  
  6751.   /* See if anything special should be done now we know how big
  6752.      everything is.  This is where relaxation is done.  */
  6753.   ldemul_after_allocation ();
  6754.  
  6755.   /* Fix any .startof. or .sizeof. symbols.  */
  6756.   lang_set_startof ();
  6757.  
  6758.   /* Do all the assignments, now that we know the final resting places
  6759.      of all the symbols.  */
  6760.   lang_do_assignments (lang_final_phase_enum);
  6761.  
  6762.   ldemul_finish ();
  6763.  
  6764.   /* Make sure that the section addresses make sense.  */
  6765.   if (command_line.check_section_addresses)
  6766.     lang_check_section_addresses ();
  6767.  
  6768.   lang_end ();
  6769. }
  6770.  
  6771. /* EXPORTED TO YACC */
  6772.  
  6773. void
  6774. lang_add_wild (struct wildcard_spec *filespec,
  6775.                struct wildcard_list *section_list,
  6776.                bfd_boolean keep_sections)
  6777. {
  6778.   struct wildcard_list *curr, *next;
  6779.   lang_wild_statement_type *new_stmt;
  6780.  
  6781.   /* Reverse the list as the parser puts it back to front.  */
  6782.   for (curr = section_list, section_list = NULL;
  6783.        curr != NULL;
  6784.        section_list = curr, curr = next)
  6785.     {
  6786.       if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
  6787.         placed_commons = TRUE;
  6788.  
  6789.       next = curr->next;
  6790.       curr->next = section_list;
  6791.     }
  6792.  
  6793.   if (filespec != NULL && filespec->name != NULL)
  6794.     {
  6795.       if (strcmp (filespec->name, "*") == 0)
  6796.         filespec->name = NULL;
  6797.       else if (! wildcardp (filespec->name))
  6798.         lang_has_input_file = TRUE;
  6799.     }
  6800.  
  6801.   new_stmt = new_stat (lang_wild_statement, stat_ptr);
  6802.   new_stmt->filename = NULL;
  6803.   new_stmt->filenames_sorted = FALSE;
  6804.   new_stmt->section_flag_list = NULL;
  6805.   if (filespec != NULL)
  6806.     {
  6807.       new_stmt->filename = filespec->name;
  6808.       new_stmt->filenames_sorted = filespec->sorted == by_name;
  6809.       new_stmt->section_flag_list = filespec->section_flag_list;
  6810.     }
  6811.   new_stmt->section_list = section_list;
  6812.   new_stmt->keep_sections = keep_sections;
  6813.   lang_list_init (&new_stmt->children);
  6814.   analyze_walk_wild_section_handler (new_stmt);
  6815. }
  6816.  
  6817. void
  6818. lang_section_start (const char *name, etree_type *address,
  6819.                     const segment_type *segment)
  6820. {
  6821.   lang_address_statement_type *ad;
  6822.  
  6823.   ad = new_stat (lang_address_statement, stat_ptr);
  6824.   ad->section_name = name;
  6825.   ad->address = address;
  6826.   ad->segment = segment;
  6827. }
  6828.  
  6829. /* Set the start symbol to NAME.  CMDLINE is nonzero if this is called
  6830.    because of a -e argument on the command line, or zero if this is
  6831.    called by ENTRY in a linker script.  Command line arguments take
  6832.    precedence.  */
  6833.  
  6834. void
  6835. lang_add_entry (const char *name, bfd_boolean cmdline)
  6836. {
  6837.   if (entry_symbol.name == NULL
  6838.       || cmdline
  6839.       || ! entry_from_cmdline)
  6840.     {
  6841.       entry_symbol.name = name;
  6842.       entry_from_cmdline = cmdline;
  6843.     }
  6844. }
  6845.  
  6846. /* Set the default start symbol to NAME.  .em files should use this,
  6847.    not lang_add_entry, to override the use of "start" if neither the
  6848.    linker script nor the command line specifies an entry point.  NAME
  6849.    must be permanently allocated.  */
  6850. void
  6851. lang_default_entry (const char *name)
  6852. {
  6853.   entry_symbol_default = name;
  6854. }
  6855.  
  6856. void
  6857. lang_add_target (const char *name)
  6858. {
  6859.   lang_target_statement_type *new_stmt;
  6860.  
  6861.   new_stmt = new_stat (lang_target_statement, stat_ptr);
  6862.   new_stmt->target = name;
  6863. }
  6864.  
  6865. void
  6866. lang_add_map (const char *name)
  6867. {
  6868.   while (*name)
  6869.     {
  6870.       switch (*name)
  6871.         {
  6872.         case 'F':
  6873.           map_option_f = TRUE;
  6874.           break;
  6875.         }
  6876.       name++;
  6877.     }
  6878. }
  6879.  
  6880. void
  6881. lang_add_fill (fill_type *fill)
  6882. {
  6883.   lang_fill_statement_type *new_stmt;
  6884.  
  6885.   new_stmt = new_stat (lang_fill_statement, stat_ptr);
  6886.   new_stmt->fill = fill;
  6887. }
  6888.  
  6889. void
  6890. lang_add_data (int type, union etree_union *exp)
  6891. {
  6892.   lang_data_statement_type *new_stmt;
  6893.  
  6894.   new_stmt = new_stat (lang_data_statement, stat_ptr);
  6895.   new_stmt->exp = exp;
  6896.   new_stmt->type = type;
  6897. }
  6898.  
  6899. /* Create a new reloc statement.  RELOC is the BFD relocation type to
  6900.    generate.  HOWTO is the corresponding howto structure (we could
  6901.    look this up, but the caller has already done so).  SECTION is the
  6902.    section to generate a reloc against, or NAME is the name of the
  6903.    symbol to generate a reloc against.  Exactly one of SECTION and
  6904.    NAME must be NULL.  ADDEND is an expression for the addend.  */
  6905.  
  6906. void
  6907. lang_add_reloc (bfd_reloc_code_real_type reloc,
  6908.                 reloc_howto_type *howto,
  6909.                 asection *section,
  6910.                 const char *name,
  6911.                 union etree_union *addend)
  6912. {
  6913.   lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
  6914.  
  6915.   p->reloc = reloc;
  6916.   p->howto = howto;
  6917.   p->section = section;
  6918.   p->name = name;
  6919.   p->addend_exp = addend;
  6920.  
  6921.   p->addend_value = 0;
  6922.   p->output_section = NULL;
  6923.   p->output_offset = 0;
  6924. }
  6925.  
  6926. lang_assignment_statement_type *
  6927. lang_add_assignment (etree_type *exp)
  6928. {
  6929.   lang_assignment_statement_type *new_stmt;
  6930.  
  6931.   new_stmt = new_stat (lang_assignment_statement, stat_ptr);
  6932.   new_stmt->exp = exp;
  6933.   return new_stmt;
  6934. }
  6935.  
  6936. void
  6937. lang_add_attribute (enum statement_enum attribute)
  6938. {
  6939.   new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
  6940. }
  6941.  
  6942. void
  6943. lang_startup (const char *name)
  6944. {
  6945.   if (first_file->filename != NULL)
  6946.     {
  6947.       einfo (_("%P%F: multiple STARTUP files\n"));
  6948.     }
  6949.   first_file->filename = name;
  6950.   first_file->local_sym_name = name;
  6951.   first_file->flags.real = TRUE;
  6952. }
  6953.  
  6954. void
  6955. lang_float (bfd_boolean maybe)
  6956. {
  6957.   lang_float_flag = maybe;
  6958. }
  6959.  
  6960.  
  6961. /* Work out the load- and run-time regions from a script statement, and
  6962.    store them in *LMA_REGION and *REGION respectively.
  6963.  
  6964.    MEMSPEC is the name of the run-time region, or the value of
  6965.    DEFAULT_MEMORY_REGION if the statement didn't specify one.
  6966.    LMA_MEMSPEC is the name of the load-time region, or null if the
  6967.    statement didn't specify one.HAVE_LMA_P is TRUE if the statement
  6968.    had an explicit load address.
  6969.  
  6970.    It is an error to specify both a load region and a load address.  */
  6971.  
  6972. static void
  6973. lang_get_regions (lang_memory_region_type **region,
  6974.                   lang_memory_region_type **lma_region,
  6975.                   const char *memspec,
  6976.                   const char *lma_memspec,
  6977.                   bfd_boolean have_lma,
  6978.                   bfd_boolean have_vma)
  6979. {
  6980.   *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
  6981.  
  6982.   /* If no runtime region or VMA has been specified, but the load region
  6983.      has been specified, then use the load region for the runtime region
  6984.      as well.  */
  6985.   if (lma_memspec != NULL
  6986.       && ! have_vma
  6987.       && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
  6988.     *region = *lma_region;
  6989.   else
  6990.     *region = lang_memory_region_lookup (memspec, FALSE);
  6991.  
  6992.   if (have_lma && lma_memspec != 0)
  6993.     einfo (_("%X%P:%S: section has both a load address and a load region\n"),
  6994.            NULL);
  6995. }
  6996.  
  6997. void
  6998. lang_leave_output_section_statement (fill_type *fill, const char *memspec,
  6999.                                      lang_output_section_phdr_list *phdrs,
  7000.                                      const char *lma_memspec)
  7001. {
  7002.   lang_get_regions (&current_section->region,
  7003.                     &current_section->lma_region,
  7004.                     memspec, lma_memspec,
  7005.                     current_section->load_base != NULL,
  7006.                     current_section->addr_tree != NULL);
  7007.  
  7008.   /* If this section has no load region or base, but uses the same
  7009.      region as the previous section, then propagate the previous
  7010.      section's load region.  */
  7011.  
  7012.   if (current_section->lma_region == NULL
  7013.       && current_section->load_base == NULL
  7014.       && current_section->addr_tree == NULL
  7015.       && current_section->region == current_section->prev->region)
  7016.     current_section->lma_region = current_section->prev->lma_region;
  7017.  
  7018.   current_section->fill = fill;
  7019.   current_section->phdrs = phdrs;
  7020.   pop_stat_ptr ();
  7021. }
  7022.  
  7023. void
  7024. lang_statement_append (lang_statement_list_type *list,
  7025.                        lang_statement_union_type *element,
  7026.                        lang_statement_union_type **field)
  7027. {
  7028.   *(list->tail) = element;
  7029.   list->tail = field;
  7030. }
  7031.  
  7032. /* Set the output format type.  -oformat overrides scripts.  */
  7033.  
  7034. void
  7035. lang_add_output_format (const char *format,
  7036.                         const char *big,
  7037.                         const char *little,
  7038.                         int from_script)
  7039. {
  7040.   if (output_target == NULL || !from_script)
  7041.     {
  7042.       if (command_line.endian == ENDIAN_BIG
  7043.           && big != NULL)
  7044.         format = big;
  7045.       else if (command_line.endian == ENDIAN_LITTLE
  7046.                && little != NULL)
  7047.         format = little;
  7048.  
  7049.       output_target = format;
  7050.     }
  7051. }
  7052.  
  7053. void
  7054. lang_add_insert (const char *where, int is_before)
  7055. {
  7056.   lang_insert_statement_type *new_stmt;
  7057.  
  7058.   new_stmt = new_stat (lang_insert_statement, stat_ptr);
  7059.   new_stmt->where = where;
  7060.   new_stmt->is_before = is_before;
  7061.   saved_script_handle = previous_script_handle;
  7062. }
  7063.  
  7064. /* Enter a group.  This creates a new lang_group_statement, and sets
  7065.    stat_ptr to build new statements within the group.  */
  7066.  
  7067. void
  7068. lang_enter_group (void)
  7069. {
  7070.   lang_group_statement_type *g;
  7071.  
  7072.   g = new_stat (lang_group_statement, stat_ptr);
  7073.   lang_list_init (&g->children);
  7074.   push_stat_ptr (&g->children);
  7075. }
  7076.  
  7077. /* Leave a group.  This just resets stat_ptr to start writing to the
  7078.    regular list of statements again.  Note that this will not work if
  7079.    groups can occur inside anything else which can adjust stat_ptr,
  7080.    but currently they can't.  */
  7081.  
  7082. void
  7083. lang_leave_group (void)
  7084. {
  7085.   pop_stat_ptr ();
  7086. }
  7087.  
  7088. /* Add a new program header.  This is called for each entry in a PHDRS
  7089.    command in a linker script.  */
  7090.  
  7091. void
  7092. lang_new_phdr (const char *name,
  7093.                etree_type *type,
  7094.                bfd_boolean filehdr,
  7095.                bfd_boolean phdrs,
  7096.                etree_type *at,
  7097.                etree_type *flags)
  7098. {
  7099.   struct lang_phdr *n, **pp;
  7100.   bfd_boolean hdrs;
  7101.  
  7102.   n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
  7103.   n->next = NULL;
  7104.   n->name = name;
  7105.   n->type = exp_get_value_int (type, 0, "program header type");
  7106.   n->filehdr = filehdr;
  7107.   n->phdrs = phdrs;
  7108.   n->at = at;
  7109.   n->flags = flags;
  7110.  
  7111.   hdrs = n->type == 1 && (phdrs || filehdr);
  7112.  
  7113.   for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
  7114.     if (hdrs
  7115.         && (*pp)->type == 1
  7116.         && !((*pp)->filehdr || (*pp)->phdrs))
  7117.       {
  7118.         einfo (_("%X%P:%S: PHDRS and FILEHDR are not supported"
  7119.                  " when prior PT_LOAD headers lack them\n"), NULL);
  7120.         hdrs = FALSE;
  7121.       }
  7122.  
  7123.   *pp = n;
  7124. }
  7125.  
  7126. /* Record the program header information in the output BFD.  FIXME: We
  7127.    should not be calling an ELF specific function here.  */
  7128.  
  7129. static void
  7130. lang_record_phdrs (void)
  7131. {
  7132.   unsigned int alc;
  7133.   asection **secs;
  7134.   lang_output_section_phdr_list *last;
  7135.   struct lang_phdr *l;
  7136.   lang_output_section_statement_type *os;
  7137.  
  7138.   alc = 10;
  7139.   secs = (asection **) xmalloc (alc * sizeof (asection *));
  7140.   last = NULL;
  7141.  
  7142.   for (l = lang_phdr_list; l != NULL; l = l->next)
  7143.     {
  7144.       unsigned int c;
  7145.       flagword flags;
  7146.       bfd_vma at;
  7147.  
  7148.       c = 0;
  7149.       for (os = &lang_output_section_statement.head->output_section_statement;
  7150.            os != NULL;
  7151.            os = os->next)
  7152.         {
  7153.           lang_output_section_phdr_list *pl;
  7154.  
  7155.           if (os->constraint < 0)
  7156.             continue;
  7157.  
  7158.           pl = os->phdrs;
  7159.           if (pl != NULL)
  7160.             last = pl;
  7161.           else
  7162.             {
  7163.               if (os->sectype == noload_section
  7164.                   || os->bfd_section == NULL
  7165.                   || (os->bfd_section->flags & SEC_ALLOC) == 0)
  7166.                 continue;
  7167.  
  7168.               /* Don't add orphans to PT_INTERP header.  */
  7169.               if (l->type == 3)
  7170.                 continue;
  7171.  
  7172.               if (last == NULL)
  7173.                 {
  7174.                   lang_output_section_statement_type * tmp_os;
  7175.  
  7176.                   /* If we have not run across a section with a program
  7177.                      header assigned to it yet, then scan forwards to find
  7178.                      one.  This prevents inconsistencies in the linker's
  7179.                      behaviour when a script has specified just a single
  7180.                      header and there are sections in that script which are
  7181.                      not assigned to it, and which occur before the first
  7182.                      use of that header. See here for more details:
  7183.                      http://sourceware.org/ml/binutils/2007-02/msg00291.html  */
  7184.                   for (tmp_os = os; tmp_os; tmp_os = tmp_os->next)
  7185.                     if (tmp_os->phdrs)
  7186.                       {
  7187.                         last = tmp_os->phdrs;
  7188.                         break;
  7189.                       }
  7190.                   if (last == NULL)
  7191.                     einfo (_("%F%P: no sections assigned to phdrs\n"));
  7192.                 }
  7193.               pl = last;
  7194.             }
  7195.  
  7196.           if (os->bfd_section == NULL)
  7197.             continue;
  7198.  
  7199.           for (; pl != NULL; pl = pl->next)
  7200.             {
  7201.               if (strcmp (pl->name, l->name) == 0)
  7202.                 {
  7203.                   if (c >= alc)
  7204.                     {
  7205.                       alc *= 2;
  7206.                       secs = (asection **) xrealloc (secs,
  7207.                                                      alc * sizeof (asection *));
  7208.                     }
  7209.                   secs[c] = os->bfd_section;
  7210.                   ++c;
  7211.                   pl->used = TRUE;
  7212.                 }
  7213.             }
  7214.         }
  7215.  
  7216.       if (l->flags == NULL)
  7217.         flags = 0;
  7218.       else
  7219.         flags = exp_get_vma (l->flags, 0, "phdr flags");
  7220.  
  7221.       if (l->at == NULL)
  7222.         at = 0;
  7223.       else
  7224.         at = exp_get_vma (l->at, 0, "phdr load address");
  7225.  
  7226.       if (! bfd_record_phdr (link_info.output_bfd, l->type,
  7227.                              l->flags != NULL, flags, l->at != NULL,
  7228.                              at, l->filehdr, l->phdrs, c, secs))
  7229.         einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
  7230.     }
  7231.  
  7232.   free (secs);
  7233.  
  7234.   /* Make sure all the phdr assignments succeeded.  */
  7235.   for (os = &lang_output_section_statement.head->output_section_statement;
  7236.        os != NULL;
  7237.        os = os->next)
  7238.     {
  7239.       lang_output_section_phdr_list *pl;
  7240.  
  7241.       if (os->constraint < 0
  7242.           || os->bfd_section == NULL)
  7243.         continue;
  7244.  
  7245.       for (pl = os->phdrs;
  7246.            pl != NULL;
  7247.            pl = pl->next)
  7248.         if (! pl->used && strcmp (pl->name, "NONE") != 0)
  7249.           einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
  7250.                  os->name, pl->name);
  7251.     }
  7252. }
  7253.  
  7254. /* Record a list of sections which may not be cross referenced.  */
  7255.  
  7256. void
  7257. lang_add_nocrossref (lang_nocrossref_type *l)
  7258. {
  7259.   struct lang_nocrossrefs *n;
  7260.  
  7261.   n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
  7262.   n->next = nocrossref_list;
  7263.   n->list = l;
  7264.   nocrossref_list = n;
  7265.  
  7266.   /* Set notice_all so that we get informed about all symbols.  */
  7267.   link_info.notice_all = TRUE;
  7268. }
  7269. /* Overlay handling.  We handle overlays with some static variables.  */
  7270.  
  7271. /* The overlay virtual address.  */
  7272. static etree_type *overlay_vma;
  7273. /* And subsection alignment.  */
  7274. static etree_type *overlay_subalign;
  7275.  
  7276. /* An expression for the maximum section size seen so far.  */
  7277. static etree_type *overlay_max;
  7278.  
  7279. /* A list of all the sections in this overlay.  */
  7280.  
  7281. struct overlay_list {
  7282.   struct overlay_list *next;
  7283.   lang_output_section_statement_type *os;
  7284. };
  7285.  
  7286. static struct overlay_list *overlay_list;
  7287.  
  7288. /* Start handling an overlay.  */
  7289.  
  7290. void
  7291. lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
  7292. {
  7293.   /* The grammar should prevent nested overlays from occurring.  */
  7294.   ASSERT (overlay_vma == NULL
  7295.           && overlay_subalign == NULL
  7296.           && overlay_max == NULL);
  7297.  
  7298.   overlay_vma = vma_expr;
  7299.   overlay_subalign = subalign;
  7300. }
  7301.  
  7302. /* Start a section in an overlay.  We handle this by calling
  7303.    lang_enter_output_section_statement with the correct VMA.
  7304.    lang_leave_overlay sets up the LMA and memory regions.  */
  7305.  
  7306. void
  7307. lang_enter_overlay_section (const char *name)
  7308. {
  7309.   struct overlay_list *n;
  7310.   etree_type *size;
  7311.  
  7312.   lang_enter_output_section_statement (name, overlay_vma, overlay_section,
  7313.                                        0, overlay_subalign, 0, 0, 0);
  7314.  
  7315.   /* If this is the first section, then base the VMA of future
  7316.      sections on this one.  This will work correctly even if `.' is
  7317.      used in the addresses.  */
  7318.   if (overlay_list == NULL)
  7319.     overlay_vma = exp_nameop (ADDR, name);
  7320.  
  7321.   /* Remember the section.  */
  7322.   n = (struct overlay_list *) xmalloc (sizeof *n);
  7323.   n->os = current_section;
  7324.   n->next = overlay_list;
  7325.   overlay_list = n;
  7326.  
  7327.   size = exp_nameop (SIZEOF, name);
  7328.  
  7329.   /* Arrange to work out the maximum section end address.  */
  7330.   if (overlay_max == NULL)
  7331.     overlay_max = size;
  7332.   else
  7333.     overlay_max = exp_binop (MAX_K, overlay_max, size);
  7334. }
  7335.  
  7336. /* Finish a section in an overlay.  There isn't any special to do
  7337.    here.  */
  7338.  
  7339. void
  7340. lang_leave_overlay_section (fill_type *fill,
  7341.                             lang_output_section_phdr_list *phdrs)
  7342. {
  7343.   const char *name;
  7344.   char *clean, *s2;
  7345.   const char *s1;
  7346.   char *buf;
  7347.  
  7348.   name = current_section->name;
  7349.  
  7350.   /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
  7351.      region and that no load-time region has been specified.  It doesn't
  7352.      really matter what we say here, since lang_leave_overlay will
  7353.      override it.  */
  7354.   lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
  7355.  
  7356.   /* Define the magic symbols.  */
  7357.  
  7358.   clean = (char *) xmalloc (strlen (name) + 1);
  7359.   s2 = clean;
  7360.   for (s1 = name; *s1 != '\0'; s1++)
  7361.     if (ISALNUM (*s1) || *s1 == '_')
  7362.       *s2++ = *s1;
  7363.   *s2 = '\0';
  7364.  
  7365.   buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_");
  7366.   sprintf (buf, "__load_start_%s", clean);
  7367.   lang_add_assignment (exp_provide (buf,
  7368.                                     exp_nameop (LOADADDR, name),
  7369.                                     FALSE));
  7370.  
  7371.   buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_");
  7372.   sprintf (buf, "__load_stop_%s", clean);
  7373.   lang_add_assignment (exp_provide (buf,
  7374.                                     exp_binop ('+',
  7375.                                                exp_nameop (LOADADDR, name),
  7376.                                                exp_nameop (SIZEOF, name)),
  7377.                                     FALSE));
  7378.  
  7379.   free (clean);
  7380. }
  7381.  
  7382. /* Finish an overlay.  If there are any overlay wide settings, this
  7383.    looks through all the sections in the overlay and sets them.  */
  7384.  
  7385. void
  7386. lang_leave_overlay (etree_type *lma_expr,
  7387.                     int nocrossrefs,
  7388.                     fill_type *fill,
  7389.                     const char *memspec,
  7390.                     lang_output_section_phdr_list *phdrs,
  7391.                     const char *lma_memspec)
  7392. {
  7393.   lang_memory_region_type *region;
  7394.   lang_memory_region_type *lma_region;
  7395.   struct overlay_list *l;
  7396.   lang_nocrossref_type *nocrossref;
  7397.  
  7398.   lang_get_regions (&region, &lma_region,
  7399.                     memspec, lma_memspec,
  7400.                     lma_expr != NULL, FALSE);
  7401.  
  7402.   nocrossref = NULL;
  7403.  
  7404.   /* After setting the size of the last section, set '.' to end of the
  7405.      overlay region.  */
  7406.   if (overlay_list != NULL)
  7407.     {
  7408.       overlay_list->os->update_dot = 1;
  7409.       overlay_list->os->update_dot_tree
  7410.         = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), FALSE);
  7411.     }
  7412.  
  7413.   l = overlay_list;
  7414.   while (l != NULL)
  7415.     {
  7416.       struct overlay_list *next;
  7417.  
  7418.       if (fill != NULL && l->os->fill == NULL)
  7419.         l->os->fill = fill;
  7420.  
  7421.       l->os->region = region;
  7422.       l->os->lma_region = lma_region;
  7423.  
  7424.       /* The first section has the load address specified in the
  7425.          OVERLAY statement.  The rest are worked out from that.
  7426.          The base address is not needed (and should be null) if
  7427.          an LMA region was specified.  */
  7428.       if (l->next == 0)
  7429.         {
  7430.           l->os->load_base = lma_expr;
  7431.           l->os->sectype = normal_section;
  7432.         }
  7433.       if (phdrs != NULL && l->os->phdrs == NULL)
  7434.         l->os->phdrs = phdrs;
  7435.  
  7436.       if (nocrossrefs)
  7437.         {
  7438.           lang_nocrossref_type *nc;
  7439.  
  7440.           nc = (lang_nocrossref_type *) xmalloc (sizeof *nc);
  7441.           nc->name = l->os->name;
  7442.           nc->next = nocrossref;
  7443.           nocrossref = nc;
  7444.         }
  7445.  
  7446.       next = l->next;
  7447.       free (l);
  7448.       l = next;
  7449.     }
  7450.  
  7451.   if (nocrossref != NULL)
  7452.     lang_add_nocrossref (nocrossref);
  7453.  
  7454.   overlay_vma = NULL;
  7455.   overlay_list = NULL;
  7456.   overlay_max = NULL;
  7457. }
  7458. /* Version handling.  This is only useful for ELF.  */
  7459.  
  7460. /* If PREV is NULL, return first version pattern matching particular symbol.
  7461.    If PREV is non-NULL, return first version pattern matching particular
  7462.    symbol after PREV (previously returned by lang_vers_match).  */
  7463.  
  7464. static struct bfd_elf_version_expr *
  7465. lang_vers_match (struct bfd_elf_version_expr_head *head,
  7466.                  struct bfd_elf_version_expr *prev,
  7467.                  const char *sym)
  7468. {
  7469.   const char *c_sym;
  7470.   const char *cxx_sym = sym;
  7471.   const char *java_sym = sym;
  7472.   struct bfd_elf_version_expr *expr = NULL;
  7473.   enum demangling_styles curr_style;
  7474.  
  7475.   curr_style = CURRENT_DEMANGLING_STYLE;
  7476.   cplus_demangle_set_style (no_demangling);
  7477.   c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS);
  7478.   if (!c_sym)
  7479.     c_sym = sym;
  7480.   cplus_demangle_set_style (curr_style);
  7481.  
  7482.   if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
  7483.     {
  7484.       cxx_sym = bfd_demangle (link_info.output_bfd, sym,
  7485.                               DMGL_PARAMS | DMGL_ANSI);
  7486.       if (!cxx_sym)
  7487.         cxx_sym = sym;
  7488.     }
  7489.   if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
  7490.     {
  7491.       java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA);
  7492.       if (!java_sym)
  7493.         java_sym = sym;
  7494.     }
  7495.  
  7496.   if (head->htab && (prev == NULL || prev->literal))
  7497.     {
  7498.       struct bfd_elf_version_expr e;
  7499.  
  7500.       switch (prev ? prev->mask : 0)
  7501.         {
  7502.         case 0:
  7503.           if (head->mask & BFD_ELF_VERSION_C_TYPE)
  7504.             {
  7505.               e.pattern = c_sym;
  7506.               expr = (struct bfd_elf_version_expr *)
  7507.                   htab_find ((htab_t) head->htab, &e);
  7508.               while (expr && strcmp (expr->pattern, c_sym) == 0)
  7509.                 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
  7510.                   goto out_ret;
  7511.                 else
  7512.                   expr = expr->next;
  7513.             }
  7514.           /* Fallthrough */
  7515.         case BFD_ELF_VERSION_C_TYPE:
  7516.           if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
  7517.             {
  7518.               e.pattern = cxx_sym;
  7519.               expr = (struct bfd_elf_version_expr *)
  7520.                   htab_find ((htab_t) head->htab, &e);
  7521.               while (expr && strcmp (expr->pattern, cxx_sym) == 0)
  7522.                 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
  7523.                   goto out_ret;
  7524.                 else
  7525.                   expr = expr->next;
  7526.             }
  7527.           /* Fallthrough */
  7528.         case BFD_ELF_VERSION_CXX_TYPE:
  7529.           if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
  7530.             {
  7531.               e.pattern = java_sym;
  7532.               expr = (struct bfd_elf_version_expr *)
  7533.                   htab_find ((htab_t) head->htab, &e);
  7534.               while (expr && strcmp (expr->pattern, java_sym) == 0)
  7535.                 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
  7536.                   goto out_ret;
  7537.                 else
  7538.                   expr = expr->next;
  7539.             }
  7540.           /* Fallthrough */
  7541.         default:
  7542.           break;
  7543.         }
  7544.     }
  7545.  
  7546.   /* Finally, try the wildcards.  */
  7547.   if (prev == NULL || prev->literal)
  7548.     expr = head->remaining;
  7549.   else
  7550.     expr = prev->next;
  7551.   for (; expr; expr = expr->next)
  7552.     {
  7553.       const char *s;
  7554.  
  7555.       if (!expr->pattern)
  7556.         continue;
  7557.  
  7558.       if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
  7559.         break;
  7560.  
  7561.       if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
  7562.         s = java_sym;
  7563.       else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
  7564.         s = cxx_sym;
  7565.       else
  7566.         s = c_sym;
  7567.       if (fnmatch (expr->pattern, s, 0) == 0)
  7568.         break;
  7569.     }
  7570.  
  7571.  out_ret:
  7572.   if (c_sym != sym)
  7573.     free ((char *) c_sym);
  7574.   if (cxx_sym != sym)
  7575.     free ((char *) cxx_sym);
  7576.   if (java_sym != sym)
  7577.     free ((char *) java_sym);
  7578.   return expr;
  7579. }
  7580.  
  7581. /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
  7582.    return a pointer to the symbol name with any backslash quotes removed.  */
  7583.  
  7584. static const char *
  7585. realsymbol (const char *pattern)
  7586. {
  7587.   const char *p;
  7588.   bfd_boolean changed = FALSE, backslash = FALSE;
  7589.   char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1);
  7590.  
  7591.   for (p = pattern, s = symbol; *p != '\0'; ++p)
  7592.     {
  7593.       /* It is a glob pattern only if there is no preceding
  7594.          backslash.  */
  7595.       if (backslash)
  7596.         {
  7597.           /* Remove the preceding backslash.  */
  7598.           *(s - 1) = *p;
  7599.           backslash = FALSE;
  7600.           changed = TRUE;
  7601.         }
  7602.       else
  7603.         {
  7604.           if (*p == '?' || *p == '*' || *p == '[')
  7605.             {
  7606.               free (symbol);
  7607.               return NULL;
  7608.             }
  7609.  
  7610.           *s++ = *p;
  7611.           backslash = *p == '\\';
  7612.         }
  7613.     }
  7614.  
  7615.   if (changed)
  7616.     {
  7617.       *s = '\0';
  7618.       return symbol;
  7619.     }
  7620.   else
  7621.     {
  7622.       free (symbol);
  7623.       return pattern;
  7624.     }
  7625. }
  7626.  
  7627. /* This is called for each variable name or match expression.  NEW_NAME is
  7628.    the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
  7629.    pattern to be matched against symbol names.  */
  7630.  
  7631. struct bfd_elf_version_expr *
  7632. lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
  7633.                        const char *new_name,
  7634.                        const char *lang,
  7635.                        bfd_boolean literal_p)
  7636. {
  7637.   struct bfd_elf_version_expr *ret;
  7638.  
  7639.   ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
  7640.   ret->next = orig;
  7641.   ret->symver = 0;
  7642.   ret->script = 0;
  7643.   ret->literal = TRUE;
  7644.   ret->pattern = literal_p ? new_name : realsymbol (new_name);
  7645.   if (ret->pattern == NULL)
  7646.     {
  7647.       ret->pattern = new_name;
  7648.       ret->literal = FALSE;
  7649.     }
  7650.  
  7651.   if (lang == NULL || strcasecmp (lang, "C") == 0)
  7652.     ret->mask = BFD_ELF_VERSION_C_TYPE;
  7653.   else if (strcasecmp (lang, "C++") == 0)
  7654.     ret->mask = BFD_ELF_VERSION_CXX_TYPE;
  7655.   else if (strcasecmp (lang, "Java") == 0)
  7656.     ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
  7657.   else
  7658.     {
  7659.       einfo (_("%X%P: unknown language `%s' in version information\n"),
  7660.              lang);
  7661.       ret->mask = BFD_ELF_VERSION_C_TYPE;
  7662.     }
  7663.  
  7664.   return ldemul_new_vers_pattern (ret);
  7665. }
  7666.  
  7667. /* This is called for each set of variable names and match
  7668.    expressions.  */
  7669.  
  7670. struct bfd_elf_version_tree *
  7671. lang_new_vers_node (struct bfd_elf_version_expr *globals,
  7672.                     struct bfd_elf_version_expr *locals)
  7673. {
  7674.   struct bfd_elf_version_tree *ret;
  7675.  
  7676.   ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret);
  7677.   ret->globals.list = globals;
  7678.   ret->locals.list = locals;
  7679.   ret->match = lang_vers_match;
  7680.   ret->name_indx = (unsigned int) -1;
  7681.   return ret;
  7682. }
  7683.  
  7684. /* This static variable keeps track of version indices.  */
  7685.  
  7686. static int version_index;
  7687.  
  7688. static hashval_t
  7689. version_expr_head_hash (const void *p)
  7690. {
  7691.   const struct bfd_elf_version_expr *e =
  7692.       (const struct bfd_elf_version_expr *) p;
  7693.  
  7694.   return htab_hash_string (e->pattern);
  7695. }
  7696.  
  7697. static int
  7698. version_expr_head_eq (const void *p1, const void *p2)
  7699. {
  7700.   const struct bfd_elf_version_expr *e1 =
  7701.       (const struct bfd_elf_version_expr *) p1;
  7702.   const struct bfd_elf_version_expr *e2 =
  7703.       (const struct bfd_elf_version_expr *) p2;
  7704.  
  7705.   return strcmp (e1->pattern, e2->pattern) == 0;
  7706. }
  7707.  
  7708. static void
  7709. lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
  7710. {
  7711.   size_t count = 0;
  7712.   struct bfd_elf_version_expr *e, *next;
  7713.   struct bfd_elf_version_expr **list_loc, **remaining_loc;
  7714.  
  7715.   for (e = head->list; e; e = e->next)
  7716.     {
  7717.       if (e->literal)
  7718.         count++;
  7719.       head->mask |= e->mask;
  7720.     }
  7721.  
  7722.   if (count)
  7723.     {
  7724.       head->htab = htab_create (count * 2, version_expr_head_hash,
  7725.                                 version_expr_head_eq, NULL);
  7726.       list_loc = &head->list;
  7727.       remaining_loc = &head->remaining;
  7728.       for (e = head->list; e; e = next)
  7729.         {
  7730.           next = e->next;
  7731.           if (!e->literal)
  7732.             {
  7733.               *remaining_loc = e;
  7734.               remaining_loc = &e->next;
  7735.             }
  7736.           else
  7737.             {
  7738.               void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT);
  7739.  
  7740.               if (*loc)
  7741.                 {
  7742.                   struct bfd_elf_version_expr *e1, *last;
  7743.  
  7744.                   e1 = (struct bfd_elf_version_expr *) *loc;
  7745.                   last = NULL;
  7746.                   do
  7747.                     {
  7748.                       if (e1->mask == e->mask)
  7749.                         {
  7750.                           last = NULL;
  7751.                           break;
  7752.                         }
  7753.                       last = e1;
  7754.                       e1 = e1->next;
  7755.                     }
  7756.                   while (e1 && strcmp (e1->pattern, e->pattern) == 0);
  7757.  
  7758.                   if (last == NULL)
  7759.                     {
  7760.                       /* This is a duplicate.  */
  7761.                       /* FIXME: Memory leak.  Sometimes pattern is not
  7762.                          xmalloced alone, but in larger chunk of memory.  */
  7763.                       /* free (e->pattern); */
  7764.                       free (e);
  7765.                     }
  7766.                   else
  7767.                     {
  7768.                       e->next = last->next;
  7769.                       last->next = e;
  7770.                     }
  7771.                 }
  7772.               else
  7773.                 {
  7774.                   *loc = e;
  7775.                   *list_loc = e;
  7776.                   list_loc = &e->next;
  7777.                 }
  7778.             }
  7779.         }
  7780.       *remaining_loc = NULL;
  7781.       *list_loc = head->remaining;
  7782.     }
  7783.   else
  7784.     head->remaining = head->list;
  7785. }
  7786.  
  7787. /* This is called when we know the name and dependencies of the
  7788.    version.  */
  7789.  
  7790. void
  7791. lang_register_vers_node (const char *name,
  7792.                          struct bfd_elf_version_tree *version,
  7793.                          struct bfd_elf_version_deps *deps)
  7794. {
  7795.   struct bfd_elf_version_tree *t, **pp;
  7796.   struct bfd_elf_version_expr *e1;
  7797.  
  7798.   if (name == NULL)
  7799.     name = "";
  7800.  
  7801.   if (link_info.version_info != NULL
  7802.       && (name[0] == '\0' || link_info.version_info->name[0] == '\0'))
  7803.     {
  7804.       einfo (_("%X%P: anonymous version tag cannot be combined"
  7805.                " with other version tags\n"));
  7806.       free (version);
  7807.       return;
  7808.     }
  7809.  
  7810.   /* Make sure this node has a unique name.  */
  7811.   for (t = link_info.version_info; t != NULL; t = t->next)
  7812.     if (strcmp (t->name, name) == 0)
  7813.       einfo (_("%X%P: duplicate version tag `%s'\n"), name);
  7814.  
  7815.   lang_finalize_version_expr_head (&version->globals);
  7816.   lang_finalize_version_expr_head (&version->locals);
  7817.  
  7818.   /* Check the global and local match names, and make sure there
  7819.      aren't any duplicates.  */
  7820.  
  7821.   for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
  7822.     {
  7823.       for (t = link_info.version_info; t != NULL; t = t->next)
  7824.         {
  7825.           struct bfd_elf_version_expr *e2;
  7826.  
  7827.           if (t->locals.htab && e1->literal)
  7828.             {
  7829.               e2 = (struct bfd_elf_version_expr *)
  7830.                   htab_find ((htab_t) t->locals.htab, e1);
  7831.               while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
  7832.                 {
  7833.                   if (e1->mask == e2->mask)
  7834.                     einfo (_("%X%P: duplicate expression `%s'"
  7835.                              " in version information\n"), e1->pattern);
  7836.                   e2 = e2->next;
  7837.                 }
  7838.             }
  7839.           else if (!e1->literal)
  7840.             for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
  7841.               if (strcmp (e1->pattern, e2->pattern) == 0
  7842.                   && e1->mask == e2->mask)
  7843.                 einfo (_("%X%P: duplicate expression `%s'"
  7844.                          " in version information\n"), e1->pattern);
  7845.         }
  7846.     }
  7847.  
  7848.   for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
  7849.     {
  7850.       for (t = link_info.version_info; t != NULL; t = t->next)
  7851.         {
  7852.           struct bfd_elf_version_expr *e2;
  7853.  
  7854.           if (t->globals.htab && e1->literal)
  7855.             {
  7856.               e2 = (struct bfd_elf_version_expr *)
  7857.                   htab_find ((htab_t) t->globals.htab, e1);
  7858.               while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
  7859.                 {
  7860.                   if (e1->mask == e2->mask)
  7861.                     einfo (_("%X%P: duplicate expression `%s'"
  7862.                              " in version information\n"),
  7863.                            e1->pattern);
  7864.                   e2 = e2->next;
  7865.                 }
  7866.             }
  7867.           else if (!e1->literal)
  7868.             for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
  7869.               if (strcmp (e1->pattern, e2->pattern) == 0
  7870.                   && e1->mask == e2->mask)
  7871.                 einfo (_("%X%P: duplicate expression `%s'"
  7872.                          " in version information\n"), e1->pattern);
  7873.         }
  7874.     }
  7875.  
  7876.   version->deps = deps;
  7877.   version->name = name;
  7878.   if (name[0] != '\0')
  7879.     {
  7880.       ++version_index;
  7881.       version->vernum = version_index;
  7882.     }
  7883.   else
  7884.     version->vernum = 0;
  7885.  
  7886.   for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next)
  7887.     ;
  7888.   *pp = version;
  7889. }
  7890.  
  7891. /* This is called when we see a version dependency.  */
  7892.  
  7893. struct bfd_elf_version_deps *
  7894. lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
  7895. {
  7896.   struct bfd_elf_version_deps *ret;
  7897.   struct bfd_elf_version_tree *t;
  7898.  
  7899.   ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
  7900.   ret->next = list;
  7901.  
  7902.   for (t = link_info.version_info; t != NULL; t = t->next)
  7903.     {
  7904.       if (strcmp (t->name, name) == 0)
  7905.         {
  7906.           ret->version_needed = t;
  7907.           return ret;
  7908.         }
  7909.     }
  7910.  
  7911.   einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
  7912.  
  7913.   ret->version_needed = NULL;
  7914.   return ret;
  7915. }
  7916.  
  7917. static void
  7918. lang_do_version_exports_section (void)
  7919. {
  7920.   struct bfd_elf_version_expr *greg = NULL, *lreg;
  7921.  
  7922.   LANG_FOR_EACH_INPUT_STATEMENT (is)
  7923.     {
  7924.       asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
  7925.       char *contents, *p;
  7926.       bfd_size_type len;
  7927.  
  7928.       if (sec == NULL)
  7929.         continue;
  7930.  
  7931.       len = sec->size;
  7932.       contents = (char *) xmalloc (len);
  7933.       if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
  7934.         einfo (_("%X%P: unable to read .exports section contents\n"), sec);
  7935.  
  7936.       p = contents;
  7937.       while (p < contents + len)
  7938.         {
  7939.           greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
  7940.           p = strchr (p, '\0') + 1;
  7941.         }
  7942.  
  7943.       /* Do not free the contents, as we used them creating the regex.  */
  7944.  
  7945.       /* Do not include this section in the link.  */
  7946.       sec->flags |= SEC_EXCLUDE | SEC_KEEP;
  7947.     }
  7948.  
  7949.   lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
  7950.   lang_register_vers_node (command_line.version_exports_section,
  7951.                            lang_new_vers_node (greg, lreg), NULL);
  7952. }
  7953.  
  7954. void
  7955. lang_add_unique (const char *name)
  7956. {
  7957.   struct unique_sections *ent;
  7958.  
  7959.   for (ent = unique_section_list; ent; ent = ent->next)
  7960.     if (strcmp (ent->name, name) == 0)
  7961.       return;
  7962.  
  7963.   ent = (struct unique_sections *) xmalloc (sizeof *ent);
  7964.   ent->name = xstrdup (name);
  7965.   ent->next = unique_section_list;
  7966.   unique_section_list = ent;
  7967. }
  7968.  
  7969. /* Append the list of dynamic symbols to the existing one.  */
  7970.  
  7971. void
  7972. lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic)
  7973. {
  7974.   if (link_info.dynamic_list)
  7975.     {
  7976.       struct bfd_elf_version_expr *tail;
  7977.       for (tail = dynamic; tail->next != NULL; tail = tail->next)
  7978.         ;
  7979.       tail->next = link_info.dynamic_list->head.list;
  7980.       link_info.dynamic_list->head.list = dynamic;
  7981.     }
  7982.   else
  7983.     {
  7984.       struct bfd_elf_dynamic_list *d;
  7985.  
  7986.       d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d);
  7987.       d->head.list = dynamic;
  7988.       d->match = lang_vers_match;
  7989.       link_info.dynamic_list = d;
  7990.     }
  7991. }
  7992.  
  7993. /* Append the list of C++ typeinfo dynamic symbols to the existing
  7994.    one.  */
  7995.  
  7996. void
  7997. lang_append_dynamic_list_cpp_typeinfo (void)
  7998. {
  7999.   const char * symbols [] =
  8000.     {
  8001.       "typeinfo name for*",
  8002.       "typeinfo for*"
  8003.     };
  8004.   struct bfd_elf_version_expr *dynamic = NULL;
  8005.   unsigned int i;
  8006.  
  8007.   for (i = 0; i < ARRAY_SIZE (symbols); i++)
  8008.     dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
  8009.                                      FALSE);
  8010.  
  8011.   lang_append_dynamic_list (dynamic);
  8012. }
  8013.  
  8014. /* Append the list of C++ operator new and delete dynamic symbols to the
  8015.    existing one.  */
  8016.  
  8017. void
  8018. lang_append_dynamic_list_cpp_new (void)
  8019. {
  8020.   const char * symbols [] =
  8021.     {
  8022.       "operator new*",
  8023.       "operator delete*"
  8024.     };
  8025.   struct bfd_elf_version_expr *dynamic = NULL;
  8026.   unsigned int i;
  8027.  
  8028.   for (i = 0; i < ARRAY_SIZE (symbols); i++)
  8029.     dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
  8030.                                      FALSE);
  8031.  
  8032.   lang_append_dynamic_list (dynamic);
  8033. }
  8034.  
  8035. /* Scan a space and/or comma separated string of features.  */
  8036.  
  8037. void
  8038. lang_ld_feature (char *str)
  8039. {
  8040.   char *p, *q;
  8041.  
  8042.   p = str;
  8043.   while (*p)
  8044.     {
  8045.       char sep;
  8046.       while (*p == ',' || ISSPACE (*p))
  8047.         ++p;
  8048.       if (!*p)
  8049.         break;
  8050.       q = p + 1;
  8051.       while (*q && *q != ',' && !ISSPACE (*q))
  8052.         ++q;
  8053.       sep = *q;
  8054.       *q = 0;
  8055.       if (strcasecmp (p, "SANE_EXPR") == 0)
  8056.         config.sane_expr = TRUE;
  8057.       else
  8058.         einfo (_("%X%P: unknown feature `%s'\n"), p);
  8059.       *q = sep;
  8060.       p = q;
  8061.     }
  8062. }
  8063.