]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/binutils/bfd/elf32-sh.c
This commit was generated by cvs2svn to compensate for changes in r61515,
[FreeBSD/FreeBSD.git] / contrib / binutils / bfd / elf32-sh.c
1 /* Hitachi SH specific support for 32-bit ELF
2    Copyright 1996, 1997, 1998 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
27 static bfd_reloc_status_type sh_elf_reloc
28   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
29 static bfd_reloc_status_type sh_elf_ignore_reloc
30   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
31 static reloc_howto_type *sh_elf_reloc_type_lookup
32   PARAMS ((bfd *, bfd_reloc_code_real_type));
33 static void sh_elf_info_to_howto
34   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
35 static boolean sh_elf_relax_section
36   PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
37 static boolean sh_elf_relax_delete_bytes
38   PARAMS ((bfd *, asection *, bfd_vma, int));
39 static boolean sh_elf_align_loads
40   PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, boolean *));
41 static boolean sh_elf_swap_insns
42   PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma));
43 static boolean sh_elf_relocate_section
44   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
45            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
46 static bfd_byte *sh_elf_get_relocated_section_contents
47   PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
48            bfd_byte *, boolean, asymbol **));
49
50 enum sh_reloc_type
51 {
52   R_SH_NONE = 0,
53   R_SH_DIR32,
54   R_SH_REL32,
55   R_SH_DIR8WPN,
56   R_SH_IND12W,
57   R_SH_DIR8WPL,
58   R_SH_DIR8WPZ,
59   R_SH_DIR8BP,
60   R_SH_DIR8W,
61   R_SH_DIR8L,
62   FIRST_INVALID_RELOC,
63   LAST_INVALID_RELOC = 24,
64   /* The remaining relocs are a GNU extension used for relaxation.  We
65      use the same constants as COFF uses, not that it really matters.  */
66   R_SH_SWITCH16 = 25,
67   R_SH_SWITCH32,
68   R_SH_USES,
69   R_SH_COUNT,
70   R_SH_ALIGN,
71   R_SH_CODE,
72   R_SH_DATA,
73   R_SH_LABEL,
74   R_SH_max
75 };
76
77 static reloc_howto_type sh_elf_howto_table[] =
78 {
79   /* No relocation.  */
80   HOWTO (R_SH_NONE,             /* type */
81          0,                     /* rightshift */
82          0,                     /* size (0 = byte, 1 = short, 2 = long) */
83          0,                     /* bitsize */
84          false,                 /* pc_relative */
85          0,                     /* bitpos */
86          complain_overflow_dont, /* complain_on_overflow */
87          sh_elf_reloc,          /* special_function */
88          "R_SH_NONE",           /* name */
89          false,                 /* partial_inplace */
90          0,                     /* src_mask */
91          0,                     /* dst_mask */
92          false),                /* pcrel_offset */
93
94   /* 32 bit absolute relocation.  Setting partial_inplace to true and
95      src_mask to a non-zero value is similar to the COFF toolchain.  */
96   HOWTO (R_SH_DIR32,            /* type */
97          0,                     /* rightshift */
98          2,                     /* size (0 = byte, 1 = short, 2 = long) */
99          32,                    /* bitsize */
100          false,                 /* pc_relative */
101          0,                     /* bitpos */
102          complain_overflow_bitfield, /* complain_on_overflow */
103          sh_elf_reloc,          /* special_function */
104          "R_SH_DIR32",          /* name */
105          true,                  /* partial_inplace */
106          0xffffffff,            /* src_mask */
107          0xffffffff,            /* dst_mask */
108          false),                /* pcrel_offset */
109
110   /* 32 bit PC relative relocation.  */
111   HOWTO (R_SH_REL32,            /* type */
112          0,                     /* rightshift */
113          2,                     /* size (0 = byte, 1 = short, 2 = long) */
114          32,                    /* bitsize */
115          true,                  /* pc_relative */
116          0,                     /* bitpos */
117          complain_overflow_signed, /* complain_on_overflow */
118          sh_elf_reloc,          /* special_function */
119          "R_SH_REL32",          /* name */
120          false,                 /* partial_inplace */
121          0,                     /* src_mask */
122          0xffffffff,            /* dst_mask */
123          true),                 /* pcrel_offset */
124
125   /* 8 bit PC relative branch divided by 2.  */
126   HOWTO (R_SH_DIR8WPN,          /* type */
127          1,                     /* rightshift */
128          1,                     /* size (0 = byte, 1 = short, 2 = long) */
129          8,                     /* bitsize */
130          true,                  /* pc_relative */
131          0,                     /* bitpos */
132          complain_overflow_signed, /* complain_on_overflow */
133          sh_elf_reloc,          /* special_function */
134          "R_SH_DIR8WPN",        /* name */
135          true,                  /* partial_inplace */
136          0xff,                  /* src_mask */
137          0xff,                  /* dst_mask */
138          true),                 /* pcrel_offset */
139
140   /* 12 bit PC relative branch divided by 2.  */
141   HOWTO (R_SH_IND12W,           /* type */
142          1,                     /* rightshift */
143          1,                     /* size (0 = byte, 1 = short, 2 = long) */
144          12,                    /* bitsize */
145          true,                  /* pc_relative */
146          0,                     /* bitpos */
147          complain_overflow_signed, /* complain_on_overflow */
148          sh_elf_reloc,          /* special_function */
149          "R_SH_IND12W",         /* name */
150          true,                  /* partial_inplace */
151          0xfff,                 /* src_mask */
152          0xfff,                 /* dst_mask */
153          true),                 /* pcrel_offset */
154
155   /* 8 bit unsigned PC relative divided by 4.  */
156   HOWTO (R_SH_DIR8WPL,          /* type */
157          2,                     /* rightshift */
158          1,                     /* size (0 = byte, 1 = short, 2 = long) */
159          8,                     /* bitsize */
160          true,                  /* pc_relative */
161          0,                     /* bitpos */
162          complain_overflow_unsigned, /* complain_on_overflow */
163          sh_elf_reloc,          /* special_function */
164          "R_SH_DIR8WPL",        /* name */
165          true,                  /* partial_inplace */
166          0xff,                  /* src_mask */
167          0xff,                  /* dst_mask */
168          true),                 /* pcrel_offset */
169
170   /* 8 bit unsigned PC relative divided by 2.  */
171   HOWTO (R_SH_DIR8WPZ,          /* type */
172          1,                     /* rightshift */
173          1,                     /* size (0 = byte, 1 = short, 2 = long) */
174          8,                     /* bitsize */
175          true,                  /* pc_relative */
176          0,                     /* bitpos */
177          complain_overflow_unsigned, /* complain_on_overflow */
178          sh_elf_reloc,          /* special_function */
179          "R_SH_DIR8WPZ",        /* name */
180          true,                  /* partial_inplace */
181          0xff,                  /* src_mask */
182          0xff,                  /* dst_mask */
183          true),                 /* pcrel_offset */
184
185   /* 8 bit GBR relative.  FIXME: This only makes sense if we have some
186      special symbol for the GBR relative area, and that is not
187      implemented.  */
188   HOWTO (R_SH_DIR8BP,           /* type */
189          0,                     /* rightshift */
190          1,                     /* size (0 = byte, 1 = short, 2 = long) */
191          8,                     /* bitsize */
192          false,                 /* pc_relative */
193          0,                     /* bitpos */
194          complain_overflow_unsigned, /* complain_on_overflow */
195          sh_elf_reloc,          /* special_function */
196          "R_SH_DIR8BP",         /* name */
197          false,                 /* partial_inplace */
198          0,                     /* src_mask */
199          0xff,                  /* dst_mask */
200          true),                 /* pcrel_offset */
201
202   /* 8 bit GBR relative divided by 2.  FIXME: This only makes sense if
203      we have some special symbol for the GBR relative area, and that
204      is not implemented.  */
205   HOWTO (R_SH_DIR8W,            /* type */
206          1,                     /* rightshift */
207          1,                     /* size (0 = byte, 1 = short, 2 = long) */
208          8,                     /* bitsize */
209          false,                 /* pc_relative */
210          0,                     /* bitpos */
211          complain_overflow_unsigned, /* complain_on_overflow */
212          sh_elf_reloc,          /* special_function */
213          "R_SH_DIR8W",          /* name */
214          false,                 /* partial_inplace */
215          0,                     /* src_mask */
216          0xff,                  /* dst_mask */
217          true),                 /* pcrel_offset */
218
219   /* 8 bit GBR relative divided by 4.  FIXME: This only makes sense if
220      we have some special symbol for the GBR relative area, and that
221      is not implemented.  */
222   HOWTO (R_SH_DIR8L,            /* type */
223          2,                     /* rightshift */
224          1,                     /* size (0 = byte, 1 = short, 2 = long) */
225          8,                     /* bitsize */
226          false,                 /* pc_relative */
227          0,                     /* bitpos */
228          complain_overflow_unsigned, /* complain_on_overflow */
229          sh_elf_reloc,          /* special_function */
230          "R_SH_DIR8L",          /* name */
231          false,                 /* partial_inplace */
232          0,                     /* src_mask */
233          0xff,                  /* dst_mask */
234          true),                 /* pcrel_offset */
235
236   { 10 },
237   { 11 },
238   { 12 },
239   { 13 },
240   { 14 },
241   { 15 },
242   { 16 },
243   { 17 },
244   { 18 },
245   { 19 },
246   { 20 },
247   { 21 },
248   { 22 },
249   { 23 },
250   { 24 },
251
252   /* The remaining relocs are a GNU extension used for relaxing.  The
253      final pass of the linker never needs to do anything with any of
254      these relocs.  Any required operations are handled by the
255      relaxation code.  */
256
257   /* A 16 bit switch table entry.  This is generated for an expression
258      such as ``.word L1 - L2''.  The offset holds the difference
259      between the reloc address and L2.  */
260   HOWTO (R_SH_SWITCH16,         /* type */
261          0,                     /* rightshift */
262          1,                     /* size (0 = byte, 1 = short, 2 = long) */
263          16,                    /* bitsize */
264          false,                 /* pc_relative */
265          0,                     /* bitpos */
266          complain_overflow_unsigned, /* complain_on_overflow */
267          sh_elf_ignore_reloc,   /* special_function */
268          "R_SH_SWITCH16",       /* name */
269          false,                 /* partial_inplace */
270          0,                     /* src_mask */
271          0,                     /* dst_mask */
272          true),                 /* pcrel_offset */
273
274   /* A 32 bit switch table entry.  This is generated for an expression
275      such as ``.long L1 - L2''.  The offset holds the difference
276      between the reloc address and L2.  */
277   HOWTO (R_SH_SWITCH32,         /* type */
278          0,                     /* rightshift */
279          2,                     /* size (0 = byte, 1 = short, 2 = long) */
280          32,                    /* bitsize */
281          false,                 /* pc_relative */
282          0,                     /* bitpos */
283          complain_overflow_unsigned, /* complain_on_overflow */
284          sh_elf_ignore_reloc,   /* special_function */
285          "R_SH_SWITCH32",       /* name */
286          false,                 /* partial_inplace */
287          0,                     /* src_mask */
288          0,                     /* dst_mask */
289          true),                 /* pcrel_offset */
290
291   /* Indicates a .uses pseudo-op.  The compiler will generate .uses
292      pseudo-ops when it finds a function call which can be relaxed.
293      The offset field holds the PC relative offset to the instruction
294      which loads the register used in the function call.  */
295   HOWTO (R_SH_USES,             /* type */
296          0,                     /* rightshift */
297          1,                     /* size (0 = byte, 1 = short, 2 = long) */
298          0,                     /* bitsize */
299          false,                 /* pc_relative */
300          0,                     /* bitpos */
301          complain_overflow_unsigned, /* complain_on_overflow */
302          sh_elf_ignore_reloc,   /* special_function */
303          "R_SH_USES",           /* name */
304          false,                 /* partial_inplace */
305          0,                     /* src_mask */
306          0,                     /* dst_mask */
307          true),                 /* pcrel_offset */
308
309   /* The assembler will generate this reloc for addresses referred to
310      by the register loads associated with USES relocs.  The offset
311      field holds the number of times the address is referenced in the
312      object file.  */
313   HOWTO (R_SH_COUNT,            /* type */
314          0,                     /* rightshift */
315          1,                     /* size (0 = byte, 1 = short, 2 = long) */
316          0,                     /* bitsize */
317          false,                 /* pc_relative */
318          0,                     /* bitpos */
319          complain_overflow_unsigned, /* complain_on_overflow */
320          sh_elf_ignore_reloc,   /* special_function */
321          "R_SH_COUNT",          /* name */
322          false,                 /* partial_inplace */
323          0,                     /* src_mask */
324          0,                     /* dst_mask */
325          true),                 /* pcrel_offset */
326
327   /* Indicates an alignment statement.  The offset field is the power
328      of 2 to which subsequent portions of the object file must be
329      aligned.  */
330   HOWTO (R_SH_ALIGN,            /* type */
331          0,                     /* rightshift */
332          1,                     /* size (0 = byte, 1 = short, 2 = long) */
333          0,                     /* bitsize */
334          false,                 /* pc_relative */
335          0,                     /* bitpos */
336          complain_overflow_unsigned, /* complain_on_overflow */
337          sh_elf_ignore_reloc,   /* special_function */
338          "R_SH_ALIGN",  /* name */
339          false,                 /* partial_inplace */
340          0,                     /* src_mask */
341          0,                     /* dst_mask */
342          true),                 /* pcrel_offset */
343
344   /* The assembler will generate this reloc before a block of
345      instructions.  A section should be processed as assumining it
346      contains data, unless this reloc is seen.  */
347   HOWTO (R_SH_CODE,             /* type */
348          0,                     /* rightshift */
349          1,                     /* size (0 = byte, 1 = short, 2 = long) */
350          0,                     /* bitsize */
351          false,                 /* pc_relative */
352          0,                     /* bitpos */
353          complain_overflow_unsigned, /* complain_on_overflow */
354          sh_elf_ignore_reloc,   /* special_function */
355          "R_SH_CODE",           /* name */
356          false,                 /* partial_inplace */
357          0,                     /* src_mask */
358          0,                     /* dst_mask */
359          true),                 /* pcrel_offset */
360
361   /* The assembler will generate this reloc after a block of
362      instructions when it sees data that is not instructions.  */
363   HOWTO (R_SH_DATA,             /* type */
364          0,                     /* rightshift */
365          1,                     /* size (0 = byte, 1 = short, 2 = long) */
366          0,                     /* bitsize */
367          false,                 /* pc_relative */
368          0,                     /* bitpos */
369          complain_overflow_unsigned, /* complain_on_overflow */
370          sh_elf_ignore_reloc,   /* special_function */
371          "R_SH_DATA",           /* name */
372          false,                 /* partial_inplace */
373          0,                     /* src_mask */
374          0,                     /* dst_mask */
375          true),                 /* pcrel_offset */
376
377   /* The assembler generates this reloc for each label within a block
378      of instructions.  This permits the linker to avoid swapping
379      instructions which are the targets of branches.  */
380   HOWTO (R_SH_LABEL,            /* type */
381          0,                     /* rightshift */
382          1,                     /* size (0 = byte, 1 = short, 2 = long) */
383          0,                     /* bitsize */
384          false,                 /* pc_relative */
385          0,                     /* bitpos */
386          complain_overflow_unsigned, /* complain_on_overflow */
387          sh_elf_ignore_reloc,   /* special_function */
388          "R_SH_LABEL",          /* name */
389          false,                 /* partial_inplace */
390          0,                     /* src_mask */
391          0,                     /* dst_mask */
392          true)                  /* pcrel_offset */
393 };
394
395 /* This function is used for normal relocs.  This is like the COFF
396    function, and is almost certainly incorrect for other ELF targets.  */
397
398 static bfd_reloc_status_type
399 sh_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
400           error_message)
401      bfd *abfd;
402      arelent *reloc_entry;
403      asymbol *symbol_in;
404      PTR data;
405      asection *input_section;
406      bfd *output_bfd;
407      char **error_message;
408 {
409   unsigned long insn;
410   bfd_vma sym_value;
411   enum sh_reloc_type r_type;
412   bfd_vma addr = reloc_entry->address;
413   bfd_byte *hit_data = addr + (bfd_byte *) data;
414
415   r_type = (enum sh_reloc_type) reloc_entry->howto->type;
416
417   if (output_bfd != NULL)
418     {
419       /* Partial linking--do nothing.  */
420       reloc_entry->address += input_section->output_offset;
421       return bfd_reloc_ok;
422     }
423
424   /* Almost all relocs have to do with relaxing.  If any work must be
425      done for them, it has been done in sh_relax_section.  */
426   if (r_type != R_SH_DIR32
427       && (r_type != R_SH_IND12W
428           || (symbol_in->flags & BSF_LOCAL) != 0))
429     return bfd_reloc_ok;
430
431   if (symbol_in != NULL
432       && bfd_is_und_section (symbol_in->section))
433     return bfd_reloc_undefined;
434
435   if (bfd_is_com_section (symbol_in->section))
436     sym_value = 0;                           
437   else 
438     sym_value = (symbol_in->value +
439                  symbol_in->section->output_section->vma +
440                  symbol_in->section->output_offset);
441
442   switch (r_type)
443     {
444     case R_SH_DIR32:
445       insn = bfd_get_32 (abfd, hit_data);
446       insn += sym_value + reloc_entry->addend;
447       bfd_put_32 (abfd, insn, hit_data);
448       break;
449     case R_SH_IND12W:
450       insn = bfd_get_16 (abfd, hit_data);
451       sym_value += reloc_entry->addend;
452       sym_value -= (input_section->output_section->vma
453                     + input_section->output_offset
454                     + addr
455                     + 4);
456       sym_value += (insn & 0xfff) << 1;
457       if (insn & 0x800)
458         sym_value -= 0x1000;
459       insn = (insn & 0xf000) | (sym_value & 0xfff);
460       bfd_put_16 (abfd, insn, hit_data);
461       if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
462         return bfd_reloc_overflow;
463       break;
464     default:
465       abort ();
466       break;
467     }
468
469   return bfd_reloc_ok;
470 }
471
472 /* This function is used for relocs which are only used for relaxing,
473    which the linker should otherwise ignore.  */
474
475 static bfd_reloc_status_type
476 sh_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
477                      output_bfd, error_message)
478      bfd *abfd;
479      arelent *reloc_entry;
480      asymbol *symbol;
481      PTR data;
482      asection *input_section;
483      bfd *output_bfd;
484      char **error_message;
485 {
486   if (output_bfd != NULL)
487     reloc_entry->address += input_section->output_offset;
488   return bfd_reloc_ok;
489 }
490
491 /* This structure is used to map BFD reloc codes to SH ELF relocs.  */
492
493 struct elf_reloc_map
494 {
495   unsigned char bfd_reloc_val;
496   unsigned char elf_reloc_val;
497 };
498
499 /* An array mapping BFD reloc codes to SH ELF relocs.  */
500
501 static const struct elf_reloc_map sh_reloc_map[] =
502 {
503   { BFD_RELOC_NONE, R_SH_NONE },
504   { BFD_RELOC_32, R_SH_DIR32 },
505   { BFD_RELOC_CTOR, R_SH_DIR32 },
506   { BFD_RELOC_32_PCREL, R_SH_REL32 },
507   { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
508   { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
509   { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
510   { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
511   { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
512   { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
513   { BFD_RELOC_SH_USES, R_SH_USES },
514   { BFD_RELOC_SH_COUNT, R_SH_COUNT },
515   { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
516   { BFD_RELOC_SH_CODE, R_SH_CODE },
517   { BFD_RELOC_SH_DATA, R_SH_DATA },
518   { BFD_RELOC_SH_LABEL, R_SH_LABEL }
519 };
520
521 /* Given a BFD reloc code, return the howto structure for the
522    corresponding SH ELf reloc.  */
523
524 static reloc_howto_type *
525 sh_elf_reloc_type_lookup (abfd, code)
526      bfd *abfd;
527      bfd_reloc_code_real_type code;
528 {
529   unsigned int i;
530
531   for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
532     {
533       if (sh_reloc_map[i].bfd_reloc_val == code)
534         return &sh_elf_howto_table[(int) sh_reloc_map[i].elf_reloc_val];
535     }
536
537   return NULL;
538 }
539
540 /* Given an ELF reloc, fill in the howto field of a relent.  */
541
542 static void
543 sh_elf_info_to_howto (abfd, cache_ptr, dst)
544      bfd *abfd;
545      arelent *cache_ptr;
546      Elf_Internal_Rela *dst;
547 {
548   unsigned int r;
549
550   r = ELF32_R_TYPE (dst->r_info);
551
552   BFD_ASSERT (r < (unsigned int) R_SH_max);
553   BFD_ASSERT (r < FIRST_INVALID_RELOC || r > LAST_INVALID_RELOC);
554
555   cache_ptr->howto = &sh_elf_howto_table[r];
556 }
557 \f
558 /* This function handles relaxing for SH ELF.  See the corresponding
559    function in coff-sh.c for a description of what this does.  FIXME:
560    There is a lot of duplication here between this code and the COFF
561    specific code.  The format of relocs and symbols is wound deeply
562    into this code, but it would still be better if the duplication
563    could be eliminated somehow.  Note in particular that although both
564    functions use symbols like R_SH_CODE, those symbols have different
565    values; in coff-sh.c they come from include/coff/sh.h, whereas here
566    they come from enum sh_reloc_type in this file.  */
567
568 static boolean 
569 sh_elf_relax_section (abfd, sec, link_info, again)
570      bfd *abfd;
571      asection *sec;
572      struct bfd_link_info *link_info;
573      boolean *again;
574 {
575   Elf_Internal_Shdr *symtab_hdr;
576   Elf_Internal_Rela *internal_relocs;
577   Elf_Internal_Rela *free_relocs = NULL;
578   boolean have_code;
579   Elf_Internal_Rela *irel, *irelend;
580   bfd_byte *contents = NULL;
581   bfd_byte *free_contents = NULL;
582   Elf32_External_Sym *extsyms = NULL;
583   Elf32_External_Sym *free_extsyms = NULL;
584
585   *again = false;
586
587   if (link_info->relocateable
588       || (sec->flags & SEC_RELOC) == 0
589       || sec->reloc_count == 0)
590     return true;
591
592   /* If this is the first time we have been called for this section,
593      initialize the cooked size.  */
594   if (sec->_cooked_size == 0)
595     sec->_cooked_size = sec->_raw_size;
596
597   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
598
599   internal_relocs = (_bfd_elf32_link_read_relocs
600                      (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
601                       link_info->keep_memory));
602   if (internal_relocs == NULL)
603     goto error_return;
604   if (! link_info->keep_memory)
605     free_relocs = internal_relocs;
606
607   have_code = false;
608
609   irelend = internal_relocs + sec->reloc_count;
610   for (irel = internal_relocs; irel < irelend; irel++)
611     {
612       bfd_vma laddr, paddr, symval;
613       unsigned short insn;
614       Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
615       bfd_signed_vma foff;
616
617       if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
618         have_code = true;
619
620       if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
621         continue;
622
623       /* Get the section contents.  */
624       if (contents == NULL)
625         {
626           if (elf_section_data (sec)->this_hdr.contents != NULL)
627             contents = elf_section_data (sec)->this_hdr.contents;
628           else
629             {
630               contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
631               if (contents == NULL)
632                 goto error_return;
633               free_contents = contents;
634
635               if (! bfd_get_section_contents (abfd, sec, contents,
636                                               (file_ptr) 0, sec->_raw_size))
637                 goto error_return;
638             }
639         }
640
641       /* The r_addend field of the R_SH_USES reloc will point us to
642          the register load.  The 4 is because the r_addend field is
643          computed as though it were a jump offset, which are based
644          from 4 bytes after the jump instruction.  */
645       laddr = irel->r_offset + 4 + irel->r_addend;
646       if (laddr >= sec->_raw_size)
647         {
648           (*_bfd_error_handler) ("%s: 0x%lx: warning: bad R_SH_USES offset",
649                                  bfd_get_filename (abfd),
650                                  (unsigned long) irel->r_offset);
651           continue;
652         }
653       insn = bfd_get_16 (abfd, contents + laddr);
654
655       /* If the instruction is not mov.l NN,rN, we don't know what to
656          do.  */
657       if ((insn & 0xf000) != 0xd000)
658         {
659           ((*_bfd_error_handler)
660            ("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x",
661             bfd_get_filename (abfd), (unsigned long) irel->r_offset, insn));
662           continue;
663         }
664
665       /* Get the address from which the register is being loaded.  The
666          displacement in the mov.l instruction is quadrupled.  It is a
667          displacement from four bytes after the movl instruction, but,
668          before adding in the PC address, two least significant bits
669          of the PC are cleared.  We assume that the section is aligned
670          on a four byte boundary.  */
671       paddr = insn & 0xff;
672       paddr *= 4;
673       paddr += (laddr + 4) &~ 3;
674       if (paddr >= sec->_raw_size)
675         {
676           ((*_bfd_error_handler)
677            ("%s: 0x%lx: warning: bad R_SH_USES load offset",
678             bfd_get_filename (abfd), (unsigned long) irel->r_offset));
679           continue;
680         }
681
682       /* Get the reloc for the address from which the register is
683          being loaded.  This reloc will tell us which function is
684          actually being called.  */
685       for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
686         if (irelfn->r_offset == paddr
687             && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
688           break;
689       if (irelfn >= irelend)
690         {
691           ((*_bfd_error_handler)
692            ("%s: 0x%lx: warning: could not find expected reloc",
693             bfd_get_filename (abfd), (unsigned long) paddr));
694           continue;
695         }
696
697       /* Read this BFD's symbols if we haven't done so already.  */
698       if (extsyms == NULL)
699         {
700           if (symtab_hdr->contents != NULL)
701             extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
702           else
703             {
704               extsyms = ((Elf32_External_Sym *)
705                          bfd_malloc (symtab_hdr->sh_size));
706               if (extsyms == NULL)
707                 goto error_return;
708               free_extsyms = extsyms;
709               if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
710                   || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
711                       != symtab_hdr->sh_size))
712                 goto error_return;
713             }
714         }
715
716       /* Get the value of the symbol referred to by the reloc.  */
717       if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
718         {
719           Elf_Internal_Sym isym;
720
721           /* A local symbol.  */
722           bfd_elf32_swap_symbol_in (abfd,
723                                     extsyms + ELF32_R_SYM (irelfn->r_info),
724                                     &isym);
725
726           if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec))
727             {
728               ((*_bfd_error_handler)
729                ("%s: 0x%lx: warning: symbol in unexpected section",
730                 bfd_get_filename (abfd), (unsigned long) paddr));
731               continue;
732             }
733
734           symval = (isym.st_value
735                     + sec->output_section->vma
736                     + sec->output_offset);
737         }
738       else
739         {
740           unsigned long indx;
741           struct elf_link_hash_entry *h;
742
743           indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
744           h = elf_sym_hashes (abfd)[indx];
745           BFD_ASSERT (h != NULL);
746           if (h->root.type != bfd_link_hash_defined
747               && h->root.type != bfd_link_hash_defweak)
748             {
749               /* This appears to be a reference to an undefined
750                  symbol.  Just ignore it--it will be caught by the
751                  regular reloc processing.  */
752               continue;
753             }
754
755           symval = (h->root.u.def.value
756                     + h->root.u.def.section->output_section->vma
757                     + h->root.u.def.section->output_offset);
758         }
759
760       symval += bfd_get_32 (abfd, contents + paddr);
761
762       /* See if this function call can be shortened.  */
763       foff = (symval
764               - (irel->r_offset
765                  + sec->output_section->vma
766                  + sec->output_offset
767                  + 4));
768       if (foff < -0x1000 || foff >= 0x1000)
769         {
770           /* After all that work, we can't shorten this function call.  */
771           continue;
772         }
773
774       /* Shorten the function call.  */
775
776       /* For simplicity of coding, we are going to modify the section
777          contents, the section relocs, and the BFD symbol table.  We
778          must tell the rest of the code not to free up this
779          information.  It would be possible to instead create a table
780          of changes which have to be made, as is done in coff-mips.c;
781          that would be more work, but would require less memory when
782          the linker is run.  */
783
784       elf_section_data (sec)->relocs = internal_relocs;
785       free_relocs = NULL;
786
787       elf_section_data (sec)->this_hdr.contents = contents;
788       free_contents = NULL;
789
790       symtab_hdr->contents = (bfd_byte *) extsyms;
791       free_extsyms = NULL;
792
793       /* Replace the jsr with a bsr.  */
794
795       /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
796          replace the jsr with a bsr.  */
797       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
798       if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
799         {
800           /* If this needs to be changed because of future relaxing,
801              it will be handled here like other internal IND12W
802              relocs.  */
803           bfd_put_16 (abfd,
804                       0xb000 | ((foff >> 1) & 0xfff),
805                       contents + irel->r_offset);
806         }
807       else
808         {
809           /* We can't fully resolve this yet, because the external
810              symbol value may be changed by future relaxing.  We let
811              the final link phase handle it.  */
812           bfd_put_16 (abfd, 0xb000, contents + irel->r_offset);
813         }
814
815       /* See if there is another R_SH_USES reloc referring to the same
816          register load.  */
817       for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
818         if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
819             && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
820           break;
821       if (irelscan < irelend)
822         {
823           /* Some other function call depends upon this register load,
824              and we have not yet converted that function call.
825              Indeed, we may never be able to convert it.  There is
826              nothing else we can do at this point.  */
827           continue;
828         }
829
830       /* Look for a R_SH_COUNT reloc on the location where the
831          function address is stored.  Do this before deleting any
832          bytes, to avoid confusion about the address.  */
833       for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
834         if (irelcount->r_offset == paddr
835             && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
836           break;
837
838       /* Delete the register load.  */
839       if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
840         goto error_return;
841
842       /* That will change things, so, just in case it permits some
843          other function call to come within range, we should relax
844          again.  Note that this is not required, and it may be slow.  */
845       *again = true;
846
847       /* Now check whether we got a COUNT reloc.  */
848       if (irelcount >= irelend)
849         {
850           ((*_bfd_error_handler)
851            ("%s: 0x%lx: warning: could not find expected COUNT reloc",
852             bfd_get_filename (abfd), (unsigned long) paddr));
853           continue;
854         }
855
856       /* The number of uses is stored in the r_addend field.  We've
857          just deleted one.  */
858       if (irelcount->r_addend == 0)
859         {
860           ((*_bfd_error_handler) ("%s: 0x%lx: warning: bad count",
861                                   bfd_get_filename (abfd),
862                                   (unsigned long) paddr));
863           continue;
864         }
865
866       --irelcount->r_addend;
867
868       /* If there are no more uses, we can delete the address.  Reload
869          the address from irelfn, in case it was changed by the
870          previous call to sh_elf_relax_delete_bytes.  */
871       if (irelcount->r_addend == 0)
872         {
873           if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
874             goto error_return;
875         }
876
877       /* We've done all we can with that function call.  */
878     }
879
880   /* Look for load and store instructions that we can align on four
881      byte boundaries.  */
882   if (have_code)
883     {
884       boolean swapped;
885
886       /* Get the section contents.  */
887       if (contents == NULL)
888         {
889           if (elf_section_data (sec)->this_hdr.contents != NULL)
890             contents = elf_section_data (sec)->this_hdr.contents;
891           else
892             {
893               contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
894               if (contents == NULL)
895                 goto error_return;
896               free_contents = contents;
897
898               if (! bfd_get_section_contents (abfd, sec, contents,
899                                               (file_ptr) 0, sec->_raw_size))
900                 goto error_return;
901             }
902         }
903
904       if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
905                                 &swapped))
906         goto error_return;
907
908       if (swapped)
909         {
910           elf_section_data (sec)->relocs = internal_relocs;
911           free_relocs = NULL;
912
913           elf_section_data (sec)->this_hdr.contents = contents;
914           free_contents = NULL;
915
916           symtab_hdr->contents = (bfd_byte *) extsyms;
917           free_extsyms = NULL;
918         }
919     }
920
921   if (free_relocs != NULL)
922     {
923       free (free_relocs);
924       free_relocs = NULL;
925     }
926
927   if (free_contents != NULL)
928     {
929       if (! link_info->keep_memory)
930         free (free_contents);
931       else
932         {
933           /* Cache the section contents for elf_link_input_bfd.  */
934           elf_section_data (sec)->this_hdr.contents = contents;
935         }
936       free_contents = NULL;
937     }
938
939   if (free_extsyms != NULL)
940     {
941       if (! link_info->keep_memory)
942         free (free_extsyms);
943       else
944         {
945           /* Cache the symbols for elf_link_input_bfd.  */
946           symtab_hdr->contents = extsyms;
947         }
948       free_extsyms = NULL;
949     }
950
951   return true;
952
953  error_return:
954   if (free_relocs != NULL)
955     free (free_relocs);
956   if (free_contents != NULL)
957     free (free_contents);
958   if (free_extsyms != NULL)
959     free (free_extsyms);
960   return false;
961 }
962
963 /* Delete some bytes from a section while relaxing.  FIXME: There is a
964    lot of duplication between this function and sh_relax_delete_bytes
965    in coff-sh.c.  */
966
967 static boolean
968 sh_elf_relax_delete_bytes (abfd, sec, addr, count)
969      bfd *abfd;
970      asection *sec;
971      bfd_vma addr;
972      int count;
973 {
974   Elf_Internal_Shdr *symtab_hdr;
975   Elf32_External_Sym *extsyms;
976   int shndx, index;
977   bfd_byte *contents;
978   Elf_Internal_Rela *irel, *irelend;
979   Elf_Internal_Rela *irelalign;
980   bfd_vma toaddr;
981   Elf32_External_Sym *esym, *esymend;
982   struct elf_link_hash_entry *sym_hash;
983   asection *o;
984
985   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
986   extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
987
988   shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
989
990   contents = elf_section_data (sec)->this_hdr.contents;
991
992   /* The deletion must stop at the next ALIGN reloc for an aligment
993      power larger than the number of bytes we are deleting.  */
994
995   irelalign = NULL;
996   toaddr = sec->_cooked_size;
997
998   irel = elf_section_data (sec)->relocs;
999   irelend = irel + sec->reloc_count;
1000   for (; irel < irelend; irel++)
1001     {
1002       if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1003           && irel->r_offset > addr
1004           && count < (1 << irel->r_addend))
1005         {
1006           irelalign = irel;
1007           toaddr = irel->r_offset;
1008           break;
1009         }
1010     }
1011
1012   /* Actually delete the bytes.  */
1013   memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1014   if (irelalign == NULL)
1015     sec->_cooked_size -= count;
1016   else
1017     {
1018       int i;
1019
1020 #define NOP_OPCODE (0x0009)
1021
1022       BFD_ASSERT ((count & 1) == 0);
1023       for (i = 0; i < count; i += 2)
1024         bfd_put_16 (abfd, NOP_OPCODE, contents + toaddr - count + i);
1025     }
1026
1027   /* Adjust all the relocs.  */
1028   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1029     {
1030       bfd_vma nraddr, stop;
1031       bfd_vma start = 0;
1032       int insn = 0;
1033       Elf_Internal_Sym sym;
1034       int off, adjust, oinsn;
1035       bfd_signed_vma voff = 0;
1036       boolean overflow;
1037
1038       /* Get the new reloc address.  */
1039       nraddr = irel->r_offset;
1040       if ((irel->r_offset > addr
1041            && irel->r_offset < toaddr)
1042           || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
1043               && irel->r_offset == toaddr))
1044         nraddr -= count;
1045
1046       /* See if this reloc was for the bytes we have deleted, in which
1047          case we no longer care about it.  Don't delete relocs which
1048          represent addresses, though.  */
1049       if (irel->r_offset >= addr
1050           && irel->r_offset < addr + count
1051           && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
1052           && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
1053           && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
1054           && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
1055         irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1056                                      (int) R_SH_NONE);
1057
1058       /* If this is a PC relative reloc, see if the range it covers
1059          includes the bytes we have deleted.  */
1060       switch ((enum sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1061         {
1062         default:
1063           break;
1064
1065         case R_SH_DIR8WPN:
1066         case R_SH_IND12W:
1067         case R_SH_DIR8WPZ:
1068         case R_SH_DIR8WPL:
1069           start = irel->r_offset;
1070           insn = bfd_get_16 (abfd, contents + nraddr);
1071           break;
1072         }
1073
1074       switch ((enum sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1075         {
1076         default:
1077           start = stop = addr;
1078           break;
1079
1080         case R_SH_DIR32:
1081           /* If this reloc is against a symbol defined in this
1082              section, and the symbol will not be adjusted below, we
1083              must check the addend to see it will put the value in
1084              range to be adjusted, and hence must be changed.  */
1085           if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1086             {
1087               bfd_elf32_swap_symbol_in (abfd,
1088                                         extsyms + ELF32_R_SYM (irel->r_info),
1089                                         &sym);
1090               if (sym.st_shndx == shndx
1091                   && (sym.st_value <= addr
1092                       || sym.st_value >= toaddr))
1093                 {
1094                   bfd_vma val;
1095
1096                   val = bfd_get_32 (abfd, contents + nraddr);
1097                   val += sym.st_value;
1098                   if (val >= addr && val < toaddr)
1099                     bfd_put_32 (abfd, val - count, contents + nraddr);
1100                 }
1101             }
1102           start = stop = addr;
1103           break;
1104
1105         case R_SH_DIR8WPN:
1106           off = insn & 0xff;
1107           if (off & 0x80)
1108             off -= 0x100;
1109           stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1110           break;
1111
1112         case R_SH_IND12W:
1113           if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
1114             start = stop = addr;
1115           else
1116             {
1117               off = insn & 0xfff;
1118               if (off & 0x800)
1119                 off -= 0x1000;
1120               stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1121             }
1122           break;
1123
1124         case R_SH_DIR8WPZ:
1125           off = insn & 0xff;
1126           stop = start + 4 + off * 2;
1127           break;
1128
1129         case R_SH_DIR8WPL:
1130           off = insn & 0xff;
1131           stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
1132           break;
1133
1134         case R_SH_SWITCH16:
1135         case R_SH_SWITCH32:
1136           /* These relocs types represent
1137                .word L2-L1
1138              The r_offset field holds the difference between the reloc
1139              address and L1.  That is the start of the reloc, and
1140              adding in the contents gives us the top.  We must adjust
1141              both the r_offset field and the section contents.  */
1142
1143           start = irel->r_offset;
1144           stop = (bfd_vma) ((bfd_signed_vma) start - (long) irel->r_addend);
1145
1146           if (start > addr
1147               && start < toaddr
1148               && (stop <= addr || stop >= toaddr))
1149             irel->r_addend += count;
1150           else if (stop > addr
1151                    && stop < toaddr
1152                    && (start <= addr || start >= toaddr))
1153             irel->r_addend -= count;
1154
1155           start = stop;
1156
1157           if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1158             voff = bfd_get_signed_16 (abfd, contents + nraddr);
1159           else
1160             voff = bfd_get_signed_32 (abfd, contents + nraddr);
1161           stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1162
1163           break;
1164
1165         case R_SH_USES:
1166           start = irel->r_offset;
1167           stop = (bfd_vma) ((bfd_signed_vma) start
1168                             + (long) irel->r_addend
1169                             + 4);
1170           break;
1171         }
1172
1173       if (start > addr
1174           && start < toaddr
1175           && (stop <= addr || stop >= toaddr))
1176         adjust = count;
1177       else if (stop > addr
1178                && stop < toaddr
1179                && (start <= addr || start >= toaddr))
1180         adjust = - count;
1181       else
1182         adjust = 0;
1183
1184       if (adjust != 0)
1185         {
1186           oinsn = insn;
1187           overflow = false;
1188           switch ((enum sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1189             {
1190             default:
1191               abort ();
1192               break;
1193
1194             case R_SH_DIR8WPN:
1195             case R_SH_DIR8WPZ:
1196               insn += adjust / 2;
1197               if ((oinsn & 0xff00) != (insn & 0xff00))
1198                 overflow = true;
1199               bfd_put_16 (abfd, insn, contents + nraddr);
1200               break;
1201
1202             case R_SH_IND12W:
1203               insn += adjust / 2;
1204               if ((oinsn & 0xf000) != (insn & 0xf000))
1205                 overflow = true;
1206               bfd_put_16 (abfd, insn, contents + nraddr);
1207               break;
1208
1209             case R_SH_DIR8WPL:
1210               BFD_ASSERT (adjust == count || count >= 4);
1211               if (count >= 4)
1212                 insn += adjust / 4;
1213               else
1214                 {
1215                   if ((irel->r_offset & 3) == 0)
1216                     ++insn;
1217                 }
1218               if ((oinsn & 0xff00) != (insn & 0xff00))
1219                 overflow = true;
1220               bfd_put_16 (abfd, insn, contents + nraddr);
1221               break;
1222
1223             case R_SH_SWITCH16:
1224               voff += adjust;
1225               if (voff < - 0x8000 || voff >= 0x8000)
1226                 overflow = true;
1227               bfd_put_signed_16 (abfd, voff, contents + nraddr);
1228               break;
1229
1230             case R_SH_SWITCH32:
1231               voff += adjust;
1232               bfd_put_signed_32 (abfd, voff, contents + nraddr);
1233               break;
1234
1235             case R_SH_USES:
1236               irel->r_addend += adjust;
1237               break;
1238             }
1239
1240           if (overflow)
1241             {
1242               ((*_bfd_error_handler)
1243                ("%s: 0x%lx: fatal: reloc overflow while relaxing",
1244                 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1245               bfd_set_error (bfd_error_bad_value);
1246               return false;
1247             }
1248         }
1249
1250       irel->r_offset = nraddr;
1251     }
1252
1253   /* Look through all the other sections.  If there contain any IMM32
1254      relocs against internal symbols which we are not going to adjust
1255      below, we may need to adjust the addends.  */
1256   for (o = abfd->sections; o != NULL; o = o->next)
1257     {
1258       Elf_Internal_Rela *internal_relocs;
1259       Elf_Internal_Rela *irelscan, *irelscanend;
1260       bfd_byte *ocontents;
1261
1262       if (o == sec
1263           || (o->flags & SEC_RELOC) == 0
1264           || o->reloc_count == 0)
1265         continue;
1266
1267       /* We always cache the relocs.  Perhaps, if info->keep_memory is
1268          false, we should free them, if we are permitted to, when we
1269          leave sh_coff_relax_section.  */
1270       internal_relocs = (_bfd_elf32_link_read_relocs
1271                          (abfd, o, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1272                           true));
1273       if (internal_relocs == NULL)
1274         return false;
1275
1276       ocontents = NULL;
1277       irelscanend = internal_relocs + o->reloc_count;
1278       for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1279         {
1280           Elf_Internal_Sym sym;
1281
1282           if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1283             continue;
1284
1285           if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1286             continue;
1287
1288           bfd_elf32_swap_symbol_in (abfd,
1289                                     extsyms + ELF32_R_SYM (irelscan->r_info),
1290                                     &sym);
1291
1292           if (sym.st_shndx == shndx
1293               && (sym.st_value <= addr
1294                   || sym.st_value >= toaddr))
1295             {
1296               bfd_vma val;
1297
1298               if (ocontents == NULL)
1299                 {
1300                   if (elf_section_data (o)->this_hdr.contents != NULL)
1301                     ocontents = elf_section_data (o)->this_hdr.contents;
1302                   else
1303                     {
1304                       /* We always cache the section contents.
1305                          Perhaps, if info->keep_memory is false, we
1306                          should free them, if we are permitted to,
1307                          when we leave sh_coff_relax_section.  */
1308                       ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1309                       if (ocontents == NULL)
1310                         return false;
1311                       if (! bfd_get_section_contents (abfd, o, ocontents,
1312                                                       (file_ptr) 0,
1313                                                       o->_raw_size))
1314                         return false;
1315                       elf_section_data (o)->this_hdr.contents = ocontents;
1316                     }
1317                 }
1318
1319               val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1320               val += sym.st_value;
1321               if (val >= addr && val < toaddr)
1322                 bfd_put_32 (abfd, val - count,
1323                             ocontents + irelscan->r_offset);
1324             }
1325         }
1326     }
1327
1328   /* Adjust the local symbols defined in this section.  */
1329   esym = extsyms;
1330   esymend = esym + symtab_hdr->sh_info;
1331   for (; esym < esymend; esym++)
1332     {
1333       Elf_Internal_Sym isym;
1334
1335       bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1336
1337       if (isym.st_shndx == shndx
1338           && isym.st_value > addr
1339           && isym.st_value < toaddr)
1340         {
1341           isym.st_value -= count;
1342           bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1343         }
1344     }
1345
1346   /* Now adjust the global symbols defined in this section.  */
1347   esym = extsyms + symtab_hdr->sh_info;
1348   esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1349   for (index = 0; esym < esymend; esym++, index++)
1350     {
1351       Elf_Internal_Sym isym;
1352
1353       bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1354       sym_hash = elf_sym_hashes (abfd)[index];
1355       if (isym.st_shndx == shndx
1356           && ((sym_hash)->root.type == bfd_link_hash_defined
1357               || (sym_hash)->root.type == bfd_link_hash_defweak)
1358           && (sym_hash)->root.u.def.section == sec
1359           && (sym_hash)->root.u.def.value > addr
1360           && (sym_hash)->root.u.def.value < toaddr)
1361         {
1362           (sym_hash)->root.u.def.value -= count;
1363         }
1364     }
1365
1366   /* See if we can move the ALIGN reloc forward.  We have adjusted
1367      r_offset for it already.  */
1368   if (irelalign != NULL)
1369     {
1370       bfd_vma alignto, alignaddr;
1371
1372       alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1373       alignaddr = BFD_ALIGN (irelalign->r_offset,
1374                              1 << irelalign->r_addend);
1375       if (alignto != alignaddr)
1376         {
1377           /* Tail recursion.  */
1378           return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1379                                             alignto - alignaddr);
1380         }
1381     }
1382
1383   return true;
1384 }
1385
1386 /* Look for loads and stores which we can align to four byte
1387    boundaries.  This is like sh_align_loads in coff-sh.c.  */
1388
1389 static boolean
1390 sh_elf_align_loads (abfd, sec, internal_relocs, contents, pswapped)
1391      bfd *abfd;
1392      asection *sec;
1393      Elf_Internal_Rela *internal_relocs;
1394      bfd_byte *contents;
1395      boolean *pswapped;
1396 {
1397   Elf_Internal_Rela *irel, *irelend;
1398   bfd_vma *labels = NULL;
1399   bfd_vma *label, *label_end;
1400
1401   *pswapped = false;
1402
1403   irelend = internal_relocs + sec->reloc_count;
1404
1405   /* Get all the addresses with labels on them.  */
1406   labels = (bfd_vma *) bfd_malloc (sec->reloc_count * sizeof (bfd_vma));
1407   if (labels == NULL)
1408     goto error_return;
1409   label_end = labels;
1410   for (irel = internal_relocs; irel < irelend; irel++)
1411     {
1412       if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1413         {
1414           *label_end = irel->r_offset;
1415           ++label_end;
1416         }
1417     }
1418
1419   /* Note that the assembler currently always outputs relocs in
1420      address order.  If that ever changes, this code will need to sort
1421      the label values and the relocs.  */
1422
1423   label = labels;
1424
1425   for (irel = internal_relocs; irel < irelend; irel++)
1426     {
1427       bfd_vma start, stop;
1428
1429       if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1430         continue;
1431
1432       start = irel->r_offset;
1433
1434       for (irel++; irel < irelend; irel++)
1435         if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1436           break;
1437       if (irel < irelend)
1438         stop = irel->r_offset;
1439       else
1440         stop = sec->_cooked_size;
1441
1442       if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1443                                      (PTR) internal_relocs, &label,
1444                                      label_end, start, stop, pswapped))
1445         goto error_return;
1446     }
1447
1448   free (labels);
1449
1450   return true;
1451
1452  error_return:
1453   if (labels != NULL)
1454     free (labels);
1455   return false;
1456 }
1457
1458 /* Swap two SH instructions.  This is like sh_swap_insns in coff-sh.c.  */
1459
1460 static boolean
1461 sh_elf_swap_insns (abfd, sec, relocs, contents, addr)
1462      bfd *abfd;
1463      asection *sec;
1464      PTR relocs;
1465      bfd_byte *contents;
1466      bfd_vma addr;
1467 {
1468   Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1469   unsigned short i1, i2;
1470   Elf_Internal_Rela *irel, *irelend;
1471
1472   /* Swap the instructions themselves.  */
1473   i1 = bfd_get_16 (abfd, contents + addr);
1474   i2 = bfd_get_16 (abfd, contents + addr + 2);
1475   bfd_put_16 (abfd, i2, contents + addr);
1476   bfd_put_16 (abfd, i1, contents + addr + 2);
1477
1478   /* Adjust all reloc addresses.  */
1479   irelend = internal_relocs + sec->reloc_count;
1480   for (irel = internal_relocs; irel < irelend; irel++)
1481     {
1482       enum sh_reloc_type type;
1483       int add;
1484
1485       /* There are a few special types of relocs that we don't want to
1486          adjust.  These relocs do not apply to the instruction itself,
1487          but are only associated with the address.  */
1488       type = (enum sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1489       if (type == R_SH_ALIGN
1490           || type == R_SH_CODE
1491           || type == R_SH_DATA
1492           || type == R_SH_LABEL)
1493         continue;
1494
1495       /* If an R_SH_USES reloc points to one of the addresses being
1496          swapped, we must adjust it.  It would be incorrect to do this
1497          for a jump, though, since we want to execute both
1498          instructions after the jump.  (We have avoided swapping
1499          around a label, so the jump will not wind up executing an
1500          instruction it shouldn't).  */
1501       if (type == R_SH_USES)
1502         {
1503           bfd_vma off;
1504
1505           off = irel->r_offset + 4 + irel->r_addend;
1506           if (off == addr)
1507             irel->r_offset += 2;
1508           else if (off == addr + 2)
1509             irel->r_offset -= 2;
1510         }
1511
1512       if (irel->r_offset == addr)
1513         {
1514           irel->r_offset += 2;
1515           add = -2;
1516         }
1517       else if (irel->r_offset == addr + 2)
1518         {
1519           irel->r_offset -= 2;
1520           add = 2;
1521         }
1522       else
1523         add = 0;
1524
1525       if (add != 0)
1526         {
1527           bfd_byte *loc;
1528           unsigned short insn, oinsn;
1529           boolean overflow;
1530
1531           loc = contents + irel->r_offset;
1532           overflow = false;
1533           switch (type)
1534             {
1535             default:
1536               break;
1537
1538             case R_SH_DIR8WPN:
1539             case R_SH_DIR8WPZ:
1540               insn = bfd_get_16 (abfd, loc);
1541               oinsn = insn;
1542               insn += add / 2;
1543               if ((oinsn & 0xff00) != (insn & 0xff00))
1544                 overflow = true;
1545               bfd_put_16 (abfd, insn, loc);
1546               break;
1547
1548             case R_SH_IND12W:
1549               insn = bfd_get_16 (abfd, loc);
1550               oinsn = insn;
1551               insn += add / 2;
1552               if ((oinsn & 0xf000) != (insn & 0xf000))
1553                 overflow = true;
1554               bfd_put_16 (abfd, insn, loc);
1555               break;
1556
1557             case R_SH_DIR8WPL:
1558               /* This reloc ignores the least significant 3 bits of
1559                  the program counter before adding in the offset.
1560                  This means that if ADDR is at an even address, the
1561                  swap will not affect the offset.  If ADDR is an at an
1562                  odd address, then the instruction will be crossing a
1563                  four byte boundary, and must be adjusted.  */
1564               if ((addr & 3) != 0)
1565                 {
1566                   insn = bfd_get_16 (abfd, loc);
1567                   oinsn = insn;
1568                   insn += add / 2;
1569                   if ((oinsn & 0xff00) != (insn & 0xff00))
1570                     overflow = true;
1571                   bfd_put_16 (abfd, insn, loc);
1572                 }
1573
1574               break;
1575             }
1576
1577           if (overflow)
1578             {
1579               ((*_bfd_error_handler)
1580                ("%s: 0x%lx: fatal: reloc overflow while relaxing",
1581                 bfd_get_filename (abfd), (unsigned long) irel->r_offset));
1582               bfd_set_error (bfd_error_bad_value);
1583               return false;
1584             }
1585         }
1586     }
1587
1588   return true;
1589 }
1590 \f
1591 /* Relocate an SH ELF section.  */
1592
1593 static boolean
1594 sh_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1595                          contents, relocs, local_syms, local_sections)
1596      bfd *output_bfd;
1597      struct bfd_link_info *info;
1598      bfd *input_bfd;
1599      asection *input_section;
1600      bfd_byte *contents;
1601      Elf_Internal_Rela *relocs;
1602      Elf_Internal_Sym *local_syms;
1603      asection **local_sections;
1604 {
1605   Elf_Internal_Shdr *symtab_hdr;
1606   struct elf_link_hash_entry **sym_hashes;
1607   Elf_Internal_Rela *rel, *relend;
1608
1609   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1610   sym_hashes = elf_sym_hashes (input_bfd);
1611
1612   rel = relocs;
1613   relend = relocs + input_section->reloc_count;
1614   for (; rel < relend; rel++)
1615     {
1616       int r_type;
1617       reloc_howto_type *howto;
1618       unsigned long r_symndx;
1619       Elf_Internal_Sym *sym;
1620       asection *sec;
1621       struct elf_link_hash_entry *h;
1622       bfd_vma relocation;
1623       bfd_reloc_status_type r;
1624
1625       r_symndx = ELF32_R_SYM (rel->r_info);
1626
1627       if (info->relocateable)
1628         {
1629           /* This is a relocateable link.  We don't have to change
1630              anything, unless the reloc is against a section symbol,
1631              in which case we have to adjust according to where the
1632              section symbol winds up in the output section.  */
1633           if (r_symndx < symtab_hdr->sh_info)
1634             {
1635               sym = local_syms + r_symndx;
1636               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1637                 {
1638                   sec = local_sections[r_symndx];
1639                   rel->r_addend += sec->output_offset + sym->st_value;
1640                 }
1641             }
1642
1643           continue;
1644         }
1645
1646       r_type = ELF32_R_TYPE (rel->r_info);
1647
1648       /* Many of the relocs are only used for relaxing, and are
1649          handled entirely by the relaxation code.  */
1650       if (r_type > (int) LAST_INVALID_RELOC)
1651         continue;
1652
1653       if (r_type < 0
1654           || r_type >= (int) FIRST_INVALID_RELOC)
1655         {
1656           bfd_set_error (bfd_error_bad_value);
1657           return false;
1658         }
1659
1660       /* FIXME: This is certainly incorrect.  However, it is how the
1661          COFF linker works.  */
1662       if (r_type != (int) R_SH_DIR32
1663           && r_type != (int) R_SH_IND12W)
1664         continue;
1665
1666       howto = sh_elf_howto_table + r_type;
1667
1668       /* This is a final link.  */
1669       h = NULL;
1670       sym = NULL;
1671       sec = NULL;
1672       if (r_symndx < symtab_hdr->sh_info)
1673         {
1674           /* There is nothing to be done for an internal IND12W
1675              relocation.  FIXME: This is probably wrong, but it's how
1676              the COFF relocations work.  */
1677           if (r_type == (int) R_SH_IND12W)
1678             continue;
1679           sym = local_syms + r_symndx;
1680           sec = local_sections[r_symndx];
1681           relocation = (sec->output_section->vma
1682                         + sec->output_offset
1683                         + sym->st_value);
1684         }
1685       else
1686         {
1687           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1688           while (h->root.type == bfd_link_hash_indirect
1689                  || h->root.type == bfd_link_hash_warning)
1690             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1691           if (h->root.type == bfd_link_hash_defined
1692               || h->root.type == bfd_link_hash_defweak)
1693             {
1694               sec = h->root.u.def.section;
1695               relocation = (h->root.u.def.value
1696                             + sec->output_section->vma
1697                             + sec->output_offset);
1698             }
1699           else if (h->root.type == bfd_link_hash_undefweak)
1700             relocation = 0;
1701           else
1702             {
1703               if (! ((*info->callbacks->undefined_symbol)
1704                      (info, h->root.root.string, input_bfd,
1705                       input_section, rel->r_offset)))
1706                 return false;
1707               relocation = 0;
1708             }
1709         }
1710
1711       /* FIXME: This is how the COFF relocations work.  */
1712       if (r_type == (int) R_SH_IND12W)
1713         relocation -= 4;
1714
1715       /* FIXME: We should use the addend, but the COFF relocations
1716          don't.  */
1717       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1718                                     contents, rel->r_offset,
1719                                     relocation, 0);
1720
1721       if (r != bfd_reloc_ok)
1722         {
1723           switch (r)
1724             {
1725             default:
1726             case bfd_reloc_outofrange:
1727               abort ();
1728             case bfd_reloc_overflow:
1729               {
1730                 const char *name;
1731
1732                 if (h != NULL)
1733                   name = h->root.root.string;
1734                 else
1735                   {
1736                     name = (bfd_elf_string_from_elf_section
1737                             (input_bfd, symtab_hdr->sh_link, sym->st_name));
1738                     if (name == NULL)
1739                       return false;
1740                     if (*name == '\0')
1741                       name = bfd_section_name (input_bfd, sec);
1742                   }
1743                 if (! ((*info->callbacks->reloc_overflow)
1744                        (info, name, howto->name, (bfd_vma) 0,
1745                         input_bfd, input_section, rel->r_offset)))
1746                   return false;
1747               }
1748               break;
1749             }
1750         }
1751     }
1752
1753   return true;
1754 }
1755
1756 /* This is a version of bfd_generic_get_relocated_section_contents
1757    which uses sh_elf_relocate_section.  */
1758
1759 static bfd_byte *
1760 sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1761                                         data, relocateable, symbols)
1762      bfd *output_bfd;
1763      struct bfd_link_info *link_info;
1764      struct bfd_link_order *link_order;
1765      bfd_byte *data;
1766      boolean relocateable;
1767      asymbol **symbols;
1768 {
1769   Elf_Internal_Shdr *symtab_hdr;
1770   asection *input_section = link_order->u.indirect.section;
1771   bfd *input_bfd = input_section->owner;
1772   asection **sections = NULL;
1773   Elf_Internal_Rela *internal_relocs = NULL;
1774   Elf32_External_Sym *external_syms = NULL;
1775   Elf_Internal_Sym *internal_syms = NULL;
1776
1777   /* We only need to handle the case of relaxing, or of having a
1778      particular set of section contents, specially.  */
1779   if (relocateable
1780       || elf_section_data (input_section)->this_hdr.contents == NULL)
1781     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1782                                                        link_order, data,
1783                                                        relocateable,
1784                                                        symbols);
1785
1786   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1787
1788   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1789           input_section->_raw_size);
1790
1791   if ((input_section->flags & SEC_RELOC) != 0
1792       && input_section->reloc_count > 0)
1793     {
1794       Elf_Internal_Sym *isymp;
1795       asection **secpp;
1796       Elf32_External_Sym *esym, *esymend;
1797
1798       if (symtab_hdr->contents != NULL)
1799         external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1800       else
1801         {
1802           external_syms = ((Elf32_External_Sym *)
1803                            bfd_malloc (symtab_hdr->sh_info
1804                                        * sizeof (Elf32_External_Sym)));
1805           if (external_syms == NULL && symtab_hdr->sh_info > 0)
1806             goto error_return;
1807           if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1808               || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1809                             symtab_hdr->sh_info, input_bfd)
1810                   != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1811             goto error_return;
1812         }
1813
1814       internal_relocs = (_bfd_elf32_link_read_relocs
1815                          (input_bfd, input_section, (PTR) NULL,
1816                           (Elf_Internal_Rela *) NULL, false));
1817       if (internal_relocs == NULL)
1818         goto error_return;
1819
1820       internal_syms = ((Elf_Internal_Sym *)
1821                        bfd_malloc (symtab_hdr->sh_info
1822                                    * sizeof (Elf_Internal_Sym)));
1823       if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1824         goto error_return;
1825
1826       sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1827                                            * sizeof (asection *));
1828       if (sections == NULL && symtab_hdr->sh_info > 0)
1829         goto error_return;
1830
1831       isymp = internal_syms;
1832       secpp = sections;
1833       esym = external_syms;
1834       esymend = esym + symtab_hdr->sh_info;
1835       for (; esym < esymend; ++esym, ++isymp, ++secpp)
1836         {
1837           asection *isec;
1838
1839           bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1840
1841           if (isymp->st_shndx == SHN_UNDEF)
1842             isec = bfd_und_section_ptr;
1843           else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1844             isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1845           else if (isymp->st_shndx == SHN_ABS)
1846             isec = bfd_abs_section_ptr;
1847           else if (isymp->st_shndx == SHN_COMMON)
1848             isec = bfd_com_section_ptr;
1849           else
1850             {
1851               /* Who knows?  */
1852               isec = NULL;
1853             }
1854
1855           *secpp = isec;
1856         }
1857
1858       if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
1859                                      input_section, data, internal_relocs,
1860                                      internal_syms, sections))
1861         goto error_return;
1862
1863       if (sections != NULL)
1864         free (sections);
1865       sections = NULL;
1866       if (internal_syms != NULL)
1867         free (internal_syms);
1868       internal_syms = NULL;
1869       if (external_syms != NULL && symtab_hdr->contents == NULL)
1870         free (external_syms);
1871       external_syms = NULL;
1872       if (internal_relocs != elf_section_data (input_section)->relocs)
1873         free (internal_relocs);
1874       internal_relocs = NULL;
1875     }
1876
1877   return data;
1878
1879  error_return:
1880   if (internal_relocs != NULL
1881       && internal_relocs != elf_section_data (input_section)->relocs)
1882     free (internal_relocs);
1883   if (external_syms != NULL && symtab_hdr->contents == NULL)
1884     free (external_syms);
1885   if (internal_syms != NULL)
1886     free (internal_syms);
1887   if (sections != NULL)
1888     free (sections);
1889   return NULL;
1890 }
1891
1892 #define TARGET_BIG_SYM          bfd_elf32_sh_vec
1893 #define TARGET_BIG_NAME         "elf32-sh"
1894 #define TARGET_LITTLE_SYM       bfd_elf32_shl_vec
1895 #define TARGET_LITTLE_NAME      "elf32-shl"
1896 #define ELF_ARCH                bfd_arch_sh
1897 #define ELF_MACHINE_CODE        EM_SH
1898 #define ELF_MAXPAGESIZE         0x1
1899
1900 #define elf_symbol_leading_char '_'
1901
1902 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
1903 #define elf_info_to_howto               sh_elf_info_to_howto
1904 #define bfd_elf32_bfd_relax_section     sh_elf_relax_section
1905 #define elf_backend_relocate_section    sh_elf_relocate_section
1906 #define bfd_elf32_bfd_get_relocated_section_contents \
1907                                         sh_elf_get_relocated_section_contents
1908
1909 #include "elf32-target.h"