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