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