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