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