Fix: Segmentation fault caused by npd in objdump
[binutils-gdb.git] / ld / emultempl / xtensaelf.em
blobf2d10b51d13ab19c4551f2580876c33981a8f5b9
1 # This shell script emits a C file. -*- C -*-
2 #   Copyright (C) 2003-2023 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
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.
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.
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.
22 # This file is sourced from elf.em, and defines extra xtensa-elf
23 # specific routines.
25 fragment <<EOF
27 #include <xtensa-dynconfig.h>
28 #include "../bfd/elf-bfd.h"
29 #include "elf/xtensa.h"
30 #include "bfd.h"
32 /* Provide default values for new configuration settings.  */
33 #ifndef XTHAL_ABI_UNDEFINED
34 #define XTHAL_ABI_UNDEFINED -1
35 #endif
37 static void xtensa_wild_group_interleave (lang_statement_union_type *);
38 static void xtensa_colocate_output_literals (lang_statement_union_type *);
39 static void xtensa_strip_inconsistent_linkonce_sections
40   (lang_statement_list_type *);
43 /* This number is irrelevant until we turn on use_literal_pages */
44 static bfd_vma xtensa_page_power = 12; /* 4K pages.  */
46 /* To force a page break between literals and text, change
47    xtensa_use_literal_pages to "true".  */
48 static bool xtensa_use_literal_pages = false;
50 #define EXTRA_VALIDATION 0
52 /* Xtensa ABI.
53    This option is defined in BDF library.  */
54 extern int elf32xtensa_abi;
57 static char *
58 elf_xtensa_choose_target (int argc ATTRIBUTE_UNUSED,
59                           char **argv ATTRIBUTE_UNUSED)
61   if (XCHAL_HAVE_BE)
62     return "${BIG_OUTPUT_FORMAT}";
63   else
64     return "${LITTLE_OUTPUT_FORMAT}";
68 static void
69 elf_xtensa_before_parse (void)
71   /* Just call the default hook.... Tensilica's version of this function
72      does some other work that isn't relevant here.  */
73   gld${EMULATION_NAME}_before_parse ();
77 static void
78 remove_section (bfd *abfd, asection *os)
80   asection **spp;
81   for (spp = &abfd->sections; *spp; spp = &(*spp)->next)
82     if (*spp == os)
83       {
84         *spp = os->next;
85         os->owner->section_count--;
86         break;
87       }
91 static bool
92 replace_insn_sec_with_prop_sec (bfd *abfd,
93                                 const char *insn_sec_name,
94                                 const char *prop_sec_name,
95                                 char **error_message)
97   asection *insn_sec;
98   asection *prop_sec;
99   bfd_byte *prop_contents = NULL;
100   bfd_byte *insn_contents = NULL;
101   unsigned entry_count;
102   unsigned entry;
103   Elf_Internal_Shdr *rel_hdr;
104   Elf_Internal_Rela *internal_relocs = NULL;
105   unsigned reloc_count;
107   *error_message = "";
108   insn_sec = bfd_get_section_by_name (abfd, insn_sec_name);
109   if (insn_sec == NULL)
110     return true;
111   entry_count = insn_sec->size / 8;
113   prop_sec = bfd_get_section_by_name (abfd, prop_sec_name);
114   if (prop_sec != NULL && insn_sec != NULL)
115     {
116       *error_message = _("file already has property tables");
117       return false;
118     }
120   if (insn_sec->size != 0)
121     {
122       insn_contents = (bfd_byte *) xmalloc (insn_sec->size);
123       if (! bfd_get_section_contents (abfd, insn_sec, insn_contents,
124                                       (file_ptr) 0, insn_sec->size))
125         {
126           *error_message = _("failed to read section contents");
127           goto cleanup;
128         }
129     }
131   /* Create a property table section for it.  */
132   prop_sec_name = strdup (prop_sec_name);
133   prop_sec = bfd_make_section_with_flags
134     (abfd, prop_sec_name, bfd_section_flags (insn_sec));
135   if (prop_sec == NULL
136       || !bfd_set_section_alignment (prop_sec, 2))
137     {
138       *error_message = _("could not create new section");
139       goto cleanup;
140     }
142   prop_sec->size = entry_count * 12;
143   prop_contents = (bfd_byte *) bfd_zalloc (abfd, prop_sec->size);
144   elf_section_data (prop_sec)->this_hdr.contents = prop_contents;
146   /* The entry size and size must be set to allow the linker to compute
147      the number of relocations since it does not use reloc_count.  */
148   rel_hdr = _bfd_elf_single_rel_hdr (prop_sec);
149   rel_hdr->sh_entsize = sizeof (Elf32_External_Rela);
150   rel_hdr->sh_size = _bfd_elf_single_rel_hdr (insn_sec)->sh_size;
152   if (prop_contents == NULL && prop_sec->size != 0)
153     {
154       *error_message = _("could not allocate section contents");
155       goto cleanup;
156     }
158   /* Read the relocations.  */
159   reloc_count = insn_sec->reloc_count;
160   if (reloc_count != 0)
161     {
162       /* If there is already an internal_reloc, then save it so that the
163          read_relocs function freshly allocates a copy.  */
164       Elf_Internal_Rela *saved_relocs = elf_section_data (insn_sec)->relocs;
166       elf_section_data (insn_sec)->relocs = NULL;
167       internal_relocs =
168         _bfd_elf_link_read_relocs (abfd, insn_sec, NULL, NULL, false);
169       elf_section_data (insn_sec)->relocs = saved_relocs;
171       if (internal_relocs == NULL)
172         {
173           *error_message = _("out of memory");
174           goto cleanup;
175         }
176     }
178   /* Create a relocation section for the property section.  */
179   if (internal_relocs != NULL)
180     {
181       elf_section_data (prop_sec)->relocs = internal_relocs;
182       prop_sec->reloc_count = reloc_count;
183     }
185   /* Now copy each insn table entry to the prop table entry with
186      appropriate flags.  */
187   for (entry = 0; entry < entry_count; ++entry)
188     {
189       unsigned value;
190       unsigned flags = (XTENSA_PROP_INSN | XTENSA_PROP_NO_TRANSFORM
191                         | XTENSA_PROP_INSN_NO_REORDER);
192       value = bfd_get_32 (abfd, insn_contents + entry * 8 + 0);
193       bfd_put_32 (abfd, value, prop_contents + entry * 12 + 0);
194       value = bfd_get_32 (abfd, insn_contents + entry * 8 + 4);
195       bfd_put_32 (abfd, value, prop_contents + entry * 12 + 4);
196       bfd_put_32 (abfd, flags, prop_contents + entry * 12 + 8);
197     }
199   /* Now copy all of the relocations.  Change offsets for the
200      instruction table section to offsets in the property table
201      section.  */
202   if (internal_relocs)
203     {
204       unsigned i;
206       for (i = 0; i < reloc_count; i++)
207         {
208           Elf_Internal_Rela *rela;
209           unsigned r_offset;
211           rela = &internal_relocs[i];
213           /* If this relocation is to the .xt.insn section,
214              change the section number and the offset.  */
215           r_offset = rela->r_offset;
216           r_offset += 4 * (r_offset / 8);
217           rela->r_offset = r_offset;
218         }
219     }
221   remove_section (abfd, insn_sec);
223   free (insn_contents);
225   return true;
227  cleanup:
228   if (prop_sec && prop_sec->owner)
229     remove_section (abfd, prop_sec);
230   free (insn_contents);
231   free (internal_relocs);
233   return false;
237 #define PROP_SEC_BASE_NAME ".xt.prop"
238 #define INSN_SEC_BASE_NAME ".xt.insn"
239 #define LINKONCE_SEC_OLD_TEXT_BASE_NAME ".gnu.linkonce.x."
242 static void
243 replace_instruction_table_sections (bfd *abfd, asection *sec)
245   char *message = "";
246   const char *insn_sec_name = NULL;
247   char *prop_sec_name = NULL;
248   char *owned_prop_sec_name = NULL;
249   const char *sec_name;
251   sec_name = bfd_section_name (sec);
252   if (strcmp (sec_name, INSN_SEC_BASE_NAME) == 0)
253     {
254       insn_sec_name = INSN_SEC_BASE_NAME;
255       prop_sec_name = PROP_SEC_BASE_NAME;
256     }
257   else if (startswith (sec_name, LINKONCE_SEC_OLD_TEXT_BASE_NAME))
258     {
259       insn_sec_name = sec_name;
260       owned_prop_sec_name = (char *) xmalloc (strlen (sec_name) + 20);
261       prop_sec_name = owned_prop_sec_name;
262       strcpy (prop_sec_name, ".gnu.linkonce.prop.t.");
263       strcat (prop_sec_name,
264               sec_name + strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME));
265     }
266   if (insn_sec_name != NULL)
267     {
268       if (! replace_insn_sec_with_prop_sec (abfd, insn_sec_name, prop_sec_name,
269                                             &message))
270         {
271           einfo (_("%P: warning: failed to convert %s table in %pB (%s); subsequent disassembly may be incomplete\n"),
272                  insn_sec_name, abfd, message);
273         }
274     }
275   free (owned_prop_sec_name);
279 /* This is called after all input sections have been opened to convert
280    instruction tables (.xt.insn, gnu.linkonce.x.*) tables into property
281    tables (.xt.prop) before any section placement.  */
283 static void
284 elf_xtensa_after_open (void)
286   /* First call the ELF version.  */
287   gld${EMULATION_NAME}_after_open ();
289   /* Now search the input files looking for instruction table sections.  */
290   LANG_FOR_EACH_INPUT_STATEMENT (f)
291     {
292       asection *sec = f->the_bfd->sections;
293       asection *next_sec;
295       /* Do not use bfd_map_over_sections here since we are removing
296          sections as we iterate.  */
297       while (sec != NULL)
298         {
299           next_sec = sec->next;
300           replace_instruction_table_sections (f->the_bfd, sec);
301           sec = next_sec;
302         }
303     }
307 static bool
308 xt_config_info_unpack_and_check (char *data,
309                                  bool *pmismatch,
310                                  char **pmsg)
312   char *d, *key;
313   int num;
315   *pmismatch = false;
317   d = data;
318   while (*d)
319     {
320       key = d;
321       d = strchr (d, '=');
322       if (! d)
323         goto error;
325       /* Overwrite the equal sign.  */
326       *d++ = 0;
328       /* Check if this is a quoted string or a number.  */
329       if (*d == '"')
330         {
331           /* No string values are currently checked by LD;
332              just skip over the quotes.  */
333           d++;
334           d = strchr (d, '"');
335           if (! d)
336             goto error;
337           /* Overwrite the trailing quote.  */
338           *d++ = 0;
339         }
340       else
341         {
342           if (*d == 0)
343             goto error;
344           num = strtoul (d, &d, 0);
346           if (! strcmp (key, "ABI"))
347             {
348               if (elf32xtensa_abi == XTHAL_ABI_UNDEFINED)
349                 {
350                   elf32xtensa_abi = num;
351                 }
352               else if (num != elf32xtensa_abi)
353                 {
354                   *pmismatch = true;
355                   *pmsg = "ABI does not match";
356                 }
357             }
358           else if (! strcmp (key, "USE_ABSOLUTE_LITERALS"))
359             {
360               if (num != XSHAL_USE_ABSOLUTE_LITERALS)
361                 {
362                   *pmismatch = true;
363                   *pmsg = "incompatible use of the Extended L32R option";
364                 }
365             }
366         }
368       if (*d++ != '\n')
369         goto error;
370     }
372   return true;
374  error:
375   return false;
379 #define XTINFO_NAME "Xtensa_Info"
380 #define XTINFO_NAMESZ 12
381 #define XTINFO_TYPE 1
383 static void
384 check_xtensa_info (bfd *abfd, asection *info_sec)
386   char *data, *errmsg = "";
387   bool mismatch;
389   data = xmalloc (info_sec->size);
390   if (! bfd_get_section_contents (abfd, info_sec, data, 0, info_sec->size))
391     einfo (_("%F%P: %pB: cannot read contents of section %pA\n"), abfd, info_sec);
393   if (info_sec->size > 24
394       && info_sec->size >= 24 + bfd_get_32 (abfd, data + 4)
395       && bfd_get_32 (abfd, data + 0) == XTINFO_NAMESZ
396       && bfd_get_32 (abfd, data + 8) == XTINFO_TYPE
397       && strcmp (data + 12, XTINFO_NAME) == 0
398       && xt_config_info_unpack_and_check (data + 12 + XTINFO_NAMESZ,
399                                           &mismatch, &errmsg))
400     {
401       if (mismatch)
402         einfo (_("%P: %pB: warning: incompatible Xtensa configuration (%s)\n"),
403                abfd, errmsg);
404     }
405   else
406     einfo (_("%P: %pB: warning: cannot parse .xtensa.info section\n"), abfd);
408   free (data);
412 /* This is called after the sections have been attached to output
413    sections, but before any sizes or addresses have been set.  */
415 static void
416 elf_xtensa_before_allocation (void)
418   asection *info_sec, *first_info_sec;
419   bfd *first_bfd;
420   bool is_big_endian = XCHAL_HAVE_BE;
422   /* Check that the output endianness matches the Xtensa
423      configuration.  The BFD library always includes both big and
424      little endian target vectors for Xtensa, but it only supports the
425      detailed instruction encode/decode operations (such as are
426      required to process relocations) for the selected Xtensa
427      configuration.  */
429   if (is_big_endian
430       && link_info.output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
431     {
432       einfo (_("%F%P: little endian output does not match "
433                "Xtensa configuration\n"));
434     }
435   if (!is_big_endian
436       && link_info.output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
437     {
438       einfo (_("%F%P: big endian output does not match "
439                "Xtensa configuration\n"));
440     }
442   /* Keep track of the first input .xtensa.info section, and as a fallback,
443      the first input bfd where a .xtensa.info section could be created.
444      After the input .xtensa.info has been checked, the contents of the
445      first one will be replaced with the output .xtensa.info table.  */
446   first_info_sec = 0;
447   first_bfd = 0;
449   LANG_FOR_EACH_INPUT_STATEMENT (f)
450     {
451       /* Check that the endianness for each input file matches the output.
452          The merge_private_bfd_data hook has already reported any mismatches
453          as errors, but those errors are not fatal.  At this point, we
454          cannot go any further if there are any mismatches.  */
455       if ((is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
456           || (!is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
457         einfo (_("%F%P: cross-endian linking for %pB not supported\n"),
458                f->the_bfd);
460       if (! first_bfd)
461         first_bfd = f->the_bfd;
463       info_sec = bfd_get_section_by_name (f->the_bfd, ".xtensa.info");
464       if (! info_sec)
465         continue;
467       if (! first_info_sec)
468         first_info_sec = info_sec;
470       /* Unpack the .xtensa.info section and check it against the current
471          Xtensa configuration.  */
472       check_xtensa_info (f->the_bfd, info_sec);
474       /* Do not include this copy of .xtensa.info in the output.  */
475       info_sec->size = 0;
476       info_sec->flags |= SEC_EXCLUDE;
477     }
479   /* Reuse the first .xtensa.info input section to hold the output
480      .xtensa.info; or, if none were found, create a new section in the
481      first input bfd (assuming there is one).  */
482   info_sec = first_info_sec;
483   if (! info_sec && first_bfd)
484     {
485       info_sec = bfd_make_section_with_flags (first_bfd, ".xtensa.info",
486                                               SEC_HAS_CONTENTS | SEC_READONLY);
487       if (! info_sec)
488         einfo (_("%F%P: failed to create .xtensa.info section\n"));
489     }
490   if (info_sec)
491     {
492       int xtensa_info_size;
493       char data[100];
495       info_sec->flags &= ~SEC_EXCLUDE;
496       info_sec->flags |= SEC_IN_MEMORY;
498       xtensa_info_size
499         = 1 + sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n",
500                        XSHAL_USE_ABSOLUTE_LITERALS, xtensa_abi_choice ());
502       /* Add enough null terminators to pad to a word boundary.  */
503       do
504         data[xtensa_info_size++] = 0;
505       while ((xtensa_info_size & 3) != 0);
507       info_sec->size = 12 + XTINFO_NAMESZ + xtensa_info_size;
508       info_sec->contents = xmalloc (info_sec->size);
509       bfd_put_32 (info_sec->owner, XTINFO_NAMESZ, info_sec->contents + 0);
510       bfd_put_32 (info_sec->owner, xtensa_info_size, info_sec->contents + 4);
511       bfd_put_32 (info_sec->owner, XTINFO_TYPE, info_sec->contents + 8);
512       memcpy (info_sec->contents + 12, XTINFO_NAME, XTINFO_NAMESZ);
513       memcpy (info_sec->contents + 12 + XTINFO_NAMESZ, data, xtensa_info_size);
514     }
516   /* Enable relaxation by default if the "--no-relax" option was not
517      specified.  This is done here instead of in the before_parse hook
518      because there is a check in main() to prohibit use of --relax and
519      -r together and that combination should be allowed for Xtensa.  */
520   if (RELAXATION_DISABLED_BY_DEFAULT)
521     ENABLE_RELAXATION;
523   xtensa_strip_inconsistent_linkonce_sections (stat_ptr);
525   gld${EMULATION_NAME}_before_allocation ();
527   xtensa_wild_group_interleave (stat_ptr->head);
529   if (RELAXATION_ENABLED)
530     xtensa_colocate_output_literals (stat_ptr->head);
532   /* TBD: We need to force the page alignments to here and only do
533      them as needed for the entire output section.  Finally, if this
534      is a relocatable link then we need to add alignment notes so
535      that the literals can be separated later.  */
539 typedef struct wildcard_list section_name_list;
541 typedef struct reloc_deps_e_t reloc_deps_e;
542 typedef struct reloc_deps_section_t reloc_deps_section;
543 typedef struct reloc_deps_graph_t reloc_deps_graph;
546 struct reloc_deps_e_t
548   asection *src; /* Contains l32rs.  */
549   asection *tgt; /* Contains literals.  */
550   reloc_deps_e *next;
553 /* Place these in the userdata field.  */
554 struct reloc_deps_section_t
556   reloc_deps_e *preds;
557   reloc_deps_e *succs;
558   bool is_only_literal;
562 struct reloc_deps_graph_t
564   size_t count;
565   size_t size;
566   asection **sections;
569 static void xtensa_layout_wild
570   (const reloc_deps_graph *, lang_wild_statement_type *);
572 typedef void (*deps_callback_t) (asection *, /* src_sec */
573                                  bfd_vma,    /* src_offset */
574                                  asection *, /* target_sec */
575                                  bfd_vma,    /* target_offset */
576                                  void *);    /* closure */
578 extern bool xtensa_callback_required_dependence
579   (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
580 static void xtensa_ldlang_clear_addresses (lang_statement_union_type *);
581 static bool ld_local_file_relocations_fit
582   (lang_statement_union_type *, const reloc_deps_graph *);
583 static bfd_vma ld_assign_relative_paged_dot
584   (bfd_vma, lang_statement_union_type *, const reloc_deps_graph *,
585    bool);
586 static bfd_vma ld_xtensa_insert_page_offsets
587   (bfd_vma, lang_statement_union_type *, reloc_deps_graph *, bool);
588 #if EXTRA_VALIDATION
589 static size_t ld_count_children (lang_statement_union_type *);
590 #endif
592 extern lang_statement_list_type constructor_list;
594 static reloc_deps_section *
595 xtensa_get_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
596                          asection *sec)
598   /* We have a separate function for this so that
599      we could in the future keep a completely independent
600      structure that maps a section to its dependence edges.
601      For now, we place these in the sec->userdata field.
602      This doesn't clash with ldlang.c use of userdata for output
603      sections, and during map output for input sections, since the
604      xtensa use is only for input sections and only extant in
605      before_allocation.  */
606   reloc_deps_section *sec_deps = bfd_section_userdata (sec);
607   return sec_deps;
610 static void
611 xtensa_set_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
612                          asection *sec,
613                          reloc_deps_section *deps_section)
615   bfd_set_section_userdata (sec, deps_section);
619 /* This is used to keep a list of all of the sections participating in
620    the graph so we can clean them up quickly.  */
622 static void
623 xtensa_append_section_deps (reloc_deps_graph *deps, asection *sec)
625   if (deps->size <= deps->count)
626     {
627       asection **new_sections;
628       size_t i;
629       size_t new_size;
631       new_size = deps->size * 2;
632       if (new_size == 0)
633         new_size = 20;
635       new_sections = xmalloc (sizeof (asection *) * new_size);
636       memset (new_sections, 0, sizeof (asection *) * new_size);
637       for (i = 0; i < deps->count; i++)
638         {
639           new_sections[i] = deps->sections[i];
640         }
641       free (deps->sections);
642       deps->sections = new_sections;
643       deps->size = new_size;
644     }
645   deps->sections[deps->count] = sec;
646   deps->count++;
650 static void
651 free_reloc_deps_graph (reloc_deps_graph *deps)
653   size_t i;
654   for (i = 0; i < deps->count; i++)
655     {
656       asection *sec = deps->sections[i];
657       reloc_deps_section *sec_deps;
658       sec_deps = xtensa_get_section_deps (deps, sec);
659       if (sec_deps)
660         {
661           reloc_deps_e *next;
662           while (sec_deps->succs != NULL)
663             {
664               next = sec_deps->succs->next;
665               free (sec_deps->succs);
666               sec_deps->succs = next;
667             }
669           while (sec_deps->preds != NULL)
670             {
671               next = sec_deps->preds->next;
672               free (sec_deps->preds);
673               sec_deps->preds = next;
674             }
675           free (sec_deps);
676         }
677       xtensa_set_section_deps (deps, sec, NULL);
678     }
679   free (deps->sections);
680   free (deps);
684 static bool
685 section_is_source (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
686                    lang_statement_union_type *s)
688   asection *sec;
689   const reloc_deps_section *sec_deps;
691   if (s->header.type != lang_input_section_enum)
692     return false;
693   sec = s->input_section.section;
695   sec_deps = xtensa_get_section_deps (deps, sec);
696   return sec_deps && sec_deps->succs != NULL;
700 static bool
701 section_is_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
702                    lang_statement_union_type *s)
704   asection *sec;
705   const reloc_deps_section *sec_deps;
707   if (s->header.type != lang_input_section_enum)
708     return false;
709   sec = s->input_section.section;
711   sec_deps = xtensa_get_section_deps (deps, sec);
712   return sec_deps && sec_deps->preds != NULL;
716 static bool
717 section_is_source_or_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
718                              lang_statement_union_type *s)
720   return (section_is_source (deps, s)
721           || section_is_target (deps, s));
725 typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
726 typedef struct xtensa_ld_iter_t xtensa_ld_iter;
728 struct xtensa_ld_iter_t
730   lang_statement_union_type *parent;    /* Parent of the list.  */
731   lang_statement_list_type *l;          /* List that holds it.  */
732   lang_statement_union_type **loc;      /* Place in the list.  */
735 struct xtensa_ld_iter_stack_t
737   xtensa_ld_iter iterloc;               /* List that hold it.  */
739   xtensa_ld_iter_stack *next;           /* Next in the stack.  */
740   xtensa_ld_iter_stack *prev;           /* Back pointer for stack.  */
744 static void
745 ld_xtensa_move_section_after (xtensa_ld_iter *to, xtensa_ld_iter *current)
747   lang_statement_union_type *to_next;
748   lang_statement_union_type *current_next;
749   lang_statement_union_type **e;
751 #if EXTRA_VALIDATION
752   size_t old_to_count, new_to_count;
753   size_t old_current_count, new_current_count;
754 #endif
756   if (to == current)
757     return;
759 #if EXTRA_VALIDATION
760   old_to_count = ld_count_children (to->parent);
761   old_current_count = ld_count_children (current->parent);
762 #endif
764   to_next = *(to->loc);
765   current_next = (*current->loc)->header.next;
767   *(to->loc) = *(current->loc);
769   *(current->loc) = current_next;
770   (*(to->loc))->header.next = to_next;
772   /* reset "to" list tail */
773   for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
774     ;
775   to->l->tail = e;
777   /* reset "current" list tail */
778   for (e = &current->l->head; *e != NULL; e = &(*e)->header.next)
779     ;
780   current->l->tail = e;
782 #if EXTRA_VALIDATION
783   new_to_count = ld_count_children (to->parent);
784   new_current_count = ld_count_children (current->parent);
786   ASSERT ((old_to_count + old_current_count)
787           == (new_to_count + new_current_count));
788 #endif
792 /* Can only be called with lang_statements that have lists.  Returns
793    FALSE if the list is empty.  */
795 static bool
796 iter_stack_empty (xtensa_ld_iter_stack **stack_p)
798   return *stack_p == NULL;
802 static bool
803 iter_stack_push (xtensa_ld_iter_stack **stack_p,
804                  lang_statement_union_type *parent)
806   xtensa_ld_iter_stack *stack;
807   lang_statement_list_type *l = NULL;
809   switch (parent->header.type)
810     {
811     case lang_output_section_statement_enum:
812       l = &parent->output_section_statement.children;
813       break;
814     case lang_wild_statement_enum:
815       l = &parent->wild_statement.children;
816       break;
817     case lang_group_statement_enum:
818       l = &parent->group_statement.children;
819       break;
820     default:
821       ASSERT (0);
822       return false;
823     }
825   /* Empty. do not push.  */
826   if (l->tail == &l->head)
827     return false;
829   stack = xmalloc (sizeof (xtensa_ld_iter_stack));
830   memset (stack, 0, sizeof (xtensa_ld_iter_stack));
831   stack->iterloc.parent = parent;
832   stack->iterloc.l = l;
833   stack->iterloc.loc = &l->head;
835   stack->next = *stack_p;
836   stack->prev = NULL;
837   if (*stack_p != NULL)
838     (*stack_p)->prev = stack;
839   *stack_p = stack;
840   return true;
844 static void
845 iter_stack_pop (xtensa_ld_iter_stack **stack_p)
847   xtensa_ld_iter_stack *stack;
849   stack = *stack_p;
851   if (stack == NULL)
852     {
853       ASSERT (stack != NULL);
854       return;
855     }
857   if (stack->next != NULL)
858     stack->next->prev = NULL;
860   *stack_p = stack->next;
861   free (stack);
865 /* This MUST be called if, during iteration, the user changes the
866    underlying structure.  It will check for a NULL current and advance
867    accordingly.  */
869 static void
870 iter_stack_update (xtensa_ld_iter_stack **stack_p)
872   if (!iter_stack_empty (stack_p)
873       && (*(*stack_p)->iterloc.loc) == NULL)
874     {
875       iter_stack_pop (stack_p);
877       while (!iter_stack_empty (stack_p)
878              && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
879         {
880           iter_stack_pop (stack_p);
881         }
882       if (!iter_stack_empty (stack_p))
883         (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
884     }
888 static void
889 iter_stack_next (xtensa_ld_iter_stack **stack_p)
891   xtensa_ld_iter_stack *stack;
892   lang_statement_union_type *current;
893   stack = *stack_p;
895   current = *stack->iterloc.loc;
896   /* If we are on the first element.  */
897   if (current != NULL)
898     {
899       switch (current->header.type)
900         {
901         case lang_output_section_statement_enum:
902         case lang_wild_statement_enum:
903         case lang_group_statement_enum:
904           /* If the list if not empty, we are done.  */
905           if (iter_stack_push (stack_p, *stack->iterloc.loc))
906             return;
907           /* Otherwise increment the pointer as normal.  */
908           break;
909         default:
910           break;
911         }
912     }
914   while (!iter_stack_empty (stack_p)
915          && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
916     {
917       iter_stack_pop (stack_p);
918     }
919   if (!iter_stack_empty (stack_p))
920     (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
924 static lang_statement_union_type *
925 iter_stack_current (xtensa_ld_iter_stack **stack_p)
927   return *((*stack_p)->iterloc.loc);
931 /* The iter stack is a preorder.  */
933 static void
934 iter_stack_create (xtensa_ld_iter_stack **stack_p,
935                    lang_statement_union_type *parent)
937   iter_stack_push (stack_p, parent);
941 static void
942 iter_stack_copy_current (xtensa_ld_iter_stack **stack_p, xtensa_ld_iter *front)
944   *front = (*stack_p)->iterloc;
948 static void
949 xtensa_colocate_literals (reloc_deps_graph *deps,
950                           lang_statement_union_type *statement)
952   /* Keep a stack of pointers to control iteration through the contours.  */
953   xtensa_ld_iter_stack *stack = NULL;
954   xtensa_ld_iter_stack **stack_p = &stack;
956   xtensa_ld_iter front;  /* Location where new insertion should occur.  */
957   xtensa_ld_iter *front_p = NULL;
959   xtensa_ld_iter current; /* Location we are checking.  */
960   xtensa_ld_iter *current_p = NULL;
961   bool in_literals = false;
963   if (deps->count == 0)
964     return;
966   iter_stack_create (stack_p, statement);
968   while (!iter_stack_empty (stack_p))
969     {
970       bool skip_increment = false;
971       lang_statement_union_type *l = iter_stack_current (stack_p);
973       switch (l->header.type)
974         {
975         case lang_assignment_statement_enum:
976           /* Any assignment statement should block reordering across it.  */
977           front_p = NULL;
978           in_literals = false;
979           break;
981         case lang_input_section_enum:
982           if (front_p == NULL)
983             {
984               in_literals = (section_is_target (deps, l)
985                              && !section_is_source (deps, l));
986               if (in_literals)
987                 {
988                   front_p = &front;
989                   iter_stack_copy_current (stack_p, front_p);
990                 }
991             }
992           else
993             {
994               bool is_target;
995               current_p = &current;
996               iter_stack_copy_current (stack_p, current_p);
997               is_target = (section_is_target (deps, l)
998                            && !section_is_source (deps, l));
1000               if (in_literals)
1001                 {
1002                   iter_stack_copy_current (stack_p, front_p);
1003                   if (!is_target)
1004                     in_literals = false;
1005                 }
1006               else
1007                 {
1008                   if (is_target)
1009                     {
1010                       /* Try to insert in place.  */
1011                       ld_xtensa_move_section_after (front_p, current_p);
1012                       ld_assign_relative_paged_dot (0x100000,
1013                                                     statement,
1014                                                     deps,
1015                                                     xtensa_use_literal_pages);
1017                       /* We use this code because it's already written.  */
1018                       if (!ld_local_file_relocations_fit (statement, deps))
1019                         {
1020                           /* Move it back.  */
1021                           ld_xtensa_move_section_after (current_p, front_p);
1022                           /* Reset the literal placement.  */
1023                           iter_stack_copy_current (stack_p, front_p);
1024                         }
1025                       else
1026                         {
1027                           /* Move front pointer up by one.  */
1028                           front_p->loc = &(*front_p->loc)->header.next;
1030                           /* Do not increment the current pointer.  */
1031                           skip_increment = true;
1032                         }
1033                     }
1034                 }
1035             }
1036           break;
1037         default:
1038           break;
1039         }
1041       if (!skip_increment)
1042         iter_stack_next (stack_p);
1043       else
1044         /* Be careful to update the stack_p if it now is a null.  */
1045         iter_stack_update (stack_p);
1046     }
1048   lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
1052 static void
1053 xtensa_move_dependencies_to_front (reloc_deps_graph *deps,
1054                                    lang_wild_statement_type *w)
1056   /* Keep a front pointer and a current pointer.  */
1057   lang_statement_union_type **front;
1058   lang_statement_union_type **current;
1060   /* Walk to the end of the targets.  */
1061   for (front = &w->children.head;
1062        (*front != NULL) && section_is_source_or_target (deps, *front);
1063        front = &(*front)->header.next)
1064     ;
1066   if (*front == NULL)
1067     return;
1069   current = &(*front)->header.next;
1070   while (*current != NULL)
1071     {
1072       if (section_is_source_or_target (deps, *current))
1073         {
1074           /* Insert in place.  */
1075           xtensa_ld_iter front_iter;
1076           xtensa_ld_iter current_iter;
1078           front_iter.parent = (lang_statement_union_type *) w;
1079           front_iter.l = &w->children;
1080           front_iter.loc = front;
1082           current_iter.parent = (lang_statement_union_type *) w;
1083           current_iter.l = &w->children;
1084           current_iter.loc = current;
1086           ld_xtensa_move_section_after (&front_iter, &current_iter);
1087           front = &(*front)->header.next;
1088         }
1089       else
1090         {
1091           current = &(*current)->header.next;
1092         }
1093     }
1097 static bool
1098 deps_has_sec_edge (const reloc_deps_graph *deps, asection *src, asection *tgt)
1100   const reloc_deps_section *sec_deps;
1101   const reloc_deps_e *sec_deps_e;
1103   sec_deps = xtensa_get_section_deps (deps, src);
1104   if (sec_deps == NULL)
1105     return false;
1107   for (sec_deps_e = sec_deps->succs;
1108        sec_deps_e != NULL;
1109        sec_deps_e = sec_deps_e->next)
1110     {
1111       ASSERT (sec_deps_e->src == src);
1112       if (sec_deps_e->tgt == tgt)
1113         return true;
1114     }
1115   return false;
1119 static bool
1120 deps_has_edge (const reloc_deps_graph *deps,
1121                lang_statement_union_type *src,
1122                lang_statement_union_type *tgt)
1124   if (!section_is_source (deps, src))
1125     return false;
1126   if (!section_is_target (deps, tgt))
1127     return false;
1129   if (src->header.type != lang_input_section_enum)
1130     return false;
1131   if (tgt->header.type != lang_input_section_enum)
1132     return false;
1134   return deps_has_sec_edge (deps, src->input_section.section,
1135                             tgt->input_section.section);
1139 static void
1140 add_deps_edge (reloc_deps_graph *deps, asection *src_sec, asection *tgt_sec)
1142   reloc_deps_section *src_sec_deps;
1143   reloc_deps_section *tgt_sec_deps;
1145   reloc_deps_e *src_edge;
1146   reloc_deps_e *tgt_edge;
1148   if (deps_has_sec_edge (deps, src_sec, tgt_sec))
1149     return;
1151   src_sec_deps = xtensa_get_section_deps (deps, src_sec);
1152   if (src_sec_deps == NULL)
1153     {
1154       /* Add a section.  */
1155       src_sec_deps = xmalloc (sizeof (reloc_deps_section));
1156       memset (src_sec_deps, 0, sizeof (reloc_deps_section));
1157       src_sec_deps->is_only_literal = 0;
1158       src_sec_deps->preds = NULL;
1159       src_sec_deps->succs = NULL;
1160       xtensa_set_section_deps (deps, src_sec, src_sec_deps);
1161       xtensa_append_section_deps (deps, src_sec);
1162     }
1164   tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
1165   if (tgt_sec_deps == NULL)
1166     {
1167       /* Add a section.  */
1168       tgt_sec_deps = xmalloc (sizeof (reloc_deps_section));
1169       memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
1170       tgt_sec_deps->is_only_literal = 0;
1171       tgt_sec_deps->preds = NULL;
1172       tgt_sec_deps->succs = NULL;
1173       xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
1174       xtensa_append_section_deps (deps, tgt_sec);
1175     }
1177   /* Add the edges.  */
1178   src_edge = xmalloc (sizeof (reloc_deps_e));
1179   memset (src_edge, 0, sizeof (reloc_deps_e));
1180   src_edge->src = src_sec;
1181   src_edge->tgt = tgt_sec;
1182   src_edge->next = src_sec_deps->succs;
1183   src_sec_deps->succs = src_edge;
1185   tgt_edge = xmalloc (sizeof (reloc_deps_e));
1186   memset (tgt_edge, 0, sizeof (reloc_deps_e));
1187   tgt_edge->src = src_sec;
1188   tgt_edge->tgt = tgt_sec;
1189   tgt_edge->next = tgt_sec_deps->preds;
1190   tgt_sec_deps->preds = tgt_edge;
1194 static void
1195 build_deps_graph_callback (asection *src_sec,
1196                            bfd_vma src_offset ATTRIBUTE_UNUSED,
1197                            asection *target_sec,
1198                            bfd_vma target_offset ATTRIBUTE_UNUSED,
1199                            void *closure)
1201   reloc_deps_graph *deps = closure;
1203   /* If the target is defined.  */
1204   if (target_sec != NULL)
1205     add_deps_edge (deps, src_sec, target_sec);
1209 static reloc_deps_graph *
1210 ld_build_required_section_dependence (lang_statement_union_type *s)
1212   reloc_deps_graph *deps;
1213   xtensa_ld_iter_stack *stack = NULL;
1215   deps = xmalloc (sizeof (reloc_deps_graph));
1216   deps->sections = NULL;
1217   deps->count = 0;
1218   deps->size = 0;
1220   for (iter_stack_create (&stack, s);
1221        !iter_stack_empty (&stack);
1222        iter_stack_next (&stack))
1223     {
1224       lang_statement_union_type *l = iter_stack_current (&stack);
1226       if (l == NULL && link_info.non_contiguous_regions)
1227         einfo (_("%F%P: Relaxation not supported with "
1228                  "--enable-non-contiguous-regions.\n"));
1230       if (l->header.type == lang_input_section_enum)
1231         {
1232           lang_input_section_type *input;
1233           input = &l->input_section;
1234           xtensa_callback_required_dependence (input->section->owner,
1235                                                input->section,
1236                                                &link_info,
1237                                                /* Use the same closure.  */
1238                                                build_deps_graph_callback,
1239                                                deps);
1240         }
1241     }
1242   return deps;
1246 #if EXTRA_VALIDATION
1247 static size_t
1248 ld_count_children (lang_statement_union_type *s)
1250   size_t count = 0;
1251   xtensa_ld_iter_stack *stack = NULL;
1252   for (iter_stack_create (&stack, s);
1253        !iter_stack_empty (&stack);
1254        iter_stack_next (&stack))
1255     {
1256       lang_statement_union_type *l = iter_stack_current (&stack);
1257       ASSERT (l != NULL);
1258       count++;
1259     }
1260   return count;
1262 #endif /* EXTRA_VALIDATION */
1265 /* Check if a particular section is included in the link.  This will only
1266    be true for one instance of a particular linkonce section.  */
1268 static bool input_section_found = false;
1269 static asection *input_section_target = NULL;
1271 static void
1272 input_section_linked_worker (lang_statement_union_type *statement)
1274   if ((statement->header.type == lang_input_section_enum
1275        && (statement->input_section.section == input_section_target)))
1276     input_section_found = true;
1279 static bool
1280 input_section_linked (asection *sec)
1282   input_section_found = false;
1283   input_section_target = sec;
1284   lang_for_each_statement_worker (input_section_linked_worker, stat_ptr->head);
1285   return input_section_found;
1289 /* Strip out any linkonce property tables or XCC exception tables where the
1290    associated linkonce text is from a different object file.  Normally,
1291    a matching set of linkonce sections is taken from the same object file,
1292    but sometimes the files are compiled differently so that some of the
1293    linkonce sections are not present in all files.  Stripping the
1294    inconsistent sections like this is not completely robust -- a much
1295    better solution is to use comdat groups.  */
1297 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
1299 static bool
1300 is_inconsistent_linkonce_section (asection *sec)
1302   bfd *abfd = sec->owner;
1303   const char *sec_name = bfd_section_name (sec);
1304   const char *name;
1306   if ((bfd_section_flags (sec) & SEC_LINK_ONCE) == 0
1307       || strncmp (sec_name, ".gnu.linkonce.", linkonce_len) != 0)
1308     return false;
1310   /* Check if this is an Xtensa property section or an exception table
1311      for Tensilica's XCC compiler.  */
1312   name = sec_name + linkonce_len;
1313   if (startswith (name, "prop."))
1314     name = strchr (name + 5, '.') ? strchr (name + 5, '.') + 1 : name + 5;
1315   else if (name[1] == '.'
1316            && (name[0] == 'p' || name[0] == 'e' || name[0] == 'h'))
1317     name += 2;
1318   else
1319     name = 0;
1321   if (name)
1322     {
1323       char *dep_sec_name = xmalloc (strlen (sec_name) + 1);
1324       asection *dep_sec;
1326       /* Get the associated linkonce text section and check if it is
1327          included in the link.  If not, this section is inconsistent
1328          and should be stripped.  */
1329       strcpy (dep_sec_name, ".gnu.linkonce.t.");
1330       strcat (dep_sec_name, name);
1331       dep_sec = bfd_get_section_by_name (abfd, dep_sec_name);
1332       if (dep_sec == NULL || ! input_section_linked (dep_sec))
1333         {
1334           free (dep_sec_name);
1335           return true;
1336         }
1337       free (dep_sec_name);
1338     }
1340   return false;
1344 static void
1345 xtensa_strip_inconsistent_linkonce_sections (lang_statement_list_type *slist)
1347   lang_statement_union_type **s_p = &slist->head;
1348   while (*s_p)
1349     {
1350       lang_statement_union_type *s = *s_p;
1351       lang_statement_union_type *s_next = (*s_p)->header.next;
1353       switch (s->header.type)
1354         {
1355         case lang_input_section_enum:
1356           if (is_inconsistent_linkonce_section (s->input_section.section))
1357             {
1358               s->input_section.section->output_section = bfd_abs_section_ptr;
1359               *s_p = s_next;
1360               continue;
1361             }
1362           break;
1364         case lang_constructors_statement_enum:
1365           xtensa_strip_inconsistent_linkonce_sections (&constructor_list);
1366           break;
1368         case lang_output_section_statement_enum:
1369           if (s->output_section_statement.children.head)
1370             xtensa_strip_inconsistent_linkonce_sections
1371               (&s->output_section_statement.children);
1372           break;
1374         case lang_wild_statement_enum:
1375           xtensa_strip_inconsistent_linkonce_sections
1376             (&s->wild_statement.children);
1377           break;
1379         case lang_group_statement_enum:
1380           xtensa_strip_inconsistent_linkonce_sections
1381             (&s->group_statement.children);
1382           break;
1384         case lang_data_statement_enum:
1385         case lang_reloc_statement_enum:
1386         case lang_object_symbols_statement_enum:
1387         case lang_output_statement_enum:
1388         case lang_target_statement_enum:
1389         case lang_input_statement_enum:
1390         case lang_assignment_statement_enum:
1391         case lang_padding_statement_enum:
1392         case lang_address_statement_enum:
1393         case lang_fill_statement_enum:
1394           break;
1396         default:
1397           FAIL ();
1398           break;
1399         }
1401       s_p = &(*s_p)->header.next;
1402     }
1404   /* Reset the tail of the list, in case the last entry was removed.  */
1405   if (s_p != slist->tail)
1406     slist->tail = s_p;
1410 static void
1411 xtensa_wild_group_interleave_callback (lang_statement_union_type *statement)
1413   lang_wild_statement_type *w;
1414   reloc_deps_graph *deps;
1415   if (statement->header.type == lang_wild_statement_enum)
1416     {
1417 #if EXTRA_VALIDATION
1418       size_t old_child_count;
1419       size_t new_child_count;
1420 #endif
1421       bool no_reorder;
1423       w = &statement->wild_statement;
1425       no_reorder = false;
1427       /* If it has 0 or 1 section bound, then do not reorder.  */
1428       if (w->children.head == NULL
1429           || (w->children.head->header.type == lang_input_section_enum
1430               && w->children.head->header.next == NULL))
1431         no_reorder = true;
1433       if (w->filenames_sorted)
1434         no_reorder = true;
1436       /* Check for sorting in a section list wildcard spec as well.  */
1437       if (!no_reorder)
1438         {
1439           struct wildcard_list *l;
1440           for (l = w->section_list; l != NULL; l = l->next)
1441             {
1442               if (l->spec.sorted == by_name)
1443                 {
1444                   no_reorder = true;
1445                   break;
1446                 }
1447             }
1448         }
1450       /* Special case until the NOREORDER linker directive is supported:
1451          *(.init) output sections and *(.fini) specs may NOT be reordered.  */
1453       /* Check for sorting in a section list wildcard spec as well.  */
1454       if (!no_reorder)
1455         {
1456           struct wildcard_list *l;
1457           for (l = w->section_list; l != NULL; l = l->next)
1458             {
1459               if (l->spec.name
1460                   && ((strcmp (".init", l->spec.name) == 0)
1461                       || (strcmp (".fini", l->spec.name) == 0)))
1462                 {
1463                   no_reorder = true;
1464                   break;
1465                 }
1466             }
1467         }
1469 #if EXTRA_VALIDATION
1470       old_child_count = ld_count_children (statement);
1471 #endif
1473       /* It is now officially a target.  Build the graph of source
1474          section -> target section (kept as a list of edges).  */
1475       deps = ld_build_required_section_dependence (statement);
1477       /* If this wildcard does not reorder....  */
1478       if (!no_reorder && deps->count != 0)
1479         {
1480           /* First check for reverse dependences.  Fix if possible.  */
1481           xtensa_layout_wild (deps, w);
1483           xtensa_move_dependencies_to_front (deps, w);
1484 #if EXTRA_VALIDATION
1485           new_child_count = ld_count_children (statement);
1486           ASSERT (new_child_count == old_child_count);
1487 #endif
1489           xtensa_colocate_literals (deps, statement);
1491 #if EXTRA_VALIDATION
1492           new_child_count = ld_count_children (statement);
1493           ASSERT (new_child_count == old_child_count);
1494 #endif
1495         }
1497       /* Clean up.  */
1498       free_reloc_deps_graph (deps);
1499     }
1503 static void
1504 xtensa_wild_group_interleave (lang_statement_union_type *s)
1506   lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
1510 static void
1511 xtensa_layout_wild (const reloc_deps_graph *deps, lang_wild_statement_type *w)
1513   /* If it does not fit initially, we need to do this step.  Move all
1514      of the wild literal sections to a new list, then move each of
1515      them back in just before the first section they depend on.  */
1516   lang_statement_union_type **s_p;
1517 #if EXTRA_VALIDATION
1518   size_t old_count, new_count;
1519   size_t ct1, ct2;
1520 #endif
1522   lang_wild_statement_type literal_wild;
1523   literal_wild.header.next = NULL;
1524   literal_wild.header.type = lang_wild_statement_enum;
1525   literal_wild.filename = NULL;
1526   literal_wild.filenames_sorted = false;
1527   literal_wild.section_list = NULL;
1528   literal_wild.keep_sections = false;
1529   literal_wild.children.head = NULL;
1530   literal_wild.children.tail = &literal_wild.children.head;
1532 #if EXTRA_VALIDATION
1533   old_count = ld_count_children ((lang_statement_union_type*) w);
1534 #endif
1536   s_p = &w->children.head;
1537   while (*s_p != NULL)
1538     {
1539       lang_statement_union_type *l = *s_p;
1540       if (l->header.type == lang_input_section_enum)
1541         {
1542           if (section_is_target (deps, l)
1543               && ! section_is_source (deps, l))
1544             {
1545               /* Detach.  */
1546               *s_p = l->header.next;
1547               if (*s_p == NULL)
1548                 w->children.tail = s_p;
1549               l->header.next = NULL;
1551               /* Append.  */
1552               *literal_wild.children.tail = l;
1553               literal_wild.children.tail = &l->header.next;
1554               continue;
1555             }
1556         }
1557       s_p = &(*s_p)->header.next;
1558     }
1560 #if EXTRA_VALIDATION
1561   ct1 = ld_count_children ((lang_statement_union_type*) w);
1562   ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
1564   ASSERT (old_count == (ct1 + ct2));
1565 #endif
1567   /* Now place them back in front of their dependent sections.  */
1569   while (literal_wild.children.head != NULL)
1570     {
1571       lang_statement_union_type *lit = literal_wild.children.head;
1572       bool placed = false;
1574 #if EXTRA_VALIDATION
1575       ASSERT (ct2 > 0);
1576       ct2--;
1577 #endif
1579       /* Detach.  */
1580       literal_wild.children.head = lit->header.next;
1581       if (literal_wild.children.head == NULL)
1582         literal_wild.children.tail = &literal_wild.children.head;
1583       lit->header.next = NULL;
1585       /* Find a spot to place it.  */
1586       for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
1587         {
1588           lang_statement_union_type *src = *s_p;
1589           if (deps_has_edge (deps, src, lit))
1590             {
1591               /* Place it here.  */
1592               lit->header.next = *s_p;
1593               *s_p = lit;
1594               placed = true;
1595               break;
1596             }
1597         }
1599       if (!placed)
1600         {
1601           /* Put it at the end.  */
1602           *w->children.tail = lit;
1603           w->children.tail = &lit->header.next;
1604         }
1605     }
1607 #if EXTRA_VALIDATION
1608   new_count = ld_count_children ((lang_statement_union_type*) w);
1609   ASSERT (new_count == old_count);
1610 #endif
1614 static void
1615 xtensa_colocate_output_literals_callback (lang_statement_union_type *statement)
1617   reloc_deps_graph *deps;
1618   if (statement->header.type == lang_output_section_statement_enum)
1619     {
1620       /* Now, we walk over the contours of the output section statement.
1622          First we build the literal section dependences as before.
1624          At the first uniquely_literal section, we mark it as a good
1625          spot to place other literals.  Continue walking (and counting
1626          sizes) until we find the next literal section.  If this
1627          section can be moved to the first one, then we move it.  If
1628          we every find a modification of ".", start over.  If we find
1629          a labeling of the current location, start over.  Finally, at
1630          the end, if we require page alignment, add page alignments.  */
1632 #if EXTRA_VALIDATION
1633       size_t old_child_count;
1634       size_t new_child_count;
1635 #endif
1636       bool no_reorder = false;
1638 #if EXTRA_VALIDATION
1639       old_child_count = ld_count_children (statement);
1640 #endif
1642       /* It is now officially a target.  Build the graph of source
1643          section -> target section (kept as a list of edges).  */
1645       deps = ld_build_required_section_dependence (statement);
1647       /* If this wildcard does not reorder....  */
1648       if (!no_reorder)
1649         {
1650           /* First check for reverse dependences.  Fix if possible.  */
1651           xtensa_colocate_literals (deps, statement);
1653 #if EXTRA_VALIDATION
1654           new_child_count = ld_count_children (statement);
1655           ASSERT (new_child_count == old_child_count);
1656 #endif
1657         }
1659       /* Insert align/offset assignment statement.  */
1660       if (xtensa_use_literal_pages)
1661         {
1662           ld_xtensa_insert_page_offsets (0, statement, deps,
1663                                          xtensa_use_literal_pages);
1664           lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
1665                                           statement);
1666         }
1668       /* Clean up.  */
1669       free_reloc_deps_graph (deps);
1670     }
1674 static void
1675 xtensa_colocate_output_literals (lang_statement_union_type *s)
1677   lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
1681 static void
1682 xtensa_ldlang_clear_addresses (lang_statement_union_type *statement)
1684   switch (statement->header.type)
1685     {
1686     case lang_input_section_enum:
1687       {
1688         asection *bfd_section = statement->input_section.section;
1689         bfd_section->output_offset = 0;
1690       }
1691       break;
1692     default:
1693       break;
1694     }
1698 static bfd_vma
1699 ld_assign_relative_paged_dot (bfd_vma dot,
1700                               lang_statement_union_type *s,
1701                               const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
1702                               bool lit_align)
1704   /* Walk through all of the input statements in this wild statement
1705      assign dot to all of them.  */
1707   xtensa_ld_iter_stack *stack = NULL;
1708   xtensa_ld_iter_stack **stack_p = &stack;
1710   bool first_section = false;
1711   bool in_literals = false;
1713   for (iter_stack_create (stack_p, s);
1714        !iter_stack_empty (stack_p);
1715        iter_stack_next (stack_p))
1716     {
1717       lang_statement_union_type *l = iter_stack_current (stack_p);
1719       switch (l->header.type)
1720         {
1721         case lang_input_section_enum:
1722           {
1723             asection *section = l->input_section.section;
1724             size_t align_pow = section->alignment_power;
1725             bool do_xtensa_alignment = false;
1727             if (lit_align)
1728               {
1729                 bool sec_is_target = section_is_target (deps, l);
1730                 bool sec_is_source = section_is_source (deps, l);
1732                 if (section->size != 0
1733                     && (first_section
1734                         || (in_literals && !sec_is_target)
1735                         || (!in_literals && sec_is_target)))
1736                   {
1737                     do_xtensa_alignment = true;
1738                   }
1739                 first_section = false;
1740                 if (section->size != 0)
1741                   in_literals = (sec_is_target && !sec_is_source);
1742               }
1744             if (do_xtensa_alignment && xtensa_page_power != 0)
1745               dot += (1 << xtensa_page_power);
1747             dot = align_power (dot, align_pow);
1748             section->output_offset = dot;
1749             dot += section->size;
1750           }
1751           break;
1752         case lang_fill_statement_enum:
1753           dot += l->fill_statement.size;
1754           break;
1755         case lang_padding_statement_enum:
1756           dot += l->padding_statement.size;
1757           break;
1758         default:
1759           break;
1760         }
1761     }
1762   return dot;
1766 static bool
1767 ld_local_file_relocations_fit (lang_statement_union_type *statement,
1768                                const reloc_deps_graph *deps ATTRIBUTE_UNUSED)
1770   /* Walk over all of the dependencies that we identified and make
1771      sure that IF the source and target are here (addr != 0):
1772      1) target addr < source addr
1773      2) (roundup(source + source_size, 4) - rounddown(target, 4))
1774         < (256K - (1 << bad align))
1775      Need a worst-case proof....  */
1777   xtensa_ld_iter_stack *stack = NULL;
1778   xtensa_ld_iter_stack **stack_p = &stack;
1779   size_t max_align_power = 0;
1780   size_t align_penalty = 256;
1781   reloc_deps_e *e;
1782   size_t i;
1784   /* Find the worst-case alignment requirement for this set of statements.  */
1785   for (iter_stack_create (stack_p, statement);
1786        !iter_stack_empty (stack_p);
1787        iter_stack_next (stack_p))
1788     {
1789       lang_statement_union_type *l = iter_stack_current (stack_p);
1790       if (l->header.type == lang_input_section_enum)
1791         {
1792           lang_input_section_type *input = &l->input_section;
1793           asection *section = input->section;
1794           if (section->alignment_power > max_align_power)
1795             max_align_power = section->alignment_power;
1796         }
1797     }
1799   /* Now check that everything fits.  */
1800   for (i = 0; i < deps->count; i++)
1801     {
1802       asection *sec = deps->sections[i];
1803       const reloc_deps_section *deps_section =
1804         xtensa_get_section_deps (deps, sec);
1805       if (deps_section)
1806         {
1807           /* We choose to walk through the successors.  */
1808           for (e = deps_section->succs; e != NULL; e = e->next)
1809             {
1810               if (e->src != e->tgt
1811                   && e->src->output_section == e->tgt->output_section
1812                   && e->src->output_offset != 0
1813                   && e->tgt->output_offset != 0)
1814                 {
1815                   bfd_vma l32r_addr =
1816                     align_power (e->src->output_offset + e->src->size, 2);
1817                   bfd_vma target_addr = e->tgt->output_offset & ~3;
1818                   if (l32r_addr < target_addr)
1819                     {
1820                       fflush (stdout);
1821                       fprintf (stderr, "Warning: "
1822                                "l32r target section before l32r\n");
1823                       fflush (stderr);
1824                       return false;
1825                     }
1827                   if (l32r_addr - target_addr > 256 * 1024 - align_penalty)
1828                     return false;
1829                 }
1830             }
1831         }
1832     }
1834   return true;
1838 static bfd_vma
1839 ld_xtensa_insert_page_offsets (bfd_vma dot,
1840                                lang_statement_union_type *s,
1841                                reloc_deps_graph *deps,
1842                                bool lit_align)
1844   xtensa_ld_iter_stack *stack = NULL;
1845   xtensa_ld_iter_stack **stack_p = &stack;
1847   bool first_section = false;
1848   bool in_literals = false;
1850   if (!lit_align)
1851     return false;
1853   for (iter_stack_create (stack_p, s);
1854        !iter_stack_empty (stack_p);
1855        iter_stack_next (stack_p))
1856     {
1857       lang_statement_union_type *l = iter_stack_current (stack_p);
1859       switch (l->header.type)
1860         {
1861         case lang_input_section_enum:
1862           {
1863             asection *section = l->input_section.section;
1864             bool do_xtensa_alignment = false;
1866             if (lit_align)
1867               {
1868                 if (section->size != 0
1869                     && (first_section
1870                         || (in_literals && !section_is_target (deps, l))
1871                         || (!in_literals && section_is_target (deps, l))))
1872                   {
1873                     do_xtensa_alignment = true;
1874                   }
1875                 first_section = false;
1876                 if (section->size != 0)
1877                   {
1878                     in_literals = (section_is_target (deps, l)
1879                                    && !section_is_source (deps, l));
1880                   }
1881               }
1883             if (do_xtensa_alignment && xtensa_page_power != 0)
1884               {
1885                 /* Create an expression that increments the current address,
1886                    i.e., "dot", by (1 << xtensa_align_power).  */
1887                 etree_type *name_op = exp_nameop (NAME, ".");
1888                 etree_type *addend_op = exp_intop (1 << xtensa_page_power);
1889                 etree_type *add_op = exp_binop ('+', name_op, addend_op);
1890                 etree_type *assign_op = exp_assign (".", add_op, false);
1892                 lang_assignment_statement_type *assign_stmt;
1893                 lang_statement_union_type *assign_union;
1894                 lang_statement_list_type tmplist;
1896                 /* There is hidden state in "lang_add_assignment".  It
1897                    appends the new assignment statement to the stat_ptr
1898                    list.  Thus, we swap it before and after the call.  */
1900                 lang_list_init (&tmplist);
1901                 push_stat_ptr (&tmplist);
1902                 /* Warning: side effect; statement appended to stat_ptr.  */
1903                 assign_stmt = lang_add_assignment (assign_op);
1904                 assign_union = (lang_statement_union_type *) assign_stmt;
1905                 pop_stat_ptr ();
1907                 assign_union->header.next = l;
1908                 *(*stack_p)->iterloc.loc = assign_union;
1909                 iter_stack_next (stack_p);
1910               }
1911           }
1912           break;
1913         default:
1914           break;
1915         }
1916     }
1917   return dot;
1922 # Define some shell vars to insert bits of code into the standard ELF
1923 # parse_args and list_options functions.
1925 PARSE_AND_LIST_PROLOGUE='
1926 #define OPTION_OPT_SIZEOPT              (300)
1927 #define OPTION_LITERAL_MOVEMENT         (OPTION_OPT_SIZEOPT + 1)
1928 #define OPTION_NO_LITERAL_MOVEMENT      (OPTION_LITERAL_MOVEMENT + 1)
1929 #define OPTION_ABI_WINDOWED             (OPTION_NO_LITERAL_MOVEMENT + 1)
1930 #define OPTION_ABI_CALL0                (OPTION_ABI_WINDOWED + 1)
1931 extern int elf32xtensa_size_opt;
1932 extern int elf32xtensa_no_literal_movement;
1933 extern int elf32xtensa_abi;
1936 PARSE_AND_LIST_LONGOPTS='
1937   { "size-opt", no_argument, NULL, OPTION_OPT_SIZEOPT},
1938   { "literal-movement", no_argument, NULL, OPTION_LITERAL_MOVEMENT},
1939   { "no-literal-movement", no_argument, NULL, OPTION_NO_LITERAL_MOVEMENT},
1940   { "abi-windowed", no_argument, NULL, OPTION_ABI_WINDOWED},
1941   { "abi-call0", no_argument, NULL, OPTION_ABI_CALL0},
1944 PARSE_AND_LIST_OPTIONS='
1945   fprintf (file, _("\
1946   --size-opt                  When relaxing longcalls, prefer size\n\
1947                                 optimization over branch target alignment\n"));
1948   fprintf (file, _("\
1949   --abi-windowed              Choose windowed ABI for the output object\n"));
1950   fprintf (file, _("\
1951   --abi-call0                 Choose call0 ABI for the output object\n"));
1954 PARSE_AND_LIST_ARGS_CASES='
1955     case OPTION_OPT_SIZEOPT:
1956       elf32xtensa_size_opt = 1;
1957       break;
1958     case OPTION_LITERAL_MOVEMENT:
1959       elf32xtensa_no_literal_movement = 0;
1960       break;
1961     case OPTION_NO_LITERAL_MOVEMENT:
1962       elf32xtensa_no_literal_movement = 1;
1963       break;
1964     case OPTION_ABI_WINDOWED:
1965       elf32xtensa_abi = XTHAL_ABI_WINDOWED;
1966       break;
1967     case OPTION_ABI_CALL0:
1968       elf32xtensa_abi = XTHAL_ABI_CALL0;
1969       break;
1972 # Replace some of the standard ELF functions with our own versions.
1974 LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
1975 LDEMUL_AFTER_OPEN=elf_xtensa_after_open
1976 LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
1977 LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation