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