1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2000, 2001, 2002, 2003, 2004, 2007, 2008
3 # Free Software Foundation, Inc.
5 # This file is part of the GNU Binutils.
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 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 # This file is sourced from elf32.em, and defines extra sh64
27 LDEMUL_AFTER_ALLOCATION=sh64_elf_${EMULATION_NAME}_after_allocation
28 LDEMUL_BEFORE_ALLOCATION=sh64_elf_${EMULATION_NAME}_before_allocation
32 #include "libiberty.h"
36 #include "elf32-sh64.h"
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
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. */
49 sh64_elf_${EMULATION_NAME}_before_allocation (void)
54 /* Call main function; we're just extending it. */
55 gld${EMULATION_NAME}_before_allocation ();
57 cranges = bfd_get_section_by_name (link_info.output_bfd,
58 SH64_CRANGES_SECTION_NAME);
62 if (command_line.relax)
64 /* FIXME: Look through incoming sections with .cranges
65 descriptors, build up some kind of descriptors that the
66 relaxing function will pick up and adjust, or perhaps make it
67 find and adjust an associated .cranges descriptor. We could
68 also look through incoming relocs and kill the ones marking
69 relaxation areas, but that wouldn't be TRT. */
71 (_("%P: Sorry, turning off relaxing: .cranges section in input.\n"));
72 einfo (_(" A .cranges section is present in:\n"));
75 LANG_FOR_EACH_INPUT_STATEMENT (f)
77 asection *input_cranges
78 = bfd_get_section_by_name (f->the_bfd,
79 SH64_CRANGES_SECTION_NAME);
80 if (input_cranges != NULL)
85 command_line.relax = FALSE;
88 /* We wouldn't need to do anything when there's already a .cranges
89 section (and have a return here), except that we need to set the
90 section flags right for output sections that *don't* need a
94 if (command_line.relax)
96 LANG_FOR_EACH_INPUT_STATEMENT (f)
98 if (bfd_get_flavour (f->the_bfd) == bfd_target_elf_flavour)
101 for (isec = f->the_bfd->sections;
105 if (elf_section_data (isec)->this_hdr.sh_flags
106 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
108 einfo (_("%P: Sorry, turning off relaxing: SHmedia sections present.\n"));
110 command_line.relax = FALSE;
111 goto done_scanning_shmedia_sections;
117 done_scanning_shmedia_sections:
119 /* For each non-empty input section in each output section, check if it
120 has the same SH64-specific flags. If some input section differs, we
121 need a .cranges section. */
122 for (osec = link_info.output_bfd->sections;
126 struct sh64_section_data *sh64_sec_data;
127 bfd_vma oflags_isa = 0;
128 bfd_vma iflags_isa = 0;
130 if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
131 einfo (_("%FError: non-ELF output formats are not supported by this target's linker.\n"));
133 sh64_sec_data = sh64_elf_section_data (osec)->sh64_info;
135 /* Omit excluded or garbage-collected sections. */
136 if (bfd_get_section_flags (link_info.output_bfd, osec) & SEC_EXCLUDE)
139 /* Make sure we have the target section data initialized. */
140 if (sh64_sec_data == NULL)
142 sh64_sec_data = xcalloc (1, sizeof (struct sh64_section_data));
143 sh64_elf_section_data (osec)->sh64_info = sh64_sec_data;
146 /* First find an input section so we have flags to compare with; the
147 flags in the output section are not valid. */
149 LANG_FOR_EACH_INPUT_STATEMENT (f)
153 for (isec = f->the_bfd->sections;
157 if (isec->output_section == osec
159 && (bfd_get_section_flags (isec->owner, isec)
163 = (elf_section_data (isec)->this_hdr.sh_flags
164 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
173 /* Check that all input sections have the same contents-type flags
174 as the first input section. */
176 LANG_FOR_EACH_INPUT_STATEMENT (f)
180 for (isec = f->the_bfd->sections;
184 if (isec->output_section == osec
186 && (bfd_get_section_flags (isec->owner, isec)
190 = (elf_section_data (isec)->this_hdr.sh_flags
191 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
193 /* If flags don't agree, we need a .cranges section.
194 Create it here if it did not exist through input
196 if (iflags_isa != oflags_isa)
200 /* This section will be *appended* to
201 sections, so the outer iteration will reach
202 it in due time and set
203 sh64_elf_section_data; no need to set it
204 specifically here. */
206 = bfd_make_section (link_info.output_bfd,
207 SH64_CRANGES_SECTION_NAME);
209 || !bfd_set_section_flags (link_info.output_bfd,
216 (_("%P%E%F: Can't make .cranges section\n"));
219 /* We don't need to look at more input sections,
220 and we know this section will have mixed
229 /* If we got here, then all input sections in this output section
230 have the same contents flag. Put that where we expect to see
231 contents flags. We don't need to do this for sections that will
232 need additional, linker-generated .cranges entries. */
233 sh64_sec_data->contents_flags = iflags_isa;
240 /* Size up and extend the .cranges section, merging generated entries. */
243 sh64_elf_${EMULATION_NAME}_after_allocation (void)
245 bfd_vma new_cranges = 0;
246 bfd_vma cranges_growth = 0;
250 asection *cranges = bfd_get_section_by_name (link_info.output_bfd,
251 SH64_CRANGES_SECTION_NAME);
253 /* If this ever starts doing something, we will pick it up. */
254 after_allocation_default ();
256 /* If there is no .cranges section, it is because it was seen earlier on
257 that none was needed. Otherwise it must have been created then, or
258 be present in input. */
262 /* First, we set the ISA flags for each output section according to the
263 first non-discarded section. For each input section in osec, we
264 check if it has the same flags. If it does not, we set flags to mark
265 a mixed section (and exit the loop early). */
266 for (osec = link_info.output_bfd->sections;
270 bfd_vma oflags_isa = 0;
271 bfd_boolean need_check_cranges = FALSE;
273 /* Omit excluded or garbage-collected sections. */
274 if (bfd_get_section_flags (link_info.output_bfd, osec) & SEC_EXCLUDE)
277 /* First find an input section so we have flags to compare with; the
278 flags in the output section are not valid. */
280 LANG_FOR_EACH_INPUT_STATEMENT (f)
284 for (isec = f->the_bfd->sections;
288 if (isec->output_section == osec
290 && (bfd_get_section_flags (isec->owner, isec)
294 = (elf_section_data (isec)->this_hdr.sh_flags
295 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
304 /* Check that all input sections have the same contents-type flags
305 as the first input section. */
307 LANG_FOR_EACH_INPUT_STATEMENT (f)
311 for (isec = f->the_bfd->sections;
315 if (isec->output_section == osec
317 && (bfd_get_section_flags (isec->owner, isec)
321 = (elf_section_data (isec)->this_hdr.sh_flags
322 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
324 /* If flags don't agree, set the target-specific data
325 of the section to mark that this section needs to
326 be have .cranges section entries added. Don't
327 bother setting ELF section flags in output section;
328 they will be cleared later and will have to be
329 re-initialized before the linked file is written. */
330 if (iflags_isa != oflags_isa)
332 oflags_isa = SHF_SH5_ISA32_MIXED;
334 BFD_ASSERT (sh64_elf_section_data (osec)->sh64_info);
336 sh64_elf_section_data (osec)->sh64_info->contents_flags
337 = SHF_SH5_ISA32_MIXED;
338 need_check_cranges = TRUE;
348 /* If there were no new ranges for this output section, we don't
349 need to iterate over the input sections to check how many are
351 if (! need_check_cranges)
354 /* If we found a section with differing contents type, we need more
355 ranges to mark the sections that are not mixed (and already have
356 .cranges descriptors). Calculate the maximum number of new
357 entries here. We may merge some of them, so that number is not
358 final; it can shrink. */
360 LANG_FOR_EACH_INPUT_STATEMENT (f)
364 for (isec = f->the_bfd->sections;
368 if (isec->output_section == osec
370 && (bfd_get_section_flags (isec->owner, isec)
372 && ((elf_section_data (isec)->this_hdr.sh_flags
373 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
374 != SHF_SH5_ISA32_MIXED))
381 /* ldemul_after_allocation may be called twice. First directly from
382 lang_process, and the second time when lang_process calls ldemul_finish,
383 which calls gld${EMULATION_NAME}_finish, e.g. gldshelf32_finish, which
384 is defined in emultempl/elf32.em and calls ldemul_after_allocation,
385 if bfd_elf_discard_info returned true. */
386 if (cranges->contents != NULL)
387 free (cranges->contents);
389 BFD_ASSERT (sh64_elf_section_data (cranges)->sh64_info != NULL);
391 /* Make sure we have .cranges in memory even if there were only
392 assembler-generated .cranges. */
393 cranges_growth = new_cranges * SH64_CRANGE_SIZE;
394 cranges->contents = xcalloc (cranges->size + cranges_growth, 1);
395 bfd_set_section_flags (cranges->owner, cranges,
396 bfd_get_section_flags (cranges->owner, cranges)
399 /* If we don't need to grow the .cranges section beyond what was in the
400 input sections, we have nothing more to do here. We then only got
401 here because there was a .cranges section coming from input. Zero
402 out the number of generated .cranges. */
403 if (new_cranges == 0)
405 sh64_elf_section_data (cranges)->sh64_info->cranges_growth = 0;
409 crangesp = cranges->contents + cranges->size;
411 /* Now pass over the sections again, and make reloc orders for the new
412 .cranges entries. Constants are set as we go. */
413 for (osec = link_info.output_bfd->sections;
417 struct bfd_link_order *cr_addr_order = NULL;
418 enum sh64_elf_cr_type last_cr_type = CRT_NONE;
419 bfd_vma last_cr_size = 0;
420 bfd_vma continuation_vma = 0;
422 /* Omit excluded or garbage-collected sections, and output sections
423 which were not marked as needing further processing. */
424 if ((bfd_get_section_flags (link_info.output_bfd, osec) & SEC_EXCLUDE) != 0
425 || (sh64_elf_section_data (osec)->sh64_info->contents_flags
426 != SHF_SH5_ISA32_MIXED))
430 LANG_FOR_EACH_INPUT_STATEMENT (f)
434 for (isec = f->the_bfd->sections;
438 /* Allow only sections that have (at least initially) a
439 non-zero size, and are not excluded, and are not marked
440 as containing mixed data, thus already having .cranges
442 if (isec->output_section == osec
444 && (bfd_get_section_flags (isec->owner, isec)
446 && ((elf_section_data (isec)->this_hdr.sh_flags
447 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
448 != SHF_SH5_ISA32_MIXED))
450 enum sh64_elf_cr_type cr_type;
453 = (elf_section_data (isec)->this_hdr.sh_flags
454 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
456 if (isa_flags == SHF_SH5_ISA32)
457 cr_type = CRT_SH5_ISA32;
458 else if ((bfd_get_section_flags (isec->owner, isec)
462 cr_type = CRT_SH5_ISA16;
464 cr_size = isec->size;
466 /* Sections can be empty, like .text in a file that
467 only contains other sections. Ranges shouldn't be
468 emitted for them. This can presumably happen after
469 relaxing and is not be caught at the "raw size"
474 /* See if this is a continuation of the previous range
475 for the same output section. If so, just change
476 the size of the last range and continue. */
477 if (cr_type == last_cr_type
479 == osec->vma + isec->output_offset))
481 last_cr_size += cr_size;
482 bfd_put_32 (link_info.output_bfd, last_cr_size,
483 crangesp - SH64_CRANGE_SIZE
484 + SH64_CRANGE_CR_SIZE_OFFSET);
486 continuation_vma += cr_size;
490 /* If we emit relocatable contents, we need a
491 relocation for the start address. */
492 if (link_info.relocatable || link_info.emitrelocations)
494 /* FIXME: We could perhaps use lang_add_reloc and
495 friends here, but I'm not really sure that
496 would leave us free to do some optimizations
499 = bfd_new_link_order (link_info.output_bfd, cranges);
501 if (cr_addr_order == NULL)
503 einfo (_("%P%F: bfd_new_link_order failed\n"));
507 cr_addr_order->type = bfd_section_reloc_link_order;
508 cr_addr_order->offset
509 = (cranges->output_offset
510 + crangesp + SH64_CRANGE_CR_ADDR_OFFSET
511 - cranges->contents);
512 cr_addr_order->size = 4;
513 cr_addr_order->u.reloc.p
514 = xmalloc (sizeof (struct bfd_link_order_reloc));
516 cr_addr_order->u.reloc.p->reloc = BFD_RELOC_32;
517 cr_addr_order->u.reloc.p->u.section = osec;
519 /* Since SH, unlike normal RELA-targets, uses a
520 "partial inplace" REL-like relocation for this,
521 we put the addend in the contents and specify 0
523 bfd_put_32 (link_info.output_bfd, isec->output_offset,
524 crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
525 cr_addr_order->u.reloc.p->addend = 0;
528 bfd_put_32 (link_info.output_bfd,
529 osec->vma + isec->output_offset,
530 crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
532 /* If we could make a reloc for cr_size we would do
533 it, but we would have to have a symbol for the size
534 of the _input_ section and there's no way to
536 bfd_put_32 (link_info.output_bfd, cr_size,
537 crangesp + SH64_CRANGE_CR_SIZE_OFFSET);
539 bfd_put_16 (link_info.output_bfd, cr_type,
540 crangesp + SH64_CRANGE_CR_TYPE_OFFSET);
542 last_cr_type = cr_type;
543 last_cr_size = cr_size;
545 = osec->vma + isec->output_offset + cr_size;
546 crangesp += SH64_CRANGE_SIZE;
553 /* The .cranges section will have this size, no larger or smaller.
554 Since relocs (if relocatable linking) will be emitted into the
555 "extended" size, we must set the raw size to the total. We have to
556 keep track of the number of new .cranges entries.
558 Sorting before writing is done by sh64_elf_final_write_processing. */
560 sh64_elf_section_data (cranges)->sh64_info->cranges_growth
561 = crangesp - cranges->contents - cranges->size;
562 cranges->size = crangesp - cranges->contents;
563 cranges->rawsize = cranges->size;