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