OSDN Git Service

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