1 /* Hitachi SH specific support for 32-bit ELF
2 Copyright 1996, 97, 98, 1999, 2000 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. */
28 static bfd_reloc_status_type sh_elf_reloc
29 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
30 static bfd_reloc_status_type sh_elf_ignore_reloc
31 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
32 static reloc_howto_type *sh_elf_reloc_type_lookup
33 PARAMS ((bfd *, bfd_reloc_code_real_type));
34 static void sh_elf_info_to_howto
35 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
36 static boolean sh_elf_set_private_flags
37 PARAMS ((bfd *, flagword));
38 static boolean sh_elf_copy_private_data
39 PARAMS ((bfd *, bfd *));
40 static boolean sh_elf_merge_private_data
41 PARAMS ((bfd *, bfd *));
42 static boolean sh_elf_set_mach_from_flags
44 static boolean sh_elf_relax_section
45 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
46 static boolean sh_elf_relax_delete_bytes
47 PARAMS ((bfd *, asection *, bfd_vma, int));
48 static boolean sh_elf_align_loads
49 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, boolean *));
50 static boolean sh_elf_swap_insns
51 PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma));
52 static boolean sh_elf_relocate_section
53 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
54 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
55 static bfd_byte *sh_elf_get_relocated_section_contents
56 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
57 bfd_byte *, boolean, asymbol **));
58 static boolean sh_elf_check_relocs
59 PARAMS ((bfd *, struct bfd_link_info *, asection *,
60 const Elf_Internal_Rela *));
61 static struct bfd_hash_entry *sh_elf_link_hash_newfunc
62 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
63 static struct bfd_link_hash_table *sh_elf_link_hash_table_create
65 static boolean sh_elf_adjust_dynamic_symbol
66 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
67 static boolean sh_elf_size_dynamic_sections
68 PARAMS ((bfd *, struct bfd_link_info *));
69 static boolean sh_elf_finish_dynamic_symbol
70 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
72 static boolean sh_elf_finish_dynamic_sections
73 PARAMS ((bfd *, struct bfd_link_info *));
75 /* The name of the dynamic interpreter. This is put in the .interp
78 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
80 static reloc_howto_type sh_elf_howto_table[] =
83 HOWTO (R_SH_NONE, /* type */
85 0, /* size (0 = byte, 1 = short, 2 = long) */
87 false, /* pc_relative */
89 complain_overflow_dont, /* complain_on_overflow */
90 sh_elf_ignore_reloc, /* special_function */
91 "R_SH_NONE", /* name */
92 false, /* partial_inplace */
95 false), /* pcrel_offset */
97 /* 32 bit absolute relocation. Setting partial_inplace to true and
98 src_mask to a non-zero value is similar to the COFF toolchain. */
99 HOWTO (R_SH_DIR32, /* type */
101 2, /* size (0 = byte, 1 = short, 2 = long) */
103 false, /* pc_relative */
105 complain_overflow_bitfield, /* complain_on_overflow */
106 sh_elf_reloc, /* special_function */
107 "R_SH_DIR32", /* name */
108 true, /* partial_inplace */
109 0xffffffff, /* src_mask */
110 0xffffffff, /* dst_mask */
111 false), /* pcrel_offset */
113 /* 32 bit PC relative relocation. */
114 HOWTO (R_SH_REL32, /* type */
116 2, /* size (0 = byte, 1 = short, 2 = long) */
118 true, /* pc_relative */
120 complain_overflow_signed, /* complain_on_overflow */
121 sh_elf_ignore_reloc, /* special_function */
122 "R_SH_REL32", /* name */
123 false, /* partial_inplace */
125 0xffffffff, /* dst_mask */
126 true), /* pcrel_offset */
128 /* 8 bit PC relative branch divided by 2. */
129 HOWTO (R_SH_DIR8WPN, /* type */
131 1, /* size (0 = byte, 1 = short, 2 = long) */
133 true, /* pc_relative */
135 complain_overflow_signed, /* complain_on_overflow */
136 sh_elf_ignore_reloc, /* special_function */
137 "R_SH_DIR8WPN", /* name */
138 true, /* partial_inplace */
141 true), /* pcrel_offset */
143 /* 12 bit PC relative branch divided by 2. */
144 HOWTO (R_SH_IND12W, /* type */
146 1, /* size (0 = byte, 1 = short, 2 = long) */
148 true, /* pc_relative */
150 complain_overflow_signed, /* complain_on_overflow */
151 sh_elf_reloc, /* special_function */
152 "R_SH_IND12W", /* name */
153 true, /* partial_inplace */
154 0xfff, /* src_mask */
155 0xfff, /* dst_mask */
156 true), /* pcrel_offset */
158 /* 8 bit unsigned PC relative divided by 4. */
159 HOWTO (R_SH_DIR8WPL, /* type */
161 1, /* size (0 = byte, 1 = short, 2 = long) */
163 true, /* pc_relative */
165 complain_overflow_unsigned, /* complain_on_overflow */
166 sh_elf_ignore_reloc, /* special_function */
167 "R_SH_DIR8WPL", /* name */
168 true, /* partial_inplace */
171 true), /* pcrel_offset */
173 /* 8 bit unsigned PC relative divided by 2. */
174 HOWTO (R_SH_DIR8WPZ, /* type */
176 1, /* size (0 = byte, 1 = short, 2 = long) */
178 true, /* pc_relative */
180 complain_overflow_unsigned, /* complain_on_overflow */
181 sh_elf_ignore_reloc, /* special_function */
182 "R_SH_DIR8WPZ", /* name */
183 true, /* partial_inplace */
186 true), /* pcrel_offset */
188 /* 8 bit GBR relative. FIXME: This only makes sense if we have some
189 special symbol for the GBR relative area, and that is not
191 HOWTO (R_SH_DIR8BP, /* type */
193 1, /* size (0 = byte, 1 = short, 2 = long) */
195 false, /* pc_relative */
197 complain_overflow_unsigned, /* complain_on_overflow */
198 sh_elf_ignore_reloc, /* special_function */
199 "R_SH_DIR8BP", /* name */
200 false, /* partial_inplace */
203 true), /* pcrel_offset */
205 /* 8 bit GBR relative divided by 2. FIXME: This only makes sense if
206 we have some special symbol for the GBR relative area, and that
207 is not implemented. */
208 HOWTO (R_SH_DIR8W, /* type */
210 1, /* size (0 = byte, 1 = short, 2 = long) */
212 false, /* pc_relative */
214 complain_overflow_unsigned, /* complain_on_overflow */
215 sh_elf_ignore_reloc, /* special_function */
216 "R_SH_DIR8W", /* name */
217 false, /* partial_inplace */
220 true), /* pcrel_offset */
222 /* 8 bit GBR relative divided by 4. FIXME: This only makes sense if
223 we have some special symbol for the GBR relative area, and that
224 is not implemented. */
225 HOWTO (R_SH_DIR8L, /* type */
227 1, /* size (0 = byte, 1 = short, 2 = long) */
229 false, /* pc_relative */
231 complain_overflow_unsigned, /* complain_on_overflow */
232 sh_elf_ignore_reloc, /* special_function */
233 "R_SH_DIR8L", /* name */
234 false, /* partial_inplace */
237 true), /* pcrel_offset */
239 HOWTO (R_SH_GOT32, /* type */
241 2, /* size (0 = byte, 1 = short, 2 = long) */
243 false, /* pc_relative */
245 complain_overflow_bitfield, /* complain_on_overflow */
246 bfd_elf_generic_reloc, /* */
247 "R_SH_GOT32", /* name */
248 true, /* partial_inplace */
249 0xffffffff, /* src_mask */
250 0xffffffff, /* dst_mask */
251 false), /* pcrel_offset */
253 HOWTO (R_SH_PLT32, /* type */
255 2, /* size (0 = byte, 1 = short, 2 = long) */
257 true, /* pc_relative */
259 complain_overflow_bitfield, /* complain_on_overflow */
260 bfd_elf_generic_reloc, /* */
261 "R_SH_PLT32", /* name */
262 true, /* partial_inplace */
263 0xffffffff, /* src_mask */
264 0xffffffff, /* dst_mask */
265 true), /* pcrel_offset */
267 HOWTO (R_SH_COPY, /* type */
269 2, /* size (0 = byte, 1 = short, 2 = long) */
271 false, /* pc_relative */
273 complain_overflow_bitfield, /* complain_on_overflow */
274 bfd_elf_generic_reloc, /* */
275 "R_SH_COPY", /* name */
276 true, /* partial_inplace */
277 0xffffffff, /* src_mask */
278 0xffffffff, /* dst_mask */
279 false), /* pcrel_offset */
281 HOWTO (R_SH_GLOB_DAT, /* type */
283 2, /* size (0 = byte, 1 = short, 2 = long) */
285 false, /* pc_relative */
287 complain_overflow_bitfield, /* complain_on_overflow */
288 bfd_elf_generic_reloc, /* */
289 "R_SH_GLOB_DAT", /* name */
290 true, /* partial_inplace */
291 0xffffffff, /* src_mask */
292 0xffffffff, /* dst_mask */
293 false), /* pcrel_offset */
295 HOWTO (R_SH_JMP_SLOT, /* type */
297 2, /* size (0 = byte, 1 = short, 2 = long) */
299 false, /* pc_relative */
301 complain_overflow_bitfield, /* complain_on_overflow */
302 bfd_elf_generic_reloc, /* */
303 "R_SH_JMP_SLOT", /* name */
304 true, /* partial_inplace */
305 0xffffffff, /* src_mask */
306 0xffffffff, /* dst_mask */
307 false), /* pcrel_offset */
309 HOWTO (R_SH_RELATIVE, /* type */
311 2, /* size (0 = byte, 1 = short, 2 = long) */
313 false, /* pc_relative */
315 complain_overflow_bitfield, /* complain_on_overflow */
316 bfd_elf_generic_reloc, /* */
317 "R_SH_RELATIVE", /* name */
318 true, /* partial_inplace */
319 0xffffffff, /* src_mask */
320 0xffffffff, /* dst_mask */
321 false), /* pcrel_offset */
323 HOWTO (R_SH_GOTOFF, /* type */
325 2, /* size (0 = byte, 1 = short, 2 = long) */
327 false, /* pc_relative */
329 complain_overflow_bitfield, /* complain_on_overflow */
330 bfd_elf_generic_reloc, /* */
331 "R_SH_GOTOFF", /* name */
332 true, /* partial_inplace */
333 0xffffffff, /* src_mask */
334 0xffffffff, /* dst_mask */
335 false), /* pcrel_offset */
337 HOWTO (R_SH_GOTPC, /* type */
339 2, /* size (0 = byte, 1 = short, 2 = long) */
341 true, /* pc_relative */
343 complain_overflow_bitfield, /* complain_on_overflow */
344 bfd_elf_generic_reloc, /* */
345 "R_SH_GOTPC", /* name */
346 true, /* partial_inplace */
347 0xffffffff, /* src_mask */
348 0xffffffff, /* dst_mask */
349 true), /* pcrel_offset */
359 /* The remaining relocs are a GNU extension used for relaxing. The
360 final pass of the linker never needs to do anything with any of
361 these relocs. Any required operations are handled by the
364 /* A 16 bit switch table entry. This is generated for an expression
365 such as ``.word L1 - L2''. The offset holds the difference
366 between the reloc address and L2. */
367 HOWTO (R_SH_SWITCH16, /* type */
369 1, /* size (0 = byte, 1 = short, 2 = long) */
371 false, /* pc_relative */
373 complain_overflow_unsigned, /* complain_on_overflow */
374 sh_elf_ignore_reloc, /* special_function */
375 "R_SH_SWITCH16", /* name */
376 false, /* partial_inplace */
379 true), /* pcrel_offset */
381 /* A 32 bit switch table entry. This is generated for an expression
382 such as ``.long L1 - L2''. The offset holds the difference
383 between the reloc address and L2. */
384 HOWTO (R_SH_SWITCH32, /* type */
386 2, /* size (0 = byte, 1 = short, 2 = long) */
388 false, /* pc_relative */
390 complain_overflow_unsigned, /* complain_on_overflow */
391 sh_elf_ignore_reloc, /* special_function */
392 "R_SH_SWITCH32", /* name */
393 false, /* partial_inplace */
396 true), /* pcrel_offset */
398 /* Indicates a .uses pseudo-op. The compiler will generate .uses
399 pseudo-ops when it finds a function call which can be relaxed.
400 The offset field holds the PC relative offset to the instruction
401 which loads the register used in the function call. */
402 HOWTO (R_SH_USES, /* type */
404 1, /* size (0 = byte, 1 = short, 2 = long) */
406 false, /* pc_relative */
408 complain_overflow_unsigned, /* complain_on_overflow */
409 sh_elf_ignore_reloc, /* special_function */
410 "R_SH_USES", /* name */
411 false, /* partial_inplace */
414 true), /* pcrel_offset */
416 /* The assembler will generate this reloc for addresses referred to
417 by the register loads associated with USES relocs. The offset
418 field holds the number of times the address is referenced in the
420 HOWTO (R_SH_COUNT, /* type */
422 1, /* size (0 = byte, 1 = short, 2 = long) */
424 false, /* pc_relative */
426 complain_overflow_unsigned, /* complain_on_overflow */
427 sh_elf_ignore_reloc, /* special_function */
428 "R_SH_COUNT", /* name */
429 false, /* partial_inplace */
432 true), /* pcrel_offset */
434 /* Indicates an alignment statement. The offset field is the power
435 of 2 to which subsequent portions of the object file must be
437 HOWTO (R_SH_ALIGN, /* type */
439 1, /* size (0 = byte, 1 = short, 2 = long) */
441 false, /* pc_relative */
443 complain_overflow_unsigned, /* complain_on_overflow */
444 sh_elf_ignore_reloc, /* special_function */
445 "R_SH_ALIGN", /* name */
446 false, /* partial_inplace */
449 true), /* pcrel_offset */
451 /* The assembler will generate this reloc before a block of
452 instructions. A section should be processed as assumining it
453 contains data, unless this reloc is seen. */
454 HOWTO (R_SH_CODE, /* type */
456 1, /* size (0 = byte, 1 = short, 2 = long) */
458 false, /* pc_relative */
460 complain_overflow_unsigned, /* complain_on_overflow */
461 sh_elf_ignore_reloc, /* special_function */
462 "R_SH_CODE", /* name */
463 false, /* partial_inplace */
466 true), /* pcrel_offset */
468 /* The assembler will generate this reloc after a block of
469 instructions when it sees data that is not instructions. */
470 HOWTO (R_SH_DATA, /* type */
472 1, /* size (0 = byte, 1 = short, 2 = long) */
474 false, /* pc_relative */
476 complain_overflow_unsigned, /* complain_on_overflow */
477 sh_elf_ignore_reloc, /* special_function */
478 "R_SH_DATA", /* name */
479 false, /* partial_inplace */
482 true), /* pcrel_offset */
484 /* The assembler generates this reloc for each label within a block
485 of instructions. This permits the linker to avoid swapping
486 instructions which are the targets of branches. */
487 HOWTO (R_SH_LABEL, /* type */
489 1, /* size (0 = byte, 1 = short, 2 = long) */
491 false, /* pc_relative */
493 complain_overflow_unsigned, /* complain_on_overflow */
494 sh_elf_ignore_reloc, /* special_function */
495 "R_SH_LABEL", /* name */
496 false, /* partial_inplace */
499 true), /* pcrel_offset */
501 /* An 8 bit switch table entry. This is generated for an expression
502 such as ``.word L1 - L2''. The offset holds the difference
503 between the reloc address and L2. */
504 HOWTO (R_SH_SWITCH8, /* type */
506 0, /* size (0 = byte, 1 = short, 2 = long) */
508 false, /* pc_relative */
510 complain_overflow_unsigned, /* complain_on_overflow */
511 sh_elf_ignore_reloc, /* special_function */
512 "R_SH_SWITCH8", /* name */
513 false, /* partial_inplace */
516 true), /* pcrel_offset */
518 /* GNU extension to record C++ vtable hierarchy */
519 HOWTO (R_SH_GNU_VTINHERIT, /* type */
521 2, /* size (0 = byte, 1 = short, 2 = long) */
523 false, /* pc_relative */
525 complain_overflow_dont, /* complain_on_overflow */
526 NULL, /* special_function */
527 "R_SH_GNU_VTINHERIT", /* name */
528 false, /* partial_inplace */
531 false), /* pcrel_offset */
533 /* GNU extension to record C++ vtable member usage */
534 HOWTO (R_SH_GNU_VTENTRY, /* type */
536 2, /* size (0 = byte, 1 = short, 2 = long) */
538 false, /* pc_relative */
540 complain_overflow_dont, /* complain_on_overflow */
541 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
542 "R_SH_GNU_VTENTRY", /* name */
543 false, /* partial_inplace */
546 false), /* pcrel_offset */
548 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
549 HOWTO (R_SH_LOOP_START, /* type */
551 1, /* size (0 = byte, 1 = short, 2 = long) */
553 false, /* pc_relative */
555 complain_overflow_signed, /* complain_on_overflow */
556 sh_elf_ignore_reloc, /* special_function */
557 "R_SH_LOOP_START", /* name */
558 true, /* partial_inplace */
561 true), /* pcrel_offset */
563 /* 8 bit PC relative divided by 2 - but specified in a very odd way. */
564 HOWTO (R_SH_LOOP_END, /* type */
566 1, /* size (0 = byte, 1 = short, 2 = long) */
568 false, /* pc_relative */
570 complain_overflow_signed, /* complain_on_overflow */
571 sh_elf_ignore_reloc, /* special_function */
572 "R_SH_LOOP_END", /* name */
573 true, /* partial_inplace */
576 true), /* pcrel_offset */
580 static bfd_reloc_status_type
581 sh_elf_reloc_loop (r_type, input_bfd, input_section, contents, addr,
582 symbol_section, start, end)
583 int r_type ATTRIBUTE_UNUSED;
585 asection *input_section;
588 asection *symbol_section;
591 static bfd_vma last_addr;
592 static asection *last_symbol_section;
593 bfd_byte *free_contents = NULL;
594 bfd_byte *start_ptr, *ptr, *last_ptr;
599 /* Sanity check the address. */
600 if (addr > input_section->_raw_size)
601 return bfd_reloc_outofrange;
603 /* We require the start and end relocations to be processed consecutively -
604 although we allow then to be processed forwards or backwards. */
608 last_symbol_section = symbol_section;
611 if (last_addr != addr)
615 if (! symbol_section || last_symbol_section != symbol_section || end < start)
616 return bfd_reloc_outofrange;
618 /* Get the symbol_section contents. */
619 if (symbol_section != input_section)
621 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
622 contents = elf_section_data (symbol_section)->this_hdr.contents;
625 free_contents = contents
626 = (bfd_byte *) bfd_malloc (symbol_section->_raw_size);
627 if (contents == NULL)
628 return bfd_reloc_outofrange;
629 if (! bfd_get_section_contents (input_bfd, symbol_section, contents,
631 symbol_section->_raw_size))
634 return bfd_reloc_outofrange;
638 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
639 start_ptr = contents + start;
640 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
642 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
645 diff = (last_ptr - ptr) >> 1;
646 cum_diff += diff & 1;
649 /* Calculate the start / end values to load into rs / re minus four -
650 so that will cancel out the four we would otherwise have to add to
651 addr to get the value to subtract in order to get relative addressing. */
655 end = (ptr + cum_diff * 2) - contents;
659 bfd_vma start0 = start - 4;
661 while (start0 >= 0 && IS_PPI (contents + start0))
663 start0 = start - 2 - ((start - start0) & 2);
664 start = start0 - cum_diff - 2;
669 free (free_contents);
671 insn = bfd_get_16 (input_bfd, contents + addr);
673 x = (insn & 0x200 ? end : start) - addr;
674 if (input_section != symbol_section)
675 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
676 - (input_section->output_section->vma
677 + input_section->output_offset));
679 if (x < -128 || x > 127)
680 return bfd_reloc_overflow;
682 x = (insn & ~0xff) | (x & 0xff);
683 bfd_put_16 (input_bfd, x, contents + addr);
688 /* This function is used for normal relocs. This used to be like the COFF
689 function, and is almost certainly incorrect for other ELF targets. */
691 static bfd_reloc_status_type
692 sh_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
695 arelent *reloc_entry;
698 asection *input_section;
700 char **error_message ATTRIBUTE_UNUSED;
704 enum elf_sh_reloc_type r_type;
705 bfd_vma addr = reloc_entry->address;
706 bfd_byte *hit_data = addr + (bfd_byte *) data;
708 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
710 if (output_bfd != NULL)
712 /* Partial linking--do nothing. */
713 reloc_entry->address += input_section->output_offset;
717 /* Almost all relocs have to do with relaxing. If any work must be
718 done for them, it has been done in sh_relax_section. */
719 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
722 if (symbol_in != NULL
723 && bfd_is_und_section (symbol_in->section))
724 return bfd_reloc_undefined;
726 if (bfd_is_com_section (symbol_in->section))
729 sym_value = (symbol_in->value +
730 symbol_in->section->output_section->vma +
731 symbol_in->section->output_offset);
736 insn = bfd_get_32 (abfd, hit_data);
737 insn += sym_value + reloc_entry->addend;
738 bfd_put_32 (abfd, insn, hit_data);
741 insn = bfd_get_16 (abfd, hit_data);
742 sym_value += reloc_entry->addend;
743 sym_value -= (input_section->output_section->vma
744 + input_section->output_offset
747 sym_value += (insn & 0xfff) << 1;
750 insn = (insn & 0xf000) | (sym_value & 0xfff);
751 bfd_put_16 (abfd, insn, hit_data);
752 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
753 return bfd_reloc_overflow;
763 /* This function is used for relocs which are only used for relaxing,
764 which the linker should otherwise ignore. */
766 static bfd_reloc_status_type
767 sh_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
768 output_bfd, error_message)
769 bfd *abfd ATTRIBUTE_UNUSED;
770 arelent *reloc_entry;
771 asymbol *symbol ATTRIBUTE_UNUSED;
772 PTR data ATTRIBUTE_UNUSED;
773 asection *input_section;
775 char **error_message ATTRIBUTE_UNUSED;
777 if (output_bfd != NULL)
778 reloc_entry->address += input_section->output_offset;
782 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
786 bfd_reloc_code_real_type bfd_reloc_val;
787 unsigned char elf_reloc_val;
790 /* An array mapping BFD reloc codes to SH ELF relocs. */
792 static const struct elf_reloc_map sh_reloc_map[] =
794 { BFD_RELOC_NONE, R_SH_NONE },
795 { BFD_RELOC_32, R_SH_DIR32 },
796 { BFD_RELOC_CTOR, R_SH_DIR32 },
797 { BFD_RELOC_32_PCREL, R_SH_REL32 },
798 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
799 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
800 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
801 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
802 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
803 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
804 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
805 { BFD_RELOC_SH_USES, R_SH_USES },
806 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
807 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
808 { BFD_RELOC_SH_CODE, R_SH_CODE },
809 { BFD_RELOC_SH_DATA, R_SH_DATA },
810 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
811 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
812 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
813 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
814 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
815 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
816 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
817 { BFD_RELOC_SH_COPY, R_SH_COPY },
818 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
819 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
820 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
821 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
822 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
825 /* Given a BFD reloc code, return the howto structure for the
826 corresponding SH ELf reloc. */
828 static reloc_howto_type *
829 sh_elf_reloc_type_lookup (abfd, code)
830 bfd *abfd ATTRIBUTE_UNUSED;
831 bfd_reloc_code_real_type code;
835 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
837 if (sh_reloc_map[i].bfd_reloc_val == code)
838 return &sh_elf_howto_table[(int) sh_reloc_map[i].elf_reloc_val];
844 /* Given an ELF reloc, fill in the howto field of a relent. */
847 sh_elf_info_to_howto (abfd, cache_ptr, dst)
848 bfd *abfd ATTRIBUTE_UNUSED;
850 Elf_Internal_Rela *dst;
854 r = ELF32_R_TYPE (dst->r_info);
856 BFD_ASSERT (r < (unsigned int) R_SH_max);
857 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
859 cache_ptr->howto = &sh_elf_howto_table[r];
862 /* This function handles relaxing for SH ELF. See the corresponding
863 function in coff-sh.c for a description of what this does. FIXME:
864 There is a lot of duplication here between this code and the COFF
865 specific code. The format of relocs and symbols is wound deeply
866 into this code, but it would still be better if the duplication
867 could be eliminated somehow. Note in particular that although both
868 functions use symbols like R_SH_CODE, those symbols have different
869 values; in coff-sh.c they come from include/coff/sh.h, whereas here
870 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
873 sh_elf_relax_section (abfd, sec, link_info, again)
876 struct bfd_link_info *link_info;
879 Elf_Internal_Shdr *symtab_hdr;
880 Elf_Internal_Rela *internal_relocs;
881 Elf_Internal_Rela *free_relocs = NULL;
883 Elf_Internal_Rela *irel, *irelend;
884 bfd_byte *contents = NULL;
885 bfd_byte *free_contents = NULL;
886 Elf32_External_Sym *extsyms = NULL;
887 Elf32_External_Sym *free_extsyms = NULL;
891 if (link_info->relocateable
892 || (sec->flags & SEC_RELOC) == 0
893 || sec->reloc_count == 0)
896 /* If this is the first time we have been called for this section,
897 initialize the cooked size. */
898 if (sec->_cooked_size == 0)
899 sec->_cooked_size = sec->_raw_size;
901 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
903 internal_relocs = (_bfd_elf32_link_read_relocs
904 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
905 link_info->keep_memory));
906 if (internal_relocs == NULL)
908 if (! link_info->keep_memory)
909 free_relocs = internal_relocs;
913 irelend = internal_relocs + sec->reloc_count;
914 for (irel = internal_relocs; irel < irelend; irel++)
916 bfd_vma laddr, paddr, symval;
918 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
921 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
924 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
927 /* Get the section contents. */
928 if (contents == NULL)
930 if (elf_section_data (sec)->this_hdr.contents != NULL)
931 contents = elf_section_data (sec)->this_hdr.contents;
934 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
935 if (contents == NULL)
937 free_contents = contents;
939 if (! bfd_get_section_contents (abfd, sec, contents,
940 (file_ptr) 0, sec->_raw_size))
945 /* The r_addend field of the R_SH_USES reloc will point us to
946 the register load. The 4 is because the r_addend field is
947 computed as though it were a jump offset, which are based
948 from 4 bytes after the jump instruction. */
949 laddr = irel->r_offset + 4 + irel->r_addend;
950 if (laddr >= sec->_raw_size)
952 (*_bfd_error_handler) (_("%s: 0x%lx: warning: bad R_SH_USES offset"),
953 bfd_get_filename (abfd),
954 (unsigned long) irel->r_offset);
957 insn = bfd_get_16 (abfd, contents + laddr);
959 /* If the instruction is not mov.l NN,rN, we don't know what to
961 if ((insn & 0xf000) != 0xd000)
963 ((*_bfd_error_handler)
964 (_("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
965 bfd_get_filename (abfd), (unsigned long) irel->r_offset, insn));
969 /* Get the address from which the register is being loaded. The
970 displacement in the mov.l instruction is quadrupled. It is a
971 displacement from four bytes after the movl instruction, but,
972 before adding in the PC address, two least significant bits
973 of the PC are cleared. We assume that the section is aligned
974 on a four byte boundary. */
977 paddr += (laddr + 4) &~ 3;
978 if (paddr >= sec->_raw_size)
980 ((*_bfd_error_handler)
981 (_("%s: 0x%lx: warning: bad R_SH_USES load offset"),
982 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
986 /* Get the reloc for the address from which the register is
987 being loaded. This reloc will tell us which function is
988 actually being called. */
989 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
990 if (irelfn->r_offset == paddr
991 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
993 if (irelfn >= irelend)
995 ((*_bfd_error_handler)
996 (_("%s: 0x%lx: warning: could not find expected reloc"),
997 bfd_get_filename (abfd), (unsigned long) paddr));
1001 /* Read this BFD's symbols if we haven't done so already. */
1002 if (extsyms == NULL)
1004 if (symtab_hdr->contents != NULL)
1005 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1008 extsyms = ((Elf32_External_Sym *)
1009 bfd_malloc (symtab_hdr->sh_size));
1010 if (extsyms == NULL)
1012 free_extsyms = extsyms;
1013 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1014 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1015 != symtab_hdr->sh_size))
1020 /* Get the value of the symbol referred to by the reloc. */
1021 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
1023 Elf_Internal_Sym isym;
1025 /* A local symbol. */
1026 bfd_elf32_swap_symbol_in (abfd,
1027 extsyms + ELF32_R_SYM (irelfn->r_info),
1030 if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec))
1032 ((*_bfd_error_handler)
1033 (_("%s: 0x%lx: warning: symbol in unexpected section"),
1034 bfd_get_filename (abfd), (unsigned long) paddr));
1038 symval = (isym.st_value
1039 + sec->output_section->vma
1040 + sec->output_offset);
1045 struct elf_link_hash_entry *h;
1047 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
1048 h = elf_sym_hashes (abfd)[indx];
1049 BFD_ASSERT (h != NULL);
1050 if (h->root.type != bfd_link_hash_defined
1051 && h->root.type != bfd_link_hash_defweak)
1053 /* This appears to be a reference to an undefined
1054 symbol. Just ignore it--it will be caught by the
1055 regular reloc processing. */
1059 symval = (h->root.u.def.value
1060 + h->root.u.def.section->output_section->vma
1061 + h->root.u.def.section->output_offset);
1064 symval += bfd_get_32 (abfd, contents + paddr);
1066 /* See if this function call can be shortened. */
1069 + sec->output_section->vma
1070 + sec->output_offset
1072 if (foff < -0x1000 || foff >= 0x1000)
1074 /* After all that work, we can't shorten this function call. */
1078 /* Shorten the function call. */
1080 /* For simplicity of coding, we are going to modify the section
1081 contents, the section relocs, and the BFD symbol table. We
1082 must tell the rest of the code not to free up this
1083 information. It would be possible to instead create a table
1084 of changes which have to be made, as is done in coff-mips.c;
1085 that would be more work, but would require less memory when
1086 the linker is run. */
1088 elf_section_data (sec)->relocs = internal_relocs;
1091 elf_section_data (sec)->this_hdr.contents = contents;
1092 free_contents = NULL;
1094 symtab_hdr->contents = (bfd_byte *) extsyms;
1095 free_extsyms = NULL;
1097 /* Replace the jsr with a bsr. */
1099 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
1100 replace the jsr with a bsr. */
1101 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
1102 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
1104 /* If this needs to be changed because of future relaxing,
1105 it will be handled here like other internal IND12W
1108 0xb000 | ((foff >> 1) & 0xfff),
1109 contents + irel->r_offset);
1113 /* We can't fully resolve this yet, because the external
1114 symbol value may be changed by future relaxing. We let
1115 the final link phase handle it. */
1116 bfd_put_16 (abfd, 0xb000, contents + irel->r_offset);
1119 /* See if there is another R_SH_USES reloc referring to the same
1121 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
1122 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
1123 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
1125 if (irelscan < irelend)
1127 /* Some other function call depends upon this register load,
1128 and we have not yet converted that function call.
1129 Indeed, we may never be able to convert it. There is
1130 nothing else we can do at this point. */
1134 /* Look for a R_SH_COUNT reloc on the location where the
1135 function address is stored. Do this before deleting any
1136 bytes, to avoid confusion about the address. */
1137 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
1138 if (irelcount->r_offset == paddr
1139 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
1142 /* Delete the register load. */
1143 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
1146 /* That will change things, so, just in case it permits some
1147 other function call to come within range, we should relax
1148 again. Note that this is not required, and it may be slow. */
1151 /* Now check whether we got a COUNT reloc. */
1152 if (irelcount >= irelend)
1154 ((*_bfd_error_handler)
1155 (_("%s: 0x%lx: warning: could not find expected COUNT reloc"),
1156 bfd_get_filename (abfd), (unsigned long) paddr));
1160 /* The number of uses is stored in the r_addend field. We've
1161 just deleted one. */
1162 if (irelcount->r_addend == 0)
1164 ((*_bfd_error_handler) (_("%s: 0x%lx: warning: bad count"),
1165 bfd_get_filename (abfd),
1166 (unsigned long) paddr));
1170 --irelcount->r_addend;
1172 /* If there are no more uses, we can delete the address. Reload
1173 the address from irelfn, in case it was changed by the
1174 previous call to sh_elf_relax_delete_bytes. */
1175 if (irelcount->r_addend == 0)
1177 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
1181 /* We've done all we can with that function call. */
1184 /* Look for load and store instructions that we can align on four
1190 /* Get the section contents. */
1191 if (contents == NULL)
1193 if (elf_section_data (sec)->this_hdr.contents != NULL)
1194 contents = elf_section_data (sec)->this_hdr.contents;
1197 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1198 if (contents == NULL)
1200 free_contents = contents;
1202 if (! bfd_get_section_contents (abfd, sec, contents,
1203 (file_ptr) 0, sec->_raw_size))
1208 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
1214 elf_section_data (sec)->relocs = internal_relocs;
1217 elf_section_data (sec)->this_hdr.contents = contents;
1218 free_contents = NULL;
1220 symtab_hdr->contents = (bfd_byte *) extsyms;
1221 free_extsyms = NULL;
1225 if (free_relocs != NULL)
1231 if (free_contents != NULL)
1233 if (! link_info->keep_memory)
1234 free (free_contents);
1237 /* Cache the section contents for elf_link_input_bfd. */
1238 elf_section_data (sec)->this_hdr.contents = contents;
1240 free_contents = NULL;
1243 if (free_extsyms != NULL)
1245 if (! link_info->keep_memory)
1246 free (free_extsyms);
1249 /* Cache the symbols for elf_link_input_bfd. */
1250 symtab_hdr->contents = extsyms;
1252 free_extsyms = NULL;
1258 if (free_relocs != NULL)
1260 if (free_contents != NULL)
1261 free (free_contents);
1262 if (free_extsyms != NULL)
1263 free (free_extsyms);
1267 /* Delete some bytes from a section while relaxing. FIXME: There is a
1268 lot of duplication between this function and sh_relax_delete_bytes
1272 sh_elf_relax_delete_bytes (abfd, sec, addr, count)
1278 Elf_Internal_Shdr *symtab_hdr;
1279 Elf32_External_Sym *extsyms;
1282 Elf_Internal_Rela *irel, *irelend;
1283 Elf_Internal_Rela *irelalign;
1285 Elf32_External_Sym *esym, *esymend;
1286 struct elf_link_hash_entry *sym_hash;
1289 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1290 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1292 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1294 contents = elf_section_data (sec)->this_hdr.contents;
1296 /* The deletion must stop at the next ALIGN reloc for an aligment
1297 power larger than the number of bytes we are deleting. */
1300 toaddr = sec->_cooked_size;
1302 irel = elf_section_data (sec)->relocs;
1303 irelend = irel + sec->reloc_count;
1304 for (; irel < irelend; irel++)
1306 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1307 && irel->r_offset > addr
1308 && count < (1 << irel->r_addend))
1311 toaddr = irel->r_offset;
1316 /* Actually delete the bytes. */
1317 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1318 if (irelalign == NULL)
1319 sec->_cooked_size -= count;
1324 #define NOP_OPCODE (0x0009)
1326 BFD_ASSERT ((count & 1) == 0);
1327 for (i = 0; i < count; i += 2)
1328 bfd_put_16 (abfd, NOP_OPCODE, contents + toaddr - count + i);
1331 /* Adjust all the relocs. */
1332 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1334 bfd_vma nraddr, stop;
1337 Elf_Internal_Sym sym;
1338 int off, adjust, oinsn;
1339 bfd_signed_vma voff = 0;
1342 /* Get the new reloc address. */
1343 nraddr = irel->r_offset;
1344 if ((irel->r_offset > addr
1345 && irel->r_offset < toaddr)
1346 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1347 && irel->r_offset == toaddr))
1350 /* See if this reloc was for the bytes we have deleted, in which
1351 case we no longer care about it. Don't delete relocs which
1352 represent addresses, though. */
1353 if (irel->r_offset >= addr
1354 && irel->r_offset < addr + count
1355 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
1356 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
1357 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
1358 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
1359 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1362 /* If this is a PC relative reloc, see if the range it covers
1363 includes the bytes we have deleted. */
1364 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1373 start = irel->r_offset;
1374 insn = bfd_get_16 (abfd, contents + nraddr);
1378 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1381 start = stop = addr;
1385 /* If this reloc is against a symbol defined in this
1386 section, and the symbol will not be adjusted below, we
1387 must check the addend to see it will put the value in
1388 range to be adjusted, and hence must be changed. */
1389 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1391 bfd_elf32_swap_symbol_in (abfd,
1392 extsyms + ELF32_R_SYM (irel->r_info),
1394 if (sym.st_shndx == shndx
1395 && (sym.st_value <= addr
1396 || sym.st_value >= toaddr))
1400 val = bfd_get_32 (abfd, contents + nraddr);
1401 val += sym.st_value;
1402 if (val > addr && val < toaddr)
1403 bfd_put_32 (abfd, val - count, contents + nraddr);
1406 start = stop = addr;
1413 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1417 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
1418 start = stop = addr;
1424 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1430 stop = start + 4 + off * 2;
1435 stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
1441 /* These relocs types represent
1443 The r_addend field holds the difference between the reloc
1444 address and L1. That is the start of the reloc, and
1445 adding in the contents gives us the top. We must adjust
1446 both the r_offset field and the section contents.
1447 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1448 and the elf bfd r_offset is called r_vaddr. */
1450 stop = irel->r_offset;
1451 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1455 && (stop <= addr || stop >= toaddr))
1456 irel->r_addend += count;
1457 else if (stop > addr
1459 && (start <= addr || start >= toaddr))
1460 irel->r_addend -= count;
1462 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1463 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1464 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1465 voff = bfd_get_8 (abfd, contents + nraddr);
1467 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1468 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1473 start = irel->r_offset;
1474 stop = (bfd_vma) ((bfd_signed_vma) start
1475 + (long) irel->r_addend
1482 && (stop <= addr || stop >= toaddr))
1484 else if (stop > addr
1486 && (start <= addr || start >= toaddr))
1495 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1504 if ((oinsn & 0xff00) != (insn & 0xff00))
1506 bfd_put_16 (abfd, insn, contents + nraddr);
1511 if ((oinsn & 0xf000) != (insn & 0xf000))
1513 bfd_put_16 (abfd, insn, contents + nraddr);
1517 BFD_ASSERT (adjust == count || count >= 4);
1522 if ((irel->r_offset & 3) == 0)
1525 if ((oinsn & 0xff00) != (insn & 0xff00))
1527 bfd_put_16 (abfd, insn, contents + nraddr);
1532 if (voff < 0 || voff >= 0xff)
1534 bfd_put_8 (abfd, voff, contents + nraddr);
1539 if (voff < - 0x8000 || voff >= 0x8000)
1541 bfd_put_signed_16 (abfd, voff, contents + nraddr);
1546 bfd_put_signed_32 (abfd, voff, contents + nraddr);
1550 irel->r_addend += adjust;
1556 ((*_bfd_error_handler)
1557 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1558 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1559 bfd_set_error (bfd_error_bad_value);
1564 irel->r_offset = nraddr;
1567 /* Look through all the other sections. If there contain any IMM32
1568 relocs against internal symbols which we are not going to adjust
1569 below, we may need to adjust the addends. */
1570 for (o = abfd->sections; o != NULL; o = o->next)
1572 Elf_Internal_Rela *internal_relocs;
1573 Elf_Internal_Rela *irelscan, *irelscanend;
1574 bfd_byte *ocontents;
1577 || (o->flags & SEC_RELOC) == 0
1578 || o->reloc_count == 0)
1581 /* We always cache the relocs. Perhaps, if info->keep_memory is
1582 false, we should free them, if we are permitted to, when we
1583 leave sh_coff_relax_section. */
1584 internal_relocs = (_bfd_elf32_link_read_relocs
1585 (abfd, o, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1587 if (internal_relocs == NULL)
1591 irelscanend = internal_relocs + o->reloc_count;
1592 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1594 Elf_Internal_Sym sym;
1596 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1597 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1599 bfd_vma start, stop;
1600 bfd_signed_vma voff;
1602 if (ocontents == NULL)
1604 if (elf_section_data (o)->this_hdr.contents != NULL)
1605 ocontents = elf_section_data (o)->this_hdr.contents;
1608 /* We always cache the section contents.
1609 Perhaps, if info->keep_memory is false, we
1610 should free them, if we are permitted to,
1611 when we leave sh_coff_relax_section. */
1612 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1613 if (ocontents == NULL)
1615 if (! bfd_get_section_contents (abfd, o, ocontents,
1619 elf_section_data (o)->this_hdr.contents = ocontents;
1623 stop = irelscan->r_offset;
1625 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1627 /* STOP is in a different section, so it won't change. */
1628 if (start > addr && start < toaddr)
1629 irelscan->r_addend += count;
1631 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1632 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1636 && (stop <= addr || stop >= toaddr))
1637 bfd_put_signed_32 (abfd, voff + count,
1638 ocontents + irelscan->r_offset);
1639 else if (stop > addr
1641 && (start <= addr || start >= toaddr))
1642 bfd_put_signed_32 (abfd, voff - count,
1643 ocontents + irelscan->r_offset);
1646 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1649 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1652 bfd_elf32_swap_symbol_in (abfd,
1653 extsyms + ELF32_R_SYM (irelscan->r_info),
1656 if (sym.st_shndx == shndx
1657 && (sym.st_value <= addr
1658 || sym.st_value >= toaddr))
1662 if (ocontents == NULL)
1664 if (elf_section_data (o)->this_hdr.contents != NULL)
1665 ocontents = elf_section_data (o)->this_hdr.contents;
1668 /* We always cache the section contents.
1669 Perhaps, if info->keep_memory is false, we
1670 should free them, if we are permitted to,
1671 when we leave sh_coff_relax_section. */
1672 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1673 if (ocontents == NULL)
1675 if (! bfd_get_section_contents (abfd, o, ocontents,
1679 elf_section_data (o)->this_hdr.contents = ocontents;
1683 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1684 val += sym.st_value;
1685 if (val > addr && val < toaddr)
1686 bfd_put_32 (abfd, val - count,
1687 ocontents + irelscan->r_offset);
1692 /* Adjust the local symbols defined in this section. */
1694 esymend = esym + symtab_hdr->sh_info;
1695 for (; esym < esymend; esym++)
1697 Elf_Internal_Sym isym;
1699 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1701 if (isym.st_shndx == shndx
1702 && isym.st_value > addr
1703 && isym.st_value < toaddr)
1705 isym.st_value -= count;
1706 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1710 /* Now adjust the global symbols defined in this section. */
1711 esym = extsyms + symtab_hdr->sh_info;
1712 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1713 for (index = 0; esym < esymend; esym++, index++)
1715 Elf_Internal_Sym isym;
1717 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1718 sym_hash = elf_sym_hashes (abfd)[index];
1719 if (isym.st_shndx == shndx
1720 && ((sym_hash)->root.type == bfd_link_hash_defined
1721 || (sym_hash)->root.type == bfd_link_hash_defweak)
1722 && (sym_hash)->root.u.def.section == sec
1723 && (sym_hash)->root.u.def.value > addr
1724 && (sym_hash)->root.u.def.value < toaddr)
1726 (sym_hash)->root.u.def.value -= count;
1730 /* See if we can move the ALIGN reloc forward. We have adjusted
1731 r_offset for it already. */
1732 if (irelalign != NULL)
1734 bfd_vma alignto, alignaddr;
1736 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1737 alignaddr = BFD_ALIGN (irelalign->r_offset,
1738 1 << irelalign->r_addend);
1739 if (alignto != alignaddr)
1741 /* Tail recursion. */
1742 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1743 alignto - alignaddr);
1750 /* Look for loads and stores which we can align to four byte
1751 boundaries. This is like sh_align_loads in coff-sh.c. */
1754 sh_elf_align_loads (abfd, sec, internal_relocs, contents, pswapped)
1757 Elf_Internal_Rela *internal_relocs;
1761 Elf_Internal_Rela *irel, *irelend;
1762 bfd_vma *labels = NULL;
1763 bfd_vma *label, *label_end;
1767 irelend = internal_relocs + sec->reloc_count;
1769 /* Get all the addresses with labels on them. */
1770 labels = (bfd_vma *) bfd_malloc (sec->reloc_count * sizeof (bfd_vma));
1774 for (irel = internal_relocs; irel < irelend; irel++)
1776 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1778 *label_end = irel->r_offset;
1783 /* Note that the assembler currently always outputs relocs in
1784 address order. If that ever changes, this code will need to sort
1785 the label values and the relocs. */
1789 for (irel = internal_relocs; irel < irelend; irel++)
1791 bfd_vma start, stop;
1793 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1796 start = irel->r_offset;
1798 for (irel++; irel < irelend; irel++)
1799 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1802 stop = irel->r_offset;
1804 stop = sec->_cooked_size;
1806 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1807 (PTR) internal_relocs, &label,
1808 label_end, start, stop, pswapped))
1822 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1825 sh_elf_swap_insns (abfd, sec, relocs, contents, addr)
1832 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1833 unsigned short i1, i2;
1834 Elf_Internal_Rela *irel, *irelend;
1836 /* Swap the instructions themselves. */
1837 i1 = bfd_get_16 (abfd, contents + addr);
1838 i2 = bfd_get_16 (abfd, contents + addr + 2);
1839 bfd_put_16 (abfd, i2, contents + addr);
1840 bfd_put_16 (abfd, i1, contents + addr + 2);
1842 /* Adjust all reloc addresses. */
1843 irelend = internal_relocs + sec->reloc_count;
1844 for (irel = internal_relocs; irel < irelend; irel++)
1846 enum elf_sh_reloc_type type;
1849 /* There are a few special types of relocs that we don't want to
1850 adjust. These relocs do not apply to the instruction itself,
1851 but are only associated with the address. */
1852 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1853 if (type == R_SH_ALIGN
1854 || type == R_SH_CODE
1855 || type == R_SH_DATA
1856 || type == R_SH_LABEL)
1859 /* If an R_SH_USES reloc points to one of the addresses being
1860 swapped, we must adjust it. It would be incorrect to do this
1861 for a jump, though, since we want to execute both
1862 instructions after the jump. (We have avoided swapping
1863 around a label, so the jump will not wind up executing an
1864 instruction it shouldn't). */
1865 if (type == R_SH_USES)
1869 off = irel->r_offset + 4 + irel->r_addend;
1871 irel->r_offset += 2;
1872 else if (off == addr + 2)
1873 irel->r_offset -= 2;
1876 if (irel->r_offset == addr)
1878 irel->r_offset += 2;
1881 else if (irel->r_offset == addr + 2)
1883 irel->r_offset -= 2;
1892 unsigned short insn, oinsn;
1895 loc = contents + irel->r_offset;
1904 insn = bfd_get_16 (abfd, loc);
1907 if ((oinsn & 0xff00) != (insn & 0xff00))
1909 bfd_put_16 (abfd, insn, loc);
1913 insn = bfd_get_16 (abfd, loc);
1916 if ((oinsn & 0xf000) != (insn & 0xf000))
1918 bfd_put_16 (abfd, insn, loc);
1922 /* This reloc ignores the least significant 3 bits of
1923 the program counter before adding in the offset.
1924 This means that if ADDR is at an even address, the
1925 swap will not affect the offset. If ADDR is an at an
1926 odd address, then the instruction will be crossing a
1927 four byte boundary, and must be adjusted. */
1928 if ((addr & 3) != 0)
1930 insn = bfd_get_16 (abfd, loc);
1933 if ((oinsn & 0xff00) != (insn & 0xff00))
1935 bfd_put_16 (abfd, insn, loc);
1943 ((*_bfd_error_handler)
1944 (_("%s: 0x%lx: fatal: reloc overflow while relaxing"),
1945 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1946 bfd_set_error (bfd_error_bad_value);
1955 /* The size in bytes of an entry in the procedure linkage table. */
1957 #define PLT_ENTRY_SIZE 28
1959 /* First entry in an absolute procedure linkage table look like this. */
1961 static const bfd_byte elf_sh_plt0_entry_be[PLT_ENTRY_SIZE] =
1963 0xd0, 0x04, /* mov.l 1f,r0 */
1964 0xd2, 0x05, /* mov.l 2f,r2 */
1965 0x60, 0x02, /* mov.l @r0,r0 */
1966 0x62, 0x22, /* mov.l @r2,r2 */
1967 0x40, 0x2b, /* jmp @r0 */
1968 0xe0, 0x00, /* mov #0,r0 */
1969 0x00, 0x09, /* nop */
1970 0x00, 0x09, /* nop */
1971 0x00, 0x09, /* nop */
1972 0x00, 0x09, /* nop */
1973 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1974 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1977 static const bfd_byte elf_sh_plt0_entry_le[PLT_ENTRY_SIZE] =
1979 0x04, 0xd0, /* mov.l 1f,r0 */
1980 0x05, 0xd2, /* mov.l 2f,r2 */
1981 0x02, 0x60, /* mov.l @r0,r0 */
1982 0x22, 0x62, /* mov.l @r2,r2 */
1983 0x2b, 0x40, /* jmp @r0 */
1984 0x00, 0xe0, /* mov #0,r0 */
1985 0x09, 0x00, /* nop */
1986 0x09, 0x00, /* nop */
1987 0x09, 0x00, /* nop */
1988 0x09, 0x00, /* nop */
1989 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1990 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1993 /* Sebsequent entries in an absolute procedure linkage table look like
1996 static const bfd_byte elf_sh_plt_entry_be[PLT_ENTRY_SIZE] =
1998 0xd0, 0x04, /* mov.l 1f,r0 */
1999 0x60, 0x02, /* mov.l @r0,r0 */
2000 0xd2, 0x02, /* mov.l 0f,r2 */
2001 0x40, 0x2b, /* jmp @r0 */
2002 0x60, 0x23, /* mov r2,r0 */
2003 0xd1, 0x03, /* mov.l 2f,r1 */
2004 0x40, 0x2b, /* jmp @r0 */
2005 0x00, 0x09, /* nop */
2006 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
2007 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2008 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2011 static const bfd_byte elf_sh_plt_entry_le[PLT_ENTRY_SIZE] =
2013 0x04, 0xd0, /* mov.l 1f,r0 */
2014 0x02, 0x60, /* mov.l @r0,r0 */
2015 0x02, 0xd2, /* mov.l 0f,r2 */
2016 0x2b, 0x40, /* jmp @r0 */
2017 0x23, 0x60, /* mov r2,r0 */
2018 0x03, 0xd1, /* mov.l 2f,r1 */
2019 0x2b, 0x40, /* jmp @r0 */
2020 0x09, 0x00, /* nop */
2021 0, 0, 0, 0, /* 0: replaced with address of .PLT. */
2022 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2023 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
2026 /* Entries in a PIC procedure linkage table look like this. */
2028 static const bfd_byte elf_sh_pic_plt_entry_be[PLT_ENTRY_SIZE] =
2030 0xd0, 0x04, /* mov.l 1f,r0 */
2031 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2032 0x40, 0x2b, /* jmp @r0 */
2033 0x00, 0x09, /* nop */
2034 0x50, 0xc2, /* 0: mov.l @(8,r12),r0 */
2035 0x52, 0xc1, /* 1: mov.l @(4,r12),r2 */
2036 0xd1, 0x02, /* mov.l 2f,r1 */
2037 0x40, 0x2b, /* jmp @r0 */
2038 0xe0, 0x00, /* mov #0,r0 ! shows the type of PLT. */
2039 0x00, 0x09, /* nop */
2040 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2041 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2044 static const bfd_byte elf_sh_pic_plt_entry_le[PLT_ENTRY_SIZE] =
2046 0x04, 0xd0, /* mov.l 1f,r0 */
2047 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2048 0x2b, 0x40, /* jmp @r0 */
2049 0x09, 0x00, /* nop */
2050 0xc2, 0x50, /* 0: mov.l @(8,r12),r0 */
2051 0xc1, 0x52, /* 1: mov.l @(4,r12),r2 */
2052 0x02, 0xd1, /* mov.l 2f,r1 */
2053 0x2b, 0x40, /* jmp @r0 */
2054 0x00, 0xe0, /* mov #0,r0 ! shows the type of PLT. */
2055 0x09, 0x00, /* nop */
2056 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
2057 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
2060 static const bfd_byte *elf_sh_plt0_entry;
2061 static const bfd_byte *elf_sh_plt_entry;
2062 static const bfd_byte *elf_sh_pic_plt_entry;
2064 /* Return size of a PLT entry. */
2065 #define elf_sh_sizeof_plt(info) PLT_ENTRY_SIZE
2067 /* Return offset of the PLT0 address in an absolute PLT entry. */
2068 #define elf_sh_plt_plt0_offset(info) 16
2070 /* Return offset of the linker in PLT0 entry. */
2071 #define elf_sh_plt0_linker_offset(info) 20
2073 /* Return offset of the GOT id in PLT0 entry. */
2074 #define elf_sh_plt0_gotid_offset(info) 24
2076 /* Return offset of the tempoline in PLT entry */
2077 #define elf_sh_plt_temp_offset(info) 8
2079 /* Return offset of the symbol in PLT entry. */
2080 #define elf_sh_plt_symbol_offset(info) 20
2082 /* Return offset of the relocation in PLT entry. */
2083 #define elf_sh_plt_reloc_offset(info) 24
2085 /* The sh linker needs to keep track of the number of relocs that it
2086 decides to copy in check_relocs for each symbol. This is so that
2087 it can discard PC relative relocs if it doesn't need them when
2088 linking with -Bsymbolic. We store the information in a field
2089 extending the regular ELF linker hash table. */
2091 /* This structure keeps track of the number of PC relative relocs we
2092 have copied for a given symbol. */
2094 struct elf_sh_pcrel_relocs_copied
2097 struct elf_sh_pcrel_relocs_copied *next;
2098 /* A section in dynobj. */
2100 /* Number of relocs copied in this section. */
2101 bfd_size_type count;
2104 /* sh ELF linker hash entry. */
2106 struct elf_sh_link_hash_entry
2108 struct elf_link_hash_entry root;
2110 /* Number of PC relative relocs copied for this symbol. */
2111 struct elf_sh_pcrel_relocs_copied *pcrel_relocs_copied;
2114 /* sh ELF linker hash table. */
2116 struct elf_sh_link_hash_table
2118 struct elf_link_hash_table root;
2121 /* Declare this now that the above structures are defined. */
2123 static boolean sh_elf_discard_copies
2124 PARAMS ((struct elf_sh_link_hash_entry *, PTR));
2126 /* Traverse an sh ELF linker hash table. */
2128 #define sh_elf_link_hash_traverse(table, func, info) \
2129 (elf_link_hash_traverse \
2131 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
2134 /* Get the sh ELF linker hash table from a link_info structure. */
2136 #define sh_elf_hash_table(p) \
2137 ((struct elf_sh_link_hash_table *) ((p)->hash))
2139 /* Create an entry in an sh ELF linker hash table. */
2141 static struct bfd_hash_entry *
2142 sh_elf_link_hash_newfunc (entry, table, string)
2143 struct bfd_hash_entry *entry;
2144 struct bfd_hash_table *table;
2147 struct elf_sh_link_hash_entry *ret =
2148 (struct elf_sh_link_hash_entry *) entry;
2150 /* Allocate the structure if it has not already been allocated by a
2152 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2153 ret = ((struct elf_sh_link_hash_entry *)
2154 bfd_hash_allocate (table,
2155 sizeof (struct elf_sh_link_hash_entry)));
2156 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2157 return (struct bfd_hash_entry *) ret;
2159 /* Call the allocation method of the superclass. */
2160 ret = ((struct elf_sh_link_hash_entry *)
2161 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2163 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2165 ret->pcrel_relocs_copied = NULL;
2168 return (struct bfd_hash_entry *) ret;
2171 /* Create an sh ELF linker hash table. */
2173 static struct bfd_link_hash_table *
2174 sh_elf_link_hash_table_create (abfd)
2177 struct elf_sh_link_hash_table *ret;
2179 ret = ((struct elf_sh_link_hash_table *)
2180 bfd_alloc (abfd, sizeof (struct elf_sh_link_hash_table)));
2181 if (ret == (struct elf_sh_link_hash_table *) NULL)
2184 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2185 sh_elf_link_hash_newfunc))
2187 bfd_release (abfd, ret);
2191 return &ret->root.root;
2194 /* Create dynamic sections when linking against a dynamic object. */
2197 sh_elf_create_dynamic_sections (abfd, info)
2199 struct bfd_link_info *info;
2201 flagword flags, pltflags;
2202 register asection *s;
2203 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2206 switch (bed->s->arch_size)
2217 bfd_set_error (bfd_error_bad_value);
2221 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2222 .rel[a].bss sections. */
2224 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2225 | SEC_LINKER_CREATED);
2228 pltflags |= SEC_CODE;
2229 if (bed->plt_not_loaded)
2230 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2231 if (bed->plt_readonly)
2232 pltflags |= SEC_READONLY;
2234 s = bfd_make_section (abfd, ".plt");
2236 || ! bfd_set_section_flags (abfd, s, pltflags)
2237 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2240 if (bed->want_plt_sym)
2242 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2244 struct elf_link_hash_entry *h = NULL;
2245 if (! (_bfd_generic_link_add_one_symbol
2246 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2247 (bfd_vma) 0, (const char *) NULL, false,
2248 get_elf_backend_data (abfd)->collect,
2249 (struct bfd_link_hash_entry **) &h)))
2251 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2252 h->type = STT_OBJECT;
2255 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2259 s = bfd_make_section (abfd,
2260 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
2262 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2263 || ! bfd_set_section_alignment (abfd, s, ptralign))
2266 if (! _bfd_elf_create_got_section (abfd, info))
2270 const char *secname;
2275 for (sec = abfd->sections; sec; sec = sec->next)
2277 secflags = bfd_get_section_flags (abfd, sec);
2278 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
2279 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
2281 secname = bfd_get_section_name (abfd, sec);
2282 relname = (char *) bfd_malloc (strlen (secname) + 6);
2283 strcpy (relname, ".rela");
2284 strcat (relname, secname);
2285 s = bfd_make_section (abfd, relname);
2287 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2288 || ! bfd_set_section_alignment (abfd, s, ptralign))
2293 if (bed->want_dynbss)
2295 /* The .dynbss section is a place to put symbols which are defined
2296 by dynamic objects, are referenced by regular objects, and are
2297 not functions. We must allocate space for them in the process
2298 image and use a R_*_COPY reloc to tell the dynamic linker to
2299 initialize them at run time. The linker script puts the .dynbss
2300 section into the .bss section of the final image. */
2301 s = bfd_make_section (abfd, ".dynbss");
2303 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
2306 /* The .rel[a].bss section holds copy relocs. This section is not
2307 normally needed. We need to create it here, though, so that the
2308 linker will map it to an output section. We can't just create it
2309 only if we need it, because we will not know whether we need it
2310 until we have seen all the input files, and the first time the
2311 main linker code calls BFD after examining all the input files
2312 (size_dynamic_sections) the input sections have already been
2313 mapped to the output sections. If the section turns out not to
2314 be needed, we can discard it later. We will never need this
2315 section when generating a shared object, since they do not use
2319 s = bfd_make_section (abfd,
2320 (bed->default_use_rela_p
2321 ? ".rela.bss" : ".rel.bss"));
2323 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2324 || ! bfd_set_section_alignment (abfd, s, ptralign))
2333 /* Adjust a symbol defined by a dynamic object and referenced by a
2334 regular object. The current definition is in some section of the
2335 dynamic object, but we're not including those sections. We have to
2336 change the definition to something the rest of the link can
2340 sh_elf_adjust_dynamic_symbol (info, h)
2341 struct bfd_link_info *info;
2342 struct elf_link_hash_entry *h;
2346 unsigned int power_of_two;
2348 dynobj = elf_hash_table (info)->dynobj;
2350 /* Make sure we know what is going on here. */
2351 BFD_ASSERT (dynobj != NULL
2352 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2353 || h->weakdef != NULL
2354 || ((h->elf_link_hash_flags
2355 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2356 && (h->elf_link_hash_flags
2357 & ELF_LINK_HASH_REF_REGULAR) != 0
2358 && (h->elf_link_hash_flags
2359 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2361 /* If this is a function, put it in the procedure linkage table. We
2362 will fill in the contents of the procedure linkage table later,
2363 when we know the address of the .got section. */
2364 if (h->type == STT_FUNC
2365 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2368 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2369 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
2371 /* This case can occur if we saw a PLT reloc in an input
2372 file, but the symbol was never referred to by a dynamic
2373 object. In such a case, we don't actually need to build
2374 a procedure linkage table, and we can just do a REL32
2376 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
2380 /* Make sure this symbol is output as a dynamic symbol. */
2381 if (h->dynindx == -1)
2383 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2387 s = bfd_get_section_by_name (dynobj, ".plt");
2388 BFD_ASSERT (s != NULL);
2390 /* If this is the first .plt entry, make room for the special
2392 if (s->_raw_size == 0)
2393 s->_raw_size += PLT_ENTRY_SIZE;
2395 /* If this symbol is not defined in a regular file, and we are
2396 not generating a shared library, then set the symbol to this
2397 location in the .plt. This is required to make function
2398 pointers compare as equal between the normal executable and
2399 the shared library. */
2401 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2403 h->root.u.def.section = s;
2404 h->root.u.def.value = s->_raw_size;
2407 h->plt.offset = s->_raw_size;
2409 /* Make room for this entry. */
2410 s->_raw_size += elf_sh_sizeof_plt (info);
2412 /* We also need to make an entry in the .got.plt section, which
2413 will be placed in the .got section by the linker script. */
2415 s = bfd_get_section_by_name (dynobj, ".got.plt");
2416 BFD_ASSERT (s != NULL);
2419 /* We also need to make an entry in the .rela.plt section. */
2421 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2422 BFD_ASSERT (s != NULL);
2423 s->_raw_size += sizeof (Elf32_External_Rela);
2428 /* If this is a weak symbol, and there is a real definition, the
2429 processor independent code will have arranged for us to see the
2430 real definition first, and we can just use the same value. */
2431 if (h->weakdef != NULL)
2433 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2434 || h->weakdef->root.type == bfd_link_hash_defweak);
2435 h->root.u.def.section = h->weakdef->root.u.def.section;
2436 h->root.u.def.value = h->weakdef->root.u.def.value;
2440 /* This is a reference to a symbol defined by a dynamic object which
2441 is not a function. */
2443 /* If we are creating a shared library, we must presume that the
2444 only references to the symbol are via the global offset table.
2445 For such cases we need not do anything here; the relocations will
2446 be handled correctly by relocate_section. */
2450 /* If there are no references to this symbol that do not use the
2451 GOT, we don't need to generate a copy reloc. */
2452 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2455 /* We must allocate the symbol in our .dynbss section, which will
2456 become part of the .bss section of the executable. There will be
2457 an entry for this symbol in the .dynsym section. The dynamic
2458 object will contain position independent code, so all references
2459 from the dynamic object to this symbol will go through the global
2460 offset table. The dynamic linker will use the .dynsym entry to
2461 determine the address it must put in the global offset table, so
2462 both the dynamic object and the regular object will refer to the
2463 same memory location for the variable. */
2465 s = bfd_get_section_by_name (dynobj, ".dynbss");
2466 BFD_ASSERT (s != NULL);
2468 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2469 copy the initial value out of the dynamic object and into the
2470 runtime process image. We need to remember the offset into the
2471 .rela.bss section we are going to use. */
2472 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2476 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2477 BFD_ASSERT (srel != NULL);
2478 srel->_raw_size += sizeof (Elf32_External_Rela);
2479 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2482 /* We need to figure out the alignment required for this symbol. I
2483 have no idea how ELF linkers handle this. */
2484 power_of_two = bfd_log2 (h->size);
2485 if (power_of_two > 3)
2488 /* Apply the required alignment. */
2489 s->_raw_size = BFD_ALIGN (s->_raw_size,
2490 (bfd_size_type) (1 << power_of_two));
2491 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2493 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2497 /* Define the symbol as being at this point in the section. */
2498 h->root.u.def.section = s;
2499 h->root.u.def.value = s->_raw_size;
2501 /* Increment the section size to make room for the symbol. */
2502 s->_raw_size += h->size;
2507 /* Set the sizes of the dynamic sections. */
2510 sh_elf_size_dynamic_sections (output_bfd, info)
2512 struct bfd_link_info *info;
2520 dynobj = elf_hash_table (info)->dynobj;
2521 BFD_ASSERT (dynobj != NULL);
2523 if (elf_hash_table (info)->dynamic_sections_created)
2525 /* Set the contents of the .interp section to the interpreter. */
2528 s = bfd_get_section_by_name (dynobj, ".interp");
2529 BFD_ASSERT (s != NULL);
2530 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2531 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2536 /* We may have created entries in the .rela.got section.
2537 However, if we are not creating the dynamic sections, we will
2538 not actually use these entries. Reset the size of .rela.got,
2539 which will cause it to get stripped from the output file
2541 s = bfd_get_section_by_name (dynobj, ".rela.got");
2546 /* If this is a -Bsymbolic shared link, then we need to discard all
2547 PC relative relocs against symbols defined in a regular object.
2548 We allocated space for them in the check_relocs routine, but we
2549 will not fill them in in the relocate_section routine. */
2550 if (info->shared && info->symbolic)
2551 sh_elf_link_hash_traverse (sh_elf_hash_table (info),
2552 sh_elf_discard_copies,
2555 /* The check_relocs and adjust_dynamic_symbol entry points have
2556 determined the sizes of the various dynamic sections. Allocate
2561 for (s = dynobj->sections; s != NULL; s = s->next)
2566 if ((s->flags & SEC_LINKER_CREATED) == 0)
2569 /* It's OK to base decisions on the section name, because none
2570 of the dynobj section names depend upon the input files. */
2571 name = bfd_get_section_name (dynobj, s);
2575 if (strcmp (name, ".plt") == 0)
2577 if (s->_raw_size == 0)
2579 /* Strip this section if we don't need it; see the
2585 /* Remember whether there is a PLT. */
2589 else if (strncmp (name, ".rela", 5) == 0)
2591 if (s->_raw_size == 0)
2593 /* If we don't need this section, strip it from the
2594 output file. This is mostly to handle .rela.bss and
2595 .rela.plt. We must create both sections in
2596 create_dynamic_sections, because they must be created
2597 before the linker maps input sections to output
2598 sections. The linker does that before
2599 adjust_dynamic_symbol is called, and it is that
2600 function which decides whether anything needs to go
2601 into these sections. */
2608 /* Remember whether there are any reloc sections other
2610 if (strcmp (name, ".rela.plt") != 0)
2612 const char *outname;
2616 /* If this relocation section applies to a read only
2617 section, then we probably need a DT_TEXTREL
2618 entry. The entries in the .rela.plt section
2619 really apply to the .got section, which we
2620 created ourselves and so know is not readonly. */
2621 outname = bfd_get_section_name (output_bfd,
2623 target = bfd_get_section_by_name (output_bfd, outname + 5);
2625 && (target->flags & SEC_READONLY) != 0
2626 && (target->flags & SEC_ALLOC) != 0)
2630 /* We use the reloc_count field as a counter if we need
2631 to copy relocs into the output file. */
2635 else if (strncmp (name, ".got", 4) != 0)
2637 /* It's not one of our sections, so don't allocate space. */
2643 _bfd_strip_section_from_output (info, s);
2647 /* Allocate memory for the section contents. */
2648 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
2649 if (s->contents == NULL && s->_raw_size != 0)
2653 if (elf_hash_table (info)->dynamic_sections_created)
2655 /* Add some entries to the .dynamic section. We fill in the
2656 values later, in sh_elf_finish_dynamic_sections, but we
2657 must add the entries now so that we get the correct size for
2658 the .dynamic section. The DT_DEBUG entry is filled in by the
2659 dynamic linker and used by the debugger. */
2662 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2668 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
2669 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2670 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2671 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2677 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2678 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2679 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2680 sizeof (Elf32_External_Rela)))
2686 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2694 /* This function is called via sh_elf_link_hash_traverse if we are
2695 creating a shared object with -Bsymbolic. It discards the space
2696 allocated to copy PC relative relocs against symbols which are
2697 defined in regular objects. We allocated space for them in the
2698 check_relocs routine, but we won't fill them in in the
2699 relocate_section routine. */
2703 sh_elf_discard_copies (h, ignore)
2704 struct elf_sh_link_hash_entry *h;
2705 PTR ignore ATTRIBUTE_UNUSED;
2707 struct elf_sh_pcrel_relocs_copied *s;
2709 /* We only discard relocs for symbols defined in a regular object. */
2710 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2713 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
2714 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
2720 /* Relocate an SH ELF section. */
2723 sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2724 contents, relocs, local_syms, local_sections)
2725 bfd *output_bfd ATTRIBUTE_UNUSED;
2726 struct bfd_link_info *info;
2728 asection *input_section;
2730 Elf_Internal_Rela *relocs;
2731 Elf_Internal_Sym *local_syms;
2732 asection **local_sections;
2734 Elf_Internal_Shdr *symtab_hdr;
2735 struct elf_link_hash_entry **sym_hashes;
2736 Elf_Internal_Rela *rel, *relend;
2738 bfd_vma *local_got_offsets;
2743 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2744 sym_hashes = elf_sym_hashes (input_bfd);
2745 dynobj = elf_hash_table (info)->dynobj;
2746 local_got_offsets = elf_local_got_offsets (input_bfd);
2753 relend = relocs + input_section->reloc_count;
2754 for (; rel < relend; rel++)
2757 reloc_howto_type *howto;
2758 unsigned long r_symndx;
2759 Elf_Internal_Sym *sym;
2761 struct elf_link_hash_entry *h;
2763 bfd_vma addend = (bfd_vma)0;
2764 bfd_reloc_status_type r;
2766 r_symndx = ELF32_R_SYM (rel->r_info);
2768 if (info->relocateable)
2770 /* This is a relocateable link. We don't have to change
2771 anything, unless the reloc is against a section symbol,
2772 in which case we have to adjust according to where the
2773 section symbol winds up in the output section. */
2774 if (r_symndx < symtab_hdr->sh_info)
2776 sym = local_syms + r_symndx;
2777 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2779 sec = local_sections[r_symndx];
2780 rel->r_addend += sec->output_offset + sym->st_value;
2787 r_type = ELF32_R_TYPE (rel->r_info);
2789 /* Many of the relocs are only used for relaxing, and are
2790 handled entirely by the relaxation code. */
2791 if (r_type > (int) R_SH_LAST_INVALID_RELOC
2792 && r_type < (int) R_SH_LOOP_START)
2796 || r_type >= R_SH_max
2797 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
2798 && r_type <= (int) R_SH_LAST_INVALID_RELOC))
2800 bfd_set_error (bfd_error_bad_value);
2804 howto = sh_elf_howto_table + r_type;
2806 /* This is a final link. */
2810 if (r_symndx < symtab_hdr->sh_info)
2812 sym = local_syms + r_symndx;
2813 sec = local_sections[r_symndx];
2814 relocation = (sec->output_section->vma
2815 + sec->output_offset
2820 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2821 while (h->root.type == bfd_link_hash_indirect
2822 || h->root.type == bfd_link_hash_warning)
2823 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2824 if (h->root.type == bfd_link_hash_defined
2825 || h->root.type == bfd_link_hash_defweak)
2827 sec = h->root.u.def.section;
2828 /* In these cases, we don't need the relocation value.
2829 We check specially because in some obscure cases
2830 sec->output_section will be NULL. */
2831 if (r_type == R_SH_GOTPC
2832 || (r_type == R_SH_PLT32
2833 && h->plt.offset != (bfd_vma) -1)
2834 || (r_type == R_SH_GOT32
2835 && elf_hash_table (info)->dynamic_sections_created
2837 || (! info->symbolic && h->dynindx != -1)
2838 || (h->elf_link_hash_flags
2839 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2840 /* The cases above are those in which relocation is
2841 overwritten in the switch block below. The cases
2842 below are those in which we must defer relocation
2843 to run-time, because we can't resolve absolute
2844 addresses when creating a shared library. */
2846 && ((! info->symbolic && h->dynindx != -1)
2847 || (h->elf_link_hash_flags
2848 & ELF_LINK_HASH_DEF_REGULAR) == 0)
2849 && ((r_type == R_SH_DIR32
2850 && !(ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2851 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN))
2852 || r_type == R_SH_REL32)
2853 && ((input_section->flags & SEC_ALLOC) != 0
2854 /* DWARF will emit R_SH_DIR32 relocations in its
2855 sections against symbols defined externally
2856 in shared libraries. We can't do anything
2858 || (input_section->flags & SEC_DEBUGGING) != 0)))
2860 else if (sec->output_section == NULL)
2862 (*_bfd_error_handler)
2863 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
2864 bfd_get_filename (input_bfd), h->root.root.string,
2865 bfd_get_section_name (input_bfd, input_section));
2869 relocation = (h->root.u.def.value
2870 + sec->output_section->vma
2871 + sec->output_offset);
2873 else if (h->root.type == bfd_link_hash_undefweak)
2875 else if (info->shared && !info->symbolic && !info->no_undefined)
2879 if (! ((*info->callbacks->undefined_symbol)
2880 (info, h->root.root.string, input_bfd,
2881 input_section, rel->r_offset, true)))
2887 switch ((int)r_type)
2889 final_link_relocate:
2890 /* COFF relocs don't use the addend. The addend is used for
2891 R_SH_DIR32 to be compatible with other compilers. */
2892 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2893 contents, rel->r_offset,
2894 relocation, addend);
2901 /* These should normally be handled by the assembler, but at
2902 least IND12W is generated by ourselves, so we must deal
2905 goto final_link_relocate;
2908 bfd_set_error (bfd_error_bad_value);
2914 && (input_section->flags & SEC_ALLOC) != 0
2915 && (r_type != R_SH_REL32
2918 && (! info->symbolic
2919 || (h->elf_link_hash_flags
2920 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2922 Elf_Internal_Rela outrel;
2923 boolean skip, relocate;
2925 /* When generating a shared object, these relocations
2926 are copied into the output file to be resolved at run
2933 name = (bfd_elf_string_from_elf_section
2935 elf_elfheader (input_bfd)->e_shstrndx,
2936 elf_section_data (input_section)->rel_hdr.sh_name));
2940 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2941 && strcmp (bfd_get_section_name (input_bfd,
2945 sreloc = bfd_get_section_by_name (dynobj, name);
2946 BFD_ASSERT (sreloc != NULL);
2951 if (elf_section_data (input_section)->stab_info == NULL)
2952 outrel.r_offset = rel->r_offset;
2957 off = (_bfd_stab_section_offset
2958 (output_bfd, &elf_hash_table (info)->stab_info,
2960 &elf_section_data (input_section)->stab_info,
2962 if (off == (bfd_vma) -1)
2964 outrel.r_offset = off;
2967 outrel.r_offset += (input_section->output_section->vma
2968 + input_section->output_offset);
2972 memset (&outrel, 0, sizeof outrel);
2975 else if (r_type == R_SH_REL32)
2977 BFD_ASSERT (h != NULL && h->dynindx != -1);
2979 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
2980 outrel.r_addend = rel->r_addend;
2984 /* h->dynindx may be -1 if this symbol was marked to
2987 || ((info->symbolic || h->dynindx == -1)
2988 && (h->elf_link_hash_flags
2989 & ELF_LINK_HASH_DEF_REGULAR) != 0))
2992 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
2993 outrel.r_addend = relocation + rel->r_addend;
2997 BFD_ASSERT (h->dynindx != -1);
2999 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
3000 outrel.r_addend = relocation + rel->r_addend;
3004 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3005 (((Elf32_External_Rela *)
3007 + sreloc->reloc_count));
3008 ++sreloc->reloc_count;
3010 /* If this reloc is against an external symbol, we do
3011 not want to fiddle with the addend. Otherwise, we
3012 need to include the symbol value so that it becomes
3013 an addend for the dynamic reloc. */
3017 else if (r_type == R_SH_DIR32)
3018 addend = rel->r_addend;
3019 goto final_link_relocate;
3022 /* Relocation is to the entry for this symbol in the global
3026 sgot = bfd_get_section_by_name (dynobj, ".got");
3027 BFD_ASSERT (sgot != NULL);
3034 off = h->got.offset;
3035 BFD_ASSERT (off != (bfd_vma) -1);
3037 if (! elf_hash_table (info)->dynamic_sections_created
3039 && (info->symbolic || h->dynindx == -1
3040 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3041 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3042 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
3044 /* This is actually a static link, or it is a
3045 -Bsymbolic link and the symbol is defined
3046 locally, or the symbol was forced to be local
3047 because of a version file. We must initialize
3048 this entry in the global offset table. Since the
3049 offset must always be a multiple of 4, we use the
3050 least significant bit to record whether we have
3051 initialized it already.
3053 When doing a dynamic link, we create a .rela.got
3054 relocation entry to initialize the value. This
3055 is done in the finish_dynamic_symbol routine. */
3060 bfd_put_32 (output_bfd, relocation,
3061 sgot->contents + off);
3066 relocation = sgot->output_offset + off;
3072 BFD_ASSERT (local_got_offsets != NULL
3073 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3075 off = local_got_offsets[r_symndx];
3077 /* The offset must always be a multiple of 4. We use
3078 the least significant bit to record whether we have
3079 already generated the necessary reloc. */
3084 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3089 Elf_Internal_Rela outrel;
3091 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3092 BFD_ASSERT (srelgot != NULL);
3094 outrel.r_offset = (sgot->output_section->vma
3095 + sgot->output_offset
3097 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
3098 outrel.r_addend = relocation;
3099 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3100 (((Elf32_External_Rela *)
3102 + srelgot->reloc_count));
3103 ++srelgot->reloc_count;
3106 local_got_offsets[r_symndx] |= 1;
3109 relocation = sgot->output_offset + off;
3112 goto final_link_relocate;
3115 /* Relocation is relative to the start of the global offset
3120 sgot = bfd_get_section_by_name (dynobj, ".got");
3121 BFD_ASSERT (sgot != NULL);
3124 /* Note that sgot->output_offset is not involved in this
3125 calculation. We always want the start of .got. If we
3126 defined _GLOBAL_OFFSET_TABLE in a different way, as is
3127 permitted by the ABI, we might have to change this
3129 relocation -= sgot->output_section->vma;
3131 goto final_link_relocate;
3134 /* Use global offset table as symbol value. */
3138 sgot = bfd_get_section_by_name (dynobj, ".got");
3139 BFD_ASSERT (sgot != NULL);
3142 relocation = sgot->output_section->vma;
3144 goto final_link_relocate;
3147 /* Relocation is to the entry for this symbol in the
3148 procedure linkage table. */
3150 /* Resolve a PLT reloc against a local symbol directly,
3151 without using the procedure linkage table. */
3153 goto final_link_relocate;
3155 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3156 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3157 goto final_link_relocate;
3159 if (h->plt.offset == (bfd_vma) -1)
3161 /* We didn't make a PLT entry for this symbol. This
3162 happens when statically linking PIC code, or when
3163 using -Bsymbolic. */
3164 goto final_link_relocate;
3169 splt = bfd_get_section_by_name (dynobj, ".plt");
3170 BFD_ASSERT (splt != NULL);
3173 relocation = (splt->output_section->vma
3174 + splt->output_offset
3177 goto final_link_relocate;
3179 case R_SH_LOOP_START:
3181 static bfd_vma start, end;
3183 start = (relocation + rel->r_addend
3184 - (sec->output_section->vma + sec->output_offset));
3185 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
3186 rel->r_offset, sec, start, end);
3190 end = (relocation + rel->r_addend
3191 - (sec->output_section->vma + sec->output_offset));
3192 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
3193 rel->r_offset, sec, start, end);
3198 if (r != bfd_reloc_ok)
3203 case bfd_reloc_outofrange:
3205 case bfd_reloc_overflow:
3210 name = h->root.root.string;
3213 name = (bfd_elf_string_from_elf_section
3214 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3218 name = bfd_section_name (input_bfd, sec);
3220 if (! ((*info->callbacks->reloc_overflow)
3221 (info, name, howto->name, (bfd_vma) 0,
3222 input_bfd, input_section, rel->r_offset)))
3233 /* This is a version of bfd_generic_get_relocated_section_contents
3234 which uses sh_elf_relocate_section. */
3237 sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
3238 data, relocateable, symbols)
3240 struct bfd_link_info *link_info;
3241 struct bfd_link_order *link_order;
3243 boolean relocateable;
3246 Elf_Internal_Shdr *symtab_hdr;
3247 asection *input_section = link_order->u.indirect.section;
3248 bfd *input_bfd = input_section->owner;
3249 asection **sections = NULL;
3250 Elf_Internal_Rela *internal_relocs = NULL;
3251 Elf32_External_Sym *external_syms = NULL;
3252 Elf_Internal_Sym *internal_syms = NULL;
3254 /* We only need to handle the case of relaxing, or of having a
3255 particular set of section contents, specially. */
3257 || elf_section_data (input_section)->this_hdr.contents == NULL)
3258 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3263 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3265 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3266 input_section->_raw_size);
3268 if ((input_section->flags & SEC_RELOC) != 0
3269 && input_section->reloc_count > 0)
3271 Elf_Internal_Sym *isymp;
3273 Elf32_External_Sym *esym, *esymend;
3275 if (symtab_hdr->contents != NULL)
3276 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
3279 external_syms = ((Elf32_External_Sym *)
3280 bfd_malloc (symtab_hdr->sh_info
3281 * sizeof (Elf32_External_Sym)));
3282 if (external_syms == NULL && symtab_hdr->sh_info > 0)
3284 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
3285 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
3286 symtab_hdr->sh_info, input_bfd)
3287 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
3291 internal_relocs = (_bfd_elf32_link_read_relocs
3292 (input_bfd, input_section, (PTR) NULL,
3293 (Elf_Internal_Rela *) NULL, false));
3294 if (internal_relocs == NULL)
3297 internal_syms = ((Elf_Internal_Sym *)
3298 bfd_malloc (symtab_hdr->sh_info
3299 * sizeof (Elf_Internal_Sym)));
3300 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
3303 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
3304 * sizeof (asection *));
3305 if (sections == NULL && symtab_hdr->sh_info > 0)
3308 isymp = internal_syms;
3310 esym = external_syms;
3311 esymend = esym + symtab_hdr->sh_info;
3312 for (; esym < esymend; ++esym, ++isymp, ++secpp)
3316 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
3318 if (isymp->st_shndx == SHN_UNDEF)
3319 isec = bfd_und_section_ptr;
3320 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
3321 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
3322 else if (isymp->st_shndx == SHN_ABS)
3323 isec = bfd_abs_section_ptr;
3324 else if (isymp->st_shndx == SHN_COMMON)
3325 isec = bfd_com_section_ptr;
3335 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
3336 input_section, data, internal_relocs,
3337 internal_syms, sections))
3340 if (sections != NULL)
3343 if (internal_syms != NULL)
3344 free (internal_syms);
3345 internal_syms = NULL;
3346 if (external_syms != NULL && symtab_hdr->contents == NULL)
3347 free (external_syms);
3348 external_syms = NULL;
3349 if (internal_relocs != elf_section_data (input_section)->relocs)
3350 free (internal_relocs);
3351 internal_relocs = NULL;
3357 if (internal_relocs != NULL
3358 && internal_relocs != elf_section_data (input_section)->relocs)
3359 free (internal_relocs);
3360 if (external_syms != NULL && symtab_hdr->contents == NULL)
3361 free (external_syms);
3362 if (internal_syms != NULL)
3363 free (internal_syms);
3364 if (sections != NULL)
3369 sh_elf_gc_mark_hook (abfd, info, rel, h, sym)
3371 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3372 Elf_Internal_Rela *rel;
3373 struct elf_link_hash_entry *h;
3374 Elf_Internal_Sym *sym;
3378 switch (ELF32_R_TYPE (rel->r_info))
3380 case R_SH_GNU_VTINHERIT:
3381 case R_SH_GNU_VTENTRY:
3385 switch (h->root.type)
3387 case bfd_link_hash_defined:
3388 case bfd_link_hash_defweak:
3389 return h->root.u.def.section;
3391 case bfd_link_hash_common:
3392 return h->root.u.c.p->section;
3401 if (!(elf_bad_symtab (abfd)
3402 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
3403 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
3404 && sym->st_shndx != SHN_COMMON))
3406 return bfd_section_from_elf_index (abfd, sym->st_shndx);
3412 /* Update the got entry reference counts for the section being removed. */
3415 sh_elf_gc_sweep_hook (abfd, info, sec, relocs)
3416 bfd *abfd ATTRIBUTE_UNUSED;
3417 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3418 asection *sec ATTRIBUTE_UNUSED;
3419 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
3421 /* We use got and plt entries for sh, but it would seem that the
3422 existing SH code does no sort of reference counting or whatnot on
3423 its GOT and PLT entries, so it is not possible to garbage collect
3424 them at this time. */
3428 /* Look through the relocs for a section during the first phase.
3429 Since we don't do .gots or .plts, we just need to consider the
3430 virtual table relocs for gc. */
3433 sh_elf_check_relocs (abfd, info, sec, relocs)
3435 struct bfd_link_info *info;
3437 const Elf_Internal_Rela *relocs;
3439 Elf_Internal_Shdr *symtab_hdr;
3440 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3441 const Elf_Internal_Rela *rel;
3442 const Elf_Internal_Rela *rel_end;
3444 bfd_vma *local_got_offsets;
3454 if (info->relocateable)
3457 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3458 sym_hashes = elf_sym_hashes (abfd);
3459 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
3460 if (!elf_bad_symtab (abfd))
3461 sym_hashes_end -= symtab_hdr->sh_info;
3463 dynobj = elf_hash_table (info)->dynobj;
3464 local_got_offsets = elf_local_got_offsets (abfd);
3466 rel_end = relocs + sec->reloc_count;
3467 for (rel = relocs; rel < rel_end; rel++)
3469 struct elf_link_hash_entry *h;
3470 unsigned long r_symndx;
3472 r_symndx = ELF32_R_SYM (rel->r_info);
3473 if (r_symndx < symtab_hdr->sh_info)
3476 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3478 /* Some relocs require a global offset table. */
3481 switch (ELF32_R_TYPE (rel->r_info))
3486 elf_hash_table (info)->dynobj = dynobj = abfd;
3487 if (! _bfd_elf_create_got_section (dynobj, info))
3496 switch (ELF32_R_TYPE (rel->r_info))
3498 /* This relocation describes the C++ object vtable hierarchy.
3499 Reconstruct it for later use during GC. */
3500 case R_SH_GNU_VTINHERIT:
3501 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3505 /* This relocation describes which C++ vtable entries are actually
3506 used. Record for later use during GC. */
3507 case R_SH_GNU_VTENTRY:
3508 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3513 /* This symbol requires a global offset table entry. */
3517 sgot = bfd_get_section_by_name (dynobj, ".got");
3518 BFD_ASSERT (sgot != NULL);
3522 && (h != NULL || info->shared))
3524 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3525 if (srelgot == NULL)
3527 srelgot = bfd_make_section (dynobj, ".rela.got");
3529 || ! bfd_set_section_flags (dynobj, srelgot,
3534 | SEC_LINKER_CREATED
3536 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
3543 if (h->got.offset != (bfd_vma) -1)
3545 /* We have already allocated space in the .got. */
3548 h->got.offset = sgot->_raw_size;
3550 /* Make sure this symbol is output as a dynamic symbol. */
3551 if (h->dynindx == -1)
3553 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3557 srelgot->_raw_size += sizeof (Elf32_External_Rela);
3561 /* This is a global offset table entry for a local
3563 if (local_got_offsets == NULL)
3566 register unsigned int i;
3568 size = symtab_hdr->sh_info * sizeof (bfd_vma);
3569 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
3570 if (local_got_offsets == NULL)
3572 elf_local_got_offsets (abfd) = local_got_offsets;
3573 for (i = 0; i < symtab_hdr->sh_info; i++)
3574 local_got_offsets[i] = (bfd_vma) -1;
3576 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
3578 /* We have already allocated space in the .got. */
3581 local_got_offsets[r_symndx] = sgot->_raw_size;
3585 /* If we are generating a shared object, we need to
3586 output a R_SH_RELATIVE reloc so that the dynamic
3587 linker can adjust this GOT entry. */
3588 srelgot->_raw_size += sizeof (Elf32_External_Rela);
3592 sgot->_raw_size += 4;
3597 /* This symbol requires a procedure linkage table entry. We
3598 actually build the entry in adjust_dynamic_symbol,
3599 because this might be a case of linking PIC code which is
3600 never referenced by a dynamic object, in which case we
3601 don't need to generate a procedure linkage table entry
3604 /* If this is a local symbol, we resolve it directly without
3605 creating a procedure linkage table entry. */
3609 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3610 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3613 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3620 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3622 /* If we are creating a shared library, and this is a reloc
3623 against a global symbol, or a non PC relative reloc
3624 against a local symbol, then we need to copy the reloc
3625 into the shared library. However, if we are linking with
3626 -Bsymbolic, we do not need to copy a reloc against a
3627 global symbol which is defined in an object we are
3628 including in the link (i.e., DEF_REGULAR is set). At
3629 this point we have not seen all the input files, so it is
3630 possible that DEF_REGULAR is not set now but will be set
3631 later (it is never cleared). We account for that
3632 possibility below by storing information in the
3633 pcrel_relocs_copied field of the hash table entry. */
3635 && (sec->flags & SEC_ALLOC) != 0
3636 && (ELF32_R_TYPE (rel->r_info) != R_SH_REL32
3638 && (! info->symbolic
3639 || (h->elf_link_hash_flags
3640 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3642 /* When creating a shared object, we must copy these
3643 reloc types into the output file. We create a reloc
3644 section in dynobj and make room for this reloc. */
3649 name = (bfd_elf_string_from_elf_section
3651 elf_elfheader (abfd)->e_shstrndx,
3652 elf_section_data (sec)->rel_hdr.sh_name));
3656 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3657 && strcmp (bfd_get_section_name (abfd, sec),
3660 sreloc = bfd_get_section_by_name (dynobj, name);
3665 sreloc = bfd_make_section (dynobj, name);
3666 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3667 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3668 if ((sec->flags & SEC_ALLOC) != 0)
3669 flags |= SEC_ALLOC | SEC_LOAD;
3671 || ! bfd_set_section_flags (dynobj, sreloc, flags)
3672 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
3677 sreloc->_raw_size += sizeof (Elf32_External_Rela);
3679 /* If we are linking with -Bsymbolic, and this is a
3680 global symbol, we count the number of PC relative
3681 relocations we have entered for this symbol, so that
3682 we can discard them again if the symbol is later
3683 defined by a regular object. Note that this function
3684 is only called if we are using an elf_sh linker
3685 hash table, which means that h is really a pointer to
3686 an elf_sh_link_hash_entry. */
3687 if (h != NULL && info->symbolic
3688 && ELF32_R_TYPE (rel->r_info) == R_SH_REL32)
3690 struct elf_sh_link_hash_entry *eh;
3691 struct elf_sh_pcrel_relocs_copied *p;
3693 eh = (struct elf_sh_link_hash_entry *) h;
3695 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
3696 if (p->section == sreloc)
3701 p = ((struct elf_sh_pcrel_relocs_copied *)
3702 bfd_alloc (dynobj, sizeof *p));
3705 p->next = eh->pcrel_relocs_copied;
3706 eh->pcrel_relocs_copied = p;
3707 p->section = sreloc;
3723 sh_elf_set_mach_from_flags (abfd)
3726 flagword flags = elf_elfheader (abfd)->e_flags;
3728 switch (flags & EF_SH_MACH_MASK)
3731 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh);
3734 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2);
3737 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh_dsp);
3740 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3);
3743 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3_dsp);
3746 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3e);
3750 bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh4);
3758 /* Function to keep SH specific file flags. */
3760 sh_elf_set_private_flags (abfd, flags)
3764 BFD_ASSERT (! elf_flags_init (abfd)
3765 || elf_elfheader (abfd)->e_flags == flags);
3767 elf_elfheader (abfd)->e_flags = flags;
3768 elf_flags_init (abfd) = true;
3769 return sh_elf_set_mach_from_flags (abfd);
3772 /* Copy backend specific data from one object module to another */
3774 sh_elf_copy_private_data (ibfd, obfd)
3778 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3779 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3782 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
3785 /* This routine checks for linking big and little endian objects
3786 together, and for linking sh-dsp with sh3e / sh4 objects. */
3789 sh_elf_merge_private_data (ibfd, obfd)
3793 flagword old_flags, new_flags;
3795 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
3798 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3799 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3802 if (! elf_flags_init (obfd))
3804 /* This happens when ld starts out with a 'blank' output file. */
3805 elf_flags_init (obfd) = true;
3806 elf_elfheader (obfd)->e_flags = EF_SH1;
3808 old_flags = elf_elfheader (obfd)->e_flags;
3809 new_flags = elf_elfheader (ibfd)->e_flags;
3810 if ((EF_SH_HAS_DSP (old_flags) && EF_SH_HAS_FP (new_flags))
3811 || (EF_SH_HAS_DSP (new_flags) && EF_SH_HAS_FP (old_flags)))
3813 (*_bfd_error_handler)
3814 ("%s: uses %s instructions while previous modules use %s instructions",
3815 bfd_get_filename (ibfd),
3816 EF_SH_HAS_DSP (new_flags) ? "dsp" : "floating point",
3817 EF_SH_HAS_DSP (new_flags) ? "floating point" : "dsp");
3818 bfd_set_error (bfd_error_bad_value);
3821 elf_elfheader (obfd)->e_flags = EF_SH_MERGE_MACH (old_flags, new_flags);
3823 return sh_elf_set_mach_from_flags (obfd);
3826 /* Finish up dynamic symbol handling. We set the contents of various
3827 dynamic sections here. */
3830 sh_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3832 struct bfd_link_info *info;
3833 struct elf_link_hash_entry *h;
3834 Elf_Internal_Sym *sym;
3838 dynobj = elf_hash_table (info)->dynobj;
3840 if (h->plt.offset != (bfd_vma) -1)
3848 Elf_Internal_Rela rel;
3850 /* This symbol has an entry in the procedure linkage table. Set
3853 BFD_ASSERT (h->dynindx != -1);
3855 splt = bfd_get_section_by_name (dynobj, ".plt");
3856 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
3857 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
3858 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
3860 /* Get the index in the procedure linkage table which
3861 corresponds to this symbol. This is the index of this symbol
3862 in all the symbols for which we are making plt entries. The
3863 first entry in the procedure linkage table is reserved. */
3864 plt_index = h->plt.offset / elf_sh_sizeof_plt (info) - 1;
3866 /* Get the offset into the .got table of the entry that
3867 corresponds to this function. Each .got entry is 4 bytes.
3868 The first three are reserved. */
3869 got_offset = (plt_index + 3) * 4;
3871 /* Fill in the entry in the procedure linkage table. */
3874 if (elf_sh_plt_entry == NULL)
3876 elf_sh_plt_entry = (bfd_big_endian (output_bfd)?
3877 elf_sh_plt_entry_be : elf_sh_plt_entry_le);
3879 memcpy (splt->contents + h->plt.offset, elf_sh_plt_entry,
3880 elf_sh_sizeof_plt (info));
3881 bfd_put_32 (output_bfd,
3882 (sgot->output_section->vma
3883 + sgot->output_offset
3885 (splt->contents + h->plt.offset
3886 + elf_sh_plt_symbol_offset (info)));
3888 bfd_put_32 (output_bfd,
3889 (splt->output_section->vma + splt->output_offset),
3890 (splt->contents + h->plt.offset
3891 + elf_sh_plt_plt0_offset (info)));
3895 if (elf_sh_pic_plt_entry == NULL)
3897 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd)?
3898 elf_sh_pic_plt_entry_be :
3899 elf_sh_pic_plt_entry_le);
3901 memcpy (splt->contents + h->plt.offset, elf_sh_pic_plt_entry,
3902 elf_sh_sizeof_plt (info));
3903 bfd_put_32 (output_bfd, got_offset,
3904 (splt->contents + h->plt.offset
3905 + elf_sh_plt_symbol_offset (info)));
3908 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
3909 (splt->contents + h->plt.offset
3910 + elf_sh_plt_reloc_offset (info)));
3912 /* Fill in the entry in the global offset table. */
3913 bfd_put_32 (output_bfd,
3914 (splt->output_section->vma
3915 + splt->output_offset
3917 + elf_sh_plt_temp_offset (info)),
3918 sgot->contents + got_offset);
3920 /* Fill in the entry in the .rela.plt section. */
3921 rel.r_offset = (sgot->output_section->vma
3922 + sgot->output_offset
3924 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
3926 bfd_elf32_swap_reloca_out (output_bfd, &rel,
3927 ((Elf32_External_Rela *) srel->contents
3930 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3932 /* Mark the symbol as undefined, rather than as defined in
3933 the .plt section. Leave the value alone. */
3934 sym->st_shndx = SHN_UNDEF;
3938 if (h->got.offset != (bfd_vma) -1)
3942 Elf_Internal_Rela rel;
3944 /* This symbol has an entry in the global offset table. Set it
3947 sgot = bfd_get_section_by_name (dynobj, ".got");
3948 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3949 BFD_ASSERT (sgot != NULL && srel != NULL);
3951 rel.r_offset = (sgot->output_section->vma
3952 + sgot->output_offset
3953 + (h->got.offset &~ 1));
3955 /* If this is a -Bsymbolic link, and the symbol is defined
3956 locally, we just want to emit a RELATIVE reloc. Likewise if
3957 the symbol was forced to be local because of a version file.
3958 The entry in the global offset table will already have been
3959 initialized in the relocate_section function. */
3961 && (info->symbolic || h->dynindx == -1)
3962 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3964 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
3965 rel.r_addend = (h->root.u.def.value
3966 + h->root.u.def.section->output_section->vma
3967 + h->root.u.def.section->output_offset);
3971 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3972 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
3976 bfd_elf32_swap_reloca_out (output_bfd, &rel,
3977 ((Elf32_External_Rela *) srel->contents
3978 + srel->reloc_count));
3979 ++srel->reloc_count;
3982 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3985 Elf_Internal_Rela rel;
3987 /* This symbol needs a copy reloc. Set it up. */
3989 BFD_ASSERT (h->dynindx != -1
3990 && (h->root.type == bfd_link_hash_defined
3991 || h->root.type == bfd_link_hash_defweak));
3993 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3995 BFD_ASSERT (s != NULL);
3997 rel.r_offset = (h->root.u.def.value
3998 + h->root.u.def.section->output_section->vma
3999 + h->root.u.def.section->output_offset);
4000 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
4002 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4003 ((Elf32_External_Rela *) s->contents
4008 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4009 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4010 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4011 sym->st_shndx = SHN_ABS;
4016 /* Finish up the dynamic sections. */
4019 sh_elf_finish_dynamic_sections (output_bfd, info)
4021 struct bfd_link_info *info;
4027 dynobj = elf_hash_table (info)->dynobj;
4029 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4030 BFD_ASSERT (sgot != NULL);
4031 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4033 if (elf_hash_table (info)->dynamic_sections_created)
4036 Elf32_External_Dyn *dyncon, *dynconend;
4038 BFD_ASSERT (sdyn != NULL);
4040 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4041 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4042 for (; dyncon < dynconend; dyncon++)
4044 Elf_Internal_Dyn dyn;
4048 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4062 s = bfd_get_section_by_name (output_bfd, name);
4063 BFD_ASSERT (s != NULL);
4064 dyn.d_un.d_ptr = s->vma;
4065 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4069 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4070 BFD_ASSERT (s != NULL);
4071 if (s->_cooked_size != 0)
4072 dyn.d_un.d_val = s->_cooked_size;
4074 dyn.d_un.d_val = s->_raw_size;
4075 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4079 /* My reading of the SVR4 ABI indicates that the
4080 procedure linkage table relocs (DT_JMPREL) should be
4081 included in the overall relocs (DT_RELA). This is
4082 what Solaris does. However, UnixWare can not handle
4083 that case. Therefore, we override the DT_RELASZ entry
4084 here to make it not include the JMPREL relocs. Since
4085 the linker script arranges for .rela.plt to follow all
4086 other relocation sections, we don't have to worry
4087 about changing the DT_RELA entry. */
4088 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4091 if (s->_cooked_size != 0)
4092 dyn.d_un.d_val -= s->_cooked_size;
4094 dyn.d_un.d_val -= s->_raw_size;
4096 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4101 /* Fill in the first entry in the procedure linkage table. */
4102 splt = bfd_get_section_by_name (dynobj, ".plt");
4103 if (splt && splt->_raw_size > 0)
4107 if (elf_sh_pic_plt_entry == NULL)
4109 elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd)?
4110 elf_sh_pic_plt_entry_be :
4111 elf_sh_pic_plt_entry_le);
4113 memcpy (splt->contents, elf_sh_pic_plt_entry,
4114 elf_sh_sizeof_plt (info));
4118 if (elf_sh_plt0_entry == NULL)
4120 elf_sh_plt0_entry = (bfd_big_endian (output_bfd)?
4121 elf_sh_plt0_entry_be :
4122 elf_sh_plt0_entry_le);
4124 memcpy (splt->contents, elf_sh_plt0_entry, PLT_ENTRY_SIZE);
4125 bfd_put_32 (output_bfd,
4126 sgot->output_section->vma + sgot->output_offset + 4,
4127 splt->contents + elf_sh_plt0_gotid_offset (info));
4128 bfd_put_32 (output_bfd,
4129 sgot->output_section->vma + sgot->output_offset + 8,
4130 splt->contents + elf_sh_plt0_linker_offset (info));
4133 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4134 really seem like the right value. */
4135 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4139 /* Fill in the first three entries in the global offset table. */
4140 if (sgot->_raw_size > 0)
4143 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4145 bfd_put_32 (output_bfd,
4146 sdyn->output_section->vma + sdyn->output_offset,
4148 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4149 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4152 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4157 #define TARGET_BIG_SYM bfd_elf32_sh_vec
4158 #define TARGET_BIG_NAME "elf32-sh"
4159 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
4160 #define TARGET_LITTLE_NAME "elf32-shl"
4161 #define ELF_ARCH bfd_arch_sh
4162 #define ELF_MACHINE_CODE EM_SH
4163 #define ELF_MAXPAGESIZE 0x1
4165 #define elf_symbol_leading_char '_'
4167 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
4168 #define elf_info_to_howto sh_elf_info_to_howto
4169 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
4170 #define elf_backend_relocate_section sh_elf_relocate_section
4171 #define bfd_elf32_bfd_get_relocated_section_contents \
4172 sh_elf_get_relocated_section_contents
4173 #define elf_backend_object_p sh_elf_set_mach_from_flags
4174 #define bfd_elf32_bfd_set_private_bfd_flags \
4175 sh_elf_set_private_flags
4176 #define bfd_elf32_bfd_copy_private_bfd_data \
4177 sh_elf_copy_private_data
4178 #define bfd_elf32_bfd_merge_private_bfd_data \
4179 sh_elf_merge_private_data
4181 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
4182 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
4183 #define elf_backend_check_relocs sh_elf_check_relocs
4185 #define elf_backend_can_gc_sections 1
4186 #define elf_backend_create_dynamic_sections \
4187 sh_elf_create_dynamic_sections
4188 #define bfd_elf32_bfd_link_hash_table_create \
4189 sh_elf_link_hash_table_create
4190 #define elf_backend_adjust_dynamic_symbol \
4191 sh_elf_adjust_dynamic_symbol
4192 #define elf_backend_size_dynamic_sections \
4193 sh_elf_size_dynamic_sections
4194 #define elf_backend_finish_dynamic_symbol \
4195 sh_elf_finish_dynamic_symbol
4196 #define elf_backend_finish_dynamic_sections \
4197 sh_elf_finish_dynamic_sections
4199 #define elf_backend_want_got_plt 1
4200 #define elf_backend_plt_readonly 1
4201 #define elf_backend_want_plt_sym 0
4202 #define elf_backend_got_header_size 12
4203 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
4204 #include "elf32-target.h"