1 /* Hitachi SH specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998 Free Software Foundation, Inc.
3 Contributed by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 static bfd_reloc_status_type sh_elf_reloc
28 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
29 static bfd_reloc_status_type sh_elf_ignore_reloc
30 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
31 static reloc_howto_type *sh_elf_reloc_type_lookup
32 PARAMS ((bfd *, bfd_reloc_code_real_type));
33 static void sh_elf_info_to_howto
34 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
35 static boolean sh_elf_relax_section
36 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
37 static boolean sh_elf_relax_delete_bytes
38 PARAMS ((bfd *, asection *, bfd_vma, int));
39 static boolean sh_elf_align_loads
40 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, boolean *));
41 static boolean sh_elf_swap_insns
42 PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma));
43 static boolean sh_elf_relocate_section
44 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
45 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
46 static bfd_byte *sh_elf_get_relocated_section_contents
47 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
48 bfd_byte *, boolean, asymbol **));
63 LAST_INVALID_RELOC = 24,
64 /* The remaining relocs are a GNU extension used for relaxation. We
65 use the same constants as COFF uses, not that it really matters. */
77 static reloc_howto_type sh_elf_howto_table[] =
80 HOWTO (R_SH_NONE, /* type */
82 0, /* size (0 = byte, 1 = short, 2 = long) */
84 false, /* pc_relative */
86 complain_overflow_dont, /* complain_on_overflow */
87 sh_elf_reloc, /* special_function */
88 "R_SH_NONE", /* name */
89 false, /* partial_inplace */
92 false), /* pcrel_offset */
94 /* 32 bit absolute relocation. Setting partial_inplace to true and
95 src_mask to a non-zero value is similar to the COFF toolchain. */
96 HOWTO (R_SH_DIR32, /* type */
98 2, /* size (0 = byte, 1 = short, 2 = long) */
100 false, /* pc_relative */
102 complain_overflow_bitfield, /* complain_on_overflow */
103 sh_elf_reloc, /* special_function */
104 "R_SH_DIR32", /* name */
105 true, /* partial_inplace */
106 0xffffffff, /* src_mask */
107 0xffffffff, /* dst_mask */
108 false), /* pcrel_offset */
110 /* 32 bit PC relative relocation. */
111 HOWTO (R_SH_REL32, /* type */
113 2, /* size (0 = byte, 1 = short, 2 = long) */
115 true, /* pc_relative */
117 complain_overflow_signed, /* complain_on_overflow */
118 sh_elf_reloc, /* special_function */
119 "R_SH_REL32", /* name */
120 false, /* partial_inplace */
122 0xffffffff, /* dst_mask */
123 true), /* pcrel_offset */
125 /* 8 bit PC relative branch divided by 2. */
126 HOWTO (R_SH_DIR8WPN, /* type */
128 1, /* size (0 = byte, 1 = short, 2 = long) */
130 true, /* pc_relative */
132 complain_overflow_signed, /* complain_on_overflow */
133 sh_elf_reloc, /* special_function */
134 "R_SH_DIR8WPN", /* name */
135 true, /* partial_inplace */
138 true), /* pcrel_offset */
140 /* 12 bit PC relative branch divided by 2. */
141 HOWTO (R_SH_IND12W, /* type */
143 1, /* size (0 = byte, 1 = short, 2 = long) */
145 true, /* pc_relative */
147 complain_overflow_signed, /* complain_on_overflow */
148 sh_elf_reloc, /* special_function */
149 "R_SH_IND12W", /* name */
150 true, /* partial_inplace */
151 0xfff, /* src_mask */
152 0xfff, /* dst_mask */
153 true), /* pcrel_offset */
155 /* 8 bit unsigned PC relative divided by 4. */
156 HOWTO (R_SH_DIR8WPL, /* type */
158 1, /* size (0 = byte, 1 = short, 2 = long) */
160 true, /* pc_relative */
162 complain_overflow_unsigned, /* complain_on_overflow */
163 sh_elf_reloc, /* special_function */
164 "R_SH_DIR8WPL", /* name */
165 true, /* partial_inplace */
168 true), /* pcrel_offset */
170 /* 8 bit unsigned PC relative divided by 2. */
171 HOWTO (R_SH_DIR8WPZ, /* type */
173 1, /* size (0 = byte, 1 = short, 2 = long) */
175 true, /* pc_relative */
177 complain_overflow_unsigned, /* complain_on_overflow */
178 sh_elf_reloc, /* special_function */
179 "R_SH_DIR8WPZ", /* name */
180 true, /* partial_inplace */
183 true), /* pcrel_offset */
185 /* 8 bit GBR relative. FIXME: This only makes sense if we have some
186 special symbol for the GBR relative area, and that is not
188 HOWTO (R_SH_DIR8BP, /* type */
190 1, /* size (0 = byte, 1 = short, 2 = long) */
192 false, /* pc_relative */
194 complain_overflow_unsigned, /* complain_on_overflow */
195 sh_elf_reloc, /* special_function */
196 "R_SH_DIR8BP", /* name */
197 false, /* partial_inplace */
200 true), /* pcrel_offset */
202 /* 8 bit GBR relative divided by 2. FIXME: This only makes sense if
203 we have some special symbol for the GBR relative area, and that
204 is not implemented. */
205 HOWTO (R_SH_DIR8W, /* type */
207 1, /* size (0 = byte, 1 = short, 2 = long) */
209 false, /* pc_relative */
211 complain_overflow_unsigned, /* complain_on_overflow */
212 sh_elf_reloc, /* special_function */
213 "R_SH_DIR8W", /* name */
214 false, /* partial_inplace */
217 true), /* pcrel_offset */
219 /* 8 bit GBR relative divided by 4. FIXME: This only makes sense if
220 we have some special symbol for the GBR relative area, and that
221 is not implemented. */
222 HOWTO (R_SH_DIR8L, /* type */
224 1, /* size (0 = byte, 1 = short, 2 = long) */
226 false, /* pc_relative */
228 complain_overflow_unsigned, /* complain_on_overflow */
229 sh_elf_reloc, /* special_function */
230 "R_SH_DIR8L", /* name */
231 false, /* partial_inplace */
234 true), /* pcrel_offset */
252 /* The remaining relocs are a GNU extension used for relaxing. The
253 final pass of the linker never needs to do anything with any of
254 these relocs. Any required operations are handled by the
257 /* A 16 bit switch table entry. This is generated for an expression
258 such as ``.word L1 - L2''. The offset holds the difference
259 between the reloc address and L2. */
260 HOWTO (R_SH_SWITCH16, /* type */
262 1, /* size (0 = byte, 1 = short, 2 = long) */
264 false, /* pc_relative */
266 complain_overflow_unsigned, /* complain_on_overflow */
267 sh_elf_ignore_reloc, /* special_function */
268 "R_SH_SWITCH16", /* name */
269 false, /* partial_inplace */
272 true), /* pcrel_offset */
274 /* A 32 bit switch table entry. This is generated for an expression
275 such as ``.long L1 - L2''. The offset holds the difference
276 between the reloc address and L2. */
277 HOWTO (R_SH_SWITCH32, /* type */
279 2, /* size (0 = byte, 1 = short, 2 = long) */
281 false, /* pc_relative */
283 complain_overflow_unsigned, /* complain_on_overflow */
284 sh_elf_ignore_reloc, /* special_function */
285 "R_SH_SWITCH32", /* name */
286 false, /* partial_inplace */
289 true), /* pcrel_offset */
291 /* Indicates a .uses pseudo-op. The compiler will generate .uses
292 pseudo-ops when it finds a function call which can be relaxed.
293 The offset field holds the PC relative offset to the instruction
294 which loads the register used in the function call. */
295 HOWTO (R_SH_USES, /* type */
297 1, /* size (0 = byte, 1 = short, 2 = long) */
299 false, /* pc_relative */
301 complain_overflow_unsigned, /* complain_on_overflow */
302 sh_elf_ignore_reloc, /* special_function */
303 "R_SH_USES", /* name */
304 false, /* partial_inplace */
307 true), /* pcrel_offset */
309 /* The assembler will generate this reloc for addresses referred to
310 by the register loads associated with USES relocs. The offset
311 field holds the number of times the address is referenced in the
313 HOWTO (R_SH_COUNT, /* type */
315 1, /* size (0 = byte, 1 = short, 2 = long) */
317 false, /* pc_relative */
319 complain_overflow_unsigned, /* complain_on_overflow */
320 sh_elf_ignore_reloc, /* special_function */
321 "R_SH_COUNT", /* name */
322 false, /* partial_inplace */
325 true), /* pcrel_offset */
327 /* Indicates an alignment statement. The offset field is the power
328 of 2 to which subsequent portions of the object file must be
330 HOWTO (R_SH_ALIGN, /* type */
332 1, /* size (0 = byte, 1 = short, 2 = long) */
334 false, /* pc_relative */
336 complain_overflow_unsigned, /* complain_on_overflow */
337 sh_elf_ignore_reloc, /* special_function */
338 "R_SH_ALIGN", /* name */
339 false, /* partial_inplace */
342 true), /* pcrel_offset */
344 /* The assembler will generate this reloc before a block of
345 instructions. A section should be processed as assumining it
346 contains data, unless this reloc is seen. */
347 HOWTO (R_SH_CODE, /* type */
349 1, /* size (0 = byte, 1 = short, 2 = long) */
351 false, /* pc_relative */
353 complain_overflow_unsigned, /* complain_on_overflow */
354 sh_elf_ignore_reloc, /* special_function */
355 "R_SH_CODE", /* name */
356 false, /* partial_inplace */
359 true), /* pcrel_offset */
361 /* The assembler will generate this reloc after a block of
362 instructions when it sees data that is not instructions. */
363 HOWTO (R_SH_DATA, /* type */
365 1, /* size (0 = byte, 1 = short, 2 = long) */
367 false, /* pc_relative */
369 complain_overflow_unsigned, /* complain_on_overflow */
370 sh_elf_ignore_reloc, /* special_function */
371 "R_SH_DATA", /* name */
372 false, /* partial_inplace */
375 true), /* pcrel_offset */
377 /* The assembler generates this reloc for each label within a block
378 of instructions. This permits the linker to avoid swapping
379 instructions which are the targets of branches. */
380 HOWTO (R_SH_LABEL, /* type */
382 1, /* size (0 = byte, 1 = short, 2 = long) */
384 false, /* pc_relative */
386 complain_overflow_unsigned, /* complain_on_overflow */
387 sh_elf_ignore_reloc, /* special_function */
388 "R_SH_LABEL", /* name */
389 false, /* partial_inplace */
392 true) /* pcrel_offset */
395 /* This function is used for normal relocs. This is like the COFF
396 function, and is almost certainly incorrect for other ELF targets. */
398 static bfd_reloc_status_type
399 sh_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
402 arelent *reloc_entry;
405 asection *input_section;
407 char **error_message;
411 enum sh_reloc_type r_type;
412 bfd_vma addr = reloc_entry->address;
413 bfd_byte *hit_data = addr + (bfd_byte *) data;
415 r_type = (enum sh_reloc_type) reloc_entry->howto->type;
417 if (output_bfd != NULL)
419 /* Partial linking--do nothing. */
420 reloc_entry->address += input_section->output_offset;
424 /* Almost all relocs have to do with relaxing. If any work must be
425 done for them, it has been done in sh_relax_section. */
426 if (r_type != R_SH_DIR32
427 && (r_type != R_SH_IND12W
428 || (symbol_in->flags & BSF_LOCAL) != 0))
431 if (symbol_in != NULL
432 && bfd_is_und_section (symbol_in->section))
433 return bfd_reloc_undefined;
435 if (bfd_is_com_section (symbol_in->section))
438 sym_value = (symbol_in->value +
439 symbol_in->section->output_section->vma +
440 symbol_in->section->output_offset);
445 insn = bfd_get_32 (abfd, hit_data);
446 insn += sym_value + reloc_entry->addend;
447 bfd_put_32 (abfd, insn, hit_data);
450 insn = bfd_get_16 (abfd, hit_data);
451 sym_value += reloc_entry->addend;
452 sym_value -= (input_section->output_section->vma
453 + input_section->output_offset
456 sym_value += (insn & 0xfff) << 1;
459 insn = (insn & 0xf000) | (sym_value & 0xfff);
460 bfd_put_16 (abfd, insn, hit_data);
461 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
462 return bfd_reloc_overflow;
472 /* This function is used for relocs which are only used for relaxing,
473 which the linker should otherwise ignore. */
475 static bfd_reloc_status_type
476 sh_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
477 output_bfd, error_message)
479 arelent *reloc_entry;
482 asection *input_section;
484 char **error_message;
486 if (output_bfd != NULL)
487 reloc_entry->address += input_section->output_offset;
491 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
495 unsigned char bfd_reloc_val;
496 unsigned char elf_reloc_val;
499 /* An array mapping BFD reloc codes to SH ELF relocs. */
501 static const struct elf_reloc_map sh_reloc_map[] =
503 { BFD_RELOC_NONE, R_SH_NONE },
504 { BFD_RELOC_32, R_SH_DIR32 },
505 { BFD_RELOC_CTOR, R_SH_DIR32 },
506 { BFD_RELOC_32_PCREL, R_SH_REL32 },
507 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
508 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
509 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
510 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
511 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
512 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
513 { BFD_RELOC_SH_USES, R_SH_USES },
514 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
515 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
516 { BFD_RELOC_SH_CODE, R_SH_CODE },
517 { BFD_RELOC_SH_DATA, R_SH_DATA },
518 { BFD_RELOC_SH_LABEL, R_SH_LABEL }
521 /* Given a BFD reloc code, return the howto structure for the
522 corresponding SH ELf reloc. */
524 static reloc_howto_type *
525 sh_elf_reloc_type_lookup (abfd, code)
527 bfd_reloc_code_real_type code;
531 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
533 if (sh_reloc_map[i].bfd_reloc_val == code)
534 return &sh_elf_howto_table[(int) sh_reloc_map[i].elf_reloc_val];
540 /* Given an ELF reloc, fill in the howto field of a relent. */
543 sh_elf_info_to_howto (abfd, cache_ptr, dst)
546 Elf_Internal_Rela *dst;
550 r = ELF32_R_TYPE (dst->r_info);
552 BFD_ASSERT (r < (unsigned int) R_SH_max);
553 BFD_ASSERT (r < FIRST_INVALID_RELOC || r > LAST_INVALID_RELOC);
555 cache_ptr->howto = &sh_elf_howto_table[r];
558 /* This function handles relaxing for SH ELF. See the corresponding
559 function in coff-sh.c for a description of what this does. FIXME:
560 There is a lot of duplication here between this code and the COFF
561 specific code. The format of relocs and symbols is wound deeply
562 into this code, but it would still be better if the duplication
563 could be eliminated somehow. Note in particular that although both
564 functions use symbols like R_SH_CODE, those symbols have different
565 values; in coff-sh.c they come from include/coff/sh.h, whereas here
566 they come from enum sh_reloc_type in this file. */
569 sh_elf_relax_section (abfd, sec, link_info, again)
572 struct bfd_link_info *link_info;
575 Elf_Internal_Shdr *symtab_hdr;
576 Elf_Internal_Rela *internal_relocs;
577 Elf_Internal_Rela *free_relocs = NULL;
579 Elf_Internal_Rela *irel, *irelend;
580 bfd_byte *contents = NULL;
581 bfd_byte *free_contents = NULL;
582 Elf32_External_Sym *extsyms = NULL;
583 Elf32_External_Sym *free_extsyms = NULL;
587 if (link_info->relocateable
588 || (sec->flags & SEC_RELOC) == 0
589 || sec->reloc_count == 0)
592 /* If this is the first time we have been called for this section,
593 initialize the cooked size. */
594 if (sec->_cooked_size == 0)
595 sec->_cooked_size = sec->_raw_size;
597 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
599 internal_relocs = (_bfd_elf32_link_read_relocs
600 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
601 link_info->keep_memory));
602 if (internal_relocs == NULL)
604 if (! link_info->keep_memory)
605 free_relocs = internal_relocs;
609 irelend = internal_relocs + sec->reloc_count;
610 for (irel = internal_relocs; irel < irelend; irel++)
612 bfd_vma laddr, paddr, symval;
614 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
617 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
620 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
623 /* Get the section contents. */
624 if (contents == NULL)
626 if (elf_section_data (sec)->this_hdr.contents != NULL)
627 contents = elf_section_data (sec)->this_hdr.contents;
630 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
631 if (contents == NULL)
633 free_contents = contents;
635 if (! bfd_get_section_contents (abfd, sec, contents,
636 (file_ptr) 0, sec->_raw_size))
641 /* The r_addend field of the R_SH_USES reloc will point us to
642 the register load. The 4 is because the r_addend field is
643 computed as though it were a jump offset, which are based
644 from 4 bytes after the jump instruction. */
645 laddr = irel->r_offset + 4 + irel->r_addend;
646 if (laddr >= sec->_raw_size)
648 (*_bfd_error_handler) ("%s: 0x%lx: warning: bad R_SH_USES offset",
649 bfd_get_filename (abfd),
650 (unsigned long) irel->r_offset);
653 insn = bfd_get_16 (abfd, contents + laddr);
655 /* If the instruction is not mov.l NN,rN, we don't know what to
657 if ((insn & 0xf000) != 0xd000)
659 ((*_bfd_error_handler)
660 ("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x",
661 bfd_get_filename (abfd), (unsigned long) irel->r_offset, insn));
665 /* Get the address from which the register is being loaded. The
666 displacement in the mov.l instruction is quadrupled. It is a
667 displacement from four bytes after the movl instruction, but,
668 before adding in the PC address, two least significant bits
669 of the PC are cleared. We assume that the section is aligned
670 on a four byte boundary. */
673 paddr += (laddr + 4) &~ 3;
674 if (paddr >= sec->_raw_size)
676 ((*_bfd_error_handler)
677 ("%s: 0x%lx: warning: bad R_SH_USES load offset",
678 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
682 /* Get the reloc for the address from which the register is
683 being loaded. This reloc will tell us which function is
684 actually being called. */
685 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
686 if (irelfn->r_offset == paddr
687 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
689 if (irelfn >= irelend)
691 ((*_bfd_error_handler)
692 ("%s: 0x%lx: warning: could not find expected reloc",
693 bfd_get_filename (abfd), (unsigned long) paddr));
697 /* Read this BFD's symbols if we haven't done so already. */
700 if (symtab_hdr->contents != NULL)
701 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
704 extsyms = ((Elf32_External_Sym *)
705 bfd_malloc (symtab_hdr->sh_size));
708 free_extsyms = extsyms;
709 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
710 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
711 != symtab_hdr->sh_size))
716 /* Get the value of the symbol referred to by the reloc. */
717 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
719 Elf_Internal_Sym isym;
721 /* A local symbol. */
722 bfd_elf32_swap_symbol_in (abfd,
723 extsyms + ELF32_R_SYM (irelfn->r_info),
726 if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec))
728 ((*_bfd_error_handler)
729 ("%s: 0x%lx: warning: symbol in unexpected section",
730 bfd_get_filename (abfd), (unsigned long) paddr));
734 symval = (isym.st_value
735 + sec->output_section->vma
736 + sec->output_offset);
741 struct elf_link_hash_entry *h;
743 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
744 h = elf_sym_hashes (abfd)[indx];
745 BFD_ASSERT (h != NULL);
746 if (h->root.type != bfd_link_hash_defined
747 && h->root.type != bfd_link_hash_defweak)
749 /* This appears to be a reference to an undefined
750 symbol. Just ignore it--it will be caught by the
751 regular reloc processing. */
755 symval = (h->root.u.def.value
756 + h->root.u.def.section->output_section->vma
757 + h->root.u.def.section->output_offset);
760 symval += bfd_get_32 (abfd, contents + paddr);
762 /* See if this function call can be shortened. */
765 + sec->output_section->vma
768 if (foff < -0x1000 || foff >= 0x1000)
770 /* After all that work, we can't shorten this function call. */
774 /* Shorten the function call. */
776 /* For simplicity of coding, we are going to modify the section
777 contents, the section relocs, and the BFD symbol table. We
778 must tell the rest of the code not to free up this
779 information. It would be possible to instead create a table
780 of changes which have to be made, as is done in coff-mips.c;
781 that would be more work, but would require less memory when
782 the linker is run. */
784 elf_section_data (sec)->relocs = internal_relocs;
787 elf_section_data (sec)->this_hdr.contents = contents;
788 free_contents = NULL;
790 symtab_hdr->contents = (bfd_byte *) extsyms;
793 /* Replace the jsr with a bsr. */
795 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
796 replace the jsr with a bsr. */
797 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
798 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
800 /* If this needs to be changed because of future relaxing,
801 it will be handled here like other internal IND12W
804 0xb000 | ((foff >> 1) & 0xfff),
805 contents + irel->r_offset);
809 /* We can't fully resolve this yet, because the external
810 symbol value may be changed by future relaxing. We let
811 the final link phase handle it. */
812 bfd_put_16 (abfd, 0xb000, contents + irel->r_offset);
815 /* See if there is another R_SH_USES reloc referring to the same
817 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
818 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
819 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
821 if (irelscan < irelend)
823 /* Some other function call depends upon this register load,
824 and we have not yet converted that function call.
825 Indeed, we may never be able to convert it. There is
826 nothing else we can do at this point. */
830 /* Look for a R_SH_COUNT reloc on the location where the
831 function address is stored. Do this before deleting any
832 bytes, to avoid confusion about the address. */
833 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
834 if (irelcount->r_offset == paddr
835 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
838 /* Delete the register load. */
839 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
842 /* That will change things, so, just in case it permits some
843 other function call to come within range, we should relax
844 again. Note that this is not required, and it may be slow. */
847 /* Now check whether we got a COUNT reloc. */
848 if (irelcount >= irelend)
850 ((*_bfd_error_handler)
851 ("%s: 0x%lx: warning: could not find expected COUNT reloc",
852 bfd_get_filename (abfd), (unsigned long) paddr));
856 /* The number of uses is stored in the r_addend field. We've
858 if (irelcount->r_addend == 0)
860 ((*_bfd_error_handler) ("%s: 0x%lx: warning: bad count",
861 bfd_get_filename (abfd),
862 (unsigned long) paddr));
866 --irelcount->r_addend;
868 /* If there are no more uses, we can delete the address. Reload
869 the address from irelfn, in case it was changed by the
870 previous call to sh_elf_relax_delete_bytes. */
871 if (irelcount->r_addend == 0)
873 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
877 /* We've done all we can with that function call. */
880 /* Look for load and store instructions that we can align on four
886 /* Get the section contents. */
887 if (contents == NULL)
889 if (elf_section_data (sec)->this_hdr.contents != NULL)
890 contents = elf_section_data (sec)->this_hdr.contents;
893 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
894 if (contents == NULL)
896 free_contents = contents;
898 if (! bfd_get_section_contents (abfd, sec, contents,
899 (file_ptr) 0, sec->_raw_size))
904 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
910 elf_section_data (sec)->relocs = internal_relocs;
913 elf_section_data (sec)->this_hdr.contents = contents;
914 free_contents = NULL;
916 symtab_hdr->contents = (bfd_byte *) extsyms;
921 if (free_relocs != NULL)
927 if (free_contents != NULL)
929 if (! link_info->keep_memory)
930 free (free_contents);
933 /* Cache the section contents for elf_link_input_bfd. */
934 elf_section_data (sec)->this_hdr.contents = contents;
936 free_contents = NULL;
939 if (free_extsyms != NULL)
941 if (! link_info->keep_memory)
945 /* Cache the symbols for elf_link_input_bfd. */
946 symtab_hdr->contents = extsyms;
954 if (free_relocs != NULL)
956 if (free_contents != NULL)
957 free (free_contents);
958 if (free_extsyms != NULL)
963 /* Delete some bytes from a section while relaxing. FIXME: There is a
964 lot of duplication between this function and sh_relax_delete_bytes
968 sh_elf_relax_delete_bytes (abfd, sec, addr, count)
974 Elf_Internal_Shdr *symtab_hdr;
975 Elf32_External_Sym *extsyms;
978 Elf_Internal_Rela *irel, *irelend;
979 Elf_Internal_Rela *irelalign;
981 Elf32_External_Sym *esym, *esymend;
982 struct elf_link_hash_entry *sym_hash;
985 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
986 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
988 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
990 contents = elf_section_data (sec)->this_hdr.contents;
992 /* The deletion must stop at the next ALIGN reloc for an aligment
993 power larger than the number of bytes we are deleting. */
996 toaddr = sec->_cooked_size;
998 irel = elf_section_data (sec)->relocs;
999 irelend = irel + sec->reloc_count;
1000 for (; irel < irelend; irel++)
1002 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1003 && irel->r_offset > addr
1004 && count < (1 << irel->r_addend))
1007 toaddr = irel->r_offset;
1012 /* Actually delete the bytes. */
1013 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1014 if (irelalign == NULL)
1015 sec->_cooked_size -= count;
1020 #define NOP_OPCODE (0x0009)
1022 BFD_ASSERT ((count & 1) == 0);
1023 for (i = 0; i < count; i += 2)
1024 bfd_put_16 (abfd, NOP_OPCODE, contents + toaddr - count + i);
1027 /* Adjust all the relocs. */
1028 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1030 bfd_vma nraddr, stop;
1033 Elf_Internal_Sym sym;
1034 int off, adjust, oinsn;
1035 bfd_signed_vma voff = 0;
1038 /* Get the new reloc address. */
1039 nraddr = irel->r_offset;
1040 if ((irel->r_offset > addr
1041 && irel->r_offset < toaddr)
1042 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1043 && irel->r_offset == toaddr))
1046 /* See if this reloc was for the bytes we have deleted, in which
1047 case we no longer care about it. Don't delete relocs which
1048 represent addresses, though. */
1049 if (irel->r_offset >= addr
1050 && irel->r_offset < addr + count
1051 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
1052 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
1053 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
1054 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
1055 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1058 /* If this is a PC relative reloc, see if the range it covers
1059 includes the bytes we have deleted. */
1060 switch ((enum sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1069 start = irel->r_offset;
1070 insn = bfd_get_16 (abfd, contents + nraddr);
1074 switch ((enum sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1077 start = stop = addr;
1081 /* If this reloc is against a symbol defined in this
1082 section, and the symbol will not be adjusted below, we
1083 must check the addend to see it will put the value in
1084 range to be adjusted, and hence must be changed. */
1085 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1087 bfd_elf32_swap_symbol_in (abfd,
1088 extsyms + ELF32_R_SYM (irel->r_info),
1090 if (sym.st_shndx == shndx
1091 && (sym.st_value <= addr
1092 || sym.st_value >= toaddr))
1096 val = bfd_get_32 (abfd, contents + nraddr);
1097 val += sym.st_value;
1098 if (val >= addr && val < toaddr)
1099 bfd_put_32 (abfd, val - count, contents + nraddr);
1102 start = stop = addr;
1109 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1113 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
1114 start = stop = addr;
1120 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1126 stop = start + 4 + off * 2;
1131 stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
1136 /* These relocs types represent
1138 The r_offset field holds the difference between the reloc
1139 address and L1. That is the start of the reloc, and
1140 adding in the contents gives us the top. We must adjust
1141 both the r_offset field and the section contents. */
1143 start = irel->r_offset;
1144 stop = (bfd_vma) ((bfd_signed_vma) start - (long) irel->r_addend);
1148 && (stop <= addr || stop >= toaddr))
1149 irel->r_addend += count;
1150 else if (stop > addr
1152 && (start <= addr || start >= toaddr))
1153 irel->r_addend -= count;
1157 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1158 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1160 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1161 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1166 start = irel->r_offset;
1167 stop = (bfd_vma) ((bfd_signed_vma) start
1168 + (long) irel->r_addend
1175 && (stop <= addr || stop >= toaddr))
1177 else if (stop > addr
1179 && (start <= addr || start >= toaddr))
1188 switch ((enum sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1197 if ((oinsn & 0xff00) != (insn & 0xff00))
1199 bfd_put_16 (abfd, insn, contents + nraddr);
1204 if ((oinsn & 0xf000) != (insn & 0xf000))
1206 bfd_put_16 (abfd, insn, contents + nraddr);
1210 BFD_ASSERT (adjust == count || count >= 4);
1215 if ((irel->r_offset & 3) == 0)
1218 if ((oinsn & 0xff00) != (insn & 0xff00))
1220 bfd_put_16 (abfd, insn, contents + nraddr);
1225 if (voff < - 0x8000 || voff >= 0x8000)
1227 bfd_put_signed_16 (abfd, voff, contents + nraddr);
1232 bfd_put_signed_32 (abfd, voff, contents + nraddr);
1236 irel->r_addend += adjust;
1242 ((*_bfd_error_handler)
1243 ("%s: 0x%lx: fatal: reloc overflow while relaxing",
1244 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1245 bfd_set_error (bfd_error_bad_value);
1250 irel->r_offset = nraddr;
1253 /* Look through all the other sections. If there contain any IMM32
1254 relocs against internal symbols which we are not going to adjust
1255 below, we may need to adjust the addends. */
1256 for (o = abfd->sections; o != NULL; o = o->next)
1258 Elf_Internal_Rela *internal_relocs;
1259 Elf_Internal_Rela *irelscan, *irelscanend;
1260 bfd_byte *ocontents;
1263 || (o->flags & SEC_RELOC) == 0
1264 || o->reloc_count == 0)
1267 /* We always cache the relocs. Perhaps, if info->keep_memory is
1268 false, we should free them, if we are permitted to, when we
1269 leave sh_coff_relax_section. */
1270 internal_relocs = (_bfd_elf32_link_read_relocs
1271 (abfd, o, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1273 if (internal_relocs == NULL)
1277 irelscanend = internal_relocs + o->reloc_count;
1278 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1280 Elf_Internal_Sym sym;
1282 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1285 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1288 bfd_elf32_swap_symbol_in (abfd,
1289 extsyms + ELF32_R_SYM (irelscan->r_info),
1292 if (sym.st_shndx == shndx
1293 && (sym.st_value <= addr
1294 || sym.st_value >= toaddr))
1298 if (ocontents == NULL)
1300 if (elf_section_data (o)->this_hdr.contents != NULL)
1301 ocontents = elf_section_data (o)->this_hdr.contents;
1304 /* We always cache the section contents.
1305 Perhaps, if info->keep_memory is false, we
1306 should free them, if we are permitted to,
1307 when we leave sh_coff_relax_section. */
1308 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1309 if (ocontents == NULL)
1311 if (! bfd_get_section_contents (abfd, o, ocontents,
1315 elf_section_data (o)->this_hdr.contents = ocontents;
1319 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1320 val += sym.st_value;
1321 if (val >= addr && val < toaddr)
1322 bfd_put_32 (abfd, val - count,
1323 ocontents + irelscan->r_offset);
1328 /* Adjust the local symbols defined in this section. */
1330 esymend = esym + symtab_hdr->sh_info;
1331 for (; esym < esymend; esym++)
1333 Elf_Internal_Sym isym;
1335 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1337 if (isym.st_shndx == shndx
1338 && isym.st_value > addr
1339 && isym.st_value < toaddr)
1341 isym.st_value -= count;
1342 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1346 /* Now adjust the global symbols defined in this section. */
1347 esym = extsyms + symtab_hdr->sh_info;
1348 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1349 for (index = 0; esym < esymend; esym++, index++)
1351 Elf_Internal_Sym isym;
1353 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1354 sym_hash = elf_sym_hashes (abfd)[index];
1355 if (isym.st_shndx == shndx
1356 && ((sym_hash)->root.type == bfd_link_hash_defined
1357 || (sym_hash)->root.type == bfd_link_hash_defweak)
1358 && (sym_hash)->root.u.def.section == sec
1359 && (sym_hash)->root.u.def.value > addr
1360 && (sym_hash)->root.u.def.value < toaddr)
1362 (sym_hash)->root.u.def.value -= count;
1366 /* See if we can move the ALIGN reloc forward. We have adjusted
1367 r_offset for it already. */
1368 if (irelalign != NULL)
1370 bfd_vma alignto, alignaddr;
1372 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1373 alignaddr = BFD_ALIGN (irelalign->r_offset,
1374 1 << irelalign->r_addend);
1375 if (alignto != alignaddr)
1377 /* Tail recursion. */
1378 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1379 alignto - alignaddr);
1386 /* Look for loads and stores which we can align to four byte
1387 boundaries. This is like sh_align_loads in coff-sh.c. */
1390 sh_elf_align_loads (abfd, sec, internal_relocs, contents, pswapped)
1393 Elf_Internal_Rela *internal_relocs;
1397 Elf_Internal_Rela *irel, *irelend;
1398 bfd_vma *labels = NULL;
1399 bfd_vma *label, *label_end;
1403 irelend = internal_relocs + sec->reloc_count;
1405 /* Get all the addresses with labels on them. */
1406 labels = (bfd_vma *) bfd_malloc (sec->reloc_count * sizeof (bfd_vma));
1410 for (irel = internal_relocs; irel < irelend; irel++)
1412 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1414 *label_end = irel->r_offset;
1419 /* Note that the assembler currently always outputs relocs in
1420 address order. If that ever changes, this code will need to sort
1421 the label values and the relocs. */
1425 for (irel = internal_relocs; irel < irelend; irel++)
1427 bfd_vma start, stop;
1429 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1432 start = irel->r_offset;
1434 for (irel++; irel < irelend; irel++)
1435 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1438 stop = irel->r_offset;
1440 stop = sec->_cooked_size;
1442 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1443 (PTR) internal_relocs, &label,
1444 label_end, start, stop, pswapped))
1458 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1461 sh_elf_swap_insns (abfd, sec, relocs, contents, addr)
1468 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1469 unsigned short i1, i2;
1470 Elf_Internal_Rela *irel, *irelend;
1472 /* Swap the instructions themselves. */
1473 i1 = bfd_get_16 (abfd, contents + addr);
1474 i2 = bfd_get_16 (abfd, contents + addr + 2);
1475 bfd_put_16 (abfd, i2, contents + addr);
1476 bfd_put_16 (abfd, i1, contents + addr + 2);
1478 /* Adjust all reloc addresses. */
1479 irelend = internal_relocs + sec->reloc_count;
1480 for (irel = internal_relocs; irel < irelend; irel++)
1482 enum sh_reloc_type type;
1485 /* There are a few special types of relocs that we don't want to
1486 adjust. These relocs do not apply to the instruction itself,
1487 but are only associated with the address. */
1488 type = (enum sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1489 if (type == R_SH_ALIGN
1490 || type == R_SH_CODE
1491 || type == R_SH_DATA
1492 || type == R_SH_LABEL)
1495 /* If an R_SH_USES reloc points to one of the addresses being
1496 swapped, we must adjust it. It would be incorrect to do this
1497 for a jump, though, since we want to execute both
1498 instructions after the jump. (We have avoided swapping
1499 around a label, so the jump will not wind up executing an
1500 instruction it shouldn't). */
1501 if (type == R_SH_USES)
1505 off = irel->r_offset + 4 + irel->r_addend;
1507 irel->r_offset += 2;
1508 else if (off == addr + 2)
1509 irel->r_offset -= 2;
1512 if (irel->r_offset == addr)
1514 irel->r_offset += 2;
1517 else if (irel->r_offset == addr + 2)
1519 irel->r_offset -= 2;
1528 unsigned short insn, oinsn;
1531 loc = contents + irel->r_offset;
1540 insn = bfd_get_16 (abfd, loc);
1543 if ((oinsn & 0xff00) != (insn & 0xff00))
1545 bfd_put_16 (abfd, insn, loc);
1549 insn = bfd_get_16 (abfd, loc);
1552 if ((oinsn & 0xf000) != (insn & 0xf000))
1554 bfd_put_16 (abfd, insn, loc);
1558 /* This reloc ignores the least significant 3 bits of
1559 the program counter before adding in the offset.
1560 This means that if ADDR is at an even address, the
1561 swap will not affect the offset. If ADDR is an at an
1562 odd address, then the instruction will be crossing a
1563 four byte boundary, and must be adjusted. */
1564 if ((addr & 3) != 0)
1566 insn = bfd_get_16 (abfd, loc);
1569 if ((oinsn & 0xff00) != (insn & 0xff00))
1571 bfd_put_16 (abfd, insn, loc);
1579 ((*_bfd_error_handler)
1580 ("%s: 0x%lx: fatal: reloc overflow while relaxing",
1581 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1582 bfd_set_error (bfd_error_bad_value);
1591 /* Relocate an SH ELF section. */
1594 sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1595 contents, relocs, local_syms, local_sections)
1597 struct bfd_link_info *info;
1599 asection *input_section;
1601 Elf_Internal_Rela *relocs;
1602 Elf_Internal_Sym *local_syms;
1603 asection **local_sections;
1605 Elf_Internal_Shdr *symtab_hdr;
1606 struct elf_link_hash_entry **sym_hashes;
1607 Elf_Internal_Rela *rel, *relend;
1609 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1610 sym_hashes = elf_sym_hashes (input_bfd);
1613 relend = relocs + input_section->reloc_count;
1614 for (; rel < relend; rel++)
1617 reloc_howto_type *howto;
1618 unsigned long r_symndx;
1619 Elf_Internal_Sym *sym;
1621 struct elf_link_hash_entry *h;
1623 bfd_reloc_status_type r;
1625 r_symndx = ELF32_R_SYM (rel->r_info);
1627 if (info->relocateable)
1629 /* This is a relocateable link. We don't have to change
1630 anything, unless the reloc is against a section symbol,
1631 in which case we have to adjust according to where the
1632 section symbol winds up in the output section. */
1633 if (r_symndx < symtab_hdr->sh_info)
1635 sym = local_syms + r_symndx;
1636 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1638 sec = local_sections[r_symndx];
1639 rel->r_addend += sec->output_offset + sym->st_value;
1646 r_type = ELF32_R_TYPE (rel->r_info);
1648 /* Many of the relocs are only used for relaxing, and are
1649 handled entirely by the relaxation code. */
1650 if (r_type > (int) LAST_INVALID_RELOC)
1654 || r_type >= (int) FIRST_INVALID_RELOC)
1656 bfd_set_error (bfd_error_bad_value);
1660 /* FIXME: This is certainly incorrect. However, it is how the
1661 COFF linker works. */
1662 if (r_type != (int) R_SH_DIR32
1663 && r_type != (int) R_SH_IND12W)
1666 howto = sh_elf_howto_table + r_type;
1668 /* This is a final link. */
1672 if (r_symndx < symtab_hdr->sh_info)
1674 /* There is nothing to be done for an internal IND12W
1675 relocation. FIXME: This is probably wrong, but it's how
1676 the COFF relocations work. */
1677 if (r_type == (int) R_SH_IND12W)
1679 sym = local_syms + r_symndx;
1680 sec = local_sections[r_symndx];
1681 relocation = (sec->output_section->vma
1682 + sec->output_offset
1687 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1688 while (h->root.type == bfd_link_hash_indirect
1689 || h->root.type == bfd_link_hash_warning)
1690 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1691 if (h->root.type == bfd_link_hash_defined
1692 || h->root.type == bfd_link_hash_defweak)
1694 sec = h->root.u.def.section;
1695 relocation = (h->root.u.def.value
1696 + sec->output_section->vma
1697 + sec->output_offset);
1699 else if (h->root.type == bfd_link_hash_undefweak)
1703 if (! ((*info->callbacks->undefined_symbol)
1704 (info, h->root.root.string, input_bfd,
1705 input_section, rel->r_offset)))
1711 /* FIXME: This is how the COFF relocations work. */
1712 if (r_type == (int) R_SH_IND12W)
1715 /* FIXME: We should use the addend, but the COFF relocations
1717 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1718 contents, rel->r_offset,
1721 if (r != bfd_reloc_ok)
1726 case bfd_reloc_outofrange:
1728 case bfd_reloc_overflow:
1733 name = h->root.root.string;
1736 name = (bfd_elf_string_from_elf_section
1737 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1741 name = bfd_section_name (input_bfd, sec);
1743 if (! ((*info->callbacks->reloc_overflow)
1744 (info, name, howto->name, (bfd_vma) 0,
1745 input_bfd, input_section, rel->r_offset)))
1756 /* This is a version of bfd_generic_get_relocated_section_contents
1757 which uses sh_elf_relocate_section. */
1760 sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1761 data, relocateable, symbols)
1763 struct bfd_link_info *link_info;
1764 struct bfd_link_order *link_order;
1766 boolean relocateable;
1769 Elf_Internal_Shdr *symtab_hdr;
1770 asection *input_section = link_order->u.indirect.section;
1771 bfd *input_bfd = input_section->owner;
1772 asection **sections = NULL;
1773 Elf_Internal_Rela *internal_relocs = NULL;
1774 Elf32_External_Sym *external_syms = NULL;
1775 Elf_Internal_Sym *internal_syms = NULL;
1777 /* We only need to handle the case of relaxing, or of having a
1778 particular set of section contents, specially. */
1780 || elf_section_data (input_section)->this_hdr.contents == NULL)
1781 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1786 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1788 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1789 input_section->_raw_size);
1791 if ((input_section->flags & SEC_RELOC) != 0
1792 && input_section->reloc_count > 0)
1794 Elf_Internal_Sym *isymp;
1796 Elf32_External_Sym *esym, *esymend;
1798 if (symtab_hdr->contents != NULL)
1799 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1802 external_syms = ((Elf32_External_Sym *)
1803 bfd_malloc (symtab_hdr->sh_info
1804 * sizeof (Elf32_External_Sym)));
1805 if (external_syms == NULL && symtab_hdr->sh_info > 0)
1807 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1808 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1809 symtab_hdr->sh_info, input_bfd)
1810 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1814 internal_relocs = (_bfd_elf32_link_read_relocs
1815 (input_bfd, input_section, (PTR) NULL,
1816 (Elf_Internal_Rela *) NULL, false));
1817 if (internal_relocs == NULL)
1820 internal_syms = ((Elf_Internal_Sym *)
1821 bfd_malloc (symtab_hdr->sh_info
1822 * sizeof (Elf_Internal_Sym)));
1823 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1826 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1827 * sizeof (asection *));
1828 if (sections == NULL && symtab_hdr->sh_info > 0)
1831 isymp = internal_syms;
1833 esym = external_syms;
1834 esymend = esym + symtab_hdr->sh_info;
1835 for (; esym < esymend; ++esym, ++isymp, ++secpp)
1839 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1841 if (isymp->st_shndx == SHN_UNDEF)
1842 isec = bfd_und_section_ptr;
1843 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1844 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1845 else if (isymp->st_shndx == SHN_ABS)
1846 isec = bfd_abs_section_ptr;
1847 else if (isymp->st_shndx == SHN_COMMON)
1848 isec = bfd_com_section_ptr;
1858 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
1859 input_section, data, internal_relocs,
1860 internal_syms, sections))
1863 if (sections != NULL)
1866 if (internal_syms != NULL)
1867 free (internal_syms);
1868 internal_syms = NULL;
1869 if (external_syms != NULL && symtab_hdr->contents == NULL)
1870 free (external_syms);
1871 external_syms = NULL;
1872 if (internal_relocs != elf_section_data (input_section)->relocs)
1873 free (internal_relocs);
1874 internal_relocs = NULL;
1880 if (internal_relocs != NULL
1881 && internal_relocs != elf_section_data (input_section)->relocs)
1882 free (internal_relocs);
1883 if (external_syms != NULL && symtab_hdr->contents == NULL)
1884 free (external_syms);
1885 if (internal_syms != NULL)
1886 free (internal_syms);
1887 if (sections != NULL)
1892 #define TARGET_BIG_SYM bfd_elf32_sh_vec
1893 #define TARGET_BIG_NAME "elf32-sh"
1894 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
1895 #define TARGET_LITTLE_NAME "elf32-shl"
1896 #define ELF_ARCH bfd_arch_sh
1897 #define ELF_MACHINE_CODE EM_SH
1898 #define ELF_MAXPAGESIZE 0x1
1900 #define elf_symbol_leading_char '_'
1902 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
1903 #define elf_info_to_howto sh_elf_info_to_howto
1904 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
1905 #define elf_backend_relocate_section sh_elf_relocate_section
1906 #define bfd_elf32_bfd_get_relocated_section_contents \
1907 sh_elf_get_relocated_section_contents
1909 #include "elf32-target.h"