* elf32-sh.c (elf_sh_pic_plt_entry_be, elf_sh_pic_plt_entry_le):
[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, 0xc8, 0x45, 0x10, /* add.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, 0xc8, 0x00, /* add.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   bfd_signed_vma gotplt_refcount;
3393 };
3394
3395 /* sh ELF linker hash table.  */
3396
3397 struct elf_sh_link_hash_table
3398 {
3399   struct elf_link_hash_table root;
3400
3401   /* Short-cuts to get to dynamic linker sections.  */
3402   asection *sgot;
3403   asection *sgotplt;
3404   asection *srelgot;
3405   asection *splt;
3406   asection *srelplt;
3407   asection *sdynbss;
3408   asection *srelbss;
3409
3410   /* Small local sym to section mapping cache.  */
3411   struct sym_sec_cache sym_sec;
3412 };
3413
3414 /* Traverse an sh ELF linker hash table.  */
3415
3416 #define sh_elf_link_hash_traverse(table, func, info)                    \
3417   (elf_link_hash_traverse                                               \
3418    (&(table)->root,                                                     \
3419     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
3420     (info)))
3421
3422 /* Get the sh ELF linker hash table from a link_info structure.  */
3423
3424 #define sh_elf_hash_table(p) \
3425   ((struct elf_sh_link_hash_table *) ((p)->hash))
3426
3427 /* Create an entry in an sh ELF linker hash table.  */
3428
3429 static struct bfd_hash_entry *
3430 sh_elf_link_hash_newfunc (entry, table, string)
3431      struct bfd_hash_entry *entry;
3432      struct bfd_hash_table *table;
3433      const char *string;
3434 {
3435   struct elf_sh_link_hash_entry *ret =
3436     (struct elf_sh_link_hash_entry *) entry;
3437
3438   /* Allocate the structure if it has not already been allocated by a
3439      subclass.  */
3440   if (ret == (struct elf_sh_link_hash_entry *) NULL)
3441     ret = ((struct elf_sh_link_hash_entry *)
3442            bfd_hash_allocate (table,
3443                               sizeof (struct elf_sh_link_hash_entry)));
3444   if (ret == (struct elf_sh_link_hash_entry *) NULL)
3445     return (struct bfd_hash_entry *) ret;
3446
3447   /* Call the allocation method of the superclass.  */
3448   ret = ((struct elf_sh_link_hash_entry *)
3449          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3450                                      table, string));
3451   if (ret != (struct elf_sh_link_hash_entry *) NULL)
3452     {
3453       struct elf_sh_link_hash_entry *eh;
3454
3455       eh = (struct elf_sh_link_hash_entry *) ret;
3456       eh->dyn_relocs = NULL;
3457       eh->gotplt_refcount = 0;
3458 #ifdef INCLUDE_SHMEDIA
3459       ret->datalabel_got_offset = (bfd_vma) -1;
3460 #endif
3461     }
3462
3463   return (struct bfd_hash_entry *) ret;
3464 }
3465
3466 /* Create an sh ELF linker hash table.  */
3467
3468 static struct bfd_link_hash_table *
3469 sh_elf_link_hash_table_create (abfd)
3470      bfd *abfd;
3471 {
3472   struct elf_sh_link_hash_table *ret;
3473   bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
3474
3475   ret = (struct elf_sh_link_hash_table *) bfd_malloc (amt);
3476   if (ret == (struct elf_sh_link_hash_table *) NULL)
3477     return NULL;
3478
3479   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3480                                        sh_elf_link_hash_newfunc))
3481     {
3482       free (ret);
3483       return NULL;
3484     }
3485
3486   ret->sgot = NULL;
3487   ret->sgotplt = NULL;
3488   ret->srelgot = NULL;
3489   ret->splt = NULL;
3490   ret->srelplt = NULL;
3491   ret->sdynbss = NULL;
3492   ret->srelbss = NULL;
3493   ret->sym_sec.abfd = NULL;
3494
3495   return &ret->root.root;
3496 }
3497
3498 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3499    shortcuts to them in our hash table.  */
3500
3501 static boolean
3502 create_got_section (dynobj, info)
3503      bfd *dynobj;
3504      struct bfd_link_info *info;
3505 {
3506   struct elf_sh_link_hash_table *htab;
3507
3508   if (! _bfd_elf_create_got_section (dynobj, info))
3509     return false;
3510
3511   htab = sh_elf_hash_table (info);
3512   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
3513   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3514   if (! htab->sgot || ! htab->sgotplt)
3515     abort ();
3516
3517   htab->srelgot = bfd_make_section (dynobj, ".rela.got");
3518   if (htab->srelgot == NULL
3519       || ! bfd_set_section_flags (dynobj, htab->srelgot,
3520                                   (SEC_ALLOC
3521                                    | SEC_LOAD
3522                                    | SEC_HAS_CONTENTS
3523                                    | SEC_IN_MEMORY
3524                                    | SEC_LINKER_CREATED
3525                                    | SEC_READONLY))
3526       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
3527     return false;
3528   return true;
3529 }
3530
3531 /* Create dynamic sections when linking against a dynamic object.  */
3532
3533 static boolean
3534 sh_elf_create_dynamic_sections (abfd, info)
3535      bfd *abfd;
3536      struct bfd_link_info *info;
3537 {
3538   struct elf_sh_link_hash_table *htab;
3539   flagword flags, pltflags;
3540   register asection *s;
3541   struct elf_backend_data *bed = get_elf_backend_data (abfd);
3542   int ptralign = 0;
3543
3544   switch (bed->s->arch_size)
3545     {
3546     case 32:
3547       ptralign = 2;
3548       break;
3549
3550     case 64:
3551       ptralign = 3;
3552       break;
3553
3554     default:
3555       bfd_set_error (bfd_error_bad_value);
3556       return false;
3557     }
3558
3559   htab = sh_elf_hash_table (info);
3560
3561   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3562      .rel[a].bss sections.  */
3563
3564   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3565            | SEC_LINKER_CREATED);
3566
3567   pltflags = flags;
3568   pltflags |= SEC_CODE;
3569   if (bed->plt_not_loaded)
3570     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
3571   if (bed->plt_readonly)
3572     pltflags |= SEC_READONLY;
3573
3574   s = bfd_make_section (abfd, ".plt");
3575   htab->splt = s;
3576   if (s == NULL
3577       || ! bfd_set_section_flags (abfd, s, pltflags)
3578       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3579     return false;
3580
3581   if (bed->want_plt_sym)
3582     {
3583       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3584          .plt section.  */
3585       struct elf_link_hash_entry *h = NULL;
3586       if (! (_bfd_generic_link_add_one_symbol
3587              (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3588               (bfd_vma) 0, (const char *) NULL, false,
3589               get_elf_backend_data (abfd)->collect,
3590               (struct bfd_link_hash_entry **) &h)))
3591         return false;
3592       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3593       h->type = STT_OBJECT;
3594
3595       if (info->shared
3596           && ! _bfd_elf_link_record_dynamic_symbol (info, h))
3597         return false;
3598     }
3599
3600   s = bfd_make_section (abfd,
3601                         bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3602   htab->srelplt = s;
3603   if (s == NULL
3604       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3605       || ! bfd_set_section_alignment (abfd, s, ptralign))
3606     return false;
3607
3608   if (! create_got_section (abfd, info))
3609     return false;
3610
3611   {
3612     const char *secname;
3613     char *relname;
3614     flagword secflags;
3615     asection *sec;
3616
3617     for (sec = abfd->sections; sec; sec = sec->next)
3618       {
3619         secflags = bfd_get_section_flags (abfd, sec);
3620         if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3621             || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3622           continue;
3623         secname = bfd_get_section_name (abfd, sec);
3624         relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3625         strcpy (relname, ".rela");
3626         strcat (relname, secname);
3627         s = bfd_make_section (abfd, relname);
3628         if (s == NULL
3629             || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3630             || ! bfd_set_section_alignment (abfd, s, ptralign))
3631           return false;
3632       }
3633   }
3634
3635   if (bed->want_dynbss)
3636     {
3637       /* The .dynbss section is a place to put symbols which are defined
3638          by dynamic objects, are referenced by regular objects, and are
3639          not functions.  We must allocate space for them in the process
3640          image and use a R_*_COPY reloc to tell the dynamic linker to
3641          initialize them at run time.  The linker script puts the .dynbss
3642          section into the .bss section of the final image.  */
3643       s = bfd_make_section (abfd, ".dynbss");
3644       htab->sdynbss = s;
3645       if (s == NULL
3646           || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
3647         return false;
3648
3649       /* The .rel[a].bss section holds copy relocs.  This section is not
3650          normally needed.  We need to create it here, though, so that the
3651          linker will map it to an output section.  We can't just create it
3652          only if we need it, because we will not know whether we need it
3653          until we have seen all the input files, and the first time the
3654          main linker code calls BFD after examining all the input files
3655          (size_dynamic_sections) the input sections have already been
3656          mapped to the output sections.  If the section turns out not to
3657          be needed, we can discard it later.  We will never need this
3658          section when generating a shared object, since they do not use
3659          copy relocs.  */
3660       if (! info->shared)
3661         {
3662           s = bfd_make_section (abfd,
3663                                 (bed->default_use_rela_p
3664                                  ? ".rela.bss" : ".rel.bss"));
3665           htab->srelbss = s;
3666           if (s == NULL
3667               || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3668               || ! bfd_set_section_alignment (abfd, s, ptralign))
3669             return false;
3670         }
3671     }
3672
3673   return true;
3674 }
3675 \f
3676 /* Adjust a symbol defined by a dynamic object and referenced by a
3677    regular object.  The current definition is in some section of the
3678    dynamic object, but we're not including those sections.  We have to
3679    change the definition to something the rest of the link can
3680    understand.  */
3681
3682 static boolean
3683 sh_elf_adjust_dynamic_symbol (info, h)
3684      struct bfd_link_info *info;
3685      struct elf_link_hash_entry *h;
3686 {
3687   struct elf_sh_link_hash_table *htab;
3688   struct elf_sh_link_hash_entry *eh;
3689   struct elf_sh_dyn_relocs *p;
3690   bfd *dynobj;
3691   asection *s;
3692   unsigned int power_of_two;
3693
3694   dynobj = elf_hash_table (info)->dynobj;
3695
3696   /* Make sure we know what is going on here.  */
3697   BFD_ASSERT (dynobj != NULL
3698               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
3699                   || h->weakdef != NULL
3700                   || ((h->elf_link_hash_flags
3701                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3702                       && (h->elf_link_hash_flags
3703                           & ELF_LINK_HASH_REF_REGULAR) != 0
3704                       && (h->elf_link_hash_flags
3705                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
3706
3707   /* If this is a function, put it in the procedure linkage table.  We
3708      will fill in the contents of the procedure linkage table later,
3709      when we know the address of the .got section.  */
3710   if (h->type == STT_FUNC
3711       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3712     {
3713       if (h->plt.refcount <= 0
3714           || (! info->shared
3715               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3716               && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
3717               && h->root.type != bfd_link_hash_undefweak
3718               && h->root.type != bfd_link_hash_undefined))
3719         {
3720           /* This case can occur if we saw a PLT reloc in an input
3721              file, but the symbol was never referred to by a dynamic
3722              object.  In such a case, we don't actually need to build
3723              a procedure linkage table, and we can just do a REL32
3724              reloc instead.  */
3725           h->plt.offset = (bfd_vma) -1;
3726           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3727         }
3728
3729       return true;
3730     }
3731   else
3732     h->plt.offset = (bfd_vma) -1;
3733
3734   /* If this is a weak symbol, and there is a real definition, the
3735      processor independent code will have arranged for us to see the
3736      real definition first, and we can just use the same value.  */
3737   if (h->weakdef != NULL)
3738     {
3739       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3740                   || h->weakdef->root.type == bfd_link_hash_defweak);
3741       h->root.u.def.section = h->weakdef->root.u.def.section;
3742       h->root.u.def.value = h->weakdef->root.u.def.value;
3743       return true;
3744     }
3745
3746   /* This is a reference to a symbol defined by a dynamic object which
3747      is not a function.  */
3748
3749   /* If we are creating a shared library, we must presume that the
3750      only references to the symbol are via the global offset table.
3751      For such cases we need not do anything here; the relocations will
3752      be handled correctly by relocate_section.  */
3753   if (info->shared)
3754     return true;
3755
3756   /* If there are no references to this symbol that do not use the
3757      GOT, we don't need to generate a copy reloc.  */
3758   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
3759     return true;
3760
3761   /* If -z nocopyreloc was given, we won't generate them either.  */
3762   if (info->nocopyreloc)
3763     {
3764       h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3765       return true;
3766     }
3767
3768   eh = (struct elf_sh_link_hash_entry *) h;
3769   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3770     {
3771       s = p->sec->output_section;
3772       if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
3773         break;
3774     }
3775
3776   /* If we didn't find any dynamic relocs in sections which needs the
3777      copy reloc, then we'll be keeping the dynamic relocs and avoiding
3778      the copy reloc.  */
3779   if (p == NULL)
3780     {
3781       h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3782       return true;
3783     }
3784
3785   /* We must allocate the symbol in our .dynbss section, which will
3786      become part of the .bss section of the executable.  There will be
3787      an entry for this symbol in the .dynsym section.  The dynamic
3788      object will contain position independent code, so all references
3789      from the dynamic object to this symbol will go through the global
3790      offset table.  The dynamic linker will use the .dynsym entry to
3791      determine the address it must put in the global offset table, so
3792      both the dynamic object and the regular object will refer to the
3793      same memory location for the variable.  */
3794
3795   htab = sh_elf_hash_table (info);
3796   s = htab->sdynbss;
3797   BFD_ASSERT (s != NULL);
3798
3799   /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
3800      copy the initial value out of the dynamic object and into the
3801      runtime process image.  We need to remember the offset into the
3802      .rela.bss section we are going to use.  */
3803   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3804     {
3805       asection *srel;
3806
3807       srel = htab->srelbss;
3808       BFD_ASSERT (srel != NULL);
3809       srel->_raw_size += sizeof (Elf32_External_Rela);
3810       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
3811     }
3812
3813   /* We need to figure out the alignment required for this symbol.  I
3814      have no idea how ELF linkers handle this.  */
3815   power_of_two = bfd_log2 (h->size);
3816   if (power_of_two > 3)
3817     power_of_two = 3;
3818
3819   /* Apply the required alignment.  */
3820   s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
3821   if (power_of_two > bfd_get_section_alignment (dynobj, s))
3822     {
3823       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
3824         return false;
3825     }
3826
3827   /* Define the symbol as being at this point in the section.  */
3828   h->root.u.def.section = s;
3829   h->root.u.def.value = s->_raw_size;
3830
3831   /* Increment the section size to make room for the symbol.  */
3832   s->_raw_size += h->size;
3833
3834   return true;
3835 }
3836
3837 /* This is the condition under which sh_elf_finish_dynamic_symbol
3838    will be called from elflink.h.  If elflink.h doesn't call our
3839    finish_dynamic_symbol routine, we'll need to do something about
3840    initializing any .plt and .got entries in sh_elf_relocate_section.  */
3841 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
3842   ((DYN)                                                                \
3843    && ((INFO)->shared                                                   \
3844        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)      \
3845    && ((H)->dynindx != -1                                               \
3846        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
3847
3848 /* Allocate space in .plt, .got and associated reloc sections for
3849    dynamic relocs.  */
3850
3851 static boolean
3852 allocate_dynrelocs (h, inf)
3853      struct elf_link_hash_entry *h;
3854      PTR inf;
3855 {
3856   struct bfd_link_info *info;
3857   struct elf_sh_link_hash_table *htab;
3858   struct elf_sh_link_hash_entry *eh;
3859   struct elf_sh_dyn_relocs *p;
3860
3861   if (h->root.type == bfd_link_hash_indirect)
3862     return true;
3863
3864   if (h->root.type == bfd_link_hash_warning)
3865     /* When warning symbols are created, they **replace** the "real"
3866        entry in the hash table, thus we never get to see the real
3867        symbol in a hash traversal.  So look at it now.  */
3868     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3869
3870   info = (struct bfd_link_info *) inf;
3871   htab = sh_elf_hash_table (info);
3872
3873   eh = (struct elf_sh_link_hash_entry *) h;
3874   if ((h->got.refcount > 0
3875       || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
3876       && eh->gotplt_refcount > 0)
3877     {
3878       /* The symbol has been forced local, or we have some direct got refs,
3879          so treat all the gotplt refs as got refs. */
3880       h->got.refcount += eh->gotplt_refcount;
3881       if (h->plt.refcount >= eh->gotplt_refcount)
3882         h->plt.refcount -= eh->gotplt_refcount;
3883     }
3884
3885   if (htab->root.dynamic_sections_created
3886       && h->plt.refcount > 0)
3887     {
3888       /* Make sure this symbol is output as a dynamic symbol.
3889          Undefined weak syms won't yet be marked as dynamic.  */
3890       if (h->dynindx == -1
3891           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3892         {
3893           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3894             return false;
3895         }
3896
3897       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
3898         {
3899           asection *s = htab->splt;
3900
3901           /* If this is the first .plt entry, make room for the special
3902              first entry.  */
3903           if (s->_raw_size == 0)
3904             s->_raw_size += PLT_ENTRY_SIZE;
3905
3906           h->plt.offset = s->_raw_size;
3907
3908           /* If this symbol is not defined in a regular file, and we are
3909              not generating a shared library, then set the symbol to this
3910              location in the .plt.  This is required to make function
3911              pointers compare as equal between the normal executable and
3912              the shared library.  */
3913           if (! info->shared
3914               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3915             {
3916               h->root.u.def.section = s;
3917               h->root.u.def.value = h->plt.offset;
3918             }
3919
3920           /* Make room for this entry.  */
3921           s->_raw_size += PLT_ENTRY_SIZE;
3922
3923           /* We also need to make an entry in the .got.plt section, which
3924              will be placed in the .got section by the linker script.  */
3925           htab->sgotplt->_raw_size += 4;
3926
3927           /* We also need to make an entry in the .rel.plt section.  */
3928           htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
3929         }
3930       else
3931         {
3932           h->plt.offset = (bfd_vma) -1;
3933           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3934         }
3935     }
3936   else
3937     {
3938       h->plt.offset = (bfd_vma) -1;
3939       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3940     }
3941
3942   if (h->got.refcount > 0)
3943     {
3944       asection *s;
3945       boolean dyn;
3946
3947       /* Make sure this symbol is output as a dynamic symbol.
3948          Undefined weak syms won't yet be marked as dynamic.  */
3949       if (h->dynindx == -1
3950           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3951         {
3952           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3953             return false;
3954         }
3955
3956       s = htab->sgot;
3957 #ifdef INCLUDE_SHMEDIA
3958       if (h->type == STT_DATALABEL)
3959         {
3960           struct elf_sh_link_hash_entry *hsh;
3961
3962           h = (struct elf_link_hash_entry *) h->root.u.i.link;
3963           hsh = (struct elf_sh_link_hash_entry *)h;
3964           hsh->datalabel_got_offset = s->_raw_size;
3965         }
3966       else
3967         h->got.offset = s->_raw_size;
3968 #else
3969       h->got.offset = s->_raw_size;
3970 #endif
3971       s->_raw_size += 4;
3972       dyn = htab->root.dynamic_sections_created;
3973       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
3974         htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
3975     }
3976   else
3977     h->got.offset = (bfd_vma) -1;
3978
3979   if (eh->dyn_relocs == NULL)
3980     return true;
3981
3982   /* In the shared -Bsymbolic case, discard space allocated for
3983      dynamic pc-relative relocs against symbols which turn out to be
3984      defined in regular objects.  For the normal shared case, discard
3985      space for pc-relative relocs that have become local due to symbol
3986      visibility changes.  */
3987
3988   if (info->shared)
3989     {
3990       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3991           && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
3992               || info->symbolic))
3993         {
3994           struct elf_sh_dyn_relocs **pp;
3995
3996           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3997             {
3998               p->count -= p->pc_count;
3999               p->pc_count = 0;
4000               if (p->count == 0)
4001                 *pp = p->next;
4002               else
4003                 pp = &p->next;
4004             }
4005         }
4006     }
4007   else
4008     {
4009       /* For the non-shared case, discard space for relocs against
4010          symbols which turn out to need copy relocs or are not
4011          dynamic.  */
4012
4013       if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
4014           && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4015                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4016               || (htab->root.dynamic_sections_created
4017                   && (h->root.type == bfd_link_hash_undefweak
4018                       || h->root.type == bfd_link_hash_undefined))))
4019         {
4020           /* Make sure this symbol is output as a dynamic symbol.
4021              Undefined weak syms won't yet be marked as dynamic.  */
4022           if (h->dynindx == -1
4023               && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4024             {
4025               if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4026                 return false;
4027             }
4028
4029           /* If that succeeded, we know we'll be keeping all the
4030              relocs.  */
4031           if (h->dynindx != -1)
4032             goto keep;
4033         }
4034
4035       eh->dyn_relocs = NULL;
4036
4037     keep: ;
4038     }
4039
4040   /* Finally, allocate space.  */
4041   for (p = eh->dyn_relocs; p != NULL; p = p->next)
4042     {
4043       asection *sreloc = elf_section_data (p->sec)->sreloc;
4044       sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
4045     }
4046
4047   return true;
4048 }
4049
4050 /* Find any dynamic relocs that apply to read-only sections.  */
4051
4052 static boolean
4053 readonly_dynrelocs (h, inf)
4054      struct elf_link_hash_entry *h;
4055      PTR inf;
4056 {
4057   struct elf_sh_link_hash_entry *eh;
4058   struct elf_sh_dyn_relocs *p;
4059
4060   if (h->root.type == bfd_link_hash_warning)
4061     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4062
4063   eh = (struct elf_sh_link_hash_entry *) h;
4064   for (p = eh->dyn_relocs; p != NULL; p = p->next)
4065     {
4066       asection *s = p->sec->output_section;
4067
4068       if (s != NULL && (s->flags & SEC_READONLY) != 0)
4069         {
4070           struct bfd_link_info *info = (struct bfd_link_info *) inf;
4071
4072           info->flags |= DF_TEXTREL;
4073
4074           /* Not an error, just cut short the traversal.  */
4075           return false;
4076         }
4077     }
4078   return true;
4079 }
4080
4081 /* Set the sizes of the dynamic sections.  */
4082
4083 static boolean
4084 sh_elf_size_dynamic_sections (output_bfd, info)
4085      bfd *output_bfd ATTRIBUTE_UNUSED;
4086      struct bfd_link_info *info;
4087 {
4088   struct elf_sh_link_hash_table *htab;
4089   bfd *dynobj;
4090   asection *s;
4091   boolean relocs;
4092   bfd *ibfd;
4093
4094   htab = sh_elf_hash_table (info);
4095   dynobj = htab->root.dynobj;
4096   BFD_ASSERT (dynobj != NULL);
4097
4098   if (htab->root.dynamic_sections_created)
4099     {
4100       /* Set the contents of the .interp section to the interpreter.  */
4101       if (! info->shared)
4102         {
4103           s = bfd_get_section_by_name (dynobj, ".interp");
4104           BFD_ASSERT (s != NULL);
4105           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4106           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4107         }
4108     }
4109
4110   /* Set up .got offsets for local syms, and space for local dynamic
4111      relocs.  */
4112   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4113     {
4114       bfd_signed_vma *local_got;
4115       bfd_signed_vma *end_local_got;
4116       bfd_size_type locsymcount;
4117       Elf_Internal_Shdr *symtab_hdr;
4118       asection *srel;
4119
4120       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4121         continue;
4122
4123       for (s = ibfd->sections; s != NULL; s = s->next)
4124         {
4125           struct elf_sh_dyn_relocs *p;
4126
4127           for (p = ((struct elf_sh_dyn_relocs *)
4128                     elf_section_data (s)->local_dynrel);
4129                p != NULL;
4130                p = p->next)
4131             {
4132               if (! bfd_is_abs_section (p->sec)
4133                   && bfd_is_abs_section (p->sec->output_section))
4134                 {
4135                   /* Input section has been discarded, either because
4136                      it is a copy of a linkonce section or due to
4137                      linker script /DISCARD/, so we'll be discarding
4138                      the relocs too.  */
4139                 }
4140               else if (p->count != 0)
4141                 {
4142                   srel = elf_section_data (p->sec)->sreloc;
4143                   srel->_raw_size += p->count * sizeof (Elf32_External_Rela);
4144                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4145                     info->flags |= DF_TEXTREL;
4146                 }
4147             }
4148         }
4149
4150       local_got = elf_local_got_refcounts (ibfd);
4151       if (!local_got)
4152         continue;
4153
4154       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4155       locsymcount = symtab_hdr->sh_info;
4156 #ifdef INCLUDE_SHMEDIA
4157       /* Count datalabel local GOT.  */
4158       locsymcount *= 2;
4159 #endif
4160       end_local_got = local_got + locsymcount;
4161       s = htab->sgot;
4162       srel = htab->srelgot;
4163       for (; local_got < end_local_got; ++local_got)
4164         {
4165           if (*local_got > 0)
4166             {
4167               *local_got = s->_raw_size;
4168               s->_raw_size += 4;
4169               if (info->shared)
4170                 srel->_raw_size += sizeof (Elf32_External_Rela);
4171             }
4172           else
4173             *local_got = (bfd_vma) -1;
4174         }
4175     }
4176
4177   /* Allocate global sym .plt and .got entries, and space for global
4178      sym dynamic relocs.  */
4179   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
4180
4181   /* We now have determined the sizes of the various dynamic sections.
4182      Allocate memory for them.  */
4183   relocs = false;
4184   for (s = dynobj->sections; s != NULL; s = s->next)
4185     {
4186       if ((s->flags & SEC_LINKER_CREATED) == 0)
4187         continue;
4188
4189       if (s == htab->splt
4190           || s == htab->sgot
4191           || s == htab->sgotplt)
4192         {
4193           /* Strip this section if we don't need it; see the
4194              comment below.  */
4195         }
4196       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4197         {
4198           if (s->_raw_size != 0 && s != htab->srelplt)
4199             relocs = true;
4200
4201           /* We use the reloc_count field as a counter if we need
4202              to copy relocs into the output file.  */
4203           s->reloc_count = 0;
4204         }
4205       else
4206         {
4207           /* It's not one of our sections, so don't allocate space.  */
4208           continue;
4209         }
4210
4211       if (s->_raw_size == 0)
4212         {
4213           /* If we don't need this section, strip it from the
4214              output file.  This is mostly to handle .rela.bss and
4215              .rela.plt.  We must create both sections in
4216              create_dynamic_sections, because they must be created
4217              before the linker maps input sections to output
4218              sections.  The linker does that before
4219              adjust_dynamic_symbol is called, and it is that
4220              function which decides whether anything needs to go
4221              into these sections.  */
4222
4223           _bfd_strip_section_from_output (info, s);
4224           continue;
4225         }
4226
4227       /* Allocate memory for the section contents.  We use bfd_zalloc
4228          here in case unused entries are not reclaimed before the
4229          section's contents are written out.  This should not happen,
4230          but this way if it does, we get a R_SH_NONE reloc instead
4231          of garbage.  */
4232       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4233       if (s->contents == NULL)
4234         return false;
4235     }
4236
4237   if (htab->root.dynamic_sections_created)
4238     {
4239       /* Add some entries to the .dynamic section.  We fill in the
4240          values later, in sh_elf_finish_dynamic_sections, but we
4241          must add the entries now so that we get the correct size for
4242          the .dynamic section.  The DT_DEBUG entry is filled in by the
4243          dynamic linker and used by the debugger.  */
4244 #define add_dynamic_entry(TAG, VAL) \
4245   bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
4246
4247       if (! info->shared)
4248         {
4249           if (! add_dynamic_entry (DT_DEBUG, 0))
4250             return false;
4251         }
4252
4253       if (htab->splt->_raw_size != 0)
4254         {
4255           if (! add_dynamic_entry (DT_PLTGOT, 0)
4256               || ! add_dynamic_entry (DT_PLTRELSZ, 0)
4257               || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
4258               || ! add_dynamic_entry (DT_JMPREL, 0))
4259             return false;
4260         }
4261
4262       if (relocs)
4263         {
4264           if (! add_dynamic_entry (DT_RELA, 0)
4265               || ! add_dynamic_entry (DT_RELASZ, 0)
4266               || ! add_dynamic_entry (DT_RELAENT,
4267                                       sizeof (Elf32_External_Rela)))
4268             return false;
4269
4270           /* If any dynamic relocs apply to a read-only section,
4271              then we need a DT_TEXTREL entry.  */
4272           if ((info->flags & DF_TEXTREL) == 0)
4273             elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
4274                                     (PTR) info);
4275
4276           if ((info->flags & DF_TEXTREL) != 0)
4277             {
4278               if (! add_dynamic_entry (DT_TEXTREL, 0))
4279                 return false;
4280             }
4281         }
4282     }
4283 #undef add_dynamic_entry
4284
4285   return true;
4286 }
4287 \f
4288 /* Relocate an SH ELF section.  */
4289
4290 static boolean
4291 sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4292                          contents, relocs, local_syms, local_sections)
4293      bfd *output_bfd;
4294      struct bfd_link_info *info;
4295      bfd *input_bfd;
4296      asection *input_section;
4297      bfd_byte *contents;
4298      Elf_Internal_Rela *relocs;
4299      Elf_Internal_Sym *local_syms;
4300      asection **local_sections;
4301 {
4302   struct elf_sh_link_hash_table *htab;
4303   Elf_Internal_Shdr *symtab_hdr;
4304   struct elf_link_hash_entry **sym_hashes;
4305   Elf_Internal_Rela *rel, *relend;
4306   bfd *dynobj;
4307   bfd_vma *local_got_offsets;
4308   asection *sgot;
4309   asection *sgotplt;
4310   asection *splt;
4311   asection *sreloc;
4312
4313   htab = sh_elf_hash_table (info);
4314   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4315   sym_hashes = elf_sym_hashes (input_bfd);
4316   dynobj = htab->root.dynobj;
4317   local_got_offsets = elf_local_got_offsets (input_bfd);
4318
4319   sgot = htab->sgot;
4320   sgotplt = htab->sgotplt;
4321   splt = htab->splt;
4322   sreloc = NULL;
4323
4324   rel = relocs;
4325   relend = relocs + input_section->reloc_count;
4326   for (; rel < relend; rel++)
4327     {
4328       int r_type;
4329       reloc_howto_type *howto;
4330       unsigned long r_symndx;
4331       Elf_Internal_Sym *sym;
4332       asection *sec;
4333       struct elf_link_hash_entry *h;
4334       bfd_vma relocation;
4335       bfd_vma addend = (bfd_vma) 0;
4336       bfd_reloc_status_type r;
4337       int seen_stt_datalabel = 0;
4338
4339       r_symndx = ELF32_R_SYM (rel->r_info);
4340
4341       r_type = ELF32_R_TYPE (rel->r_info);
4342
4343       /* Many of the relocs are only used for relaxing, and are
4344          handled entirely by the relaxation code.  */
4345       if (r_type > (int) R_SH_LAST_INVALID_RELOC
4346           && r_type < (int) R_SH_LOOP_START)
4347         continue;
4348       if (r_type == (int) R_SH_NONE)
4349         continue;
4350
4351       if (r_type < 0
4352           || r_type >= R_SH_max
4353           || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
4354               && r_type <= (int) R_SH_LAST_INVALID_RELOC)
4355           || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
4356               && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
4357           || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
4358               && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
4359           || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
4360               && r_type <= (int) R_SH_LAST_INVALID_RELOC_2))
4361         {
4362           bfd_set_error (bfd_error_bad_value);
4363           return false;
4364         }
4365
4366       howto = sh_elf_howto_table + r_type;
4367
4368       /* For relocs that aren't partial_inplace, we get the addend from
4369          the relocation.  */
4370       if (! howto->partial_inplace)
4371         addend = rel->r_addend;
4372
4373       h = NULL;
4374       sym = NULL;
4375       sec = NULL;
4376       if (r_symndx < symtab_hdr->sh_info)
4377         {
4378           sym = local_syms + r_symndx;
4379           sec = local_sections[r_symndx];
4380           relocation = (sec->output_section->vma
4381                         + sec->output_offset
4382                         + sym->st_value);
4383           /* A local symbol never has STO_SH5_ISA32, so we don't need
4384              datalabel processing here.  Make sure this does not change
4385              without notice.  */
4386           if ((sym->st_other & STO_SH5_ISA32) != 0)
4387             ((*info->callbacks->reloc_dangerous)
4388              (info,
4389               _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
4390               input_bfd, input_section, rel->r_offset));
4391           if (info->relocateable)
4392             {
4393               /* This is a relocateable link.  We don't have to change
4394                  anything, unless the reloc is against a section symbol,
4395                  in which case we have to adjust according to where the
4396                  section symbol winds up in the output section.  */
4397               sym = local_syms + r_symndx;
4398               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4399                 {
4400                   if (! howto->partial_inplace)
4401                     {
4402                       /* For relocations with the addend in the
4403                          relocation, we need just to update the addend.
4404                          All real relocs are of type partial_inplace; this
4405                          code is mostly for completeness.  */
4406                       rel->r_addend += sec->output_offset + sym->st_value;
4407
4408                       continue;
4409                     }
4410
4411                   /* Relocs of type partial_inplace need to pick up the
4412                      contents in the contents and add the offset resulting
4413                      from the changed location of the section symbol.
4414                      Using _bfd_final_link_relocate (e.g. goto
4415                      final_link_relocate) here would be wrong, because
4416                      relocations marked pc_relative would get the current
4417                      location subtracted, and we must only do that at the
4418                      final link.  */
4419                   r = _bfd_relocate_contents (howto, input_bfd,
4420                                               sec->output_offset
4421                                               + sym->st_value,
4422                                               contents + rel->r_offset);
4423                   goto relocation_done;
4424                 }
4425
4426               continue;
4427             }
4428           else if (! howto->partial_inplace)
4429             {
4430               relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
4431               addend = rel->r_addend;
4432             }
4433           else if ((sec->flags & SEC_MERGE)
4434                    && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4435             {
4436               asection *msec;
4437
4438               if (howto->rightshift || howto->src_mask != 0xffffffff)
4439                 {
4440                   (*_bfd_error_handler)
4441                     (_("%s(%s+0x%lx): %s relocation against SEC_MERGE section"),
4442                      bfd_archive_filename (input_bfd),
4443                      bfd_get_section_name (input_bfd, input_section),
4444                      (long) rel->r_offset, howto->name);
4445                   return false;
4446                 }
4447
4448               addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4449               msec = sec;
4450               addend =
4451                 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
4452                 - relocation;
4453               addend += msec->output_section->vma + msec->output_offset;
4454               bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
4455               addend = 0;
4456             }
4457         }
4458       else
4459         {
4460           /* Section symbol are never (?) placed in the hash table, so
4461              we can just ignore hash relocations when creating a
4462              relocateable object file.  */
4463           if (info->relocateable)
4464             continue;
4465
4466           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4467           while (h->root.type == bfd_link_hash_indirect
4468                  || h->root.type == bfd_link_hash_warning)
4469             {
4470 #ifdef INCLUDE_SHMEDIA
4471               /* If the reference passes a symbol marked with
4472                  STT_DATALABEL, then any STO_SH5_ISA32 on the final value
4473                  doesn't count.  */
4474               seen_stt_datalabel |= h->type == STT_DATALABEL;
4475 #endif
4476               h = (struct elf_link_hash_entry *) h->root.u.i.link;
4477             }
4478           if (h->root.type == bfd_link_hash_defined
4479               || h->root.type == bfd_link_hash_defweak)
4480             {
4481               boolean dyn;
4482
4483               dyn = htab->root.dynamic_sections_created;
4484               sec = h->root.u.def.section;
4485               /* In these cases, we don't need the relocation value.
4486                  We check specially because in some obscure cases
4487                  sec->output_section will be NULL.  */
4488               if (r_type == R_SH_GOTPC
4489                   || r_type == R_SH_GOTPC_LOW16
4490                   || r_type == R_SH_GOTPC_MEDLOW16
4491                   || r_type == R_SH_GOTPC_MEDHI16
4492                   || r_type == R_SH_GOTPC_HI16
4493                   || ((r_type == R_SH_PLT32
4494                        || r_type == R_SH_PLT_LOW16
4495                        || r_type == R_SH_PLT_MEDLOW16
4496                        || r_type == R_SH_PLT_MEDHI16
4497                        || r_type == R_SH_PLT_HI16)
4498                       && h->plt.offset != (bfd_vma) -1)
4499                   || ((r_type == R_SH_GOT32
4500                        || r_type == R_SH_GOT_LOW16
4501                        || r_type == R_SH_GOT_MEDLOW16
4502                        || r_type == R_SH_GOT_MEDHI16
4503                        || r_type == R_SH_GOT_HI16)
4504                       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
4505                       && (! info->shared
4506                           || (! info->symbolic && h->dynindx != -1)
4507                           || (h->elf_link_hash_flags
4508                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
4509                   /* The cases above are those in which relocation is
4510                      overwritten in the switch block below.  The cases
4511                      below are those in which we must defer relocation
4512                      to run-time, because we can't resolve absolute
4513                      addresses when creating a shared library.  */
4514                   || (info->shared
4515                       && ((! info->symbolic && h->dynindx != -1)
4516                           || (h->elf_link_hash_flags
4517                               & ELF_LINK_HASH_DEF_REGULAR) == 0)
4518                       && ((r_type == R_SH_DIR32
4519                            && (h->elf_link_hash_flags
4520                                & ELF_LINK_FORCED_LOCAL) == 0)
4521                           || r_type == R_SH_REL32)
4522                       && ((input_section->flags & SEC_ALLOC) != 0
4523                           /* DWARF will emit R_SH_DIR32 relocations in its
4524                              sections against symbols defined externally
4525                              in shared libraries.  We can't do anything
4526                              with them here.  */
4527                           || ((input_section->flags & SEC_DEBUGGING) != 0
4528                               && (h->elf_link_hash_flags
4529                                   & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
4530                 relocation = 0;
4531               else if (sec->output_section == NULL)
4532                 {
4533                   (*_bfd_error_handler)
4534                     (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
4535                      bfd_archive_filename (input_bfd), h->root.root.string,
4536                      bfd_get_section_name (input_bfd, input_section));
4537                   relocation = 0;
4538                 }
4539               else
4540                 relocation = ((h->root.u.def.value
4541                               + sec->output_section->vma
4542                               + sec->output_offset)
4543                               /* A STO_SH5_ISA32 causes a "bitor 1" to the
4544                                  symbol value, unless we've seen
4545                                  STT_DATALABEL on the way to it.  */
4546                               | ((h->other & STO_SH5_ISA32) != 0
4547                                  && ! seen_stt_datalabel));
4548             }
4549           else if (h->root.type == bfd_link_hash_undefweak)
4550             relocation = 0;
4551           else if (info->shared
4552                    && (! info->symbolic || info->allow_shlib_undefined)
4553                    && ! info->no_undefined
4554                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4555             relocation = 0;
4556           else
4557             {
4558               if (! ((*info->callbacks->undefined_symbol)
4559                      (info, h->root.root.string, input_bfd,
4560                       input_section, rel->r_offset,
4561                       (!info->shared || info->no_undefined
4562                        || ELF_ST_VISIBILITY (h->other)))))
4563                 return false;
4564               relocation = 0;
4565             }
4566         }
4567
4568       switch ((int) r_type)
4569         {
4570         final_link_relocate:
4571           /* COFF relocs don't use the addend. The addend is used for
4572              R_SH_DIR32 to be compatible with other compilers.  */
4573           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4574                                         contents, rel->r_offset,
4575                                         relocation, addend);
4576           break;
4577
4578         case R_SH_IND12W:
4579           relocation -= 4;
4580           goto final_link_relocate;
4581
4582         case R_SH_DIR8WPN:
4583         case R_SH_DIR8WPZ:
4584         case R_SH_DIR8WPL:
4585           /* If the reloc is against the start of this section, then
4586              the assembler has already taken care of it and the reloc
4587              is here only to assist in relaxing.  If the reloc is not
4588              against the start of this section, then it's against an
4589              external symbol and we must deal with it ourselves.  */
4590           if (input_section->output_section->vma + input_section->output_offset
4591               != relocation)
4592             {
4593               int disp = (relocation
4594                           - input_section->output_section->vma
4595                           - input_section->output_offset
4596                           - rel->r_offset);
4597               int mask = 0;
4598               switch (r_type)
4599                 {
4600                 case R_SH_DIR8WPN:
4601                 case R_SH_DIR8WPZ: mask = 1; break;
4602                 case R_SH_DIR8WPL: mask = 3; break;
4603                 default: mask = 0; break;
4604                 }
4605               if (disp & mask)
4606                 {
4607                   ((*_bfd_error_handler)
4608                    (_("%s: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
4609                     bfd_archive_filename (input_section->owner),
4610                     (unsigned long) rel->r_offset));
4611                   bfd_set_error (bfd_error_bad_value);
4612                   return false;
4613                 }
4614               relocation -= 4;
4615               goto final_link_relocate;
4616             }
4617           r = bfd_reloc_ok;
4618           break;
4619
4620         default:
4621 #ifdef INCLUDE_SHMEDIA
4622           if (shmedia_prepare_reloc (info, input_bfd, input_section,
4623                                      contents, rel, &relocation))
4624             goto final_link_relocate;
4625 #endif
4626           bfd_set_error (bfd_error_bad_value);
4627           return false;
4628
4629         case R_SH_DIR32:
4630         case R_SH_REL32:
4631           if (info->shared
4632               && r_symndx != 0
4633               && (input_section->flags & SEC_ALLOC) != 0
4634               && (r_type != R_SH_REL32
4635                   || (h != NULL
4636                       && h->dynindx != -1
4637                       && (! info->symbolic
4638                           || (h->elf_link_hash_flags
4639                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
4640             {
4641               Elf_Internal_Rela outrel;
4642               boolean skip, relocate;
4643
4644               /* When generating a shared object, these relocations
4645                  are copied into the output file to be resolved at run
4646                  time.  */
4647
4648               if (sreloc == NULL)
4649                 {
4650                   const char *name;
4651
4652                   name = (bfd_elf_string_from_elf_section
4653                           (input_bfd,
4654                            elf_elfheader (input_bfd)->e_shstrndx,
4655                            elf_section_data (input_section)->rel_hdr.sh_name));
4656                   if (name == NULL)
4657                     return false;
4658
4659                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4660                               && strcmp (bfd_get_section_name (input_bfd,
4661                                                                input_section),
4662                                          name + 5) == 0);
4663
4664                   sreloc = bfd_get_section_by_name (dynobj, name);
4665                   BFD_ASSERT (sreloc != NULL);
4666                 }
4667
4668               skip = false;
4669               relocate = false;
4670
4671               outrel.r_offset =
4672                 _bfd_elf_section_offset (output_bfd, info, input_section,
4673                                          rel->r_offset);
4674               if (outrel.r_offset == (bfd_vma) -1)
4675                 skip = true;
4676               else if (outrel.r_offset == (bfd_vma) -2)
4677                 skip = true, relocate = true;
4678               outrel.r_offset += (input_section->output_section->vma
4679                                   + input_section->output_offset);
4680
4681               if (skip)
4682                 memset (&outrel, 0, sizeof outrel);
4683               else if (r_type == R_SH_REL32)
4684                 {
4685                   BFD_ASSERT (h != NULL && h->dynindx != -1);
4686                   outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
4687                   outrel.r_addend
4688                     = bfd_get_32 (input_bfd, contents + rel->r_offset);
4689                 }
4690               else
4691                 {
4692                   /* h->dynindx may be -1 if this symbol was marked to
4693                      become local.  */
4694                   if (h == NULL
4695                       || ((info->symbolic || h->dynindx == -1)
4696                           && (h->elf_link_hash_flags
4697                               & ELF_LINK_HASH_DEF_REGULAR) != 0))
4698                     {
4699                       relocate = true;
4700                       outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4701                       outrel.r_addend
4702                         = relocation + bfd_get_32 (input_bfd,
4703                                                    contents + rel->r_offset);
4704                     }
4705                   else
4706                     {
4707                       BFD_ASSERT (h->dynindx != -1);
4708                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
4709                       outrel.r_addend
4710                         = relocation + bfd_get_32 (input_bfd,
4711                                                    contents + rel->r_offset);
4712                     }
4713                 }
4714
4715               bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4716                                          (((Elf32_External_Rela *)
4717                                            sreloc->contents)
4718                                           + sreloc->reloc_count));
4719               ++sreloc->reloc_count;
4720
4721               /* If this reloc is against an external symbol, we do
4722                  not want to fiddle with the addend.  Otherwise, we
4723                  need to include the symbol value so that it becomes
4724                  an addend for the dynamic reloc.  */
4725               if (! relocate)
4726                 continue;
4727             }
4728           goto final_link_relocate;
4729
4730         case R_SH_GOTPLT32:
4731 #ifdef INCLUDE_SHMEDIA
4732         case R_SH_GOTPLT_LOW16:
4733         case R_SH_GOTPLT_MEDLOW16:
4734         case R_SH_GOTPLT_MEDHI16:
4735         case R_SH_GOTPLT_HI16:
4736         case R_SH_GOTPLT10BY4:
4737         case R_SH_GOTPLT10BY8:
4738 #endif
4739           /* Relocation is to the entry for this symbol in the
4740              procedure linkage table.  */
4741
4742           if (h == NULL
4743               || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
4744               || ! info->shared
4745               || info->symbolic
4746               || h->dynindx == -1
4747               || h->plt.offset == (bfd_vma) -1
4748               || h->got.offset != (bfd_vma) -1)
4749             goto force_got;
4750
4751           /* Relocation is to the entry for this symbol in the global
4752              offset table extension for the procedure linkage table.  */
4753
4754           BFD_ASSERT (sgotplt != NULL);
4755           relocation = (sgotplt->output_offset
4756                         + ((h->plt.offset / elf_sh_sizeof_plt (info)
4757                             - 1 + 3) * 4));
4758
4759 #ifdef GOT_BIAS
4760           relocation -= GOT_BIAS;
4761 #endif
4762
4763           goto final_link_relocate;
4764
4765         force_got:
4766         case R_SH_GOT32:
4767 #ifdef INCLUDE_SHMEDIA
4768         case R_SH_GOT_LOW16:
4769         case R_SH_GOT_MEDLOW16:
4770         case R_SH_GOT_MEDHI16:
4771         case R_SH_GOT_HI16:
4772         case R_SH_GOT10BY4:
4773         case R_SH_GOT10BY8:
4774 #endif
4775           /* Relocation is to the entry for this symbol in the global
4776              offset table.  */
4777
4778           BFD_ASSERT (sgot != NULL);
4779
4780           if (h != NULL)
4781             {
4782               bfd_vma off;
4783               boolean dyn;
4784
4785               off = h->got.offset;
4786 #ifdef INCLUDE_SHMEDIA
4787               if (seen_stt_datalabel)
4788                 {
4789                   struct elf_sh_link_hash_entry *hsh;
4790
4791                   hsh = (struct elf_sh_link_hash_entry *)h;
4792                   off = hsh->datalabel_got_offset;
4793                 }
4794 #endif
4795               BFD_ASSERT (off != (bfd_vma) -1);
4796
4797               dyn = htab->root.dynamic_sections_created;
4798               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
4799                   || (info->shared
4800                       && (info->symbolic || h->dynindx == -1
4801                           || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
4802                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
4803                 {
4804                   /* This is actually a static link, or it is a
4805                      -Bsymbolic link and the symbol is defined
4806                      locally, or the symbol was forced to be local
4807                      because of a version file.  We must initialize
4808                      this entry in the global offset table.  Since the
4809                      offset must always be a multiple of 4, we use the
4810                      least significant bit to record whether we have
4811                      initialized it already.
4812
4813                      When doing a dynamic link, we create a .rela.got
4814                      relocation entry to initialize the value.  This
4815                      is done in the finish_dynamic_symbol routine.  */
4816                   if ((off & 1) != 0)
4817                     off &= ~1;
4818                   else
4819                     {
4820                       bfd_put_32 (output_bfd, relocation,
4821                                   sgot->contents + off);
4822 #ifdef INCLUDE_SHMEDIA
4823                       if (seen_stt_datalabel)
4824                         {
4825                           struct elf_sh_link_hash_entry *hsh;
4826
4827                           hsh = (struct elf_sh_link_hash_entry *)h;
4828                           hsh->datalabel_got_offset |= 1;
4829                         }
4830                       else
4831 #endif
4832                         h->got.offset |= 1;
4833                     }
4834                 }
4835
4836               relocation = sgot->output_offset + off;
4837             }
4838           else
4839             {
4840               bfd_vma off;
4841
4842 #ifdef INCLUDE_SHMEDIA
4843               if (rel->r_addend)
4844                 {
4845                   BFD_ASSERT (local_got_offsets != NULL
4846                               && (local_got_offsets[symtab_hdr->sh_info
4847                                                     + r_symndx]
4848                                   != (bfd_vma) -1));
4849
4850                   off = local_got_offsets[symtab_hdr->sh_info
4851                                           + r_symndx];
4852                 }
4853               else
4854                 {
4855 #endif
4856               BFD_ASSERT (local_got_offsets != NULL
4857                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
4858
4859               off = local_got_offsets[r_symndx];
4860 #ifdef INCLUDE_SHMEDIA
4861                 }
4862 #endif
4863
4864               /* The offset must always be a multiple of 4.  We use
4865                  the least significant bit to record whether we have
4866                  already generated the necessary reloc.  */
4867               if ((off & 1) != 0)
4868                 off &= ~1;
4869               else
4870                 {
4871                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4872
4873                   if (info->shared)
4874                     {
4875                       asection *srelgot;
4876                       Elf_Internal_Rela outrel;
4877
4878                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4879                       BFD_ASSERT (srelgot != NULL);
4880
4881                       outrel.r_offset = (sgot->output_section->vma
4882                                          + sgot->output_offset
4883                                          + off);
4884                       outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4885                       outrel.r_addend = relocation;
4886                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4887                                                 (((Elf32_External_Rela *)
4888                                                   srelgot->contents)
4889                                                  + srelgot->reloc_count));
4890                       ++srelgot->reloc_count;
4891                     }
4892
4893 #ifdef INCLUDE_SHMEDIA
4894                   if (rel->r_addend)
4895                     local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
4896                   else
4897 #endif
4898                     local_got_offsets[r_symndx] |= 1;
4899                 }
4900
4901               relocation = sgot->output_offset + off;
4902             }
4903
4904 #ifdef GOT_BIAS
4905           relocation -= GOT_BIAS;
4906 #endif
4907
4908           goto final_link_relocate;
4909
4910         case R_SH_GOTOFF:
4911 #ifdef INCLUDE_SHMEDIA
4912         case R_SH_GOTOFF_LOW16:
4913         case R_SH_GOTOFF_MEDLOW16:
4914         case R_SH_GOTOFF_MEDHI16:
4915         case R_SH_GOTOFF_HI16:
4916 #endif
4917           /* Relocation is relative to the start of the global offset
4918              table.  */
4919
4920           BFD_ASSERT (sgot != NULL);
4921
4922           /* Note that sgot->output_offset is not involved in this
4923              calculation.  We always want the start of .got.  If we
4924              defined _GLOBAL_OFFSET_TABLE in a different way, as is
4925              permitted by the ABI, we might have to change this
4926              calculation.  */
4927           relocation -= sgot->output_section->vma;
4928
4929 #ifdef GOT_BIAS
4930           relocation -= GOT_BIAS;
4931 #endif
4932
4933           addend = rel->r_addend;
4934
4935           goto final_link_relocate;
4936
4937         case R_SH_GOTPC:
4938 #ifdef INCLUDE_SHMEDIA
4939         case R_SH_GOTPC_LOW16:
4940         case R_SH_GOTPC_MEDLOW16:
4941         case R_SH_GOTPC_MEDHI16:
4942         case R_SH_GOTPC_HI16:
4943 #endif
4944           /* Use global offset table as symbol value.  */
4945
4946           BFD_ASSERT (sgot != NULL);
4947           relocation = sgot->output_section->vma;
4948
4949 #ifdef GOT_BIAS
4950           relocation += GOT_BIAS;
4951 #endif
4952
4953           addend = rel->r_addend;
4954
4955           goto final_link_relocate;
4956
4957         case R_SH_PLT32:
4958 #ifdef INCLUDE_SHMEDIA
4959         case R_SH_PLT_LOW16:
4960         case R_SH_PLT_MEDLOW16:
4961         case R_SH_PLT_MEDHI16:
4962         case R_SH_PLT_HI16:
4963 #endif
4964           /* Relocation is to the entry for this symbol in the
4965              procedure linkage table.  */
4966
4967           /* Resolve a PLT reloc against a local symbol directly,
4968              without using the procedure linkage table.  */
4969           if (h == NULL)
4970             goto final_link_relocate;
4971
4972           if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
4973             goto final_link_relocate;
4974
4975           if (h->plt.offset == (bfd_vma) -1)
4976             {
4977               /* We didn't make a PLT entry for this symbol.  This
4978                  happens when statically linking PIC code, or when
4979                  using -Bsymbolic.  */
4980               goto final_link_relocate;
4981             }
4982
4983           BFD_ASSERT (splt != NULL);
4984           relocation = (splt->output_section->vma
4985                         + splt->output_offset
4986                         + h->plt.offset);
4987
4988 #ifdef INCLUDE_SHMEDIA
4989           relocation++;
4990 #endif
4991
4992           addend = rel->r_addend;
4993
4994           goto final_link_relocate;
4995
4996         case R_SH_LOOP_START:
4997           {
4998             static bfd_vma start, end;
4999
5000             start = (relocation + rel->r_addend
5001                      - (sec->output_section->vma + sec->output_offset));
5002             r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5003                                    rel->r_offset, sec, start, end);
5004             break;
5005
5006         case R_SH_LOOP_END:
5007             end = (relocation + rel->r_addend
5008                    - (sec->output_section->vma + sec->output_offset));
5009             r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5010                                    rel->r_offset, sec, start, end);
5011             break;
5012           }
5013         }
5014
5015     relocation_done:
5016       if (r != bfd_reloc_ok)
5017         {
5018           switch (r)
5019             {
5020             default:
5021             case bfd_reloc_outofrange:
5022               abort ();
5023             case bfd_reloc_overflow:
5024               {
5025                 const char *name;
5026
5027                 if (h != NULL)
5028                   name = h->root.root.string;
5029                 else
5030                   {
5031                     name = (bfd_elf_string_from_elf_section
5032                             (input_bfd, symtab_hdr->sh_link, sym->st_name));
5033                     if (name == NULL)
5034                       return false;
5035                     if (*name == '\0')
5036                       name = bfd_section_name (input_bfd, sec);
5037                   }
5038                 if (! ((*info->callbacks->reloc_overflow)
5039                        (info, name, howto->name, (bfd_vma) 0,
5040                         input_bfd, input_section, rel->r_offset)))
5041                   return false;
5042               }
5043               break;
5044             }
5045         }
5046     }
5047
5048   return true;
5049 }
5050
5051 /* This is a version of bfd_generic_get_relocated_section_contents
5052    which uses sh_elf_relocate_section.  */
5053
5054 static bfd_byte *
5055 sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
5056                                        data, relocateable, symbols)
5057      bfd *output_bfd;
5058      struct bfd_link_info *link_info;
5059      struct bfd_link_order *link_order;
5060      bfd_byte *data;
5061      boolean relocateable;
5062      asymbol **symbols;
5063 {
5064   Elf_Internal_Shdr *symtab_hdr;
5065   asection *input_section = link_order->u.indirect.section;
5066   bfd *input_bfd = input_section->owner;
5067   asection **sections = NULL;
5068   Elf_Internal_Rela *internal_relocs = NULL;
5069   Elf_Internal_Sym *isymbuf = NULL;
5070
5071   /* We only need to handle the case of relaxing, or of having a
5072      particular set of section contents, specially.  */
5073   if (relocateable
5074       || elf_section_data (input_section)->this_hdr.contents == NULL)
5075     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5076                                                        link_order, data,
5077                                                        relocateable,
5078                                                        symbols);
5079
5080   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5081
5082   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
5083           (size_t) input_section->_raw_size);
5084
5085   if ((input_section->flags & SEC_RELOC) != 0
5086       && input_section->reloc_count > 0)
5087     {
5088       asection **secpp;
5089       Elf_Internal_Sym *isym, *isymend;
5090       bfd_size_type amt;
5091
5092       internal_relocs = (_bfd_elf32_link_read_relocs
5093                          (input_bfd, input_section, (PTR) NULL,
5094                           (Elf_Internal_Rela *) NULL, false));
5095       if (internal_relocs == NULL)
5096         goto error_return;
5097
5098       if (symtab_hdr->sh_info != 0)
5099         {
5100           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5101           if (isymbuf == NULL)
5102             isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5103                                             symtab_hdr->sh_info, 0,
5104                                             NULL, NULL, NULL);
5105           if (isymbuf == NULL)
5106             goto error_return;
5107         }
5108
5109       amt = symtab_hdr->sh_info;
5110       amt *= sizeof (asection *);
5111       sections = (asection **) bfd_malloc (amt);
5112       if (sections == NULL && amt != 0)
5113         goto error_return;
5114
5115       isymend = isymbuf + symtab_hdr->sh_info;
5116       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5117         {
5118           asection *isec;
5119
5120           if (isym->st_shndx == SHN_UNDEF)
5121             isec = bfd_und_section_ptr;
5122           else if (isym->st_shndx == SHN_ABS)
5123             isec = bfd_abs_section_ptr;
5124           else if (isym->st_shndx == SHN_COMMON)
5125             isec = bfd_com_section_ptr;
5126           else
5127             isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5128
5129           *secpp = isec;
5130         }
5131
5132       if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5133                                      input_section, data, internal_relocs,
5134                                      isymbuf, sections))
5135         goto error_return;
5136
5137       if (sections != NULL)
5138         free (sections);
5139       if (isymbuf != NULL
5140           && symtab_hdr->contents != (unsigned char *) isymbuf)
5141         free (isymbuf);
5142       if (elf_section_data (input_section)->relocs != internal_relocs)
5143         free (internal_relocs);
5144     }
5145
5146   return data;
5147
5148  error_return:
5149   if (sections != NULL)
5150     free (sections);
5151   if (isymbuf != NULL
5152       && symtab_hdr->contents != (unsigned char *) isymbuf)
5153     free (isymbuf);
5154   if (internal_relocs != NULL
5155       && elf_section_data (input_section)->relocs != internal_relocs)
5156     free (internal_relocs);
5157   return NULL;
5158 }
5159
5160 static asection *
5161 sh_elf_gc_mark_hook (sec, info, rel, h, sym)
5162      asection *sec;
5163      struct bfd_link_info *info ATTRIBUTE_UNUSED;
5164      Elf_Internal_Rela *rel;
5165      struct elf_link_hash_entry *h;
5166      Elf_Internal_Sym *sym;
5167 {
5168   if (h != NULL)
5169     {
5170       switch (ELF32_R_TYPE (rel->r_info))
5171         {
5172         case R_SH_GNU_VTINHERIT:
5173         case R_SH_GNU_VTENTRY:
5174           break;
5175
5176         default:
5177 #ifdef INCLUDE_SHMEDIA
5178           while (h->root.type == bfd_link_hash_indirect
5179                  && h->root.u.i.link)
5180             h = (struct elf_link_hash_entry *) h->root.u.i.link;
5181 #endif
5182           switch (h->root.type)
5183             {
5184             case bfd_link_hash_defined:
5185             case bfd_link_hash_defweak:
5186               return h->root.u.def.section;
5187
5188             case bfd_link_hash_common:
5189               return h->root.u.c.p->section;
5190
5191             default:
5192               break;
5193             }
5194         }
5195     }
5196   else
5197     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5198
5199   return NULL;
5200 }
5201
5202 /* Update the got entry reference counts for the section being removed.  */
5203
5204 static boolean
5205 sh_elf_gc_sweep_hook (abfd, info, sec, relocs)
5206      bfd *abfd;
5207      struct bfd_link_info *info;
5208      asection *sec;
5209      const Elf_Internal_Rela *relocs;
5210 {
5211   Elf_Internal_Shdr *symtab_hdr;
5212   struct elf_link_hash_entry **sym_hashes;
5213   bfd_signed_vma *local_got_refcounts;
5214   const Elf_Internal_Rela *rel, *relend;
5215   unsigned long r_symndx;
5216   struct elf_link_hash_entry *h;
5217   struct elf_sh_link_hash_entry *eh;
5218
5219   elf_section_data (sec)->local_dynrel = NULL;
5220
5221   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5222   sym_hashes = elf_sym_hashes (abfd);
5223   local_got_refcounts = elf_local_got_refcounts (abfd);
5224
5225   relend = relocs + sec->reloc_count;
5226   for (rel = relocs; rel < relend; rel++)
5227     switch (ELF32_R_TYPE (rel->r_info))
5228       {
5229       case R_SH_GOT32:
5230       case R_SH_GOTOFF:
5231       case R_SH_GOTPC:
5232 #ifdef INCLUDE_SHMEDIA
5233       case R_SH_GOT_LOW16:
5234       case R_SH_GOT_MEDLOW16:
5235       case R_SH_GOT_MEDHI16:
5236       case R_SH_GOT_HI16:
5237       case R_SH_GOT10BY4:
5238       case R_SH_GOT10BY8:
5239       case R_SH_GOTOFF_LOW16:
5240       case R_SH_GOTOFF_MEDLOW16:
5241       case R_SH_GOTOFF_MEDHI16:
5242       case R_SH_GOTOFF_HI16:
5243       case R_SH_GOTPC_LOW16:
5244       case R_SH_GOTPC_MEDLOW16:
5245       case R_SH_GOTPC_MEDHI16:
5246       case R_SH_GOTPC_HI16:
5247 #endif
5248         r_symndx = ELF32_R_SYM (rel->r_info);
5249         if (r_symndx >= symtab_hdr->sh_info)
5250           {
5251             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5252             if (h->got.refcount > 0)
5253               h->got.refcount -= 1;
5254           }
5255         else if (local_got_refcounts != NULL)
5256           {
5257             if (local_got_refcounts[r_symndx] > 0)
5258               local_got_refcounts[r_symndx] -= 1;
5259           }
5260         break;
5261
5262       case R_SH_DIR32:
5263       case R_SH_REL32:
5264         r_symndx = ELF32_R_SYM (rel->r_info);
5265         if (r_symndx >= symtab_hdr->sh_info)
5266           {
5267             struct elf_sh_link_hash_entry *eh;
5268             struct elf_sh_dyn_relocs **pp;
5269             struct elf_sh_dyn_relocs *p;
5270
5271             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5272
5273             if (!info->shared && h->plt.refcount > 0)
5274               h->plt.refcount -= 1;
5275
5276             eh = (struct elf_sh_link_hash_entry *) h;
5277
5278             for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5279               if (p->sec == sec)
5280                 {
5281                   if (ELF32_R_TYPE (rel->r_info) == R_SH_REL32)
5282                     p->pc_count -= 1;
5283                   p->count -= 1;
5284                   if (p->count == 0)
5285                     *pp = p->next;
5286                   break;
5287                 }
5288           }
5289         break;
5290
5291       case R_SH_PLT32:
5292 #ifdef INCLUDE_SHMEDIA
5293       case R_SH_PLT_LOW16:
5294       case R_SH_PLT_MEDLOW16:
5295       case R_SH_PLT_MEDHI16:
5296       case R_SH_PLT_HI16:
5297 #endif
5298         r_symndx = ELF32_R_SYM (rel->r_info);
5299         if (r_symndx >= symtab_hdr->sh_info)
5300           {
5301             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5302             if (h->plt.refcount > 0)
5303               h->plt.refcount -= 1;
5304           }
5305         break;
5306
5307       case R_SH_GOTPLT32:
5308 #ifdef INCLUDE_SHMEDIA
5309       case R_SH_GOTPLT_LOW16:
5310       case R_SH_GOTPLT_MEDLOW16:
5311       case R_SH_GOTPLT_MEDHI16:
5312       case R_SH_GOTPLT_HI16:
5313       case R_SH_GOTPLT10BY4:
5314       case R_SH_GOTPLT10BY8:
5315 #endif
5316         r_symndx = ELF32_R_SYM (rel->r_info);
5317         if (r_symndx >= symtab_hdr->sh_info)
5318           {
5319             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5320             eh = (struct elf_sh_link_hash_entry *) h;
5321             if (eh->gotplt_refcount > 0)
5322               {
5323                 eh->gotplt_refcount -= 1;
5324                 if (h->plt.refcount > 0)
5325                   h->plt.refcount -= 1;
5326               }
5327             else if (h->got.refcount > 0)
5328               h->got.refcount -= 1;
5329           }
5330         else if (local_got_refcounts != NULL)
5331           {
5332             if (local_got_refcounts[r_symndx] > 0)
5333               local_got_refcounts[r_symndx] -= 1;
5334           }
5335         break;
5336
5337       default:
5338         break;
5339       }
5340
5341   return true;
5342 }
5343
5344 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
5345
5346 static void
5347 sh_elf_copy_indirect_symbol (bed, dir, ind)
5348      struct elf_backend_data *bed;
5349      struct elf_link_hash_entry *dir, *ind;
5350 {
5351   struct elf_sh_link_hash_entry *edir, *eind;
5352
5353   edir = (struct elf_sh_link_hash_entry *) dir;
5354   eind = (struct elf_sh_link_hash_entry *) ind;
5355
5356   if (eind->dyn_relocs != NULL)
5357     {
5358       if (edir->dyn_relocs != NULL)
5359         {
5360           struct elf_sh_dyn_relocs **pp;
5361           struct elf_sh_dyn_relocs *p;
5362
5363           BFD_ASSERT (ind->root.type != bfd_link_hash_indirect);
5364
5365           /* Add reloc counts against the weak sym to the strong sym
5366              list.  Merge any entries against the same section.  */
5367           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5368             {
5369               struct elf_sh_dyn_relocs *q;
5370
5371               for (q = edir->dyn_relocs; q != NULL; q = q->next)
5372                 if (q->sec == p->sec)
5373                   {
5374                     q->pc_count += p->pc_count;
5375                     q->count += p->count;
5376                     *pp = p->next;
5377                     break;
5378                   }
5379               if (q == NULL)
5380                 pp = &p->next;
5381             }
5382           *pp = edir->dyn_relocs;
5383         }
5384
5385       edir->dyn_relocs = eind->dyn_relocs;
5386       eind->dyn_relocs = NULL;
5387     }
5388   edir->gotplt_refcount = eind->gotplt_refcount;
5389   eind->gotplt_refcount = 0;
5390
5391   _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
5392 }
5393
5394 /* Look through the relocs for a section during the first phase.
5395    Since we don't do .gots or .plts, we just need to consider the
5396    virtual table relocs for gc.  */
5397
5398 static boolean
5399 sh_elf_check_relocs (abfd, info, sec, relocs)
5400      bfd *abfd;
5401      struct bfd_link_info *info;
5402      asection *sec;
5403      const Elf_Internal_Rela *relocs;
5404 {
5405   Elf_Internal_Shdr *symtab_hdr;
5406   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
5407   struct elf_sh_link_hash_table *htab;
5408   const Elf_Internal_Rela *rel;
5409   const Elf_Internal_Rela *rel_end;
5410   bfd *dynobj;
5411   bfd_vma *local_got_offsets;
5412   asection *sgot;
5413   asection *srelgot;
5414   asection *sreloc;
5415
5416   sgot = NULL;
5417   srelgot = NULL;
5418   sreloc = NULL;
5419
5420   if (info->relocateable)
5421     return true;
5422
5423   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5424   sym_hashes = elf_sym_hashes (abfd);
5425   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
5426   if (!elf_bad_symtab (abfd))
5427     sym_hashes_end -= symtab_hdr->sh_info;
5428
5429   htab = sh_elf_hash_table (info);
5430   dynobj = htab->root.dynobj;
5431   local_got_offsets = elf_local_got_offsets (abfd);
5432
5433   rel_end = relocs + sec->reloc_count;
5434   for (rel = relocs; rel < rel_end; rel++)
5435     {
5436       struct elf_link_hash_entry *h;
5437       unsigned long r_symndx;
5438
5439       r_symndx = ELF32_R_SYM (rel->r_info);
5440       if (r_symndx < symtab_hdr->sh_info)
5441         h = NULL;
5442       else
5443         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5444
5445       /* Some relocs require a global offset table.  */
5446       if (htab->sgot == NULL)
5447         {
5448           switch (ELF32_R_TYPE (rel->r_info))
5449             {
5450             case R_SH_GOTPLT32:
5451             case R_SH_GOT32:
5452             case R_SH_GOTOFF:
5453             case R_SH_GOTPC:
5454 #ifdef INCLUDE_SHMEDIA
5455             case R_SH_GOTPLT_LOW16:
5456             case R_SH_GOTPLT_MEDLOW16:
5457             case R_SH_GOTPLT_MEDHI16:
5458             case R_SH_GOTPLT_HI16:
5459             case R_SH_GOTPLT10BY4:
5460             case R_SH_GOTPLT10BY8:
5461             case R_SH_GOT_LOW16:
5462             case R_SH_GOT_MEDLOW16:
5463             case R_SH_GOT_MEDHI16:
5464             case R_SH_GOT_HI16:
5465             case R_SH_GOT10BY4:
5466             case R_SH_GOT10BY8:
5467             case R_SH_GOTOFF_LOW16:
5468             case R_SH_GOTOFF_MEDLOW16:
5469             case R_SH_GOTOFF_MEDHI16:
5470             case R_SH_GOTOFF_HI16:
5471             case R_SH_GOTPC_LOW16:
5472             case R_SH_GOTPC_MEDLOW16:
5473             case R_SH_GOTPC_MEDHI16:
5474             case R_SH_GOTPC_HI16:
5475 #endif
5476               if (dynobj == NULL)
5477                 htab->root.dynobj = dynobj = abfd;
5478               if (! create_got_section (dynobj, info))
5479                 return false;
5480               break;
5481
5482             default:
5483               break;
5484             }
5485         }
5486
5487       switch (ELF32_R_TYPE (rel->r_info))
5488         {
5489           /* This relocation describes the C++ object vtable hierarchy.
5490              Reconstruct it for later use during GC.  */
5491         case R_SH_GNU_VTINHERIT:
5492           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5493             return false;
5494           break;
5495
5496           /* This relocation describes which C++ vtable entries are actually
5497              used.  Record for later use during GC.  */
5498         case R_SH_GNU_VTENTRY:
5499           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5500             return false;
5501           break;
5502
5503         force_got:
5504         case R_SH_GOT32:
5505 #ifdef INCLUDE_SHMEDIA
5506         case R_SH_GOT_LOW16:
5507         case R_SH_GOT_MEDLOW16:
5508         case R_SH_GOT_MEDHI16:
5509         case R_SH_GOT_HI16:
5510         case R_SH_GOT10BY4:
5511         case R_SH_GOT10BY8:
5512 #endif
5513           if (h != NULL)
5514             h->got.refcount += 1;
5515           else
5516             {
5517               bfd_signed_vma *local_got_refcounts;
5518
5519               /* This is a global offset table entry for a local
5520                  symbol.  */
5521               local_got_refcounts = elf_local_got_refcounts (abfd);
5522               if (local_got_refcounts == NULL)
5523                 {
5524                   bfd_size_type size;
5525
5526                   size = symtab_hdr->sh_info;
5527                   size *= sizeof (bfd_signed_vma);
5528 #ifdef INCLUDE_SHMEDIA
5529                   /* Reserve space for both the datalabel and
5530                      codelabel local GOT offsets.  */
5531                   size *= 2;
5532 #endif
5533                   local_got_refcounts = ((bfd_signed_vma *)
5534                                          bfd_zalloc (abfd, size));
5535                   if (local_got_refcounts == NULL)
5536                     return false;
5537                   elf_local_got_refcounts (abfd) = local_got_refcounts;
5538                 }
5539               local_got_refcounts[r_symndx] += 1;
5540             }
5541           break;
5542
5543         case R_SH_GOTPLT32:
5544 #ifdef INCLUDE_SHMEDIA
5545         case R_SH_GOTPLT_LOW16:
5546         case R_SH_GOTPLT_MEDLOW16:
5547         case R_SH_GOTPLT_MEDHI16:
5548         case R_SH_GOTPLT_HI16:
5549         case R_SH_GOTPLT10BY4:
5550         case R_SH_GOTPLT10BY8:
5551 #endif
5552           /* If this is a local symbol, we resolve it directly without
5553              creating a procedure linkage table entry.  */
5554
5555           if (h == NULL
5556               || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
5557               || ! info->shared
5558               || info->symbolic
5559               || h->dynindx == -1)
5560             goto force_got;
5561
5562           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
5563           h->plt.refcount += 1;
5564           ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
5565
5566           break;
5567
5568         case R_SH_PLT32:
5569 #ifdef INCLUDE_SHMEDIA
5570         case R_SH_PLT_LOW16:
5571         case R_SH_PLT_MEDLOW16:
5572         case R_SH_PLT_MEDHI16:
5573         case R_SH_PLT_HI16:
5574 #endif
5575           /* This symbol requires a procedure linkage table entry.  We
5576              actually build the entry in adjust_dynamic_symbol,
5577              because this might be a case of linking PIC code which is
5578              never referenced by a dynamic object, in which case we
5579              don't need to generate a procedure linkage table entry
5580              after all.  */
5581
5582           /* If this is a local symbol, we resolve it directly without
5583              creating a procedure linkage table entry.  */
5584           if (h == NULL)
5585             continue;
5586
5587           if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
5588             break;
5589
5590           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
5591           h->plt.refcount += 1;
5592           break;
5593
5594         case R_SH_DIR32:
5595         case R_SH_REL32:
5596           if (h != NULL && ! info->shared)
5597             {
5598               h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
5599               h->plt.refcount += 1;
5600             }
5601
5602           /* If we are creating a shared library, and this is a reloc
5603              against a global symbol, or a non PC relative reloc
5604              against a local symbol, then we need to copy the reloc
5605              into the shared library.  However, if we are linking with
5606              -Bsymbolic, we do not need to copy a reloc against a
5607              global symbol which is defined in an object we are
5608              including in the link (i.e., DEF_REGULAR is set).  At
5609              this point we have not seen all the input files, so it is
5610              possible that DEF_REGULAR is not set now but will be set
5611              later (it is never cleared).  We account for that
5612              possibility below by storing information in the
5613              dyn_relocs field of the hash table entry. A similar
5614              situation occurs when creating shared libraries and symbol
5615              visibility changes render the symbol local.
5616
5617              If on the other hand, we are creating an executable, we
5618              may need to keep relocations for symbols satisfied by a
5619              dynamic library if we manage to avoid copy relocs for the
5620              symbol.  */
5621           if ((info->shared
5622                && (sec->flags & SEC_ALLOC) != 0
5623                && (ELF32_R_TYPE (rel->r_info) != R_SH_REL32
5624                    || (h != NULL
5625                        && (! info->symbolic
5626                            || h->root.type == bfd_link_hash_defweak
5627                            || (h->elf_link_hash_flags
5628                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
5629               || (! info->shared
5630                   && (sec->flags & SEC_ALLOC) != 0
5631                   && h != NULL
5632                   && (h->root.type == bfd_link_hash_defweak
5633                       || (h->elf_link_hash_flags
5634                           & ELF_LINK_HASH_DEF_REGULAR) == 0)))
5635             {
5636               struct elf_sh_dyn_relocs *p;
5637               struct elf_sh_dyn_relocs **head;
5638
5639               if (dynobj == NULL)
5640                 htab->root.dynobj = dynobj = abfd;
5641
5642               /* When creating a shared object, we must copy these
5643                  reloc types into the output file.  We create a reloc
5644                  section in dynobj and make room for this reloc.  */
5645               if (sreloc == NULL)
5646                 {
5647                   const char *name;
5648
5649                   name = (bfd_elf_string_from_elf_section
5650                           (abfd,
5651                            elf_elfheader (abfd)->e_shstrndx,
5652                            elf_section_data (sec)->rel_hdr.sh_name));
5653                   if (name == NULL)
5654                     return false;
5655
5656                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5657                               && strcmp (bfd_get_section_name (abfd, sec),
5658                                          name + 5) == 0);
5659
5660                   sreloc = bfd_get_section_by_name (dynobj, name);
5661                   if (sreloc == NULL)
5662                     {
5663                       flagword flags;
5664
5665                       sreloc = bfd_make_section (dynobj, name);
5666                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
5667                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
5668                       if ((sec->flags & SEC_ALLOC) != 0)
5669                         flags |= SEC_ALLOC | SEC_LOAD;
5670                       if (sreloc == NULL
5671                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
5672                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
5673                         return false;
5674                     }
5675                   if (sec->flags & SEC_READONLY)
5676                     info->flags |= DF_TEXTREL;
5677                   elf_section_data (sec)->sreloc = sreloc;
5678                 }
5679
5680               /* If this is a global symbol, we count the number of
5681                  relocations we need for this symbol.  */
5682               if (h != NULL)
5683                 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
5684               else
5685                 {
5686                   asection *s;
5687
5688                   /* Track dynamic relocs needed for local syms too.  */
5689                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
5690                                                  sec, r_symndx);
5691                   if (s == NULL)
5692                     return false;
5693
5694                   head = ((struct elf_sh_dyn_relocs **)
5695                           &elf_section_data (s)->local_dynrel);
5696                 }
5697
5698               p = *head;
5699               if (p == NULL || p->sec != sec)
5700                 {
5701                   bfd_size_type amt = sizeof (*p);
5702                   p = ((struct elf_sh_dyn_relocs *) bfd_alloc (dynobj, amt));
5703                   if (p == NULL)
5704                     return false;
5705                   p->next = *head;
5706                   *head = p;
5707                   p->sec = sec;
5708                   p->count = 0;
5709                   p->pc_count = 0;
5710                 }
5711
5712               p->count += 1;
5713               if (ELF32_R_TYPE (rel->r_info) == R_SH_REL32)
5714                 p->pc_count += 1;
5715             }
5716
5717           break;
5718         }
5719     }
5720
5721   return true;
5722 }
5723
5724 #ifndef sh_elf_set_mach_from_flags
5725 static boolean
5726 sh_elf_set_mach_from_flags (abfd)
5727      bfd *abfd;
5728 {
5729   flagword flags = elf_elfheader (abfd)->e_flags;
5730
5731   switch (flags & EF_SH_MACH_MASK)
5732     {
5733     case EF_SH1:
5734       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh);
5735       break;
5736     case EF_SH2:
5737       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2);
5738       break;
5739     case EF_SH_DSP:
5740       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh_dsp);
5741       break;
5742     case EF_SH3:
5743       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3);
5744       break;
5745     case EF_SH3_DSP:
5746       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3_dsp);
5747       break;
5748     case EF_SH3E:
5749       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3e);
5750       break;
5751     case EF_SH_UNKNOWN:
5752     case EF_SH4:
5753       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh4);
5754       break;
5755     default:
5756       return false;
5757     }
5758   return true;
5759 }
5760 #endif /* not sh_elf_set_mach_from_flags */
5761
5762 #ifndef sh_elf_set_private_flags
5763 /* Function to keep SH specific file flags.  */
5764
5765 static boolean
5766 sh_elf_set_private_flags (abfd, flags)
5767      bfd *abfd;
5768      flagword flags;
5769 {
5770   BFD_ASSERT (! elf_flags_init (abfd)
5771               || elf_elfheader (abfd)->e_flags == flags);
5772
5773   elf_elfheader (abfd)->e_flags = flags;
5774   elf_flags_init (abfd) = true;
5775   return sh_elf_set_mach_from_flags (abfd);
5776 }
5777 #endif /* not sh_elf_set_private_flags */
5778
5779 #ifndef sh_elf_copy_private_data
5780 /* Copy backend specific data from one object module to another */
5781
5782 static boolean
5783 sh_elf_copy_private_data (ibfd, obfd)
5784      bfd * ibfd;
5785      bfd * obfd;
5786 {
5787   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5788       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5789     return true;
5790
5791   return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
5792 }
5793 #endif /* not sh_elf_copy_private_data */
5794
5795 #ifndef sh_elf_merge_private_data
5796 /* This routine checks for linking big and little endian objects
5797    together, and for linking sh-dsp with sh3e / sh4 objects.  */
5798
5799 static boolean
5800 sh_elf_merge_private_data (ibfd, obfd)
5801      bfd *ibfd;
5802      bfd *obfd;
5803 {
5804   flagword old_flags, new_flags;
5805
5806   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
5807     return false;
5808
5809   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5810       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5811     return true;
5812
5813   if (! elf_flags_init (obfd))
5814     {
5815       /* This happens when ld starts out with a 'blank' output file.  */
5816       elf_flags_init (obfd) = true;
5817       elf_elfheader (obfd)->e_flags = EF_SH1;
5818     }
5819   old_flags = elf_elfheader (obfd)->e_flags;
5820   new_flags = elf_elfheader (ibfd)->e_flags;
5821   if ((EF_SH_HAS_DSP (old_flags) && EF_SH_HAS_FP (new_flags))
5822       || (EF_SH_HAS_DSP (new_flags) && EF_SH_HAS_FP (old_flags)))
5823     {
5824       (*_bfd_error_handler)
5825         ("%s: uses %s instructions while previous modules use %s instructions",
5826          bfd_archive_filename (ibfd),
5827          EF_SH_HAS_DSP (new_flags) ? "dsp" : "floating point",
5828          EF_SH_HAS_DSP (new_flags) ? "floating point" : "dsp");
5829       bfd_set_error (bfd_error_bad_value);
5830       return false;
5831     }
5832   elf_elfheader (obfd)->e_flags = EF_SH_MERGE_MACH (old_flags, new_flags);
5833
5834   return sh_elf_set_mach_from_flags (obfd);
5835 }
5836 #endif /* not sh_elf_merge_private_data */
5837
5838 /* Finish up dynamic symbol handling.  We set the contents of various
5839    dynamic sections here.  */
5840
5841 static boolean
5842 sh_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
5843      bfd *output_bfd;
5844      struct bfd_link_info *info;
5845      struct elf_link_hash_entry *h;
5846      Elf_Internal_Sym *sym;
5847 {
5848   struct elf_sh_link_hash_table *htab;
5849   bfd *dynobj;
5850
5851   htab = sh_elf_hash_table (info);
5852   dynobj = htab->root.dynobj;
5853
5854   if (h->plt.offset != (bfd_vma) -1)
5855     {
5856       asection *splt;
5857       asection *sgot;
5858       asection *srel;
5859
5860       bfd_vma plt_index;
5861       bfd_vma got_offset;
5862       Elf_Internal_Rela rel;
5863
5864       /* This symbol has an entry in the procedure linkage table.  Set
5865          it up.  */
5866
5867       BFD_ASSERT (h->dynindx != -1);
5868
5869       splt = htab->splt;
5870       sgot = htab->sgotplt;
5871       srel = htab->srelplt;
5872       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
5873
5874       /* Get the index in the procedure linkage table which
5875          corresponds to this symbol.  This is the index of this symbol
5876          in all the symbols for which we are making plt entries.  The
5877          first entry in the procedure linkage table is reserved.  */
5878       plt_index = h->plt.offset / elf_sh_sizeof_plt (info) - 1;
5879
5880       /* Get the offset into the .got table of the entry that
5881          corresponds to this function.  Each .got entry is 4 bytes.
5882          The first three are reserved.  */
5883       got_offset = (plt_index + 3) * 4;
5884
5885 #ifdef GOT_BIAS
5886       if (info->shared)
5887         got_offset -= GOT_BIAS;
5888 #endif
5889
5890       /* Fill in the entry in the procedure linkage table.  */
5891       if (! info->shared)
5892         {
5893           if (elf_sh_plt_entry == NULL)
5894             {
5895               elf_sh_plt_entry = (bfd_big_endian (output_bfd) ?
5896                                   elf_sh_plt_entry_be : elf_sh_plt_entry_le);
5897             }
5898           memcpy (splt->contents + h->plt.offset, elf_sh_plt_entry,
5899                   elf_sh_sizeof_plt (info));
5900 #ifdef INCLUDE_SHMEDIA
5901           movi_shori_putval (output_bfd,
5902                              (sgot->output_section->vma
5903                               + sgot->output_offset
5904                               + got_offset),
5905                              (splt->contents + h->plt.offset
5906                               + elf_sh_plt_symbol_offset (info)));
5907
5908           /* Set bottom bit because its for a branch to SHmedia */
5909           movi_shori_putval (output_bfd,
5910                              (splt->output_section->vma + splt->output_offset)
5911                              | 1,
5912                              (splt->contents + h->plt.offset
5913                               + elf_sh_plt_plt0_offset (info)));
5914 #else
5915           bfd_put_32 (output_bfd,
5916                       (sgot->output_section->vma
5917                        + sgot->output_offset
5918                        + got_offset),
5919                       (splt->contents + h->plt.offset
5920                        + elf_sh_plt_symbol_offset (info)));
5921
5922           bfd_put_32 (output_bfd,
5923                       (splt->output_section->vma + splt->output_offset),
5924                       (splt->contents + h->plt.offset
5925                        + elf_sh_plt_plt0_offset (info)));
5926 #endif
5927         }
5928       else
5929         {
5930           if (elf_sh_pic_plt_entry == NULL)
5931             {
5932               elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
5933                                       elf_sh_pic_plt_entry_be :
5934                                       elf_sh_pic_plt_entry_le);
5935             }
5936           memcpy (splt->contents + h->plt.offset, elf_sh_pic_plt_entry,
5937                   elf_sh_sizeof_plt (info));
5938 #ifdef INCLUDE_SHMEDIA
5939           movi_shori_putval (output_bfd, got_offset,
5940                              (splt->contents + h->plt.offset
5941                               + elf_sh_plt_symbol_offset (info)));
5942 #else
5943           bfd_put_32 (output_bfd, got_offset,
5944                       (splt->contents + h->plt.offset
5945                        + elf_sh_plt_symbol_offset (info)));
5946 #endif
5947         }
5948
5949 #ifdef GOT_BIAS
5950       if (info->shared)
5951         got_offset += GOT_BIAS;
5952 #endif
5953
5954 #ifdef INCLUDE_SHMEDIA
5955       movi_shori_putval (output_bfd,
5956                          plt_index * sizeof (Elf32_External_Rela),
5957                          (splt->contents + h->plt.offset
5958                           + elf_sh_plt_reloc_offset (info)));
5959 #else
5960       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
5961                   (splt->contents + h->plt.offset
5962                    + elf_sh_plt_reloc_offset (info)));
5963 #endif
5964
5965       /* Fill in the entry in the global offset table.  */
5966       bfd_put_32 (output_bfd,
5967                   (splt->output_section->vma
5968                    + splt->output_offset
5969                    + h->plt.offset
5970                    + elf_sh_plt_temp_offset (info)),
5971                   sgot->contents + got_offset);
5972
5973       /* Fill in the entry in the .rela.plt section.  */
5974       rel.r_offset = (sgot->output_section->vma
5975                       + sgot->output_offset
5976                       + got_offset);
5977       rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
5978       rel.r_addend = 0;
5979 #ifdef GOT_BIAS
5980       rel.r_addend = GOT_BIAS;
5981 #endif
5982       bfd_elf32_swap_reloca_out (output_bfd, &rel,
5983                                 ((Elf32_External_Rela *) srel->contents
5984                                  + plt_index));
5985
5986       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5987         {
5988           /* Mark the symbol as undefined, rather than as defined in
5989              the .plt section.  Leave the value alone.  */
5990           sym->st_shndx = SHN_UNDEF;
5991         }
5992     }
5993
5994   if (h->got.offset != (bfd_vma) -1)
5995     {
5996       asection *sgot;
5997       asection *srel;
5998       Elf_Internal_Rela rel;
5999
6000       /* This symbol has an entry in the global offset table.  Set it
6001          up.  */
6002
6003       sgot = htab->sgot;
6004       srel = htab->srelgot;
6005       BFD_ASSERT (sgot != NULL && srel != NULL);
6006
6007       rel.r_offset = (sgot->output_section->vma
6008                       + sgot->output_offset
6009                       + (h->got.offset &~ (bfd_vma) 1));
6010
6011       /* If this is a static link, or it is a -Bsymbolic link and the
6012          symbol is defined locally or was forced to be local because
6013          of a version file, we just want to emit a RELATIVE reloc.
6014          The entry in the global offset table will already have been
6015          initialized in the relocate_section function.  */
6016       if (info->shared
6017           && (info->symbolic
6018               || h->dynindx == -1
6019               || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
6020           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
6021         {
6022           rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6023           rel.r_addend = (h->root.u.def.value
6024                           + h->root.u.def.section->output_section->vma
6025                           + h->root.u.def.section->output_offset);
6026         }
6027       else
6028         {
6029           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
6030           rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6031           rel.r_addend = 0;
6032         }
6033
6034       bfd_elf32_swap_reloca_out (output_bfd, &rel,
6035                                  ((Elf32_External_Rela *) srel->contents
6036                                   + srel->reloc_count));
6037       ++srel->reloc_count;
6038     }
6039
6040   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
6041     {
6042       asection *s;
6043       Elf_Internal_Rela rel;
6044
6045       /* This symbol needs a copy reloc.  Set it up.  */
6046
6047       BFD_ASSERT (h->dynindx != -1
6048                   && (h->root.type == bfd_link_hash_defined
6049                       || h->root.type == bfd_link_hash_defweak));
6050
6051       s = bfd_get_section_by_name (h->root.u.def.section->owner,
6052                                    ".rela.bss");
6053       BFD_ASSERT (s != NULL);
6054
6055       rel.r_offset = (h->root.u.def.value
6056                       + h->root.u.def.section->output_section->vma
6057                       + h->root.u.def.section->output_offset);
6058       rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
6059       rel.r_addend = 0;
6060       bfd_elf32_swap_reloca_out (output_bfd, &rel,
6061                                  ((Elf32_External_Rela *) s->contents
6062                                   + s->reloc_count));
6063       ++s->reloc_count;
6064     }
6065
6066   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
6067   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6068       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6069     sym->st_shndx = SHN_ABS;
6070
6071   return true;
6072 }
6073
6074 /* Finish up the dynamic sections.  */
6075
6076 static boolean
6077 sh_elf_finish_dynamic_sections (output_bfd, info)
6078      bfd *output_bfd;
6079      struct bfd_link_info *info;
6080 {
6081   struct elf_sh_link_hash_table *htab;
6082   bfd *dynobj;
6083   asection *sgot;
6084   asection *sdyn;
6085
6086   htab = sh_elf_hash_table (info);
6087   dynobj = htab->root.dynobj;
6088
6089   sgot = htab->sgotplt;
6090   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6091
6092   if (htab->root.dynamic_sections_created)
6093     {
6094       asection *splt;
6095       Elf32_External_Dyn *dyncon, *dynconend;
6096
6097       BFD_ASSERT (sgot != NULL && sdyn != NULL);
6098
6099       dyncon = (Elf32_External_Dyn *) sdyn->contents;
6100       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
6101       for (; dyncon < dynconend; dyncon++)
6102         {
6103           Elf_Internal_Dyn dyn;
6104           asection *s;
6105 #ifdef INCLUDE_SHMEDIA
6106           const char *name;
6107 #endif
6108
6109           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6110
6111           switch (dyn.d_tag)
6112             {
6113             default:
6114               break;
6115
6116 #ifdef INCLUDE_SHMEDIA
6117             case DT_INIT:
6118               name = info->init_function;
6119               goto get_sym;
6120
6121             case DT_FINI:
6122               name = info->fini_function;
6123             get_sym:
6124               if (dyn.d_un.d_val != 0)
6125                 {
6126                   struct elf_link_hash_entry *h;
6127
6128                   h = elf_link_hash_lookup (&htab->root, name,
6129                                             false, false, true);
6130                   if (h != NULL && (h->other & STO_SH5_ISA32))
6131                     {
6132                       dyn.d_un.d_val |= 1;
6133                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6134                     }
6135                 }
6136               break;
6137 #endif
6138
6139             case DT_PLTGOT:
6140               s = htab->sgot->output_section;
6141               goto get_vma;
6142
6143             case DT_JMPREL:
6144               s = htab->srelplt->output_section;
6145             get_vma:
6146               BFD_ASSERT (s != NULL);
6147               dyn.d_un.d_ptr = s->vma;
6148               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6149               break;
6150
6151             case DT_PLTRELSZ:
6152               s = htab->srelplt->output_section;
6153               BFD_ASSERT (s != NULL);
6154               if (s->_cooked_size != 0)
6155                 dyn.d_un.d_val = s->_cooked_size;
6156               else
6157                 dyn.d_un.d_val = s->_raw_size;
6158               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6159               break;
6160
6161             case DT_RELASZ:
6162               /* My reading of the SVR4 ABI indicates that the
6163                  procedure linkage table relocs (DT_JMPREL) should be
6164                  included in the overall relocs (DT_RELA).  This is
6165                  what Solaris does.  However, UnixWare can not handle
6166                  that case.  Therefore, we override the DT_RELASZ entry
6167                  here to make it not include the JMPREL relocs.  Since
6168                  the linker script arranges for .rela.plt to follow all
6169                  other relocation sections, we don't have to worry
6170                  about changing the DT_RELA entry.  */
6171               if (htab->srelplt != NULL)
6172                 {
6173                   s = htab->srelplt->output_section;
6174                   if (s->_cooked_size != 0)
6175                     dyn.d_un.d_val -= s->_cooked_size;
6176                   else
6177                     dyn.d_un.d_val -= s->_raw_size;
6178                 }
6179               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6180               break;
6181             }
6182         }
6183
6184       /* Fill in the first entry in the procedure linkage table.  */
6185       splt = htab->splt;
6186       if (splt && splt->_raw_size > 0)
6187         {
6188           if (info->shared)
6189             {
6190               if (elf_sh_pic_plt_entry == NULL)
6191                 {
6192                   elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
6193                                           elf_sh_pic_plt_entry_be :
6194                                           elf_sh_pic_plt_entry_le);
6195                 }
6196               memcpy (splt->contents, elf_sh_pic_plt_entry,
6197                       elf_sh_sizeof_plt (info));
6198             }
6199           else
6200             {
6201               if (elf_sh_plt0_entry == NULL)
6202                 {
6203                   elf_sh_plt0_entry = (bfd_big_endian (output_bfd) ?
6204                                        elf_sh_plt0_entry_be :
6205                                        elf_sh_plt0_entry_le);
6206                 }
6207               memcpy (splt->contents, elf_sh_plt0_entry, PLT_ENTRY_SIZE);
6208 #ifdef INCLUDE_SHMEDIA
6209               movi_shori_putval (output_bfd,
6210                                  sgot->output_section->vma
6211                                  + sgot->output_offset,
6212                                  splt->contents
6213                                  + elf_sh_plt0_gotplt_offset (info));
6214 #else
6215               bfd_put_32 (output_bfd,
6216                           sgot->output_section->vma + sgot->output_offset + 4,
6217                           splt->contents + elf_sh_plt0_gotid_offset (info));
6218               bfd_put_32 (output_bfd,
6219                           sgot->output_section->vma + sgot->output_offset + 8,
6220                           splt->contents + elf_sh_plt0_linker_offset (info));
6221 #endif
6222             }
6223
6224           /* UnixWare sets the entsize of .plt to 4, although that doesn't
6225              really seem like the right value.  */
6226           elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
6227         }
6228     }
6229
6230   /* Fill in the first three entries in the global offset table.  */
6231   if (sgot && sgot->_raw_size > 0)
6232     {
6233       if (sdyn == NULL)
6234         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
6235       else
6236         bfd_put_32 (output_bfd,
6237                     sdyn->output_section->vma + sdyn->output_offset,
6238                     sgot->contents);
6239       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
6240       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
6241
6242       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
6243     }
6244
6245   return true;
6246 }
6247
6248 static enum elf_reloc_type_class
6249 sh_elf_reloc_type_class (rela)
6250      const Elf_Internal_Rela *rela;
6251 {
6252   switch ((int) ELF32_R_TYPE (rela->r_info))
6253     {
6254     case R_SH_RELATIVE:
6255       return reloc_class_relative;
6256     case R_SH_JMP_SLOT:
6257       return reloc_class_plt;
6258     case R_SH_COPY:
6259       return reloc_class_copy;
6260     default:
6261       return reloc_class_normal;
6262     }
6263 }
6264
6265 #ifndef ELF_ARCH
6266 #define TARGET_BIG_SYM          bfd_elf32_sh_vec
6267 #define TARGET_BIG_NAME         "elf32-sh"
6268 #define TARGET_LITTLE_SYM       bfd_elf32_shl_vec
6269 #define TARGET_LITTLE_NAME      "elf32-shl"
6270 #define ELF_ARCH                bfd_arch_sh
6271 #define ELF_MACHINE_CODE        EM_SH
6272 #define ELF_MAXPAGESIZE         128
6273
6274 #define elf_symbol_leading_char '_'
6275 #endif /* ELF_ARCH */
6276
6277 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
6278 #define elf_info_to_howto               sh_elf_info_to_howto
6279 #define bfd_elf32_bfd_relax_section     sh_elf_relax_section
6280 #define elf_backend_relocate_section    sh_elf_relocate_section
6281 #define bfd_elf32_bfd_get_relocated_section_contents \
6282                                         sh_elf_get_relocated_section_contents
6283 #define elf_backend_object_p            sh_elf_set_mach_from_flags
6284 #define bfd_elf32_bfd_set_private_bfd_flags \
6285                                         sh_elf_set_private_flags
6286 #define bfd_elf32_bfd_copy_private_bfd_data \
6287                                         sh_elf_copy_private_data
6288 #define bfd_elf32_bfd_merge_private_bfd_data \
6289                                         sh_elf_merge_private_data
6290
6291 #define elf_backend_gc_mark_hook        sh_elf_gc_mark_hook
6292 #define elf_backend_gc_sweep_hook       sh_elf_gc_sweep_hook
6293 #define elf_backend_check_relocs        sh_elf_check_relocs
6294 #define elf_backend_copy_indirect_symbol \
6295                                         sh_elf_copy_indirect_symbol
6296 #define elf_backend_create_dynamic_sections \
6297                                         sh_elf_create_dynamic_sections
6298 #define bfd_elf32_bfd_link_hash_table_create \
6299                                         sh_elf_link_hash_table_create
6300 #define elf_backend_adjust_dynamic_symbol \
6301                                         sh_elf_adjust_dynamic_symbol
6302 #define elf_backend_size_dynamic_sections \
6303                                         sh_elf_size_dynamic_sections
6304 #define elf_backend_finish_dynamic_symbol \
6305                                         sh_elf_finish_dynamic_symbol
6306 #define elf_backend_finish_dynamic_sections \
6307                                         sh_elf_finish_dynamic_sections
6308 #define elf_backend_reloc_type_class    sh_elf_reloc_type_class
6309
6310 #define elf_backend_can_gc_sections     1
6311 #define elf_backend_can_refcount        1
6312 #define elf_backend_want_got_plt        1
6313 #define elf_backend_plt_readonly        1
6314 #define elf_backend_want_plt_sym        0
6315 #define elf_backend_got_header_size     12
6316 #define elf_backend_plt_header_size     PLT_ENTRY_SIZE
6317
6318 #ifndef ELF32_SH_C_INCLUDED
6319 #include "elf32-target.h"
6320 #endif
6321