1 /* BFD back-end for Hitachi Super-H COFF binaries.
2 Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4 Written by Steve Chamberlain, <sac@cygnus.com>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "coff/internal.h"
31 /* Internal functions. */
32 static bfd_reloc_status_type sh_reloc
33 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
34 static long get_symbol_value PARAMS ((asymbol *));
35 static boolean sh_relax_section
36 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
37 static boolean sh_relax_delete_bytes
38 PARAMS ((bfd *, asection *, bfd_vma, int));
39 static boolean sh_relocate_section
40 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
41 struct internal_reloc *, struct internal_syment *, asection **));
42 static bfd_byte *sh_coff_get_relocated_section_contents
43 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
44 bfd_byte *, boolean, asymbol **));
46 /* Default section alignment to 2**2. */
47 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
49 /* Generate long file names. */
50 #define COFF_LONG_FILENAMES
52 /* The supported relocations. There are a lot of relocations defined
53 in coff/internal.h which we do not expect to ever see. */
54 static reloc_howto_type sh_coff_howtos[] =
59 { 3 }, /* R_SH_PCREL8 */
60 { 4 }, /* R_SH_PCREL16 */
61 { 5 }, /* R_SH_HIGH8 */
62 { 6 }, /* R_SH_IMM24 */
63 { 7 }, /* R_SH_LOW16 */
65 { 9 }, /* R_SH_PCDISP8BY4 */
67 HOWTO (R_SH_PCDISP8BY2, /* type */
69 1, /* size (0 = byte, 1 = short, 2 = long) */
71 true, /* pc_relative */
73 complain_overflow_signed, /* complain_on_overflow */
74 sh_reloc, /* special_function */
75 "r_pcdisp8by2", /* name */
76 true, /* partial_inplace */
79 true), /* pcrel_offset */
81 { 11 }, /* R_SH_PCDISP8 */
83 HOWTO (R_SH_PCDISP, /* type */
85 1, /* size (0 = byte, 1 = short, 2 = long) */
87 true, /* pc_relative */
89 complain_overflow_signed, /* complain_on_overflow */
90 sh_reloc, /* special_function */
91 "r_pcdisp12by2", /* name */
92 true, /* partial_inplace */
95 true), /* pcrel_offset */
99 HOWTO (R_SH_IMM32, /* type */
101 2, /* size (0 = byte, 1 = short, 2 = long) */
103 false, /* pc_relative */
105 complain_overflow_bitfield, /* complain_on_overflow */
106 sh_reloc, /* special_function */
107 "r_imm32", /* name */
108 true, /* partial_inplace */
109 0xffffffff, /* src_mask */
110 0xffffffff, /* dst_mask */
111 false), /* pcrel_offset */
114 { 16 }, /* R_SH_IMM8 */
115 { 17 }, /* R_SH_IMM8BY2 */
116 { 18 }, /* R_SH_IMM8BY4 */
117 { 19 }, /* R_SH_IMM4 */
118 { 20 }, /* R_SH_IMM4BY2 */
119 { 21 }, /* R_SH_IMM4BY4 */
121 HOWTO (R_SH_PCRELIMM8BY2, /* type */
123 1, /* size (0 = byte, 1 = short, 2 = long) */
125 true, /* pc_relative */
127 complain_overflow_unsigned, /* complain_on_overflow */
128 sh_reloc, /* special_function */
129 "r_pcrelimm8by2", /* name */
130 true, /* partial_inplace */
133 true), /* pcrel_offset */
135 HOWTO (R_SH_PCRELIMM8BY4, /* type */
137 1, /* size (0 = byte, 1 = short, 2 = long) */
139 true, /* pc_relative */
141 complain_overflow_unsigned, /* complain_on_overflow */
142 sh_reloc, /* special_function */
143 "r_pcrelimm8by4", /* name */
144 true, /* partial_inplace */
147 true), /* pcrel_offset */
149 HOWTO (R_SH_IMM16, /* type */
151 1, /* size (0 = byte, 1 = short, 2 = long) */
153 false, /* pc_relative */
155 complain_overflow_bitfield, /* complain_on_overflow */
156 sh_reloc, /* special_function */
157 "r_imm16", /* name */
158 true, /* partial_inplace */
159 0xffff, /* src_mask */
160 0xffff, /* dst_mask */
161 false), /* pcrel_offset */
163 HOWTO (R_SH_SWITCH16, /* type */
165 1, /* size (0 = byte, 1 = short, 2 = long) */
167 false, /* pc_relative */
169 complain_overflow_bitfield, /* complain_on_overflow */
170 sh_reloc, /* special_function */
171 "r_switch16", /* name */
172 true, /* partial_inplace */
173 0xffff, /* src_mask */
174 0xffff, /* dst_mask */
175 false), /* pcrel_offset */
177 HOWTO (R_SH_SWITCH32, /* type */
179 2, /* size (0 = byte, 1 = short, 2 = long) */
181 false, /* pc_relative */
183 complain_overflow_bitfield, /* complain_on_overflow */
184 sh_reloc, /* special_function */
185 "r_switch32", /* name */
186 true, /* partial_inplace */
187 0xffffffff, /* src_mask */
188 0xffffffff, /* dst_mask */
189 false), /* pcrel_offset */
191 HOWTO (R_SH_USES, /* type */
193 1, /* size (0 = byte, 1 = short, 2 = long) */
195 false, /* pc_relative */
197 complain_overflow_bitfield, /* complain_on_overflow */
198 sh_reloc, /* special_function */
200 true, /* partial_inplace */
201 0xffff, /* src_mask */
202 0xffff, /* dst_mask */
203 false), /* pcrel_offset */
205 HOWTO (R_SH_COUNT, /* type */
207 2, /* size (0 = byte, 1 = short, 2 = long) */
209 false, /* pc_relative */
211 complain_overflow_bitfield, /* complain_on_overflow */
212 sh_reloc, /* special_function */
213 "r_count", /* name */
214 true, /* partial_inplace */
215 0xffffffff, /* src_mask */
216 0xffffffff, /* dst_mask */
217 false), /* pcrel_offset */
219 HOWTO (R_SH_ALIGN, /* type */
221 2, /* size (0 = byte, 1 = short, 2 = long) */
223 false, /* pc_relative */
225 complain_overflow_bitfield, /* complain_on_overflow */
226 sh_reloc, /* special_function */
227 "r_align", /* name */
228 true, /* partial_inplace */
229 0xffffffff, /* src_mask */
230 0xffffffff, /* dst_mask */
231 false) /* pcrel_offset */
234 #define SH_COFF_HOWTO_COUNT (sizeof sh_coff_howtos / sizeof sh_coff_howtos[0])
236 /* Check for a bad magic number. */
237 #define BADMAG(x) SHBADMAG(x)
239 /* Customize coffcode.h (this is not currently used). */
242 /* FIXME: This should not be set here. */
243 #define __A_MAGIC_SET__
245 /* Swap the r_offset field in and out. */
246 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
247 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
249 /* Swap out extra information in the reloc structure. */
250 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
253 dst->r_stuff[0] = 'S'; \
254 dst->r_stuff[1] = 'C'; \
258 /* Get the value of a symbol, when performing a relocation. */
261 get_symbol_value (symbol)
266 if (bfd_is_com_section (symbol->section))
269 relocation = (symbol->value +
270 symbol->section->output_section->vma +
271 symbol->section->output_offset);
276 /* This macro is used in coffcode.h to get the howto corresponding to
277 an internal reloc. */
279 #define RTYPE2HOWTO(relent, internal) \
281 ((internal)->r_type < SH_COFF_HOWTO_COUNT \
282 ? &sh_coff_howtos[(internal)->r_type] \
283 : (reloc_howto_type *) NULL))
285 /* This is the same as the macro in coffcode.h, except that it copies
286 r_offset into reloc_entry->addend for some relocs. */
287 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
289 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
290 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
291 coffsym = (obj_symbols (abfd) \
292 + (cache_ptr->sym_ptr_ptr - symbols)); \
294 coffsym = coff_symbol_from (abfd, ptr); \
295 if (coffsym != (coff_symbol_type *) NULL \
296 && coffsym->native->u.syment.n_scnum == 0) \
297 cache_ptr->addend = 0; \
298 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
299 && ptr->section != (asection *) NULL) \
300 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
302 cache_ptr->addend = 0; \
303 if ((reloc).r_type == R_SH_SWITCH16 \
304 || (reloc).r_type == R_SH_SWITCH32 \
305 || (reloc).r_type == R_SH_USES \
306 || (reloc).r_type == R_SH_COUNT \
307 || (reloc).r_type == R_SH_ALIGN) \
308 cache_ptr->addend = (reloc).r_offset; \
311 /* This is the howto function for the SH relocations. */
313 static bfd_reloc_status_type
314 sh_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
317 arelent *reloc_entry;
320 asection *input_section;
322 char **error_message;
326 unsigned short r_type;
327 bfd_vma addr = reloc_entry->address;
328 bfd_byte *hit_data = addr + (bfd_byte *) data;
330 r_type = reloc_entry->howto->type;
332 if (output_bfd != NULL)
334 /* Partial linking--do nothing. */
335 reloc_entry->address += input_section->output_offset;
339 /* Almost all relocs have to do with relaxing. If any work must be
340 done for them, it has been done in sh_relax_section. */
341 if (r_type != R_SH_IMM32
342 && (r_type != R_SH_PCDISP
343 || (symbol_in->flags & BSF_LOCAL) != 0))
346 if (symbol_in != NULL
347 && bfd_is_und_section (symbol_in->section))
348 return bfd_reloc_undefined;
350 sym_value = get_symbol_value (symbol_in);
355 insn = bfd_get_32 (abfd, hit_data);
356 insn += sym_value + reloc_entry->addend;
357 bfd_put_32 (abfd, insn, hit_data);
360 insn = bfd_get_16 (abfd, hit_data);
361 sym_value += reloc_entry->addend;
362 sym_value -= (input_section->output_section->vma
363 + input_section->output_offset
366 sym_value += (insn & 0xfff) << 1;
369 insn = (insn & 0xf000) | (sym_value & 0xfff);
370 bfd_put_16 (abfd, insn, hit_data);
371 if (sym_value < -0x1000 || sym_value >= 0x1000)
372 return bfd_reloc_overflow;
382 /* We can do relaxing. */
383 #define coff_bfd_relax_section sh_relax_section
385 /* We use the special COFF backend linker. */
386 #define coff_relocate_section sh_relocate_section
388 /* When relaxing, we need to use special code to get the relocated
390 #define coff_bfd_get_relocated_section_contents \
391 sh_coff_get_relocated_section_contents
393 #include "coffcode.h"
395 /* This function handles relaxing on the SH.
397 Function calls on the SH look like this:
406 The compiler and assembler will cooperate to create R_SH_USES
407 relocs on the jsr instructions. The r_offset field of the
408 R_SH_USES reloc is the PC relative offset to the instruction which
409 loads the register (the r_offset field is computed as though it
410 were a jump instruction, so the offset value is actually from four
411 bytes past the instruction). The linker can use this reloc to
412 determine just which function is being called, and thus decide
413 whether it is possible to replace the jsr with a bsr.
415 If multiple function calls are all based on a single register load
416 (i.e., the same function is called multiple times), the compiler
417 guarantees that each function call will have an R_SH_USES reloc.
418 Therefore, if the linker is able to convert each R_SH_USES reloc
419 which refers to that address, it can safely eliminate the register
422 When the assembler creates an R_SH_USES reloc, it examines it to
423 determine which address is being loaded (L1 in the above example).
424 It then counts the number of references to that address, and
425 creates an R_SH_COUNT reloc at that address. The r_offset field of
426 the R_SH_COUNT reloc will be the number of references. If the
427 linker is able to eliminate a register load, it can use the
428 R_SH_COUNT reloc to see whether it can also eliminate the function
432 sh_relax_section (abfd, sec, link_info, again)
435 struct bfd_link_info *link_info;
438 struct internal_reloc *internal_relocs;
439 struct internal_reloc *free_relocs = NULL;
440 struct internal_reloc *irel, *irelend;
441 bfd_byte *contents = NULL;
442 bfd_byte *free_contents = NULL;
446 if (link_info->relocateable
447 || (sec->flags & SEC_RELOC) == 0
448 || sec->reloc_count == 0)
451 /* If this is the first time we have been called for this section,
452 initialize the cooked size. */
453 if (sec->_cooked_size == 0)
454 sec->_cooked_size = sec->_raw_size;
456 internal_relocs = (_bfd_coff_read_internal_relocs
457 (abfd, sec, link_info->keep_memory,
458 (bfd_byte *) NULL, false,
459 (struct internal_reloc *) NULL));
460 if (internal_relocs == NULL)
462 if (! link_info->keep_memory)
463 free_relocs = internal_relocs;
465 irelend = internal_relocs + sec->reloc_count;
466 for (irel = internal_relocs; irel < irelend; irel++)
468 bfd_vma laddr, paddr, symval;
470 struct internal_reloc *irelfn, *irelscan, *irelcount;
471 struct internal_syment sym;
474 if (irel->r_type != R_SH_USES)
477 /* Get the section contents. */
478 if (contents == NULL)
480 if (coff_section_data (abfd, sec) != NULL
481 && coff_section_data (abfd, sec)->contents != NULL)
482 contents = coff_section_data (abfd, sec)->contents;
485 contents = (bfd_byte *) malloc (sec->_raw_size);
486 if (contents == NULL)
488 bfd_set_error (bfd_error_no_memory);
491 free_contents = contents;
493 if (! bfd_get_section_contents (abfd, sec, contents,
494 (file_ptr) 0, sec->_raw_size))
499 /* The r_offset field of the R_SH_USES reloc will point us to
500 the register load. The 4 is because the r_offset field is
501 computed as though it were a jump offset, which are based
502 from 4 bytes after the jump instruction. */
503 laddr = irel->r_vaddr - sec->vma + 4 + irel->r_offset;
504 if (laddr >= sec->_raw_size)
506 (*_bfd_error_handler) ("%s: 0x%lx: warning: bad R_SH_USES offset",
507 bfd_get_filename (abfd),
508 (unsigned long) irel->r_vaddr);
511 insn = bfd_get_16 (abfd, contents + laddr);
513 /* If the instruction is not mov.l NN,rN, we don't know what to
515 if ((insn & 0xf000) != 0xd000)
517 ((*_bfd_error_handler)
518 ("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x",
519 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr, insn));
523 /* Get the address from which the register is being loaded. The
524 displacement in the mov.l instruction is quadrupled. It is a
525 displacement from four bytes after the movl instruction, but,
526 before adding in the PC address, two least significant bits
527 of the PC are cleared. We assume that the section is aligned
528 on a four byte boundary. */
531 paddr += (laddr + 4) &~ 3;
532 if (paddr >= sec->_raw_size)
534 ((*_bfd_error_handler)
535 ("%s: 0x%lx: warning: bad R_SH_USES load offset",
536 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr));
540 /* Get the reloc for the address from which the register is
541 being loaded. This reloc will tell us which function is
542 actually being called. */
544 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
545 if (irelfn->r_vaddr == paddr
546 && irelfn->r_type == R_SH_IMM32)
548 if (irelfn >= irelend)
550 ((*_bfd_error_handler)
551 ("%s: 0x%lx: warning: could not find expected reloc",
552 bfd_get_filename (abfd), (unsigned long) paddr));
556 /* Get the value of the symbol referred to by the reloc. */
557 if (! _bfd_coff_get_external_symbols (abfd))
559 bfd_coff_swap_sym_in (abfd,
560 ((bfd_byte *) obj_coff_external_syms (abfd)
562 * bfd_coff_symesz (abfd))),
564 if (sym.n_scnum != 0 && sym.n_scnum != sec->target_index)
566 ((*_bfd_error_handler)
567 ("%s: 0x%lx: warning: symbol in unexpected section",
568 bfd_get_filename (abfd), (unsigned long) paddr));
572 if (sym.n_sclass != C_EXT)
574 symval = (sym.n_value
576 + sec->output_section->vma
577 + sec->output_offset);
581 struct coff_link_hash_entry *h;
583 h = obj_coff_sym_hashes (abfd)[irelfn->r_symndx];
584 BFD_ASSERT (h != NULL);
585 if (h->root.type != bfd_link_hash_defined
586 && h->root.type != bfd_link_hash_defweak)
588 /* This appears to be a reference to an undefined
589 symbol. Just ignore it--it will be caught by the
590 regular reloc processing. */
594 symval = (h->root.u.def.value
595 + h->root.u.def.section->output_section->vma
596 + h->root.u.def.section->output_offset);
599 symval += bfd_get_32 (abfd, contents + paddr - sec->vma);
601 /* See if this function call can be shortened. */
605 + sec->output_section->vma
608 if (foff < -0x1000 || foff >= 0x1000)
610 /* After all that work, we can't shorten this function call. */
614 /* Shorten the function call. */
616 /* For simplicity of coding, we are going to modify the section
617 contents, the section relocs, and the BFD symbol table. We
618 must tell the rest of the code not to free up this
619 information. It would be possible to instead create a table
620 of changes which have to be made, as is done in coff-mips.c;
621 that would be more work, but would require less memory when
622 the linker is run. */
624 if (coff_section_data (abfd, sec) == NULL)
627 ((struct coff_section_tdata *)
628 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
629 if (sec->used_by_bfd == NULL)
631 bfd_set_error (bfd_error_no_memory);
636 coff_section_data (abfd, sec)->relocs = internal_relocs;
637 coff_section_data (abfd, sec)->keep_relocs = true;
640 coff_section_data (abfd, sec)->contents = contents;
641 coff_section_data (abfd, sec)->keep_contents = true;
642 free_contents = NULL;
644 obj_coff_keep_syms (abfd) = true;
646 /* Replace the jsr with a bsr. */
648 /* Change the R_SH_USES reloc into an R_SH_PCDISP reloc, and
649 replace the jsr with a bsr. */
650 irel->r_type = R_SH_PCDISP;
651 irel->r_symndx = irelfn->r_symndx;
652 if (sym.n_sclass != C_EXT)
654 /* If this needs to be changed because of future relaxing,
655 it will be handled here like other internal PCDISP
658 0xb000 | ((foff >> 1) & 0xfff),
659 contents + irel->r_vaddr - sec->vma);
663 /* We can't fully resolve this yet, because the external
664 symbol value may be changed by future relaxing. We let
665 the final link phase handle it. */
666 bfd_put_16 (abfd, 0xb000, contents + irel->r_vaddr - sec->vma);
669 /* See if there is another R_SH_USES reloc referring to the same
671 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
672 if (irelscan->r_type == R_SH_USES
673 && laddr == irelscan->r_vaddr - sec->vma + 4 + irelscan->r_offset)
675 if (irelscan < irelend)
677 /* Some other function call depends upon this register load,
678 and we have not yet converted that function call.
679 Indeed, we may never be able to convert it. There is
680 nothing else we can do at this point. */
684 /* Look for a R_SH_COUNT reloc on the location where the
685 function address is stored. Do this before deleting any
686 bytes, to avoid confusion about the address. */
687 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
688 if (irelcount->r_vaddr == paddr
689 && irelcount->r_type == R_SH_COUNT)
692 /* Delete the register load. */
693 if (! sh_relax_delete_bytes (abfd, sec, laddr, 2))
696 /* That will change things, so, just in case it permits some
697 other function call to come within range, we should relax
698 again. Note that this is not required, and it may be slow. */
701 /* Now check whether we got a COUNT reloc. */
702 if (irelcount >= irelend)
704 ((*_bfd_error_handler)
705 ("%s: 0x%lx: warning: could not find expected COUNT reloc",
706 bfd_get_filename (abfd), (unsigned long) paddr));
710 /* The number of uses is stored in the r_offset field. We've
712 if (irelcount->r_offset == 0)
714 ((*_bfd_error_handler) ("%s: 0x%lx: warning: bad count",
715 bfd_get_filename (abfd),
716 (unsigned long) paddr));
720 --irelcount->r_offset;
722 /* If there are no more uses, we can delete the address. Reload
723 the address from irelfn, in case it was changed by the
724 previous call to sh_relax_delete_bytes. */
725 if (irelcount->r_offset == 0)
727 if (! sh_relax_delete_bytes (abfd, sec,
728 irelfn->r_vaddr - sec->vma, 4))
732 /* We've done all we can with that function call. */
735 if (free_relocs != NULL)
741 if (free_contents != NULL)
743 if (! link_info->keep_memory)
744 free (free_contents);
747 /* Cache the section contents for coff_link_input_bfd. */
748 if (coff_section_data (abfd, sec) == NULL)
751 ((struct coff_section_tdata *)
752 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
753 if (sec->used_by_bfd == NULL)
755 bfd_set_error (bfd_error_no_memory);
758 coff_section_data (abfd, sec)->relocs = NULL;
760 coff_section_data (abfd, sec)->contents = contents;
767 if (free_relocs != NULL)
769 if (free_contents != NULL)
770 free (free_contents);
774 /* Delete some bytes from a section while relaxing. */
777 sh_relax_delete_bytes (abfd, sec, addr, count)
784 struct internal_reloc *irel, *irelend;
785 struct internal_reloc *irelalign;
787 bfd_byte *esym, *esymend;
788 bfd_size_type symesz;
789 struct coff_link_hash_entry **sym_hash;
792 contents = coff_section_data (abfd, sec)->contents;
794 /* The deletion must stop at the next ALIGN reloc for an aligment
795 power larger than the number of bytes we are deleting. */
798 toaddr = sec->_cooked_size;
800 irel = coff_section_data (abfd, sec)->relocs;
801 irelend = irel + sec->reloc_count;
802 for (; irel < irelend; irel++)
804 if (irel->r_type == R_SH_ALIGN
805 && irel->r_vaddr - sec->vma > addr
806 && count < (1 << irel->r_offset))
809 toaddr = irel->r_vaddr - sec->vma;
814 /* Actually delete the bytes. */
815 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
816 if (irelalign == NULL)
817 sec->_cooked_size -= count;
819 memset (contents + toaddr - count, 0, count);
821 /* Adjust all the relocs. */
822 for (irel = coff_section_data (abfd, sec)->relocs; irel < irelend; irel++)
824 bfd_vma nraddr, start, stop;
826 struct internal_syment sym;
827 int off, adjust, oinsn;
831 /* Get the new reloc address. */
832 nraddr = irel->r_vaddr - sec->vma;
833 if ((irel->r_vaddr - sec->vma > addr
834 && irel->r_vaddr - sec->vma < toaddr)
835 || (irel->r_type == R_SH_ALIGN
836 && irel->r_vaddr - sec->vma == toaddr))
839 /* See if this reloc was for the bytes we have deleted, in which
840 case we no longer care about it. */
841 if (irel->r_vaddr - sec->vma >= addr
842 && irel->r_vaddr - sec->vma < addr + count
843 && irel->r_type != R_SH_ALIGN)
844 irel->r_type = R_SH_UNUSED;
846 /* If this is a PC relative reloc, see if the range it covers
847 includes the bytes we have deleted. */
848 switch (irel->r_type)
853 case R_SH_PCDISP8BY2:
855 case R_SH_PCRELIMM8BY2:
856 case R_SH_PCRELIMM8BY4:
857 start = irel->r_vaddr - sec->vma;
858 insn = bfd_get_16 (abfd, contents + nraddr);
862 switch (irel->r_type)
869 /* If this reloc is against a symbol defined in this
870 section, and the symbol will not be adjusted below, we
871 must check the addend to see it will put the value in
872 range to be adjusted, and hence must be changed. */
873 bfd_coff_swap_sym_in (abfd,
874 ((bfd_byte *) obj_coff_external_syms (abfd)
876 * bfd_coff_symesz (abfd))),
878 if (sym.n_sclass != C_EXT
879 && sym.n_scnum == sec->target_index
880 && (sym.n_value < addr
881 || sym.n_value >= toaddr))
885 val = bfd_get_32 (abfd, contents + nraddr);
887 if (val >= addr && val < toaddr)
888 bfd_put_32 (abfd, val - count, contents + nraddr);
893 case R_SH_PCDISP8BY2:
897 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
901 bfd_coff_swap_sym_in (abfd,
902 ((bfd_byte *) obj_coff_external_syms (abfd)
904 * bfd_coff_symesz (abfd))),
906 if (sym.n_sclass == C_EXT)
913 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
917 case R_SH_PCRELIMM8BY2:
919 stop = start + 4 + off * 2;
922 case R_SH_PCRELIMM8BY4:
924 stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
929 /* These relocs types represent
931 The r_offset field holds the difference between the reloc
932 address and L1. That is the start of the reloc, and
933 adding in the contents gives us the top. We must adjust
934 both the r_offset field and the section contents. */
936 start = irel->r_vaddr - sec->vma;
937 stop = (bfd_vma) ((bfd_signed_vma) start - (long) irel->r_offset);
941 && (stop <= addr || stop >= toaddr))
942 irel->r_offset += count;
945 && (start <= addr || start >= toaddr))
946 irel->r_offset -= count;
950 if (irel->r_type == R_SH_SWITCH16)
951 voff = bfd_get_signed_16 (abfd, contents + nraddr);
953 voff = bfd_get_signed_32 (abfd, contents + nraddr);
954 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
959 start = irel->r_vaddr - sec->vma;
960 stop = (bfd_vma) ((bfd_signed_vma) start + (long) irel->r_offset);
966 && (stop <= addr || stop >= toaddr))
970 && (start <= addr || start >= toaddr))
979 switch (irel->r_type)
985 case R_SH_PCDISP8BY2:
986 case R_SH_PCRELIMM8BY2:
988 if ((oinsn & 0xff00) != (insn & 0xff00))
990 bfd_put_16 (abfd, insn, contents + nraddr);
995 if ((oinsn & 0xf000) != (insn & 0xf000))
997 bfd_put_16 (abfd, insn, contents + nraddr);
1000 case R_SH_PCRELIMM8BY4:
1001 BFD_ASSERT (adjust == count || count >= 4);
1006 if ((irel->r_vaddr & 3) == 0)
1009 if ((oinsn & 0xff00) != (insn & 0xff00))
1011 bfd_put_16 (abfd, insn, contents + nraddr);
1016 if (voff < - 0x8000 || voff >= 0x8000)
1018 bfd_put_signed_16 (abfd, voff, contents + nraddr);
1023 bfd_put_signed_32 (abfd, voff, contents + nraddr);
1027 irel->r_offset += adjust;
1033 ((*_bfd_error_handler)
1034 ("%s: 0x%lx: fatal: reloc overflow while relaxing",
1035 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr));
1036 bfd_set_error (bfd_error_bad_value);
1041 irel->r_vaddr = nraddr + sec->vma;
1044 /* Look through all the other sections. If there contain any IMM32
1045 relocs against internal symbols which we are not going to adjust
1046 below, we may need to adjust the addends. */
1047 for (o = abfd->sections; o != NULL; o = o->next)
1049 struct internal_reloc *internal_relocs;
1050 struct internal_reloc *irelscan, *irelscanend;
1051 bfd_byte *ocontents;
1054 || (o->flags & SEC_RELOC) == 0
1055 || o->reloc_count == 0)
1058 /* We always cache the relocs. Perhaps, if info->keep_memory is
1059 false, we should free them, if we are permitted to, when we
1060 leave sh_coff_relax_section. */
1061 internal_relocs = (_bfd_coff_read_internal_relocs
1062 (abfd, o, true, (bfd_byte *) NULL, false,
1063 (struct internal_reloc *) NULL));
1064 if (internal_relocs == NULL)
1068 irelscanend = internal_relocs + o->reloc_count;
1069 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1071 struct internal_syment sym;
1073 if (irelscan->r_type != R_SH_IMM32)
1076 bfd_coff_swap_sym_in (abfd,
1077 ((bfd_byte *) obj_coff_external_syms (abfd)
1078 + (irelscan->r_symndx
1079 * bfd_coff_symesz (abfd))),
1081 if (sym.n_sclass != C_EXT
1082 && sym.n_scnum == sec->target_index
1083 && (sym.n_value < addr
1084 || sym.n_value >= toaddr))
1088 if (ocontents == NULL)
1090 if (coff_section_data (abfd, o)->contents != NULL)
1091 ocontents = coff_section_data (abfd, o)->contents;
1094 /* We always cache the section contents.
1095 Perhaps, if info->keep_memory is false, we
1096 should free them, if we are permitted to,
1097 when we leave sh_coff_relax_section. */
1098 ocontents = (bfd_byte *) malloc (o->_raw_size);
1099 if (ocontents == NULL)
1101 bfd_set_error (bfd_error_no_memory);
1104 if (! bfd_get_section_contents (abfd, o, ocontents,
1108 coff_section_data (abfd, o)->contents = ocontents;
1112 val = bfd_get_32 (abfd, ocontents + irelscan->r_vaddr - o->vma);
1114 if (val >= addr && val < toaddr)
1115 bfd_put_32 (abfd, val - count,
1116 ocontents + irelscan->r_vaddr - o->vma);
1118 coff_section_data (abfd, o)->keep_contents = true;
1123 /* Adjusting the internal symbols will not work if something has
1124 already retrieved the generic symbols. It would be possible to
1125 make this work by adjusting the generic symbols at the same time.
1126 However, this case should not arise in normal usage. */
1127 if (obj_symbols (abfd) != NULL
1128 || obj_raw_syments (abfd) != NULL)
1130 ((*_bfd_error_handler)
1131 ("%s: fatal: generic symbols retrieved before relaxing",
1132 bfd_get_filename (abfd)));
1133 bfd_set_error (bfd_error_invalid_operation);
1137 /* Adjust all the symbols. */
1138 sym_hash = obj_coff_sym_hashes (abfd);
1139 symesz = bfd_coff_symesz (abfd);
1140 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1141 esymend = esym + obj_raw_syment_count (abfd) * symesz;
1142 while (esym < esymend)
1144 struct internal_syment isym;
1146 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &isym);
1148 if (isym.n_scnum == sec->target_index
1149 && isym.n_value >= addr
1150 && isym.n_value < toaddr)
1152 isym.n_value -= count;
1154 bfd_coff_swap_sym_out (abfd, (PTR) &isym, (PTR) esym);
1156 if (*sym_hash != NULL)
1158 BFD_ASSERT ((*sym_hash)->root.type == bfd_link_hash_defined
1159 || (*sym_hash)->root.type == bfd_link_hash_defweak);
1160 BFD_ASSERT ((*sym_hash)->root.u.def.value >= addr
1161 && (*sym_hash)->root.u.def.value < toaddr);
1162 (*sym_hash)->root.u.def.value -= count;
1166 esym += (isym.n_numaux + 1) * symesz;
1167 sym_hash += isym.n_numaux + 1;
1170 /* See if we can move the ALIGN reloc forward. We have adjusted
1171 r_vaddr for it already. */
1172 if (irelalign != NULL)
1176 alignaddr = BFD_ALIGN (irelalign->r_vaddr - sec->vma,
1177 1 << irelalign->r_offset);
1178 if (alignaddr != toaddr)
1180 /* Tail recursion. */
1181 return sh_relax_delete_bytes (abfd, sec,
1182 irelalign->r_vaddr - sec->vma,
1183 1 << irelalign->r_offset);
1190 /* This is a modification of _bfd_coff_generic_relocate_section, which
1191 will handle SH relaxing. */
1194 sh_relocate_section (output_bfd, info, input_bfd, input_section, contents,
1195 relocs, syms, sections)
1197 struct bfd_link_info *info;
1199 asection *input_section;
1201 struct internal_reloc *relocs;
1202 struct internal_syment *syms;
1203 asection **sections;
1205 struct internal_reloc *rel;
1206 struct internal_reloc *relend;
1209 relend = rel + input_section->reloc_count;
1210 for (; rel < relend; rel++)
1213 struct coff_link_hash_entry *h;
1214 struct internal_syment *sym;
1217 reloc_howto_type *howto;
1218 bfd_reloc_status_type rstat;
1220 /* Almost all relocs have to do with relaxing. If any work must
1221 be done for them, it has been done in sh_relax_section. */
1222 if (rel->r_type != R_SH_IMM32
1223 && rel->r_type != R_SH_PCDISP)
1226 symndx = rel->r_symndx;
1235 h = obj_coff_sym_hashes (input_bfd)[symndx];
1236 sym = syms + symndx;
1239 if (sym != NULL && sym->n_scnum != 0)
1240 addend = - sym->n_value;
1244 if (rel->r_type == R_SH_PCDISP)
1247 if (rel->r_type >= SH_COFF_HOWTO_COUNT)
1250 howto = &sh_coff_howtos[rel->r_type];
1254 bfd_set_error (bfd_error_bad_value);
1264 /* There is nothing to do for an internal PCDISP reloc. */
1265 if (rel->r_type == R_SH_PCDISP)
1270 sec = bfd_abs_section_ptr;
1275 sec = sections[symndx];
1276 val = (sec->output_section->vma
1277 + sec->output_offset
1284 if (h->root.type == bfd_link_hash_defined
1285 || h->root.type == bfd_link_hash_defweak)
1289 sec = h->root.u.def.section;
1290 val = (h->root.u.def.value
1291 + sec->output_section->vma
1292 + sec->output_offset);
1294 else if (! info->relocateable)
1296 if (! ((*info->callbacks->undefined_symbol)
1297 (info, h->root.root.string, input_bfd, input_section,
1298 rel->r_vaddr - input_section->vma)))
1303 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
1305 rel->r_vaddr - input_section->vma,
1314 case bfd_reloc_overflow:
1317 char buf[SYMNMLEN + 1];
1322 name = h->root.root.string;
1323 else if (sym->_n._n_n._n_zeroes == 0
1324 && sym->_n._n_n._n_offset != 0)
1325 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1328 strncpy (buf, sym->_n._n_name, SYMNMLEN);
1329 buf[SYMNMLEN] = '\0';
1333 if (! ((*info->callbacks->reloc_overflow)
1334 (info, name, howto->name, (bfd_vma) 0, input_bfd,
1335 input_section, rel->r_vaddr - input_section->vma)))
1344 /* This is a version of bfd_generic_get_relocated_section_contents
1345 which uses sh_relocate_section. */
1348 sh_coff_get_relocated_section_contents (output_bfd, link_info, link_order,
1349 data, relocateable, symbols)
1351 struct bfd_link_info *link_info;
1352 struct bfd_link_order *link_order;
1354 boolean relocateable;
1357 asection *input_section = link_order->u.indirect.section;
1358 bfd *input_bfd = input_section->owner;
1359 asection **sections = NULL;
1360 struct internal_reloc *internal_relocs = NULL;
1361 struct internal_syment *internal_syms = NULL;
1363 /* We only need to handle the case of relaxing, or of having a
1364 particular set of section contents, specially. */
1366 || coff_section_data (input_bfd, input_section) == NULL
1367 || coff_section_data (input_bfd, input_section)->contents == NULL)
1368 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1373 memcpy (data, coff_section_data (input_bfd, input_section)->contents,
1374 input_section->_raw_size);
1376 if ((input_section->flags & SEC_RELOC) != 0
1377 && input_section->reloc_count > 0)
1379 bfd_size_type symesz = bfd_coff_symesz (input_bfd);
1380 bfd_byte *esym, *esymend;
1381 struct internal_syment *isymp;
1384 if (! _bfd_coff_get_external_symbols (input_bfd))
1387 internal_relocs = (_bfd_coff_read_internal_relocs
1388 (input_bfd, input_section, false, (bfd_byte *) NULL,
1389 false, (struct internal_reloc *) NULL));
1390 if (internal_relocs == NULL)
1393 internal_syms = ((struct internal_syment *)
1394 malloc (obj_raw_syment_count (input_bfd)
1395 * sizeof (struct internal_syment)));
1396 if (internal_syms == NULL)
1398 bfd_set_error (bfd_error_no_memory);
1402 sections = (asection **) malloc (obj_raw_syment_count (input_bfd)
1403 * sizeof (asection *));
1404 if (sections == NULL)
1406 bfd_set_error (bfd_error_no_memory);
1410 isymp = internal_syms;
1412 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1413 esymend = esym + obj_raw_syment_count (input_bfd) * symesz;
1414 while (esym < esymend)
1416 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1418 if (isymp->n_scnum != 0)
1419 *secpp = coff_section_from_bfd_index (input_bfd, isymp->n_scnum);
1422 if (isymp->n_value == 0)
1423 *secpp = bfd_und_section_ptr;
1425 *secpp = bfd_com_section_ptr;
1428 esym += (isymp->n_numaux + 1) * symesz;
1429 secpp += isymp->n_numaux + 1;
1430 isymp += isymp->n_numaux + 1;
1433 if (! sh_relocate_section (output_bfd, link_info, input_bfd,
1434 input_section, data, internal_relocs,
1435 internal_syms, sections))
1440 free (internal_syms);
1441 internal_syms = NULL;
1442 free (internal_relocs);
1443 internal_relocs = NULL;
1449 if (internal_relocs != NULL)
1450 free (internal_relocs);
1451 if (internal_syms != NULL)
1452 free (internal_syms);
1453 if (sections != NULL)
1458 /* The target vectors. */
1460 const bfd_target shcoff_vec =
1462 "coff-sh", /* name */
1463 bfd_target_coff_flavour,
1464 true, /* data byte order is big */
1465 true, /* header byte order is big */
1467 (HAS_RELOC | EXEC_P | /* object flags */
1468 HAS_LINENO | HAS_DEBUG |
1469 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
1471 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
1472 '_', /* leading symbol underscore */
1473 '/', /* ar_pad_char */
1474 15, /* ar_max_namelen */
1475 2, /* minimum section alignment */
1476 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1477 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1478 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1479 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1480 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1481 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1483 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1484 bfd_generic_archive_p, _bfd_dummy_target},
1485 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
1487 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
1488 _bfd_write_archive_contents, bfd_false},
1490 BFD_JUMP_TABLE_GENERIC (coff),
1491 BFD_JUMP_TABLE_COPY (coff),
1492 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1493 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
1494 BFD_JUMP_TABLE_SYMBOLS (coff),
1495 BFD_JUMP_TABLE_RELOCS (coff),
1496 BFD_JUMP_TABLE_WRITE (coff),
1497 BFD_JUMP_TABLE_LINK (coff),
1498 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1503 const bfd_target shlcoff_vec =
1505 "coff-shl", /* name */
1506 bfd_target_coff_flavour,
1507 false, /* data byte order is little */
1508 false, /* header byte order is little endian too*/
1510 (HAS_RELOC | EXEC_P | /* object flags */
1511 HAS_LINENO | HAS_DEBUG |
1512 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
1514 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
1515 '_', /* leading symbol underscore */
1516 '/', /* ar_pad_char */
1517 15, /* ar_max_namelen */
1518 2, /* minimum section alignment */
1519 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1520 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1521 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
1522 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1523 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1524 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
1526 /* Note that we use a special archive recognizer.
1527 This is so that we only use one archive format for both
1528 object file types */
1529 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1530 _bfd_dummy_target, _bfd_dummy_target},
1531 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
1533 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
1534 _bfd_write_archive_contents, bfd_false},
1536 BFD_JUMP_TABLE_GENERIC (coff),
1537 BFD_JUMP_TABLE_COPY (coff),
1538 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1539 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
1540 BFD_JUMP_TABLE_SYMBOLS (coff),
1541 BFD_JUMP_TABLE_RELOCS (coff),
1542 BFD_JUMP_TABLE_WRITE (coff),
1543 BFD_JUMP_TABLE_LINK (coff),
1544 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),