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