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