PR binutils/4797
[deliverable/binutils-gdb.git] / ld / emultempl / sh64elf.em
CommitLineData
2d92cb7d 1# This shell script emits a C file. -*- C -*-
f96b4a7b 2# Copyright 2000, 2001, 2002, 2003, 2004, 2007 Free Software Foundation, Inc.
2d92cb7d 3#
f96b4a7b 4# This file is part of the GNU Binutils.
2d92cb7d
AO
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
f96b4a7b 8# the Free Software Foundation; either version 3 of the License, or
2d92cb7d
AO
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
f96b4a7b
NC
18# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19# MA 02110-1301, USA.
2d92cb7d
AO
20#
21
22# This file is sourced from elf32.em, and defines extra sh64
23# specific routines.
24#
25
26LDEMUL_AFTER_ALLOCATION=sh64_elf_${EMULATION_NAME}_after_allocation
27LDEMUL_BEFORE_ALLOCATION=sh64_elf_${EMULATION_NAME}_before_allocation
28
29cat >>e${EMULATION_NAME}.c <<EOF
30
31#include "libiberty.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
34#include "elf/sh.h"
f0abc2a1 35#include "elf32-sh64.h"
2d92cb7d 36
2d92cb7d
AO
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
47static void
0c7a8e5a 48sh64_elf_${EMULATION_NAME}_before_allocation (void)
2d92cb7d
AO
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 (output_bfd, SH64_CRANGES_SECTION_NAME);
57
58 if (cranges != NULL)
59 {
60 if (command_line.relax)
61 {
62 /* FIXME: Look through incoming sections with .cranges
63 descriptors, build up some kind of descriptors that the
64 relaxing function will pick up and adjust, or perhaps make it
65 find and adjust an associated .cranges descriptor. We could
66 also look through incoming relocs and kill the ones marking
67 relaxation areas, but that wouldn't be TRT. */
68 einfo
69 (_("%P: Sorry, turning off relaxing: .cranges section in input.\n"));
70 einfo (_(" A .cranges section is present in:\n"));
71
72 {
73 LANG_FOR_EACH_INPUT_STATEMENT (f)
74 {
75 asection *input_cranges
76 = bfd_get_section_by_name (f->the_bfd,
77 SH64_CRANGES_SECTION_NAME);
78 if (input_cranges != NULL)
79 einfo (" %I\n", f);
80 }
81 }
82
b34976b6 83 command_line.relax = FALSE;
2d92cb7d
AO
84 }
85
86 /* We wouldn't need to do anything when there's already a .cranges
87 section (and have a return here), except that we need to set the
88 section flags right for output sections that *don't* need a
89 .cranges section. */
90 }
91
92 if (command_line.relax)
93 {
94 LANG_FOR_EACH_INPUT_STATEMENT (f)
95 {
96 if (bfd_get_flavour (f->the_bfd) == bfd_target_elf_flavour)
97 {
98 asection *isec;
99 for (isec = f->the_bfd->sections;
100 isec != NULL;
101 isec = isec->next)
102 {
103 if (elf_section_data (isec)->this_hdr.sh_flags
104 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
105 {
106 einfo (_("%P: Sorry, turning off relaxing: SHmedia sections present.\n"));
107 einfo (" %I\n", f);
b34976b6 108 command_line.relax = FALSE;
2d92cb7d
AO
109 goto done_scanning_shmedia_sections;
110 }
111 }
112 }
113 }
114 }
115 done_scanning_shmedia_sections:
116
117 /* For each non-empty input section in each output section, check if it
118 has the same SH64-specific flags. If some input section differs, we
119 need a .cranges section. */
120 for (osec = output_bfd->sections;
121 osec != NULL;
122 osec = osec->next)
123 {
124 struct sh64_section_data *sh64_sec_data;
125 bfd_vma oflags_isa = 0;
126 bfd_vma iflags_isa = 0;
127
128 if (bfd_get_flavour (output_bfd) != bfd_target_elf_flavour)
129 einfo (_("%FError: non-ELF output formats are not supported by this target's linker.\n"));
130
f0abc2a1 131 sh64_sec_data = sh64_elf_section_data (osec)->sh64_info;
2d92cb7d
AO
132
133 /* Omit excluded or garbage-collected sections. */
134 if (bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE)
135 continue;
136
137 /* Make sure we have the target section data initialized. */
138 if (sh64_sec_data == NULL)
139 {
140 sh64_sec_data = xcalloc (1, sizeof (struct sh64_section_data));
f0abc2a1 141 sh64_elf_section_data (osec)->sh64_info = sh64_sec_data;
2d92cb7d
AO
142 }
143
144 /* First find an input section so we have flags to compare with; the
145 flags in the output section are not valid. */
146 {
147 LANG_FOR_EACH_INPUT_STATEMENT (f)
148 {
149 asection *isec;
150
151 for (isec = f->the_bfd->sections;
152 isec != NULL;
153 isec = isec->next)
154 {
155 if (isec->output_section == osec
eea6121a 156 && isec->size != 0
2d92cb7d
AO
157 && (bfd_get_section_flags (isec->owner, isec)
158 & SEC_EXCLUDE) == 0)
159 {
160 oflags_isa
161 = (elf_section_data (isec)->this_hdr.sh_flags
162 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
163 goto break_1;
164 }
165 }
166 }
167 }
168
169 break_1:
170
171 /* Check that all input sections have the same contents-type flags
172 as the first input section. */
173 {
174 LANG_FOR_EACH_INPUT_STATEMENT (f)
175 {
176 asection *isec;
177
178 for (isec = f->the_bfd->sections;
179 isec != NULL;
180 isec = isec->next)
181 {
182 if (isec->output_section == osec
eea6121a 183 && isec->size != 0
2d92cb7d
AO
184 && (bfd_get_section_flags (isec->owner, isec)
185 & SEC_EXCLUDE) == 0)
186 {
187 iflags_isa
188 = (elf_section_data (isec)->this_hdr.sh_flags
189 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
190
191 /* If flags don't agree, we need a .cranges section.
192 Create it here if it did not exist through input
193 sections. */
194 if (iflags_isa != oflags_isa)
195 {
196 if (cranges == NULL)
197 {
198 /* This section will be *appended* to
199 sections, so the outer iteration will reach
200 it in due time and set
201 sh64_elf_section_data; no need to set it
202 specifically here. */
203 cranges
204 = bfd_make_section (output_bfd,
205 SH64_CRANGES_SECTION_NAME);
206 if (cranges == NULL
b34976b6 207 || !bfd_set_section_flags (output_bfd,
2d92cb7d
AO
208 cranges,
209 SEC_LINKER_CREATED
210 | SEC_KEEP
211 | SEC_HAS_CONTENTS
b34976b6 212 | SEC_DEBUGGING))
2d92cb7d
AO
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
240static void
0c7a8e5a 241sh64_elf_${EMULATION_NAME}_after_allocation (void)
2d92cb7d
AO
242{
243 bfd_vma new_cranges = 0;
244 bfd_vma cranges_growth = 0;
245 asection *osec;
246 bfd_byte *crangesp;
247
248 asection *cranges
249 = bfd_get_section_by_name (output_bfd, SH64_CRANGES_SECTION_NAME);
250
251 /* If this ever starts doing something, we will pick it up. */
252 after_allocation_default ();
253
254 /* If there is no .cranges section, it is because it was seen earlier on
255 that none was needed. Otherwise it must have been created then, or
256 be present in input. */
257 if (cranges == NULL)
258 return;
259
260 /* First, we set the ISA flags for each output section according to the
261 first non-discarded section. For each input section in osec, we
262 check if it has the same flags. If it does not, we set flags to mark
263 a mixed section (and exit the loop early). */
264 for (osec = output_bfd->sections;
265 osec != NULL;
266 osec = osec->next)
267 {
268 bfd_vma oflags_isa = 0;
b34976b6 269 bfd_boolean need_check_cranges = FALSE;
2d92cb7d
AO
270
271 /* Omit excluded or garbage-collected sections. */
272 if (bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE)
273 continue;
274
275 /* First find an input section so we have flags to compare with; the
276 flags in the output section are not valid. */
277 {
278 LANG_FOR_EACH_INPUT_STATEMENT (f)
279 {
280 asection *isec;
281
282 for (isec = f->the_bfd->sections;
283 isec != NULL;
284 isec = isec->next)
285 {
286 if (isec->output_section == osec
eea6121a 287 && isec->size != 0
2d92cb7d
AO
288 && (bfd_get_section_flags (isec->owner, isec)
289 & SEC_EXCLUDE) == 0)
290 {
291 oflags_isa
292 = (elf_section_data (isec)->this_hdr.sh_flags
293 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
294 goto break_1;
295 }
296 }
297 }
298 }
299
300 break_1:
301
302 /* Check that all input sections have the same contents-type flags
303 as the first input section. */
304 {
305 LANG_FOR_EACH_INPUT_STATEMENT (f)
306 {
307 asection *isec;
308
309 for (isec = f->the_bfd->sections;
310 isec != NULL;
311 isec = isec->next)
312 {
313 if (isec->output_section == osec
eea6121a 314 && isec->size != 0
2d92cb7d
AO
315 && (bfd_get_section_flags (isec->owner, isec)
316 & SEC_EXCLUDE) == 0)
317 {
318 bfd_vma iflags_isa
319 = (elf_section_data (isec)->this_hdr.sh_flags
320 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
321
322 /* If flags don't agree, set the target-specific data
323 of the section to mark that this section needs to
324 be have .cranges section entries added. Don't
325 bother setting ELF section flags in output section;
326 they will be cleared later and will have to be
327 re-initialized before the linked file is written. */
328 if (iflags_isa != oflags_isa)
329 {
330 oflags_isa = SHF_SH5_ISA32_MIXED;
331
f0abc2a1 332 BFD_ASSERT (sh64_elf_section_data (osec)->sh64_info);
2d92cb7d 333
f0abc2a1 334 sh64_elf_section_data (osec)->sh64_info->contents_flags
2d92cb7d 335 = SHF_SH5_ISA32_MIXED;
b34976b6 336 need_check_cranges = TRUE;
2d92cb7d
AO
337 goto break_2;
338 }
339 }
340 }
341 }
342 }
343
344 break_2:
345
346 /* If there were no new ranges for this output section, we don't
347 need to iterate over the input sections to check how many are
348 needed. */
349 if (! need_check_cranges)
350 continue;
351
352 /* If we found a section with differing contents type, we need more
353 ranges to mark the sections that are not mixed (and already have
354 .cranges descriptors). Calculate the maximum number of new
355 entries here. We may merge some of them, so that number is not
356 final; it can shrink. */
357 {
358 LANG_FOR_EACH_INPUT_STATEMENT (f)
359 {
360 asection *isec;
361
362 for (isec = f->the_bfd->sections;
363 isec != NULL;
364 isec = isec->next)
365 {
366 if (isec->output_section == osec
eea6121a 367 && isec->size != 0
2d92cb7d
AO
368 && (bfd_get_section_flags (isec->owner, isec)
369 & SEC_EXCLUDE) == 0
370 && ((elf_section_data (isec)->this_hdr.sh_flags
371 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
372 != SHF_SH5_ISA32_MIXED))
373 new_cranges++;
374 }
375 }
376 }
377 }
378
fb319d8d
JR
379 /* ldemul_after_allocation may be called twice. First directly from
380 lang_process, and the second time when lang_process calls ldemul_finish,
381 which calls gld${EMULATION_NAME}_finish, e.g. gldshelf32_finish, which
382 is defined in emultempl/elf32.em and calls ldemul_after_allocation,
7a945db6 383 if bfd_elf_discard_info returned true. */
fb319d8d
JR
384 if (cranges->contents != NULL)
385 free (cranges->contents);
386
f0abc2a1 387 BFD_ASSERT (sh64_elf_section_data (cranges)->sh64_info != NULL);
2d92cb7d
AO
388
389 /* Make sure we have .cranges in memory even if there were only
390 assembler-generated .cranges. */
391 cranges_growth = new_cranges * SH64_CRANGE_SIZE;
eea6121a 392 cranges->contents = xcalloc (cranges->size + cranges_growth, 1);
2d92cb7d
AO
393 bfd_set_section_flags (cranges->owner, cranges,
394 bfd_get_section_flags (cranges->owner, cranges)
395 | SEC_IN_MEMORY);
396
397 /* If we don't need to grow the .cranges section beyond what was in the
398 input sections, we have nothing more to do here. We then only got
399 here because there was a .cranges section coming from input. Zero
400 out the number of generated .cranges. */
401 if (new_cranges == 0)
402 {
f0abc2a1 403 sh64_elf_section_data (cranges)->sh64_info->cranges_growth = 0;
2d92cb7d
AO
404 return;
405 }
406
eea6121a 407 crangesp = cranges->contents + cranges->size;
2d92cb7d
AO
408
409 /* Now pass over the sections again, and make reloc orders for the new
410 .cranges entries. Constants are set as we go. */
411 for (osec = output_bfd->sections;
412 osec != NULL;
413 osec = osec->next)
414 {
415 struct bfd_link_order *cr_addr_order = NULL;
416 enum sh64_elf_cr_type last_cr_type = CRT_NONE;
417 bfd_vma last_cr_size = 0;
418 bfd_vma continuation_vma = 0;
419
420 /* Omit excluded or garbage-collected sections, and output sections
421 which were not marked as needing further processing. */
422 if ((bfd_get_section_flags (output_bfd, osec) & SEC_EXCLUDE) != 0
f0abc2a1 423 || (sh64_elf_section_data (osec)->sh64_info->contents_flags
2d92cb7d
AO
424 != SHF_SH5_ISA32_MIXED))
425 continue;
426
427 {
428 LANG_FOR_EACH_INPUT_STATEMENT (f)
429 {
430 asection *isec;
431
432 for (isec = f->the_bfd->sections;
433 isec != NULL;
434 isec = isec->next)
435 {
436 /* Allow only sections that have (at least initially) a
437 non-zero size, and are not excluded, and are not marked
438 as containing mixed data, thus already having .cranges
439 entries. */
440 if (isec->output_section == osec
eea6121a 441 && isec->size != 0
2d92cb7d
AO
442 && (bfd_get_section_flags (isec->owner, isec)
443 & SEC_EXCLUDE) == 0
444 && ((elf_section_data (isec)->this_hdr.sh_flags
445 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
446 != SHF_SH5_ISA32_MIXED))
447 {
448 enum sh64_elf_cr_type cr_type;
449 bfd_vma cr_size;
450 bfd_vma isa_flags
451 = (elf_section_data (isec)->this_hdr.sh_flags
452 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
453
454 if (isa_flags == SHF_SH5_ISA32)
455 cr_type = CRT_SH5_ISA32;
456 else if ((bfd_get_section_flags (isec->owner, isec)
457 & SEC_CODE) == 0)
458 cr_type = CRT_DATA;
459 else
460 cr_type = CRT_SH5_ISA16;
461
eea6121a 462 cr_size = isec->size;
2d92cb7d
AO
463
464 /* Sections can be empty, like .text in a file that
465 only contains other sections. Ranges shouldn't be
466 emitted for them. This can presumably happen after
467 relaxing and is not be caught at the "raw size"
468 test above. */
469 if (cr_size == 0)
470 continue;
471
472 /* See if this is a continuation of the previous range
473 for the same output section. If so, just change
474 the size of the last range and continue. */
475 if (cr_type == last_cr_type
476 && (continuation_vma
477 == osec->vma + isec->output_offset))
478 {
479 last_cr_size += cr_size;
480 bfd_put_32 (output_bfd, last_cr_size,
481 crangesp - SH64_CRANGE_SIZE
482 + SH64_CRANGE_CR_SIZE_OFFSET);
483
484 continuation_vma += cr_size;
485 continue;
486 }
487
1049f94e 488 /* If we emit relocatable contents, we need a
2d92cb7d 489 relocation for the start address. */
1049f94e 490 if (link_info.relocatable || link_info.emitrelocations)
2d92cb7d
AO
491 {
492 /* FIXME: We could perhaps use lang_add_reloc and
493 friends here, but I'm not really sure that
494 would leave us free to do some optimizations
495 later. */
496 cr_addr_order
497 = bfd_new_link_order (output_bfd, cranges);
498
499 if (cr_addr_order == NULL)
500 {
501 einfo (_("%P%F: bfd_new_link_order failed\n"));
502 return;
503 }
504
505 cr_addr_order->type = bfd_section_reloc_link_order;
506 cr_addr_order->offset
507 = (cranges->output_offset
508 + crangesp + SH64_CRANGE_CR_ADDR_OFFSET
509 - cranges->contents);
510 cr_addr_order->size = 4;
511 cr_addr_order->u.reloc.p
0c7a8e5a 512 = xmalloc (sizeof (struct bfd_link_order_reloc));
2d92cb7d
AO
513
514 cr_addr_order->u.reloc.p->reloc = BFD_RELOC_32;
515 cr_addr_order->u.reloc.p->u.section = osec;
516
517 /* Since SH, unlike normal RELA-targets, uses a
518 "partial inplace" REL-like relocation for this,
519 we put the addend in the contents and specify 0
520 for the reloc. */
521 bfd_put_32 (output_bfd, isec->output_offset,
522 crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
523 cr_addr_order->u.reloc.p->addend = 0;
2d92cb7d
AO
524 }
525 else
526 bfd_put_32 (output_bfd,
527 osec->vma + isec->output_offset,
528 crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
529
530 /* If we could make a reloc for cr_size we would do
531 it, but we would have to have a symbol for the size
532 of the _input_ section and there's no way to
533 generate that. */
534 bfd_put_32 (output_bfd, cr_size,
535 crangesp + SH64_CRANGE_CR_SIZE_OFFSET);
536
0c7a8e5a 537 bfd_put_16 (output_bfd, cr_type,
2d92cb7d
AO
538 crangesp + SH64_CRANGE_CR_TYPE_OFFSET);
539
540 last_cr_type = cr_type;
541 last_cr_size = cr_size;
542 continuation_vma
543 = osec->vma + isec->output_offset + cr_size;
544 crangesp += SH64_CRANGE_SIZE;
545 }
546 }
547 }
548 }
549 }
550
551 /* The .cranges section will have this size, no larger or smaller.
1049f94e 552 Since relocs (if relocatable linking) will be emitted into the
2d92cb7d
AO
553 "extended" size, we must set the raw size to the total. We have to
554 keep track of the number of new .cranges entries.
555
556 Sorting before writing is done by sh64_elf_final_write_processing. */
557
f0abc2a1 558 sh64_elf_section_data (cranges)->sh64_info->cranges_growth
eea6121a
AM
559 = crangesp - cranges->contents - cranges->size;
560 cranges->size = crangesp - cranges->contents;
24dc748d 561 cranges->rawsize = cranges->size;
2d92cb7d 562}
This page took 0.32875 seconds and 4 git commands to generate.