1 # This shell script emits a C file. -*- C -*-
3 # Free Software Foundation, Inc.
5 # This file is part of GLD, the Gnu Linker.
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.
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.
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.
22 # This file is sourced from elf32.em, and defines extra xtensa-elf
25 cat >>e${EMULATION_NAME}.c <<EOF
27 #include <xtensa-config.h>
28 #include "../bfd/elf-bfd.h"
29 #include "../bfd/libbfd.h"
30 #include "elf/xtensa.h"
33 static void xtensa_wild_group_interleave (lang_statement_union_type *);
34 static void xtensa_colocate_output_literals (lang_statement_union_type *);
37 /* Flag for the emulation-specific "--no-relax" option. */
38 static bfd_boolean disable_relaxation = FALSE;
40 /* This number is irrelevant until we turn on use_literal_pages */
41 static bfd_vma xtensa_page_power = 12; /* 4K pages. */
43 /* To force a page break between literals and text, change
44 xtensa_use_literal_pages to "TRUE". */
45 static bfd_boolean xtensa_use_literal_pages = FALSE;
47 #define EXTRA_VALIDATION 0
51 elf_xtensa_choose_target (int argc ATTRIBUTE_UNUSED,
52 char **argv ATTRIBUTE_UNUSED)
55 return "${BIG_OUTPUT_FORMAT}";
57 return "${LITTLE_OUTPUT_FORMAT}";
62 elf_xtensa_place_orphan (lang_input_statement_type *file, asection *s)
64 /* Early exit for relocatable links. */
65 if (link_info.relocatable)
68 return gld${EMULATION_NAME}_place_orphan (file, s);
73 elf_xtensa_before_parse (void)
75 /* Just call the default hook.... Tensilica's version of this function
76 does some other work that isn't relevant here. */
77 gld${EMULATION_NAME}_before_parse ();
82 remove_section (bfd *abfd, asection *os)
85 for (spp = &abfd->sections; *spp; spp = &(*spp)->next)
89 os->owner->section_count--;
96 replace_insn_sec_with_prop_sec (bfd *abfd,
97 const char *insn_sec_name,
98 const char *prop_sec_name,
103 bfd_byte *prop_contents = NULL;
104 bfd_byte *insn_contents = NULL;
105 unsigned entry_count;
107 Elf_Internal_Shdr *symtab_hdr;
108 Elf_Internal_Rela *internal_relocs = NULL;
109 unsigned reloc_count;
112 insn_sec = bfd_get_section_by_name (abfd, insn_sec_name);
113 if (insn_sec == NULL)
115 entry_count = insn_sec->size / 8;
117 prop_sec = bfd_get_section_by_name (abfd, prop_sec_name);
118 if (prop_sec != NULL && insn_sec != NULL)
120 *error_message = _("file already has property tables");
124 if (insn_sec->size != 0)
126 insn_contents = (bfd_byte *) bfd_malloc (insn_sec->size);
127 if (insn_contents == NULL)
129 *error_message = _("out of memory");
132 if (! bfd_get_section_contents (abfd, insn_sec, insn_contents,
133 (file_ptr) 0, insn_sec->size))
135 *error_message = _("failed to read section contents");
140 /* Create a Property table section and relocation section for it. */
141 prop_sec_name = strdup (prop_sec_name);
142 prop_sec = bfd_make_section (abfd, prop_sec_name);
144 || ! bfd_set_section_flags (abfd, prop_sec,
145 bfd_get_section_flags (abfd, insn_sec))
146 || ! bfd_set_section_alignment (abfd, prop_sec, 2))
148 *error_message = _("could not create new section");
152 if (! bfd_set_section_flags (abfd, prop_sec,
153 bfd_get_section_flags (abfd, insn_sec))
154 || ! bfd_set_section_alignment (abfd, prop_sec, 2))
156 *error_message = _("could not set new section properties");
159 prop_sec->size = entry_count * 12;
160 prop_contents = (bfd_byte *) bfd_zalloc (abfd, prop_sec->size);
161 elf_section_data (prop_sec)->this_hdr.contents = prop_contents;
163 /* The entry size and size must be set to allow the linker to compute
164 the number of relocations since it does not use reloc_count. */
165 elf_section_data (prop_sec)->rel_hdr.sh_entsize =
166 sizeof (Elf32_External_Rela);
167 elf_section_data (prop_sec)->rel_hdr.sh_size =
168 elf_section_data (insn_sec)->rel_hdr.sh_size;
170 if (prop_contents == NULL && prop_sec->size != 0)
172 *error_message = _("could not allocate section contents");
176 /* Read the relocations. */
177 reloc_count = insn_sec->reloc_count;
178 if (reloc_count != 0)
180 /* If there is already an internal_reloc, then save it so that the
181 read_relocs function freshly allocates a copy. */
182 Elf_Internal_Rela *saved_relocs = elf_section_data (insn_sec)->relocs;
184 elf_section_data (insn_sec)->relocs = NULL;
186 _bfd_elf_link_read_relocs (abfd, insn_sec, NULL, NULL, FALSE);
187 elf_section_data (insn_sec)->relocs = saved_relocs;
189 if (internal_relocs == NULL)
191 *error_message = _("out of memory");
196 /* Create a relocation section for the property section. */
197 if (internal_relocs != NULL)
199 elf_section_data (prop_sec)->relocs = internal_relocs;
200 prop_sec->reloc_count = reloc_count;
203 /* Now copy each insn table entry to the prop table entry with
204 appropriate flags. */
205 for (entry = 0; entry < entry_count; ++entry)
208 unsigned flags = (XTENSA_PROP_INSN | XTENSA_PROP_INSN_NO_TRANSFORM
209 | XTENSA_PROP_INSN_NO_REORDER);
210 value = bfd_get_32 (abfd, insn_contents + entry * 8 + 0);
211 bfd_put_32 (abfd, value, prop_contents + entry * 12 + 0);
212 value = bfd_get_32 (abfd, insn_contents + entry * 8 + 4);
213 bfd_put_32 (abfd, value, prop_contents + entry * 12 + 4);
214 bfd_put_32 (abfd, flags, prop_contents + entry * 12 + 8);
217 /* Now copy all of the relocations. Change offsets for the
218 instruction table section to offsets in the property table
223 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
225 for (i = 0; i < reloc_count; i++)
227 Elf_Internal_Rela *rela;
230 rela = &internal_relocs[i];
232 /* If this relocation is to the .xt.insn section,
233 change the section number and the offset. */
234 r_offset = rela->r_offset;
235 r_offset += 4 * (r_offset / 8);
236 rela->r_offset = r_offset;
240 remove_section (abfd, insn_sec);
243 free (insn_contents);
248 if (prop_sec && prop_sec->owner)
249 remove_section (abfd, prop_sec);
251 free (insn_contents);
253 free (internal_relocs);
259 #define PROP_SEC_BASE_NAME ".xt.prop"
260 #define INSN_SEC_BASE_NAME ".xt.insn"
261 #define LINKONCE_SEC_OLD_TEXT_BASE_NAME ".gnu.linkonce.x."
265 replace_instruction_table_sections (bfd *abfd, asection *sec)
268 const char *insn_sec_name = NULL;
269 char *prop_sec_name = NULL;
270 char *owned_prop_sec_name = NULL;
271 const char *sec_name;
273 sec_name = bfd_get_section_name (abfd, sec);
274 if (strcmp (sec_name, INSN_SEC_BASE_NAME) == 0)
276 insn_sec_name = INSN_SEC_BASE_NAME;
277 prop_sec_name = PROP_SEC_BASE_NAME;
279 else if (strncmp (sec_name, LINKONCE_SEC_OLD_TEXT_BASE_NAME,
280 strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME)) == 0)
282 insn_sec_name = sec_name;
283 owned_prop_sec_name = (char *) xmalloc (strlen (sec_name) + 20);
284 prop_sec_name = owned_prop_sec_name;
285 strcpy (prop_sec_name, ".gnu.linkonce.prop.t.");
286 strcat (prop_sec_name,
287 sec_name + strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME));
289 if (insn_sec_name != NULL)
291 if (! replace_insn_sec_with_prop_sec (abfd, insn_sec_name, prop_sec_name,
294 einfo (_("%P: warning: failed to convert %s table in %B (%s); subsequent disassembly may be incomplete\n"),
295 insn_sec_name, abfd, message);
298 if (owned_prop_sec_name)
299 free (owned_prop_sec_name);
303 /* This is called after all input sections have been opened to convert
304 instruction tables (.xt.insn, gnu.linkonce.x.*) tables into property
305 tables (.xt.prop) before any section placement. */
308 elf_xtensa_after_open (void)
312 /* First call the ELF version. */
313 gld${EMULATION_NAME}_after_open ();
315 /* Now search the input files looking for instruction table sections. */
316 for (abfd = link_info.input_bfds;
318 abfd = abfd->link_next)
320 asection *sec = abfd->sections;
323 /* Do not use bfd_map_over_sections here since we are removing
324 sections as we iterate. */
327 next_sec = sec->next;
328 replace_instruction_table_sections (abfd, sec);
335 /* This is called after the sections have been attached to output
336 sections, but before any sizes or addresses have been set. */
339 elf_xtensa_before_allocation (void)
342 bfd_boolean is_big_endian = XCHAL_HAVE_BE;
344 /* Check that the output endianness matches the Xtensa
345 configuration. The BFD library always includes both big and
346 little endian target vectors for Xtensa, but it only supports the
347 detailed instruction encode/decode operations (such as are
348 required to process relocations) for the selected Xtensa
351 if (is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
353 einfo (_("%F%P: little endian output does not match "
354 "Xtensa configuration\n"));
356 if (!is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
358 einfo (_("%F%P: big endian output does not match "
359 "Xtensa configuration\n"));
362 /* Check that the endianness for each input file matches the output.
363 The merge_private_bfd_data hook has already reported any mismatches
364 as errors, but those errors are not fatal. At this point, we
365 cannot go any further if there are any mismatches. */
367 for (in_bfd = link_info.input_bfds;
369 in_bfd = in_bfd->link_next)
371 if ((is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
372 || (!is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
373 einfo (_("%F%P: cross-endian linking not supported\n"));
376 /* Enable relaxation by default if the "--no-relax" option was not
377 specified. This is done here instead of in the before_parse hook
378 because there is a check in main() to prohibit use of --relax and
379 -r together and that combination should be allowed for Xtensa. */
381 if (!disable_relaxation)
382 command_line.relax = TRUE;
384 gld${EMULATION_NAME}_before_allocation ();
386 xtensa_wild_group_interleave (stat_ptr->head);
387 if (command_line.relax)
388 xtensa_colocate_output_literals (stat_ptr->head);
390 /* TBD: We need to force the page alignments to here and only do
391 them as needed for the entire output section. Finally, if this
392 is a relocatable link then we need to add alignment notes so
393 that the literals can be separated later. */
397 typedef struct wildcard_list section_name_list;
399 typedef struct reloc_deps_e_t reloc_deps_e;
400 typedef struct reloc_deps_section_t reloc_deps_section;
401 typedef struct reloc_deps_graph_t reloc_deps_graph;
404 struct reloc_deps_e_t
406 asection *src; /* Contains l32rs. */
407 asection *tgt; /* Contains literals. */
411 /* Place these in the userdata field. */
412 struct reloc_deps_section_t
416 bfd_boolean is_only_literal;
420 struct reloc_deps_graph_t
427 static void xtensa_layout_wild
428 (const reloc_deps_graph *, lang_wild_statement_type *);
430 typedef void (*deps_callback_t) (asection *, /* src_sec */
431 bfd_vma, /* src_offset */
432 asection *, /* target_sec */
433 bfd_vma, /* target_offset */
434 void *); /* closure */
436 extern bfd_boolean xtensa_callback_required_dependence
437 (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
438 static void xtensa_ldlang_clear_addresses (lang_statement_union_type *);
439 static bfd_boolean ld_local_file_relocations_fit
440 (lang_statement_union_type *, const reloc_deps_graph *);
441 static bfd_vma ld_assign_relative_paged_dot
442 (bfd_vma, lang_statement_union_type *, const reloc_deps_graph *,
444 static bfd_vma ld_xtensa_insert_page_offsets
445 (bfd_vma, lang_statement_union_type *, reloc_deps_graph *, bfd_boolean);
447 static size_t ld_count_children (lang_statement_union_type *);
450 extern lang_statement_list_type constructor_list;
452 /* Begin verbatim code from ldlang.c:
453 the following are copied from ldlang.c because they are defined
457 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
458 lang_statement_union_type *s)
460 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
464 switch (s->header.type)
466 case lang_constructors_statement_enum:
467 lang_for_each_statement_worker (func, constructor_list.head);
469 case lang_output_section_statement_enum:
470 lang_for_each_statement_worker
472 s->output_section_statement.children.head);
474 case lang_wild_statement_enum:
475 lang_for_each_statement_worker
477 s->wild_statement.children.head);
479 case lang_group_statement_enum:
480 lang_for_each_statement_worker (func,
481 s->group_statement.children.head);
483 case lang_data_statement_enum:
484 case lang_reloc_statement_enum:
485 case lang_object_symbols_statement_enum:
486 case lang_output_statement_enum:
487 case lang_target_statement_enum:
488 case lang_input_section_enum:
489 case lang_input_statement_enum:
490 case lang_assignment_statement_enum:
491 case lang_padding_statement_enum:
492 case lang_address_statement_enum:
493 case lang_fill_statement_enum:
502 /* End of verbatim code from ldlang.c. */
505 static reloc_deps_section *
506 xtensa_get_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
509 /* We have a separate function for this so that
510 we could in the future keep a completely independent
511 structure that maps a section to its dependence edges.
512 For now, we place these in the sec->userdata field. */
513 reloc_deps_section *sec_deps = sec->userdata;
518 xtensa_set_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
520 reloc_deps_section *deps_section)
522 sec->userdata = deps_section;
526 /* This is used to keep a list of all of the sections participating in
527 the graph so we can clean them up quickly. */
530 xtensa_append_section_deps (reloc_deps_graph *deps, asection *sec)
532 if (deps->size <= deps->count)
534 asection **new_sections;
538 new_size = deps->size * 2;
542 new_sections = xmalloc (sizeof (asection *) * new_size);
543 memset (new_sections, 0, sizeof (asection *) * new_size);
544 for (i = 0; i < deps->count; i++)
546 new_sections[i] = deps->sections[i];
548 if (deps->sections != NULL)
549 free (deps->sections);
550 deps->sections = new_sections;
551 deps->size = new_size;
553 deps->sections[deps->count] = sec;
559 free_reloc_deps_graph (reloc_deps_graph *deps)
562 for (i = 0; i < deps->count; i++)
564 asection *sec = deps->sections[i];
565 reloc_deps_section *sec_deps;
566 sec_deps = xtensa_get_section_deps (deps, sec);
570 while (sec_deps->succs != NULL)
572 next = sec_deps->succs->next;
573 free (sec_deps->succs);
574 sec_deps->succs = next;
577 while (sec_deps->preds != NULL)
579 next = sec_deps->preds->next;
580 free (sec_deps->preds);
581 sec_deps->preds = next;
585 xtensa_set_section_deps (deps, sec, NULL);
588 free (deps->sections);
595 section_is_source (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
596 lang_statement_union_type *s)
599 const reloc_deps_section *sec_deps;
601 if (s->header.type != lang_input_section_enum)
603 sec = s->input_section.section;
605 sec_deps = xtensa_get_section_deps (deps, sec);
606 return sec_deps && sec_deps->succs != NULL;
611 section_is_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
612 lang_statement_union_type *s)
615 const reloc_deps_section *sec_deps;
617 if (s->header.type != lang_input_section_enum)
619 sec = s->input_section.section;
621 sec_deps = xtensa_get_section_deps (deps, sec);
622 return sec_deps && sec_deps->preds != NULL;
627 section_is_source_or_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
628 lang_statement_union_type *s)
630 return (section_is_source (deps, s)
631 || section_is_target (deps, s));
635 typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
636 typedef struct xtensa_ld_iter_t xtensa_ld_iter;
638 struct xtensa_ld_iter_t
640 lang_statement_union_type *parent; /* Parent of the list. */
641 lang_statement_list_type *l; /* List that holds it. */
642 lang_statement_union_type **loc; /* Place in the list. */
645 struct xtensa_ld_iter_stack_t
647 xtensa_ld_iter iterloc; /* List that hold it. */
649 xtensa_ld_iter_stack *next; /* Next in the stack. */
650 xtensa_ld_iter_stack *prev; /* Back pointer for stack. */
655 ld_xtensa_move_section_after (xtensa_ld_iter *to, xtensa_ld_iter *current)
657 lang_statement_union_type *to_next;
658 lang_statement_union_type *current_next;
659 lang_statement_union_type **e;
662 size_t old_to_count, new_to_count;
663 size_t old_current_count, new_current_count;
670 old_to_count = ld_count_children (to->parent);
671 old_current_count = ld_count_children (current->parent);
674 to_next = *(to->loc);
675 current_next = (*current->loc)->header.next;
677 *(to->loc) = *(current->loc);
679 *(current->loc) = current_next;
680 (*(to->loc))->header.next = to_next;
682 /* reset "to" list tail */
683 for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
687 /* reset "current" list tail */
688 for (e = ¤t->l->head; *e != NULL; e = &(*e)->header.next)
690 current->l->tail = e;
693 new_to_count = ld_count_children (to->parent);
694 new_current_count = ld_count_children (current->parent);
696 ASSERT ((old_to_count + old_current_count)
697 == (new_to_count + new_current_count));
702 /* Can only be called with lang_statements that have lists. Returns
703 FALSE if the list is empty. */
706 iter_stack_empty (xtensa_ld_iter_stack **stack_p)
708 return *stack_p == NULL;
713 iter_stack_push (xtensa_ld_iter_stack **stack_p,
714 lang_statement_union_type *parent)
716 xtensa_ld_iter_stack *stack;
717 lang_statement_list_type *l = NULL;
719 switch (parent->header.type)
721 case lang_output_section_statement_enum:
722 l = &parent->output_section_statement.children;
724 case lang_wild_statement_enum:
725 l = &parent->wild_statement.children;
727 case lang_group_statement_enum:
728 l = &parent->group_statement.children;
735 /* Empty. do not push. */
736 if (l->tail == &l->head)
739 stack = xmalloc (sizeof (xtensa_ld_iter_stack));
740 memset (stack, 0, sizeof (xtensa_ld_iter_stack));
741 stack->iterloc.parent = parent;
742 stack->iterloc.l = l;
743 stack->iterloc.loc = &l->head;
745 stack->next = *stack_p;
747 if (*stack_p != NULL)
748 (*stack_p)->prev = stack;
755 iter_stack_pop (xtensa_ld_iter_stack **stack_p)
757 xtensa_ld_iter_stack *stack;
763 ASSERT (stack != NULL);
767 if (stack->next != NULL)
768 stack->next->prev = NULL;
770 *stack_p = stack->next;
775 /* This MUST be called if, during iteration, the user changes the
776 underlying structure. It will check for a NULL current and advance
780 iter_stack_update (xtensa_ld_iter_stack **stack_p)
782 if (!iter_stack_empty (stack_p)
783 && (*(*stack_p)->iterloc.loc) == NULL)
785 iter_stack_pop (stack_p);
787 while (!iter_stack_empty (stack_p)
788 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
790 iter_stack_pop (stack_p);
792 if (!iter_stack_empty (stack_p))
793 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
799 iter_stack_next (xtensa_ld_iter_stack **stack_p)
801 xtensa_ld_iter_stack *stack;
802 lang_statement_union_type *current;
805 current = *stack->iterloc.loc;
806 /* If we are on the first element. */
809 switch (current->header.type)
811 case lang_output_section_statement_enum:
812 case lang_wild_statement_enum:
813 case lang_group_statement_enum:
814 /* If the list if not empty, we are done. */
815 if (iter_stack_push (stack_p, *stack->iterloc.loc))
817 /* Otherwise increment the pointer as normal. */
824 while (!iter_stack_empty (stack_p)
825 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
827 iter_stack_pop (stack_p);
829 if (!iter_stack_empty (stack_p))
830 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
834 static lang_statement_union_type *
835 iter_stack_current (xtensa_ld_iter_stack **stack_p)
837 return *((*stack_p)->iterloc.loc);
841 /* The iter stack is a preorder. */
844 iter_stack_create (xtensa_ld_iter_stack **stack_p,
845 lang_statement_union_type *parent)
847 iter_stack_push (stack_p, parent);
852 iter_stack_copy_current (xtensa_ld_iter_stack **stack_p, xtensa_ld_iter *front)
854 *front = (*stack_p)->iterloc;
859 xtensa_colocate_literals (reloc_deps_graph *deps,
860 lang_statement_union_type *statement)
862 /* Keep a stack of pointers to control iteration through the contours. */
863 xtensa_ld_iter_stack *stack = NULL;
864 xtensa_ld_iter_stack **stack_p = &stack;
866 xtensa_ld_iter front; /* Location where new insertion should occur. */
867 xtensa_ld_iter *front_p = NULL;
869 xtensa_ld_iter current; /* Location we are checking. */
870 xtensa_ld_iter *current_p = NULL;
871 bfd_boolean in_literals = FALSE;
873 if (deps->count == 0)
877 ld_assign_relative_paged_dot (0x100000, statement, deps,
878 xtensa_use_literal_pages);
880 if (!ld_local_file_relocations_fit (statement, deps))
881 fprintf (stderr, "initial relocation placement does not fit\n");
883 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
886 iter_stack_create (stack_p, statement);
888 while (!iter_stack_empty (stack_p))
890 bfd_boolean skip_increment = FALSE;
891 lang_statement_union_type *l = iter_stack_current (stack_p);
893 switch (l->header.type)
895 case lang_assignment_statement_enum:
896 /* Any assignment statement should block reordering across it. */
901 case lang_input_section_enum:
904 in_literals = (section_is_target (deps, l)
905 && !section_is_source (deps, l));
909 iter_stack_copy_current (stack_p, front_p);
914 bfd_boolean is_target;
915 current_p = ¤t;
916 iter_stack_copy_current (stack_p, current_p);
917 is_target = (section_is_target (deps, l)
918 && !section_is_source (deps, l));
922 iter_stack_copy_current (stack_p, front_p);
930 /* Try to insert in place. */
931 ld_xtensa_move_section_after (front_p, current_p);
932 ld_assign_relative_paged_dot (0x100000,
935 xtensa_use_literal_pages);
937 /* We use this code because it's already written. */
938 if (!ld_local_file_relocations_fit (statement, deps))
941 ld_xtensa_move_section_after (current_p, front_p);
942 /* Reset the literal placement. */
943 iter_stack_copy_current (stack_p, front_p);
947 /* Move front pointer up by one. */
948 front_p->loc = &(*front_p->loc)->header.next;
950 /* Do not increment the current pointer. */
951 skip_increment = TRUE;
962 iter_stack_next (stack_p);
964 /* Be careful to update the stack_p if it now is a null. */
965 iter_stack_update (stack_p);
968 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
973 xtensa_move_dependencies_to_front (reloc_deps_graph *deps,
974 lang_wild_statement_type *w)
976 /* Keep a front pointer and a current pointer. */
977 lang_statement_union_type **front;
978 lang_statement_union_type **current;
980 /* Walk to the end of the targets. */
981 for (front = &w->children.head;
982 (*front != NULL) && section_is_source_or_target (deps, *front);
983 front = &(*front)->header.next)
989 current = &(*front)->header.next;
990 while (*current != NULL)
992 if (section_is_source_or_target (deps, *current))
994 /* Insert in place. */
995 xtensa_ld_iter front_iter;
996 xtensa_ld_iter current_iter;
998 front_iter.parent = (lang_statement_union_type *) w;
999 front_iter.l = &w->children;
1000 front_iter.loc = front;
1002 current_iter.parent = (lang_statement_union_type *) w;
1003 current_iter.l = &w->children;
1004 current_iter.loc = current;
1006 ld_xtensa_move_section_after (&front_iter, ¤t_iter);
1007 front = &(*front)->header.next;
1011 current = &(*current)->header.next;
1018 deps_has_sec_edge (const reloc_deps_graph *deps, asection *src, asection *tgt)
1020 const reloc_deps_section *sec_deps;
1021 const reloc_deps_e *sec_deps_e;
1023 sec_deps = xtensa_get_section_deps (deps, src);
1024 if (sec_deps == NULL)
1027 for (sec_deps_e = sec_deps->succs;
1029 sec_deps_e = sec_deps_e->next)
1031 ASSERT (sec_deps_e->src == src);
1032 if (sec_deps_e->tgt == tgt)
1040 deps_has_edge (const reloc_deps_graph *deps,
1041 lang_statement_union_type *src,
1042 lang_statement_union_type *tgt)
1044 if (!section_is_source (deps, src))
1046 if (!section_is_target (deps, tgt))
1049 if (src->header.type != lang_input_section_enum)
1051 if (tgt->header.type != lang_input_section_enum)
1054 return deps_has_sec_edge (deps, src->input_section.section,
1055 tgt->input_section.section);
1060 add_deps_edge (reloc_deps_graph *deps, asection *src_sec, asection *tgt_sec)
1062 reloc_deps_section *src_sec_deps;
1063 reloc_deps_section *tgt_sec_deps;
1065 reloc_deps_e *src_edge;
1066 reloc_deps_e *tgt_edge;
1068 if (deps_has_sec_edge (deps, src_sec, tgt_sec))
1071 src_sec_deps = xtensa_get_section_deps (deps, src_sec);
1072 if (src_sec_deps == NULL)
1074 /* Add a section. */
1075 src_sec_deps = xmalloc (sizeof (reloc_deps_section));
1076 memset (src_sec_deps, 0, sizeof (reloc_deps_section));
1077 src_sec_deps->is_only_literal = 0;
1078 src_sec_deps->preds = NULL;
1079 src_sec_deps->succs = NULL;
1080 xtensa_set_section_deps (deps, src_sec, src_sec_deps);
1081 xtensa_append_section_deps (deps, src_sec);
1084 tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
1085 if (tgt_sec_deps == NULL)
1087 /* Add a section. */
1088 tgt_sec_deps = xmalloc (sizeof (reloc_deps_section));
1089 memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
1090 tgt_sec_deps->is_only_literal = 0;
1091 tgt_sec_deps->preds = NULL;
1092 tgt_sec_deps->succs = NULL;
1093 xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
1094 xtensa_append_section_deps (deps, tgt_sec);
1097 /* Add the edges. */
1098 src_edge = xmalloc (sizeof (reloc_deps_e));
1099 memset (src_edge, 0, sizeof (reloc_deps_e));
1100 src_edge->src = src_sec;
1101 src_edge->tgt = tgt_sec;
1102 src_edge->next = src_sec_deps->succs;
1103 src_sec_deps->succs = src_edge;
1105 tgt_edge = xmalloc (sizeof (reloc_deps_e));
1106 memset (tgt_edge, 0, sizeof (reloc_deps_e));
1107 tgt_edge->src = src_sec;
1108 tgt_edge->tgt = tgt_sec;
1109 tgt_edge->next = tgt_sec_deps->preds;
1110 tgt_sec_deps->preds = tgt_edge;
1115 build_deps_graph_callback (asection *src_sec,
1116 bfd_vma src_offset ATTRIBUTE_UNUSED,
1117 asection *target_sec,
1118 bfd_vma target_offset ATTRIBUTE_UNUSED,
1121 reloc_deps_graph *deps = closure;
1123 /* If the target is defined. */
1124 if (target_sec != NULL)
1125 add_deps_edge (deps, src_sec, target_sec);
1129 static reloc_deps_graph *
1130 ld_build_required_section_dependence (lang_statement_union_type *s)
1132 reloc_deps_graph *deps;
1133 xtensa_ld_iter_stack *stack = NULL;
1135 deps = xmalloc (sizeof (reloc_deps_graph));
1136 deps->sections = NULL;
1140 for (iter_stack_create (&stack, s);
1141 !iter_stack_empty (&stack);
1142 iter_stack_next (&stack))
1144 lang_statement_union_type *l = iter_stack_current (&stack);
1146 if (l->header.type == lang_input_section_enum)
1148 lang_input_section_type *input;
1149 input = &l->input_section;
1150 xtensa_callback_required_dependence (input->ifile->the_bfd,
1153 /* Use the same closure. */
1154 build_deps_graph_callback,
1162 #if EXTRA_VALIDATION
1164 ld_count_children (lang_statement_union_type *s)
1167 xtensa_ld_iter_stack *stack = NULL;
1168 for (iter_stack_create (&stack, s);
1169 !iter_stack_empty (&stack);
1170 iter_stack_next (&stack))
1172 lang_statement_union_type *l = iter_stack_current (&stack);
1178 #endif /* EXTRA_VALIDATION */
1182 xtensa_wild_group_interleave_callback (lang_statement_union_type *statement)
1184 lang_wild_statement_type *w;
1185 reloc_deps_graph *deps;
1186 if (statement->header.type == lang_wild_statement_enum)
1188 #if EXTRA_VALIDATION
1189 size_t old_child_count;
1190 size_t new_child_count;
1192 bfd_boolean no_reorder;
1194 w = &statement->wild_statement;
1198 /* If it has 0 or 1 section bound, then do not reorder. */
1199 if (w->children.head == NULL
1200 || (w->children.head->header.type == lang_input_section_enum
1201 && w->children.head->header.next == NULL))
1204 if (w->filenames_sorted)
1207 /* Check for sorting in a section list wildcard spec as well. */
1210 struct wildcard_list *l;
1211 for (l = w->section_list; l != NULL; l = l->next)
1213 if (l->spec.sorted == TRUE)
1221 /* Special case until the NOREORDER linker directive is supported:
1222 *(.init) output sections and *(.fini) specs may NOT be reordered. */
1224 /* Check for sorting in a section list wildcard spec as well. */
1227 struct wildcard_list *l;
1228 for (l = w->section_list; l != NULL; l = l->next)
1231 && ((strcmp (".init", l->spec.name) == 0)
1232 || (strcmp (".fini", l->spec.name) == 0)))
1240 #if EXTRA_VALIDATION
1241 old_child_count = ld_count_children (statement);
1244 /* It is now officially a target. Build the graph of source
1245 section -> target section (kept as a list of edges). */
1246 deps = ld_build_required_section_dependence (statement);
1248 /* If this wildcard does not reorder.... */
1249 if (!no_reorder && deps->count != 0)
1251 /* First check for reverse dependences. Fix if possible. */
1252 xtensa_layout_wild (deps, w);
1254 xtensa_move_dependencies_to_front (deps, w);
1255 #if EXTRA_VALIDATION
1256 new_child_count = ld_count_children (statement);
1257 ASSERT (new_child_count == old_child_count);
1260 xtensa_colocate_literals (deps, statement);
1262 #if EXTRA_VALIDATION
1263 new_child_count = ld_count_children (statement);
1264 ASSERT (new_child_count == old_child_count);
1269 free_reloc_deps_graph (deps);
1275 xtensa_wild_group_interleave (lang_statement_union_type *s)
1277 lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
1282 xtensa_layout_wild (const reloc_deps_graph *deps, lang_wild_statement_type *w)
1284 /* If it does not fit initially, we need to do this step. Move all
1285 of the wild literal sections to a new list, then move each of
1286 them back in just before the first section they depend on. */
1287 lang_statement_union_type **s_p;
1288 #if EXTRA_VALIDATION
1289 size_t old_count, new_count;
1293 lang_wild_statement_type literal_wild;
1294 literal_wild.header.next = NULL;
1295 literal_wild.header.type = lang_wild_statement_enum;
1296 literal_wild.filename = NULL;
1297 literal_wild.filenames_sorted = FALSE;
1298 literal_wild.section_list = NULL;
1299 literal_wild.keep_sections = FALSE;
1300 literal_wild.children.head = NULL;
1301 literal_wild.children.tail = &literal_wild.children.head;
1303 #if EXTRA_VALIDATION
1304 old_count = ld_count_children ((lang_statement_union_type*) w);
1307 s_p = &w->children.head;
1308 while (*s_p != NULL)
1310 lang_statement_union_type *l = *s_p;
1311 if (l->header.type == lang_input_section_enum)
1313 if (section_is_target (deps, l)
1314 && ! section_is_source (deps, l))
1317 *s_p = l->header.next;
1319 w->children.tail = s_p;
1320 l->header.next = NULL;
1323 *literal_wild.children.tail = l;
1324 literal_wild.children.tail = &l->header.next;
1328 s_p = &(*s_p)->header.next;
1331 #if EXTRA_VALIDATION
1332 ct1 = ld_count_children ((lang_statement_union_type*) w);
1333 ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
1335 ASSERT (old_count == (ct1 + ct2));
1338 /* Now place them back in front of their dependent sections. */
1340 while (literal_wild.children.head != NULL)
1342 lang_statement_union_type *lit = literal_wild.children.head;
1343 bfd_boolean placed = FALSE;
1345 #if EXTRA_VALIDATION
1351 literal_wild.children.head = lit->header.next;
1352 if (literal_wild.children.head == NULL)
1353 literal_wild.children.tail = &literal_wild.children.head;
1354 lit->header.next = NULL;
1356 /* Find a spot to place it. */
1357 for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
1359 lang_statement_union_type *src = *s_p;
1360 if (deps_has_edge (deps, src, lit))
1362 /* Place it here. */
1363 lit->header.next = *s_p;
1372 /* Put it at the end. */
1373 *w->children.tail = lit;
1374 w->children.tail = &lit->header.next;
1378 #if EXTRA_VALIDATION
1379 new_count = ld_count_children ((lang_statement_union_type*) w);
1380 ASSERT (new_count == old_count);
1386 xtensa_colocate_output_literals_callback (lang_statement_union_type *statement)
1388 lang_output_section_statement_type *os;
1389 reloc_deps_graph *deps;
1390 if (statement->header.type == lang_output_section_statement_enum)
1392 /* Now, we walk over the contours of the output section statement.
1394 First we build the literal section dependences as before.
1396 At the first uniquely_literal section, we mark it as a good
1397 spot to place other literals. Continue walking (and counting
1398 sizes) until we find the next literal section. If this
1399 section can be moved to the first one, then we move it. If
1400 we every find a modification of ".", start over. If we find
1401 a labeling of the current location, start over. Finally, at
1402 the end, if we require page alignment, add page alignments. */
1404 #if EXTRA_VALIDATION
1405 size_t old_child_count;
1406 size_t new_child_count;
1408 bfd_boolean no_reorder = FALSE;
1410 os = &statement->output_section_statement;
1412 #if EXTRA_VALIDATION
1413 old_child_count = ld_count_children (statement);
1416 /* It is now officially a target. Build the graph of source
1417 section -> target section (kept as a list of edges). */
1419 deps = ld_build_required_section_dependence (statement);
1421 /* If this wildcard does not reorder.... */
1424 /* First check for reverse dependences. Fix if possible. */
1425 xtensa_colocate_literals (deps, statement);
1427 #if EXTRA_VALIDATION
1428 new_child_count = ld_count_children (statement);
1429 ASSERT (new_child_count == old_child_count);
1433 /* Insert align/offset assignment statement. */
1434 if (xtensa_use_literal_pages)
1436 ld_xtensa_insert_page_offsets (0, statement, deps,
1437 xtensa_use_literal_pages);
1438 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
1443 free_reloc_deps_graph (deps);
1449 xtensa_colocate_output_literals (lang_statement_union_type *s)
1451 lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
1456 xtensa_ldlang_clear_addresses (lang_statement_union_type *statement)
1458 switch (statement->header.type)
1460 case lang_input_section_enum:
1462 asection *bfd_section = statement->input_section.section;
1463 bfd_section->output_offset = 0;
1473 ld_assign_relative_paged_dot (bfd_vma dot,
1474 lang_statement_union_type *s,
1475 const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
1476 bfd_boolean lit_align)
1478 /* Walk through all of the input statements in this wild statement
1479 assign dot to all of them. */
1481 xtensa_ld_iter_stack *stack = NULL;
1482 xtensa_ld_iter_stack **stack_p = &stack;
1484 bfd_boolean first_section = FALSE;
1485 bfd_boolean in_literals = FALSE;
1487 for (iter_stack_create (stack_p, s);
1488 !iter_stack_empty (stack_p);
1489 iter_stack_next (stack_p))
1491 lang_statement_union_type *l = iter_stack_current (stack_p);
1493 switch (l->header.type)
1495 case lang_input_section_enum:
1497 asection *section = l->input_section.section;
1498 size_t align_pow = section->alignment_power;
1499 bfd_boolean do_xtensa_alignment = FALSE;
1503 bfd_boolean sec_is_target = section_is_target (deps, l);
1504 bfd_boolean sec_is_source = section_is_source (deps, l);
1506 if (section->size != 0
1508 || (in_literals && !sec_is_target)
1509 || (!in_literals && sec_is_target)))
1511 do_xtensa_alignment = TRUE;
1513 first_section = FALSE;
1514 if (section->size != 0)
1515 in_literals = (sec_is_target && !sec_is_source);
1518 if (do_xtensa_alignment && xtensa_page_power != 0)
1519 dot += (1 << xtensa_page_power);
1521 dot = align_power (dot, align_pow);
1522 section->output_offset = dot;
1523 dot += section->size;
1526 case lang_fill_statement_enum:
1527 dot += l->fill_statement.size;
1529 case lang_padding_statement_enum:
1530 dot += l->padding_statement.size;
1541 ld_local_file_relocations_fit (lang_statement_union_type *statement,
1542 const reloc_deps_graph *deps ATTRIBUTE_UNUSED)
1544 /* Walk over all of the dependencies that we identified and make
1545 sure that IF the source and target are here (addr != 0):
1546 1) target addr < source addr
1547 2) (roundup(source + source_size, 4) - rounddown(target, 4))
1548 < (256K - (1 << bad align))
1549 Need a worst-case proof.... */
1551 xtensa_ld_iter_stack *stack = NULL;
1552 xtensa_ld_iter_stack **stack_p = &stack;
1553 size_t max_align_power = 0;
1554 size_t align_penalty = 256;
1558 /* Find the worst-case alignment requirement for this set of statements. */
1559 for (iter_stack_create (stack_p, statement);
1560 !iter_stack_empty (stack_p);
1561 iter_stack_next (stack_p))
1563 lang_statement_union_type *l = iter_stack_current (stack_p);
1564 if (l->header.type == lang_input_section_enum)
1566 lang_input_section_type *input = &l->input_section;
1567 asection *section = input->section;
1568 if (section->alignment_power > max_align_power)
1569 max_align_power = section->alignment_power;
1573 /* Now check that everything fits. */
1574 for (i = 0; i < deps->count; i++)
1576 asection *sec = deps->sections[i];
1577 const reloc_deps_section *deps_section =
1578 xtensa_get_section_deps (deps, sec);
1581 /* We choose to walk through the successors. */
1582 for (e = deps_section->succs; e != NULL; e = e->next)
1584 if (e->src != e->tgt
1585 && e->src->output_section == e->tgt->output_section
1586 && e->src->output_offset != 0
1587 && e->tgt->output_offset != 0)
1590 align_power (e->src->output_offset + e->src->size, 2);
1591 bfd_vma target_addr = e->tgt->output_offset & ~3;
1592 if (l32r_addr < target_addr)
1594 fprintf (stderr, "Warning: "
1595 "l32r target section before l32r\n");
1599 if (l32r_addr - target_addr > 256 * 1024 - align_penalty)
1611 ld_xtensa_insert_page_offsets (bfd_vma dot,
1612 lang_statement_union_type *s,
1613 reloc_deps_graph *deps,
1614 bfd_boolean lit_align)
1616 xtensa_ld_iter_stack *stack = NULL;
1617 xtensa_ld_iter_stack **stack_p = &stack;
1619 bfd_boolean first_section = FALSE;
1620 bfd_boolean in_literals = FALSE;
1625 for (iter_stack_create (stack_p, s);
1626 !iter_stack_empty (stack_p);
1627 iter_stack_next (stack_p))
1629 lang_statement_union_type *l = iter_stack_current (stack_p);
1631 switch (l->header.type)
1633 case lang_input_section_enum:
1635 asection *section = l->input_section.section;
1636 bfd_boolean do_xtensa_alignment = FALSE;
1640 if (section->size != 0
1642 || (in_literals && !section_is_target (deps, l))
1643 || (!in_literals && section_is_target (deps, l))))
1645 do_xtensa_alignment = TRUE;
1647 first_section = FALSE;
1648 if (section->size != 0)
1650 in_literals = (section_is_target (deps, l)
1651 && !section_is_source (deps, l));
1655 if (do_xtensa_alignment && xtensa_page_power != 0)
1657 /* Create an expression that increments the current address,
1658 i.e., "dot", by (1 << xtensa_align_power). */
1659 etree_type *name_op = exp_nameop (NAME, ".");
1660 etree_type *addend_op = exp_intop (1 << xtensa_page_power);
1661 etree_type *add_op = exp_binop ('+', name_op, addend_op);
1662 etree_type *assign_op = exp_assop ('=', ".", add_op);
1664 lang_assignment_statement_type *assign_stmt;
1665 lang_statement_union_type *assign_union;
1666 lang_statement_list_type tmplist;
1667 lang_statement_list_type *old_stat_ptr = stat_ptr;
1669 /* There is hidden state in "lang_add_assignment". It
1670 appends the new assignment statement to the stat_ptr
1671 list. Thus, we swap it before and after the call. */
1673 tmplist.head = NULL;
1674 tmplist.tail = &tmplist.head;
1676 stat_ptr = &tmplist;
1677 /* Warning: side effect; statement appended to stat_ptr. */
1678 assign_stmt = lang_add_assignment (assign_op);
1679 assign_union = (lang_statement_union_type *) assign_stmt;
1680 stat_ptr = old_stat_ptr;
1682 assign_union->header.next = l;
1683 *(*stack_p)->iterloc.loc = assign_union;
1684 iter_stack_next (stack_p);
1697 # Define some shell vars to insert bits of code into the standard ELF
1698 # parse_args and list_options functions.
1700 PARSE_AND_LIST_PROLOGUE='
1701 #define OPTION_OPT_SIZEOPT (300)
1702 #define OPTION_NO_RELAX (OPTION_OPT_SIZEOPT + 1)
1703 #define OPTION_LITERAL_MOVEMENT (OPTION_NO_RELAX + 1)
1704 #define OPTION_NO_LITERAL_MOVEMENT (OPTION_LITERAL_MOVEMENT + 1)
1705 extern int elf32xtensa_size_opt;
1706 extern int elf32xtensa_no_literal_movement;
1709 PARSE_AND_LIST_LONGOPTS='
1710 { "size-opt", no_argument, NULL, OPTION_OPT_SIZEOPT},
1711 { "no-relax", no_argument, NULL, OPTION_NO_RELAX},
1712 { "literal-movement", no_argument, NULL, OPTION_LITERAL_MOVEMENT},
1713 { "no-literal-movement", no_argument, NULL, OPTION_NO_LITERAL_MOVEMENT},
1716 PARSE_AND_LIST_OPTIONS='
1717 fprintf (file, _(" --size-opt\t\tWhen relaxing longcalls, prefer size optimization\n\t\t\t over branch target alignment\n"));
1718 fprintf (file, _(" --no-relax\t\tDo not relax branches or coalesce literals\n"));
1721 PARSE_AND_LIST_ARGS_CASES='
1722 case OPTION_OPT_SIZEOPT:
1723 elf32xtensa_size_opt = 1;
1725 case OPTION_NO_RELAX:
1726 disable_relaxation = TRUE;
1728 case OPTION_LITERAL_MOVEMENT:
1729 elf32xtensa_no_literal_movement = 0;
1731 case OPTION_NO_LITERAL_MOVEMENT:
1732 elf32xtensa_no_literal_movement = 1;
1736 # Replace some of the standard ELF functions with our own versions.
1738 LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
1739 LDEMUL_AFTER_OPEN=elf_xtensa_after_open
1740 LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
1741 LDEMUL_PLACE_ORPHAN=elf_xtensa_place_orphan
1742 LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation