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