PR ld/10956
[binutils.git] / ld / emultempl / sh64elf.em
blob88035b0dc51177a4c2771e131f8c0aa57102ca02
1 # This shell script emits a C file. -*- C -*-
2 #   Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
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,
20 # MA 02110-1301, USA.
23 # This file is sourced from elf32.em, and defines extra sh64
24 # specific routines.
27 LDEMUL_AFTER_ALLOCATION=sh64_elf_${EMULATION_NAME}_after_allocation
28 LDEMUL_BEFORE_ALLOCATION=sh64_elf_${EMULATION_NAME}_before_allocation
30 fragment <<EOF
32 #include "libiberty.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/sh.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
41    the output.
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.  */
48 static void
49 sh64_elf_${EMULATION_NAME}_before_allocation (void)
51   asection *cranges;
52   asection *osec;
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);
60   if (cranges != NULL)
61     {
62       if (RELAXATION_ENABLED)
63         {
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.  */
70           einfo
71             (_("%P: Sorry, turning off relaxing: .cranges section in input.\n"));
72           einfo (_(" A .cranges section is present in:\n"));
74           {
75             LANG_FOR_EACH_INPUT_STATEMENT (f)
76               {
77                 asection *input_cranges
78                   = bfd_get_section_by_name (f->the_bfd,
79                                              SH64_CRANGES_SECTION_NAME);
80                 if (input_cranges != NULL)
81                   einfo (" %I\n", f);
82               }
83           }
85           DISABLE_RELAXATION;
86         }
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
91          .cranges section.  */
92     }
94   if (RELAXATION_ENABLED)
95     {
96       LANG_FOR_EACH_INPUT_STATEMENT (f)
97         {
98           if (bfd_get_flavour (f->the_bfd) == bfd_target_elf_flavour)
99             {
100               asection *isec;
102               for (isec = f->the_bfd->sections;
103                    isec != NULL;
104                    isec = isec->next)
105                 {
106                   if (elf_section_data (isec)->this_hdr.sh_flags
107                       & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
108                     {
109                       einfo (_("%P: Sorry, turning off relaxing: SHmedia sections present.\n"));
110                       einfo ("  %I\n", f);
111                       DISABLE_RELAXATION;
112                       goto done_scanning_shmedia_sections;
113                     }
114                 }
115             }
116         }
117     }
118  done_scanning_shmedia_sections:
120   /* For each non-empty input section in each output section, check if it
121      has the same SH64-specific flags.  If some input section differs, we
122      need a .cranges section.  */
123   for (osec = link_info.output_bfd->sections;
124        osec != NULL;
125        osec = osec->next)
126     {
127       struct sh64_section_data *sh64_sec_data;
128       bfd_vma oflags_isa = 0;
129       bfd_vma iflags_isa = 0;
131       if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
132         einfo (_("%FError: non-ELF output formats are not supported by this target's linker.\n"));
134       sh64_sec_data = sh64_elf_section_data (osec)->sh64_info;
136       /* Omit excluded or garbage-collected sections.  */
137       if (bfd_get_section_flags (link_info.output_bfd, osec) & SEC_EXCLUDE)
138         continue;
140       /* Make sure we have the target section data initialized.  */
141       if (sh64_sec_data == NULL)
142         {
143           sh64_sec_data = xcalloc (1, sizeof (struct sh64_section_data));
144           sh64_elf_section_data (osec)->sh64_info = sh64_sec_data;
145         }
147       /* First find an input section so we have flags to compare with; the
148          flags in the output section are not valid.  */
149       {
150         LANG_FOR_EACH_INPUT_STATEMENT (f)
151           {
152             asection *isec;
154             for (isec = f->the_bfd->sections;
155                  isec != NULL;
156                  isec = isec->next)
157               {
158                 if (isec->output_section == osec
159                     && isec->size != 0
160                     && (bfd_get_section_flags (isec->owner, isec)
161                         & SEC_EXCLUDE) == 0)
162                   {
163                     oflags_isa
164                       = (elf_section_data (isec)->this_hdr.sh_flags
165                          & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
166                     goto break_1;
167                   }
168               }
169           }
170       }
172     break_1:
174       /* Check that all input sections have the same contents-type flags
175          as the first input section.  */
176       {
177         LANG_FOR_EACH_INPUT_STATEMENT (f)
178           {
179             asection *isec;
181             for (isec = f->the_bfd->sections;
182                  isec != NULL;
183                  isec = isec->next)
184               {
185                 if (isec->output_section == osec
186                     && isec->size != 0
187                     && (bfd_get_section_flags (isec->owner, isec)
188                         & SEC_EXCLUDE) == 0)
189                   {
190                     iflags_isa
191                       = (elf_section_data (isec)->this_hdr.sh_flags
192                          & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
194                     /* If flags don't agree, we need a .cranges section.
195                        Create it here if it did not exist through input
196                        sections.  */
197                     if (iflags_isa != oflags_isa)
198                       {
199                         if (cranges == NULL)
200                           {
201                             /* This section will be *appended* to
202                                sections, so the outer iteration will reach
203                                it in due time and set
204                                sh64_elf_section_data; no need to set it
205                                specifically here.  */
206                             cranges
207                               = bfd_make_section_with_flags (link_info.output_bfd,
208                                                              SH64_CRANGES_SECTION_NAME,
209                                                              SEC_LINKER_CREATED
210                                                              | SEC_KEEP
211                                                              | SEC_HAS_CONTENTS
212                                                              | SEC_DEBUGGING);
213                             if (cranges == NULL)
214                               einfo
215                                 (_("%P%E%F: Can't make .cranges section\n"));
216                           }
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       }
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;
234     break_2:
235       ;
236     }
239 /* Size up and extend the .cranges section, merging generated entries.  */
241 static void
242 sh64_elf_${EMULATION_NAME}_after_allocation (void)
244   bfd_vma new_cranges = 0;
245   bfd_vma cranges_growth = 0;
246   asection *osec;
247   bfd_byte *crangesp;
248   asection *cranges;
250   gld${EMULATION_NAME}_after_allocation ();
252   cranges = bfd_get_section_by_name (link_info.output_bfd,
253                                      SH64_CRANGES_SECTION_NAME);
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;
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 = link_info.output_bfd->sections;
266        osec != NULL;
267        osec = osec->next)
268     {
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 (link_info.output_bfd, osec) & SEC_EXCLUDE)
274         continue;
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;
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       }
301     break_1:
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;
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));
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;
333                         BFD_ASSERT (sh64_elf_section_data (osec)->sh64_info);
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       }
345     break_2:
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;
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;
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     }
380   if (cranges->contents != NULL)
381     free (cranges->contents);
383   BFD_ASSERT (sh64_elf_section_data (cranges)->sh64_info != NULL);
385   /* Make sure we have .cranges in memory even if there were only
386      assembler-generated .cranges.  */
387   cranges_growth = new_cranges * SH64_CRANGE_SIZE;
388   cranges->contents = xcalloc (cranges->size + cranges_growth, 1);
389   bfd_set_section_flags (cranges->owner, cranges,
390                          bfd_get_section_flags (cranges->owner, cranges)
391                          | SEC_IN_MEMORY);
393   /* If we don't need to grow the .cranges section beyond what was in the
394      input sections, we have nothing more to do here.  We then only got
395      here because there was a .cranges section coming from input.  Zero
396      out the number of generated .cranges.  */
397   if (new_cranges == 0)
398     {
399       sh64_elf_section_data (cranges)->sh64_info->cranges_growth = 0;
400       return;
401     }
403   crangesp = cranges->contents + cranges->size;
405   /* Now pass over the sections again, and make reloc orders for the new
406      .cranges entries.  Constants are set as we go.  */
407   for (osec = link_info.output_bfd->sections;
408        osec != NULL;
409        osec = osec->next)
410     {
411       struct bfd_link_order *cr_addr_order = NULL;
412       enum sh64_elf_cr_type last_cr_type = CRT_NONE;
413       bfd_vma last_cr_size = 0;
414       bfd_vma continuation_vma = 0;
416       /* Omit excluded or garbage-collected sections, and output sections
417          which were not marked as needing further processing.  */
418       if ((bfd_get_section_flags (link_info.output_bfd, osec) & SEC_EXCLUDE) != 0
419           || (sh64_elf_section_data (osec)->sh64_info->contents_flags
420               != SHF_SH5_ISA32_MIXED))
421         continue;
423       {
424         LANG_FOR_EACH_INPUT_STATEMENT (f)
425           {
426             asection *isec;
428             for (isec = f->the_bfd->sections;
429                  isec != NULL;
430                  isec = isec->next)
431               {
432                 /* Allow only sections that have (at least initially) a
433                    non-zero size, and are not excluded, and are not marked
434                    as containing mixed data, thus already having .cranges
435                    entries.  */
436                 if (isec->output_section == osec
437                     && isec->size != 0
438                     && (bfd_get_section_flags (isec->owner, isec)
439                         & SEC_EXCLUDE) == 0
440                     && ((elf_section_data (isec)->this_hdr.sh_flags
441                          & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
442                         != SHF_SH5_ISA32_MIXED))
443                   {
444                     enum sh64_elf_cr_type cr_type;
445                     bfd_vma cr_size;
446                     bfd_vma isa_flags
447                       = (elf_section_data (isec)->this_hdr.sh_flags
448                          & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
450                     if (isa_flags == SHF_SH5_ISA32)
451                       cr_type = CRT_SH5_ISA32;
452                     else if ((bfd_get_section_flags (isec->owner, isec)
453                               & SEC_CODE) == 0)
454                       cr_type = CRT_DATA;
455                     else
456                       cr_type = CRT_SH5_ISA16;
458                     cr_size = isec->size;
460                     /* Sections can be empty, like .text in a file that
461                        only contains other sections.  Ranges shouldn't be
462                        emitted for them.  This can presumably happen after
463                        relaxing and is not be caught at the "raw size"
464                        test above.  */
465                     if (cr_size == 0)
466                       continue;
468                     /* See if this is a continuation of the previous range
469                        for the same output section.  If so, just change
470                        the size of the last range and continue.  */
471                     if (cr_type == last_cr_type
472                         && (continuation_vma
473                             == osec->vma + isec->output_offset))
474                       {
475                         last_cr_size += cr_size;
476                         bfd_put_32 (link_info.output_bfd, last_cr_size,
477                                     crangesp - SH64_CRANGE_SIZE
478                                     + SH64_CRANGE_CR_SIZE_OFFSET);
480                         continuation_vma += cr_size;
481                         continue;
482                       }
484                     /* If we emit relocatable contents, we need a
485                        relocation for the start address.  */
486                     if (link_info.relocatable || link_info.emitrelocations)
487                       {
488                         /* FIXME: We could perhaps use lang_add_reloc and
489                            friends here, but I'm not really sure that
490                            would leave us free to do some optimizations
491                            later.  */
492                         cr_addr_order
493                           = bfd_new_link_order (link_info.output_bfd, cranges);
495                         if (cr_addr_order == NULL)
496                           {
497                             einfo (_("%P%F: bfd_new_link_order failed\n"));
498                             return;
499                           }
501                         cr_addr_order->type = bfd_section_reloc_link_order;
502                         cr_addr_order->offset
503                           = (cranges->output_offset
504                              + crangesp + SH64_CRANGE_CR_ADDR_OFFSET
505                              - cranges->contents);
506                         cr_addr_order->size = 4;
507                         cr_addr_order->u.reloc.p
508                           = xmalloc (sizeof (struct bfd_link_order_reloc));
510                         cr_addr_order->u.reloc.p->reloc = BFD_RELOC_32;
511                         cr_addr_order->u.reloc.p->u.section = osec;
513                         /* Since SH, unlike normal RELA-targets, uses a
514                            "partial inplace" REL-like relocation for this,
515                            we put the addend in the contents and specify 0
516                            for the reloc.  */
517                         bfd_put_32 (link_info.output_bfd, isec->output_offset,
518                                     crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
519                         cr_addr_order->u.reloc.p->addend = 0;
520                       }
521                     else
522                       bfd_put_32 (link_info.output_bfd,
523                                   osec->vma + isec->output_offset,
524                                   crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
526                     /* If we could make a reloc for cr_size we would do
527                        it, but we would have to have a symbol for the size
528                        of the _input_ section and there's no way to
529                        generate that.  */
530                     bfd_put_32 (link_info.output_bfd, cr_size,
531                                 crangesp + SH64_CRANGE_CR_SIZE_OFFSET);
533                     bfd_put_16 (link_info.output_bfd, cr_type,
534                                 crangesp + SH64_CRANGE_CR_TYPE_OFFSET);
536                     last_cr_type = cr_type;
537                     last_cr_size = cr_size;
538                     continuation_vma
539                       = osec->vma + isec->output_offset + cr_size;
540                     crangesp += SH64_CRANGE_SIZE;
541                   }
542               }
543           }
544       }
545     }
547   /* The .cranges section will have this size, no larger or smaller.
548      Since relocs (if relocatable linking) will be emitted into the
549      "extended" size, we must set the raw size to the total.  We have to
550      keep track of the number of new .cranges entries.
552      Sorting before writing is done by sh64_elf_final_write_processing.  */
554   sh64_elf_section_data (cranges)->sh64_info->cranges_growth
555     = crangesp - cranges->contents - cranges->size;
556   cranges->size = crangesp - cranges->contents;
557   cranges->rawsize = cranges->size;