bfd ChangeLog
[external/binutils.git] / ld / emultempl / xtensaelf.em
1 # This shell script emits a C file. -*- C -*-
2 #   Copyright 2003, 2004
3 #   Free Software Foundation, Inc.
4 #
5 # This file is part of GLD, the Gnu Linker.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #
21
22 # This file is sourced from elf32.em, and defines extra xtensa-elf
23 # specific routines.
24 #
25 cat >>e${EMULATION_NAME}.c <<EOF
26
27 #include <xtensa-config.h>
28 #include "../bfd/elf-bfd.h"
29 #include "../bfd/libbfd.h"
30 #include "elf/xtensa.h"
31 #include "bfd.h"
32
33 static void xtensa_wild_group_interleave (lang_statement_union_type *);
34 static void xtensa_colocate_output_literals (lang_statement_union_type *);
35 static void remove_section (bfd *, asection *);
36 static bfd_boolean replace_insn_sec_with_prop_sec (bfd *, const char *,
37                                                    const char *, char **);
38 static void replace_instruction_table_sections (bfd *, asection *);
39
40
41 /* Flag for the emulation-specific "--no-relax" option.  */
42 static bfd_boolean disable_relaxation = FALSE;
43
44 /* This number is irrelevant until we turn on use_literal_pages */
45 static bfd_vma xtensa_page_power = 12; /* 4K pages.  */
46
47 /* To force a page break between literals and text, change
48    xtensa_use_literal_pages to "TRUE".  */
49 static bfd_boolean xtensa_use_literal_pages = FALSE;
50
51 #define EXTRA_VALIDATION 0
52
53
54 static char *
55 elf_xtensa_choose_target (int argc ATTRIBUTE_UNUSED,
56                           char **argv ATTRIBUTE_UNUSED)
57 {
58   if (XCHAL_HAVE_BE)
59     return "${BIG_OUTPUT_FORMAT}";
60   else
61     return "${LITTLE_OUTPUT_FORMAT}";
62 }
63
64
65 static bfd_boolean
66 elf_xtensa_place_orphan (lang_input_statement_type *file, asection *s)
67 {
68   /* Early exit for relocatable links.  */
69   if (link_info.relocatable)
70     return FALSE;
71
72   return gld${EMULATION_NAME}_place_orphan (file, s);
73 }
74
75
76 static void
77 elf_xtensa_before_parse (void)
78 {
79   /* Just call the default hook.... Tensilica's version of this function
80      does some other work that isn't relevant here.  */
81   gld${EMULATION_NAME}_before_parse ();
82 }
83
84
85 void
86 remove_section (abfd, os)
87      bfd *abfd;
88      asection *os;
89 {
90   asection **spp;
91   for (spp = &abfd->sections; *spp; spp = &(*spp)->next)
92     if (*spp == os)
93       {
94         *spp = os->next;
95         os->owner->section_count--;
96         break;
97       }
98 }
99
100
101 bfd_boolean
102 replace_insn_sec_with_prop_sec (abfd, insn_sec_name, prop_sec_name,
103                                 error_message)
104      bfd *abfd;
105      const char *insn_sec_name;
106      const char *prop_sec_name;
107      char **error_message;
108 {
109   asection *insn_sec;
110   asection *prop_sec;
111   bfd_byte *prop_contents = NULL;
112   bfd_byte *insn_contents = NULL;
113   unsigned entry_count;
114   unsigned entry;
115   Elf_Internal_Shdr *symtab_hdr;
116   Elf_Internal_Rela *internal_relocs = NULL;
117   unsigned reloc_count;
118  
119   *error_message = "";
120   insn_sec = bfd_get_section_by_name (abfd, insn_sec_name);
121   if (insn_sec == NULL)
122     return TRUE;
123   entry_count = insn_sec->size / 8;
124
125   prop_sec = bfd_get_section_by_name (abfd, prop_sec_name);
126   if (prop_sec != NULL && insn_sec != NULL)
127     {
128       *error_message = _("file already has property tables");
129       return FALSE;
130     }
131   
132   if (insn_sec->size != 0)
133     {
134       insn_contents = (bfd_byte *) bfd_malloc (insn_sec->size);
135       if (insn_contents == NULL)
136         {
137           *error_message = _("out of memory");
138           goto cleanup;
139         }
140       if (! bfd_get_section_contents (abfd, insn_sec, insn_contents,
141                                       (file_ptr) 0, insn_sec->size))
142         {
143           *error_message = _("failed to read section contents");
144           goto cleanup;
145         }
146     }
147
148   /* Create a Property table section and relocation section for it.  */
149   prop_sec_name = strdup (prop_sec_name);
150   prop_sec = bfd_make_section (abfd, prop_sec_name);
151   if (prop_sec == NULL
152       || ! bfd_set_section_flags (abfd, prop_sec, 
153                                   bfd_get_section_flags (abfd, insn_sec))
154       || ! bfd_set_section_alignment (abfd, prop_sec, 2))
155     {
156       *error_message = _("could not create new section");
157       goto cleanup;
158     }
159   
160   if (! bfd_set_section_flags (abfd, prop_sec, 
161                                bfd_get_section_flags (abfd, insn_sec))
162       || ! bfd_set_section_alignment (abfd, prop_sec, 2))
163     {
164       *error_message = _("could not set new section properties");
165       goto cleanup;
166     }
167   prop_sec->size = entry_count * 12;
168   prop_contents = (bfd_byte *) bfd_zalloc (abfd, prop_sec->size);
169   elf_section_data (prop_sec)->this_hdr.contents = prop_contents;
170
171   /* The entry size and size must be set to allow the linker to compute
172      the number of relocations since it does not use reloc_count.  */
173   elf_section_data (prop_sec)->rel_hdr.sh_entsize =
174     sizeof (Elf32_External_Rela);
175   elf_section_data (prop_sec)->rel_hdr.sh_size = 
176     elf_section_data (insn_sec)->rel_hdr.sh_size;
177
178   if (prop_contents == NULL && prop_sec->size != 0)
179     {
180       *error_message = _("could not allocate section contents");
181       goto cleanup;
182     }
183
184   /* Read the relocations.  */
185   reloc_count = insn_sec->reloc_count;
186   if (reloc_count != 0)
187     {
188       /* If there is already an internal_reloc, then save it so that the
189          read_relocs function freshly allocates a copy.  */
190       Elf_Internal_Rela *saved_relocs = elf_section_data (insn_sec)->relocs;
191       
192       elf_section_data (insn_sec)->relocs = NULL;
193       internal_relocs = 
194         _bfd_elf_link_read_relocs (abfd, insn_sec, NULL, NULL, FALSE);
195       elf_section_data (insn_sec)->relocs = saved_relocs;
196       
197       if (internal_relocs == NULL)
198         {
199           *error_message = _("out of memory");
200           goto cleanup;
201         }
202     }
203
204   /* Create a relocation section for the property section.  */
205   if (internal_relocs != NULL)
206     {
207       elf_section_data (prop_sec)->relocs = internal_relocs;
208       prop_sec->reloc_count = reloc_count;
209     }
210   
211   /* Now copy each insn table entry to the prop table entry with
212      appropriate flags.  */
213   for (entry = 0; entry < entry_count; ++entry)
214     {
215       unsigned value;
216       unsigned flags = (XTENSA_PROP_INSN | XTENSA_PROP_INSN_NO_TRANSFORM
217                         | XTENSA_PROP_INSN_NO_REORDER);
218       value = bfd_get_32 (abfd, insn_contents + entry * 8 + 0);
219       bfd_put_32 (abfd, value, prop_contents + entry * 12 + 0);
220       value = bfd_get_32 (abfd, insn_contents + entry * 8 + 4);
221       bfd_put_32 (abfd, value, prop_contents + entry * 12 + 4);
222       bfd_put_32 (abfd, flags, prop_contents + entry * 12 + 8);
223     }
224
225   /* Now copy all of the relocations.  Change offsets for the
226      instruction table section to offsets in the property table
227      section.  */
228   if (internal_relocs)
229     {
230       unsigned i;
231       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
232
233       for (i = 0; i < reloc_count; i++)
234         {
235           Elf_Internal_Rela *rela;
236           unsigned r_offset;
237
238           rela = &internal_relocs[i];
239
240           /* If this relocation is to the .xt.insn section, 
241              change the section number and the offset.  */
242           r_offset = rela->r_offset;
243           r_offset += 4 * (r_offset / 8);
244           rela->r_offset = r_offset;
245         }
246     }
247
248   remove_section (abfd, insn_sec);
249   
250   if (insn_contents)
251     free (insn_contents);
252   
253   return TRUE;
254
255  cleanup:
256   if (prop_sec && prop_sec->owner)
257     remove_section (abfd, prop_sec);
258   if (insn_contents)
259     free (insn_contents);
260   if (internal_relocs)
261     free (internal_relocs);
262
263   return FALSE;
264 }
265
266
267 #define PROP_SEC_BASE_NAME ".xt.prop"
268 #define INSN_SEC_BASE_NAME ".xt.insn"
269 #define LINKONCE_SEC_OLD_TEXT_BASE_NAME ".gnu.linkonce.x."
270
271
272 void
273 replace_instruction_table_sections (abfd, sec)
274      bfd *abfd;
275      asection *sec;
276 {
277   char *message = "";
278   const char *insn_sec_name = NULL;
279   char *prop_sec_name = NULL;
280   char *owned_prop_sec_name = NULL;
281   const char *sec_name;
282     
283   sec_name = bfd_get_section_name (abfd, sec);
284   if (strcmp (sec_name, INSN_SEC_BASE_NAME) == 0)
285     {
286       insn_sec_name = INSN_SEC_BASE_NAME;
287       prop_sec_name = PROP_SEC_BASE_NAME;
288     }
289   else if (strncmp (sec_name, LINKONCE_SEC_OLD_TEXT_BASE_NAME,
290                     strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME)) == 0)
291     {
292       insn_sec_name = sec_name;
293       owned_prop_sec_name = (char *) xmalloc (strlen (sec_name) + 20);
294       prop_sec_name = owned_prop_sec_name;
295       strcpy (prop_sec_name, ".gnu.linkonce.prop.t.");
296       strcat (prop_sec_name,
297               sec_name + strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME));
298     }
299   if (insn_sec_name != NULL)
300     {
301       if (! replace_insn_sec_with_prop_sec (abfd, insn_sec_name, prop_sec_name,
302                                             &message))
303         {
304           einfo (_("%P: warning: failed to convert %s table in %B (%s); subsequent disassembly may be incomplete\n"),
305                  insn_sec_name, abfd, message);
306         }
307     }
308   if (owned_prop_sec_name)
309     free (owned_prop_sec_name);
310 }
311
312
313 /* This is called after all input sections have been opened to convert
314    instruction tables (.xt.insn, gnu.linkonce.x.*) tables into property
315    tables (.xt.prop) before any section placement.  */
316
317 static void
318 elf_xtensa_after_open (void)
319 {
320   bfd *abfd;
321
322   /* First call the ELF version.  */
323   gld${EMULATION_NAME}_after_open ();
324   
325   /* Now search the input files looking for instruction table sections.  */
326   for (abfd = link_info.input_bfds;
327        abfd != NULL;
328        abfd = abfd->link_next)
329     {
330       asection *sec = abfd->sections;
331       asection *next_sec;
332
333       /* Do not use bfd_map_over_sections here since we are removing
334          sections as we iterate.  */
335       while (sec != NULL)
336         {
337           next_sec = sec->next;
338           replace_instruction_table_sections (abfd, sec);
339           sec = next_sec;
340         }
341     }
342 }
343
344
345 /* This is called after the sections have been attached to output
346    sections, but before any sizes or addresses have been set.  */
347
348 static void
349 elf_xtensa_before_allocation (void)
350 {
351   bfd *in_bfd;
352   bfd_boolean is_big_endian = XCHAL_HAVE_BE;
353
354   /* Check that the output endianness matches the Xtensa
355      configuration.  The BFD library always includes both big and
356      little endian target vectors for Xtensa, but it only supports the
357      detailed instruction encode/decode operations (such as are
358      required to process relocations) for the selected Xtensa
359      configuration.  */
360
361   if (is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
362     {
363       einfo (_("%F%P: little endian output does not match "
364                "Xtensa configuration\n"));
365     }
366   if (!is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
367     {
368       einfo (_("%F%P: big endian output does not match "
369                "Xtensa configuration\n"));
370     }
371
372   /* Check that the endianness for each input file matches the output.
373      The merge_private_bfd_data hook has already reported any mismatches
374      as errors, but those errors are not fatal.  At this point, we
375      cannot go any further if there are any mismatches.  */
376
377   for (in_bfd = link_info.input_bfds;
378        in_bfd != NULL;
379        in_bfd = in_bfd->link_next)
380     {
381       if ((is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
382           || (!is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
383         einfo (_("%F%P: cross-endian linking not supported\n"));
384     }
385
386   /* Enable relaxation by default if the "--no-relax" option was not
387      specified.  This is done here instead of in the before_parse hook
388      because there is a check in main() to prohibit use of --relax and
389      -r together and that combination should be allowed for Xtensa.  */
390
391   if (!disable_relaxation)
392     command_line.relax = TRUE;
393
394   gld${EMULATION_NAME}_before_allocation ();
395
396   xtensa_wild_group_interleave (stat_ptr->head);
397   if (command_line.relax)
398     xtensa_colocate_output_literals (stat_ptr->head);
399
400   /* TBD: We need to force the page alignments to here and only do
401      them as needed for the entire output section.  Finally, if this
402      is a relocatable link then we need to add alignment notes so
403      that the literals can be separated later.  */
404 }
405
406
407 typedef struct wildcard_list section_name_list;
408
409 typedef struct reloc_deps_e_t reloc_deps_e;
410 typedef struct reloc_deps_section_t reloc_deps_section;
411 typedef struct reloc_deps_graph_t reloc_deps_graph;
412
413
414 struct reloc_deps_e_t
415 {
416   asection *src; /* Contains l32rs.  */
417   asection *tgt; /* Contains literals.  */
418   reloc_deps_e *next;
419 };
420
421 /* Place these in the userdata field.  */
422 struct reloc_deps_section_t
423 {
424   reloc_deps_e *preds;
425   reloc_deps_e *succs;
426   bfd_boolean is_only_literal;
427 };
428
429
430 struct reloc_deps_graph_t
431 {
432   size_t count;
433   size_t size;
434   asection **sections;
435 };
436
437 static void xtensa_layout_wild
438   (const reloc_deps_graph *, lang_wild_statement_type *);
439
440 typedef void (*deps_callback_t) (asection *, /* src_sec */
441                                  bfd_vma,    /* src_offset */
442                                  asection *, /* target_sec */
443                                  bfd_vma,    /* target_offset */
444                                  void *);    /* closure */
445
446 extern bfd_boolean xtensa_callback_required_dependence
447   (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
448 static void xtensa_ldlang_clear_addresses
449   (lang_statement_union_type *);
450 static bfd_boolean ld_local_file_relocations_fit
451   (lang_statement_union_type *, const reloc_deps_graph *);
452 static bfd_vma ld_assign_relative_paged_dot
453   (bfd_vma, lang_statement_union_type *, const reloc_deps_graph *,
454    bfd_boolean);
455 static bfd_vma ld_xtensa_insert_page_offsets
456   (bfd_vma, lang_statement_union_type *, reloc_deps_graph *, bfd_boolean);
457 #if EXTRA_VALIDATION
458 static size_t ld_count_children
459   (lang_statement_union_type *);
460 #endif
461
462 extern lang_statement_list_type constructor_list;
463
464 /*  Begin verbatim code from ldlang.c:
465     the following are copied from ldlang.c because they are defined
466     there statically.  */
467
468 static void
469 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
470                                 lang_statement_union_type *s)
471 {
472   for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
473     {
474       func (s);
475
476       switch (s->header.type)
477         {
478         case lang_constructors_statement_enum:
479           lang_for_each_statement_worker (func, constructor_list.head);
480           break;
481         case lang_output_section_statement_enum:
482           lang_for_each_statement_worker
483             (func,
484              s->output_section_statement.children.head);
485           break;
486         case lang_wild_statement_enum:
487           lang_for_each_statement_worker
488             (func,
489              s->wild_statement.children.head);
490           break;
491         case lang_group_statement_enum:
492           lang_for_each_statement_worker (func,
493                                           s->group_statement.children.head);
494           break;
495         case lang_data_statement_enum:
496         case lang_reloc_statement_enum:
497         case lang_object_symbols_statement_enum:
498         case lang_output_statement_enum:
499         case lang_target_statement_enum:
500         case lang_input_section_enum:
501         case lang_input_statement_enum:
502         case lang_assignment_statement_enum:
503         case lang_padding_statement_enum:
504         case lang_address_statement_enum:
505         case lang_fill_statement_enum:
506           break;
507         default:
508           FAIL ();
509           break;
510         }
511     }
512 }
513
514 /* End of verbatim code from ldlang.c.  */
515
516
517 static reloc_deps_section *
518 xtensa_get_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
519                          asection *sec)
520 {
521   /* We have a separate function for this so that
522      we could in the future keep a completely independent
523      structure that maps a section to its dependence edges.
524      For now, we place these in the sec->userdata field.  */
525   reloc_deps_section *sec_deps = sec->userdata;
526   return sec_deps;
527 }
528
529 static void
530 xtensa_set_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
531                          asection *sec,
532                          reloc_deps_section *deps_section)
533 {
534   sec->userdata = deps_section;
535 }
536
537
538 /* This is used to keep a list of all of the sections participating in
539    the graph so we can clean them up quickly.  */
540
541 static void
542 xtensa_append_section_deps (reloc_deps_graph *deps, asection *sec)
543 {
544   if (deps->size <= deps->count)
545     {
546       asection **new_sections;
547       size_t i;
548       size_t new_size;
549
550       new_size = deps->size * 2;
551       if (new_size == 0)
552         new_size = 20;
553
554       new_sections = xmalloc (sizeof (asection *) * new_size);
555       memset (new_sections, 0, sizeof (asection *) * new_size);
556       for (i = 0; i < deps->count; i++)
557         {
558           new_sections[i] = deps->sections[i];
559         }
560       if (deps->sections != NULL)
561         free (deps->sections);
562       deps->sections = new_sections;
563       deps->size = new_size;
564     }
565   deps->sections[deps->count] = sec;
566   deps->count++;
567 }
568
569
570 static void
571 free_reloc_deps_graph (reloc_deps_graph *deps)
572 {
573   size_t i;
574   for (i = 0; i < deps->count; i++)
575     {
576       asection *sec = deps->sections[i];
577       reloc_deps_section *sec_deps;
578       sec_deps = xtensa_get_section_deps (deps, sec);
579       if (sec_deps)
580         {
581           reloc_deps_e *next;
582           while (sec_deps->succs != NULL)
583             {
584               next = sec_deps->succs->next;
585               free (sec_deps->succs);
586               sec_deps->succs = next;
587             }
588
589           while (sec_deps->preds != NULL)
590             {
591               next = sec_deps->preds->next;
592               free (sec_deps->preds);
593               sec_deps->preds = next;
594             }
595           free (sec_deps);
596         }
597       xtensa_set_section_deps (deps, sec, NULL);
598     }
599   if (deps->sections)
600     free (deps->sections);
601
602   free (deps);
603 }
604
605
606 static bfd_boolean
607 section_is_source (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
608                    lang_statement_union_type *s)
609 {
610   asection *sec;
611   const reloc_deps_section *sec_deps;
612
613   if (s->header.type != lang_input_section_enum)
614     return FALSE;
615   sec = s->input_section.section;
616
617   sec_deps = xtensa_get_section_deps (deps, sec);
618   return sec_deps && sec_deps->succs != NULL;
619 }
620
621
622 static bfd_boolean
623 section_is_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
624                    lang_statement_union_type *s)
625 {
626   asection *sec;
627   const reloc_deps_section *sec_deps;
628
629   if (s->header.type != lang_input_section_enum)
630     return FALSE;
631   sec = s->input_section.section;
632
633   sec_deps = xtensa_get_section_deps (deps, sec);
634   return sec_deps && sec_deps->preds != NULL;
635 }
636
637 static bfd_boolean
638 section_is_source_or_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
639                              lang_statement_union_type *s)
640 {
641   return (section_is_source (deps, s)
642           || section_is_target (deps, s));
643 }
644
645
646 typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
647 typedef struct xtensa_ld_iter_t xtensa_ld_iter;
648
649 struct xtensa_ld_iter_t
650 {
651   lang_statement_union_type *parent;    /* Parent of the list.  */
652   lang_statement_list_type *l;          /* List that holds it.  */
653   lang_statement_union_type **loc;      /* Place in the list.  */
654 };
655
656 struct xtensa_ld_iter_stack_t
657 {
658   xtensa_ld_iter iterloc;               /* List that hold it.  */
659
660   xtensa_ld_iter_stack *next;           /* Next in the stack.  */
661   xtensa_ld_iter_stack *prev;           /* Back pointer for stack.  */
662 };
663
664
665 static void
666 ld_xtensa_move_section_after (xtensa_ld_iter *to, xtensa_ld_iter *current)
667 {
668   lang_statement_union_type *to_next;
669   lang_statement_union_type *current_next;
670   lang_statement_union_type **e;
671
672 #if EXTRA_VALIDATION
673   size_t old_to_count, new_to_count;
674   size_t old_current_count, new_current_count;
675 #endif
676
677   if (to == current)
678     return;
679
680 #if EXTRA_VALIDATION
681   old_to_count = ld_count_children (to->parent);
682   old_current_count = ld_count_children (current->parent);
683 #endif
684
685   to_next = *(to->loc);
686   current_next = (*current->loc)->header.next;
687
688   *(to->loc) = *(current->loc);
689
690   *(current->loc) = current_next;
691   (*(to->loc))->header.next = to_next;
692
693   /* reset "to" list tail */
694   for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
695     ;
696   to->l->tail = e;
697
698   /* reset "current" list tail */
699   for (e = &current->l->head; *e != NULL; e = &(*e)->header.next)
700     ;
701   current->l->tail = e;
702
703 #if EXTRA_VALIDATION
704   new_to_count = ld_count_children (to->parent);
705   new_current_count = ld_count_children (current->parent);
706
707   ASSERT ((old_to_count + old_current_count)
708           == (new_to_count + new_current_count));
709 #endif
710 }
711
712
713 /* Can only be called with lang_statements that have lists.  Returns
714    FALSE if the list is empty.  */
715
716 static bfd_boolean
717 iter_stack_empty (xtensa_ld_iter_stack **stack_p)
718 {
719   return *stack_p == NULL;
720 }
721
722
723 static bfd_boolean
724 iter_stack_push (xtensa_ld_iter_stack **stack_p,
725                  lang_statement_union_type *parent)
726 {
727   xtensa_ld_iter_stack *stack;
728   lang_statement_list_type *l = NULL;
729
730   switch (parent->header.type)
731     {
732     case lang_output_section_statement_enum:
733       l = &parent->output_section_statement.children;
734       break;
735     case lang_wild_statement_enum:
736       l = &parent->wild_statement.children;
737       break;
738     case lang_group_statement_enum:
739       l = &parent->group_statement.children;
740       break;
741     default:
742       ASSERT (0);
743       return FALSE;
744     }
745
746   /* Empty. do not push.  */
747   if (l->tail == &l->head)
748     return FALSE;
749
750   stack = xmalloc (sizeof (xtensa_ld_iter_stack));
751   memset (stack, 0, sizeof (xtensa_ld_iter_stack));
752   stack->iterloc.parent = parent;
753   stack->iterloc.l = l;
754   stack->iterloc.loc = &l->head;
755
756   stack->next = *stack_p;
757   stack->prev = NULL;
758   if (*stack_p != NULL)
759     (*stack_p)->prev = stack;
760   *stack_p = stack;
761   return TRUE;
762 }
763
764
765 static void
766 iter_stack_pop (xtensa_ld_iter_stack **stack_p)
767 {
768   xtensa_ld_iter_stack *stack;
769
770   stack = *stack_p;
771
772   if (stack == NULL)
773     {
774       ASSERT (stack != NULL);
775       return;
776     }
777
778   if (stack->next != NULL)
779     stack->next->prev = NULL;
780
781   *stack_p = stack->next;
782   free (stack);
783 }
784
785
786 /* This MUST be called if, during iteration, the user changes the
787    underlying structure.  It will check for a NULL current and advance
788    accordingly.  */
789
790 static void
791 iter_stack_update (xtensa_ld_iter_stack **stack_p)
792 {
793   if (!iter_stack_empty (stack_p)
794       && (*(*stack_p)->iterloc.loc) == NULL)
795     {
796       iter_stack_pop (stack_p);
797
798       while (!iter_stack_empty (stack_p)
799              && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
800         {
801           iter_stack_pop (stack_p);
802         }
803       if (!iter_stack_empty (stack_p))
804         (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
805     }
806 }
807
808
809 static void
810 iter_stack_next (xtensa_ld_iter_stack **stack_p)
811 {
812   xtensa_ld_iter_stack *stack;
813   lang_statement_union_type *current;
814   stack = *stack_p;
815
816   current = *stack->iterloc.loc;
817   /* If we are on the first element.  */
818   if (current != NULL)
819     {
820       switch (current->header.type)
821         {
822         case lang_output_section_statement_enum:
823         case lang_wild_statement_enum:
824         case lang_group_statement_enum:
825           /* If the list if not empty, we are done.  */
826           if (iter_stack_push (stack_p, *stack->iterloc.loc))
827             return;
828           /* Otherwise increment the pointer as normal.  */
829           break;
830         default:
831           break;
832         }
833     }
834
835   while (!iter_stack_empty (stack_p)
836          && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
837     {
838       iter_stack_pop (stack_p);
839     }
840   if (!iter_stack_empty (stack_p))
841     (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
842 }
843
844
845 static lang_statement_union_type *
846 iter_stack_current (xtensa_ld_iter_stack **stack_p)
847 {
848   return *((*stack_p)->iterloc.loc);
849 }
850
851
852 /* The iter stack is a preorder.  */
853
854 static void
855 iter_stack_create (xtensa_ld_iter_stack **stack_p,
856                    lang_statement_union_type *parent)
857 {
858   iter_stack_push (stack_p, parent);
859 }
860
861
862 static void
863 iter_stack_copy_current (xtensa_ld_iter_stack **stack_p,
864                          xtensa_ld_iter *front)
865 {
866   *front = (*stack_p)->iterloc;
867 }
868
869
870 static void
871 xtensa_colocate_literals (reloc_deps_graph *deps,
872                           lang_statement_union_type *statement)
873 {
874   /* Keep a stack of pointers to control iteration through the contours.  */
875   xtensa_ld_iter_stack *stack = NULL;
876   xtensa_ld_iter_stack **stack_p = &stack;
877
878   xtensa_ld_iter front;  /* Location where new insertion should occur.  */
879   xtensa_ld_iter *front_p = NULL;
880
881   xtensa_ld_iter current; /* Location we are checking.  */
882   xtensa_ld_iter *current_p = NULL;
883   bfd_boolean in_literals = FALSE;
884
885   if (deps->count == 0)
886     return;
887
888 #if 0
889   ld_assign_relative_paged_dot (0x100000, statement, deps,
890                                 xtensa_use_literal_pages);
891
892   if (!ld_local_file_relocations_fit (statement, deps))
893     fprintf (stderr, "initial relocation placement does not fit\n");
894
895   lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
896 #endif
897
898   iter_stack_create (stack_p, statement);
899
900   while (!iter_stack_empty (stack_p))
901     {
902       bfd_boolean skip_increment = FALSE;
903       lang_statement_union_type *l = iter_stack_current (stack_p);
904
905       switch (l->header.type)
906         {
907         case lang_assignment_statement_enum:
908           /* Any assignment statement should block reordering across it.  */
909           front_p = NULL;
910           in_literals = FALSE;
911           break;
912
913         case lang_input_section_enum:
914           if (front_p == NULL)
915             {
916               in_literals = (section_is_target (deps, l)
917                              && !section_is_source (deps, l));
918               if (in_literals)
919                 {
920                   front_p = &front;
921                   iter_stack_copy_current (stack_p, front_p);
922                 }
923             }
924           else
925             {
926               bfd_boolean is_target;
927               current_p = &current;
928               iter_stack_copy_current (stack_p, current_p);
929               is_target = (section_is_target (deps, l)
930                            && !section_is_source (deps, l));
931
932               if (in_literals)
933                 {
934                   iter_stack_copy_current (stack_p, front_p);
935                   if (!is_target)
936                     in_literals = FALSE;
937                 }
938               else
939                 {
940                   if (is_target)
941                     {
942                       /* Try to insert in place.  */
943                       ld_xtensa_move_section_after (front_p, current_p);
944                       ld_assign_relative_paged_dot (0x100000,
945                                                     statement,
946                                                     deps,
947                                                     xtensa_use_literal_pages);
948
949                       /* We use this code because it's already written.  */
950                       if (!ld_local_file_relocations_fit (statement, deps))
951                         {
952                           /* Move it back.  */
953                           ld_xtensa_move_section_after (current_p, front_p);
954                           /* Reset the literal placement.  */
955                           iter_stack_copy_current (stack_p, front_p);
956                         }
957                       else
958                         {
959                           /* Move front pointer up by one.  */
960                           front_p->loc = &(*front_p->loc)->header.next;
961
962                           /* Do not increment the current pointer.  */
963                           skip_increment = TRUE;
964                         }
965                     }
966                 }
967             }
968           break;
969         default:
970           break;
971         }
972
973       if (!skip_increment)
974         iter_stack_next (stack_p);
975       else
976         /* Be careful to update the stack_p if it now is a null.  */
977         iter_stack_update (stack_p);
978     }
979
980   lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
981 }
982
983
984 static void
985 xtensa_move_dependencies_to_front (reloc_deps_graph *deps,
986                                    lang_wild_statement_type *w)
987 {
988   /* Keep a front pointer and a current pointer.  */
989   lang_statement_union_type **front;
990   lang_statement_union_type **current;
991
992   /* Walk to the end of the targets.  */
993   for (front = &w->children.head;
994        (*front != NULL) && section_is_source_or_target (deps, *front);
995        front = &(*front)->header.next)
996     ;
997
998   if (*front == NULL)
999     return;
1000
1001   current = &(*front)->header.next;
1002   while (*current != NULL)
1003     {
1004       if (section_is_source_or_target (deps, *current))
1005         {
1006           /* Insert in place.  */
1007           xtensa_ld_iter front_iter;
1008           xtensa_ld_iter current_iter;
1009
1010           front_iter.parent = (lang_statement_union_type *) w;
1011           front_iter.l = &w->children;
1012           front_iter.loc = front;
1013
1014           current_iter.parent = (lang_statement_union_type *) w;
1015           current_iter.l = &w->children;
1016           current_iter.loc = current;
1017
1018           ld_xtensa_move_section_after (&front_iter, &current_iter);
1019           front = &(*front)->header.next;
1020         }
1021       else
1022         {
1023           current = &(*current)->header.next;
1024         }
1025     }
1026 }
1027
1028
1029 static bfd_boolean
1030 deps_has_sec_edge (const reloc_deps_graph *deps,
1031                    asection *src,
1032                    asection *tgt)
1033 {
1034   const reloc_deps_section *sec_deps;
1035   const reloc_deps_e *sec_deps_e;
1036
1037   sec_deps = xtensa_get_section_deps (deps, src);
1038   if (sec_deps == NULL)
1039     return FALSE;
1040
1041   for (sec_deps_e = sec_deps->succs;
1042        sec_deps_e != NULL;
1043        sec_deps_e = sec_deps_e->next)
1044     {
1045       ASSERT (sec_deps_e->src == src);
1046       if (sec_deps_e->tgt == tgt)
1047         return TRUE;
1048     }
1049   return FALSE;
1050 }
1051
1052
1053 static bfd_boolean
1054 deps_has_edge (const reloc_deps_graph *deps,
1055                lang_statement_union_type *src,
1056                lang_statement_union_type *tgt)
1057 {
1058   if (!section_is_source (deps, src))
1059     return FALSE;
1060   if (!section_is_target (deps, tgt))
1061     return FALSE;
1062
1063   if (src->header.type != lang_input_section_enum)
1064     return FALSE;
1065   if (tgt->header.type != lang_input_section_enum)
1066     return FALSE;
1067
1068   return deps_has_sec_edge (deps, src->input_section.section,
1069                             tgt->input_section.section);
1070 }
1071
1072
1073 static void
1074 add_deps_edge (reloc_deps_graph *deps,
1075                asection *src_sec,
1076                asection *tgt_sec)
1077 {
1078   reloc_deps_section *src_sec_deps;
1079   reloc_deps_section *tgt_sec_deps;
1080
1081   reloc_deps_e *src_edge;
1082   reloc_deps_e *tgt_edge;
1083
1084   if (deps_has_sec_edge (deps, src_sec, tgt_sec))
1085     return;
1086
1087   src_sec_deps = xtensa_get_section_deps (deps, src_sec);
1088   if (src_sec_deps == NULL)
1089     {
1090       /* Add a section.  */
1091       src_sec_deps = xmalloc (sizeof (reloc_deps_section));
1092       memset (src_sec_deps, 0, sizeof (reloc_deps_section));
1093       src_sec_deps->is_only_literal = 0;
1094       src_sec_deps->preds = NULL;
1095       src_sec_deps->succs = NULL;
1096       xtensa_set_section_deps (deps, src_sec, src_sec_deps);
1097       xtensa_append_section_deps (deps, src_sec);
1098     }
1099
1100   tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
1101   if (tgt_sec_deps == NULL)
1102     {
1103       /* Add a section.  */
1104       tgt_sec_deps = xmalloc (sizeof (reloc_deps_section));
1105       memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
1106       tgt_sec_deps->is_only_literal = 0;
1107       tgt_sec_deps->preds = NULL;
1108       tgt_sec_deps->succs = NULL;
1109       xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
1110       xtensa_append_section_deps (deps, tgt_sec);
1111     }
1112
1113   /* Add the edges.  */
1114   src_edge = xmalloc (sizeof (reloc_deps_e));
1115   memset (src_edge, 0, sizeof (reloc_deps_e));
1116   src_edge->src = src_sec;
1117   src_edge->tgt = tgt_sec;
1118   src_edge->next = src_sec_deps->succs;
1119   src_sec_deps->succs = src_edge;
1120
1121   tgt_edge = xmalloc (sizeof (reloc_deps_e));
1122   memset (tgt_edge, 0, sizeof (reloc_deps_e));
1123   tgt_edge->src = src_sec;
1124   tgt_edge->tgt = tgt_sec;
1125   tgt_edge->next = tgt_sec_deps->preds;
1126   tgt_sec_deps->preds = tgt_edge;
1127 }
1128
1129
1130 static void
1131 build_deps_graph_callback (asection *src_sec,
1132                            bfd_vma src_offset ATTRIBUTE_UNUSED,
1133                            asection *target_sec,
1134                            bfd_vma target_offset ATTRIBUTE_UNUSED,
1135                            void *closure)
1136 {
1137   reloc_deps_graph *deps = closure;
1138
1139   /* If the target is defined.  */
1140   if (target_sec != NULL)
1141     add_deps_edge (deps, src_sec, target_sec);
1142 }
1143
1144
1145 static reloc_deps_graph *
1146 ld_build_required_section_dependence (lang_statement_union_type *s)
1147 {
1148   reloc_deps_graph *deps;
1149   xtensa_ld_iter_stack *stack = NULL;
1150
1151   deps = xmalloc (sizeof (reloc_deps_graph));
1152   deps->sections = NULL;
1153   deps->count = 0;
1154   deps->size = 0;
1155
1156   for (iter_stack_create (&stack, s);
1157        !iter_stack_empty (&stack);
1158        iter_stack_next (&stack))
1159     {
1160       lang_statement_union_type *l = iter_stack_current (&stack);
1161
1162       if (l->header.type == lang_input_section_enum)
1163         {
1164           lang_input_section_type *input;
1165           input = &l->input_section;
1166           xtensa_callback_required_dependence (input->ifile->the_bfd,
1167                                                input->section,
1168                                                &link_info,
1169                                                /* Use the same closure.  */
1170                                                build_deps_graph_callback,
1171                                                deps);
1172         }
1173     }
1174   return deps;
1175 }
1176
1177
1178 #if EXTRA_VALIDATION
1179 static size_t
1180 ld_count_children (lang_statement_union_type *s)
1181 {
1182   size_t count = 0;
1183   xtensa_ld_iter_stack *stack = NULL;
1184   for (iter_stack_create (&stack, s);
1185        !iter_stack_empty (&stack);
1186        iter_stack_next (&stack))
1187     {
1188       lang_statement_union_type *l = iter_stack_current (&stack);
1189       ASSERT (l != NULL);
1190       count++;
1191     }
1192   return count;
1193 }
1194 #endif /* EXTRA_VALIDATION */
1195
1196
1197 static void
1198 xtensa_wild_group_interleave_callback (lang_statement_union_type *statement)
1199 {
1200   lang_wild_statement_type *w;
1201   reloc_deps_graph *deps;
1202   if (statement->header.type == lang_wild_statement_enum)
1203     {
1204 #if EXTRA_VALIDATION
1205       size_t old_child_count;
1206       size_t new_child_count;
1207 #endif
1208       bfd_boolean no_reorder;
1209
1210       w = &statement->wild_statement;
1211
1212       no_reorder = FALSE;
1213
1214       /* If it has 0 or 1 section bound, then do not reorder.  */
1215       if (w->children.head == NULL
1216           || (w->children.head->header.type == lang_input_section_enum
1217               && w->children.head->header.next == NULL))
1218         no_reorder = TRUE;
1219
1220       if (w->filenames_sorted)
1221         no_reorder = TRUE;
1222
1223       /* Check for sorting in a section list wildcard spec as well.  */
1224       if (!no_reorder)
1225         {
1226           struct wildcard_list *l;
1227           for (l = w->section_list; l != NULL; l = l->next)
1228             {
1229               if (l->spec.sorted == TRUE)
1230                 {
1231                   no_reorder = TRUE;
1232                   break;
1233                 }
1234             }
1235         }
1236
1237       /* Special case until the NOREORDER linker directive is supported:
1238          *(.init) output sections and *(.fini) specs may NOT be reordered.  */
1239
1240       /* Check for sorting in a section list wildcard spec as well.  */
1241       if (!no_reorder)
1242         {
1243           struct wildcard_list *l;
1244           for (l = w->section_list; l != NULL; l = l->next)
1245             {
1246               if (l->spec.name
1247                   && ((strcmp (".init", l->spec.name) == 0)
1248                       || (strcmp (".fini", l->spec.name) == 0)))
1249                 {
1250                   no_reorder = TRUE;
1251                   break;
1252                 }
1253             }
1254         }
1255
1256 #if EXTRA_VALIDATION
1257       old_child_count = ld_count_children (statement);
1258 #endif
1259
1260       /* It is now officially a target.  Build the graph of source
1261          section -> target section (kept as a list of edges).  */
1262       deps = ld_build_required_section_dependence (statement);
1263
1264       /* If this wildcard does not reorder....  */
1265       if (!no_reorder && deps->count != 0)
1266         {
1267           /* First check for reverse dependences.  Fix if possible.  */
1268           xtensa_layout_wild (deps, w);
1269
1270           xtensa_move_dependencies_to_front (deps, w);
1271 #if EXTRA_VALIDATION
1272           new_child_count = ld_count_children (statement);
1273           ASSERT (new_child_count == old_child_count);
1274 #endif
1275
1276           xtensa_colocate_literals (deps, statement);
1277
1278 #if EXTRA_VALIDATION
1279           new_child_count = ld_count_children (statement);
1280           ASSERT (new_child_count == old_child_count);
1281 #endif
1282         }
1283
1284       /* Clean up.  */
1285       free_reloc_deps_graph (deps);
1286     }
1287 }
1288
1289
1290 static void
1291 xtensa_wild_group_interleave (lang_statement_union_type *s)
1292 {
1293   lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
1294 }
1295
1296
1297 static void
1298 xtensa_layout_wild (const reloc_deps_graph *deps,
1299                     lang_wild_statement_type *w)
1300 {
1301   /* If it does not fit initially, we need to do this step.  Move all
1302      of the wild literal sections to a new list, then move each of
1303      them back in just before the first section they depend on.  */
1304   lang_statement_union_type **s_p;
1305 #if EXTRA_VALIDATION
1306   size_t old_count, new_count;
1307   size_t ct1, ct2;
1308 #endif
1309
1310   lang_wild_statement_type literal_wild;
1311   literal_wild.header.next = NULL;
1312   literal_wild.header.type = lang_wild_statement_enum;
1313   literal_wild.filename = NULL;
1314   literal_wild.filenames_sorted = FALSE;
1315   literal_wild.section_list = NULL;
1316   literal_wild.keep_sections = FALSE;
1317   literal_wild.children.head = NULL;
1318   literal_wild.children.tail = &literal_wild.children.head;
1319
1320 #if EXTRA_VALIDATION
1321   old_count = ld_count_children ((lang_statement_union_type*) w);
1322 #endif
1323
1324   s_p = &w->children.head;
1325   while (*s_p != NULL)
1326     {
1327       lang_statement_union_type *l = *s_p;
1328       if (l->header.type == lang_input_section_enum)
1329         {
1330           if (section_is_target (deps, l)
1331               && ! section_is_source (deps, l))
1332             {
1333               /* Detach.  */
1334               *s_p = l->header.next;
1335               if (*s_p == NULL)
1336                 w->children.tail = s_p;
1337               l->header.next = NULL;
1338
1339               /* Append.  */
1340               *literal_wild.children.tail = l;
1341               literal_wild.children.tail = &l->header.next;
1342               continue;
1343             }
1344         }
1345       s_p = &(*s_p)->header.next;
1346     }
1347
1348 #if EXTRA_VALIDATION
1349   ct1 = ld_count_children ((lang_statement_union_type*) w);
1350   ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
1351
1352   ASSERT (old_count == (ct1 + ct2));
1353 #endif
1354
1355   /* Now place them back in front of their dependent sections.  */
1356
1357   while (literal_wild.children.head != NULL)
1358     {
1359       lang_statement_union_type *lit = literal_wild.children.head;
1360       bfd_boolean placed = FALSE;
1361
1362 #if EXTRA_VALIDATION
1363       ASSERT (ct2 > 0);
1364       ct2--;
1365 #endif
1366
1367       /* Detach.  */
1368       literal_wild.children.head = lit->header.next;
1369       if (literal_wild.children.head == NULL)
1370         literal_wild.children.tail = &literal_wild.children.head;
1371       lit->header.next = NULL;
1372
1373       /* Find a spot to place it.  */
1374       for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
1375         {
1376           lang_statement_union_type *src = *s_p;
1377           if (deps_has_edge (deps, src, lit))
1378             {
1379               /* Place it here.  */
1380               lit->header.next = *s_p;
1381               *s_p = lit;
1382               placed = TRUE;
1383               break;
1384             }
1385         }
1386
1387       if (!placed)
1388         {
1389           /* Put it at the end.  */
1390           *w->children.tail = lit;
1391           w->children.tail = &lit->header.next;
1392         }
1393     }
1394
1395 #if EXTRA_VALIDATION
1396   new_count = ld_count_children ((lang_statement_union_type*) w);
1397   ASSERT (new_count == old_count);
1398 #endif
1399 }
1400
1401
1402 static void
1403 xtensa_colocate_output_literals_callback (lang_statement_union_type *statement)
1404 {
1405   lang_output_section_statement_type *os;
1406   reloc_deps_graph *deps;
1407   if (statement->header.type == lang_output_section_statement_enum)
1408     {
1409       /* Now, we walk over the contours of the output section statement.
1410
1411          First we build the literal section dependences as before.
1412
1413          At the first uniquely_literal section, we mark it as a good
1414          spot to place other literals.  Continue walking (and counting
1415          sizes) until we find the next literal section.  If this
1416          section can be moved to the first one, then we move it.  If
1417          we every find a modification of ".", start over.  If we find
1418          a labeling of the current location, start over.  Finally, at
1419          the end, if we require page alignment, add page alignments.  */
1420
1421 #if EXTRA_VALIDATION
1422       size_t old_child_count;
1423       size_t new_child_count;
1424 #endif
1425       bfd_boolean no_reorder = FALSE;
1426
1427       os = &statement->output_section_statement;
1428
1429 #if EXTRA_VALIDATION
1430       old_child_count = ld_count_children (statement);
1431 #endif
1432
1433       /* It is now officially a target.  Build the graph of source
1434          section -> target section (kept as a list of edges).  */
1435
1436       deps = ld_build_required_section_dependence (statement);
1437
1438       /* If this wildcard does not reorder....  */
1439       if (!no_reorder)
1440         {
1441           /* First check for reverse dependences.  Fix if possible.  */
1442           xtensa_colocate_literals (deps, statement);
1443
1444 #if EXTRA_VALIDATION
1445           new_child_count = ld_count_children (statement);
1446           ASSERT (new_child_count == old_child_count);
1447 #endif
1448         }
1449
1450       /* Insert align/offset assignment statement.  */
1451       if (xtensa_use_literal_pages)
1452         {
1453           ld_xtensa_insert_page_offsets (0, statement, deps,
1454                                          xtensa_use_literal_pages);
1455           lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
1456                                           statement);
1457         }
1458
1459       /* Clean up.  */
1460       free_reloc_deps_graph (deps);
1461     }
1462 }
1463
1464
1465 static void
1466 xtensa_colocate_output_literals (lang_statement_union_type *s)
1467 {
1468   lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
1469 }
1470
1471
1472 static void
1473 xtensa_ldlang_clear_addresses (lang_statement_union_type *statement)
1474 {
1475   switch (statement->header.type)
1476     {
1477     case lang_input_section_enum:
1478       {
1479         asection *bfd_section = statement->input_section.section;
1480         bfd_section->output_offset = 0;
1481       }
1482       break;
1483     default:
1484       break;
1485     }
1486 }
1487
1488
1489 static bfd_vma
1490 ld_assign_relative_paged_dot (bfd_vma dot,
1491                               lang_statement_union_type *s,
1492                               const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
1493                               bfd_boolean lit_align)
1494 {
1495   /* Walk through all of the input statements in this wild statement
1496      assign dot to all of them.  */
1497
1498   xtensa_ld_iter_stack *stack = NULL;
1499   xtensa_ld_iter_stack **stack_p = &stack;
1500
1501   bfd_boolean first_section = FALSE;
1502   bfd_boolean in_literals = FALSE;
1503
1504   for (iter_stack_create (stack_p, s);
1505        !iter_stack_empty (stack_p);
1506        iter_stack_next (stack_p))
1507     {
1508       lang_statement_union_type *l = iter_stack_current (stack_p);
1509
1510       switch (l->header.type)
1511         {
1512         case lang_input_section_enum:
1513           {
1514             asection *section = l->input_section.section;
1515             size_t align_pow = section->alignment_power;
1516             bfd_boolean do_xtensa_alignment = FALSE;
1517
1518             if (lit_align)
1519               {
1520                 bfd_boolean sec_is_target = section_is_target (deps, l);
1521                 bfd_boolean sec_is_source = section_is_source (deps, l);
1522
1523                 if (section->size != 0
1524                     && (first_section
1525                         || (in_literals && !sec_is_target)
1526                         || (!in_literals && sec_is_target)))
1527                   {
1528                     do_xtensa_alignment = TRUE;
1529                   }
1530                 first_section = FALSE;
1531                 if (section->size != 0)
1532                   in_literals = (sec_is_target && !sec_is_source);
1533               }
1534
1535             if (do_xtensa_alignment && xtensa_page_power != 0)
1536               dot += (1 << xtensa_page_power);
1537
1538             dot = align_power (dot, align_pow);
1539             section->output_offset = dot;
1540             dot += section->size;
1541           }
1542           break;
1543         case lang_fill_statement_enum:
1544           dot += l->fill_statement.size;
1545           break;
1546         case lang_padding_statement_enum:
1547           dot += l->padding_statement.size;
1548           break;
1549         default:
1550           break;
1551         }
1552     }
1553   return dot;
1554 }
1555
1556
1557 static bfd_boolean
1558 ld_local_file_relocations_fit (lang_statement_union_type *statement,
1559                                const reloc_deps_graph *deps ATTRIBUTE_UNUSED)
1560 {
1561   /* Walk over all of the dependencies that we identified and make
1562      sure that IF the source and target are here (addr != 0):
1563      1) target addr < source addr
1564      2) (roundup(source + source_size, 4) - rounddown(target, 4))
1565         < (256K - (1 << bad align))
1566      Need a worst-case proof....  */
1567
1568   xtensa_ld_iter_stack *stack = NULL;
1569   xtensa_ld_iter_stack **stack_p = &stack;
1570   size_t max_align_power = 0;
1571   size_t align_penalty = 256;
1572   reloc_deps_e *e;
1573   size_t i;
1574
1575   /* Find the worst-case alignment requirement for this set of statements.  */
1576   for (iter_stack_create (stack_p, statement);
1577        !iter_stack_empty (stack_p);
1578        iter_stack_next (stack_p))
1579     {
1580       lang_statement_union_type *l = iter_stack_current (stack_p);
1581       if (l->header.type == lang_input_section_enum)
1582         {
1583           lang_input_section_type *input = &l->input_section;
1584           asection *section = input->section;
1585           if (section->alignment_power > max_align_power)
1586             max_align_power = section->alignment_power;
1587         }
1588     }
1589
1590   /* Now check that everything fits.  */
1591   for (i = 0; i < deps->count; i++)
1592     {
1593       asection *sec = deps->sections[i];
1594       const reloc_deps_section *deps_section =
1595         xtensa_get_section_deps (deps, sec);
1596       if (deps_section)
1597         {
1598           /* We choose to walk through the successors.  */
1599           for (e = deps_section->succs; e != NULL; e = e->next)
1600             {
1601               if (e->src != e->tgt
1602                   && e->src->output_section == e->tgt->output_section
1603                   && e->src->output_offset != 0
1604                   && e->tgt->output_offset != 0)
1605                 {
1606                   bfd_vma l32r_addr =
1607                     align_power (e->src->output_offset + e->src->size, 2);
1608                   bfd_vma target_addr = e->tgt->output_offset & ~3;
1609                   if (l32r_addr < target_addr)
1610                     {
1611                       fprintf (stderr, "Warning: "
1612                                "l32r target section before l32r\n");
1613                       return FALSE;
1614                     }
1615
1616                   if (l32r_addr - target_addr > 256 * 1024 - align_penalty)
1617                     return FALSE;
1618                 }
1619             }
1620         }
1621     }
1622
1623   return TRUE;
1624 }
1625
1626
1627 static bfd_vma
1628 ld_xtensa_insert_page_offsets (bfd_vma dot,
1629                                lang_statement_union_type *s,
1630                                reloc_deps_graph *deps,
1631                                bfd_boolean lit_align)
1632 {
1633   xtensa_ld_iter_stack *stack = NULL;
1634   xtensa_ld_iter_stack **stack_p = &stack;
1635
1636   bfd_boolean first_section = FALSE;
1637   bfd_boolean in_literals = FALSE;
1638
1639   if (!lit_align)
1640     return FALSE;
1641
1642   for (iter_stack_create (stack_p, s);
1643        !iter_stack_empty (stack_p);
1644        iter_stack_next (stack_p))
1645     {
1646       lang_statement_union_type *l = iter_stack_current (stack_p);
1647
1648       switch (l->header.type)
1649         {
1650         case lang_input_section_enum:
1651           {
1652             asection *section = l->input_section.section;
1653             bfd_boolean do_xtensa_alignment = FALSE;
1654
1655             if (lit_align)
1656               {
1657                 if (section->size != 0
1658                     && (first_section
1659                         || (in_literals && !section_is_target (deps, l))
1660                         || (!in_literals && section_is_target (deps, l))))
1661                   {
1662                     do_xtensa_alignment = TRUE;
1663                   }
1664                 first_section = FALSE;
1665                 if (section->size != 0)
1666                   {
1667                     in_literals = (section_is_target (deps, l)
1668                                    && !section_is_source (deps, l));
1669                   }
1670               }
1671
1672             if (do_xtensa_alignment && xtensa_page_power != 0)
1673               {
1674                 /* Create an expression that increments the current address,
1675                    i.e., "dot", by (1 << xtensa_align_power).  */
1676                 etree_type *name_op = exp_nameop (NAME, ".");
1677                 etree_type *addend_op = exp_intop (1 << xtensa_page_power);
1678                 etree_type *add_op = exp_binop ('+', name_op, addend_op);
1679                 etree_type *assign_op = exp_assop ('=', ".", add_op);
1680
1681                 lang_assignment_statement_type *assign_stmt;
1682                 lang_statement_union_type *assign_union;
1683                 lang_statement_list_type tmplist;
1684                 lang_statement_list_type *old_stat_ptr = stat_ptr;
1685
1686                 /* There is hidden state in "lang_add_assignment".  It
1687                    appends the new assignment statement to the stat_ptr
1688                    list.  Thus, we swap it before and after the call.  */
1689
1690                 tmplist.head = NULL;
1691                 tmplist.tail = &tmplist.head;
1692
1693                 stat_ptr = &tmplist;
1694                 /* Warning: side effect; statement appended to stat_ptr.  */
1695                 assign_stmt = lang_add_assignment (assign_op);
1696                 assign_union = (lang_statement_union_type *) assign_stmt;
1697                 stat_ptr = old_stat_ptr;
1698
1699                 assign_union->header.next = l;
1700                 *(*stack_p)->iterloc.loc = assign_union;
1701                 iter_stack_next (stack_p);
1702               }
1703           }
1704           break;
1705         default:
1706           break;
1707         }
1708     }
1709   return dot;
1710 }
1711
1712 EOF
1713
1714 # Define some shell vars to insert bits of code into the standard ELF
1715 # parse_args and list_options functions.
1716 #
1717 PARSE_AND_LIST_PROLOGUE='
1718 #define OPTION_OPT_SIZEOPT              (300)
1719 #define OPTION_NO_RELAX                 (OPTION_OPT_SIZEOPT + 1)
1720 #define OPTION_LITERAL_MOVEMENT         (OPTION_NO_RELAX + 1)
1721 #define OPTION_NO_LITERAL_MOVEMENT      (OPTION_LITERAL_MOVEMENT + 1)
1722 extern int elf32xtensa_size_opt;
1723 extern int elf32xtensa_no_literal_movement;
1724 '
1725
1726 PARSE_AND_LIST_LONGOPTS='
1727   { "size-opt", no_argument, NULL, OPTION_OPT_SIZEOPT},
1728   { "no-relax", no_argument, NULL, OPTION_NO_RELAX},
1729   { "literal-movement", no_argument, NULL, OPTION_LITERAL_MOVEMENT},
1730   { "no-literal-movement", no_argument, NULL, OPTION_NO_LITERAL_MOVEMENT},
1731 '
1732
1733 PARSE_AND_LIST_OPTIONS='
1734   fprintf (file, _("  --size-opt\t\tWhen relaxing longcalls, prefer size optimization\n\t\t\t  over branch target alignment\n"));
1735   fprintf (file, _("  --no-relax\t\tDo not relax branches or coalesce literals\n"));
1736 '
1737
1738 PARSE_AND_LIST_ARGS_CASES='
1739     case OPTION_OPT_SIZEOPT:
1740       elf32xtensa_size_opt = 1;
1741       break;
1742     case OPTION_NO_RELAX:
1743       disable_relaxation = TRUE;
1744       break;
1745     case OPTION_LITERAL_MOVEMENT:
1746       elf32xtensa_no_literal_movement = 0;
1747       break;
1748     case OPTION_NO_LITERAL_MOVEMENT:
1749       elf32xtensa_no_literal_movement = 1;
1750       break;
1751 '
1752
1753 # Replace some of the standard ELF functions with our own versions.
1754 #
1755 LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
1756 LDEMUL_AFTER_OPEN=elf_xtensa_after_open
1757 LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
1758 LDEMUL_PLACE_ORPHAN=elf_xtensa_place_orphan
1759 LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation
1760