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