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