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