OSDN Git Service

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