OSDN Git Service

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