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