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