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