1# This shell script emits a C file. -*- C -*-
2#   Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3#
4# This file is part of GLD, the Gnu Linker.
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 2 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, MA 02110-1301, USA.
19#
20
21# This file is sourced from elf32.em, and defines extra sh64
22# specific routines.
23#
24
25LDEMUL_AFTER_ALLOCATION=sh64_elf_${EMULATION_NAME}_after_allocation
26LDEMUL_BEFORE_ALLOCATION=sh64_elf_${EMULATION_NAME}_before_allocation
27
28cat >>e${EMULATION_NAME}.c <<EOF
29
30#include "libiberty.h"
31#include "libbfd.h"
32#include "elf-bfd.h"
33#include "elf/sh.h"
34#include "elf32-sh64.h"
35
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
39   the output.
40
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.  */
45
46static void
47sh64_elf_${EMULATION_NAME}_before_allocation (void)
48{
49  asection *cranges;
50  asection *osec;
51
52  /* Call main function; we're just extending it.  */
53  gld${EMULATION_NAME}_before_allocation ();
54
55  cranges = bfd_get_section_by_name (output_bfd, SH64_CRANGES_SECTION_NAME);
56
57  if (cranges != NULL)
58    {
59      if (command_line.relax)
60	{
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.  */
67	  einfo
68	    (_("%P: Sorry, turning off relaxing: .cranges section in input.\n"));
69	  einfo (_(" A .cranges section is present in:\n"));
70
71	  {
72	    LANG_FOR_EACH_INPUT_STATEMENT (f)
73	      {
74		asection *input_cranges
75		  = bfd_get_section_by_name (f->the_bfd,
76					     SH64_CRANGES_SECTION_NAME);
77		if (input_cranges != NULL)
78		  einfo (" %I\n", f);
79	      }
80	  }
81
82	  command_line.relax = FALSE;
83	}
84
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
88	 .cranges section.  */
89    }
90
91  if (command_line.relax)
92    {
93      LANG_FOR_EACH_INPUT_STATEMENT (f)
94	{
95	  if (bfd_get_flavour (f->the_bfd) == bfd_target_elf_flavour)
96	    {
97	      asection *isec;
98	      for (isec = f->the_bfd->sections;
99		   isec != NULL;
100		   isec = isec->next)
101		{
102		  if (elf_section_data (isec)->this_hdr.sh_flags
103		      & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
104		    {
105		      einfo (_("%P: Sorry, turning off relaxing: SHmedia sections present.\n"));
106		      einfo ("  %I\n", f);
107		      command_line.relax = FALSE;
108		      goto done_scanning_shmedia_sections;
109		    }
110		}
111	    }
112	}
113    }
114 done_scanning_shmedia_sections:
115
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;
120       osec != NULL;
121       osec = osec->next)
122    {
123      struct sh64_section_data *sh64_sec_data;
124      bfd_vma oflags_isa = 0;
125      bfd_vma iflags_isa = 0;
126
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"));
129
130      sh64_sec_data = sh64_elf_section_data (osec)->sh64_info;
131
132      /* Omit excluded or garbage-collected sections.  */
133      if (bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE)
134	continue;
135
136      /* Make sure we have the target section data initialized.  */
137      if (sh64_sec_data == NULL)
138	{
139	  sh64_sec_data = xcalloc (1, sizeof (struct sh64_section_data));
140	  sh64_elf_section_data (osec)->sh64_info = sh64_sec_data;
141	}
142
143      /* First find an input section so we have flags to compare with; the
144	 flags in the output section are not valid.  */
145      {
146	LANG_FOR_EACH_INPUT_STATEMENT (f)
147	  {
148	    asection *isec;
149
150	    for (isec = f->the_bfd->sections;
151		 isec != NULL;
152		 isec = isec->next)
153	      {
154		if (isec->output_section == osec
155		    && isec->size != 0
156		    && (bfd_get_section_flags (isec->owner, isec)
157			& SEC_EXCLUDE) == 0)
158		  {
159		    oflags_isa
160		      = (elf_section_data (isec)->this_hdr.sh_flags
161			 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
162		    goto break_1;
163		  }
164	      }
165	  }
166      }
167
168    break_1:
169
170      /* Check that all input sections have the same contents-type flags
171         as the first input section.  */
172      {
173	LANG_FOR_EACH_INPUT_STATEMENT (f)
174	  {
175	    asection *isec;
176
177	    for (isec = f->the_bfd->sections;
178		 isec != NULL;
179		 isec = isec->next)
180	      {
181		if (isec->output_section == osec
182		    && isec->size != 0
183		    && (bfd_get_section_flags (isec->owner, isec)
184			& SEC_EXCLUDE) == 0)
185		  {
186		    iflags_isa
187		      = (elf_section_data (isec)->this_hdr.sh_flags
188			 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
189
190		    /* If flags don't agree, we need a .cranges section.
191		       Create it here if it did not exist through input
192		       sections.  */
193		    if (iflags_isa != oflags_isa)
194		      {
195			if (cranges == NULL)
196			  {
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.  */
202			    cranges
203			      = bfd_make_section (output_bfd,
204						  SH64_CRANGES_SECTION_NAME);
205			    if (cranges == NULL
206				|| !bfd_set_section_flags (output_bfd,
207							   cranges,
208							   SEC_LINKER_CREATED
209							   | SEC_KEEP
210							   | SEC_HAS_CONTENTS
211							   | SEC_DEBUGGING))
212			      einfo
213				(_("%P%E%F: Can't make .cranges section\n"));
214			  }
215
216			/* We don't need to look at more input sections,
217			   and we know this section will have mixed
218			   contents.  */
219			goto break_2;
220		      }
221		  }
222	      }
223	  }
224      }
225
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;
231
232    break_2:
233      ;
234    }
235}
236
237/* Size up and extend the .cranges section, merging generated entries.  */
238
239static void
240sh64_elf_${EMULATION_NAME}_after_allocation (void)
241{
242  bfd_vma new_cranges = 0;
243  bfd_vma cranges_growth = 0;
244  asection *osec;
245  bfd_byte *crangesp;
246
247  asection *cranges
248    = bfd_get_section_by_name (output_bfd, SH64_CRANGES_SECTION_NAME);
249
250  /* If this ever starts doing something, we will pick it up.  */
251  after_allocation_default ();
252
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.  */
256  if (cranges == NULL)
257    return;
258
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;
264       osec != NULL;
265       osec = osec->next)
266    {
267      bfd_vma oflags_isa = 0;
268      bfd_boolean need_check_cranges = FALSE;
269
270      /* Omit excluded or garbage-collected sections.  */
271      if (bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE)
272	continue;
273
274      /* First find an input section so we have flags to compare with; the
275	 flags in the output section are not valid.  */
276      {
277	LANG_FOR_EACH_INPUT_STATEMENT (f)
278	  {
279	    asection *isec;
280
281	    for (isec = f->the_bfd->sections;
282		 isec != NULL;
283		 isec = isec->next)
284	      {
285		if (isec->output_section == osec
286		    && isec->size != 0
287		    && (bfd_get_section_flags (isec->owner, isec)
288			& SEC_EXCLUDE) == 0)
289		  {
290		    oflags_isa
291		      = (elf_section_data (isec)->this_hdr.sh_flags
292			 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
293		    goto break_1;
294		  }
295	      }
296	  }
297      }
298
299    break_1:
300
301      /* Check that all input sections have the same contents-type flags
302         as the first input section.  */
303      {
304	LANG_FOR_EACH_INPUT_STATEMENT (f)
305	  {
306	    asection *isec;
307
308	    for (isec = f->the_bfd->sections;
309		 isec != NULL;
310		 isec = isec->next)
311	      {
312		if (isec->output_section == osec
313		    && isec->size != 0
314		    && (bfd_get_section_flags (isec->owner, isec)
315			& SEC_EXCLUDE) == 0)
316		  {
317		    bfd_vma iflags_isa
318		      = (elf_section_data (isec)->this_hdr.sh_flags
319			 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
320
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)
328		      {
329			oflags_isa = SHF_SH5_ISA32_MIXED;
330
331			BFD_ASSERT (sh64_elf_section_data (osec)->sh64_info);
332
333			sh64_elf_section_data (osec)->sh64_info->contents_flags
334			  = SHF_SH5_ISA32_MIXED;
335			need_check_cranges = TRUE;
336			goto break_2;
337		      }
338		  }
339	      }
340	  }
341      }
342
343    break_2:
344
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
347	 needed.  */
348      if (! need_check_cranges)
349	continue;
350
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.  */
356      {
357	LANG_FOR_EACH_INPUT_STATEMENT (f)
358	  {
359	    asection *isec;
360
361	    for (isec = f->the_bfd->sections;
362		 isec != NULL;
363		 isec = isec->next)
364	      {
365		if (isec->output_section == osec
366		    && isec->size != 0
367		    && (bfd_get_section_flags (isec->owner, isec)
368			& SEC_EXCLUDE) == 0
369		    && ((elf_section_data (isec)->this_hdr.sh_flags
370			 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
371			!= SHF_SH5_ISA32_MIXED))
372		  new_cranges++;
373	      }
374	  }
375      }
376    }
377
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);
385
386  BFD_ASSERT (sh64_elf_section_data (cranges)->sh64_info != NULL);
387
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->size + cranges_growth, 1);
392  bfd_set_section_flags (cranges->owner, cranges,
393			 bfd_get_section_flags (cranges->owner, cranges)
394			 | SEC_IN_MEMORY);
395
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)
401    {
402      sh64_elf_section_data (cranges)->sh64_info->cranges_growth = 0;
403      return;
404    }
405
406  crangesp = cranges->contents + cranges->size;
407
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;
411       osec != NULL;
412       osec = osec->next)
413    {
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;
418
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))
424	continue;
425
426      {
427	LANG_FOR_EACH_INPUT_STATEMENT (f)
428	  {
429	    asection *isec;
430
431	    for (isec = f->the_bfd->sections;
432		 isec != NULL;
433		 isec = isec->next)
434	      {
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
438		   entries.  */
439		if (isec->output_section == osec
440		    && isec->size != 0
441		    && (bfd_get_section_flags (isec->owner, isec)
442			& SEC_EXCLUDE) == 0
443		    && ((elf_section_data (isec)->this_hdr.sh_flags
444			 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
445			!= SHF_SH5_ISA32_MIXED))
446		  {
447		    enum sh64_elf_cr_type cr_type;
448		    bfd_vma cr_size;
449		    bfd_vma isa_flags
450		      = (elf_section_data (isec)->this_hdr.sh_flags
451			 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
452
453		    if (isa_flags == SHF_SH5_ISA32)
454		      cr_type = CRT_SH5_ISA32;
455		    else if ((bfd_get_section_flags (isec->owner, isec)
456			      & SEC_CODE) == 0)
457		      cr_type = CRT_DATA;
458		    else
459		      cr_type = CRT_SH5_ISA16;
460
461		    cr_size = isec->size;
462
463		    /* Sections can be empty, like .text in a file that
464		       only contains other sections.  Ranges shouldn't be
465		       emitted for them.  This can presumably happen after
466		       relaxing and is not be caught at the "raw size"
467		       test above.  */
468		    if (cr_size == 0)
469		      continue;
470
471		    /* See if this is a continuation of the previous range
472		       for the same output section.  If so, just change
473		       the size of the last range and continue.  */
474		    if (cr_type == last_cr_type
475			&& (continuation_vma
476			    == osec->vma + isec->output_offset))
477		      {
478			last_cr_size += cr_size;
479			bfd_put_32 (output_bfd, last_cr_size,
480				    crangesp - SH64_CRANGE_SIZE
481				    + SH64_CRANGE_CR_SIZE_OFFSET);
482
483			continuation_vma += cr_size;
484			continue;
485		      }
486
487		    /* If we emit relocatable contents, we need a
488		       relocation for the start address.  */
489		    if (link_info.relocatable || link_info.emitrelocations)
490		      {
491			/* FIXME: We could perhaps use lang_add_reloc and
492			   friends here, but I'm not really sure that
493			   would leave us free to do some optimizations
494			   later.  */
495			cr_addr_order
496			  = bfd_new_link_order (output_bfd, cranges);
497
498			if (cr_addr_order == NULL)
499			  {
500			    einfo (_("%P%F: bfd_new_link_order failed\n"));
501			    return;
502			  }
503
504			cr_addr_order->type = bfd_section_reloc_link_order;
505			cr_addr_order->offset
506			  = (cranges->output_offset
507			     + crangesp + SH64_CRANGE_CR_ADDR_OFFSET
508			     - cranges->contents);
509			cr_addr_order->size = 4;
510			cr_addr_order->u.reloc.p
511			  = xmalloc (sizeof (struct bfd_link_order_reloc));
512
513			cr_addr_order->u.reloc.p->reloc = BFD_RELOC_32;
514			cr_addr_order->u.reloc.p->u.section = osec;
515
516			/* Since SH, unlike normal RELA-targets, uses a
517			   "partial inplace" REL-like relocation for this,
518			   we put the addend in the contents and specify 0
519			   for the reloc.  */
520			bfd_put_32 (output_bfd, isec->output_offset,
521				    crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
522			cr_addr_order->u.reloc.p->addend = 0;
523		      }
524		    else
525		      bfd_put_32 (output_bfd,
526				  osec->vma + isec->output_offset,
527				  crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
528
529		    /* If we could make a reloc for cr_size we would do
530		       it, but we would have to have a symbol for the size
531		       of the _input_ section and there's no way to
532		       generate that.  */
533		    bfd_put_32 (output_bfd, cr_size,
534				crangesp + SH64_CRANGE_CR_SIZE_OFFSET);
535
536		    bfd_put_16 (output_bfd, cr_type,
537				crangesp + SH64_CRANGE_CR_TYPE_OFFSET);
538
539		    last_cr_type = cr_type;
540		    last_cr_size = cr_size;
541		    continuation_vma
542		      = osec->vma + isec->output_offset + cr_size;
543		    crangesp += SH64_CRANGE_SIZE;
544		  }
545	      }
546	  }
547      }
548    }
549
550  /* The .cranges section will have this size, no larger or smaller.
551     Since relocs (if relocatable linking) will be emitted into the
552     "extended" size, we must set the raw size to the total.  We have to
553     keep track of the number of new .cranges entries.
554
555     Sorting before writing is done by sh64_elf_final_write_processing.  */
556
557  sh64_elf_section_data (cranges)->sh64_info->cranges_growth
558    = crangesp - cranges->contents - cranges->size;
559  cranges->size = crangesp - cranges->contents;
560  cranges->rawsize = cranges->size;
561}
562