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