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