* elf-m10300.c (mn10300_elf_relax_section): Preserve reloc addend
[platform/upstream/binutils.git] / bfd / elf-m10300.c
1 /* Matsushita 10300 specific support for 32-bit ELF
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation, Inc.
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 "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/mn10300.h"
26
27 static bfd_reloc_status_type mn10300_elf_final_link_relocate
28   PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
29            bfd_vma, bfd_vma, bfd_vma,
30            struct elf_link_hash_entry *, unsigned long, struct bfd_link_info *,
31            asection *, int));
32 static bfd_boolean mn10300_elf_relocate_section
33   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
34            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
35 static bfd_boolean mn10300_elf_relax_section
36   PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
37 static bfd_byte * mn10300_elf_get_relocated_section_contents
38   PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
39            bfd_byte *, bfd_boolean, asymbol **));
40 static unsigned long elf_mn10300_mach
41   PARAMS ((flagword));
42 void _bfd_mn10300_elf_final_write_processing
43   PARAMS ((bfd *, bfd_boolean));
44 bfd_boolean _bfd_mn10300_elf_object_p
45   PARAMS ((bfd *));
46 bfd_boolean _bfd_mn10300_elf_merge_private_bfd_data
47   PARAMS ((bfd *,bfd *));
48
49 /* The mn10300 linker needs to keep track of the number of relocs that
50    it decides to copy in check_relocs for each symbol.  This is so
51    that it can discard PC relative relocs if it doesn't need them when
52    linking with -Bsymbolic.  We store the information in a field
53    extending the regular ELF linker hash table.  */
54
55 /* This structure keeps track of the number of PC relative relocs we
56    have copied for a given symbol.  */
57
58 struct elf_mn10300_pcrel_relocs_copied
59 {
60   /* Next section.  */
61   struct elf_mn10300_pcrel_relocs_copied * next;
62   /* A section in dynobj.  */
63   asection * section;
64   /* Number of relocs copied in this section.  */
65   bfd_size_type count;
66 };
67
68 struct elf32_mn10300_link_hash_entry {
69   /* The basic elf link hash table entry.  */
70   struct elf_link_hash_entry root;
71
72   /* For function symbols, the number of times this function is
73      called directly (ie by name).  */
74   unsigned int direct_calls;
75
76   /* For function symbols, the size of this function's stack
77      (if <= 255 bytes).  We stuff this into "call" instructions
78      to this target when it's valid and profitable to do so.
79
80      This does not include stack allocated by movm!  */
81   unsigned char stack_size;
82
83   /* For function symbols, arguments (if any) for movm instruction
84      in the prologue.  We stuff this value into "call" instructions
85      to the target when it's valid and profitable to do so.  */
86   unsigned char movm_args;
87
88   /* For function symbols, the amount of stack space that would be allocated
89      by the movm instruction.  This is redundant with movm_args, but we
90      add it to the hash table to avoid computing it over and over.  */
91   unsigned char movm_stack_size;
92
93   /* Number of PC relative relocs copied for this symbol.  */
94   struct elf_mn10300_pcrel_relocs_copied * pcrel_relocs_copied;
95
96 /* When set, convert all "call" instructions to this target into "calls"
97    instructions.  */
98 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
99
100 /* Used to mark functions which have had redundant parts of their
101    prologue deleted.  */
102 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
103   unsigned char flags;
104 };
105
106 /* We derive a hash table from the main elf linker hash table so
107    we can store state variables and a secondary hash table without
108    resorting to global variables.  */
109 struct elf32_mn10300_link_hash_table {
110   /* The main hash table.  */
111   struct elf_link_hash_table root;
112
113   /* A hash table for static functions.  We could derive a new hash table
114      instead of using the full elf32_mn10300_link_hash_table if we wanted
115      to save some memory.  */
116   struct elf32_mn10300_link_hash_table *static_hash_table;
117
118   /* Random linker state flags.  */
119 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
120   char flags;
121 };
122
123 /* For MN10300 linker hash table.  */
124
125 /* Get the MN10300 ELF linker hash table from a link_info structure.  */
126
127 #define elf32_mn10300_hash_table(p) \
128   ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
129
130 #define elf32_mn10300_link_hash_traverse(table, func, info)             \
131   (elf_link_hash_traverse                                               \
132    (&(table)->root,                                                     \
133     (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
134     (info)))
135
136 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
137   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
138 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
139   PARAMS ((bfd *));
140 static void elf32_mn10300_link_hash_table_free
141   PARAMS ((struct bfd_link_hash_table *));
142
143 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
144   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
145 static void mn10300_info_to_howto
146   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
147 static bfd_boolean mn10300_elf_check_relocs
148   PARAMS ((bfd *, struct bfd_link_info *, asection *,
149            const Elf_Internal_Rela *));
150 static asection *mn10300_elf_gc_mark_hook
151   PARAMS ((asection *, struct bfd_link_info *info, Elf_Internal_Rela *,
152            struct elf_link_hash_entry *, Elf_Internal_Sym *));
153 static bfd_boolean mn10300_elf_relax_delete_bytes
154   PARAMS ((bfd *, asection *, bfd_vma, int));
155 static bfd_boolean mn10300_elf_symbol_address_p
156   PARAMS ((bfd *, asection *, Elf_Internal_Sym *, bfd_vma));
157 static bfd_boolean elf32_mn10300_finish_hash_table_entry
158   PARAMS ((struct bfd_hash_entry *, PTR));
159 static void compute_function_info
160   PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
161            bfd_vma, unsigned char *));
162
163 static bfd_boolean _bfd_mn10300_elf_create_got_section
164   PARAMS ((bfd *, struct bfd_link_info *));
165 static bfd_boolean _bfd_mn10300_elf_create_dynamic_sections
166   PARAMS ((bfd *, struct bfd_link_info *));
167 static bfd_boolean _bfd_mn10300_elf_adjust_dynamic_symbol
168   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
169 static bfd_boolean _bfd_mn10300_elf_discard_copies
170   PARAMS ((struct elf32_mn10300_link_hash_entry *,
171            struct bfd_link_info *));
172 static bfd_boolean _bfd_mn10300_elf_size_dynamic_sections
173   PARAMS ((bfd *, struct bfd_link_info *));
174 static bfd_boolean _bfd_mn10300_elf_finish_dynamic_symbol
175   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
176            Elf_Internal_Sym *));
177 static bfd_boolean _bfd_mn10300_elf_finish_dynamic_sections
178   PARAMS ((bfd *, struct bfd_link_info *));
179
180 static reloc_howto_type elf_mn10300_howto_table[] = {
181   /* Dummy relocation.  Does nothing.  */
182   HOWTO (R_MN10300_NONE,
183          0,
184          2,
185          16,
186          FALSE,
187          0,
188          complain_overflow_bitfield,
189          bfd_elf_generic_reloc,
190          "R_MN10300_NONE",
191          FALSE,
192          0,
193          0,
194          FALSE),
195   /* Standard 32 bit reloc.  */
196   HOWTO (R_MN10300_32,
197          0,
198          2,
199          32,
200          FALSE,
201          0,
202          complain_overflow_bitfield,
203          bfd_elf_generic_reloc,
204          "R_MN10300_32",
205          FALSE,
206          0xffffffff,
207          0xffffffff,
208          FALSE),
209   /* Standard 16 bit reloc.  */
210   HOWTO (R_MN10300_16,
211          0,
212          1,
213          16,
214          FALSE,
215          0,
216          complain_overflow_bitfield,
217          bfd_elf_generic_reloc,
218          "R_MN10300_16",
219          FALSE,
220          0xffff,
221          0xffff,
222          FALSE),
223   /* Standard 8 bit reloc.  */
224   HOWTO (R_MN10300_8,
225          0,
226          0,
227          8,
228          FALSE,
229          0,
230          complain_overflow_bitfield,
231          bfd_elf_generic_reloc,
232          "R_MN10300_8",
233          FALSE,
234          0xff,
235          0xff,
236          FALSE),
237   /* Standard 32bit pc-relative reloc.  */
238   HOWTO (R_MN10300_PCREL32,
239          0,
240          2,
241          32,
242          TRUE,
243          0,
244          complain_overflow_bitfield,
245          bfd_elf_generic_reloc,
246          "R_MN10300_PCREL32",
247          FALSE,
248          0xffffffff,
249          0xffffffff,
250          TRUE),
251   /* Standard 16bit pc-relative reloc.  */
252   HOWTO (R_MN10300_PCREL16,
253          0,
254          1,
255          16,
256          TRUE,
257          0,
258          complain_overflow_bitfield,
259          bfd_elf_generic_reloc,
260          "R_MN10300_PCREL16",
261          FALSE,
262          0xffff,
263          0xffff,
264          TRUE),
265   /* Standard 8 pc-relative reloc.  */
266   HOWTO (R_MN10300_PCREL8,
267          0,
268          0,
269          8,
270          TRUE,
271          0,
272          complain_overflow_bitfield,
273          bfd_elf_generic_reloc,
274          "R_MN10300_PCREL8",
275          FALSE,
276          0xff,
277          0xff,
278          TRUE),
279
280   /* GNU extension to record C++ vtable hierarchy */
281   HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
282          0,                     /* rightshift */
283          0,                     /* size (0 = byte, 1 = short, 2 = long) */
284          0,                     /* bitsize */
285          FALSE,                 /* pc_relative */
286          0,                     /* bitpos */
287          complain_overflow_dont, /* complain_on_overflow */
288          NULL,                  /* special_function */
289          "R_MN10300_GNU_VTINHERIT", /* name */
290          FALSE,                 /* partial_inplace */
291          0,                     /* src_mask */
292          0,                     /* dst_mask */
293          FALSE),                /* pcrel_offset */
294
295   /* GNU extension to record C++ vtable member usage */
296   HOWTO (R_MN10300_GNU_VTENTRY, /* type */
297          0,                     /* rightshift */
298          0,                     /* size (0 = byte, 1 = short, 2 = long) */
299          0,                     /* bitsize */
300          FALSE,                 /* pc_relative */
301          0,                     /* bitpos */
302          complain_overflow_dont, /* complain_on_overflow */
303          NULL,                  /* special_function */
304          "R_MN10300_GNU_VTENTRY", /* name */
305          FALSE,                 /* partial_inplace */
306          0,                     /* src_mask */
307          0,                     /* dst_mask */
308          FALSE),                /* pcrel_offset */
309
310   /* Standard 24 bit reloc.  */
311   HOWTO (R_MN10300_24,
312          0,
313          2,
314          24,
315          FALSE,
316          0,
317          complain_overflow_bitfield,
318          bfd_elf_generic_reloc,
319          "R_MN10300_24",
320          FALSE,
321          0xffffff,
322          0xffffff,
323          FALSE),
324   HOWTO (R_MN10300_GOTPC32,     /* type */
325          0,                     /* rightshift */
326          2,                     /* size (0 = byte, 1 = short, 2 = long) */
327          32,                    /* bitsize */
328          TRUE,                  /* pc_relative */
329          0,                     /* bitpos */
330          complain_overflow_bitfield, /* complain_on_overflow */
331          bfd_elf_generic_reloc, /* */
332          "R_MN10300_GOTPC32",   /* name */
333          FALSE,                 /* partial_inplace */
334          0xffffffff,            /* src_mask */
335          0xffffffff,            /* dst_mask */
336          TRUE),                 /* pcrel_offset */
337
338   HOWTO (R_MN10300_GOTPC16,     /* type */
339          0,                     /* rightshift */
340          1,                     /* size (0 = byte, 1 = short, 2 = long) */
341          16,                    /* bitsize */
342          TRUE,                  /* pc_relative */
343          0,                     /* bitpos */
344          complain_overflow_bitfield, /* complain_on_overflow */
345          bfd_elf_generic_reloc, /* */
346          "R_MN10300_GOTPC16",   /* name */
347          FALSE,                 /* partial_inplace */
348          0xffff,                /* src_mask */
349          0xffff,                /* dst_mask */
350          TRUE),                 /* pcrel_offset */
351
352   HOWTO (R_MN10300_GOTOFF32,    /* type */
353          0,                     /* rightshift */
354          2,                     /* size (0 = byte, 1 = short, 2 = long) */
355          32,                    /* bitsize */
356          FALSE,                 /* pc_relative */
357          0,                     /* bitpos */
358          complain_overflow_bitfield, /* complain_on_overflow */
359          bfd_elf_generic_reloc, /* */
360          "R_MN10300_GOTOFF32",  /* name */
361          FALSE,                 /* partial_inplace */
362          0xffffffff,            /* src_mask */
363          0xffffffff,            /* dst_mask */
364          FALSE),                /* pcrel_offset */
365
366   HOWTO (R_MN10300_GOTOFF24,    /* type */
367          0,                     /* rightshift */
368          2,                     /* size (0 = byte, 1 = short, 2 = long) */
369          24,                    /* bitsize */
370          FALSE,                 /* pc_relative */
371          0,                     /* bitpos */
372          complain_overflow_bitfield, /* complain_on_overflow */
373          bfd_elf_generic_reloc, /* */
374          "R_MN10300_GOTOFF24",  /* name */
375          FALSE,                 /* partial_inplace */
376          0xffffff,              /* src_mask */
377          0xffffff,              /* dst_mask */
378          FALSE),                /* pcrel_offset */
379
380   HOWTO (R_MN10300_GOTOFF16,    /* type */
381          0,                     /* rightshift */
382          1,                     /* size (0 = byte, 1 = short, 2 = long) */
383          16,                    /* bitsize */
384          FALSE,                 /* pc_relative */
385          0,                     /* bitpos */
386          complain_overflow_bitfield, /* complain_on_overflow */
387          bfd_elf_generic_reloc, /* */
388          "R_MN10300_GOTOFF16",  /* name */
389          FALSE,                 /* partial_inplace */
390          0xffff,                /* src_mask */
391          0xffff,                /* dst_mask */
392          FALSE),                /* pcrel_offset */
393
394   HOWTO (R_MN10300_PLT32,       /* type */
395          0,                     /* rightshift */
396          2,                     /* size (0 = byte, 1 = short, 2 = long) */
397          32,                    /* bitsize */
398          TRUE,                  /* pc_relative */
399          0,                     /* bitpos */
400          complain_overflow_bitfield, /* complain_on_overflow */
401          bfd_elf_generic_reloc, /* */
402          "R_MN10300_PLT32",     /* name */
403          FALSE,                 /* partial_inplace */
404          0xffffffff,            /* src_mask */
405          0xffffffff,            /* dst_mask */
406          TRUE),                 /* pcrel_offset */
407
408   HOWTO (R_MN10300_PLT16,       /* type */
409          0,                     /* rightshift */
410          1,                     /* size (0 = byte, 1 = short, 2 = long) */
411          16,                    /* bitsize */
412          TRUE,                  /* pc_relative */
413          0,                     /* bitpos */
414          complain_overflow_bitfield, /* complain_on_overflow */
415          bfd_elf_generic_reloc, /* */
416          "R_MN10300_PLT16",     /* name */
417          FALSE,                 /* partial_inplace */
418          0xffff,                /* src_mask */
419          0xffff,                /* dst_mask */
420          TRUE),                 /* pcrel_offset */
421
422   HOWTO (R_MN10300_GOT32,       /* type */
423          0,                     /* rightshift */
424          2,                     /* size (0 = byte, 1 = short, 2 = long) */
425          32,                    /* bitsize */
426          FALSE,                 /* pc_relative */
427          0,                     /* bitpos */
428          complain_overflow_bitfield, /* complain_on_overflow */
429          bfd_elf_generic_reloc, /* */
430          "R_MN10300_GOT32",     /* name */
431          FALSE,                 /* partial_inplace */
432          0xffffffff,            /* src_mask */
433          0xffffffff,            /* dst_mask */
434          FALSE),                /* pcrel_offset */
435
436   HOWTO (R_MN10300_GOT24,       /* type */
437          0,                     /* rightshift */
438          2,                     /* size (0 = byte, 1 = short, 2 = long) */
439          24,                    /* bitsize */
440          FALSE,                 /* pc_relative */
441          0,                     /* bitpos */
442          complain_overflow_bitfield, /* complain_on_overflow */
443          bfd_elf_generic_reloc, /* */
444          "R_MN10300_GOT24",     /* name */
445          FALSE,                 /* partial_inplace */
446          0xffffffff,            /* src_mask */
447          0xffffffff,            /* dst_mask */
448          FALSE),                /* pcrel_offset */
449
450   HOWTO (R_MN10300_GOT16,       /* type */
451          0,                     /* rightshift */
452          1,                     /* size (0 = byte, 1 = short, 2 = long) */
453          16,                    /* bitsize */
454          FALSE,                 /* pc_relative */
455          0,                     /* bitpos */
456          complain_overflow_bitfield, /* complain_on_overflow */
457          bfd_elf_generic_reloc, /* */
458          "R_MN10300_GOT16",     /* name */
459          FALSE,                 /* partial_inplace */
460          0xffffffff,            /* src_mask */
461          0xffffffff,            /* dst_mask */
462          FALSE),                /* pcrel_offset */
463
464   HOWTO (R_MN10300_COPY,        /* type */
465          0,                     /* rightshift */
466          2,                     /* size (0 = byte, 1 = short, 2 = long) */
467          32,                    /* bitsize */
468          FALSE,                 /* pc_relative */
469          0,                     /* bitpos */
470          complain_overflow_bitfield, /* complain_on_overflow */
471          bfd_elf_generic_reloc, /* */
472          "R_MN10300_COPY",              /* name */
473          FALSE,                 /* partial_inplace */
474          0xffffffff,            /* src_mask */
475          0xffffffff,            /* dst_mask */
476          FALSE),                /* pcrel_offset */
477
478   HOWTO (R_MN10300_GLOB_DAT,    /* type */
479          0,                     /* rightshift */
480          2,                     /* size (0 = byte, 1 = short, 2 = long) */
481          32,                    /* bitsize */
482          FALSE,                 /* pc_relative */
483          0,                     /* bitpos */
484          complain_overflow_bitfield, /* complain_on_overflow */
485          bfd_elf_generic_reloc, /* */
486          "R_MN10300_GLOB_DAT",  /* name */
487          FALSE,                 /* partial_inplace */
488          0xffffffff,            /* src_mask */
489          0xffffffff,            /* dst_mask */
490          FALSE),                /* pcrel_offset */
491
492   HOWTO (R_MN10300_JMP_SLOT,    /* type */
493          0,                     /* rightshift */
494          2,                     /* size (0 = byte, 1 = short, 2 = long) */
495          32,                    /* bitsize */
496          FALSE,                 /* pc_relative */
497          0,                     /* bitpos */
498          complain_overflow_bitfield, /* complain_on_overflow */
499          bfd_elf_generic_reloc, /* */
500          "R_MN10300_JMP_SLOT",  /* name */
501          FALSE,                 /* partial_inplace */
502          0xffffffff,            /* src_mask */
503          0xffffffff,            /* dst_mask */
504          FALSE),                /* pcrel_offset */
505
506   HOWTO (R_MN10300_RELATIVE,    /* type */
507          0,                     /* rightshift */
508          2,                     /* size (0 = byte, 1 = short, 2 = long) */
509          32,                    /* bitsize */
510          FALSE,                 /* pc_relative */
511          0,                     /* bitpos */
512          complain_overflow_bitfield, /* complain_on_overflow */
513          bfd_elf_generic_reloc, /* */
514          "R_MN10300_RELATIVE",  /* name */
515          FALSE,                 /* partial_inplace */
516          0xffffffff,            /* src_mask */
517          0xffffffff,            /* dst_mask */
518          FALSE),                /* pcrel_offset */
519
520 };
521
522 struct mn10300_reloc_map {
523   bfd_reloc_code_real_type bfd_reloc_val;
524   unsigned char elf_reloc_val;
525 };
526
527 static const struct mn10300_reloc_map mn10300_reloc_map[] = {
528   { BFD_RELOC_NONE, R_MN10300_NONE, },
529   { BFD_RELOC_32, R_MN10300_32, },
530   { BFD_RELOC_16, R_MN10300_16, },
531   { BFD_RELOC_8, R_MN10300_8, },
532   { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
533   { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
534   { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
535   { BFD_RELOC_24, R_MN10300_24, },
536   { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
537   { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
538   { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 },
539   { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 },
540   { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 },
541   { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 },
542   { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 },
543   { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 },
544   { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 },
545   { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 },
546   { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 },
547   { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 },
548   { BFD_RELOC_MN10300_COPY, R_MN10300_COPY },
549   { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT },
550   { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT },
551   { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE },
552 };
553
554 /* Create the GOT section.  */
555
556 static bfd_boolean
557 _bfd_mn10300_elf_create_got_section (abfd, info)
558      bfd * abfd;
559      struct bfd_link_info * info;
560 {
561   flagword   flags;
562   flagword   pltflags;
563   asection * s;
564   struct bfd_link_hash_entry * bh;
565   struct elf_link_hash_entry * h;
566   const struct elf_backend_data * bed = get_elf_backend_data (abfd);
567   int ptralign;
568
569   /* This function may be called more than once.  */
570   if (bfd_get_section_by_name (abfd, ".got") != NULL)
571     return TRUE;
572
573   switch (bed->s->arch_size)
574     {
575     case 32:
576       ptralign = 2;
577       break;
578
579     case 64:
580       ptralign = 3;
581       break;
582
583     default:
584       bfd_set_error (bfd_error_bad_value);
585       return FALSE;
586     }
587
588   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
589            | SEC_LINKER_CREATED);
590
591   pltflags = flags;
592   pltflags |= SEC_CODE;
593   if (bed->plt_not_loaded)
594     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
595   if (bed->plt_readonly)
596     pltflags |= SEC_READONLY;
597
598   s = bfd_make_section (abfd, ".plt");
599   if (s == NULL
600       || ! bfd_set_section_flags (abfd, s, pltflags)
601       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
602     return FALSE;
603
604   if (bed->want_plt_sym)
605     {
606       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
607          .plt section.  */
608       bh = NULL;
609       if (! (_bfd_generic_link_add_one_symbol
610              (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
611               (bfd_vma) 0, (const char *) NULL, FALSE,
612               get_elf_backend_data (abfd)->collect, &bh)))
613         return FALSE;
614       h = (struct elf_link_hash_entry *) bh;
615       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
616       h->type = STT_OBJECT;
617
618       if (info->shared
619           && ! bfd_elf_link_record_dynamic_symbol (info, h))
620         return FALSE;
621     }
622
623   s = bfd_make_section (abfd, ".got");
624   if (s == NULL
625       || ! bfd_set_section_flags (abfd, s, flags)
626       || ! bfd_set_section_alignment (abfd, s, ptralign))
627     return FALSE;
628
629   if (bed->want_got_plt)
630     {
631       s = bfd_make_section (abfd, ".got.plt");
632       if (s == NULL
633           || ! bfd_set_section_flags (abfd, s, flags)
634           || ! bfd_set_section_alignment (abfd, s, ptralign))
635         return FALSE;
636     }
637
638   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
639      (or .got.plt) section.  We don't do this in the linker script
640      because we don't want to define the symbol if we are not creating
641      a global offset table.  */
642   bh = NULL;
643   if (!(_bfd_generic_link_add_one_symbol
644         (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
645          bed->got_symbol_offset, (const char *) NULL, FALSE,
646          bed->collect, &bh)))
647     return FALSE;
648   h = (struct elf_link_hash_entry *) bh;
649   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
650   h->type = STT_OBJECT;
651
652   if (info->shared
653       && ! bfd_elf_link_record_dynamic_symbol (info, h))
654     return FALSE;
655
656   elf_hash_table (info)->hgot = h;
657
658   /* The first bit of the global offset table is the header.  */
659   s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
660
661   return TRUE;
662 }
663
664 static reloc_howto_type *
665 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
666      bfd *abfd ATTRIBUTE_UNUSED;
667      bfd_reloc_code_real_type code;
668 {
669   unsigned int i;
670
671   for (i = 0;
672        i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
673        i++)
674     {
675       if (mn10300_reloc_map[i].bfd_reloc_val == code)
676         return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
677     }
678
679   return NULL;
680 }
681
682 /* Set the howto pointer for an MN10300 ELF reloc.  */
683
684 static void
685 mn10300_info_to_howto (abfd, cache_ptr, dst)
686      bfd *abfd ATTRIBUTE_UNUSED;
687      arelent *cache_ptr;
688      Elf_Internal_Rela *dst;
689 {
690   unsigned int r_type;
691
692   r_type = ELF32_R_TYPE (dst->r_info);
693   BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
694   cache_ptr->howto = &elf_mn10300_howto_table[r_type];
695 }
696
697 /* Look through the relocs for a section during the first phase.
698    Since we don't do .gots or .plts, we just need to consider the
699    virtual table relocs for gc.  */
700
701 static bfd_boolean
702 mn10300_elf_check_relocs (abfd, info, sec, relocs)
703      bfd *abfd;
704      struct bfd_link_info *info;
705      asection *sec;
706      const Elf_Internal_Rela *relocs;
707 {
708   Elf_Internal_Shdr *symtab_hdr;
709   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
710   const Elf_Internal_Rela *rel;
711   const Elf_Internal_Rela *rel_end;
712   bfd *      dynobj;
713   bfd_vma *  local_got_offsets;
714   asection * sgot;
715   asection * srelgot;
716   asection * sreloc;
717
718   sgot    = NULL;
719   srelgot = NULL;
720   sreloc  = NULL;
721
722   if (info->relocatable)
723     return TRUE;
724
725   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
726   sym_hashes = elf_sym_hashes (abfd);
727   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
728   if (!elf_bad_symtab (abfd))
729     sym_hashes_end -= symtab_hdr->sh_info;
730
731   dynobj = elf_hash_table (info)->dynobj;
732   local_got_offsets = elf_local_got_offsets (abfd);
733   rel_end = relocs + sec->reloc_count;
734   for (rel = relocs; rel < rel_end; rel++)
735     {
736       struct elf_link_hash_entry *h;
737       unsigned long r_symndx;
738
739       r_symndx = ELF32_R_SYM (rel->r_info);
740       if (r_symndx < symtab_hdr->sh_info)
741         h = NULL;
742       else
743         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
744
745       /* Some relocs require a global offset table.  */
746       if (dynobj == NULL)
747         {
748           switch (ELF32_R_TYPE (rel->r_info))
749             {
750             case R_MN10300_GOT32:
751             case R_MN10300_GOT24:
752             case R_MN10300_GOT16:
753             case R_MN10300_GOTOFF32:
754             case R_MN10300_GOTOFF24:
755             case R_MN10300_GOTOFF16:
756             case R_MN10300_GOTPC32:
757             case R_MN10300_GOTPC16:
758               elf_hash_table (info)->dynobj = dynobj = abfd;
759               if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
760                 return FALSE;
761               break;
762
763             default:
764               break;
765             }
766         }
767
768       switch (ELF32_R_TYPE (rel->r_info))
769         {
770         /* This relocation describes the C++ object vtable hierarchy.
771            Reconstruct it for later use during GC.  */
772         case R_MN10300_GNU_VTINHERIT:
773           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
774             return FALSE;
775           break;
776
777         /* This relocation describes which C++ vtable entries are actually
778            used.  Record for later use during GC.  */
779         case R_MN10300_GNU_VTENTRY:
780           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
781             return FALSE;
782           break;
783         case R_MN10300_GOT32:
784         case R_MN10300_GOT24:
785         case R_MN10300_GOT16:
786           /* This symbol requires a global offset table entry.  */
787
788           if (sgot == NULL)
789             {
790               sgot = bfd_get_section_by_name (dynobj, ".got");
791               BFD_ASSERT (sgot != NULL);
792             }
793
794           if (srelgot == NULL
795               && (h != NULL || info->shared))
796             {
797               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
798               if (srelgot == NULL)
799                 {
800                   srelgot = bfd_make_section (dynobj, ".rela.got");
801                   if (srelgot == NULL
802                       || ! bfd_set_section_flags (dynobj, srelgot,
803                                                   (SEC_ALLOC
804                                                    | SEC_LOAD
805                                                    | SEC_HAS_CONTENTS
806                                                    | SEC_IN_MEMORY
807                                                    | SEC_LINKER_CREATED
808                                                    | SEC_READONLY))
809                       || ! bfd_set_section_alignment (dynobj, srelgot, 2))
810                     return FALSE;
811                 }
812             }
813
814           if (h != NULL)
815             {
816               if (h->got.offset != (bfd_vma) -1)
817                 /* We have already allocated space in the .got.  */
818                 break;
819
820               h->got.offset = sgot->_raw_size;
821
822               /* Make sure this symbol is output as a dynamic symbol.  */
823               if (h->dynindx == -1)
824                 {
825                   if (! bfd_elf_link_record_dynamic_symbol (info, h))
826                     return FALSE;
827                 }
828
829               srelgot->_raw_size += sizeof (Elf32_External_Rela);
830             }
831           else
832             {
833               /* This is a global offset table entry for a local
834                  symbol.  */
835               if (local_got_offsets == NULL)
836                 {
837                   size_t       size;
838                   unsigned int i;
839
840                   size = symtab_hdr->sh_info * sizeof (bfd_vma);
841                   local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
842
843                   if (local_got_offsets == NULL)
844                     return FALSE;
845                   elf_local_got_offsets (abfd) = local_got_offsets;
846
847                   for (i = 0; i < symtab_hdr->sh_info; i++)
848                     local_got_offsets[i] = (bfd_vma) -1;
849                 }
850
851               if (local_got_offsets[r_symndx] != (bfd_vma) -1)
852                 /* We have already allocated space in the .got.  */
853                 break;
854
855               local_got_offsets[r_symndx] = sgot->_raw_size;
856
857               if (info->shared)
858                 /* If we are generating a shared object, we need to
859                    output a R_MN10300_RELATIVE reloc so that the dynamic
860                    linker can adjust this GOT entry.  */
861                 srelgot->_raw_size += sizeof (Elf32_External_Rela);
862             }
863
864           sgot->_raw_size += 4;
865
866           break;
867
868         case R_MN10300_PLT32:
869         case R_MN10300_PLT16:
870           /* This symbol requires a procedure linkage table entry.  We
871              actually build the entry in adjust_dynamic_symbol,
872              because this might be a case of linking PIC code which is
873              never referenced by a dynamic object, in which case we
874              don't need to generate a procedure linkage table entry
875              after all.  */
876
877           /* If this is a local symbol, we resolve it directly without
878              creating a procedure linkage table entry.  */
879           if (h == NULL)
880             continue;
881
882           if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
883               || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
884             break;
885
886           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
887
888           break;
889
890         case R_MN10300_32:
891         case R_MN10300_24:
892         case R_MN10300_16:
893         case R_MN10300_8:
894         case R_MN10300_PCREL32:
895         case R_MN10300_PCREL16:
896         case R_MN10300_PCREL8:
897           if (h != NULL)
898             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
899
900           /* If we are creating a shared library, and this is a reloc
901              against a global symbol, or a non PC relative reloc
902              against a local symbol, then we need to copy the reloc
903              into the shared library.  However, if we are linking with
904              -Bsymbolic, we do not need to copy a reloc against a
905              global symbol which is defined in an object we are
906              including in the link (i.e., DEF_REGULAR is set).  At
907              this point we have not seen all the input files, so it is
908              possible that DEF_REGULAR is not set now but will be set
909              later (it is never cleared).  We account for that
910              possibility below by storing information in the
911              pcrel_relocs_copied field of the hash table entry.  */
912           if (info->shared
913               && (sec->flags & SEC_ALLOC) != 0
914               && (! (elf_mn10300_howto_table[ELF32_R_TYPE (rel->r_info)]
915                      .pc_relative)
916                   || (h != NULL
917                       && (! info->symbolic
918                           || h->root.type == bfd_link_hash_defweak
919                           || (h->elf_link_hash_flags
920                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
921             {
922               /* When creating a shared object, we must copy these
923                  reloc types into the output file.  We create a reloc
924                  section in dynobj and make room for this reloc.  */
925               if (sreloc == NULL)
926                 {
927                   const char * name;
928
929                   name = (bfd_elf_string_from_elf_section
930                           (abfd,
931                            elf_elfheader (abfd)->e_shstrndx,
932                            elf_section_data (sec)->rel_hdr.sh_name));
933                   if (name == NULL)
934                     return FALSE;
935
936                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
937                               && strcmp (bfd_get_section_name (abfd, sec),
938                                          name + 5) == 0);
939
940                   sreloc = bfd_get_section_by_name (dynobj, name);
941                   if (sreloc == NULL)
942                     {
943                       flagword flags;
944
945                       sreloc = bfd_make_section (dynobj, name);
946                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
947                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
948                       if ((sec->flags & SEC_ALLOC) != 0)
949                         flags |= SEC_ALLOC | SEC_LOAD;
950                       if (sreloc == NULL
951                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
952                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
953                         return FALSE;
954                     }
955                 }
956
957               sreloc->_raw_size += sizeof (Elf32_External_Rela);
958
959               /* If we are linking with -Bsymbolic, and this is a
960                  global symbol, we count the number of PC relative
961                  relocations we have entered for this symbol, so that
962                  we can discard them again if the symbol is later
963                  defined by a regular object.  Note that this function
964                  is only called if we are using an elf_sh linker
965                  hash table, which means that h is really a pointer to
966                  an elf32_mn10300_link_hash_entry.  */
967               if (h != NULL
968                   && (elf_mn10300_howto_table[ELF32_R_TYPE (rel->r_info)]
969                       .pc_relative))
970                 {
971                   struct elf32_mn10300_link_hash_entry *eh;
972                   struct elf_mn10300_pcrel_relocs_copied *p;
973
974                   eh = (struct elf32_mn10300_link_hash_entry *) h;
975
976                   for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
977                     if (p->section == sreloc)
978                       break;
979
980                   if (p == NULL)
981                     {
982                       p = ((struct elf_mn10300_pcrel_relocs_copied *)
983                            bfd_alloc (dynobj, sizeof *p));
984                       if (p == NULL)
985                         return FALSE;
986
987                       p->next = eh->pcrel_relocs_copied;
988                       eh->pcrel_relocs_copied = p;
989                       p->section = sreloc;
990                       p->count = 0;
991                     }
992
993                   ++p->count;
994                 }
995             }
996
997           break;
998         }
999     }
1000
1001   return TRUE;
1002 }
1003
1004 /* Return the section that should be marked against GC for a given
1005    relocation.  */
1006
1007 static asection *
1008 mn10300_elf_gc_mark_hook (sec, info, rel, h, sym)
1009      asection *sec;
1010      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1011      Elf_Internal_Rela *rel;
1012      struct elf_link_hash_entry *h;
1013      Elf_Internal_Sym *sym;
1014 {
1015   if (h != NULL)
1016     {
1017       switch (ELF32_R_TYPE (rel->r_info))
1018         {
1019         case R_MN10300_GNU_VTINHERIT:
1020         case R_MN10300_GNU_VTENTRY:
1021           break;
1022
1023         default:
1024           switch (h->root.type)
1025             {
1026             case bfd_link_hash_defined:
1027             case bfd_link_hash_defweak:
1028               return h->root.u.def.section;
1029
1030             case bfd_link_hash_common:
1031               return h->root.u.c.p->section;
1032
1033             default:
1034               break;
1035             }
1036         }
1037     }
1038   else
1039     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1040
1041   return NULL;
1042 }
1043
1044 /* Perform a relocation as part of a final link.  */
1045 static bfd_reloc_status_type
1046 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1047                                  input_section, contents, offset, value,
1048                                  addend, h, symndx, info, sym_sec, is_local)
1049      reloc_howto_type *howto;
1050      bfd *input_bfd;
1051      bfd *output_bfd ATTRIBUTE_UNUSED;
1052      asection *input_section;
1053      bfd_byte *contents;
1054      bfd_vma offset;
1055      bfd_vma value;
1056      bfd_vma addend;
1057      struct elf_link_hash_entry * h;
1058      unsigned long symndx;
1059      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1060      asection *sym_sec ATTRIBUTE_UNUSED;
1061      int is_local ATTRIBUTE_UNUSED;
1062 {
1063   unsigned long r_type = howto->type;
1064   bfd_byte *hit_data = contents + offset;
1065   bfd *      dynobj;
1066   bfd_vma *  local_got_offsets;
1067   asection * sgot;
1068   asection * splt;
1069   asection * sreloc;
1070
1071   dynobj = elf_hash_table (info)->dynobj;
1072   local_got_offsets = elf_local_got_offsets (input_bfd);
1073
1074   sgot   = NULL;
1075   splt   = NULL;
1076   sreloc = NULL;
1077
1078   switch (r_type)
1079     {
1080     case R_MN10300_NONE:
1081       return bfd_reloc_ok;
1082
1083     case R_MN10300_32:
1084       if (info->shared
1085           && (input_section->flags & SEC_ALLOC) != 0)
1086         {
1087           Elf_Internal_Rela outrel;
1088           bfd_boolean skip, relocate;
1089
1090           /* When generating a shared object, these relocations are
1091              copied into the output file to be resolved at run
1092              time.  */
1093           if (sreloc == NULL)
1094             {
1095               const char * name;
1096
1097               name = (bfd_elf_string_from_elf_section
1098                       (input_bfd,
1099                        elf_elfheader (input_bfd)->e_shstrndx,
1100                        elf_section_data (input_section)->rel_hdr.sh_name));
1101               if (name == NULL)
1102                 return FALSE;
1103
1104               BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1105                           && strcmp (bfd_get_section_name (input_bfd,
1106                                                            input_section),
1107                                      name + 5) == 0);
1108
1109               sreloc = bfd_get_section_by_name (dynobj, name);
1110               BFD_ASSERT (sreloc != NULL);
1111             }
1112
1113           skip = FALSE;
1114
1115           if (elf_section_data (input_section)->sec_info == NULL
1116               || (input_section->sec_info_type != ELF_INFO_TYPE_STABS))
1117             outrel.r_offset = offset;
1118           else
1119             {
1120               bfd_vma off;
1121
1122               off = (_bfd_stab_section_offset
1123                      (output_bfd, & elf_hash_table (info)->stab_info,
1124                       input_section,
1125                       & elf_section_data (input_section)->sec_info,
1126                       offset));
1127               if (off == (bfd_vma) -1)
1128                 skip = TRUE;
1129               outrel.r_offset = off;
1130             }
1131
1132           outrel.r_offset += (input_section->output_section->vma
1133                               + input_section->output_offset);
1134
1135           if (skip)
1136             {
1137               memset (&outrel, 0, sizeof outrel);
1138               relocate = FALSE;
1139             }
1140           else
1141             {
1142               /* h->dynindx may be -1 if this symbol was marked to
1143                  become local.  */
1144               if (h == NULL
1145                   || ((info->symbolic || h->dynindx == -1)
1146                       && (h->elf_link_hash_flags
1147                           & ELF_LINK_HASH_DEF_REGULAR) != 0))
1148                 {
1149                   relocate = TRUE;
1150                   outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1151                   outrel.r_addend = value + addend;
1152                 }
1153               else
1154                 {
1155                   BFD_ASSERT (h->dynindx != -1);
1156                   relocate = FALSE;
1157                   outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
1158                   outrel.r_addend = value + addend;
1159                 }
1160             }
1161
1162           bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1163                                      (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
1164                                                    + sreloc->reloc_count));
1165           ++sreloc->reloc_count;
1166
1167           /* If this reloc is against an external symbol, we do
1168              not want to fiddle with the addend.  Otherwise, we
1169              need to include the symbol value so that it becomes
1170              an addend for the dynamic reloc.  */
1171           if (! relocate)
1172             return bfd_reloc_ok;
1173         }
1174       value += addend;
1175       bfd_put_32 (input_bfd, value, hit_data);
1176       return bfd_reloc_ok;
1177
1178     case R_MN10300_24:
1179       value += addend;
1180
1181       if ((long) value > 0x7fffff || (long) value < -0x800000)
1182         return bfd_reloc_overflow;
1183
1184       bfd_put_8 (input_bfd, value & 0xff, hit_data);
1185       bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1186       bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1187       return bfd_reloc_ok;
1188
1189     case R_MN10300_16:
1190       value += addend;
1191
1192       if ((long) value > 0x7fff || (long) value < -0x8000)
1193         return bfd_reloc_overflow;
1194
1195       bfd_put_16 (input_bfd, value, hit_data);
1196       return bfd_reloc_ok;
1197
1198     case R_MN10300_8:
1199       value += addend;
1200
1201       if ((long) value > 0x7f || (long) value < -0x80)
1202         return bfd_reloc_overflow;
1203
1204       bfd_put_8 (input_bfd, value, hit_data);
1205       return bfd_reloc_ok;
1206
1207     case R_MN10300_PCREL8:
1208       value -= (input_section->output_section->vma
1209                 + input_section->output_offset);
1210       value -= offset;
1211       value += addend;
1212
1213       if ((long) value > 0xff || (long) value < -0x100)
1214         return bfd_reloc_overflow;
1215
1216       bfd_put_8 (input_bfd, value, hit_data);
1217       return bfd_reloc_ok;
1218
1219     case R_MN10300_PCREL16:
1220       value -= (input_section->output_section->vma
1221                 + input_section->output_offset);
1222       value -= offset;
1223       value += addend;
1224
1225       if ((long) value > 0xffff || (long) value < -0x10000)
1226         return bfd_reloc_overflow;
1227
1228       bfd_put_16 (input_bfd, value, hit_data);
1229       return bfd_reloc_ok;
1230
1231     case R_MN10300_PCREL32:
1232       if (info->shared
1233           && (input_section->flags & SEC_ALLOC) != 0
1234           && h != NULL
1235           && h->dynindx != -1
1236           && (! info->symbolic
1237               || (h->elf_link_hash_flags
1238                   & ELF_LINK_HASH_DEF_REGULAR) == 0))
1239         {
1240           Elf_Internal_Rela outrel;
1241           bfd_boolean skip;
1242
1243           /* When generating a shared object, these relocations
1244              are copied into the output file to be resolved at run
1245              time.  */
1246
1247           if (sreloc == NULL)
1248             {
1249               const char * name;
1250
1251               name = (bfd_elf_string_from_elf_section
1252                       (input_bfd,
1253                        elf_elfheader (input_bfd)->e_shstrndx,
1254                        elf_section_data (input_section)->rel_hdr.sh_name));
1255               if (name == NULL)
1256                 return FALSE;
1257
1258               BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1259                           && strcmp (bfd_get_section_name (input_bfd,
1260                                                            input_section),
1261                                      name + 5) == 0);
1262
1263               sreloc = bfd_get_section_by_name (dynobj, name);
1264               BFD_ASSERT (sreloc != NULL);
1265             }
1266
1267           skip = FALSE;
1268
1269           if (elf_section_data (input_section)->sec_info == NULL
1270               || (input_section->sec_info_type != ELF_INFO_TYPE_STABS))
1271             outrel.r_offset = offset;
1272           else
1273             {
1274               bfd_vma off;
1275
1276               off = (_bfd_stab_section_offset
1277                      (output_bfd, & elf_hash_table (info)->stab_info,
1278                       input_section,
1279                       & elf_section_data (input_section)->sec_info,
1280                       offset));
1281               if (off == (bfd_vma) -1)
1282                 skip = TRUE;
1283               outrel.r_offset = off;
1284             }
1285
1286           outrel.r_offset += (input_section->output_section->vma
1287                               + input_section->output_offset);
1288
1289           if (skip)
1290             memset (&outrel, 0, sizeof outrel);
1291           else
1292             {
1293               BFD_ASSERT (h != NULL && h->dynindx != -1);
1294               outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_PCREL32);
1295               outrel.r_addend = addend;
1296             }
1297
1298           bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1299                                      (bfd_byte *) (((Elf32_External_Rela *)
1300                                                     sreloc->contents)
1301                                                    + sreloc->reloc_count));
1302           ++sreloc->reloc_count;
1303
1304           return bfd_reloc_ok;
1305         }
1306
1307       value -= (input_section->output_section->vma
1308                 + input_section->output_offset);
1309       value -= offset;
1310       value += addend;
1311
1312       bfd_put_32 (input_bfd, value, hit_data);
1313       return bfd_reloc_ok;
1314
1315     case R_MN10300_GNU_VTINHERIT:
1316     case R_MN10300_GNU_VTENTRY:
1317       return bfd_reloc_ok;
1318
1319     case R_MN10300_GOTPC32:
1320       /* Use global offset table as symbol value.  */
1321
1322       value = bfd_get_section_by_name (dynobj,
1323                                        ".got")->output_section->vma;
1324       value -= (input_section->output_section->vma
1325                 + input_section->output_offset);
1326       value -= offset;
1327       value += addend;
1328
1329       bfd_put_32 (input_bfd, value, hit_data);
1330       return bfd_reloc_ok;
1331       
1332     case R_MN10300_GOTPC16:
1333       /* Use global offset table as symbol value.  */
1334
1335       value = bfd_get_section_by_name (dynobj,
1336                                        ".got")->output_section->vma;
1337       value -= (input_section->output_section->vma
1338                 + input_section->output_offset);
1339       value -= offset;
1340       value += addend;
1341
1342       if ((long) value > 0xffff || (long) value < -0x10000)
1343         return bfd_reloc_overflow;
1344
1345       bfd_put_16 (input_bfd, value, hit_data);
1346       return bfd_reloc_ok;
1347
1348     case R_MN10300_GOTOFF32:
1349       value -= bfd_get_section_by_name (dynobj,
1350                                         ".got")->output_section->vma;
1351       value += addend;
1352       
1353       bfd_put_32 (input_bfd, value, hit_data);
1354       return bfd_reloc_ok;
1355
1356     case R_MN10300_GOTOFF24:
1357       value -= bfd_get_section_by_name (dynobj,
1358                                         ".got")->output_section->vma;
1359       value += addend;
1360       
1361       if ((long) value > 0x7fffff || (long) value < -0x800000)
1362         return bfd_reloc_overflow;
1363
1364       bfd_put_8 (input_bfd, value, hit_data);
1365       bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1366       bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1367       return bfd_reloc_ok;
1368
1369     case R_MN10300_GOTOFF16:
1370       value -= bfd_get_section_by_name (dynobj,
1371                                         ".got")->output_section->vma;
1372       value += addend;
1373       
1374       if ((long) value > 0xffff || (long) value < -0x10000)
1375         return bfd_reloc_overflow;
1376
1377       bfd_put_16 (input_bfd, value, hit_data);
1378       return bfd_reloc_ok;
1379
1380     case R_MN10300_PLT32:
1381       if (h != NULL
1382           && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1383           && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1384           && h->plt.offset != (bfd_vma) -1)
1385         {
1386           asection * splt;
1387
1388           splt = bfd_get_section_by_name (dynobj, ".plt");
1389           
1390           value = (splt->output_section->vma
1391                    + splt->output_offset
1392                    + h->plt.offset) - value;
1393         }
1394
1395       value -= (input_section->output_section->vma
1396                 + input_section->output_offset);
1397       value -= offset;
1398       value += addend;
1399
1400       bfd_put_32 (input_bfd, value, hit_data);
1401       return bfd_reloc_ok;
1402
1403     case R_MN10300_PLT16:
1404       if (h != NULL
1405           && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1406           && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1407           && h->plt.offset != (bfd_vma) -1)
1408         {
1409           asection * splt;
1410
1411           splt = bfd_get_section_by_name (dynobj, ".plt");
1412           
1413           value = (splt->output_section->vma
1414                    + splt->output_offset
1415                    + h->plt.offset) - value;
1416         }
1417
1418       value -= (input_section->output_section->vma
1419                 + input_section->output_offset);
1420       value -= offset;
1421       value += addend;
1422
1423       if ((long) value > 0xffff || (long) value < -0x10000)
1424         return bfd_reloc_overflow;
1425
1426       bfd_put_16 (input_bfd, value, hit_data);
1427       return bfd_reloc_ok;
1428
1429     case R_MN10300_GOT32:
1430     case R_MN10300_GOT24:
1431     case R_MN10300_GOT16:
1432       {
1433         asection * sgot;
1434
1435         sgot = bfd_get_section_by_name (dynobj, ".got");
1436         
1437           if (h != NULL)
1438             {
1439               bfd_vma off;
1440
1441               off = h->got.offset;
1442               BFD_ASSERT (off != (bfd_vma) -1);
1443
1444               if (! elf_hash_table (info)->dynamic_sections_created
1445                   || (info->shared
1446                       && (info->symbolic || h->dynindx == -1)
1447                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1448                 /* This is actually a static link, or it is a
1449                    -Bsymbolic link and the symbol is defined
1450                    locally, or the symbol was forced to be local
1451                    because of a version file.  We must initialize
1452                    this entry in the global offset table.
1453
1454                    When doing a dynamic link, we create a .rela.got
1455                    relocation entry to initialize the value.  This
1456                    is done in the finish_dynamic_symbol routine.  */
1457                 bfd_put_32 (output_bfd, value,
1458                             sgot->contents + off);
1459
1460               value = sgot->output_offset + off;
1461             }
1462           else
1463             {
1464               bfd_vma off;
1465
1466               off = elf_local_got_offsets (input_bfd)[symndx];
1467
1468               bfd_put_32 (output_bfd, value, sgot->contents + off);
1469
1470               if (info->shared)
1471                 {
1472                   asection * srelgot;
1473                   Elf_Internal_Rela outrel;
1474
1475                   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1476                   BFD_ASSERT (srelgot != NULL);
1477
1478                   outrel.r_offset = (sgot->output_section->vma
1479                                      + sgot->output_offset
1480                                      + off);
1481                   outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1482                   outrel.r_addend = value;
1483                   bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1484                                              (bfd_byte *) (((Elf32_External_Rela *)
1485                                                             srelgot->contents)
1486                                                            + srelgot->reloc_count));
1487                   ++ srelgot->reloc_count;
1488                 }
1489
1490               value = sgot->output_offset + off;
1491             }
1492       }
1493
1494       value += addend;
1495
1496       if (r_type == R_MN10300_GOT32)
1497         {
1498           bfd_put_32 (input_bfd, value, hit_data);
1499           return bfd_reloc_ok;
1500         }
1501       else if (r_type == R_MN10300_GOT24)
1502         {
1503           if ((long) value > 0x7fffff || (long) value < -0x800000)
1504             return bfd_reloc_overflow;
1505
1506           bfd_put_8 (input_bfd, value & 0xff, hit_data);
1507           bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1508           bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1509           return bfd_reloc_ok;
1510         }
1511       else if (r_type == R_MN10300_GOT16)
1512         {
1513           if ((long) value > 0xffff || (long) value < -0x10000)
1514             return bfd_reloc_overflow;
1515
1516           bfd_put_16 (input_bfd, value, hit_data);
1517           return bfd_reloc_ok;
1518         }
1519       /* Fall through.  */
1520       
1521     default:
1522       return bfd_reloc_notsupported;
1523     }
1524 }
1525 \f
1526 /* Relocate an MN10300 ELF section.  */
1527 static bfd_boolean
1528 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1529                               contents, relocs, local_syms, local_sections)
1530      bfd *output_bfd;
1531      struct bfd_link_info *info;
1532      bfd *input_bfd;
1533      asection *input_section;
1534      bfd_byte *contents;
1535      Elf_Internal_Rela *relocs;
1536      Elf_Internal_Sym *local_syms;
1537      asection **local_sections;
1538 {
1539   Elf_Internal_Shdr *symtab_hdr;
1540   struct elf_link_hash_entry **sym_hashes;
1541   Elf_Internal_Rela *rel, *relend;
1542
1543   if (info->relocatable)
1544     return TRUE;
1545
1546   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1547   sym_hashes = elf_sym_hashes (input_bfd);
1548
1549   rel = relocs;
1550   relend = relocs + input_section->reloc_count;
1551   for (; rel < relend; rel++)
1552     {
1553       int r_type;
1554       reloc_howto_type *howto;
1555       unsigned long r_symndx;
1556       Elf_Internal_Sym *sym;
1557       asection *sec;
1558       struct elf32_mn10300_link_hash_entry *h;
1559       bfd_vma relocation;
1560       bfd_reloc_status_type r;
1561
1562       r_symndx = ELF32_R_SYM (rel->r_info);
1563       r_type = ELF32_R_TYPE (rel->r_info);
1564       howto = elf_mn10300_howto_table + r_type;
1565
1566       /* Just skip the vtable gc relocs.  */
1567       if (r_type == R_MN10300_GNU_VTINHERIT
1568           || r_type == R_MN10300_GNU_VTENTRY)
1569         continue;
1570
1571       h = NULL;
1572       sym = NULL;
1573       sec = NULL;
1574       if (r_symndx < symtab_hdr->sh_info)
1575         {
1576           sym = local_syms + r_symndx;
1577           sec = local_sections[r_symndx];
1578           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1579         }
1580       else
1581         {
1582           bfd_boolean unresolved_reloc;
1583           bfd_boolean warned;
1584           struct elf_link_hash_entry *hh;
1585
1586           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1587                                    r_symndx, symtab_hdr, sym_hashes,
1588                                    hh, sec, relocation,
1589                                    unresolved_reloc, warned);
1590
1591           h = (struct elf32_mn10300_link_hash_entry *) hh;
1592
1593           if ((h->root.root.type == bfd_link_hash_defined
1594               || h->root.root.type == bfd_link_hash_defweak)
1595               && (   r_type == R_MN10300_GOTPC32
1596                   || r_type == R_MN10300_GOTPC16
1597                   || ((   r_type == R_MN10300_PLT32
1598                        || r_type == R_MN10300_PLT16)
1599                       && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
1600                       && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
1601                       && h->root.plt.offset != (bfd_vma) -1)
1602                   || ((   r_type == R_MN10300_GOT32
1603                        || r_type == R_MN10300_GOT24
1604                        || r_type == R_MN10300_GOT16)
1605                       && elf_hash_table (info)->dynamic_sections_created
1606                       && (! info->shared
1607                           || (! info->symbolic && h->root.dynindx != -1)
1608                           || (h->root.elf_link_hash_flags
1609                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
1610                   || (info->shared
1611                       && ((! info->symbolic && h->root.dynindx != -1)
1612                           || (h->root.elf_link_hash_flags
1613                               & ELF_LINK_HASH_DEF_REGULAR) == 0)
1614                       && (   r_type == R_MN10300_32
1615                           || r_type == R_MN10300_PCREL32)
1616                       && ((input_section->flags & SEC_ALLOC) != 0
1617                           /* DWARF will emit R_MN10300_32 relocations
1618                              in its sections against symbols defined
1619                              externally in shared libraries.  We can't
1620                              do anything with them here.  */
1621                           || ((input_section->flags & SEC_DEBUGGING) != 0
1622                               && (h->root.elf_link_hash_flags
1623                                   & ELF_LINK_HASH_DEF_DYNAMIC) != 0)))))
1624             /* In these cases, we don't need the relocation
1625                value.  We check specially because in some
1626                obscure cases sec->output_section will be NULL.  */
1627             relocation = 0;
1628
1629           else if (unresolved_reloc)
1630             (*_bfd_error_handler)
1631               (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1632                bfd_get_filename (input_bfd), h->root.root.root.string,
1633                bfd_get_section_name (input_bfd, input_section));
1634         }
1635
1636       r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1637                                            input_section,
1638                                            contents, rel->r_offset,
1639                                            relocation, rel->r_addend,
1640                                            (struct elf_link_hash_entry *)h,
1641                                            r_symndx,
1642                                            info, sec, h == NULL);
1643
1644       if (r != bfd_reloc_ok)
1645         {
1646           const char *name;
1647           const char *msg = (const char *) 0;
1648
1649           if (h != NULL)
1650             name = h->root.root.root.string;
1651           else
1652             {
1653               name = (bfd_elf_string_from_elf_section
1654                       (input_bfd, symtab_hdr->sh_link, sym->st_name));
1655               if (name == NULL || *name == '\0')
1656                 name = bfd_section_name (input_bfd, sec);
1657             }
1658
1659           switch (r)
1660             {
1661             case bfd_reloc_overflow:
1662               if (! ((*info->callbacks->reloc_overflow)
1663                      (info, name, howto->name, (bfd_vma) 0,
1664                       input_bfd, input_section, rel->r_offset)))
1665                 return FALSE;
1666               break;
1667
1668             case bfd_reloc_undefined:
1669               if (! ((*info->callbacks->undefined_symbol)
1670                      (info, name, input_bfd, input_section,
1671                       rel->r_offset, TRUE)))
1672                 return FALSE;
1673               break;
1674
1675             case bfd_reloc_outofrange:
1676               msg = _("internal error: out of range error");
1677               goto common_error;
1678
1679             case bfd_reloc_notsupported:
1680               msg = _("internal error: unsupported relocation error");
1681               goto common_error;
1682
1683             case bfd_reloc_dangerous:
1684               msg = _("internal error: dangerous error");
1685               goto common_error;
1686
1687             default:
1688               msg = _("internal error: unknown error");
1689               /* fall through */
1690
1691             common_error:
1692               if (!((*info->callbacks->warning)
1693                     (info, msg, name, input_bfd, input_section,
1694                      rel->r_offset)))
1695                 return FALSE;
1696               break;
1697             }
1698         }
1699     }
1700
1701   return TRUE;
1702 }
1703
1704 /* Finish initializing one hash table entry.  */
1705 static bfd_boolean
1706 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
1707      struct bfd_hash_entry *gen_entry;
1708      PTR in_args;
1709 {
1710   struct elf32_mn10300_link_hash_entry *entry;
1711   struct bfd_link_info *link_info = (struct bfd_link_info *)in_args;
1712   unsigned int byte_count = 0;
1713
1714   entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1715
1716   if (entry->root.root.type == bfd_link_hash_warning)
1717     entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
1718
1719   /* If we already know we want to convert "call" to "calls" for calls
1720      to this symbol, then return now.  */
1721   if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1722     return TRUE;
1723
1724   /* If there are no named calls to this symbol, or there's nothing we
1725      can move from the function itself into the "call" instruction,
1726      then note that all "call" instructions should be converted into
1727      "calls" instructions and return.  If a symbol is available for
1728      dynamic symbol resolution (overridable or overriding), avoid
1729      custom calling conventions.  */
1730   if (entry->direct_calls == 0
1731       || (entry->stack_size == 0 && entry->movm_args == 0)
1732       || (elf_hash_table (link_info)->dynamic_sections_created
1733           && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1734           && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1735     {
1736       /* Make a note that we should convert "call" instructions to "calls"
1737          instructions for calls to this symbol.  */
1738       entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1739       return TRUE;
1740     }
1741
1742   /* We may be able to move some instructions from the function itself into
1743      the "call" instruction.  Count how many bytes we might be able to
1744      eliminate in the function itself.  */
1745
1746   /* A movm instruction is two bytes.  */
1747   if (entry->movm_args)
1748     byte_count += 2;
1749
1750   /* Count the insn to allocate stack space too.  */
1751   if (entry->stack_size > 0)
1752     {
1753       if (entry->stack_size <= 128)
1754         byte_count += 3;
1755       else
1756         byte_count += 4;
1757     }
1758
1759   /* If using "call" will result in larger code, then turn all
1760      the associated "call" instructions into "calls" instructions.  */
1761   if (byte_count < entry->direct_calls)
1762     entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1763
1764   /* This routine never fails.  */
1765   return TRUE;
1766 }
1767
1768 /* This function handles relaxing for the mn10300.
1769
1770    There are quite a few relaxing opportunities available on the mn10300:
1771
1772         * calls:32 -> calls:16                                     2 bytes
1773         * call:32  -> call:16                                      2 bytes
1774
1775         * call:32 -> calls:32                                      1 byte
1776         * call:16 -> calls:16                                      1 byte
1777                 * These are done anytime using "calls" would result
1778                 in smaller code, or when necessary to preserve the
1779                 meaning of the program.
1780
1781         * call:32                                                  varies
1782         * call:16
1783                 * In some circumstances we can move instructions
1784                 from a function prologue into a "call" instruction.
1785                 This is only done if the resulting code is no larger
1786                 than the original code.
1787
1788         * jmp:32 -> jmp:16                                         2 bytes
1789         * jmp:16 -> bra:8                                          1 byte
1790
1791                 * If the previous instruction is a conditional branch
1792                 around the jump/bra, we may be able to reverse its condition
1793                 and change its target to the jump's target.  The jump/bra
1794                 can then be deleted.                               2 bytes
1795
1796         * mov abs32 -> mov abs16                                   1 or 2 bytes
1797
1798         * Most instructions which accept imm32 can relax to imm16  1 or 2 bytes
1799         - Most instructions which accept imm16 can relax to imm8   1 or 2 bytes
1800
1801         * Most instructions which accept d32 can relax to d16      1 or 2 bytes
1802         - Most instructions which accept d16 can relax to d8       1 or 2 bytes
1803
1804         We don't handle imm16->imm8 or d16->d8 as they're very rare
1805         and somewhat more difficult to support.  */
1806
1807 static bfd_boolean
1808 mn10300_elf_relax_section (abfd, sec, link_info, again)
1809      bfd *abfd;
1810      asection *sec;
1811      struct bfd_link_info *link_info;
1812      bfd_boolean *again;
1813 {
1814   Elf_Internal_Shdr *symtab_hdr;
1815   Elf_Internal_Rela *internal_relocs = NULL;
1816   Elf_Internal_Rela *irel, *irelend;
1817   bfd_byte *contents = NULL;
1818   Elf_Internal_Sym *isymbuf = NULL;
1819   struct elf32_mn10300_link_hash_table *hash_table;
1820   asection *section = sec;
1821
1822   /* Assume nothing changes.  */
1823   *again = FALSE;
1824
1825   /* We need a pointer to the mn10300 specific hash table.  */
1826   hash_table = elf32_mn10300_hash_table (link_info);
1827
1828   /* Initialize fields in each hash table entry the first time through.  */
1829   if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
1830     {
1831       bfd *input_bfd;
1832
1833       /* Iterate over all the input bfds.  */
1834       for (input_bfd = link_info->input_bfds;
1835            input_bfd != NULL;
1836            input_bfd = input_bfd->link_next)
1837         {
1838           /* We're going to need all the symbols for each bfd.  */
1839           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1840           if (symtab_hdr->sh_info != 0)
1841             {
1842               isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1843               if (isymbuf == NULL)
1844                 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1845                                                 symtab_hdr->sh_info, 0,
1846                                                 NULL, NULL, NULL);
1847               if (isymbuf == NULL)
1848                 goto error_return;
1849             }
1850
1851           /* Iterate over each section in this bfd.  */
1852           for (section = input_bfd->sections;
1853                section != NULL;
1854                section = section->next)
1855             {
1856               struct elf32_mn10300_link_hash_entry *hash;
1857               Elf_Internal_Sym *sym;
1858               asection *sym_sec = NULL;
1859               const char *sym_name;
1860               char *new_name;
1861
1862               /* If there's nothing to do in this section, skip it.  */
1863               if (! (((section->flags & SEC_RELOC) != 0
1864                       && section->reloc_count != 0)
1865                      || (section->flags & SEC_CODE) != 0))
1866                 continue;
1867
1868               /* Get cached copy of section contents if it exists.  */
1869               if (elf_section_data (section)->this_hdr.contents != NULL)
1870                 contents = elf_section_data (section)->this_hdr.contents;
1871               else if (section->_raw_size != 0)
1872                 {
1873                   /* Go get them off disk.  */
1874                   contents = (bfd_byte *) bfd_malloc (section->_raw_size);
1875                   if (contents == NULL)
1876                     goto error_return;
1877
1878                   if (!bfd_get_section_contents (input_bfd, section,
1879                                                  contents, (file_ptr) 0,
1880                                                  section->_raw_size))
1881                     goto error_return;
1882                 }
1883               else
1884                 contents = NULL;
1885
1886               /* If there aren't any relocs, then there's nothing to do.  */
1887               if ((section->flags & SEC_RELOC) != 0
1888                   && section->reloc_count != 0)
1889                 {
1890
1891                   /* Get a copy of the native relocations.  */
1892                   internal_relocs = (_bfd_elf_link_read_relocs
1893                                      (input_bfd, section, (PTR) NULL,
1894                                       (Elf_Internal_Rela *) NULL,
1895                                       link_info->keep_memory));
1896                   if (internal_relocs == NULL)
1897                     goto error_return;
1898
1899                   /* Now examine each relocation.  */
1900                   irel = internal_relocs;
1901                   irelend = irel + section->reloc_count;
1902                   for (; irel < irelend; irel++)
1903                     {
1904                       long r_type;
1905                       unsigned long r_index;
1906                       unsigned char code;
1907
1908                       r_type = ELF32_R_TYPE (irel->r_info);
1909                       r_index = ELF32_R_SYM (irel->r_info);
1910
1911                       if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
1912                         goto error_return;
1913
1914                       /* We need the name and hash table entry of the target
1915                          symbol!  */
1916                       hash = NULL;
1917                       sym = NULL;
1918                       sym_sec = NULL;
1919
1920                       if (r_index < symtab_hdr->sh_info)
1921                         {
1922                           /* A local symbol.  */
1923                           Elf_Internal_Sym *isym;
1924                           struct elf_link_hash_table *elftab;
1925                           bfd_size_type amt;
1926
1927                           isym = isymbuf + r_index;
1928                           if (isym->st_shndx == SHN_UNDEF)
1929                             sym_sec = bfd_und_section_ptr;
1930                           else if (isym->st_shndx == SHN_ABS)
1931                             sym_sec = bfd_abs_section_ptr;
1932                           else if (isym->st_shndx == SHN_COMMON)
1933                             sym_sec = bfd_com_section_ptr;
1934                           else
1935                             sym_sec
1936                               = bfd_section_from_elf_index (input_bfd,
1937                                                             isym->st_shndx);
1938
1939                           sym_name
1940                             = bfd_elf_string_from_elf_section (input_bfd,
1941                                                                (symtab_hdr
1942                                                                 ->sh_link),
1943                                                                isym->st_name);
1944
1945                           /* If it isn't a function, then we don't care
1946                              about it.  */
1947                           if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
1948                             continue;
1949
1950                           /* Tack on an ID so we can uniquely identify this
1951                              local symbol in the global hash table.  */
1952                           amt = strlen (sym_name) + 10;
1953                           new_name = bfd_malloc (amt);
1954                           if (new_name == 0)
1955                             goto error_return;
1956
1957                           sprintf (new_name, "%s_%08x",
1958                                    sym_name, (int) sym_sec);
1959                           sym_name = new_name;
1960
1961                           elftab = &hash_table->static_hash_table->root;
1962                           hash = ((struct elf32_mn10300_link_hash_entry *)
1963                                   elf_link_hash_lookup (elftab, sym_name,
1964                                                         TRUE, TRUE, FALSE));
1965                           free (new_name);
1966                         }
1967                       else
1968                         {
1969                           r_index -= symtab_hdr->sh_info;
1970                           hash = (struct elf32_mn10300_link_hash_entry *)
1971                                    elf_sym_hashes (input_bfd)[r_index];
1972                         }
1973
1974                       /* If this is not a "call" instruction, then we
1975                          should convert "call" instructions to "calls"
1976                          instructions.  */
1977                       code = bfd_get_8 (input_bfd,
1978                                         contents + irel->r_offset - 1);
1979                       if (code != 0xdd && code != 0xcd)
1980                         hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1981
1982                       /* If this is a jump/call, then bump the
1983                          direct_calls counter.  Else force "call" to
1984                          "calls" conversions.  */
1985                       if (r_type == R_MN10300_PCREL32
1986                           || r_type == R_MN10300_PLT32
1987                           || r_type == R_MN10300_PLT16
1988                           || r_type == R_MN10300_PCREL16)
1989                         hash->direct_calls++;
1990                       else
1991                         hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1992                     }
1993                 }
1994
1995               /* Now look at the actual contents to get the stack size,
1996                  and a list of what registers were saved in the prologue
1997                  (ie movm_args).  */
1998               if ((section->flags & SEC_CODE) != 0)
1999                 {
2000                   Elf_Internal_Sym *isym, *isymend;
2001                   unsigned int sec_shndx;
2002                   struct elf_link_hash_entry **hashes;
2003                   struct elf_link_hash_entry **end_hashes;
2004                   unsigned int symcount;
2005
2006                   sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2007                                                                  section);
2008
2009                   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2010                               - symtab_hdr->sh_info);
2011                   hashes = elf_sym_hashes (input_bfd);
2012                   end_hashes = hashes + symcount;
2013
2014                   /* Look at each function defined in this section and
2015                      update info for that function.  */
2016                   isymend = isymbuf + symtab_hdr->sh_info;
2017                   for (isym = isymbuf; isym < isymend; isym++)
2018                     {
2019                       if (isym->st_shndx == sec_shndx
2020                           && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
2021                         {
2022                           struct elf_link_hash_table *elftab;
2023                           bfd_size_type amt;
2024                           struct elf_link_hash_entry **lhashes = hashes;
2025
2026                           /* Skip a local symbol if it aliases a
2027                              global one.  */
2028                           for (; lhashes < end_hashes; lhashes++)
2029                             {
2030                               hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
2031                               if ((hash->root.root.type == bfd_link_hash_defined
2032                                    || hash->root.root.type == bfd_link_hash_defweak)
2033                                   && hash->root.root.u.def.section == section
2034                                   && hash->root.type == STT_FUNC
2035                                   && hash->root.root.u.def.value == isym->st_value)
2036                                 break;
2037                             }
2038                           if (lhashes != end_hashes)
2039                             continue;
2040
2041                           if (isym->st_shndx == SHN_UNDEF)
2042                             sym_sec = bfd_und_section_ptr;
2043                           else if (isym->st_shndx == SHN_ABS)
2044                             sym_sec = bfd_abs_section_ptr;
2045                           else if (isym->st_shndx == SHN_COMMON)
2046                             sym_sec = bfd_com_section_ptr;
2047                           else
2048                             sym_sec
2049                               = bfd_section_from_elf_index (input_bfd,
2050                                                             isym->st_shndx);
2051
2052                           sym_name = (bfd_elf_string_from_elf_section
2053                                       (input_bfd, symtab_hdr->sh_link,
2054                                        isym->st_name));
2055
2056                           /* Tack on an ID so we can uniquely identify this
2057                              local symbol in the global hash table.  */
2058                           amt = strlen (sym_name) + 10;
2059                           new_name = bfd_malloc (amt);
2060                           if (new_name == 0)
2061                             goto error_return;
2062
2063                           sprintf (new_name, "%s_%08x",
2064                                    sym_name, (int) sym_sec);
2065                           sym_name = new_name;
2066
2067                           elftab = &hash_table->static_hash_table->root;
2068                           hash = ((struct elf32_mn10300_link_hash_entry *)
2069                                   elf_link_hash_lookup (elftab, sym_name,
2070                                                         TRUE, TRUE, FALSE));
2071                           free (new_name);
2072                           compute_function_info (input_bfd, hash,
2073                                                  isym->st_value, contents);
2074                         }
2075                     }
2076
2077                   for (; hashes < end_hashes; hashes++)
2078                     {
2079                       hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2080                       if ((hash->root.root.type == bfd_link_hash_defined
2081                            || hash->root.root.type == bfd_link_hash_defweak)
2082                           && hash->root.root.u.def.section == section
2083                           && hash->root.type == STT_FUNC)
2084                         compute_function_info (input_bfd, hash,
2085                                                (hash)->root.root.u.def.value,
2086                                                contents);
2087                     }
2088                 }
2089
2090               /* Cache or free any memory we allocated for the relocs.  */
2091               if (internal_relocs != NULL
2092                   && elf_section_data (section)->relocs != internal_relocs)
2093                 free (internal_relocs);
2094               internal_relocs = NULL;
2095
2096               /* Cache or free any memory we allocated for the contents.  */
2097               if (contents != NULL
2098                   && elf_section_data (section)->this_hdr.contents != contents)
2099                 {
2100                   if (! link_info->keep_memory)
2101                     free (contents);
2102                   else
2103                     {
2104                       /* Cache the section contents for elf_link_input_bfd.  */
2105                       elf_section_data (section)->this_hdr.contents = contents;
2106                     }
2107                 }
2108               contents = NULL;
2109             }
2110
2111           /* Cache or free any memory we allocated for the symbols.  */
2112           if (isymbuf != NULL
2113               && symtab_hdr->contents != (unsigned char *) isymbuf)
2114             {
2115               if (! link_info->keep_memory)
2116                 free (isymbuf);
2117               else
2118                 {
2119                   /* Cache the symbols for elf_link_input_bfd.  */
2120                   symtab_hdr->contents = (unsigned char *) isymbuf;
2121                 }
2122             }
2123           isymbuf = NULL;
2124         }
2125
2126       /* Now iterate on each symbol in the hash table and perform
2127          the final initialization steps on each.  */
2128       elf32_mn10300_link_hash_traverse (hash_table,
2129                                         elf32_mn10300_finish_hash_table_entry,
2130                                         link_info);
2131       elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2132                                         elf32_mn10300_finish_hash_table_entry,
2133                                         link_info);
2134
2135       /* All entries in the hash table are fully initialized.  */
2136       hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
2137
2138       /* Now that everything has been initialized, go through each
2139          code section and delete any prologue insns which will be
2140          redundant because their operations will be performed by
2141          a "call" instruction.  */
2142       for (input_bfd = link_info->input_bfds;
2143            input_bfd != NULL;
2144            input_bfd = input_bfd->link_next)
2145         {
2146           /* We're going to need all the local symbols for each bfd.  */
2147           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2148           if (symtab_hdr->sh_info != 0)
2149             {
2150               isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2151               if (isymbuf == NULL)
2152                 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2153                                                 symtab_hdr->sh_info, 0,
2154                                                 NULL, NULL, NULL);
2155               if (isymbuf == NULL)
2156                 goto error_return;
2157             }
2158
2159           /* Walk over each section in this bfd.  */
2160           for (section = input_bfd->sections;
2161                section != NULL;
2162                section = section->next)
2163             {
2164               unsigned int sec_shndx;
2165               Elf_Internal_Sym *isym, *isymend;
2166               struct elf_link_hash_entry **hashes;
2167               struct elf_link_hash_entry **end_hashes;
2168               unsigned int symcount;
2169
2170               /* Skip non-code sections and empty sections.  */
2171               if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
2172                 continue;
2173
2174               if (section->reloc_count != 0)
2175                 {
2176                   /* Get a copy of the native relocations.  */
2177                   internal_relocs = (_bfd_elf_link_read_relocs
2178                                      (input_bfd, section, (PTR) NULL,
2179                                       (Elf_Internal_Rela *) NULL,
2180                                       link_info->keep_memory));
2181                   if (internal_relocs == NULL)
2182                     goto error_return;
2183                 }
2184
2185               /* Get cached copy of section contents if it exists.  */
2186               if (elf_section_data (section)->this_hdr.contents != NULL)
2187                 contents = elf_section_data (section)->this_hdr.contents;
2188               else
2189                 {
2190                   /* Go get them off disk.  */
2191                   contents = (bfd_byte *) bfd_malloc (section->_raw_size);
2192                   if (contents == NULL)
2193                     goto error_return;
2194
2195                   if (!bfd_get_section_contents (input_bfd, section,
2196                                                  contents, (file_ptr) 0,
2197                                                  section->_raw_size))
2198                     goto error_return;
2199                 }
2200
2201               sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2202                                                              section);
2203
2204               /* Now look for any function in this section which needs
2205                  insns deleted from its prologue.  */
2206               isymend = isymbuf + symtab_hdr->sh_info;
2207               for (isym = isymbuf; isym < isymend; isym++)
2208                 {
2209                   struct elf32_mn10300_link_hash_entry *sym_hash;
2210                   asection *sym_sec = NULL;
2211                   const char *sym_name;
2212                   char *new_name;
2213                   struct elf_link_hash_table *elftab;
2214                   bfd_size_type amt;
2215
2216                   if (isym->st_shndx != sec_shndx)
2217                     continue;
2218
2219                   if (isym->st_shndx == SHN_UNDEF)
2220                     sym_sec = bfd_und_section_ptr;
2221                   else if (isym->st_shndx == SHN_ABS)
2222                     sym_sec = bfd_abs_section_ptr;
2223                   else if (isym->st_shndx == SHN_COMMON)
2224                     sym_sec = bfd_com_section_ptr;
2225                   else
2226                     sym_sec
2227                       = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2228
2229                   sym_name
2230                     = bfd_elf_string_from_elf_section (input_bfd,
2231                                                        symtab_hdr->sh_link,
2232                                                        isym->st_name);
2233
2234                   /* Tack on an ID so we can uniquely identify this
2235                      local symbol in the global hash table.  */
2236                   amt = strlen (sym_name) + 10;
2237                   new_name = bfd_malloc (amt);
2238                   if (new_name == 0)
2239                     goto error_return;
2240                   sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
2241                   sym_name = new_name;
2242
2243                   elftab = &hash_table->static_hash_table->root;
2244                   sym_hash = ((struct elf32_mn10300_link_hash_entry *)
2245                               elf_link_hash_lookup (elftab, sym_name,
2246                                                     FALSE, FALSE, FALSE));
2247
2248                   free (new_name);
2249                   if (sym_hash == NULL)
2250                     continue;
2251
2252                   if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2253                       && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2254                     {
2255                       int bytes = 0;
2256
2257                       /* Note that we've changed things.  */
2258                       elf_section_data (section)->relocs = internal_relocs;
2259                       elf_section_data (section)->this_hdr.contents = contents;
2260                       symtab_hdr->contents = (unsigned char *) isymbuf;
2261
2262                       /* Count how many bytes we're going to delete.  */
2263                       if (sym_hash->movm_args)
2264                         bytes += 2;
2265
2266                       if (sym_hash->stack_size > 0)
2267                         {
2268                           if (sym_hash->stack_size <= 128)
2269                             bytes += 3;
2270                           else
2271                             bytes += 4;
2272                         }
2273
2274                       /* Note that we've deleted prologue bytes for this
2275                          function.  */
2276                       sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2277
2278                       /* Actually delete the bytes.  */
2279                       if (!mn10300_elf_relax_delete_bytes (input_bfd,
2280                                                            section,
2281                                                            isym->st_value,
2282                                                            bytes))
2283                         goto error_return;
2284
2285                       /* Something changed.  Not strictly necessary, but
2286                          may lead to more relaxing opportunities.  */
2287                       *again = TRUE;
2288                     }
2289                 }
2290
2291               /* Look for any global functions in this section which
2292                  need insns deleted from their prologues.  */
2293               symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2294                           - symtab_hdr->sh_info);
2295               hashes = elf_sym_hashes (input_bfd);
2296               end_hashes = hashes + symcount;
2297               for (; hashes < end_hashes; hashes++)
2298                 {
2299                   struct elf32_mn10300_link_hash_entry *sym_hash;
2300
2301                   sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2302                   if ((sym_hash->root.root.type == bfd_link_hash_defined
2303                        || sym_hash->root.root.type == bfd_link_hash_defweak)
2304                       && sym_hash->root.root.u.def.section == section
2305                       && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2306                       && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2307                     {
2308                       int bytes = 0;
2309                       bfd_vma symval;
2310
2311                       /* Note that we've changed things.  */
2312                       elf_section_data (section)->relocs = internal_relocs;
2313                       elf_section_data (section)->this_hdr.contents = contents;
2314                       symtab_hdr->contents = (unsigned char *) isymbuf;
2315
2316                       /* Count how many bytes we're going to delete.  */
2317                       if (sym_hash->movm_args)
2318                         bytes += 2;
2319
2320                       if (sym_hash->stack_size > 0)
2321                         {
2322                           if (sym_hash->stack_size <= 128)
2323                             bytes += 3;
2324                           else
2325                             bytes += 4;
2326                         }
2327
2328                       /* Note that we've deleted prologue bytes for this
2329                          function.  */
2330                       sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2331
2332                       /* Actually delete the bytes.  */
2333                       symval = sym_hash->root.root.u.def.value;
2334                       if (!mn10300_elf_relax_delete_bytes (input_bfd,
2335                                                            section,
2336                                                            symval,
2337                                                            bytes))
2338                         goto error_return;
2339
2340                       /* Something changed.  Not strictly necessary, but
2341                          may lead to more relaxing opportunities.  */
2342                       *again = TRUE;
2343                     }
2344                 }
2345
2346               /* Cache or free any memory we allocated for the relocs.  */
2347               if (internal_relocs != NULL
2348                   && elf_section_data (section)->relocs != internal_relocs)
2349                 free (internal_relocs);
2350               internal_relocs = NULL;
2351
2352               /* Cache or free any memory we allocated for the contents.  */
2353               if (contents != NULL
2354                   && elf_section_data (section)->this_hdr.contents != contents)
2355                 {
2356                   if (! link_info->keep_memory)
2357                     free (contents);
2358                   else
2359                     {
2360                       /* Cache the section contents for elf_link_input_bfd.  */
2361                       elf_section_data (section)->this_hdr.contents = contents;
2362                     }
2363                 }
2364               contents = NULL;
2365             }
2366
2367           /* Cache or free any memory we allocated for the symbols.  */
2368           if (isymbuf != NULL
2369               && symtab_hdr->contents != (unsigned char *) isymbuf)
2370             {
2371               if (! link_info->keep_memory)
2372                 free (isymbuf);
2373               else
2374                 {
2375                   /* Cache the symbols for elf_link_input_bfd.  */
2376                   symtab_hdr->contents = (unsigned char *) isymbuf;
2377                 }
2378             }
2379           isymbuf = NULL;
2380         }
2381     }
2382
2383   /* (Re)initialize for the basic instruction shortening/relaxing pass.  */
2384   contents = NULL;
2385   internal_relocs = NULL;
2386   isymbuf = NULL;
2387   /* For error_return.  */
2388   section = sec;
2389
2390   /* We don't have to do anything for a relocatable link, if
2391      this section does not have relocs, or if this is not a
2392      code section.  */
2393   if (link_info->relocatable
2394       || (sec->flags & SEC_RELOC) == 0
2395       || sec->reloc_count == 0
2396       || (sec->flags & SEC_CODE) == 0)
2397     return TRUE;
2398
2399   /* If this is the first time we have been called for this section,
2400      initialize the cooked size.  */
2401   if (sec->_cooked_size == 0)
2402     sec->_cooked_size = sec->_raw_size;
2403
2404   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2405
2406   /* Get a copy of the native relocations.  */
2407   internal_relocs = (_bfd_elf_link_read_relocs
2408                      (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2409                       link_info->keep_memory));
2410   if (internal_relocs == NULL)
2411     goto error_return;
2412
2413   /* Walk through them looking for relaxing opportunities.  */
2414   irelend = internal_relocs + sec->reloc_count;
2415   for (irel = internal_relocs; irel < irelend; irel++)
2416     {
2417       bfd_vma symval;
2418       struct elf32_mn10300_link_hash_entry *h = NULL;
2419
2420       /* If this isn't something that can be relaxed, then ignore
2421          this reloc.  */
2422       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2423           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2424           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2425         continue;
2426
2427       /* Get the section contents if we haven't done so already.  */
2428       if (contents == NULL)
2429         {
2430           /* Get cached copy if it exists.  */
2431           if (elf_section_data (sec)->this_hdr.contents != NULL)
2432             contents = elf_section_data (sec)->this_hdr.contents;
2433           else
2434             {
2435               /* Go get them off disk.  */
2436               contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
2437               if (contents == NULL)
2438                 goto error_return;
2439
2440               if (! bfd_get_section_contents (abfd, sec, contents,
2441                                               (file_ptr) 0, sec->_raw_size))
2442                 goto error_return;
2443             }
2444         }
2445
2446       /* Read this BFD's symbols if we haven't done so already.  */
2447       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2448         {
2449           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2450           if (isymbuf == NULL)
2451             isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2452                                             symtab_hdr->sh_info, 0,
2453                                             NULL, NULL, NULL);
2454           if (isymbuf == NULL)
2455             goto error_return;
2456         }
2457
2458       /* Get the value of the symbol referred to by the reloc.  */
2459       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2460         {
2461           Elf_Internal_Sym *isym;
2462           asection *sym_sec = NULL;
2463           const char *sym_name;
2464           char *new_name;
2465           bfd_vma saved_addend;
2466
2467           /* A local symbol.  */
2468           isym = isymbuf + ELF32_R_SYM (irel->r_info);
2469           if (isym->st_shndx == SHN_UNDEF)
2470             sym_sec = bfd_und_section_ptr;
2471           else if (isym->st_shndx == SHN_ABS)
2472             sym_sec = bfd_abs_section_ptr;
2473           else if (isym->st_shndx == SHN_COMMON)
2474             sym_sec = bfd_com_section_ptr;
2475           else
2476             sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2477
2478           sym_name = bfd_elf_string_from_elf_section (abfd,
2479                                                       symtab_hdr->sh_link,
2480                                                       isym->st_name);
2481
2482           if ((sym_sec->flags & SEC_MERGE)
2483               && ELF_ST_TYPE (isym->st_info) == STT_SECTION
2484               && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2485             {
2486               saved_addend = irel->r_addend;
2487               symval = _bfd_elf_rela_local_sym (abfd, isym, &sym_sec, irel);
2488               symval += irel->r_addend;
2489               irel->r_addend = saved_addend;
2490             }
2491           else
2492             {
2493               symval = (isym->st_value
2494                         + sym_sec->output_section->vma
2495                         + sym_sec->output_offset);
2496             }
2497           /* Tack on an ID so we can uniquely identify this
2498              local symbol in the global hash table.  */
2499           new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2500           if (new_name == 0)
2501             goto error_return;
2502           sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
2503           sym_name = new_name;
2504
2505           h = (struct elf32_mn10300_link_hash_entry *)
2506                 elf_link_hash_lookup (&hash_table->static_hash_table->root,
2507                                       sym_name, FALSE, FALSE, FALSE);
2508           free (new_name);
2509         }
2510       else
2511         {
2512           unsigned long indx;
2513
2514           /* An external symbol.  */
2515           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2516           h = (struct elf32_mn10300_link_hash_entry *)
2517                 (elf_sym_hashes (abfd)[indx]);
2518           BFD_ASSERT (h != NULL);
2519           if (h->root.root.type != bfd_link_hash_defined
2520               && h->root.root.type != bfd_link_hash_defweak)
2521             {
2522               /* This appears to be a reference to an undefined
2523                 symbol.  Just ignore it--it will be caught by the
2524                 regular reloc processing.  */
2525               continue;
2526             }
2527
2528           symval = (h->root.root.u.def.value
2529                     + h->root.root.u.def.section->output_section->vma
2530                     + h->root.root.u.def.section->output_offset);
2531         }
2532
2533       /* For simplicity of coding, we are going to modify the section
2534          contents, the section relocs, and the BFD symbol table.  We
2535          must tell the rest of the code not to free up this
2536          information.  It would be possible to instead create a table
2537          of changes which have to be made, as is done in coff-mips.c;
2538          that would be more work, but would require less memory when
2539          the linker is run.  */
2540
2541       /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2542          branch/call, also deal with "call" -> "calls" conversions and
2543          insertion of prologue data into "call" instructions.  */
2544       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2545           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2546         {
2547           bfd_vma value = symval;
2548
2549           if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2550               && h != NULL
2551               && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2552               && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2553               && h->root.plt.offset != (bfd_vma) -1)
2554             {
2555               asection * splt;
2556
2557               splt = bfd_get_section_by_name (elf_hash_table (link_info)
2558                                               ->dynobj, ".plt");
2559           
2560               value = ((splt->output_section->vma
2561                         + splt->output_offset
2562                         + h->root.plt.offset)
2563                        - (sec->output_section->vma
2564                           + sec->output_offset
2565                           + irel->r_offset));
2566             }
2567
2568           /* If we've got a "call" instruction that needs to be turned
2569              into a "calls" instruction, do so now.  It saves a byte.  */
2570           if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2571             {
2572               unsigned char code;
2573
2574               /* Get the opcode.  */
2575               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2576
2577               /* Make sure we're working with a "call" instruction!  */
2578               if (code == 0xdd)
2579                 {
2580                   /* Note that we've changed the relocs, section contents,
2581                      etc.  */
2582                   elf_section_data (sec)->relocs = internal_relocs;
2583                   elf_section_data (sec)->this_hdr.contents = contents;
2584                   symtab_hdr->contents = (unsigned char *) isymbuf;
2585
2586                   /* Fix the opcode.  */
2587                   bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2588                   bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2589
2590                   /* Fix irel->r_offset and irel->r_addend.  */
2591                   irel->r_offset += 1;
2592                   irel->r_addend += 1;
2593
2594                   /* Delete one byte of data.  */
2595                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2596                                                        irel->r_offset + 3, 1))
2597                     goto error_return;
2598
2599                   /* That will change things, so, we should relax again.
2600                      Note that this is not required, and it may be slow.  */
2601                   *again = TRUE;
2602                 }
2603             }
2604           else if (h)
2605             {
2606               /* We've got a "call" instruction which needs some data
2607                  from target function filled in.  */
2608               unsigned char code;
2609
2610               /* Get the opcode.  */
2611               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2612
2613               /* Insert data from the target function into the "call"
2614                  instruction if needed.  */
2615               if (code == 0xdd)
2616                 {
2617                   bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2618                   bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2619                              contents + irel->r_offset + 5);
2620                 }
2621             }
2622
2623           /* Deal with pc-relative gunk.  */
2624           value -= (sec->output_section->vma + sec->output_offset);
2625           value -= irel->r_offset;
2626           value += irel->r_addend;
2627
2628           /* See if the value will fit in 16 bits, note the high value is
2629              0x7fff + 2 as the target will be two bytes closer if we are
2630              able to relax.  */
2631           if ((long) value < 0x8001 && (long) value > -0x8000)
2632             {
2633               unsigned char code;
2634
2635               /* Get the opcode.  */
2636               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2637
2638               if (code != 0xdc && code != 0xdd && code != 0xff)
2639                 continue;
2640
2641               /* Note that we've changed the relocs, section contents, etc.  */
2642               elf_section_data (sec)->relocs = internal_relocs;
2643               elf_section_data (sec)->this_hdr.contents = contents;
2644               symtab_hdr->contents = (unsigned char *) isymbuf;
2645
2646               /* Fix the opcode.  */
2647               if (code == 0xdc)
2648                 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2649               else if (code == 0xdd)
2650                 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2651               else if (code == 0xff)
2652                 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2653
2654               /* Fix the relocation's type.  */
2655               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2656                                            (ELF32_R_TYPE (irel->r_info)
2657                                             == (int) R_MN10300_PLT32)
2658                                            ? R_MN10300_PLT16 :
2659                                            R_MN10300_PCREL16);
2660
2661               /* Delete two bytes of data.  */
2662               if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2663                                                    irel->r_offset + 1, 2))
2664                 goto error_return;
2665
2666               /* That will change things, so, we should relax again.
2667                  Note that this is not required, and it may be slow.  */
2668               *again = TRUE;
2669             }
2670         }
2671
2672       /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2673          branch.  */
2674       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
2675         {
2676           bfd_vma value = symval;
2677
2678           /* If we've got a "call" instruction that needs to be turned
2679              into a "calls" instruction, do so now.  It saves a byte.  */
2680           if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2681             {
2682               unsigned char code;
2683
2684               /* Get the opcode.  */
2685               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2686
2687               /* Make sure we're working with a "call" instruction!  */
2688               if (code == 0xcd)
2689                 {
2690                   /* Note that we've changed the relocs, section contents,
2691                      etc.  */
2692                   elf_section_data (sec)->relocs = internal_relocs;
2693                   elf_section_data (sec)->this_hdr.contents = contents;
2694                   symtab_hdr->contents = (unsigned char *) isymbuf;
2695
2696                   /* Fix the opcode.  */
2697                   bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
2698                   bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2699
2700                   /* Fix irel->r_offset and irel->r_addend.  */
2701                   irel->r_offset += 1;
2702                   irel->r_addend += 1;
2703
2704                   /* Delete one byte of data.  */
2705                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2706                                                        irel->r_offset + 1, 1))
2707                     goto error_return;
2708
2709                   /* That will change things, so, we should relax again.
2710                      Note that this is not required, and it may be slow.  */
2711                   *again = TRUE;
2712                 }
2713             }
2714           else if (h)
2715             {
2716               unsigned char code;
2717
2718               /* Get the opcode.  */
2719               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2720
2721               /* Insert data from the target function into the "call"
2722                  instruction if needed.  */
2723               if (code == 0xcd)
2724                 {
2725                   bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
2726                   bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2727                              contents + irel->r_offset + 3);
2728                 }
2729             }
2730
2731           /* Deal with pc-relative gunk.  */
2732           value -= (sec->output_section->vma + sec->output_offset);
2733           value -= irel->r_offset;
2734           value += irel->r_addend;
2735
2736           /* See if the value will fit in 8 bits, note the high value is
2737              0x7f + 1 as the target will be one bytes closer if we are
2738              able to relax.  */
2739           if ((long) value < 0x80 && (long) value > -0x80)
2740             {
2741               unsigned char code;
2742
2743               /* Get the opcode.  */
2744               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2745
2746               if (code != 0xcc)
2747                 continue;
2748
2749               /* Note that we've changed the relocs, section contents, etc.  */
2750               elf_section_data (sec)->relocs = internal_relocs;
2751               elf_section_data (sec)->this_hdr.contents = contents;
2752               symtab_hdr->contents = (unsigned char *) isymbuf;
2753
2754               /* Fix the opcode.  */
2755               bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
2756
2757               /* Fix the relocation's type.  */
2758               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2759                                            R_MN10300_PCREL8);
2760
2761               /* Delete one byte of data.  */
2762               if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2763                                                    irel->r_offset + 1, 1))
2764                 goto error_return;
2765
2766               /* That will change things, so, we should relax again.
2767                  Note that this is not required, and it may be slow.  */
2768               *again = TRUE;
2769             }
2770         }
2771
2772       /* Try to eliminate an unconditional 8 bit pc-relative branch
2773          which immediately follows a conditional 8 bit pc-relative
2774          branch around the unconditional branch.
2775
2776             original:           new:
2777             bCC lab1            bCC' lab2
2778             bra lab2
2779            lab1:               lab1:
2780
2781          This happens when the bCC can't reach lab2 at assembly time,
2782          but due to other relaxations it can reach at link time.  */
2783       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
2784         {
2785           Elf_Internal_Rela *nrel;
2786           bfd_vma value = symval;
2787           unsigned char code;
2788
2789           /* Deal with pc-relative gunk.  */
2790           value -= (sec->output_section->vma + sec->output_offset);
2791           value -= irel->r_offset;
2792           value += irel->r_addend;
2793
2794           /* Do nothing if this reloc is the last byte in the section.  */
2795           if (irel->r_offset == sec->_cooked_size)
2796             continue;
2797
2798           /* See if the next instruction is an unconditional pc-relative
2799              branch, more often than not this test will fail, so we
2800              test it first to speed things up.  */
2801           code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2802           if (code != 0xca)
2803             continue;
2804
2805           /* Also make sure the next relocation applies to the next
2806              instruction and that it's a pc-relative 8 bit branch.  */
2807           nrel = irel + 1;
2808           if (nrel == irelend
2809               || irel->r_offset + 2 != nrel->r_offset
2810               || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
2811             continue;
2812
2813           /* Make sure our destination immediately follows the
2814              unconditional branch.  */
2815           if (symval != (sec->output_section->vma + sec->output_offset
2816                          + irel->r_offset + 3))
2817             continue;
2818
2819           /* Now make sure we are a conditional branch.  This may not
2820              be necessary, but why take the chance.
2821
2822              Note these checks assume that R_MN10300_PCREL8 relocs
2823              only occur on bCC and bCCx insns.  If they occured
2824              elsewhere, we'd need to know the start of this insn
2825              for this check to be accurate.  */
2826           code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2827           if (code != 0xc0 && code != 0xc1 && code != 0xc2
2828               && code != 0xc3 && code != 0xc4 && code != 0xc5
2829               && code != 0xc6 && code != 0xc7 && code != 0xc8
2830               && code != 0xc9 && code != 0xe8 && code != 0xe9
2831               && code != 0xea && code != 0xeb)
2832             continue;
2833
2834           /* We also have to be sure there is no symbol/label
2835              at the unconditional branch.  */
2836           if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
2837                                             irel->r_offset + 1))
2838             continue;
2839
2840           /* Note that we've changed the relocs, section contents, etc.  */
2841           elf_section_data (sec)->relocs = internal_relocs;
2842           elf_section_data (sec)->this_hdr.contents = contents;
2843           symtab_hdr->contents = (unsigned char *) isymbuf;
2844
2845           /* Reverse the condition of the first branch.  */
2846           switch (code)
2847             {
2848             case 0xc8:
2849               code = 0xc9;
2850               break;
2851             case 0xc9:
2852               code = 0xc8;
2853               break;
2854             case 0xc0:
2855               code = 0xc2;
2856               break;
2857             case 0xc2:
2858               code = 0xc0;
2859               break;
2860             case 0xc3:
2861               code = 0xc1;
2862               break;
2863             case 0xc1:
2864               code = 0xc3;
2865               break;
2866             case 0xc4:
2867               code = 0xc6;
2868               break;
2869             case 0xc6:
2870               code = 0xc4;
2871               break;
2872             case 0xc7:
2873               code = 0xc5;
2874               break;
2875             case 0xc5:
2876               code = 0xc7;
2877               break;
2878             case 0xe8:
2879               code = 0xe9;
2880               break;
2881             case 0x9d:
2882               code = 0xe8;
2883               break;
2884             case 0xea:
2885               code = 0xeb;
2886               break;
2887             case 0xeb:
2888               code = 0xea;
2889               break;
2890             }
2891           bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2892
2893           /* Set the reloc type and symbol for the first branch
2894              from the second branch.  */
2895           irel->r_info = nrel->r_info;
2896
2897           /* Make the reloc for the second branch a null reloc.  */
2898           nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
2899                                        R_MN10300_NONE);
2900
2901           /* Delete two bytes of data.  */
2902           if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2903                                                irel->r_offset + 1, 2))
2904             goto error_return;
2905
2906           /* That will change things, so, we should relax again.
2907              Note that this is not required, and it may be slow.  */
2908           *again = TRUE;
2909         }
2910
2911       /* Try to turn a 24 immediate, displacement or absolute address
2912          into a 8 immediate, displacement or absolute address.  */
2913       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
2914         {
2915           bfd_vma value = symval;
2916           value += irel->r_addend;
2917
2918           /* See if the value will fit in 8 bits.  */
2919           if ((long) value < 0x7f && (long) value > -0x80)
2920             {
2921               unsigned char code;
2922
2923               /* AM33 insns which have 24 operands are 6 bytes long and
2924                  will have 0xfd as the first byte.  */
2925
2926               /* Get the first opcode.  */
2927               code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2928
2929               if (code == 0xfd)
2930                 {
2931                   /* Get the second opcode.  */
2932                   code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2933
2934                   /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2935                      equivalent instructions exists.  */
2936                   if (code != 0x6b && code != 0x7b
2937                       && code != 0x8b && code != 0x9b
2938                       && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2939                           || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2940                           || (code & 0x0f) == 0x0e))
2941                     {
2942                       /* Not safe if the high bit is on as relaxing may
2943                          move the value out of high mem and thus not fit
2944                          in a signed 8bit value.  This is currently over
2945                          conservative.  */
2946                       if ((value & 0x80) == 0)
2947                         {
2948                           /* Note that we've changed the relocation contents,
2949                              etc.  */
2950                           elf_section_data (sec)->relocs = internal_relocs;
2951                           elf_section_data (sec)->this_hdr.contents = contents;
2952                           symtab_hdr->contents = (unsigned char *) isymbuf;
2953
2954                           /* Fix the opcode.  */
2955                           bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
2956                           bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2957
2958                           /* Fix the relocation's type.  */
2959                           irel->r_info =
2960                             ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2961                                           R_MN10300_8);
2962
2963                           /* Delete two bytes of data.  */
2964                           if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2965                                                                irel->r_offset + 1, 2))
2966                             goto error_return;
2967
2968                           /* That will change things, so, we should relax
2969                              again.  Note that this is not required, and it
2970                              may be slow.  */
2971                           *again = TRUE;
2972                           break;
2973                         }
2974                     }
2975                 }
2976             }
2977         }
2978
2979       /* Try to turn a 32bit immediate, displacement or absolute address
2980          into a 16bit immediate, displacement or absolute address.  */
2981       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
2982           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
2983           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32
2984           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
2985         {
2986           bfd_vma value = symval;
2987
2988           if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
2989             {
2990               asection * sgot;
2991
2992               sgot = bfd_get_section_by_name (elf_hash_table (link_info)
2993                                               ->dynobj, ".got");
2994
2995               if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
2996                 {
2997                   value = sgot->output_offset;
2998
2999                   if (h)
3000                     value += h->root.got.offset;
3001                   else
3002                     value += (elf_local_got_offsets
3003                               (abfd)[ELF32_R_SYM (irel->r_info)]);
3004                 }
3005               else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3006                 value -= sgot->output_section->vma;
3007               else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
3008                 value = (sgot->output_section->vma
3009                          - (sec->output_section->vma
3010                             + sec->output_offset
3011                             + irel->r_offset));
3012               else
3013                 abort ();
3014             }
3015
3016           value += irel->r_addend;
3017
3018           /* See if the value will fit in 24 bits.
3019              We allow any 16bit match here.  We prune those we can't
3020              handle below.  */
3021           if ((long) value < 0x7fffff && (long) value > -0x800000)
3022             {
3023               unsigned char code;
3024
3025               /* AM33 insns which have 32bit operands are 7 bytes long and
3026                  will have 0xfe as the first byte.  */
3027
3028               /* Get the first opcode.  */
3029               code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3030
3031               if (code == 0xfe)
3032                 {
3033                   /* Get the second opcode.  */
3034                   code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3035
3036                   /* All the am33 32 -> 24 relaxing possibilities.  */
3037                   /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3038                      equivalent instructions exists.  */
3039                   if (code != 0x6b && code != 0x7b
3040                       && code != 0x8b && code != 0x9b
3041                       && (ELF32_R_TYPE (irel->r_info)
3042                           != (int) R_MN10300_GOTPC32)
3043                       && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3044                           || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3045                           || (code & 0x0f) == 0x0e))
3046                     {
3047                       /* Not safe if the high bit is on as relaxing may
3048                          move the value out of high mem and thus not fit
3049                          in a signed 16bit value.  This is currently over
3050                          conservative.  */
3051                       if ((value & 0x8000) == 0)
3052                         {
3053                           /* Note that we've changed the relocation contents,
3054                              etc.  */
3055                           elf_section_data (sec)->relocs = internal_relocs;
3056                           elf_section_data (sec)->this_hdr.contents = contents;
3057                           symtab_hdr->contents = (unsigned char *) isymbuf;
3058
3059                           /* Fix the opcode.  */
3060                           bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
3061                           bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3062
3063                           /* Fix the relocation's type.  */
3064                           irel->r_info =
3065                             ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3066                                           (ELF32_R_TYPE (irel->r_info)
3067                                            == (int) R_MN10300_GOTOFF32)
3068                                           ? R_MN10300_GOTOFF24
3069                                           : (ELF32_R_TYPE (irel->r_info)
3070                                              == (int) R_MN10300_GOT32)
3071                                           ? R_MN10300_GOT24 :
3072                                           R_MN10300_24);
3073
3074                           /* Delete one byte of data.  */
3075                           if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3076                                                                irel->r_offset + 3, 1))
3077                             goto error_return;
3078
3079                           /* That will change things, so, we should relax
3080                              again.  Note that this is not required, and it
3081                              may be slow.  */
3082                           *again = TRUE;
3083                           break;
3084                         }
3085                     }
3086                 }
3087             }
3088
3089           /* See if the value will fit in 16 bits.
3090              We allow any 16bit match here.  We prune those we can't
3091              handle below.  */
3092           if ((long) value < 0x7fff && (long) value > -0x8000)
3093             {
3094               unsigned char code;
3095
3096               /* Most insns which have 32bit operands are 6 bytes long;
3097                  exceptions are pcrel insns and bit insns.
3098
3099                  We handle pcrel insns above.  We don't bother trying
3100                  to handle the bit insns here.
3101
3102                  The first byte of the remaining insns will be 0xfc.  */
3103
3104               /* Get the first opcode.  */
3105               code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3106
3107               if (code != 0xfc)
3108                 continue;
3109
3110               /* Get the second opcode.  */
3111               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3112
3113               if ((code & 0xf0) < 0x80)
3114                 switch (code & 0xf0)
3115                   {
3116                   /* mov (d32,am),dn   -> mov (d32,am),dn
3117                      mov dm,(d32,am)   -> mov dn,(d32,am)
3118                      mov (d32,am),an   -> mov (d32,am),an
3119                      mov dm,(d32,am)   -> mov dn,(d32,am)
3120                      movbu (d32,am),dn -> movbu (d32,am),dn
3121                      movbu dm,(d32,am) -> movbu dn,(d32,am)
3122                      movhu (d32,am),dn -> movhu (d32,am),dn
3123                      movhu dm,(d32,am) -> movhu dn,(d32,am) */
3124                   case 0x00:
3125                   case 0x10:
3126                   case 0x20:
3127                   case 0x30:
3128                   case 0x40:
3129                   case 0x50:
3130                   case 0x60:
3131                   case 0x70:
3132                     /* Not safe if the high bit is on as relaxing may
3133                        move the value out of high mem and thus not fit
3134                        in a signed 16bit value.  */
3135                     if (code == 0xcc
3136                         && (value & 0x8000))
3137                       continue;
3138
3139                     /* Note that we've changed the relocation contents, etc.  */
3140                     elf_section_data (sec)->relocs = internal_relocs;
3141                     elf_section_data (sec)->this_hdr.contents = contents;
3142                     symtab_hdr->contents = (unsigned char *) isymbuf;
3143
3144                     /* Fix the opcode.  */
3145                     bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3146                     bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3147
3148                     /* Fix the relocation's type.  */
3149                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3150                                                  (ELF32_R_TYPE (irel->r_info)
3151                                                   == (int) R_MN10300_GOTOFF32)
3152                                                  ? R_MN10300_GOTOFF16
3153                                                  : (ELF32_R_TYPE (irel->r_info)
3154                                                     == (int) R_MN10300_GOT32)
3155                                                  ? R_MN10300_GOT16
3156                                                  : (ELF32_R_TYPE (irel->r_info)
3157                                                     == (int) R_MN10300_GOTPC32)
3158                                                  ? R_MN10300_GOTPC16 :
3159                                                  R_MN10300_16);
3160
3161                     /* Delete two bytes of data.  */
3162                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3163                                                          irel->r_offset + 2, 2))
3164                       goto error_return;
3165
3166                     /* That will change things, so, we should relax again.
3167                        Note that this is not required, and it may be slow.  */
3168                     *again = TRUE;
3169                     break;
3170                   }
3171               else if ((code & 0xf0) == 0x80
3172                        || (code & 0xf0) == 0x90)
3173                 switch (code & 0xf3)
3174                   {
3175                   /* mov dn,(abs32)   -> mov dn,(abs16)
3176                      movbu dn,(abs32) -> movbu dn,(abs16)
3177                      movhu dn,(abs32) -> movhu dn,(abs16)  */
3178                   case 0x81:
3179                   case 0x82:
3180                   case 0x83:
3181                     /* Note that we've changed the relocation contents, etc.  */
3182                     elf_section_data (sec)->relocs = internal_relocs;
3183                     elf_section_data (sec)->this_hdr.contents = contents;
3184                     symtab_hdr->contents = (unsigned char *) isymbuf;
3185
3186                     if ((code & 0xf3) == 0x81)
3187                       code = 0x01 + (code & 0x0c);
3188                     else if ((code & 0xf3) == 0x82)
3189                       code = 0x02 + (code & 0x0c);
3190                     else if ((code & 0xf3) == 0x83)
3191                       code = 0x03 + (code & 0x0c);
3192                     else
3193                       abort ();
3194
3195                     /* Fix the opcode.  */
3196                     bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3197
3198                     /* Fix the relocation's type.  */
3199                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3200                                                  (ELF32_R_TYPE (irel->r_info)
3201                                                   == (int) R_MN10300_GOTOFF32)
3202                                                  ? R_MN10300_GOTOFF16
3203                                                  : (ELF32_R_TYPE (irel->r_info)
3204                                                     == (int) R_MN10300_GOT32)
3205                                                  ? R_MN10300_GOT16
3206                                                  : (ELF32_R_TYPE (irel->r_info)
3207                                                     == (int) R_MN10300_GOTPC32)
3208                                                  ? R_MN10300_GOTPC16 :
3209                                                  R_MN10300_16);
3210
3211                     /* The opcode got shorter too, so we have to fix the
3212                        addend and offset too!  */
3213                     irel->r_offset -= 1;
3214
3215                     /* Delete three bytes of data.  */
3216                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3217                                                          irel->r_offset + 1, 3))
3218                       goto error_return;
3219
3220                     /* That will change things, so, we should relax again.
3221                        Note that this is not required, and it may be slow.  */
3222                     *again = TRUE;
3223                     break;
3224
3225                   /* mov am,(abs32)    -> mov am,(abs16)
3226                      mov am,(d32,sp)   -> mov am,(d16,sp)
3227                      mov dm,(d32,sp)   -> mov dm,(d32,sp)
3228                      movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3229                      movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3230                   case 0x80:
3231                   case 0x90:
3232                   case 0x91:
3233                   case 0x92:
3234                   case 0x93:
3235                     /* sp-based offsets are zero-extended.  */
3236                     if (code >= 0x90 && code <= 0x93
3237                         && (long)value < 0)
3238                       continue;
3239
3240                     /* Note that we've changed the relocation contents, etc.  */
3241                     elf_section_data (sec)->relocs = internal_relocs;
3242                     elf_section_data (sec)->this_hdr.contents = contents;
3243                     symtab_hdr->contents = (unsigned char *) isymbuf;
3244
3245                     /* Fix the opcode.  */
3246                     bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3247                     bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3248
3249                     /* Fix the relocation's type.  */
3250                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3251                                                  (ELF32_R_TYPE (irel->r_info)
3252                                                   == (int) R_MN10300_GOTOFF32)
3253                                                  ? R_MN10300_GOTOFF16
3254                                                  : (ELF32_R_TYPE (irel->r_info)
3255                                                     == (int) R_MN10300_GOT32)
3256                                                  ? R_MN10300_GOT16
3257                                                  : (ELF32_R_TYPE (irel->r_info)
3258                                                     == (int) R_MN10300_GOTPC32)
3259                                                  ? R_MN10300_GOTPC16 :
3260                                                  R_MN10300_16);
3261
3262                     /* Delete two bytes of data.  */
3263                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3264                                                          irel->r_offset + 2, 2))
3265                       goto error_return;
3266
3267                     /* That will change things, so, we should relax again.
3268                        Note that this is not required, and it may be slow.  */
3269                     *again = TRUE;
3270                     break;
3271                   }
3272               else if ((code & 0xf0) < 0xf0)
3273                 switch (code & 0xfc)
3274                   {
3275                   /* mov imm32,dn     -> mov imm16,dn
3276                      mov imm32,an     -> mov imm16,an
3277                      mov (abs32),dn   -> mov (abs16),dn
3278                      movbu (abs32),dn -> movbu (abs16),dn
3279                      movhu (abs32),dn -> movhu (abs16),dn  */
3280                   case 0xcc:
3281                   case 0xdc:
3282                   case 0xa4:
3283                   case 0xa8:
3284                   case 0xac:
3285                     /* Not safe if the high bit is on as relaxing may
3286                        move the value out of high mem and thus not fit
3287                        in a signed 16bit value.  */
3288                     if (code == 0xcc
3289                         && (value & 0x8000))
3290                       continue;
3291
3292                     /* mov imm16, an zero-extends the immediate.  */
3293                     if (code == 0xdc
3294                         && (long)value < 0)
3295                       continue;
3296
3297                     /* Note that we've changed the relocation contents, etc.  */
3298                     elf_section_data (sec)->relocs = internal_relocs;
3299                     elf_section_data (sec)->this_hdr.contents = contents;
3300                     symtab_hdr->contents = (unsigned char *) isymbuf;
3301
3302                     if ((code & 0xfc) == 0xcc)
3303                       code = 0x2c + (code & 0x03);
3304                     else if ((code & 0xfc) == 0xdc)
3305                       code = 0x24 + (code & 0x03);
3306                     else if ((code & 0xfc) == 0xa4)
3307                       code = 0x30 + (code & 0x03);
3308                     else if ((code & 0xfc) == 0xa8)
3309                       code = 0x34 + (code & 0x03);
3310                     else if ((code & 0xfc) == 0xac)
3311                       code = 0x38 + (code & 0x03);
3312                     else
3313                       abort ();
3314
3315                     /* Fix the opcode.  */
3316                     bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3317
3318                     /* Fix the relocation's type.  */
3319                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3320                                                  (ELF32_R_TYPE (irel->r_info)
3321                                                   == (int) R_MN10300_GOTOFF32)
3322                                                  ? R_MN10300_GOTOFF16
3323                                                  : (ELF32_R_TYPE (irel->r_info)
3324                                                     == (int) R_MN10300_GOT32)
3325                                                  ? R_MN10300_GOT16
3326                                                  : (ELF32_R_TYPE (irel->r_info)
3327                                                     == (int) R_MN10300_GOTPC32)
3328                                                  ? R_MN10300_GOTPC16 :
3329                                                  R_MN10300_16);
3330
3331                     /* The opcode got shorter too, so we have to fix the
3332                        addend and offset too!  */
3333                     irel->r_offset -= 1;
3334
3335                     /* Delete three bytes of data.  */
3336                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3337                                                          irel->r_offset + 1, 3))
3338                       goto error_return;
3339
3340                     /* That will change things, so, we should relax again.
3341                        Note that this is not required, and it may be slow.  */
3342                     *again = TRUE;
3343                     break;
3344
3345                   /* mov (abs32),an    -> mov (abs16),an
3346                      mov (d32,sp),an   -> mov (d16,sp),an
3347                      mov (d32,sp),dn   -> mov (d16,sp),dn
3348                      movbu (d32,sp),dn -> movbu (d16,sp),dn
3349                      movhu (d32,sp),dn -> movhu (d16,sp),dn
3350                      add imm32,dn      -> add imm16,dn
3351                      cmp imm32,dn      -> cmp imm16,dn
3352                      add imm32,an      -> add imm16,an
3353                      cmp imm32,an      -> cmp imm16,an
3354                      and imm32,dn      -> and imm16,dn
3355                      or imm32,dn       -> or imm16,dn
3356                      xor imm32,dn      -> xor imm16,dn
3357                      btst imm32,dn     -> btst imm16,dn */
3358
3359                   case 0xa0:
3360                   case 0xb0:
3361                   case 0xb1:
3362                   case 0xb2:
3363                   case 0xb3:
3364                   case 0xc0:
3365                   case 0xc8:
3366
3367                   case 0xd0:
3368                   case 0xd8:
3369                   case 0xe0:
3370                   case 0xe1:
3371                   case 0xe2:
3372                   case 0xe3:
3373                     /* cmp imm16, an zero-extends the immediate.  */
3374                     if (code == 0xdc
3375                         && (long)value < 0)
3376                       continue;
3377
3378                     /* So do sp-based offsets.  */
3379                     if (code >= 0xb0 && code <= 0xb3
3380                         && (long)value < 0)
3381                       continue;
3382
3383                     /* Note that we've changed the relocation contents, etc.  */
3384                     elf_section_data (sec)->relocs = internal_relocs;
3385                     elf_section_data (sec)->this_hdr.contents = contents;
3386                     symtab_hdr->contents = (unsigned char *) isymbuf;
3387
3388                     /* Fix the opcode.  */
3389                     bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3390                     bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3391
3392                     /* Fix the relocation's type.  */
3393                     irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3394                                                  (ELF32_R_TYPE (irel->r_info)
3395                                                   == (int) R_MN10300_GOTOFF32)
3396                                                  ? R_MN10300_GOTOFF16
3397                                                  : (ELF32_R_TYPE (irel->r_info)
3398                                                     == (int) R_MN10300_GOT32)
3399                                                  ? R_MN10300_GOT16
3400                                                  : (ELF32_R_TYPE (irel->r_info)
3401                                                     == (int) R_MN10300_GOTPC32)
3402                                                  ? R_MN10300_GOTPC16 :
3403                                                  R_MN10300_16);
3404
3405                     /* Delete two bytes of data.  */
3406                     if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3407                                                          irel->r_offset + 2, 2))
3408                       goto error_return;
3409
3410                     /* That will change things, so, we should relax again.
3411                        Note that this is not required, and it may be slow.  */
3412                     *again = TRUE;
3413                     break;
3414                   }
3415               else if (code == 0xfe)
3416                 {
3417                   /* add imm32,sp -> add imm16,sp  */
3418
3419                   /* Note that we've changed the relocation contents, etc.  */
3420                   elf_section_data (sec)->relocs = internal_relocs;
3421                   elf_section_data (sec)->this_hdr.contents = contents;
3422                   symtab_hdr->contents = (unsigned char *) isymbuf;
3423
3424                   /* Fix the opcode.  */
3425                   bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3426                   bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3427
3428                   /* Fix the relocation's type.  */
3429                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3430                                                (ELF32_R_TYPE (irel->r_info)
3431                                                 == (int) R_MN10300_GOT32)
3432                                                ? R_MN10300_GOT16
3433                                                : (ELF32_R_TYPE (irel->r_info)
3434                                                   == (int) R_MN10300_GOTOFF32)
3435                                                ? R_MN10300_GOTOFF16
3436                                                : (ELF32_R_TYPE (irel->r_info)
3437                                                   == (int) R_MN10300_GOTPC32)
3438                                                ? R_MN10300_GOTPC16 :
3439                                                R_MN10300_16);
3440
3441                   /* Delete two bytes of data.  */
3442                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3443                                                        irel->r_offset + 2, 2))
3444                     goto error_return;
3445
3446                   /* That will change things, so, we should relax again.
3447                      Note that this is not required, and it may be slow.  */
3448                   *again = TRUE;
3449                   break;
3450                 }
3451             }
3452         }
3453     }
3454
3455   if (isymbuf != NULL
3456       && symtab_hdr->contents != (unsigned char *) isymbuf)
3457     {
3458       if (! link_info->keep_memory)
3459         free (isymbuf);
3460       else
3461         {
3462           /* Cache the symbols for elf_link_input_bfd.  */
3463           symtab_hdr->contents = (unsigned char *) isymbuf;
3464         }
3465     }
3466
3467   if (contents != NULL
3468       && elf_section_data (sec)->this_hdr.contents != contents)
3469     {
3470       if (! link_info->keep_memory)
3471         free (contents);
3472       else
3473         {
3474           /* Cache the section contents for elf_link_input_bfd.  */
3475           elf_section_data (sec)->this_hdr.contents = contents;
3476         }
3477     }
3478
3479   if (internal_relocs != NULL
3480       && elf_section_data (sec)->relocs != internal_relocs)
3481     free (internal_relocs);
3482
3483   return TRUE;
3484
3485  error_return:
3486   if (isymbuf != NULL
3487       && symtab_hdr->contents != (unsigned char *) isymbuf)
3488     free (isymbuf);
3489   if (contents != NULL
3490       && elf_section_data (section)->this_hdr.contents != contents)
3491     free (contents);
3492   if (internal_relocs != NULL
3493       && elf_section_data (section)->relocs != internal_relocs)
3494     free (internal_relocs);
3495
3496   return FALSE;
3497 }
3498
3499 /* Compute the stack size and movm arguments for the function
3500    referred to by HASH at address ADDR in section with
3501    contents CONTENTS, store the information in the hash table.  */
3502 static void
3503 compute_function_info (abfd, hash, addr, contents)
3504      bfd *abfd;
3505      struct elf32_mn10300_link_hash_entry *hash;
3506      bfd_vma addr;
3507      unsigned char *contents;
3508 {
3509   unsigned char byte1, byte2;
3510   /* We only care about a very small subset of the possible prologue
3511      sequences here.  Basically we look for:
3512
3513      movm [d2,d3,a2,a3],sp (optional)
3514      add <size>,sp (optional, and only for sizes which fit in an unsigned
3515                     8 bit number)
3516
3517      If we find anything else, we quit.  */
3518
3519   /* Look for movm [regs],sp */
3520   byte1 = bfd_get_8 (abfd, contents + addr);
3521   byte2 = bfd_get_8 (abfd, contents + addr + 1);
3522
3523   if (byte1 == 0xcf)
3524     {
3525       hash->movm_args = byte2;
3526       addr += 2;
3527       byte1 = bfd_get_8 (abfd, contents + addr);
3528       byte2 = bfd_get_8 (abfd, contents + addr + 1);
3529     }
3530
3531   /* Now figure out how much stack space will be allocated by the movm
3532      instruction.  We need this kept separate from the function's normal
3533      stack space.  */
3534   if (hash->movm_args)
3535     {
3536       /* Space for d2.  */
3537       if (hash->movm_args & 0x80)
3538         hash->movm_stack_size += 4;
3539
3540       /* Space for d3.  */
3541       if (hash->movm_args & 0x40)
3542         hash->movm_stack_size += 4;
3543
3544       /* Space for a2.  */
3545       if (hash->movm_args & 0x20)
3546         hash->movm_stack_size += 4;
3547
3548       /* Space for a3.  */
3549       if (hash->movm_args & 0x10)
3550         hash->movm_stack_size += 4;
3551
3552       /* "other" space.  d0, d1, a0, a1, mdr, lir, lar, 4 byte pad.  */
3553       if (hash->movm_args & 0x08)
3554         hash->movm_stack_size += 8 * 4;
3555
3556       if (bfd_get_mach (abfd) == bfd_mach_am33
3557           || bfd_get_mach (abfd) == bfd_mach_am33_2)
3558         {
3559           /* "exother" space.  e0, e1, mdrq, mcrh, mcrl, mcvf */
3560           if (hash->movm_args & 0x1)
3561             hash->movm_stack_size += 6 * 4;
3562
3563           /* exreg1 space.  e4, e5, e6, e7 */
3564           if (hash->movm_args & 0x2)
3565             hash->movm_stack_size += 4 * 4;
3566
3567           /* exreg0 space.  e2, e3  */
3568           if (hash->movm_args & 0x4)
3569             hash->movm_stack_size += 2 * 4;
3570         }
3571     }
3572
3573   /* Now look for the two stack adjustment variants.  */
3574   if (byte1 == 0xf8 && byte2 == 0xfe)
3575     {
3576       int temp = bfd_get_8 (abfd, contents + addr + 2);
3577       temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
3578
3579       hash->stack_size = -temp;
3580     }
3581   else if (byte1 == 0xfa && byte2 == 0xfe)
3582     {
3583       int temp = bfd_get_16 (abfd, contents + addr + 2);
3584       temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
3585       temp = -temp;
3586
3587       if (temp < 255)
3588         hash->stack_size = temp;
3589     }
3590
3591   /* If the total stack to be allocated by the call instruction is more
3592      than 255 bytes, then we can't remove the stack adjustment by using
3593      "call" (we might still be able to remove the "movm" instruction.  */
3594   if (hash->stack_size + hash->movm_stack_size > 255)
3595     hash->stack_size = 0;
3596
3597   return;
3598 }
3599
3600 /* Delete some bytes from a section while relaxing.  */
3601
3602 static bfd_boolean
3603 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
3604      bfd *abfd;
3605      asection *sec;
3606      bfd_vma addr;
3607      int count;
3608 {
3609   Elf_Internal_Shdr *symtab_hdr;
3610   unsigned int sec_shndx;
3611   bfd_byte *contents;
3612   Elf_Internal_Rela *irel, *irelend;
3613   Elf_Internal_Rela *irelalign;
3614   bfd_vma toaddr;
3615   Elf_Internal_Sym *isym, *isymend;
3616   struct elf_link_hash_entry **sym_hashes;
3617   struct elf_link_hash_entry **end_hashes;
3618   unsigned int symcount;
3619
3620   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3621
3622   contents = elf_section_data (sec)->this_hdr.contents;
3623
3624   /* The deletion must stop at the next ALIGN reloc for an aligment
3625      power larger than the number of bytes we are deleting.  */
3626
3627   irelalign = NULL;
3628   toaddr = sec->_cooked_size;
3629
3630   irel = elf_section_data (sec)->relocs;
3631   irelend = irel + sec->reloc_count;
3632
3633   /* Actually delete the bytes.  */
3634   memmove (contents + addr, contents + addr + count,
3635            (size_t) (toaddr - addr - count));
3636   sec->_cooked_size -= count;
3637
3638   /* Adjust all the relocs.  */
3639   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
3640     {
3641       /* Get the new reloc address.  */
3642       if ((irel->r_offset > addr
3643            && irel->r_offset < toaddr))
3644         irel->r_offset -= count;
3645     }
3646
3647   /* Adjust the local symbols defined in this section.  */
3648   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3649   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
3650   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3651     {
3652       if (isym->st_shndx == sec_shndx
3653           && isym->st_value > addr
3654           && isym->st_value < toaddr)
3655         isym->st_value -= count;
3656     }
3657
3658   /* Now adjust the global symbols defined in this section.  */
3659   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3660               - symtab_hdr->sh_info);
3661   sym_hashes = elf_sym_hashes (abfd);
3662   end_hashes = sym_hashes + symcount;
3663   for (; sym_hashes < end_hashes; sym_hashes++)
3664     {
3665       struct elf_link_hash_entry *sym_hash = *sym_hashes;
3666       if ((sym_hash->root.type == bfd_link_hash_defined
3667            || sym_hash->root.type == bfd_link_hash_defweak)
3668           && sym_hash->root.u.def.section == sec
3669           && sym_hash->root.u.def.value > addr
3670           && sym_hash->root.u.def.value < toaddr)
3671         {
3672           sym_hash->root.u.def.value -= count;
3673         }
3674     }
3675
3676   return TRUE;
3677 }
3678
3679 /* Return TRUE if a symbol exists at the given address, else return
3680    FALSE.  */
3681 static bfd_boolean
3682 mn10300_elf_symbol_address_p (abfd, sec, isym, addr)
3683      bfd *abfd;
3684      asection *sec;
3685      Elf_Internal_Sym *isym;
3686      bfd_vma addr;
3687 {
3688   Elf_Internal_Shdr *symtab_hdr;
3689   unsigned int sec_shndx;
3690   Elf_Internal_Sym *isymend;
3691   struct elf_link_hash_entry **sym_hashes;
3692   struct elf_link_hash_entry **end_hashes;
3693   unsigned int symcount;
3694
3695   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3696
3697   /* Examine all the symbols.  */
3698   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3699   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3700     {
3701       if (isym->st_shndx == sec_shndx
3702           && isym->st_value == addr)
3703         return TRUE;
3704     }
3705
3706   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3707               - symtab_hdr->sh_info);
3708   sym_hashes = elf_sym_hashes (abfd);
3709   end_hashes = sym_hashes + symcount;
3710   for (; sym_hashes < end_hashes; sym_hashes++)
3711     {
3712       struct elf_link_hash_entry *sym_hash = *sym_hashes;
3713       if ((sym_hash->root.type == bfd_link_hash_defined
3714            || sym_hash->root.type == bfd_link_hash_defweak)
3715           && sym_hash->root.u.def.section == sec
3716           && sym_hash->root.u.def.value == addr)
3717         return TRUE;
3718     }
3719
3720   return FALSE;
3721 }
3722
3723 /* This is a version of bfd_generic_get_relocated_section_contents
3724    which uses mn10300_elf_relocate_section.  */
3725
3726 static bfd_byte *
3727 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
3728                                             data, relocatable, symbols)
3729      bfd *output_bfd;
3730      struct bfd_link_info *link_info;
3731      struct bfd_link_order *link_order;
3732      bfd_byte *data;
3733      bfd_boolean relocatable;
3734      asymbol **symbols;
3735 {
3736   Elf_Internal_Shdr *symtab_hdr;
3737   asection *input_section = link_order->u.indirect.section;
3738   bfd *input_bfd = input_section->owner;
3739   asection **sections = NULL;
3740   Elf_Internal_Rela *internal_relocs = NULL;
3741   Elf_Internal_Sym *isymbuf = NULL;
3742
3743   /* We only need to handle the case of relaxing, or of having a
3744      particular set of section contents, specially.  */
3745   if (relocatable
3746       || elf_section_data (input_section)->this_hdr.contents == NULL)
3747     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3748                                                        link_order, data,
3749                                                        relocatable,
3750                                                        symbols);
3751
3752   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3753
3754   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3755           (size_t) input_section->_raw_size);
3756
3757   if ((input_section->flags & SEC_RELOC) != 0
3758       && input_section->reloc_count > 0)
3759     {
3760       asection **secpp;
3761       Elf_Internal_Sym *isym, *isymend;
3762       bfd_size_type amt;
3763
3764       internal_relocs = (_bfd_elf_link_read_relocs
3765                          (input_bfd, input_section, (PTR) NULL,
3766                           (Elf_Internal_Rela *) NULL, FALSE));
3767       if (internal_relocs == NULL)
3768         goto error_return;
3769
3770       if (symtab_hdr->sh_info != 0)
3771         {
3772           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3773           if (isymbuf == NULL)
3774             isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3775                                             symtab_hdr->sh_info, 0,
3776                                             NULL, NULL, NULL);
3777           if (isymbuf == NULL)
3778             goto error_return;
3779         }
3780
3781       amt = symtab_hdr->sh_info;
3782       amt *= sizeof (asection *);
3783       sections = (asection **) bfd_malloc (amt);
3784       if (sections == NULL && amt != 0)
3785         goto error_return;
3786
3787       isymend = isymbuf + symtab_hdr->sh_info;
3788       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3789         {
3790           asection *isec;
3791
3792           if (isym->st_shndx == SHN_UNDEF)
3793             isec = bfd_und_section_ptr;
3794           else if (isym->st_shndx == SHN_ABS)
3795             isec = bfd_abs_section_ptr;
3796           else if (isym->st_shndx == SHN_COMMON)
3797             isec = bfd_com_section_ptr;
3798           else
3799             isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3800
3801           *secpp = isec;
3802         }
3803
3804       if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3805                                      input_section, data, internal_relocs,
3806                                      isymbuf, sections))
3807         goto error_return;
3808
3809       if (sections != NULL)
3810         free (sections);
3811       if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3812         free (isymbuf);
3813       if (internal_relocs != elf_section_data (input_section)->relocs)
3814         free (internal_relocs);
3815     }
3816
3817   return data;
3818
3819  error_return:
3820   if (sections != NULL)
3821     free (sections);
3822   if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3823     free (isymbuf);
3824   if (internal_relocs != NULL
3825       && internal_relocs != elf_section_data (input_section)->relocs)
3826     free (internal_relocs);
3827   return NULL;
3828 }
3829
3830 /* Assorted hash table functions.  */
3831
3832 /* Initialize an entry in the link hash table.  */
3833
3834 /* Create an entry in an MN10300 ELF linker hash table.  */
3835
3836 static struct bfd_hash_entry *
3837 elf32_mn10300_link_hash_newfunc (entry, table, string)
3838      struct bfd_hash_entry *entry;
3839      struct bfd_hash_table *table;
3840      const char *string;
3841 {
3842   struct elf32_mn10300_link_hash_entry *ret =
3843     (struct elf32_mn10300_link_hash_entry *) entry;
3844
3845   /* Allocate the structure if it has not already been allocated by a
3846      subclass.  */
3847   if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3848     ret = ((struct elf32_mn10300_link_hash_entry *)
3849            bfd_hash_allocate (table,
3850                               sizeof (struct elf32_mn10300_link_hash_entry)));
3851   if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3852     return (struct bfd_hash_entry *) ret;
3853
3854   /* Call the allocation method of the superclass.  */
3855   ret = ((struct elf32_mn10300_link_hash_entry *)
3856          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3857                                      table, string));
3858   if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
3859     {
3860       ret->direct_calls = 0;
3861       ret->stack_size = 0;
3862       ret->movm_args = 0;
3863       ret->movm_stack_size = 0;
3864       ret->pcrel_relocs_copied = NULL;
3865       ret->flags = 0;
3866     }
3867
3868   return (struct bfd_hash_entry *) ret;
3869 }
3870
3871 /* Create an mn10300 ELF linker hash table.  */
3872
3873 static struct bfd_link_hash_table *
3874 elf32_mn10300_link_hash_table_create (abfd)
3875      bfd *abfd;
3876 {
3877   struct elf32_mn10300_link_hash_table *ret;
3878   bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
3879
3880   ret = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3881   if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
3882     return NULL;
3883
3884   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3885                                        elf32_mn10300_link_hash_newfunc))
3886     {
3887       free (ret);
3888       return NULL;
3889     }
3890
3891   ret->flags = 0;
3892   amt = sizeof (struct elf_link_hash_table);
3893   ret->static_hash_table
3894     = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3895   if (ret->static_hash_table == NULL)
3896     {
3897       free (ret);
3898       return NULL;
3899     }
3900
3901   if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
3902                                        elf32_mn10300_link_hash_newfunc))
3903     {
3904       free (ret->static_hash_table);
3905       free (ret);
3906       return NULL;
3907     }
3908   return &ret->root.root;
3909 }
3910
3911 /* Free an mn10300 ELF linker hash table.  */
3912
3913 static void
3914 elf32_mn10300_link_hash_table_free (hash)
3915      struct bfd_link_hash_table *hash;
3916 {
3917   struct elf32_mn10300_link_hash_table *ret
3918     = (struct elf32_mn10300_link_hash_table *) hash;
3919
3920   _bfd_generic_link_hash_table_free
3921     ((struct bfd_link_hash_table *) ret->static_hash_table);
3922   _bfd_generic_link_hash_table_free
3923     ((struct bfd_link_hash_table *) ret);
3924 }
3925
3926 static unsigned long
3927 elf_mn10300_mach (flags)
3928      flagword flags;
3929 {
3930   switch (flags & EF_MN10300_MACH)
3931     {
3932     case E_MN10300_MACH_MN10300:
3933     default:
3934       return bfd_mach_mn10300;
3935
3936     case E_MN10300_MACH_AM33:
3937       return bfd_mach_am33;
3938
3939     case E_MN10300_MACH_AM33_2:
3940       return bfd_mach_am33_2;
3941     }
3942 }
3943
3944 /* The final processing done just before writing out a MN10300 ELF object
3945    file.  This gets the MN10300 architecture right based on the machine
3946    number.  */
3947
3948 void
3949 _bfd_mn10300_elf_final_write_processing (abfd, linker)
3950      bfd *abfd;
3951      bfd_boolean linker ATTRIBUTE_UNUSED;
3952 {
3953   unsigned long val;
3954
3955   switch (bfd_get_mach (abfd))
3956     {
3957     default:
3958     case bfd_mach_mn10300:
3959       val = E_MN10300_MACH_MN10300;
3960       break;
3961
3962     case bfd_mach_am33:
3963       val = E_MN10300_MACH_AM33;
3964       break;
3965
3966     case bfd_mach_am33_2:
3967       val = E_MN10300_MACH_AM33_2;
3968       break;
3969     }
3970
3971   elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
3972   elf_elfheader (abfd)->e_flags |= val;
3973 }
3974
3975 bfd_boolean
3976 _bfd_mn10300_elf_object_p (abfd)
3977      bfd *abfd;
3978 {
3979   bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
3980                              elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
3981   return TRUE;
3982 }
3983
3984 /* Merge backend specific data from an object file to the output
3985    object file when linking.  */
3986
3987 bfd_boolean
3988 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
3989      bfd *ibfd;
3990      bfd *obfd;
3991 {
3992   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3993       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3994     return TRUE;
3995
3996   if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3997       && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
3998     {
3999       if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4000                                bfd_get_mach (ibfd)))
4001         return FALSE;
4002     }
4003
4004   return TRUE;
4005 }
4006
4007 #define PLT0_ENTRY_SIZE 15
4008 #define PLT_ENTRY_SIZE 20
4009 #define PIC_PLT_ENTRY_SIZE 24
4010
4011 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
4012 {
4013   0xfc, 0xa0, 0, 0, 0, 0,       /* mov  (.got+8),a0 */
4014   0xfe, 0xe, 0x10, 0, 0, 0, 0,  /* mov  (.got+4),r1 */
4015   0xf0, 0xf4,                   /* jmp  (a0) */
4016 };
4017
4018 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
4019 {
4020   0xfc, 0xa0, 0, 0, 0, 0,       /* mov  (nameN@GOT + .got),a0 */
4021   0xf0, 0xf4,                   /* jmp  (a0) */
4022   0xfe, 8, 0, 0, 0, 0, 0,       /* mov  reloc-table-address,r0 */
4023   0xdc, 0, 0, 0, 0,             /* jmp  .plt0 */
4024 };
4025
4026 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
4027 {
4028   0xfc, 0x22, 0, 0, 0, 0,       /* mov  (nameN@GOT,a2),a0 */
4029   0xf0, 0xf4,                   /* jmp  (a0) */
4030   0xfe, 8, 0, 0, 0, 0, 0,       /* mov  reloc-table-address,r0 */
4031   0xf8, 0x22, 8,                /* mov  (8,a2),a0 */
4032   0xfb, 0xa, 0x1a, 4,           /* mov  (4,a2),r1 */
4033   0xf0, 0xf4,                   /* jmp  (a0) */
4034 };
4035
4036 /* Return size of the first PLT entry.  */
4037 #define elf_mn10300_sizeof_plt0(info) \
4038   (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4039
4040 /* Return size of a PLT entry.  */
4041 #define elf_mn10300_sizeof_plt(info) \
4042   (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4043
4044 /* Return offset of the PLT0 address in an absolute PLT entry.  */
4045 #define elf_mn10300_plt_plt0_offset(info) 16
4046
4047 /* Return offset of the linker in PLT0 entry.  */
4048 #define elf_mn10300_plt0_linker_offset(info) 2
4049
4050 /* Return offset of the GOT id in PLT0 entry.  */
4051 #define elf_mn10300_plt0_gotid_offset(info) 9
4052
4053 /* Return offset of the temporary in PLT entry */
4054 #define elf_mn10300_plt_temp_offset(info) 8
4055
4056 /* Return offset of the symbol in PLT entry.  */
4057 #define elf_mn10300_plt_symbol_offset(info) 2
4058
4059 /* Return offset of the relocation in PLT entry.  */
4060 #define elf_mn10300_plt_reloc_offset(info) 11
4061
4062 /* The name of the dynamic interpreter.  This is put in the .interp
4063    section.  */
4064
4065 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4066
4067 /* Create dynamic sections when linking against a dynamic object.  */
4068
4069 static bfd_boolean
4070 _bfd_mn10300_elf_create_dynamic_sections (abfd, info)
4071      bfd *abfd;
4072      struct bfd_link_info *info;
4073 {
4074   flagword   flags;
4075   asection * s;
4076   const struct elf_backend_data * bed = get_elf_backend_data (abfd);
4077   int ptralign = 0;
4078
4079   switch (bed->s->arch_size)
4080     {
4081     case 32:
4082       ptralign = 2;
4083       break;
4084
4085     case 64:
4086       ptralign = 3;
4087       break;
4088
4089     default:
4090       bfd_set_error (bfd_error_bad_value);
4091       return FALSE;
4092     }
4093
4094   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4095      .rel[a].bss sections.  */
4096
4097   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4098            | SEC_LINKER_CREATED);
4099
4100   s = bfd_make_section (abfd,
4101                         bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
4102   if (s == NULL
4103       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
4104       || ! bfd_set_section_alignment (abfd, s, ptralign))
4105     return FALSE;
4106
4107   if (! _bfd_mn10300_elf_create_got_section (abfd, info))
4108     return FALSE;
4109
4110   {
4111     const char * secname;
4112     char *       relname;
4113     flagword     secflags;
4114     asection *   sec;
4115
4116     for (sec = abfd->sections; sec; sec = sec->next)
4117       {
4118         secflags = bfd_get_section_flags (abfd, sec);
4119         if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
4120             || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
4121           continue;
4122
4123         secname = bfd_get_section_name (abfd, sec);
4124         relname = (char *) bfd_malloc (strlen (secname) + 6);
4125         strcpy (relname, ".rela");
4126         strcat (relname, secname);
4127
4128         s = bfd_make_section (abfd, relname);
4129         if (s == NULL
4130             || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
4131             || ! bfd_set_section_alignment (abfd, s, ptralign))
4132           return FALSE;
4133       }
4134   }
4135
4136   if (bed->want_dynbss)
4137     {
4138       /* The .dynbss section is a place to put symbols which are defined
4139          by dynamic objects, are referenced by regular objects, and are
4140          not functions.  We must allocate space for them in the process
4141          image and use a R_*_COPY reloc to tell the dynamic linker to
4142          initialize them at run time.  The linker script puts the .dynbss
4143          section into the .bss section of the final image.  */
4144       s = bfd_make_section (abfd, ".dynbss");
4145       if (s == NULL
4146           || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
4147         return FALSE;
4148
4149       /* The .rel[a].bss section holds copy relocs.  This section is not
4150          normally needed.  We need to create it here, though, so that the
4151          linker will map it to an output section.  We can't just create it
4152          only if we need it, because we will not know whether we need it
4153          until we have seen all the input files, and the first time the
4154          main linker code calls BFD after examining all the input files
4155          (size_dynamic_sections) the input sections have already been
4156          mapped to the output sections.  If the section turns out not to
4157          be needed, we can discard it later.  We will never need this
4158          section when generating a shared object, since they do not use
4159          copy relocs.  */
4160       if (! info->shared)
4161         {
4162           s = bfd_make_section (abfd,
4163                                 (bed->default_use_rela_p
4164                                  ? ".rela.bss" : ".rel.bss"));
4165           if (s == NULL
4166               || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
4167               || ! bfd_set_section_alignment (abfd, s, ptralign))
4168             return FALSE;
4169         }
4170     }
4171
4172   return TRUE;
4173 }
4174 \f
4175 /* Adjust a symbol defined by a dynamic object and referenced by a
4176    regular object.  The current definition is in some section of the
4177    dynamic object, but we're not including those sections.  We have to
4178    change the definition to something the rest of the link can
4179    understand.  */
4180
4181 static bfd_boolean
4182 _bfd_mn10300_elf_adjust_dynamic_symbol (info, h)
4183      struct bfd_link_info * info;
4184      struct elf_link_hash_entry * h;
4185 {
4186   bfd * dynobj;
4187   asection * s;
4188   unsigned int power_of_two;
4189
4190   dynobj = elf_hash_table (info)->dynobj;
4191
4192   /* Make sure we know what is going on here.  */
4193   BFD_ASSERT (dynobj != NULL
4194               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
4195                   || h->weakdef != NULL
4196                   || ((h->elf_link_hash_flags
4197                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4198                       && (h->elf_link_hash_flags
4199                           & ELF_LINK_HASH_REF_REGULAR) != 0
4200                       && (h->elf_link_hash_flags
4201                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
4202
4203   /* If this is a function, put it in the procedure linkage table.  We
4204      will fill in the contents of the procedure linkage table later,
4205      when we know the address of the .got section.  */
4206   if (h->type == STT_FUNC
4207       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4208     {
4209       if (! info->shared
4210           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4211           && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
4212         {
4213           /* This case can occur if we saw a PLT reloc in an input
4214              file, but the symbol was never referred to by a dynamic
4215              object.  In such a case, we don't actually need to build
4216              a procedure linkage table, and we can just do a REL32
4217              reloc instead.  */
4218           BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
4219           return TRUE;
4220         }
4221
4222       /* Make sure this symbol is output as a dynamic symbol.  */
4223       if (h->dynindx == -1)
4224         {
4225           if (! bfd_elf_link_record_dynamic_symbol (info, h))
4226             return FALSE;
4227         }
4228
4229       s = bfd_get_section_by_name (dynobj, ".plt");
4230       BFD_ASSERT (s != NULL);
4231
4232       /* If this is the first .plt entry, make room for the special
4233          first entry.  */
4234       if (s->_raw_size == 0)
4235         s->_raw_size += elf_mn10300_sizeof_plt0 (info);
4236
4237       /* If this symbol is not defined in a regular file, and we are
4238          not generating a shared library, then set the symbol to this
4239          location in the .plt.  This is required to make function
4240          pointers compare as equal between the normal executable and
4241          the shared library.  */
4242       if (! info->shared
4243           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4244         {
4245           h->root.u.def.section = s;
4246           h->root.u.def.value = s->_raw_size;
4247         }
4248
4249       h->plt.offset = s->_raw_size;
4250
4251       /* Make room for this entry.  */
4252       s->_raw_size += elf_mn10300_sizeof_plt (info);
4253
4254       /* We also need to make an entry in the .got.plt section, which
4255          will be placed in the .got section by the linker script.  */
4256
4257       s = bfd_get_section_by_name (dynobj, ".got.plt");
4258       BFD_ASSERT (s != NULL);
4259       s->_raw_size += 4;
4260
4261       /* We also need to make an entry in the .rela.plt section.  */
4262
4263       s = bfd_get_section_by_name (dynobj, ".rela.plt");
4264       BFD_ASSERT (s != NULL);
4265       s->_raw_size += sizeof (Elf32_External_Rela);
4266
4267       return TRUE;
4268     }
4269
4270   /* If this is a weak symbol, and there is a real definition, the
4271      processor independent code will have arranged for us to see the
4272      real definition first, and we can just use the same value.  */
4273   if (h->weakdef != NULL)
4274     {
4275       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4276                   || h->weakdef->root.type == bfd_link_hash_defweak);
4277       h->root.u.def.section = h->weakdef->root.u.def.section;
4278       h->root.u.def.value = h->weakdef->root.u.def.value;
4279       return TRUE;
4280     }
4281
4282   /* This is a reference to a symbol defined by a dynamic object which
4283      is not a function.  */
4284
4285   /* If we are creating a shared library, we must presume that the
4286      only references to the symbol are via the global offset table.
4287      For such cases we need not do anything here; the relocations will
4288      be handled correctly by relocate_section.  */
4289   if (info->shared)
4290     return TRUE;
4291
4292   /* If there are no references to this symbol that do not use the
4293      GOT, we don't need to generate a copy reloc.  */
4294   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
4295     return TRUE;
4296
4297   /* We must allocate the symbol in our .dynbss section, which will
4298      become part of the .bss section of the executable.  There will be
4299      an entry for this symbol in the .dynsym section.  The dynamic
4300      object will contain position independent code, so all references
4301      from the dynamic object to this symbol will go through the global
4302      offset table.  The dynamic linker will use the .dynsym entry to
4303      determine the address it must put in the global offset table, so
4304      both the dynamic object and the regular object will refer to the
4305      same memory location for the variable.  */
4306
4307   s = bfd_get_section_by_name (dynobj, ".dynbss");
4308   BFD_ASSERT (s != NULL);
4309
4310   /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4311      copy the initial value out of the dynamic object and into the
4312      runtime process image.  We need to remember the offset into the
4313      .rela.bss section we are going to use.  */
4314   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4315     {
4316       asection * srel;
4317
4318       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4319       BFD_ASSERT (srel != NULL);
4320       srel->_raw_size += sizeof (Elf32_External_Rela);
4321       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
4322     }
4323
4324   /* We need to figure out the alignment required for this symbol.  I
4325      have no idea how ELF linkers handle this.  */
4326   power_of_two = bfd_log2 (h->size);
4327   if (power_of_two > 3)
4328     power_of_two = 3;
4329
4330   /* Apply the required alignment.  */
4331   s->_raw_size = BFD_ALIGN (s->_raw_size,
4332                             (bfd_size_type) (1 << power_of_two));
4333   if (power_of_two > bfd_get_section_alignment (dynobj, s))
4334     {
4335       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
4336         return FALSE;
4337     }
4338
4339   /* Define the symbol as being at this point in the section.  */
4340   h->root.u.def.section = s;
4341   h->root.u.def.value = s->_raw_size;
4342
4343   /* Increment the section size to make room for the symbol.  */
4344   s->_raw_size += h->size;
4345
4346   return TRUE;
4347 }
4348
4349 /* This function is called via elf32_mn10300_link_hash_traverse if we are
4350    creating a shared object with -Bsymbolic.  It discards the space
4351    allocated to copy PC relative relocs against symbols which are
4352    defined in regular objects.  We allocated space for them in the
4353    check_relocs routine, but we won't fill them in in the
4354    relocate_section routine.  */
4355
4356 static bfd_boolean
4357 _bfd_mn10300_elf_discard_copies (h, info)
4358      struct elf32_mn10300_link_hash_entry *h;
4359      struct bfd_link_info *info;
4360 {
4361   struct elf_mn10300_pcrel_relocs_copied *s;
4362
4363   /* If a symbol has been forced local or we have found a regular
4364      definition for the symbolic link case, then we won't be needing
4365      any relocs.  */
4366   if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4367       && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
4368           || info->symbolic))
4369     {
4370       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
4371         s->section->_raw_size -= s->count * sizeof (Elf32_External_Rel);
4372     }
4373
4374   return TRUE;
4375 }
4376
4377 /* Set the sizes of the dynamic sections.  */
4378
4379 static bfd_boolean
4380 _bfd_mn10300_elf_size_dynamic_sections (output_bfd, info)
4381      bfd * output_bfd;
4382      struct bfd_link_info * info;
4383 {
4384   bfd * dynobj;
4385   asection * s;
4386   bfd_boolean plt;
4387   bfd_boolean relocs;
4388   bfd_boolean reltext;
4389
4390   dynobj = elf_hash_table (info)->dynobj;
4391   BFD_ASSERT (dynobj != NULL);
4392
4393   if (elf_hash_table (info)->dynamic_sections_created)
4394     {
4395       /* Set the contents of the .interp section to the interpreter.  */
4396       if (info->executable)
4397         {
4398           s = bfd_get_section_by_name (dynobj, ".interp");
4399           BFD_ASSERT (s != NULL);
4400           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4401           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4402         }
4403     }
4404   else
4405     {
4406       /* We may have created entries in the .rela.got section.
4407          However, if we are not creating the dynamic sections, we will
4408          not actually use these entries.  Reset the size of .rela.got,
4409          which will cause it to get stripped from the output file
4410          below.  */
4411       s = bfd_get_section_by_name (dynobj, ".rela.got");
4412       if (s != NULL)
4413         s->_raw_size = 0;
4414     }
4415
4416   /* If this is a -Bsymbolic shared link, then we need to discard all
4417      PC relative relocs against symbols defined in a regular object.
4418      We allocated space for them in the check_relocs routine, but we
4419      will not fill them in in the relocate_section routine.  */
4420   if (info->shared && info->symbolic)
4421     elf32_mn10300_link_hash_traverse (elf32_mn10300_hash_table (info),
4422                                       _bfd_mn10300_elf_discard_copies,
4423                                       info);
4424
4425   /* The check_relocs and adjust_dynamic_symbol entry points have
4426      determined the sizes of the various dynamic sections.  Allocate
4427      memory for them.  */
4428   plt = FALSE;
4429   relocs = FALSE;
4430   reltext = FALSE;
4431   for (s = dynobj->sections; s != NULL; s = s->next)
4432     {
4433       const char * name;
4434       bfd_boolean strip;
4435
4436       if ((s->flags & SEC_LINKER_CREATED) == 0)
4437         continue;
4438
4439       /* It's OK to base decisions on the section name, because none
4440          of the dynobj section names depend upon the input files.  */
4441       name = bfd_get_section_name (dynobj, s);
4442
4443       strip = FALSE;
4444
4445       if (strcmp (name, ".plt") == 0)
4446         {
4447           if (s->_raw_size == 0)
4448             /* Strip this section if we don't need it; see the
4449                comment below.  */
4450             strip = TRUE;
4451           else
4452             /* Remember whether there is a PLT.  */
4453             plt = TRUE;
4454         }
4455       else if (strncmp (name, ".rela", 5) == 0)
4456         {
4457           if (s->_raw_size == 0)
4458             {
4459               /* If we don't need this section, strip it from the
4460                  output file.  This is mostly to handle .rela.bss and
4461                  .rela.plt.  We must create both sections in
4462                  create_dynamic_sections, because they must be created
4463                  before the linker maps input sections to output
4464                  sections.  The linker does that before
4465                  adjust_dynamic_symbol is called, and it is that
4466                  function which decides whether anything needs to go
4467                  into these sections.  */
4468               strip = TRUE;
4469             }
4470           else
4471             {
4472               asection * target;
4473
4474               /* Remember whether there are any reloc sections other
4475                  than .rela.plt.  */
4476               if (strcmp (name, ".rela.plt") != 0)
4477                 {
4478                   const char * outname;
4479
4480                   relocs = TRUE;
4481
4482                   /* If this relocation section applies to a read only
4483                      section, then we probably need a DT_TEXTREL
4484                      entry.  The entries in the .rela.plt section
4485                      really apply to the .got section, which we
4486                      created ourselves and so know is not readonly.  */
4487                   outname = bfd_get_section_name (output_bfd,
4488                                                   s->output_section);
4489                   target = bfd_get_section_by_name (output_bfd, outname + 5);
4490                   if (target != NULL
4491                       && (target->flags & SEC_READONLY) != 0
4492                       && (target->flags & SEC_ALLOC) != 0)
4493                     reltext = TRUE;
4494                 }
4495
4496               /* We use the reloc_count field as a counter if we need
4497                  to copy relocs into the output file.  */
4498               s->reloc_count = 0;
4499             }
4500         }
4501       else if (strncmp (name, ".got", 4) != 0)
4502         /* It's not one of our sections, so don't allocate space.  */
4503         continue;
4504
4505       if (strip)
4506         {
4507           _bfd_strip_section_from_output (info, s);
4508           continue;
4509         }
4510
4511       /* Allocate memory for the section contents.  We use bfd_zalloc
4512          here in case unused entries are not reclaimed before the
4513          section's contents are written out.  This should not happen,
4514          but this way if it does, we get a R_MN10300_NONE reloc
4515          instead of garbage.  */
4516       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4517       if (s->contents == NULL && s->_raw_size != 0)
4518         return FALSE;
4519     }
4520
4521   if (elf_hash_table (info)->dynamic_sections_created)
4522     {
4523       /* Add some entries to the .dynamic section.  We fill in the
4524          values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4525          but we must add the entries now so that we get the correct
4526          size for the .dynamic section.  The DT_DEBUG entry is filled
4527          in by the dynamic linker and used by the debugger.  */
4528       if (! info->shared)
4529         {
4530           if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4531             return FALSE;
4532         }
4533
4534       if (plt)
4535         {
4536           if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4537               || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4538               || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4539               || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4540             return FALSE;
4541         }
4542
4543       if (relocs)
4544         {
4545           if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4546               || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4547               || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4548                                               sizeof (Elf32_External_Rela)))
4549             return FALSE;
4550         }
4551
4552       if (reltext)
4553         {
4554           if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4555             return FALSE;
4556         }
4557     }
4558
4559   return TRUE;
4560 }
4561
4562 /* Finish up dynamic symbol handling.  We set the contents of various
4563    dynamic sections here.  */
4564
4565 static bfd_boolean
4566 _bfd_mn10300_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
4567      bfd * output_bfd;
4568      struct bfd_link_info * info;
4569      struct elf_link_hash_entry * h;
4570      Elf_Internal_Sym * sym;
4571 {
4572   bfd * dynobj;
4573
4574   dynobj = elf_hash_table (info)->dynobj;
4575
4576   if (h->plt.offset != (bfd_vma) -1)
4577     {
4578       asection *        splt;
4579       asection *        sgot;
4580       asection *        srel;
4581       bfd_vma           plt_index;
4582       bfd_vma           got_offset;
4583       Elf_Internal_Rela rel;
4584
4585       /* This symbol has an entry in the procedure linkage table.  Set
4586          it up.  */
4587
4588       BFD_ASSERT (h->dynindx != -1);
4589
4590       splt = bfd_get_section_by_name (dynobj, ".plt");
4591       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4592       srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4593       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4594
4595       /* Get the index in the procedure linkage table which
4596          corresponds to this symbol.  This is the index of this symbol
4597          in all the symbols for which we are making plt entries.  The
4598          first entry in the procedure linkage table is reserved.  */
4599       plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4600                    / elf_mn10300_sizeof_plt (info));
4601
4602       /* Get the offset into the .got table of the entry that
4603          corresponds to this function.  Each .got entry is 4 bytes.
4604          The first three are reserved.  */
4605       got_offset = (plt_index + 3) * 4;
4606
4607       /* Fill in the entry in the procedure linkage table.  */
4608       if (! info->shared)
4609         {
4610           memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4611                   elf_mn10300_sizeof_plt (info));
4612           bfd_put_32 (output_bfd,
4613                       (sgot->output_section->vma
4614                        + sgot->output_offset
4615                        + got_offset),
4616                       (splt->contents + h->plt.offset
4617                        + elf_mn10300_plt_symbol_offset (info)));
4618
4619           bfd_put_32 (output_bfd,
4620                       (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4621                       (splt->contents + h->plt.offset
4622                        + elf_mn10300_plt_plt0_offset (info)));
4623         }
4624       else
4625         {
4626           memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4627                   elf_mn10300_sizeof_plt (info));
4628
4629           bfd_put_32 (output_bfd, got_offset,
4630                       (splt->contents + h->plt.offset
4631                        + elf_mn10300_plt_symbol_offset (info)));
4632         }
4633
4634       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4635                   (splt->contents + h->plt.offset
4636                    + elf_mn10300_plt_reloc_offset (info)));
4637
4638       /* Fill in the entry in the global offset table.  */
4639       bfd_put_32 (output_bfd,
4640                   (splt->output_section->vma
4641                    + splt->output_offset
4642                    + h->plt.offset
4643                    + elf_mn10300_plt_temp_offset (info)),
4644                   sgot->contents + got_offset);
4645
4646       /* Fill in the entry in the .rela.plt section.  */
4647       rel.r_offset = (sgot->output_section->vma
4648                       + sgot->output_offset
4649                       + got_offset);
4650       rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4651       rel.r_addend = 0;
4652       bfd_elf32_swap_reloca_out (output_bfd, &rel,
4653                                  (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4654                                                + plt_index));
4655
4656       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4657         /* Mark the symbol as undefined, rather than as defined in
4658            the .plt section.  Leave the value alone.  */
4659         sym->st_shndx = SHN_UNDEF;
4660     }
4661
4662   if (h->got.offset != (bfd_vma) -1)
4663     {
4664       asection *        sgot;
4665       asection *        srel;
4666       Elf_Internal_Rela rel;
4667
4668       /* This symbol has an entry in the global offset table.  Set it up.  */
4669
4670       sgot = bfd_get_section_by_name (dynobj, ".got");
4671       srel = bfd_get_section_by_name (dynobj, ".rela.got");
4672       BFD_ASSERT (sgot != NULL && srel != NULL);
4673
4674       rel.r_offset = (sgot->output_section->vma
4675                       + sgot->output_offset
4676                       + (h->got.offset &~ 1));
4677
4678       /* If this is a -Bsymbolic link, and the symbol is defined
4679          locally, we just want to emit a RELATIVE reloc.  Likewise if
4680          the symbol was forced to be local because of a version file.
4681          The entry in the global offset table will already have been
4682          initialized in the relocate_section function.  */
4683       if (info->shared
4684           && (info->symbolic || h->dynindx == -1)
4685           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
4686         {
4687           rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4688           rel.r_addend = (h->root.u.def.value
4689                           + h->root.u.def.section->output_section->vma
4690                           + h->root.u.def.section->output_offset);
4691         }
4692       else
4693         {
4694           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4695           rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4696           rel.r_addend = 0;
4697         }
4698
4699       bfd_elf32_swap_reloca_out (output_bfd, &rel,
4700                                  (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4701                                                + srel->reloc_count));
4702       ++ srel->reloc_count;
4703     }
4704
4705   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4706     {
4707       asection *        s;
4708       Elf_Internal_Rela rel;
4709
4710       /* This symbol needs a copy reloc.  Set it up.  */
4711       BFD_ASSERT (h->dynindx != -1
4712                   && (h->root.type == bfd_link_hash_defined
4713                       || h->root.type == bfd_link_hash_defweak));
4714
4715       s = bfd_get_section_by_name (h->root.u.def.section->owner,
4716                                    ".rela.bss");
4717       BFD_ASSERT (s != NULL);
4718
4719       rel.r_offset = (h->root.u.def.value
4720                       + h->root.u.def.section->output_section->vma
4721                       + h->root.u.def.section->output_offset);
4722       rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4723       rel.r_addend = 0;
4724       bfd_elf32_swap_reloca_out (output_bfd, &rel,
4725                                  (bfd_byte *) ((Elf32_External_Rela *) s->contents
4726                                                + s->reloc_count));
4727       ++ s->reloc_count;
4728     }
4729
4730   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
4731   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4732       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4733     sym->st_shndx = SHN_ABS;
4734
4735   return TRUE;
4736 }
4737
4738 /* Finish up the dynamic sections.  */
4739
4740 static bfd_boolean
4741 _bfd_mn10300_elf_finish_dynamic_sections (output_bfd, info)
4742      bfd * output_bfd;
4743      struct bfd_link_info * info;
4744 {
4745   bfd *      dynobj;
4746   asection * sgot;
4747   asection * sdyn;
4748
4749   dynobj = elf_hash_table (info)->dynobj;
4750
4751   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4752   BFD_ASSERT (sgot != NULL);
4753   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4754
4755   if (elf_hash_table (info)->dynamic_sections_created)
4756     {
4757       asection *           splt;
4758       Elf32_External_Dyn * dyncon;
4759       Elf32_External_Dyn * dynconend;
4760
4761       BFD_ASSERT (sdyn != NULL);
4762
4763       dyncon = (Elf32_External_Dyn *) sdyn->contents;
4764       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4765
4766       for (; dyncon < dynconend; dyncon++)
4767         {
4768           Elf_Internal_Dyn dyn;
4769           const char * name;
4770           asection * s;
4771
4772           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4773
4774           switch (dyn.d_tag)
4775             {
4776             default:
4777               break;
4778
4779             case DT_PLTGOT:
4780               name = ".got";
4781               goto get_vma;
4782
4783             case DT_JMPREL:
4784               name = ".rela.plt";
4785             get_vma:
4786               s = bfd_get_section_by_name (output_bfd, name);
4787               BFD_ASSERT (s != NULL);
4788               dyn.d_un.d_ptr = s->vma;
4789               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4790               break;
4791
4792             case DT_PLTRELSZ:
4793               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4794               BFD_ASSERT (s != NULL);
4795               if (s->_cooked_size != 0)
4796                 dyn.d_un.d_val = s->_cooked_size;
4797               else
4798                 dyn.d_un.d_val = s->_raw_size;
4799               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4800               break;
4801
4802             case DT_RELASZ:
4803               /* My reading of the SVR4 ABI indicates that the
4804                  procedure linkage table relocs (DT_JMPREL) should be
4805                  included in the overall relocs (DT_RELA).  This is
4806                  what Solaris does.  However, UnixWare can not handle
4807                  that case.  Therefore, we override the DT_RELASZ entry
4808                  here to make it not include the JMPREL relocs.  Since
4809                  the linker script arranges for .rela.plt to follow all
4810                  other relocation sections, we don't have to worry
4811                  about changing the DT_RELA entry.  */
4812               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4813               if (s != NULL)
4814                 {
4815                   if (s->_cooked_size != 0)
4816                     dyn.d_un.d_val -= s->_cooked_size;
4817                   else
4818                     dyn.d_un.d_val -= s->_raw_size;
4819                 }
4820               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4821               break;
4822             }
4823         }
4824
4825       /* Fill in the first entry in the procedure linkage table.  */
4826       splt = bfd_get_section_by_name (dynobj, ".plt");
4827       if (splt && splt->_raw_size > 0)
4828         {
4829           if (info->shared)
4830             {
4831               memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4832                       elf_mn10300_sizeof_plt (info));
4833             }
4834           else
4835             {
4836               memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4837               bfd_put_32 (output_bfd,
4838                           sgot->output_section->vma + sgot->output_offset + 4,
4839                           splt->contents + elf_mn10300_plt0_gotid_offset (info));
4840               bfd_put_32 (output_bfd,
4841                           sgot->output_section->vma + sgot->output_offset + 8,
4842                           splt->contents + elf_mn10300_plt0_linker_offset (info));
4843             }
4844
4845           /* UnixWare sets the entsize of .plt to 4, although that doesn't
4846              really seem like the right value.  */
4847           elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4848         }
4849     }
4850
4851   /* Fill in the first three entries in the global offset table.  */
4852   if (sgot->_raw_size > 0)
4853     {
4854       if (sdyn == NULL)
4855         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4856       else
4857         bfd_put_32 (output_bfd,
4858                     sdyn->output_section->vma + sdyn->output_offset,
4859                     sgot->contents);
4860       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4861       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4862     }
4863
4864   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4865
4866   return TRUE;
4867 }
4868
4869 #ifndef ELF_ARCH
4870 #define TARGET_LITTLE_SYM       bfd_elf32_mn10300_vec
4871 #define TARGET_LITTLE_NAME      "elf32-mn10300"
4872 #define ELF_ARCH                bfd_arch_mn10300
4873 #define ELF_MACHINE_CODE        EM_MN10300
4874 #define ELF_MACHINE_ALT1        EM_CYGNUS_MN10300
4875 #define ELF_MAXPAGESIZE         0x1000
4876 #endif
4877
4878 #define elf_info_to_howto               mn10300_info_to_howto
4879 #define elf_info_to_howto_rel           0
4880 #define elf_backend_can_gc_sections     1
4881 #define elf_backend_rela_normal         1
4882 #define elf_backend_check_relocs        mn10300_elf_check_relocs
4883 #define elf_backend_gc_mark_hook        mn10300_elf_gc_mark_hook
4884 #define elf_backend_relocate_section    mn10300_elf_relocate_section
4885 #define bfd_elf32_bfd_relax_section     mn10300_elf_relax_section
4886 #define bfd_elf32_bfd_get_relocated_section_contents \
4887                                 mn10300_elf_get_relocated_section_contents
4888 #define bfd_elf32_bfd_link_hash_table_create \
4889                                 elf32_mn10300_link_hash_table_create
4890 #define bfd_elf32_bfd_link_hash_table_free \
4891                                 elf32_mn10300_link_hash_table_free
4892
4893 #ifndef elf_symbol_leading_char
4894 #define elf_symbol_leading_char '_'
4895 #endif
4896
4897 /* So we can set bits in e_flags.  */
4898 #define elf_backend_final_write_processing \
4899                                         _bfd_mn10300_elf_final_write_processing
4900 #define elf_backend_object_p            _bfd_mn10300_elf_object_p
4901
4902 #define bfd_elf32_bfd_merge_private_bfd_data \
4903                                         _bfd_mn10300_elf_merge_private_bfd_data
4904
4905 #define elf_backend_can_gc_sections     1
4906 #define elf_backend_create_dynamic_sections \
4907   _bfd_mn10300_elf_create_dynamic_sections
4908 #define elf_backend_adjust_dynamic_symbol \
4909   _bfd_mn10300_elf_adjust_dynamic_symbol
4910 #define elf_backend_size_dynamic_sections \
4911   _bfd_mn10300_elf_size_dynamic_sections
4912 #define elf_backend_finish_dynamic_symbol \
4913   _bfd_mn10300_elf_finish_dynamic_symbol
4914 #define elf_backend_finish_dynamic_sections \
4915   _bfd_mn10300_elf_finish_dynamic_sections
4916
4917 #define elf_backend_want_got_plt        1
4918 #define elf_backend_plt_readonly        1
4919 #define elf_backend_want_plt_sym        0
4920 #define elf_backend_got_header_size     12
4921
4922 #include "elf32-target.h"