-Wimplicit-fallthrough noreturn fixes
[external/binutils.git] / bfd / elf32-sh.c
1 /* Renesas / SuperH SH specific support for 32-bit ELF
2    Copyright (C) 1996-2016 Free Software Foundation, Inc.
3    Contributed by Ian Lance Taylor, Cygnus Support.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
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 3 of the License, or
10    (at your option) any later version.
11
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.
16
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., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf-vxworks.h"
28 #include "elf/sh.h"
29 #include "dwarf2.h"
30 #include "libiberty.h"
31 #include "../opcodes/sh-opc.h"
32
33 static bfd_reloc_status_type sh_elf_reloc
34   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
35 static bfd_reloc_status_type sh_elf_ignore_reloc
36   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
37 static bfd_boolean sh_elf_relax_delete_bytes
38   (bfd *, asection *, bfd_vma, int);
39 static bfd_boolean sh_elf_align_loads
40   (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *);
41 #ifndef SH64_ELF
42 static bfd_boolean sh_elf_swap_insns
43   (bfd *, asection *, void *, bfd_byte *, bfd_vma);
44 #endif
45 static int sh_elf_optimized_tls_reloc
46   (struct bfd_link_info *, int, int);
47 static bfd_vma dtpoff_base
48   (struct bfd_link_info *);
49 static bfd_vma tpoff
50   (struct bfd_link_info *, bfd_vma);
51
52 /* The name of the dynamic interpreter.  This is put in the .interp
53    section.  */
54
55 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
56
57 /* FDPIC binaries have a default 128K stack.  */
58 #define DEFAULT_STACK_SIZE 0x20000
59
60 #define MINUS_ONE ((bfd_vma) 0 - 1)
61
62 /* Decide whether a reference to a symbol can be resolved locally or
63    not.  If the symbol is protected, we want the local address, but
64    its function descriptor must be assigned by the dynamic linker.  */
65 #define SYMBOL_FUNCDESC_LOCAL(INFO, H) \
66   (SYMBOL_REFERENCES_LOCAL (INFO, H) \
67    || ! elf_hash_table (INFO)->dynamic_sections_created)
68 \f
69 #define SH_PARTIAL32 TRUE
70 #define SH_SRC_MASK32 0xffffffff
71 #define SH_ELF_RELOC sh_elf_reloc
72 static reloc_howto_type sh_elf_howto_table[] =
73 {
74 #include "elf32-sh-relocs.h"
75 };
76
77 #define SH_PARTIAL32 FALSE
78 #define SH_SRC_MASK32 0
79 #define SH_ELF_RELOC bfd_elf_generic_reloc
80 static reloc_howto_type sh_vxworks_howto_table[] =
81 {
82 #include "elf32-sh-relocs.h"
83 };
84 \f
85 /* Return true if OUTPUT_BFD is a VxWorks object.  */
86
87 static bfd_boolean
88 vxworks_object_p (bfd *abfd ATTRIBUTE_UNUSED)
89 {
90 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
91   extern const bfd_target sh_elf32_vxworks_le_vec;
92   extern const bfd_target sh_elf32_vxworks_vec;
93
94   return (abfd->xvec == &sh_elf32_vxworks_le_vec
95           || abfd->xvec == &sh_elf32_vxworks_vec);
96 #else
97   return FALSE;
98 #endif
99 }
100
101 /* Return true if OUTPUT_BFD is an FDPIC object.  */
102
103 static bfd_boolean
104 fdpic_object_p (bfd *abfd ATTRIBUTE_UNUSED)
105 {
106 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
107   extern const bfd_target sh_elf32_fdpic_le_vec;
108   extern const bfd_target sh_elf32_fdpic_be_vec;
109
110   return (abfd->xvec == &sh_elf32_fdpic_le_vec
111           || abfd->xvec == &sh_elf32_fdpic_be_vec);
112 #else
113   return FALSE;
114 #endif
115 }
116
117 /* Return the howto table for ABFD.  */
118
119 static reloc_howto_type *
120 get_howto_table (bfd *abfd)
121 {
122   if (vxworks_object_p (abfd))
123     return sh_vxworks_howto_table;
124   return sh_elf_howto_table;
125 }
126
127 static bfd_reloc_status_type
128 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd,
129                    asection *input_section, bfd_byte *contents,
130                    bfd_vma addr, asection *symbol_section,
131                    bfd_vma start, bfd_vma end)
132 {
133   static bfd_vma last_addr;
134   static asection *last_symbol_section;
135   bfd_byte *start_ptr, *ptr, *last_ptr;
136   int diff, cum_diff;
137   bfd_signed_vma x;
138   int insn;
139
140   /* Sanity check the address.  */
141   if (addr > bfd_get_section_limit (input_bfd, input_section))
142     return bfd_reloc_outofrange;
143
144   /* We require the start and end relocations to be processed consecutively -
145      although we allow then to be processed forwards or backwards.  */
146   if (! last_addr)
147     {
148       last_addr = addr;
149       last_symbol_section = symbol_section;
150       return bfd_reloc_ok;
151     }
152   if (last_addr != addr)
153     abort ();
154   last_addr = 0;
155
156   if (! symbol_section || last_symbol_section != symbol_section || end < start)
157     return bfd_reloc_outofrange;
158
159   /* Get the symbol_section contents.  */
160   if (symbol_section != input_section)
161     {
162       if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
163         contents = elf_section_data (symbol_section)->this_hdr.contents;
164       else
165         {
166           if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
167                                            &contents))
168             {
169               if (contents != NULL)
170                 free (contents);
171               return bfd_reloc_outofrange;
172             }
173         }
174     }
175 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
176   start_ptr = contents + start;
177   for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
178     {
179       for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
180         ptr -= 2;
181       ptr += 2;
182       diff = (last_ptr - ptr) >> 1;
183       cum_diff += diff & 1;
184       cum_diff += diff;
185     }
186   /* Calculate the start / end values to load into rs / re minus four -
187      so that will cancel out the four we would otherwise have to add to
188      addr to get the value to subtract in order to get relative addressing.  */
189   if (cum_diff >= 0)
190     {
191       start -= 4;
192       end = (ptr + cum_diff * 2) - contents;
193     }
194   else
195     {
196       bfd_vma start0 = start - 4;
197
198       while (start0 && IS_PPI (contents + start0))
199         start0 -= 2;
200       start0 = start - 2 - ((start - start0) & 2);
201       start = start0 - cum_diff - 2;
202       end = start0;
203     }
204
205   if (contents != NULL
206       && elf_section_data (symbol_section)->this_hdr.contents != contents)
207     free (contents);
208
209   insn = bfd_get_16 (input_bfd, contents + addr);
210
211   x = (insn & 0x200 ? end : start) - addr;
212   if (input_section != symbol_section)
213     x += ((symbol_section->output_section->vma + symbol_section->output_offset)
214           - (input_section->output_section->vma
215              + input_section->output_offset));
216   x >>= 1;
217   if (x < -128 || x > 127)
218     return bfd_reloc_overflow;
219
220   x = (insn & ~0xff) | (x & 0xff);
221   bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
222
223   return bfd_reloc_ok;
224 }
225
226 /* This function is used for normal relocs.  This used to be like the COFF
227    function, and is almost certainly incorrect for other ELF targets.  */
228
229 static bfd_reloc_status_type
230 sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
231               void *data, asection *input_section, bfd *output_bfd,
232               char **error_message ATTRIBUTE_UNUSED)
233 {
234   unsigned long insn;
235   bfd_vma sym_value;
236   enum elf_sh_reloc_type r_type;
237   bfd_vma addr = reloc_entry->address;
238   bfd_byte *hit_data = addr + (bfd_byte *) data;
239
240   r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
241
242   if (output_bfd != NULL)
243     {
244       /* Partial linking--do nothing.  */
245       reloc_entry->address += input_section->output_offset;
246       return bfd_reloc_ok;
247     }
248
249   /* Almost all relocs have to do with relaxing.  If any work must be
250      done for them, it has been done in sh_relax_section.  */
251   if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
252     return bfd_reloc_ok;
253
254   if (symbol_in != NULL
255       && bfd_is_und_section (symbol_in->section))
256     return bfd_reloc_undefined;
257
258   /* PR 17512: file: 9891ca98.  */
259   if (addr * bfd_octets_per_byte (abfd) + bfd_get_reloc_size (reloc_entry->howto)
260       > bfd_get_section_limit_octets (abfd, input_section))
261     return bfd_reloc_outofrange;
262
263   if (bfd_is_com_section (symbol_in->section))
264     sym_value = 0;
265   else
266     sym_value = (symbol_in->value +
267                  symbol_in->section->output_section->vma +
268                  symbol_in->section->output_offset);
269
270   switch (r_type)
271     {
272     case R_SH_DIR32:
273       insn = bfd_get_32 (abfd, hit_data);
274       insn += sym_value + reloc_entry->addend;
275       bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
276       break;
277     case R_SH_IND12W:
278       insn = bfd_get_16 (abfd, hit_data);
279       sym_value += reloc_entry->addend;
280       sym_value -= (input_section->output_section->vma
281                     + input_section->output_offset
282                     + addr
283                     + 4);
284       sym_value += (insn & 0xfff) << 1;
285       if (insn & 0x800)
286         sym_value -= 0x1000;
287       insn = (insn & 0xf000) | (sym_value & 0xfff);
288       bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
289       if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
290         return bfd_reloc_overflow;
291       break;
292     default:
293       abort ();
294       break;
295     }
296
297   return bfd_reloc_ok;
298 }
299
300 /* This function is used for relocs which are only used for relaxing,
301    which the linker should otherwise ignore.  */
302
303 static bfd_reloc_status_type
304 sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
305                      asymbol *symbol ATTRIBUTE_UNUSED,
306                      void *data ATTRIBUTE_UNUSED, asection *input_section,
307                      bfd *output_bfd,
308                      char **error_message ATTRIBUTE_UNUSED)
309 {
310   if (output_bfd != NULL)
311     reloc_entry->address += input_section->output_offset;
312   return bfd_reloc_ok;
313 }
314
315 /* This structure is used to map BFD reloc codes to SH ELF relocs.  */
316
317 struct elf_reloc_map
318 {
319   bfd_reloc_code_real_type bfd_reloc_val;
320   unsigned char elf_reloc_val;
321 };
322
323 /* An array mapping BFD reloc codes to SH ELF relocs.  */
324
325 static const struct elf_reloc_map sh_reloc_map[] =
326 {
327   { BFD_RELOC_NONE, R_SH_NONE },
328   { BFD_RELOC_32, R_SH_DIR32 },
329   { BFD_RELOC_16, R_SH_DIR16 },
330   { BFD_RELOC_8, R_SH_DIR8 },
331   { BFD_RELOC_CTOR, R_SH_DIR32 },
332   { BFD_RELOC_32_PCREL, R_SH_REL32 },
333   { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
334   { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
335   { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
336   { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
337   { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
338   { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
339   { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
340   { BFD_RELOC_SH_USES, R_SH_USES },
341   { BFD_RELOC_SH_COUNT, R_SH_COUNT },
342   { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
343   { BFD_RELOC_SH_CODE, R_SH_CODE },
344   { BFD_RELOC_SH_DATA, R_SH_DATA },
345   { BFD_RELOC_SH_LABEL, R_SH_LABEL },
346   { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
347   { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
348   { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
349   { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
350   { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
351   { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
352   { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
353   { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
354   { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
355   { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
356   { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
357   { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
358   { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
359   { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
360   { BFD_RELOC_SH_COPY, R_SH_COPY },
361   { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
362   { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
363   { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
364   { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
365   { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
366   { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
367   { BFD_RELOC_SH_GOT20, R_SH_GOT20 },
368   { BFD_RELOC_SH_GOTOFF20, R_SH_GOTOFF20 },
369   { BFD_RELOC_SH_GOTFUNCDESC, R_SH_GOTFUNCDESC },
370   { BFD_RELOC_SH_GOTFUNCDESC20, R_SH_GOTFUNCDESC20 },
371   { BFD_RELOC_SH_GOTOFFFUNCDESC, R_SH_GOTOFFFUNCDESC },
372   { BFD_RELOC_SH_GOTOFFFUNCDESC20, R_SH_GOTOFFFUNCDESC20 },
373   { BFD_RELOC_SH_FUNCDESC, R_SH_FUNCDESC },
374 #ifdef INCLUDE_SHMEDIA
375   { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 },
376   { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 },
377   { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 },
378   { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 },
379   { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 },
380   { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 },
381   { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 },
382   { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 },
383   { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 },
384   { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 },
385   { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 },
386   { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 },
387   { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 },
388   { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 },
389   { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 },
390   { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 },
391   { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 },
392   { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 },
393   { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 },
394   { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 },
395   { BFD_RELOC_SH_COPY64, R_SH_COPY64 },
396   { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 },
397   { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 },
398   { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 },
399   { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 },
400   { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 },
401   { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 },
402   { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 },
403   { BFD_RELOC_SH_PT_16, R_SH_PT_16 },
404   { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE },
405   { BFD_RELOC_SH_IMMU5, R_SH_DIR5U },
406   { BFD_RELOC_SH_IMMS6, R_SH_DIR6S },
407   { BFD_RELOC_SH_IMMU6, R_SH_DIR6U },
408   { BFD_RELOC_SH_IMMS10, R_SH_DIR10S },
409   { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW },
410   { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL },
411   { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ },
412   { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 },
413   { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 },
414   { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 },
415   { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL },
416   { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 },
417   { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL },
418   { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 },
419   { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL },
420   { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 },
421   { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL },
422   { BFD_RELOC_64, R_SH_64 },
423   { BFD_RELOC_64_PCREL, R_SH_64_PCREL },
424 #endif /* not INCLUDE_SHMEDIA */
425 };
426
427 /* Given a BFD reloc code, return the howto structure for the
428    corresponding SH ELF reloc.  */
429
430 static reloc_howto_type *
431 sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
432 {
433   unsigned int i;
434
435   for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
436     {
437       if (sh_reloc_map[i].bfd_reloc_val == code)
438         return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val;
439     }
440
441   return NULL;
442 }
443
444 static reloc_howto_type *
445 sh_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
446 {
447   unsigned int i;
448
449   if (vxworks_object_p (abfd))
450     {
451       for (i = 0;
452            i < (sizeof (sh_vxworks_howto_table)
453                 / sizeof (sh_vxworks_howto_table[0]));
454            i++)
455         if (sh_vxworks_howto_table[i].name != NULL
456             && strcasecmp (sh_vxworks_howto_table[i].name, r_name) == 0)
457           return &sh_vxworks_howto_table[i];
458     }
459   else
460     {
461       for (i = 0;
462            i < (sizeof (sh_elf_howto_table)
463                 / sizeof (sh_elf_howto_table[0]));
464            i++)
465         if (sh_elf_howto_table[i].name != NULL
466             && strcasecmp (sh_elf_howto_table[i].name, r_name) == 0)
467           return &sh_elf_howto_table[i];
468     }
469
470   return NULL;
471 }
472
473 /* Given an ELF reloc, fill in the howto field of a relent.  */
474
475 static void
476 sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
477 {
478   unsigned int r;
479
480   r = ELF32_R_TYPE (dst->r_info);
481
482   if (r >= R_SH_max
483       || (r >= R_SH_FIRST_INVALID_RELOC   && r <= R_SH_LAST_INVALID_RELOC)
484       || (r >= R_SH_FIRST_INVALID_RELOC_2 && r <= R_SH_LAST_INVALID_RELOC_2)
485       || (r >= R_SH_FIRST_INVALID_RELOC_3 && r <= R_SH_LAST_INVALID_RELOC_3)
486       || (r >= R_SH_FIRST_INVALID_RELOC_4 && r <= R_SH_LAST_INVALID_RELOC_4)
487       || (r >= R_SH_FIRST_INVALID_RELOC_5 && r <= R_SH_LAST_INVALID_RELOC_5)
488       || (r >= R_SH_FIRST_INVALID_RELOC_6 && r <= R_SH_LAST_INVALID_RELOC_6))
489     {
490       _bfd_error_handler (_("%B: unrecognised SH reloc number: %d"),
491                           abfd, r);
492       bfd_set_error (bfd_error_bad_value);
493       r = R_SH_NONE;
494     }
495
496   cache_ptr->howto = get_howto_table (abfd) + r;
497 }
498 \f
499 /* This function handles relaxing for SH ELF.  See the corresponding
500    function in coff-sh.c for a description of what this does.  FIXME:
501    There is a lot of duplication here between this code and the COFF
502    specific code.  The format of relocs and symbols is wound deeply
503    into this code, but it would still be better if the duplication
504    could be eliminated somehow.  Note in particular that although both
505    functions use symbols like R_SH_CODE, those symbols have different
506    values; in coff-sh.c they come from include/coff/sh.h, whereas here
507    they come from enum elf_sh_reloc_type in include/elf/sh.h.  */
508
509 static bfd_boolean
510 sh_elf_relax_section (bfd *abfd, asection *sec,
511                       struct bfd_link_info *link_info, bfd_boolean *again)
512 {
513   Elf_Internal_Shdr *symtab_hdr;
514   Elf_Internal_Rela *internal_relocs;
515   bfd_boolean have_code;
516   Elf_Internal_Rela *irel, *irelend;
517   bfd_byte *contents = NULL;
518   Elf_Internal_Sym *isymbuf = NULL;
519
520   *again = FALSE;
521
522   if (bfd_link_relocatable (link_info)
523       || (sec->flags & SEC_RELOC) == 0
524       || sec->reloc_count == 0)
525     return TRUE;
526
527 #ifdef INCLUDE_SHMEDIA
528   if (elf_section_data (sec)->this_hdr.sh_flags
529       & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
530     {
531       return TRUE;
532     }
533 #endif
534
535   symtab_hdr = &elf_symtab_hdr (abfd);
536
537   internal_relocs = (_bfd_elf_link_read_relocs
538                      (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
539                       link_info->keep_memory));
540   if (internal_relocs == NULL)
541     goto error_return;
542
543   have_code = FALSE;
544
545   irelend = internal_relocs + sec->reloc_count;
546   for (irel = internal_relocs; irel < irelend; irel++)
547     {
548       bfd_vma laddr, paddr, symval;
549       unsigned short insn;
550       Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
551       bfd_signed_vma foff;
552
553       if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
554         have_code = TRUE;
555
556       if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
557         continue;
558
559       /* Get the section contents.  */
560       if (contents == NULL)
561         {
562           if (elf_section_data (sec)->this_hdr.contents != NULL)
563             contents = elf_section_data (sec)->this_hdr.contents;
564           else
565             {
566               if (!bfd_malloc_and_get_section (abfd, sec, &contents))
567                 goto error_return;
568             }
569         }
570
571       /* The r_addend field of the R_SH_USES reloc will point us to
572          the register load.  The 4 is because the r_addend field is
573          computed as though it were a jump offset, which are based
574          from 4 bytes after the jump instruction.  */
575       laddr = irel->r_offset + 4 + irel->r_addend;
576       if (laddr >= sec->size)
577         {
578           _bfd_error_handler (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
579                               abfd, (unsigned long) irel->r_offset);
580           continue;
581         }
582       insn = bfd_get_16 (abfd, contents + laddr);
583
584       /* If the instruction is not mov.l NN,rN, we don't know what to
585          do.  */
586       if ((insn & 0xf000) != 0xd000)
587         {
588           _bfd_error_handler
589             (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
590              abfd, (unsigned long) irel->r_offset, insn);
591           continue;
592         }
593
594       /* Get the address from which the register is being loaded.  The
595          displacement in the mov.l instruction is quadrupled.  It is a
596          displacement from four bytes after the movl instruction, but,
597          before adding in the PC address, two least significant bits
598          of the PC are cleared.  We assume that the section is aligned
599          on a four byte boundary.  */
600       paddr = insn & 0xff;
601       paddr *= 4;
602       paddr += (laddr + 4) &~ (bfd_vma) 3;
603       if (paddr >= sec->size)
604         {
605           _bfd_error_handler
606             (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
607              abfd, (unsigned long) irel->r_offset);
608           continue;
609         }
610
611       /* Get the reloc for the address from which the register is
612          being loaded.  This reloc will tell us which function is
613          actually being called.  */
614       for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
615         if (irelfn->r_offset == paddr
616             && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
617           break;
618       if (irelfn >= irelend)
619         {
620           _bfd_error_handler
621             (_("%B: 0x%lx: warning: could not find expected reloc"),
622              abfd, (unsigned long) paddr);
623           continue;
624         }
625
626       /* Read this BFD's symbols if we haven't done so already.  */
627       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
628         {
629           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
630           if (isymbuf == NULL)
631             isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
632                                             symtab_hdr->sh_info, 0,
633                                             NULL, NULL, NULL);
634           if (isymbuf == NULL)
635             goto error_return;
636         }
637
638       /* Get the value of the symbol referred to by the reloc.  */
639       if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
640         {
641           /* A local symbol.  */
642           Elf_Internal_Sym *isym;
643
644           isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
645           if (isym->st_shndx
646               != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
647             {
648               _bfd_error_handler
649                 (_("%B: 0x%lx: warning: symbol in unexpected section"),
650                  abfd, (unsigned long) paddr);
651               continue;
652             }
653
654           symval = (isym->st_value
655                     + sec->output_section->vma
656                     + sec->output_offset);
657         }
658       else
659         {
660           unsigned long indx;
661           struct elf_link_hash_entry *h;
662
663           indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
664           h = elf_sym_hashes (abfd)[indx];
665           BFD_ASSERT (h != NULL);
666           if (h->root.type != bfd_link_hash_defined
667               && h->root.type != bfd_link_hash_defweak)
668             {
669               /* This appears to be a reference to an undefined
670                  symbol.  Just ignore it--it will be caught by the
671                  regular reloc processing.  */
672               continue;
673             }
674
675           symval = (h->root.u.def.value
676                     + h->root.u.def.section->output_section->vma
677                     + h->root.u.def.section->output_offset);
678         }
679
680       if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
681         symval += bfd_get_32 (abfd, contents + paddr);
682       else
683         symval += irelfn->r_addend;
684
685       /* See if this function call can be shortened.  */
686       foff = (symval
687               - (irel->r_offset
688                  + sec->output_section->vma
689                  + sec->output_offset
690                  + 4));
691       /* A branch to an address beyond ours might be increased by an
692          .align that doesn't move when bytes behind us are deleted.
693          So, we add some slop in this calculation to allow for
694          that.  */
695       if (foff < -0x1000 || foff >= 0x1000 - 8)
696         {
697           /* After all that work, we can't shorten this function call.  */
698           continue;
699         }
700
701       /* Shorten the function call.  */
702
703       /* For simplicity of coding, we are going to modify the section
704          contents, the section relocs, and the BFD symbol table.  We
705          must tell the rest of the code not to free up this
706          information.  It would be possible to instead create a table
707          of changes which have to be made, as is done in coff-mips.c;
708          that would be more work, but would require less memory when
709          the linker is run.  */
710
711       elf_section_data (sec)->relocs = internal_relocs;
712       elf_section_data (sec)->this_hdr.contents = contents;
713       symtab_hdr->contents = (unsigned char *) isymbuf;
714
715       /* Replace the jmp/jsr with a bra/bsr.  */
716
717       /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
718          replace the jmp/jsr with a bra/bsr.  */
719       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
720       /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
721          here, but that only checks if the symbol is an external symbol,
722          not if the symbol is in a different section.  Besides, we need
723          a consistent meaning for the relocation, so we just assume here that
724          the value of the symbol is not available.  */
725
726       /* We can't fully resolve this yet, because the external
727          symbol value may be changed by future relaxing.  We let
728          the final link phase handle it.  */
729       if (bfd_get_16 (abfd, contents + irel->r_offset) & 0x0020)
730         bfd_put_16 (abfd, (bfd_vma) 0xa000, contents + irel->r_offset);
731       else
732         bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
733
734       irel->r_addend = -4;
735
736       /* When we calculated the symbol "value" we had an offset in the
737          DIR32's word in memory (we read and add it above).  However,
738          the jsr we create does NOT have this offset encoded, so we
739          have to add it to the addend to preserve it.  */
740       irel->r_addend += bfd_get_32 (abfd, contents + paddr);
741
742       /* See if there is another R_SH_USES reloc referring to the same
743          register load.  */
744       for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
745         if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
746             && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
747           break;
748       if (irelscan < irelend)
749         {
750           /* Some other function call depends upon this register load,
751              and we have not yet converted that function call.
752              Indeed, we may never be able to convert it.  There is
753              nothing else we can do at this point.  */
754           continue;
755         }
756
757       /* Look for a R_SH_COUNT reloc on the location where the
758          function address is stored.  Do this before deleting any
759          bytes, to avoid confusion about the address.  */
760       for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
761         if (irelcount->r_offset == paddr
762             && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
763           break;
764
765       /* Delete the register load.  */
766       if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
767         goto error_return;
768
769       /* That will change things, so, just in case it permits some
770          other function call to come within range, we should relax
771          again.  Note that this is not required, and it may be slow.  */
772       *again = TRUE;
773
774       /* Now check whether we got a COUNT reloc.  */
775       if (irelcount >= irelend)
776         {
777           _bfd_error_handler
778             (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
779              abfd, (unsigned long) paddr);
780           continue;
781         }
782
783       /* The number of uses is stored in the r_addend field.  We've
784          just deleted one.  */
785       if (irelcount->r_addend == 0)
786         {
787           _bfd_error_handler (_("%B: 0x%lx: warning: bad count"),
788                               abfd, (unsigned long) paddr);
789           continue;
790         }
791
792       --irelcount->r_addend;
793
794       /* If there are no more uses, we can delete the address.  Reload
795          the address from irelfn, in case it was changed by the
796          previous call to sh_elf_relax_delete_bytes.  */
797       if (irelcount->r_addend == 0)
798         {
799           if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
800             goto error_return;
801         }
802
803       /* We've done all we can with that function call.  */
804     }
805
806   /* Look for load and store instructions that we can align on four
807      byte boundaries.  */
808   if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
809       && have_code)
810     {
811       bfd_boolean swapped;
812
813       /* Get the section contents.  */
814       if (contents == NULL)
815         {
816           if (elf_section_data (sec)->this_hdr.contents != NULL)
817             contents = elf_section_data (sec)->this_hdr.contents;
818           else
819             {
820               if (!bfd_malloc_and_get_section (abfd, sec, &contents))
821                 goto error_return;
822             }
823         }
824
825       if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
826                                 &swapped))
827         goto error_return;
828
829       if (swapped)
830         {
831           elf_section_data (sec)->relocs = internal_relocs;
832           elf_section_data (sec)->this_hdr.contents = contents;
833           symtab_hdr->contents = (unsigned char *) isymbuf;
834         }
835     }
836
837   if (isymbuf != NULL
838       && symtab_hdr->contents != (unsigned char *) isymbuf)
839     {
840       if (! link_info->keep_memory)
841         free (isymbuf);
842       else
843         {
844           /* Cache the symbols for elf_link_input_bfd.  */
845           symtab_hdr->contents = (unsigned char *) isymbuf;
846         }
847     }
848
849   if (contents != NULL
850       && elf_section_data (sec)->this_hdr.contents != contents)
851     {
852       if (! link_info->keep_memory)
853         free (contents);
854       else
855         {
856           /* Cache the section contents for elf_link_input_bfd.  */
857           elf_section_data (sec)->this_hdr.contents = contents;
858         }
859     }
860
861   if (internal_relocs != NULL
862       && elf_section_data (sec)->relocs != internal_relocs)
863     free (internal_relocs);
864
865   return TRUE;
866
867  error_return:
868   if (isymbuf != NULL
869       && symtab_hdr->contents != (unsigned char *) isymbuf)
870     free (isymbuf);
871   if (contents != NULL
872       && elf_section_data (sec)->this_hdr.contents != contents)
873     free (contents);
874   if (internal_relocs != NULL
875       && elf_section_data (sec)->relocs != internal_relocs)
876     free (internal_relocs);
877
878   return FALSE;
879 }
880
881 /* Delete some bytes from a section while relaxing.  FIXME: There is a
882    lot of duplication between this function and sh_relax_delete_bytes
883    in coff-sh.c.  */
884
885 static bfd_boolean
886 sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
887                            int count)
888 {
889   Elf_Internal_Shdr *symtab_hdr;
890   unsigned int sec_shndx;
891   bfd_byte *contents;
892   Elf_Internal_Rela *irel, *irelend;
893   Elf_Internal_Rela *irelalign;
894   bfd_vma toaddr;
895   Elf_Internal_Sym *isymbuf, *isym, *isymend;
896   struct elf_link_hash_entry **sym_hashes;
897   struct elf_link_hash_entry **end_hashes;
898   unsigned int symcount;
899   asection *o;
900
901   symtab_hdr = &elf_symtab_hdr (abfd);
902   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
903
904   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
905
906   contents = elf_section_data (sec)->this_hdr.contents;
907
908   /* The deletion must stop at the next ALIGN reloc for an aligment
909      power larger than the number of bytes we are deleting.  */
910
911   irelalign = NULL;
912   toaddr = sec->size;
913
914   irel = elf_section_data (sec)->relocs;
915   irelend = irel + sec->reloc_count;
916   for (; irel < irelend; irel++)
917     {
918       if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
919           && irel->r_offset > addr
920           && count < (1 << irel->r_addend))
921         {
922           irelalign = irel;
923           toaddr = irel->r_offset;
924           break;
925         }
926     }
927
928   /* Actually delete the bytes.  */
929   memmove (contents + addr, contents + addr + count,
930            (size_t) (toaddr - addr - count));
931   if (irelalign == NULL)
932     sec->size -= count;
933   else
934     {
935       int i;
936
937 #define NOP_OPCODE (0x0009)
938
939       BFD_ASSERT ((count & 1) == 0);
940       for (i = 0; i < count; i += 2)
941         bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
942     }
943
944   /* Adjust all the relocs.  */
945   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
946     {
947       bfd_vma nraddr, stop;
948       bfd_vma start = 0;
949       int insn = 0;
950       int off, adjust, oinsn;
951       bfd_signed_vma voff = 0;
952       bfd_boolean overflow;
953
954       /* Get the new reloc address.  */
955       nraddr = irel->r_offset;
956       if ((irel->r_offset > addr
957            && irel->r_offset < toaddr)
958           || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
959               && irel->r_offset == toaddr))
960         nraddr -= count;
961
962       /* See if this reloc was for the bytes we have deleted, in which
963          case we no longer care about it.  Don't delete relocs which
964          represent addresses, though.  */
965       if (irel->r_offset >= addr
966           && irel->r_offset < addr + count
967           && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
968           && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
969           && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
970           && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
971         irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
972                                      (int) R_SH_NONE);
973
974       /* If this is a PC relative reloc, see if the range it covers
975          includes the bytes we have deleted.  */
976       switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
977         {
978         default:
979           break;
980
981         case R_SH_DIR8WPN:
982         case R_SH_IND12W:
983         case R_SH_DIR8WPZ:
984         case R_SH_DIR8WPL:
985           start = irel->r_offset;
986           insn = bfd_get_16 (abfd, contents + nraddr);
987           break;
988         }
989
990       switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
991         {
992         default:
993           start = stop = addr;
994           break;
995
996         case R_SH_DIR32:
997           /* If this reloc is against a symbol defined in this
998              section, and the symbol will not be adjusted below, we
999              must check the addend to see it will put the value in
1000              range to be adjusted, and hence must be changed.  */
1001           if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1002             {
1003               isym = isymbuf + ELF32_R_SYM (irel->r_info);
1004               if (isym->st_shndx == sec_shndx
1005                   && (isym->st_value <= addr
1006                       || isym->st_value >= toaddr))
1007                 {
1008                   bfd_vma val;
1009
1010                   if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
1011                     {
1012                       val = bfd_get_32 (abfd, contents + nraddr);
1013                       val += isym->st_value;
1014                       if (val > addr && val < toaddr)
1015                         bfd_put_32 (abfd, val - count, contents + nraddr);
1016                     }
1017                   else
1018                     {
1019                       val = isym->st_value + irel->r_addend;
1020                       if (val > addr && val < toaddr)
1021                         irel->r_addend -= count;
1022                     }
1023                 }
1024             }
1025           start = stop = addr;
1026           break;
1027
1028         case R_SH_DIR8WPN:
1029           off = insn & 0xff;
1030           if (off & 0x80)
1031             off -= 0x100;
1032           stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1033           break;
1034
1035         case R_SH_IND12W:
1036           off = insn & 0xfff;
1037           if (! off)
1038             {
1039               /* This has been made by previous relaxation.  Since the
1040                  relocation will be against an external symbol, the
1041                  final relocation will just do the right thing.  */
1042               start = stop = addr;
1043             }
1044           else
1045             {
1046               if (off & 0x800)
1047                 off -= 0x1000;
1048               stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1049
1050               /* The addend will be against the section symbol, thus
1051                  for adjusting the addend, the relevant start is the
1052                  start of the section.
1053                  N.B. If we want to abandon in-place changes here and
1054                  test directly using symbol + addend, we have to take into
1055                  account that the addend has already been adjusted by -4.  */
1056               if (stop > addr && stop < toaddr)
1057                 irel->r_addend -= count;
1058             }
1059           break;
1060
1061         case R_SH_DIR8WPZ:
1062           off = insn & 0xff;
1063           stop = start + 4 + off * 2;
1064           break;
1065
1066         case R_SH_DIR8WPL:
1067           off = insn & 0xff;
1068           stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
1069           break;
1070
1071         case R_SH_SWITCH8:
1072         case R_SH_SWITCH16:
1073         case R_SH_SWITCH32:
1074           /* These relocs types represent
1075                .word L2-L1
1076              The r_addend field holds the difference between the reloc
1077              address and L1.  That is the start of the reloc, and
1078              adding in the contents gives us the top.  We must adjust
1079              both the r_offset field and the section contents.
1080              N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1081              and the elf bfd r_offset is called r_vaddr.  */
1082
1083           stop = irel->r_offset;
1084           start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1085
1086           if (start > addr
1087               && start < toaddr
1088               && (stop <= addr || stop >= toaddr))
1089             irel->r_addend += count;
1090           else if (stop > addr
1091                    && stop < toaddr
1092                    && (start <= addr || start >= toaddr))
1093             irel->r_addend -= count;
1094
1095           if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1096             voff = bfd_get_signed_16 (abfd, contents + nraddr);
1097           else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1098             voff = bfd_get_8 (abfd, contents + nraddr);
1099           else
1100             voff = bfd_get_signed_32 (abfd, contents + nraddr);
1101           stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1102
1103           break;
1104
1105         case R_SH_USES:
1106           start = irel->r_offset;
1107           stop = (bfd_vma) ((bfd_signed_vma) start
1108                             + (long) irel->r_addend
1109                             + 4);
1110           break;
1111         }
1112
1113       if (start > addr
1114           && start < toaddr
1115           && (stop <= addr || stop >= toaddr))
1116         adjust = count;
1117       else if (stop > addr
1118                && stop < toaddr
1119                && (start <= addr || start >= toaddr))
1120         adjust = - count;
1121       else
1122         adjust = 0;
1123
1124       if (adjust != 0)
1125         {
1126           oinsn = insn;
1127           overflow = FALSE;
1128           switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1129             {
1130             default:
1131               abort ();
1132               break;
1133
1134             case R_SH_DIR8WPN:
1135             case R_SH_DIR8WPZ:
1136               insn += adjust / 2;
1137               if ((oinsn & 0xff00) != (insn & 0xff00))
1138                 overflow = TRUE;
1139               bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1140               break;
1141
1142             case R_SH_IND12W:
1143               insn += adjust / 2;
1144               if ((oinsn & 0xf000) != (insn & 0xf000))
1145                 overflow = TRUE;
1146               bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1147               break;
1148
1149             case R_SH_DIR8WPL:
1150               BFD_ASSERT (adjust == count || count >= 4);
1151               if (count >= 4)
1152                 insn += adjust / 4;
1153               else
1154                 {
1155                   if ((irel->r_offset & 3) == 0)
1156                     ++insn;
1157                 }
1158               if ((oinsn & 0xff00) != (insn & 0xff00))
1159                 overflow = TRUE;
1160               bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1161               break;
1162
1163             case R_SH_SWITCH8:
1164               voff += adjust;
1165               if (voff < 0 || voff >= 0xff)
1166                 overflow = TRUE;
1167               bfd_put_8 (abfd, voff, contents + nraddr);
1168               break;
1169
1170             case R_SH_SWITCH16:
1171               voff += adjust;
1172               if (voff < - 0x8000 || voff >= 0x8000)
1173                 overflow = TRUE;
1174               bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
1175               break;
1176
1177             case R_SH_SWITCH32:
1178               voff += adjust;
1179               bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
1180               break;
1181
1182             case R_SH_USES:
1183               irel->r_addend += adjust;
1184               break;
1185             }
1186
1187           if (overflow)
1188             {
1189               _bfd_error_handler
1190                 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1191                  abfd, (unsigned long) irel->r_offset);
1192               bfd_set_error (bfd_error_bad_value);
1193               return FALSE;
1194             }
1195         }
1196
1197       irel->r_offset = nraddr;
1198     }
1199
1200   /* Look through all the other sections.  If there contain any IMM32
1201      relocs against internal symbols which we are not going to adjust
1202      below, we may need to adjust the addends.  */
1203   for (o = abfd->sections; o != NULL; o = o->next)
1204     {
1205       Elf_Internal_Rela *internal_relocs;
1206       Elf_Internal_Rela *irelscan, *irelscanend;
1207       bfd_byte *ocontents;
1208
1209       if (o == sec
1210           || (o->flags & SEC_RELOC) == 0
1211           || o->reloc_count == 0)
1212         continue;
1213
1214       /* We always cache the relocs.  Perhaps, if info->keep_memory is
1215          FALSE, we should free them, if we are permitted to, when we
1216          leave sh_coff_relax_section.  */
1217       internal_relocs = (_bfd_elf_link_read_relocs
1218                          (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
1219       if (internal_relocs == NULL)
1220         return FALSE;
1221
1222       ocontents = NULL;
1223       irelscanend = internal_relocs + o->reloc_count;
1224       for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1225         {
1226           /* Dwarf line numbers use R_SH_SWITCH32 relocs.  */
1227           if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1228             {
1229               bfd_vma start, stop;
1230               bfd_signed_vma voff;
1231
1232               if (ocontents == NULL)
1233                 {
1234                   if (elf_section_data (o)->this_hdr.contents != NULL)
1235                     ocontents = elf_section_data (o)->this_hdr.contents;
1236                   else
1237                     {
1238                       /* We always cache the section contents.
1239                          Perhaps, if info->keep_memory is FALSE, we
1240                          should free them, if we are permitted to,
1241                          when we leave sh_coff_relax_section.  */
1242                       if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1243                         {
1244                           if (ocontents != NULL)
1245                             free (ocontents);
1246                           return FALSE;
1247                         }
1248
1249                       elf_section_data (o)->this_hdr.contents = ocontents;
1250                     }
1251                 }
1252
1253               stop = irelscan->r_offset;
1254               start
1255                 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1256
1257               /* STOP is in a different section, so it won't change.  */
1258               if (start > addr && start < toaddr)
1259                 irelscan->r_addend += count;
1260
1261               voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1262               stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1263
1264               if (start > addr
1265                   && start < toaddr
1266                   && (stop <= addr || stop >= toaddr))
1267                 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
1268                                    ocontents + irelscan->r_offset);
1269               else if (stop > addr
1270                        && stop < toaddr
1271                        && (start <= addr || start >= toaddr))
1272                 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
1273                                    ocontents + irelscan->r_offset);
1274             }
1275
1276           if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1277             continue;
1278
1279           if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1280             continue;
1281
1282
1283           isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1284           if (isym->st_shndx == sec_shndx
1285               && (isym->st_value <= addr
1286                   || isym->st_value >= toaddr))
1287             {
1288               bfd_vma val;
1289
1290               if (ocontents == NULL)
1291                 {
1292                   if (elf_section_data (o)->this_hdr.contents != NULL)
1293                     ocontents = elf_section_data (o)->this_hdr.contents;
1294                   else
1295                     {
1296                       /* We always cache the section contents.
1297                          Perhaps, if info->keep_memory is FALSE, we
1298                          should free them, if we are permitted to,
1299                          when we leave sh_coff_relax_section.  */
1300                       if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1301                         {
1302                           if (ocontents != NULL)
1303                             free (ocontents);
1304                           return FALSE;
1305                         }
1306
1307                       elf_section_data (o)->this_hdr.contents = ocontents;
1308                     }
1309                 }
1310
1311               val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1312               val += isym->st_value;
1313               if (val > addr && val < toaddr)
1314                 bfd_put_32 (abfd, val - count,
1315                             ocontents + irelscan->r_offset);
1316             }
1317         }
1318     }
1319
1320   /* Adjust the local symbols defined in this section.  */
1321   isymend = isymbuf + symtab_hdr->sh_info;
1322   for (isym = isymbuf; isym < isymend; isym++)
1323     {
1324       if (isym->st_shndx == sec_shndx
1325           && isym->st_value > addr
1326           && isym->st_value < toaddr)
1327         isym->st_value -= count;
1328     }
1329
1330   /* Now adjust the global symbols defined in this section.  */
1331   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1332               - symtab_hdr->sh_info);
1333   sym_hashes = elf_sym_hashes (abfd);
1334   end_hashes = sym_hashes + symcount;
1335   for (; sym_hashes < end_hashes; sym_hashes++)
1336     {
1337       struct elf_link_hash_entry *sym_hash = *sym_hashes;
1338       if ((sym_hash->root.type == bfd_link_hash_defined
1339            || sym_hash->root.type == bfd_link_hash_defweak)
1340           && sym_hash->root.u.def.section == sec
1341           && sym_hash->root.u.def.value > addr
1342           && sym_hash->root.u.def.value < toaddr)
1343         {
1344           sym_hash->root.u.def.value -= count;
1345         }
1346     }
1347
1348   /* See if we can move the ALIGN reloc forward.  We have adjusted
1349      r_offset for it already.  */
1350   if (irelalign != NULL)
1351     {
1352       bfd_vma alignto, alignaddr;
1353
1354       alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1355       alignaddr = BFD_ALIGN (irelalign->r_offset,
1356                              1 << irelalign->r_addend);
1357       if (alignto != alignaddr)
1358         {
1359           /* Tail recursion.  */
1360           return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1361                                             (int) (alignto - alignaddr));
1362         }
1363     }
1364
1365   return TRUE;
1366 }
1367
1368 /* Look for loads and stores which we can align to four byte
1369    boundaries.  This is like sh_align_loads in coff-sh.c.  */
1370
1371 static bfd_boolean
1372 sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1373                     Elf_Internal_Rela *internal_relocs,
1374                     bfd_byte *contents ATTRIBUTE_UNUSED,
1375                     bfd_boolean *pswapped)
1376 {
1377   Elf_Internal_Rela *irel, *irelend;
1378   bfd_vma *labels = NULL;
1379   bfd_vma *label, *label_end;
1380   bfd_size_type amt;
1381
1382   *pswapped = FALSE;
1383
1384   irelend = internal_relocs + sec->reloc_count;
1385
1386   /* Get all the addresses with labels on them.  */
1387   amt = sec->reloc_count;
1388   amt *= sizeof (bfd_vma);
1389   labels = (bfd_vma *) bfd_malloc (amt);
1390   if (labels == NULL)
1391     goto error_return;
1392   label_end = labels;
1393   for (irel = internal_relocs; irel < irelend; irel++)
1394     {
1395       if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1396         {
1397           *label_end = irel->r_offset;
1398           ++label_end;
1399         }
1400     }
1401
1402   /* Note that the assembler currently always outputs relocs in
1403      address order.  If that ever changes, this code will need to sort
1404      the label values and the relocs.  */
1405
1406   label = labels;
1407
1408   for (irel = internal_relocs; irel < irelend; irel++)
1409     {
1410       bfd_vma start, stop;
1411
1412       if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1413         continue;
1414
1415       start = irel->r_offset;
1416
1417       for (irel++; irel < irelend; irel++)
1418         if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1419           break;
1420       if (irel < irelend)
1421         stop = irel->r_offset;
1422       else
1423         stop = sec->size;
1424
1425       if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1426                                      internal_relocs, &label,
1427                                      label_end, start, stop, pswapped))
1428         goto error_return;
1429     }
1430
1431   free (labels);
1432
1433   return TRUE;
1434
1435  error_return:
1436   if (labels != NULL)
1437     free (labels);
1438   return FALSE;
1439 }
1440
1441 #ifndef SH64_ELF
1442 /* Swap two SH instructions.  This is like sh_swap_insns in coff-sh.c.  */
1443
1444 static bfd_boolean
1445 sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
1446                    bfd_byte *contents, bfd_vma addr)
1447 {
1448   Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1449   unsigned short i1, i2;
1450   Elf_Internal_Rela *irel, *irelend;
1451
1452   /* Swap the instructions themselves.  */
1453   i1 = bfd_get_16 (abfd, contents + addr);
1454   i2 = bfd_get_16 (abfd, contents + addr + 2);
1455   bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
1456   bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
1457
1458   /* Adjust all reloc addresses.  */
1459   irelend = internal_relocs + sec->reloc_count;
1460   for (irel = internal_relocs; irel < irelend; irel++)
1461     {
1462       enum elf_sh_reloc_type type;
1463       int add;
1464
1465       /* There are a few special types of relocs that we don't want to
1466          adjust.  These relocs do not apply to the instruction itself,
1467          but are only associated with the address.  */
1468       type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1469       if (type == R_SH_ALIGN
1470           || type == R_SH_CODE
1471           || type == R_SH_DATA
1472           || type == R_SH_LABEL)
1473         continue;
1474
1475       /* If an R_SH_USES reloc points to one of the addresses being
1476          swapped, we must adjust it.  It would be incorrect to do this
1477          for a jump, though, since we want to execute both
1478          instructions after the jump.  (We have avoided swapping
1479          around a label, so the jump will not wind up executing an
1480          instruction it shouldn't).  */
1481       if (type == R_SH_USES)
1482         {
1483           bfd_vma off;
1484
1485           off = irel->r_offset + 4 + irel->r_addend;
1486           if (off == addr)
1487             irel->r_offset += 2;
1488           else if (off == addr + 2)
1489             irel->r_offset -= 2;
1490         }
1491
1492       if (irel->r_offset == addr)
1493         {
1494           irel->r_offset += 2;
1495           add = -2;
1496         }
1497       else if (irel->r_offset == addr + 2)
1498         {
1499           irel->r_offset -= 2;
1500           add = 2;
1501         }
1502       else
1503         add = 0;
1504
1505       if (add != 0)
1506         {
1507           bfd_byte *loc;
1508           unsigned short insn, oinsn;
1509           bfd_boolean overflow;
1510
1511           loc = contents + irel->r_offset;
1512           overflow = FALSE;
1513           switch (type)
1514             {
1515             default:
1516               break;
1517
1518             case R_SH_DIR8WPN:
1519             case R_SH_DIR8WPZ:
1520               insn = bfd_get_16 (abfd, loc);
1521               oinsn = insn;
1522               insn += add / 2;
1523               if ((oinsn & 0xff00) != (insn & 0xff00))
1524                 overflow = TRUE;
1525               bfd_put_16 (abfd, (bfd_vma) insn, loc);
1526               break;
1527
1528             case R_SH_IND12W:
1529               insn = bfd_get_16 (abfd, loc);
1530               oinsn = insn;
1531               insn += add / 2;
1532               if ((oinsn & 0xf000) != (insn & 0xf000))
1533                 overflow = TRUE;
1534               bfd_put_16 (abfd, (bfd_vma) insn, loc);
1535               break;
1536
1537             case R_SH_DIR8WPL:
1538               /* This reloc ignores the least significant 3 bits of
1539                  the program counter before adding in the offset.
1540                  This means that if ADDR is at an even address, the
1541                  swap will not affect the offset.  If ADDR is an at an
1542                  odd address, then the instruction will be crossing a
1543                  four byte boundary, and must be adjusted.  */
1544               if ((addr & 3) != 0)
1545                 {
1546                   insn = bfd_get_16 (abfd, loc);
1547                   oinsn = insn;
1548                   insn += add / 2;
1549                   if ((oinsn & 0xff00) != (insn & 0xff00))
1550                     overflow = TRUE;
1551                   bfd_put_16 (abfd, (bfd_vma) insn, loc);
1552                 }
1553
1554               break;
1555             }
1556
1557           if (overflow)
1558             {
1559               _bfd_error_handler
1560                 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1561                  abfd, (unsigned long) irel->r_offset);
1562               bfd_set_error (bfd_error_bad_value);
1563               return FALSE;
1564             }
1565         }
1566     }
1567
1568   return TRUE;
1569 }
1570 #endif /* defined SH64_ELF */
1571 \f
1572 /* Describes one of the various PLT styles.  */
1573
1574 struct elf_sh_plt_info
1575 {
1576   /* The template for the first PLT entry, or NULL if there is no special
1577      first entry.  */
1578   const bfd_byte *plt0_entry;
1579
1580   /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL.  */
1581   bfd_vma plt0_entry_size;
1582
1583   /* Index I is the offset into PLT0_ENTRY of a pointer to
1584      _GLOBAL_OFFSET_TABLE_ + I * 4.  The value is MINUS_ONE
1585      if there is no such pointer.  */
1586   bfd_vma plt0_got_fields[3];
1587
1588   /* The template for a symbol's PLT entry.  */
1589   const bfd_byte *symbol_entry;
1590
1591   /* The size of SYMBOL_ENTRY in bytes.  */
1592   bfd_vma symbol_entry_size;
1593
1594   /* Byte offsets of fields in SYMBOL_ENTRY.  Not all fields are used
1595      on all targets.  The comments by each member indicate the value
1596      that the field must hold.  */
1597   struct {
1598     bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
1599     bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
1600     bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
1601     bfd_boolean got20; /* TRUE if got_entry points to a movi20
1602                           instruction (instead of a constant pool
1603                           entry).  */
1604   } symbol_fields;
1605
1606   /* The offset of the resolver stub from the start of SYMBOL_ENTRY.  */
1607   bfd_vma symbol_resolve_offset;
1608
1609   /* A different PLT layout which can be used for the first
1610      MAX_SHORT_PLT entries.  It must share the same plt0.  NULL in
1611      other cases.  */
1612   const struct elf_sh_plt_info *short_plt;
1613 };
1614
1615 #ifdef INCLUDE_SHMEDIA
1616
1617 /* The size in bytes of an entry in the procedure linkage table.  */
1618
1619 #define ELF_PLT_ENTRY_SIZE 64
1620
1621 /* First entry in an absolute procedure linkage table look like this.  */
1622
1623 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1624 {
1625   0xcc, 0x00, 0x01, 0x10, /* movi  .got.plt >> 16, r17 */
1626   0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1627   0x89, 0x10, 0x09, 0x90, /* ld.l  r17, 8, r25 */
1628   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1629   0x89, 0x10, 0x05, 0x10, /* ld.l  r17, 4, r17 */
1630   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1631   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1632   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1633   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1634   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1635   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1636   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1637   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1638   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1639   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1640   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1641 };
1642
1643 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1644 {
1645   0x10, 0x01, 0x00, 0xcc, /* movi  .got.plt >> 16, r17 */
1646   0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1647   0x90, 0x09, 0x10, 0x89, /* ld.l  r17, 8, r25 */
1648   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1649   0x10, 0x05, 0x10, 0x89, /* ld.l  r17, 4, r17 */
1650   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1651   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1652   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1653   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1654   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1655   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1656   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1657   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1658   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1659   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1660   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1661 };
1662
1663 /* Sebsequent entries in an absolute procedure linkage table look like
1664    this.  */
1665
1666 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1667 {
1668   0xcc, 0x00, 0x01, 0x90, /* movi  nameN-in-GOT >> 16, r25 */
1669   0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1670   0x89, 0x90, 0x01, 0x90, /* ld.l  r25, 0, r25 */
1671   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1672   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1673   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1674   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1675   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1676   0xcc, 0x00, 0x01, 0x90, /* movi  .PLT0 >> 16, r25 */
1677   0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1678   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1679   0xcc, 0x00, 0x01, 0x50, /* movi  reloc-offset >> 16, r21 */
1680   0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1681   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1682   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1683   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1684 };
1685
1686 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1687 {
1688   0x90, 0x01, 0x00, 0xcc, /* movi  nameN-in-GOT >> 16, r25 */
1689   0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1690   0x90, 0x01, 0x90, 0x89, /* ld.l  r25, 0, r25 */
1691   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1692   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1693   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1694   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1695   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1696   0x90, 0x01, 0x00, 0xcc, /* movi  .PLT0 >> 16, r25 */
1697   0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1698   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1699   0x50, 0x01, 0x00, 0xcc, /* movi  reloc-offset >> 16, r21 */
1700   0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1701   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1702   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1703   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1704 };
1705
1706 /* Entries in a PIC procedure linkage table look like this.  */
1707
1708 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1709 {
1710   0xcc, 0x00, 0x01, 0x90, /* movi  nameN@GOT >> 16, r25 */
1711   0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1712   0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1713   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1714   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1715   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1716   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1717   0x6f, 0xf0, 0xff, 0xf0, /* nop */
1718   0xce, 0x00, 0x01, 0x10, /* movi  -GOT_BIAS, r17 */
1719   0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
1720   0x89, 0x10, 0x09, 0x90, /* ld.l  r17, 8, r25 */
1721   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1722   0x89, 0x10, 0x05, 0x10, /* ld.l  r17, 4, r17 */
1723   0xcc, 0x00, 0x01, 0x50, /* movi  reloc-offset >> 16, r21 */
1724   0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1725   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1726 };
1727
1728 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1729 {
1730   0x90, 0x01, 0x00, 0xcc, /* movi  nameN@GOT >> 16, r25 */
1731   0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1732   0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1733   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1734   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1735   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1736   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1737   0xf0, 0xff, 0xf0, 0x6f, /* nop */
1738   0x10, 0x01, 0x00, 0xce, /* movi  -GOT_BIAS, r17 */
1739   0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
1740   0x90, 0x09, 0x10, 0x89, /* ld.l  r17, 8, r25 */
1741   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1742   0x10, 0x05, 0x10, 0x89, /* ld.l  r17, 4, r17 */
1743   0x50, 0x01, 0x00, 0xcc, /* movi  reloc-offset >> 16, r21 */
1744   0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1745   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1746 };
1747
1748 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1749   {
1750     {
1751       /* Big-endian non-PIC.  */
1752       elf_sh_plt0_entry_be,
1753       ELF_PLT_ENTRY_SIZE,
1754       { 0, MINUS_ONE, MINUS_ONE },
1755       elf_sh_plt_entry_be,
1756       ELF_PLT_ENTRY_SIZE,
1757       { 0, 32, 48, FALSE },
1758       33, /* includes ISA encoding */
1759       NULL
1760     },
1761     {
1762       /* Little-endian non-PIC.  */
1763       elf_sh_plt0_entry_le,
1764       ELF_PLT_ENTRY_SIZE,
1765       { 0, MINUS_ONE, MINUS_ONE },
1766       elf_sh_plt_entry_le,
1767       ELF_PLT_ENTRY_SIZE,
1768       { 0, 32, 48, FALSE },
1769       33, /* includes ISA encoding */
1770       NULL
1771     },
1772   },
1773   {
1774     {
1775       /* Big-endian PIC.  */
1776       elf_sh_plt0_entry_be,
1777       ELF_PLT_ENTRY_SIZE,
1778       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1779       elf_sh_pic_plt_entry_be,
1780       ELF_PLT_ENTRY_SIZE,
1781       { 0, MINUS_ONE, 52, FALSE },
1782       33, /* includes ISA encoding */
1783       NULL
1784     },
1785     {
1786       /* Little-endian PIC.  */
1787       elf_sh_plt0_entry_le,
1788       ELF_PLT_ENTRY_SIZE,
1789       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1790       elf_sh_pic_plt_entry_le,
1791       ELF_PLT_ENTRY_SIZE,
1792       { 0, MINUS_ONE, 52, FALSE },
1793       33, /* includes ISA encoding */
1794       NULL
1795     },
1796   }
1797 };
1798
1799 /* Return offset of the linker in PLT0 entry.  */
1800 #define elf_sh_plt0_gotplt_offset(info) 0
1801
1802 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1803    VALUE is the field's value and CODE_P is true if VALUE refers to code,
1804    not data.
1805
1806    On SH64, each 32-bit field is loaded by a movi/shori pair.  */
1807
1808 inline static void
1809 install_plt_field (bfd *output_bfd, bfd_boolean code_p,
1810                    unsigned long value, bfd_byte *addr)
1811 {
1812   value |= code_p;
1813   bfd_put_32 (output_bfd,
1814               bfd_get_32 (output_bfd, addr)
1815               | ((value >> 6) & 0x3fffc00),
1816               addr);
1817   bfd_put_32 (output_bfd,
1818               bfd_get_32 (output_bfd, addr + 4)
1819               | ((value << 10) & 0x3fffc00),
1820               addr + 4);
1821 }
1822
1823 /* Return the type of PLT associated with ABFD.  PIC_P is true if
1824    the object is position-independent.  */
1825
1826 static const struct elf_sh_plt_info *
1827 get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
1828 {
1829   return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
1830 }
1831 #else
1832 /* The size in bytes of an entry in the procedure linkage table.  */
1833
1834 #define ELF_PLT_ENTRY_SIZE 28
1835
1836 /* First entry in an absolute procedure linkage table look like this.  */
1837
1838 /* Note - this code has been "optimised" not to use r2.  r2 is used by
1839    GCC to return the address of large structures, so it should not be
1840    corrupted here.  This does mean however, that this PLT does not conform
1841    to the SH PIC ABI.  That spec says that r0 contains the type of the PLT
1842    and r2 contains the GOT id.  This version stores the GOT id in r0 and
1843    ignores the type.  Loaders can easily detect this difference however,
1844    since the type will always be 0 or 8, and the GOT ids will always be
1845    greater than or equal to 12.  */
1846 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1847 {
1848   0xd0, 0x05,   /* mov.l 2f,r0 */
1849   0x60, 0x02,   /* mov.l @r0,r0 */
1850   0x2f, 0x06,   /* mov.l r0,@-r15 */
1851   0xd0, 0x03,   /* mov.l 1f,r0 */
1852   0x60, 0x02,   /* mov.l @r0,r0 */
1853   0x40, 0x2b,   /* jmp @r0 */
1854   0x60, 0xf6,   /*  mov.l @r15+,r0 */
1855   0x00, 0x09,   /* nop */
1856   0x00, 0x09,   /* nop */
1857   0x00, 0x09,   /* nop */
1858   0, 0, 0, 0,   /* 1: replaced with address of .got.plt + 8.  */
1859   0, 0, 0, 0,   /* 2: replaced with address of .got.plt + 4.  */
1860 };
1861
1862 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1863 {
1864   0x05, 0xd0,   /* mov.l 2f,r0 */
1865   0x02, 0x60,   /* mov.l @r0,r0 */
1866   0x06, 0x2f,   /* mov.l r0,@-r15 */
1867   0x03, 0xd0,   /* mov.l 1f,r0 */
1868   0x02, 0x60,   /* mov.l @r0,r0 */
1869   0x2b, 0x40,   /* jmp @r0 */
1870   0xf6, 0x60,   /*  mov.l @r15+,r0 */
1871   0x09, 0x00,   /* nop */
1872   0x09, 0x00,   /* nop */
1873   0x09, 0x00,   /* nop */
1874   0, 0, 0, 0,   /* 1: replaced with address of .got.plt + 8.  */
1875   0, 0, 0, 0,   /* 2: replaced with address of .got.plt + 4.  */
1876 };
1877
1878 /* Sebsequent entries in an absolute procedure linkage table look like
1879    this.  */
1880
1881 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1882 {
1883   0xd0, 0x04,   /* mov.l 1f,r0 */
1884   0x60, 0x02,   /* mov.l @(r0,r12),r0 */
1885   0xd1, 0x02,   /* mov.l 0f,r1 */
1886   0x40, 0x2b,   /* jmp @r0 */
1887   0x60, 0x13,   /*  mov r1,r0 */
1888   0xd1, 0x03,   /* mov.l 2f,r1 */
1889   0x40, 0x2b,   /* jmp @r0 */
1890   0x00, 0x09,   /* nop */
1891   0, 0, 0, 0,   /* 0: replaced with address of .PLT0.  */
1892   0, 0, 0, 0,   /* 1: replaced with address of this symbol in .got.  */
1893   0, 0, 0, 0,   /* 2: replaced with offset into relocation table.  */
1894 };
1895
1896 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1897 {
1898   0x04, 0xd0,   /* mov.l 1f,r0 */
1899   0x02, 0x60,   /* mov.l @r0,r0 */
1900   0x02, 0xd1,   /* mov.l 0f,r1 */
1901   0x2b, 0x40,   /* jmp @r0 */
1902   0x13, 0x60,   /*  mov r1,r0 */
1903   0x03, 0xd1,   /* mov.l 2f,r1 */
1904   0x2b, 0x40,   /* jmp @r0 */
1905   0x09, 0x00,   /*  nop */
1906   0, 0, 0, 0,   /* 0: replaced with address of .PLT0.  */
1907   0, 0, 0, 0,   /* 1: replaced with address of this symbol in .got.  */
1908   0, 0, 0, 0,   /* 2: replaced with offset into relocation table.  */
1909 };
1910
1911 /* Entries in a PIC procedure linkage table look like this.  */
1912
1913 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1914 {
1915   0xd0, 0x04,   /* mov.l 1f,r0 */
1916   0x00, 0xce,   /* mov.l @(r0,r12),r0 */
1917   0x40, 0x2b,   /* jmp @r0 */
1918   0x00, 0x09,   /*  nop */
1919   0x50, 0xc2,   /* mov.l @(8,r12),r0 */
1920   0xd1, 0x03,   /* mov.l 2f,r1 */
1921   0x40, 0x2b,   /* jmp @r0 */
1922   0x50, 0xc1,   /*  mov.l @(4,r12),r0 */
1923   0x00, 0x09,   /* nop */
1924   0x00, 0x09,   /* nop */
1925   0, 0, 0, 0,   /* 1: replaced with address of this symbol in .got.  */
1926   0, 0, 0, 0    /* 2: replaced with offset into relocation table.  */
1927 };
1928
1929 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1930 {
1931   0x04, 0xd0,   /* mov.l 1f,r0 */
1932   0xce, 0x00,   /* mov.l @(r0,r12),r0 */
1933   0x2b, 0x40,   /* jmp @r0 */
1934   0x09, 0x00,   /*  nop */
1935   0xc2, 0x50,   /* mov.l @(8,r12),r0 */
1936   0x03, 0xd1,   /* mov.l 2f,r1 */
1937   0x2b, 0x40,   /* jmp @r0 */
1938   0xc1, 0x50,   /*  mov.l @(4,r12),r0 */
1939   0x09, 0x00,   /*  nop */
1940   0x09, 0x00,   /* nop */
1941   0, 0, 0, 0,   /* 1: replaced with address of this symbol in .got.  */
1942   0, 0, 0, 0    /* 2: replaced with offset into relocation table.  */
1943 };
1944
1945 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1946   {
1947     {
1948       /* Big-endian non-PIC.  */
1949       elf_sh_plt0_entry_be,
1950       ELF_PLT_ENTRY_SIZE,
1951       { MINUS_ONE, 24, 20 },
1952       elf_sh_plt_entry_be,
1953       ELF_PLT_ENTRY_SIZE,
1954       { 20, 16, 24, FALSE },
1955       8,
1956       NULL
1957     },
1958     {
1959       /* Little-endian non-PIC.  */
1960       elf_sh_plt0_entry_le,
1961       ELF_PLT_ENTRY_SIZE,
1962       { MINUS_ONE, 24, 20 },
1963       elf_sh_plt_entry_le,
1964       ELF_PLT_ENTRY_SIZE,
1965       { 20, 16, 24, FALSE },
1966       8,
1967       NULL
1968     },
1969   },
1970   {
1971     {
1972       /* Big-endian PIC.  */
1973       elf_sh_plt0_entry_be,
1974       ELF_PLT_ENTRY_SIZE,
1975       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1976       elf_sh_pic_plt_entry_be,
1977       ELF_PLT_ENTRY_SIZE,
1978       { 20, MINUS_ONE, 24, FALSE },
1979       8,
1980       NULL
1981     },
1982     {
1983       /* Little-endian PIC.  */
1984       elf_sh_plt0_entry_le,
1985       ELF_PLT_ENTRY_SIZE,
1986       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1987       elf_sh_pic_plt_entry_le,
1988       ELF_PLT_ENTRY_SIZE,
1989       { 20, MINUS_ONE, 24, FALSE },
1990       8,
1991       NULL
1992     },
1993   }
1994 };
1995
1996 #define VXWORKS_PLT_HEADER_SIZE 12
1997 #define VXWORKS_PLT_ENTRY_SIZE 24
1998
1999 static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
2000 {
2001   0xd1, 0x01,   /* mov.l @(8,pc),r1 */
2002   0x61, 0x12,   /* mov.l @r1,r1 */
2003   0x41, 0x2b,   /* jmp @r1 */
2004   0x00, 0x09,   /* nop */
2005   0, 0, 0, 0    /* 0: replaced with _GLOBAL_OFFSET_TABLE+8.  */
2006 };
2007
2008 static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
2009 {
2010   0x01, 0xd1,   /* mov.l @(8,pc),r1 */
2011   0x12, 0x61,   /* mov.l @r1,r1 */
2012   0x2b, 0x41,   /* jmp @r1 */
2013   0x09, 0x00,   /* nop */
2014   0, 0, 0, 0    /* 0: replaced with _GLOBAL_OFFSET_TABLE+8.  */
2015 };
2016
2017 static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
2018 {
2019   0xd0, 0x01,   /* mov.l @(8,pc),r0 */
2020   0x60, 0x02,   /* mov.l @r0,r0 */
2021   0x40, 0x2b,   /* jmp @r0 */
2022   0x00, 0x09,   /* nop */
2023   0, 0, 0, 0,   /* 0: replaced with address of this symbol in .got.  */
2024   0xd0, 0x01,   /* mov.l @(8,pc),r0 */
2025   0xa0, 0x00,   /* bra PLT (We need to fix the offset.)  */
2026   0x00, 0x09,   /* nop */
2027   0x00, 0x09,   /* nop */
2028   0, 0, 0, 0,   /* 1: replaced with offset into relocation table.  */
2029 };
2030
2031 static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
2032 {
2033   0x01, 0xd0,   /* mov.l @(8,pc),r0 */
2034   0x02, 0x60,   /* mov.l @r0,r0 */
2035   0x2b, 0x40,   /* jmp @r0 */
2036   0x09, 0x00,   /* nop */
2037   0, 0, 0, 0,   /* 0: replaced with address of this symbol in .got.  */
2038   0x01, 0xd0,   /* mov.l @(8,pc),r0 */
2039   0x00, 0xa0,   /* bra PLT (We need to fix the offset.)  */
2040   0x09, 0x00,   /* nop */
2041   0x09, 0x00,   /* nop */
2042   0, 0, 0, 0,   /* 1: replaced with offset into relocation table.  */
2043 };
2044
2045 static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
2046 {
2047   0xd0, 0x01,   /* mov.l @(8,pc),r0 */
2048   0x00, 0xce,   /* mov.l @(r0,r12),r0 */
2049   0x40, 0x2b,   /* jmp @r0 */
2050   0x00, 0x09,   /* nop */
2051   0, 0, 0, 0,   /* 0: replaced with offset of this symbol in .got.  */
2052   0xd0, 0x01,   /* mov.l @(8,pc),r0 */
2053   0x51, 0xc2,   /* mov.l @(8,r12),r1 */
2054   0x41, 0x2b,   /* jmp @r1 */
2055   0x00, 0x09,   /* nop */
2056   0, 0, 0, 0,   /* 1: replaced with offset into relocation table.  */
2057 };
2058
2059 static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
2060 {
2061   0x01, 0xd0,   /* mov.l @(8,pc),r0 */
2062   0xce, 0x00,   /* mov.l @(r0,r12),r0 */
2063   0x2b, 0x40,   /* jmp @r0 */
2064   0x09, 0x00,   /* nop */
2065   0, 0, 0, 0,   /* 0: replaced with offset of this symbol in .got.  */
2066   0x01, 0xd0,   /* mov.l @(8,pc),r0 */
2067   0xc2, 0x51,   /* mov.l @(8,r12),r1 */
2068   0x2b, 0x41,   /* jmp @r1 */
2069   0x09, 0x00,   /* nop */
2070   0, 0, 0, 0,   /* 1: replaced with offset into relocation table.  */
2071 };
2072
2073 static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
2074   {
2075     {
2076       /* Big-endian non-PIC.  */
2077       vxworks_sh_plt0_entry_be,
2078       VXWORKS_PLT_HEADER_SIZE,
2079       { MINUS_ONE, MINUS_ONE, 8 },
2080       vxworks_sh_plt_entry_be,
2081       VXWORKS_PLT_ENTRY_SIZE,
2082       { 8, 14, 20, FALSE },
2083       12,
2084       NULL
2085     },
2086     {
2087       /* Little-endian non-PIC.  */
2088       vxworks_sh_plt0_entry_le,
2089       VXWORKS_PLT_HEADER_SIZE,
2090       { MINUS_ONE, MINUS_ONE, 8 },
2091       vxworks_sh_plt_entry_le,
2092       VXWORKS_PLT_ENTRY_SIZE,
2093       { 8, 14, 20, FALSE },
2094       12,
2095       NULL
2096     },
2097   },
2098   {
2099     {
2100       /* Big-endian PIC.  */
2101       NULL,
2102       0,
2103       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2104       vxworks_sh_pic_plt_entry_be,
2105       VXWORKS_PLT_ENTRY_SIZE,
2106       { 8, MINUS_ONE, 20, FALSE },
2107       12,
2108       NULL
2109     },
2110     {
2111       /* Little-endian PIC.  */
2112       NULL,
2113       0,
2114       { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2115       vxworks_sh_pic_plt_entry_le,
2116       VXWORKS_PLT_ENTRY_SIZE,
2117       { 8, MINUS_ONE, 20, FALSE },
2118       12,
2119       NULL
2120     },
2121   }
2122 };
2123
2124 /* FDPIC PLT entries.  Two unimplemented optimizations for lazy
2125    binding are to omit the lazy binding stub when linking with -z now
2126    and to move lazy binding stubs into a separate region for better
2127    cache behavior.  */
2128
2129 #define FDPIC_PLT_ENTRY_SIZE 28
2130 #define FDPIC_PLT_LAZY_OFFSET 20
2131
2132 /* FIXME: The lazy binding stub requires a plt0 - which may need to be
2133    duplicated if it is out of range, or which can be inlined.  So
2134    right now it is always inlined, which wastes a word per stub.  It
2135    might be easier to handle the duplication if we put the lazy
2136    stubs separately.  */
2137
2138 static const bfd_byte fdpic_sh_plt_entry_be[FDPIC_PLT_ENTRY_SIZE] =
2139 {
2140   0xd0, 0x02,   /* mov.l @(12,pc),r0 */
2141   0x01, 0xce,   /* mov.l @(r0,r12),r1 */
2142   0x70, 0x04,   /* add #4, r0 */
2143   0x41, 0x2b,   /* jmp @r1 */
2144   0x0c, 0xce,   /* mov.l @(r0,r12),r12 */
2145   0x00, 0x09,   /* nop */
2146   0, 0, 0, 0,   /* 0: replaced with offset of this symbol's funcdesc */
2147   0, 0, 0, 0,   /* 1: replaced with offset into relocation table.  */
2148   0x60, 0xc2,   /* mov.l @r12,r0 */
2149   0x40, 0x2b,   /* jmp @r0 */
2150   0x53, 0xc1,   /*  mov.l @(4,r12),r3 */
2151   0x00, 0x09,   /* nop */
2152 };
2153
2154 static const bfd_byte fdpic_sh_plt_entry_le[FDPIC_PLT_ENTRY_SIZE] =
2155 {
2156   0x02, 0xd0,   /* mov.l @(12,pc),r0 */
2157   0xce, 0x01,   /* mov.l @(r0,r12),r1 */
2158   0x04, 0x70,   /* add #4, r0 */
2159   0x2b, 0x41,   /* jmp @r1 */
2160   0xce, 0x0c,   /* mov.l @(r0,r12),r12 */
2161   0x09, 0x00,   /* nop */
2162   0, 0, 0, 0,   /* 0: replaced with offset of this symbol's funcdesc */
2163   0, 0, 0, 0,   /* 1: replaced with offset into relocation table.  */
2164   0xc2, 0x60,   /* mov.l @r12,r0 */
2165   0x2b, 0x40,   /* jmp @r0 */
2166   0xc1, 0x53,   /*  mov.l @(4,r12),r3 */
2167   0x09, 0x00,   /* nop */
2168 };
2169
2170 static const struct elf_sh_plt_info fdpic_sh_plts[2] = {
2171   {
2172     /* Big-endian PIC.  */
2173     NULL,
2174     0,
2175     { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2176     fdpic_sh_plt_entry_be,
2177     FDPIC_PLT_ENTRY_SIZE,
2178     { 12, MINUS_ONE, 16, FALSE },
2179     FDPIC_PLT_LAZY_OFFSET,
2180     NULL
2181   },
2182   {
2183     /* Little-endian PIC.  */
2184     NULL,
2185     0,
2186     { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2187     fdpic_sh_plt_entry_le,
2188     FDPIC_PLT_ENTRY_SIZE,
2189     { 12, MINUS_ONE, 16, FALSE },
2190     FDPIC_PLT_LAZY_OFFSET,
2191     NULL
2192   },
2193 };
2194
2195 /* On SH2A, we can use the movi20 instruction to generate shorter PLT
2196    entries for the first 64K slots.  We use the normal FDPIC PLT entry
2197    past that point; we could also use movi20s, which might be faster,
2198    but would not be any smaller.  */
2199
2200 #define FDPIC_SH2A_PLT_ENTRY_SIZE 24
2201 #define FDPIC_SH2A_PLT_LAZY_OFFSET 16
2202
2203 static const bfd_byte fdpic_sh2a_plt_entry_be[FDPIC_SH2A_PLT_ENTRY_SIZE] =
2204 {
2205   0, 0, 0, 0,   /* movi20 #gotofffuncdesc,r0 */
2206   0x01, 0xce,   /* mov.l @(r0,r12),r1 */
2207   0x70, 0x04,   /* add #4, r0 */
2208   0x41, 0x2b,   /* jmp @r1 */
2209   0x0c, 0xce,   /* mov.l @(r0,r12),r12 */
2210   0, 0, 0, 0,   /* 1: replaced with offset into relocation table.  */
2211   0x60, 0xc2,   /* mov.l @r12,r0 */
2212   0x40, 0x2b,   /* jmp @r0 */
2213   0x53, 0xc1,   /*  mov.l @(4,r12),r3 */
2214   0x00, 0x09,   /* nop */
2215 };
2216
2217 static const bfd_byte fdpic_sh2a_plt_entry_le[FDPIC_SH2A_PLT_ENTRY_SIZE] =
2218 {
2219   0, 0, 0, 0,   /* movi20 #gotofffuncdesc,r0 */
2220   0xce, 0x01,   /* mov.l @(r0,r12),r1 */
2221   0x04, 0x70,   /* add #4, r0 */
2222   0x2b, 0x41,   /* jmp @r1 */
2223   0xce, 0x0c,   /* mov.l @(r0,r12),r12 */
2224   0, 0, 0, 0,   /* 1: replaced with offset into relocation table.  */
2225   0xc2, 0x60,   /* mov.l @r12,r0 */
2226   0x2b, 0x40,   /* jmp @r0 */
2227   0xc1, 0x53,   /*  mov.l @(4,r12),r3 */
2228   0x09, 0x00,   /* nop */
2229 };
2230
2231 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be = {
2232   /* Big-endian FDPIC, max index 64K.  */
2233   NULL,
2234   0,
2235   { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2236   fdpic_sh2a_plt_entry_be,
2237   FDPIC_SH2A_PLT_ENTRY_SIZE,
2238   { 0, MINUS_ONE, 12, TRUE },
2239   FDPIC_SH2A_PLT_LAZY_OFFSET,
2240   NULL
2241 };
2242
2243 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le = {
2244   /* Little-endian FDPIC, max index 64K.  */
2245   NULL,
2246   0,
2247   { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2248   fdpic_sh2a_plt_entry_le,
2249   FDPIC_SH2A_PLT_ENTRY_SIZE,
2250   { 0, MINUS_ONE, 12, TRUE },
2251   FDPIC_SH2A_PLT_LAZY_OFFSET,
2252   NULL
2253 };
2254
2255 static const struct elf_sh_plt_info fdpic_sh2a_plts[2] = {
2256   {
2257     /* Big-endian PIC.  */
2258     NULL,
2259     0,
2260     { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2261     fdpic_sh_plt_entry_be,
2262     FDPIC_PLT_ENTRY_SIZE,
2263     { 12, MINUS_ONE, 16, FALSE },
2264     FDPIC_PLT_LAZY_OFFSET,
2265     &fdpic_sh2a_short_plt_be
2266   },
2267   {
2268     /* Little-endian PIC.  */
2269     NULL,
2270     0,
2271     { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2272     fdpic_sh_plt_entry_le,
2273     FDPIC_PLT_ENTRY_SIZE,
2274     { 12, MINUS_ONE, 16, FALSE },
2275     FDPIC_PLT_LAZY_OFFSET,
2276     &fdpic_sh2a_short_plt_le
2277   },
2278 };
2279
2280 /* Return the type of PLT associated with ABFD.  PIC_P is true if
2281    the object is position-independent.  */
2282
2283 static const struct elf_sh_plt_info *
2284 get_plt_info (bfd *abfd, bfd_boolean pic_p)
2285 {
2286   if (fdpic_object_p (abfd))
2287     {
2288       /* If any input file requires SH2A we can use a shorter PLT
2289          sequence.  */
2290       if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd)) & arch_sh2a_base)
2291         return &fdpic_sh2a_plts[!bfd_big_endian (abfd)];
2292       else
2293         return &fdpic_sh_plts[!bfd_big_endian (abfd)];
2294     }
2295   if (vxworks_object_p (abfd))
2296     return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
2297   return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
2298 }
2299
2300 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2301    VALUE is the field's value and CODE_P is true if VALUE refers to code,
2302    not data.  */
2303
2304 inline static void
2305 install_plt_field (bfd *output_bfd, bfd_boolean code_p ATTRIBUTE_UNUSED,
2306                    unsigned long value, bfd_byte *addr)
2307 {
2308   bfd_put_32 (output_bfd, value, addr);
2309 }
2310 #endif
2311
2312 /* The number of PLT entries which can use a shorter PLT, if any.
2313    Currently always 64K, since only SH-2A FDPIC uses this; a
2314    20-bit movi20 can address that many function descriptors below
2315    _GLOBAL_OFFSET_TABLE_.  */
2316 #define MAX_SHORT_PLT 65536
2317
2318 /* Return the index of the PLT entry at byte offset OFFSET.  */
2319
2320 static bfd_vma
2321 get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
2322 {
2323   bfd_vma plt_index = 0;
2324
2325   offset -= info->plt0_entry_size;
2326   if (info->short_plt != NULL)
2327     {
2328       if (offset > MAX_SHORT_PLT * info->short_plt->symbol_entry_size)
2329         {
2330           plt_index = MAX_SHORT_PLT;
2331           offset -= MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2332         }
2333       else
2334         info = info->short_plt;
2335     }
2336   return plt_index + offset / info->symbol_entry_size;
2337 }
2338
2339 /* Do the inverse operation.  */
2340
2341 static bfd_vma
2342 get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma plt_index)
2343 {
2344   bfd_vma offset = 0;
2345
2346   if (info->short_plt != NULL)
2347     {
2348       if (plt_index > MAX_SHORT_PLT)
2349         {
2350           offset = MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2351           plt_index -= MAX_SHORT_PLT;
2352         }
2353       else
2354         info = info->short_plt;
2355     }
2356   return (offset + info->plt0_entry_size
2357           + (plt_index * info->symbol_entry_size));
2358 }
2359
2360 /* The sh linker needs to keep track of the number of relocs that it
2361    decides to copy as dynamic relocs in check_relocs for each symbol.
2362    This is so that it can later discard them if they are found to be
2363    unnecessary.  We store the information in a field extending the
2364    regular ELF linker hash table.  */
2365
2366 struct elf_sh_dyn_relocs
2367 {
2368   struct elf_sh_dyn_relocs *next;
2369
2370   /* The input section of the reloc.  */
2371   asection *sec;
2372
2373   /* Total number of relocs copied for the input section.  */
2374   bfd_size_type count;
2375
2376   /* Number of pc-relative relocs copied for the input section.  */
2377   bfd_size_type pc_count;
2378 };
2379
2380 union gotref
2381 {
2382   bfd_signed_vma refcount;
2383   bfd_vma offset;
2384 };
2385
2386 /* sh ELF linker hash entry.  */
2387
2388 struct elf_sh_link_hash_entry
2389 {
2390   struct elf_link_hash_entry root;
2391
2392 #ifdef INCLUDE_SHMEDIA
2393   union
2394   {
2395     bfd_signed_vma refcount;
2396     bfd_vma offset;
2397   } datalabel_got;
2398 #endif
2399
2400   /* Track dynamic relocs copied for this symbol.  */
2401   struct elf_sh_dyn_relocs *dyn_relocs;
2402
2403   bfd_signed_vma gotplt_refcount;
2404
2405   /* A local function descriptor, for FDPIC.  The refcount counts
2406      R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
2407      relocations; the PLT and GOT entry are accounted
2408      for separately.  After adjust_dynamic_symbol, the offset is
2409      MINUS_ONE if there is no local descriptor (dynamic linker
2410      managed and no PLT entry, or undefined weak non-dynamic).
2411      During check_relocs we do not yet know whether the local
2412      descriptor will be canonical.  */
2413   union gotref funcdesc;
2414
2415   /* How many of the above refcounted relocations were R_SH_FUNCDESC,
2416      and thus require fixups or relocations.  */
2417   bfd_signed_vma abs_funcdesc_refcount;
2418
2419   enum got_type {
2420     GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE, GOT_FUNCDESC
2421   } got_type;
2422 };
2423
2424 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2425
2426 struct sh_elf_obj_tdata
2427 {
2428   struct elf_obj_tdata root;
2429
2430   /* got_type for each local got entry.  */
2431   char *local_got_type;
2432
2433   /* Function descriptor refcount and offset for each local symbol.  */
2434   union gotref *local_funcdesc;
2435 };
2436
2437 #define sh_elf_tdata(abfd) \
2438   ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2439
2440 #define sh_elf_local_got_type(abfd) \
2441   (sh_elf_tdata (abfd)->local_got_type)
2442
2443 #define sh_elf_local_funcdesc(abfd) \
2444   (sh_elf_tdata (abfd)->local_funcdesc)
2445
2446 #define is_sh_elf(bfd) \
2447   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2448    && elf_tdata (bfd) != NULL \
2449    && elf_object_id (bfd) == SH_ELF_DATA)
2450
2451 /* Override the generic function because we need to store sh_elf_obj_tdata
2452    as the specific tdata.  */
2453
2454 static bfd_boolean
2455 sh_elf_mkobject (bfd *abfd)
2456 {
2457   return bfd_elf_allocate_object (abfd, sizeof (struct sh_elf_obj_tdata),
2458                                   SH_ELF_DATA);
2459 }
2460
2461 /* sh ELF linker hash table.  */
2462
2463 struct elf_sh_link_hash_table
2464 {
2465   struct elf_link_hash_table root;
2466
2467   /* Short-cuts to get to dynamic linker sections.  */
2468   asection *sgot;
2469   asection *sgotplt;
2470   asection *srelgot;
2471   asection *splt;
2472   asection *srelplt;
2473   asection *sdynbss;
2474   asection *srelbss;
2475   asection *sfuncdesc;
2476   asection *srelfuncdesc;
2477   asection *srofixup;
2478
2479   /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
2480   asection *srelplt2;
2481
2482   /* Small local sym cache.  */
2483   struct sym_cache sym_cache;
2484
2485   /* A counter or offset to track a TLS got entry.  */
2486   union
2487     {
2488       bfd_signed_vma refcount;
2489       bfd_vma offset;
2490     } tls_ldm_got;
2491
2492   /* The type of PLT to use.  */
2493   const struct elf_sh_plt_info *plt_info;
2494
2495   /* True if the target system is VxWorks.  */
2496   bfd_boolean vxworks_p;
2497
2498   /* True if the target system uses FDPIC.  */
2499   bfd_boolean fdpic_p;
2500 };
2501
2502 /* Traverse an sh ELF linker hash table.  */
2503
2504 #define sh_elf_link_hash_traverse(table, func, info)                    \
2505   (elf_link_hash_traverse                                               \
2506    (&(table)->root,                                                     \
2507     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2508     (info)))
2509
2510 /* Get the sh ELF linker hash table from a link_info structure.  */
2511
2512 #define sh_elf_hash_table(p) \
2513   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2514   == SH_ELF_DATA ? ((struct elf_sh_link_hash_table *) ((p)->hash)) : NULL)
2515
2516 /* Create an entry in an sh ELF linker hash table.  */
2517
2518 static struct bfd_hash_entry *
2519 sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2520                           struct bfd_hash_table *table,
2521                           const char *string)
2522 {
2523   struct elf_sh_link_hash_entry *ret =
2524     (struct elf_sh_link_hash_entry *) entry;
2525
2526   /* Allocate the structure if it has not already been allocated by a
2527      subclass.  */
2528   if (ret == (struct elf_sh_link_hash_entry *) NULL)
2529     ret = ((struct elf_sh_link_hash_entry *)
2530            bfd_hash_allocate (table,
2531                               sizeof (struct elf_sh_link_hash_entry)));
2532   if (ret == (struct elf_sh_link_hash_entry *) NULL)
2533     return (struct bfd_hash_entry *) ret;
2534
2535   /* Call the allocation method of the superclass.  */
2536   ret = ((struct elf_sh_link_hash_entry *)
2537          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2538                                      table, string));
2539   if (ret != (struct elf_sh_link_hash_entry *) NULL)
2540     {
2541       ret->dyn_relocs = NULL;
2542       ret->gotplt_refcount = 0;
2543 #ifdef INCLUDE_SHMEDIA
2544       ret->datalabel_got.refcount = ret->root.got.refcount;
2545 #endif
2546       ret->funcdesc.refcount = 0;
2547       ret->abs_funcdesc_refcount = 0;
2548       ret->got_type = GOT_UNKNOWN;
2549     }
2550
2551   return (struct bfd_hash_entry *) ret;
2552 }
2553
2554 /* Create an sh ELF linker hash table.  */
2555
2556 static struct bfd_link_hash_table *
2557 sh_elf_link_hash_table_create (bfd *abfd)
2558 {
2559   struct elf_sh_link_hash_table *ret;
2560   bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
2561
2562   ret = (struct elf_sh_link_hash_table *) bfd_zmalloc (amt);
2563   if (ret == (struct elf_sh_link_hash_table *) NULL)
2564     return NULL;
2565
2566   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2567                                       sh_elf_link_hash_newfunc,
2568                                       sizeof (struct elf_sh_link_hash_entry),
2569                                       SH_ELF_DATA))
2570     {
2571       free (ret);
2572       return NULL;
2573     }
2574
2575   ret->vxworks_p = vxworks_object_p (abfd);
2576   ret->fdpic_p = fdpic_object_p (abfd);
2577
2578   return &ret->root.root;
2579 }
2580
2581 static bfd_boolean
2582 sh_elf_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
2583                             struct bfd_link_info *info, asection *p)
2584 {
2585   struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
2586
2587   /* Non-FDPIC binaries do not need dynamic symbols for sections.  */
2588   if (!htab->fdpic_p)
2589     return TRUE;
2590
2591   /* We need dynamic symbols for every section, since segments can
2592      relocate independently.  */
2593   switch (elf_section_data (p)->this_hdr.sh_type)
2594     {
2595     case SHT_PROGBITS:
2596     case SHT_NOBITS:
2597       /* If sh_type is yet undecided, assume it could be
2598          SHT_PROGBITS/SHT_NOBITS.  */
2599     case SHT_NULL:
2600       return FALSE;
2601
2602       /* There shouldn't be section relative relocations
2603          against any other section.  */
2604     default:
2605       return TRUE;
2606     }
2607 }
2608
2609 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2610    shortcuts to them in our hash table.  */
2611
2612 static bfd_boolean
2613 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2614 {
2615   struct elf_sh_link_hash_table *htab;
2616
2617   if (! _bfd_elf_create_got_section (dynobj, info))
2618     return FALSE;
2619
2620   htab = sh_elf_hash_table (info);
2621   if (htab == NULL)
2622     return FALSE;
2623
2624   htab->sgot = bfd_get_linker_section (dynobj, ".got");
2625   htab->sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
2626   htab->srelgot = bfd_get_linker_section (dynobj, ".rela.got");
2627   if (! htab->sgot || ! htab->sgotplt || ! htab->srelgot)
2628     abort ();
2629
2630   htab->sfuncdesc = bfd_make_section_anyway_with_flags (dynobj, ".got.funcdesc",
2631                                                         (SEC_ALLOC | SEC_LOAD
2632                                                          | SEC_HAS_CONTENTS
2633                                                          | SEC_IN_MEMORY
2634                                                          | SEC_LINKER_CREATED));
2635   if (htab->sfuncdesc == NULL
2636       || ! bfd_set_section_alignment (dynobj, htab->sfuncdesc, 2))
2637     return FALSE;
2638
2639   htab->srelfuncdesc = bfd_make_section_anyway_with_flags (dynobj,
2640                                                            ".rela.got.funcdesc",
2641                                                            (SEC_ALLOC | SEC_LOAD
2642                                                             | SEC_HAS_CONTENTS
2643                                                             | SEC_IN_MEMORY
2644                                                             | SEC_LINKER_CREATED
2645                                                             | SEC_READONLY));
2646   if (htab->srelfuncdesc == NULL
2647       || ! bfd_set_section_alignment (dynobj, htab->srelfuncdesc, 2))
2648     return FALSE;
2649
2650   /* Also create .rofixup.  */
2651   htab->srofixup = bfd_make_section_anyway_with_flags (dynobj, ".rofixup",
2652                                                        (SEC_ALLOC | SEC_LOAD
2653                                                         | SEC_HAS_CONTENTS
2654                                                         | SEC_IN_MEMORY
2655                                                         | SEC_LINKER_CREATED
2656                                                         | SEC_READONLY));
2657   if (htab->srofixup == NULL
2658       || ! bfd_set_section_alignment (dynobj, htab->srofixup, 2))
2659     return FALSE;
2660
2661   return TRUE;
2662 }
2663
2664 /* Create dynamic sections when linking against a dynamic object.  */
2665
2666 static bfd_boolean
2667 sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2668 {
2669   struct elf_sh_link_hash_table *htab;
2670   flagword flags, pltflags;
2671   asection *s;
2672   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2673   int ptralign = 0;
2674
2675   switch (bed->s->arch_size)
2676     {
2677     case 32:
2678       ptralign = 2;
2679       break;
2680
2681     case 64:
2682       ptralign = 3;
2683       break;
2684
2685     default:
2686       bfd_set_error (bfd_error_bad_value);
2687       return FALSE;
2688     }
2689
2690   htab = sh_elf_hash_table (info);
2691   if (htab == NULL)
2692     return FALSE;
2693
2694   if (htab->root.dynamic_sections_created)
2695     return TRUE;
2696
2697   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2698      .rel[a].bss sections.  */
2699
2700   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2701            | SEC_LINKER_CREATED);
2702
2703   pltflags = flags;
2704   pltflags |= SEC_CODE;
2705   if (bed->plt_not_loaded)
2706     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2707   if (bed->plt_readonly)
2708     pltflags |= SEC_READONLY;
2709
2710   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
2711   htab->splt = s;
2712   if (s == NULL
2713       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2714     return FALSE;
2715
2716   if (bed->want_plt_sym)
2717     {
2718       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2719          .plt section.  */
2720       struct elf_link_hash_entry *h;
2721       struct bfd_link_hash_entry *bh = NULL;
2722
2723       if (! (_bfd_generic_link_add_one_symbol
2724              (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2725               (bfd_vma) 0, (const char *) NULL, FALSE,
2726               get_elf_backend_data (abfd)->collect, &bh)))
2727         return FALSE;
2728
2729       h = (struct elf_link_hash_entry *) bh;
2730       h->def_regular = 1;
2731       h->type = STT_OBJECT;
2732       htab->root.hplt = h;
2733
2734       if (bfd_link_pic (info)
2735           && ! bfd_elf_link_record_dynamic_symbol (info, h))
2736         return FALSE;
2737     }
2738
2739   s = bfd_make_section_anyway_with_flags (abfd,
2740                                           bed->default_use_rela_p
2741                                           ? ".rela.plt" : ".rel.plt",
2742                                           flags | SEC_READONLY);
2743   htab->srelplt = s;
2744   if (s == NULL
2745       || ! bfd_set_section_alignment (abfd, s, ptralign))
2746     return FALSE;
2747
2748   if (htab->sgot == NULL
2749       && !create_got_section (abfd, info))
2750     return FALSE;
2751
2752   if (bed->want_dynbss)
2753     {
2754       /* The .dynbss section is a place to put symbols which are defined
2755          by dynamic objects, are referenced by regular objects, and are
2756          not functions.  We must allocate space for them in the process
2757          image and use a R_*_COPY reloc to tell the dynamic linker to
2758          initialize them at run time.  The linker script puts the .dynbss
2759          section into the .bss section of the final image.  */
2760       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
2761                                               SEC_ALLOC | SEC_LINKER_CREATED);
2762       htab->sdynbss = s;
2763       if (s == NULL)
2764         return FALSE;
2765
2766       /* The .rel[a].bss section holds copy relocs.  This section is not
2767          normally needed.  We need to create it here, though, so that the
2768          linker will map it to an output section.  We can't just create it
2769          only if we need it, because we will not know whether we need it
2770          until we have seen all the input files, and the first time the
2771          main linker code calls BFD after examining all the input files
2772          (size_dynamic_sections) the input sections have already been
2773          mapped to the output sections.  If the section turns out not to
2774          be needed, we can discard it later.  We will never need this
2775          section when generating a shared object, since they do not use
2776          copy relocs.  */
2777       if (! bfd_link_pic (info))
2778         {
2779           s = bfd_make_section_anyway_with_flags (abfd,
2780                                                   (bed->default_use_rela_p
2781                                                    ? ".rela.bss" : ".rel.bss"),
2782                                                   flags | SEC_READONLY);
2783           htab->srelbss = s;
2784           if (s == NULL
2785               || ! bfd_set_section_alignment (abfd, s, ptralign))
2786             return FALSE;
2787         }
2788     }
2789
2790   if (htab->vxworks_p)
2791     {
2792       if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2793         return FALSE;
2794     }
2795
2796   return TRUE;
2797 }
2798 \f
2799 /* Adjust a symbol defined by a dynamic object and referenced by a
2800    regular object.  The current definition is in some section of the
2801    dynamic object, but we're not including those sections.  We have to
2802    change the definition to something the rest of the link can
2803    understand.  */
2804
2805 static bfd_boolean
2806 sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2807                               struct elf_link_hash_entry *h)
2808 {
2809   struct elf_sh_link_hash_table *htab;
2810   struct elf_sh_link_hash_entry *eh;
2811   struct elf_sh_dyn_relocs *p;
2812   asection *s;
2813
2814   htab = sh_elf_hash_table (info);
2815   if (htab == NULL)
2816     return FALSE;
2817
2818   /* Make sure we know what is going on here.  */
2819   BFD_ASSERT (htab->root.dynobj != NULL
2820               && (h->needs_plt
2821                   || h->u.weakdef != NULL
2822                   || (h->def_dynamic
2823                       && h->ref_regular
2824                       && !h->def_regular)));
2825
2826   /* If this is a function, put it in the procedure linkage table.  We
2827      will fill in the contents of the procedure linkage table later,
2828      when we know the address of the .got section.  */
2829   if (h->type == STT_FUNC
2830       || h->needs_plt)
2831     {
2832       if (h->plt.refcount <= 0
2833           || SYMBOL_CALLS_LOCAL (info, h)
2834           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2835               && h->root.type == bfd_link_hash_undefweak))
2836         {
2837           /* This case can occur if we saw a PLT reloc in an input
2838              file, but the symbol was never referred to by a dynamic
2839              object.  In such a case, we don't actually need to build
2840              a procedure linkage table, and we can just do a REL32
2841              reloc instead.  */
2842           h->plt.offset = (bfd_vma) -1;
2843           h->needs_plt = 0;
2844         }
2845
2846       return TRUE;
2847     }
2848   else
2849     h->plt.offset = (bfd_vma) -1;
2850
2851   /* If this is a weak symbol, and there is a real definition, the
2852      processor independent code will have arranged for us to see the
2853      real definition first, and we can just use the same value.  */
2854   if (h->u.weakdef != NULL)
2855     {
2856       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2857                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
2858       h->root.u.def.section = h->u.weakdef->root.u.def.section;
2859       h->root.u.def.value = h->u.weakdef->root.u.def.value;
2860       if (info->nocopyreloc)
2861         h->non_got_ref = h->u.weakdef->non_got_ref;
2862       return TRUE;
2863     }
2864
2865   /* This is a reference to a symbol defined by a dynamic object which
2866      is not a function.  */
2867
2868   /* If we are creating a shared library, we must presume that the
2869      only references to the symbol are via the global offset table.
2870      For such cases we need not do anything here; the relocations will
2871      be handled correctly by relocate_section.  */
2872   if (bfd_link_pic (info))
2873     return TRUE;
2874
2875   /* If there are no references to this symbol that do not use the
2876      GOT, we don't need to generate a copy reloc.  */
2877   if (!h->non_got_ref)
2878     return TRUE;
2879
2880   /* If -z nocopyreloc was given, we won't generate them either.  */
2881   if (info->nocopyreloc)
2882     {
2883       h->non_got_ref = 0;
2884       return TRUE;
2885     }
2886
2887   eh = (struct elf_sh_link_hash_entry *) h;
2888   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2889     {
2890       s = p->sec->output_section;
2891       if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2892         break;
2893     }
2894
2895   /* If we didn't find any dynamic relocs in sections which needs the
2896      copy reloc, then we'll be keeping the dynamic relocs and avoiding
2897      the copy reloc.  */
2898   if (p == NULL)
2899     {
2900       h->non_got_ref = 0;
2901       return TRUE;
2902     }
2903
2904   /* We must allocate the symbol in our .dynbss section, which will
2905      become part of the .bss section of the executable.  There will be
2906      an entry for this symbol in the .dynsym section.  The dynamic
2907      object will contain position independent code, so all references
2908      from the dynamic object to this symbol will go through the global
2909      offset table.  The dynamic linker will use the .dynsym entry to
2910      determine the address it must put in the global offset table, so
2911      both the dynamic object and the regular object will refer to the
2912      same memory location for the variable.  */
2913
2914   s = htab->sdynbss;
2915   BFD_ASSERT (s != NULL);
2916
2917   /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2918      copy the initial value out of the dynamic object and into the
2919      runtime process image.  We need to remember the offset into the
2920      .rela.bss section we are going to use.  */
2921   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2922     {
2923       asection *srel;
2924
2925       srel = htab->srelbss;
2926       BFD_ASSERT (srel != NULL);
2927       srel->size += sizeof (Elf32_External_Rela);
2928       h->needs_copy = 1;
2929     }
2930
2931   return _bfd_elf_adjust_dynamic_copy (info, h, s);
2932 }
2933
2934 /* Allocate space in .plt, .got and associated reloc sections for
2935    dynamic relocs.  */
2936
2937 static bfd_boolean
2938 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2939 {
2940   struct bfd_link_info *info;
2941   struct elf_sh_link_hash_table *htab;
2942   struct elf_sh_link_hash_entry *eh;
2943   struct elf_sh_dyn_relocs *p;
2944
2945   if (h->root.type == bfd_link_hash_indirect)
2946     return TRUE;
2947
2948   info = (struct bfd_link_info *) inf;
2949   htab = sh_elf_hash_table (info);
2950   if (htab == NULL)
2951     return FALSE;
2952
2953   eh = (struct elf_sh_link_hash_entry *) h;
2954   if ((h->got.refcount > 0
2955        || h->forced_local)
2956       && eh->gotplt_refcount > 0)
2957     {
2958       /* The symbol has been forced local, or we have some direct got refs,
2959          so treat all the gotplt refs as got refs. */
2960       h->got.refcount += eh->gotplt_refcount;
2961       if (h->plt.refcount >= eh->gotplt_refcount)
2962         h->plt.refcount -= eh->gotplt_refcount;
2963     }
2964
2965   if (htab->root.dynamic_sections_created
2966       && h->plt.refcount > 0
2967       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2968           || h->root.type != bfd_link_hash_undefweak))
2969     {
2970       /* Make sure this symbol is output as a dynamic symbol.
2971          Undefined weak syms won't yet be marked as dynamic.  */
2972       if (h->dynindx == -1
2973           && !h->forced_local)
2974         {
2975           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2976             return FALSE;
2977         }
2978
2979       if (bfd_link_pic (info)
2980           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2981         {
2982           asection *s = htab->splt;
2983           const struct elf_sh_plt_info *plt_info;
2984
2985           /* If this is the first .plt entry, make room for the special
2986              first entry.  */
2987           if (s->size == 0)
2988             s->size += htab->plt_info->plt0_entry_size;
2989
2990           h->plt.offset = s->size;
2991
2992           /* If this symbol is not defined in a regular file, and we are
2993              not generating a shared library, then set the symbol to this
2994              location in the .plt.  This is required to make function
2995              pointers compare as equal between the normal executable and
2996              the shared library.  Skip this for FDPIC, since the
2997              function's address will be the address of the canonical
2998              function descriptor.  */
2999           if (!htab->fdpic_p && !bfd_link_pic (info) && !h->def_regular)
3000             {
3001               h->root.u.def.section = s;
3002               h->root.u.def.value = h->plt.offset;
3003             }
3004
3005           /* Make room for this entry.  */
3006           plt_info = htab->plt_info;
3007           if (plt_info->short_plt != NULL
3008               && (get_plt_index (plt_info->short_plt, s->size) < MAX_SHORT_PLT))
3009             plt_info = plt_info->short_plt;
3010           s->size += plt_info->symbol_entry_size;
3011
3012           /* We also need to make an entry in the .got.plt section, which
3013              will be placed in the .got section by the linker script.  */
3014           if (!htab->fdpic_p)
3015             htab->sgotplt->size += 4;
3016           else
3017             htab->sgotplt->size += 8;
3018
3019           /* We also need to make an entry in the .rel.plt section.  */
3020           htab->srelplt->size += sizeof (Elf32_External_Rela);
3021
3022           if (htab->vxworks_p && !bfd_link_pic (info))
3023             {
3024               /* VxWorks executables have a second set of relocations
3025                  for each PLT entry.  They go in a separate relocation
3026                  section, which is processed by the kernel loader.  */
3027
3028               /* There is a relocation for the initial PLT entry:
3029                  an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_.  */
3030               if (h->plt.offset == htab->plt_info->plt0_entry_size)
3031                 htab->srelplt2->size += sizeof (Elf32_External_Rela);
3032
3033               /* There are two extra relocations for each subsequent
3034                  PLT entry: an R_SH_DIR32 relocation for the GOT entry,
3035                  and an R_SH_DIR32 relocation for the PLT entry.  */
3036               htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
3037             }
3038         }
3039       else
3040         {
3041           h->plt.offset = (bfd_vma) -1;
3042           h->needs_plt = 0;
3043         }
3044     }
3045   else
3046     {
3047       h->plt.offset = (bfd_vma) -1;
3048       h->needs_plt = 0;
3049     }
3050
3051   if (h->got.refcount > 0)
3052     {
3053       asection *s;
3054       bfd_boolean dyn;
3055       enum got_type got_type = sh_elf_hash_entry (h)->got_type;
3056
3057       /* Make sure this symbol is output as a dynamic symbol.
3058          Undefined weak syms won't yet be marked as dynamic.  */
3059       if (h->dynindx == -1
3060           && !h->forced_local)
3061         {
3062           if (! bfd_elf_link_record_dynamic_symbol (info, h))
3063             return FALSE;
3064         }
3065
3066       s = htab->sgot;
3067       h->got.offset = s->size;
3068       s->size += 4;
3069       /* R_SH_TLS_GD needs 2 consecutive GOT slots.  */
3070       if (got_type == GOT_TLS_GD)
3071         s->size += 4;
3072       dyn = htab->root.dynamic_sections_created;
3073       if (!dyn)
3074         {
3075           /* No dynamic relocations required.  */
3076           if (htab->fdpic_p && !bfd_link_pic (info)
3077               && h->root.type != bfd_link_hash_undefweak
3078               && (got_type == GOT_NORMAL || got_type == GOT_FUNCDESC))
3079             htab->srofixup->size += 4;
3080         }
3081       /* No dynamic relocations required when IE->LE conversion happens.  */
3082       else if (got_type == GOT_TLS_IE
3083                && !h->def_dynamic
3084                && !bfd_link_pic (info))
3085         ;
3086       /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
3087          R_SH_TLS_GD needs one if local symbol and two if global.  */
3088       else if ((got_type == GOT_TLS_GD && h->dynindx == -1)
3089                || got_type == GOT_TLS_IE)
3090         htab->srelgot->size += sizeof (Elf32_External_Rela);
3091       else if (got_type == GOT_TLS_GD)
3092         htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
3093       else if (got_type == GOT_FUNCDESC)
3094         {
3095           if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
3096             htab->srofixup->size += 4;
3097           else
3098             htab->srelgot->size += sizeof (Elf32_External_Rela);
3099         }
3100       else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3101                 || h->root.type != bfd_link_hash_undefweak)
3102                && (bfd_link_pic (info)
3103                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3104         htab->srelgot->size += sizeof (Elf32_External_Rela);
3105       else if (htab->fdpic_p
3106                && !bfd_link_pic (info)
3107                && got_type == GOT_NORMAL
3108                && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3109                    || h->root.type != bfd_link_hash_undefweak))
3110         htab->srofixup->size += 4;
3111     }
3112   else
3113     h->got.offset = (bfd_vma) -1;
3114
3115 #ifdef INCLUDE_SHMEDIA
3116   if (eh->datalabel_got.refcount > 0)
3117     {
3118       asection *s;
3119       bfd_boolean dyn;
3120
3121       /* Make sure this symbol is output as a dynamic symbol.
3122          Undefined weak syms won't yet be marked as dynamic.  */
3123       if (h->dynindx == -1
3124           && !h->forced_local)
3125         {
3126           if (! bfd_elf_link_record_dynamic_symbol (info, h))
3127             return FALSE;
3128         }
3129
3130       s = htab->sgot;
3131       eh->datalabel_got.offset = s->size;
3132       s->size += 4;
3133       dyn = htab->root.dynamic_sections_created;
3134       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
3135         htab->srelgot->size += sizeof (Elf32_External_Rela);
3136     }
3137   else
3138     eh->datalabel_got.offset = (bfd_vma) -1;
3139 #endif
3140
3141   /* Allocate space for any dynamic relocations to function
3142      descriptors, canonical or otherwise.  We need to relocate the
3143      reference unless it resolves to zero, which only happens for
3144      undefined weak symbols (either non-default visibility, or when
3145      static linking).  Any GOT slot is accounted for elsewhere.  */
3146   if (eh->abs_funcdesc_refcount > 0
3147       && (h->root.type != bfd_link_hash_undefweak
3148           || (htab->root.dynamic_sections_created
3149               && ! SYMBOL_CALLS_LOCAL (info, h))))
3150     {
3151       if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
3152         htab->srofixup->size += eh->abs_funcdesc_refcount * 4;
3153       else
3154         htab->srelgot->size
3155           += eh->abs_funcdesc_refcount * sizeof (Elf32_External_Rela);
3156     }
3157
3158   /* We must allocate a function descriptor if there are references to
3159      a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
3160      the dynamic linker isn't going to allocate it.  None of this
3161      applies if we already created one in .got.plt, but if the
3162      canonical function descriptor can be in this object, there
3163      won't be a PLT entry at all.  */
3164   if ((eh->funcdesc.refcount > 0
3165        || (h->got.offset != MINUS_ONE && eh->got_type == GOT_FUNCDESC))
3166       && h->root.type != bfd_link_hash_undefweak
3167       && SYMBOL_FUNCDESC_LOCAL (info, h))
3168     {
3169       /* Make room for this function descriptor.  */
3170       eh->funcdesc.offset = htab->sfuncdesc->size;
3171       htab->sfuncdesc->size += 8;
3172
3173       /* We will need a relocation or two fixups to initialize the
3174          function descriptor, so allocate those too.  */
3175       if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
3176         htab->srofixup->size += 8;
3177       else
3178         htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3179     }
3180
3181   if (eh->dyn_relocs == NULL)
3182     return TRUE;
3183
3184   /* In the shared -Bsymbolic case, discard space allocated for
3185      dynamic pc-relative relocs against symbols which turn out to be
3186      defined in regular objects.  For the normal shared case, discard
3187      space for pc-relative relocs that have become local due to symbol
3188      visibility changes.  */
3189
3190   if (bfd_link_pic (info))
3191     {
3192       if (SYMBOL_CALLS_LOCAL (info, h))
3193         {
3194           struct elf_sh_dyn_relocs **pp;
3195
3196           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3197             {
3198               p->count -= p->pc_count;
3199               p->pc_count = 0;
3200               if (p->count == 0)
3201                 *pp = p->next;
3202               else
3203                 pp = &p->next;
3204             }
3205         }
3206
3207       if (htab->vxworks_p)
3208         {
3209           struct elf_sh_dyn_relocs **pp;
3210
3211           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3212             {
3213               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
3214                 *pp = p->next;
3215               else
3216                 pp = &p->next;
3217             }
3218         }
3219
3220       /* Also discard relocs on undefined weak syms with non-default
3221          visibility.  */
3222       if (eh->dyn_relocs != NULL
3223           && h->root.type == bfd_link_hash_undefweak)
3224         {
3225           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3226             eh->dyn_relocs = NULL;
3227
3228           /* Make sure undefined weak symbols are output as a dynamic
3229              symbol in PIEs.  */
3230           else if (h->dynindx == -1
3231                    && !h->forced_local)
3232             {
3233               if (! bfd_elf_link_record_dynamic_symbol (info, h))
3234                 return FALSE;
3235             }
3236         }
3237     }
3238   else
3239     {
3240       /* For the non-shared case, discard space for relocs against
3241          symbols which turn out to need copy relocs or are not
3242          dynamic.  */
3243
3244       if (!h->non_got_ref
3245           && ((h->def_dynamic
3246                && !h->def_regular)
3247               || (htab->root.dynamic_sections_created
3248                   && (h->root.type == bfd_link_hash_undefweak
3249                       || h->root.type == bfd_link_hash_undefined))))
3250         {
3251           /* Make sure this symbol is output as a dynamic symbol.
3252              Undefined weak syms won't yet be marked as dynamic.  */
3253           if (h->dynindx == -1
3254               && !h->forced_local)
3255             {
3256               if (! bfd_elf_link_record_dynamic_symbol (info, h))
3257                 return FALSE;
3258             }
3259
3260           /* If that succeeded, we know we'll be keeping all the
3261              relocs.  */
3262           if (h->dynindx != -1)
3263             goto keep;
3264         }
3265
3266       eh->dyn_relocs = NULL;
3267
3268     keep: ;
3269     }
3270
3271   /* Finally, allocate space.  */
3272   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3273     {
3274       asection *sreloc = elf_section_data (p->sec)->sreloc;
3275       sreloc->size += p->count * sizeof (Elf32_External_Rela);
3276
3277       /* If we need relocations, we do not need fixups.  */
3278       if (htab->fdpic_p && !bfd_link_pic (info))
3279         htab->srofixup->size -= 4 * (p->count - p->pc_count);
3280     }
3281
3282   return TRUE;
3283 }
3284
3285 /* Find any dynamic relocs that apply to read-only sections.  */
3286
3287 static bfd_boolean
3288 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3289 {
3290   struct elf_sh_link_hash_entry *eh;
3291   struct elf_sh_dyn_relocs *p;
3292
3293   eh = (struct elf_sh_link_hash_entry *) h;
3294   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3295     {
3296       asection *s = p->sec->output_section;
3297
3298       if (s != NULL && (s->flags & SEC_READONLY) != 0)
3299         {
3300           struct bfd_link_info *info = (struct bfd_link_info *) inf;
3301
3302           info->flags |= DF_TEXTREL;
3303
3304           /* Not an error, just cut short the traversal.  */
3305           return FALSE;
3306         }
3307     }
3308   return TRUE;
3309 }
3310
3311 /* This function is called after all the input files have been read,
3312    and the input sections have been assigned to output sections.
3313    It's a convenient place to determine the PLT style.  */
3314
3315 static bfd_boolean
3316 sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
3317 {
3318   sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd,
3319                                                      bfd_link_pic (info));
3320
3321   if (sh_elf_hash_table (info)->fdpic_p && !bfd_link_relocatable (info)
3322       && !bfd_elf_stack_segment_size (output_bfd, info,
3323                                       "__stacksize", DEFAULT_STACK_SIZE))
3324     return FALSE;
3325   return TRUE;
3326 }
3327
3328 /* Set the sizes of the dynamic sections.  */
3329
3330 static bfd_boolean
3331 sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3332                               struct bfd_link_info *info)
3333 {
3334   struct elf_sh_link_hash_table *htab;
3335   bfd *dynobj;
3336   asection *s;
3337   bfd_boolean relocs;
3338   bfd *ibfd;
3339
3340   htab = sh_elf_hash_table (info);
3341   if (htab == NULL)
3342     return FALSE;
3343
3344   dynobj = htab->root.dynobj;
3345   BFD_ASSERT (dynobj != NULL);
3346
3347   if (htab->root.dynamic_sections_created)
3348     {
3349       /* Set the contents of the .interp section to the interpreter.  */
3350       if (bfd_link_executable (info) && !info->nointerp)
3351         {
3352           s = bfd_get_linker_section (dynobj, ".interp");
3353           BFD_ASSERT (s != NULL);
3354           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3355           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3356         }
3357     }
3358
3359   /* Set up .got offsets for local syms, and space for local dynamic
3360      relocs.  */
3361   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3362     {
3363       bfd_signed_vma *local_got;
3364       bfd_signed_vma *end_local_got;
3365       union gotref *local_funcdesc, *end_local_funcdesc;
3366       char *local_got_type;
3367       bfd_size_type locsymcount;
3368       Elf_Internal_Shdr *symtab_hdr;
3369       asection *srel;
3370
3371       if (! is_sh_elf (ibfd))
3372         continue;
3373
3374       for (s = ibfd->sections; s != NULL; s = s->next)
3375         {
3376           struct elf_sh_dyn_relocs *p;
3377
3378           for (p = ((struct elf_sh_dyn_relocs *)
3379                     elf_section_data (s)->local_dynrel);
3380                p != NULL;
3381                p = p->next)
3382             {
3383               if (! bfd_is_abs_section (p->sec)
3384                   && bfd_is_abs_section (p->sec->output_section))
3385                 {
3386                   /* Input section has been discarded, either because
3387                      it is a copy of a linkonce section or due to
3388                      linker script /DISCARD/, so we'll be discarding
3389                      the relocs too.  */
3390                 }
3391               else if (htab->vxworks_p
3392                        && strcmp (p->sec->output_section->name,
3393                                   ".tls_vars") == 0)
3394                 {
3395                   /* Relocations in vxworks .tls_vars sections are
3396                      handled specially by the loader.  */
3397                 }
3398               else if (p->count != 0)
3399                 {
3400                   srel = elf_section_data (p->sec)->sreloc;
3401                   srel->size += p->count * sizeof (Elf32_External_Rela);
3402                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3403                     info->flags |= DF_TEXTREL;
3404
3405                   /* If we need relocations, we do not need fixups.  */
3406                   if (htab->fdpic_p && !bfd_link_pic (info))
3407                     htab->srofixup->size -= 4 * (p->count - p->pc_count);
3408                 }
3409             }
3410         }
3411
3412       symtab_hdr = &elf_symtab_hdr (ibfd);
3413       locsymcount = symtab_hdr->sh_info;
3414 #ifdef INCLUDE_SHMEDIA
3415       /* Count datalabel local GOT.  */
3416       locsymcount *= 2;
3417 #endif
3418       s = htab->sgot;
3419       srel = htab->srelgot;
3420
3421       local_got = elf_local_got_refcounts (ibfd);
3422       if (local_got)
3423         {
3424           end_local_got = local_got + locsymcount;
3425           local_got_type = sh_elf_local_got_type (ibfd);
3426           local_funcdesc = sh_elf_local_funcdesc (ibfd);
3427           for (; local_got < end_local_got; ++local_got)
3428             {
3429               if (*local_got > 0)
3430                 {
3431                   *local_got = s->size;
3432                   s->size += 4;
3433                   if (*local_got_type == GOT_TLS_GD)
3434                     s->size += 4;
3435                   if (bfd_link_pic (info))
3436                     srel->size += sizeof (Elf32_External_Rela);
3437                   else
3438                     htab->srofixup->size += 4;
3439
3440                   if (*local_got_type == GOT_FUNCDESC)
3441                     {
3442                       if (local_funcdesc == NULL)
3443                         {
3444                           bfd_size_type size;
3445
3446                           size = locsymcount * sizeof (union gotref);
3447                           local_funcdesc = (union gotref *) bfd_zalloc (ibfd,
3448                                                                         size);
3449                           if (local_funcdesc == NULL)
3450                             return FALSE;
3451                           sh_elf_local_funcdesc (ibfd) = local_funcdesc;
3452                           local_funcdesc += (local_got
3453                                              - elf_local_got_refcounts (ibfd));
3454                         }
3455                       local_funcdesc->refcount++;
3456                       ++local_funcdesc;
3457                     }
3458                 }
3459               else
3460                 *local_got = (bfd_vma) -1;
3461               ++local_got_type;
3462             }
3463         }
3464
3465       local_funcdesc = sh_elf_local_funcdesc (ibfd);
3466       if (local_funcdesc)
3467         {
3468           end_local_funcdesc = local_funcdesc + locsymcount;
3469
3470           for (; local_funcdesc < end_local_funcdesc; ++local_funcdesc)
3471             {
3472               if (local_funcdesc->refcount > 0)
3473                 {
3474                   local_funcdesc->offset = htab->sfuncdesc->size;
3475                   htab->sfuncdesc->size += 8;
3476                   if (!bfd_link_pic (info))
3477                     htab->srofixup->size += 8;
3478                   else
3479                     htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3480                 }
3481               else
3482                 local_funcdesc->offset = MINUS_ONE;
3483             }
3484         }
3485
3486     }
3487
3488   if (htab->tls_ldm_got.refcount > 0)
3489     {
3490       /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3491          relocs.  */
3492       htab->tls_ldm_got.offset = htab->sgot->size;
3493       htab->sgot->size += 8;
3494       htab->srelgot->size += sizeof (Elf32_External_Rela);
3495     }
3496   else
3497     htab->tls_ldm_got.offset = -1;
3498
3499   /* Only the reserved entries should be present.  For FDPIC, they go at
3500      the end of .got.plt.  */
3501   if (htab->fdpic_p)
3502     {
3503       BFD_ASSERT (htab->sgotplt && htab->sgotplt->size == 12);
3504       htab->sgotplt->size = 0;
3505     }
3506
3507   /* Allocate global sym .plt and .got entries, and space for global
3508      sym dynamic relocs.  */
3509   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
3510
3511   /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3512      end of the FDPIC .got.plt.  */
3513   if (htab->fdpic_p)
3514     {
3515       htab->root.hgot->root.u.def.value = htab->sgotplt->size;
3516       htab->sgotplt->size += 12;
3517     }
3518
3519   /* At the very end of the .rofixup section is a pointer to the GOT.  */
3520   if (htab->fdpic_p && htab->srofixup != NULL)
3521     htab->srofixup->size += 4;
3522
3523   /* We now have determined the sizes of the various dynamic sections.
3524      Allocate memory for them.  */
3525   relocs = FALSE;
3526   for (s = dynobj->sections; s != NULL; s = s->next)
3527     {
3528       if ((s->flags & SEC_LINKER_CREATED) == 0)
3529         continue;
3530
3531       if (s == htab->splt
3532           || s == htab->sgot
3533           || s == htab->sgotplt
3534           || s == htab->sfuncdesc
3535           || s == htab->srofixup
3536           || s == htab->sdynbss)
3537         {
3538           /* Strip this section if we don't need it; see the
3539              comment below.  */
3540         }
3541       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
3542         {
3543           if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
3544             relocs = TRUE;
3545
3546           /* We use the reloc_count field as a counter if we need
3547              to copy relocs into the output file.  */
3548           s->reloc_count = 0;
3549         }
3550       else
3551         {
3552           /* It's not one of our sections, so don't allocate space.  */
3553           continue;
3554         }
3555
3556       if (s->size == 0)
3557         {
3558           /* If we don't need this section, strip it from the
3559              output file.  This is mostly to handle .rela.bss and
3560              .rela.plt.  We must create both sections in
3561              create_dynamic_sections, because they must be created
3562              before the linker maps input sections to output
3563              sections.  The linker does that before
3564              adjust_dynamic_symbol is called, and it is that
3565              function which decides whether anything needs to go
3566              into these sections.  */
3567
3568           s->flags |= SEC_EXCLUDE;
3569           continue;
3570         }
3571
3572       if ((s->flags & SEC_HAS_CONTENTS) == 0)
3573         continue;
3574
3575       /* Allocate memory for the section contents.  We use bfd_zalloc
3576          here in case unused entries are not reclaimed before the
3577          section's contents are written out.  This should not happen,
3578          but this way if it does, we get a R_SH_NONE reloc instead
3579          of garbage.  */
3580       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3581       if (s->contents == NULL)
3582         return FALSE;
3583     }
3584
3585   if (htab->root.dynamic_sections_created)
3586     {
3587       /* Add some entries to the .dynamic section.  We fill in the
3588          values later, in sh_elf_finish_dynamic_sections, but we
3589          must add the entries now so that we get the correct size for
3590          the .dynamic section.  The DT_DEBUG entry is filled in by the
3591          dynamic linker and used by the debugger.  */
3592 #define add_dynamic_entry(TAG, VAL) \
3593   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3594
3595       if (bfd_link_executable (info))
3596         {
3597           if (! add_dynamic_entry (DT_DEBUG, 0))
3598             return FALSE;
3599         }
3600
3601       if (htab->splt->size != 0)
3602         {
3603           if (! add_dynamic_entry (DT_PLTGOT, 0)
3604               || ! add_dynamic_entry (DT_PLTRELSZ, 0)
3605               || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
3606               || ! add_dynamic_entry (DT_JMPREL, 0))
3607             return FALSE;
3608         }
3609       else if ((elf_elfheader (output_bfd)->e_flags & EF_SH_FDPIC))
3610         {
3611           if (! add_dynamic_entry (DT_PLTGOT, 0))
3612             return FALSE;
3613         }
3614
3615       if (relocs)
3616         {
3617           if (! add_dynamic_entry (DT_RELA, 0)
3618               || ! add_dynamic_entry (DT_RELASZ, 0)
3619               || ! add_dynamic_entry (DT_RELAENT,
3620                                       sizeof (Elf32_External_Rela)))
3621             return FALSE;
3622
3623           /* If any dynamic relocs apply to a read-only section,
3624              then we need a DT_TEXTREL entry.  */
3625           if ((info->flags & DF_TEXTREL) == 0)
3626             elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info);
3627
3628           if ((info->flags & DF_TEXTREL) != 0)
3629             {
3630               if (! add_dynamic_entry (DT_TEXTREL, 0))
3631                 return FALSE;
3632             }
3633         }
3634       if (htab->vxworks_p
3635           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3636         return FALSE;
3637     }
3638 #undef add_dynamic_entry
3639
3640   return TRUE;
3641 }
3642 \f
3643 /* Add a dynamic relocation to the SRELOC section.  */
3644
3645 inline static bfd_vma
3646 sh_elf_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
3647                       int reloc_type, long dynindx, bfd_vma addend)
3648 {
3649   Elf_Internal_Rela outrel;
3650   bfd_vma reloc_offset;
3651
3652   outrel.r_offset = offset;
3653   outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
3654   outrel.r_addend = addend;
3655
3656   reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rela);
3657   BFD_ASSERT (reloc_offset < sreloc->size);
3658   bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3659                              sreloc->contents + reloc_offset);
3660   sreloc->reloc_count++;
3661
3662   return reloc_offset;
3663 }
3664
3665 /* Add an FDPIC read-only fixup.  */
3666
3667 inline static void
3668 sh_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
3669 {
3670   bfd_vma fixup_offset;
3671
3672   fixup_offset = srofixup->reloc_count++ * 4;
3673   BFD_ASSERT (fixup_offset < srofixup->size);
3674   bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
3675 }
3676
3677 /* Return the offset of the generated .got section from the
3678    _GLOBAL_OFFSET_TABLE_ symbol.  */
3679
3680 static bfd_signed_vma
3681 sh_elf_got_offset (struct elf_sh_link_hash_table *htab)
3682 {
3683   return (htab->sgot->output_offset - htab->sgotplt->output_offset
3684           - htab->root.hgot->root.u.def.value);
3685 }
3686
3687 /* Find the segment number in which OSEC, and output section, is
3688    located.  */
3689
3690 static unsigned
3691 sh_elf_osec_to_segment (bfd *output_bfd, asection *osec)
3692 {
3693   Elf_Internal_Phdr *p = NULL;
3694
3695   if (output_bfd->xvec->flavour == bfd_target_elf_flavour
3696       /* PR ld/17110: Do not look for output segments in an input bfd.  */
3697       && output_bfd->direction != read_direction)
3698     p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
3699
3700   /* FIXME: Nothing ever says what this index is relative to.  The kernel
3701      supplies data in terms of the number of load segments but this is
3702      a phdr index and the first phdr may not be a load segment.  */
3703   return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
3704 }
3705
3706 static bfd_boolean
3707 sh_elf_osec_readonly_p (bfd *output_bfd, asection *osec)
3708 {
3709   unsigned seg = sh_elf_osec_to_segment (output_bfd, osec);
3710
3711   return (seg != (unsigned) -1
3712           && ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W));
3713 }
3714
3715 /* Generate the initial contents of a local function descriptor, along
3716    with any relocations or fixups required.  */
3717 static bfd_boolean
3718 sh_elf_initialize_funcdesc (bfd *output_bfd,
3719                             struct bfd_link_info *info,
3720                             struct elf_link_hash_entry *h,
3721                             bfd_vma offset,
3722                             asection *section,
3723                             bfd_vma value)
3724 {
3725   struct elf_sh_link_hash_table *htab;
3726   int dynindx;
3727   bfd_vma addr, seg;
3728
3729   htab = sh_elf_hash_table (info);
3730
3731   /* FIXME: The ABI says that the offset to the function goes in the
3732      descriptor, along with the segment index.  We're RELA, so it could
3733      go in the reloc instead... */
3734
3735   if (h != NULL && SYMBOL_CALLS_LOCAL (info, h))
3736     {
3737       section = h->root.u.def.section;
3738       value = h->root.u.def.value;
3739     }
3740
3741   if (h == NULL || SYMBOL_CALLS_LOCAL (info, h))
3742     {
3743       dynindx = elf_section_data (section->output_section)->dynindx;
3744       addr = value + section->output_offset;
3745       seg = sh_elf_osec_to_segment (output_bfd, section->output_section);
3746     }
3747   else
3748     {
3749       BFD_ASSERT (h->dynindx != -1);
3750       dynindx = h->dynindx;
3751       addr = seg = 0;
3752     }
3753
3754   if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
3755     {
3756       if (h == NULL || h->root.type != bfd_link_hash_undefweak)
3757         {
3758           sh_elf_add_rofixup (output_bfd, htab->srofixup,
3759                               offset
3760                               + htab->sfuncdesc->output_section->vma
3761                               + htab->sfuncdesc->output_offset);
3762           sh_elf_add_rofixup (output_bfd, htab->srofixup,
3763                               offset + 4
3764                               + htab->sfuncdesc->output_section->vma
3765                               + htab->sfuncdesc->output_offset);
3766         }
3767
3768       /* There are no dynamic relocations so fill in the final
3769          address and gp value (barring fixups).  */
3770       addr += section->output_section->vma;
3771       seg = htab->root.hgot->root.u.def.value
3772         + htab->root.hgot->root.u.def.section->output_section->vma
3773         + htab->root.hgot->root.u.def.section->output_offset;
3774     }
3775   else
3776     sh_elf_add_dyn_reloc (output_bfd, htab->srelfuncdesc,
3777                           offset
3778                           + htab->sfuncdesc->output_section->vma
3779                           + htab->sfuncdesc->output_offset,
3780                           R_SH_FUNCDESC_VALUE, dynindx, 0);
3781
3782   bfd_put_32 (output_bfd, addr, htab->sfuncdesc->contents + offset);
3783   bfd_put_32 (output_bfd, seg, htab->sfuncdesc->contents + offset + 4);
3784
3785   return TRUE;
3786 }
3787
3788 /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3789    VALUE is the field's value.  Return bfd_reloc_ok if successful or an error
3790    otherwise.  */
3791
3792 static bfd_reloc_status_type
3793 install_movi20_field (bfd *output_bfd, unsigned long relocation,
3794                       bfd *input_bfd, asection *input_section,
3795                       bfd_byte *contents, bfd_vma offset)
3796 {
3797   unsigned long cur_val;
3798   bfd_byte *addr;
3799   bfd_reloc_status_type r;
3800
3801   if (offset > bfd_get_section_limit (input_bfd, input_section))
3802     return bfd_reloc_outofrange;
3803
3804   r = bfd_check_overflow (complain_overflow_signed, 20, 0,
3805                           bfd_arch_bits_per_address (input_bfd), relocation);
3806   if (r != bfd_reloc_ok)
3807     return r;
3808
3809   addr = contents + offset;
3810   cur_val = bfd_get_16 (output_bfd, addr);
3811   bfd_put_16 (output_bfd, cur_val | ((relocation & 0xf0000) >> 12), addr);
3812   bfd_put_16 (output_bfd, relocation & 0xffff, addr + 2);
3813
3814   return bfd_reloc_ok;
3815 }
3816
3817 /* Relocate an SH ELF section.  */
3818
3819 static bfd_boolean
3820 sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3821                          bfd *input_bfd, asection *input_section,
3822                          bfd_byte *contents, Elf_Internal_Rela *relocs,
3823                          Elf_Internal_Sym *local_syms,
3824                          asection **local_sections)
3825 {
3826   struct elf_sh_link_hash_table *htab;
3827   Elf_Internal_Shdr *symtab_hdr;
3828   struct elf_link_hash_entry **sym_hashes;
3829   Elf_Internal_Rela *rel, *relend;
3830   bfd *dynobj = NULL;
3831   bfd_vma *local_got_offsets;
3832   asection *sgot = NULL;
3833   asection *sgotplt = NULL;
3834   asection *splt = NULL;
3835   asection *sreloc = NULL;
3836   asection *srelgot = NULL;
3837   bfd_boolean is_vxworks_tls;
3838   unsigned isec_segment, got_segment, plt_segment, check_segment[2];
3839   bfd_boolean fdpic_p = FALSE;
3840
3841   BFD_ASSERT (is_sh_elf (input_bfd));
3842
3843   htab = sh_elf_hash_table (info);
3844   if (htab != NULL)
3845     {
3846       dynobj = htab->root.dynobj;
3847       sgot = htab->sgot;
3848       sgotplt = htab->sgotplt;
3849       splt = htab->splt;
3850       fdpic_p = htab->fdpic_p;
3851     }
3852   symtab_hdr = &elf_symtab_hdr (input_bfd);
3853   sym_hashes = elf_sym_hashes (input_bfd);
3854   local_got_offsets = elf_local_got_offsets (input_bfd);
3855
3856   isec_segment = sh_elf_osec_to_segment (output_bfd,
3857                                          input_section->output_section);
3858   if (fdpic_p && sgot)
3859     got_segment = sh_elf_osec_to_segment (output_bfd,
3860                                           sgot->output_section);
3861   else
3862     got_segment = -1;
3863   if (fdpic_p && splt)
3864     plt_segment = sh_elf_osec_to_segment (output_bfd,
3865                                           splt->output_section);
3866   else
3867     plt_segment = -1;
3868
3869   /* We have to handle relocations in vxworks .tls_vars sections
3870      specially, because the dynamic loader is 'weird'.  */
3871   is_vxworks_tls = (htab && htab->vxworks_p && bfd_link_pic (info)
3872                     && !strcmp (input_section->output_section->name,
3873                                 ".tls_vars"));
3874
3875   rel = relocs;
3876   relend = relocs + input_section->reloc_count;
3877   for (; rel < relend; rel++)
3878     {
3879       int r_type;
3880       reloc_howto_type *howto;
3881       unsigned long r_symndx;
3882       Elf_Internal_Sym *sym;
3883       asection *sec;
3884       struct elf_link_hash_entry *h;
3885       bfd_vma relocation;
3886       bfd_vma addend = (bfd_vma) 0;
3887       bfd_reloc_status_type r;
3888       int seen_stt_datalabel = 0;
3889       bfd_vma off;
3890       enum got_type got_type;
3891       const char *symname = NULL;
3892
3893       r_symndx = ELF32_R_SYM (rel->r_info);
3894
3895       r_type = ELF32_R_TYPE (rel->r_info);
3896
3897       /* Many of the relocs are only used for relaxing, and are
3898          handled entirely by the relaxation code.  */
3899       if (r_type >= (int) R_SH_GNU_VTINHERIT
3900           && r_type <= (int) R_SH_LABEL)
3901         continue;
3902       if (r_type == (int) R_SH_NONE)
3903         continue;
3904
3905       if (r_type < 0
3906           || r_type >= R_SH_max
3907           || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
3908               && r_type <= (int) R_SH_LAST_INVALID_RELOC)
3909           || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3910               && r_type <= (int) R_SH_LAST_INVALID_RELOC_2)
3911           || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
3912               && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
3913           || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
3914               && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3915           || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
3916               && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
3917           || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_6
3918               && r_type <= (int) R_SH_LAST_INVALID_RELOC_6))
3919         {
3920           bfd_set_error (bfd_error_bad_value);
3921           return FALSE;
3922         }
3923
3924       howto = get_howto_table (output_bfd) + r_type;
3925
3926       /* For relocs that aren't partial_inplace, we get the addend from
3927          the relocation.  */
3928       if (! howto->partial_inplace)
3929         addend = rel->r_addend;
3930
3931       h = NULL;
3932       sym = NULL;
3933       sec = NULL;
3934       check_segment[0] = -1;
3935       check_segment[1] = -1;
3936       if (r_symndx < symtab_hdr->sh_info)
3937         {
3938           sym = local_syms + r_symndx;
3939           sec = local_sections[r_symndx];
3940
3941           symname = bfd_elf_string_from_elf_section
3942             (input_bfd, symtab_hdr->sh_link, sym->st_name);
3943           if (symname == NULL || *symname == '\0')
3944             symname = bfd_section_name (input_bfd, sec);
3945
3946           relocation = (sec->output_section->vma
3947                         + sec->output_offset
3948                         + sym->st_value);
3949           /* A local symbol never has STO_SH5_ISA32, so we don't need
3950              datalabel processing here.  Make sure this does not change
3951              without notice.  */
3952           if ((sym->st_other & STO_SH5_ISA32) != 0)
3953             (*info->callbacks->reloc_dangerous)
3954               (info,
3955                _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
3956                input_bfd, input_section, rel->r_offset);
3957
3958           if (sec != NULL && discarded_section (sec))
3959             /* Handled below.  */
3960             ;
3961           else if (bfd_link_relocatable (info))
3962             {
3963               /* This is a relocatable link.  We don't have to change
3964                  anything, unless the reloc is against a section symbol,
3965                  in which case we have to adjust according to where the
3966                  section symbol winds up in the output section.  */
3967               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3968                 {
3969                   if (! howto->partial_inplace)
3970                     {
3971                       /* For relocations with the addend in the
3972                          relocation, we need just to update the addend.
3973                          All real relocs are of type partial_inplace; this
3974                          code is mostly for completeness.  */
3975                       rel->r_addend += sec->output_offset;
3976
3977                       continue;
3978                     }
3979
3980                   /* Relocs of type partial_inplace need to pick up the
3981                      contents in the contents and add the offset resulting
3982                      from the changed location of the section symbol.
3983                      Using _bfd_final_link_relocate (e.g. goto
3984                      final_link_relocate) here would be wrong, because
3985                      relocations marked pc_relative would get the current
3986                      location subtracted, and we must only do that at the
3987                      final link.  */
3988                   r = _bfd_relocate_contents (howto, input_bfd,
3989                                               sec->output_offset
3990                                               + sym->st_value,
3991                                               contents + rel->r_offset);
3992                   goto relocation_done;
3993                 }
3994
3995               continue;
3996             }
3997           else if (! howto->partial_inplace)
3998             {
3999               relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4000               addend = rel->r_addend;
4001             }
4002           else if ((sec->flags & SEC_MERGE)
4003                    && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4004             {
4005               asection *msec;
4006
4007               if (howto->rightshift || howto->src_mask != 0xffffffff)
4008                 {
4009                   _bfd_error_handler
4010                     (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
4011                      input_bfd, input_section,
4012                      (long) rel->r_offset, howto->name);
4013                   return FALSE;
4014                 }
4015
4016               addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4017               msec = sec;
4018               addend =
4019                 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
4020                 - relocation;
4021               addend += msec->output_section->vma + msec->output_offset;
4022               bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
4023               addend = 0;
4024             }
4025         }
4026       else
4027         {
4028           /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro.  */
4029
4030           relocation = 0;
4031           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4032           symname = h->root.root.string;
4033           while (h->root.type == bfd_link_hash_indirect
4034                  || h->root.type == bfd_link_hash_warning)
4035             {
4036 #ifdef INCLUDE_SHMEDIA
4037               /* If the reference passes a symbol marked with
4038                  STT_DATALABEL, then any STO_SH5_ISA32 on the final value
4039                  doesn't count.  */
4040               seen_stt_datalabel |= h->type == STT_DATALABEL;
4041 #endif
4042               h = (struct elf_link_hash_entry *) h->root.u.i.link;
4043             }
4044           if (h->root.type == bfd_link_hash_defined
4045               || h->root.type == bfd_link_hash_defweak)
4046             {
4047               bfd_boolean dyn;
4048
4049               dyn = htab ? htab->root.dynamic_sections_created : FALSE;
4050               sec = h->root.u.def.section;
4051               /* In these cases, we don't need the relocation value.
4052                  We check specially because in some obscure cases
4053                  sec->output_section will be NULL.  */
4054               if (r_type == R_SH_GOTPC
4055                   || r_type == R_SH_GOTPC_LOW16
4056                   || r_type == R_SH_GOTPC_MEDLOW16
4057                   || r_type == R_SH_GOTPC_MEDHI16
4058                   || r_type == R_SH_GOTPC_HI16
4059                   || ((r_type == R_SH_PLT32
4060                        || r_type == R_SH_PLT_LOW16
4061                        || r_type == R_SH_PLT_MEDLOW16
4062                        || r_type == R_SH_PLT_MEDHI16
4063                        || r_type == R_SH_PLT_HI16)
4064                       && h->plt.offset != (bfd_vma) -1)
4065                   || ((r_type == R_SH_GOT32
4066                        || r_type == R_SH_GOT20
4067                        || r_type == R_SH_GOTFUNCDESC
4068                        || r_type == R_SH_GOTFUNCDESC20
4069                        || r_type == R_SH_GOTOFFFUNCDESC
4070                        || r_type == R_SH_GOTOFFFUNCDESC20
4071                        || r_type == R_SH_FUNCDESC
4072                        || r_type == R_SH_GOT_LOW16
4073                        || r_type == R_SH_GOT_MEDLOW16
4074                        || r_type == R_SH_GOT_MEDHI16
4075                        || r_type == R_SH_GOT_HI16)
4076                       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4077                                                           bfd_link_pic (info),
4078                                                           h)
4079                       && (! bfd_link_pic (info)
4080                           || (! info->symbolic && h->dynindx != -1)
4081                           || !h->def_regular))
4082                   /* The cases above are those in which relocation is
4083                      overwritten in the switch block below.  The cases
4084                      below are those in which we must defer relocation
4085                      to run-time, because we can't resolve absolute
4086                      addresses when creating a shared library.  */
4087                   || (bfd_link_pic (info)
4088                       && ((! info->symbolic && h->dynindx != -1)
4089                           || !h->def_regular)
4090                       && ((r_type == R_SH_DIR32
4091                            && !h->forced_local)
4092                           || (r_type == R_SH_REL32
4093                               && !SYMBOL_CALLS_LOCAL (info, h)))
4094                       && ((input_section->flags & SEC_ALLOC) != 0
4095                           /* DWARF will emit R_SH_DIR32 relocations in its
4096                              sections against symbols defined externally
4097                              in shared libraries.  We can't do anything
4098                              with them here.  */
4099                           || ((input_section->flags & SEC_DEBUGGING) != 0
4100                               && h->def_dynamic)))
4101                   /* Dynamic relocs are not propagated for SEC_DEBUGGING
4102                      sections because such sections are not SEC_ALLOC and
4103                      thus ld.so will not process them.  */
4104                   || (sec->output_section == NULL
4105                       && ((input_section->flags & SEC_DEBUGGING) != 0
4106                           && h->def_dynamic))
4107                   || (sec->output_section == NULL
4108                       && (sh_elf_hash_entry (h)->got_type == GOT_TLS_IE
4109                           || sh_elf_hash_entry (h)->got_type == GOT_TLS_GD)))
4110                 ;
4111               else if (sec->output_section != NULL)
4112                 relocation = ((h->root.u.def.value
4113                               + sec->output_section->vma
4114                               + sec->output_offset)
4115                               /* A STO_SH5_ISA32 causes a "bitor 1" to the
4116                                  symbol value, unless we've seen
4117                                  STT_DATALABEL on the way to it.  */
4118                               | ((h->other & STO_SH5_ISA32) != 0
4119                                  && ! seen_stt_datalabel));
4120               else if (!bfd_link_relocatable (info)
4121                        && (_bfd_elf_section_offset (output_bfd, info,
4122                                                     input_section,
4123                                                     rel->r_offset)
4124                            != (bfd_vma) -1))
4125                 {
4126                   _bfd_error_handler
4127                     (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4128                      input_bfd,
4129                      input_section,
4130                      (long) rel->r_offset,
4131                      howto->name,
4132                      h->root.root.string);
4133                   return FALSE;
4134                 }
4135             }
4136           else if (h->root.type == bfd_link_hash_undefweak)
4137             ;
4138           else if (info->unresolved_syms_in_objects == RM_IGNORE
4139                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4140             ;
4141           else if (!bfd_link_relocatable (info))
4142             (*info->callbacks->undefined_symbol)
4143               (info, h->root.root.string, input_bfd,
4144                input_section, rel->r_offset,
4145                (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
4146                 || ELF_ST_VISIBILITY (h->other)));
4147         }
4148
4149       if (sec != NULL && discarded_section (sec))
4150         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4151                                          rel, 1, relend, howto, 0, contents);
4152
4153       if (bfd_link_relocatable (info))
4154         continue;
4155
4156       /* Check for inter-segment relocations in FDPIC files.  Most
4157          relocations connect the relocation site to the location of
4158          the target symbol, but there are some exceptions below.  */
4159       check_segment[0] = isec_segment;
4160       if (sec != NULL)
4161         check_segment[1] = sh_elf_osec_to_segment (output_bfd,
4162                                                    sec->output_section);
4163       else
4164         check_segment[1] = -1;
4165
4166       switch ((int) r_type)
4167         {
4168         final_link_relocate:
4169           /* COFF relocs don't use the addend. The addend is used for
4170              R_SH_DIR32 to be compatible with other compilers.  */
4171           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4172                                         contents, rel->r_offset,
4173                                         relocation, addend);
4174           break;
4175
4176         case R_SH_IND12W:
4177           goto final_link_relocate;
4178
4179         case R_SH_DIR8WPN:
4180         case R_SH_DIR8WPZ:
4181         case R_SH_DIR8WPL:
4182           /* If the reloc is against the start of this section, then
4183              the assembler has already taken care of it and the reloc
4184              is here only to assist in relaxing.  If the reloc is not
4185              against the start of this section, then it's against an
4186              external symbol and we must deal with it ourselves.  */
4187           if (input_section->output_section->vma + input_section->output_offset
4188               != relocation)
4189             {
4190               int disp = (relocation
4191                           - input_section->output_section->vma
4192                           - input_section->output_offset
4193                           - rel->r_offset);
4194               int mask = 0;
4195               switch (r_type)
4196                 {
4197                 case R_SH_DIR8WPN:
4198                 case R_SH_DIR8WPZ: mask = 1; break;
4199                 case R_SH_DIR8WPL: mask = 3; break;
4200                 default: mask = 0; break;
4201                 }
4202               if (disp & mask)
4203                 {
4204                   _bfd_error_handler
4205                     (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
4206                      input_section->owner,
4207                      (unsigned long) rel->r_offset);
4208                   bfd_set_error (bfd_error_bad_value);
4209                   return FALSE;
4210                 }
4211               relocation -= 4;
4212               goto final_link_relocate;
4213             }
4214           r = bfd_reloc_ok;
4215           break;
4216
4217         default:
4218 #ifdef INCLUDE_SHMEDIA
4219           if (shmedia_prepare_reloc (info, input_bfd, input_section,
4220                                      contents, rel, &relocation))
4221             goto final_link_relocate;
4222 #endif
4223           bfd_set_error (bfd_error_bad_value);
4224           return FALSE;
4225
4226         case R_SH_DIR16:
4227         case R_SH_DIR8:
4228         case R_SH_DIR8U:
4229         case R_SH_DIR8S:
4230         case R_SH_DIR4U:
4231           goto final_link_relocate;
4232
4233         case R_SH_DIR8UL:
4234         case R_SH_DIR4UL:
4235           if (relocation & 3)
4236             {
4237               _bfd_error_handler
4238                 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
4239                  input_section->owner,
4240                  (unsigned long) rel->r_offset, howto->name,
4241                  (unsigned long) relocation);
4242               bfd_set_error (bfd_error_bad_value);
4243               return FALSE;
4244             }
4245           goto final_link_relocate;
4246
4247         case R_SH_DIR8UW:
4248         case R_SH_DIR8SW:
4249         case R_SH_DIR4UW:
4250           if (relocation & 1)
4251             {
4252               _bfd_error_handler
4253                 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
4254                  input_section->owner,
4255                  (unsigned long) rel->r_offset, howto->name,
4256                  (unsigned long) relocation);
4257               bfd_set_error (bfd_error_bad_value);
4258               return FALSE;
4259             }
4260           goto final_link_relocate;
4261
4262         case R_SH_PSHA:
4263           if ((signed int)relocation < -32
4264               || (signed int)relocation > 32)
4265             {
4266               _bfd_error_handler
4267                 (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"),
4268                  input_section->owner,
4269                  (unsigned long) rel->r_offset,
4270                  (unsigned long) relocation);
4271               bfd_set_error (bfd_error_bad_value);
4272               return FALSE;
4273             }
4274           goto final_link_relocate;
4275
4276         case R_SH_PSHL:
4277           if ((signed int)relocation < -16
4278               || (signed int)relocation > 16)
4279             {
4280               _bfd_error_handler
4281                 (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"),
4282                  input_section->owner,
4283                  (unsigned long) rel->r_offset,
4284                  (unsigned long) relocation);
4285               bfd_set_error (bfd_error_bad_value);
4286               return FALSE;
4287             }
4288           goto final_link_relocate;
4289
4290         case R_SH_DIR32:
4291         case R_SH_REL32:
4292 #ifdef INCLUDE_SHMEDIA
4293         case R_SH_IMM_LOW16_PCREL:
4294         case R_SH_IMM_MEDLOW16_PCREL:
4295         case R_SH_IMM_MEDHI16_PCREL:
4296         case R_SH_IMM_HI16_PCREL:
4297 #endif
4298           if (bfd_link_pic (info)
4299               && (h == NULL
4300                   || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4301                   || h->root.type != bfd_link_hash_undefweak)
4302               && r_symndx != STN_UNDEF
4303               && (input_section->flags & SEC_ALLOC) != 0
4304               && !is_vxworks_tls
4305               && (r_type == R_SH_DIR32
4306                   || !SYMBOL_CALLS_LOCAL (info, h)))
4307             {
4308               Elf_Internal_Rela outrel;
4309               bfd_byte *loc;
4310               bfd_boolean skip, relocate;
4311
4312               /* When generating a shared object, these relocations
4313                  are copied into the output file to be resolved at run
4314                  time.  */
4315
4316               if (sreloc == NULL)
4317                 {
4318                   sreloc = _bfd_elf_get_dynamic_reloc_section
4319                     (input_bfd, input_section, /*rela?*/ TRUE);
4320                   if (sreloc == NULL)
4321                     return FALSE;
4322                 }
4323
4324               skip = FALSE;
4325               relocate = FALSE;
4326
4327               outrel.r_offset =
4328                 _bfd_elf_section_offset (output_bfd, info, input_section,
4329                                          rel->r_offset);
4330               if (outrel.r_offset == (bfd_vma) -1)
4331                 skip = TRUE;
4332               else if (outrel.r_offset == (bfd_vma) -2)
4333                 skip = TRUE, relocate = TRUE;
4334               outrel.r_offset += (input_section->output_section->vma
4335                                   + input_section->output_offset);
4336
4337               if (skip)
4338                 memset (&outrel, 0, sizeof outrel);
4339               else if (r_type == R_SH_REL32)
4340                 {
4341                   BFD_ASSERT (h != NULL && h->dynindx != -1);
4342                   outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
4343                   outrel.r_addend
4344                     = (howto->partial_inplace
4345                        ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4346                        : addend);
4347                 }
4348 #ifdef INCLUDE_SHMEDIA
4349               else if (r_type == R_SH_IMM_LOW16_PCREL
4350                        || r_type == R_SH_IMM_MEDLOW16_PCREL
4351                        || r_type == R_SH_IMM_MEDHI16_PCREL
4352                        || r_type == R_SH_IMM_HI16_PCREL)
4353                 {
4354                   BFD_ASSERT (h != NULL && h->dynindx != -1);
4355                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4356                   outrel.r_addend = addend;
4357                 }
4358 #endif
4359               else if (fdpic_p
4360                        && (h == NULL
4361                            || ((info->symbolic || h->dynindx == -1)
4362                                && h->def_regular)))
4363                 {
4364                   int dynindx;
4365
4366                   BFD_ASSERT (sec != NULL);
4367                   BFD_ASSERT (sec->output_section != NULL);
4368                   dynindx = elf_section_data (sec->output_section)->dynindx;
4369                   outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4370                   outrel.r_addend = relocation;
4371                   outrel.r_addend
4372                     += (howto->partial_inplace
4373                         ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4374                         : addend);
4375                   outrel.r_addend -= sec->output_section->vma;
4376                 }
4377               else
4378                 {
4379                   /* h->dynindx may be -1 if this symbol was marked to
4380                      become local.  */
4381                   if (h == NULL
4382                       || ((info->symbolic || h->dynindx == -1)
4383                           && h->def_regular))
4384                     {
4385                       relocate = howto->partial_inplace;
4386                       outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4387                     }
4388                   else
4389                     {
4390                       BFD_ASSERT (h->dynindx != -1);
4391                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
4392                     }
4393                   outrel.r_addend = relocation;
4394                   outrel.r_addend
4395                     += (howto->partial_inplace
4396                         ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4397                         : addend);
4398                 }
4399
4400               loc = sreloc->contents;
4401               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4402               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4403
4404               check_segment[0] = check_segment[1] = -1;
4405
4406               /* If this reloc is against an external symbol, we do
4407                  not want to fiddle with the addend.  Otherwise, we
4408                  need to include the symbol value so that it becomes
4409                  an addend for the dynamic reloc.  */
4410               if (! relocate)
4411                 continue;
4412             }
4413           else if (fdpic_p && !bfd_link_pic (info)
4414                    && r_type == R_SH_DIR32
4415                    && (input_section->flags & SEC_ALLOC) != 0)
4416             {
4417               bfd_vma offset;
4418
4419               BFD_ASSERT (htab);
4420
4421                 if (sh_elf_osec_readonly_p (output_bfd,
4422                                             input_section->output_section))
4423                   {
4424                     _bfd_error_handler
4425                       (_("%B(%A+0x%lx): cannot emit fixup to `%s' in read-only section"),
4426                        input_bfd,
4427                        input_section,
4428                        (long) rel->r_offset,
4429                        symname);
4430                     return FALSE;
4431                   }
4432
4433               offset = _bfd_elf_section_offset (output_bfd, info,
4434                                                 input_section, rel->r_offset);
4435               if (offset != (bfd_vma)-1)
4436                 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4437                                     input_section->output_section->vma
4438                                     + input_section->output_offset
4439                                     + rel->r_offset);
4440
4441               check_segment[0] = check_segment[1] = -1;
4442             }
4443             /* We don't want warnings for non-NULL tests on undefined weak
4444                symbols.  */
4445             else if (r_type == R_SH_REL32
4446                      && h
4447                      && h->root.type == bfd_link_hash_undefweak)
4448               check_segment[0] = check_segment[1] = -1;
4449           goto final_link_relocate;
4450
4451         case R_SH_GOTPLT32:
4452 #ifdef INCLUDE_SHMEDIA
4453         case R_SH_GOTPLT_LOW16:
4454         case R_SH_GOTPLT_MEDLOW16:
4455         case R_SH_GOTPLT_MEDHI16:
4456         case R_SH_GOTPLT_HI16:
4457         case R_SH_GOTPLT10BY4:
4458         case R_SH_GOTPLT10BY8:
4459 #endif
4460           /* Relocation is to the entry for this symbol in the
4461              procedure linkage table.  */
4462
4463           if (h == NULL
4464               || h->forced_local
4465               || ! bfd_link_pic (info)
4466               || info->symbolic
4467               || h->dynindx == -1
4468               || h->plt.offset == (bfd_vma) -1
4469               || h->got.offset != (bfd_vma) -1)
4470             goto force_got;
4471
4472           /* Relocation is to the entry for this symbol in the global
4473              offset table extension for the procedure linkage table.  */
4474
4475           BFD_ASSERT (htab);
4476           BFD_ASSERT (sgotplt != NULL);
4477           relocation = (sgotplt->output_offset
4478                         + (get_plt_index (htab->plt_info, h->plt.offset)
4479                            + 3) * 4);
4480
4481 #ifdef GOT_BIAS
4482           relocation -= GOT_BIAS;
4483 #endif
4484
4485           goto final_link_relocate;
4486
4487         force_got:
4488         case R_SH_GOT32:
4489         case R_SH_GOT20:
4490 #ifdef INCLUDE_SHMEDIA
4491         case R_SH_GOT_LOW16:
4492         case R_SH_GOT_MEDLOW16:
4493         case R_SH_GOT_MEDHI16:
4494         case R_SH_GOT_HI16:
4495         case R_SH_GOT10BY4:
4496         case R_SH_GOT10BY8:
4497 #endif
4498           /* Relocation is to the entry for this symbol in the global
4499              offset table.  */
4500
4501           BFD_ASSERT (htab);
4502           BFD_ASSERT (sgot != NULL);
4503           check_segment[0] = check_segment[1] = -1;
4504
4505           if (h != NULL)
4506             {
4507               bfd_boolean dyn;
4508
4509               off = h->got.offset;
4510 #ifdef INCLUDE_SHMEDIA
4511               if (seen_stt_datalabel)
4512                 {
4513                   struct elf_sh_link_hash_entry *hsh;
4514
4515                   hsh = (struct elf_sh_link_hash_entry *)h;
4516                   off = hsh->datalabel_got.offset;
4517                 }
4518 #endif
4519               BFD_ASSERT (off != (bfd_vma) -1);
4520
4521               dyn = htab->root.dynamic_sections_created;
4522               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4523                                                      bfd_link_pic (info),
4524                                                      h)
4525                   || (bfd_link_pic (info)
4526                       && SYMBOL_REFERENCES_LOCAL (info, h))
4527                   || (ELF_ST_VISIBILITY (h->other)
4528                       && h->root.type == bfd_link_hash_undefweak))
4529                 {
4530                   /* This is actually a static link, or it is a
4531                      -Bsymbolic link and the symbol is defined
4532                      locally, or the symbol was forced to be local
4533                      because of a version file.  We must initialize
4534                      this entry in the global offset table.  Since the
4535                      offset must always be a multiple of 4, we use the
4536                      least significant bit to record whether we have
4537                      initialized it already.
4538
4539                      When doing a dynamic link, we create a .rela.got
4540                      relocation entry to initialize the value.  This
4541                      is done in the finish_dynamic_symbol routine.  */
4542                   if ((off & 1) != 0)
4543                     off &= ~1;
4544                   else
4545                     {
4546                       bfd_put_32 (output_bfd, relocation,
4547                                   sgot->contents + off);
4548 #ifdef INCLUDE_SHMEDIA
4549                       if (seen_stt_datalabel)
4550                         {
4551                           struct elf_sh_link_hash_entry *hsh;
4552
4553                           hsh = (struct elf_sh_link_hash_entry *)h;
4554                           hsh->datalabel_got.offset |= 1;
4555                         }
4556                       else
4557 #endif
4558                         h->got.offset |= 1;
4559
4560                       /* If we initialize the GOT entry here with a valid
4561                          symbol address, also add a fixup.  */
4562                       if (fdpic_p && !bfd_link_pic (info)
4563                           && sh_elf_hash_entry (h)->got_type == GOT_NORMAL
4564                           && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4565                               || h->root.type != bfd_link_hash_undefweak))
4566                         sh_elf_add_rofixup (output_bfd, htab->srofixup,
4567                                             sgot->output_section->vma
4568                                             + sgot->output_offset
4569                                             + off);
4570                     }
4571                 }
4572
4573               relocation = sh_elf_got_offset (htab) + off;
4574             }
4575           else
4576             {
4577 #ifdef INCLUDE_SHMEDIA
4578               if (rel->r_addend)
4579                 {
4580                   BFD_ASSERT (local_got_offsets != NULL
4581                               && (local_got_offsets[symtab_hdr->sh_info
4582                                                     + r_symndx]
4583                                   != (bfd_vma) -1));
4584
4585                   off = local_got_offsets[symtab_hdr->sh_info
4586                                           + r_symndx];
4587                 }
4588               else
4589                 {
4590 #endif
4591               BFD_ASSERT (local_got_offsets != NULL
4592                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
4593
4594               off = local_got_offsets[r_symndx];
4595 #ifdef INCLUDE_SHMEDIA
4596                 }
4597 #endif
4598
4599               /* The offset must always be a multiple of 4.  We use
4600                  the least significant bit to record whether we have
4601                  already generated the necessary reloc.  */
4602               if ((off & 1) != 0)
4603                 off &= ~1;
4604               else
4605                 {
4606                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4607
4608                   if (bfd_link_pic (info))
4609                     {
4610                       Elf_Internal_Rela outrel;
4611                       bfd_byte *loc;
4612
4613                       if (srelgot == NULL)
4614                         {
4615                           srelgot = bfd_get_linker_section (dynobj,
4616                                                             ".rela.got");
4617                           BFD_ASSERT (srelgot != NULL);
4618                         }
4619
4620                       outrel.r_offset = (sgot->output_section->vma
4621                                          + sgot->output_offset
4622                                          + off);
4623                       if (fdpic_p)
4624                         {
4625                           int dynindx
4626                             = elf_section_data (sec->output_section)->dynindx;
4627                           outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4628                           outrel.r_addend = relocation;
4629                           outrel.r_addend -= sec->output_section->vma;
4630                         }
4631                       else
4632                         {
4633                           outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4634                           outrel.r_addend = relocation;
4635                         }
4636                       loc = srelgot->contents;
4637                       loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4638                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4639                     }
4640                   else if (fdpic_p
4641                            && (sh_elf_local_got_type (input_bfd) [r_symndx]
4642                                == GOT_NORMAL))
4643                     sh_elf_add_rofixup (output_bfd, htab->srofixup,
4644                                         sgot->output_section->vma
4645                                         + sgot->output_offset
4646                                         + off);
4647
4648 #ifdef INCLUDE_SHMEDIA
4649                   if (rel->r_addend)
4650                     local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
4651                   else
4652 #endif
4653                     local_got_offsets[r_symndx] |= 1;
4654                 }
4655
4656               relocation = sh_elf_got_offset (htab) + off;
4657             }
4658
4659 #ifdef GOT_BIAS
4660           relocation -= GOT_BIAS;
4661 #endif
4662
4663           if (r_type == R_SH_GOT20)
4664             {
4665               r = install_movi20_field (output_bfd, relocation + addend,
4666                                         input_bfd, input_section, contents,
4667                                         rel->r_offset);
4668               break;
4669             }
4670           else
4671             goto final_link_relocate;
4672
4673         case R_SH_GOTOFF:
4674         case R_SH_GOTOFF20:
4675 #ifdef INCLUDE_SHMEDIA
4676         case R_SH_GOTOFF_LOW16:
4677         case R_SH_GOTOFF_MEDLOW16:
4678         case R_SH_GOTOFF_MEDHI16:
4679         case R_SH_GOTOFF_HI16:
4680 #endif
4681           /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
4682              we place at the start of the .got.plt section.  This is the same
4683              as the start of the output .got section, unless there are function
4684              descriptors in front of it.  */
4685           BFD_ASSERT (htab);
4686           BFD_ASSERT (sgotplt != NULL);
4687           check_segment[0] = got_segment;
4688           relocation -= sgotplt->output_section->vma + sgotplt->output_offset
4689             + htab->root.hgot->root.u.def.value;
4690
4691 #ifdef GOT_BIAS
4692           relocation -= GOT_BIAS;
4693 #endif
4694
4695           addend = rel->r_addend;
4696
4697           if (r_type == R_SH_GOTOFF20)
4698             {
4699               r = install_movi20_field (output_bfd, relocation + addend,
4700                                         input_bfd, input_section, contents,
4701                                         rel->r_offset);
4702               break;
4703             }
4704           else
4705             goto final_link_relocate;
4706
4707         case R_SH_GOTPC:
4708 #ifdef INCLUDE_SHMEDIA
4709         case R_SH_GOTPC_LOW16:
4710         case R_SH_GOTPC_MEDLOW16:
4711         case R_SH_GOTPC_MEDHI16:
4712         case R_SH_GOTPC_HI16:
4713 #endif
4714           /* Use global offset table as symbol value.  */
4715
4716           BFD_ASSERT (sgotplt != NULL);
4717           relocation = sgotplt->output_section->vma + sgotplt->output_offset;
4718
4719 #ifdef GOT_BIAS
4720           relocation += GOT_BIAS;
4721 #endif
4722
4723           addend = rel->r_addend;
4724
4725           goto final_link_relocate;
4726
4727         case R_SH_PLT32:
4728 #ifdef INCLUDE_SHMEDIA
4729         case R_SH_PLT_LOW16:
4730         case R_SH_PLT_MEDLOW16:
4731         case R_SH_PLT_MEDHI16:
4732         case R_SH_PLT_HI16:
4733 #endif
4734           /* Relocation is to the entry for this symbol in the
4735              procedure linkage table.  */
4736
4737           /* Resolve a PLT reloc against a local symbol directly,
4738              without using the procedure linkage table.  */
4739           if (h == NULL)
4740             goto final_link_relocate;
4741
4742           /* We don't want to warn on calls to undefined weak symbols,
4743              as calls to them must be protected by non-NULL tests
4744              anyway, and unprotected calls would invoke undefined
4745              behavior.  */
4746           if (h->root.type == bfd_link_hash_undefweak)
4747             check_segment[0] = check_segment[1] = -1;
4748
4749           if (h->forced_local)
4750             goto final_link_relocate;
4751
4752           if (h->plt.offset == (bfd_vma) -1)
4753             {
4754               /* We didn't make a PLT entry for this symbol.  This
4755                  happens when statically linking PIC code, or when
4756                  using -Bsymbolic.  */
4757               goto final_link_relocate;
4758             }
4759
4760           BFD_ASSERT (splt != NULL);
4761           check_segment[1] = plt_segment;
4762           relocation = (splt->output_section->vma
4763                         + splt->output_offset
4764                         + h->plt.offset);
4765
4766 #ifdef INCLUDE_SHMEDIA
4767           relocation++;
4768 #endif
4769
4770           addend = rel->r_addend;
4771
4772           goto final_link_relocate;
4773
4774         /* Relocation is to the canonical function descriptor for this
4775            symbol, possibly via the GOT.  Initialize the GOT
4776            entry and function descriptor if necessary.  */
4777         case R_SH_GOTFUNCDESC:
4778         case R_SH_GOTFUNCDESC20:
4779         case R_SH_FUNCDESC:
4780           {
4781             int dynindx = -1;
4782             asection *reloc_section;
4783             bfd_vma reloc_offset;
4784             int reloc_type = R_SH_FUNCDESC;
4785
4786             BFD_ASSERT (htab);
4787
4788             check_segment[0] = check_segment[1] = -1;
4789
4790             /* FIXME: See what FRV does for global symbols in the
4791                executable, with --export-dynamic.  Do they need ld.so
4792                to allocate official descriptors?  See what this code
4793                does.  */
4794
4795             relocation = 0;
4796             addend = 0;
4797
4798             if (r_type == R_SH_FUNCDESC)
4799               {
4800                 reloc_section = input_section;
4801                 reloc_offset = rel->r_offset;
4802               }
4803             else
4804               {
4805                 reloc_section = sgot;
4806
4807                 if (h != NULL)
4808                   reloc_offset = h->got.offset;
4809                 else
4810                   {
4811                     BFD_ASSERT (local_got_offsets != NULL);
4812                     reloc_offset = local_got_offsets[r_symndx];
4813                   }
4814                 BFD_ASSERT (reloc_offset != MINUS_ONE);
4815
4816                 if (reloc_offset & 1)
4817                   {
4818                     reloc_offset &= ~1;
4819                     goto funcdesc_done_got;
4820                   }
4821               }
4822
4823             if (h && h->root.type == bfd_link_hash_undefweak
4824                 && (SYMBOL_CALLS_LOCAL (info, h)
4825                     || !htab->root.dynamic_sections_created))
4826               /* Undefined weak symbol which will not be dynamically
4827                  resolved later; leave it at zero.  */
4828               goto funcdesc_leave_zero;
4829             else if (SYMBOL_CALLS_LOCAL (info, h)
4830                      && ! SYMBOL_FUNCDESC_LOCAL (info, h))
4831               {
4832                 /* If the symbol needs a non-local function descriptor
4833                    but binds locally (i.e., its visibility is
4834                    protected), emit a dynamic relocation decayed to
4835                    section+offset.  This is an optimization; the dynamic
4836                    linker would resolve our function descriptor request
4837                    to our copy of the function anyway.  */
4838                 dynindx = elf_section_data (h->root.u.def.section
4839                                             ->output_section)->dynindx;
4840                 relocation += h->root.u.def.section->output_offset
4841                   + h->root.u.def.value;
4842               }
4843             else if (! SYMBOL_FUNCDESC_LOCAL (info, h))
4844               {
4845                 /* If the symbol is dynamic and there will be dynamic
4846                    symbol resolution because we are or are linked with a
4847                    shared library, emit a FUNCDESC relocation such that
4848                    the dynamic linker will allocate the function
4849                    descriptor.  */
4850                 BFD_ASSERT (h->dynindx != -1);
4851                 dynindx = h->dynindx;
4852               }
4853             else
4854               {
4855                 bfd_vma offset;
4856
4857                 /* Otherwise, we know we have a private function
4858                    descriptor, so reference it directly.  */
4859                 reloc_type = R_SH_DIR32;
4860                 dynindx = elf_section_data (htab->sfuncdesc
4861                                             ->output_section)->dynindx;
4862
4863                 if (h)
4864                   {
4865                     offset = sh_elf_hash_entry (h)->funcdesc.offset;
4866                     BFD_ASSERT (offset != MINUS_ONE);
4867                     if ((offset & 1) == 0)
4868                       {
4869                         if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
4870                                                          offset, NULL, 0))
4871                           return FALSE;
4872                         sh_elf_hash_entry (h)->funcdesc.offset |= 1;
4873                       }
4874                   }
4875                 else
4876                   {
4877                     union gotref *local_funcdesc;
4878
4879                     local_funcdesc = sh_elf_local_funcdesc (input_bfd);
4880                     offset = local_funcdesc[r_symndx].offset;
4881                     BFD_ASSERT (offset != MINUS_ONE);
4882                     if ((offset & 1) == 0)
4883                       {
4884                         if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
4885                                                          offset, sec,
4886                                                          sym->st_value))
4887                           return FALSE;
4888                         local_funcdesc[r_symndx].offset |= 1;
4889                       }
4890                   }
4891
4892                 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
4893               }
4894
4895             if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
4896               {
4897                 bfd_vma offset;
4898
4899                 if (sh_elf_osec_readonly_p (output_bfd,
4900                                             reloc_section->output_section))
4901                   {
4902                     _bfd_error_handler
4903                       (_("%B(%A+0x%lx): cannot emit fixup to `%s' in read-only section"),
4904                        input_bfd,
4905                        input_section,
4906                        (long) rel->r_offset,
4907                        symname);
4908                     return FALSE;
4909                   }
4910
4911                 offset = _bfd_elf_section_offset (output_bfd, info,
4912                                                   reloc_section, reloc_offset);
4913
4914                 if (offset != (bfd_vma)-1)
4915                   sh_elf_add_rofixup (output_bfd, htab->srofixup,
4916                                       offset
4917                                       + reloc_section->output_section->vma
4918                                       + reloc_section->output_offset);
4919               }
4920             else if ((reloc_section->output_section->flags
4921                       & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
4922               {
4923                 bfd_vma offset;
4924
4925                 if (sh_elf_osec_readonly_p (output_bfd,
4926                                             reloc_section->output_section))
4927                   {
4928                     info->callbacks->warning
4929                       (info,
4930                        _("cannot emit dynamic relocations in read-only section"),
4931                        symname, input_bfd, reloc_section, reloc_offset);
4932                     return FALSE;
4933                   }
4934
4935                 if (srelgot == NULL)
4936                   {
4937                     srelgot = bfd_get_linker_section (dynobj, ".rela.got");
4938                     BFD_ASSERT (srelgot != NULL);
4939                   }
4940
4941                 offset = _bfd_elf_section_offset (output_bfd, info,
4942                                                   reloc_section, reloc_offset);
4943
4944                 if (offset != (bfd_vma)-1)
4945                   sh_elf_add_dyn_reloc (output_bfd, srelgot,
4946                                         offset
4947                                         + reloc_section->output_section->vma
4948                                         + reloc_section->output_offset,
4949                                         reloc_type, dynindx, relocation);
4950
4951                 if (r_type == R_SH_FUNCDESC)
4952                   {
4953                     r = bfd_reloc_ok;
4954                     break;
4955                   }
4956                 else
4957                   {
4958                     relocation = 0;
4959                     goto funcdesc_leave_zero;
4960                   }
4961               }
4962
4963             if (SYMBOL_FUNCDESC_LOCAL (info, h))
4964               relocation += htab->sfuncdesc->output_section->vma;
4965           funcdesc_leave_zero:
4966             if (r_type != R_SH_FUNCDESC)
4967               {
4968                 bfd_put_32 (output_bfd, relocation,
4969                             reloc_section->contents + reloc_offset);
4970                 if (h != NULL)
4971                   h->got.offset |= 1;
4972                 else
4973                   local_got_offsets[r_symndx] |= 1;
4974
4975               funcdesc_done_got:
4976
4977                 relocation = sh_elf_got_offset (htab) + reloc_offset;
4978 #ifdef GOT_BIAS
4979                 relocation -= GOT_BIAS;
4980 #endif
4981               }
4982             if (r_type == R_SH_GOTFUNCDESC20)
4983               {
4984                 r = install_movi20_field (output_bfd, relocation + addend,
4985                                           input_bfd, input_section, contents,
4986                                           rel->r_offset);
4987                 break;
4988               }
4989             else
4990               goto final_link_relocate;
4991           }
4992           break;
4993
4994         case R_SH_GOTOFFFUNCDESC:
4995         case R_SH_GOTOFFFUNCDESC20:
4996           /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
4997              executable and --export-dynamic.  If such symbols get
4998              ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
4999              for them.  */
5000           BFD_ASSERT (htab);
5001
5002           check_segment[0] = check_segment[1] = -1;
5003           relocation = 0;
5004           addend = rel->r_addend;
5005
5006           if (h && (h->root.type == bfd_link_hash_undefweak
5007                     || !SYMBOL_FUNCDESC_LOCAL (info, h)))
5008             {
5009               _bfd_error_handler
5010                 (_("%B(%A+0x%lx): %s relocation against external symbol \"%s\""),
5011                  input_bfd, input_section, (long) rel->r_offset, howto->name,
5012                  h->root.root.string);
5013               return FALSE;
5014             }
5015           else
5016             {
5017               bfd_vma offset;
5018
5019               /* Otherwise, we know we have a private function
5020                  descriptor, so reference it directly.  */
5021               if (h)
5022                 {
5023                   offset = sh_elf_hash_entry (h)->funcdesc.offset;
5024                   BFD_ASSERT (offset != MINUS_ONE);
5025                   if ((offset & 1) == 0)
5026                     {
5027                       if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
5028                                                        offset, NULL, 0))
5029                         return FALSE;
5030                       sh_elf_hash_entry (h)->funcdesc.offset |= 1;
5031                     }
5032                 }
5033               else
5034                 {
5035                   union gotref *local_funcdesc;
5036
5037                   local_funcdesc = sh_elf_local_funcdesc (input_bfd);
5038                   offset = local_funcdesc[r_symndx].offset;
5039                   BFD_ASSERT (offset != MINUS_ONE);
5040                   if ((offset & 1) == 0)
5041                     {
5042                       if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
5043                                                        offset, sec,
5044                                                        sym->st_value))
5045                         return FALSE;
5046                       local_funcdesc[r_symndx].offset |= 1;
5047                     }
5048                 }
5049
5050               relocation = htab->sfuncdesc->output_offset + (offset & ~1);
5051             }
5052
5053           relocation -= (htab->root.hgot->root.u.def.value
5054                          + sgotplt->output_offset);
5055 #ifdef GOT_BIAS
5056           relocation -= GOT_BIAS;
5057 #endif
5058
5059           if (r_type == R_SH_GOTOFFFUNCDESC20)
5060             {
5061               r = install_movi20_field (output_bfd, relocation + addend,
5062                                         input_bfd, input_section, contents,
5063                                         rel->r_offset);
5064               break;
5065             }
5066           else
5067             goto final_link_relocate;
5068
5069         case R_SH_LOOP_START:
5070           {
5071             static bfd_vma start, end;
5072
5073             start = (relocation + rel->r_addend
5074                      - (sec->output_section->vma + sec->output_offset));
5075             r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5076                                    rel->r_offset, sec, start, end);
5077             break;
5078
5079         case R_SH_LOOP_END:
5080             end = (relocation + rel->r_addend
5081                    - (sec->output_section->vma + sec->output_offset));
5082             r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5083                                    rel->r_offset, sec, start, end);
5084             break;
5085           }
5086
5087         case R_SH_TLS_GD_32:
5088         case R_SH_TLS_IE_32:
5089           BFD_ASSERT (htab);
5090           check_segment[0] = check_segment[1] = -1;
5091           r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5092           got_type = GOT_UNKNOWN;
5093           if (h == NULL && local_got_offsets)
5094             got_type = sh_elf_local_got_type (input_bfd) [r_symndx];
5095           else if (h != NULL)
5096             {
5097               got_type = sh_elf_hash_entry (h)->got_type;
5098               if (! bfd_link_pic (info)
5099                   && (h->dynindx == -1
5100                       || h->def_regular))
5101                 r_type = R_SH_TLS_LE_32;
5102             }
5103
5104           if (r_type == R_SH_TLS_GD_32 && got_type == GOT_TLS_IE)
5105             r_type = R_SH_TLS_IE_32;
5106
5107           if (r_type == R_SH_TLS_LE_32)
5108             {
5109               bfd_vma offset;
5110               unsigned short insn;
5111
5112               if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
5113                 {
5114                   /* GD->LE transition:
5115                        mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5116                        jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5117                        1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5118                      We change it into:
5119                        mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
5120                        nop; nop; ...
5121                        1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:.  */
5122
5123                   offset = rel->r_offset;
5124                   BFD_ASSERT (offset >= 16);
5125                   /* Size of GD instructions is 16 or 18.  */
5126                   offset -= 16;
5127                   insn = bfd_get_16 (input_bfd, contents + offset + 0);
5128                   if ((insn & 0xff00) == 0xc700)
5129                     {
5130                       BFD_ASSERT (offset >= 2);
5131                       offset -= 2;
5132                       insn = bfd_get_16 (input_bfd, contents + offset + 0);
5133                     }
5134
5135                   BFD_ASSERT ((insn & 0xff00) == 0xd400);
5136                   insn = bfd_get_16 (input_bfd, contents + offset + 2);
5137                   BFD_ASSERT ((insn & 0xff00) == 0xc700);
5138                   insn = bfd_get_16 (input_bfd, contents + offset + 4);
5139                   BFD_ASSERT ((insn & 0xff00) == 0xd100);
5140                   insn = bfd_get_16 (input_bfd, contents + offset + 6);
5141                   BFD_ASSERT (insn == 0x310c);
5142                   insn = bfd_get_16 (input_bfd, contents + offset + 8);
5143                   BFD_ASSERT (insn == 0x410b);
5144                   insn = bfd_get_16 (input_bfd, contents + offset + 10);
5145                   BFD_ASSERT (insn == 0x34cc);
5146
5147                   bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
5148                   bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
5149                   bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5150                   bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5151                   bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5152                 }
5153               else
5154                 {
5155                   int target;
5156
5157                   /* IE->LE transition:
5158                      mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
5159                      bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
5160                      We change it into:
5161                      mov.l .Ln,rM; stc gbr,rN; nop; ...;
5162                      1: x@TPOFF; 2:.  */
5163
5164                   offset = rel->r_offset;
5165                   BFD_ASSERT (offset >= 16);
5166                   /* Size of IE instructions is 10 or 12.  */
5167                   offset -= 10;
5168                   insn = bfd_get_16 (input_bfd, contents + offset + 0);
5169                   if ((insn & 0xf0ff) == 0x0012)
5170                     {
5171                       BFD_ASSERT (offset >= 2);
5172                       offset -= 2;
5173                       insn = bfd_get_16 (input_bfd, contents + offset + 0);
5174                     }
5175
5176                   BFD_ASSERT ((insn & 0xff00) == 0xd000);
5177                   target = insn & 0x00ff;
5178                   insn = bfd_get_16 (input_bfd, contents + offset + 2);
5179                   BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
5180                   insn = bfd_get_16 (input_bfd, contents + offset + 4);
5181                   BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
5182                   insn = 0xd000 | (insn & 0x0f00) | target;
5183                   bfd_put_16 (output_bfd, insn, contents + offset + 0);
5184                   bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5185                 }
5186
5187               bfd_put_32 (output_bfd, tpoff (info, relocation),
5188                           contents + rel->r_offset);
5189               continue;
5190             }
5191
5192           if (sgot == NULL || sgotplt == NULL)
5193             abort ();
5194
5195           if (h != NULL)
5196             off = h->got.offset;
5197           else
5198             {
5199               if (local_got_offsets == NULL)
5200                 abort ();
5201
5202               off = local_got_offsets[r_symndx];
5203             }
5204
5205           /* Relocate R_SH_TLS_IE_32 directly when statically linking.  */
5206           if (r_type == R_SH_TLS_IE_32
5207               && ! htab->root.dynamic_sections_created)
5208             {
5209               off &= ~1;
5210               bfd_put_32 (output_bfd, tpoff (info, relocation),
5211                           sgot->contents + off);
5212               bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
5213                           contents + rel->r_offset);
5214               continue;
5215             }
5216
5217           if ((off & 1) != 0)
5218             off &= ~1;
5219           else
5220             {
5221               Elf_Internal_Rela outrel;
5222               bfd_byte *loc;
5223               int dr_type, indx;
5224
5225               if (srelgot == NULL)
5226                 {
5227                   srelgot = bfd_get_linker_section (dynobj, ".rela.got");
5228                   BFD_ASSERT (srelgot != NULL);
5229                 }
5230
5231               outrel.r_offset = (sgot->output_section->vma
5232                                  + sgot->output_offset + off);
5233
5234               if (h == NULL || h->dynindx == -1)
5235                 indx = 0;
5236               else
5237                 indx = h->dynindx;
5238
5239               dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
5240                          R_SH_TLS_TPOFF32);
5241               if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
5242                 outrel.r_addend = relocation - dtpoff_base (info);
5243               else
5244                 outrel.r_addend = 0;
5245               outrel.r_info = ELF32_R_INFO (indx, dr_type);
5246               loc = srelgot->contents;
5247               loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5248               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5249
5250               if (r_type == R_SH_TLS_GD_32)
5251                 {
5252                   if (indx == 0)
5253                     {
5254                       bfd_put_32 (output_bfd,
5255                                   relocation - dtpoff_base (info),
5256                                   sgot->contents + off + 4);
5257                     }
5258                   else
5259                     {
5260                       outrel.r_info = ELF32_R_INFO (indx,
5261                                                     R_SH_TLS_DTPOFF32);
5262                       outrel.r_offset += 4;
5263                       outrel.r_addend = 0;
5264                       srelgot->reloc_count++;
5265                       loc += sizeof (Elf32_External_Rela);
5266                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5267                     }
5268                 }
5269
5270               if (h != NULL)
5271                 h->got.offset |= 1;
5272               else
5273                 local_got_offsets[r_symndx] |= 1;
5274             }
5275
5276           if (off >= (bfd_vma) -2)
5277             abort ();
5278
5279           if (r_type == (int) ELF32_R_TYPE (rel->r_info))
5280             relocation = sh_elf_got_offset (htab) + off;
5281           else
5282             {
5283               bfd_vma offset;
5284               unsigned short insn;
5285
5286               /* GD->IE transition:
5287                    mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5288                    jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5289                    1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5290                  We change it into:
5291                    mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
5292                    nop; nop; bra 3f; nop; .align 2;
5293                    1: .long x@TPOFF; 2:...; 3:.  */
5294
5295               offset = rel->r_offset;
5296               BFD_ASSERT (offset >= 16);
5297               /* Size of GD instructions is 16 or 18.  */
5298               offset -= 16;
5299               insn = bfd_get_16 (input_bfd, contents + offset + 0);
5300               if ((insn & 0xff00) == 0xc700)
5301                 {
5302                   BFD_ASSERT (offset >= 2);
5303                   offset -= 2;
5304                   insn = bfd_get_16 (input_bfd, contents + offset + 0);
5305                 }
5306
5307               BFD_ASSERT ((insn & 0xff00) == 0xd400);
5308
5309               /* Replace mov.l 1f,R4 with mov.l 1f,r0.  */
5310               bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
5311
5312               insn = bfd_get_16 (input_bfd, contents + offset + 2);
5313               BFD_ASSERT ((insn & 0xff00) == 0xc700);
5314               insn = bfd_get_16 (input_bfd, contents + offset + 4);
5315               BFD_ASSERT ((insn & 0xff00) == 0xd100);
5316               insn = bfd_get_16 (input_bfd, contents + offset + 6);
5317               BFD_ASSERT (insn == 0x310c);
5318               insn = bfd_get_16 (input_bfd, contents + offset + 8);
5319               BFD_ASSERT (insn == 0x410b);
5320               insn = bfd_get_16 (input_bfd, contents + offset + 10);
5321               BFD_ASSERT (insn == 0x34cc);
5322
5323               bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
5324               bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
5325               bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
5326               bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5327               bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5328
5329               bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
5330                           contents + rel->r_offset);
5331
5332               continue;
5333           }
5334
5335           addend = rel->r_addend;
5336
5337           goto final_link_relocate;
5338
5339         case R_SH_TLS_LD_32:
5340           BFD_ASSERT (htab);
5341           check_segment[0] = check_segment[1] = -1;
5342           if (! bfd_link_pic (info))
5343             {
5344               bfd_vma offset;
5345               unsigned short insn;
5346
5347               /* LD->LE transition:
5348                    mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5349                    jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5350                    1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5351                  We change it into:
5352                    stc gbr,r0; nop; nop; nop;
5353                    nop; nop; bra 3f; ...; 3:.  */
5354
5355               offset = rel->r_offset;
5356               BFD_ASSERT (offset >= 16);
5357               /* Size of LD instructions is 16 or 18.  */
5358               offset -= 16;
5359               insn = bfd_get_16 (input_bfd, contents + offset + 0);
5360               if ((insn & 0xff00) == 0xc700)
5361                 {
5362                   BFD_ASSERT (offset >= 2);
5363                   offset -= 2;
5364                   insn = bfd_get_16 (input_bfd, contents + offset + 0);
5365                 }
5366
5367               BFD_ASSERT ((insn & 0xff00) == 0xd400);
5368               insn = bfd_get_16 (input_bfd, contents + offset + 2);
5369               BFD_ASSERT ((insn & 0xff00) == 0xc700);
5370               insn = bfd_get_16 (input_bfd, contents + offset + 4);
5371               BFD_ASSERT ((insn & 0xff00) == 0xd100);
5372               insn = bfd_get_16 (input_bfd, contents + offset + 6);
5373               BFD_ASSERT (insn == 0x310c);
5374               insn = bfd_get_16 (input_bfd, contents + offset + 8);
5375               BFD_ASSERT (insn == 0x410b);
5376               insn = bfd_get_16 (input_bfd, contents + offset + 10);
5377               BFD_ASSERT (insn == 0x34cc);
5378
5379               bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
5380               bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
5381               bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5382               bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5383               bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5384               bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5385
5386               continue;
5387             }
5388
5389           if (sgot == NULL || sgotplt == NULL)
5390             abort ();
5391
5392           off = htab->tls_ldm_got.offset;
5393           if (off & 1)
5394             off &= ~1;
5395           else
5396             {
5397               Elf_Internal_Rela outrel;
5398               bfd_byte *loc;
5399
5400               srelgot = htab->srelgot;
5401               if (srelgot == NULL)
5402                 abort ();
5403
5404               outrel.r_offset = (sgot->output_section->vma
5405                                  + sgot->output_offset + off);
5406               outrel.r_addend = 0;
5407               outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
5408               loc = srelgot->contents;
5409               loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5410               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5411               htab->tls_ldm_got.offset |= 1;
5412             }
5413
5414           relocation = sh_elf_got_offset (htab) + off;
5415           addend = rel->r_addend;
5416
5417           goto final_link_relocate;
5418
5419         case R_SH_TLS_LDO_32:
5420           check_segment[0] = check_segment[1] = -1;
5421           if (! bfd_link_pic (info))
5422             relocation = tpoff (info, relocation);
5423           else
5424             relocation -= dtpoff_base (info);
5425
5426           addend = rel->r_addend;
5427           goto final_link_relocate;
5428
5429         case R_SH_TLS_LE_32:
5430           {
5431             int indx;
5432             Elf_Internal_Rela outrel;
5433             bfd_byte *loc;
5434
5435             check_segment[0] = check_segment[1] = -1;
5436
5437             if (!bfd_link_dll (info))
5438               {
5439                 relocation = tpoff (info, relocation);
5440                 addend = rel->r_addend;
5441                 goto final_link_relocate;
5442               }
5443
5444             if (sreloc == NULL)
5445               {
5446                 sreloc = _bfd_elf_get_dynamic_reloc_section
5447                   (input_bfd, input_section, /*rela?*/ TRUE);
5448                 if (sreloc == NULL)
5449                   return FALSE;
5450               }
5451
5452             if (h == NULL || h->dynindx == -1)
5453               indx = 0;
5454             else
5455               indx = h->dynindx;
5456
5457             outrel.r_offset = (input_section->output_section->vma
5458                                + input_section->output_offset
5459                                + rel->r_offset);
5460             outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5461             if (indx == 0)
5462               outrel.r_addend = relocation - dtpoff_base (info);
5463             else
5464               outrel.r_addend = 0;
5465
5466             loc = sreloc->contents;
5467             loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5468             bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5469             continue;
5470           }
5471         }
5472
5473     relocation_done:
5474       if (fdpic_p && check_segment[0] != (unsigned) -1
5475           && check_segment[0] != check_segment[1])
5476         {
5477           /* We don't want duplicate errors for undefined symbols.  */
5478           if (!h || h->root.type != bfd_link_hash_undefined)
5479             {
5480               if (bfd_link_pic (info))
5481                 {
5482                   info->callbacks->einfo
5483                     (_("%X%C: relocation to \"%s\" references a different segment\n"),
5484                      input_bfd, input_section, rel->r_offset, symname);
5485                   return FALSE;
5486                 }
5487               else
5488                 info->callbacks->einfo
5489                   (_("%C: warning: relocation to \"%s\" references a different segment\n"),
5490                    input_bfd, input_section, rel->r_offset, symname);
5491             }
5492
5493           elf_elfheader (output_bfd)->e_flags |= EF_SH_PIC;
5494         }
5495
5496       if (r != bfd_reloc_ok)
5497         {
5498           switch (r)
5499             {
5500             default:
5501             case bfd_reloc_outofrange:
5502               abort ();
5503             case bfd_reloc_overflow:
5504               {
5505                 const char *name;
5506
5507                 if (h != NULL)
5508                   name = NULL;
5509                 else
5510                   {
5511                     name = (bfd_elf_string_from_elf_section
5512                             (input_bfd, symtab_hdr->sh_link, sym->st_name));
5513                     if (name == NULL)
5514                       return FALSE;
5515                     if (*name == '\0')
5516                       name = bfd_section_name (input_bfd, sec);
5517                   }
5518                 (*info->callbacks->reloc_overflow)
5519                   (info, (h ? &h->root : NULL), name, howto->name,
5520                    (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5521               }
5522               break;
5523             }
5524         }
5525     }
5526
5527   return TRUE;
5528 }
5529
5530 /* This is a version of bfd_generic_get_relocated_section_contents
5531    which uses sh_elf_relocate_section.  */
5532
5533 static bfd_byte *
5534 sh_elf_get_relocated_section_contents (bfd *output_bfd,
5535                                        struct bfd_link_info *link_info,
5536                                        struct bfd_link_order *link_order,
5537                                        bfd_byte *data,
5538                                        bfd_boolean relocatable,
5539                                        asymbol **symbols)
5540 {
5541   Elf_Internal_Shdr *symtab_hdr;
5542   asection *input_section = link_order->u.indirect.section;
5543   bfd *input_bfd = input_section->owner;
5544   asection **sections = NULL;
5545   Elf_Internal_Rela *internal_relocs = NULL;
5546   Elf_Internal_Sym *isymbuf = NULL;
5547
5548   /* We only need to handle the case of relaxing, or of having a
5549      particular set of section contents, specially.  */
5550   if (relocatable
5551       || elf_section_data (input_section)->this_hdr.contents == NULL)
5552     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5553                                                        link_order, data,
5554                                                        relocatable,
5555                                                        symbols);
5556
5557   symtab_hdr = &elf_symtab_hdr (input_bfd);
5558
5559   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
5560           (size_t) input_section->size);
5561
5562   if ((input_section->flags & SEC_RELOC) != 0
5563       && input_section->reloc_count > 0)
5564     {
5565       asection **secpp;
5566       Elf_Internal_Sym *isym, *isymend;
5567       bfd_size_type amt;
5568
5569       internal_relocs = (_bfd_elf_link_read_relocs
5570                          (input_bfd, input_section, NULL,
5571                           (Elf_Internal_Rela *) NULL, FALSE));
5572       if (internal_relocs == NULL)
5573         goto error_return;
5574
5575       if (symtab_hdr->sh_info != 0)
5576         {
5577           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5578           if (isymbuf == NULL)
5579             isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5580                                             symtab_hdr->sh_info, 0,
5581                                             NULL, NULL, NULL);
5582           if (isymbuf == NULL)
5583             goto error_return;
5584         }
5585
5586       amt = symtab_hdr->sh_info;
5587       amt *= sizeof (asection *);
5588       sections = (asection **) bfd_malloc (amt);
5589       if (sections == NULL && amt != 0)
5590         goto error_return;
5591
5592       isymend = isymbuf + symtab_hdr->sh_info;
5593       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5594         {
5595           asection *isec;
5596
5597           if (isym->st_shndx == SHN_UNDEF)
5598             isec = bfd_und_section_ptr;
5599           else if (isym->st_shndx == SHN_ABS)
5600             isec = bfd_abs_section_ptr;
5601           else if (isym->st_shndx == SHN_COMMON)
5602             isec = bfd_com_section_ptr;
5603           else
5604             isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5605
5606           *secpp = isec;
5607         }
5608
5609       if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5610                                      input_section, data, internal_relocs,
5611                                      isymbuf, sections))
5612         goto error_return;
5613
5614       if (sections != NULL)
5615         free (sections);
5616       if (isymbuf != NULL
5617           && symtab_hdr->contents != (unsigned char *) isymbuf)
5618         free (isymbuf);
5619       if (elf_section_data (input_section)->relocs != internal_relocs)
5620         free (internal_relocs);
5621     }
5622
5623   return data;
5624
5625  error_return:
5626   if (sections != NULL)
5627     free (sections);
5628   if (isymbuf != NULL
5629       && symtab_hdr->contents != (unsigned char *) isymbuf)
5630     free (isymbuf);
5631   if (internal_relocs != NULL
5632       && elf_section_data (input_section)->relocs != internal_relocs)
5633     free (internal_relocs);
5634   return NULL;
5635 }
5636
5637 /* Return the base VMA address which should be subtracted from real addresses
5638    when resolving @dtpoff relocation.
5639    This is PT_TLS segment p_vaddr.  */
5640
5641 static bfd_vma
5642 dtpoff_base (struct bfd_link_info *info)
5643 {
5644   /* If tls_sec is NULL, we should have signalled an error already.  */
5645   if (elf_hash_table (info)->tls_sec == NULL)
5646     return 0;
5647   return elf_hash_table (info)->tls_sec->vma;
5648 }
5649
5650 /* Return the relocation value for R_SH_TLS_TPOFF32..  */
5651
5652 static bfd_vma
5653 tpoff (struct bfd_link_info *info, bfd_vma address)
5654 {
5655   /* If tls_sec is NULL, we should have signalled an error already.  */
5656   if (elf_hash_table (info)->tls_sec == NULL)
5657     return 0;
5658   /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5659      structure which has 2 pointer fields.  */
5660   return (address - elf_hash_table (info)->tls_sec->vma
5661           + align_power ((bfd_vma) 8,
5662                          elf_hash_table (info)->tls_sec->alignment_power));
5663 }
5664
5665 static asection *
5666 sh_elf_gc_mark_hook (asection *sec,
5667                      struct bfd_link_info *info,
5668                      Elf_Internal_Rela *rel,
5669                      struct elf_link_hash_entry *h,
5670                      Elf_Internal_Sym *sym)
5671 {
5672   if (h != NULL)
5673     switch (ELF32_R_TYPE (rel->r_info))
5674       {
5675       case R_SH_GNU_VTINHERIT:
5676       case R_SH_GNU_VTENTRY:
5677         return NULL;
5678       }
5679
5680   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5681 }
5682
5683 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
5684
5685 static void
5686 sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
5687                              struct elf_link_hash_entry *dir,
5688                              struct elf_link_hash_entry *ind)
5689 {
5690   struct elf_sh_link_hash_entry *edir, *eind;
5691
5692   edir = (struct elf_sh_link_hash_entry *) dir;
5693   eind = (struct elf_sh_link_hash_entry *) ind;
5694
5695   if (eind->dyn_relocs != NULL)
5696     {
5697       if (edir->dyn_relocs != NULL)
5698         {
5699           struct elf_sh_dyn_relocs **pp;
5700           struct elf_sh_dyn_relocs *p;
5701
5702           /* Add reloc counts against the indirect sym to the direct sym
5703              list.  Merge any entries against the same section.  */
5704           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5705             {
5706               struct elf_sh_dyn_relocs *q;
5707
5708               for (q = edir->dyn_relocs; q != NULL; q = q->next)
5709                 if (q->sec == p->sec)
5710                   {
5711                     q->pc_count += p->pc_count;
5712                     q->count += p->count;
5713                     *pp = p->next;
5714                     break;
5715                   }
5716               if (q == NULL)
5717                 pp = &p->next;
5718             }
5719           *pp = edir->dyn_relocs;
5720         }
5721
5722       edir->dyn_relocs = eind->dyn_relocs;
5723       eind->dyn_relocs = NULL;
5724     }
5725   edir->gotplt_refcount = eind->gotplt_refcount;
5726   eind->gotplt_refcount = 0;
5727 #ifdef INCLUDE_SHMEDIA
5728   edir->datalabel_got.refcount += eind->datalabel_got.refcount;
5729   eind->datalabel_got.refcount = 0;
5730 #endif
5731   edir->funcdesc.refcount += eind->funcdesc.refcount;
5732   eind->funcdesc.refcount = 0;
5733   edir->abs_funcdesc_refcount += eind->abs_funcdesc_refcount;
5734   eind->abs_funcdesc_refcount = 0;
5735
5736   if (ind->root.type == bfd_link_hash_indirect
5737       && dir->got.refcount <= 0)
5738     {
5739       edir->got_type = eind->got_type;
5740       eind->got_type = GOT_UNKNOWN;
5741     }
5742
5743   if (ind->root.type != bfd_link_hash_indirect
5744       && dir->dynamic_adjusted)
5745     {
5746       /* If called to transfer flags for a weakdef during processing
5747          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5748          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
5749       dir->ref_dynamic |= ind->ref_dynamic;
5750       dir->ref_regular |= ind->ref_regular;
5751       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
5752       dir->needs_plt |= ind->needs_plt;
5753     }
5754   else
5755     _bfd_elf_link_hash_copy_indirect (info, dir, ind);
5756 }
5757
5758 static int
5759 sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
5760                             int is_local)
5761 {
5762   if (bfd_link_pic (info))
5763     return r_type;
5764
5765   switch (r_type)
5766     {
5767     case R_SH_TLS_GD_32:
5768     case R_SH_TLS_IE_32:
5769       if (is_local)
5770         return R_SH_TLS_LE_32;
5771       return R_SH_TLS_IE_32;
5772     case R_SH_TLS_LD_32:
5773       return R_SH_TLS_LE_32;
5774     }
5775
5776   return r_type;
5777 }
5778
5779 /* Look through the relocs for a section during the first phase.
5780    Since we don't do .gots or .plts, we just need to consider the
5781    virtual table relocs for gc.  */
5782
5783 static bfd_boolean
5784 sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
5785                      const Elf_Internal_Rela *relocs)
5786 {
5787   Elf_Internal_Shdr *symtab_hdr;
5788   struct elf_link_hash_entry **sym_hashes;
5789   struct elf_sh_link_hash_table *htab;
5790   const Elf_Internal_Rela *rel;
5791   const Elf_Internal_Rela *rel_end;
5792   asection *sreloc;
5793   unsigned int r_type;
5794   enum got_type got_type, old_got_type;
5795
5796   sreloc = NULL;
5797
5798   if (bfd_link_relocatable (info))
5799     return TRUE;
5800
5801   BFD_ASSERT (is_sh_elf (abfd));
5802
5803   symtab_hdr = &elf_symtab_hdr (abfd);
5804   sym_hashes = elf_sym_hashes (abfd);
5805
5806   htab = sh_elf_hash_table (info);
5807   if (htab == NULL)
5808     return FALSE;
5809
5810   rel_end = relocs + sec->reloc_count;
5811   for (rel = relocs; rel < rel_end; rel++)
5812     {
5813       struct elf_link_hash_entry *h;
5814       unsigned long r_symndx;
5815 #ifdef INCLUDE_SHMEDIA
5816       int seen_stt_datalabel = 0;
5817 #endif
5818
5819       r_symndx = ELF32_R_SYM (rel->r_info);
5820       r_type = ELF32_R_TYPE (rel->r_info);
5821
5822       if (r_symndx < symtab_hdr->sh_info)
5823         h = NULL;
5824       else
5825         {
5826           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5827           while (h->root.type == bfd_link_hash_indirect
5828                  || h->root.type == bfd_link_hash_warning)
5829             {
5830 #ifdef INCLUDE_SHMEDIA
5831               seen_stt_datalabel |= h->type == STT_DATALABEL;
5832 #endif
5833               h = (struct elf_link_hash_entry *) h->root.u.i.link;
5834             }
5835
5836           /* PR15323, ref flags aren't set for references in the same
5837              object.  */
5838           h->root.non_ir_ref = 1;
5839         }
5840
5841       r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5842       if (! bfd_link_pic (info)
5843           && r_type == R_SH_TLS_IE_32
5844           && h != NULL
5845           && h->root.type != bfd_link_hash_undefined
5846           && h->root.type != bfd_link_hash_undefweak
5847           && (h->dynindx == -1
5848               || h->def_regular))
5849         r_type = R_SH_TLS_LE_32;
5850
5851       if (htab->fdpic_p)
5852         switch (r_type)
5853           {
5854           case R_SH_GOTOFFFUNCDESC:
5855           case R_SH_GOTOFFFUNCDESC20:
5856           case R_SH_FUNCDESC:
5857           case R_SH_GOTFUNCDESC:
5858           case R_SH_GOTFUNCDESC20:
5859             if (h != NULL)
5860               {
5861                 if (h->dynindx == -1)
5862                   switch (ELF_ST_VISIBILITY (h->other))
5863                     {
5864                     case STV_INTERNAL:
5865                     case STV_HIDDEN:
5866                       break;
5867                     default:
5868                       bfd_elf_link_record_dynamic_symbol (info, h);
5869                       break;
5870                     }
5871               }
5872             break;
5873           }
5874
5875       /* Some relocs require a global offset table.  */
5876       if (htab->sgot == NULL)
5877         {
5878           switch (r_type)
5879             {
5880             case R_SH_DIR32:
5881               /* This may require an rofixup.  */
5882               if (!htab->fdpic_p)
5883                 break;
5884             case R_SH_GOTPLT32:
5885             case R_SH_GOT32:
5886             case R_SH_GOT20:
5887             case R_SH_GOTOFF:
5888             case R_SH_GOTOFF20:
5889             case R_SH_FUNCDESC:
5890             case R_SH_GOTFUNCDESC:
5891             case R_SH_GOTFUNCDESC20:
5892             case R_SH_GOTOFFFUNCDESC:
5893             case R_SH_GOTOFFFUNCDESC20:
5894             case R_SH_GOTPC:
5895 #ifdef INCLUDE_SHMEDIA
5896             case R_SH_GOTPLT_LOW16:
5897             case R_SH_GOTPLT_MEDLOW16:
5898             case R_SH_GOTPLT_MEDHI16:
5899             case R_SH_GOTPLT_HI16:
5900             case R_SH_GOTPLT10BY4:
5901             case R_SH_GOTPLT10BY8:
5902             case R_SH_GOT_LOW16:
5903             case R_SH_GOT_MEDLOW16:
5904             case R_SH_GOT_MEDHI16:
5905             case R_SH_GOT_HI16:
5906             case R_SH_GOT10BY4:
5907             case R_SH_GOT10BY8:
5908             case R_SH_GOTOFF_LOW16:
5909             case R_SH_GOTOFF_MEDLOW16:
5910             case R_SH_GOTOFF_MEDHI16:
5911             case R_SH_GOTOFF_HI16:
5912             case R_SH_GOTPC_LOW16:
5913             case R_SH_GOTPC_MEDLOW16:
5914             case R_SH_GOTPC_MEDHI16:
5915             case R_SH_GOTPC_HI16:
5916 #endif
5917             case R_SH_TLS_GD_32:
5918             case R_SH_TLS_LD_32:
5919             case R_SH_TLS_IE_32:
5920               if (htab->root.dynobj == NULL)
5921                 htab->root.dynobj = abfd;
5922               if (!create_got_section (htab->root.dynobj, info))
5923                 return FALSE;
5924               break;
5925
5926             default:
5927               break;
5928             }
5929         }
5930
5931       switch (r_type)
5932         {
5933           /* This relocation describes the C++ object vtable hierarchy.
5934              Reconstruct it for later use during GC.  */
5935         case R_SH_GNU_VTINHERIT:
5936           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5937             return FALSE;
5938           break;
5939
5940           /* This relocation describes which C++ vtable entries are actually
5941              used.  Record for later use during GC.  */
5942         case R_SH_GNU_VTENTRY:
5943           BFD_ASSERT (h != NULL);
5944           if (h != NULL
5945               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5946             return FALSE;
5947           break;
5948
5949         case R_SH_TLS_IE_32:
5950           if (bfd_link_pic (info))
5951             info->flags |= DF_STATIC_TLS;
5952
5953           /* FALLTHROUGH */
5954         force_got:
5955         case R_SH_TLS_GD_32:
5956         case R_SH_GOT32:
5957         case R_SH_GOT20:
5958 #ifdef INCLUDE_SHMEDIA
5959         case R_SH_GOT_LOW16:
5960         case R_SH_GOT_MEDLOW16:
5961         case R_SH_GOT_MEDHI16:
5962         case R_SH_GOT_HI16:
5963         case R_SH_GOT10BY4:
5964         case R_SH_GOT10BY8:
5965 #endif
5966         case R_SH_GOTFUNCDESC:
5967         case R_SH_GOTFUNCDESC20:
5968           switch (r_type)
5969             {
5970             default:
5971               got_type = GOT_NORMAL;
5972               break;
5973             case R_SH_TLS_GD_32:
5974               got_type = GOT_TLS_GD;
5975               break;
5976             case R_SH_TLS_IE_32:
5977               got_type = GOT_TLS_IE;
5978               break;
5979             case R_SH_GOTFUNCDESC:
5980             case R_SH_GOTFUNCDESC20:
5981               got_type = GOT_FUNCDESC;
5982               break;
5983             }
5984
5985           if (h != NULL)
5986             {
5987 #ifdef INCLUDE_SHMEDIA
5988               if (seen_stt_datalabel)
5989                 {
5990                   struct elf_sh_link_hash_entry *eh
5991                     = (struct elf_sh_link_hash_entry *) h;
5992
5993                   eh->datalabel_got.refcount += 1;
5994                 }
5995               else
5996 #endif
5997                 h->got.refcount += 1;
5998               old_got_type = sh_elf_hash_entry (h)->got_type;
5999             }
6000           else
6001             {
6002               bfd_signed_vma *local_got_refcounts;
6003
6004               /* This is a global offset table entry for a local
6005                  symbol.  */
6006               local_got_refcounts = elf_local_got_refcounts (abfd);
6007               if (local_got_refcounts == NULL)
6008                 {
6009                   bfd_size_type size;
6010
6011                   size = symtab_hdr->sh_info;
6012                   size *= sizeof (bfd_signed_vma);
6013 #ifdef INCLUDE_SHMEDIA
6014                   /* Reserve space for both the datalabel and
6015                      codelabel local GOT offsets.  */
6016                   size *= 2;
6017 #endif
6018                   size += symtab_hdr->sh_info;
6019                   local_got_refcounts = ((bfd_signed_vma *)
6020                                          bfd_zalloc (abfd, size));
6021                   if (local_got_refcounts == NULL)
6022                     return FALSE;
6023                   elf_local_got_refcounts (abfd) = local_got_refcounts;
6024 #ifdef  INCLUDE_SHMEDIA
6025                   /* Take care of both the datalabel and codelabel local
6026                      GOT offsets.  */
6027                   sh_elf_local_got_type (abfd)
6028                     = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
6029 #else
6030                   sh_elf_local_got_type (abfd)
6031                     = (char *) (local_got_refcounts + symtab_hdr->sh_info);
6032 #endif
6033                 }
6034 #ifdef INCLUDE_SHMEDIA
6035               if (rel->r_addend & 1)
6036                 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
6037               else
6038 #endif
6039                 local_got_refcounts[r_symndx] += 1;
6040               old_got_type = sh_elf_local_got_type (abfd) [r_symndx];
6041             }
6042
6043           /* If a TLS symbol is accessed using IE at least once,
6044              there is no point to use dynamic model for it.  */
6045           if (old_got_type != got_type && old_got_type != GOT_UNKNOWN
6046               && (old_got_type != GOT_TLS_GD || got_type != GOT_TLS_IE))
6047             {
6048               if (old_got_type == GOT_TLS_IE && got_type == GOT_TLS_GD)
6049                 got_type = GOT_TLS_IE;
6050               else
6051                 {
6052                   if ((old_got_type == GOT_FUNCDESC || got_type == GOT_FUNCDESC)
6053                       && (old_got_type == GOT_NORMAL || got_type == GOT_NORMAL))
6054                     _bfd_error_handler
6055                       (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6056                        abfd, h->root.root.string);
6057                   else if (old_got_type == GOT_FUNCDESC
6058                            || got_type == GOT_FUNCDESC)
6059                     _bfd_error_handler
6060                       (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6061                        abfd, h->root.root.string);
6062                   else
6063                     _bfd_error_handler
6064                       (_("%B: `%s' accessed both as normal and thread local symbol"),
6065                        abfd, h->root.root.string);
6066                   return FALSE;
6067                 }
6068             }
6069
6070           if (old_got_type != got_type)
6071             {
6072               if (h != NULL)
6073                 sh_elf_hash_entry (h)->got_type = got_type;
6074               else
6075                 sh_elf_local_got_type (abfd) [r_symndx] = got_type;
6076             }
6077
6078           break;
6079
6080         case R_SH_TLS_LD_32:
6081           sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
6082           break;
6083
6084         case R_SH_FUNCDESC:
6085         case R_SH_GOTOFFFUNCDESC:
6086         case R_SH_GOTOFFFUNCDESC20:
6087           if (rel->r_addend)
6088             {
6089               _bfd_error_handler
6090                 (_("%B: Function descriptor relocation with non-zero addend"),
6091                  abfd);
6092               return FALSE;
6093             }
6094
6095           if (h == NULL)
6096             {
6097               union gotref *local_funcdesc;
6098
6099               /* We need a function descriptor for a local symbol.  */
6100               local_funcdesc = sh_elf_local_funcdesc (abfd);
6101               if (local_funcdesc == NULL)
6102                 {
6103                   bfd_size_type size;
6104
6105                   size = symtab_hdr->sh_info * sizeof (union gotref);
6106 #ifdef INCLUDE_SHMEDIA
6107                   /* Count datalabel local GOT.  */
6108                   size *= 2;
6109 #endif
6110                   local_funcdesc = (union gotref *) bfd_zalloc (abfd, size);
6111                   if (local_funcdesc == NULL)
6112                     return FALSE;
6113                   sh_elf_local_funcdesc (abfd) = local_funcdesc;
6114                 }
6115               local_funcdesc[r_symndx].refcount += 1;
6116
6117               if (r_type == R_SH_FUNCDESC)
6118                 {
6119                   if (!bfd_link_pic (info))
6120                     htab->srofixup->size += 4;
6121                   else
6122                     htab->srelgot->size += sizeof (Elf32_External_Rela);
6123                 }
6124             }
6125           else
6126             {
6127               sh_elf_hash_entry (h)->funcdesc.refcount++;
6128               if (r_type == R_SH_FUNCDESC)
6129                 sh_elf_hash_entry (h)->abs_funcdesc_refcount++;
6130
6131               /* If there is a function descriptor reference, then
6132                  there should not be any non-FDPIC references.  */
6133               old_got_type = sh_elf_hash_entry (h)->got_type;
6134               if (old_got_type != GOT_FUNCDESC && old_got_type != GOT_UNKNOWN)
6135                 {
6136                   if (old_got_type == GOT_NORMAL)
6137                     _bfd_error_handler
6138                       (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6139                        abfd, h->root.root.string);
6140                   else
6141                     _bfd_error_handler
6142                       (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6143                        abfd, h->root.root.string);
6144                 }
6145             }
6146           break;
6147
6148         case R_SH_GOTPLT32:
6149 #ifdef INCLUDE_SHMEDIA
6150         case R_SH_GOTPLT_LOW16:
6151         case R_SH_GOTPLT_MEDLOW16:
6152         case R_SH_GOTPLT_MEDHI16:
6153         case R_SH_GOTPLT_HI16:
6154         case R_SH_GOTPLT10BY4:
6155         case R_SH_GOTPLT10BY8:
6156 #endif
6157           /* If this is a local symbol, we resolve it directly without
6158              creating a procedure linkage table entry.  */
6159
6160           if (h == NULL
6161               || h->forced_local
6162               || ! bfd_link_pic (info)
6163               || info->symbolic
6164               || h->dynindx == -1)
6165             goto force_got;
6166
6167           h->needs_plt = 1;
6168           h->plt.refcount += 1;
6169           ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
6170
6171           break;
6172
6173         case R_SH_PLT32:
6174 #ifdef INCLUDE_SHMEDIA
6175         case R_SH_PLT_LOW16:
6176         case R_SH_PLT_MEDLOW16:
6177         case R_SH_PLT_MEDHI16:
6178         case R_SH_PLT_HI16:
6179 #endif
6180           /* This symbol requires a procedure linkage table entry.  We
6181              actually build the entry in adjust_dynamic_symbol,
6182              because this might be a case of linking PIC code which is
6183              never referenced by a dynamic object, in which case we
6184              don't need to generate a procedure linkage table entry
6185              after all.  */
6186
6187           /* If this is a local symbol, we resolve it directly without
6188              creating a procedure linkage table entry.  */
6189           if (h == NULL)
6190             continue;
6191
6192           if (h->forced_local)
6193             break;
6194
6195           h->needs_plt = 1;
6196           h->plt.refcount += 1;
6197           break;
6198
6199         case R_SH_DIR32:
6200         case R_SH_REL32:
6201 #ifdef INCLUDE_SHMEDIA
6202         case R_SH_IMM_LOW16_PCREL:
6203         case R_SH_IMM_MEDLOW16_PCREL:
6204         case R_SH_IMM_MEDHI16_PCREL:
6205         case R_SH_IMM_HI16_PCREL:
6206 #endif
6207           if (h != NULL && ! bfd_link_pic (info))
6208             {
6209               h->non_got_ref = 1;
6210               h->plt.refcount += 1;
6211             }
6212
6213           /* If we are creating a shared library, and this is a reloc
6214              against a global symbol, or a non PC relative reloc
6215              against a local symbol, then we need to copy the reloc
6216              into the shared library.  However, if we are linking with
6217              -Bsymbolic, we do not need to copy a reloc against a
6218              global symbol which is defined in an object we are
6219              including in the link (i.e., DEF_REGULAR is set).  At
6220              this point we have not seen all the input files, so it is
6221              possible that DEF_REGULAR is not set now but will be set
6222              later (it is never cleared).  We account for that
6223              possibility below by storing information in the
6224              dyn_relocs field of the hash table entry. A similar
6225              situation occurs when creating shared libraries and symbol
6226              visibility changes render the symbol local.
6227
6228              If on the other hand, we are creating an executable, we
6229              may need to keep relocations for symbols satisfied by a
6230              dynamic library if we manage to avoid copy relocs for the
6231              symbol.  */
6232           if ((bfd_link_pic (info)
6233                && (sec->flags & SEC_ALLOC) != 0
6234                && (r_type != R_SH_REL32
6235                    || (h != NULL
6236                        && (! info->symbolic
6237                            || h->root.type == bfd_link_hash_defweak
6238                            || !h->def_regular))))
6239               || (! bfd_link_pic (info)
6240                   && (sec->flags & SEC_ALLOC) != 0
6241                   && h != NULL
6242                   && (h->root.type == bfd_link_hash_defweak
6243                       || !h->def_regular)))
6244             {
6245               struct elf_sh_dyn_relocs *p;
6246               struct elf_sh_dyn_relocs **head;
6247
6248               if (htab->root.dynobj == NULL)
6249                 htab->root.dynobj = abfd;
6250
6251               /* When creating a shared object, we must copy these
6252                  reloc types into the output file.  We create a reloc
6253                  section in dynobj and make room for this reloc.  */
6254               if (sreloc == NULL)
6255                 {
6256                   sreloc = _bfd_elf_make_dynamic_reloc_section
6257                     (sec, htab->root.dynobj, 2, abfd, /*rela?*/ TRUE);
6258
6259                   if (sreloc == NULL)
6260                     return FALSE;
6261                 }
6262
6263               /* If this is a global symbol, we count the number of
6264                  relocations we need for this symbol.  */
6265               if (h != NULL)
6266                 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
6267               else
6268                 {
6269                   /* Track dynamic relocs needed for local syms too.  */
6270                   asection *s;
6271                   void *vpp;
6272                   Elf_Internal_Sym *isym;
6273
6274                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6275                                                 abfd, r_symndx);
6276                   if (isym == NULL)
6277                     return FALSE;
6278
6279                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6280                   if (s == NULL)
6281                     s = sec;
6282
6283                   vpp = &elf_section_data (s)->local_dynrel;
6284                   head = (struct elf_sh_dyn_relocs **) vpp;
6285                 }
6286
6287               p = *head;
6288               if (p == NULL || p->sec != sec)
6289                 {
6290                   bfd_size_type amt = sizeof (*p);
6291                   p = bfd_alloc (htab->root.dynobj, amt);
6292                   if (p == NULL)
6293                     return FALSE;
6294                   p->next = *head;
6295                   *head = p;
6296                   p->sec = sec;
6297                   p->count = 0;
6298                   p->pc_count = 0;
6299                 }
6300
6301               p->count += 1;
6302               if (r_type == R_SH_REL32
6303 #ifdef INCLUDE_SHMEDIA
6304                   || r_type == R_SH_IMM_LOW16_PCREL
6305                   || r_type == R_SH_IMM_MEDLOW16_PCREL
6306                   || r_type == R_SH_IMM_MEDHI16_PCREL
6307                   || r_type == R_SH_IMM_HI16_PCREL
6308 #endif
6309                   )
6310                 p->pc_count += 1;
6311             }
6312
6313           /* Allocate the fixup regardless of whether we need a relocation.
6314              If we end up generating the relocation, we'll unallocate the
6315              fixup.  */
6316           if (htab->fdpic_p && !bfd_link_pic (info)
6317               && r_type == R_SH_DIR32
6318               && (sec->flags & SEC_ALLOC) != 0)
6319             htab->srofixup->size += 4;
6320           break;
6321
6322         case R_SH_TLS_LE_32:
6323           if (bfd_link_dll (info))
6324             {
6325               _bfd_error_handler
6326                 (_("%B: TLS local exec code cannot be linked into shared objects"),
6327                  abfd);
6328               return FALSE;
6329             }
6330
6331           break;
6332
6333         case R_SH_TLS_LDO_32:
6334           /* Nothing to do.  */
6335           break;
6336
6337         default:
6338           break;
6339         }
6340     }
6341
6342   return TRUE;
6343 }
6344
6345 #ifndef sh_elf_set_mach_from_flags
6346 static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
6347
6348 static bfd_boolean
6349 sh_elf_set_mach_from_flags (bfd *abfd)
6350 {
6351   flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
6352
6353   if (flags >= sizeof(sh_ef_bfd_table))
6354     return FALSE;
6355
6356   if (sh_ef_bfd_table[flags] == 0)
6357     return FALSE;
6358
6359   bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
6360
6361   return TRUE;
6362 }
6363
6364
6365 /* Reverse table lookup for sh_ef_bfd_table[].
6366    Given a bfd MACH value from archures.c
6367    return the equivalent ELF flags from the table.
6368    Return -1 if no match is found.  */
6369
6370 int
6371 sh_elf_get_flags_from_mach (unsigned long mach)
6372 {
6373   int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
6374
6375   for (; i>0; i--)
6376     if (sh_ef_bfd_table[i] == mach)
6377       return i;
6378
6379   /* shouldn't get here */
6380   BFD_FAIL();
6381
6382   return -1;
6383 }
6384 #endif /* not sh_elf_set_mach_from_flags */
6385
6386 #ifndef sh_elf_copy_private_data
6387 /* Copy backend specific data from one object module to another */
6388
6389 static bfd_boolean
6390 sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
6391 {
6392   if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6393     return TRUE;
6394
6395   if (! _bfd_elf_copy_private_bfd_data (ibfd, obfd))
6396     return FALSE;
6397
6398   return sh_elf_set_mach_from_flags (obfd);
6399 }
6400 #endif /* not sh_elf_copy_private_data */
6401
6402 #ifndef sh_elf_merge_private_data
6403
6404 /* This function returns the ELF architecture number that
6405    corresponds to the given arch_sh* flags.  */
6406
6407 int
6408 sh_find_elf_flags (unsigned int arch_set)
6409 {
6410   extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
6411   unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
6412
6413   return sh_elf_get_flags_from_mach (bfd_mach);
6414 }
6415
6416 /* This routine initialises the elf flags when required and
6417    calls sh_merge_bfd_arch() to check dsp/fpu compatibility.  */
6418
6419 static bfd_boolean
6420 sh_elf_merge_private_data (bfd *ibfd, bfd *obfd)
6421 {
6422   extern bfd_boolean sh_merge_bfd_arch (bfd *, bfd *);
6423
6424   if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6425     return TRUE;
6426
6427   if (! elf_flags_init (obfd))
6428     {
6429       /* This happens when ld starts out with a 'blank' output file.  */
6430       elf_flags_init (obfd) = TRUE;
6431       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6432       sh_elf_set_mach_from_flags (obfd);
6433       if (elf_elfheader (obfd)->e_flags & EF_SH_FDPIC)
6434         elf_elfheader (obfd)->e_flags &= ~EF_SH_PIC;
6435     }
6436
6437   if (! sh_merge_bfd_arch (ibfd, obfd))
6438     {
6439       _bfd_error_handler ("%B: uses instructions which are incompatible "
6440                           "with instructions used in previous modules",
6441                           ibfd);
6442       bfd_set_error (bfd_error_bad_value);
6443       return FALSE;
6444     }
6445
6446   elf_elfheader (obfd)->e_flags &= ~EF_SH_MACH_MASK;
6447   elf_elfheader (obfd)->e_flags |=
6448     sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
6449
6450   if (fdpic_object_p (ibfd) != fdpic_object_p (obfd))
6451     {
6452       _bfd_error_handler ("%B: attempt to mix FDPIC and non-FDPIC objects",
6453                           ibfd);
6454       bfd_set_error (bfd_error_bad_value);
6455       return FALSE;
6456     }
6457
6458   return TRUE;
6459 }
6460 #endif /* not sh_elf_merge_private_data */
6461
6462 /* Override the generic function because we need to store sh_elf_obj_tdata
6463    as the specific tdata.  We set also the machine architecture from flags
6464    here.  */
6465
6466 static bfd_boolean
6467 sh_elf_object_p (bfd *abfd)
6468 {
6469   if (! sh_elf_set_mach_from_flags (abfd))
6470     return FALSE;
6471
6472   return (((elf_elfheader (abfd)->e_flags & EF_SH_FDPIC) != 0)
6473           == fdpic_object_p (abfd));
6474 }
6475
6476 /* Finish up dynamic symbol handling.  We set the contents of various
6477    dynamic sections here.  */
6478
6479 static bfd_boolean
6480 sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6481                               struct elf_link_hash_entry *h,
6482                               Elf_Internal_Sym *sym)
6483 {
6484   struct elf_sh_link_hash_table *htab;
6485
6486   htab = sh_elf_hash_table (info);
6487   if (htab == NULL)
6488     return FALSE;
6489
6490   if (h->plt.offset != (bfd_vma) -1)
6491     {
6492       asection *splt;
6493       asection *sgotplt;
6494       asection *srelplt;
6495
6496       bfd_vma plt_index;
6497       bfd_vma got_offset;
6498       Elf_Internal_Rela rel;
6499       bfd_byte *loc;
6500       const struct elf_sh_plt_info *plt_info;
6501
6502       /* This symbol has an entry in the procedure linkage table.  Set
6503          it up.  */
6504
6505       BFD_ASSERT (h->dynindx != -1);
6506
6507       splt = htab->splt;
6508       sgotplt = htab->sgotplt;
6509       srelplt = htab->srelplt;
6510       BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL);
6511
6512       /* Get the index in the procedure linkage table which
6513          corresponds to this symbol.  This is the index of this symbol
6514          in all the symbols for which we are making plt entries.  The
6515          first entry in the procedure linkage table is reserved.  */
6516       plt_index = get_plt_index (htab->plt_info, h->plt.offset);
6517
6518       plt_info = htab->plt_info;
6519       if (plt_info->short_plt != NULL && plt_index <= MAX_SHORT_PLT)
6520         plt_info = plt_info->short_plt;
6521
6522       /* Get the offset into the .got table of the entry that
6523          corresponds to this function.  */
6524       if (htab->fdpic_p)
6525         /* The offset must be relative to the GOT symbol, twelve bytes
6526            before the end of .got.plt.  Each descriptor is eight
6527            bytes.  */
6528         got_offset = plt_index * 8 + 12 - sgotplt->size;
6529       else
6530         /* Each .got entry is 4 bytes.  The first three are
6531            reserved.  */
6532         got_offset = (plt_index + 3) * 4;
6533
6534 #ifdef GOT_BIAS
6535       if (bfd_link_pic (info))
6536         got_offset -= GOT_BIAS;
6537 #endif
6538
6539       /* Fill in the entry in the procedure linkage table.  */
6540       memcpy (splt->contents + h->plt.offset,
6541               plt_info->symbol_entry,
6542               plt_info->symbol_entry_size);
6543
6544       if (bfd_link_pic (info) || htab->fdpic_p)
6545         {
6546           if (plt_info->symbol_fields.got20)
6547             {
6548               bfd_reloc_status_type r;
6549               r = install_movi20_field (output_bfd, got_offset,
6550                                         splt->owner, splt, splt->contents,
6551                                         h->plt.offset
6552                                         + plt_info->symbol_fields.got_entry);
6553               BFD_ASSERT (r == bfd_reloc_ok);
6554             }
6555           else
6556             install_plt_field (output_bfd, FALSE, got_offset,
6557                                (splt->contents
6558                                 + h->plt.offset
6559                                 + plt_info->symbol_fields.got_entry));
6560         }
6561       else
6562         {
6563           BFD_ASSERT (!plt_info->symbol_fields.got20);
6564
6565           install_plt_field (output_bfd, FALSE,
6566                              (sgotplt->output_section->vma
6567                               + sgotplt->output_offset
6568                               + got_offset),
6569                              (splt->contents
6570                               + h->plt.offset
6571                               + plt_info->symbol_fields.got_entry));
6572           if (htab->vxworks_p)
6573             {
6574               unsigned int reachable_plts, plts_per_4k;
6575               int distance;
6576
6577               /* Divide the PLT into groups.  The first group contains
6578                  REACHABLE_PLTS entries and the other groups contain
6579                  PLTS_PER_4K entries.  Entries in the first group can
6580                  branch directly to .plt; those in later groups branch
6581                  to the last element of the previous group.  */
6582               /* ??? It would be better to create multiple copies of
6583                  the common resolver stub.  */
6584               reachable_plts = ((4096
6585                                  - plt_info->plt0_entry_size
6586                                  - (plt_info->symbol_fields.plt + 4))
6587                                 / plt_info->symbol_entry_size) + 1;
6588               plts_per_4k = (4096 / plt_info->symbol_entry_size);
6589               if (plt_index < reachable_plts)
6590                 distance = -(h->plt.offset
6591                              + plt_info->symbol_fields.plt);
6592               else
6593                 distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
6594                              * plt_info->symbol_entry_size);
6595
6596               /* Install the 'bra' with this offset.  */
6597               bfd_put_16 (output_bfd,
6598                           0xa000 | (0x0fff & ((distance - 4) / 2)),
6599                           (splt->contents
6600                            + h->plt.offset
6601                            + plt_info->symbol_fields.plt));
6602             }
6603           else
6604             install_plt_field (output_bfd, TRUE,
6605                                splt->output_section->vma + splt->output_offset,
6606                                (splt->contents
6607                                 + h->plt.offset
6608                                 + plt_info->symbol_fields.plt));
6609         }
6610
6611       /* Make got_offset relative to the start of .got.plt.  */
6612 #ifdef GOT_BIAS
6613       if (bfd_link_pic (info))
6614         got_offset += GOT_BIAS;
6615 #endif
6616       if (htab->fdpic_p)
6617         got_offset = plt_index * 8;
6618
6619       if (plt_info->symbol_fields.reloc_offset != MINUS_ONE)
6620         install_plt_field (output_bfd, FALSE,
6621                            plt_index * sizeof (Elf32_External_Rela),
6622                            (splt->contents
6623                             + h->plt.offset
6624                             + plt_info->symbol_fields.reloc_offset));
6625
6626       /* Fill in the entry in the global offset table.  */
6627       bfd_put_32 (output_bfd,
6628                   (splt->output_section->vma
6629                    + splt->output_offset
6630                    + h->plt.offset
6631                    + plt_info->symbol_resolve_offset),
6632                   sgotplt->contents + got_offset);
6633       if (htab->fdpic_p)
6634         bfd_put_32 (output_bfd,
6635                     sh_elf_osec_to_segment (output_bfd,
6636                                             htab->splt->output_section),
6637                     sgotplt->contents + got_offset + 4);
6638
6639       /* Fill in the entry in the .rela.plt section.  */
6640       rel.r_offset = (sgotplt->output_section->vma
6641                       + sgotplt->output_offset
6642                       + got_offset);
6643       if (htab->fdpic_p)
6644         rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_FUNCDESC_VALUE);
6645       else
6646         rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
6647       rel.r_addend = 0;
6648 #ifdef GOT_BIAS
6649       rel.r_addend = GOT_BIAS;
6650 #endif
6651       loc = srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
6652       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6653
6654       if (htab->vxworks_p && !bfd_link_pic (info))
6655         {
6656           /* Create the .rela.plt.unloaded relocations for this PLT entry.
6657              Begin by pointing LOC to the first such relocation.  */
6658           loc = (htab->srelplt2->contents
6659                  + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
6660
6661           /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6662              for the PLT entry's pointer to the .got.plt entry.  */
6663           rel.r_offset = (htab->splt->output_section->vma
6664                           + htab->splt->output_offset
6665                           + h->plt.offset
6666                           + plt_info->symbol_fields.got_entry);
6667           rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6668           rel.r_addend = got_offset;
6669           bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6670           loc += sizeof (Elf32_External_Rela);
6671
6672           /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6673              the .got.plt entry, which initially points to .plt.  */
6674           rel.r_offset = (sgotplt->output_section->vma
6675                           + sgotplt->output_offset
6676                           + got_offset);
6677           rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
6678           rel.r_addend = 0;
6679           bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6680         }
6681
6682       if (!h->def_regular)
6683         {
6684           /* Mark the symbol as undefined, rather than as defined in
6685              the .plt section.  Leave the value alone.  */
6686           sym->st_shndx = SHN_UNDEF;
6687         }
6688     }
6689
6690   if (h->got.offset != (bfd_vma) -1
6691       && sh_elf_hash_entry (h)->got_type != GOT_TLS_GD
6692       && sh_elf_hash_entry (h)->got_type != GOT_TLS_IE
6693       && sh_elf_hash_entry (h)->got_type != GOT_FUNCDESC)
6694     {
6695       asection *sgot;
6696       asection *srelgot;
6697       Elf_Internal_Rela rel;
6698       bfd_byte *loc;
6699
6700       /* This symbol has an entry in the global offset table.  Set it
6701          up.  */
6702
6703       sgot = htab->sgot;
6704       srelgot = htab->srelgot;
6705       BFD_ASSERT (sgot != NULL && srelgot != NULL);
6706
6707       rel.r_offset = (sgot->output_section->vma
6708                       + sgot->output_offset
6709                       + (h->got.offset &~ (bfd_vma) 1));
6710
6711       /* If this is a static link, or it is a -Bsymbolic link and the
6712          symbol is defined locally or was forced to be local because
6713          of a version file, we just want to emit a RELATIVE reloc.
6714          The entry in the global offset table will already have been
6715          initialized in the relocate_section function.  */
6716       if (bfd_link_pic (info)
6717           && SYMBOL_REFERENCES_LOCAL (info, h))
6718         {
6719           if (htab->fdpic_p)
6720             {
6721               asection *sec = h->root.u.def.section;
6722               int dynindx
6723                 = elf_section_data (sec->output_section)->dynindx;
6724
6725               rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
6726               rel.r_addend = (h->root.u.def.value
6727                               + h->root.u.def.section->output_offset);
6728             }
6729           else
6730             {
6731               rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6732               rel.r_addend = (h->root.u.def.value
6733                               + h->root.u.def.section->output_section->vma
6734                               + h->root.u.def.section->output_offset);
6735             }
6736         }
6737       else
6738         {
6739           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
6740           rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6741           rel.r_addend = 0;
6742         }
6743
6744       loc = srelgot->contents;
6745       loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
6746       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6747     }
6748
6749 #ifdef INCLUDE_SHMEDIA
6750   {
6751     struct elf_sh_link_hash_entry *eh;
6752
6753     eh = (struct elf_sh_link_hash_entry *) h;
6754     if (eh->datalabel_got.offset != (bfd_vma) -1)
6755       {
6756         asection *sgot;
6757         asection *srelgot;
6758         Elf_Internal_Rela rel;
6759         bfd_byte *loc;
6760
6761         /* This symbol has a datalabel entry in the global offset table.
6762            Set it up.  */
6763
6764         sgot = htab->sgot;
6765         srelgot = htab->srelgot;
6766         BFD_ASSERT (sgot != NULL && srelgot != NULL);
6767
6768         rel.r_offset = (sgot->output_section->vma
6769                         + sgot->output_offset
6770                         + (eh->datalabel_got.offset &~ (bfd_vma) 1));
6771
6772         /* If this is a static link, or it is a -Bsymbolic link and the
6773            symbol is defined locally or was forced to be local because
6774            of a version file, we just want to emit a RELATIVE reloc.
6775            The entry in the global offset table will already have been
6776            initialized in the relocate_section function.  */
6777         if (bfd_link_pic (info)
6778             && SYMBOL_REFERENCES_LOCAL (info, h))
6779           {
6780             if (htab->fdpic_p)
6781               {
6782                 asection *sec = h->root.u.def.section;
6783                 int dynindx
6784                   = elf_section_data (sec->output_section)->dynindx;
6785
6786                 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
6787                 rel.r_addend = (h->root.u.def.value
6788                                 + h->root.u.def.section->output_offset);
6789               }
6790             else
6791               {
6792                 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6793                 rel.r_addend = (h->root.u.def.value
6794                                 + h->root.u.def.section->output_section->vma
6795                                 + h->root.u.def.section->output_offset);
6796               }
6797           }
6798         else
6799           {
6800             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
6801                         + eh->datalabel_got.offset);
6802             rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6803             rel.r_addend = 0;
6804           }
6805
6806         loc = srelgot->contents;
6807         loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
6808         bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6809       }
6810   }
6811 #endif
6812
6813   if (h->needs_copy)
6814     {
6815       asection *s;
6816       Elf_Internal_Rela rel;
6817       bfd_byte *loc;
6818
6819       /* This symbol needs a copy reloc.  Set it up.  */
6820
6821       BFD_ASSERT (h->dynindx != -1
6822                   && (h->root.type == bfd_link_hash_defined
6823                       || h->root.type == bfd_link_hash_defweak));
6824
6825       s = bfd_get_linker_section (htab->root.dynobj, ".rela.bss");
6826       BFD_ASSERT (s != NULL);
6827
6828       rel.r_offset = (h->root.u.def.value
6829                       + h->root.u.def.section->output_section->vma
6830                       + h->root.u.def.section->output_offset);
6831       rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
6832       rel.r_addend = 0;
6833       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6834       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6835     }
6836
6837   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
6838      _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
6839      ".got" section.  */
6840   if (h == htab->root.hdynamic
6841       || (!htab->vxworks_p && h == htab->root.hgot))
6842     sym->st_shndx = SHN_ABS;
6843
6844   return TRUE;
6845 }
6846
6847 /* Finish up the dynamic sections.  */
6848
6849 static bfd_boolean
6850 sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6851 {
6852   struct elf_sh_link_hash_table *htab;
6853   asection *sgotplt;
6854   asection *sdyn;
6855
6856   htab = sh_elf_hash_table (info);
6857   if (htab == NULL)
6858     return FALSE;
6859
6860   sgotplt = htab->sgotplt;
6861   sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
6862
6863   if (htab->root.dynamic_sections_created)
6864     {
6865       asection *splt;
6866       Elf32_External_Dyn *dyncon, *dynconend;
6867
6868       BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6869
6870       dyncon = (Elf32_External_Dyn *) sdyn->contents;
6871       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6872       for (; dyncon < dynconend; dyncon++)
6873         {
6874           Elf_Internal_Dyn dyn;
6875           asection *s;
6876 #ifdef INCLUDE_SHMEDIA
6877           const char *name;
6878 #endif
6879
6880           bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
6881
6882           switch (dyn.d_tag)
6883             {
6884             default:
6885               if (htab->vxworks_p
6886                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
6887                 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6888               break;
6889
6890 #ifdef INCLUDE_SHMEDIA
6891             case DT_INIT:
6892               name = info->init_function;
6893               goto get_sym;
6894
6895             case DT_FINI:
6896               name = info->fini_function;
6897             get_sym:
6898               if (dyn.d_un.d_val != 0)
6899                 {
6900                   struct elf_link_hash_entry *h;
6901
6902                   h = elf_link_hash_lookup (&htab->root, name,
6903                                             FALSE, FALSE, TRUE);
6904                   if (h != NULL && (h->other & STO_SH5_ISA32))
6905                     {
6906                       dyn.d_un.d_val |= 1;
6907                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6908                     }
6909                 }
6910               break;
6911 #endif
6912
6913             case DT_PLTGOT:
6914               BFD_ASSERT (htab->root.hgot != NULL);
6915               s = htab->root.hgot->root.u.def.section;
6916               dyn.d_un.d_ptr = htab->root.hgot->root.u.def.value
6917                 + s->output_section->vma + s->output_offset;
6918               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6919               break;
6920
6921             case DT_JMPREL:
6922               s = htab->srelplt->output_section;
6923               BFD_ASSERT (s != NULL);
6924               dyn.d_un.d_ptr = s->vma;
6925               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6926               break;
6927
6928             case DT_PLTRELSZ:
6929               s = htab->srelplt->output_section;
6930               BFD_ASSERT (s != NULL);
6931               dyn.d_un.d_val = s->size;
6932               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6933               break;
6934
6935             case DT_RELASZ:
6936               /* My reading of the SVR4 ABI indicates that the
6937                  procedure linkage table relocs (DT_JMPREL) should be
6938                  included in the overall relocs (DT_RELA).  This is
6939                  what Solaris does.  However, UnixWare can not handle
6940                  that case.  Therefore, we override the DT_RELASZ entry
6941                  here to make it not include the JMPREL relocs.  Since
6942                  the linker script arranges for .rela.plt to follow all
6943                  other relocation sections, we don't have to worry
6944                  about changing the DT_RELA entry.  */
6945               if (htab->srelplt != NULL)
6946                 {
6947                   s = htab->srelplt->output_section;
6948                   dyn.d_un.d_val -= s->size;
6949                 }
6950               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6951               break;
6952             }
6953         }
6954
6955       /* Fill in the first entry in the procedure linkage table.  */
6956       splt = htab->splt;
6957       if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
6958         {
6959           unsigned int i;
6960
6961           memcpy (splt->contents,
6962                   htab->plt_info->plt0_entry,
6963                   htab->plt_info->plt0_entry_size);
6964           for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
6965             if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
6966               install_plt_field (output_bfd, FALSE,
6967                                  (sgotplt->output_section->vma
6968                                   + sgotplt->output_offset
6969                                   + (i * 4)),
6970                                  (splt->contents
6971                                   + htab->plt_info->plt0_got_fields[i]));
6972
6973           if (htab->vxworks_p)
6974             {
6975               /* Finalize the .rela.plt.unloaded contents.  */
6976               Elf_Internal_Rela rel;
6977               bfd_byte *loc;
6978
6979               /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
6980                  first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8.  */
6981               loc = htab->srelplt2->contents;
6982               rel.r_offset = (splt->output_section->vma
6983                               + splt->output_offset
6984                               + htab->plt_info->plt0_got_fields[2]);
6985               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6986               rel.r_addend = 8;
6987               bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6988               loc += sizeof (Elf32_External_Rela);
6989
6990               /* Fix up the remaining .rela.plt.unloaded relocations.
6991                  They may have the wrong symbol index for _G_O_T_ or
6992                  _P_L_T_ depending on the order in which symbols were
6993                  output.  */
6994               while (loc < htab->srelplt2->contents + htab->srelplt2->size)
6995                 {
6996                   /* The PLT entry's pointer to the .got.plt slot.  */
6997                   bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
6998                   rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
6999                                              R_SH_DIR32);
7000                   bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7001                   loc += sizeof (Elf32_External_Rela);
7002
7003                   /* The .got.plt slot's pointer to .plt.  */
7004                   bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7005                   rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
7006                                              R_SH_DIR32);
7007                   bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7008                   loc += sizeof (Elf32_External_Rela);
7009                 }
7010             }
7011
7012           /* UnixWare sets the entsize of .plt to 4, although that doesn't
7013              really seem like the right value.  */
7014           elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
7015         }
7016     }
7017
7018   /* Fill in the first three entries in the global offset table.  */
7019   if (sgotplt && sgotplt->size > 0 && !htab->fdpic_p)
7020     {
7021       if (sdyn == NULL)
7022         bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
7023       else
7024         bfd_put_32 (output_bfd,
7025                     sdyn->output_section->vma + sdyn->output_offset,
7026                     sgotplt->contents);
7027       bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
7028       bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
7029     }
7030
7031   if (sgotplt && sgotplt->size > 0)
7032     elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
7033
7034   /* At the very end of the .rofixup section is a pointer to the GOT.  */
7035   if (htab->fdpic_p && htab->srofixup != NULL)
7036     {
7037       struct elf_link_hash_entry *hgot = htab->root.hgot;
7038       bfd_vma got_value = hgot->root.u.def.value
7039         + hgot->root.u.def.section->output_section->vma
7040         + hgot->root.u.def.section->output_offset;
7041
7042       sh_elf_add_rofixup (output_bfd, htab->srofixup, got_value);
7043
7044       /* Make sure we allocated and generated the same number of fixups.  */
7045       BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
7046     }
7047
7048   if (htab->srelfuncdesc)
7049     BFD_ASSERT (htab->srelfuncdesc->reloc_count * sizeof (Elf32_External_Rela)
7050                 == htab->srelfuncdesc->size);
7051
7052   if (htab->srelgot)
7053     BFD_ASSERT (htab->srelgot->reloc_count * sizeof (Elf32_External_Rela)
7054                 == htab->srelgot->size);
7055
7056   return TRUE;
7057 }
7058
7059 static enum elf_reloc_type_class
7060 sh_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
7061                          const asection *rel_sec ATTRIBUTE_UNUSED,
7062                          const Elf_Internal_Rela *rela)
7063 {
7064   switch ((int) ELF32_R_TYPE (rela->r_info))
7065     {
7066     case R_SH_RELATIVE:
7067       return reloc_class_relative;
7068     case R_SH_JMP_SLOT:
7069       return reloc_class_plt;
7070     case R_SH_COPY:
7071       return reloc_class_copy;
7072     default:
7073       return reloc_class_normal;
7074     }
7075 }
7076
7077 #if !defined SH_TARGET_ALREADY_DEFINED
7078 /* Support for Linux core dump NOTE sections.  */
7079
7080 static bfd_boolean
7081 elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7082 {
7083   int offset;
7084   unsigned int size;
7085
7086   switch (note->descsz)
7087     {
7088       default:
7089         return FALSE;
7090
7091       case 168:         /* Linux/SH */
7092         /* pr_cursig */
7093         elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
7094
7095         /* pr_pid */
7096         elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
7097
7098         /* pr_reg */
7099         offset = 72;
7100         size = 92;
7101
7102         break;
7103     }
7104
7105   /* Make a ".reg/999" section.  */
7106   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7107                                           size, note->descpos + offset);
7108 }
7109
7110 static bfd_boolean
7111 elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7112 {
7113   switch (note->descsz)
7114     {
7115       default:
7116         return FALSE;
7117
7118       case 124:         /* Linux/SH elf_prpsinfo */
7119         elf_tdata (abfd)->core->program
7120          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
7121         elf_tdata (abfd)->core->command
7122          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
7123     }
7124
7125   /* Note that for some reason, a spurious space is tacked
7126      onto the end of the args in some (at least one anyway)
7127      implementations, so strip it off if it exists.  */
7128
7129   {
7130     char *command = elf_tdata (abfd)->core->command;
7131     int n = strlen (command);
7132
7133     if (0 < n && command[n - 1] == ' ')
7134       command[n - 1] = '\0';
7135   }
7136
7137   return TRUE;
7138 }
7139 #endif /* not SH_TARGET_ALREADY_DEFINED */
7140
7141
7142 /* Return address for Ith PLT stub in section PLT, for relocation REL
7143    or (bfd_vma) -1 if it should not be included.  */
7144
7145 static bfd_vma
7146 sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
7147                     const arelent *rel ATTRIBUTE_UNUSED)
7148 {
7149   const struct elf_sh_plt_info *plt_info;
7150
7151   plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
7152   return plt->vma + get_plt_offset (plt_info, i);
7153 }
7154
7155 /* Decide whether to attempt to turn absptr or lsda encodings in
7156    shared libraries into pcrel within the given input section.  */
7157
7158 static bfd_boolean
7159 sh_elf_use_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
7160                               struct bfd_link_info *info,
7161                               asection *eh_frame_section ATTRIBUTE_UNUSED)
7162 {
7163   struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7164
7165   /* We can't use PC-relative encodings in FDPIC binaries, in general.  */
7166   if (htab->fdpic_p)
7167     return FALSE;
7168
7169   return TRUE;
7170 }
7171
7172 /* Adjust the contents of an eh_frame_hdr section before they're output.  */
7173
7174 static bfd_byte
7175 sh_elf_encode_eh_address (bfd *abfd,
7176                           struct bfd_link_info *info,
7177                           asection *osec, bfd_vma offset,
7178                           asection *loc_sec, bfd_vma loc_offset,
7179                           bfd_vma *encoded)
7180 {
7181   struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7182   struct elf_link_hash_entry *h;
7183
7184   if (!htab->fdpic_p)
7185     return _bfd_elf_encode_eh_address (abfd, info, osec, offset, loc_sec,
7186                                        loc_offset, encoded);
7187
7188   h = htab->root.hgot;
7189   BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
7190
7191   if (! h || (sh_elf_osec_to_segment (abfd, osec)
7192               == sh_elf_osec_to_segment (abfd, loc_sec->output_section)))
7193     return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
7194                                        loc_sec, loc_offset, encoded);
7195
7196   BFD_ASSERT (sh_elf_osec_to_segment (abfd, osec)
7197               == (sh_elf_osec_to_segment
7198                   (abfd, h->root.u.def.section->output_section)));
7199
7200   *encoded = osec->vma + offset
7201     - (h->root.u.def.value
7202        + h->root.u.def.section->output_section->vma
7203        + h->root.u.def.section->output_offset);
7204
7205   return DW_EH_PE_datarel | DW_EH_PE_sdata4;
7206 }
7207
7208 #if !defined SH_TARGET_ALREADY_DEFINED
7209 #define TARGET_BIG_SYM          sh_elf32_vec
7210 #define TARGET_BIG_NAME         "elf32-sh"
7211 #define TARGET_LITTLE_SYM       sh_elf32_le_vec
7212 #define TARGET_LITTLE_NAME      "elf32-shl"
7213 #endif
7214
7215 #define ELF_ARCH                bfd_arch_sh
7216 #define ELF_TARGET_ID           SH_ELF_DATA
7217 #define ELF_MACHINE_CODE        EM_SH
7218 #ifdef __QNXTARGET__
7219 #define ELF_MAXPAGESIZE         0x1000
7220 #else
7221 #define ELF_MAXPAGESIZE         0x80
7222 #endif
7223
7224 #define elf_symbol_leading_char '_'
7225
7226 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
7227 #define bfd_elf32_bfd_reloc_name_lookup \
7228                                         sh_elf_reloc_name_lookup
7229 #define elf_info_to_howto               sh_elf_info_to_howto
7230 #define bfd_elf32_bfd_relax_section     sh_elf_relax_section
7231 #define elf_backend_relocate_section    sh_elf_relocate_section
7232 #define bfd_elf32_bfd_get_relocated_section_contents \
7233                                         sh_elf_get_relocated_section_contents
7234 #define bfd_elf32_mkobject              sh_elf_mkobject
7235 #define elf_backend_object_p            sh_elf_object_p
7236 #define bfd_elf32_bfd_copy_private_bfd_data \
7237                                         sh_elf_copy_private_data
7238 #define bfd_elf32_bfd_merge_private_bfd_data \
7239                                         sh_elf_merge_private_data
7240
7241 #define elf_backend_gc_mark_hook        sh_elf_gc_mark_hook
7242 #define elf_backend_check_relocs        sh_elf_check_relocs
7243 #define elf_backend_copy_indirect_symbol \
7244                                         sh_elf_copy_indirect_symbol
7245 #define elf_backend_create_dynamic_sections \
7246                                         sh_elf_create_dynamic_sections
7247 #define bfd_elf32_bfd_link_hash_table_create \
7248                                         sh_elf_link_hash_table_create
7249 #define elf_backend_adjust_dynamic_symbol \
7250                                         sh_elf_adjust_dynamic_symbol
7251 #define elf_backend_always_size_sections \
7252                                         sh_elf_always_size_sections
7253 #define elf_backend_size_dynamic_sections \
7254                                         sh_elf_size_dynamic_sections
7255 #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
7256 #define elf_backend_finish_dynamic_symbol \
7257                                         sh_elf_finish_dynamic_symbol
7258 #define elf_backend_finish_dynamic_sections \
7259                                         sh_elf_finish_dynamic_sections
7260 #define elf_backend_reloc_type_class    sh_elf_reloc_type_class
7261 #define elf_backend_plt_sym_val         sh_elf_plt_sym_val
7262 #define elf_backend_can_make_relative_eh_frame \
7263                                         sh_elf_use_relative_eh_frame
7264 #define elf_backend_can_make_lsda_relative_eh_frame \
7265                                         sh_elf_use_relative_eh_frame
7266 #define elf_backend_encode_eh_address \
7267                                         sh_elf_encode_eh_address
7268
7269 #define elf_backend_stack_align         8
7270 #define elf_backend_can_gc_sections     1
7271 #define elf_backend_can_refcount        1
7272 #define elf_backend_want_got_plt        1
7273 #define elf_backend_plt_readonly        1
7274 #define elf_backend_want_plt_sym        0
7275 #define elf_backend_got_header_size     12
7276
7277 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
7278
7279 #include "elf32-target.h"
7280
7281 /* NetBSD support.  */
7282 #undef  TARGET_BIG_SYM
7283 #define TARGET_BIG_SYM                  sh_elf32_nbsd_vec
7284 #undef  TARGET_BIG_NAME
7285 #define TARGET_BIG_NAME                 "elf32-sh-nbsd"
7286 #undef  TARGET_LITTLE_SYM
7287 #define TARGET_LITTLE_SYM               sh_elf32_nbsd_le_vec
7288 #undef  TARGET_LITTLE_NAME
7289 #define TARGET_LITTLE_NAME              "elf32-shl-nbsd"
7290 #undef  ELF_MAXPAGESIZE
7291 #define ELF_MAXPAGESIZE                 0x10000
7292 #undef  ELF_COMMONPAGESIZE
7293 #undef  elf_symbol_leading_char
7294 #define elf_symbol_leading_char         0
7295 #undef  elf32_bed
7296 #define elf32_bed                       elf32_sh_nbsd_bed
7297
7298 #include "elf32-target.h"
7299
7300
7301 /* Linux support.  */
7302 #undef  TARGET_BIG_SYM
7303 #define TARGET_BIG_SYM                  sh_elf32_linux_be_vec
7304 #undef  TARGET_BIG_NAME
7305 #define TARGET_BIG_NAME                 "elf32-shbig-linux"
7306 #undef  TARGET_LITTLE_SYM
7307 #define TARGET_LITTLE_SYM               sh_elf32_linux_vec
7308 #undef  TARGET_LITTLE_NAME
7309 #define TARGET_LITTLE_NAME              "elf32-sh-linux"
7310 #undef  ELF_COMMONPAGESIZE
7311 #define ELF_COMMONPAGESIZE              0x1000
7312
7313 #undef  elf_backend_grok_prstatus
7314 #define elf_backend_grok_prstatus       elf32_shlin_grok_prstatus
7315 #undef  elf_backend_grok_psinfo
7316 #define elf_backend_grok_psinfo         elf32_shlin_grok_psinfo
7317 #undef  elf32_bed
7318 #define elf32_bed                       elf32_sh_lin_bed
7319
7320 #include "elf32-target.h"
7321
7322
7323 /* FDPIC support.  */
7324 #undef  TARGET_BIG_SYM
7325 #define TARGET_BIG_SYM                  sh_elf32_fdpic_be_vec
7326 #undef  TARGET_BIG_NAME
7327 #define TARGET_BIG_NAME                 "elf32-shbig-fdpic"
7328 #undef  TARGET_LITTLE_SYM
7329 #define TARGET_LITTLE_SYM               sh_elf32_fdpic_le_vec
7330 #undef  TARGET_LITTLE_NAME
7331 #define TARGET_LITTLE_NAME              "elf32-sh-fdpic"
7332
7333 #undef  elf32_bed
7334 #define elf32_bed                       elf32_sh_fd_bed
7335
7336 #include "elf32-target.h"
7337
7338 #undef elf_backend_modify_program_headers
7339
7340 /* VxWorks support.  */
7341 #undef  TARGET_BIG_SYM
7342 #define TARGET_BIG_SYM                  sh_elf32_vxworks_vec
7343 #undef  TARGET_BIG_NAME
7344 #define TARGET_BIG_NAME                 "elf32-sh-vxworks"
7345 #undef  TARGET_LITTLE_SYM
7346 #define TARGET_LITTLE_SYM               sh_elf32_vxworks_le_vec
7347 #undef  TARGET_LITTLE_NAME
7348 #define TARGET_LITTLE_NAME              "elf32-shl-vxworks"
7349 #undef  elf32_bed
7350 #define elf32_bed                       elf32_sh_vxworks_bed
7351
7352 #undef  elf_backend_want_plt_sym
7353 #define elf_backend_want_plt_sym        1
7354 #undef  elf_symbol_leading_char
7355 #define elf_symbol_leading_char         '_'
7356 #define elf_backend_want_got_underscore 1
7357 #undef  elf_backend_grok_prstatus
7358 #undef  elf_backend_grok_psinfo
7359 #undef  elf_backend_add_symbol_hook
7360 #define elf_backend_add_symbol_hook     elf_vxworks_add_symbol_hook
7361 #undef  elf_backend_link_output_symbol_hook
7362 #define elf_backend_link_output_symbol_hook \
7363                                         elf_vxworks_link_output_symbol_hook
7364 #undef  elf_backend_emit_relocs
7365 #define elf_backend_emit_relocs         elf_vxworks_emit_relocs
7366 #undef  elf_backend_final_write_processing
7367 #define elf_backend_final_write_processing \
7368                                         elf_vxworks_final_write_processing
7369 #undef  ELF_MAXPAGESIZE
7370 #define ELF_MAXPAGESIZE                 0x1000
7371 #undef  ELF_COMMONPAGESIZE
7372
7373 #include "elf32-target.h"
7374
7375 #endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */