OSDN Git Service

include/
[pf3gnuchains/pf3gnuchains3x.git] / bfd / elf64-sh64.c
1 /* SuperH SH64-specific support for 64-bit ELF
2    Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3
4    This file is part of BFD, the Binary File Descriptor library.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #define SH64_ELF64
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 /* Add a suffix for datalabel indirection symbols.  It must not match any
30    other symbols; user symbols with or without version or other
31    decoration.  It must only be used internally and not emitted by any
32    means.  */
33 #define DATALABEL_SUFFIX " DL"
34
35 #define GOT_BIAS (-((long)-32768))
36
37 #define PLT_ENTRY_SIZE 64
38
39 /* Return size of a PLT entry.  */
40 #define elf_sh64_sizeof_plt(info) PLT_ENTRY_SIZE
41
42 /* Return offset of the PLT0 address in an absolute PLT entry.  */
43 #define elf_sh64_plt_plt0_offset(info) 32
44
45 /* Return offset of the linker in PLT0 entry.  */
46 #define elf_sh64_plt0_gotplt_offset(info) 0
47
48 /* Return offset of the trampoline in PLT entry */
49 #define elf_sh64_plt_temp_offset(info) 33 /* Add one because it's SHmedia.  */
50
51 /* Return offset of the symbol in PLT entry.  */
52 #define elf_sh64_plt_symbol_offset(info) 0
53
54 /* Return offset of the relocation in PLT entry.  */
55 #define elf_sh64_plt_reloc_offset(info) (info->shared ? 52 : 44)
56
57 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
58
59 /* The sh linker needs to keep track of the number of relocs that it
60    decides to copy in check_relocs for each symbol.  This is so that
61    it can discard PC relative relocs if it doesn't need them when
62    linking with -Bsymbolic.  We store the information in a field
63    extending the regular ELF linker hash table.  */
64
65 /* This structure keeps track of the number of PC relative relocs we
66    have copied for a given symbol.  */
67
68 struct elf_sh64_pcrel_relocs_copied
69 {
70   /* Next section.  */
71   struct elf_sh64_pcrel_relocs_copied *next;
72   /* A section in dynobj.  */
73   asection *section;
74   /* Number of relocs copied in this section.  */
75   bfd_size_type count;
76 };
77
78 /* sh ELF linker hash entry.  */
79
80 struct elf_sh64_link_hash_entry
81 {
82   struct elf_link_hash_entry root;
83
84   bfd_vma datalabel_got_offset;
85
86   /* Number of PC relative relocs copied for this symbol.  */
87   struct elf_sh64_pcrel_relocs_copied *pcrel_relocs_copied;
88 };
89
90 /* sh ELF linker hash table.  */
91
92 struct elf_sh64_link_hash_table
93 {
94   struct elf_link_hash_table root;
95 };
96
97 /* Traverse an sh ELF linker hash table.  */
98
99 #define sh64_elf64_link_hash_traverse(table, func, info)                \
100   (elf_link_hash_traverse                                               \
101    (&(table)->root,                                                     \
102     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
103     (info)))
104
105 /* Get the sh ELF linker hash table from a link_info structure.  */
106
107 #define sh64_elf64_hash_table(p) \
108   ((struct elf_sh64_link_hash_table *) ((p)->hash))
109
110 static bfd_boolean sh_elf64_copy_private_data
111   (bfd *, bfd *);
112 static bfd_boolean sh_elf64_copy_private_data_internal
113   (bfd *, bfd *);
114 static bfd_boolean sh_elf64_merge_private_data
115   (bfd *, bfd *);
116 static bfd_reloc_status_type sh_elf64_ignore_reloc
117   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
118 static bfd_reloc_status_type sh_elf64_reloc
119   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
120 static reloc_howto_type *sh_elf64_reloc_type_lookup
121   (bfd *, bfd_reloc_code_real_type);
122 static void sh_elf64_info_to_howto
123   (bfd *, arelent *, Elf_Internal_Rela *);
124 static bfd_boolean sh_elf64_relocate_section
125   (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
126    Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
127 static bfd_byte *sh_elf64_get_relocated_section_contents
128   (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
129    bfd_boolean, asymbol **);
130 static bfd_boolean sh_elf64_set_mach_from_flags
131   (bfd *);
132 static bfd_boolean sh_elf64_set_private_flags
133   (bfd *, flagword);
134 static asection *sh_elf64_gc_mark_hook
135   (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
136    struct elf_link_hash_entry *, Elf_Internal_Sym *);
137 static bfd_boolean sh_elf64_gc_sweep_hook
138   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
139 static bfd_boolean sh_elf64_check_relocs
140   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
141 static int sh64_elf64_get_symbol_type
142   (Elf_Internal_Sym *, int);
143 static bfd_boolean sh64_elf64_add_symbol_hook
144   (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, const char **,
145    flagword *, asection **, bfd_vma *);
146 static bfd_boolean sh64_elf64_link_output_symbol_hook
147   (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
148    struct elf_link_hash_entry *);
149 static bfd_boolean sh64_elf64_fake_sections
150   (bfd *, Elf_Internal_Shdr *, asection *);
151 static void sh64_elf64_final_write_processing
152   (bfd *, bfd_boolean);
153 static struct bfd_hash_entry *sh64_elf64_link_hash_newfunc
154   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
155 static struct bfd_link_hash_table *sh64_elf64_link_hash_table_create
156   (bfd *);
157 inline static void movi_shori_putval
158   (bfd *, unsigned long, char *);
159 inline static void movi_3shori_putval
160   (bfd *, bfd_vma, char *);
161 static bfd_boolean sh64_elf64_create_dynamic_sections
162   (bfd *, struct bfd_link_info *);
163 static bfd_boolean sh64_elf64_adjust_dynamic_symbol
164   (struct bfd_link_info *info, struct elf_link_hash_entry *);
165 static bfd_boolean sh64_elf64_discard_copies
166   (struct elf_sh64_link_hash_entry *, void *);
167 static bfd_boolean sh64_elf64_size_dynamic_sections
168   (bfd *, struct bfd_link_info *);
169 static bfd_boolean sh64_elf64_finish_dynamic_symbol
170   (bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
171    Elf_Internal_Sym *);
172 static bfd_boolean sh64_elf64_finish_dynamic_sections
173   (bfd *, struct bfd_link_info *);
174
175 static reloc_howto_type sh_elf64_howto_table[] = {
176   /* No relocation.  */
177   HOWTO (R_SH_NONE,             /* type */
178          0,                     /* rightshift */
179          0,                     /* size (0 = byte, 1 = short, 2 = long) */
180          0,                     /* bitsize */
181          FALSE,                 /* pc_relative */
182          0,                     /* bitpos */
183          complain_overflow_dont, /* complain_on_overflow */
184          sh_elf64_ignore_reloc, /* special_function */
185          "R_SH_NONE",           /* name */
186          FALSE,                 /* partial_inplace */
187          0,                     /* src_mask */
188          0,                     /* dst_mask */
189          FALSE),                /* pcrel_offset */
190
191   /* 32 bit absolute relocation.  Setting partial_inplace to TRUE and
192      src_mask to a non-zero value is similar to the COFF toolchain.  */
193   HOWTO (R_SH_DIR32,            /* type */
194          0,                     /* rightshift */
195          2,                     /* size (0 = byte, 1 = short, 2 = long) */
196          32,                    /* bitsize */
197          FALSE,                 /* pc_relative */
198          0,                     /* bitpos */
199          complain_overflow_bitfield, /* complain_on_overflow */
200          sh_elf64_reloc,                /* special_function */
201          "R_SH_DIR32",          /* name */
202          TRUE,                  /* partial_inplace */
203          0xffffffff,            /* src_mask */
204          0xffffffff,            /* dst_mask */
205          FALSE),                /* pcrel_offset */
206
207   /* 32 bit PC relative relocation.  */
208   HOWTO (R_SH_REL32,            /* type */
209          0,                     /* rightshift */
210          2,                     /* size (0 = byte, 1 = short, 2 = long) */
211          32,                    /* bitsize */
212          TRUE,                  /* pc_relative */
213          0,                     /* bitpos */
214          complain_overflow_signed, /* complain_on_overflow */
215          sh_elf64_ignore_reloc, /* special_function */
216          "R_SH_REL32",          /* name */
217          FALSE,                 /* partial_inplace */
218          0,                     /* src_mask */
219          0xffffffff,            /* dst_mask */
220          TRUE),                 /* pcrel_offset */
221
222   /* For 32-bit sh, this is R_SH_DIR8WPN.  */
223   EMPTY_HOWTO (3),
224
225   /* For 32-bit sh, this is R_SH_IND12W.  */
226   EMPTY_HOWTO (4),
227
228   /* For 32-bit sh, this is R_SH_DIR8WPL.  */
229   EMPTY_HOWTO (5),
230
231   /* For 32-bit sh, this is R_SH_DIR8WPZ.  */
232   EMPTY_HOWTO (6),
233
234   /* For 32-bit sh, this is R_SH_DIR8BP.  */
235   EMPTY_HOWTO (7),
236
237   /* For 32-bit sh, this is R_SH_DIR8W.  */
238   EMPTY_HOWTO (8),
239
240   /* For 32-bit sh, this is R_SH_DIR8L.  */
241   EMPTY_HOWTO (9),
242
243   EMPTY_HOWTO (10),
244   EMPTY_HOWTO (11),
245   EMPTY_HOWTO (12),
246   EMPTY_HOWTO (13),
247   EMPTY_HOWTO (14),
248   EMPTY_HOWTO (15),
249   EMPTY_HOWTO (16),
250   EMPTY_HOWTO (17),
251   EMPTY_HOWTO (18),
252   EMPTY_HOWTO (19),
253   EMPTY_HOWTO (20),
254   EMPTY_HOWTO (21),
255   EMPTY_HOWTO (22),
256   EMPTY_HOWTO (23),
257   EMPTY_HOWTO (24),
258
259   /* The remaining relocs are a GNU extension used for relaxing.  The
260      final pass of the linker never needs to do anything with any of
261      these relocs.  Any required operations are handled by the
262      relaxation code.  */
263
264   /* A 16 bit switch table entry.  This is generated for an expression
265      such as ``.word L1 - L2''.  The offset holds the difference
266      between the reloc address and L2.  */
267   HOWTO (R_SH_SWITCH16,         /* type */
268          0,                     /* rightshift */
269          1,                     /* size (0 = byte, 1 = short, 2 = long) */
270          16,                    /* bitsize */
271          FALSE,                 /* pc_relative */
272          0,                     /* bitpos */
273          complain_overflow_unsigned, /* complain_on_overflow */
274          sh_elf64_ignore_reloc, /* special_function */
275          "R_SH_SWITCH16",       /* name */
276          FALSE,                 /* partial_inplace */
277          0,                     /* src_mask */
278          0,                     /* dst_mask */
279          TRUE),                 /* pcrel_offset */
280
281   /* A 32 bit switch table entry.  This is generated for an expression
282      such as ``.long L1 - L2''.  The offset holds the difference
283      between the reloc address and L2.  */
284   HOWTO (R_SH_SWITCH32,         /* type */
285          0,                     /* rightshift */
286          2,                     /* size (0 = byte, 1 = short, 2 = long) */
287          32,                    /* bitsize */
288          FALSE,                 /* pc_relative */
289          0,                     /* bitpos */
290          complain_overflow_unsigned, /* complain_on_overflow */
291          sh_elf64_ignore_reloc, /* special_function */
292          "R_SH_SWITCH32",       /* name */
293          FALSE,                 /* partial_inplace */
294          0,                     /* src_mask */
295          0,                     /* dst_mask */
296          TRUE),                 /* pcrel_offset */
297
298   /* For 32-bit sh, this is R_SH_USES.  */
299   EMPTY_HOWTO (27),
300
301   /* For 32-bit sh, this is R_SH_COUNT.  */
302   EMPTY_HOWTO (28),
303
304   /* For 32-bit sh, this is R_SH_ALIGN.  FIXME: For linker relaxation,
305      this might be emitted.  When linker relaxation is implemented, we
306      might want to use it.  */
307   EMPTY_HOWTO (29),
308
309   /* For 32-bit sh, this is R_SH_CODE.  FIXME: For linker relaxation,
310      this might be emitted.  When linker relaxation is implemented, we
311      might want to use it.  */
312   EMPTY_HOWTO (30),
313
314   /* For 32-bit sh, this is R_SH_DATA.  FIXME: For linker relaxation,
315      this might be emitted.  When linker relaxation is implemented, we
316      might want to use it.  */
317   EMPTY_HOWTO (31),
318
319   /* For 32-bit sh, this is R_SH_LABEL.  FIXME: For linker relaxation,
320      this might be emitted.  When linker relaxation is implemented, we
321      might want to use it.  */
322   EMPTY_HOWTO (32),
323
324   /* An 8 bit switch table entry.  This is generated for an expression
325      such as ``.word L1 - L2''.  The offset holds the difference
326      between the reloc address and L2.  */
327   HOWTO (R_SH_SWITCH8,          /* type */
328          0,                     /* rightshift */
329          0,                     /* size (0 = byte, 1 = short, 2 = long) */
330          8,                     /* bitsize */
331          FALSE,                 /* pc_relative */
332          0,                     /* bitpos */
333          complain_overflow_unsigned, /* complain_on_overflow */
334          sh_elf64_ignore_reloc, /* special_function */
335          "R_SH_SWITCH8",        /* name */
336          FALSE,                 /* partial_inplace */
337          0,                     /* src_mask */
338          0,                     /* dst_mask */
339          TRUE),                 /* pcrel_offset */
340
341   /* GNU extension to record C++ vtable hierarchy */
342   HOWTO (R_SH_GNU_VTINHERIT, /* type */
343          0,                     /* rightshift */
344          2,                     /* size (0 = byte, 1 = short, 2 = long) */
345          0,                     /* bitsize */
346          FALSE,                 /* pc_relative */
347          0,                     /* bitpos */
348          complain_overflow_dont, /* complain_on_overflow */
349          NULL,                  /* special_function */
350          "R_SH_GNU_VTINHERIT", /* name */
351          FALSE,                 /* partial_inplace */
352          0,                     /* src_mask */
353          0,                     /* dst_mask */
354          FALSE),                /* pcrel_offset */
355
356   /* GNU extension to record C++ vtable member usage */
357   HOWTO (R_SH_GNU_VTENTRY,     /* type */
358          0,                     /* rightshift */
359          2,                     /* size (0 = byte, 1 = short, 2 = long) */
360          0,                     /* bitsize */
361          FALSE,                 /* pc_relative */
362          0,                     /* bitpos */
363          complain_overflow_dont, /* complain_on_overflow */
364          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
365          "R_SH_GNU_VTENTRY",   /* name */
366          FALSE,                 /* partial_inplace */
367          0,                     /* src_mask */
368          0,                     /* dst_mask */
369          FALSE),                /* pcrel_offset */
370
371   /* For 32-bit sh, this is R_SH_LOOP_START.  */
372   EMPTY_HOWTO (36),
373
374   /* For 32-bit sh, this is R_SH_LOOP_END.  */
375   EMPTY_HOWTO (37),
376
377   EMPTY_HOWTO (38),
378   EMPTY_HOWTO (39),
379   EMPTY_HOWTO (40),
380   EMPTY_HOWTO (41),
381   EMPTY_HOWTO (42),
382   EMPTY_HOWTO (43),
383   EMPTY_HOWTO (44),
384
385   /* Used in SHLLI.L and SHLRI.L.  */
386   HOWTO (R_SH_DIR5U,            /* type */
387          0,                     /* rightshift */
388          2,                     /* size (0 = byte, 1 = short, 2 = long) */
389          5,                     /* bitsize */
390          FALSE,                 /* pc_relative */
391          10,                    /* bitpos */
392          complain_overflow_unsigned, /* complain_on_overflow */
393          bfd_elf_generic_reloc, /* special_function */
394          "R_SH_DIR5U",          /* name */
395          FALSE,                 /* partial_inplace */
396          0,                     /* src_mask */
397          0xfc00,                /* dst_mask */
398          FALSE),                /* pcrel_offset */
399
400   /* Used in SHARI, SHLLI et al.  */
401   HOWTO (R_SH_DIR6U,            /* type */
402          0,                     /* rightshift */
403          2,                     /* size (0 = byte, 1 = short, 2 = long) */
404          6,                     /* bitsize */
405          FALSE,                 /* pc_relative */
406          10,                    /* bitpos */
407          complain_overflow_unsigned, /* complain_on_overflow */
408          bfd_elf_generic_reloc, /* special_function */
409          "R_SH_DIR6U",          /* name */
410          FALSE,                 /* partial_inplace */
411          0,                     /* src_mask */
412          0xfc00,                /* dst_mask */
413          FALSE),                /* pcrel_offset */
414
415   /* Used in BxxI, LDHI.L et al.  */
416   HOWTO (R_SH_DIR6S,            /* type */
417          0,                     /* rightshift */
418          2,                     /* size (0 = byte, 1 = short, 2 = long) */
419          6,                     /* bitsize */
420          FALSE,                 /* pc_relative */
421          10,                    /* bitpos */
422          complain_overflow_signed, /* complain_on_overflow */
423          bfd_elf_generic_reloc, /* special_function */
424          "R_SH_DIR6S",          /* name */
425          FALSE,                 /* partial_inplace */
426          0,                     /* src_mask */
427          0xfc00,                /* dst_mask */
428          FALSE),                /* pcrel_offset */
429
430   /* Used in ADDI, ANDI et al.  */
431   HOWTO (R_SH_DIR10S,           /* type */
432          0,                     /* rightshift */
433          2,                     /* size (0 = byte, 1 = short, 2 = long) */
434          10,                    /* bitsize */
435          FALSE,                 /* pc_relative */
436          10,                    /* bitpos */
437          complain_overflow_signed, /* complain_on_overflow */
438          bfd_elf_generic_reloc, /* special_function */
439          "R_SH_DIR10S",         /* name */
440          FALSE,                 /* partial_inplace */
441          0,                     /* src_mask */
442          0xffc00,               /* dst_mask */
443          FALSE),                /* pcrel_offset */
444
445   /* Used in LD.UW, ST.W et al.  */
446   HOWTO (R_SH_DIR10SW,  /* type */
447          1,                     /* rightshift */
448          2,                     /* size (0 = byte, 1 = short, 2 = long) */
449          11,                    /* bitsize */
450          FALSE,                 /* pc_relative */
451          10,                    /* bitpos */
452          complain_overflow_signed, /* complain_on_overflow */
453          bfd_elf_generic_reloc, /* special_function */
454          "R_SH_DIR10SW",        /* name */
455          FALSE,                 /* partial_inplace */
456          0,                     /* src_mask */
457          0xffc00,               /* dst_mask */
458          FALSE),                /* pcrel_offset */
459
460   /* Used in LD.L, FLD.S et al.  */
461   HOWTO (R_SH_DIR10SL,  /* type */
462          2,                     /* rightshift */
463          2,                     /* size (0 = byte, 1 = short, 2 = long) */
464          12,                    /* bitsize */
465          FALSE,                 /* pc_relative */
466          10,                    /* bitpos */
467          complain_overflow_signed, /* complain_on_overflow */
468          bfd_elf_generic_reloc, /* special_function */
469          "R_SH_DIR10SL",        /* name */
470          FALSE,                 /* partial_inplace */
471          0,                     /* src_mask */
472          0xffc00,               /* dst_mask */
473          FALSE),                /* pcrel_offset */
474
475   /* Used in FLD.D, FST.P et al.  */
476   HOWTO (R_SH_DIR10SQ,  /* type */
477          3,                     /* rightshift */
478          2,                     /* size (0 = byte, 1 = short, 2 = long) */
479          13,                    /* bitsize */
480          FALSE,                 /* pc_relative */
481          10,                    /* bitpos */
482          complain_overflow_signed, /* complain_on_overflow */
483          bfd_elf_generic_reloc, /* special_function */
484          "R_SH_DIR10SQ",        /* name */
485          FALSE,                 /* partial_inplace */
486          0,                     /* src_mask */
487          0xffc00,               /* dst_mask */
488          FALSE),                /* pcrel_offset */
489
490   EMPTY_HOWTO (52),
491   EMPTY_HOWTO (53),
492   EMPTY_HOWTO (54),
493   EMPTY_HOWTO (55),
494   EMPTY_HOWTO (56),
495   EMPTY_HOWTO (57),
496   EMPTY_HOWTO (58),
497   EMPTY_HOWTO (59),
498   EMPTY_HOWTO (60),
499   EMPTY_HOWTO (61),
500   EMPTY_HOWTO (62),
501   EMPTY_HOWTO (63),
502   EMPTY_HOWTO (64),
503   EMPTY_HOWTO (65),
504   EMPTY_HOWTO (66),
505   EMPTY_HOWTO (67),
506   EMPTY_HOWTO (68),
507   EMPTY_HOWTO (69),
508   EMPTY_HOWTO (70),
509   EMPTY_HOWTO (71),
510   EMPTY_HOWTO (72),
511   EMPTY_HOWTO (73),
512   EMPTY_HOWTO (74),
513   EMPTY_HOWTO (75),
514   EMPTY_HOWTO (76),
515   EMPTY_HOWTO (77),
516   EMPTY_HOWTO (78),
517   EMPTY_HOWTO (79),
518   EMPTY_HOWTO (80),
519   EMPTY_HOWTO (81),
520   EMPTY_HOWTO (82),
521   EMPTY_HOWTO (83),
522   EMPTY_HOWTO (84),
523   EMPTY_HOWTO (85),
524   EMPTY_HOWTO (86),
525   EMPTY_HOWTO (87),
526   EMPTY_HOWTO (88),
527   EMPTY_HOWTO (89),
528   EMPTY_HOWTO (90),
529   EMPTY_HOWTO (91),
530   EMPTY_HOWTO (92),
531   EMPTY_HOWTO (93),
532   EMPTY_HOWTO (94),
533   EMPTY_HOWTO (95),
534   EMPTY_HOWTO (96),
535   EMPTY_HOWTO (97),
536   EMPTY_HOWTO (98),
537   EMPTY_HOWTO (99),
538   EMPTY_HOWTO (100),
539   EMPTY_HOWTO (101),
540   EMPTY_HOWTO (102),
541   EMPTY_HOWTO (103),
542   EMPTY_HOWTO (104),
543   EMPTY_HOWTO (105),
544   EMPTY_HOWTO (106),
545   EMPTY_HOWTO (107),
546   EMPTY_HOWTO (108),
547   EMPTY_HOWTO (109),
548   EMPTY_HOWTO (110),
549   EMPTY_HOWTO (111),
550   EMPTY_HOWTO (112),
551   EMPTY_HOWTO (113),
552   EMPTY_HOWTO (114),
553   EMPTY_HOWTO (115),
554   EMPTY_HOWTO (116),
555   EMPTY_HOWTO (117),
556   EMPTY_HOWTO (118),
557   EMPTY_HOWTO (119),
558   EMPTY_HOWTO (120),
559   EMPTY_HOWTO (121),
560   EMPTY_HOWTO (122),
561   EMPTY_HOWTO (123),
562   EMPTY_HOWTO (124),
563   EMPTY_HOWTO (125),
564   EMPTY_HOWTO (126),
565   EMPTY_HOWTO (127),
566   EMPTY_HOWTO (128),
567   EMPTY_HOWTO (129),
568   EMPTY_HOWTO (130),
569   EMPTY_HOWTO (131),
570   EMPTY_HOWTO (132),
571   EMPTY_HOWTO (133),
572   EMPTY_HOWTO (134),
573   EMPTY_HOWTO (135),
574   EMPTY_HOWTO (136),
575   EMPTY_HOWTO (137),
576   EMPTY_HOWTO (138),
577   EMPTY_HOWTO (139),
578   EMPTY_HOWTO (140),
579   EMPTY_HOWTO (141),
580   EMPTY_HOWTO (142),
581   EMPTY_HOWTO (143),
582   EMPTY_HOWTO (144),
583   EMPTY_HOWTO (145),
584   EMPTY_HOWTO (146),
585   EMPTY_HOWTO (147),
586   EMPTY_HOWTO (148),
587   EMPTY_HOWTO (149),
588   EMPTY_HOWTO (150),
589   EMPTY_HOWTO (151),
590   EMPTY_HOWTO (152),
591   EMPTY_HOWTO (153),
592   EMPTY_HOWTO (154),
593   EMPTY_HOWTO (155),
594   EMPTY_HOWTO (156),
595   EMPTY_HOWTO (157),
596   EMPTY_HOWTO (158),
597   EMPTY_HOWTO (159),
598
599   /* Relocs for dynamic linking for 32-bit SH would follow.  We don't have
600      any dynamic linking support for 64-bit SH at present.  */
601
602   EMPTY_HOWTO (160),
603   EMPTY_HOWTO (161),
604   EMPTY_HOWTO (162),
605   EMPTY_HOWTO (163),
606   EMPTY_HOWTO (164),
607   EMPTY_HOWTO (165),
608   EMPTY_HOWTO (166),
609   EMPTY_HOWTO (167),
610   EMPTY_HOWTO (168),
611
612   /* Back to SH5 relocations.  */
613   /* Used in MOVI and SHORI (x & 65536).  */
614   HOWTO (R_SH_GOT_LOW16,        /* type */
615          0,                     /* rightshift */
616          2,                     /* size (0 = byte, 1 = short, 2 = long) */
617          64,                    /* bitsize */
618          FALSE,                 /* pc_relative */
619          10,                    /* bitpos */
620          complain_overflow_dont, /* complain_on_overflow */
621          bfd_elf_generic_reloc, /* special_function */
622          "R_SH_GOT_LOW16",      /* name */
623          FALSE,                 /* partial_inplace */
624          0,                     /* src_mask */
625          0x3fffc00,             /* dst_mask */
626          FALSE),                /* pcrel_offset */
627
628   /* Used in MOVI and SHORI ((x >> 16) & 65536).  */
629   HOWTO (R_SH_GOT_MEDLOW16,     /* type */
630          16,                    /* rightshift */
631          2,                     /* size (0 = byte, 1 = short, 2 = long) */
632          64,                    /* bitsize */
633          FALSE,                 /* pc_relative */
634          10,                    /* bitpos */
635          complain_overflow_dont, /* complain_on_overflow */
636          bfd_elf_generic_reloc, /* special_function */
637          "R_SH_GOT_MEDLOW16",   /* name */
638          FALSE,                 /* partial_inplace */
639          0,                     /* src_mask */
640          0x3fffc00,             /* dst_mask */
641          FALSE),                /* pcrel_offset */
642
643   /* Used in MOVI and SHORI ((x >> 32) & 65536).  */
644   HOWTO (R_SH_GOT_MEDHI16,      /* type */
645          32,                    /* rightshift */
646          2,                     /* size (0 = byte, 1 = short, 2 = long) */
647          64,                    /* bitsize */
648          FALSE,                 /* pc_relative */
649          10,                    /* bitpos */
650          complain_overflow_dont, /* complain_on_overflow */
651          bfd_elf_generic_reloc, /* special_function */
652          "R_SH_GOT_MEDHI16",    /* name */
653          FALSE,                 /* partial_inplace */
654          0,                     /* src_mask */
655          0x3fffc00,             /* dst_mask */
656          FALSE),                /* pcrel_offset */
657
658   /* Used in MOVI and SHORI ((x >> 48) & 65536).  */
659   HOWTO (R_SH_GOT_HI16,         /* type */
660          48,                    /* rightshift */
661          2,                     /* size (0 = byte, 1 = short, 2 = long) */
662          64,                    /* bitsize */
663          FALSE,                 /* pc_relative */
664          10,                    /* bitpos */
665          complain_overflow_dont, /* complain_on_overflow */
666          bfd_elf_generic_reloc, /* special_function */
667          "R_SH_GOT_HI16",       /* name */
668          FALSE,                 /* partial_inplace */
669          0,                     /* src_mask */
670          0x3fffc00,             /* dst_mask */
671          FALSE),                /* pcrel_offset */
672
673   /* Used in MOVI and SHORI (x & 65536).  */
674   HOWTO (R_SH_GOTPLT_LOW16,     /* type */
675          0,                     /* rightshift */
676          2,                     /* size (0 = byte, 1 = short, 2 = long) */
677          64,                    /* bitsize */
678          FALSE,                 /* pc_relative */
679          10,                    /* bitpos */
680          complain_overflow_dont, /* complain_on_overflow */
681          bfd_elf_generic_reloc, /* special_function */
682          "R_SH_GOTPLT_LOW16",   /* name */
683          FALSE,                 /* partial_inplace */
684          0,                     /* src_mask */
685          0x3fffc00,             /* dst_mask */
686          FALSE),                /* pcrel_offset */
687
688   /* Used in MOVI and SHORI ((x >> 16) & 65536).  */
689   HOWTO (R_SH_GOTPLT_MEDLOW16,  /* type */
690          16,                    /* rightshift */
691          2,                     /* size (0 = byte, 1 = short, 2 = long) */
692          64,                    /* bitsize */
693          FALSE,                 /* pc_relative */
694          10,                    /* bitpos */
695          complain_overflow_dont, /* complain_on_overflow */
696          bfd_elf_generic_reloc, /* special_function */
697          "R_SH_GOTPLT_MEDLOW16", /* name */
698          FALSE,                 /* partial_inplace */
699          0,                     /* src_mask */
700          0x3fffc00,             /* dst_mask */
701          FALSE),                /* pcrel_offset */
702
703   /* Used in MOVI and SHORI ((x >> 32) & 65536).  */
704   HOWTO (R_SH_GOTPLT_MEDHI16,   /* type */
705          32,                    /* rightshift */
706          2,                     /* size (0 = byte, 1 = short, 2 = long) */
707          64,                    /* bitsize */
708          FALSE,                 /* pc_relative */
709          10,                    /* bitpos */
710          complain_overflow_dont, /* complain_on_overflow */
711          bfd_elf_generic_reloc, /* special_function */
712          "R_SH_GOTPLT_MEDHI16", /* name */
713          FALSE,                 /* partial_inplace */
714          0,                     /* src_mask */
715          0x3fffc00,             /* dst_mask */
716          FALSE),                /* pcrel_offset */
717
718   /* Used in MOVI and SHORI ((x >> 48) & 65536).  */
719   HOWTO (R_SH_GOTPLT_HI16,      /* type */
720          48,                    /* rightshift */
721          2,                     /* size (0 = byte, 1 = short, 2 = long) */
722          64,                    /* bitsize */
723          FALSE,                 /* pc_relative */
724          10,                    /* bitpos */
725          complain_overflow_dont, /* complain_on_overflow */
726          bfd_elf_generic_reloc, /* special_function */
727          "R_SH_GOTPLT_HI16",    /* name */
728          FALSE,                 /* partial_inplace */
729          0,                     /* src_mask */
730          0x3fffc00,             /* dst_mask */
731          FALSE),                /* pcrel_offset */
732
733   /* Used in MOVI and SHORI (x & 65536).  */
734   HOWTO (R_SH_PLT_LOW16,        /* type */
735          0,                     /* rightshift */
736          2,                     /* size (0 = byte, 1 = short, 2 = long) */
737          64,                    /* bitsize */
738          TRUE,                  /* pc_relative */
739          10,                    /* bitpos */
740          complain_overflow_dont, /* complain_on_overflow */
741          bfd_elf_generic_reloc, /* special_function */
742          "R_SH_PLT_LOW16",      /* name */
743          FALSE,                 /* partial_inplace */
744          0,                     /* src_mask */
745          0x3fffc00,             /* dst_mask */
746          TRUE),                 /* pcrel_offset */
747
748   /* Used in MOVI and SHORI ((x >> 16) & 65536).  */
749   HOWTO (R_SH_PLT_MEDLOW16,     /* type */
750          16,                    /* rightshift */
751          2,                     /* size (0 = byte, 1 = short, 2 = long) */
752          64,                    /* bitsize */
753          TRUE,                  /* pc_relative */
754          10,                    /* bitpos */
755          complain_overflow_dont, /* complain_on_overflow */
756          bfd_elf_generic_reloc, /* special_function */
757          "R_SH_PLT_MEDLOW16",   /* name */
758          FALSE,                 /* partial_inplace */
759          0,                     /* src_mask */
760          0x3fffc00,             /* dst_mask */
761          TRUE),                 /* pcrel_offset */
762
763   /* Used in MOVI and SHORI ((x >> 32) & 65536).  */
764   HOWTO (R_SH_PLT_MEDHI16,      /* type */
765          32,                    /* rightshift */
766          2,                     /* size (0 = byte, 1 = short, 2 = long) */
767          64,                    /* bitsize */
768          TRUE,                  /* pc_relative */
769          10,                    /* bitpos */
770          complain_overflow_dont, /* complain_on_overflow */
771          bfd_elf_generic_reloc, /* special_function */
772          "R_SH_PLT_MEDHI16",    /* name */
773          FALSE,                 /* partial_inplace */
774          0,                     /* src_mask */
775          0x3fffc00,             /* dst_mask */
776          TRUE),                 /* pcrel_offset */
777
778   /* Used in MOVI and SHORI ((x >> 48) & 65536).  */
779   HOWTO (R_SH_PLT_HI16,         /* type */
780          48,                    /* rightshift */
781          2,                     /* size (0 = byte, 1 = short, 2 = long) */
782          64,                    /* bitsize */
783          TRUE,                  /* pc_relative */
784          10,                    /* bitpos */
785          complain_overflow_dont, /* complain_on_overflow */
786          bfd_elf_generic_reloc, /* special_function */
787          "R_SH_PLT_HI16",       /* name */
788          FALSE,                 /* partial_inplace */
789          0,                     /* src_mask */
790          0x3fffc00,             /* dst_mask */
791          TRUE),                 /* pcrel_offset */
792
793   /* Used in MOVI and SHORI (x & 65536).  */
794   HOWTO (R_SH_GOTOFF_LOW16,     /* type */
795          0,                     /* rightshift */
796          2,                     /* size (0 = byte, 1 = short, 2 = long) */
797          64,                    /* bitsize */
798          FALSE,                 /* pc_relative */
799          10,                    /* bitpos */
800          complain_overflow_dont, /* complain_on_overflow */
801          bfd_elf_generic_reloc, /* special_function */
802          "R_SH_GOTOFF_LOW16",   /* name */
803          FALSE,                 /* partial_inplace */
804          0,                     /* src_mask */
805          0x3fffc00,             /* dst_mask */
806          FALSE),                /* pcrel_offset */
807
808   /* Used in MOVI and SHORI ((x >> 16) & 65536).  */
809   HOWTO (R_SH_GOTOFF_MEDLOW16,  /* type */
810          16,                    /* rightshift */
811          2,                     /* size (0 = byte, 1 = short, 2 = long) */
812          64,                    /* bitsize */
813          FALSE,                 /* pc_relative */
814          10,                    /* bitpos */
815          complain_overflow_dont, /* complain_on_overflow */
816          bfd_elf_generic_reloc, /* special_function */
817          "R_SH_GOTOFF_MEDLOW16", /* name */
818          FALSE,                 /* partial_inplace */
819          0,                     /* src_mask */
820          0x3fffc00,             /* dst_mask */
821          FALSE),                /* pcrel_offset */
822
823   /* Used in MOVI and SHORI ((x >> 32) & 65536).  */
824   HOWTO (R_SH_GOTOFF_MEDHI16,   /* type */
825          32,                    /* rightshift */
826          2,                     /* size (0 = byte, 1 = short, 2 = long) */
827          64,                    /* bitsize */
828          FALSE,                 /* pc_relative */
829          10,                    /* bitpos */
830          complain_overflow_dont, /* complain_on_overflow */
831          bfd_elf_generic_reloc, /* special_function */
832          "R_SH_GOTOFF_MEDHI16", /* name */
833          FALSE,                 /* partial_inplace */
834          0,                     /* src_mask */
835          0x3fffc00,             /* dst_mask */
836          FALSE),                /* pcrel_offset */
837
838   /* Used in MOVI and SHORI ((x >> 48) & 65536).  */
839   HOWTO (R_SH_GOTOFF_HI16,      /* type */
840          48,                    /* rightshift */
841          2,                     /* size (0 = byte, 1 = short, 2 = long) */
842          64,                    /* bitsize */
843          FALSE,                 /* pc_relative */
844          10,                    /* bitpos */
845          complain_overflow_dont, /* complain_on_overflow */
846          bfd_elf_generic_reloc, /* special_function */
847          "R_SH_GOTOFF_HI16",    /* name */
848          FALSE,                 /* partial_inplace */
849          0,                     /* src_mask */
850          0x3fffc00,             /* dst_mask */
851          FALSE),                /* pcrel_offset */
852
853   /* Used in MOVI and SHORI (x & 65536).  */
854   HOWTO (R_SH_GOTPC_LOW16,      /* type */
855          0,                     /* rightshift */
856          2,                     /* size (0 = byte, 1 = short, 2 = long) */
857          64,                    /* bitsize */
858          TRUE,                  /* pc_relative */
859          10,                    /* bitpos */
860          complain_overflow_dont, /* complain_on_overflow */
861          bfd_elf_generic_reloc, /* special_function */
862          "R_SH_GOTPC_LOW16",    /* name */
863          FALSE,                 /* partial_inplace */
864          0,                     /* src_mask */
865          0x3fffc00,             /* dst_mask */
866          TRUE),                 /* pcrel_offset */
867
868   /* Used in MOVI and SHORI ((x >> 16) & 65536).  */
869   HOWTO (R_SH_GOTPC_MEDLOW16,   /* type */
870          16,                    /* rightshift */
871          2,                     /* size (0 = byte, 1 = short, 2 = long) */
872          64,                    /* bitsize */
873          TRUE,                  /* pc_relative */
874          10,                    /* bitpos */
875          complain_overflow_dont, /* complain_on_overflow */
876          bfd_elf_generic_reloc, /* special_function */
877          "R_SH_GOTPC_MEDLOW16", /* name */
878          FALSE,                 /* partial_inplace */
879          0,                     /* src_mask */
880          0x3fffc00,             /* dst_mask */
881          TRUE),                 /* pcrel_offset */
882
883   /* Used in MOVI and SHORI ((x >> 32) & 65536).  */
884   HOWTO (R_SH_GOTPC_MEDHI16,    /* type */
885          32,                    /* rightshift */
886          2,                     /* size (0 = byte, 1 = short, 2 = long) */
887          64,                    /* bitsize */
888          TRUE,                  /* pc_relative */
889          10,                    /* bitpos */
890          complain_overflow_dont, /* complain_on_overflow */
891          bfd_elf_generic_reloc, /* special_function */
892          "R_SH_GOTPC_MEDHI16",  /* name */
893          FALSE,                 /* partial_inplace */
894          0,                     /* src_mask */
895          0x3fffc00,             /* dst_mask */
896          TRUE),                 /* pcrel_offset */
897
898   /* Used in MOVI and SHORI ((x >> 48) & 65536).  */
899   HOWTO (R_SH_GOTPC_HI16,       /* type */
900          48,                    /* rightshift */
901          2,                     /* size (0 = byte, 1 = short, 2 = long) */
902          64,                    /* bitsize */
903          TRUE,                  /* pc_relative */
904          10,                    /* bitpos */
905          complain_overflow_dont, /* complain_on_overflow */
906          bfd_elf_generic_reloc, /* special_function */
907          "R_SH_GOTPC_HI16",     /* name */
908          FALSE,                 /* partial_inplace */
909          0,                     /* src_mask */
910          0x3fffc00,             /* dst_mask */
911          TRUE),                 /* pcrel_offset */
912
913   /* Used in LD.L, FLD.S et al.  */
914   HOWTO (R_SH_GOT10BY4,         /* type */
915          2,                     /* rightshift */
916          2,                     /* size (0 = byte, 1 = short, 2 = long) */
917          12,                    /* bitsize */
918          FALSE,                 /* pc_relative */
919          10,                    /* bitpos */
920          complain_overflow_signed, /* complain_on_overflow */
921          bfd_elf_generic_reloc, /* special_function */
922          "R_SH_GOT10BY4",       /* name */
923          FALSE,                 /* partial_inplace */
924          0,                     /* src_mask */
925          0xffc00,               /* dst_mask */
926          FALSE),                /* pcrel_offset */
927
928   /* Used in LD.L, FLD.S et al.  */
929   HOWTO (R_SH_GOTPLT10BY4,      /* type */
930          2,                     /* rightshift */
931          2,                     /* size (0 = byte, 1 = short, 2 = long) */
932          12,                    /* bitsize */
933          FALSE,                 /* pc_relative */
934          10,                    /* bitpos */
935          complain_overflow_signed, /* complain_on_overflow */
936          bfd_elf_generic_reloc, /* special_function */
937          "R_SH_GOTPLT10BY4",    /* name */
938          FALSE,                 /* partial_inplace */
939          0,                     /* src_mask */
940          0xffc00,               /* dst_mask */
941          FALSE),                /* pcrel_offset */
942
943   /* Used in FLD.D, FST.P et al.  */
944   HOWTO (R_SH_GOT10BY8,         /* type */
945          3,                     /* rightshift */
946          2,                     /* size (0 = byte, 1 = short, 2 = long) */
947          13,                    /* bitsize */
948          FALSE,                 /* pc_relative */
949          10,                    /* bitpos */
950          complain_overflow_signed, /* complain_on_overflow */
951          bfd_elf_generic_reloc, /* special_function */
952          "R_SH_GOT10BY8",       /* name */
953          FALSE,                 /* partial_inplace */
954          0,                     /* src_mask */
955          0xffc00,               /* dst_mask */
956          FALSE),                /* pcrel_offset */
957
958   /* Used in FLD.D, FST.P et al.  */
959   HOWTO (R_SH_GOTPLT10BY8,      /* type */
960          3,                     /* rightshift */
961          2,                     /* size (0 = byte, 1 = short, 2 = long) */
962          13,                    /* bitsize */
963          FALSE,                 /* pc_relative */
964          10,                    /* bitpos */
965          complain_overflow_signed, /* complain_on_overflow */
966          bfd_elf_generic_reloc, /* special_function */
967          "R_SH_GOTPLT10BY8",    /* name */
968          FALSE,                 /* partial_inplace */
969          0,                     /* src_mask */
970          0xffc00,               /* dst_mask */
971          FALSE),                /* pcrel_offset */
972
973   HOWTO (R_SH_COPY64,           /* type */
974          0,                     /* rightshift */
975          4,                     /* size (0 = byte, 1 = short, 2 = long) */
976          64,                    /* bitsize */
977          FALSE,                 /* pc_relative */
978          0,                     /* bitpos */
979          complain_overflow_dont, /* complain_on_overflow */
980          bfd_elf_generic_reloc, /* special_function */
981          "R_SH_COPY64",         /* name */
982          FALSE,                 /* partial_inplace */
983          0,                     /* src_mask */
984          ((bfd_vma) 0) - 1,     /* dst_mask */
985          FALSE),                /* pcrel_offset */
986
987   HOWTO (R_SH_GLOB_DAT64,       /* type */
988          0,                     /* rightshift */
989          4,                     /* size (0 = byte, 1 = short, 2 = long) */
990          64,                    /* bitsize */
991          FALSE,                 /* pc_relative */
992          0,                     /* bitpos */
993          complain_overflow_dont, /* complain_on_overflow */
994          bfd_elf_generic_reloc, /* special_function */
995          "R_SH_GLOB_DAT64",     /* name */
996          FALSE,                 /* partial_inplace */
997          0,                     /* src_mask */
998          ((bfd_vma) 0) - 1,     /* dst_mask */
999          FALSE),                /* pcrel_offset */
1000
1001   HOWTO (R_SH_JMP_SLOT64,       /* type */
1002          0,                     /* rightshift */
1003          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1004          64,                    /* bitsize */
1005          FALSE,                 /* pc_relative */
1006          0,                     /* bitpos */
1007          complain_overflow_dont, /* complain_on_overflow */
1008          bfd_elf_generic_reloc, /* special_function */
1009          "R_SH_JMP_SLOT64",     /* name */
1010          FALSE,                 /* partial_inplace */
1011          0,                     /* src_mask */
1012          ((bfd_vma) 0) - 1,     /* dst_mask */
1013          FALSE),                /* pcrel_offset */
1014
1015   HOWTO (R_SH_RELATIVE64,       /* type */
1016          0,                     /* rightshift */
1017          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1018          64,                    /* bitsize */
1019          FALSE,                 /* pc_relative */
1020          0,                     /* bitpos */
1021          complain_overflow_dont, /* complain_on_overflow */
1022          bfd_elf_generic_reloc, /* special_function */
1023          "R_SH_RELATIVE64",     /* name */
1024          FALSE,                 /* partial_inplace */
1025          0,                     /* src_mask */
1026          ((bfd_vma) 0) - 1,     /* dst_mask */
1027          FALSE),                /* pcrel_offset */
1028
1029   EMPTY_HOWTO (197),
1030   EMPTY_HOWTO (198),
1031   EMPTY_HOWTO (199),
1032   EMPTY_HOWTO (200),
1033   EMPTY_HOWTO (201),
1034   EMPTY_HOWTO (202),
1035   EMPTY_HOWTO (203),
1036   EMPTY_HOWTO (204),
1037   EMPTY_HOWTO (205),
1038   EMPTY_HOWTO (206),
1039   EMPTY_HOWTO (207),
1040   EMPTY_HOWTO (208),
1041   EMPTY_HOWTO (209),
1042   EMPTY_HOWTO (210),
1043   EMPTY_HOWTO (211),
1044   EMPTY_HOWTO (212),
1045   EMPTY_HOWTO (213),
1046   EMPTY_HOWTO (214),
1047   EMPTY_HOWTO (215),
1048   EMPTY_HOWTO (216),
1049   EMPTY_HOWTO (217),
1050   EMPTY_HOWTO (218),
1051   EMPTY_HOWTO (219),
1052   EMPTY_HOWTO (220),
1053   EMPTY_HOWTO (221),
1054   EMPTY_HOWTO (222),
1055   EMPTY_HOWTO (223),
1056   EMPTY_HOWTO (224),
1057   EMPTY_HOWTO (225),
1058   EMPTY_HOWTO (226),
1059   EMPTY_HOWTO (227),
1060   EMPTY_HOWTO (228),
1061   EMPTY_HOWTO (229),
1062   EMPTY_HOWTO (230),
1063   EMPTY_HOWTO (231),
1064   EMPTY_HOWTO (232),
1065   EMPTY_HOWTO (233),
1066   EMPTY_HOWTO (234),
1067   EMPTY_HOWTO (235),
1068   EMPTY_HOWTO (236),
1069   EMPTY_HOWTO (237),
1070   EMPTY_HOWTO (238),
1071   EMPTY_HOWTO (239),
1072   EMPTY_HOWTO (240),
1073   EMPTY_HOWTO (241),
1074
1075   /* Relocations for SHmedia code.  None of these are partial_inplace or
1076      use the field being relocated.  */
1077
1078   /* The assembler will generate this reloc before a block of SHmedia
1079      instructions.  A section should be processed as assuming it contains
1080      data, unless this reloc is seen.  Note that a block of SHcompact
1081      instructions are instead preceded by R_SH_CODE.
1082      This is currently not implemented, but should be used for SHmedia
1083      linker relaxation.  */
1084   HOWTO (R_SH_SHMEDIA_CODE,     /* type */
1085          0,                     /* rightshift */
1086          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1087          0,                     /* bitsize */
1088          FALSE,                 /* pc_relative */
1089          0,                     /* bitpos */
1090          complain_overflow_unsigned, /* complain_on_overflow */
1091          sh_elf64_ignore_reloc, /* special_function */
1092          "R_SH_SHMEDIA_CODE",   /* name */
1093          FALSE,                 /* partial_inplace */
1094          0,                     /* src_mask */
1095          0,                     /* dst_mask */
1096          FALSE),                /* pcrel_offset */
1097
1098   /* The assembler will generate this reloc at a PTA or PTB instruction,
1099      and the linker checks the right type of target, or changes a PTA to a
1100      PTB, if the original insn was PT.  */
1101   HOWTO (R_SH_PT_16,            /* type */
1102          2,                     /* rightshift */
1103          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1104          18,                    /* bitsize */
1105          TRUE,                  /* pc_relative */
1106          10,                    /* bitpos */
1107          complain_overflow_signed, /* complain_on_overflow */
1108          bfd_elf_generic_reloc, /* special_function */
1109          "R_SH_PT_16",          /* name */
1110          FALSE,                 /* partial_inplace */
1111          0,                     /* src_mask */
1112          0x3fffc00,             /* dst_mask */
1113          TRUE),                 /* pcrel_offset */
1114
1115   /* Used in unexpanded MOVI.  */
1116   HOWTO (R_SH_IMMS16,           /* type */
1117          0,                     /* rightshift */
1118          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1119          16,                    /* bitsize */
1120          FALSE,                 /* pc_relative */
1121          10,                    /* bitpos */
1122          complain_overflow_signed, /* complain_on_overflow */
1123          bfd_elf_generic_reloc, /* special_function */
1124          "R_SH_IMMS16",         /* name */
1125          FALSE,                 /* partial_inplace */
1126          0,                     /* src_mask */
1127          0x3fffc00,             /* dst_mask */
1128          FALSE),                /* pcrel_offset */
1129
1130   /* Used in SHORI.  */
1131   HOWTO (R_SH_IMMU16,           /* type */
1132          0,                     /* rightshift */
1133          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1134          16,                    /* bitsize */
1135          FALSE,                 /* pc_relative */
1136          10,                    /* bitpos */
1137          complain_overflow_unsigned, /* complain_on_overflow */
1138          bfd_elf_generic_reloc, /* special_function */
1139          "R_SH_IMMU16",         /* name */
1140          FALSE,                 /* partial_inplace */
1141          0,                     /* src_mask */
1142          0x3fffc00,             /* dst_mask */
1143          FALSE),                /* pcrel_offset */
1144
1145   /* Used in MOVI and SHORI (x & 65536).  */
1146   HOWTO (R_SH_IMM_LOW16,        /* type */
1147          0,                     /* rightshift */
1148          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1149          64,                    /* bitsize */
1150          FALSE,                 /* pc_relative */
1151          10,                    /* bitpos */
1152          complain_overflow_dont, /* complain_on_overflow */
1153          bfd_elf_generic_reloc, /* special_function */
1154          "R_SH_IMM_LOW16",      /* name */
1155          FALSE,                 /* partial_inplace */
1156          0,                     /* src_mask */
1157          0x3fffc00,             /* dst_mask */
1158          FALSE),                /* pcrel_offset */
1159
1160   /* Used in MOVI and SHORI ((x - $) & 65536).  */
1161   HOWTO (R_SH_IMM_LOW16_PCREL,  /* type */
1162          0,                     /* rightshift */
1163          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1164          64,                    /* bitsize */
1165          TRUE,                  /* pc_relative */
1166          10,                    /* bitpos */
1167          complain_overflow_dont, /* complain_on_overflow */
1168          bfd_elf_generic_reloc, /* special_function */
1169          "R_SH_IMM_LOW16_PCREL", /* name */
1170          FALSE,                 /* partial_inplace */
1171          0,                     /* src_mask */
1172          0x3fffc00,             /* dst_mask */
1173          TRUE),                 /* pcrel_offset */
1174
1175   /* Used in MOVI and SHORI ((x >> 16) & 65536).  */
1176   HOWTO (R_SH_IMM_MEDLOW16,     /* type */
1177          16,                    /* rightshift */
1178          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1179          64,                    /* bitsize */
1180          FALSE,                 /* pc_relative */
1181          10,                    /* bitpos */
1182          complain_overflow_dont, /* complain_on_overflow */
1183          bfd_elf_generic_reloc, /* special_function */
1184          "R_SH_IMM_MEDLOW16",   /* name */
1185          FALSE,                 /* partial_inplace */
1186          0,                     /* src_mask */
1187          0x3fffc00,             /* dst_mask */
1188          FALSE),                /* pcrel_offset */
1189
1190   /* Used in MOVI and SHORI (((x - $) >> 16) & 65536).  */
1191   HOWTO (R_SH_IMM_MEDLOW16_PCREL, /* type */
1192          16,                    /* rightshift */
1193          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1194          64,                    /* bitsize */
1195          TRUE,                  /* pc_relative */
1196          10,                    /* bitpos */
1197          complain_overflow_dont, /* complain_on_overflow */
1198          bfd_elf_generic_reloc, /* special_function */
1199          "R_SH_IMM_MEDLOW16_PCREL", /* name */
1200          FALSE,                 /* partial_inplace */
1201          0,                     /* src_mask */
1202          0x3fffc00,             /* dst_mask */
1203          TRUE),                 /* pcrel_offset */
1204
1205   /* Used in MOVI and SHORI ((x >> 32) & 65536).  */
1206   HOWTO (R_SH_IMM_MEDHI16,      /* type */
1207          32,                    /* rightshift */
1208          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1209          64,                    /* bitsize */
1210          FALSE,                 /* pc_relative */
1211          10,                    /* bitpos */
1212          complain_overflow_dont, /* complain_on_overflow */
1213          bfd_elf_generic_reloc, /* special_function */
1214          "R_SH_IMM_MEDHI16",    /* name */
1215          FALSE,                 /* partial_inplace */
1216          0,                     /* src_mask */
1217          0x3fffc00,             /* dst_mask */
1218          FALSE),                /* pcrel_offset */
1219
1220   /* Used in MOVI and SHORI (((x - $) >> 32) & 65536).  */
1221   HOWTO (R_SH_IMM_MEDHI16_PCREL, /* type */
1222          32,                    /* rightshift */
1223          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1224          64,                    /* bitsize */
1225          TRUE,                  /* pc_relative */
1226          10,                    /* bitpos */
1227          complain_overflow_dont, /* complain_on_overflow */
1228          bfd_elf_generic_reloc, /* special_function */
1229          "R_SH_IMM_MEDHI16_PCREL", /* name */
1230          FALSE,                 /* partial_inplace */
1231          0,                     /* src_mask */
1232          0x3fffc00,             /* dst_mask */
1233          TRUE),                 /* pcrel_offset */
1234
1235   /* Used in MOVI and SHORI ((x >> 48) & 65536).  */
1236   HOWTO (R_SH_IMM_HI16,         /* type */
1237          48,                    /* rightshift */
1238          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1239          64,                    /* bitsize */
1240          FALSE,                 /* pc_relative */
1241          10,                    /* bitpos */
1242          complain_overflow_dont, /* complain_on_overflow */
1243          bfd_elf_generic_reloc, /* special_function */
1244          "R_SH_IMM_HI16",       /* name */
1245          FALSE,                 /* partial_inplace */
1246          0,                     /* src_mask */
1247          0x3fffc00,             /* dst_mask */
1248          FALSE),                /* pcrel_offset */
1249
1250   /* Used in MOVI and SHORI (((x - $) >> 48) & 65536).  */
1251   HOWTO (R_SH_IMM_HI16_PCREL,   /* type */
1252          48,                    /* rightshift */
1253          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1254          64,                    /* bitsize */
1255          TRUE,                  /* pc_relative */
1256          10,                    /* bitpos */
1257          complain_overflow_dont, /* complain_on_overflow */
1258          bfd_elf_generic_reloc, /* special_function */
1259          "R_SH_IMM_HI16_PCREL", /* name */
1260          FALSE,                 /* partial_inplace */
1261          0,                     /* src_mask */
1262          0x3fffc00,             /* dst_mask */
1263          TRUE),                 /* pcrel_offset */
1264
1265   /* For the .uaquad pseudo.  */
1266   HOWTO (R_SH_64,               /* type */
1267          0,                     /* rightshift */
1268          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1269          64,                    /* bitsize */
1270          FALSE,                 /* pc_relative */
1271          0,                     /* bitpos */
1272          complain_overflow_dont, /* complain_on_overflow */
1273          bfd_elf_generic_reloc, /* special_function */
1274          "R_SH_64",             /* name */
1275          FALSE,                 /* partial_inplace */
1276          0,                     /* src_mask */
1277          ((bfd_vma) 0) - 1,     /* dst_mask */
1278          FALSE),                /* pcrel_offset */
1279
1280   /* For the .uaquad pseudo, (x - $).  */
1281   HOWTO (R_SH_64_PCREL,         /* type */
1282          48,                    /* rightshift */
1283          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1284          64,                    /* bitsize */
1285          TRUE,                  /* pc_relative */
1286          10,                    /* bitpos */
1287          complain_overflow_dont, /* complain_on_overflow */
1288          bfd_elf_generic_reloc, /* special_function */
1289          "R_SH_64_PCREL",       /* name */
1290          FALSE,                 /* partial_inplace */
1291          0,                     /* src_mask */
1292          ((bfd_vma) 0) - 1,     /* dst_mask */
1293          TRUE),                 /* pcrel_offset */
1294
1295 };
1296
1297 /* This function is used for relocs which are only used for relaxing,
1298    which the linker should otherwise ignore.  */
1299
1300 static bfd_reloc_status_type
1301 sh_elf64_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1302                        asymbol *symbol ATTRIBUTE_UNUSED,
1303                        void *data ATTRIBUTE_UNUSED, asection *input_section,
1304                        bfd *output_bfd,
1305                        char **error_message ATTRIBUTE_UNUSED)
1306 {
1307   if (output_bfd != NULL)
1308     reloc_entry->address += input_section->output_offset;
1309   return bfd_reloc_ok;
1310 }
1311
1312 /* This function is used for normal relocs.  This used to be like the COFF
1313    function, and is almost certainly incorrect for other ELF targets.
1314
1315    See sh_elf_reloc in elf32-sh.c for the original.  */
1316
1317 static bfd_reloc_status_type
1318 sh_elf64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
1319                 void *data, asection *input_section, bfd *output_bfd,
1320                 char **error_message ATTRIBUTE_UNUSED)
1321 {
1322   unsigned long insn;
1323   bfd_vma sym_value;
1324   enum elf_sh_reloc_type r_type;
1325   bfd_vma addr = reloc_entry->address;
1326   bfd_byte *hit_data = addr + (bfd_byte *) data;
1327
1328   r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
1329
1330   if (output_bfd != NULL)
1331     {
1332       /* Partial linking--do nothing.  */
1333       reloc_entry->address += input_section->output_offset;
1334       return bfd_reloc_ok;
1335     }
1336
1337   if (symbol_in != NULL
1338       && bfd_is_und_section (symbol_in->section))
1339     return bfd_reloc_undefined;
1340
1341   if (bfd_is_com_section (symbol_in->section))
1342     sym_value = 0;
1343   else
1344     sym_value = (symbol_in->value +
1345                  symbol_in->section->output_section->vma +
1346                  symbol_in->section->output_offset);
1347
1348   switch (r_type)
1349     {
1350     case R_SH_DIR32:
1351       insn = bfd_get_32 (abfd, hit_data);
1352       insn += sym_value + reloc_entry->addend;
1353       bfd_put_32 (abfd, insn, hit_data);
1354       break;
1355
1356     default:
1357       abort ();
1358       break;
1359     }
1360
1361   return bfd_reloc_ok;
1362 }
1363
1364 /* This structure is used to map BFD reloc codes to SH ELF relocs.  */
1365
1366 struct elf_reloc_map
1367 {
1368   bfd_reloc_code_real_type bfd_reloc_val;
1369   unsigned char elf_reloc_val;
1370 };
1371
1372 /* An array mapping BFD reloc codes to SH ELF relocs.  */
1373
1374 static const struct elf_reloc_map sh64_reloc_map[] =
1375 {
1376   { BFD_RELOC_NONE, R_SH_NONE },
1377   { BFD_RELOC_32, R_SH_DIR32 },
1378   { BFD_RELOC_CTOR, R_SH_DIR32 },
1379   { BFD_RELOC_32_PCREL, R_SH_REL32 },
1380   { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
1381   { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
1382   { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
1383   { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
1384   { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
1385   { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 },
1386   { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 },
1387   { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 },
1388   { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 },
1389   { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 },
1390   { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 },
1391   { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 },
1392   { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 },
1393   { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 },
1394   { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 },
1395   { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 },
1396   { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 },
1397   { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 },
1398   { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 },
1399   { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 },
1400   { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 },
1401   { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 },
1402   { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 },
1403   { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 },
1404   { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 },
1405   { BFD_RELOC_SH_COPY64, R_SH_COPY64 },
1406   { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 },
1407   { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 },
1408   { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 },
1409   { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 },
1410   { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 },
1411   { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 },
1412   { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 },
1413   { BFD_RELOC_SH_PT_16, R_SH_PT_16 },
1414   { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE },
1415   { BFD_RELOC_SH_IMMU5, R_SH_DIR5U },
1416   { BFD_RELOC_SH_IMMS6, R_SH_DIR6S },
1417   { BFD_RELOC_SH_IMMU6, R_SH_DIR6U },
1418   { BFD_RELOC_SH_IMMS10, R_SH_DIR10S },
1419   { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW },
1420   { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL },
1421   { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ },
1422   { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 },
1423   { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 },
1424   { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 },
1425   { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL },
1426   { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 },
1427   { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL },
1428   { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 },
1429   { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL },
1430   { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 },
1431   { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL },
1432   { BFD_RELOC_64, R_SH_64 },
1433   { BFD_RELOC_64_PCREL, R_SH_64_PCREL },
1434 };
1435
1436 /* Given a BFD reloc code, return the howto structure for the
1437    corresponding SH ELf reloc.  */
1438
1439 static reloc_howto_type *
1440 sh_elf64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1441                             bfd_reloc_code_real_type code)
1442 {
1443   unsigned int i;
1444
1445   for (i = 0; i < sizeof (sh64_reloc_map) / sizeof (struct elf_reloc_map); i++)
1446     {
1447       if (sh64_reloc_map[i].bfd_reloc_val == code)
1448         return &sh_elf64_howto_table[(int) sh64_reloc_map[i].elf_reloc_val];
1449     }
1450
1451   return NULL;
1452 }
1453
1454 /* Given an ELF reloc, fill in the howto field of a relent.
1455
1456    See sh_elf_info_to_howto in elf32-sh.c for the original.  */
1457
1458 static void
1459 sh_elf64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
1460                         Elf_Internal_Rela *dst)
1461 {
1462   unsigned int r;
1463
1464   r = ELF64_R_TYPE (dst->r_info);
1465
1466   BFD_ASSERT (r <= (unsigned int) R_SH_64_PCREL);
1467   BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
1468   BFD_ASSERT (r < R_SH_DIR8WPN || r > R_SH_LAST_INVALID_RELOC_2);
1469   BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_3 || r > R_SH_GOTPLT32);
1470   BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_4 || r > R_SH_LAST_INVALID_RELOC_4);
1471
1472   cache_ptr->howto = &sh_elf64_howto_table[r];
1473 }
1474
1475 /* Relocate an SH ELF section.
1476
1477    See sh_elf_info_to_howto in elf32-sh.c for the original.  */
1478
1479 static bfd_boolean
1480 sh_elf64_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1481                            struct bfd_link_info *info, bfd *input_bfd,
1482                            asection *input_section, bfd_byte *contents,
1483                            Elf_Internal_Rela *relocs,
1484                            Elf_Internal_Sym *local_syms,
1485                            asection **local_sections)
1486 {
1487   Elf_Internal_Shdr *symtab_hdr;
1488   struct elf_link_hash_entry **sym_hashes;
1489   Elf_Internal_Rela *rel, *relend;
1490   bfd *dynobj;
1491   bfd_vma *local_got_offsets;
1492   asection *sgot;
1493   asection *sgotplt;
1494   asection *splt;
1495   asection *sreloc;
1496   bfd_vma disp, dropped;
1497
1498   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1499   sym_hashes = elf_sym_hashes (input_bfd);
1500   dynobj = elf_hash_table (info)->dynobj;
1501   local_got_offsets = elf_local_got_offsets (input_bfd);
1502
1503   sgot = NULL;
1504   sgotplt = NULL;
1505   splt = NULL;
1506   sreloc = NULL;
1507
1508   rel = relocs;
1509   relend = relocs + input_section->reloc_count;
1510   for (; rel < relend; rel++)
1511     {
1512       int r_type;
1513       reloc_howto_type *howto;
1514       unsigned long r_symndx;
1515       Elf_Internal_Sym *sym;
1516       asection *sec;
1517       struct elf_link_hash_entry *h;
1518       bfd_vma relocation;
1519       bfd_vma addend = (bfd_vma)0;
1520       bfd_reloc_status_type r;
1521       int seen_stt_datalabel = 0;
1522
1523       r_symndx = ELF64_R_SYM (rel->r_info);
1524
1525       r_type = ELF64_R_TYPE (rel->r_info);
1526
1527       if (r_type == (int) R_SH_NONE)
1528         continue;
1529
1530       if (r_type < 0
1531           || r_type > R_SH_64_PCREL
1532           || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
1533               && r_type <= (int) R_SH_LAST_INVALID_RELOC)
1534           || (r_type >= (int) R_SH_DIR8WPN
1535               && r_type <= (int) R_SH_LAST_INVALID_RELOC)
1536           || (r_type >= (int) R_SH_GNU_VTINHERIT
1537               && r_type <= (int) R_SH_PSHL)
1538           || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
1539               && r_type <= R_SH_GOTPLT32)
1540           || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
1541               && r_type <= (int) R_SH_LAST_INVALID_RELOC_4))
1542         {
1543           bfd_set_error (bfd_error_bad_value);
1544           return FALSE;
1545         }
1546
1547       howto = sh_elf64_howto_table + r_type;
1548
1549       /* This is a final link.  */
1550       h = NULL;
1551       sym = NULL;
1552       sec = NULL;
1553       if (r_symndx < symtab_hdr->sh_info)
1554         {
1555           sym = local_syms + r_symndx;
1556           sec = local_sections[r_symndx];
1557           relocation = ((sec->output_section->vma
1558                          + sec->output_offset
1559                          + sym->st_value)
1560                         | ((sym->st_other & STO_SH5_ISA32) != 0));
1561
1562           /* A local symbol never has STO_SH5_ISA32, so we don't need
1563              datalabel processing here.  Make sure this does not change
1564              without notice.  */
1565           if ((sym->st_other & STO_SH5_ISA32) != 0)
1566             ((*info->callbacks->reloc_dangerous)
1567              (info,
1568               _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
1569               input_bfd, input_section, rel->r_offset));
1570
1571           if (info->relocatable)
1572             {
1573               /* This is a relocatable link.  We don't have to change
1574                  anything, unless the reloc is against a section symbol,
1575                  in which case we have to adjust according to where the
1576                  section symbol winds up in the output section.  */
1577               sym = local_syms + r_symndx;
1578               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1579                 goto final_link_relocate;
1580
1581               continue;
1582             }
1583           else if (! howto->partial_inplace)
1584             {
1585               relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1586               relocation |= ((sym->st_other & STO_SH5_ISA32) != 0);
1587             }
1588           else if ((sec->flags & SEC_MERGE)
1589                    && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1590             {
1591               asection *msec;
1592
1593               if (howto->rightshift || howto->src_mask != 0xffffffff)
1594                 {
1595                   (*_bfd_error_handler)
1596                     (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
1597                      input_bfd, input_section,
1598                      (long) rel->r_offset, howto->name);
1599                   return FALSE;
1600                 }
1601
1602               addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
1603               msec = sec;
1604               addend =
1605                 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
1606                 - relocation;
1607               addend += msec->output_section->vma + msec->output_offset;
1608               bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
1609               addend = 0;
1610             }
1611         }
1612       else
1613         {
1614           /* ??? Could we use the RELOC_FOR_GLOBAL_SYMBOL macro here ?  */
1615
1616           /* Section symbols are never (?) placed in the hash table, so
1617              we can just ignore hash relocations when creating a
1618              relocatable object file.  */
1619           if (info->relocatable)
1620             continue;
1621
1622           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1623           while (h->root.type == bfd_link_hash_indirect
1624                  || h->root.type == bfd_link_hash_warning)
1625             {
1626               /* If the reference passes a symbol marked with
1627                  STT_DATALABEL, then any STO_SH5_ISA32 on the final value
1628                  doesn't count.  */
1629               seen_stt_datalabel |= h->type == STT_DATALABEL;
1630               h = (struct elf_link_hash_entry *) h->root.u.i.link;
1631             }
1632
1633           if (h->root.type == bfd_link_hash_defined
1634               || h->root.type == bfd_link_hash_defweak)
1635             {
1636               sec = h->root.u.def.section;
1637               /* In these cases, we don't need the relocation value.
1638                  We check specially because in some obscure cases
1639                  sec->output_section will be NULL.  */
1640               if (r_type == R_SH_GOTPC_LOW16
1641                   || r_type == R_SH_GOTPC_MEDLOW16
1642                   || r_type == R_SH_GOTPC_MEDHI16
1643                   || r_type == R_SH_GOTPC_HI16
1644                   || ((r_type == R_SH_PLT_LOW16
1645                        || r_type == R_SH_PLT_MEDLOW16
1646                        || r_type == R_SH_PLT_MEDHI16
1647                        || r_type == R_SH_PLT_HI16)
1648                       && h->plt.offset != (bfd_vma) -1)
1649                   || ((r_type == R_SH_GOT_LOW16
1650                        || r_type == R_SH_GOT_MEDLOW16
1651                        || r_type == R_SH_GOT_MEDHI16
1652                        || r_type == R_SH_GOT_HI16)
1653                       && elf_hash_table (info)->dynamic_sections_created
1654                       && (! info->shared
1655                           || (! info->symbolic && h->dynindx != -1)
1656                           || !h->def_regular))
1657                   /* The cases above are those in which relocation is
1658                      overwritten in the switch block below.  The cases
1659                      below are those in which we must defer relocation
1660                      to run-time, because we can't resolve absolute
1661                      addresses when creating a shared library.  */
1662                   || (info->shared
1663                       && ((! info->symbolic && h->dynindx != -1)
1664                           || !h->def_regular)
1665                       && ((r_type == R_SH_64
1666                            && !(ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
1667                                 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN))
1668                           || r_type == R_SH_64_PCREL)
1669                       && ((input_section->flags & SEC_ALLOC) != 0
1670                           /* DWARF will emit R_SH_DIR32 relocations in its
1671                              sections against symbols defined externally
1672                              in shared libraries.  We can't do anything
1673                              with them here.  */
1674                           || (input_section->flags & SEC_DEBUGGING) != 0))
1675                   /* Dynamic relocs are not propagated for SEC_DEBUGGING
1676                      sections because such sections are not SEC_ALLOC and
1677                      thus ld.so will not process them.  */
1678                   || (sec->output_section == NULL
1679                       && ((input_section->flags & SEC_DEBUGGING) != 0
1680                           && h->def_dynamic)))
1681                 relocation = 0;
1682               else if (sec->output_section == NULL)
1683                 {
1684                   (*_bfd_error_handler)
1685                     (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1686                      bfd_get_filename (input_bfd), h->root.root.string,
1687                      bfd_get_section_name (input_bfd, input_section));
1688                   relocation = 0;
1689                 }
1690               else
1691                 relocation = ((h->root.u.def.value
1692                                + sec->output_section->vma
1693                                + sec->output_offset)
1694                               /* A STO_SH5_ISA32 causes a "bitor 1" to the
1695                                  symbol value, unless we've seen
1696                                  STT_DATALABEL on the way to it.  */
1697                               | ((h->other & STO_SH5_ISA32) != 0
1698                                  && ! seen_stt_datalabel));
1699             }
1700           else if (h->root.type == bfd_link_hash_undefweak)
1701             relocation = 0;
1702           else if (info->unresolved_syms_in_objects == RM_IGNORE
1703                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1704             relocation = 0;
1705           else
1706             {
1707               if (! ((*info->callbacks->undefined_symbol)
1708                      (info, h->root.root.string, input_bfd,
1709                       input_section, rel->r_offset,
1710                       (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
1711                        || ELF_ST_VISIBILITY (h->other)))))
1712                 return FALSE;
1713               relocation = 0;
1714             }
1715         }
1716
1717       disp = (relocation
1718               - input_section->output_section->vma
1719               - input_section->output_offset
1720               - rel->r_offset);
1721       dropped = 0;
1722       switch ((int)r_type)
1723         {
1724         case R_SH_PT_16:     dropped = disp & 2; break;
1725         case R_SH_DIR10SW: dropped = disp & 1; break;
1726         case R_SH_DIR10SL: dropped = disp & 3; break;
1727         case R_SH_DIR10SQ: dropped = disp & 7; break;
1728         }
1729       if (dropped != 0)
1730         {
1731           (*_bfd_error_handler)
1732             (_("%s: error: unaligned relocation type %d at %08x reloc %08x\n"),
1733              bfd_get_filename (input_bfd), (int)r_type, (unsigned)rel->r_offset, (unsigned)relocation);
1734           bfd_set_error (bfd_error_bad_value);
1735           return FALSE;
1736         }
1737       switch ((int)r_type)
1738         {
1739         case R_SH_64:
1740         case R_SH_64_PCREL:
1741           if (info->shared
1742               && (input_section->flags & SEC_ALLOC) != 0
1743               && (r_type != R_SH_64_PCREL
1744                   || (h != NULL
1745                       && h->dynindx != -1
1746                       && (! info->symbolic
1747                           || !h->def_regular))))
1748             {
1749               Elf_Internal_Rela outrel;
1750               bfd_byte *loc;
1751               bfd_boolean skip, relocate;
1752
1753               /* When generating a shared object, these relocations
1754                  are copied into the output file to be resolved at run
1755                  time.  */
1756
1757               if (sreloc == NULL)
1758                 {
1759                   const char *name;
1760
1761                   name = (bfd_elf_string_from_elf_section
1762                           (input_bfd,
1763                            elf_elfheader (input_bfd)->e_shstrndx,
1764                            elf_section_data (input_section)->rel_hdr.sh_name));
1765                   if (name == NULL)
1766                     return FALSE;
1767
1768                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1769                               && strcmp (bfd_get_section_name (input_bfd,
1770                                                                input_section),
1771                                          name + 5) == 0);
1772
1773                   sreloc = bfd_get_section_by_name (dynobj, name);
1774                   BFD_ASSERT (sreloc != NULL);
1775                 }
1776
1777               skip = FALSE;
1778               relocate = FALSE;
1779
1780               outrel.r_offset
1781                 = _bfd_elf_section_offset (output_bfd, info,
1782                                            input_section, rel->r_offset);
1783
1784               if (outrel.r_offset == (bfd_vma) -1)
1785                 skip = TRUE;
1786               else if (outrel.r_offset == (bfd_vma) -2)
1787                 skip = TRUE, relocate = TRUE;
1788
1789               outrel.r_offset += (input_section->output_section->vma
1790                                   + input_section->output_offset);
1791
1792               if (skip)
1793                 memset (&outrel, 0, sizeof outrel);
1794               else if (r_type == R_SH_64_PCREL)
1795                 {
1796                   BFD_ASSERT (h != NULL && h->dynindx != -1);
1797                   outrel.r_info = ELF64_R_INFO (h->dynindx, R_SH_64_PCREL);
1798                   outrel.r_addend = rel->r_addend;
1799                 }
1800               else
1801                 {
1802                   /* h->dynindx may be -1 if this symbol was marked to
1803                      become local.  */
1804                   if (h == NULL
1805                       || ((info->symbolic || h->dynindx == -1)
1806                           && h->def_regular))
1807                     {
1808                       relocate = TRUE;
1809                       outrel.r_info = ELF64_R_INFO (0, R_SH_RELATIVE64);
1810                       outrel.r_addend = relocation + rel->r_addend;
1811                     }
1812                   else
1813                     {
1814                       BFD_ASSERT (h->dynindx != -1);
1815                       outrel.r_info = ELF64_R_INFO (h->dynindx, R_SH_64);
1816                       outrel.r_addend = relocation + rel->r_addend;
1817                     }
1818                 }
1819
1820               loc = sreloc->contents;
1821               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
1822               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1823
1824               /* If this reloc is against an external symbol, we do
1825                  not want to fiddle with the addend.  Otherwise, we
1826                  need to include the symbol value so that it becomes
1827                  an addend for the dynamic reloc.  */
1828               if (! relocate)
1829                 continue;
1830             }
1831           else if (r_type == R_SH_64)
1832             addend = rel->r_addend;
1833           goto final_link_relocate;
1834
1835         case R_SH_GOTPLT_LOW16:
1836         case R_SH_GOTPLT_MEDLOW16:
1837         case R_SH_GOTPLT_MEDHI16:
1838         case R_SH_GOTPLT_HI16:
1839         case R_SH_GOTPLT10BY4:
1840         case R_SH_GOTPLT10BY8:
1841           /* Relocation is to the entry for this symbol in the
1842              procedure linkage table.  */
1843
1844           if (h == NULL
1845               || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
1846               || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
1847               || ! info->shared
1848               || info->symbolic
1849               || h->dynindx == -1
1850               || h->plt.offset == (bfd_vma) -1
1851               || h->got.offset != (bfd_vma) -1)
1852             goto force_got;
1853
1854           /* Relocation is to the entry for this symbol in the global
1855              offset table extension for the procedure linkage table.  */
1856           if (sgotplt == NULL)
1857             {
1858               sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1859               BFD_ASSERT (sgotplt != NULL);
1860             }
1861
1862           relocation = (sgotplt->output_offset
1863                         + ((h->plt.offset / elf_sh64_sizeof_plt (info)
1864                             - 1 + 3) * 8));
1865
1866           relocation -= GOT_BIAS;
1867
1868           goto final_link_relocate;
1869
1870         force_got:
1871         case R_SH_GOT_LOW16:
1872         case R_SH_GOT_MEDLOW16:
1873         case R_SH_GOT_MEDHI16:
1874         case R_SH_GOT_HI16:
1875         case R_SH_GOT10BY4:
1876         case R_SH_GOT10BY8:
1877           /* Relocation is to the entry for this symbol in the global
1878              offset table.  */
1879           if (sgot == NULL)
1880             {
1881               sgot = bfd_get_section_by_name (dynobj, ".got");
1882               BFD_ASSERT (sgot != NULL);
1883             }
1884
1885           if (h != NULL)
1886             {
1887               bfd_vma off;
1888
1889               off = h->got.offset;
1890               if (seen_stt_datalabel)
1891                 {
1892                   struct elf_sh64_link_hash_entry *hsh;
1893
1894                   hsh = (struct elf_sh64_link_hash_entry *)h;
1895                   off = hsh->datalabel_got_offset;
1896                 }
1897               BFD_ASSERT (off != (bfd_vma) -1);
1898
1899               if (! elf_hash_table (info)->dynamic_sections_created
1900                   || (info->shared
1901                       && (info->symbolic || h->dynindx == -1
1902                           || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
1903                           || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
1904                       && h->def_regular))
1905                 {
1906                   /* This is actually a static link, or it is a
1907                      -Bsymbolic link and the symbol is defined
1908                      locally, or the symbol was forced to be local
1909                      because of a version file.  We must initialize
1910                      this entry in the global offset table.  Since the
1911                      offset must always be a multiple of 4, we use the
1912                      least significant bit to record whether we have
1913                      initialized it already.
1914
1915                      When doing a dynamic link, we create a .rela.got
1916                      relocation entry to initialize the value.  This
1917                      is done in the finish_dynamic_symbol routine.  */
1918                   if ((off & 1) != 0)
1919                     off &= ~1;
1920                   else
1921                     {
1922                       bfd_put_64 (output_bfd, relocation,
1923                                   sgot->contents + off);
1924                       if (seen_stt_datalabel)
1925                         {
1926                           struct elf_sh64_link_hash_entry *hsh;
1927
1928                           hsh = (struct elf_sh64_link_hash_entry *)h;
1929                           hsh->datalabel_got_offset |= 1;
1930                         }
1931                       else
1932                         h->got.offset |= 1;
1933                     }
1934                 }
1935
1936               relocation = sgot->output_offset + off;
1937             }
1938           else
1939             {
1940               bfd_vma off;
1941
1942               if (rel->r_addend)
1943                 {
1944                   BFD_ASSERT (local_got_offsets != NULL
1945                               && (local_got_offsets[symtab_hdr->sh_info
1946                                                     + r_symndx]
1947                                   != (bfd_vma) -1));
1948
1949                   off = local_got_offsets[symtab_hdr->sh_info
1950                                           + r_symndx];
1951                 }
1952               else
1953                 {
1954                   BFD_ASSERT (local_got_offsets != NULL
1955                               && local_got_offsets[r_symndx] != (bfd_vma) -1);
1956
1957                   off = local_got_offsets[r_symndx];
1958                 }
1959
1960               /* The offset must always be a multiple of 8.  We use
1961                  the least significant bit to record whether we have
1962                  already generated the necessary reloc.  */
1963               if ((off & 1) != 0)
1964                 off &= ~1;
1965               else
1966                 {
1967                   bfd_put_64 (output_bfd, relocation, sgot->contents + off);
1968
1969                   if (info->shared)
1970                     {
1971                       asection *s;
1972                       Elf_Internal_Rela outrel;
1973                       bfd_byte *loc;
1974
1975                       s = bfd_get_section_by_name (dynobj, ".rela.got");
1976                       BFD_ASSERT (s != NULL);
1977
1978                       outrel.r_offset = (sgot->output_section->vma
1979                                          + sgot->output_offset
1980                                          + off);
1981                       outrel.r_info = ELF64_R_INFO (0, R_SH_RELATIVE64);
1982                       outrel.r_addend = relocation;
1983                       loc = s->contents;
1984                       loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
1985                       bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1986                     }
1987
1988                   if (rel->r_addend)
1989                     local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
1990                   else
1991                     local_got_offsets[r_symndx] |= 1;
1992                 }
1993
1994               relocation = sgot->output_offset + off;
1995             }
1996
1997           relocation -= GOT_BIAS;
1998
1999           goto final_link_relocate;
2000
2001         case R_SH_GOTOFF_LOW16:
2002         case R_SH_GOTOFF_MEDLOW16:
2003         case R_SH_GOTOFF_MEDHI16:
2004         case R_SH_GOTOFF_HI16:
2005           /* Relocation is relative to the start of the global offset
2006              table.  */
2007
2008           if (sgot == NULL)
2009             {
2010               sgot = bfd_get_section_by_name (dynobj, ".got");
2011               BFD_ASSERT (sgot != NULL);
2012             }
2013
2014           /* Note that sgot->output_offset is not involved in this
2015              calculation.  We always want the start of .got.  If we
2016              defined _GLOBAL_OFFSET_TABLE in a different way, as is
2017              permitted by the ABI, we might have to change this
2018              calculation.  */
2019           relocation -= sgot->output_section->vma;
2020
2021           relocation -= GOT_BIAS;
2022
2023           addend = rel->r_addend;
2024
2025           goto final_link_relocate;
2026
2027         case R_SH_GOTPC_LOW16:
2028         case R_SH_GOTPC_MEDLOW16:
2029         case R_SH_GOTPC_MEDHI16:
2030         case R_SH_GOTPC_HI16:
2031           /* Use global offset table as symbol value.  */
2032
2033           if (sgot == NULL)
2034             {
2035               sgot = bfd_get_section_by_name (dynobj, ".got");
2036               BFD_ASSERT (sgot != NULL);
2037             }
2038
2039           relocation = sgot->output_section->vma;
2040
2041           relocation += GOT_BIAS;
2042
2043           addend = rel->r_addend;
2044
2045           goto final_link_relocate;
2046
2047         case R_SH_PLT_LOW16:
2048         case R_SH_PLT_MEDLOW16:
2049         case R_SH_PLT_MEDHI16:
2050         case R_SH_PLT_HI16:
2051           /* Relocation is to the entry for this symbol in the
2052              procedure linkage table.  */
2053
2054           /* Resolve a PLT reloc against a local symbol directly,
2055              without using the procedure linkage table.  */
2056           if (h == NULL)
2057             goto final_link_relocate;
2058
2059           if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2060               || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
2061             goto final_link_relocate;
2062
2063           if (h->plt.offset == (bfd_vma) -1)
2064             {
2065               /* We didn't make a PLT entry for this symbol.  This
2066                  happens when statically linking PIC code, or when
2067                  using -Bsymbolic.  */
2068               goto final_link_relocate;
2069             }
2070
2071           if (splt == NULL)
2072             {
2073               splt = bfd_get_section_by_name (dynobj, ".plt");
2074               BFD_ASSERT (splt != NULL);
2075             }
2076
2077           relocation = (splt->output_section->vma
2078                         + splt->output_offset
2079                         + h->plt.offset);
2080           relocation++;
2081
2082           addend = rel->r_addend;
2083
2084           goto final_link_relocate;
2085
2086         case R_SH_DIR32:
2087         case R_SH_SHMEDIA_CODE:
2088         case R_SH_PT_16:
2089         case R_SH_DIR5U:
2090         case R_SH_DIR6S:
2091         case R_SH_DIR6U:
2092         case R_SH_DIR10S:
2093         case R_SH_DIR10SW:
2094         case R_SH_DIR10SL:
2095         case R_SH_DIR10SQ:
2096         case R_SH_IMMS16:
2097         case R_SH_IMMU16:
2098         case R_SH_IMM_LOW16:
2099         case R_SH_IMM_LOW16_PCREL:
2100         case R_SH_IMM_MEDLOW16:
2101         case R_SH_IMM_MEDLOW16_PCREL:
2102         case R_SH_IMM_MEDHI16:
2103         case R_SH_IMM_MEDHI16_PCREL:
2104         case R_SH_IMM_HI16:
2105         case R_SH_IMM_HI16_PCREL:
2106           addend = rel->r_addend;
2107           /* Fall through.  */
2108         case R_SH_REL32:
2109         final_link_relocate:
2110           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2111                                         contents, rel->r_offset,
2112                                         relocation, addend);
2113           break;
2114
2115         default:
2116           bfd_set_error (bfd_error_bad_value);
2117           return FALSE;
2118
2119         }
2120
2121       if (r != bfd_reloc_ok)
2122         {
2123           switch (r)
2124             {
2125             default:
2126             case bfd_reloc_outofrange:
2127               abort ();
2128             case bfd_reloc_overflow:
2129               {
2130                 const char *name;
2131
2132                 if (h != NULL)
2133                   name = h->root.root.string;
2134                 else
2135                   {
2136                     name = (bfd_elf_string_from_elf_section
2137                             (input_bfd, symtab_hdr->sh_link, sym->st_name));
2138                     if (name == NULL)
2139                       return FALSE;
2140                     if (*name == '\0')
2141                       name = bfd_section_name (input_bfd, sec);
2142                   }
2143                 if (! ((*info->callbacks->reloc_overflow)
2144                        (info, name, howto->name, (bfd_vma) 0,
2145                         input_bfd, input_section, rel->r_offset)))
2146                   return FALSE;
2147               }
2148               break;
2149             }
2150         }
2151     }
2152
2153   return TRUE;
2154 }
2155
2156 /* This is a version of bfd_generic_get_relocated_section_contents
2157    that uses sh_elf64_relocate_section.
2158
2159    See sh_elf_relocate_section in elf32-sh.c for the original.  */
2160
2161 static bfd_byte *
2162 sh_elf64_get_relocated_section_contents (bfd *output_bfd,
2163                                          struct bfd_link_info *link_info,
2164                                          struct bfd_link_order *link_order,
2165                                          bfd_byte *data,
2166                                          bfd_boolean relocatable,
2167                                          asymbol **symbols)
2168 {
2169   Elf_Internal_Shdr *symtab_hdr;
2170   asection *input_section = link_order->u.indirect.section;
2171   bfd *input_bfd = input_section->owner;
2172   asection **sections = NULL;
2173   Elf_Internal_Rela *internal_relocs = NULL;
2174   Elf_Internal_Sym *isymbuf = NULL;
2175
2176   /* We only need to handle the case of relaxing, or of having a
2177      particular set of section contents, specially.  */
2178   if (relocatable
2179       || elf_section_data (input_section)->this_hdr.contents == NULL)
2180     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2181                                                        link_order, data,
2182                                                        relocatable,
2183                                                        symbols);
2184
2185   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2186
2187   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2188           input_section->size);
2189
2190   if ((input_section->flags & SEC_RELOC) != 0
2191       && input_section->reloc_count > 0)
2192     {
2193       Elf_Internal_Sym *isymp;
2194       Elf_Internal_Sym *isymend;
2195       asection **secpp;
2196
2197       /* Read this BFD's local symbols.  */
2198       if (symtab_hdr->sh_info != 0)
2199         {
2200           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2201           if (isymbuf == NULL)
2202             isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2203                                             symtab_hdr->sh_info, 0,
2204                                             NULL, NULL, NULL);
2205           if (isymbuf == NULL)
2206             goto error_return;
2207         }
2208
2209       internal_relocs = (_bfd_elf_link_read_relocs
2210                          (input_bfd, input_section, NULL,
2211                           (Elf_Internal_Rela *) NULL, FALSE));
2212       if (internal_relocs == NULL)
2213         goto error_return;
2214
2215       sections = (asection **) bfd_malloc (symtab_hdr->sh_info
2216                                            * sizeof (asection *));
2217       if (sections == NULL && symtab_hdr->sh_info > 0)
2218         goto error_return;
2219
2220       secpp = sections;
2221       isymend = isymbuf + symtab_hdr->sh_info;
2222       for (isymp = isymbuf; isymp < isymend; ++isymp, ++secpp)
2223         {
2224           asection *isec;
2225
2226           if (isymp->st_shndx == SHN_UNDEF)
2227             isec = bfd_und_section_ptr;
2228           else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
2229             isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2230           else if (isymp->st_shndx == SHN_ABS)
2231             isec = bfd_abs_section_ptr;
2232           else if (isymp->st_shndx == SHN_COMMON)
2233             isec = bfd_com_section_ptr;
2234           else
2235             {
2236               /* Who knows?  */
2237               isec = NULL;
2238             }
2239
2240           *secpp = isec;
2241         }
2242
2243       if (! sh_elf64_relocate_section (output_bfd, link_info, input_bfd,
2244                                        input_section, data, internal_relocs,
2245                                        isymbuf, sections))
2246         goto error_return;
2247
2248       if (sections != NULL)
2249         free (sections);
2250       if (internal_relocs != elf_section_data (input_section)->relocs)
2251         free (internal_relocs);
2252       if (isymbuf != NULL
2253           && (unsigned char *) isymbuf != symtab_hdr->contents)
2254         free (isymbuf);
2255     }
2256
2257   return data;
2258
2259  error_return:
2260   if (sections != NULL)
2261     free (sections);
2262   if (internal_relocs != NULL
2263       && internal_relocs != elf_section_data (input_section)->relocs)
2264     free (internal_relocs);
2265   if (isymbuf != NULL
2266       && (unsigned char *) isymbuf != symtab_hdr->contents)
2267     free (isymbuf);
2268   return NULL;
2269 }
2270
2271 /* Set the SHF_SH5_ISA32 flag for ISA SHmedia code sections.  */
2272
2273 bfd_boolean
2274 sh64_elf64_fake_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2275                           Elf_Internal_Shdr *elf_section_hdr,
2276                           asection *asect)
2277 {
2278   /* Code sections can only contain SH64 code, so mark them as such.  */
2279   if (bfd_get_section_flags (output_bfd, asect) & SEC_CODE)
2280     elf_section_hdr->sh_flags |= SHF_SH5_ISA32;
2281
2282   return TRUE;
2283 }
2284
2285 static bfd_boolean
2286 sh_elf64_set_mach_from_flags (bfd *abfd)
2287 {
2288   flagword flags = elf_elfheader (abfd)->e_flags;
2289
2290   switch (flags & EF_SH_MACH_MASK)
2291     {
2292     case EF_SH5:
2293       /* Just one, but keep the switch construct to make additions easy.  */
2294       bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh5);
2295       break;
2296
2297     default:
2298       bfd_set_error (bfd_error_wrong_format);
2299       return FALSE;
2300     }
2301   return TRUE;
2302 }
2303
2304 /* Function to keep SH64 specific file flags.
2305
2306    See sh64_elf_set_private_flags in elf32-sh64.c for the original.  */
2307
2308 static bfd_boolean
2309 sh_elf64_set_private_flags (bfd *abfd, flagword flags)
2310 {
2311   BFD_ASSERT (! elf_flags_init (abfd)
2312               || elf_elfheader (abfd)->e_flags == flags);
2313
2314   elf_elfheader (abfd)->e_flags = flags;
2315   elf_flags_init (abfd) = TRUE;
2316   return sh_elf64_set_mach_from_flags (abfd);
2317 }
2318
2319 /* Copy the SHF_SH5_ISA32 attribute that we keep on all sections with
2320    code, to keep attributes the same as for SHmedia in 32-bit ELF.  */
2321
2322 static bfd_boolean
2323 sh_elf64_copy_private_data_internal (bfd *ibfd, bfd *obfd)
2324 {
2325   Elf_Internal_Shdr **o_shdrp;
2326   asection *isec;
2327   asection *osec;
2328
2329   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2330       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2331     return TRUE;
2332
2333   o_shdrp = elf_elfsections (obfd);
2334   for (osec = obfd->sections; osec; osec = osec->next)
2335     {
2336       int oIndex = ((struct bfd_elf_section_data *) elf_section_data (osec))->this_idx;
2337       for (isec = ibfd->sections; isec; isec = isec->next)
2338         {
2339           if (strcmp (osec->name, isec->name) == 0)
2340             {
2341               /* Note that we're not disallowing mixing data and code.  */
2342               if ((elf_section_data (isec)->this_hdr.sh_flags
2343                    & SHF_SH5_ISA32) != 0)
2344                 o_shdrp[oIndex]->sh_flags |= SHF_SH5_ISA32;
2345               break;
2346             }
2347         }
2348     }
2349
2350   return sh_elf64_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
2351 }
2352
2353 static bfd_boolean
2354 sh_elf64_copy_private_data (bfd *ibfd, bfd *obfd)
2355 {
2356   return sh_elf64_copy_private_data_internal (ibfd, obfd);
2357 }
2358
2359 static bfd_boolean
2360 sh_elf64_merge_private_data (bfd *ibfd, bfd *obfd)
2361 {
2362   flagword old_flags, new_flags;
2363
2364   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2365     return FALSE;
2366
2367   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2368       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2369     return TRUE;
2370
2371   if (bfd_get_arch_size (ibfd) != bfd_get_arch_size (obfd))
2372     {
2373       const char *msg;
2374
2375       if (bfd_get_arch_size (ibfd) == 32
2376           && bfd_get_arch_size (obfd) == 64)
2377         msg = _("%s: compiled as 32-bit object and %s is 64-bit");
2378       else if (bfd_get_arch_size (ibfd) == 64
2379                && bfd_get_arch_size (obfd) == 32)
2380         msg = _("%s: compiled as 64-bit object and %s is 32-bit");
2381       else
2382         msg = _("%s: object size does not match that of target %s");
2383
2384       (*_bfd_error_handler) (msg, bfd_get_filename (ibfd),
2385                              bfd_get_filename (obfd));
2386       bfd_set_error (bfd_error_wrong_format);
2387       return FALSE;
2388     }
2389
2390   old_flags = elf_elfheader (obfd)->e_flags;
2391   new_flags = elf_elfheader (ibfd)->e_flags;
2392   if (! elf_flags_init (obfd))
2393     {
2394       /* This happens when ld starts out with a 'blank' output file.  */
2395       elf_flags_init (obfd) = TRUE;
2396       elf_elfheader (obfd)->e_flags = old_flags = new_flags;
2397     }
2398   /* We don't allow linking in anything else than SH64 code, and since
2399      this is a 64-bit ELF, we assume the 64-bit ABI is used.  Add code
2400      here as things change.  */
2401   else if ((new_flags & EF_SH_MACH_MASK) != EF_SH5)
2402     {
2403       (*_bfd_error_handler)
2404         ("%s: does not use the SH64 64-bit ABI as previous modules do",
2405          bfd_get_filename (ibfd));
2406       bfd_set_error (bfd_error_bad_value);
2407       return FALSE;
2408     }
2409
2410   sh_elf64_copy_private_data_internal (ibfd, obfd);
2411
2412   /* I can't think of anything sane other than old_flags being EF_SH5 and
2413      that we need to preserve that.  */
2414   elf_elfheader (obfd)->e_flags = old_flags;
2415
2416   return sh_elf64_set_mach_from_flags (obfd);
2417 }
2418
2419 /* Return the section that should be marked against GC for a given
2420    relocation.  */
2421
2422 static asection *
2423 sh_elf64_gc_mark_hook (asection *sec,
2424                        struct bfd_link_info *info ATTRIBUTE_UNUSED,
2425                        Elf_Internal_Rela *rel,
2426                        struct elf_link_hash_entry *h,
2427                        Elf_Internal_Sym *sym)
2428 {
2429   if (h != NULL)
2430     {
2431       switch (ELF64_R_TYPE (rel->r_info))
2432         {
2433         case R_SH_GNU_VTINHERIT:
2434         case R_SH_GNU_VTENTRY:
2435           break;
2436
2437         default:
2438           while (h->root.type == bfd_link_hash_indirect
2439                  && h->root.u.i.link)
2440             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2441           switch (h->root.type)
2442             {
2443             case bfd_link_hash_defined:
2444             case bfd_link_hash_defweak:
2445               return h->root.u.def.section;
2446
2447             case bfd_link_hash_common:
2448               return h->root.u.c.p->section;
2449
2450             default:
2451               break;
2452             }
2453         }
2454     }
2455   else
2456     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2457
2458   return NULL;
2459 }
2460
2461 /* Update the got entry reference counts for the section being removed.  */
2462
2463 static bfd_boolean
2464 sh_elf64_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
2465                         struct bfd_link_info *info ATTRIBUTE_UNUSED,
2466                         asection *sec ATTRIBUTE_UNUSED,
2467                         const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
2468 {
2469   /* No got and plt entries for 64-bit SH at present.  */
2470   return TRUE;
2471 }
2472
2473 /* Look through the relocs for a section during the first phase.
2474    Since we don't do .gots or .plts, we just need to consider the
2475    virtual table relocs for gc.  */
2476
2477 static bfd_boolean
2478 sh_elf64_check_relocs (bfd *abfd, struct bfd_link_info *info,
2479                        asection *sec, const Elf_Internal_Rela *relocs)
2480 {
2481   Elf_Internal_Shdr *symtab_hdr;
2482   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2483   const Elf_Internal_Rela *rel;
2484   const Elf_Internal_Rela *rel_end;
2485   bfd *dynobj;
2486   bfd_vma *local_got_offsets;
2487   asection *sgot;
2488   asection *srelgot;
2489   asection *sreloc;
2490
2491   sgot = NULL;
2492   srelgot = NULL;
2493   sreloc = NULL;
2494
2495   if (info->relocatable)
2496     return TRUE;
2497
2498   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2499   sym_hashes = elf_sym_hashes (abfd);
2500   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf64_External_Sym);
2501   if (!elf_bad_symtab (abfd))
2502     sym_hashes_end -= symtab_hdr->sh_info;
2503
2504   dynobj = elf_hash_table (info)->dynobj;
2505   local_got_offsets = elf_local_got_offsets (abfd);
2506
2507   rel_end = relocs + sec->reloc_count;
2508   for (rel = relocs; rel < rel_end; rel++)
2509     {
2510       struct elf_link_hash_entry *h;
2511       unsigned long r_symndx;
2512
2513       r_symndx = ELF64_R_SYM (rel->r_info);
2514       if (r_symndx < symtab_hdr->sh_info)
2515         h = NULL;
2516       else
2517         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2518
2519       /* Some relocs require a global offset table.  */
2520       if (dynobj == NULL)
2521         {
2522           switch (ELF64_R_TYPE (rel->r_info))
2523             {
2524             case R_SH_GOTPLT_LOW16:
2525             case R_SH_GOTPLT_MEDLOW16:
2526             case R_SH_GOTPLT_MEDHI16:
2527             case R_SH_GOTPLT_HI16:
2528             case R_SH_GOTPLT10BY4:
2529             case R_SH_GOTPLT10BY8:
2530             case R_SH_GOT_LOW16:
2531             case R_SH_GOT_MEDLOW16:
2532             case R_SH_GOT_MEDHI16:
2533             case R_SH_GOT_HI16:
2534             case R_SH_GOT10BY4:
2535             case R_SH_GOT10BY8:
2536             case R_SH_GOTOFF_LOW16:
2537             case R_SH_GOTOFF_MEDLOW16:
2538             case R_SH_GOTOFF_MEDHI16:
2539             case R_SH_GOTOFF_HI16:
2540             case R_SH_GOTPC_LOW16:
2541             case R_SH_GOTPC_MEDLOW16:
2542             case R_SH_GOTPC_MEDHI16:
2543             case R_SH_GOTPC_HI16:
2544               elf_hash_table (info)->dynobj = dynobj = abfd;
2545               if (! _bfd_elf_create_got_section (dynobj, info))
2546                 return FALSE;
2547               break;
2548
2549             default:
2550               break;
2551             }
2552         }
2553
2554       switch (ELF64_R_TYPE (rel->r_info))
2555         {
2556           /* This relocation describes the C++ object vtable hierarchy.
2557              Reconstruct it for later use during GC.  */
2558         case R_SH_GNU_VTINHERIT:
2559           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2560             return FALSE;
2561           break;
2562
2563           /* This relocation describes which C++ vtable entries are actually
2564              used.  Record for later use during GC.  */
2565         case R_SH_GNU_VTENTRY:
2566           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2567             return FALSE;
2568           break;
2569
2570         force_got:
2571         case R_SH_GOT_LOW16:
2572         case R_SH_GOT_MEDLOW16:
2573         case R_SH_GOT_MEDHI16:
2574         case R_SH_GOT_HI16:
2575         case R_SH_GOT10BY4:
2576         case R_SH_GOT10BY8:
2577           /* This symbol requires a global offset table entry.  */
2578
2579           if (sgot == NULL)
2580             {
2581               sgot = bfd_get_section_by_name (dynobj, ".got");
2582               BFD_ASSERT (sgot != NULL);
2583             }
2584
2585           if (srelgot == NULL
2586               && (h != NULL || info->shared))
2587             {
2588               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2589               if (srelgot == NULL)
2590                 {
2591                   srelgot = bfd_make_section (dynobj, ".rela.got");
2592                   if (srelgot == NULL
2593                       || ! bfd_set_section_flags (dynobj, srelgot,
2594                                                   (SEC_ALLOC
2595                                                    | SEC_LOAD
2596                                                    | SEC_HAS_CONTENTS
2597                                                    | SEC_IN_MEMORY
2598                                                    | SEC_LINKER_CREATED
2599                                                    | SEC_READONLY))
2600                       || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2601                     return FALSE;
2602                 }
2603             }
2604
2605           if (h != NULL)
2606             {
2607               if (h->type == STT_DATALABEL)
2608                 {
2609                   struct elf_sh64_link_hash_entry *hsh;
2610
2611                   h = (struct elf_link_hash_entry *) h->root.u.i.link;
2612                   hsh = (struct elf_sh64_link_hash_entry *)h;
2613                   if (hsh->datalabel_got_offset != (bfd_vma) -1)
2614                     break;
2615
2616                   hsh->datalabel_got_offset = sgot->size;
2617                 }
2618               else
2619                 {
2620                   if (h->got.offset != (bfd_vma) -1)
2621                     {
2622                       /* We have already allocated space in the .got.  */
2623                       break;
2624                     }
2625                   h->got.offset = sgot->size;
2626                 }
2627
2628               /* Make sure this symbol is output as a dynamic symbol.  */
2629               if (h->dynindx == -1)
2630                 {
2631                   if (! bfd_elf_link_record_dynamic_symbol (info, h))
2632                     return FALSE;
2633                 }
2634
2635               srelgot->size += sizeof (Elf64_External_Rela);
2636             }
2637           else
2638             {
2639               /* This is a global offset table entry for a local
2640                  symbol.  */
2641               if (local_got_offsets == NULL)
2642                 {
2643                   size_t size;
2644                   register unsigned int i;
2645
2646                   size = symtab_hdr->sh_info * sizeof (bfd_vma);
2647                   /* Reserve space for both the datalabel and
2648                      codelabel local GOT offsets.  */
2649                   size *= 2;
2650                   local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
2651                   if (local_got_offsets == NULL)
2652                     return FALSE;
2653                   elf_local_got_offsets (abfd) = local_got_offsets;
2654                   for (i = 0; i < symtab_hdr->sh_info; i++)
2655                     local_got_offsets[i] = (bfd_vma) -1;
2656                   for (; i < 2 * symtab_hdr->sh_info; i++)
2657                     local_got_offsets[i] = (bfd_vma) -1;
2658                 }
2659               if ((rel->r_addend & 1) != 0)
2660                 {
2661                   if (local_got_offsets[symtab_hdr->sh_info
2662                                         + r_symndx] != (bfd_vma) -1)
2663                     {
2664                       /* We have already allocated space in the .got.  */
2665                       break;
2666                     }
2667                   local_got_offsets[symtab_hdr->sh_info
2668                                     + r_symndx] = sgot->size;
2669                 }
2670               else
2671                 {
2672                   if (local_got_offsets[r_symndx] != (bfd_vma) -1)
2673                     {
2674                       /* We have already allocated space in the .got.  */
2675                       break;
2676                     }
2677                   local_got_offsets[r_symndx] = sgot->size;
2678                 }
2679
2680               if (info->shared)
2681                 {
2682                   /* If we are generating a shared object, we need to
2683                      output a R_SH_RELATIVE reloc so that the dynamic
2684                      linker can adjust this GOT entry.  */
2685                   srelgot->size += sizeof (Elf64_External_Rela);
2686                 }
2687             }
2688
2689           sgot->size += 8;
2690
2691           break;
2692
2693         case R_SH_GOTPLT_LOW16:
2694         case R_SH_GOTPLT_MEDLOW16:
2695         case R_SH_GOTPLT_MEDHI16:
2696         case R_SH_GOTPLT_HI16:
2697         case R_SH_GOTPLT10BY4:
2698         case R_SH_GOTPLT10BY8:
2699           /* If this is a local symbol, we resolve it directly without
2700              creating a procedure linkage table entry.  */
2701
2702           if (h == NULL
2703               || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2704               || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2705               || ! info->shared
2706               || info->symbolic
2707               || h->dynindx == -1
2708               || h->got.offset != (bfd_vma) -1)
2709             goto force_got;
2710
2711           /* Make sure this symbol is output as a dynamic symbol.  */
2712           if (h->dynindx == -1)
2713             {
2714               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2715                 return FALSE;
2716             }
2717
2718           h->needs_plt = 1;
2719
2720           break;
2721
2722         case R_SH_PLT_LOW16:
2723         case R_SH_PLT_MEDLOW16:
2724         case R_SH_PLT_MEDHI16:
2725         case R_SH_PLT_HI16:
2726           /* This symbol requires a procedure linkage table entry.  We
2727              actually build the entry in adjust_dynamic_symbol,
2728              because this might be a case of linking PIC code which is
2729              never referenced by a dynamic object, in which case we
2730              don't need to generate a procedure linkage table entry
2731              after all.  */
2732
2733           /* If this is a local symbol, we resolve it directly without
2734              creating a procedure linkage table entry.  */
2735           if (h == NULL)
2736             continue;
2737
2738           if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2739               || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
2740             break;
2741
2742           h->needs_plt = 1;
2743
2744           break;
2745
2746         case R_SH_64:
2747         case R_SH_64_PCREL:
2748           if (h != NULL)
2749             h->non_got_ref = 1;
2750
2751           /* If we are creating a shared library, and this is a reloc
2752              against a global symbol, or a non PC relative reloc
2753              against a local symbol, then we need to copy the reloc
2754              into the shared library.  However, if we are linking with
2755              -Bsymbolic, we do not need to copy a reloc against a
2756              global symbol which is defined in an object we are
2757              including in the link (i.e., DEF_REGULAR is set).  At
2758              this point we have not seen all the input files, so it is
2759              possible that DEF_REGULAR is not set now but will be set
2760              later (it is never cleared).  We account for that
2761              possibility below by storing information in the
2762              pcrel_relocs_copied field of the hash table entry.  */
2763           if (info->shared
2764               && (sec->flags & SEC_ALLOC) != 0
2765               && (ELF32_R_TYPE (rel->r_info) != R_SH_64_PCREL
2766                   || (h != NULL
2767                       && (! info->symbolic
2768                           || !h->def_regular))))
2769             {
2770               /* When creating a shared object, we must copy these
2771                  reloc types into the output file.  We create a reloc
2772                  section in dynobj and make room for this reloc.  */
2773               if (sreloc == NULL)
2774                 {
2775                   const char *name;
2776
2777                   name = (bfd_elf_string_from_elf_section
2778                           (abfd,
2779                            elf_elfheader (abfd)->e_shstrndx,
2780                            elf_section_data (sec)->rel_hdr.sh_name));
2781                   if (name == NULL)
2782                     return FALSE;
2783
2784                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2785                               && strcmp (bfd_get_section_name (abfd, sec),
2786                                          name + 5) == 0);
2787
2788                   sreloc = bfd_get_section_by_name (dynobj, name);
2789                   if (sreloc == NULL)
2790                     {
2791                       flagword flags;
2792
2793                       sreloc = bfd_make_section (dynobj, name);
2794                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
2795                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2796                       if ((sec->flags & SEC_ALLOC) != 0)
2797                         flags |= SEC_ALLOC | SEC_LOAD;
2798                       if (sreloc == NULL
2799                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
2800                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
2801                         return FALSE;
2802                     }
2803                 }
2804
2805               sreloc->size += sizeof (Elf64_External_Rela);
2806
2807               /* If we are linking with -Bsymbolic, and this is a
2808                  global symbol, we count the number of PC relative
2809                  relocations we have entered for this symbol, so that
2810                  we can discard them again if the symbol is later
2811                  defined by a regular object.  Note that this function
2812                  is only called if we are using an elf_sh linker
2813                  hash table, which means that h is really a pointer to
2814                  an elf_sh_link_hash_entry.  */
2815               if (h != NULL && info->symbolic
2816                   && ELF64_R_TYPE (rel->r_info) == R_SH_64_PCREL)
2817                 {
2818                   struct elf_sh64_link_hash_entry *eh;
2819                   struct elf_sh64_pcrel_relocs_copied *p;
2820
2821                   eh = (struct elf_sh64_link_hash_entry *) h;
2822
2823                   for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2824                     if (p->section == sreloc)
2825                       break;
2826
2827                   if (p == NULL)
2828                     {
2829                       p = ((struct elf_sh64_pcrel_relocs_copied *)
2830                            bfd_alloc (dynobj, sizeof *p));
2831                       if (p == NULL)
2832                         return FALSE;
2833                       p->next = eh->pcrel_relocs_copied;
2834                       eh->pcrel_relocs_copied = p;
2835                       p->section = sreloc;
2836                       p->count = 0;
2837                     }
2838
2839                   ++p->count;
2840                 }
2841             }
2842
2843           break;
2844         }
2845     }
2846
2847   return TRUE;
2848 }
2849
2850 static int
2851 sh64_elf64_get_symbol_type (Elf_Internal_Sym * elf_sym, int type)
2852 {
2853   if (ELF_ST_TYPE (elf_sym->st_info) == STT_DATALABEL)
2854     return STT_DATALABEL;
2855
2856   return type;
2857 }
2858
2859 /* FIXME: This is a copy of sh64_elf_add_symbol_hook in elf32-sh64.c.
2860    Either file can presumably exist without the other, but do not differ
2861    in elf-size-ness.  How to share?
2862
2863    Hook called by the linker routine which adds symbols from an object
2864    file.  We must make indirect symbols for undefined symbols marked with
2865    STT_DATALABEL, so relocations passing them will pick up that attribute
2866    and neutralize STO_SH5_ISA32 found on the symbol definition.
2867
2868    There is a problem, though: We want to fill in the hash-table entry for
2869    this symbol and signal to the caller that no further processing is
2870    needed.  But we don't have the index for this hash-table entry.  We
2871    rely here on that the current entry is the first hash-entry with NULL,
2872    which seems brittle.  Also, iterating over the hash-table to find that
2873    entry is a linear operation on the number of symbols in this input
2874    file, and this function should take constant time, so that's not good
2875    too.  Only comfort is that DataLabel references should only be found in
2876    hand-written assembly code and thus be rare.  FIXME: Talk maintainers
2877    into adding an option to elf_add_symbol_hook (preferably) for the index
2878    or the hash entry, alternatively adding the index to Elf_Internal_Sym
2879    (not so good).  */
2880
2881 static bfd_boolean
2882 sh64_elf64_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
2883                             Elf_Internal_Sym *sym, const char **namep,
2884                             flagword *flagsp ATTRIBUTE_UNUSED,
2885                             asection **secp, bfd_vma *valp)
2886 {
2887   /* We want to do this for relocatable as well as final linking.  */
2888   if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL
2889       && is_elf_hash_table (info->hash))
2890     {
2891       struct elf_link_hash_entry *h;
2892
2893       /* For relocatable links, we register the DataLabel sym in its own
2894          right, and tweak the name when it's output.  Otherwise, we make
2895          an indirect symbol of it.  */
2896       flagword flags
2897         = info->relocatable || info->emitrelocations
2898         ? BSF_GLOBAL : BSF_GLOBAL | BSF_INDIRECT;
2899
2900       char *dl_name
2901         = bfd_malloc (strlen (*namep) + sizeof (DATALABEL_SUFFIX));
2902       struct elf_link_hash_entry ** sym_hash = elf_sym_hashes (abfd);
2903
2904       BFD_ASSERT (sym_hash != NULL);
2905
2906       /* Allocation may fail.  */
2907       if (dl_name == NULL)
2908         return FALSE;
2909
2910       strcpy (dl_name, *namep);
2911       strcat (dl_name, DATALABEL_SUFFIX);
2912
2913       h = (struct elf_link_hash_entry *)
2914         bfd_link_hash_lookup (info->hash, dl_name, FALSE, FALSE, FALSE);
2915
2916       if (h == NULL)
2917         {
2918           /* No previous datalabel symbol.  Make one.  */
2919           struct bfd_link_hash_entry *bh = NULL;
2920           const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2921
2922           if (! _bfd_generic_link_add_one_symbol (info, abfd, dl_name,
2923                                                   flags, *secp, *valp,
2924                                                   *namep, FALSE,
2925                                                   bed->collect, &bh))
2926             {
2927               free (dl_name);
2928               return FALSE;
2929             }
2930
2931           h = (struct elf_link_hash_entry *) bh;
2932           h->non_elf = 0;
2933           h->type = STT_DATALABEL;
2934         }
2935       else
2936         /* If a new symbol was created, it holds the allocated name.
2937            Otherwise, we don't need it anymore and should deallocate it.  */
2938         free (dl_name);
2939
2940       if (h->type != STT_DATALABEL
2941           || ((info->relocatable || info->emitrelocations)
2942               && h->root.type != bfd_link_hash_undefined)
2943           || (! info->relocatable && !info->emitrelocations
2944               && h->root.type != bfd_link_hash_indirect))
2945         {
2946           /* Make sure we don't get confused on invalid input.  */
2947           (*_bfd_error_handler)
2948             (_("%s: encountered datalabel symbol in input"),
2949              bfd_get_filename (abfd));
2950           bfd_set_error (bfd_error_bad_value);
2951           return FALSE;
2952         }
2953
2954       /* Now find the hash-table slot for this entry and fill it in.  */
2955       while (*sym_hash != NULL)
2956         sym_hash++;
2957       *sym_hash = h;
2958
2959       /* Signal to caller to skip this symbol - we've handled it.  */
2960       *namep = NULL;
2961     }
2962
2963   return TRUE;
2964 }
2965
2966 /* This hook function is called before the linker writes out a global
2967    symbol.  For relocatable links, DataLabel symbols will be present in
2968    linker output.  We cut off the special suffix on those symbols, so the
2969    right name appears in the output.
2970
2971    When linking and emitting relocations, there can appear global symbols
2972    that are not referenced by relocs, but rather only implicitly through
2973    DataLabel references, a relation that is not visible to the linker.
2974    Since no stripping of global symbols in done when doing such linking,
2975    we don't need to look up and make sure to emit the main symbol for each
2976    DataLabel symbol.  */
2977
2978 static bfd_boolean
2979 sh64_elf64_link_output_symbol_hook (struct bfd_link_info *info,
2980                                     const char *cname,
2981                                     Elf_Internal_Sym *sym,
2982                                     asection *input_sec ATTRIBUTE_UNUSED,
2983                                     struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
2984 {
2985   char *name = (char *) cname;
2986
2987   if (info->relocatable || info->emitrelocations)
2988     {
2989       if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL)
2990         name[strlen (name) - strlen (DATALABEL_SUFFIX)] = 0;
2991     }
2992
2993   return TRUE;
2994 }
2995
2996 /* Set bit 0 on the entry address; it always points to SHmedia code.  This
2997    is mostly for symmetry with the 32-bit format, where code can be
2998    SHcompact and we need to make a distinction to make sure execution
2999    starts in the right ISA mode.  It is also convenient for a loader,
3000    which would otherwise have to set this bit when loading a TR register
3001    before jumping to the program entry.  */
3002
3003 static void
3004 sh64_elf64_final_write_processing (bfd *abfd,
3005                                    bfd_boolean linker ATTRIBUTE_UNUSED)
3006 {
3007   /* FIXME: Perhaps we shouldn't do this if the entry address was supplied
3008      numerically, but we currently lack the infrastructure to recognize
3009      that: The entry symbol, and info whether it is numeric or a symbol
3010      name is kept private in the linker.  */
3011   if (elf_elfheader (abfd)->e_type == ET_EXEC)
3012     elf_elfheader (abfd)->e_entry |= 1;
3013 }
3014
3015 /* First entry in an absolute procedure linkage table look like this.  */
3016
3017 static const bfd_byte elf_sh64_plt0_entry_be[PLT_ENTRY_SIZE] =
3018 {
3019   0xcc, 0x00, 0x01, 0x10, /* movi  .got.plt >> 48, r17 */
3020   0xc8, 0x00, 0x01, 0x10, /* shori (.got.plt >> 32) & 65535, r17 */
3021   0xc8, 0x00, 0x01, 0x10, /* shori (.got.plt >> 16) & 65535, r17 */
3022   0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
3023   0x8d, 0x10, 0x09, 0x90, /* ld.q  r17, 16, r25 */
3024   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3025   0x8d, 0x10, 0x05, 0x10, /* ld.q  r17, 8, r17 */
3026   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3027   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3028   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3029   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3030   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3031   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3032   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3033   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3034   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3035 };
3036
3037 static const bfd_byte elf_sh64_plt0_entry_le[PLT_ENTRY_SIZE] =
3038 {
3039   0x10, 0x01, 0x00, 0xcc, /* movi  .got.plt >> 16, r17 */
3040   0x10, 0x01, 0x00, 0xc8, /* shori (.got.plt >> 32) & 65535, r17 */
3041   0x10, 0x01, 0x00, 0xc8, /* shori (.got.plt >> 16) & 65535, r17 */
3042   0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
3043   0x90, 0x09, 0x10, 0x8d, /* ld.q  r17, 16, r25 */
3044   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3045   0x10, 0x05, 0x10, 0x8d, /* ld.q  r17, 8, r17 */
3046   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3047   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3048   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3049   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3050   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3051   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3052   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3053   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3054   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3055 };
3056
3057 /* Sebsequent entries in an absolute procedure linkage table look like
3058    this.  */
3059
3060 static const bfd_byte elf_sh64_plt_entry_be[PLT_ENTRY_SIZE] =
3061 {
3062   0xcc, 0x00, 0x01, 0x90, /* movi  nameN-in-GOT >> 48, r25 */
3063   0xc8, 0x00, 0x01, 0x90, /* shori (nameN-in-GOT >> 32) & 65535, r25 */
3064   0xc8, 0x00, 0x01, 0x90, /* shori (nameN-in-GOT >> 16) & 65535, r25 */
3065   0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
3066   0x8d, 0x90, 0x01, 0x90, /* ld.q  r25, 0, r25 */
3067   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3068   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3069   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3070   0xcc, 0x00, 0x01, 0x90, /* movi  (.+8-.PLT0) >> 16, r25 */
3071   0xc8, 0x00, 0x01, 0x90, /* shori (.+4-.PLT0) & 65535, r25 */
3072   0x6b, 0xf5, 0x66, 0x00, /* ptrel r25, tr0 */
3073   0xcc, 0x00, 0x01, 0x50, /* movi  reloc-offset >> 16, r21 */
3074   0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
3075   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3076   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3077   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3078 };
3079
3080 static const bfd_byte elf_sh64_plt_entry_le[PLT_ENTRY_SIZE] =
3081 {
3082   0x90, 0x01, 0x00, 0xcc, /* movi  nameN-in-GOT >> 16, r25 */
3083   0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
3084   0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
3085   0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
3086   0x90, 0x01, 0x90, 0x8d, /* ld.q  r25, 0, r25 */
3087   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3088   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3089   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3090   0x90, 0x01, 0x00, 0xcc, /* movi  (.+8-.PLT0) >> 16, r25 */
3091   0x90, 0x01, 0x00, 0xc8, /* shori (.+4-.PLT0) & 65535, r25 */
3092   0x00, 0x66, 0xf5, 0x6b, /* ptrel r25, tr0 */
3093   0x50, 0x01, 0x00, 0xcc, /* movi  reloc-offset >> 16, r21 */
3094   0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
3095   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3096   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3097   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3098 };
3099
3100 /* Entries in a PIC procedure linkage table look like this.  */
3101
3102 static const bfd_byte elf_sh64_pic_plt_entry_be[PLT_ENTRY_SIZE] =
3103 {
3104   0xcc, 0x00, 0x01, 0x90, /* movi  nameN@GOT >> 16, r25 */
3105   0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
3106   0x40, 0xc3, 0x65, 0x90, /* ldx.q r12, r25, r25 */
3107   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3108   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3109   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3110   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3111   0x6f, 0xf0, 0xff, 0xf0, /* nop */
3112   0xce, 0x00, 0x01, 0x10, /* movi  -GOT_BIAS, r17 */
3113   0x00, 0xc9, 0x45, 0x10, /* add   r12, r17, r17 */
3114   0x8d, 0x10, 0x09, 0x90, /* ld.q  r17, 16, r25 */
3115   0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
3116   0x8d, 0x10, 0x05, 0x10, /* ld.q  r17, 8, r17 */
3117   0xcc, 0x00, 0x01, 0x50, /* movi  reloc-offset >> 16, r21 */
3118   0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
3119   0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
3120 };
3121
3122 static const bfd_byte elf_sh64_pic_plt_entry_le[PLT_ENTRY_SIZE] =
3123 {
3124   0x90, 0x01, 0x00, 0xcc, /* movi  nameN@GOT >> 16, r25 */
3125   0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
3126   0x90, 0x65, 0xc3, 0x40, /* ldx.q r12, r25, r25 */
3127   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3128   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3129   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3130   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3131   0xf0, 0xff, 0xf0, 0x6f, /* nop */
3132   0x10, 0x01, 0x00, 0xce, /* movi  -GOT_BIAS, r17 */
3133   0x10, 0x45, 0xc9, 0x00, /* add   r12, r17, r17 */
3134   0x90, 0x09, 0x10, 0x8d, /* ld.q  r17, 16, r25 */
3135   0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
3136   0x10, 0x05, 0x10, 0x8d, /* ld.q  r17, 8, r17 */
3137   0x50, 0x01, 0x00, 0xcc, /* movi  reloc-offset >> 16, r21 */
3138   0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
3139   0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
3140 };
3141
3142 static const bfd_byte *elf_sh64_plt0_entry;
3143 static const bfd_byte *elf_sh64_plt_entry;
3144 static const bfd_byte *elf_sh64_pic_plt_entry;
3145
3146 /* Create an entry in an sh ELF linker hash table.  */
3147
3148 static struct bfd_hash_entry *
3149 sh64_elf64_link_hash_newfunc (struct bfd_hash_entry *entry,
3150                               struct bfd_hash_table *table,
3151                               const char *string)
3152 {
3153   struct elf_sh64_link_hash_entry *ret =
3154     (struct elf_sh64_link_hash_entry *) entry;
3155
3156   /* Allocate the structure if it has not already been allocated by a
3157      subclass.  */
3158   if (ret == (struct elf_sh64_link_hash_entry *) NULL)
3159     ret = ((struct elf_sh64_link_hash_entry *)
3160            bfd_hash_allocate (table,
3161                               sizeof (struct elf_sh64_link_hash_entry)));
3162   if (ret == (struct elf_sh64_link_hash_entry *) NULL)
3163     return (struct bfd_hash_entry *) ret;
3164
3165   /* Call the allocation method of the superclass.  */
3166   ret = ((struct elf_sh64_link_hash_entry *)
3167          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3168                                      table, string));
3169   if (ret != (struct elf_sh64_link_hash_entry *) NULL)
3170     {
3171       ret->pcrel_relocs_copied = NULL;
3172       ret->datalabel_got_offset = (bfd_vma) -1;
3173     }
3174
3175   return (struct bfd_hash_entry *) ret;
3176 }
3177
3178 /* Create an sh64 ELF linker hash table.  */
3179
3180 static struct bfd_link_hash_table *
3181 sh64_elf64_link_hash_table_create (bfd *abfd)
3182 {
3183   struct elf_sh64_link_hash_table *ret;
3184
3185   ret = ((struct elf_sh64_link_hash_table *)
3186          bfd_malloc (sizeof (struct elf_sh64_link_hash_table)));
3187   if (ret == (struct elf_sh64_link_hash_table *) NULL)
3188     return NULL;
3189
3190   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3191                                        sh64_elf64_link_hash_newfunc))
3192     {
3193       free (ret);
3194       return NULL;
3195     }
3196
3197   return &ret->root.root;
3198 }
3199
3200 inline static void
3201 movi_shori_putval (bfd *output_bfd, unsigned long value, char *addr)
3202 {
3203   bfd_put_32 (output_bfd,
3204               bfd_get_32 (output_bfd, addr)
3205               | ((value >> 6) & 0x3fffc00),
3206               addr);
3207   bfd_put_32 (output_bfd,
3208               bfd_get_32 (output_bfd, addr + 4)
3209               | ((value << 10) & 0x3fffc00),
3210               addr + 4);
3211 }
3212
3213 inline static void
3214 movi_3shori_putval (bfd *output_bfd, bfd_vma value, char *addr)
3215 {
3216   bfd_put_32 (output_bfd,
3217               bfd_get_32 (output_bfd, addr)
3218               | ((value >> 38) & 0x3fffc00),
3219               addr);
3220   bfd_put_32 (output_bfd,
3221               bfd_get_32 (output_bfd, addr + 4)
3222               | ((value >> 22) & 0x3fffc00),
3223               addr + 4);
3224   bfd_put_32 (output_bfd,
3225               bfd_get_32 (output_bfd, addr + 8)
3226               | ((value >> 6) & 0x3fffc00),
3227               addr + 8);
3228   bfd_put_32 (output_bfd,
3229               bfd_get_32 (output_bfd, addr + 12)
3230               | ((value << 10) & 0x3fffc00),
3231               addr + 12);
3232 }
3233
3234 /* Create dynamic sections when linking against a dynamic object.  */
3235
3236 static bfd_boolean
3237 sh64_elf64_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3238 {
3239   flagword flags, pltflags;
3240   register asection *s;
3241   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3242   int ptralign = 0;
3243
3244   switch (bed->s->arch_size)
3245     {
3246     case 32:
3247       ptralign = 2;
3248       break;
3249
3250     case 64:
3251       ptralign = 3;
3252       break;
3253
3254     default:
3255       bfd_set_error (bfd_error_bad_value);
3256       return FALSE;
3257     }
3258
3259   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3260      .rel[a].bss sections.  */
3261
3262   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3263            | SEC_LINKER_CREATED);
3264
3265   pltflags = flags;
3266   pltflags |= SEC_CODE;
3267   if (bed->plt_not_loaded)
3268     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
3269   if (bed->plt_readonly)
3270     pltflags |= SEC_READONLY;
3271
3272   s = bfd_make_section (abfd, ".plt");
3273   if (s == NULL
3274       || ! bfd_set_section_flags (abfd, s, pltflags)
3275       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3276     return FALSE;
3277
3278   if (bed->want_plt_sym)
3279     {
3280       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3281          .plt section.  */
3282       struct elf_link_hash_entry *h;
3283       struct bfd_link_hash_entry *bh = NULL;
3284
3285       if (! (_bfd_generic_link_add_one_symbol
3286              (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3287               (bfd_vma) 0, (const char *) NULL, FALSE, bed->collect, &bh)))
3288         return FALSE;
3289
3290       h = (struct elf_link_hash_entry *) bh;
3291       h->def_regular = 1;
3292       h->type = STT_OBJECT;
3293
3294       if (info->shared
3295           && ! bfd_elf_link_record_dynamic_symbol (info, h))
3296         return FALSE;
3297     }
3298
3299   s = bfd_make_section (abfd,
3300                         bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3301   if (s == NULL
3302       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3303       || ! bfd_set_section_alignment (abfd, s, ptralign))
3304     return FALSE;
3305
3306   if (! _bfd_elf_create_got_section (abfd, info))
3307     return FALSE;
3308
3309   {
3310     const char *secname;
3311     char *relname;
3312     flagword secflags;
3313     asection *sec;
3314
3315     for (sec = abfd->sections; sec; sec = sec->next)
3316       {
3317         secflags = bfd_get_section_flags (abfd, sec);
3318         if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3319             || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3320           continue;
3321         secname = bfd_get_section_name (abfd, sec);
3322         relname = (char *) bfd_malloc (strlen (secname) + 6);
3323         strcpy (relname, ".rela");
3324         strcat (relname, secname);
3325         s = bfd_make_section (abfd, relname);
3326         if (s == NULL
3327             || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3328             || ! bfd_set_section_alignment (abfd, s, ptralign))
3329           return FALSE;
3330       }
3331   }
3332
3333   if (bed->want_dynbss)
3334     {
3335       /* The .dynbss section is a place to put symbols which are defined
3336          by dynamic objects, are referenced by regular objects, and are
3337          not functions.  We must allocate space for them in the process
3338          image and use a R_*_COPY reloc to tell the dynamic linker to
3339          initialize them at run time.  The linker script puts the .dynbss
3340          section into the .bss section of the final image.  */
3341       s = bfd_make_section (abfd, ".dynbss");
3342       if (s == NULL
3343           || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
3344         return FALSE;
3345
3346       /* The .rel[a].bss section holds copy relocs.  This section is not
3347          normally needed.  We need to create it here, though, so that the
3348          linker will map it to an output section.  We can't just create it
3349          only if we need it, because we will not know whether we need it
3350          until we have seen all the input files, and the first time the
3351          main linker code calls BFD after examining all the input files
3352          (size_dynamic_sections) the input sections have already been
3353          mapped to the output sections.  If the section turns out not to
3354          be needed, we can discard it later.  We will never need this
3355          section when generating a shared object, since they do not use
3356          copy relocs.  */
3357       if (! info->shared)
3358         {
3359           s = bfd_make_section (abfd,
3360                                 (bed->default_use_rela_p
3361                                  ? ".rela.bss" : ".rel.bss"));
3362           if (s == NULL
3363               || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3364               || ! bfd_set_section_alignment (abfd, s, ptralign))
3365             return FALSE;
3366         }
3367     }
3368
3369   return TRUE;
3370 }
3371 \f
3372 /* Adjust a symbol defined by a dynamic object and referenced by a
3373    regular object.  The current definition is in some section of the
3374    dynamic object, but we're not including those sections.  We have to
3375    change the definition to something the rest of the link can
3376    understand.  */
3377
3378 static bfd_boolean
3379 sh64_elf64_adjust_dynamic_symbol (struct bfd_link_info *info,
3380                                   struct elf_link_hash_entry *h)
3381 {
3382   bfd *dynobj;
3383   asection *s;
3384   unsigned int power_of_two;
3385
3386   dynobj = elf_hash_table (info)->dynobj;
3387
3388   /* Make sure we know what is going on here.  */
3389   BFD_ASSERT (dynobj != NULL
3390               && (h->needs_plt
3391                   || h->u.weakdef != NULL
3392                   || (h->def_dynamic
3393                       && h->ref_regular
3394                       && !h->def_regular)));
3395
3396   /* If this is a function, put it in the procedure linkage table.  We
3397      will fill in the contents of the procedure linkage table later,
3398      when we know the address of the .got section.  */
3399   if (h->type == STT_FUNC
3400       || h->needs_plt)
3401     {
3402       if (! info->shared
3403           && !h->def_dynamic
3404           && !h->ref_dynamic)
3405         {
3406           /* This case can occur if we saw a PLT reloc in an input
3407              file, but the symbol was never referred to by a dynamic
3408              object.  In such a case, we don't actually need to build
3409              a procedure linkage table, and we can just do a REL64
3410              reloc instead.  */
3411           BFD_ASSERT (h->needs_plt);
3412           return TRUE;
3413         }
3414
3415       /* Make sure this symbol is output as a dynamic symbol.  */
3416       if (h->dynindx == -1)
3417         {
3418           if (! bfd_elf_link_record_dynamic_symbol (info, h))
3419             return FALSE;
3420         }
3421
3422       s = bfd_get_section_by_name (dynobj, ".plt");
3423       BFD_ASSERT (s != NULL);
3424
3425       /* If this is the first .plt entry, make room for the special
3426          first entry.  */
3427       if (s->size == 0)
3428         s->size += PLT_ENTRY_SIZE;
3429
3430       /* If this symbol is not defined in a regular file, and we are
3431          not generating a shared library, then set the symbol to this
3432          location in the .plt.  This is required to make function
3433          pointers compare as equal between the normal executable and
3434          the shared library.  */
3435       if (! info->shared
3436           && !h->def_regular)
3437         {
3438           h->root.u.def.section = s;
3439           h->root.u.def.value = s->size;
3440         }
3441
3442       h->plt.offset = s->size;
3443
3444       /* Make room for this entry.  */
3445       s->size += elf_sh64_sizeof_plt (info);
3446
3447       /* We also need to make an entry in the .got.plt section, which
3448          will be placed in the .got section by the linker script.  */
3449
3450       s = bfd_get_section_by_name (dynobj, ".got.plt");
3451       BFD_ASSERT (s != NULL);
3452       s->size += 8;
3453
3454       /* We also need to make an entry in the .rela.plt section.  */
3455
3456       s = bfd_get_section_by_name (dynobj, ".rela.plt");
3457       BFD_ASSERT (s != NULL);
3458       s->size += sizeof (Elf64_External_Rela);
3459
3460       return TRUE;
3461     }
3462
3463   /* If this is a weak symbol, and there is a real definition, the
3464      processor independent code will have arranged for us to see the
3465      real definition first, and we can just use the same value.  */
3466   if (h->u.weakdef != NULL)
3467     {
3468       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3469                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
3470       h->root.u.def.section = h->u.weakdef->root.u.def.section;
3471       h->root.u.def.value = h->u.weakdef->root.u.def.value;
3472       return TRUE;
3473     }
3474
3475   /* This is a reference to a symbol defined by a dynamic object which
3476      is not a function.  */
3477
3478   /* If we are creating a shared library, we must presume that the
3479      only references to the symbol are via the global offset table.
3480      For such cases we need not do anything here; the relocations will
3481      be handled correctly by relocate_section.  */
3482   if (info->shared)
3483     return TRUE;
3484
3485   /* If there are no references to this symbol that do not use the
3486      GOT, we don't need to generate a copy reloc.  */
3487   if (!h->non_got_ref)
3488     return TRUE;
3489
3490   /* We must allocate the symbol in our .dynbss section, which will
3491      become part of the .bss section of the executable.  There will be
3492      an entry for this symbol in the .dynsym section.  The dynamic
3493      object will contain position independent code, so all references
3494      from the dynamic object to this symbol will go through the global
3495      offset table.  The dynamic linker will use the .dynsym entry to
3496      determine the address it must put in the global offset table, so
3497      both the dynamic object and the regular object will refer to the
3498      same memory location for the variable.  */
3499
3500   s = bfd_get_section_by_name (dynobj, ".dynbss");
3501   BFD_ASSERT (s != NULL);
3502
3503   /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
3504      copy the initial value out of the dynamic object and into the
3505      runtime process image.  We need to remember the offset into the
3506      .rela.bss section we are going to use.  */
3507   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3508     {
3509       asection *srel;
3510
3511       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
3512       BFD_ASSERT (srel != NULL);
3513       srel->size += sizeof (Elf64_External_Rela);
3514       h->needs_copy = 1;
3515     }
3516
3517   /* We need to figure out the alignment required for this symbol.  I
3518      have no idea how ELF linkers handle this.  */
3519   power_of_two = bfd_log2 (h->size);
3520   if (power_of_two > 3)
3521     power_of_two = 3;
3522
3523   /* Apply the required alignment.  */
3524   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3525   if (power_of_two > bfd_get_section_alignment (dynobj, s))
3526     {
3527       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
3528         return FALSE;
3529     }
3530
3531   /* Define the symbol as being at this point in the section.  */
3532   h->root.u.def.section = s;
3533   h->root.u.def.value = s->size;
3534
3535   /* Increment the section size to make room for the symbol.  */
3536   s->size += h->size;
3537
3538   return TRUE;
3539 }
3540
3541 /* This function is called via sh_elf_link_hash_traverse if we are
3542    creating a shared object with -Bsymbolic.  It discards the space
3543    allocated to copy PC relative relocs against symbols which are
3544    defined in regular objects.  We allocated space for them in the
3545    check_relocs routine, but we won't fill them in in the
3546    relocate_section routine.  */
3547
3548 static bfd_boolean
3549 sh64_elf64_discard_copies (struct elf_sh64_link_hash_entry *h,
3550                            void *ignore ATTRIBUTE_UNUSED)
3551 {
3552   struct elf_sh64_pcrel_relocs_copied *s;
3553
3554   if (h->root.root.type == bfd_link_hash_warning)
3555     h = (struct elf_sh64_link_hash_entry *) h->root.root.u.i.link;
3556
3557   /* We only discard relocs for symbols defined in a regular object.  */
3558   if (!h->root.def_regular)
3559     return TRUE;
3560
3561   for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
3562     s->section->size -= s->count * sizeof (Elf64_External_Rela);
3563
3564   return TRUE;
3565 }
3566
3567 /* Set the sizes of the dynamic sections.  */
3568
3569 static bfd_boolean
3570 sh64_elf64_size_dynamic_sections (bfd *output_bfd,
3571                                   struct bfd_link_info *info)
3572 {
3573   bfd *dynobj;
3574   asection *s;
3575   bfd_boolean plt;
3576   bfd_boolean relocs;
3577   bfd_boolean reltext;
3578
3579   dynobj = elf_hash_table (info)->dynobj;
3580   BFD_ASSERT (dynobj != NULL);
3581
3582   if (elf_hash_table (info)->dynamic_sections_created)
3583     {
3584       /* Set the contents of the .interp section to the interpreter.  */
3585       if (info->executable)
3586         {
3587           s = bfd_get_section_by_name (dynobj, ".interp");
3588           BFD_ASSERT (s != NULL);
3589           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3590           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3591         }
3592     }
3593   else
3594     {
3595       /* We may have created entries in the .rela.got section.
3596          However, if we are not creating the dynamic sections, we will
3597          not actually use these entries.  Reset the size of .rela.got,
3598          which will cause it to get stripped from the output file
3599          below.  */
3600       s = bfd_get_section_by_name (dynobj, ".rela.got");
3601       if (s != NULL)
3602         s->size = 0;
3603     }
3604
3605   /* If this is a -Bsymbolic shared link, then we need to discard all
3606      PC relative relocs against symbols defined in a regular object.
3607      We allocated space for them in the check_relocs routine, but we
3608      will not fill them in in the relocate_section routine.  */
3609   if (info->shared && info->symbolic)
3610     sh64_elf64_link_hash_traverse (sh64_elf64_hash_table (info),
3611                                    sh64_elf64_discard_copies, NULL);
3612
3613   /* The check_relocs and adjust_dynamic_symbol entry points have
3614      determined the sizes of the various dynamic sections.  Allocate
3615      memory for them.  */
3616   plt = FALSE;
3617   relocs = FALSE;
3618   reltext = FALSE;
3619   for (s = dynobj->sections; s != NULL; s = s->next)
3620     {
3621       const char *name;
3622       bfd_boolean strip;
3623
3624       if ((s->flags & SEC_LINKER_CREATED) == 0)
3625         continue;
3626
3627       /* It's OK to base decisions on the section name, because none
3628          of the dynobj section names depend upon the input files.  */
3629       name = bfd_get_section_name (dynobj, s);
3630
3631       strip = FALSE;
3632
3633       if (strcmp (name, ".plt") == 0)
3634         {
3635           if (s->size == 0)
3636             {
3637               /* Strip this section if we don't need it; see the
3638                  comment below.  */
3639               strip = TRUE;
3640             }
3641           else
3642             {
3643               /* Remember whether there is a PLT.  */
3644               plt = TRUE;
3645             }
3646         }
3647       else if (strncmp (name, ".rela", 5) == 0)
3648         {
3649           if (s->size == 0)
3650             {
3651               /* If we don't need this section, strip it from the
3652                  output file.  This is mostly to handle .rela.bss and
3653                  .rela.plt.  We must create both sections in
3654                  create_dynamic_sections, because they must be created
3655                  before the linker maps input sections to output
3656                  sections.  The linker does that before
3657                  adjust_dynamic_symbol is called, and it is that
3658                  function which decides whether anything needs to go
3659                  into these sections.  */
3660               strip = TRUE;
3661             }
3662           else
3663             {
3664               asection *target;
3665
3666               /* Remember whether there are any reloc sections other
3667                  than .rela.plt.  */
3668               if (strcmp (name, ".rela.plt") != 0)
3669                 {
3670                   const char *outname;
3671
3672                   relocs = TRUE;
3673
3674                   /* If this relocation section applies to a read only
3675                      section, then we probably need a DT_TEXTREL
3676                      entry.  The entries in the .rela.plt section
3677                      really apply to the .got section, which we
3678                      created ourselves and so know is not readonly.  */
3679                   outname = bfd_get_section_name (output_bfd,
3680                                                   s->output_section);
3681                   target = bfd_get_section_by_name (output_bfd, outname + 5);
3682                   if (target != NULL
3683                       && (target->flags & SEC_READONLY) != 0
3684                       && (target->flags & SEC_ALLOC) != 0)
3685                     reltext = TRUE;
3686                 }
3687
3688               /* We use the reloc_count field as a counter if we need
3689                  to copy relocs into the output file.  */
3690               s->reloc_count = 0;
3691             }
3692         }
3693       else if (strncmp (name, ".got", 4) != 0)
3694         {
3695           /* It's not one of our sections, so don't allocate space.  */
3696           continue;
3697         }
3698
3699       if (strip)
3700         {
3701           _bfd_strip_section_from_output (info, s);
3702           continue;
3703         }
3704
3705       /* Allocate memory for the section contents.  */
3706       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3707       if (s->contents == NULL && s->size != 0)
3708         return FALSE;
3709     }
3710
3711   if (elf_hash_table (info)->dynamic_sections_created)
3712     {
3713       /* Add some entries to the .dynamic section.  We fill in the
3714          values later, in sh64_elf64_finish_dynamic_sections, but we
3715          must add the entries now so that we get the correct size for
3716          the .dynamic section.  The DT_DEBUG entry is filled in by the
3717          dynamic linker and used by the debugger.  */
3718       if (info->executable)
3719         {
3720           if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
3721             return FALSE;
3722         }
3723
3724       if (plt)
3725         {
3726           if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
3727               || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
3728               || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
3729               || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
3730             return FALSE;
3731         }
3732
3733       if (relocs)
3734         {
3735           if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
3736               || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
3737               || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
3738                                               sizeof (Elf64_External_Rela)))
3739             return FALSE;
3740         }
3741
3742       if (reltext)
3743         {
3744           if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
3745             return FALSE;
3746         }
3747     }
3748
3749   return TRUE;
3750 }
3751
3752 /* Finish up dynamic symbol handling.  We set the contents of various
3753    dynamic sections here.  */
3754
3755 static bfd_boolean
3756 sh64_elf64_finish_dynamic_symbol (bfd *output_bfd,
3757                                   struct bfd_link_info *info,
3758                                   struct elf_link_hash_entry *h,
3759                                   Elf_Internal_Sym *sym)
3760 {
3761   bfd *dynobj;
3762
3763   dynobj = elf_hash_table (info)->dynobj;
3764
3765   if (h->plt.offset != (bfd_vma) -1)
3766     {
3767       asection *splt;
3768       asection *sgot;
3769       asection *srel;
3770
3771       bfd_vma plt_index;
3772       bfd_vma got_offset;
3773       Elf_Internal_Rela rel;
3774       bfd_byte *loc;
3775
3776       /* This symbol has an entry in the procedure linkage table.  Set
3777          it up.  */
3778
3779       BFD_ASSERT (h->dynindx != -1);
3780
3781       splt = bfd_get_section_by_name (dynobj, ".plt");
3782       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
3783       srel = bfd_get_section_by_name (dynobj, ".rela.plt");
3784       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
3785
3786       /* Get the index in the procedure linkage table which
3787          corresponds to this symbol.  This is the index of this symbol
3788          in all the symbols for which we are making plt entries.  The
3789          first entry in the procedure linkage table is reserved.  */
3790       plt_index = h->plt.offset / elf_sh64_sizeof_plt (info) - 1;
3791
3792       /* Get the offset into the .got table of the entry that
3793          corresponds to this function.  Each .got entry is 8 bytes.
3794          The first three are reserved.  */
3795       got_offset = (plt_index + 3) * 8;
3796
3797       if (info->shared)
3798         got_offset -= GOT_BIAS;
3799
3800       /* Fill in the entry in the procedure linkage table.  */
3801       if (! info->shared)
3802         {
3803           if (elf_sh64_plt_entry == NULL)
3804             {
3805               elf_sh64_plt_entry = (bfd_big_endian (output_bfd) ?
3806                                   elf_sh64_plt_entry_be : elf_sh64_plt_entry_le);
3807             }
3808           memcpy (splt->contents + h->plt.offset, elf_sh64_plt_entry,
3809                   elf_sh64_sizeof_plt (info));
3810           movi_3shori_putval (output_bfd,
3811                               (sgot->output_section->vma
3812                                + sgot->output_offset
3813                                + got_offset),
3814                               (splt->contents + h->plt.offset
3815                                + elf_sh64_plt_symbol_offset (info)));
3816
3817           /* Set bottom bit because its for a branch to SHmedia */
3818           movi_shori_putval (output_bfd,
3819                              -(h->plt.offset
3820                               + elf_sh64_plt_plt0_offset (info) + 8)
3821                              | 1,
3822                              (splt->contents + h->plt.offset
3823                               + elf_sh64_plt_plt0_offset (info)));
3824         }
3825       else
3826         {
3827           if (elf_sh64_pic_plt_entry == NULL)
3828             {
3829               elf_sh64_pic_plt_entry = (bfd_big_endian (output_bfd) ?
3830                                       elf_sh64_pic_plt_entry_be :
3831                                       elf_sh64_pic_plt_entry_le);
3832             }
3833           memcpy (splt->contents + h->plt.offset, elf_sh64_pic_plt_entry,
3834                   elf_sh64_sizeof_plt (info));
3835           movi_shori_putval (output_bfd, got_offset,
3836                              (splt->contents + h->plt.offset
3837                               + elf_sh64_plt_symbol_offset (info)));
3838         }
3839
3840       if (info->shared)
3841         got_offset += GOT_BIAS;
3842
3843       movi_shori_putval (output_bfd,
3844                          plt_index * sizeof (Elf64_External_Rela),
3845                          (splt->contents + h->plt.offset
3846                           + elf_sh64_plt_reloc_offset (info)));
3847
3848       /* Fill in the entry in the global offset table.  */
3849       bfd_put_64 (output_bfd,
3850                   (splt->output_section->vma
3851                    + splt->output_offset
3852                    + h->plt.offset
3853                    + elf_sh64_plt_temp_offset (info)),
3854                   sgot->contents + got_offset);
3855
3856       /* Fill in the entry in the .rela.plt section.  */
3857       rel.r_offset = (sgot->output_section->vma
3858                       + sgot->output_offset
3859                       + got_offset);
3860       rel.r_info = ELF64_R_INFO (h->dynindx, R_SH_JMP_SLOT64);
3861       rel.r_addend = 0;
3862       rel.r_addend = GOT_BIAS;
3863       loc = srel->contents + plt_index * sizeof (Elf64_External_Rela);
3864       bfd_elf64_swap_reloca_out (output_bfd, &rel, loc);
3865
3866       if (!h->def_regular)
3867         {
3868           /* Mark the symbol as undefined, rather than as defined in
3869              the .plt section.  Leave the value alone.  */
3870           sym->st_shndx = SHN_UNDEF;
3871         }
3872     }
3873
3874   if (h->got.offset != (bfd_vma) -1)
3875     {
3876       asection *sgot;
3877       asection *srel;
3878       Elf_Internal_Rela rel;
3879       bfd_byte *loc;
3880
3881       /* This symbol has an entry in the global offset table.  Set it
3882          up.  */
3883
3884       sgot = bfd_get_section_by_name (dynobj, ".got");
3885       srel = bfd_get_section_by_name (dynobj, ".rela.got");
3886       BFD_ASSERT (sgot != NULL && srel != NULL);
3887
3888       rel.r_offset = (sgot->output_section->vma
3889                       + sgot->output_offset
3890                       + (h->got.offset &~ 1));
3891
3892       /* If this is a -Bsymbolic link, and the symbol is defined
3893          locally, we just want to emit a RELATIVE reloc.  Likewise if
3894          the symbol was forced to be local because of a version file.
3895          The entry in the global offset table will already have been
3896          initialized in the relocate_section function.  */
3897       if (info->shared
3898           && (info->symbolic || h->dynindx == -1)
3899           && h->def_regular)
3900         {
3901           rel.r_info = ELF64_R_INFO (0, R_SH_RELATIVE64);
3902           rel.r_addend = (h->root.u.def.value
3903                           + h->root.u.def.section->output_section->vma
3904                           + h->root.u.def.section->output_offset);
3905         }
3906       else
3907         {
3908           bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3909           rel.r_info = ELF64_R_INFO (h->dynindx, R_SH_GLOB_DAT64);
3910           rel.r_addend = 0;
3911         }
3912
3913       loc = srel->contents;
3914       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
3915       bfd_elf64_swap_reloca_out (output_bfd, &rel, loc);
3916     }
3917
3918   if (h->needs_copy)
3919     {
3920       asection *s;
3921       Elf_Internal_Rela rel;
3922       bfd_byte *loc;
3923
3924       /* This symbol needs a copy reloc.  Set it up.  */
3925
3926       BFD_ASSERT (h->dynindx != -1
3927                   && (h->root.type == bfd_link_hash_defined
3928                       || h->root.type == bfd_link_hash_defweak));
3929
3930       s = bfd_get_section_by_name (h->root.u.def.section->owner,
3931                                    ".rela.bss");
3932       BFD_ASSERT (s != NULL);
3933
3934       rel.r_offset = (h->root.u.def.value
3935                       + h->root.u.def.section->output_section->vma
3936                       + h->root.u.def.section->output_offset);
3937       rel.r_info = ELF64_R_INFO (h->dynindx, R_SH_COPY64);
3938       rel.r_addend = 0;
3939       loc = s->contents;
3940       loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
3941       bfd_elf64_swap_reloca_out (output_bfd, &rel, loc);
3942     }
3943
3944   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
3945   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3946       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3947     sym->st_shndx = SHN_ABS;
3948
3949   return TRUE;
3950 }
3951
3952 /* Finish up the dynamic sections.  */
3953
3954 static bfd_boolean
3955 sh64_elf64_finish_dynamic_sections (bfd *output_bfd,
3956                                     struct bfd_link_info *info)
3957 {
3958   bfd *dynobj;
3959   asection *sgot;
3960   asection *sdyn;
3961
3962   dynobj = elf_hash_table (info)->dynobj;
3963
3964   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
3965   BFD_ASSERT (sgot != NULL);
3966   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3967
3968   if (elf_hash_table (info)->dynamic_sections_created)
3969     {
3970       asection *splt;
3971       Elf64_External_Dyn *dyncon, *dynconend;
3972
3973       BFD_ASSERT (sdyn != NULL);
3974
3975       dyncon = (Elf64_External_Dyn *) sdyn->contents;
3976       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3977       for (; dyncon < dynconend; dyncon++)
3978         {
3979           Elf_Internal_Dyn dyn;
3980           const char *name;
3981           asection *s;
3982           struct elf_link_hash_entry *h;
3983
3984           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3985
3986           switch (dyn.d_tag)
3987             {
3988             default:
3989               break;
3990
3991             case DT_INIT:
3992               name = info->init_function;
3993               goto get_sym;
3994
3995             case DT_FINI:
3996               name = info->fini_function;
3997             get_sym:
3998               if (dyn.d_un.d_val != 0)
3999                 {
4000                   h = elf_link_hash_lookup (elf_hash_table (info), name,
4001                                             FALSE, FALSE, TRUE);
4002                   if (h != NULL && (h->other & STO_SH5_ISA32))
4003                     {
4004                       dyn.d_un.d_val |= 1;
4005                       bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4006                     }
4007                 }
4008               break;
4009
4010             case DT_PLTGOT:
4011               name = ".got";
4012               goto get_vma;
4013
4014             case DT_JMPREL:
4015               name = ".rela.plt";
4016             get_vma:
4017               s = bfd_get_section_by_name (output_bfd, name);
4018               BFD_ASSERT (s != NULL);
4019               dyn.d_un.d_ptr = s->vma;
4020               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4021               break;
4022
4023             case DT_PLTRELSZ:
4024               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4025               BFD_ASSERT (s != NULL);
4026               dyn.d_un.d_val = s->size;
4027               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4028               break;
4029
4030             case DT_RELASZ:
4031               /* My reading of the SVR4 ABI indicates that the
4032                  procedure linkage table relocs (DT_JMPREL) should be
4033                  included in the overall relocs (DT_RELA).  This is
4034                  what Solaris does.  However, UnixWare can not handle
4035                  that case.  Therefore, we override the DT_RELASZ entry
4036                  here to make it not include the JMPREL relocs.  Since
4037                  the linker script arranges for .rela.plt to follow all
4038                  other relocation sections, we don't have to worry
4039                  about changing the DT_RELA entry.  */
4040               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4041               if (s != NULL)
4042                 dyn.d_un.d_val -= s->size;
4043               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4044               break;
4045             }
4046         }
4047
4048       /* Fill in the first entry in the procedure linkage table.  */
4049       splt = bfd_get_section_by_name (dynobj, ".plt");
4050       if (splt && splt->size > 0)
4051         {
4052           if (info->shared)
4053             {
4054               if (elf_sh64_pic_plt_entry == NULL)
4055                 {
4056                   elf_sh64_pic_plt_entry = (bfd_big_endian (output_bfd) ?
4057                                           elf_sh64_pic_plt_entry_be :
4058                                           elf_sh64_pic_plt_entry_le);
4059                 }
4060               memcpy (splt->contents, elf_sh64_pic_plt_entry,
4061                       elf_sh64_sizeof_plt (info));
4062             }
4063           else
4064             {
4065               if (elf_sh64_plt0_entry == NULL)
4066                 {
4067                   elf_sh64_plt0_entry = (bfd_big_endian (output_bfd) ?
4068                                        elf_sh64_plt0_entry_be :
4069                                        elf_sh64_plt0_entry_le);
4070                 }
4071               memcpy (splt->contents, elf_sh64_plt0_entry, PLT_ENTRY_SIZE);
4072               movi_3shori_putval (output_bfd,
4073                                   sgot->output_section->vma
4074                                   + sgot->output_offset,
4075                                   splt->contents
4076                                   + elf_sh64_plt0_gotplt_offset (info));
4077             }
4078
4079           /* UnixWare sets the entsize of .plt to 8, although that doesn't
4080              really seem like the right value.  */
4081           elf_section_data (splt->output_section)->this_hdr.sh_entsize = 8;
4082         }
4083     }
4084
4085   /* Fill in the first three entries in the global offset table.  */
4086   if (sgot->size > 0)
4087     {
4088       if (sdyn == NULL)
4089         bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
4090       else
4091         bfd_put_64 (output_bfd,
4092                     sdyn->output_section->vma + sdyn->output_offset,
4093                     sgot->contents);
4094       bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4095       bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + 16);
4096     }
4097
4098   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 8;
4099
4100   return TRUE;
4101 }
4102
4103 /* Merge non visibility st_other attribute when the symbol comes from
4104    a dynamic object.  */
4105 static void
4106 sh64_elf64_merge_symbol_attribute (struct elf_link_hash_entry *h,
4107                                    const Elf_Internal_Sym *isym,
4108                                    bfd_boolean definition,
4109                                    bfd_boolean dynamic)
4110 {
4111   if (isym->st_other != 0 && dynamic)
4112     {
4113       unsigned char other;
4114
4115       /* Take the balance of OTHER from the definition.  */
4116       other = (definition ? isym->st_other : h->other);
4117       other &= ~ ELF_ST_VISIBILITY (-1);
4118       h->other = other | ELF_ST_VISIBILITY (h->other);
4119     }
4120
4121   return;
4122 }
4123
4124 static struct bfd_elf_special_section const sh64_elf64_special_sections[]=
4125 {
4126   { ".cranges", 8, 0, SHT_PROGBITS, 0 },
4127   { NULL,       0, 0, 0,            0 }
4128 };
4129
4130 #define TARGET_BIG_SYM          bfd_elf64_sh64_vec
4131 #define TARGET_BIG_NAME         "elf64-sh64"
4132 #define TARGET_LITTLE_SYM       bfd_elf64_sh64l_vec
4133 #define TARGET_LITTLE_NAME      "elf64-sh64l"
4134 #define ELF_ARCH                bfd_arch_sh
4135 #define ELF_MACHINE_CODE        EM_SH
4136 #define ELF_MAXPAGESIZE         128
4137
4138 #define elf_symbol_leading_char '_'
4139
4140 #define bfd_elf64_bfd_reloc_type_lookup sh_elf64_reloc_type_lookup
4141 #define elf_info_to_howto               sh_elf64_info_to_howto
4142
4143 /* Note: there's no relaxation at present.  */
4144
4145 #define elf_backend_relocate_section    sh_elf64_relocate_section
4146 #define bfd_elf64_bfd_get_relocated_section_contents \
4147                                         sh_elf64_get_relocated_section_contents
4148 #define elf_backend_object_p            sh_elf64_set_mach_from_flags
4149 #define bfd_elf64_bfd_set_private_flags \
4150                                         sh_elf64_set_private_flags
4151 #define bfd_elf64_bfd_copy_private_bfd_data \
4152                                         sh_elf64_copy_private_data
4153 #define bfd_elf64_bfd_merge_private_bfd_data \
4154                                         sh_elf64_merge_private_data
4155 #define elf_backend_fake_sections       sh64_elf64_fake_sections
4156
4157 #define elf_backend_gc_mark_hook        sh_elf64_gc_mark_hook
4158 #define elf_backend_gc_sweep_hook       sh_elf64_gc_sweep_hook
4159 #define elf_backend_check_relocs        sh_elf64_check_relocs
4160
4161 #define elf_backend_can_gc_sections     1
4162
4163 #define elf_backend_get_symbol_type     sh64_elf64_get_symbol_type
4164
4165 #define elf_backend_add_symbol_hook     sh64_elf64_add_symbol_hook
4166
4167 #define elf_backend_link_output_symbol_hook \
4168         sh64_elf64_link_output_symbol_hook
4169
4170 #define elf_backend_merge_symbol_attribute \
4171         sh64_elf64_merge_symbol_attribute
4172
4173 #define elf_backend_final_write_processing \
4174         sh64_elf64_final_write_processing
4175
4176 #define elf_backend_create_dynamic_sections \
4177                                         sh64_elf64_create_dynamic_sections
4178 #define bfd_elf64_bfd_link_hash_table_create \
4179                                         sh64_elf64_link_hash_table_create
4180 #define elf_backend_adjust_dynamic_symbol \
4181                                         sh64_elf64_adjust_dynamic_symbol
4182 #define elf_backend_size_dynamic_sections \
4183                                         sh64_elf64_size_dynamic_sections
4184 #define elf_backend_finish_dynamic_symbol \
4185                                         sh64_elf64_finish_dynamic_symbol
4186 #define elf_backend_finish_dynamic_sections \
4187                                         sh64_elf64_finish_dynamic_sections
4188 #define elf_backend_special_sections    sh64_elf64_special_sections
4189
4190 #define elf_backend_want_got_plt        1
4191 #define elf_backend_plt_readonly        1
4192 #define elf_backend_want_plt_sym        0
4193 #define elf_backend_got_header_size     24
4194
4195 #include "elf64-target.h"
4196
4197 /* NetBSD support.  */
4198 #undef  TARGET_BIG_SYM
4199 #define TARGET_BIG_SYM                  bfd_elf64_sh64nbsd_vec
4200 #undef  TARGET_BIG_NAME
4201 #define TARGET_BIG_NAME                 "elf64-sh64-nbsd"
4202 #undef  TARGET_LITTLE_SYM
4203 #define TARGET_LITTLE_SYM               bfd_elf64_sh64lnbsd_vec
4204 #undef  TARGET_LITTLE_NAME
4205 #define TARGET_LITTLE_NAME              "elf64-sh64l-nbsd"
4206 #undef  ELF_MAXPAGESIZE
4207 #define ELF_MAXPAGESIZE                 0x10000
4208 #undef  elf_symbol_leading_char
4209 #define elf_symbol_leading_char         0
4210
4211 #define elf64_bed                       elf64_sh64_nbsd_bed
4212
4213 #include "elf64-target.h"
4214
4215 /* Linux support.  */
4216 #undef  TARGET_BIG_SYM
4217 #define TARGET_BIG_SYM                  bfd_elf64_sh64blin_vec
4218 #undef  TARGET_BIG_NAME
4219 #define TARGET_BIG_NAME                 "elf64-sh64big-linux"
4220 #undef  TARGET_LITTLE_SYM
4221 #define TARGET_LITTLE_SYM               bfd_elf64_sh64lin_vec
4222 #undef  TARGET_LITTLE_NAME
4223 #define TARGET_LITTLE_NAME              "elf64-sh64-linux"
4224
4225 #define INCLUDED_TARGET_FILE
4226 #include "elf64-target.h"