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