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