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