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