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