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