1# $MirOS: src/gnu/usr.bin/binutils/ld/emultempl/sh64elf.em,v 1.3 2005/06/05 21:24:43 tg Exp $ 2# 3# This shell script emits a C file. -*- C -*- 4# Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. 5# 6# This file is part of GLD, the Gnu Linker. 7# 8# This program is free software; you can redistribute it and/or modify 9# it under the terms of the GNU General Public License as published by 10# the Free Software Foundation; either version 2 of the License, or 11# (at your option) any later version. 12# 13# This program is distributed in the hope that it will be useful, 14# but WITHOUT ANY WARRANTY; without even the implied warranty of 15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16# GNU General Public License for more details. 17# 18# You should have received a copy of the GNU General Public License 19# along with this program; if not, write to the Free Software 20# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 21# 22 23# This file is sourced from elf32.em, and defines extra sh64 24# specific routines. 25# 26 27LDEMUL_AFTER_ALLOCATION=sh64_elf_${EMULATION_NAME}_after_allocation 28LDEMUL_BEFORE_ALLOCATION=sh64_elf_${EMULATION_NAME}_before_allocation 29 30cat >>e${EMULATION_NAME}.c <<EOF 31 32#include "libiberty.h" 33#include "libbfd.h" 34#include "elf-bfd.h" 35#include "elf/sh.h" 36#include "elf32-sh64.h" 37 38/* Check if we need a .cranges section and create it if it's not in any 39 input file. It might seem better to always create it and if unneeded, 40 discard it, but I don't find a simple way to discard it totally from 41 the output. 42 43 Putting it here instead of as a elf_backend_always_size_sections hook 44 in elf32-sh64.c, means that we have access to linker command line 45 options here, and we can access input sections in the order in which 46 they will be linked. */ 47 48static void 49sh64_elf_${EMULATION_NAME}_before_allocation (void) 50{ 51 asection *cranges; 52 asection *osec; 53 54 /* Call main function; we're just extending it. */ 55 gld${EMULATION_NAME}_before_allocation (); 56 57 cranges = bfd_get_section_by_name (output_bfd, SH64_CRANGES_SECTION_NAME); 58 59 if (cranges != NULL) 60 { 61 if (command_line.relax) 62 { 63 /* FIXME: Look through incoming sections with .cranges 64 descriptors, build up some kind of descriptors that the 65 relaxing function will pick up and adjust, or perhaps make it 66 find and adjust an associated .cranges descriptor. We could 67 also look through incoming relocs and kill the ones marking 68 relaxation areas, but that wouldn't be TRT. */ 69 einfo 70 (_("%P: Sorry, turning off relaxing: .cranges section in input.\n")); 71 einfo (_(" A .cranges section is present in:\n")); 72 73 { 74 LANG_FOR_EACH_INPUT_STATEMENT (f) 75 { 76 asection *input_cranges 77 = bfd_get_section_by_name (f->the_bfd, 78 SH64_CRANGES_SECTION_NAME); 79 if (input_cranges != NULL) 80 einfo (" %I\n", f); 81 } 82 } 83 84 command_line.relax = FALSE; 85 } 86 87 /* We wouldn't need to do anything when there's already a .cranges 88 section (and have a return here), except that we need to set the 89 section flags right for output sections that *don't* need a 90 .cranges section. */ 91 } 92 93 if (command_line.relax) 94 { 95 LANG_FOR_EACH_INPUT_STATEMENT (f) 96 { 97 if (bfd_get_flavour (f->the_bfd) == bfd_target_elf_flavour) 98 { 99 asection *isec; 100 for (isec = f->the_bfd->sections; 101 isec != NULL; 102 isec = isec->next) 103 { 104 if (elf_section_data (isec)->this_hdr.sh_flags 105 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)) 106 { 107 einfo (_("%P: Sorry, turning off relaxing: SHmedia sections present.\n")); 108 einfo (" %I\n", f); 109 command_line.relax = FALSE; 110 goto done_scanning_shmedia_sections; 111 } 112 } 113 } 114 } 115 } 116 done_scanning_shmedia_sections: 117 118 /* For each non-empty input section in each output section, check if it 119 has the same SH64-specific flags. If some input section differs, we 120 need a .cranges section. */ 121 for (osec = output_bfd->sections; 122 osec != NULL; 123 osec = osec->next) 124 { 125 struct sh64_section_data *sh64_sec_data; 126 bfd_vma oflags_isa = 0; 127 bfd_vma iflags_isa = 0; 128 129 if (bfd_get_flavour (output_bfd) != bfd_target_elf_flavour) 130 einfo (_("%FError: non-ELF output formats are not supported by this target's linker.\n")); 131 132 sh64_sec_data = sh64_elf_section_data (osec)->sh64_info; 133 134 /* Omit excluded or garbage-collected sections. */ 135 if (bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE) 136 continue; 137 138 /* Make sure we have the target section data initialized. */ 139 if (sh64_sec_data == NULL) 140 { 141 sh64_sec_data = xcalloc (1, sizeof (struct sh64_section_data)); 142 sh64_elf_section_data (osec)->sh64_info = sh64_sec_data; 143 } 144 145 /* First find an input section so we have flags to compare with; the 146 flags in the output section are not valid. */ 147 { 148 LANG_FOR_EACH_INPUT_STATEMENT (f) 149 { 150 asection *isec; 151 152 for (isec = f->the_bfd->sections; 153 isec != NULL; 154 isec = isec->next) 155 { 156 if (isec->output_section == osec 157 && isec->size != 0 158 && (bfd_get_section_flags (isec->owner, isec) 159 & SEC_EXCLUDE) == 0) 160 { 161 oflags_isa 162 = (elf_section_data (isec)->this_hdr.sh_flags 163 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)); 164 goto break_1; 165 } 166 } 167 } 168 } 169 170 break_1: 171 172 /* Check that all input sections have the same contents-type flags 173 as the first input section. */ 174 { 175 LANG_FOR_EACH_INPUT_STATEMENT (f) 176 { 177 asection *isec; 178 179 for (isec = f->the_bfd->sections; 180 isec != NULL; 181 isec = isec->next) 182 { 183 if (isec->output_section == osec 184 && isec->size != 0 185 && (bfd_get_section_flags (isec->owner, isec) 186 & SEC_EXCLUDE) == 0) 187 { 188 iflags_isa 189 = (elf_section_data (isec)->this_hdr.sh_flags 190 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)); 191 192 /* If flags don't agree, we need a .cranges section. 193 Create it here if it did not exist through input 194 sections. */ 195 if (iflags_isa != oflags_isa) 196 { 197 if (cranges == NULL) 198 { 199 /* This section will be *appended* to 200 sections, so the outer iteration will reach 201 it in due time and set 202 sh64_elf_section_data; no need to set it 203 specifically here. */ 204 cranges 205 = bfd_make_section (output_bfd, 206 SH64_CRANGES_SECTION_NAME); 207 if (cranges == NULL 208 || !bfd_set_section_flags (output_bfd, 209 cranges, 210 SEC_LINKER_CREATED 211 | SEC_KEEP 212 | SEC_HAS_CONTENTS 213 | SEC_DEBUGGING)) 214 einfo 215 (_("%P%E%F: Can't make .cranges section\n")); 216 } 217 218 /* We don't need to look at more input sections, 219 and we know this section will have mixed 220 contents. */ 221 goto break_2; 222 } 223 } 224 } 225 } 226 } 227 228 /* If we got here, then all input sections in this output section 229 have the same contents flag. Put that where we expect to see 230 contents flags. We don't need to do this for sections that will 231 need additional, linker-generated .cranges entries. */ 232 sh64_sec_data->contents_flags = iflags_isa; 233 234 break_2: 235 ; 236 } 237} 238 239/* Size up and extend the .cranges section, merging generated entries. */ 240 241static void 242sh64_elf_${EMULATION_NAME}_after_allocation (void) 243{ 244 bfd_vma new_cranges = 0; 245 bfd_vma cranges_growth = 0; 246 asection *osec; 247 bfd_byte *crangesp; 248 249 asection *cranges 250 = bfd_get_section_by_name (output_bfd, SH64_CRANGES_SECTION_NAME); 251 252 /* If this ever starts doing something, we will pick it up. */ 253 after_allocation_default (); 254 255 /* If there is no .cranges section, it is because it was seen earlier on 256 that none was needed. Otherwise it must have been created then, or 257 be present in input. */ 258 if (cranges == NULL) 259 return; 260 261 /* First, we set the ISA flags for each output section according to the 262 first non-discarded section. For each input section in osec, we 263 check if it has the same flags. If it does not, we set flags to mark 264 a mixed section (and exit the loop early). */ 265 for (osec = output_bfd->sections; 266 osec != NULL; 267 osec = osec->next) 268 { 269 bfd_vma oflags_isa = 0; 270 bfd_boolean need_check_cranges = FALSE; 271 272 /* Omit excluded or garbage-collected sections. */ 273 if (bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE) 274 continue; 275 276 /* First find an input section so we have flags to compare with; the 277 flags in the output section are not valid. */ 278 { 279 LANG_FOR_EACH_INPUT_STATEMENT (f) 280 { 281 asection *isec; 282 283 for (isec = f->the_bfd->sections; 284 isec != NULL; 285 isec = isec->next) 286 { 287 if (isec->output_section == osec 288 && isec->size != 0 289 && (bfd_get_section_flags (isec->owner, isec) 290 & SEC_EXCLUDE) == 0) 291 { 292 oflags_isa 293 = (elf_section_data (isec)->this_hdr.sh_flags 294 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)); 295 goto break_1; 296 } 297 } 298 } 299 } 300 301 break_1: 302 303 /* Check that all input sections have the same contents-type flags 304 as the first input section. */ 305 { 306 LANG_FOR_EACH_INPUT_STATEMENT (f) 307 { 308 asection *isec; 309 310 for (isec = f->the_bfd->sections; 311 isec != NULL; 312 isec = isec->next) 313 { 314 if (isec->output_section == osec 315 && isec->size != 0 316 && (bfd_get_section_flags (isec->owner, isec) 317 & SEC_EXCLUDE) == 0) 318 { 319 bfd_vma iflags_isa 320 = (elf_section_data (isec)->this_hdr.sh_flags 321 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)); 322 323 /* If flags don't agree, set the target-specific data 324 of the section to mark that this section needs to 325 be have .cranges section entries added. Don't 326 bother setting ELF section flags in output section; 327 they will be cleared later and will have to be 328 re-initialized before the linked file is written. */ 329 if (iflags_isa != oflags_isa) 330 { 331 oflags_isa = SHF_SH5_ISA32_MIXED; 332 333 BFD_ASSERT (sh64_elf_section_data (osec)->sh64_info); 334 335 sh64_elf_section_data (osec)->sh64_info->contents_flags 336 = SHF_SH5_ISA32_MIXED; 337 need_check_cranges = TRUE; 338 goto break_2; 339 } 340 } 341 } 342 } 343 } 344 345 break_2: 346 347 /* If there were no new ranges for this output section, we don't 348 need to iterate over the input sections to check how many are 349 needed. */ 350 if (! need_check_cranges) 351 continue; 352 353 /* If we found a section with differing contents type, we need more 354 ranges to mark the sections that are not mixed (and already have 355 .cranges descriptors). Calculate the maximum number of new 356 entries here. We may merge some of them, so that number is not 357 final; it can shrink. */ 358 { 359 LANG_FOR_EACH_INPUT_STATEMENT (f) 360 { 361 asection *isec; 362 363 for (isec = f->the_bfd->sections; 364 isec != NULL; 365 isec = isec->next) 366 { 367 if (isec->output_section == osec 368 && isec->size != 0 369 && (bfd_get_section_flags (isec->owner, isec) 370 & SEC_EXCLUDE) == 0 371 && ((elf_section_data (isec)->this_hdr.sh_flags 372 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)) 373 != SHF_SH5_ISA32_MIXED)) 374 new_cranges++; 375 } 376 } 377 } 378 } 379 380 /* ldemul_after_allocation may be called twice. First directly from 381 lang_process, and the second time when lang_process calls ldemul_finish, 382 which calls gld${EMULATION_NAME}_finish, e.g. gldshelf32_finish, which 383 is defined in emultempl/elf32.em and calls ldemul_after_allocation, 384 if bfd_elf_discard_info returned true. */ 385 if (cranges->contents != NULL) 386 free (cranges->contents); 387 388 BFD_ASSERT (sh64_elf_section_data (cranges)->sh64_info != NULL); 389 390 /* Make sure we have .cranges in memory even if there were only 391 assembler-generated .cranges. */ 392 cranges_growth = new_cranges * SH64_CRANGE_SIZE; 393 cranges->contents = xcalloc (cranges->size + cranges_growth, 1); 394 bfd_set_section_flags (cranges->owner, cranges, 395 bfd_get_section_flags (cranges->owner, cranges) 396 | SEC_IN_MEMORY); 397 398 /* If we don't need to grow the .cranges section beyond what was in the 399 input sections, we have nothing more to do here. We then only got 400 here because there was a .cranges section coming from input. Zero 401 out the number of generated .cranges. */ 402 if (new_cranges == 0) 403 { 404 sh64_elf_section_data (cranges)->sh64_info->cranges_growth = 0; 405 return; 406 } 407 408 crangesp = cranges->contents + cranges->size; 409 410 /* Now pass over the sections again, and make reloc orders for the new 411 .cranges entries. Constants are set as we go. */ 412 for (osec = output_bfd->sections; 413 osec != NULL; 414 osec = osec->next) 415 { 416 struct bfd_link_order *cr_addr_order = NULL; 417 enum sh64_elf_cr_type last_cr_type = CRT_NONE; 418 bfd_vma last_cr_size = 0; 419 bfd_vma continuation_vma = 0; 420 421 /* Omit excluded or garbage-collected sections, and output sections 422 which were not marked as needing further processing. */ 423 if ((bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE) != 0 424 || (sh64_elf_section_data (osec)->sh64_info->contents_flags 425 != SHF_SH5_ISA32_MIXED)) 426 continue; 427 428 { 429 LANG_FOR_EACH_INPUT_STATEMENT (f) 430 { 431 asection *isec; 432 433 for (isec = f->the_bfd->sections; 434 isec != NULL; 435 isec = isec->next) 436 { 437 /* Allow only sections that have (at least initially) a 438 non-zero size, and are not excluded, and are not marked 439 as containing mixed data, thus already having .cranges 440 entries. */ 441 if (isec->output_section == osec 442 && isec->size != 0 443 && (bfd_get_section_flags (isec->owner, isec) 444 & SEC_EXCLUDE) == 0 445 && ((elf_section_data (isec)->this_hdr.sh_flags 446 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)) 447 != SHF_SH5_ISA32_MIXED)) 448 { 449 enum sh64_elf_cr_type cr_type; 450 bfd_vma cr_size; 451 bfd_vma isa_flags 452 = (elf_section_data (isec)->this_hdr.sh_flags 453 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED)); 454 455 if (isa_flags == SHF_SH5_ISA32) 456 cr_type = CRT_SH5_ISA32; 457 else if ((bfd_get_section_flags (isec->owner, isec) 458 & SEC_CODE) == 0) 459 cr_type = CRT_DATA; 460 else 461 cr_type = CRT_SH5_ISA16; 462 463 cr_size = isec->size; 464 465 /* Sections can be empty, like .text in a file that 466 only contains other sections. Ranges shouldn't be 467 emitted for them. This can presumably happen after 468 relaxing and is not be caught at the "raw size" 469 test above. */ 470 if (cr_size == 0) 471 continue; 472 473 /* See if this is a continuation of the previous range 474 for the same output section. If so, just change 475 the size of the last range and continue. */ 476 if (cr_type == last_cr_type 477 && (continuation_vma 478 == osec->vma + isec->output_offset)) 479 { 480 last_cr_size += cr_size; 481 bfd_put_32 (output_bfd, last_cr_size, 482 crangesp - SH64_CRANGE_SIZE 483 + SH64_CRANGE_CR_SIZE_OFFSET); 484 485 continuation_vma += cr_size; 486 continue; 487 } 488 489 /* If we emit relocatable contents, we need a 490 relocation for the start address. */ 491 if (link_info.relocatable || link_info.emitrelocations) 492 { 493 /* FIXME: We could perhaps use lang_add_reloc and 494 friends here, but I'm not really sure that 495 would leave us free to do some optimizations 496 later. */ 497 cr_addr_order 498 = bfd_new_link_order (output_bfd, cranges); 499 500 if (cr_addr_order == NULL) 501 { 502 einfo (_("%P%F: bfd_new_link_order failed\n")); 503 return; 504 } 505 506 cr_addr_order->type = bfd_section_reloc_link_order; 507 cr_addr_order->offset 508 = (cranges->output_offset 509 + crangesp + SH64_CRANGE_CR_ADDR_OFFSET 510 - cranges->contents); 511 cr_addr_order->size = 4; 512 cr_addr_order->u.reloc.p 513 = xmalloc (sizeof (struct bfd_link_order_reloc)); 514 515 cr_addr_order->u.reloc.p->reloc = BFD_RELOC_32; 516 cr_addr_order->u.reloc.p->u.section = osec; 517 518 /* Since SH, unlike normal RELA-targets, uses a 519 "partial inplace" REL-like relocation for this, 520 we put the addend in the contents and specify 0 521 for the reloc. */ 522 bfd_put_32 (output_bfd, isec->output_offset, 523 crangesp + SH64_CRANGE_CR_ADDR_OFFSET); 524 cr_addr_order->u.reloc.p->addend = 0; 525 526 /* We must update the number of relocations here, 527 since the elf linker does not take link orders 528 into account when setting header sizes. The 529 actual relocation orders are however executed 530 correctly. */ 531 elf_section_data(cranges)->rel_count++; 532 } 533 else 534 bfd_put_32 (output_bfd, 535 osec->vma + isec->output_offset, 536 crangesp + SH64_CRANGE_CR_ADDR_OFFSET); 537 538 /* If we could make a reloc for cr_size we would do 539 it, but we would have to have a symbol for the size 540 of the _input_ section and there's no way to 541 generate that. */ 542 bfd_put_32 (output_bfd, cr_size, 543 crangesp + SH64_CRANGE_CR_SIZE_OFFSET); 544 545 bfd_put_16 (output_bfd, cr_type, 546 crangesp + SH64_CRANGE_CR_TYPE_OFFSET); 547 548 last_cr_type = cr_type; 549 last_cr_size = cr_size; 550 continuation_vma 551 = osec->vma + isec->output_offset + cr_size; 552 crangesp += SH64_CRANGE_SIZE; 553 } 554 } 555 } 556 } 557 } 558 559 /* The .cranges section will have this size, no larger or smaller. 560 Since relocs (if relocatable linking) will be emitted into the 561 "extended" size, we must set the raw size to the total. We have to 562 keep track of the number of new .cranges entries. 563 564 Sorting before writing is done by sh64_elf_final_write_processing. */ 565 566 sh64_elf_section_data (cranges)->sh64_info->cranges_growth 567 = crangesp - cranges->contents - cranges->size; 568 cranges->size = crangesp - cranges->contents; 569} 570EOF 571