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