* elf32-sh.c (elf_sh_link_hash_entry): Replace
[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   union
3509   {
3510     bfd_signed_vma refcount;
3511     bfd_vma offset;
3512   } datalabel_got;
3513 #endif
3514
3515   /* Track dynamic relocs copied for this symbol.  */
3516   struct elf_sh_dyn_relocs *dyn_relocs;
3517
3518   bfd_signed_vma gotplt_refcount;
3519
3520   enum {
3521     GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE
3522   } tls_type;
3523
3524   /* If true, R_SH_TLS_TPOFF32 relocation is generated.  */
3525   boolean tls_tpoff32;
3526 };
3527
3528 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
3529
3530 struct sh_elf_obj_tdata
3531 {
3532   struct elf_obj_tdata root;
3533
3534   /* tls_type for each local got entry.  */
3535   char *local_got_tls_type;
3536 };
3537
3538 #define sh_elf_tdata(abfd) \
3539   ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
3540
3541 #define sh_elf_local_got_tls_type(abfd) \
3542   (sh_elf_tdata (abfd)->local_got_tls_type)
3543
3544 /* Override the generic function because we need to store sh_elf_obj_tdata
3545    as the specific tdata.  */
3546
3547 static boolean
3548 sh_elf_mkobject (abfd)
3549      bfd *abfd;
3550 {
3551   bfd_size_type amt = sizeof (struct sh_elf_obj_tdata);
3552   abfd->tdata.any = bfd_zalloc (abfd, amt);
3553   if (abfd->tdata.any == NULL)
3554     return false;
3555   return true;
3556 }
3557                  
3558 /* sh ELF linker hash table.  */
3559
3560 struct elf_sh_link_hash_table
3561 {
3562   struct elf_link_hash_table root;
3563
3564   /* Short-cuts to get to dynamic linker sections.  */
3565   asection *sgot;
3566   asection *sgotplt;
3567   asection *srelgot;
3568   asection *splt;
3569   asection *srelplt;
3570   asection *sdynbss;
3571   asection *srelbss;
3572
3573   /* Small local sym to section mapping cache.  */
3574   struct sym_sec_cache sym_sec;
3575
3576   /* A counter or offset to track a TLS got entry.  */
3577   union
3578     {
3579       bfd_signed_vma refcount;
3580       bfd_vma offset;
3581     } tls_ldm_got;
3582 };
3583
3584 /* Traverse an sh ELF linker hash table.  */
3585
3586 #define sh_elf_link_hash_traverse(table, func, info)                    \
3587   (elf_link_hash_traverse                                               \
3588    (&(table)->root,                                                     \
3589     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
3590     (info)))
3591
3592 /* Get the sh ELF linker hash table from a link_info structure.  */
3593
3594 #define sh_elf_hash_table(p) \
3595   ((struct elf_sh_link_hash_table *) ((p)->hash))
3596
3597 /* Create an entry in an sh ELF linker hash table.  */
3598
3599 static struct bfd_hash_entry *
3600 sh_elf_link_hash_newfunc (entry, table, string)
3601      struct bfd_hash_entry *entry;
3602      struct bfd_hash_table *table;
3603      const char *string;
3604 {
3605   struct elf_sh_link_hash_entry *ret =
3606     (struct elf_sh_link_hash_entry *) entry;
3607
3608   /* Allocate the structure if it has not already been allocated by a
3609      subclass.  */
3610   if (ret == (struct elf_sh_link_hash_entry *) NULL)
3611     ret = ((struct elf_sh_link_hash_entry *)
3612            bfd_hash_allocate (table,
3613                               sizeof (struct elf_sh_link_hash_entry)));
3614   if (ret == (struct elf_sh_link_hash_entry *) NULL)
3615     return (struct bfd_hash_entry *) ret;
3616
3617   /* Call the allocation method of the superclass.  */
3618   ret = ((struct elf_sh_link_hash_entry *)
3619          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3620                                      table, string));
3621   if (ret != (struct elf_sh_link_hash_entry *) NULL)
3622     {
3623       ret->dyn_relocs = NULL;
3624       ret->gotplt_refcount = 0;
3625 #ifdef INCLUDE_SHMEDIA
3626       ret->datalabel_got.refcount = ret->root.got.refcount;
3627 #endif
3628       ret->tls_type = GOT_UNKNOWN;
3629       ret->tls_tpoff32 = false;
3630     }
3631
3632   return (struct bfd_hash_entry *) ret;
3633 }
3634
3635 /* Create an sh ELF linker hash table.  */
3636
3637 static struct bfd_link_hash_table *
3638 sh_elf_link_hash_table_create (abfd)
3639      bfd *abfd;
3640 {
3641   struct elf_sh_link_hash_table *ret;
3642   bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
3643
3644   ret = (struct elf_sh_link_hash_table *) bfd_malloc (amt);
3645   if (ret == (struct elf_sh_link_hash_table *) NULL)
3646     return NULL;
3647
3648   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3649                                        sh_elf_link_hash_newfunc))
3650     {
3651       free (ret);
3652       return NULL;
3653     }
3654
3655   ret->sgot = NULL;
3656   ret->sgotplt = NULL;
3657   ret->srelgot = NULL;
3658   ret->splt = NULL;
3659   ret->srelplt = NULL;
3660   ret->sdynbss = NULL;
3661   ret->srelbss = NULL;
3662   ret->sym_sec.abfd = NULL;
3663   ret->tls_ldm_got.refcount = 0;
3664
3665   return &ret->root.root;
3666 }
3667
3668 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3669    shortcuts to them in our hash table.  */
3670
3671 static boolean
3672 create_got_section (dynobj, info)
3673      bfd *dynobj;
3674      struct bfd_link_info *info;
3675 {
3676   struct elf_sh_link_hash_table *htab;
3677
3678   if (! _bfd_elf_create_got_section (dynobj, info))
3679     return false;
3680
3681   htab = sh_elf_hash_table (info);
3682   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
3683   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3684   if (! htab->sgot || ! htab->sgotplt)
3685     abort ();
3686
3687   htab->srelgot = bfd_make_section (dynobj, ".rela.got");
3688   if (htab->srelgot == NULL
3689       || ! bfd_set_section_flags (dynobj, htab->srelgot,
3690                                   (SEC_ALLOC
3691                                    | SEC_LOAD
3692                                    | SEC_HAS_CONTENTS
3693                                    | SEC_IN_MEMORY
3694                                    | SEC_LINKER_CREATED
3695                                    | SEC_READONLY))
3696       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
3697     return false;
3698   return true;
3699 }
3700
3701 /* Create dynamic sections when linking against a dynamic object.  */
3702
3703 static boolean
3704 sh_elf_create_dynamic_sections (abfd, info)
3705      bfd *abfd;
3706      struct bfd_link_info *info;
3707 {
3708   struct elf_sh_link_hash_table *htab;
3709   flagword flags, pltflags;
3710   register asection *s;
3711   struct elf_backend_data *bed = get_elf_backend_data (abfd);
3712   int ptralign = 0;
3713
3714   switch (bed->s->arch_size)
3715     {
3716     case 32:
3717       ptralign = 2;
3718       break;
3719
3720     case 64:
3721       ptralign = 3;
3722       break;
3723
3724     default:
3725       bfd_set_error (bfd_error_bad_value);
3726       return false;
3727     }
3728
3729   htab = sh_elf_hash_table (info);
3730
3731   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3732      .rel[a].bss sections.  */
3733
3734   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3735            | SEC_LINKER_CREATED);
3736
3737   pltflags = flags;
3738   pltflags |= SEC_CODE;
3739   if (bed->plt_not_loaded)
3740     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
3741   if (bed->plt_readonly)
3742     pltflags |= SEC_READONLY;
3743
3744   s = bfd_make_section (abfd, ".plt");
3745   htab->splt = s;
3746   if (s == NULL
3747       || ! bfd_set_section_flags (abfd, s, pltflags)
3748       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3749     return false;
3750
3751   if (bed->want_plt_sym)
3752     {
3753       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3754          .plt section.  */
3755       struct elf_link_hash_entry *h;
3756       struct bfd_link_hash_entry *bh = NULL;
3757
3758       if (! (_bfd_generic_link_add_one_symbol
3759              (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3760               (bfd_vma) 0, (const char *) NULL, false,
3761               get_elf_backend_data (abfd)->collect, &bh)))
3762         return false;
3763
3764       h = (struct elf_link_hash_entry *) bh;
3765       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3766       h->type = STT_OBJECT;
3767
3768       if (info->shared
3769           && ! _bfd_elf_link_record_dynamic_symbol (info, h))
3770         return false;
3771     }
3772
3773   s = bfd_make_section (abfd,
3774                         bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3775   htab->srelplt = s;
3776   if (s == NULL
3777       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3778       || ! bfd_set_section_alignment (abfd, s, ptralign))
3779     return false;
3780
3781   if (! create_got_section (abfd, info))
3782     return false;
3783
3784   {
3785     const char *secname;
3786     char *relname;
3787     flagword secflags;
3788     asection *sec;
3789
3790     for (sec = abfd->sections; sec; sec = sec->next)
3791       {
3792         secflags = bfd_get_section_flags (abfd, sec);
3793         if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3794             || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3795           continue;
3796         secname = bfd_get_section_name (abfd, sec);
3797         relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3798         strcpy (relname, ".rela");
3799         strcat (relname, secname);
3800         s = bfd_make_section (abfd, relname);
3801         if (s == NULL
3802             || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3803             || ! bfd_set_section_alignment (abfd, s, ptralign))
3804           return false;
3805       }
3806   }
3807
3808   if (bed->want_dynbss)
3809     {
3810       /* The .dynbss section is a place to put symbols which are defined
3811          by dynamic objects, are referenced by regular objects, and are
3812          not functions.  We must allocate space for them in the process
3813          image and use a R_*_COPY reloc to tell the dynamic linker to
3814          initialize them at run time.  The linker script puts the .dynbss
3815          section into the .bss section of the final image.  */
3816       s = bfd_make_section (abfd, ".dynbss");
3817       htab->sdynbss = s;
3818       if (s == NULL
3819           || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
3820         return false;
3821
3822       /* The .rel[a].bss section holds copy relocs.  This section is not
3823          normally needed.  We need to create it here, though, so that the
3824          linker will map it to an output section.  We can't just create it
3825          only if we need it, because we will not know whether we need it
3826          until we have seen all the input files, and the first time the
3827          main linker code calls BFD after examining all the input files
3828          (size_dynamic_sections) the input sections have already been
3829          mapped to the output sections.  If the section turns out not to
3830          be needed, we can discard it later.  We will never need this
3831          section when generating a shared object, since they do not use
3832          copy relocs.  */
3833       if (! info->shared)
3834         {
3835           s = bfd_make_section (abfd,
3836                                 (bed->default_use_rela_p
3837                                  ? ".rela.bss" : ".rel.bss"));
3838           htab->srelbss = s;
3839           if (s == NULL
3840               || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3841               || ! bfd_set_section_alignment (abfd, s, ptralign))
3842             return false;
3843         }
3844     }
3845
3846   return true;
3847 }
3848 \f
3849 /* Adjust a symbol defined by a dynamic object and referenced by a
3850    regular object.  The current definition is in some section of the
3851    dynamic object, but we're not including those sections.  We have to
3852    change the definition to something the rest of the link can
3853    understand.  */
3854
3855 static boolean
3856 sh_elf_adjust_dynamic_symbol (info, h)
3857      struct bfd_link_info *info;
3858      struct elf_link_hash_entry *h;
3859 {
3860   struct elf_sh_link_hash_table *htab;
3861   struct elf_sh_link_hash_entry *eh;
3862   struct elf_sh_dyn_relocs *p;
3863   bfd *dynobj;
3864   asection *s;
3865   unsigned int power_of_two;
3866
3867   dynobj = elf_hash_table (info)->dynobj;
3868
3869   /* Make sure we know what is going on here.  */
3870   BFD_ASSERT (dynobj != NULL
3871               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
3872                   || h->weakdef != NULL
3873                   || ((h->elf_link_hash_flags
3874                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3875                       && (h->elf_link_hash_flags
3876                           & ELF_LINK_HASH_REF_REGULAR) != 0
3877                       && (h->elf_link_hash_flags
3878                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
3879
3880   /* If this is a function, put it in the procedure linkage table.  We
3881      will fill in the contents of the procedure linkage table later,
3882      when we know the address of the .got section.  */
3883   if (h->type == STT_FUNC
3884       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3885     {
3886       if (h->plt.refcount <= 0
3887           || (! info->shared
3888               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3889               && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
3890               && h->root.type != bfd_link_hash_undefweak
3891               && h->root.type != bfd_link_hash_undefined))
3892         {
3893           /* This case can occur if we saw a PLT reloc in an input
3894              file, but the symbol was never referred to by a dynamic
3895              object.  In such a case, we don't actually need to build
3896              a procedure linkage table, and we can just do a REL32
3897              reloc instead.  */
3898           h->plt.offset = (bfd_vma) -1;
3899           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3900         }
3901
3902       return true;
3903     }
3904   else
3905     h->plt.offset = (bfd_vma) -1;
3906
3907   /* If this is a weak symbol, and there is a real definition, the
3908      processor independent code will have arranged for us to see the
3909      real definition first, and we can just use the same value.  */
3910   if (h->weakdef != NULL)
3911     {
3912       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3913                   || h->weakdef->root.type == bfd_link_hash_defweak);
3914       h->root.u.def.section = h->weakdef->root.u.def.section;
3915       h->root.u.def.value = h->weakdef->root.u.def.value;
3916       return true;
3917     }
3918
3919   /* This is a reference to a symbol defined by a dynamic object which
3920      is not a function.  */
3921
3922   /* If we are creating a shared library, we must presume that the
3923      only references to the symbol are via the global offset table.
3924      For such cases we need not do anything here; the relocations will
3925      be handled correctly by relocate_section.  */
3926   if (info->shared)
3927     return true;
3928
3929   /* If there are no references to this symbol that do not use the
3930      GOT, we don't need to generate a copy reloc.  */
3931   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
3932     return true;
3933
3934   /* If -z nocopyreloc was given, we won't generate them either.  */
3935   if (info->nocopyreloc)
3936     {
3937       h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3938       return true;
3939     }
3940
3941   eh = (struct elf_sh_link_hash_entry *) h;
3942   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3943     {
3944       s = p->sec->output_section;
3945       if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
3946         break;
3947     }
3948
3949   /* If we didn't find any dynamic relocs in sections which needs the
3950      copy reloc, then we'll be keeping the dynamic relocs and avoiding
3951      the copy reloc.  */
3952   if (p == NULL)
3953     {
3954       h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3955       return true;
3956     }
3957
3958   /* We must allocate the symbol in our .dynbss section, which will
3959      become part of the .bss section of the executable.  There will be
3960      an entry for this symbol in the .dynsym section.  The dynamic
3961      object will contain position independent code, so all references
3962      from the dynamic object to this symbol will go through the global
3963      offset table.  The dynamic linker will use the .dynsym entry to
3964      determine the address it must put in the global offset table, so
3965      both the dynamic object and the regular object will refer to the
3966      same memory location for the variable.  */
3967
3968   htab = sh_elf_hash_table (info);
3969   s = htab->sdynbss;
3970   BFD_ASSERT (s != NULL);
3971
3972   /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
3973      copy the initial value out of the dynamic object and into the
3974      runtime process image.  We need to remember the offset into the
3975      .rela.bss section we are going to use.  */
3976   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3977     {
3978       asection *srel;
3979
3980       srel = htab->srelbss;
3981       BFD_ASSERT (srel != NULL);
3982       srel->_raw_size += sizeof (Elf32_External_Rela);
3983       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
3984     }
3985
3986   /* We need to figure out the alignment required for this symbol.  I
3987      have no idea how ELF linkers handle this.  */
3988   power_of_two = bfd_log2 (h->size);
3989   if (power_of_two > 3)
3990     power_of_two = 3;
3991
3992   /* Apply the required alignment.  */
3993   s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
3994   if (power_of_two > bfd_get_section_alignment (dynobj, s))
3995     {
3996       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
3997         return false;
3998     }
3999
4000   /* Define the symbol as being at this point in the section.  */
4001   h->root.u.def.section = s;
4002   h->root.u.def.value = s->_raw_size;
4003
4004   /* Increment the section size to make room for the symbol.  */
4005   s->_raw_size += h->size;
4006
4007   return true;
4008 }
4009
4010 /* This is the condition under which sh_elf_finish_dynamic_symbol
4011    will be called from elflink.h.  If elflink.h doesn't call our
4012    finish_dynamic_symbol routine, we'll need to do something about
4013    initializing any .plt and .got entries in sh_elf_relocate_section.  */
4014 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
4015   ((DYN)                                                                \
4016    && ((INFO)->shared                                                   \
4017        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)      \
4018    && ((H)->dynindx != -1                                               \
4019        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
4020
4021 /* Allocate space in .plt, .got and associated reloc sections for
4022    dynamic relocs.  */
4023
4024 static boolean
4025 allocate_dynrelocs (h, inf)
4026      struct elf_link_hash_entry *h;
4027      PTR inf;
4028 {
4029   struct bfd_link_info *info;
4030   struct elf_sh_link_hash_table *htab;
4031   struct elf_sh_link_hash_entry *eh;
4032   struct elf_sh_dyn_relocs *p;
4033
4034   if (h->root.type == bfd_link_hash_indirect)
4035     return true;
4036
4037   if (h->root.type == bfd_link_hash_warning)
4038     /* When warning symbols are created, they **replace** the "real"
4039        entry in the hash table, thus we never get to see the real
4040        symbol in a hash traversal.  So look at it now.  */
4041     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4042
4043   info = (struct bfd_link_info *) inf;
4044   htab = sh_elf_hash_table (info);
4045
4046   eh = (struct elf_sh_link_hash_entry *) h;
4047   if ((h->got.refcount > 0
4048       || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
4049       && eh->gotplt_refcount > 0)
4050     {
4051       /* The symbol has been forced local, or we have some direct got refs,
4052          so treat all the gotplt refs as got refs. */
4053       h->got.refcount += eh->gotplt_refcount;
4054       if (h->plt.refcount >= eh->gotplt_refcount)
4055         h->plt.refcount -= eh->gotplt_refcount;
4056     }
4057
4058   if (htab->root.dynamic_sections_created
4059       && h->plt.refcount > 0)
4060     {
4061       /* Make sure this symbol is output as a dynamic symbol.
4062          Undefined weak syms won't yet be marked as dynamic.  */
4063       if (h->dynindx == -1
4064           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4065         {
4066           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4067             return false;
4068         }
4069
4070       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
4071         {
4072           asection *s = htab->splt;
4073
4074           /* If this is the first .plt entry, make room for the special
4075              first entry.  */
4076           if (s->_raw_size == 0)
4077             s->_raw_size += PLT_ENTRY_SIZE;
4078
4079           h->plt.offset = s->_raw_size;
4080
4081           /* If this symbol is not defined in a regular file, and we are
4082              not generating a shared library, then set the symbol to this
4083              location in the .plt.  This is required to make function
4084              pointers compare as equal between the normal executable and
4085              the shared library.  */
4086           if (! info->shared
4087               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4088             {
4089               h->root.u.def.section = s;
4090               h->root.u.def.value = h->plt.offset;
4091             }
4092
4093           /* Make room for this entry.  */
4094           s->_raw_size += PLT_ENTRY_SIZE;
4095
4096           /* We also need to make an entry in the .got.plt section, which
4097              will be placed in the .got section by the linker script.  */
4098           htab->sgotplt->_raw_size += 4;
4099
4100           /* We also need to make an entry in the .rel.plt section.  */
4101           htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
4102         }
4103       else
4104         {
4105           h->plt.offset = (bfd_vma) -1;
4106           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4107         }
4108     }
4109   else
4110     {
4111       h->plt.offset = (bfd_vma) -1;
4112       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4113     }
4114
4115   if (h->got.refcount > 0)
4116     {
4117       asection *s;
4118       boolean dyn;
4119       int tls_type = sh_elf_hash_entry (h)->tls_type;
4120
4121       /* Make sure this symbol is output as a dynamic symbol.
4122          Undefined weak syms won't yet be marked as dynamic.  */
4123       if (h->dynindx == -1
4124           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4125         {
4126           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4127             return false;
4128         }
4129
4130       s = htab->sgot;
4131       h->got.offset = s->_raw_size;
4132       s->_raw_size += 4;
4133       /* R_SH_TLS_GD needs 2 consecutive GOT slots.  */
4134       if (tls_type == GOT_TLS_GD)
4135         s->_raw_size += 4;
4136       dyn = htab->root.dynamic_sections_created;
4137       /* R_SH_TLS_IE_32 needs one dynamic relocation,
4138          R_SH_TLS_GD needs one if local symbol and two if global.  */
4139       if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
4140           || tls_type == GOT_TLS_IE)
4141         htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4142       else if (tls_type == GOT_TLS_GD)
4143         htab->srelgot->_raw_size += 2 * sizeof (Elf32_External_Rela);
4144       else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
4145         htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4146     }
4147   else
4148     h->got.offset = (bfd_vma) -1;
4149
4150 #ifdef INCLUDE_SHMEDIA
4151   if (eh->datalabel_got.refcount > 0)
4152     {
4153       asection *s;
4154       boolean dyn;
4155
4156       /* Make sure this symbol is output as a dynamic symbol.
4157          Undefined weak syms won't yet be marked as dynamic.  */
4158       if (h->dynindx == -1
4159           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4160         {
4161           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4162             return false;
4163         }
4164
4165       s = htab->sgot;
4166       eh->datalabel_got.offset = s->_raw_size;
4167       s->_raw_size += 4;
4168       dyn = htab->root.dynamic_sections_created;
4169       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
4170         htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4171     }
4172   else
4173     eh->datalabel_got.offset = (bfd_vma) -1;
4174 #endif
4175
4176   if (eh->dyn_relocs == NULL)
4177     return true;
4178
4179   /* In the shared -Bsymbolic case, discard space allocated for
4180      dynamic pc-relative relocs against symbols which turn out to be
4181      defined in regular objects.  For the normal shared case, discard
4182      space for pc-relative relocs that have become local due to symbol
4183      visibility changes.  */
4184
4185   if (info->shared)
4186     {
4187       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4188           && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
4189               || info->symbolic))
4190         {
4191           struct elf_sh_dyn_relocs **pp;
4192
4193           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
4194             {
4195               p->count -= p->pc_count;
4196               p->pc_count = 0;
4197               if (p->count == 0)
4198                 *pp = p->next;
4199               else
4200                 pp = &p->next;
4201             }
4202         }
4203     }
4204   else
4205     {
4206       if (sh_elf_hash_entry (h)->tls_tpoff32)
4207         goto keep;
4208
4209       /* For the non-shared case, discard space for relocs against
4210          symbols which turn out to need copy relocs or are not
4211          dynamic.  */
4212
4213       if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
4214           && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4215                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4216               || (htab->root.dynamic_sections_created
4217                   && (h->root.type == bfd_link_hash_undefweak
4218                       || h->root.type == bfd_link_hash_undefined))))
4219         {
4220           /* Make sure this symbol is output as a dynamic symbol.
4221              Undefined weak syms won't yet be marked as dynamic.  */
4222           if (h->dynindx == -1
4223               && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4224             {
4225               if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4226                 return false;
4227             }
4228
4229           /* If that succeeded, we know we'll be keeping all the
4230              relocs.  */
4231           if (h->dynindx != -1)
4232             goto keep;
4233         }
4234
4235       eh->dyn_relocs = NULL;
4236
4237     keep: ;
4238     }
4239
4240   /* Finally, allocate space.  */
4241   for (p = eh->dyn_relocs; p != NULL; p = p->next)
4242     {
4243       asection *sreloc = elf_section_data (p->sec)->sreloc;
4244       sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
4245     }
4246
4247   return true;
4248 }
4249
4250 /* Find any dynamic relocs that apply to read-only sections.  */
4251
4252 static boolean
4253 readonly_dynrelocs (h, inf)
4254      struct elf_link_hash_entry *h;
4255      PTR inf;
4256 {
4257   struct elf_sh_link_hash_entry *eh;
4258   struct elf_sh_dyn_relocs *p;
4259
4260   if (h->root.type == bfd_link_hash_warning)
4261     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4262
4263   eh = (struct elf_sh_link_hash_entry *) h;
4264   for (p = eh->dyn_relocs; p != NULL; p = p->next)
4265     {
4266       asection *s = p->sec->output_section;
4267
4268       if (s != NULL && (s->flags & SEC_READONLY) != 0)
4269         {
4270           struct bfd_link_info *info = (struct bfd_link_info *) inf;
4271
4272           info->flags |= DF_TEXTREL;
4273
4274           /* Not an error, just cut short the traversal.  */
4275           return false;
4276         }
4277     }
4278   return true;
4279 }
4280
4281 /* Set the sizes of the dynamic sections.  */
4282
4283 static boolean
4284 sh_elf_size_dynamic_sections (output_bfd, info)
4285      bfd *output_bfd ATTRIBUTE_UNUSED;
4286      struct bfd_link_info *info;
4287 {
4288   struct elf_sh_link_hash_table *htab;
4289   bfd *dynobj;
4290   asection *s;
4291   boolean relocs;
4292   bfd *ibfd;
4293
4294   htab = sh_elf_hash_table (info);
4295   dynobj = htab->root.dynobj;
4296   BFD_ASSERT (dynobj != NULL);
4297
4298   if (htab->root.dynamic_sections_created)
4299     {
4300       /* Set the contents of the .interp section to the interpreter.  */
4301       if (! info->shared)
4302         {
4303           s = bfd_get_section_by_name (dynobj, ".interp");
4304           BFD_ASSERT (s != NULL);
4305           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4306           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4307         }
4308     }
4309
4310   /* Set up .got offsets for local syms, and space for local dynamic
4311      relocs.  */
4312   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4313     {
4314       bfd_signed_vma *local_got;
4315       bfd_signed_vma *end_local_got;
4316       char *local_tls_type;
4317       bfd_size_type locsymcount;
4318       Elf_Internal_Shdr *symtab_hdr;
4319       asection *srel;
4320
4321       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4322         continue;
4323
4324       for (s = ibfd->sections; s != NULL; s = s->next)
4325         {
4326           struct elf_sh_dyn_relocs *p;
4327
4328           for (p = ((struct elf_sh_dyn_relocs *)
4329                     elf_section_data (s)->local_dynrel);
4330                p != NULL;
4331                p = p->next)
4332             {
4333               if (! bfd_is_abs_section (p->sec)
4334                   && bfd_is_abs_section (p->sec->output_section))
4335                 {
4336                   /* Input section has been discarded, either because
4337                      it is a copy of a linkonce section or due to
4338                      linker script /DISCARD/, so we'll be discarding
4339                      the relocs too.  */
4340                 }
4341               else if (p->count != 0)
4342                 {
4343                   srel = elf_section_data (p->sec)->sreloc;
4344                   srel->_raw_size += p->count * sizeof (Elf32_External_Rela);
4345                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4346                     info->flags |= DF_TEXTREL;
4347                 }
4348             }
4349         }
4350
4351       local_got = elf_local_got_refcounts (ibfd);
4352       if (!local_got)
4353         continue;
4354
4355       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4356       locsymcount = symtab_hdr->sh_info;
4357 #ifdef INCLUDE_SHMEDIA
4358       /* Count datalabel local GOT.  */
4359       locsymcount *= 2;
4360 #endif
4361       end_local_got = local_got + locsymcount;
4362       local_tls_type = sh_elf_local_got_tls_type (ibfd);
4363       s = htab->sgot;
4364       srel = htab->srelgot;
4365       for (; local_got < end_local_got; ++local_got)
4366         {
4367           if (*local_got > 0)
4368             {
4369               *local_got = s->_raw_size;
4370               s->_raw_size += 4;
4371               if (*local_tls_type == GOT_TLS_GD)
4372                 s->_raw_size += 4;
4373               if (info->shared)
4374                 srel->_raw_size += sizeof (Elf32_External_Rela);
4375             }
4376           else
4377             *local_got = (bfd_vma) -1;
4378           ++local_tls_type;
4379         }
4380     }
4381
4382   if (htab->tls_ldm_got.refcount > 0)
4383     {
4384       /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
4385          relocs.  */
4386       htab->tls_ldm_got.offset = htab->sgot->_raw_size;
4387       htab->sgot->_raw_size += 8;
4388       htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
4389     }
4390   else
4391     htab->tls_ldm_got.offset = -1;
4392
4393   /* Allocate global sym .plt and .got entries, and space for global
4394      sym dynamic relocs.  */
4395   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
4396
4397   /* We now have determined the sizes of the various dynamic sections.
4398      Allocate memory for them.  */
4399   relocs = false;
4400   for (s = dynobj->sections; s != NULL; s = s->next)
4401     {
4402       if ((s->flags & SEC_LINKER_CREATED) == 0)
4403         continue;
4404
4405       if (s == htab->splt
4406           || s == htab->sgot
4407           || s == htab->sgotplt)
4408         {
4409           /* Strip this section if we don't need it; see the
4410              comment below.  */
4411         }
4412       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4413         {
4414           if (s->_raw_size != 0 && s != htab->srelplt)
4415             relocs = true;
4416
4417           /* We use the reloc_count field as a counter if we need
4418              to copy relocs into the output file.  */
4419           s->reloc_count = 0;
4420         }
4421       else
4422         {
4423           /* It's not one of our sections, so don't allocate space.  */
4424           continue;
4425         }
4426
4427       if (s->_raw_size == 0)
4428         {
4429           /* If we don't need this section, strip it from the
4430              output file.  This is mostly to handle .rela.bss and
4431              .rela.plt.  We must create both sections in
4432              create_dynamic_sections, because they must be created
4433              before the linker maps input sections to output
4434              sections.  The linker does that before
4435              adjust_dynamic_symbol is called, and it is that
4436              function which decides whether anything needs to go
4437              into these sections.  */
4438
4439           _bfd_strip_section_from_output (info, s);
4440           continue;
4441         }
4442
4443       /* Allocate memory for the section contents.  We use bfd_zalloc
4444          here in case unused entries are not reclaimed before the
4445          section's contents are written out.  This should not happen,
4446          but this way if it does, we get a R_SH_NONE reloc instead
4447          of garbage.  */
4448       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4449       if (s->contents == NULL)
4450         return false;
4451     }
4452
4453   if (htab->root.dynamic_sections_created)
4454     {
4455       /* Add some entries to the .dynamic section.  We fill in the
4456          values later, in sh_elf_finish_dynamic_sections, but we
4457          must add the entries now so that we get the correct size for
4458          the .dynamic section.  The DT_DEBUG entry is filled in by the
4459          dynamic linker and used by the debugger.  */
4460 #define add_dynamic_entry(TAG, VAL) \
4461   bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
4462
4463       if (! info->shared)
4464         {
4465           if (! add_dynamic_entry (DT_DEBUG, 0))
4466             return false;
4467         }
4468
4469       if (htab->splt->_raw_size != 0)
4470         {
4471           if (! add_dynamic_entry (DT_PLTGOT, 0)
4472               || ! add_dynamic_entry (DT_PLTRELSZ, 0)
4473               || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
4474               || ! add_dynamic_entry (DT_JMPREL, 0))
4475             return false;
4476         }
4477
4478       if (relocs)
4479         {
4480           if (! add_dynamic_entry (DT_RELA, 0)
4481               || ! add_dynamic_entry (DT_RELASZ, 0)
4482               || ! add_dynamic_entry (DT_RELAENT,
4483                                       sizeof (Elf32_External_Rela)))
4484             return false;
4485
4486           /* If any dynamic relocs apply to a read-only section,
4487              then we need a DT_TEXTREL entry.  */
4488           if ((info->flags & DF_TEXTREL) == 0)
4489             elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
4490                                     (PTR) info);
4491
4492           if ((info->flags & DF_TEXTREL) != 0)
4493             {
4494               if (! add_dynamic_entry (DT_TEXTREL, 0))
4495                 return false;
4496             }
4497         }
4498     }
4499 #undef add_dynamic_entry
4500
4501   return true;
4502 }
4503 \f
4504 /* Relocate an SH ELF section.  */
4505
4506 static boolean
4507 sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4508                          contents, relocs, local_syms, local_sections)
4509      bfd *output_bfd;
4510      struct bfd_link_info *info;
4511      bfd *input_bfd;
4512      asection *input_section;
4513      bfd_byte *contents;
4514      Elf_Internal_Rela *relocs;
4515      Elf_Internal_Sym *local_syms;
4516      asection **local_sections;
4517 {
4518   struct elf_sh_link_hash_table *htab;
4519   Elf_Internal_Shdr *symtab_hdr;
4520   struct elf_link_hash_entry **sym_hashes;
4521   Elf_Internal_Rela *rel, *relend;
4522   bfd *dynobj;
4523   bfd_vma *local_got_offsets;
4524   asection *sgot;
4525   asection *sgotplt;
4526   asection *splt;
4527   asection *sreloc;
4528   asection *srelgot;
4529
4530   htab = sh_elf_hash_table (info);
4531   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4532   sym_hashes = elf_sym_hashes (input_bfd);
4533   dynobj = htab->root.dynobj;
4534   local_got_offsets = elf_local_got_offsets (input_bfd);
4535
4536   sgot = htab->sgot;
4537   sgotplt = htab->sgotplt;
4538   splt = htab->splt;
4539   sreloc = NULL;
4540   srelgot = NULL;
4541
4542   rel = relocs;
4543   relend = relocs + input_section->reloc_count;
4544   for (; rel < relend; rel++)
4545     {
4546       int r_type;
4547       reloc_howto_type *howto;
4548       unsigned long r_symndx;
4549       Elf_Internal_Sym *sym;
4550       asection *sec;
4551       struct elf_link_hash_entry *h;
4552       bfd_vma relocation;
4553       bfd_vma addend = (bfd_vma) 0;
4554       bfd_reloc_status_type r;
4555       int seen_stt_datalabel = 0;
4556       bfd_vma off;
4557       int tls_type;
4558
4559       r_symndx = ELF32_R_SYM (rel->r_info);
4560
4561       r_type = ELF32_R_TYPE (rel->r_info);
4562
4563       /* Many of the relocs are only used for relaxing, and are
4564          handled entirely by the relaxation code.  */
4565       if (r_type > (int) R_SH_LAST_INVALID_RELOC
4566           && r_type < (int) R_SH_LOOP_START)
4567         continue;
4568       if (r_type == (int) R_SH_NONE)
4569         continue;
4570
4571       if (r_type < 0
4572           || r_type >= R_SH_max
4573           || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
4574               && r_type <= (int) R_SH_LAST_INVALID_RELOC)
4575           || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
4576               && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
4577           || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
4578               && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
4579           || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
4580               && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
4581           || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
4582               && r_type <= (int) R_SH_LAST_INVALID_RELOC_2))
4583         {
4584           bfd_set_error (bfd_error_bad_value);
4585           return false;
4586         }
4587
4588       howto = sh_elf_howto_table + r_type;
4589
4590       /* For relocs that aren't partial_inplace, we get the addend from
4591          the relocation.  */
4592       if (! howto->partial_inplace)
4593         addend = rel->r_addend;
4594
4595       h = NULL;
4596       sym = NULL;
4597       sec = NULL;
4598       if (r_symndx < symtab_hdr->sh_info)
4599         {
4600           sym = local_syms + r_symndx;
4601           sec = local_sections[r_symndx];
4602           relocation = (sec->output_section->vma
4603                         + sec->output_offset
4604                         + sym->st_value);
4605           /* A local symbol never has STO_SH5_ISA32, so we don't need
4606              datalabel processing here.  Make sure this does not change
4607              without notice.  */
4608           if ((sym->st_other & STO_SH5_ISA32) != 0)
4609             ((*info->callbacks->reloc_dangerous)
4610              (info,
4611               _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
4612               input_bfd, input_section, rel->r_offset));
4613           if (info->relocateable)
4614             {
4615               /* This is a relocateable link.  We don't have to change
4616                  anything, unless the reloc is against a section symbol,
4617                  in which case we have to adjust according to where the
4618                  section symbol winds up in the output section.  */
4619               sym = local_syms + r_symndx;
4620               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4621                 {
4622                   if (! howto->partial_inplace)
4623                     {
4624                       /* For relocations with the addend in the
4625                          relocation, we need just to update the addend.
4626                          All real relocs are of type partial_inplace; this
4627                          code is mostly for completeness.  */
4628                       rel->r_addend += sec->output_offset + sym->st_value;
4629
4630                       continue;
4631                     }
4632
4633                   /* Relocs of type partial_inplace need to pick up the
4634                      contents in the contents and add the offset resulting
4635                      from the changed location of the section symbol.
4636                      Using _bfd_final_link_relocate (e.g. goto
4637                      final_link_relocate) here would be wrong, because
4638                      relocations marked pc_relative would get the current
4639                      location subtracted, and we must only do that at the
4640                      final link.  */
4641                   r = _bfd_relocate_contents (howto, input_bfd,
4642                                               sec->output_offset
4643                                               + sym->st_value,
4644                                               contents + rel->r_offset);
4645                   goto relocation_done;
4646                 }
4647
4648               continue;
4649             }
4650           else if (! howto->partial_inplace)
4651             {
4652               relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
4653               addend = rel->r_addend;
4654             }
4655           else if ((sec->flags & SEC_MERGE)
4656                    && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4657             {
4658               asection *msec;
4659
4660               if (howto->rightshift || howto->src_mask != 0xffffffff)
4661                 {
4662                   (*_bfd_error_handler)
4663                     (_("%s(%s+0x%lx): %s relocation against SEC_MERGE section"),
4664                      bfd_archive_filename (input_bfd),
4665                      bfd_get_section_name (input_bfd, input_section),
4666                      (long) rel->r_offset, howto->name);
4667                   return false;
4668                 }
4669
4670               addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4671               msec = sec;
4672               addend =
4673                 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
4674                 - relocation;
4675               addend += msec->output_section->vma + msec->output_offset;
4676               bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
4677               addend = 0;
4678             }
4679         }
4680       else
4681         {
4682           /* Section symbol are never (?) placed in the hash table, so
4683              we can just ignore hash relocations when creating a
4684              relocateable object file.  */
4685           if (info->relocateable)
4686             continue;
4687
4688           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4689           while (h->root.type == bfd_link_hash_indirect
4690                  || h->root.type == bfd_link_hash_warning)
4691             {
4692 #ifdef INCLUDE_SHMEDIA
4693               /* If the reference passes a symbol marked with
4694                  STT_DATALABEL, then any STO_SH5_ISA32 on the final value
4695                  doesn't count.  */
4696               seen_stt_datalabel |= h->type == STT_DATALABEL;
4697 #endif
4698               h = (struct elf_link_hash_entry *) h->root.u.i.link;
4699             }
4700           if (h->root.type == bfd_link_hash_defined
4701               || h->root.type == bfd_link_hash_defweak)
4702             {
4703               boolean dyn;
4704
4705               dyn = htab->root.dynamic_sections_created;
4706               sec = h->root.u.def.section;
4707               /* In these cases, we don't need the relocation value.
4708                  We check specially because in some obscure cases
4709                  sec->output_section will be NULL.  */
4710               if (r_type == R_SH_GOTPC
4711                   || r_type == R_SH_GOTPC_LOW16
4712                   || r_type == R_SH_GOTPC_MEDLOW16
4713                   || r_type == R_SH_GOTPC_MEDHI16
4714                   || r_type == R_SH_GOTPC_HI16
4715                   || ((r_type == R_SH_PLT32
4716                        || r_type == R_SH_PLT_LOW16
4717                        || r_type == R_SH_PLT_MEDLOW16
4718                        || r_type == R_SH_PLT_MEDHI16
4719                        || r_type == R_SH_PLT_HI16)
4720                       && h->plt.offset != (bfd_vma) -1)
4721                   || ((r_type == R_SH_GOT32
4722                        || r_type == R_SH_GOT_LOW16
4723                        || r_type == R_SH_GOT_MEDLOW16
4724                        || r_type == R_SH_GOT_MEDHI16
4725                        || r_type == R_SH_GOT_HI16)
4726                       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
4727                       && (! info->shared
4728                           || (! info->symbolic && h->dynindx != -1)
4729                           || (h->elf_link_hash_flags
4730                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
4731                   /* The cases above are those in which relocation is
4732                      overwritten in the switch block below.  The cases
4733                      below are those in which we must defer relocation
4734                      to run-time, because we can't resolve absolute
4735                      addresses when creating a shared library.  */
4736                   || (info->shared
4737                       && ((! info->symbolic && h->dynindx != -1)
4738                           || (h->elf_link_hash_flags
4739                               & ELF_LINK_HASH_DEF_REGULAR) == 0)
4740                       && ((r_type == R_SH_DIR32
4741                            && (h->elf_link_hash_flags
4742                                & ELF_LINK_FORCED_LOCAL) == 0)
4743                           || r_type == R_SH_REL32)
4744                       && ((input_section->flags & SEC_ALLOC) != 0
4745                           /* DWARF will emit R_SH_DIR32 relocations in its
4746                              sections against symbols defined externally
4747                              in shared libraries.  We can't do anything
4748                              with them here.  */
4749                           || ((input_section->flags & SEC_DEBUGGING) != 0
4750                               && (h->elf_link_hash_flags
4751                                   & ELF_LINK_HASH_DEF_DYNAMIC) != 0)))
4752                   || (sec->output_section == NULL
4753                       && (sh_elf_hash_entry (h)->tls_type == GOT_TLS_IE
4754                           || sh_elf_hash_entry (h)->tls_type == GOT_TLS_GD)))
4755                 relocation = 0;
4756               else if (sec->output_section == NULL)
4757                 {
4758                   (*_bfd_error_handler)
4759                     (_("%s: unresolvable relocation against symbol `%s' from %s section"),
4760                      bfd_archive_filename (input_bfd), h->root.root.string,
4761                      bfd_get_section_name (input_bfd, input_section));
4762                   return false;
4763                 }
4764               else
4765                 relocation = ((h->root.u.def.value
4766                               + sec->output_section->vma
4767                               + sec->output_offset)
4768                               /* A STO_SH5_ISA32 causes a "bitor 1" to the
4769                                  symbol value, unless we've seen
4770                                  STT_DATALABEL on the way to it.  */
4771                               | ((h->other & STO_SH5_ISA32) != 0
4772                                  && ! seen_stt_datalabel));
4773             }
4774           else if (h->root.type == bfd_link_hash_undefweak)
4775             relocation = 0;
4776           else if (info->shared
4777                    && (! info->symbolic || info->allow_shlib_undefined)
4778                    && ! info->no_undefined
4779                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4780             relocation = 0;
4781           else
4782             {
4783               if (! ((*info->callbacks->undefined_symbol)
4784                      (info, h->root.root.string, input_bfd,
4785                       input_section, rel->r_offset,
4786                       (!info->shared || info->no_undefined
4787                        || ELF_ST_VISIBILITY (h->other)))))
4788                 return false;
4789               relocation = 0;
4790             }
4791         }
4792
4793       switch ((int) r_type)
4794         {
4795         final_link_relocate:
4796           /* COFF relocs don't use the addend. The addend is used for
4797              R_SH_DIR32 to be compatible with other compilers.  */
4798           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4799                                         contents, rel->r_offset,
4800                                         relocation, addend);
4801           break;
4802
4803         case R_SH_IND12W:
4804           relocation -= 4;
4805           goto final_link_relocate;
4806
4807         case R_SH_DIR8WPN:
4808         case R_SH_DIR8WPZ:
4809         case R_SH_DIR8WPL:
4810           /* If the reloc is against the start of this section, then
4811              the assembler has already taken care of it and the reloc
4812              is here only to assist in relaxing.  If the reloc is not
4813              against the start of this section, then it's against an
4814              external symbol and we must deal with it ourselves.  */
4815           if (input_section->output_section->vma + input_section->output_offset
4816               != relocation)
4817             {
4818               int disp = (relocation
4819                           - input_section->output_section->vma
4820                           - input_section->output_offset
4821                           - rel->r_offset);
4822               int mask = 0;
4823               switch (r_type)
4824                 {
4825                 case R_SH_DIR8WPN:
4826                 case R_SH_DIR8WPZ: mask = 1; break;
4827                 case R_SH_DIR8WPL: mask = 3; break;
4828                 default: mask = 0; break;
4829                 }
4830               if (disp & mask)
4831                 {
4832                   ((*_bfd_error_handler)
4833                    (_("%s: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
4834                     bfd_archive_filename (input_section->owner),
4835                     (unsigned long) rel->r_offset));
4836                   bfd_set_error (bfd_error_bad_value);
4837                   return false;
4838                 }
4839               relocation -= 4;
4840               goto final_link_relocate;
4841             }
4842           r = bfd_reloc_ok;
4843           break;
4844
4845         default:
4846 #ifdef INCLUDE_SHMEDIA
4847           if (shmedia_prepare_reloc (info, input_bfd, input_section,
4848                                      contents, rel, &relocation))
4849             goto final_link_relocate;
4850 #endif
4851           bfd_set_error (bfd_error_bad_value);
4852           return false;
4853
4854         case R_SH_DIR32:
4855         case R_SH_REL32:
4856           if (info->shared
4857               && r_symndx != 0
4858               && (input_section->flags & SEC_ALLOC) != 0
4859               && (r_type != R_SH_REL32
4860                   || (h != NULL
4861                       && h->dynindx != -1
4862                       && (! info->symbolic
4863                           || (h->elf_link_hash_flags
4864                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
4865             {
4866               Elf_Internal_Rela outrel;
4867               boolean skip, relocate;
4868
4869               /* When generating a shared object, these relocations
4870                  are copied into the output file to be resolved at run
4871                  time.  */
4872
4873               if (sreloc == NULL)
4874                 {
4875                   const char *name;
4876
4877                   name = (bfd_elf_string_from_elf_section
4878                           (input_bfd,
4879                            elf_elfheader (input_bfd)->e_shstrndx,
4880                            elf_section_data (input_section)->rel_hdr.sh_name));
4881                   if (name == NULL)
4882                     return false;
4883
4884                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4885                               && strcmp (bfd_get_section_name (input_bfd,
4886                                                                input_section),
4887                                          name + 5) == 0);
4888
4889                   sreloc = bfd_get_section_by_name (dynobj, name);
4890                   BFD_ASSERT (sreloc != NULL);
4891                 }
4892
4893               skip = false;
4894               relocate = false;
4895
4896               outrel.r_offset =
4897                 _bfd_elf_section_offset (output_bfd, info, input_section,
4898                                          rel->r_offset);
4899               if (outrel.r_offset == (bfd_vma) -1)
4900                 skip = true;
4901               else if (outrel.r_offset == (bfd_vma) -2)
4902                 skip = true, relocate = true;
4903               outrel.r_offset += (input_section->output_section->vma
4904                                   + input_section->output_offset);
4905
4906               if (skip)
4907                 memset (&outrel, 0, sizeof outrel);
4908               else if (r_type == R_SH_REL32)
4909                 {
4910                   BFD_ASSERT (h != NULL && h->dynindx != -1);
4911                   outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
4912                   outrel.r_addend
4913                     = bfd_get_32 (input_bfd, contents + rel->r_offset);
4914                 }
4915               else
4916                 {
4917                   /* h->dynindx may be -1 if this symbol was marked to
4918                      become local.  */
4919                   if (h == NULL
4920                       || ((info->symbolic || h->dynindx == -1)
4921                           && (h->elf_link_hash_flags
4922                               & ELF_LINK_HASH_DEF_REGULAR) != 0))
4923                     {
4924                       relocate = true;
4925                       outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4926                       outrel.r_addend
4927                         = relocation + bfd_get_32 (input_bfd,
4928                                                    contents + rel->r_offset);
4929                     }
4930                   else
4931                     {
4932                       BFD_ASSERT (h->dynindx != -1);
4933                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
4934                       outrel.r_addend
4935                         = relocation + bfd_get_32 (input_bfd,
4936                                                    contents + rel->r_offset);
4937                     }
4938                 }
4939
4940               bfd_elf32_swap_reloca_out (output_bfd, &outrel,
4941                                          (((Elf32_External_Rela *)
4942                                            sreloc->contents)
4943                                           + sreloc->reloc_count));
4944               ++sreloc->reloc_count;
4945
4946               /* If this reloc is against an external symbol, we do
4947                  not want to fiddle with the addend.  Otherwise, we
4948                  need to include the symbol value so that it becomes
4949                  an addend for the dynamic reloc.  */
4950               if (! relocate)
4951                 continue;
4952             }
4953           goto final_link_relocate;
4954
4955         case R_SH_GOTPLT32:
4956 #ifdef INCLUDE_SHMEDIA
4957         case R_SH_GOTPLT_LOW16:
4958         case R_SH_GOTPLT_MEDLOW16:
4959         case R_SH_GOTPLT_MEDHI16:
4960         case R_SH_GOTPLT_HI16:
4961         case R_SH_GOTPLT10BY4:
4962         case R_SH_GOTPLT10BY8:
4963 #endif
4964           /* Relocation is to the entry for this symbol in the
4965              procedure linkage table.  */
4966
4967           if (h == NULL
4968               || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
4969               || ! info->shared
4970               || info->symbolic
4971               || h->dynindx == -1
4972               || h->plt.offset == (bfd_vma) -1
4973               || h->got.offset != (bfd_vma) -1)
4974             goto force_got;
4975
4976           /* Relocation is to the entry for this symbol in the global
4977              offset table extension for the procedure linkage table.  */
4978
4979           BFD_ASSERT (sgotplt != NULL);
4980           relocation = (sgotplt->output_offset
4981                         + ((h->plt.offset / elf_sh_sizeof_plt (info)
4982                             - 1 + 3) * 4));
4983
4984 #ifdef GOT_BIAS
4985           relocation -= GOT_BIAS;
4986 #endif
4987
4988           goto final_link_relocate;
4989
4990         force_got:
4991         case R_SH_GOT32:
4992 #ifdef INCLUDE_SHMEDIA
4993         case R_SH_GOT_LOW16:
4994         case R_SH_GOT_MEDLOW16:
4995         case R_SH_GOT_MEDHI16:
4996         case R_SH_GOT_HI16:
4997         case R_SH_GOT10BY4:
4998         case R_SH_GOT10BY8:
4999 #endif
5000           /* Relocation is to the entry for this symbol in the global
5001              offset table.  */
5002
5003           BFD_ASSERT (sgot != NULL);
5004
5005           if (h != NULL)
5006             {
5007               boolean dyn;
5008
5009               off = h->got.offset;
5010 #ifdef INCLUDE_SHMEDIA
5011               if (seen_stt_datalabel)
5012                 {
5013                   struct elf_sh_link_hash_entry *hsh;
5014
5015                   hsh = (struct elf_sh_link_hash_entry *)h;
5016                   off = hsh->datalabel_got.offset;
5017                 }
5018 #endif
5019               BFD_ASSERT (off != (bfd_vma) -1);
5020
5021               dyn = htab->root.dynamic_sections_created;
5022               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
5023                   || (info->shared
5024                       && (info->symbolic || h->dynindx == -1
5025                           || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
5026                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
5027                 {
5028                   /* This is actually a static link, or it is a
5029                      -Bsymbolic link and the symbol is defined
5030                      locally, or the symbol was forced to be local
5031                      because of a version file.  We must initialize
5032                      this entry in the global offset table.  Since the
5033                      offset must always be a multiple of 4, we use the
5034                      least significant bit to record whether we have
5035                      initialized it already.
5036
5037                      When doing a dynamic link, we create a .rela.got
5038                      relocation entry to initialize the value.  This
5039                      is done in the finish_dynamic_symbol routine.  */
5040                   if ((off & 1) != 0)
5041                     off &= ~1;
5042                   else
5043                     {
5044                       bfd_put_32 (output_bfd, relocation,
5045                                   sgot->contents + off);
5046 #ifdef INCLUDE_SHMEDIA
5047                       if (seen_stt_datalabel)
5048                         {
5049                           struct elf_sh_link_hash_entry *hsh;
5050
5051                           hsh = (struct elf_sh_link_hash_entry *)h;
5052                           hsh->datalabel_got.offset |= 1;
5053                         }
5054                       else
5055 #endif
5056                         h->got.offset |= 1;
5057                     }
5058                 }
5059
5060               relocation = sgot->output_offset + off;
5061             }
5062           else
5063             {
5064 #ifdef INCLUDE_SHMEDIA
5065               if (rel->r_addend)
5066                 {
5067                   BFD_ASSERT (local_got_offsets != NULL
5068                               && (local_got_offsets[symtab_hdr->sh_info
5069                                                     + r_symndx]
5070                                   != (bfd_vma) -1));
5071
5072                   off = local_got_offsets[symtab_hdr->sh_info
5073                                           + r_symndx];
5074                 }
5075               else
5076                 {
5077 #endif
5078               BFD_ASSERT (local_got_offsets != NULL
5079                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
5080
5081               off = local_got_offsets[r_symndx];
5082 #ifdef INCLUDE_SHMEDIA
5083                 }
5084 #endif
5085
5086               /* The offset must always be a multiple of 4.  We use
5087                  the least significant bit to record whether we have
5088                  already generated the necessary reloc.  */
5089               if ((off & 1) != 0)
5090                 off &= ~1;
5091               else
5092                 {
5093                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5094
5095                   if (info->shared)
5096                     {
5097                       Elf_Internal_Rela outrel;
5098
5099                       if (srelgot == NULL)
5100                         {
5101                           srelgot = bfd_get_section_by_name (dynobj,
5102                                                              ".rela.got");
5103                           BFD_ASSERT (srelgot != NULL);
5104                         }
5105
5106                       outrel.r_offset = (sgot->output_section->vma
5107                                          + sgot->output_offset
5108                                          + off);
5109                       outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
5110                       outrel.r_addend = relocation;
5111                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,
5112                                                 (((Elf32_External_Rela *)
5113                                                   srelgot->contents)
5114                                                  + srelgot->reloc_count));
5115                       ++srelgot->reloc_count;
5116                     }
5117
5118 #ifdef INCLUDE_SHMEDIA
5119                   if (rel->r_addend)
5120                     local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
5121                   else
5122 #endif
5123                     local_got_offsets[r_symndx] |= 1;
5124                 }
5125
5126               relocation = sgot->output_offset + off;
5127             }
5128
5129 #ifdef GOT_BIAS
5130           relocation -= GOT_BIAS;
5131 #endif
5132
5133           goto final_link_relocate;
5134
5135         case R_SH_GOTOFF:
5136 #ifdef INCLUDE_SHMEDIA
5137         case R_SH_GOTOFF_LOW16:
5138         case R_SH_GOTOFF_MEDLOW16:
5139         case R_SH_GOTOFF_MEDHI16:
5140         case R_SH_GOTOFF_HI16:
5141 #endif
5142           /* Relocation is relative to the start of the global offset
5143              table.  */
5144
5145           BFD_ASSERT (sgot != NULL);
5146
5147           /* Note that sgot->output_offset is not involved in this
5148              calculation.  We always want the start of .got.  If we
5149              defined _GLOBAL_OFFSET_TABLE in a different way, as is
5150              permitted by the ABI, we might have to change this
5151              calculation.  */
5152           relocation -= sgot->output_section->vma;
5153
5154 #ifdef GOT_BIAS
5155           relocation -= GOT_BIAS;
5156 #endif
5157
5158           addend = rel->r_addend;
5159
5160           goto final_link_relocate;
5161
5162         case R_SH_GOTPC:
5163 #ifdef INCLUDE_SHMEDIA
5164         case R_SH_GOTPC_LOW16:
5165         case R_SH_GOTPC_MEDLOW16:
5166         case R_SH_GOTPC_MEDHI16:
5167         case R_SH_GOTPC_HI16:
5168 #endif
5169           /* Use global offset table as symbol value.  */
5170
5171           BFD_ASSERT (sgot != NULL);
5172           relocation = sgot->output_section->vma;
5173
5174 #ifdef GOT_BIAS
5175           relocation += GOT_BIAS;
5176 #endif
5177
5178           addend = rel->r_addend;
5179
5180           goto final_link_relocate;
5181
5182         case R_SH_PLT32:
5183 #ifdef INCLUDE_SHMEDIA
5184         case R_SH_PLT_LOW16:
5185         case R_SH_PLT_MEDLOW16:
5186         case R_SH_PLT_MEDHI16:
5187         case R_SH_PLT_HI16:
5188 #endif
5189           /* Relocation is to the entry for this symbol in the
5190              procedure linkage table.  */
5191
5192           /* Resolve a PLT reloc against a local symbol directly,
5193              without using the procedure linkage table.  */
5194           if (h == NULL)
5195             goto final_link_relocate;
5196
5197           if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
5198             goto final_link_relocate;
5199
5200           if (h->plt.offset == (bfd_vma) -1)
5201             {
5202               /* We didn't make a PLT entry for this symbol.  This
5203                  happens when statically linking PIC code, or when
5204                  using -Bsymbolic.  */
5205               goto final_link_relocate;
5206             }
5207
5208           BFD_ASSERT (splt != NULL);
5209           relocation = (splt->output_section->vma
5210                         + splt->output_offset
5211                         + h->plt.offset);
5212
5213 #ifdef INCLUDE_SHMEDIA
5214           relocation++;
5215 #endif
5216
5217           addend = rel->r_addend;
5218
5219           goto final_link_relocate;
5220
5221         case R_SH_LOOP_START:
5222           {
5223             static bfd_vma start, end;
5224
5225             start = (relocation + rel->r_addend
5226                      - (sec->output_section->vma + sec->output_offset));
5227             r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5228                                    rel->r_offset, sec, start, end);
5229             break;
5230
5231         case R_SH_LOOP_END:
5232             end = (relocation + rel->r_addend
5233                    - (sec->output_section->vma + sec->output_offset));
5234             r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5235                                    rel->r_offset, sec, start, end);
5236             break;
5237           }
5238
5239         case R_SH_TLS_GD_32:
5240         case R_SH_TLS_IE_32:
5241           r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5242           tls_type = GOT_UNKNOWN;
5243           if (h == NULL && local_got_offsets)
5244             tls_type = sh_elf_local_got_tls_type (input_bfd) [r_symndx];
5245           else if (h != NULL)
5246             {
5247               tls_type = sh_elf_hash_entry (h)->tls_type;
5248               if (! info->shared
5249                   && (h->dynindx == -1
5250                       || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
5251                   && (tls_type == GOT_TLS_IE
5252                       || sh_elf_hash_entry (h)->tls_tpoff32))
5253                 r_type = R_SH_TLS_LE_32;
5254             }
5255
5256           if (r_type == R_SH_TLS_GD_32 && tls_type == GOT_TLS_IE)
5257             r_type = R_SH_TLS_IE_32;
5258
5259           if (r_type == R_SH_TLS_LE_32)
5260             {
5261               bfd_vma offset;
5262               unsigned short insn;
5263               int indx;
5264               Elf_Internal_Rela outrel;
5265
5266               if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
5267                 {
5268                   /* GD->LE transition:
5269                        mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5270                        jsr @r1; add r12,r4; bra 3f; nop; .align 2; 
5271                        1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5272                      We change it into:
5273                        mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
5274                        nop; nop; ...
5275                        1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:.  */
5276
5277                   offset = rel->r_offset;
5278                   BFD_ASSERT (offset >= 16);
5279                   /* Size of GD instructions is 16 or 18.  */
5280                   offset -= 16;
5281                   insn = bfd_get_16 (input_bfd, contents + offset + 0);
5282                   if ((insn & 0xff00) == 0xc700)
5283                     {
5284                       BFD_ASSERT (offset >= 2);
5285                       offset -= 2;
5286                       insn = bfd_get_16 (input_bfd, contents + offset + 0);
5287                     }
5288
5289                   BFD_ASSERT ((insn & 0xff00) == 0xd400);
5290                   insn = bfd_get_16 (input_bfd, contents + offset + 2);
5291                   BFD_ASSERT ((insn & 0xff00) == 0xc700);
5292                   insn = bfd_get_16 (input_bfd, contents + offset + 4);
5293                   BFD_ASSERT ((insn & 0xff00) == 0xd100);
5294                   insn = bfd_get_16 (input_bfd, contents + offset + 6);
5295                   BFD_ASSERT (insn == 0x310c);
5296                   insn = bfd_get_16 (input_bfd, contents + offset + 8);
5297                   BFD_ASSERT (insn == 0x410b);
5298                   insn = bfd_get_16 (input_bfd, contents + offset + 10);
5299                   BFD_ASSERT (insn == 0x34cc);
5300
5301                   bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
5302                   bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
5303                   bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5304                   bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5305                   bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5306                 }
5307               else
5308                 {
5309                   int index;
5310
5311                   /* IE->LE transition:
5312                      mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
5313                      bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
5314                      We change it into:
5315                      mov.l .Ln,rM; stc gbr,rN; nop; ...;
5316                      1: x@TPOFF; 2:.  */
5317
5318                   offset = rel->r_offset;
5319                   BFD_ASSERT (offset >= 16);
5320                   /* Size of IE instructions is 10 or 12.  */
5321                   offset -= 10;
5322                   insn = bfd_get_16 (input_bfd, contents + offset + 0);
5323                   if ((insn & 0xf0ff) == 0x0012)
5324                     {
5325                       BFD_ASSERT (offset >= 2);
5326                       offset -= 2;
5327                       insn = bfd_get_16 (input_bfd, contents + offset + 0);
5328                     }
5329
5330                   BFD_ASSERT ((insn & 0xff00) == 0xd000);
5331                   index = insn & 0x00ff;
5332                   insn = bfd_get_16 (input_bfd, contents + offset + 2);
5333                   BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
5334                   insn = bfd_get_16 (input_bfd, contents + offset + 4);
5335                   BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
5336                   insn = 0xd000 | (insn & 0x0f00) | index;
5337                   bfd_put_16 (output_bfd, insn, contents + offset + 0);
5338                   bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5339                 }
5340
5341               if (sreloc == NULL)
5342                 {
5343                   const char *name;
5344
5345                   name = (bfd_elf_string_from_elf_section
5346                           (input_bfd,
5347                            elf_elfheader (input_bfd)->e_shstrndx,
5348                            elf_section_data (input_section)->rel_hdr.sh_name));
5349                   if (name == NULL)
5350                     return false;
5351
5352                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5353                               && strcmp (bfd_get_section_name (input_bfd,
5354                                                                input_section),
5355                                          name + 5) == 0);
5356
5357                   sreloc = bfd_get_section_by_name (dynobj, name);
5358                   BFD_ASSERT (sreloc != NULL);
5359                 }
5360
5361               indx = (h && h->dynindx != -1) ? h->dynindx : 0;
5362               outrel.r_offset = (input_section->output_section->vma
5363                                  + input_section->output_offset
5364                                  + rel->r_offset);
5365               outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5366               if (indx == 0)
5367                 outrel.r_addend = relocation - dtpoff_base (info);
5368               else
5369                 outrel.r_addend = 0;
5370               bfd_elf32_swap_reloca_out (output_bfd, &outrel,
5371                                          (((Elf32_External_Rela *)
5372                                            sreloc->contents)
5373                                           + sreloc->reloc_count));
5374               ++sreloc->reloc_count;
5375
5376               continue;
5377             }
5378
5379           sgot = htab->sgot;
5380           if (sgot == NULL)
5381             abort ();
5382
5383           if (h != NULL)
5384             off = h->got.offset;
5385           else
5386             {
5387               if (local_got_offsets == NULL)
5388                 abort ();
5389
5390               off = local_got_offsets[r_symndx];
5391             }
5392
5393           if ((off & 1) != 0)
5394             off &= ~1;
5395           else
5396             {
5397               Elf_Internal_Rela outrel;
5398               Elf32_External_Rela *loc;
5399               int dr_type, indx;
5400
5401               if (srelgot == NULL)
5402                 {
5403                   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5404                   BFD_ASSERT (srelgot != NULL);
5405                 }
5406
5407               outrel.r_offset = (sgot->output_section->vma
5408                                  + sgot->output_offset + off);
5409
5410               indx = (h && h->dynindx != -1) ? h->dynindx : 0;
5411               dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
5412                          R_SH_TLS_TPOFF32);
5413               if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
5414                 outrel.r_addend = relocation - dtpoff_base (info);
5415               else
5416                 outrel.r_addend = 0;
5417               outrel.r_info = ELF32_R_INFO (indx, dr_type);
5418               loc = (Elf32_External_Rela *) srelgot->contents;
5419               loc += srelgot->reloc_count++;
5420               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5421
5422               if (r_type == R_SH_TLS_GD_32)
5423                 {
5424                   if (indx == 0)
5425                     {
5426                       bfd_put_32 (output_bfd,
5427                                   relocation - dtpoff_base (info),
5428                                   sgot->contents + off + 4);
5429                     }
5430                   else
5431                     {
5432                       outrel.r_info = ELF32_R_INFO (indx,
5433                                                     R_SH_TLS_DTPOFF32);
5434                       outrel.r_offset += 4;
5435                       outrel.r_addend = 0;
5436                       srelgot->reloc_count++;
5437                       loc++;
5438                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,
5439                                                 loc);
5440                     }
5441                 }
5442
5443               if (h != NULL)
5444                 h->got.offset |= 1;
5445               else
5446                 local_got_offsets[r_symndx] |= 1;
5447             }
5448
5449           if (off >= (bfd_vma) -2)
5450             abort ();
5451
5452           if (r_type == (int) ELF32_R_TYPE (rel->r_info))
5453             relocation = sgot->output_offset + off;
5454           else
5455             {
5456               bfd_vma offset;
5457               unsigned short insn;
5458
5459               /* GD->IE transition:
5460                    mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5461                    jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5462                    1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5463                  We change it into:
5464                    mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
5465                    nop; nop; bra 3f; nop; .align 2;
5466                    1: .long x@TPOFF; 2:...; 3:.  */
5467
5468               offset = rel->r_offset;
5469               BFD_ASSERT (offset >= 16);
5470               /* Size of GD instructions is 16 or 18.  */
5471               offset -= 16;
5472               insn = bfd_get_16 (input_bfd, contents + offset + 0);
5473               if ((insn & 0xff00) == 0xc700)
5474                 {
5475                   BFD_ASSERT (offset >= 2);
5476                   offset -= 2;
5477                   insn = bfd_get_16 (input_bfd, contents + offset + 0);
5478                 }
5479
5480               BFD_ASSERT ((insn & 0xff00) == 0xd400);
5481
5482               /* Replace mov.l 1f,R4 with mov.l 1f,r0.  */
5483               bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
5484
5485               insn = bfd_get_16 (input_bfd, contents + offset + 2);
5486               BFD_ASSERT ((insn & 0xff00) == 0xc700);
5487               insn = bfd_get_16 (input_bfd, contents + offset + 4);
5488               BFD_ASSERT ((insn & 0xff00) == 0xd100);
5489               insn = bfd_get_16 (input_bfd, contents + offset + 6);
5490               BFD_ASSERT (insn == 0x310c);
5491               insn = bfd_get_16 (input_bfd, contents + offset + 8);
5492               BFD_ASSERT (insn == 0x410b);
5493               insn = bfd_get_16 (input_bfd, contents + offset + 10);
5494               BFD_ASSERT (insn == 0x34cc);
5495
5496               bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
5497               bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
5498               bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
5499               bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5500               bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5501
5502               bfd_put_32 (output_bfd, sgot->output_offset + off,
5503                           contents + rel->r_offset);
5504
5505               continue;
5506           }
5507
5508           addend = rel->r_addend;
5509
5510           goto final_link_relocate;
5511
5512         case R_SH_TLS_LD_32:
5513           if (! info->shared)
5514             {
5515               bfd_vma offset;
5516               unsigned short insn;
5517
5518               /* LD->LE transition:
5519                    mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5520                    jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5521                    1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5522                  We change it into:
5523                    stc gbr,r0; nop; nop; nop;
5524                    nop; nop; bra 3f; ...; 3:.  */
5525
5526               offset = rel->r_offset;
5527               BFD_ASSERT (offset >= 16);
5528               /* Size of LD instructions is 16 or 18.  */
5529               offset -= 16;
5530               insn = bfd_get_16 (input_bfd, contents + offset + 0);
5531               if ((insn & 0xff00) == 0xc700)
5532                 {
5533                   BFD_ASSERT (offset >= 2);
5534                   offset -= 2;
5535                   insn = bfd_get_16 (input_bfd, contents + offset + 0);
5536                 }
5537
5538               BFD_ASSERT ((insn & 0xff00) == 0xd400);
5539               insn = bfd_get_16 (input_bfd, contents + offset + 2);
5540               BFD_ASSERT ((insn & 0xff00) == 0xc700);
5541               insn = bfd_get_16 (input_bfd, contents + offset + 4);
5542               BFD_ASSERT ((insn & 0xff00) == 0xd100);
5543               insn = bfd_get_16 (input_bfd, contents + offset + 6);
5544               BFD_ASSERT (insn == 0x310c);
5545               insn = bfd_get_16 (input_bfd, contents + offset + 8);
5546               BFD_ASSERT (insn == 0x410b);
5547               insn = bfd_get_16 (input_bfd, contents + offset + 10);
5548               BFD_ASSERT (insn == 0x34cc);
5549
5550               bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
5551               bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
5552               bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5553               bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5554               bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5555               bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5556
5557               continue;
5558             }
5559
5560           sgot = htab->sgot;
5561           if (sgot == NULL)
5562             abort ();
5563
5564           off = htab->tls_ldm_got.offset;
5565           if (off & 1)
5566             off &= ~1;
5567           else
5568             {
5569               Elf_Internal_Rela outrel;
5570               Elf32_External_Rela *loc;
5571
5572               srelgot = htab->srelgot;
5573               if (srelgot == NULL)
5574                 abort ();
5575
5576               outrel.r_offset = (sgot->output_section->vma
5577                                  + sgot->output_offset + off);
5578               outrel.r_addend = 0;
5579               outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
5580               loc = (Elf32_External_Rela *) srelgot->contents;
5581               loc += srelgot->reloc_count++;
5582               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5583               htab->tls_ldm_got.offset |= 1;
5584             }
5585
5586           relocation = sgot->output_offset + off;
5587           addend = rel->r_addend;
5588
5589           goto final_link_relocate;
5590
5591         case R_SH_TLS_LDO_32:
5592           if (! info->shared)
5593             {
5594               int indx;
5595               Elf_Internal_Rela outrel;
5596
5597               if (sreloc == NULL)
5598                 {
5599                   const char *name;
5600
5601                   name = (bfd_elf_string_from_elf_section
5602                           (input_bfd,
5603                            elf_elfheader (input_bfd)->e_shstrndx,
5604                            elf_section_data (input_section)->rel_hdr.sh_name));
5605                   if (name == NULL)
5606                     return false;
5607
5608                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5609                               && strcmp (bfd_get_section_name (input_bfd,
5610                                                                input_section),
5611                                          name + 5) == 0);
5612
5613                   sreloc = bfd_get_section_by_name (dynobj, name);
5614                   BFD_ASSERT (sreloc != NULL);
5615                 }
5616
5617               indx = (h && h->dynindx != -1) ? h->dynindx : 0;
5618               outrel.r_offset = (input_section->output_section->vma
5619                                  + input_section->output_offset
5620                                  + rel->r_offset);
5621               outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5622               if (indx == 0)
5623                 outrel.r_addend = relocation - dtpoff_base (info);
5624               else
5625                 outrel.r_addend = 0;
5626               bfd_elf32_swap_reloca_out (output_bfd, &outrel,
5627                                          (((Elf32_External_Rela *)
5628                                            sreloc->contents)
5629                                           + sreloc->reloc_count));
5630               ++sreloc->reloc_count;
5631
5632               continue;
5633             }
5634           else
5635             relocation -= dtpoff_base (info);
5636
5637           addend = rel->r_addend;
5638           goto final_link_relocate;
5639
5640         case R_SH_TLS_LE_32:
5641           {
5642             int indx;
5643             Elf_Internal_Rela outrel;
5644
5645             if (sreloc == NULL)
5646               {
5647                 const char *name;
5648
5649                 name = (bfd_elf_string_from_elf_section
5650                         (input_bfd,
5651                          elf_elfheader (input_bfd)->e_shstrndx,
5652                          elf_section_data (input_section)->rel_hdr.sh_name));
5653                 if (name == NULL)
5654                   return false;
5655
5656                 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5657                             && strcmp (bfd_get_section_name (input_bfd,
5658                                                              input_section),
5659                                        name + 5) == 0);
5660
5661                 sreloc = bfd_get_section_by_name (dynobj, name);
5662                 BFD_ASSERT (sreloc != NULL);
5663               }
5664
5665             indx = (h && h->dynindx != -1) ? h->dynindx : 0;
5666             outrel.r_offset = (input_section->output_section->vma
5667                                + input_section->output_offset
5668                                + rel->r_offset);
5669             outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5670             if (indx == 0)
5671               outrel.r_addend = relocation - dtpoff_base (info);
5672             else
5673               outrel.r_addend = 0;
5674             bfd_elf32_swap_reloca_out (output_bfd, &outrel,
5675                                        (((Elf32_External_Rela *)
5676                                          sreloc->contents)
5677                                         + sreloc->reloc_count));
5678             ++sreloc->reloc_count;
5679
5680             continue;
5681           }
5682         }
5683
5684     relocation_done:
5685       if (r != bfd_reloc_ok)
5686         {
5687           switch (r)
5688             {
5689             default:
5690             case bfd_reloc_outofrange:
5691               abort ();
5692             case bfd_reloc_overflow:
5693               {
5694                 const char *name;
5695
5696                 if (h != NULL)
5697                   name = h->root.root.string;
5698                 else
5699                   {
5700                     name = (bfd_elf_string_from_elf_section
5701                             (input_bfd, symtab_hdr->sh_link, sym->st_name));
5702                     if (name == NULL)
5703                       return false;
5704                     if (*name == '\0')
5705                       name = bfd_section_name (input_bfd, sec);
5706                   }
5707                 if (! ((*info->callbacks->reloc_overflow)
5708                        (info, name, howto->name, (bfd_vma) 0,
5709                         input_bfd, input_section, rel->r_offset)))
5710                   return false;
5711               }
5712               break;
5713             }
5714         }
5715     }
5716
5717   return true;
5718 }
5719
5720 /* This is a version of bfd_generic_get_relocated_section_contents
5721    which uses sh_elf_relocate_section.  */
5722
5723 static bfd_byte *
5724 sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
5725                                        data, relocateable, symbols)
5726      bfd *output_bfd;
5727      struct bfd_link_info *link_info;
5728      struct bfd_link_order *link_order;
5729      bfd_byte *data;
5730      boolean relocateable;
5731      asymbol **symbols;
5732 {
5733   Elf_Internal_Shdr *symtab_hdr;
5734   asection *input_section = link_order->u.indirect.section;
5735   bfd *input_bfd = input_section->owner;
5736   asection **sections = NULL;
5737   Elf_Internal_Rela *internal_relocs = NULL;
5738   Elf_Internal_Sym *isymbuf = NULL;
5739
5740   /* We only need to handle the case of relaxing, or of having a
5741      particular set of section contents, specially.  */
5742   if (relocateable
5743       || elf_section_data (input_section)->this_hdr.contents == NULL)
5744     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5745                                                        link_order, data,
5746                                                        relocateable,
5747                                                        symbols);
5748
5749   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5750
5751   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
5752           (size_t) input_section->_raw_size);
5753
5754   if ((input_section->flags & SEC_RELOC) != 0
5755       && input_section->reloc_count > 0)
5756     {
5757       asection **secpp;
5758       Elf_Internal_Sym *isym, *isymend;
5759       bfd_size_type amt;
5760
5761       internal_relocs = (_bfd_elf32_link_read_relocs
5762                          (input_bfd, input_section, (PTR) NULL,
5763                           (Elf_Internal_Rela *) NULL, false));
5764       if (internal_relocs == NULL)
5765         goto error_return;
5766
5767       if (symtab_hdr->sh_info != 0)
5768         {
5769           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5770           if (isymbuf == NULL)
5771             isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5772                                             symtab_hdr->sh_info, 0,
5773                                             NULL, NULL, NULL);
5774           if (isymbuf == NULL)
5775             goto error_return;
5776         }
5777
5778       amt = symtab_hdr->sh_info;
5779       amt *= sizeof (asection *);
5780       sections = (asection **) bfd_malloc (amt);
5781       if (sections == NULL && amt != 0)
5782         goto error_return;
5783
5784       isymend = isymbuf + symtab_hdr->sh_info;
5785       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5786         {
5787           asection *isec;
5788
5789           if (isym->st_shndx == SHN_UNDEF)
5790             isec = bfd_und_section_ptr;
5791           else if (isym->st_shndx == SHN_ABS)
5792             isec = bfd_abs_section_ptr;
5793           else if (isym->st_shndx == SHN_COMMON)
5794             isec = bfd_com_section_ptr;
5795           else
5796             isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5797
5798           *secpp = isec;
5799         }
5800
5801       if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5802                                      input_section, data, internal_relocs,
5803                                      isymbuf, sections))
5804         goto error_return;
5805
5806       if (sections != NULL)
5807         free (sections);
5808       if (isymbuf != NULL
5809           && symtab_hdr->contents != (unsigned char *) isymbuf)
5810         free (isymbuf);
5811       if (elf_section_data (input_section)->relocs != internal_relocs)
5812         free (internal_relocs);
5813     }
5814
5815   return data;
5816
5817  error_return:
5818   if (sections != NULL)
5819     free (sections);
5820   if (isymbuf != NULL
5821       && symtab_hdr->contents != (unsigned char *) isymbuf)
5822     free (isymbuf);
5823   if (internal_relocs != NULL
5824       && elf_section_data (input_section)->relocs != internal_relocs)
5825     free (internal_relocs);
5826   return NULL;
5827 }
5828
5829 /* Return the base VMA address which should be subtracted from real addresses
5830    when resolving @dtpoff relocation.
5831    This is PT_TLS segment p_vaddr.  */
5832
5833 static bfd_vma
5834 dtpoff_base (info)
5835      struct bfd_link_info *info;
5836 {
5837  /* If tls_segment is NULL, we should have signalled an error already.  */
5838  if (elf_hash_table (info)->tls_segment == NULL)
5839    return 0;
5840   return elf_hash_table (info)->tls_segment->start;
5841 }
5842
5843 static asection *
5844 sh_elf_gc_mark_hook (sec, info, rel, h, sym)
5845      asection *sec;
5846      struct bfd_link_info *info ATTRIBUTE_UNUSED;
5847      Elf_Internal_Rela *rel;
5848      struct elf_link_hash_entry *h;
5849      Elf_Internal_Sym *sym;
5850 {
5851   if (h != NULL)
5852     {
5853       switch (ELF32_R_TYPE (rel->r_info))
5854         {
5855         case R_SH_GNU_VTINHERIT:
5856         case R_SH_GNU_VTENTRY:
5857           break;
5858
5859         default:
5860 #ifdef INCLUDE_SHMEDIA
5861           while (h->root.type == bfd_link_hash_indirect
5862                  && h->root.u.i.link)
5863             h = (struct elf_link_hash_entry *) h->root.u.i.link;
5864 #endif
5865           switch (h->root.type)
5866             {
5867             case bfd_link_hash_defined:
5868             case bfd_link_hash_defweak:
5869               return h->root.u.def.section;
5870
5871             case bfd_link_hash_common:
5872               return h->root.u.c.p->section;
5873
5874             default:
5875               break;
5876             }
5877         }
5878     }
5879   else
5880     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5881
5882   return NULL;
5883 }
5884
5885 /* Update the got entry reference counts for the section being removed.  */
5886
5887 static boolean
5888 sh_elf_gc_sweep_hook (abfd, info, sec, relocs)
5889      bfd *abfd;
5890      struct bfd_link_info *info;
5891      asection *sec;
5892      const Elf_Internal_Rela *relocs;
5893 {
5894   Elf_Internal_Shdr *symtab_hdr;
5895   struct elf_link_hash_entry **sym_hashes;
5896   bfd_signed_vma *local_got_refcounts;
5897   const Elf_Internal_Rela *rel, *relend;
5898   unsigned long r_symndx;
5899   struct elf_link_hash_entry *h;
5900   struct elf_sh_link_hash_entry *eh;
5901
5902   elf_section_data (sec)->local_dynrel = NULL;
5903
5904   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5905   sym_hashes = elf_sym_hashes (abfd);
5906   local_got_refcounts = elf_local_got_refcounts (abfd);
5907
5908   relend = relocs + sec->reloc_count;
5909   for (rel = relocs; rel < relend; rel++)
5910     {
5911 #ifdef INCLUDE_SHMEDIA
5912       int seen_stt_datalabel = 0;
5913 #endif
5914
5915       r_symndx = ELF32_R_SYM (rel->r_info);
5916       if (r_symndx < symtab_hdr->sh_info)
5917         h = NULL;
5918       else
5919         {
5920           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5921 #ifdef INCLUDE_SHMEDIA
5922           while (h->root.type == bfd_link_hash_indirect
5923                  || h->root.type == bfd_link_hash_warning)
5924             {
5925               seen_stt_datalabel |= h->type == STT_DATALABEL;
5926               h = (struct elf_link_hash_entry *) h->root.u.i.link;
5927             }
5928 #endif
5929         }
5930       eh = (struct elf_sh_link_hash_entry *) h;
5931
5932       switch (sh_elf_optimized_tls_reloc (info, ELF32_R_TYPE (rel->r_info),
5933                                           ELF32_R_SYM (rel->r_info)
5934                                           >= symtab_hdr->sh_info))
5935         {
5936         case R_SH_TLS_LD_32:
5937           if (sh_elf_hash_table (info)->tls_ldm_got.refcount > 0)
5938             sh_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
5939           break;
5940
5941         case R_SH_GOT32:
5942         case R_SH_GOTOFF:
5943         case R_SH_GOTPC:
5944 #ifdef INCLUDE_SHMEDIA
5945         case R_SH_GOT_LOW16:
5946         case R_SH_GOT_MEDLOW16:
5947         case R_SH_GOT_MEDHI16:
5948         case R_SH_GOT_HI16:
5949         case R_SH_GOT10BY4:
5950         case R_SH_GOT10BY8:
5951         case R_SH_GOTOFF_LOW16:
5952         case R_SH_GOTOFF_MEDLOW16:
5953         case R_SH_GOTOFF_MEDHI16:
5954         case R_SH_GOTOFF_HI16:
5955         case R_SH_GOTPC_LOW16:
5956         case R_SH_GOTPC_MEDLOW16:
5957         case R_SH_GOTPC_MEDHI16:
5958         case R_SH_GOTPC_HI16:
5959 #endif
5960         case R_SH_TLS_GD_32:
5961         case R_SH_TLS_IE_32:
5962           if (h != NULL)
5963             {
5964 #ifdef INCLUDE_SHMEDIA
5965               if (seen_stt_datalabel)
5966                 {
5967                   if (eh->datalabel_got.refcount > 0)
5968                     eh->datalabel_got.refcount -= 1;
5969                 }
5970               else
5971 #endif
5972                 if (h->got.refcount > 0)
5973                   h->got.refcount -= 1;
5974             }
5975           else if (local_got_refcounts != NULL)
5976             {
5977 #ifdef INCLUDE_SHMEDIA
5978               if (rel->r_addend & 1)
5979                 {
5980                   if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
5981                     local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
5982                 }
5983               else
5984 #endif
5985                 if (local_got_refcounts[r_symndx] > 0)
5986                   local_got_refcounts[r_symndx] -= 1;
5987             }
5988           break;
5989
5990         case R_SH_DIR32:
5991         case R_SH_REL32:
5992           if (h != NULL)
5993             {
5994               struct elf_sh_dyn_relocs **pp;
5995               struct elf_sh_dyn_relocs *p;
5996
5997
5998               if (!info->shared && h->plt.refcount > 0)
5999                 h->plt.refcount -= 1;
6000
6001               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6002                 if (p->sec == sec)
6003                   {
6004                     if (ELF32_R_TYPE (rel->r_info) == R_SH_REL32)
6005                       p->pc_count -= 1;
6006                     p->count -= 1;
6007                     if (p->count == 0)
6008                       *pp = p->next;
6009                     break;
6010                   }
6011             }
6012           break;
6013
6014         case R_SH_PLT32:
6015 #ifdef INCLUDE_SHMEDIA
6016         case R_SH_PLT_LOW16:
6017         case R_SH_PLT_MEDLOW16:
6018         case R_SH_PLT_MEDHI16:
6019         case R_SH_PLT_HI16:
6020 #endif
6021           if (h != NULL)
6022             {
6023               if (h->plt.refcount > 0)
6024                 h->plt.refcount -= 1;
6025             }
6026           break;
6027
6028         case R_SH_GOTPLT32:
6029 #ifdef INCLUDE_SHMEDIA
6030         case R_SH_GOTPLT_LOW16:
6031         case R_SH_GOTPLT_MEDLOW16:
6032         case R_SH_GOTPLT_MEDHI16:
6033         case R_SH_GOTPLT_HI16:
6034         case R_SH_GOTPLT10BY4:
6035         case R_SH_GOTPLT10BY8:
6036 #endif
6037           if (h != NULL)
6038             {
6039               if (eh->gotplt_refcount > 0)
6040                 {
6041                   eh->gotplt_refcount -= 1;
6042                   if (h->plt.refcount > 0)
6043                     h->plt.refcount -= 1;
6044                 }
6045 #ifdef INCLUDE_SHMEDIA
6046               else if (seen_stt_datalabel)
6047                 {
6048                   if (eh->datalabel_got.refcount > 0)
6049                     eh->datalabel_got.refcount -= 1;
6050                 }
6051 #endif
6052               else if (h->got.refcount > 0)
6053                 h->got.refcount -= 1;
6054             }
6055           else if (local_got_refcounts != NULL)
6056             {
6057 #ifdef INCLUDE_SHMEDIA
6058               if (rel->r_addend & 1)
6059                 {
6060                   if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
6061                     local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
6062                 }
6063               else
6064 #endif
6065                 if (local_got_refcounts[r_symndx] > 0)
6066                   local_got_refcounts[r_symndx] -= 1;
6067             }
6068           break;
6069
6070         default:
6071           break;
6072         }
6073     }
6074
6075   return true;
6076 }
6077
6078 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
6079
6080 static void
6081 sh_elf_copy_indirect_symbol (bed, dir, ind)
6082      struct elf_backend_data *bed;
6083      struct elf_link_hash_entry *dir, *ind;
6084 {
6085   struct elf_sh_link_hash_entry *edir, *eind;
6086 #ifdef INCLUDE_SHMEDIA
6087   bfd_signed_vma tmp;
6088 #endif
6089
6090   edir = (struct elf_sh_link_hash_entry *) dir;
6091   eind = (struct elf_sh_link_hash_entry *) ind;
6092
6093   if (eind->dyn_relocs != NULL)
6094     {
6095       if (edir->dyn_relocs != NULL)
6096         {
6097           struct elf_sh_dyn_relocs **pp;
6098           struct elf_sh_dyn_relocs *p;
6099
6100           BFD_ASSERT (ind->root.type != bfd_link_hash_indirect);
6101
6102           /* Add reloc counts against the weak sym to the strong sym
6103              list.  Merge any entries against the same section.  */
6104           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
6105             {
6106               struct elf_sh_dyn_relocs *q;
6107
6108               for (q = edir->dyn_relocs; q != NULL; q = q->next)
6109                 if (q->sec == p->sec)
6110                   {
6111                     q->pc_count += p->pc_count;
6112                     q->count += p->count;
6113                     *pp = p->next;
6114                     break;
6115                   }
6116               if (q == NULL)
6117                 pp = &p->next;
6118             }
6119           *pp = edir->dyn_relocs;
6120         }
6121
6122       edir->dyn_relocs = eind->dyn_relocs;
6123       eind->dyn_relocs = NULL;
6124     }
6125   edir->gotplt_refcount = eind->gotplt_refcount;
6126   eind->gotplt_refcount = 0;
6127 #ifdef INCLUDE_SHMEDIA
6128   tmp = edir->datalabel_got.refcount;
6129   if (tmp < 1)
6130     {
6131       edir->datalabel_got.refcount = eind->datalabel_got.refcount;
6132       eind->datalabel_got.refcount = tmp;
6133     }
6134   else
6135     BFD_ASSERT (eind->datalabel_got.refcount < 1);
6136 #endif
6137
6138   if (ind->root.type == bfd_link_hash_indirect
6139       && dir->got.refcount <= 0)
6140     {
6141       edir->tls_type = eind->tls_type;
6142       eind->tls_type = GOT_UNKNOWN;
6143     }
6144
6145   _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
6146 }
6147
6148 static int
6149 sh_elf_optimized_tls_reloc (info, r_type, is_local)
6150      struct bfd_link_info *info;
6151      int r_type;
6152      int is_local;
6153 {
6154   if (info->shared)
6155     return r_type;
6156
6157   switch (r_type)
6158     {
6159     case R_SH_TLS_GD_32:
6160     case R_SH_TLS_IE_32:
6161       if (is_local)
6162         return R_SH_TLS_LE_32;
6163       return R_SH_TLS_IE_32;
6164     case R_SH_TLS_LD_32:
6165       return R_SH_TLS_LE_32;
6166     }
6167
6168   return r_type;
6169 }
6170
6171 /* Look through the relocs for a section during the first phase.
6172    Since we don't do .gots or .plts, we just need to consider the
6173    virtual table relocs for gc.  */
6174
6175 static boolean
6176 sh_elf_check_relocs (abfd, info, sec, relocs)
6177      bfd *abfd;
6178      struct bfd_link_info *info;
6179      asection *sec;
6180      const Elf_Internal_Rela *relocs;
6181 {
6182   Elf_Internal_Shdr *symtab_hdr;
6183   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6184   struct elf_sh_link_hash_table *htab;
6185   const Elf_Internal_Rela *rel;
6186   const Elf_Internal_Rela *rel_end;
6187   bfd *dynobj;
6188   bfd_vma *local_got_offsets;
6189   asection *sgot;
6190   asection *srelgot;
6191   asection *sreloc;
6192   unsigned int r_type;
6193   int tls_type, old_tls_type;
6194
6195   sgot = NULL;
6196   srelgot = NULL;
6197   sreloc = NULL;
6198
6199   if (info->relocateable)
6200     return true;
6201
6202   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6203   sym_hashes = elf_sym_hashes (abfd);
6204   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
6205   if (!elf_bad_symtab (abfd))
6206     sym_hashes_end -= symtab_hdr->sh_info;
6207
6208   htab = sh_elf_hash_table (info);
6209   dynobj = htab->root.dynobj;
6210   local_got_offsets = elf_local_got_offsets (abfd);
6211
6212   rel_end = relocs + sec->reloc_count;
6213   for (rel = relocs; rel < rel_end; rel++)
6214     {
6215       struct elf_link_hash_entry *h;
6216       unsigned long r_symndx;
6217 #ifdef INCLUDE_SHMEDIA
6218       int seen_stt_datalabel = 0;
6219 #endif
6220
6221       r_symndx = ELF32_R_SYM (rel->r_info);
6222       r_type = ELF32_R_TYPE (rel->r_info);
6223
6224       if (r_symndx < symtab_hdr->sh_info)
6225         h = NULL;
6226       else
6227         {
6228           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6229 #ifdef INCLUDE_SHMEDIA
6230           while (h->root.type == bfd_link_hash_indirect
6231                  || h->root.type == bfd_link_hash_warning)
6232             {
6233               seen_stt_datalabel |= h->type == STT_DATALABEL;
6234               h = (struct elf_link_hash_entry *) h->root.u.i.link;
6235             }
6236 #endif
6237         }
6238
6239       r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
6240       if (! info->shared
6241           && r_type == R_SH_TLS_IE_32
6242           && h != NULL
6243           && h->root.type != bfd_link_hash_undefined
6244           && h->root.type != bfd_link_hash_undefweak
6245           && (h->dynindx == -1
6246               || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
6247         r_type = R_SH_TLS_LE_32;
6248
6249       /* Some relocs require a global offset table.  */
6250       if (htab->sgot == NULL)
6251         {
6252           switch (r_type)
6253             {
6254             case R_SH_GOTPLT32:
6255             case R_SH_GOT32:
6256             case R_SH_GOTOFF:
6257             case R_SH_GOTPC:
6258 #ifdef INCLUDE_SHMEDIA
6259             case R_SH_GOTPLT_LOW16:
6260             case R_SH_GOTPLT_MEDLOW16:
6261             case R_SH_GOTPLT_MEDHI16:
6262             case R_SH_GOTPLT_HI16:
6263             case R_SH_GOTPLT10BY4:
6264             case R_SH_GOTPLT10BY8:
6265             case R_SH_GOT_LOW16:
6266             case R_SH_GOT_MEDLOW16:
6267             case R_SH_GOT_MEDHI16:
6268             case R_SH_GOT_HI16:
6269             case R_SH_GOT10BY4:
6270             case R_SH_GOT10BY8:
6271             case R_SH_GOTOFF_LOW16:
6272             case R_SH_GOTOFF_MEDLOW16:
6273             case R_SH_GOTOFF_MEDHI16:
6274             case R_SH_GOTOFF_HI16:
6275             case R_SH_GOTPC_LOW16:
6276             case R_SH_GOTPC_MEDLOW16:
6277             case R_SH_GOTPC_MEDHI16:
6278             case R_SH_GOTPC_HI16:
6279 #endif
6280             case R_SH_TLS_GD_32:
6281             case R_SH_TLS_LD_32:
6282             case R_SH_TLS_IE_32:
6283               if (dynobj == NULL)
6284                 htab->root.dynobj = dynobj = abfd;
6285               if (! create_got_section (dynobj, info))
6286                 return false;
6287               break;
6288
6289             default:
6290               break;
6291             }
6292         }
6293
6294       switch (r_type)
6295         {
6296           /* This relocation describes the C++ object vtable hierarchy.
6297              Reconstruct it for later use during GC.  */
6298         case R_SH_GNU_VTINHERIT:
6299           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6300             return false;
6301           break;
6302
6303           /* This relocation describes which C++ vtable entries are actually
6304              used.  Record for later use during GC.  */
6305         case R_SH_GNU_VTENTRY:
6306           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6307             return false;
6308           break;
6309
6310         force_got:
6311         case R_SH_TLS_IE_32:
6312           if (info->shared)
6313             info->flags |= DF_STATIC_TLS;
6314
6315           /* FALLTHROUGH */
6316         case R_SH_TLS_GD_32:
6317         case R_SH_GOT32:
6318 #ifdef INCLUDE_SHMEDIA
6319         case R_SH_GOT_LOW16:
6320         case R_SH_GOT_MEDLOW16:
6321         case R_SH_GOT_MEDHI16:
6322         case R_SH_GOT_HI16:
6323         case R_SH_GOT10BY4:
6324         case R_SH_GOT10BY8:
6325 #endif
6326           switch (r_type)
6327             {
6328             default:
6329               tls_type = GOT_NORMAL;
6330               break;
6331             case R_SH_TLS_GD_32:
6332               tls_type = GOT_TLS_GD;
6333               break;
6334             case R_SH_TLS_IE_32:
6335               tls_type = GOT_TLS_IE;
6336               break;
6337             }
6338
6339           if (h != NULL)
6340             {
6341 #ifdef INCLUDE_SHMEDIA
6342               if (seen_stt_datalabel)
6343                 {
6344                   struct elf_sh_link_hash_entry *eh = 
6345                     (struct elf_sh_link_hash_entry *)h;
6346
6347                   eh->datalabel_got.refcount += 1;
6348                 }
6349               else
6350 #endif
6351                 h->got.refcount += 1;
6352               old_tls_type = sh_elf_hash_entry (h)->tls_type;
6353             }
6354           else
6355             {
6356               bfd_signed_vma *local_got_refcounts;
6357
6358               /* This is a global offset table entry for a local
6359                  symbol.  */
6360               local_got_refcounts = elf_local_got_refcounts (abfd);
6361               if (local_got_refcounts == NULL)
6362                 {
6363                   bfd_size_type size;
6364
6365                   size = symtab_hdr->sh_info;
6366                   size *= sizeof (bfd_signed_vma);
6367 #ifdef INCLUDE_SHMEDIA
6368                   /* Reserve space for both the datalabel and
6369                      codelabel local GOT offsets.  */
6370                   size *= 2;
6371 #endif
6372                   size += symtab_hdr->sh_info;
6373                   local_got_refcounts = ((bfd_signed_vma *)
6374                                          bfd_zalloc (abfd, size));
6375                   if (local_got_refcounts == NULL)
6376                     return false;
6377                   elf_local_got_refcounts (abfd) = local_got_refcounts;
6378 #ifdef  INCLUDE_SHMEDIA
6379                   /* Take care of both the datalabel and codelabel local
6380                      GOT offsets.  */
6381                   sh_elf_local_got_tls_type (abfd)
6382                     = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
6383 #else
6384                   sh_elf_local_got_tls_type (abfd)
6385                     = (char *) (local_got_refcounts + symtab_hdr->sh_info);
6386 #endif
6387                 }
6388 #ifdef INCLUDE_SHMEDIA
6389               if (rel->r_addend & 1)
6390                 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
6391               else
6392 #endif
6393                 local_got_refcounts[r_symndx] += 1;
6394               old_tls_type = sh_elf_local_got_tls_type (abfd) [r_symndx];
6395             }
6396
6397           /* If a TLS symbol is accessed using IE at least once,
6398              there is no point to use dynamic model for it.  */
6399           if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
6400               && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
6401             {
6402               if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
6403                 tls_type = GOT_TLS_IE;
6404               else
6405                 {
6406                   (*_bfd_error_handler)
6407                     (_("%s: `%s' accessed both as normal and thread local symbol"),
6408                      bfd_archive_filename (abfd), h->root.root.string);
6409                   return false;
6410                 }
6411             }
6412
6413           if (old_tls_type != tls_type)
6414             {
6415               if (h != NULL)
6416                 sh_elf_hash_entry (h)->tls_type = tls_type;
6417               else
6418                 sh_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
6419             }
6420
6421           break;
6422
6423         case R_SH_TLS_LD_32:
6424           sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
6425           break;
6426
6427         case R_SH_GOTPLT32:
6428 #ifdef INCLUDE_SHMEDIA
6429         case R_SH_GOTPLT_LOW16:
6430         case R_SH_GOTPLT_MEDLOW16:
6431         case R_SH_GOTPLT_MEDHI16:
6432         case R_SH_GOTPLT_HI16:
6433         case R_SH_GOTPLT10BY4:
6434         case R_SH_GOTPLT10BY8:
6435 #endif
6436           /* If this is a local symbol, we resolve it directly without
6437              creating a procedure linkage table entry.  */
6438
6439           if (h == NULL
6440               || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
6441               || ! info->shared
6442               || info->symbolic
6443               || h->dynindx == -1)
6444             goto force_got;
6445
6446           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6447           h->plt.refcount += 1;
6448           ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
6449
6450           break;
6451
6452         case R_SH_PLT32:
6453 #ifdef INCLUDE_SHMEDIA
6454         case R_SH_PLT_LOW16:
6455         case R_SH_PLT_MEDLOW16:
6456         case R_SH_PLT_MEDHI16:
6457         case R_SH_PLT_HI16:
6458 #endif
6459           /* This symbol requires a procedure linkage table entry.  We
6460              actually build the entry in adjust_dynamic_symbol,
6461              because this might be a case of linking PIC code which is
6462              never referenced by a dynamic object, in which case we
6463              don't need to generate a procedure linkage table entry
6464              after all.  */
6465
6466           /* If this is a local symbol, we resolve it directly without
6467              creating a procedure linkage table entry.  */
6468           if (h == NULL)
6469             continue;
6470
6471           if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
6472             break;
6473
6474           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6475           h->plt.refcount += 1;
6476           break;
6477
6478         case R_SH_DIR32:
6479         case R_SH_REL32:
6480           if (h != NULL && ! info->shared)
6481             {
6482               h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
6483               h->plt.refcount += 1;
6484             }
6485
6486           /* If we are creating a shared library, and this is a reloc
6487              against a global symbol, or a non PC relative reloc
6488              against a local symbol, then we need to copy the reloc
6489              into the shared library.  However, if we are linking with
6490              -Bsymbolic, we do not need to copy a reloc against a
6491              global symbol which is defined in an object we are
6492              including in the link (i.e., DEF_REGULAR is set).  At
6493              this point we have not seen all the input files, so it is
6494              possible that DEF_REGULAR is not set now but will be set
6495              later (it is never cleared).  We account for that
6496              possibility below by storing information in the
6497              dyn_relocs field of the hash table entry. A similar
6498              situation occurs when creating shared libraries and symbol
6499              visibility changes render the symbol local.
6500
6501              If on the other hand, we are creating an executable, we
6502              may need to keep relocations for symbols satisfied by a
6503              dynamic library if we manage to avoid copy relocs for the
6504              symbol.  */
6505           if ((info->shared
6506                && (sec->flags & SEC_ALLOC) != 0
6507                && (r_type != R_SH_REL32
6508                    || (h != NULL
6509                        && (! info->symbolic
6510                            || h->root.type == bfd_link_hash_defweak
6511                            || (h->elf_link_hash_flags
6512                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
6513               || (! info->shared
6514                   && (sec->flags & SEC_ALLOC) != 0
6515                   && h != NULL
6516                   && (h->root.type == bfd_link_hash_defweak
6517                       || (h->elf_link_hash_flags
6518                           & ELF_LINK_HASH_DEF_REGULAR) == 0)))
6519             {
6520               struct elf_sh_dyn_relocs *p;
6521               struct elf_sh_dyn_relocs **head;
6522
6523               if (dynobj == NULL)
6524                 htab->root.dynobj = dynobj = abfd;
6525
6526               /* When creating a shared object, we must copy these
6527                  reloc types into the output file.  We create a reloc
6528                  section in dynobj and make room for this reloc.  */
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                   if (sec->flags & SEC_READONLY)
6560                     info->flags |= DF_TEXTREL;
6561                   elf_section_data (sec)->sreloc = sreloc;
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 (r_type == R_SH_REL32)
6598                 p->pc_count += 1;
6599             }
6600
6601           break;
6602
6603         case R_SH_TLS_LE_32:
6604           if (info->shared)
6605             {
6606               (*_bfd_error_handler) (_("%s: TLS local exec code cannot be linked into shared objects"),
6607                                      bfd_archive_filename (abfd));
6608               return false;
6609             }
6610
6611           if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_LD_32)
6612             break;
6613
6614           /* FALLTHROUGH */
6615         case R_SH_TLS_LDO_32:
6616           /* We make a R_SH_TLS_TPOFF32 relocation. Count it as a
6617              copy relocation.  */
6618           if (! info->shared)
6619             {
6620               struct elf_sh_dyn_relocs *p;
6621               struct elf_sh_dyn_relocs **head;
6622
6623               if (dynobj == NULL)
6624                 htab->root.dynobj = dynobj = abfd;
6625
6626               if (sreloc == NULL)
6627                 {
6628                   const char *name;
6629
6630                   name = (bfd_elf_string_from_elf_section
6631                           (abfd,
6632                            elf_elfheader (abfd)->e_shstrndx,
6633                            elf_section_data (sec)->rel_hdr.sh_name));
6634                   if (name == NULL)
6635                     return false;
6636
6637                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
6638                               && strcmp (bfd_get_section_name (abfd, sec),
6639                                          name + 5) == 0);
6640
6641                   sreloc = bfd_get_section_by_name (dynobj, name);
6642                   if (sreloc == NULL)
6643                     {
6644                       flagword flags;
6645
6646                       sreloc = bfd_make_section (dynobj, name);
6647                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
6648                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
6649                       if ((sec->flags & SEC_ALLOC) != 0)
6650                         flags |= SEC_ALLOC | SEC_LOAD;
6651                       if (sreloc == NULL
6652                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
6653                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
6654                         return false;
6655                     }
6656                   elf_section_data (sec)->sreloc = sreloc;
6657                   if (sec->flags & SEC_READONLY)
6658                     info->flags |= DF_TEXTREL;
6659                 }
6660
6661               /* If this is a global symbol, we count the number of
6662                  relocations we need for this symbol.  */
6663               if (h != NULL)
6664                 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
6665               else
6666                 {
6667                   asection *s;
6668
6669                   /* Track dynamic relocs needed for local syms too.  */
6670                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
6671                                                  sec, r_symndx);
6672                   if (s == NULL)
6673                     return false;
6674
6675                   head = ((struct elf_sh_dyn_relocs **)
6676                           &elf_section_data (s)->local_dynrel);
6677                 }
6678
6679               p = *head;
6680               if (p == NULL || p->sec != sec)
6681                 {
6682                   bfd_size_type amt = sizeof (*p);
6683                   p = ((struct elf_sh_dyn_relocs *) bfd_alloc (dynobj, amt));
6684                   if (p == NULL)
6685                     return false;
6686                   p->next = *head;
6687                   *head = p;
6688                   p->sec = sec;
6689                   p->count = 0;
6690                   p->pc_count = 0;
6691                 }
6692
6693               p->count += 1;
6694               if (h)
6695                 sh_elf_hash_entry (h)->tls_tpoff32 = true;
6696             }
6697           break;
6698
6699         default:
6700           break;
6701         }
6702     }
6703
6704   return true;
6705 }
6706
6707 #ifndef sh_elf_set_mach_from_flags
6708 static boolean
6709 sh_elf_set_mach_from_flags (abfd)
6710      bfd *abfd;
6711 {
6712   flagword flags = elf_elfheader (abfd)->e_flags;
6713
6714   switch (flags & EF_SH_MACH_MASK)
6715     {
6716     case EF_SH1:
6717       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh);
6718       break;
6719     case EF_SH2:
6720       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh2);
6721       break;
6722     case EF_SH_DSP:
6723       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh_dsp);
6724       break;
6725     case EF_SH3:
6726       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3);
6727       break;
6728     case EF_SH3_DSP:
6729       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3_dsp);
6730       break;
6731     case EF_SH3E:
6732       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh3e);
6733       break;
6734     case EF_SH_UNKNOWN:
6735     case EF_SH4:
6736       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh4);
6737       break;
6738     default:
6739       return false;
6740     }
6741   return true;
6742 }
6743 #endif /* not sh_elf_set_mach_from_flags */
6744
6745 #ifndef sh_elf_set_private_flags
6746 /* Function to keep SH specific file flags.  */
6747
6748 static boolean
6749 sh_elf_set_private_flags (abfd, flags)
6750      bfd *abfd;
6751      flagword flags;
6752 {
6753   BFD_ASSERT (! elf_flags_init (abfd)
6754               || elf_elfheader (abfd)->e_flags == flags);
6755
6756   elf_elfheader (abfd)->e_flags = flags;
6757   elf_flags_init (abfd) = true;
6758   return sh_elf_set_mach_from_flags (abfd);
6759 }
6760 #endif /* not sh_elf_set_private_flags */
6761
6762 #ifndef sh_elf_copy_private_data
6763 /* Copy backend specific data from one object module to another */
6764
6765 static boolean
6766 sh_elf_copy_private_data (ibfd, obfd)
6767      bfd * ibfd;
6768      bfd * obfd;
6769 {
6770   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6771       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6772     return true;
6773
6774   return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
6775 }
6776 #endif /* not sh_elf_copy_private_data */
6777
6778 #ifndef sh_elf_merge_private_data
6779 /* This routine checks for linking big and little endian objects
6780    together, and for linking sh-dsp with sh3e / sh4 objects.  */
6781
6782 static boolean
6783 sh_elf_merge_private_data (ibfd, obfd)
6784      bfd *ibfd;
6785      bfd *obfd;
6786 {
6787   flagword old_flags, new_flags;
6788
6789   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
6790     return false;
6791
6792   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6793       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6794     return true;
6795
6796   if (! elf_flags_init (obfd))
6797     {
6798       /* This happens when ld starts out with a 'blank' output file.  */
6799       elf_flags_init (obfd) = true;
6800       elf_elfheader (obfd)->e_flags = EF_SH1;
6801     }
6802   old_flags = elf_elfheader (obfd)->e_flags;
6803   new_flags = elf_elfheader (ibfd)->e_flags;
6804   if ((EF_SH_HAS_DSP (old_flags) && EF_SH_HAS_FP (new_flags))
6805       || (EF_SH_HAS_DSP (new_flags) && EF_SH_HAS_FP (old_flags)))
6806     {
6807       (*_bfd_error_handler)
6808         ("%s: uses %s instructions while previous modules use %s instructions",
6809          bfd_archive_filename (ibfd),
6810          EF_SH_HAS_DSP (new_flags) ? "dsp" : "floating point",
6811          EF_SH_HAS_DSP (new_flags) ? "floating point" : "dsp");
6812       bfd_set_error (bfd_error_bad_value);
6813       return false;
6814     }
6815   elf_elfheader (obfd)->e_flags = EF_SH_MERGE_MACH (old_flags, new_flags);
6816
6817   return sh_elf_set_mach_from_flags (obfd);
6818 }
6819 #endif /* not sh_elf_merge_private_data */
6820
6821 /* Override the generic function because we need to store sh_elf_obj_tdata
6822    as the specific tdata.  We set also the machine architecture from flags
6823    here.  */
6824
6825 static boolean
6826 sh_elf_object_p (abfd)
6827   bfd *abfd;
6828 {
6829   struct sh_elf_obj_tdata *new_tdata;
6830   bfd_size_type amt = sizeof (struct sh_elf_obj_tdata);
6831
6832   if (sh_elf_set_mach_from_flags (abfd) == false)
6833     return false;
6834
6835   /* Allocate our special target data.  */
6836   new_tdata = bfd_zalloc (abfd, amt);
6837   if (new_tdata == NULL)
6838     return false;
6839   new_tdata->root = *abfd->tdata.elf_obj_data;
6840   abfd->tdata.any = new_tdata;
6841   return true;
6842 }
6843
6844 /* Finish up dynamic symbol handling.  We set the contents of various
6845    dynamic sections here.  */
6846
6847 static boolean
6848 sh_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
6849      bfd *output_bfd;
6850      struct bfd_link_info *info;
6851      struct elf_link_hash_entry *h;
6852      Elf_Internal_Sym *sym;
6853 {
6854   struct elf_sh_link_hash_table *htab;
6855   bfd *dynobj;
6856
6857   htab = sh_elf_hash_table (info);
6858   dynobj = htab->root.dynobj;
6859
6860   if (h->plt.offset != (bfd_vma) -1)
6861     {
6862       asection *splt;
6863       asection *sgot;
6864       asection *srel;
6865
6866       bfd_vma plt_index;
6867       bfd_vma got_offset;
6868       Elf_Internal_Rela rel;
6869
6870       /* This symbol has an entry in the procedure linkage table.  Set
6871          it up.  */
6872
6873       BFD_ASSERT (h->dynindx != -1);
6874
6875       splt = htab->splt;
6876       sgot = htab->sgotplt;
6877       srel = htab->srelplt;
6878       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
6879
6880       /* Get the index in the procedure linkage table which
6881          corresponds to this symbol.  This is the index of this symbol
6882          in all the symbols for which we are making plt entries.  The
6883          first entry in the procedure linkage table is reserved.  */
6884       plt_index = h->plt.offset / elf_sh_sizeof_plt (info) - 1;
6885
6886       /* Get the offset into the .got table of the entry that
6887          corresponds to this function.  Each .got entry is 4 bytes.
6888          The first three are reserved.  */
6889       got_offset = (plt_index + 3) * 4;
6890
6891 #ifdef GOT_BIAS
6892       if (info->shared)
6893         got_offset -= GOT_BIAS;
6894 #endif
6895
6896       /* Fill in the entry in the procedure linkage table.  */
6897       if (! info->shared)
6898         {
6899           if (elf_sh_plt_entry == NULL)
6900             {
6901               elf_sh_plt_entry = (bfd_big_endian (output_bfd) ?
6902                                   elf_sh_plt_entry_be : elf_sh_plt_entry_le);
6903             }
6904           memcpy (splt->contents + h->plt.offset, elf_sh_plt_entry,
6905                   elf_sh_sizeof_plt (info));
6906 #ifdef INCLUDE_SHMEDIA
6907           movi_shori_putval (output_bfd,
6908                              (sgot->output_section->vma
6909                               + sgot->output_offset
6910                               + got_offset),
6911                              (splt->contents + h->plt.offset
6912                               + elf_sh_plt_symbol_offset (info)));
6913
6914           /* Set bottom bit because its for a branch to SHmedia */
6915           movi_shori_putval (output_bfd,
6916                              (splt->output_section->vma + splt->output_offset)
6917                              | 1,
6918                              (splt->contents + h->plt.offset
6919                               + elf_sh_plt_plt0_offset (info)));
6920 #else
6921           bfd_put_32 (output_bfd,
6922                       (sgot->output_section->vma
6923                        + sgot->output_offset
6924                        + got_offset),
6925                       (splt->contents + h->plt.offset
6926                        + elf_sh_plt_symbol_offset (info)));
6927
6928           bfd_put_32 (output_bfd,
6929                       (splt->output_section->vma + splt->output_offset),
6930                       (splt->contents + h->plt.offset
6931                        + elf_sh_plt_plt0_offset (info)));
6932 #endif
6933         }
6934       else
6935         {
6936           if (elf_sh_pic_plt_entry == NULL)
6937             {
6938               elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
6939                                       elf_sh_pic_plt_entry_be :
6940                                       elf_sh_pic_plt_entry_le);
6941             }
6942           memcpy (splt->contents + h->plt.offset, elf_sh_pic_plt_entry,
6943                   elf_sh_sizeof_plt (info));
6944 #ifdef INCLUDE_SHMEDIA
6945           movi_shori_putval (output_bfd, got_offset,
6946                              (splt->contents + h->plt.offset
6947                               + elf_sh_plt_symbol_offset (info)));
6948 #else
6949           bfd_put_32 (output_bfd, got_offset,
6950                       (splt->contents + h->plt.offset
6951                        + elf_sh_plt_symbol_offset (info)));
6952 #endif
6953         }
6954
6955 #ifdef GOT_BIAS
6956       if (info->shared)
6957         got_offset += GOT_BIAS;
6958 #endif
6959
6960 #ifdef INCLUDE_SHMEDIA
6961       movi_shori_putval (output_bfd,
6962                          plt_index * sizeof (Elf32_External_Rela),
6963                          (splt->contents + h->plt.offset
6964                           + elf_sh_plt_reloc_offset (info)));
6965 #else
6966       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
6967                   (splt->contents + h->plt.offset
6968                    + elf_sh_plt_reloc_offset (info)));
6969 #endif
6970
6971       /* Fill in the entry in the global offset table.  */
6972       bfd_put_32 (output_bfd,
6973                   (splt->output_section->vma
6974                    + splt->output_offset
6975                    + h->plt.offset
6976                    + elf_sh_plt_temp_offset (info)),
6977                   sgot->contents + got_offset);
6978
6979       /* Fill in the entry in the .rela.plt section.  */
6980       rel.r_offset = (sgot->output_section->vma
6981                       + sgot->output_offset
6982                       + got_offset);
6983       rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
6984       rel.r_addend = 0;
6985 #ifdef GOT_BIAS
6986       rel.r_addend = GOT_BIAS;
6987 #endif
6988       bfd_elf32_swap_reloca_out (output_bfd, &rel,
6989                                 ((Elf32_External_Rela *) srel->contents
6990                                  + plt_index));
6991
6992       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6993         {
6994           /* Mark the symbol as undefined, rather than as defined in
6995              the .plt section.  Leave the value alone.  */
6996           sym->st_shndx = SHN_UNDEF;
6997         }
6998     }
6999
7000   if (h->got.offset != (bfd_vma) -1
7001       && sh_elf_hash_entry (h)->tls_type != GOT_TLS_GD
7002       && sh_elf_hash_entry (h)->tls_type != GOT_TLS_IE)
7003     {
7004       asection *sgot;
7005       asection *srel;
7006       Elf_Internal_Rela rel;
7007
7008       /* This symbol has an entry in the global offset table.  Set it
7009          up.  */
7010
7011       sgot = htab->sgot;
7012       srel = htab->srelgot;
7013       BFD_ASSERT (sgot != NULL && srel != NULL);
7014
7015       rel.r_offset = (sgot->output_section->vma
7016                       + sgot->output_offset
7017                       + (h->got.offset &~ (bfd_vma) 1));
7018
7019       /* If this is a static link, or it is a -Bsymbolic link and the
7020          symbol is defined locally or was forced to be local because
7021          of a version file, we just want to emit a RELATIVE reloc.
7022          The entry in the global offset table will already have been
7023          initialized in the relocate_section function.  */
7024       if (info->shared
7025           && (info->symbolic
7026               || h->dynindx == -1
7027               || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
7028           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
7029         {
7030           rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
7031           rel.r_addend = (h->root.u.def.value
7032                           + h->root.u.def.section->output_section->vma
7033                           + h->root.u.def.section->output_offset);
7034         }
7035       else
7036         {
7037           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
7038           rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
7039           rel.r_addend = 0;
7040         }
7041
7042       bfd_elf32_swap_reloca_out (output_bfd, &rel,
7043                                  ((Elf32_External_Rela *) srel->contents
7044                                   + srel->reloc_count));
7045       ++srel->reloc_count;
7046     }
7047
7048 #ifdef INCLUDE_SHMEDIA
7049   {
7050     struct elf_sh_link_hash_entry *eh;
7051
7052     eh = (struct elf_sh_link_hash_entry *) h;
7053     if (eh->datalabel_got.offset != (bfd_vma) -1)
7054       {
7055         asection *sgot;
7056         asection *srel;
7057         Elf_Internal_Rela rel;
7058
7059         /* This symbol has a datalabel entry in the global offset table.
7060            Set it up.  */
7061
7062         sgot = htab->sgot;
7063         srel = htab->srelgot;
7064         BFD_ASSERT (sgot != NULL && srel != NULL);
7065
7066         rel.r_offset = (sgot->output_section->vma
7067                         + sgot->output_offset
7068                         + (eh->datalabel_got.offset &~ (bfd_vma) 1));
7069
7070         /* If this is a static link, or it is a -Bsymbolic link and the
7071            symbol is defined locally or was forced to be local because
7072            of a version file, we just want to emit a RELATIVE reloc.
7073            The entry in the global offset table will already have been
7074            initialized in the relocate_section function.  */
7075         if (info->shared
7076             && (info->symbolic
7077                 || h->dynindx == -1
7078                 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
7079             && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
7080           {
7081             rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
7082             rel.r_addend = (h->root.u.def.value
7083                             + h->root.u.def.section->output_section->vma
7084                             + h->root.u.def.section->output_offset);
7085           }
7086         else
7087           {
7088             bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
7089                         + eh->datalabel_got.offset);
7090             rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
7091             rel.r_addend = 0;
7092           }
7093
7094         bfd_elf32_swap_reloca_out (output_bfd, &rel,
7095                                    ((Elf32_External_Rela *) srel->contents
7096                                     + srel->reloc_count));
7097         ++srel->reloc_count;
7098       }
7099   }
7100 #endif
7101
7102   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
7103     {
7104       asection *s;
7105       Elf_Internal_Rela rel;
7106
7107       /* This symbol needs a copy reloc.  Set it up.  */
7108
7109       BFD_ASSERT (h->dynindx != -1
7110                   && (h->root.type == bfd_link_hash_defined
7111                       || h->root.type == bfd_link_hash_defweak));
7112
7113       s = bfd_get_section_by_name (h->root.u.def.section->owner,
7114                                    ".rela.bss");
7115       BFD_ASSERT (s != NULL);
7116
7117       rel.r_offset = (h->root.u.def.value
7118                       + h->root.u.def.section->output_section->vma
7119                       + h->root.u.def.section->output_offset);
7120       rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
7121       rel.r_addend = 0;
7122       bfd_elf32_swap_reloca_out (output_bfd, &rel,
7123                                  ((Elf32_External_Rela *) s->contents
7124                                   + s->reloc_count));
7125       ++s->reloc_count;
7126     }
7127
7128   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
7129   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
7130       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
7131     sym->st_shndx = SHN_ABS;
7132
7133   return true;
7134 }
7135
7136 /* Finish up the dynamic sections.  */
7137
7138 static boolean
7139 sh_elf_finish_dynamic_sections (output_bfd, info)
7140      bfd *output_bfd;
7141      struct bfd_link_info *info;
7142 {
7143   struct elf_sh_link_hash_table *htab;
7144   bfd *dynobj;
7145   asection *sgot;
7146   asection *sdyn;
7147
7148   htab = sh_elf_hash_table (info);
7149   dynobj = htab->root.dynobj;
7150
7151   sgot = htab->sgotplt;
7152   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7153
7154   if (htab->root.dynamic_sections_created)
7155     {
7156       asection *splt;
7157       Elf32_External_Dyn *dyncon, *dynconend;
7158
7159       BFD_ASSERT (sgot != NULL && sdyn != NULL);
7160
7161       dyncon = (Elf32_External_Dyn *) sdyn->contents;
7162       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
7163       for (; dyncon < dynconend; dyncon++)
7164         {
7165           Elf_Internal_Dyn dyn;
7166           asection *s;
7167 #ifdef INCLUDE_SHMEDIA
7168           const char *name;
7169 #endif
7170
7171           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7172
7173           switch (dyn.d_tag)
7174             {
7175             default:
7176               break;
7177
7178 #ifdef INCLUDE_SHMEDIA
7179             case DT_INIT:
7180               name = info->init_function;
7181               goto get_sym;
7182
7183             case DT_FINI:
7184               name = info->fini_function;
7185             get_sym:
7186               if (dyn.d_un.d_val != 0)
7187                 {
7188                   struct elf_link_hash_entry *h;
7189
7190                   h = elf_link_hash_lookup (&htab->root, name,
7191                                             false, false, true);
7192                   if (h != NULL && (h->other & STO_SH5_ISA32))
7193                     {
7194                       dyn.d_un.d_val |= 1;
7195                       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7196                     }
7197                 }
7198               break;
7199 #endif
7200
7201             case DT_PLTGOT:
7202               s = htab->sgot->output_section;
7203               goto get_vma;
7204
7205             case DT_JMPREL:
7206               s = htab->srelplt->output_section;
7207             get_vma:
7208               BFD_ASSERT (s != NULL);
7209               dyn.d_un.d_ptr = s->vma;
7210               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7211               break;
7212
7213             case DT_PLTRELSZ:
7214               s = htab->srelplt->output_section;
7215               BFD_ASSERT (s != NULL);
7216               if (s->_cooked_size != 0)
7217                 dyn.d_un.d_val = s->_cooked_size;
7218               else
7219                 dyn.d_un.d_val = s->_raw_size;
7220               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7221               break;
7222
7223             case DT_RELASZ:
7224               /* My reading of the SVR4 ABI indicates that the
7225                  procedure linkage table relocs (DT_JMPREL) should be
7226                  included in the overall relocs (DT_RELA).  This is
7227                  what Solaris does.  However, UnixWare can not handle
7228                  that case.  Therefore, we override the DT_RELASZ entry
7229                  here to make it not include the JMPREL relocs.  Since
7230                  the linker script arranges for .rela.plt to follow all
7231                  other relocation sections, we don't have to worry
7232                  about changing the DT_RELA entry.  */
7233               if (htab->srelplt != NULL)
7234                 {
7235                   s = htab->srelplt->output_section;
7236                   if (s->_cooked_size != 0)
7237                     dyn.d_un.d_val -= s->_cooked_size;
7238                   else
7239                     dyn.d_un.d_val -= s->_raw_size;
7240                 }
7241               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7242               break;
7243             }
7244         }
7245
7246       /* Fill in the first entry in the procedure linkage table.  */
7247       splt = htab->splt;
7248       if (splt && splt->_raw_size > 0)
7249         {
7250           if (info->shared)
7251             {
7252               if (elf_sh_pic_plt_entry == NULL)
7253                 {
7254                   elf_sh_pic_plt_entry = (bfd_big_endian (output_bfd) ?
7255                                           elf_sh_pic_plt_entry_be :
7256                                           elf_sh_pic_plt_entry_le);
7257                 }
7258               memcpy (splt->contents, elf_sh_pic_plt_entry,
7259                       elf_sh_sizeof_plt (info));
7260             }
7261           else
7262             {
7263               if (elf_sh_plt0_entry == NULL)
7264                 {
7265                   elf_sh_plt0_entry = (bfd_big_endian (output_bfd) ?
7266                                        elf_sh_plt0_entry_be :
7267                                        elf_sh_plt0_entry_le);
7268                 }
7269               memcpy (splt->contents, elf_sh_plt0_entry, PLT_ENTRY_SIZE);
7270 #ifdef INCLUDE_SHMEDIA
7271               movi_shori_putval (output_bfd,
7272                                  sgot->output_section->vma
7273                                  + sgot->output_offset,
7274                                  splt->contents
7275                                  + elf_sh_plt0_gotplt_offset (info));
7276 #else
7277               bfd_put_32 (output_bfd,
7278                           sgot->output_section->vma + sgot->output_offset + 4,
7279                           splt->contents + elf_sh_plt0_gotid_offset (info));
7280               bfd_put_32 (output_bfd,
7281                           sgot->output_section->vma + sgot->output_offset + 8,
7282                           splt->contents + elf_sh_plt0_linker_offset (info));
7283 #endif
7284             }
7285
7286           /* UnixWare sets the entsize of .plt to 4, although that doesn't
7287              really seem like the right value.  */
7288           elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
7289         }
7290     }
7291
7292   /* Fill in the first three entries in the global offset table.  */
7293   if (sgot && sgot->_raw_size > 0)
7294     {
7295       if (sdyn == NULL)
7296         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7297       else
7298         bfd_put_32 (output_bfd,
7299                     sdyn->output_section->vma + sdyn->output_offset,
7300                     sgot->contents);
7301       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
7302       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
7303
7304       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7305     }
7306
7307   return true;
7308 }
7309
7310 static enum elf_reloc_type_class
7311 sh_elf_reloc_type_class (rela)
7312      const Elf_Internal_Rela *rela;
7313 {
7314   switch ((int) ELF32_R_TYPE (rela->r_info))
7315     {
7316     case R_SH_RELATIVE:
7317       return reloc_class_relative;
7318     case R_SH_JMP_SLOT:
7319       return reloc_class_plt;
7320     case R_SH_COPY:
7321       return reloc_class_copy;
7322     default:
7323       return reloc_class_normal;
7324     }
7325 }
7326
7327 #ifndef ELF_ARCH
7328 #define TARGET_BIG_SYM          bfd_elf32_sh_vec
7329 #define TARGET_BIG_NAME         "elf32-sh"
7330 #define TARGET_LITTLE_SYM       bfd_elf32_shl_vec
7331 #define TARGET_LITTLE_NAME      "elf32-shl"
7332 #define ELF_ARCH                bfd_arch_sh
7333 #define ELF_MACHINE_CODE        EM_SH
7334 #define ELF_MAXPAGESIZE         128
7335
7336 #define elf_symbol_leading_char '_'
7337 #endif /* ELF_ARCH */
7338
7339 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
7340 #define elf_info_to_howto               sh_elf_info_to_howto
7341 #define bfd_elf32_bfd_relax_section     sh_elf_relax_section
7342 #define elf_backend_relocate_section    sh_elf_relocate_section
7343 #define bfd_elf32_bfd_get_relocated_section_contents \
7344                                         sh_elf_get_relocated_section_contents
7345 #define bfd_elf32_mkobject              sh_elf_mkobject
7346 #define elf_backend_object_p            sh_elf_object_p
7347 #define bfd_elf32_bfd_set_private_bfd_flags \
7348                                         sh_elf_set_private_flags
7349 #define bfd_elf32_bfd_copy_private_bfd_data \
7350                                         sh_elf_copy_private_data
7351 #define bfd_elf32_bfd_merge_private_bfd_data \
7352                                         sh_elf_merge_private_data
7353
7354 #define elf_backend_gc_mark_hook        sh_elf_gc_mark_hook
7355 #define elf_backend_gc_sweep_hook       sh_elf_gc_sweep_hook
7356 #define elf_backend_check_relocs        sh_elf_check_relocs
7357 #define elf_backend_copy_indirect_symbol \
7358                                         sh_elf_copy_indirect_symbol
7359 #define elf_backend_create_dynamic_sections \
7360                                         sh_elf_create_dynamic_sections
7361 #define bfd_elf32_bfd_link_hash_table_create \
7362                                         sh_elf_link_hash_table_create
7363 #define elf_backend_adjust_dynamic_symbol \
7364                                         sh_elf_adjust_dynamic_symbol
7365 #define elf_backend_size_dynamic_sections \
7366                                         sh_elf_size_dynamic_sections
7367 #define elf_backend_finish_dynamic_symbol \
7368                                         sh_elf_finish_dynamic_symbol
7369 #define elf_backend_finish_dynamic_sections \
7370                                         sh_elf_finish_dynamic_sections
7371 #define elf_backend_reloc_type_class    sh_elf_reloc_type_class
7372
7373 #define elf_backend_can_gc_sections     1
7374 #define elf_backend_can_refcount        1
7375 #define elf_backend_want_got_plt        1
7376 #define elf_backend_plt_readonly        1
7377 #define elf_backend_want_plt_sym        0
7378 #define elf_backend_got_header_size     12
7379 #define elf_backend_plt_header_size     PLT_ENTRY_SIZE
7380
7381 #ifndef ELF32_SH_C_INCLUDED
7382 #include "elf32-target.h"
7383 #endif
7384