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