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