OSDN Git Service

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