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