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