1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2000, 2001, 2002, 2003 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"
34 #include "elf32-sh64.h"
36 /* Check if we need a .cranges section and create it if it's not in any
37 input file. It might seem better to always create it and if unneeded,
38 discard it, but I don't find a simple way to discard it totally from
41 Putting it here instead of as a elf_backend_always_size_sections hook
42 in elf32-sh64.c, means that we have access to linker command line
43 options here, and we can access input sections in the order in which
44 they will be linked. */
47 sh64_elf_${EMULATION_NAME}_before_allocation (void)
52 /* Call main function; we're just extending it. */
53 gld${EMULATION_NAME}_before_allocation ();
55 cranges = bfd_get_section_by_name (output_bfd, SH64_CRANGES_SECTION_NAME);
59 if (command_line.relax)
61 /* FIXME: Look through incoming sections with .cranges
62 descriptors, build up some kind of descriptors that the
63 relaxing function will pick up and adjust, or perhaps make it
64 find and adjust an associated .cranges descriptor. We could
65 also look through incoming relocs and kill the ones marking
66 relaxation areas, but that wouldn't be TRT. */
68 (_("%P: Sorry, turning off relaxing: .cranges section in input.\n"));
69 einfo (_(" A .cranges section is present in:\n"));
72 LANG_FOR_EACH_INPUT_STATEMENT (f)
74 asection *input_cranges
75 = bfd_get_section_by_name (f->the_bfd,
76 SH64_CRANGES_SECTION_NAME);
77 if (input_cranges != NULL)
82 command_line.relax = FALSE;
85 /* We wouldn't need to do anything when there's already a .cranges
86 section (and have a return here), except that we need to set the
87 section flags right for output sections that *don't* need a
91 if (command_line.relax)
93 LANG_FOR_EACH_INPUT_STATEMENT (f)
95 if (bfd_get_flavour (f->the_bfd) == bfd_target_elf_flavour)
98 for (isec = f->the_bfd->sections;
102 if (elf_section_data (isec)->this_hdr.sh_flags
103 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
105 einfo (_("%P: Sorry, turning off relaxing: SHmedia sections present.\n"));
107 command_line.relax = FALSE;
108 goto done_scanning_shmedia_sections;
114 done_scanning_shmedia_sections:
116 /* For each non-empty input section in each output section, check if it
117 has the same SH64-specific flags. If some input section differs, we
118 need a .cranges section. */
119 for (osec = output_bfd->sections;
123 struct sh64_section_data *sh64_sec_data;
124 bfd_vma oflags_isa = 0;
125 bfd_vma iflags_isa = 0;
127 if (bfd_get_flavour (output_bfd) != bfd_target_elf_flavour)
128 einfo (_("%FError: non-ELF output formats are not supported by this target's linker.\n"));
130 sh64_sec_data = sh64_elf_section_data (osec)->sh64_info;
132 /* Omit excluded or garbage-collected sections. */
133 if (bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE)
136 /* Make sure we have the target section data initialized. */
137 if (sh64_sec_data == NULL)
139 sh64_sec_data = xcalloc (1, sizeof (struct sh64_section_data));
140 sh64_elf_section_data (osec)->sh64_info = sh64_sec_data;
143 /* First find an input section so we have flags to compare with; the
144 flags in the output section are not valid. */
146 LANG_FOR_EACH_INPUT_STATEMENT (f)
150 for (isec = f->the_bfd->sections;
154 if (isec->output_section == osec
155 && isec->_raw_size != 0
156 && (bfd_get_section_flags (isec->owner, isec)
160 = (elf_section_data (isec)->this_hdr.sh_flags
161 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
170 /* Check that all input sections have the same contents-type flags
171 as the first input section. */
173 LANG_FOR_EACH_INPUT_STATEMENT (f)
177 for (isec = f->the_bfd->sections;
181 if (isec->output_section == osec
182 && isec->_raw_size != 0
183 && (bfd_get_section_flags (isec->owner, isec)
187 = (elf_section_data (isec)->this_hdr.sh_flags
188 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
190 /* If flags don't agree, we need a .cranges section.
191 Create it here if it did not exist through input
193 if (iflags_isa != oflags_isa)
197 /* This section will be *appended* to
198 sections, so the outer iteration will reach
199 it in due time and set
200 sh64_elf_section_data; no need to set it
201 specifically here. */
203 = bfd_make_section (output_bfd,
204 SH64_CRANGES_SECTION_NAME);
206 || !bfd_set_section_flags (output_bfd,
213 (_("%P%E%F: Can't make .cranges section\n"));
216 /* We don't need to look at more input sections,
217 and we know this section will have mixed
226 /* If we got here, then all input sections in this output section
227 have the same contents flag. Put that where we expect to see
228 contents flags. We don't need to do this for sections that will
229 need additional, linker-generated .cranges entries. */
230 sh64_sec_data->contents_flags = iflags_isa;
237 /* Size up and extend the .cranges section, merging generated entries. */
240 sh64_elf_${EMULATION_NAME}_after_allocation (void)
242 bfd_vma new_cranges = 0;
243 bfd_vma cranges_growth = 0;
248 = bfd_get_section_by_name (output_bfd, SH64_CRANGES_SECTION_NAME);
250 /* If this ever starts doing something, we will pick it up. */
251 after_allocation_default ();
253 /* If there is no .cranges section, it is because it was seen earlier on
254 that none was needed. Otherwise it must have been created then, or
255 be present in input. */
259 /* First, we set the ISA flags for each output section according to the
260 first non-discarded section. For each input section in osec, we
261 check if it has the same flags. If it does not, we set flags to mark
262 a mixed section (and exit the loop early). */
263 for (osec = output_bfd->sections;
267 bfd_vma oflags_isa = 0;
268 bfd_boolean need_check_cranges = FALSE;
270 /* Omit excluded or garbage-collected sections. */
271 if (bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE)
274 /* First find an input section so we have flags to compare with; the
275 flags in the output section are not valid. */
277 LANG_FOR_EACH_INPUT_STATEMENT (f)
281 for (isec = f->the_bfd->sections;
285 if (isec->output_section == osec
286 && isec->_raw_size != 0
287 && (bfd_get_section_flags (isec->owner, isec)
291 = (elf_section_data (isec)->this_hdr.sh_flags
292 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
301 /* Check that all input sections have the same contents-type flags
302 as the first input section. */
304 LANG_FOR_EACH_INPUT_STATEMENT (f)
308 for (isec = f->the_bfd->sections;
312 if (isec->output_section == osec
313 && isec->_raw_size != 0
314 && (bfd_get_section_flags (isec->owner, isec)
318 = (elf_section_data (isec)->this_hdr.sh_flags
319 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
321 /* If flags don't agree, set the target-specific data
322 of the section to mark that this section needs to
323 be have .cranges section entries added. Don't
324 bother setting ELF section flags in output section;
325 they will be cleared later and will have to be
326 re-initialized before the linked file is written. */
327 if (iflags_isa != oflags_isa)
329 oflags_isa = SHF_SH5_ISA32_MIXED;
331 BFD_ASSERT (sh64_elf_section_data (osec)->sh64_info);
333 sh64_elf_section_data (osec)->sh64_info->contents_flags
334 = SHF_SH5_ISA32_MIXED;
335 need_check_cranges = TRUE;
345 /* If there were no new ranges for this output section, we don't
346 need to iterate over the input sections to check how many are
348 if (! need_check_cranges)
351 /* If we found a section with differing contents type, we need more
352 ranges to mark the sections that are not mixed (and already have
353 .cranges descriptors). Calculate the maximum number of new
354 entries here. We may merge some of them, so that number is not
355 final; it can shrink. */
357 LANG_FOR_EACH_INPUT_STATEMENT (f)
361 for (isec = f->the_bfd->sections;
365 if (isec->output_section == osec
366 && isec->_raw_size != 0
367 && (bfd_get_section_flags (isec->owner, isec)
369 && ((elf_section_data (isec)->this_hdr.sh_flags
370 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
371 != SHF_SH5_ISA32_MIXED))
378 /* ldemul_after_allocation may be called twice. First directly from
379 lang_process, and the second time when lang_process calls ldemul_finish,
380 which calls gld${EMULATION_NAME}_finish, e.g. gldshelf32_finish, which
381 is defined in emultempl/elf32.em and calls ldemul_after_allocation,
382 if bfd_elf_discard_info returned true. */
383 if (cranges->contents != NULL)
384 free (cranges->contents);
386 BFD_ASSERT (sh64_elf_section_data (cranges)->sh64_info != NULL);
388 /* Make sure we have .cranges in memory even if there were only
389 assembler-generated .cranges. */
390 cranges_growth = new_cranges * SH64_CRANGE_SIZE;
391 cranges->contents = xcalloc (cranges->_raw_size + cranges_growth, 1);
392 bfd_set_section_flags (cranges->owner, cranges,
393 bfd_get_section_flags (cranges->owner, cranges)
396 /* If we don't need to grow the .cranges section beyond what was in the
397 input sections, we have nothing more to do here. We then only got
398 here because there was a .cranges section coming from input. Zero
399 out the number of generated .cranges. */
400 if (new_cranges == 0)
402 sh64_elf_section_data (cranges)->sh64_info->cranges_growth = 0;
406 crangesp = cranges->contents + cranges->_raw_size;
408 /* Now pass over the sections again, and make reloc orders for the new
409 .cranges entries. Constants are set as we go. */
410 for (osec = output_bfd->sections;
414 struct bfd_link_order *cr_addr_order = NULL;
415 enum sh64_elf_cr_type last_cr_type = CRT_NONE;
416 bfd_vma last_cr_size = 0;
417 bfd_vma continuation_vma = 0;
419 /* Omit excluded or garbage-collected sections, and output sections
420 which were not marked as needing further processing. */
421 if ((bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE) != 0
422 || (sh64_elf_section_data (osec)->sh64_info->contents_flags
423 != SHF_SH5_ISA32_MIXED))
427 LANG_FOR_EACH_INPUT_STATEMENT (f)
431 for (isec = f->the_bfd->sections;
435 /* Allow only sections that have (at least initially) a
436 non-zero size, and are not excluded, and are not marked
437 as containing mixed data, thus already having .cranges
439 if (isec->output_section == osec
440 && isec->_raw_size != 0
441 && (bfd_get_section_flags (isec->owner, isec)
443 && ((elf_section_data (isec)->this_hdr.sh_flags
444 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
445 != SHF_SH5_ISA32_MIXED))
447 enum sh64_elf_cr_type cr_type;
450 = (elf_section_data (isec)->this_hdr.sh_flags
451 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
453 if (isa_flags == SHF_SH5_ISA32)
454 cr_type = CRT_SH5_ISA32;
455 else if ((bfd_get_section_flags (isec->owner, isec)
459 cr_type = CRT_SH5_ISA16;
462 = (isec->_cooked_size
463 ? isec->_cooked_size : isec->_raw_size);
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"
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
478 == osec->vma + isec->output_offset))
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);
485 continuation_vma += cr_size;
489 /* If we emit relocatable contents, we need a
490 relocation for the start address. */
491 if (link_info.relocatable || link_info.emitrelocations)
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
498 = bfd_new_link_order (output_bfd, cranges);
500 if (cr_addr_order == NULL)
502 einfo (_("%P%F: bfd_new_link_order failed\n"));
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));
515 cr_addr_order->u.reloc.p->reloc = BFD_RELOC_32;
516 cr_addr_order->u.reloc.p->u.section = osec;
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
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;
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
531 elf_section_data(cranges)->rel_count++;
534 bfd_put_32 (output_bfd,
535 osec->vma + isec->output_offset,
536 crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
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
542 bfd_put_32 (output_bfd, cr_size,
543 crangesp + SH64_CRANGE_CR_SIZE_OFFSET);
545 bfd_put_16 (output_bfd, cr_type,
546 crangesp + SH64_CRANGE_CR_TYPE_OFFSET);
548 last_cr_type = cr_type;
549 last_cr_size = cr_size;
551 = osec->vma + isec->output_offset + cr_size;
552 crangesp += SH64_CRANGE_SIZE;
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.
564 Sorting before writing is done by sh64_elf_final_write_processing. */
566 cranges->_cooked_size = crangesp - cranges->contents;
567 sh64_elf_section_data (cranges)->sh64_info->cranges_growth
568 = cranges->_cooked_size - cranges->_raw_size;
569 cranges->_raw_size = cranges->_cooked_size;