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 *);
35 static void remove_section (bfd *, asection *);
36 static bfd_boolean replace_insn_sec_with_prop_sec (bfd *, const char *,
37 const char *, char **);
38 static void replace_instruction_table_sections (bfd *, asection *);
41 /* Flag for the emulation-specific "--no-relax" option. */
42 static bfd_boolean disable_relaxation = FALSE;
44 /* This number is irrelevant until we turn on use_literal_pages */
45 static bfd_vma xtensa_page_power = 12; /* 4K pages. */
47 /* To force a page break between literals and text, change
48 xtensa_use_literal_pages to "TRUE". */
49 static bfd_boolean xtensa_use_literal_pages = FALSE;
51 #define EXTRA_VALIDATION 0
55 elf_xtensa_choose_target (int argc ATTRIBUTE_UNUSED,
56 char **argv ATTRIBUTE_UNUSED)
59 return "${BIG_OUTPUT_FORMAT}";
61 return "${LITTLE_OUTPUT_FORMAT}";
66 elf_xtensa_place_orphan (lang_input_statement_type *file, asection *s)
68 /* Early exit for relocatable links. */
69 if (link_info.relocatable)
72 return gld${EMULATION_NAME}_place_orphan (file, s);
77 elf_xtensa_before_parse (void)
79 /* Just call the default hook.... Tensilica's version of this function
80 does some other work that isn't relevant here. */
81 gld${EMULATION_NAME}_before_parse ();
86 remove_section (abfd, os)
91 for (spp = &abfd->sections; *spp; spp = &(*spp)->next)
95 os->owner->section_count--;
102 replace_insn_sec_with_prop_sec (abfd, insn_sec_name, prop_sec_name,
105 const char *insn_sec_name;
106 const char *prop_sec_name;
107 char **error_message;
111 bfd_byte *prop_contents = NULL;
112 bfd_byte *insn_contents = NULL;
113 unsigned entry_count;
115 Elf_Internal_Shdr *symtab_hdr;
116 Elf_Internal_Rela *internal_relocs = NULL;
117 unsigned reloc_count;
120 insn_sec = bfd_get_section_by_name (abfd, insn_sec_name);
121 if (insn_sec == NULL)
123 entry_count = insn_sec->size / 8;
125 prop_sec = bfd_get_section_by_name (abfd, prop_sec_name);
126 if (prop_sec != NULL && insn_sec != NULL)
128 *error_message = _("file already has property tables");
132 if (insn_sec->size != 0)
134 insn_contents = (bfd_byte *) bfd_malloc (insn_sec->size);
135 if (insn_contents == NULL)
137 *error_message = _("out of memory");
140 if (! bfd_get_section_contents (abfd, insn_sec, insn_contents,
141 (file_ptr) 0, insn_sec->size))
143 *error_message = _("failed to read section contents");
148 /* Create a Property table section and relocation section for it. */
149 prop_sec_name = strdup (prop_sec_name);
150 prop_sec = bfd_make_section (abfd, prop_sec_name);
152 || ! bfd_set_section_flags (abfd, prop_sec,
153 bfd_get_section_flags (abfd, insn_sec))
154 || ! bfd_set_section_alignment (abfd, prop_sec, 2))
156 *error_message = _("could not create new section");
160 if (! bfd_set_section_flags (abfd, prop_sec,
161 bfd_get_section_flags (abfd, insn_sec))
162 || ! bfd_set_section_alignment (abfd, prop_sec, 2))
164 *error_message = _("could not set new section properties");
167 prop_sec->size = entry_count * 12;
168 prop_contents = (bfd_byte *) bfd_zalloc (abfd, prop_sec->size);
169 elf_section_data (prop_sec)->this_hdr.contents = prop_contents;
171 /* The entry size and size must be set to allow the linker to compute
172 the number of relocations since it does not use reloc_count. */
173 elf_section_data (prop_sec)->rel_hdr.sh_entsize =
174 sizeof (Elf32_External_Rela);
175 elf_section_data (prop_sec)->rel_hdr.sh_size =
176 elf_section_data (insn_sec)->rel_hdr.sh_size;
178 if (prop_contents == NULL && prop_sec->size != 0)
180 *error_message = _("could not allocate section contents");
184 /* Read the relocations. */
185 reloc_count = insn_sec->reloc_count;
186 if (reloc_count != 0)
188 /* If there is already an internal_reloc, then save it so that the
189 read_relocs function freshly allocates a copy. */
190 Elf_Internal_Rela *saved_relocs = elf_section_data (insn_sec)->relocs;
192 elf_section_data (insn_sec)->relocs = NULL;
194 _bfd_elf_link_read_relocs (abfd, insn_sec, NULL, NULL, FALSE);
195 elf_section_data (insn_sec)->relocs = saved_relocs;
197 if (internal_relocs == NULL)
199 *error_message = _("out of memory");
204 /* Create a relocation section for the property section. */
205 if (internal_relocs != NULL)
207 elf_section_data (prop_sec)->relocs = internal_relocs;
208 prop_sec->reloc_count = reloc_count;
211 /* Now copy each insn table entry to the prop table entry with
212 appropriate flags. */
213 for (entry = 0; entry < entry_count; ++entry)
216 unsigned flags = (XTENSA_PROP_INSN | XTENSA_PROP_INSN_NO_TRANSFORM
217 | XTENSA_PROP_INSN_NO_REORDER);
218 value = bfd_get_32 (abfd, insn_contents + entry * 8 + 0);
219 bfd_put_32 (abfd, value, prop_contents + entry * 12 + 0);
220 value = bfd_get_32 (abfd, insn_contents + entry * 8 + 4);
221 bfd_put_32 (abfd, value, prop_contents + entry * 12 + 4);
222 bfd_put_32 (abfd, flags, prop_contents + entry * 12 + 8);
225 /* Now copy all of the relocations. Change offsets for the
226 instruction table section to offsets in the property table
231 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
233 for (i = 0; i < reloc_count; i++)
235 Elf_Internal_Rela *rela;
238 rela = &internal_relocs[i];
240 /* If this relocation is to the .xt.insn section,
241 change the section number and the offset. */
242 r_offset = rela->r_offset;
243 r_offset += 4 * (r_offset / 8);
244 rela->r_offset = r_offset;
248 remove_section (abfd, insn_sec);
251 free (insn_contents);
256 if (prop_sec && prop_sec->owner)
257 remove_section (abfd, prop_sec);
259 free (insn_contents);
261 free (internal_relocs);
267 #define PROP_SEC_BASE_NAME ".xt.prop"
268 #define INSN_SEC_BASE_NAME ".xt.insn"
269 #define LINKONCE_SEC_OLD_TEXT_BASE_NAME ".gnu.linkonce.x."
273 replace_instruction_table_sections (abfd, sec)
278 const char *insn_sec_name = NULL;
279 char *prop_sec_name = NULL;
280 char *owned_prop_sec_name = NULL;
281 const char *sec_name;
283 sec_name = bfd_get_section_name (abfd, sec);
284 if (strcmp (sec_name, INSN_SEC_BASE_NAME) == 0)
286 insn_sec_name = INSN_SEC_BASE_NAME;
287 prop_sec_name = PROP_SEC_BASE_NAME;
289 else if (strncmp (sec_name, LINKONCE_SEC_OLD_TEXT_BASE_NAME,
290 strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME)) == 0)
292 insn_sec_name = sec_name;
293 owned_prop_sec_name = (char *) xmalloc (strlen (sec_name) + 20);
294 prop_sec_name = owned_prop_sec_name;
295 strcpy (prop_sec_name, ".gnu.linkonce.prop.t.");
296 strcat (prop_sec_name,
297 sec_name + strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME));
299 if (insn_sec_name != NULL)
301 if (! replace_insn_sec_with_prop_sec (abfd, insn_sec_name, prop_sec_name,
304 einfo (_("%P: warning: failed to convert %s table in %B (%s); subsequent disassembly may be incomplete\n"),
305 insn_sec_name, abfd, message);
308 if (owned_prop_sec_name)
309 free (owned_prop_sec_name);
313 /* This is called after all input sections have been opened to convert
314 instruction tables (.xt.insn, gnu.linkonce.x.*) tables into property
315 tables (.xt.prop) before any section placement. */
318 elf_xtensa_after_open (void)
322 /* First call the ELF version. */
323 gld${EMULATION_NAME}_after_open ();
325 /* Now search the input files looking for instruction table sections. */
326 for (abfd = link_info.input_bfds;
328 abfd = abfd->link_next)
330 asection *sec = abfd->sections;
333 /* Do not use bfd_map_over_sections here since we are removing
334 sections as we iterate. */
337 next_sec = sec->next;
338 replace_instruction_table_sections (abfd, sec);
345 /* This is called after the sections have been attached to output
346 sections, but before any sizes or addresses have been set. */
349 elf_xtensa_before_allocation (void)
352 bfd_boolean is_big_endian = XCHAL_HAVE_BE;
354 /* Check that the output endianness matches the Xtensa
355 configuration. The BFD library always includes both big and
356 little endian target vectors for Xtensa, but it only supports the
357 detailed instruction encode/decode operations (such as are
358 required to process relocations) for the selected Xtensa
361 if (is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
363 einfo (_("%F%P: little endian output does not match "
364 "Xtensa configuration\n"));
366 if (!is_big_endian && output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
368 einfo (_("%F%P: big endian output does not match "
369 "Xtensa configuration\n"));
372 /* Check that the endianness for each input file matches the output.
373 The merge_private_bfd_data hook has already reported any mismatches
374 as errors, but those errors are not fatal. At this point, we
375 cannot go any further if there are any mismatches. */
377 for (in_bfd = link_info.input_bfds;
379 in_bfd = in_bfd->link_next)
381 if ((is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
382 || (!is_big_endian && in_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
383 einfo (_("%F%P: cross-endian linking not supported\n"));
386 /* Enable relaxation by default if the "--no-relax" option was not
387 specified. This is done here instead of in the before_parse hook
388 because there is a check in main() to prohibit use of --relax and
389 -r together and that combination should be allowed for Xtensa. */
391 if (!disable_relaxation)
392 command_line.relax = TRUE;
394 gld${EMULATION_NAME}_before_allocation ();
396 xtensa_wild_group_interleave (stat_ptr->head);
397 if (command_line.relax)
398 xtensa_colocate_output_literals (stat_ptr->head);
400 /* TBD: We need to force the page alignments to here and only do
401 them as needed for the entire output section. Finally, if this
402 is a relocatable link then we need to add alignment notes so
403 that the literals can be separated later. */
407 typedef struct wildcard_list section_name_list;
409 typedef struct reloc_deps_e_t reloc_deps_e;
410 typedef struct reloc_deps_section_t reloc_deps_section;
411 typedef struct reloc_deps_graph_t reloc_deps_graph;
414 struct reloc_deps_e_t
416 asection *src; /* Contains l32rs. */
417 asection *tgt; /* Contains literals. */
421 /* Place these in the userdata field. */
422 struct reloc_deps_section_t
426 bfd_boolean is_only_literal;
430 struct reloc_deps_graph_t
437 static void xtensa_layout_wild
438 (const reloc_deps_graph *, lang_wild_statement_type *);
440 typedef void (*deps_callback_t) (asection *, /* src_sec */
441 bfd_vma, /* src_offset */
442 asection *, /* target_sec */
443 bfd_vma, /* target_offset */
444 void *); /* closure */
446 extern bfd_boolean xtensa_callback_required_dependence
447 (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
448 static void xtensa_ldlang_clear_addresses
449 (lang_statement_union_type *);
450 static bfd_boolean ld_local_file_relocations_fit
451 (lang_statement_union_type *, const reloc_deps_graph *);
452 static bfd_vma ld_assign_relative_paged_dot
453 (bfd_vma, lang_statement_union_type *, const reloc_deps_graph *,
455 static bfd_vma ld_xtensa_insert_page_offsets
456 (bfd_vma, lang_statement_union_type *, reloc_deps_graph *, bfd_boolean);
458 static size_t ld_count_children
459 (lang_statement_union_type *);
462 extern lang_statement_list_type constructor_list;
464 /* Begin verbatim code from ldlang.c:
465 the following are copied from ldlang.c because they are defined
469 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
470 lang_statement_union_type *s)
472 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
476 switch (s->header.type)
478 case lang_constructors_statement_enum:
479 lang_for_each_statement_worker (func, constructor_list.head);
481 case lang_output_section_statement_enum:
482 lang_for_each_statement_worker
484 s->output_section_statement.children.head);
486 case lang_wild_statement_enum:
487 lang_for_each_statement_worker
489 s->wild_statement.children.head);
491 case lang_group_statement_enum:
492 lang_for_each_statement_worker (func,
493 s->group_statement.children.head);
495 case lang_data_statement_enum:
496 case lang_reloc_statement_enum:
497 case lang_object_symbols_statement_enum:
498 case lang_output_statement_enum:
499 case lang_target_statement_enum:
500 case lang_input_section_enum:
501 case lang_input_statement_enum:
502 case lang_assignment_statement_enum:
503 case lang_padding_statement_enum:
504 case lang_address_statement_enum:
505 case lang_fill_statement_enum:
514 /* End of verbatim code from ldlang.c. */
517 static reloc_deps_section *
518 xtensa_get_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
521 /* We have a separate function for this so that
522 we could in the future keep a completely independent
523 structure that maps a section to its dependence edges.
524 For now, we place these in the sec->userdata field. */
525 reloc_deps_section *sec_deps = sec->userdata;
530 xtensa_set_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
532 reloc_deps_section *deps_section)
534 sec->userdata = deps_section;
538 /* This is used to keep a list of all of the sections participating in
539 the graph so we can clean them up quickly. */
542 xtensa_append_section_deps (reloc_deps_graph *deps, asection *sec)
544 if (deps->size <= deps->count)
546 asection **new_sections;
550 new_size = deps->size * 2;
554 new_sections = xmalloc (sizeof (asection *) * new_size);
555 memset (new_sections, 0, sizeof (asection *) * new_size);
556 for (i = 0; i < deps->count; i++)
558 new_sections[i] = deps->sections[i];
560 if (deps->sections != NULL)
561 free (deps->sections);
562 deps->sections = new_sections;
563 deps->size = new_size;
565 deps->sections[deps->count] = sec;
571 free_reloc_deps_graph (reloc_deps_graph *deps)
574 for (i = 0; i < deps->count; i++)
576 asection *sec = deps->sections[i];
577 reloc_deps_section *sec_deps;
578 sec_deps = xtensa_get_section_deps (deps, sec);
582 while (sec_deps->succs != NULL)
584 next = sec_deps->succs->next;
585 free (sec_deps->succs);
586 sec_deps->succs = next;
589 while (sec_deps->preds != NULL)
591 next = sec_deps->preds->next;
592 free (sec_deps->preds);
593 sec_deps->preds = next;
597 xtensa_set_section_deps (deps, sec, NULL);
600 free (deps->sections);
607 section_is_source (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
608 lang_statement_union_type *s)
611 const reloc_deps_section *sec_deps;
613 if (s->header.type != lang_input_section_enum)
615 sec = s->input_section.section;
617 sec_deps = xtensa_get_section_deps (deps, sec);
618 return sec_deps && sec_deps->succs != NULL;
623 section_is_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
624 lang_statement_union_type *s)
627 const reloc_deps_section *sec_deps;
629 if (s->header.type != lang_input_section_enum)
631 sec = s->input_section.section;
633 sec_deps = xtensa_get_section_deps (deps, sec);
634 return sec_deps && sec_deps->preds != NULL;
638 section_is_source_or_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
639 lang_statement_union_type *s)
641 return (section_is_source (deps, s)
642 || section_is_target (deps, s));
646 typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
647 typedef struct xtensa_ld_iter_t xtensa_ld_iter;
649 struct xtensa_ld_iter_t
651 lang_statement_union_type *parent; /* Parent of the list. */
652 lang_statement_list_type *l; /* List that holds it. */
653 lang_statement_union_type **loc; /* Place in the list. */
656 struct xtensa_ld_iter_stack_t
658 xtensa_ld_iter iterloc; /* List that hold it. */
660 xtensa_ld_iter_stack *next; /* Next in the stack. */
661 xtensa_ld_iter_stack *prev; /* Back pointer for stack. */
666 ld_xtensa_move_section_after (xtensa_ld_iter *to, xtensa_ld_iter *current)
668 lang_statement_union_type *to_next;
669 lang_statement_union_type *current_next;
670 lang_statement_union_type **e;
673 size_t old_to_count, new_to_count;
674 size_t old_current_count, new_current_count;
681 old_to_count = ld_count_children (to->parent);
682 old_current_count = ld_count_children (current->parent);
685 to_next = *(to->loc);
686 current_next = (*current->loc)->header.next;
688 *(to->loc) = *(current->loc);
690 *(current->loc) = current_next;
691 (*(to->loc))->header.next = to_next;
693 /* reset "to" list tail */
694 for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
698 /* reset "current" list tail */
699 for (e = ¤t->l->head; *e != NULL; e = &(*e)->header.next)
701 current->l->tail = e;
704 new_to_count = ld_count_children (to->parent);
705 new_current_count = ld_count_children (current->parent);
707 ASSERT ((old_to_count + old_current_count)
708 == (new_to_count + new_current_count));
713 /* Can only be called with lang_statements that have lists. Returns
714 FALSE if the list is empty. */
717 iter_stack_empty (xtensa_ld_iter_stack **stack_p)
719 return *stack_p == NULL;
724 iter_stack_push (xtensa_ld_iter_stack **stack_p,
725 lang_statement_union_type *parent)
727 xtensa_ld_iter_stack *stack;
728 lang_statement_list_type *l = NULL;
730 switch (parent->header.type)
732 case lang_output_section_statement_enum:
733 l = &parent->output_section_statement.children;
735 case lang_wild_statement_enum:
736 l = &parent->wild_statement.children;
738 case lang_group_statement_enum:
739 l = &parent->group_statement.children;
746 /* Empty. do not push. */
747 if (l->tail == &l->head)
750 stack = xmalloc (sizeof (xtensa_ld_iter_stack));
751 memset (stack, 0, sizeof (xtensa_ld_iter_stack));
752 stack->iterloc.parent = parent;
753 stack->iterloc.l = l;
754 stack->iterloc.loc = &l->head;
756 stack->next = *stack_p;
758 if (*stack_p != NULL)
759 (*stack_p)->prev = stack;
766 iter_stack_pop (xtensa_ld_iter_stack **stack_p)
768 xtensa_ld_iter_stack *stack;
774 ASSERT (stack != NULL);
778 if (stack->next != NULL)
779 stack->next->prev = NULL;
781 *stack_p = stack->next;
786 /* This MUST be called if, during iteration, the user changes the
787 underlying structure. It will check for a NULL current and advance
791 iter_stack_update (xtensa_ld_iter_stack **stack_p)
793 if (!iter_stack_empty (stack_p)
794 && (*(*stack_p)->iterloc.loc) == NULL)
796 iter_stack_pop (stack_p);
798 while (!iter_stack_empty (stack_p)
799 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
801 iter_stack_pop (stack_p);
803 if (!iter_stack_empty (stack_p))
804 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
810 iter_stack_next (xtensa_ld_iter_stack **stack_p)
812 xtensa_ld_iter_stack *stack;
813 lang_statement_union_type *current;
816 current = *stack->iterloc.loc;
817 /* If we are on the first element. */
820 switch (current->header.type)
822 case lang_output_section_statement_enum:
823 case lang_wild_statement_enum:
824 case lang_group_statement_enum:
825 /* If the list if not empty, we are done. */
826 if (iter_stack_push (stack_p, *stack->iterloc.loc))
828 /* Otherwise increment the pointer as normal. */
835 while (!iter_stack_empty (stack_p)
836 && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
838 iter_stack_pop (stack_p);
840 if (!iter_stack_empty (stack_p))
841 (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
845 static lang_statement_union_type *
846 iter_stack_current (xtensa_ld_iter_stack **stack_p)
848 return *((*stack_p)->iterloc.loc);
852 /* The iter stack is a preorder. */
855 iter_stack_create (xtensa_ld_iter_stack **stack_p,
856 lang_statement_union_type *parent)
858 iter_stack_push (stack_p, parent);
863 iter_stack_copy_current (xtensa_ld_iter_stack **stack_p,
864 xtensa_ld_iter *front)
866 *front = (*stack_p)->iterloc;
871 xtensa_colocate_literals (reloc_deps_graph *deps,
872 lang_statement_union_type *statement)
874 /* Keep a stack of pointers to control iteration through the contours. */
875 xtensa_ld_iter_stack *stack = NULL;
876 xtensa_ld_iter_stack **stack_p = &stack;
878 xtensa_ld_iter front; /* Location where new insertion should occur. */
879 xtensa_ld_iter *front_p = NULL;
881 xtensa_ld_iter current; /* Location we are checking. */
882 xtensa_ld_iter *current_p = NULL;
883 bfd_boolean in_literals = FALSE;
885 if (deps->count == 0)
889 ld_assign_relative_paged_dot (0x100000, statement, deps,
890 xtensa_use_literal_pages);
892 if (!ld_local_file_relocations_fit (statement, deps))
893 fprintf (stderr, "initial relocation placement does not fit\n");
895 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
898 iter_stack_create (stack_p, statement);
900 while (!iter_stack_empty (stack_p))
902 bfd_boolean skip_increment = FALSE;
903 lang_statement_union_type *l = iter_stack_current (stack_p);
905 switch (l->header.type)
907 case lang_assignment_statement_enum:
908 /* Any assignment statement should block reordering across it. */
913 case lang_input_section_enum:
916 in_literals = (section_is_target (deps, l)
917 && !section_is_source (deps, l));
921 iter_stack_copy_current (stack_p, front_p);
926 bfd_boolean is_target;
927 current_p = ¤t;
928 iter_stack_copy_current (stack_p, current_p);
929 is_target = (section_is_target (deps, l)
930 && !section_is_source (deps, l));
934 iter_stack_copy_current (stack_p, front_p);
942 /* Try to insert in place. */
943 ld_xtensa_move_section_after (front_p, current_p);
944 ld_assign_relative_paged_dot (0x100000,
947 xtensa_use_literal_pages);
949 /* We use this code because it's already written. */
950 if (!ld_local_file_relocations_fit (statement, deps))
953 ld_xtensa_move_section_after (current_p, front_p);
954 /* Reset the literal placement. */
955 iter_stack_copy_current (stack_p, front_p);
959 /* Move front pointer up by one. */
960 front_p->loc = &(*front_p->loc)->header.next;
962 /* Do not increment the current pointer. */
963 skip_increment = TRUE;
974 iter_stack_next (stack_p);
976 /* Be careful to update the stack_p if it now is a null. */
977 iter_stack_update (stack_p);
980 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
985 xtensa_move_dependencies_to_front (reloc_deps_graph *deps,
986 lang_wild_statement_type *w)
988 /* Keep a front pointer and a current pointer. */
989 lang_statement_union_type **front;
990 lang_statement_union_type **current;
992 /* Walk to the end of the targets. */
993 for (front = &w->children.head;
994 (*front != NULL) && section_is_source_or_target (deps, *front);
995 front = &(*front)->header.next)
1001 current = &(*front)->header.next;
1002 while (*current != NULL)
1004 if (section_is_source_or_target (deps, *current))
1006 /* Insert in place. */
1007 xtensa_ld_iter front_iter;
1008 xtensa_ld_iter current_iter;
1010 front_iter.parent = (lang_statement_union_type *) w;
1011 front_iter.l = &w->children;
1012 front_iter.loc = front;
1014 current_iter.parent = (lang_statement_union_type *) w;
1015 current_iter.l = &w->children;
1016 current_iter.loc = current;
1018 ld_xtensa_move_section_after (&front_iter, ¤t_iter);
1019 front = &(*front)->header.next;
1023 current = &(*current)->header.next;
1030 deps_has_sec_edge (const reloc_deps_graph *deps,
1034 const reloc_deps_section *sec_deps;
1035 const reloc_deps_e *sec_deps_e;
1037 sec_deps = xtensa_get_section_deps (deps, src);
1038 if (sec_deps == NULL)
1041 for (sec_deps_e = sec_deps->succs;
1043 sec_deps_e = sec_deps_e->next)
1045 ASSERT (sec_deps_e->src == src);
1046 if (sec_deps_e->tgt == tgt)
1054 deps_has_edge (const reloc_deps_graph *deps,
1055 lang_statement_union_type *src,
1056 lang_statement_union_type *tgt)
1058 if (!section_is_source (deps, src))
1060 if (!section_is_target (deps, tgt))
1063 if (src->header.type != lang_input_section_enum)
1065 if (tgt->header.type != lang_input_section_enum)
1068 return deps_has_sec_edge (deps, src->input_section.section,
1069 tgt->input_section.section);
1074 add_deps_edge (reloc_deps_graph *deps,
1078 reloc_deps_section *src_sec_deps;
1079 reloc_deps_section *tgt_sec_deps;
1081 reloc_deps_e *src_edge;
1082 reloc_deps_e *tgt_edge;
1084 if (deps_has_sec_edge (deps, src_sec, tgt_sec))
1087 src_sec_deps = xtensa_get_section_deps (deps, src_sec);
1088 if (src_sec_deps == NULL)
1090 /* Add a section. */
1091 src_sec_deps = xmalloc (sizeof (reloc_deps_section));
1092 memset (src_sec_deps, 0, sizeof (reloc_deps_section));
1093 src_sec_deps->is_only_literal = 0;
1094 src_sec_deps->preds = NULL;
1095 src_sec_deps->succs = NULL;
1096 xtensa_set_section_deps (deps, src_sec, src_sec_deps);
1097 xtensa_append_section_deps (deps, src_sec);
1100 tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
1101 if (tgt_sec_deps == NULL)
1103 /* Add a section. */
1104 tgt_sec_deps = xmalloc (sizeof (reloc_deps_section));
1105 memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
1106 tgt_sec_deps->is_only_literal = 0;
1107 tgt_sec_deps->preds = NULL;
1108 tgt_sec_deps->succs = NULL;
1109 xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
1110 xtensa_append_section_deps (deps, tgt_sec);
1113 /* Add the edges. */
1114 src_edge = xmalloc (sizeof (reloc_deps_e));
1115 memset (src_edge, 0, sizeof (reloc_deps_e));
1116 src_edge->src = src_sec;
1117 src_edge->tgt = tgt_sec;
1118 src_edge->next = src_sec_deps->succs;
1119 src_sec_deps->succs = src_edge;
1121 tgt_edge = xmalloc (sizeof (reloc_deps_e));
1122 memset (tgt_edge, 0, sizeof (reloc_deps_e));
1123 tgt_edge->src = src_sec;
1124 tgt_edge->tgt = tgt_sec;
1125 tgt_edge->next = tgt_sec_deps->preds;
1126 tgt_sec_deps->preds = tgt_edge;
1131 build_deps_graph_callback (asection *src_sec,
1132 bfd_vma src_offset ATTRIBUTE_UNUSED,
1133 asection *target_sec,
1134 bfd_vma target_offset ATTRIBUTE_UNUSED,
1137 reloc_deps_graph *deps = closure;
1139 /* If the target is defined. */
1140 if (target_sec != NULL)
1141 add_deps_edge (deps, src_sec, target_sec);
1145 static reloc_deps_graph *
1146 ld_build_required_section_dependence (lang_statement_union_type *s)
1148 reloc_deps_graph *deps;
1149 xtensa_ld_iter_stack *stack = NULL;
1151 deps = xmalloc (sizeof (reloc_deps_graph));
1152 deps->sections = NULL;
1156 for (iter_stack_create (&stack, s);
1157 !iter_stack_empty (&stack);
1158 iter_stack_next (&stack))
1160 lang_statement_union_type *l = iter_stack_current (&stack);
1162 if (l->header.type == lang_input_section_enum)
1164 lang_input_section_type *input;
1165 input = &l->input_section;
1166 xtensa_callback_required_dependence (input->ifile->the_bfd,
1169 /* Use the same closure. */
1170 build_deps_graph_callback,
1178 #if EXTRA_VALIDATION
1180 ld_count_children (lang_statement_union_type *s)
1183 xtensa_ld_iter_stack *stack = NULL;
1184 for (iter_stack_create (&stack, s);
1185 !iter_stack_empty (&stack);
1186 iter_stack_next (&stack))
1188 lang_statement_union_type *l = iter_stack_current (&stack);
1194 #endif /* EXTRA_VALIDATION */
1198 xtensa_wild_group_interleave_callback (lang_statement_union_type *statement)
1200 lang_wild_statement_type *w;
1201 reloc_deps_graph *deps;
1202 if (statement->header.type == lang_wild_statement_enum)
1204 #if EXTRA_VALIDATION
1205 size_t old_child_count;
1206 size_t new_child_count;
1208 bfd_boolean no_reorder;
1210 w = &statement->wild_statement;
1214 /* If it has 0 or 1 section bound, then do not reorder. */
1215 if (w->children.head == NULL
1216 || (w->children.head->header.type == lang_input_section_enum
1217 && w->children.head->header.next == NULL))
1220 if (w->filenames_sorted)
1223 /* Check for sorting in a section list wildcard spec as well. */
1226 struct wildcard_list *l;
1227 for (l = w->section_list; l != NULL; l = l->next)
1229 if (l->spec.sorted == TRUE)
1237 /* Special case until the NOREORDER linker directive is supported:
1238 *(.init) output sections and *(.fini) specs may NOT be reordered. */
1240 /* Check for sorting in a section list wildcard spec as well. */
1243 struct wildcard_list *l;
1244 for (l = w->section_list; l != NULL; l = l->next)
1247 && ((strcmp (".init", l->spec.name) == 0)
1248 || (strcmp (".fini", l->spec.name) == 0)))
1256 #if EXTRA_VALIDATION
1257 old_child_count = ld_count_children (statement);
1260 /* It is now officially a target. Build the graph of source
1261 section -> target section (kept as a list of edges). */
1262 deps = ld_build_required_section_dependence (statement);
1264 /* If this wildcard does not reorder.... */
1265 if (!no_reorder && deps->count != 0)
1267 /* First check for reverse dependences. Fix if possible. */
1268 xtensa_layout_wild (deps, w);
1270 xtensa_move_dependencies_to_front (deps, w);
1271 #if EXTRA_VALIDATION
1272 new_child_count = ld_count_children (statement);
1273 ASSERT (new_child_count == old_child_count);
1276 xtensa_colocate_literals (deps, statement);
1278 #if EXTRA_VALIDATION
1279 new_child_count = ld_count_children (statement);
1280 ASSERT (new_child_count == old_child_count);
1285 free_reloc_deps_graph (deps);
1291 xtensa_wild_group_interleave (lang_statement_union_type *s)
1293 lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
1298 xtensa_layout_wild (const reloc_deps_graph *deps,
1299 lang_wild_statement_type *w)
1301 /* If it does not fit initially, we need to do this step. Move all
1302 of the wild literal sections to a new list, then move each of
1303 them back in just before the first section they depend on. */
1304 lang_statement_union_type **s_p;
1305 #if EXTRA_VALIDATION
1306 size_t old_count, new_count;
1310 lang_wild_statement_type literal_wild;
1311 literal_wild.header.next = NULL;
1312 literal_wild.header.type = lang_wild_statement_enum;
1313 literal_wild.filename = NULL;
1314 literal_wild.filenames_sorted = FALSE;
1315 literal_wild.section_list = NULL;
1316 literal_wild.keep_sections = FALSE;
1317 literal_wild.children.head = NULL;
1318 literal_wild.children.tail = &literal_wild.children.head;
1320 #if EXTRA_VALIDATION
1321 old_count = ld_count_children ((lang_statement_union_type*) w);
1324 s_p = &w->children.head;
1325 while (*s_p != NULL)
1327 lang_statement_union_type *l = *s_p;
1328 if (l->header.type == lang_input_section_enum)
1330 if (section_is_target (deps, l)
1331 && ! section_is_source (deps, l))
1334 *s_p = l->header.next;
1336 w->children.tail = s_p;
1337 l->header.next = NULL;
1340 *literal_wild.children.tail = l;
1341 literal_wild.children.tail = &l->header.next;
1345 s_p = &(*s_p)->header.next;
1348 #if EXTRA_VALIDATION
1349 ct1 = ld_count_children ((lang_statement_union_type*) w);
1350 ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
1352 ASSERT (old_count == (ct1 + ct2));
1355 /* Now place them back in front of their dependent sections. */
1357 while (literal_wild.children.head != NULL)
1359 lang_statement_union_type *lit = literal_wild.children.head;
1360 bfd_boolean placed = FALSE;
1362 #if EXTRA_VALIDATION
1368 literal_wild.children.head = lit->header.next;
1369 if (literal_wild.children.head == NULL)
1370 literal_wild.children.tail = &literal_wild.children.head;
1371 lit->header.next = NULL;
1373 /* Find a spot to place it. */
1374 for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
1376 lang_statement_union_type *src = *s_p;
1377 if (deps_has_edge (deps, src, lit))
1379 /* Place it here. */
1380 lit->header.next = *s_p;
1389 /* Put it at the end. */
1390 *w->children.tail = lit;
1391 w->children.tail = &lit->header.next;
1395 #if EXTRA_VALIDATION
1396 new_count = ld_count_children ((lang_statement_union_type*) w);
1397 ASSERT (new_count == old_count);
1403 xtensa_colocate_output_literals_callback (lang_statement_union_type *statement)
1405 lang_output_section_statement_type *os;
1406 reloc_deps_graph *deps;
1407 if (statement->header.type == lang_output_section_statement_enum)
1409 /* Now, we walk over the contours of the output section statement.
1411 First we build the literal section dependences as before.
1413 At the first uniquely_literal section, we mark it as a good
1414 spot to place other literals. Continue walking (and counting
1415 sizes) until we find the next literal section. If this
1416 section can be moved to the first one, then we move it. If
1417 we every find a modification of ".", start over. If we find
1418 a labeling of the current location, start over. Finally, at
1419 the end, if we require page alignment, add page alignments. */
1421 #if EXTRA_VALIDATION
1422 size_t old_child_count;
1423 size_t new_child_count;
1425 bfd_boolean no_reorder = FALSE;
1427 os = &statement->output_section_statement;
1429 #if EXTRA_VALIDATION
1430 old_child_count = ld_count_children (statement);
1433 /* It is now officially a target. Build the graph of source
1434 section -> target section (kept as a list of edges). */
1436 deps = ld_build_required_section_dependence (statement);
1438 /* If this wildcard does not reorder.... */
1441 /* First check for reverse dependences. Fix if possible. */
1442 xtensa_colocate_literals (deps, statement);
1444 #if EXTRA_VALIDATION
1445 new_child_count = ld_count_children (statement);
1446 ASSERT (new_child_count == old_child_count);
1450 /* Insert align/offset assignment statement. */
1451 if (xtensa_use_literal_pages)
1453 ld_xtensa_insert_page_offsets (0, statement, deps,
1454 xtensa_use_literal_pages);
1455 lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
1460 free_reloc_deps_graph (deps);
1466 xtensa_colocate_output_literals (lang_statement_union_type *s)
1468 lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
1473 xtensa_ldlang_clear_addresses (lang_statement_union_type *statement)
1475 switch (statement->header.type)
1477 case lang_input_section_enum:
1479 asection *bfd_section = statement->input_section.section;
1480 bfd_section->output_offset = 0;
1490 ld_assign_relative_paged_dot (bfd_vma dot,
1491 lang_statement_union_type *s,
1492 const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
1493 bfd_boolean lit_align)
1495 /* Walk through all of the input statements in this wild statement
1496 assign dot to all of them. */
1498 xtensa_ld_iter_stack *stack = NULL;
1499 xtensa_ld_iter_stack **stack_p = &stack;
1501 bfd_boolean first_section = FALSE;
1502 bfd_boolean in_literals = FALSE;
1504 for (iter_stack_create (stack_p, s);
1505 !iter_stack_empty (stack_p);
1506 iter_stack_next (stack_p))
1508 lang_statement_union_type *l = iter_stack_current (stack_p);
1510 switch (l->header.type)
1512 case lang_input_section_enum:
1514 asection *section = l->input_section.section;
1515 size_t align_pow = section->alignment_power;
1516 bfd_boolean do_xtensa_alignment = FALSE;
1520 bfd_boolean sec_is_target = section_is_target (deps, l);
1521 bfd_boolean sec_is_source = section_is_source (deps, l);
1523 if (section->size != 0
1525 || (in_literals && !sec_is_target)
1526 || (!in_literals && sec_is_target)))
1528 do_xtensa_alignment = TRUE;
1530 first_section = FALSE;
1531 if (section->size != 0)
1532 in_literals = (sec_is_target && !sec_is_source);
1535 if (do_xtensa_alignment && xtensa_page_power != 0)
1536 dot += (1 << xtensa_page_power);
1538 dot = align_power (dot, align_pow);
1539 section->output_offset = dot;
1540 dot += section->size;
1543 case lang_fill_statement_enum:
1544 dot += l->fill_statement.size;
1546 case lang_padding_statement_enum:
1547 dot += l->padding_statement.size;
1558 ld_local_file_relocations_fit (lang_statement_union_type *statement,
1559 const reloc_deps_graph *deps ATTRIBUTE_UNUSED)
1561 /* Walk over all of the dependencies that we identified and make
1562 sure that IF the source and target are here (addr != 0):
1563 1) target addr < source addr
1564 2) (roundup(source + source_size, 4) - rounddown(target, 4))
1565 < (256K - (1 << bad align))
1566 Need a worst-case proof.... */
1568 xtensa_ld_iter_stack *stack = NULL;
1569 xtensa_ld_iter_stack **stack_p = &stack;
1570 size_t max_align_power = 0;
1571 size_t align_penalty = 256;
1575 /* Find the worst-case alignment requirement for this set of statements. */
1576 for (iter_stack_create (stack_p, statement);
1577 !iter_stack_empty (stack_p);
1578 iter_stack_next (stack_p))
1580 lang_statement_union_type *l = iter_stack_current (stack_p);
1581 if (l->header.type == lang_input_section_enum)
1583 lang_input_section_type *input = &l->input_section;
1584 asection *section = input->section;
1585 if (section->alignment_power > max_align_power)
1586 max_align_power = section->alignment_power;
1590 /* Now check that everything fits. */
1591 for (i = 0; i < deps->count; i++)
1593 asection *sec = deps->sections[i];
1594 const reloc_deps_section *deps_section =
1595 xtensa_get_section_deps (deps, sec);
1598 /* We choose to walk through the successors. */
1599 for (e = deps_section->succs; e != NULL; e = e->next)
1601 if (e->src != e->tgt
1602 && e->src->output_section == e->tgt->output_section
1603 && e->src->output_offset != 0
1604 && e->tgt->output_offset != 0)
1607 align_power (e->src->output_offset + e->src->size, 2);
1608 bfd_vma target_addr = e->tgt->output_offset & ~3;
1609 if (l32r_addr < target_addr)
1611 fprintf (stderr, "Warning: "
1612 "l32r target section before l32r\n");
1616 if (l32r_addr - target_addr > 256 * 1024 - align_penalty)
1628 ld_xtensa_insert_page_offsets (bfd_vma dot,
1629 lang_statement_union_type *s,
1630 reloc_deps_graph *deps,
1631 bfd_boolean lit_align)
1633 xtensa_ld_iter_stack *stack = NULL;
1634 xtensa_ld_iter_stack **stack_p = &stack;
1636 bfd_boolean first_section = FALSE;
1637 bfd_boolean in_literals = FALSE;
1642 for (iter_stack_create (stack_p, s);
1643 !iter_stack_empty (stack_p);
1644 iter_stack_next (stack_p))
1646 lang_statement_union_type *l = iter_stack_current (stack_p);
1648 switch (l->header.type)
1650 case lang_input_section_enum:
1652 asection *section = l->input_section.section;
1653 bfd_boolean do_xtensa_alignment = FALSE;
1657 if (section->size != 0
1659 || (in_literals && !section_is_target (deps, l))
1660 || (!in_literals && section_is_target (deps, l))))
1662 do_xtensa_alignment = TRUE;
1664 first_section = FALSE;
1665 if (section->size != 0)
1667 in_literals = (section_is_target (deps, l)
1668 && !section_is_source (deps, l));
1672 if (do_xtensa_alignment && xtensa_page_power != 0)
1674 /* Create an expression that increments the current address,
1675 i.e., "dot", by (1 << xtensa_align_power). */
1676 etree_type *name_op = exp_nameop (NAME, ".");
1677 etree_type *addend_op = exp_intop (1 << xtensa_page_power);
1678 etree_type *add_op = exp_binop ('+', name_op, addend_op);
1679 etree_type *assign_op = exp_assop ('=', ".", add_op);
1681 lang_assignment_statement_type *assign_stmt;
1682 lang_statement_union_type *assign_union;
1683 lang_statement_list_type tmplist;
1684 lang_statement_list_type *old_stat_ptr = stat_ptr;
1686 /* There is hidden state in "lang_add_assignment". It
1687 appends the new assignment statement to the stat_ptr
1688 list. Thus, we swap it before and after the call. */
1690 tmplist.head = NULL;
1691 tmplist.tail = &tmplist.head;
1693 stat_ptr = &tmplist;
1694 /* Warning: side effect; statement appended to stat_ptr. */
1695 assign_stmt = lang_add_assignment (assign_op);
1696 assign_union = (lang_statement_union_type *) assign_stmt;
1697 stat_ptr = old_stat_ptr;
1699 assign_union->header.next = l;
1700 *(*stack_p)->iterloc.loc = assign_union;
1701 iter_stack_next (stack_p);
1714 # Define some shell vars to insert bits of code into the standard ELF
1715 # parse_args and list_options functions.
1717 PARSE_AND_LIST_PROLOGUE='
1718 #define OPTION_OPT_SIZEOPT (300)
1719 #define OPTION_NO_RELAX (OPTION_OPT_SIZEOPT + 1)
1720 #define OPTION_LITERAL_MOVEMENT (OPTION_NO_RELAX + 1)
1721 #define OPTION_NO_LITERAL_MOVEMENT (OPTION_LITERAL_MOVEMENT + 1)
1722 extern int elf32xtensa_size_opt;
1723 extern int elf32xtensa_no_literal_movement;
1726 PARSE_AND_LIST_LONGOPTS='
1727 { "size-opt", no_argument, NULL, OPTION_OPT_SIZEOPT},
1728 { "no-relax", no_argument, NULL, OPTION_NO_RELAX},
1729 { "literal-movement", no_argument, NULL, OPTION_LITERAL_MOVEMENT},
1730 { "no-literal-movement", no_argument, NULL, OPTION_NO_LITERAL_MOVEMENT},
1733 PARSE_AND_LIST_OPTIONS='
1734 fprintf (file, _(" --size-opt\t\tWhen relaxing longcalls, prefer size optimization\n\t\t\t over branch target alignment\n"));
1735 fprintf (file, _(" --no-relax\t\tDo not relax branches or coalesce literals\n"));
1738 PARSE_AND_LIST_ARGS_CASES='
1739 case OPTION_OPT_SIZEOPT:
1740 elf32xtensa_size_opt = 1;
1742 case OPTION_NO_RELAX:
1743 disable_relaxation = TRUE;
1745 case OPTION_LITERAL_MOVEMENT:
1746 elf32xtensa_no_literal_movement = 0;
1748 case OPTION_NO_LITERAL_MOVEMENT:
1749 elf32xtensa_no_literal_movement = 1;
1753 # Replace some of the standard ELF functions with our own versions.
1755 LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
1756 LDEMUL_AFTER_OPEN=elf_xtensa_after_open
1757 LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
1758 LDEMUL_PLACE_ORPHAN=elf_xtensa_place_orphan
1759 LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation