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