1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
4 # This file is part of GLD, the Gnu Linker.
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 2 of the License, or
9 # (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 # This file is sourced from elf32.em, and defines extra sh64
25 LDEMUL_AFTER_ALLOCATION=sh64_elf_${EMULATION_NAME}_after_allocation
26 LDEMUL_BEFORE_ALLOCATION=sh64_elf_${EMULATION_NAME}_before_allocation
28 cat >>e${EMULATION_NAME}.c <<EOF
30 #include "libiberty.h"
35 static void sh64_elf_${EMULATION_NAME}_before_allocation PARAMS ((void));
36 static void sh64_elf_${EMULATION_NAME}_after_allocation PARAMS ((void));
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 ()
54 /* Call main function; we're just extending it. */
55 gld${EMULATION_NAME}_before_allocation ();
57 cranges = bfd_get_section_by_name (output_bfd, SH64_CRANGES_SECTION_NAME);
61 if (command_line.relax)
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. */
70 (_("%P: Sorry, turning off relaxing: .cranges section in input.\n"));
71 einfo (_(" A .cranges section is present in:\n"));
74 LANG_FOR_EACH_INPUT_STATEMENT (f)
76 asection *input_cranges
77 = bfd_get_section_by_name (f->the_bfd,
78 SH64_CRANGES_SECTION_NAME);
79 if (input_cranges != NULL)
84 command_line.relax = FALSE;
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
93 if (command_line.relax)
95 LANG_FOR_EACH_INPUT_STATEMENT (f)
97 if (bfd_get_flavour (f->the_bfd) == bfd_target_elf_flavour)
100 for (isec = f->the_bfd->sections;
104 if (elf_section_data (isec)->this_hdr.sh_flags
105 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
107 einfo (_("%P: Sorry, turning off relaxing: SHmedia sections present.\n"));
109 command_line.relax = FALSE;
110 goto done_scanning_shmedia_sections;
116 done_scanning_shmedia_sections:
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;
125 struct sh64_section_data *sh64_sec_data;
126 bfd_vma oflags_isa = 0;
127 bfd_vma iflags_isa = 0;
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"));
132 sh64_sec_data = sh64_elf_section_data (osec);
134 /* Omit excluded or garbage-collected sections. */
135 if (bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE)
138 /* Make sure we have the target section data initialized. */
139 if (sh64_sec_data == NULL)
141 sh64_sec_data = xcalloc (1, sizeof (struct sh64_section_data));
142 sh64_elf_section_data (osec) = sh64_sec_data;
145 /* First find an input section so we have flags to compare with; the
146 flags in the output section are not valid. */
148 LANG_FOR_EACH_INPUT_STATEMENT (f)
152 for (isec = f->the_bfd->sections;
156 if (isec->output_section == osec
157 && isec->_raw_size != 0
158 && (bfd_get_section_flags (isec->owner, isec)
162 = (elf_section_data (isec)->this_hdr.sh_flags
163 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
172 /* Check that all input sections have the same contents-type flags
173 as the first input section. */
175 LANG_FOR_EACH_INPUT_STATEMENT (f)
179 for (isec = f->the_bfd->sections;
183 if (isec->output_section == osec
184 && isec->_raw_size != 0
185 && (bfd_get_section_flags (isec->owner, isec)
189 = (elf_section_data (isec)->this_hdr.sh_flags
190 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
192 /* If flags don't agree, we need a .cranges section.
193 Create it here if it did not exist through input
195 if (iflags_isa != oflags_isa)
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. */
205 = bfd_make_section (output_bfd,
206 SH64_CRANGES_SECTION_NAME);
208 || !bfd_set_section_flags (output_bfd,
215 (_("%P%E%F: Can't make .cranges section\n"));
218 /* We don't need to look at more input sections,
219 and we know this section will have mixed
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;
239 /* Size up and extend the .cranges section, merging generated entries. */
242 sh64_elf_${EMULATION_NAME}_after_allocation ()
244 bfd_vma new_cranges = 0;
245 bfd_vma cranges_growth = 0;
250 = bfd_get_section_by_name (output_bfd, SH64_CRANGES_SECTION_NAME);
252 /* If this ever starts doing something, we will pick it up. */
253 after_allocation_default ();
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. */
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;
269 bfd_vma oflags_isa = 0;
270 bfd_boolean need_check_cranges = FALSE;
272 /* Omit excluded or garbage-collected sections. */
273 if (bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE)
276 /* First find an input section so we have flags to compare with; the
277 flags in the output section are not valid. */
279 LANG_FOR_EACH_INPUT_STATEMENT (f)
283 for (isec = f->the_bfd->sections;
287 if (isec->output_section == osec
288 && isec->_raw_size != 0
289 && (bfd_get_section_flags (isec->owner, isec)
293 = (elf_section_data (isec)->this_hdr.sh_flags
294 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
303 /* Check that all input sections have the same contents-type flags
304 as the first input section. */
306 LANG_FOR_EACH_INPUT_STATEMENT (f)
310 for (isec = f->the_bfd->sections;
314 if (isec->output_section == osec
315 && isec->_raw_size != 0
316 && (bfd_get_section_flags (isec->owner, isec)
320 = (elf_section_data (isec)->this_hdr.sh_flags
321 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
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)
331 oflags_isa = SHF_SH5_ISA32_MIXED;
333 BFD_ASSERT (sh64_elf_section_data (osec) != NULL);
335 sh64_elf_section_data (osec)->contents_flags
336 = SHF_SH5_ISA32_MIXED;
337 need_check_cranges = TRUE;
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
350 if (! need_check_cranges)
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. */
359 LANG_FOR_EACH_INPUT_STATEMENT (f)
363 for (isec = f->the_bfd->sections;
367 if (isec->output_section == osec
368 && isec->_raw_size != 0
369 && (bfd_get_section_flags (isec->owner, isec)
371 && ((elf_section_data (isec)->this_hdr.sh_flags
372 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
373 != SHF_SH5_ISA32_MIXED))
380 BFD_ASSERT (cranges->contents == NULL);
381 BFD_ASSERT (sh64_elf_section_data (cranges) != NULL);
383 /* Make sure we have .cranges in memory even if there were only
384 assembler-generated .cranges. */
385 cranges_growth = new_cranges * SH64_CRANGE_SIZE;
387 = (bfd_byte *) xcalloc (cranges->_raw_size + cranges_growth, 1);
388 bfd_set_section_flags (cranges->owner, cranges,
389 bfd_get_section_flags (cranges->owner, cranges)
392 /* If we don't need to grow the .cranges section beyond what was in the
393 input sections, we have nothing more to do here. We then only got
394 here because there was a .cranges section coming from input. Zero
395 out the number of generated .cranges. */
396 if (new_cranges == 0)
398 sh64_elf_section_data (cranges)->cranges_growth = 0;
402 crangesp = cranges->contents + cranges->_raw_size;
404 /* Now pass over the sections again, and make reloc orders for the new
405 .cranges entries. Constants are set as we go. */
406 for (osec = output_bfd->sections;
410 struct bfd_link_order *cr_addr_order = NULL;
411 enum sh64_elf_cr_type last_cr_type = CRT_NONE;
412 bfd_vma last_cr_size = 0;
413 bfd_vma continuation_vma = 0;
415 /* Omit excluded or garbage-collected sections, and output sections
416 which were not marked as needing further processing. */
417 if ((bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE) != 0
418 || (sh64_elf_section_data (osec)->contents_flags
419 != SHF_SH5_ISA32_MIXED))
423 LANG_FOR_EACH_INPUT_STATEMENT (f)
427 for (isec = f->the_bfd->sections;
431 /* Allow only sections that have (at least initially) a
432 non-zero size, and are not excluded, and are not marked
433 as containing mixed data, thus already having .cranges
435 if (isec->output_section == osec
436 && isec->_raw_size != 0
437 && (bfd_get_section_flags (isec->owner, isec)
439 && ((elf_section_data (isec)->this_hdr.sh_flags
440 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
441 != SHF_SH5_ISA32_MIXED))
443 enum sh64_elf_cr_type cr_type;
446 = (elf_section_data (isec)->this_hdr.sh_flags
447 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
449 if (isa_flags == SHF_SH5_ISA32)
450 cr_type = CRT_SH5_ISA32;
451 else if ((bfd_get_section_flags (isec->owner, isec)
455 cr_type = CRT_SH5_ISA16;
458 = (isec->_cooked_size
459 ? isec->_cooked_size : isec->_raw_size);
461 /* Sections can be empty, like .text in a file that
462 only contains other sections. Ranges shouldn't be
463 emitted for them. This can presumably happen after
464 relaxing and is not be caught at the "raw size"
469 /* See if this is a continuation of the previous range
470 for the same output section. If so, just change
471 the size of the last range and continue. */
472 if (cr_type == last_cr_type
474 == osec->vma + isec->output_offset))
476 last_cr_size += cr_size;
477 bfd_put_32 (output_bfd, last_cr_size,
478 crangesp - SH64_CRANGE_SIZE
479 + SH64_CRANGE_CR_SIZE_OFFSET);
481 continuation_vma += cr_size;
485 /* If we emit relocateable contents, we need a
486 relocation for the start address. */
487 if (link_info.relocateable || link_info.emitrelocations)
489 /* FIXME: We could perhaps use lang_add_reloc and
490 friends here, but I'm not really sure that
491 would leave us free to do some optimizations
494 = bfd_new_link_order (output_bfd, cranges);
496 if (cr_addr_order == NULL)
498 einfo (_("%P%F: bfd_new_link_order failed\n"));
502 cr_addr_order->type = bfd_section_reloc_link_order;
503 cr_addr_order->offset
504 = (cranges->output_offset
505 + crangesp + SH64_CRANGE_CR_ADDR_OFFSET
506 - cranges->contents);
507 cr_addr_order->size = 4;
508 cr_addr_order->u.reloc.p
509 = ((struct bfd_link_order_reloc *)
510 xmalloc (sizeof (struct bfd_link_order_reloc)));
512 cr_addr_order->u.reloc.p->reloc = BFD_RELOC_32;
513 cr_addr_order->u.reloc.p->u.section = osec;
515 /* Since SH, unlike normal RELA-targets, uses a
516 "partial inplace" REL-like relocation for this,
517 we put the addend in the contents and specify 0
519 bfd_put_32 (output_bfd, isec->output_offset,
520 crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
521 cr_addr_order->u.reloc.p->addend = 0;
523 /* We must update the number of relocations here,
524 since the elf linker does not take link orders
525 into account when setting header sizes. The
526 actual relocation orders are however executed
528 elf_section_data(cranges)->rel_count++;
531 bfd_put_32 (output_bfd,
532 osec->vma + isec->output_offset,
533 crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
535 /* If we could make a reloc for cr_size we would do
536 it, but we would have to have a symbol for the size
537 of the _input_ section and there's no way to
539 bfd_put_32 (output_bfd, cr_size,
540 crangesp + SH64_CRANGE_CR_SIZE_OFFSET);
542 bfd_put_16 (output_bfd, (bfd_vma) cr_type,
543 crangesp + SH64_CRANGE_CR_TYPE_OFFSET);
545 last_cr_type = cr_type;
546 last_cr_size = cr_size;
548 = osec->vma + isec->output_offset + cr_size;
549 crangesp += SH64_CRANGE_SIZE;
556 /* The .cranges section will have this size, no larger or smaller.
557 Since relocs (if relocateable linking) will be emitted into the
558 "extended" size, we must set the raw size to the total. We have to
559 keep track of the number of new .cranges entries.
561 Sorting before writing is done by sh64_elf_final_write_processing. */
563 cranges->_cooked_size = crangesp - cranges->contents;
564 sh64_elf_section_data (cranges)->cranges_growth
565 = cranges->_cooked_size - cranges->_raw_size;
566 cranges->_raw_size = cranges->_cooked_size;