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