-Wimplicit-fallthrough warning 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               /* Fall through.  */
5885             case R_SH_GOTPLT32:
5886             case R_SH_GOT32:
5887             case R_SH_GOT20:
5888             case R_SH_GOTOFF:
5889             case R_SH_GOTOFF20:
5890             case R_SH_FUNCDESC:
5891             case R_SH_GOTFUNCDESC:
5892             case R_SH_GOTFUNCDESC20:
5893             case R_SH_GOTOFFFUNCDESC:
5894             case R_SH_GOTOFFFUNCDESC20:
5895             case R_SH_GOTPC:
5896 #ifdef INCLUDE_SHMEDIA
5897             case R_SH_GOTPLT_LOW16:
5898             case R_SH_GOTPLT_MEDLOW16:
5899             case R_SH_GOTPLT_MEDHI16:
5900             case R_SH_GOTPLT_HI16:
5901             case R_SH_GOTPLT10BY4:
5902             case R_SH_GOTPLT10BY8:
5903             case R_SH_GOT_LOW16:
5904             case R_SH_GOT_MEDLOW16:
5905             case R_SH_GOT_MEDHI16:
5906             case R_SH_GOT_HI16:
5907             case R_SH_GOT10BY4:
5908             case R_SH_GOT10BY8:
5909             case R_SH_GOTOFF_LOW16:
5910             case R_SH_GOTOFF_MEDLOW16:
5911             case R_SH_GOTOFF_MEDHI16:
5912             case R_SH_GOTOFF_HI16:
5913             case R_SH_GOTPC_LOW16:
5914             case R_SH_GOTPC_MEDLOW16:
5915             case R_SH_GOTPC_MEDHI16:
5916             case R_SH_GOTPC_HI16:
5917 #endif
5918             case R_SH_TLS_GD_32:
5919             case R_SH_TLS_LD_32:
5920             case R_SH_TLS_IE_32:
5921               if (htab->root.dynobj == NULL)
5922                 htab->root.dynobj = abfd;
5923               if (!create_got_section (htab->root.dynobj, info))
5924                 return FALSE;
5925               break;
5926
5927             default:
5928               break;
5929             }
5930         }
5931
5932       switch (r_type)
5933         {
5934           /* This relocation describes the C++ object vtable hierarchy.
5935              Reconstruct it for later use during GC.  */
5936         case R_SH_GNU_VTINHERIT:
5937           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5938             return FALSE;
5939           break;
5940
5941           /* This relocation describes which C++ vtable entries are actually
5942              used.  Record for later use during GC.  */
5943         case R_SH_GNU_VTENTRY:
5944           BFD_ASSERT (h != NULL);
5945           if (h != NULL
5946               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5947             return FALSE;
5948           break;
5949
5950         case R_SH_TLS_IE_32:
5951           if (bfd_link_pic (info))
5952             info->flags |= DF_STATIC_TLS;
5953
5954           /* FALLTHROUGH */
5955         force_got:
5956         case R_SH_TLS_GD_32:
5957         case R_SH_GOT32:
5958         case R_SH_GOT20:
5959 #ifdef INCLUDE_SHMEDIA
5960         case R_SH_GOT_LOW16:
5961         case R_SH_GOT_MEDLOW16:
5962         case R_SH_GOT_MEDHI16:
5963         case R_SH_GOT_HI16:
5964         case R_SH_GOT10BY4:
5965         case R_SH_GOT10BY8:
5966 #endif
5967         case R_SH_GOTFUNCDESC:
5968         case R_SH_GOTFUNCDESC20:
5969           switch (r_type)
5970             {
5971             default:
5972               got_type = GOT_NORMAL;
5973               break;
5974             case R_SH_TLS_GD_32:
5975               got_type = GOT_TLS_GD;
5976               break;
5977             case R_SH_TLS_IE_32:
5978               got_type = GOT_TLS_IE;
5979               break;
5980             case R_SH_GOTFUNCDESC:
5981             case R_SH_GOTFUNCDESC20:
5982               got_type = GOT_FUNCDESC;
5983               break;
5984             }
5985
5986           if (h != NULL)
5987             {
5988 #ifdef INCLUDE_SHMEDIA
5989               if (seen_stt_datalabel)
5990                 {
5991                   struct elf_sh_link_hash_entry *eh
5992                     = (struct elf_sh_link_hash_entry *) h;
5993
5994                   eh->datalabel_got.refcount += 1;
5995                 }
5996               else
5997 #endif
5998                 h->got.refcount += 1;
5999               old_got_type = sh_elf_hash_entry (h)->got_type;
6000             }
6001           else
6002             {
6003               bfd_signed_vma *local_got_refcounts;
6004
6005               /* This is a global offset table entry for a local
6006                  symbol.  */
6007               local_got_refcounts = elf_local_got_refcounts (abfd);
6008               if (local_got_refcounts == NULL)
6009                 {
6010                   bfd_size_type size;
6011
6012                   size = symtab_hdr->sh_info;
6013                   size *= sizeof (bfd_signed_vma);
6014 #ifdef INCLUDE_SHMEDIA
6015                   /* Reserve space for both the datalabel and
6016                      codelabel local GOT offsets.  */
6017                   size *= 2;
6018 #endif
6019                   size += symtab_hdr->sh_info;
6020                   local_got_refcounts = ((bfd_signed_vma *)
6021                                          bfd_zalloc (abfd, size));
6022                   if (local_got_refcounts == NULL)
6023                     return FALSE;
6024                   elf_local_got_refcounts (abfd) = local_got_refcounts;
6025 #ifdef  INCLUDE_SHMEDIA
6026                   /* Take care of both the datalabel and codelabel local
6027                      GOT offsets.  */
6028                   sh_elf_local_got_type (abfd)
6029                     = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
6030 #else
6031                   sh_elf_local_got_type (abfd)
6032                     = (char *) (local_got_refcounts + symtab_hdr->sh_info);
6033 #endif
6034                 }
6035 #ifdef INCLUDE_SHMEDIA
6036               if (rel->r_addend & 1)
6037                 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
6038               else
6039 #endif
6040                 local_got_refcounts[r_symndx] += 1;
6041               old_got_type = sh_elf_local_got_type (abfd) [r_symndx];
6042             }
6043
6044           /* If a TLS symbol is accessed using IE at least once,
6045              there is no point to use dynamic model for it.  */
6046           if (old_got_type != got_type && old_got_type != GOT_UNKNOWN
6047               && (old_got_type != GOT_TLS_GD || got_type != GOT_TLS_IE))
6048             {
6049               if (old_got_type == GOT_TLS_IE && got_type == GOT_TLS_GD)
6050                 got_type = GOT_TLS_IE;
6051               else
6052                 {
6053                   if ((old_got_type == GOT_FUNCDESC || got_type == GOT_FUNCDESC)
6054                       && (old_got_type == GOT_NORMAL || got_type == GOT_NORMAL))
6055                     _bfd_error_handler
6056                       (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6057                        abfd, h->root.root.string);
6058                   else if (old_got_type == GOT_FUNCDESC
6059                            || got_type == GOT_FUNCDESC)
6060                     _bfd_error_handler
6061                       (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6062                        abfd, h->root.root.string);
6063                   else
6064                     _bfd_error_handler
6065                       (_("%B: `%s' accessed both as normal and thread local symbol"),
6066                        abfd, h->root.root.string);
6067                   return FALSE;
6068                 }
6069             }
6070
6071           if (old_got_type != got_type)
6072             {
6073               if (h != NULL)
6074                 sh_elf_hash_entry (h)->got_type = got_type;
6075               else
6076                 sh_elf_local_got_type (abfd) [r_symndx] = got_type;
6077             }
6078
6079           break;
6080
6081         case R_SH_TLS_LD_32:
6082           sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
6083           break;
6084
6085         case R_SH_FUNCDESC:
6086         case R_SH_GOTOFFFUNCDESC:
6087         case R_SH_GOTOFFFUNCDESC20:
6088           if (rel->r_addend)
6089             {
6090               _bfd_error_handler
6091                 (_("%B: Function descriptor relocation with non-zero addend"),
6092                  abfd);
6093               return FALSE;
6094             }
6095
6096           if (h == NULL)
6097             {
6098               union gotref *local_funcdesc;
6099
6100               /* We need a function descriptor for a local symbol.  */
6101               local_funcdesc = sh_elf_local_funcdesc (abfd);
6102               if (local_funcdesc == NULL)
6103                 {
6104                   bfd_size_type size;
6105
6106                   size = symtab_hdr->sh_info * sizeof (union gotref);
6107 #ifdef INCLUDE_SHMEDIA
6108                   /* Count datalabel local GOT.  */
6109                   size *= 2;
6110 #endif
6111                   local_funcdesc = (union gotref *) bfd_zalloc (abfd, size);
6112                   if (local_funcdesc == NULL)
6113                     return FALSE;
6114                   sh_elf_local_funcdesc (abfd) = local_funcdesc;
6115                 }
6116               local_funcdesc[r_symndx].refcount += 1;
6117
6118               if (r_type == R_SH_FUNCDESC)
6119                 {
6120                   if (!bfd_link_pic (info))
6121                     htab->srofixup->size += 4;
6122                   else
6123                     htab->srelgot->size += sizeof (Elf32_External_Rela);
6124                 }
6125             }
6126           else
6127             {
6128               sh_elf_hash_entry (h)->funcdesc.refcount++;
6129               if (r_type == R_SH_FUNCDESC)
6130                 sh_elf_hash_entry (h)->abs_funcdesc_refcount++;
6131
6132               /* If there is a function descriptor reference, then
6133                  there should not be any non-FDPIC references.  */
6134               old_got_type = sh_elf_hash_entry (h)->got_type;
6135               if (old_got_type != GOT_FUNCDESC && old_got_type != GOT_UNKNOWN)
6136                 {
6137                   if (old_got_type == GOT_NORMAL)
6138                     _bfd_error_handler
6139                       (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6140                        abfd, h->root.root.string);
6141                   else
6142                     _bfd_error_handler
6143                       (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6144                        abfd, h->root.root.string);
6145                 }
6146             }
6147           break;
6148
6149         case R_SH_GOTPLT32:
6150 #ifdef INCLUDE_SHMEDIA
6151         case R_SH_GOTPLT_LOW16:
6152         case R_SH_GOTPLT_MEDLOW16:
6153         case R_SH_GOTPLT_MEDHI16:
6154         case R_SH_GOTPLT_HI16:
6155         case R_SH_GOTPLT10BY4:
6156         case R_SH_GOTPLT10BY8:
6157 #endif
6158           /* If this is a local symbol, we resolve it directly without
6159              creating a procedure linkage table entry.  */
6160
6161           if (h == NULL
6162               || h->forced_local
6163               || ! bfd_link_pic (info)
6164               || info->symbolic
6165               || h->dynindx == -1)
6166             goto force_got;
6167
6168           h->needs_plt = 1;
6169           h->plt.refcount += 1;
6170           ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
6171
6172           break;
6173
6174         case R_SH_PLT32:
6175 #ifdef INCLUDE_SHMEDIA
6176         case R_SH_PLT_LOW16:
6177         case R_SH_PLT_MEDLOW16:
6178         case R_SH_PLT_MEDHI16:
6179         case R_SH_PLT_HI16:
6180 #endif
6181           /* This symbol requires a procedure linkage table entry.  We
6182              actually build the entry in adjust_dynamic_symbol,
6183              because this might be a case of linking PIC code which is
6184              never referenced by a dynamic object, in which case we
6185              don't need to generate a procedure linkage table entry
6186              after all.  */
6187
6188           /* If this is a local symbol, we resolve it directly without
6189              creating a procedure linkage table entry.  */
6190           if (h == NULL)
6191             continue;
6192
6193           if (h->forced_local)
6194             break;
6195
6196           h->needs_plt = 1;
6197           h->plt.refcount += 1;
6198           break;
6199
6200         case R_SH_DIR32:
6201         case R_SH_REL32:
6202 #ifdef INCLUDE_SHMEDIA
6203         case R_SH_IMM_LOW16_PCREL:
6204         case R_SH_IMM_MEDLOW16_PCREL:
6205         case R_SH_IMM_MEDHI16_PCREL:
6206         case R_SH_IMM_HI16_PCREL:
6207 #endif
6208           if (h != NULL && ! bfd_link_pic (info))
6209             {
6210               h->non_got_ref = 1;
6211               h->plt.refcount += 1;
6212             }
6213
6214           /* If we are creating a shared library, and this is a reloc
6215              against a global symbol, or a non PC relative reloc
6216              against a local symbol, then we need to copy the reloc
6217              into the shared library.  However, if we are linking with
6218              -Bsymbolic, we do not need to copy a reloc against a
6219              global symbol which is defined in an object we are
6220              including in the link (i.e., DEF_REGULAR is set).  At
6221              this point we have not seen all the input files, so it is
6222              possible that DEF_REGULAR is not set now but will be set
6223              later (it is never cleared).  We account for that
6224              possibility below by storing information in the
6225              dyn_relocs field of the hash table entry. A similar
6226              situation occurs when creating shared libraries and symbol
6227              visibility changes render the symbol local.
6228
6229              If on the other hand, we are creating an executable, we
6230              may need to keep relocations for symbols satisfied by a
6231              dynamic library if we manage to avoid copy relocs for the
6232              symbol.  */
6233           if ((bfd_link_pic (info)
6234                && (sec->flags & SEC_ALLOC) != 0
6235                && (r_type != R_SH_REL32
6236                    || (h != NULL
6237                        && (! info->symbolic
6238                            || h->root.type == bfd_link_hash_defweak
6239                            || !h->def_regular))))
6240               || (! bfd_link_pic (info)
6241                   && (sec->flags & SEC_ALLOC) != 0
6242                   && h != NULL
6243                   && (h->root.type == bfd_link_hash_defweak
6244                       || !h->def_regular)))
6245             {
6246               struct elf_sh_dyn_relocs *p;
6247               struct elf_sh_dyn_relocs **head;
6248
6249               if (htab->root.dynobj == NULL)
6250                 htab->root.dynobj = abfd;
6251
6252               /* When creating a shared object, we must copy these
6253                  reloc types into the output file.  We create a reloc
6254                  section in dynobj and make room for this reloc.  */
6255               if (sreloc == NULL)
6256                 {
6257                   sreloc = _bfd_elf_make_dynamic_reloc_section
6258                     (sec, htab->root.dynobj, 2, abfd, /*rela?*/ TRUE);
6259
6260                   if (sreloc == NULL)
6261                     return FALSE;
6262                 }
6263
6264               /* If this is a global symbol, we count the number of
6265                  relocations we need for this symbol.  */
6266               if (h != NULL)
6267                 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
6268               else
6269                 {
6270                   /* Track dynamic relocs needed for local syms too.  */
6271                   asection *s;
6272                   void *vpp;
6273                   Elf_Internal_Sym *isym;
6274
6275                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6276                                                 abfd, r_symndx);
6277                   if (isym == NULL)
6278                     return FALSE;
6279
6280                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6281                   if (s == NULL)
6282                     s = sec;
6283
6284                   vpp = &elf_section_data (s)->local_dynrel;
6285                   head = (struct elf_sh_dyn_relocs **) vpp;
6286                 }
6287
6288               p = *head;
6289               if (p == NULL || p->sec != sec)
6290                 {
6291                   bfd_size_type amt = sizeof (*p);
6292                   p = bfd_alloc (htab->root.dynobj, amt);
6293                   if (p == NULL)
6294                     return FALSE;
6295                   p->next = *head;
6296                   *head = p;
6297                   p->sec = sec;
6298                   p->count = 0;
6299                   p->pc_count = 0;
6300                 }
6301
6302               p->count += 1;
6303               if (r_type == R_SH_REL32
6304 #ifdef INCLUDE_SHMEDIA
6305                   || r_type == R_SH_IMM_LOW16_PCREL
6306                   || r_type == R_SH_IMM_MEDLOW16_PCREL
6307                   || r_type == R_SH_IMM_MEDHI16_PCREL
6308                   || r_type == R_SH_IMM_HI16_PCREL
6309 #endif
6310                   )
6311                 p->pc_count += 1;
6312             }
6313
6314           /* Allocate the fixup regardless of whether we need a relocation.
6315              If we end up generating the relocation, we'll unallocate the
6316              fixup.  */
6317           if (htab->fdpic_p && !bfd_link_pic (info)
6318               && r_type == R_SH_DIR32
6319               && (sec->flags & SEC_ALLOC) != 0)
6320             htab->srofixup->size += 4;
6321           break;
6322
6323         case R_SH_TLS_LE_32:
6324           if (bfd_link_dll (info))
6325             {
6326               _bfd_error_handler
6327                 (_("%B: TLS local exec code cannot be linked into shared objects"),
6328                  abfd);
6329               return FALSE;
6330             }
6331
6332           break;
6333
6334         case R_SH_TLS_LDO_32:
6335           /* Nothing to do.  */
6336           break;
6337
6338         default:
6339           break;
6340         }
6341     }
6342
6343   return TRUE;
6344 }
6345
6346 #ifndef sh_elf_set_mach_from_flags
6347 static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
6348
6349 static bfd_boolean
6350 sh_elf_set_mach_from_flags (bfd *abfd)
6351 {
6352   flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
6353
6354   if (flags >= sizeof(sh_ef_bfd_table))
6355     return FALSE;
6356
6357   if (sh_ef_bfd_table[flags] == 0)
6358     return FALSE;
6359
6360   bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
6361
6362   return TRUE;
6363 }
6364
6365
6366 /* Reverse table lookup for sh_ef_bfd_table[].
6367    Given a bfd MACH value from archures.c
6368    return the equivalent ELF flags from the table.
6369    Return -1 if no match is found.  */
6370
6371 int
6372 sh_elf_get_flags_from_mach (unsigned long mach)
6373 {
6374   int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
6375
6376   for (; i>0; i--)
6377     if (sh_ef_bfd_table[i] == mach)
6378       return i;
6379
6380   /* shouldn't get here */
6381   BFD_FAIL();
6382
6383   return -1;
6384 }
6385 #endif /* not sh_elf_set_mach_from_flags */
6386
6387 #ifndef sh_elf_copy_private_data
6388 /* Copy backend specific data from one object module to another */
6389
6390 static bfd_boolean
6391 sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
6392 {
6393   if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6394     return TRUE;
6395
6396   if (! _bfd_elf_copy_private_bfd_data (ibfd, obfd))
6397     return FALSE;
6398
6399   return sh_elf_set_mach_from_flags (obfd);
6400 }
6401 #endif /* not sh_elf_copy_private_data */
6402
6403 #ifndef sh_elf_merge_private_data
6404
6405 /* This function returns the ELF architecture number that
6406    corresponds to the given arch_sh* flags.  */
6407
6408 int
6409 sh_find_elf_flags (unsigned int arch_set)
6410 {
6411   extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
6412   unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
6413
6414   return sh_elf_get_flags_from_mach (bfd_mach);
6415 }
6416
6417 /* This routine initialises the elf flags when required and
6418    calls sh_merge_bfd_arch() to check dsp/fpu compatibility.  */
6419
6420 static bfd_boolean
6421 sh_elf_merge_private_data (bfd *ibfd, bfd *obfd)
6422 {
6423   extern bfd_boolean sh_merge_bfd_arch (bfd *, bfd *);
6424
6425   if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6426     return TRUE;
6427
6428   if (! elf_flags_init (obfd))
6429     {
6430       /* This happens when ld starts out with a 'blank' output file.  */
6431       elf_flags_init (obfd) = TRUE;
6432       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6433       sh_elf_set_mach_from_flags (obfd);
6434       if (elf_elfheader (obfd)->e_flags & EF_SH_FDPIC)
6435         elf_elfheader (obfd)->e_flags &= ~EF_SH_PIC;
6436     }
6437
6438   if (! sh_merge_bfd_arch (ibfd, obfd))
6439     {
6440       _bfd_error_handler ("%B: uses instructions which are incompatible "
6441                           "with instructions used in previous modules",
6442                           ibfd);
6443       bfd_set_error (bfd_error_bad_value);
6444       return FALSE;
6445     }
6446
6447   elf_elfheader (obfd)->e_flags &= ~EF_SH_MACH_MASK;
6448   elf_elfheader (obfd)->e_flags |=
6449     sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
6450
6451   if (fdpic_object_p (ibfd) != fdpic_object_p (obfd))
6452     {
6453       _bfd_error_handler ("%B: attempt to mix FDPIC and non-FDPIC objects",
6454                           ibfd);
6455       bfd_set_error (bfd_error_bad_value);
6456       return FALSE;
6457     }
6458
6459   return TRUE;
6460 }
6461 #endif /* not sh_elf_merge_private_data */
6462
6463 /* Override the generic function because we need to store sh_elf_obj_tdata
6464    as the specific tdata.  We set also the machine architecture from flags
6465    here.  */
6466
6467 static bfd_boolean
6468 sh_elf_object_p (bfd *abfd)
6469 {
6470   if (! sh_elf_set_mach_from_flags (abfd))
6471     return FALSE;
6472
6473   return (((elf_elfheader (abfd)->e_flags & EF_SH_FDPIC) != 0)
6474           == fdpic_object_p (abfd));
6475 }
6476
6477 /* Finish up dynamic symbol handling.  We set the contents of various
6478    dynamic sections here.  */
6479
6480 static bfd_boolean
6481 sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6482                               struct elf_link_hash_entry *h,
6483                               Elf_Internal_Sym *sym)
6484 {
6485   struct elf_sh_link_hash_table *htab;
6486
6487   htab = sh_elf_hash_table (info);
6488   if (htab == NULL)
6489     return FALSE;
6490
6491   if (h->plt.offset != (bfd_vma) -1)
6492     {
6493       asection *splt;
6494       asection *sgotplt;
6495       asection *srelplt;
6496
6497       bfd_vma plt_index;
6498       bfd_vma got_offset;
6499       Elf_Internal_Rela rel;
6500       bfd_byte *loc;
6501       const struct elf_sh_plt_info *plt_info;
6502
6503       /* This symbol has an entry in the procedure linkage table.  Set
6504          it up.  */
6505
6506       BFD_ASSERT (h->dynindx != -1);
6507
6508       splt = htab->splt;
6509       sgotplt = htab->sgotplt;
6510       srelplt = htab->srelplt;
6511       BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL);
6512
6513       /* Get the index in the procedure linkage table which
6514          corresponds to this symbol.  This is the index of this symbol
6515          in all the symbols for which we are making plt entries.  The
6516          first entry in the procedure linkage table is reserved.  */
6517       plt_index = get_plt_index (htab->plt_info, h->plt.offset);
6518
6519       plt_info = htab->plt_info;
6520       if (plt_info->short_plt != NULL && plt_index <= MAX_SHORT_PLT)
6521         plt_info = plt_info->short_plt;
6522
6523       /* Get the offset into the .got table of the entry that
6524          corresponds to this function.  */
6525       if (htab->fdpic_p)
6526         /* The offset must be relative to the GOT symbol, twelve bytes
6527            before the end of .got.plt.  Each descriptor is eight
6528            bytes.  */
6529         got_offset = plt_index * 8 + 12 - sgotplt->size;
6530       else
6531         /* Each .got entry is 4 bytes.  The first three are
6532            reserved.  */
6533         got_offset = (plt_index + 3) * 4;
6534
6535 #ifdef GOT_BIAS
6536       if (bfd_link_pic (info))
6537         got_offset -= GOT_BIAS;
6538 #endif
6539
6540       /* Fill in the entry in the procedure linkage table.  */
6541       memcpy (splt->contents + h->plt.offset,
6542               plt_info->symbol_entry,
6543               plt_info->symbol_entry_size);
6544
6545       if (bfd_link_pic (info) || htab->fdpic_p)
6546         {
6547           if (plt_info->symbol_fields.got20)
6548             {
6549               bfd_reloc_status_type r;
6550               r = install_movi20_field (output_bfd, got_offset,
6551                                         splt->owner, splt, splt->contents,
6552                                         h->plt.offset
6553                                         + plt_info->symbol_fields.got_entry);
6554               BFD_ASSERT (r == bfd_reloc_ok);
6555             }
6556           else
6557             install_plt_field (output_bfd, FALSE, got_offset,
6558                                (splt->contents
6559                                 + h->plt.offset
6560                                 + plt_info->symbol_fields.got_entry));
6561         }
6562       else
6563         {
6564           BFD_ASSERT (!plt_info->symbol_fields.got20);
6565
6566           install_plt_field (output_bfd, FALSE,
6567                              (sgotplt->output_section->vma
6568                               + sgotplt->output_offset
6569                               + got_offset),
6570                              (splt->contents
6571                               + h->plt.offset
6572                               + plt_info->symbol_fields.got_entry));
6573           if (htab->vxworks_p)
6574             {
6575               unsigned int reachable_plts, plts_per_4k;
6576               int distance;
6577
6578               /* Divide the PLT into groups.  The first group contains
6579                  REACHABLE_PLTS entries and the other groups contain
6580                  PLTS_PER_4K entries.  Entries in the first group can
6581                  branch directly to .plt; those in later groups branch
6582                  to the last element of the previous group.  */
6583               /* ??? It would be better to create multiple copies of
6584                  the common resolver stub.  */
6585               reachable_plts = ((4096
6586                                  - plt_info->plt0_entry_size
6587                                  - (plt_info->symbol_fields.plt + 4))
6588                                 / plt_info->symbol_entry_size) + 1;
6589               plts_per_4k = (4096 / plt_info->symbol_entry_size);
6590               if (plt_index < reachable_plts)
6591                 distance = -(h->plt.offset
6592                              + plt_info->symbol_fields.plt);
6593               else
6594                 distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
6595                              * plt_info->symbol_entry_size);
6596
6597               /* Install the 'bra' with this offset.  */
6598               bfd_put_16 (output_bfd,
6599                           0xa000 | (0x0fff & ((distance - 4) / 2)),
6600                           (splt->contents
6601                            + h->plt.offset
6602                            + plt_info->symbol_fields.plt));
6603             }
6604           else
6605             install_plt_field (output_bfd, TRUE,
6606                                splt->output_section->vma + splt->output_offset,
6607                                (splt->contents
6608                                 + h->plt.offset
6609                                 + plt_info->symbol_fields.plt));
6610         }
6611
6612       /* Make got_offset relative to the start of .got.plt.  */
6613 #ifdef GOT_BIAS
6614       if (bfd_link_pic (info))
6615         got_offset += GOT_BIAS;
6616 #endif
6617       if (htab->fdpic_p)
6618         got_offset = plt_index * 8;
6619
6620       if (plt_info->symbol_fields.reloc_offset != MINUS_ONE)
6621         install_plt_field (output_bfd, FALSE,
6622                            plt_index * sizeof (Elf32_External_Rela),
6623                            (splt->contents
6624                             + h->plt.offset
6625                             + plt_info->symbol_fields.reloc_offset));
6626
6627       /* Fill in the entry in the global offset table.  */
6628       bfd_put_32 (output_bfd,
6629                   (splt->output_section->vma
6630                    + splt->output_offset
6631                    + h->plt.offset
6632                    + plt_info->symbol_resolve_offset),
6633                   sgotplt->contents + got_offset);
6634       if (htab->fdpic_p)
6635         bfd_put_32 (output_bfd,
6636                     sh_elf_osec_to_segment (output_bfd,
6637                                             htab->splt->output_section),
6638                     sgotplt->contents + got_offset + 4);
6639
6640       /* Fill in the entry in the .rela.plt section.  */
6641       rel.r_offset = (sgotplt->output_section->vma
6642                       + sgotplt->output_offset
6643                       + got_offset);
6644       if (htab->fdpic_p)
6645         rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_FUNCDESC_VALUE);
6646       else
6647         rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
6648       rel.r_addend = 0;
6649 #ifdef GOT_BIAS
6650       rel.r_addend = GOT_BIAS;
6651 #endif
6652       loc = srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
6653       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6654
6655       if (htab->vxworks_p && !bfd_link_pic (info))
6656         {
6657           /* Create the .rela.plt.unloaded relocations for this PLT entry.
6658              Begin by pointing LOC to the first such relocation.  */
6659           loc = (htab->srelplt2->contents
6660                  + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
6661
6662           /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6663              for the PLT entry's pointer to the .got.plt entry.  */
6664           rel.r_offset = (htab->splt->output_section->vma
6665                           + htab->splt->output_offset
6666                           + h->plt.offset
6667                           + plt_info->symbol_fields.got_entry);
6668           rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6669           rel.r_addend = got_offset;
6670           bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6671           loc += sizeof (Elf32_External_Rela);
6672
6673           /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6674              the .got.plt entry, which initially points to .plt.  */
6675           rel.r_offset = (sgotplt->output_section->vma
6676                           + sgotplt->output_offset
6677                           + got_offset);
6678           rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
6679           rel.r_addend = 0;
6680           bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6681         }
6682
6683       if (!h->def_regular)
6684         {
6685           /* Mark the symbol as undefined, rather than as defined in
6686              the .plt section.  Leave the value alone.  */
6687           sym->st_shndx = SHN_UNDEF;
6688         }
6689     }
6690
6691   if (h->got.offset != (bfd_vma) -1
6692       && sh_elf_hash_entry (h)->got_type != GOT_TLS_GD
6693       && sh_elf_hash_entry (h)->got_type != GOT_TLS_IE
6694       && sh_elf_hash_entry (h)->got_type != GOT_FUNCDESC)
6695     {
6696       asection *sgot;
6697       asection *srelgot;
6698       Elf_Internal_Rela rel;
6699       bfd_byte *loc;
6700
6701       /* This symbol has an entry in the global offset table.  Set it
6702          up.  */
6703
6704       sgot = htab->sgot;
6705       srelgot = htab->srelgot;
6706       BFD_ASSERT (sgot != NULL && srelgot != NULL);
6707
6708       rel.r_offset = (sgot->output_section->vma
6709                       + sgot->output_offset
6710                       + (h->got.offset &~ (bfd_vma) 1));
6711
6712       /* If this is a static link, or it is a -Bsymbolic link and the
6713          symbol is defined locally or was forced to be local because
6714          of a version file, we just want to emit a RELATIVE reloc.
6715          The entry in the global offset table will already have been
6716          initialized in the relocate_section function.  */
6717       if (bfd_link_pic (info)
6718           && SYMBOL_REFERENCES_LOCAL (info, h))
6719         {
6720           if (htab->fdpic_p)
6721             {
6722               asection *sec = h->root.u.def.section;
6723               int dynindx
6724                 = elf_section_data (sec->output_section)->dynindx;
6725
6726               rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
6727               rel.r_addend = (h->root.u.def.value
6728                               + h->root.u.def.section->output_offset);
6729             }
6730           else
6731             {
6732               rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6733               rel.r_addend = (h->root.u.def.value
6734                               + h->root.u.def.section->output_section->vma
6735                               + h->root.u.def.section->output_offset);
6736             }
6737         }
6738       else
6739         {
6740           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
6741           rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6742           rel.r_addend = 0;
6743         }
6744
6745       loc = srelgot->contents;
6746       loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
6747       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6748     }
6749
6750 #ifdef INCLUDE_SHMEDIA
6751   {
6752     struct elf_sh_link_hash_entry *eh;
6753
6754     eh = (struct elf_sh_link_hash_entry *) h;
6755     if (eh->datalabel_got.offset != (bfd_vma) -1)
6756       {
6757         asection *sgot;
6758         asection *srelgot;
6759         Elf_Internal_Rela rel;
6760         bfd_byte *loc;
6761
6762         /* This symbol has a datalabel entry in the global offset table.
6763            Set it up.  */
6764
6765         sgot = htab->sgot;
6766         srelgot = htab->srelgot;
6767         BFD_ASSERT (sgot != NULL && srelgot != NULL);
6768
6769         rel.r_offset = (sgot->output_section->vma
6770                         + sgot->output_offset
6771                         + (eh->datalabel_got.offset &~ (bfd_vma) 1));
6772
6773         /* If this is a static link, or it is a -Bsymbolic link and the
6774            symbol is defined locally or was forced to be local because
6775            of a version file, we just want to emit a RELATIVE reloc.
6776            The entry in the global offset table will already have been
6777            initialized in the relocate_section function.  */
6778         if (bfd_link_pic (info)
6779             && SYMBOL_REFERENCES_LOCAL (info, h))
6780           {
6781             if (htab->fdpic_p)
6782               {
6783                 asection *sec = h->root.u.def.section;
6784                 int dynindx
6785                   = elf_section_data (sec->output_section)->dynindx;
6786
6787                 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
6788                 rel.r_addend = (h->root.u.def.value
6789                                 + h->root.u.def.section->output_offset);
6790               }
6791             else
6792               {
6793                 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6794                 rel.r_addend = (h->root.u.def.value
6795                                 + h->root.u.def.section->output_section->vma
6796                                 + h->root.u.def.section->output_offset);
6797               }
6798           }
6799         else
6800           {
6801             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
6802                         + eh->datalabel_got.offset);
6803             rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6804             rel.r_addend = 0;
6805           }
6806
6807         loc = srelgot->contents;
6808         loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
6809         bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6810       }
6811   }
6812 #endif
6813
6814   if (h->needs_copy)
6815     {
6816       asection *s;
6817       Elf_Internal_Rela rel;
6818       bfd_byte *loc;
6819
6820       /* This symbol needs a copy reloc.  Set it up.  */
6821
6822       BFD_ASSERT (h->dynindx != -1
6823                   && (h->root.type == bfd_link_hash_defined
6824                       || h->root.type == bfd_link_hash_defweak));
6825
6826       s = bfd_get_linker_section (htab->root.dynobj, ".rela.bss");
6827       BFD_ASSERT (s != NULL);
6828
6829       rel.r_offset = (h->root.u.def.value
6830                       + h->root.u.def.section->output_section->vma
6831                       + h->root.u.def.section->output_offset);
6832       rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
6833       rel.r_addend = 0;
6834       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6835       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6836     }
6837
6838   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
6839      _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
6840      ".got" section.  */
6841   if (h == htab->root.hdynamic
6842       || (!htab->vxworks_p && h == htab->root.hgot))
6843     sym->st_shndx = SHN_ABS;
6844
6845   return TRUE;
6846 }
6847
6848 /* Finish up the dynamic sections.  */
6849
6850 static bfd_boolean
6851 sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6852 {
6853   struct elf_sh_link_hash_table *htab;
6854   asection *sgotplt;
6855   asection *sdyn;
6856
6857   htab = sh_elf_hash_table (info);
6858   if (htab == NULL)
6859     return FALSE;
6860
6861   sgotplt = htab->sgotplt;
6862   sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
6863
6864   if (htab->root.dynamic_sections_created)
6865     {
6866       asection *splt;
6867       Elf32_External_Dyn *dyncon, *dynconend;
6868
6869       BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6870
6871       dyncon = (Elf32_External_Dyn *) sdyn->contents;
6872       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6873       for (; dyncon < dynconend; dyncon++)
6874         {
6875           Elf_Internal_Dyn dyn;
6876           asection *s;
6877 #ifdef INCLUDE_SHMEDIA
6878           const char *name;
6879 #endif
6880
6881           bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
6882
6883           switch (dyn.d_tag)
6884             {
6885             default:
6886               if (htab->vxworks_p
6887                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
6888                 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6889               break;
6890
6891 #ifdef INCLUDE_SHMEDIA
6892             case DT_INIT:
6893               name = info->init_function;
6894               goto get_sym;
6895
6896             case DT_FINI:
6897               name = info->fini_function;
6898             get_sym:
6899               if (dyn.d_un.d_val != 0)
6900                 {
6901                   struct elf_link_hash_entry *h;
6902
6903                   h = elf_link_hash_lookup (&htab->root, name,
6904                                             FALSE, FALSE, TRUE);
6905                   if (h != NULL && (h->other & STO_SH5_ISA32))
6906                     {
6907                       dyn.d_un.d_val |= 1;
6908                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6909                     }
6910                 }
6911               break;
6912 #endif
6913
6914             case DT_PLTGOT:
6915               BFD_ASSERT (htab->root.hgot != NULL);
6916               s = htab->root.hgot->root.u.def.section;
6917               dyn.d_un.d_ptr = htab->root.hgot->root.u.def.value
6918                 + s->output_section->vma + s->output_offset;
6919               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6920               break;
6921
6922             case DT_JMPREL:
6923               s = htab->srelplt->output_section;
6924               BFD_ASSERT (s != NULL);
6925               dyn.d_un.d_ptr = s->vma;
6926               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6927               break;
6928
6929             case DT_PLTRELSZ:
6930               s = htab->srelplt->output_section;
6931               BFD_ASSERT (s != NULL);
6932               dyn.d_un.d_val = s->size;
6933               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6934               break;
6935
6936             case DT_RELASZ:
6937               /* My reading of the SVR4 ABI indicates that the
6938                  procedure linkage table relocs (DT_JMPREL) should be
6939                  included in the overall relocs (DT_RELA).  This is
6940                  what Solaris does.  However, UnixWare can not handle
6941                  that case.  Therefore, we override the DT_RELASZ entry
6942                  here to make it not include the JMPREL relocs.  Since
6943                  the linker script arranges for .rela.plt to follow all
6944                  other relocation sections, we don't have to worry
6945                  about changing the DT_RELA entry.  */
6946               if (htab->srelplt != NULL)
6947                 {
6948                   s = htab->srelplt->output_section;
6949                   dyn.d_un.d_val -= s->size;
6950                 }
6951               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6952               break;
6953             }
6954         }
6955
6956       /* Fill in the first entry in the procedure linkage table.  */
6957       splt = htab->splt;
6958       if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
6959         {
6960           unsigned int i;
6961
6962           memcpy (splt->contents,
6963                   htab->plt_info->plt0_entry,
6964                   htab->plt_info->plt0_entry_size);
6965           for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
6966             if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
6967               install_plt_field (output_bfd, FALSE,
6968                                  (sgotplt->output_section->vma
6969                                   + sgotplt->output_offset
6970                                   + (i * 4)),
6971                                  (splt->contents
6972                                   + htab->plt_info->plt0_got_fields[i]));
6973
6974           if (htab->vxworks_p)
6975             {
6976               /* Finalize the .rela.plt.unloaded contents.  */
6977               Elf_Internal_Rela rel;
6978               bfd_byte *loc;
6979
6980               /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
6981                  first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8.  */
6982               loc = htab->srelplt2->contents;
6983               rel.r_offset = (splt->output_section->vma
6984                               + splt->output_offset
6985                               + htab->plt_info->plt0_got_fields[2]);
6986               rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6987               rel.r_addend = 8;
6988               bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6989               loc += sizeof (Elf32_External_Rela);
6990
6991               /* Fix up the remaining .rela.plt.unloaded relocations.
6992                  They may have the wrong symbol index for _G_O_T_ or
6993                  _P_L_T_ depending on the order in which symbols were
6994                  output.  */
6995               while (loc < htab->srelplt2->contents + htab->srelplt2->size)
6996                 {
6997                   /* The PLT entry's pointer to the .got.plt slot.  */
6998                   bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
6999                   rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
7000                                              R_SH_DIR32);
7001                   bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7002                   loc += sizeof (Elf32_External_Rela);
7003
7004                   /* The .got.plt slot's pointer to .plt.  */
7005                   bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7006                   rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
7007                                              R_SH_DIR32);
7008                   bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7009                   loc += sizeof (Elf32_External_Rela);
7010                 }
7011             }
7012
7013           /* UnixWare sets the entsize of .plt to 4, although that doesn't
7014              really seem like the right value.  */
7015           elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
7016         }
7017     }
7018
7019   /* Fill in the first three entries in the global offset table.  */
7020   if (sgotplt && sgotplt->size > 0 && !htab->fdpic_p)
7021     {
7022       if (sdyn == NULL)
7023         bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
7024       else
7025         bfd_put_32 (output_bfd,
7026                     sdyn->output_section->vma + sdyn->output_offset,
7027                     sgotplt->contents);
7028       bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
7029       bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
7030     }
7031
7032   if (sgotplt && sgotplt->size > 0)
7033     elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
7034
7035   /* At the very end of the .rofixup section is a pointer to the GOT.  */
7036   if (htab->fdpic_p && htab->srofixup != NULL)
7037     {
7038       struct elf_link_hash_entry *hgot = htab->root.hgot;
7039       bfd_vma got_value = hgot->root.u.def.value
7040         + hgot->root.u.def.section->output_section->vma
7041         + hgot->root.u.def.section->output_offset;
7042
7043       sh_elf_add_rofixup (output_bfd, htab->srofixup, got_value);
7044
7045       /* Make sure we allocated and generated the same number of fixups.  */
7046       BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
7047     }
7048
7049   if (htab->srelfuncdesc)
7050     BFD_ASSERT (htab->srelfuncdesc->reloc_count * sizeof (Elf32_External_Rela)
7051                 == htab->srelfuncdesc->size);
7052
7053   if (htab->srelgot)
7054     BFD_ASSERT (htab->srelgot->reloc_count * sizeof (Elf32_External_Rela)
7055                 == htab->srelgot->size);
7056
7057   return TRUE;
7058 }
7059
7060 static enum elf_reloc_type_class
7061 sh_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
7062                          const asection *rel_sec ATTRIBUTE_UNUSED,
7063                          const Elf_Internal_Rela *rela)
7064 {
7065   switch ((int) ELF32_R_TYPE (rela->r_info))
7066     {
7067     case R_SH_RELATIVE:
7068       return reloc_class_relative;
7069     case R_SH_JMP_SLOT:
7070       return reloc_class_plt;
7071     case R_SH_COPY:
7072       return reloc_class_copy;
7073     default:
7074       return reloc_class_normal;
7075     }
7076 }
7077
7078 #if !defined SH_TARGET_ALREADY_DEFINED
7079 /* Support for Linux core dump NOTE sections.  */
7080
7081 static bfd_boolean
7082 elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7083 {
7084   int offset;
7085   unsigned int size;
7086
7087   switch (note->descsz)
7088     {
7089       default:
7090         return FALSE;
7091
7092       case 168:         /* Linux/SH */
7093         /* pr_cursig */
7094         elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
7095
7096         /* pr_pid */
7097         elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
7098
7099         /* pr_reg */
7100         offset = 72;
7101         size = 92;
7102
7103         break;
7104     }
7105
7106   /* Make a ".reg/999" section.  */
7107   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7108                                           size, note->descpos + offset);
7109 }
7110
7111 static bfd_boolean
7112 elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7113 {
7114   switch (note->descsz)
7115     {
7116       default:
7117         return FALSE;
7118
7119       case 124:         /* Linux/SH elf_prpsinfo */
7120         elf_tdata (abfd)->core->program
7121          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
7122         elf_tdata (abfd)->core->command
7123          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
7124     }
7125
7126   /* Note that for some reason, a spurious space is tacked
7127      onto the end of the args in some (at least one anyway)
7128      implementations, so strip it off if it exists.  */
7129
7130   {
7131     char *command = elf_tdata (abfd)->core->command;
7132     int n = strlen (command);
7133
7134     if (0 < n && command[n - 1] == ' ')
7135       command[n - 1] = '\0';
7136   }
7137
7138   return TRUE;
7139 }
7140 #endif /* not SH_TARGET_ALREADY_DEFINED */
7141
7142
7143 /* Return address for Ith PLT stub in section PLT, for relocation REL
7144    or (bfd_vma) -1 if it should not be included.  */
7145
7146 static bfd_vma
7147 sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
7148                     const arelent *rel ATTRIBUTE_UNUSED)
7149 {
7150   const struct elf_sh_plt_info *plt_info;
7151
7152   plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
7153   return plt->vma + get_plt_offset (plt_info, i);
7154 }
7155
7156 /* Decide whether to attempt to turn absptr or lsda encodings in
7157    shared libraries into pcrel within the given input section.  */
7158
7159 static bfd_boolean
7160 sh_elf_use_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
7161                               struct bfd_link_info *info,
7162                               asection *eh_frame_section ATTRIBUTE_UNUSED)
7163 {
7164   struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7165
7166   /* We can't use PC-relative encodings in FDPIC binaries, in general.  */
7167   if (htab->fdpic_p)
7168     return FALSE;
7169
7170   return TRUE;
7171 }
7172
7173 /* Adjust the contents of an eh_frame_hdr section before they're output.  */
7174
7175 static bfd_byte
7176 sh_elf_encode_eh_address (bfd *abfd,
7177                           struct bfd_link_info *info,
7178                           asection *osec, bfd_vma offset,
7179                           asection *loc_sec, bfd_vma loc_offset,
7180                           bfd_vma *encoded)
7181 {
7182   struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7183   struct elf_link_hash_entry *h;
7184
7185   if (!htab->fdpic_p)
7186     return _bfd_elf_encode_eh_address (abfd, info, osec, offset, loc_sec,
7187                                        loc_offset, encoded);
7188
7189   h = htab->root.hgot;
7190   BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
7191
7192   if (! h || (sh_elf_osec_to_segment (abfd, osec)
7193               == sh_elf_osec_to_segment (abfd, loc_sec->output_section)))
7194     return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
7195                                        loc_sec, loc_offset, encoded);
7196
7197   BFD_ASSERT (sh_elf_osec_to_segment (abfd, osec)
7198               == (sh_elf_osec_to_segment
7199                   (abfd, h->root.u.def.section->output_section)));
7200
7201   *encoded = osec->vma + offset
7202     - (h->root.u.def.value
7203        + h->root.u.def.section->output_section->vma
7204        + h->root.u.def.section->output_offset);
7205
7206   return DW_EH_PE_datarel | DW_EH_PE_sdata4;
7207 }
7208
7209 #if !defined SH_TARGET_ALREADY_DEFINED
7210 #define TARGET_BIG_SYM          sh_elf32_vec
7211 #define TARGET_BIG_NAME         "elf32-sh"
7212 #define TARGET_LITTLE_SYM       sh_elf32_le_vec
7213 #define TARGET_LITTLE_NAME      "elf32-shl"
7214 #endif
7215
7216 #define ELF_ARCH                bfd_arch_sh
7217 #define ELF_TARGET_ID           SH_ELF_DATA
7218 #define ELF_MACHINE_CODE        EM_SH
7219 #ifdef __QNXTARGET__
7220 #define ELF_MAXPAGESIZE         0x1000
7221 #else
7222 #define ELF_MAXPAGESIZE         0x80
7223 #endif
7224
7225 #define elf_symbol_leading_char '_'
7226
7227 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
7228 #define bfd_elf32_bfd_reloc_name_lookup \
7229                                         sh_elf_reloc_name_lookup
7230 #define elf_info_to_howto               sh_elf_info_to_howto
7231 #define bfd_elf32_bfd_relax_section     sh_elf_relax_section
7232 #define elf_backend_relocate_section    sh_elf_relocate_section
7233 #define bfd_elf32_bfd_get_relocated_section_contents \
7234                                         sh_elf_get_relocated_section_contents
7235 #define bfd_elf32_mkobject              sh_elf_mkobject
7236 #define elf_backend_object_p            sh_elf_object_p
7237 #define bfd_elf32_bfd_copy_private_bfd_data \
7238                                         sh_elf_copy_private_data
7239 #define bfd_elf32_bfd_merge_private_bfd_data \
7240                                         sh_elf_merge_private_data
7241
7242 #define elf_backend_gc_mark_hook        sh_elf_gc_mark_hook
7243 #define elf_backend_check_relocs        sh_elf_check_relocs
7244 #define elf_backend_copy_indirect_symbol \
7245                                         sh_elf_copy_indirect_symbol
7246 #define elf_backend_create_dynamic_sections \
7247                                         sh_elf_create_dynamic_sections
7248 #define bfd_elf32_bfd_link_hash_table_create \
7249                                         sh_elf_link_hash_table_create
7250 #define elf_backend_adjust_dynamic_symbol \
7251                                         sh_elf_adjust_dynamic_symbol
7252 #define elf_backend_always_size_sections \
7253                                         sh_elf_always_size_sections
7254 #define elf_backend_size_dynamic_sections \
7255                                         sh_elf_size_dynamic_sections
7256 #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
7257 #define elf_backend_finish_dynamic_symbol \
7258                                         sh_elf_finish_dynamic_symbol
7259 #define elf_backend_finish_dynamic_sections \
7260                                         sh_elf_finish_dynamic_sections
7261 #define elf_backend_reloc_type_class    sh_elf_reloc_type_class
7262 #define elf_backend_plt_sym_val         sh_elf_plt_sym_val
7263 #define elf_backend_can_make_relative_eh_frame \
7264                                         sh_elf_use_relative_eh_frame
7265 #define elf_backend_can_make_lsda_relative_eh_frame \
7266                                         sh_elf_use_relative_eh_frame
7267 #define elf_backend_encode_eh_address \
7268                                         sh_elf_encode_eh_address
7269
7270 #define elf_backend_stack_align         8
7271 #define elf_backend_can_gc_sections     1
7272 #define elf_backend_can_refcount        1
7273 #define elf_backend_want_got_plt        1
7274 #define elf_backend_plt_readonly        1
7275 #define elf_backend_want_plt_sym        0
7276 #define elf_backend_got_header_size     12
7277
7278 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
7279
7280 #include "elf32-target.h"
7281
7282 /* NetBSD support.  */
7283 #undef  TARGET_BIG_SYM
7284 #define TARGET_BIG_SYM                  sh_elf32_nbsd_vec
7285 #undef  TARGET_BIG_NAME
7286 #define TARGET_BIG_NAME                 "elf32-sh-nbsd"
7287 #undef  TARGET_LITTLE_SYM
7288 #define TARGET_LITTLE_SYM               sh_elf32_nbsd_le_vec
7289 #undef  TARGET_LITTLE_NAME
7290 #define TARGET_LITTLE_NAME              "elf32-shl-nbsd"
7291 #undef  ELF_MAXPAGESIZE
7292 #define ELF_MAXPAGESIZE                 0x10000
7293 #undef  ELF_COMMONPAGESIZE
7294 #undef  elf_symbol_leading_char
7295 #define elf_symbol_leading_char         0
7296 #undef  elf32_bed
7297 #define elf32_bed                       elf32_sh_nbsd_bed
7298
7299 #include "elf32-target.h"
7300
7301
7302 /* Linux support.  */
7303 #undef  TARGET_BIG_SYM
7304 #define TARGET_BIG_SYM                  sh_elf32_linux_be_vec
7305 #undef  TARGET_BIG_NAME
7306 #define TARGET_BIG_NAME                 "elf32-shbig-linux"
7307 #undef  TARGET_LITTLE_SYM
7308 #define TARGET_LITTLE_SYM               sh_elf32_linux_vec
7309 #undef  TARGET_LITTLE_NAME
7310 #define TARGET_LITTLE_NAME              "elf32-sh-linux"
7311 #undef  ELF_COMMONPAGESIZE
7312 #define ELF_COMMONPAGESIZE              0x1000
7313
7314 #undef  elf_backend_grok_prstatus
7315 #define elf_backend_grok_prstatus       elf32_shlin_grok_prstatus
7316 #undef  elf_backend_grok_psinfo
7317 #define elf_backend_grok_psinfo         elf32_shlin_grok_psinfo
7318 #undef  elf32_bed
7319 #define elf32_bed                       elf32_sh_lin_bed
7320
7321 #include "elf32-target.h"
7322
7323
7324 /* FDPIC support.  */
7325 #undef  TARGET_BIG_SYM
7326 #define TARGET_BIG_SYM                  sh_elf32_fdpic_be_vec
7327 #undef  TARGET_BIG_NAME
7328 #define TARGET_BIG_NAME                 "elf32-shbig-fdpic"
7329 #undef  TARGET_LITTLE_SYM
7330 #define TARGET_LITTLE_SYM               sh_elf32_fdpic_le_vec
7331 #undef  TARGET_LITTLE_NAME
7332 #define TARGET_LITTLE_NAME              "elf32-sh-fdpic"
7333
7334 #undef  elf32_bed
7335 #define elf32_bed                       elf32_sh_fd_bed
7336
7337 #include "elf32-target.h"
7338
7339 #undef elf_backend_modify_program_headers
7340
7341 /* VxWorks support.  */
7342 #undef  TARGET_BIG_SYM
7343 #define TARGET_BIG_SYM                  sh_elf32_vxworks_vec
7344 #undef  TARGET_BIG_NAME
7345 #define TARGET_BIG_NAME                 "elf32-sh-vxworks"
7346 #undef  TARGET_LITTLE_SYM
7347 #define TARGET_LITTLE_SYM               sh_elf32_vxworks_le_vec
7348 #undef  TARGET_LITTLE_NAME
7349 #define TARGET_LITTLE_NAME              "elf32-shl-vxworks"
7350 #undef  elf32_bed
7351 #define elf32_bed                       elf32_sh_vxworks_bed
7352
7353 #undef  elf_backend_want_plt_sym
7354 #define elf_backend_want_plt_sym        1
7355 #undef  elf_symbol_leading_char
7356 #define elf_symbol_leading_char         '_'
7357 #define elf_backend_want_got_underscore 1
7358 #undef  elf_backend_grok_prstatus
7359 #undef  elf_backend_grok_psinfo
7360 #undef  elf_backend_add_symbol_hook
7361 #define elf_backend_add_symbol_hook     elf_vxworks_add_symbol_hook
7362 #undef  elf_backend_link_output_symbol_hook
7363 #define elf_backend_link_output_symbol_hook \
7364                                         elf_vxworks_link_output_symbol_hook
7365 #undef  elf_backend_emit_relocs
7366 #define elf_backend_emit_relocs         elf_vxworks_emit_relocs
7367 #undef  elf_backend_final_write_processing
7368 #define elf_backend_final_write_processing \
7369                                         elf_vxworks_final_write_processing
7370 #undef  ELF_MAXPAGESIZE
7371 #define ELF_MAXPAGESIZE                 0x1000
7372 #undef  ELF_COMMONPAGESIZE
7373
7374 #include "elf32-target.h"
7375
7376 #endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */