packaging: Enable testing infrastructure
[external/binutils.git] / bfd / elf32-cr16.c
1 /* BFD back-end for National Semiconductor's CR16 ELF
2    Copyright (C) 2007-2019 Free Software Foundation, Inc.
3    Written by M R Swami Reddy.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software Foundation,
19    Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "libiberty.h"
26 #include "elf-bfd.h"
27 #include "elf/cr16.h"
28
29 /* The cr16 linker needs to keep track of the number of relocs that
30    it decides to copy in check_relocs for each symbol.  This is so
31    that it can discard PC relative relocs if it doesn't need them when
32    linking with -Bsymbolic.  We store the information in a field
33    extending the regular ELF linker hash table.  */
34
35 struct elf32_cr16_link_hash_entry
36 {
37   /* The basic elf link hash table entry.  */
38   struct elf_link_hash_entry root;
39
40   /* For function symbols, the number of times this function is
41      called directly (ie by name).  */
42   unsigned int direct_calls;
43
44   /* For function symbols, the size of this function's stack
45      (if <= 255 bytes).  We stuff this into "call" instructions
46      to this target when it's valid and profitable to do so.
47
48      This does not include stack allocated by movm!  */
49   unsigned char stack_size;
50
51   /* For function symbols, arguments (if any) for movm instruction
52      in the prologue.  We stuff this value into "call" instructions
53      to the target when it's valid and profitable to do so.  */
54   unsigned char movm_args;
55
56   /* For function symbols, the amount of stack space that would be allocated
57      by the movm instruction.  This is redundant with movm_args, but we
58      add it to the hash table to avoid computing it over and over.  */
59   unsigned char movm_stack_size;
60
61 /* Used to mark functions which have had redundant parts of their
62    prologue deleted.  */
63 #define CR16_DELETED_PROLOGUE_BYTES 0x1
64   unsigned char flags;
65
66   /* Calculated value.  */
67   bfd_vma value;
68 };
69
70 /* cr16_reloc_map array maps BFD relocation enum into a CRGAS relocation type.  */
71
72 struct cr16_reloc_map
73 {
74   bfd_reloc_code_real_type bfd_reloc_enum; /* BFD relocation enum.  */
75   unsigned short cr16_reloc_type;          /* CR16 relocation type.  */
76 };
77
78 static const struct cr16_reloc_map cr16_reloc_map[R_CR16_MAX] =
79 {
80   {BFD_RELOC_NONE,           R_CR16_NONE},
81   {BFD_RELOC_CR16_NUM8,      R_CR16_NUM8},
82   {BFD_RELOC_CR16_NUM16,     R_CR16_NUM16},
83   {BFD_RELOC_CR16_NUM32,     R_CR16_NUM32},
84   {BFD_RELOC_CR16_NUM32a,    R_CR16_NUM32a},
85   {BFD_RELOC_CR16_REGREL4,   R_CR16_REGREL4},
86   {BFD_RELOC_CR16_REGREL4a,  R_CR16_REGREL4a},
87   {BFD_RELOC_CR16_REGREL14,  R_CR16_REGREL14},
88   {BFD_RELOC_CR16_REGREL14a, R_CR16_REGREL14a},
89   {BFD_RELOC_CR16_REGREL16,  R_CR16_REGREL16},
90   {BFD_RELOC_CR16_REGREL20,  R_CR16_REGREL20},
91   {BFD_RELOC_CR16_REGREL20a, R_CR16_REGREL20a},
92   {BFD_RELOC_CR16_ABS20,     R_CR16_ABS20},
93   {BFD_RELOC_CR16_ABS24,     R_CR16_ABS24},
94   {BFD_RELOC_CR16_IMM4,      R_CR16_IMM4},
95   {BFD_RELOC_CR16_IMM8,      R_CR16_IMM8},
96   {BFD_RELOC_CR16_IMM16,     R_CR16_IMM16},
97   {BFD_RELOC_CR16_IMM20,     R_CR16_IMM20},
98   {BFD_RELOC_CR16_IMM24,     R_CR16_IMM24},
99   {BFD_RELOC_CR16_IMM32,     R_CR16_IMM32},
100   {BFD_RELOC_CR16_IMM32a,    R_CR16_IMM32a},
101   {BFD_RELOC_CR16_DISP4,     R_CR16_DISP4},
102   {BFD_RELOC_CR16_DISP8,     R_CR16_DISP8},
103   {BFD_RELOC_CR16_DISP16,    R_CR16_DISP16},
104   {BFD_RELOC_CR16_DISP24,    R_CR16_DISP24},
105   {BFD_RELOC_CR16_DISP24a,   R_CR16_DISP24a},
106   {BFD_RELOC_CR16_SWITCH8,   R_CR16_SWITCH8},
107   {BFD_RELOC_CR16_SWITCH16,  R_CR16_SWITCH16},
108   {BFD_RELOC_CR16_SWITCH32,  R_CR16_SWITCH32},
109   {BFD_RELOC_CR16_GOT_REGREL20, R_CR16_GOT_REGREL20},
110   {BFD_RELOC_CR16_GOTC_REGREL20, R_CR16_GOTC_REGREL20},
111   {BFD_RELOC_CR16_GLOB_DAT,  R_CR16_GLOB_DAT}
112 };
113
114 static reloc_howto_type cr16_elf_howto_table[] =
115 {
116   HOWTO (R_CR16_NONE,              /* type */
117          0,                        /* rightshift */
118          3,                        /* size */
119          0,                        /* bitsize */
120          FALSE,                    /* pc_relative */
121          0,                        /* bitpos */
122          complain_overflow_dont,   /* complain_on_overflow */
123          bfd_elf_generic_reloc,    /* special_function */
124          "R_CR16_NONE",            /* name */
125          FALSE,                    /* partial_inplace */
126          0,                        /* src_mask */
127          0,                        /* dst_mask */
128          FALSE),                   /* pcrel_offset */
129
130   HOWTO (R_CR16_NUM8,              /* type */
131          0,                        /* rightshift */
132          0,                        /* size */
133          8,                        /* bitsize */
134          FALSE,                    /* pc_relative */
135          0,                        /* bitpos */
136          complain_overflow_bitfield,/* complain_on_overflow */
137          bfd_elf_generic_reloc,    /* special_function */
138          "R_CR16_NUM8",            /* name */
139          FALSE,                    /* partial_inplace */
140          0x0,                      /* src_mask */
141          0xff,                     /* dst_mask */
142          FALSE),                   /* pcrel_offset */
143
144   HOWTO (R_CR16_NUM16,             /* type */
145          0,                        /* rightshift */
146          1,                        /* size */
147          16,                       /* bitsize */
148          FALSE,                    /* pc_relative */
149          0,                        /* bitpos */
150          complain_overflow_bitfield,/* complain_on_overflow */
151          bfd_elf_generic_reloc,    /* special_function */
152          "R_CR16_NUM16",           /* name */
153          FALSE,                    /* partial_inplace */
154          0x0,                      /* src_mask */
155          0xffff,                   /* dst_mask */
156          FALSE),                   /* pcrel_offset */
157
158   HOWTO (R_CR16_NUM32,             /* type */
159          0,                        /* rightshift */
160          2,                        /* size */
161          32,                       /* bitsize */
162          FALSE,                    /* pc_relative */
163          0,                        /* bitpos */
164          complain_overflow_bitfield,/* complain_on_overflow */
165          bfd_elf_generic_reloc,    /* special_function */
166          "R_CR16_NUM32",           /* name */
167          FALSE,                    /* partial_inplace */
168          0x0,                      /* src_mask */
169          0xffffffff,               /* dst_mask */
170          FALSE),                   /* pcrel_offset */
171
172   HOWTO (R_CR16_NUM32a,            /* type */
173          1,                        /* rightshift */
174          2,                        /* size */
175          32,                       /* bitsize */
176          FALSE,                    /* pc_relative */
177          0,                        /* bitpos */
178          complain_overflow_bitfield,/* complain_on_overflow */
179          bfd_elf_generic_reloc,    /* special_function */
180          "R_CR16_NUM32a",          /* name */
181          FALSE,                    /* partial_inplace */
182          0x0,                      /* src_mask */
183          0xffffffff,               /* dst_mask */
184          FALSE),                   /* pcrel_offset */
185
186   HOWTO (R_CR16_REGREL4,           /* type */
187          0,                        /* rightshift */
188          0,                        /* size */
189          4,                        /* bitsize */
190          FALSE,                    /* pc_relative */
191          0,                        /* bitpos */
192          complain_overflow_bitfield,/* complain_on_overflow */
193          bfd_elf_generic_reloc,    /* special_function */
194          "R_CR16_REGREL4",         /* name */
195          FALSE,                    /* partial_inplace */
196          0x0,                      /* src_mask */
197          0xf,                      /* dst_mask */
198          FALSE),                   /* pcrel_offset */
199
200   HOWTO (R_CR16_REGREL4a,          /* type */
201          0,                        /* rightshift */
202          0,                        /* size */
203          4,                        /* bitsize */
204          FALSE,                    /* pc_relative */
205          0,                        /* bitpos */
206          complain_overflow_bitfield,/* complain_on_overflow */
207          bfd_elf_generic_reloc,    /* special_function */
208          "R_CR16_REGREL4a",        /* name */
209          FALSE,                    /* partial_inplace */
210          0x0,                      /* src_mask */
211          0xf,                      /* dst_mask */
212          FALSE),                   /* pcrel_offset */
213
214   HOWTO (R_CR16_REGREL14,          /* type */
215          0,                        /* rightshift */
216          1,                        /* size */
217          14,                       /* bitsize */
218          FALSE,                    /* pc_relative */
219          0,                        /* bitpos */
220          complain_overflow_bitfield,/* complain_on_overflow */
221          bfd_elf_generic_reloc,    /* special_function */
222          "R_CR16_REGREL14",        /* name */
223          FALSE,                    /* partial_inplace */
224          0x0,                      /* src_mask */
225          0x3fff,                   /* dst_mask */
226          FALSE),                   /* pcrel_offset */
227
228   HOWTO (R_CR16_REGREL14a,         /* type */
229          0,                        /* rightshift */
230          1,                        /* size */
231          14,                       /* bitsize */
232          FALSE,                    /* pc_relative */
233          0,                        /* bitpos */
234          complain_overflow_bitfield,/* complain_on_overflow */
235          bfd_elf_generic_reloc,    /* special_function */
236          "R_CR16_REGREL14a",       /* name */
237          FALSE,                    /* partial_inplace */
238          0x0,                      /* src_mask */
239          0x3fff,                   /* dst_mask */
240          FALSE),                   /* pcrel_offset */
241
242   HOWTO (R_CR16_REGREL16,          /* type */
243          0,                        /* rightshift */
244          1,                        /* size */
245          16,                       /* bitsize */
246          FALSE,                    /* pc_relative */
247          0,                        /* bitpos */
248          complain_overflow_bitfield,/* complain_on_overflow */
249          bfd_elf_generic_reloc,    /* special_function */
250          "R_CR16_REGREL16",        /* name */
251          FALSE,                    /* partial_inplace */
252          0x0,                      /* src_mask */
253          0xffff,                   /* dst_mask */
254          FALSE),                   /* pcrel_offset */
255
256   HOWTO (R_CR16_REGREL20,          /* type */
257          0,                        /* rightshift */
258          2,                        /* size */
259          20,                       /* bitsize */
260          FALSE,                    /* pc_relative */
261          0,                        /* bitpos */
262          complain_overflow_bitfield,/* complain_on_overflow */
263          bfd_elf_generic_reloc,    /* special_function */
264          "R_CR16_REGREL20",        /* name */
265          FALSE,                    /* partial_inplace */
266          0x0,                      /* src_mask */
267          0xfffff,                  /* dst_mask */
268          FALSE),                   /* pcrel_offset */
269
270   HOWTO (R_CR16_REGREL20a,         /* type */
271          0,                        /* rightshift */
272          2,                        /* size */
273          20,                       /* bitsize */
274          FALSE,                    /* pc_relative */
275          0,                        /* bitpos */
276          complain_overflow_bitfield,/* complain_on_overflow */
277          bfd_elf_generic_reloc,    /* special_function */
278          "R_CR16_REGREL20a",       /* name */
279          FALSE,                    /* partial_inplace */
280          0x0,                      /* src_mask */
281          0xfffff,                  /* dst_mask */
282          FALSE),                   /* pcrel_offset */
283
284   HOWTO (R_CR16_ABS20,             /* type */
285          0,                        /* rightshift */
286          2,                        /* size */
287          20,                       /* bitsize */
288          FALSE,                    /* pc_relative */
289          0,                        /* bitpos */
290          complain_overflow_bitfield,/* complain_on_overflow */
291          bfd_elf_generic_reloc,    /* special_function */
292          "R_CR16_ABS20",           /* name */
293          FALSE,                    /* partial_inplace */
294          0x0,                      /* src_mask */
295          0xfffff,                  /* dst_mask */
296          FALSE),                   /* pcrel_offset */
297
298   HOWTO (R_CR16_ABS24,             /* type */
299          0,                        /* rightshift */
300          2,                        /* size */
301          24,                       /* bitsize */
302          FALSE,                    /* pc_relative */
303          0,                        /* bitpos */
304          complain_overflow_bitfield,/* complain_on_overflow */
305          bfd_elf_generic_reloc,    /* special_function */
306          "R_CR16_ABS24",           /* name */
307          FALSE,                    /* partial_inplace */
308          0x0,                      /* src_mask */
309          0xffffff,                 /* dst_mask */
310          FALSE),                   /* pcrel_offset */
311
312   HOWTO (R_CR16_IMM4,              /* type */
313          0,                        /* rightshift */
314          0,                        /* size */
315          4,                        /* bitsize */
316          FALSE,                    /* pc_relative */
317          0,                        /* bitpos */
318          complain_overflow_bitfield,/* complain_on_overflow */
319          bfd_elf_generic_reloc,    /* special_function */
320          "R_CR16_IMM4",            /* name */
321          FALSE,                    /* partial_inplace */
322          0x0,                      /* src_mask */
323          0xf,                      /* dst_mask */
324          FALSE),                   /* pcrel_offset */
325
326   HOWTO (R_CR16_IMM8,              /* type */
327          0,                        /* rightshift */
328          0,                        /* size */
329          8,                        /* bitsize */
330          FALSE,                    /* pc_relative */
331          0,                        /* bitpos */
332          complain_overflow_bitfield,/* complain_on_overflow */
333          bfd_elf_generic_reloc,    /* special_function */
334          "R_CR16_IMM8",            /* name */
335          FALSE,                    /* partial_inplace */
336          0x0,                      /* src_mask */
337          0xff,                     /* dst_mask */
338          FALSE),                   /* pcrel_offset */
339
340   HOWTO (R_CR16_IMM16,             /* type */
341          0,                        /* rightshift */
342          1,                        /* size */
343          16,                       /* bitsize */
344          FALSE,                    /* pc_relative */
345          0,                        /* bitpos */
346          complain_overflow_bitfield,/* complain_on_overflow */
347          bfd_elf_generic_reloc,    /* special_function */
348          "R_CR16_IMM16",           /* name */
349          FALSE,                    /* partial_inplace */
350          0x0,                      /* src_mask */
351          0xffff,                   /* dst_mask */
352          FALSE),                   /* pcrel_offset */
353
354   HOWTO (R_CR16_IMM20,             /* type */
355          0,                        /* rightshift */
356          2,                        /* size */
357          20,                       /* bitsize */
358          FALSE,                    /* pc_relative */
359          0,                        /* bitpos */
360          complain_overflow_bitfield,/* complain_on_overflow */
361          bfd_elf_generic_reloc,    /* special_function */
362          "R_CR16_IMM20",           /* name */
363          FALSE,                    /* partial_inplace */
364          0x0,                      /* src_mask */
365          0xfffff,                  /* dst_mask */
366          FALSE),                   /* pcrel_offset */
367
368   HOWTO (R_CR16_IMM24,             /* type */
369          0,                        /* rightshift */
370          2,                        /* size */
371          24,                       /* bitsize */
372          FALSE,                    /* pc_relative */
373          0,                        /* bitpos */
374          complain_overflow_bitfield,/* complain_on_overflow */
375          bfd_elf_generic_reloc,    /* special_function */
376          "R_CR16_IMM24",           /* name */
377          FALSE,                    /* partial_inplace */
378          0x0,                      /* src_mask */
379          0xffffff,                 /* dst_mask */
380          FALSE),                   /* pcrel_offset */
381
382   HOWTO (R_CR16_IMM32,             /* type */
383          0,                        /* rightshift */
384          2,                        /* size */
385          32,                       /* bitsize */
386          FALSE,                    /* pc_relative */
387          0,                        /* bitpos */
388          complain_overflow_bitfield,/* complain_on_overflow */
389          bfd_elf_generic_reloc,    /* special_function */
390          "R_CR16_IMM32",           /* name */
391          FALSE,                    /* partial_inplace */
392          0x0,                      /* src_mask */
393          0xffffffff,               /* dst_mask */
394          FALSE),                   /* pcrel_offset */
395
396   HOWTO (R_CR16_IMM32a,            /* type */
397          1,                        /* rightshift */
398          2,                        /* size */
399          32,                       /* bitsize */
400          FALSE,                    /* pc_relative */
401          0,                        /* bitpos */
402          complain_overflow_bitfield,/* complain_on_overflow */
403          bfd_elf_generic_reloc,    /* special_function */
404          "R_CR16_IMM32a",          /* name */
405          FALSE,                    /* partial_inplace */
406          0x0,                      /* src_mask */
407          0xffffffff,               /* dst_mask */
408          FALSE),                   /* pcrel_offset */
409
410   HOWTO (R_CR16_DISP4,             /* type */
411          1,                        /* rightshift */
412          0,                        /* size (0 = byte, 1 = short, 2 = long) */
413          4,                        /* bitsize */
414          TRUE,                     /* pc_relative */
415          0,                        /* bitpos */
416          complain_overflow_unsigned, /* complain_on_overflow */
417          bfd_elf_generic_reloc,    /* special_function */
418          "R_CR16_DISP4",           /* name */
419          FALSE,                    /* partial_inplace */
420          0x0,                      /* src_mask */
421          0xf,                      /* dst_mask */
422          FALSE),                   /* pcrel_offset */
423
424   HOWTO (R_CR16_DISP8,             /* type */
425          1,                        /* rightshift */
426          0,                        /* size (0 = byte, 1 = short, 2 = long) */
427          8,                        /* bitsize */
428          TRUE,                     /* pc_relative */
429          0,                        /* bitpos */
430          complain_overflow_unsigned, /* complain_on_overflow */
431          bfd_elf_generic_reloc,    /* special_function */
432          "R_CR16_DISP8",           /* name */
433          FALSE,                    /* partial_inplace */
434          0x0,                      /* src_mask */
435          0x1ff,                    /* dst_mask */
436          FALSE),                   /* pcrel_offset */
437
438   HOWTO (R_CR16_DISP16,            /* type */
439          0,                        /* rightshift REVIITS: To sync with WinIDEA*/
440          1,                        /* size (0 = byte, 1 = short, 2 = long) */
441          16,                       /* bitsize */
442          TRUE,                     /* pc_relative */
443          0,                        /* bitpos */
444          complain_overflow_unsigned, /* complain_on_overflow */
445          bfd_elf_generic_reloc,    /* special_function */
446          "R_CR16_DISP16",          /* name */
447          FALSE,                    /* partial_inplace */
448          0x0,                      /* src_mask */
449          0x1ffff,                  /* dst_mask */
450          FALSE),                   /* pcrel_offset */
451   /* REVISIT: DISP24 should be left-shift by 2 as per ISA doc
452      but its not done, to sync with WinIDEA and CR16 4.1 tools */
453   HOWTO (R_CR16_DISP24,            /* type */
454          0,                        /* rightshift */
455          2,                        /* size (0 = byte, 1 = short, 2 = long) */
456          24,                       /* bitsize */
457          TRUE,                     /* pc_relative */
458          0,                        /* bitpos */
459          complain_overflow_unsigned, /* complain_on_overflow */
460          bfd_elf_generic_reloc,    /* special_function */
461          "R_CR16_DISP24",          /* name */
462          FALSE,                    /* partial_inplace */
463          0x0,                      /* src_mask */
464          0x1ffffff,                /* dst_mask */
465          FALSE),                   /* pcrel_offset */
466
467   HOWTO (R_CR16_DISP24a,           /* type */
468          0,                        /* rightshift */
469          2,                        /* size (0 = byte, 1 = short, 2 = long) */
470          24,                       /* bitsize */
471          TRUE,                     /* pc_relative */
472          0,                        /* bitpos */
473          complain_overflow_unsigned, /* complain_on_overflow */
474          bfd_elf_generic_reloc,    /* special_function */
475          "R_CR16_DISP24a",         /* name */
476          FALSE,                    /* partial_inplace */
477          0x0,                      /* src_mask */
478          0xffffff,                 /* dst_mask */
479          FALSE),                   /* pcrel_offset */
480
481   /* An 8 bit switch table entry.  This is generated for an expression
482      such as ``.byte L1 - L2''.  The offset holds the difference
483      between the reloc address and L2.  */
484   HOWTO (R_CR16_SWITCH8,           /* type */
485          0,                        /* rightshift */
486          0,                        /* size (0 = byte, 1 = short, 2 = long) */
487          8,                        /* bitsize */
488          FALSE,                    /* pc_relative */
489          0,                        /* bitpos */
490          complain_overflow_unsigned, /* complain_on_overflow */
491          bfd_elf_generic_reloc,    /* special_function */
492          "R_CR16_SWITCH8",         /* name */
493          FALSE,                    /* partial_inplace */
494          0x0,                      /* src_mask */
495          0xff,                     /* dst_mask */
496          TRUE),                    /* pcrel_offset */
497
498   /* A 16 bit switch table entry.  This is generated for an expression
499      such as ``.word L1 - L2''.  The offset holds the difference
500      between the reloc address and L2.  */
501   HOWTO (R_CR16_SWITCH16,          /* type */
502          0,                        /* rightshift */
503          1,                        /* size (0 = byte, 1 = short, 2 = long) */
504          16,                       /* bitsize */
505          FALSE,                    /* pc_relative */
506          0,                        /* bitpos */
507          complain_overflow_unsigned, /* complain_on_overflow */
508          bfd_elf_generic_reloc,    /* special_function */
509          "R_CR16_SWITCH16",        /* name */
510          FALSE,                    /* partial_inplace */
511          0x0,                      /* src_mask */
512          0xffff,                   /* dst_mask */
513          TRUE),                    /* pcrel_offset */
514
515   /* A 32 bit switch table entry.  This is generated for an expression
516      such as ``.long L1 - L2''.  The offset holds the difference
517      between the reloc address and L2.  */
518   HOWTO (R_CR16_SWITCH32,          /* type */
519          0,                        /* rightshift */
520          2,                        /* size (0 = byte, 1 = short, 2 = long) */
521          32,                       /* bitsize */
522          FALSE,                    /* pc_relative */
523          0,                        /* bitpos */
524          complain_overflow_unsigned, /* complain_on_overflow */
525          bfd_elf_generic_reloc,    /* special_function */
526          "R_CR16_SWITCH32",        /* name */
527          FALSE,                    /* partial_inplace */
528          0x0,                      /* src_mask */
529          0xffffffff,               /* dst_mask */
530          TRUE),                    /* pcrel_offset */
531
532   HOWTO (R_CR16_GOT_REGREL20,      /* type */
533          0,                        /* rightshift */
534          2,                        /* size */
535          20,                       /* bitsize */
536          FALSE,                    /* pc_relative */
537          0,                        /* bitpos */
538          complain_overflow_bitfield,/* complain_on_overflow */
539          bfd_elf_generic_reloc,    /* special_function */
540          "R_CR16_GOT_REGREL20",    /* name */
541          TRUE,                     /* partial_inplace */
542          0x0,                      /* src_mask */
543          0xfffff,                  /* dst_mask */
544          FALSE),                   /* pcrel_offset */
545
546   HOWTO (R_CR16_GOTC_REGREL20,     /* type */
547          0,                        /* rightshift */
548          2,                        /* size */
549          20,                       /* bitsize */
550          FALSE,                    /* pc_relative */
551          0,                        /* bitpos */
552          complain_overflow_bitfield,/* complain_on_overflow */
553          bfd_elf_generic_reloc,    /* special_function */
554          "R_CR16_GOTC_REGREL20",   /* name */
555          TRUE,                     /* partial_inplace */
556          0x0,                      /* src_mask */
557          0xfffff,                  /* dst_mask */
558          FALSE),                   /* pcrel_offset */
559
560   HOWTO (R_CR16_GLOB_DAT,          /* type */
561          0,                        /* rightshift */
562          2,                        /* size (0 = byte, 1 = short, 2 = long) */
563          32,                       /* bitsize */
564          FALSE,                    /* pc_relative */
565          0,                        /* bitpos */
566          complain_overflow_unsigned, /* complain_on_overflow */
567          bfd_elf_generic_reloc,    /* special_function */
568          "R_CR16_GLOB_DAT",        /* name */
569          FALSE,                    /* partial_inplace */
570          0x0,                      /* src_mask */
571          0xffffffff,               /* dst_mask */
572          TRUE)                     /* pcrel_offset */
573 };
574
575
576 /* Create the GOT section.  */
577
578 static bfd_boolean
579 _bfd_cr16_elf_create_got_section (bfd * abfd, struct bfd_link_info * info)
580 {
581   flagword   flags;
582   asection * s;
583   struct elf_link_hash_entry * h;
584   const struct elf_backend_data * bed = get_elf_backend_data (abfd);
585   struct elf_link_hash_table *htab = elf_hash_table (info);
586   int ptralign;
587
588   /* This function may be called more than once.  */
589   if (htab->sgot != NULL)
590     return TRUE;
591
592   switch (bed->s->arch_size)
593     {
594     case 16:
595       ptralign = 1;
596       break;
597
598     case 32:
599       ptralign = 2;
600       break;
601
602     default:
603       bfd_set_error (bfd_error_bad_value);
604       return FALSE;
605     }
606
607   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
608            | SEC_LINKER_CREATED);
609
610   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
611   htab->sgot= s;
612   if (s == NULL
613       || ! bfd_set_section_alignment (abfd, s, ptralign))
614     return FALSE;
615
616   if (bed->want_got_plt)
617     {
618       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
619       htab->sgotplt = s;
620       if (s == NULL
621           || ! bfd_set_section_alignment (abfd, s, ptralign))
622         return FALSE;
623     }
624
625   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
626      (or .got.plt) section.  We don't do this in the linker script
627      because we don't want to define the symbol if we are not creating
628      a global offset table.  */
629   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
630   htab->hgot = h;
631   if (h == NULL)
632     return FALSE;
633
634   /* The first bit of the global offset table is the header.  */
635   s->size += bed->got_header_size;
636
637   return TRUE;
638 }
639
640
641 /* Retrieve a howto ptr using a BFD reloc_code.  */
642
643 static reloc_howto_type *
644 elf_cr16_reloc_type_lookup (bfd *abfd,
645                             bfd_reloc_code_real_type code)
646 {
647   unsigned int i;
648
649   for (i = 0; i < R_CR16_MAX; i++)
650     if (code == cr16_reloc_map[i].bfd_reloc_enum)
651       return &cr16_elf_howto_table[cr16_reloc_map[i].cr16_reloc_type];
652
653   _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
654                       abfd, code);
655   return NULL;
656 }
657
658 static reloc_howto_type *
659 elf_cr16_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
660                             const char *r_name)
661 {
662   unsigned int i;
663
664   for (i = 0; ARRAY_SIZE (cr16_elf_howto_table); i++)
665     if (cr16_elf_howto_table[i].name != NULL
666         && strcasecmp (cr16_elf_howto_table[i].name, r_name) == 0)
667       return cr16_elf_howto_table + i;
668
669   return NULL;
670 }
671
672 /* Retrieve a howto ptr using an internal relocation entry.  */
673
674 static bfd_boolean
675 elf_cr16_info_to_howto (bfd *abfd, arelent *cache_ptr,
676                         Elf_Internal_Rela *dst)
677 {
678   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
679
680   if (r_type >= R_CR16_MAX)
681     {
682       /* xgettext:c-format */
683       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
684                           abfd, r_type);
685       bfd_set_error (bfd_error_bad_value);
686       return FALSE;
687     }
688   cache_ptr->howto = cr16_elf_howto_table + r_type;
689   return TRUE;
690 }
691
692 /* Look through the relocs for a section during the first phase.
693    Since we don't do .gots or .plts, we just need to consider the
694    virtual table relocs for gc.  */
695
696 static bfd_boolean
697 cr16_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
698                        const Elf_Internal_Rela *relocs)
699 {
700   Elf_Internal_Shdr *symtab_hdr;
701   Elf_Internal_Sym * isymbuf = NULL;
702   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
703   const Elf_Internal_Rela *rel;
704   const Elf_Internal_Rela *rel_end;
705   bfd *      dynobj;
706   bfd_vma *  local_got_offsets;
707   asection * sgot;
708   asection * srelgot;
709
710   sgot    = NULL;
711   srelgot = NULL;
712   bfd_boolean result = FALSE;
713
714   if (bfd_link_relocatable (info))
715     return TRUE;
716
717   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
718   sym_hashes = elf_sym_hashes (abfd);
719   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
720   if (!elf_bad_symtab (abfd))
721     sym_hashes_end -= symtab_hdr->sh_info;
722
723   dynobj = elf_hash_table (info)->dynobj;
724   local_got_offsets = elf_local_got_offsets (abfd);
725   rel_end = relocs + sec->reloc_count;
726   for (rel = relocs; rel < rel_end; rel++)
727     {
728       struct elf_link_hash_entry *h;
729       unsigned long r_symndx;
730
731       r_symndx = ELF32_R_SYM (rel->r_info);
732       if (r_symndx < symtab_hdr->sh_info)
733         h = NULL;
734       else
735         {
736           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
737           while (h->root.type == bfd_link_hash_indirect
738                  || h->root.type == bfd_link_hash_warning)
739             h = (struct elf_link_hash_entry *) h->root.u.i.link;
740         }
741
742       /* Some relocs require a global offset table.  */
743       if (dynobj == NULL)
744         {
745           switch (ELF32_R_TYPE (rel->r_info))
746             {
747             case R_CR16_GOT_REGREL20:
748             case R_CR16_GOTC_REGREL20:
749               elf_hash_table (info)->dynobj = dynobj = abfd;
750               if (! _bfd_cr16_elf_create_got_section (dynobj, info))
751                 goto fail;
752               break;
753
754             default:
755               break;
756             }
757         }
758
759       switch (ELF32_R_TYPE (rel->r_info))
760         {
761         case R_CR16_GOT_REGREL20:
762         case R_CR16_GOTC_REGREL20:
763           /* This symbol requires a global offset table entry.  */
764
765           sgot = elf_hash_table (info)->sgot;
766           srelgot = elf_hash_table (info)->srelgot;
767           BFD_ASSERT (sgot != NULL && srelgot != NULL);
768
769           if (h != NULL)
770             {
771               if (h->got.offset != (bfd_vma) -1)
772                 /* We have already allocated space in the .got.  */
773                 break;
774
775               h->got.offset = sgot->size;
776
777               /* Make sure this symbol is output as a dynamic symbol.  */
778               if (h->dynindx == -1)
779                 {
780                   if (! bfd_elf_link_record_dynamic_symbol (info, h))
781                     goto fail;
782                 }
783
784               srelgot->size += sizeof (Elf32_External_Rela);
785             }
786           else
787             {
788               /* This is a global offset table entry for a local
789                  symbol.  */
790               if (local_got_offsets == NULL)
791                 {
792                   size_t       size;
793                   unsigned int i;
794
795                   size = symtab_hdr->sh_info * sizeof (bfd_vma);
796                   local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
797
798                   if (local_got_offsets == NULL)
799                     goto fail;
800
801                   elf_local_got_offsets (abfd) = local_got_offsets;
802
803                   for (i = 0; i < symtab_hdr->sh_info; i++)
804                     local_got_offsets[i] = (bfd_vma) -1;
805                 }
806
807               if (local_got_offsets[r_symndx] != (bfd_vma) -1)
808                 /* We have already allocated space in the .got.  */
809                 break;
810
811               local_got_offsets[r_symndx] = sgot->size;
812
813               if (bfd_link_executable (info))
814                 /* If we are generating a shared object, we need to
815                    output a R_CR16_RELATIVE reloc so that the dynamic
816                    linker can adjust this GOT entry.  */
817                 srelgot->size += sizeof (Elf32_External_Rela);
818             }
819
820           sgot->size += 4;
821           break;
822
823         }
824     }
825
826    result = TRUE;
827   fail:
828     if (isymbuf != NULL)
829       free (isymbuf);
830
831   return result;
832 }
833
834 /* Perform a relocation as part of a final link.  */
835
836 static bfd_reloc_status_type
837 cr16_elf_final_link_relocate (reloc_howto_type *howto,
838                               bfd *input_bfd,
839                               bfd *output_bfd ATTRIBUTE_UNUSED,
840                               asection *input_section,
841                               bfd_byte *contents,
842                               bfd_vma offset,
843                               bfd_vma Rvalue,
844                               bfd_vma addend,
845                               struct elf_link_hash_entry * h,
846                               unsigned long symndx  ATTRIBUTE_UNUSED,
847                               struct bfd_link_info *info ATTRIBUTE_UNUSED,
848                               asection *sec ATTRIBUTE_UNUSED,
849                               int is_local ATTRIBUTE_UNUSED)
850 {
851   unsigned short r_type = howto->type;
852   bfd_byte *hit_data = contents + offset;
853   bfd_vma reloc_bits, check, Rvalue1;
854
855   switch (r_type)
856     {
857      case R_CR16_IMM4:
858      case R_CR16_IMM20:
859      case R_CR16_ABS20:
860        break;
861
862      case R_CR16_IMM8:
863      case R_CR16_IMM16:
864      case R_CR16_IMM32:
865      case R_CR16_IMM32a:
866      case R_CR16_REGREL4:
867      case R_CR16_REGREL4a:
868      case R_CR16_REGREL14:
869      case R_CR16_REGREL14a:
870      case R_CR16_REGREL16:
871      case R_CR16_REGREL20:
872      case R_CR16_REGREL20a:
873      case R_CR16_GOT_REGREL20:
874      case R_CR16_GOTC_REGREL20:
875      case R_CR16_ABS24:
876      case R_CR16_DISP16:
877      case R_CR16_DISP24:
878        /* 'hit_data' is relative to the start of the instruction, not the
879            relocation offset. Advance it to account for the exact offset.  */
880        hit_data += 2;
881        break;
882
883      case R_CR16_NONE:
884        return bfd_reloc_ok;
885        break;
886
887      case R_CR16_DISP4:
888        if (is_local)
889         Rvalue += -1;
890        break;
891
892      case R_CR16_DISP8:
893      case R_CR16_DISP24a:
894        if (is_local)
895         Rvalue -= -1;
896        break;
897
898      case R_CR16_SWITCH8:
899      case R_CR16_SWITCH16:
900      case R_CR16_SWITCH32:
901        /* We only care about the addend, where the difference between
902           expressions is kept.  */
903        Rvalue = 0;
904
905      default:
906        break;
907     }
908
909   if (howto->pc_relative)
910     {
911       /* Subtract the address of the section containing the location.  */
912       Rvalue -= (input_section->output_section->vma
913                  + input_section->output_offset);
914       /* Subtract the position of the location within the section.  */
915       Rvalue -= offset;
916     }
917
918   /* Add in supplied addend.  */
919   Rvalue += addend;
920
921   /* Complain if the bitfield overflows, whether it is considered
922      as signed or unsigned.  */
923   check = Rvalue >> howto->rightshift;
924
925   /* Assumes two's complement.  This expression avoids
926      overflow if howto->bitsize is the number of bits in
927      bfd_vma.  */
928   reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
929
930   /* For GOT and GOTC relocs no boundary checks applied.  */
931   if (!((r_type == R_CR16_GOT_REGREL20)
932       || (r_type == R_CR16_GOTC_REGREL20)))
933     {
934       if (((bfd_vma) check & ~reloc_bits) != 0
935           && (((bfd_vma) check & ~reloc_bits)
936           != (-(bfd_vma) 1 & ~reloc_bits)))
937         {
938           /* The above right shift is incorrect for a signed
939              value.  See if turning on the upper bits fixes the
940              overflow.  */
941           if (howto->rightshift && (bfd_signed_vma) Rvalue < 0)
942             {
943               check |= ((bfd_vma) - 1
944                         & ~((bfd_vma) - 1
945                          >> howto->rightshift));
946
947               if (((bfd_vma) check & ~reloc_bits)
948                   != (-(bfd_vma) 1 & ~reloc_bits))
949                  return bfd_reloc_overflow;
950             }
951           else
952             return bfd_reloc_overflow;
953         }
954
955       /* Drop unwanted bits from the value we are relocating to.  */
956       Rvalue >>= (bfd_vma) howto->rightshift;
957
958       /* Apply dst_mask to select only relocatable part of the insn.  */
959       Rvalue &= howto->dst_mask;
960     }
961
962   switch (howto->size)
963     {
964       case 0:
965         if (r_type == R_CR16_DISP8)
966           {
967              Rvalue1 = bfd_get_16 (input_bfd, hit_data);
968              Rvalue = ((Rvalue1 & 0xf000) | ((Rvalue << 4) & 0xf00)
969                        | (Rvalue1 & 0x00f0) | (Rvalue & 0xf));
970              bfd_put_16 (input_bfd, Rvalue, hit_data);
971           }
972         else if (r_type == R_CR16_IMM4)
973           {
974              Rvalue1 = bfd_get_16 (input_bfd, hit_data);
975              Rvalue = (((Rvalue1 & 0xff) << 8) | ((Rvalue << 4) & 0xf0)
976                        | ((Rvalue1 & 0x0f00) >> 8));
977              bfd_put_16 (input_bfd, Rvalue, hit_data);
978           }
979         else if (r_type == R_CR16_DISP4)
980           {
981              Rvalue1 = bfd_get_16 (input_bfd, hit_data);
982              Rvalue = (Rvalue1 | ((Rvalue & 0xf) << 4));
983              bfd_put_16 (input_bfd, Rvalue, hit_data);
984           }
985         else
986           {
987              bfd_put_8 (input_bfd, (unsigned char) Rvalue, hit_data);
988           }
989         break;
990
991       case 1:
992         if (r_type == R_CR16_DISP16)
993           {
994             Rvalue |= (bfd_get_16 (input_bfd, hit_data));
995             Rvalue = ((Rvalue & 0xfffe) | ((Rvalue >> 16) & 0x1));
996           }
997         if (r_type == R_CR16_IMM16)
998           {
999             Rvalue1 = bfd_get_16 (input_bfd, hit_data);
1000
1001             /* Add or subtract the offset value.  */
1002             if (Rvalue1 & 0x8000)
1003               Rvalue -= (~Rvalue1 + 1) & 0xffff;
1004             else
1005               Rvalue += Rvalue1;
1006
1007              /* Check for range.  */
1008              if ((long) Rvalue > 0xffff || (long) Rvalue < 0x0)
1009               return bfd_reloc_overflow;
1010           }
1011
1012         bfd_put_16 (input_bfd, Rvalue, hit_data);
1013         break;
1014
1015       case 2:
1016         if ((r_type == R_CR16_ABS20) || (r_type == R_CR16_IMM20))
1017           {
1018              Rvalue1 = (bfd_get_16 (input_bfd, hit_data + 2)
1019                         | (((bfd_get_16 (input_bfd, hit_data) & 0xf) <<16)));
1020
1021              /* Add or subtract the offset value.  */
1022              if (Rvalue1 & 0x80000)
1023                 Rvalue -= (~Rvalue1 + 1) & 0xfffff;
1024               else
1025                 Rvalue += Rvalue1;
1026
1027               /* Check for range.  */
1028               if ((long) Rvalue > 0xfffff || (long) Rvalue < 0x0)
1029                return bfd_reloc_overflow;
1030
1031             bfd_put_16 (input_bfd, ((bfd_get_16 (input_bfd, hit_data) & 0xfff0)
1032                         | ((Rvalue >> 16) & 0xf)), hit_data);
1033             bfd_put_16 (input_bfd, (Rvalue) & 0xffff, hit_data + 2);
1034           }
1035         else if (r_type == R_CR16_GOT_REGREL20)
1036           {
1037             asection *sgot = elf_hash_table (info)->sgot;
1038
1039             if (h != NULL)
1040               {
1041                 bfd_vma off;
1042
1043                 off = h->got.offset;
1044                 BFD_ASSERT (off != (bfd_vma) -1);
1045
1046                 if (! elf_hash_table (info)->dynamic_sections_created
1047                      || SYMBOL_REFERENCES_LOCAL (info, h))
1048                     /* This is actually a static link, or it is a
1049                        -Bsymbolic link and the symbol is defined
1050                        locally, or the symbol was forced to be local
1051                        because of a version file.  We must initialize
1052                        this entry in the global offset table.
1053                        When doing a dynamic link, we create a .rela.got
1054                        relocation entry to initialize the value.  This
1055                        is done in the finish_dynamic_symbol routine.  */
1056                   bfd_put_32 (output_bfd, Rvalue, sgot->contents + off);
1057
1058                   Rvalue = sgot->output_offset + off;
1059                 }
1060               else
1061                 {
1062                    bfd_vma off;
1063
1064                    off = elf_local_got_offsets (input_bfd)[symndx];
1065                    bfd_put_32 (output_bfd,Rvalue, sgot->contents + off);
1066
1067                    Rvalue = sgot->output_offset + off;
1068                 }
1069
1070              Rvalue += addend;
1071
1072              /* REVISIT: if ((long) Rvalue > 0xffffff ||
1073                                     (long) Rvalue < -0x800000).  */
1074              if ((long) Rvalue > 0xffffff || (long) Rvalue < 0)
1075                return bfd_reloc_overflow;
1076
1077
1078              bfd_put_16 (input_bfd, (bfd_get_16 (input_bfd, hit_data))
1079                          | (((Rvalue >> 16) & 0xf) << 8), hit_data);
1080              bfd_put_16 (input_bfd, (Rvalue) & 0xffff, hit_data + 2);
1081
1082           }
1083         else if (r_type == R_CR16_GOTC_REGREL20)
1084           {
1085              asection *sgot = elf_hash_table (info)->sgot;
1086
1087              if (h != NULL)
1088                {
1089                  bfd_vma off;
1090
1091                  off = h->got.offset;
1092                  BFD_ASSERT (off != (bfd_vma) -1);
1093
1094                   Rvalue >>=1; /* For code symbols.  */
1095
1096                  if (! elf_hash_table (info)->dynamic_sections_created
1097                       || SYMBOL_REFERENCES_LOCAL (info, h))
1098                  /* This is actually a static link, or it is a
1099                     -Bsymbolic link and the symbol is defined
1100                      locally, or the symbol was forced to be local
1101                      because of a version file.  We must initialize
1102                      this entry in the global offset table.
1103                      When doing a dynamic link, we create a .rela.got
1104                      relocation entry to initialize the value.  This
1105                      is done in the finish_dynamic_symbol routine.  */
1106                   bfd_put_32 (output_bfd, Rvalue, sgot->contents + off);
1107
1108                   Rvalue = sgot->output_offset + off;
1109                }
1110              else
1111                {
1112                   bfd_vma off;
1113
1114                   off = elf_local_got_offsets (input_bfd)[symndx];
1115                   Rvalue >>= 1;
1116                   bfd_put_32 (output_bfd,Rvalue, sgot->contents + off);
1117                   Rvalue = sgot->output_offset + off;
1118                }
1119
1120              Rvalue += addend;
1121
1122              /* Check if any value in DISP.  */
1123              Rvalue1 =((bfd_get_32 (input_bfd, hit_data) >>16)
1124                        | (((bfd_get_32 (input_bfd, hit_data) & 0xfff) >> 8) <<16));
1125
1126              /* Add or subtract the offset value.  */
1127              if (Rvalue1 & 0x80000)
1128                Rvalue -= (~Rvalue1 + 1) & 0xfffff;
1129              else
1130                Rvalue += Rvalue1;
1131
1132               /* Check for range.  */
1133              /* REVISIT: if ((long) Rvalue > 0xffffff
1134                              || (long) Rvalue < -0x800000).  */
1135              if ((long) Rvalue > 0xffffff || (long) Rvalue < 0)
1136                return bfd_reloc_overflow;
1137
1138              bfd_put_16 (input_bfd, (bfd_get_16 (input_bfd, hit_data))
1139                          | (((Rvalue >> 16) & 0xf) << 8), hit_data);
1140              bfd_put_16 (input_bfd, (Rvalue) & 0xffff, hit_data + 2);
1141           }
1142         else
1143           {
1144              if (r_type == R_CR16_ABS24)
1145                {
1146                   Rvalue1 = ((bfd_get_32 (input_bfd, hit_data) >> 16)
1147                              | (((bfd_get_32 (input_bfd, hit_data) & 0xfff) >> 8) <<16)
1148                              | (((bfd_get_32 (input_bfd, hit_data) & 0xf) <<20)));
1149
1150                   /* Add or subtract the offset value.  */
1151                   if (Rvalue1 & 0x800000)
1152                     Rvalue -= (~Rvalue1 + 1) & 0xffffff;
1153                   else
1154                     Rvalue += Rvalue1;
1155
1156                  /* Check for Range.  */
1157                  if ((long) Rvalue > 0xffffff || (long) Rvalue < 0x0)
1158                    return bfd_reloc_overflow;
1159
1160                  Rvalue = ((((Rvalue >> 20) & 0xf) | (((Rvalue >> 16) & 0xf)<<8)
1161                            | (bfd_get_32 (input_bfd, hit_data) & 0xf0f0))
1162                            | ((Rvalue & 0xffff) << 16));
1163                }
1164              else if (r_type == R_CR16_DISP24)
1165                {
1166                   Rvalue = ((((Rvalue >> 20)& 0xf) | (((Rvalue >>16) & 0xf)<<8)
1167                             | (bfd_get_16 (input_bfd, hit_data)))
1168                             | (((Rvalue & 0xfffe) | ((Rvalue >> 24) & 0x1)) << 16));
1169                }
1170              else if ((r_type == R_CR16_IMM32) || (r_type == R_CR16_IMM32a))
1171                {
1172                   Rvalue1 =((((bfd_get_32 (input_bfd, hit_data)) >> 16) &0xffff)
1173                             | (((bfd_get_32 (input_bfd, hit_data)) &0xffff)) << 16);
1174
1175                  /* Add or subtract the offset value.  */
1176                  if (Rvalue1 & 0x80000000)
1177                    Rvalue -= (~Rvalue1 + 1) & 0xffffffff;
1178                  else
1179                    Rvalue += Rvalue1;
1180
1181                  /* Check for range.  */
1182                  if (Rvalue > 0xffffffff || (long) Rvalue < 0x0)
1183                    return bfd_reloc_overflow;
1184
1185                  Rvalue = (((Rvalue >> 16)& 0xffff) | (Rvalue & 0xffff) << 16);
1186                }
1187              else if (r_type == R_CR16_DISP24a)
1188                {
1189                   Rvalue = (((Rvalue & 0xfffffe) | (Rvalue >> 23)));
1190                   Rvalue = ((Rvalue >> 16) & 0xff) | ((Rvalue & 0xffff) << 16)
1191                             | (bfd_get_32 (input_bfd, hit_data));
1192                }
1193              else if ((r_type == R_CR16_REGREL20)
1194                       || (r_type == R_CR16_REGREL20a))
1195                {
1196                   Rvalue1 = ((bfd_get_32 (input_bfd, hit_data) >> 16)
1197                              | (((bfd_get_32 (input_bfd, hit_data) & 0xfff) >> 8) <<16));
1198                   /* Add or subtract the offset value.  */
1199                   if (Rvalue1 & 0x80000)
1200                      Rvalue -= (~Rvalue1 + 1) & 0xfffff;
1201                   else
1202                      Rvalue += Rvalue1;
1203
1204                   /* Check for range.  */
1205                   if ((long) Rvalue > 0xfffff || (long) Rvalue < 0x0)
1206                     return bfd_reloc_overflow;
1207
1208                   Rvalue = (((((Rvalue >> 20)& 0xf) | (((Rvalue >>16) & 0xf)<<8)
1209                             | ((Rvalue & 0xffff) << 16)))
1210                             | (bfd_get_32 (input_bfd, hit_data) & 0xf0ff));
1211
1212               }
1213             else if (r_type == R_CR16_NUM32)
1214               {
1215                  Rvalue1 = (bfd_get_32 (input_bfd, hit_data));
1216
1217                  /* Add or subtract the offset value */
1218                  if (Rvalue1 & 0x80000000)
1219                    Rvalue -= (~Rvalue1 + 1) & 0xffffffff;
1220                  else
1221                    Rvalue += Rvalue1;
1222
1223                 /* Check for Ranga */
1224                 if (Rvalue > 0xffffffff)
1225                   return bfd_reloc_overflow;
1226               }
1227
1228             bfd_put_32 (input_bfd, Rvalue, hit_data);
1229           }
1230         break;
1231
1232       default:
1233         return bfd_reloc_notsupported;
1234     }
1235
1236   return bfd_reloc_ok;
1237 }
1238
1239 /* Delete some bytes from a section while relaxing.  */
1240
1241 static bfd_boolean
1242 elf32_cr16_relax_delete_bytes (struct bfd_link_info *link_info, bfd *abfd,
1243                                asection *sec, bfd_vma addr, int count)
1244 {
1245   Elf_Internal_Shdr *symtab_hdr;
1246   unsigned int sec_shndx;
1247   bfd_byte *contents;
1248   Elf_Internal_Rela *irel, *irelend;
1249   bfd_vma toaddr;
1250   Elf_Internal_Sym *isym;
1251   Elf_Internal_Sym *isymend;
1252   struct elf_link_hash_entry **sym_hashes;
1253   struct elf_link_hash_entry **end_hashes;
1254   struct elf_link_hash_entry **start_hashes;
1255   unsigned int symcount;
1256
1257   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1258
1259   contents = elf_section_data (sec)->this_hdr.contents;
1260
1261   toaddr = sec->size;
1262
1263   irel = elf_section_data (sec)->relocs;
1264   irelend = irel + sec->reloc_count;
1265
1266   /* Actually delete the bytes.  */
1267   memmove (contents + addr, contents + addr + count,
1268            (size_t) (toaddr - addr - count));
1269   sec->size -= count;
1270
1271   /* Adjust all the relocs.  */
1272   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1273     /* Get the new reloc address.  */
1274     if ((irel->r_offset > addr && irel->r_offset < toaddr))
1275         irel->r_offset -= count;
1276
1277   /* Adjust the local symbols defined in this section.  */
1278   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1279   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1280   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1281     {
1282       if (isym->st_shndx == sec_shndx
1283           && isym->st_value > addr
1284           && isym->st_value < toaddr)
1285         {
1286           /* Adjust the addend of SWITCH relocations in this section,
1287              which reference this local symbol.  */
1288 #if 0
1289           for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1290             {
1291               unsigned long r_symndx;
1292               Elf_Internal_Sym *rsym;
1293               bfd_vma addsym, subsym;
1294
1295               /* Skip if not a SWITCH relocation.  */
1296               if (ELF32_R_TYPE (irel->r_info) != (int) R_CR16_SWITCH8
1297                   && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_SWITCH16
1298                   && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_SWITCH32)
1299                  continue;
1300
1301               r_symndx = ELF32_R_SYM (irel->r_info);
1302               rsym = (Elf_Internal_Sym *) symtab_hdr->contents + r_symndx;
1303
1304               /* Skip if not the local adjusted symbol.  */
1305               if (rsym != isym)
1306                 continue;
1307
1308               addsym = isym->st_value;
1309               subsym = addsym - irel->r_addend;
1310
1311               /* Fix the addend only when -->> (addsym > addr >= subsym).  */
1312               if (subsym <= addr)
1313                 irel->r_addend -= count;
1314               else
1315                 continue;
1316             }
1317 #endif
1318
1319           isym->st_value -= count;
1320         }
1321     }
1322
1323   /* Now adjust the global symbols defined in this section.  */
1324   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1325                - symtab_hdr->sh_info);
1326   sym_hashes = start_hashes = elf_sym_hashes (abfd);
1327   end_hashes = sym_hashes + symcount;
1328
1329   for (; sym_hashes < end_hashes; sym_hashes++)
1330     {
1331       struct elf_link_hash_entry *sym_hash = *sym_hashes;
1332
1333       /* The '--wrap SYMBOL' option is causing a pain when the object file,
1334          containing the definition of __wrap_SYMBOL, includes a direct
1335          call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
1336          the same symbol (which is __wrap_SYMBOL), but still exist as two
1337          different symbols in 'sym_hashes', we don't want to adjust
1338          the global symbol __wrap_SYMBOL twice.
1339          This check is only relevant when symbols are being wrapped.  */
1340       if (link_info->wrap_hash != NULL)
1341         {
1342           struct elf_link_hash_entry **cur_sym_hashes;
1343
1344           /* Loop only over the symbols whom been already checked.  */
1345           for (cur_sym_hashes = start_hashes; cur_sym_hashes < sym_hashes;
1346                cur_sym_hashes++)
1347             /* If the current symbol is identical to 'sym_hash', that means
1348                the symbol was already adjusted (or at least checked).  */
1349             if (*cur_sym_hashes == sym_hash)
1350               break;
1351
1352           /* Don't adjust the symbol again.  */
1353           if (cur_sym_hashes < sym_hashes)
1354             continue;
1355         }
1356
1357       if ((sym_hash->root.type == bfd_link_hash_defined
1358           || sym_hash->root.type == bfd_link_hash_defweak)
1359           && sym_hash->root.u.def.section == sec
1360           && sym_hash->root.u.def.value > addr
1361           && sym_hash->root.u.def.value < toaddr)
1362         sym_hash->root.u.def.value -= count;
1363     }
1364
1365   return TRUE;
1366 }
1367
1368 /* Relocate a CR16 ELF section.  */
1369
1370 static bfd_boolean
1371 elf32_cr16_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
1372                              bfd *input_bfd, asection *input_section,
1373                              bfd_byte *contents, Elf_Internal_Rela *relocs,
1374                              Elf_Internal_Sym *local_syms,
1375                              asection **local_sections)
1376 {
1377   Elf_Internal_Shdr *symtab_hdr;
1378   struct elf_link_hash_entry **sym_hashes;
1379   Elf_Internal_Rela *rel, *relend;
1380
1381   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1382   sym_hashes = elf_sym_hashes (input_bfd);
1383
1384   rel = relocs;
1385   relend = relocs + input_section->reloc_count;
1386   for (; rel < relend; rel++)
1387     {
1388       int r_type;
1389       reloc_howto_type *howto;
1390       unsigned long r_symndx;
1391       Elf_Internal_Sym *sym;
1392       asection *sec;
1393       struct elf_link_hash_entry *h;
1394       bfd_vma relocation;
1395       bfd_reloc_status_type r;
1396
1397       r_symndx = ELF32_R_SYM (rel->r_info);
1398       r_type = ELF32_R_TYPE (rel->r_info);
1399       howto = cr16_elf_howto_table + (r_type);
1400
1401       h = NULL;
1402       sym = NULL;
1403       sec = NULL;
1404       if (r_symndx < symtab_hdr->sh_info)
1405         {
1406           sym = local_syms + r_symndx;
1407           sec = local_sections[r_symndx];
1408           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1409         }
1410       else
1411         {
1412           bfd_boolean unresolved_reloc, warned, ignored;
1413
1414           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1415                                    r_symndx, symtab_hdr, sym_hashes,
1416                                    h, sec, relocation,
1417                                    unresolved_reloc, warned, ignored);
1418         }
1419
1420       if (sec != NULL && discarded_section (sec))
1421         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1422                                          rel, 1, relend, howto, 0, contents);
1423
1424       if (bfd_link_relocatable (info))
1425         continue;
1426
1427       r = cr16_elf_final_link_relocate (howto, input_bfd, output_bfd,
1428                                         input_section,
1429                                         contents, rel->r_offset,
1430                                         relocation, rel->r_addend,
1431                                         (struct elf_link_hash_entry *) h,
1432                                         r_symndx,
1433                                         info, sec, h == NULL);
1434
1435       if (r != bfd_reloc_ok)
1436         {
1437           const char *name;
1438           const char *msg = NULL;
1439
1440           if (h != NULL)
1441             name = h->root.root.string;
1442           else
1443             {
1444               name = (bfd_elf_string_from_elf_section
1445                       (input_bfd, symtab_hdr->sh_link, sym->st_name));
1446               if (name == NULL || *name == '\0')
1447                 name = bfd_section_name (input_bfd, sec);
1448             }
1449
1450           switch (r)
1451             {
1452              case bfd_reloc_overflow:
1453                (*info->callbacks->reloc_overflow)
1454                  (info, (h ? &h->root : NULL), name, howto->name,
1455                   (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1456                break;
1457
1458              case bfd_reloc_undefined:
1459                (*info->callbacks->undefined_symbol)
1460                  (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1461                break;
1462
1463              case bfd_reloc_outofrange:
1464                msg = _("internal error: out of range error");
1465                goto common_error;
1466
1467              case bfd_reloc_notsupported:
1468                msg = _("internal error: unsupported relocation error");
1469                goto common_error;
1470
1471              case bfd_reloc_dangerous:
1472                msg = _("internal error: dangerous error");
1473                goto common_error;
1474
1475              default:
1476                msg = _("internal error: unknown error");
1477                /* Fall through.  */
1478
1479              common_error:
1480                (*info->callbacks->warning) (info, msg, name, input_bfd,
1481                                             input_section, rel->r_offset);
1482                break;
1483             }
1484         }
1485     }
1486
1487   return TRUE;
1488 }
1489
1490 /* This is a version of bfd_generic_get_relocated_section_contents
1491    which uses elf32_cr16_relocate_section.  */
1492
1493 static bfd_byte *
1494 elf32_cr16_get_relocated_section_contents (bfd *output_bfd,
1495                                            struct bfd_link_info *link_info,
1496                                            struct bfd_link_order *link_order,
1497                                            bfd_byte *data,
1498                                            bfd_boolean relocatable,
1499                                            asymbol **symbols)
1500 {
1501   Elf_Internal_Shdr *symtab_hdr;
1502   asection *input_section = link_order->u.indirect.section;
1503   bfd *input_bfd = input_section->owner;
1504   asection **sections = NULL;
1505   Elf_Internal_Rela *internal_relocs = NULL;
1506   Elf_Internal_Sym *isymbuf = NULL;
1507
1508   /* We only need to handle the case of relaxing, or of having a
1509      particular set of section contents, specially.  */
1510   if (relocatable
1511       || elf_section_data (input_section)->this_hdr.contents == NULL)
1512     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1513                                                        link_order, data,
1514                                                        relocatable,
1515                                                        symbols);
1516
1517   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1518
1519   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1520           (size_t) input_section->size);
1521
1522   if ((input_section->flags & SEC_RELOC) != 0
1523       && input_section->reloc_count > 0)
1524     {
1525       Elf_Internal_Sym *isym;
1526       Elf_Internal_Sym *isymend;
1527       asection **secpp;
1528       bfd_size_type amt;
1529
1530       internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
1531                                                    NULL, NULL, FALSE);
1532       if (internal_relocs == NULL)
1533         goto error_return;
1534
1535       if (symtab_hdr->sh_info != 0)
1536         {
1537           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1538           if (isymbuf == NULL)
1539             isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1540                                             symtab_hdr->sh_info, 0,
1541                                             NULL, NULL, NULL);
1542           if (isymbuf == NULL)
1543             goto error_return;
1544         }
1545
1546       amt = symtab_hdr->sh_info;
1547       amt *= sizeof (asection *);
1548       sections = bfd_malloc (amt);
1549       if (sections == NULL && amt != 0)
1550         goto error_return;
1551
1552       isymend = isymbuf + symtab_hdr->sh_info;
1553       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
1554         {
1555           asection *isec;
1556
1557           if (isym->st_shndx == SHN_UNDEF)
1558             isec = bfd_und_section_ptr;
1559           else if (isym->st_shndx == SHN_ABS)
1560             isec = bfd_abs_section_ptr;
1561           else if (isym->st_shndx == SHN_COMMON)
1562             isec = bfd_com_section_ptr;
1563           else
1564             isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
1565
1566           *secpp = isec;
1567         }
1568
1569       if (! elf32_cr16_relocate_section (output_bfd, link_info, input_bfd,
1570                                      input_section, data, internal_relocs,
1571                                      isymbuf, sections))
1572         goto error_return;
1573
1574       if (sections != NULL)
1575         free (sections);
1576       if (isymbuf != NULL
1577           && symtab_hdr->contents != (unsigned char *) isymbuf)
1578         free (isymbuf);
1579       if (elf_section_data (input_section)->relocs != internal_relocs)
1580         free (internal_relocs);
1581     }
1582
1583   return data;
1584
1585  error_return:
1586   if (sections != NULL)
1587     free (sections);
1588   if (isymbuf != NULL
1589       && symtab_hdr->contents != (unsigned char *) isymbuf)
1590     free (isymbuf);
1591   if (internal_relocs != NULL
1592       && elf_section_data (input_section)->relocs != internal_relocs)
1593     free (internal_relocs);
1594   return NULL;
1595 }
1596
1597 /* Assorted hash table functions.  */
1598
1599 /* Initialize an entry in the link hash table.  */
1600
1601 /* Create an entry in an CR16 ELF linker hash table.  */
1602
1603 static struct bfd_hash_entry *
1604 elf32_cr16_link_hash_newfunc (struct bfd_hash_entry *entry,
1605                               struct bfd_hash_table *table,
1606                               const char *string)
1607 {
1608   struct elf32_cr16_link_hash_entry *ret =
1609     (struct elf32_cr16_link_hash_entry *) entry;
1610
1611   /* Allocate the structure if it has not already been allocated by a
1612      subclass.  */
1613   if (ret == (struct elf32_cr16_link_hash_entry *) NULL)
1614     ret = ((struct elf32_cr16_link_hash_entry *)
1615            bfd_hash_allocate (table,
1616                               sizeof (struct elf32_cr16_link_hash_entry)));
1617   if (ret == (struct elf32_cr16_link_hash_entry *) NULL)
1618     return (struct bfd_hash_entry *) ret;
1619
1620   /* Call the allocation method of the superclass.  */
1621   ret = ((struct elf32_cr16_link_hash_entry *)
1622          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1623                                      table, string));
1624   if (ret != (struct elf32_cr16_link_hash_entry *) NULL)
1625     {
1626       ret->direct_calls = 0;
1627       ret->stack_size = 0;
1628       ret->movm_args = 0;
1629       ret->movm_stack_size = 0;
1630       ret->flags = 0;
1631       ret->value = 0;
1632     }
1633
1634   return (struct bfd_hash_entry *) ret;
1635 }
1636
1637 /* Create an cr16 ELF linker hash table.  */
1638
1639 static struct bfd_link_hash_table *
1640 elf32_cr16_link_hash_table_create (bfd *abfd)
1641 {
1642   struct elf_link_hash_table *ret;
1643   bfd_size_type amt = sizeof (struct elf_link_hash_table);
1644
1645   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
1646   if (ret == (struct elf_link_hash_table *) NULL)
1647     return NULL;
1648
1649   if (!_bfd_elf_link_hash_table_init (ret, abfd,
1650                                       elf32_cr16_link_hash_newfunc,
1651                                       sizeof (struct elf32_cr16_link_hash_entry),
1652                                       GENERIC_ELF_DATA))
1653     {
1654       free (ret);
1655       return NULL;
1656     }
1657
1658   return &ret->root;
1659 }
1660
1661 static unsigned long
1662 elf_cr16_mach (flagword flags)
1663 {
1664   switch (flags)
1665     {
1666       case EM_CR16:
1667       default:
1668       return bfd_mach_cr16;
1669     }
1670 }
1671
1672 /* The final processing done just before writing out a CR16 ELF object
1673    file.  This gets the CR16 architecture right based on the machine
1674    number.  */
1675
1676 static bfd_boolean
1677 _bfd_cr16_elf_final_write_processing (bfd *abfd)
1678 {
1679   unsigned long val;
1680   switch (bfd_get_mach (abfd))
1681     {
1682      default:
1683      case bfd_mach_cr16:
1684         val = EM_CR16;
1685         break;
1686     }
1687   elf_elfheader (abfd)->e_flags |= val;
1688   return _bfd_elf_final_write_processing (abfd);
1689 }
1690
1691
1692 static bfd_boolean
1693 _bfd_cr16_elf_object_p (bfd *abfd)
1694 {
1695   bfd_default_set_arch_mach (abfd, bfd_arch_cr16,
1696                              elf_cr16_mach (elf_elfheader (abfd)->e_flags));
1697   return TRUE;
1698 }
1699
1700 /* Merge backend specific data from an object file to the output
1701    object file when linking.  */
1702
1703 static bfd_boolean
1704 _bfd_cr16_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
1705 {
1706   bfd *obfd = info->output_bfd;
1707
1708   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1709       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1710     return TRUE;
1711
1712   if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1713       && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
1714     {
1715       if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1716                                bfd_get_mach (ibfd)))
1717          return FALSE;
1718      }
1719
1720   return TRUE;
1721 }
1722
1723
1724 /* This function handles relaxing for the CR16.
1725
1726    There's quite a few relaxing opportunites available on the CR16:
1727
1728         * bcond:24 -> bcond:16                                1 byte
1729         * bcond:16 -> bcond:8                                 1 byte
1730         * arithmetic imm32 -> arithmetic imm20                12 bits
1731         * arithmetic imm20/imm16 -> arithmetic imm4           12/16 bits
1732
1733    Symbol- and reloc-reading infrastructure copied from elf-m10200.c.  */
1734
1735 static bfd_boolean
1736 elf32_cr16_relax_section (bfd *abfd, asection *sec,
1737                           struct bfd_link_info *link_info, bfd_boolean *again)
1738 {
1739   Elf_Internal_Shdr *symtab_hdr;
1740   Elf_Internal_Rela *internal_relocs;
1741   Elf_Internal_Rela *irel, *irelend;
1742   bfd_byte *contents = NULL;
1743   Elf_Internal_Sym *isymbuf = NULL;
1744
1745   /* Assume nothing changes.  */
1746   *again = FALSE;
1747
1748   /* We don't have to do anything for a relocatable link, if
1749      this section does not have relocs, or if this is not a
1750      code section.  */
1751   if (bfd_link_relocatable (link_info)
1752       || (sec->flags & SEC_RELOC) == 0
1753       || sec->reloc_count == 0
1754       || (sec->flags & SEC_CODE) == 0)
1755     return TRUE;
1756
1757   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1758
1759   /* Get a copy of the native relocations.  */
1760   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
1761                                                link_info->keep_memory);
1762   if (internal_relocs == NULL)
1763     goto error_return;
1764
1765   /* Walk through them looking for relaxing opportunities.  */
1766   irelend = internal_relocs + sec->reloc_count;
1767   for (irel = internal_relocs; irel < irelend; irel++)
1768     {
1769       bfd_vma symval;
1770
1771       /* If this isn't something that can be relaxed, then ignore
1772          this reloc.  */
1773       if (ELF32_R_TYPE (irel->r_info) != (int) R_CR16_DISP16
1774           && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_DISP24
1775           && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_IMM32
1776           && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_IMM20
1777           && ELF32_R_TYPE (irel->r_info) != (int) R_CR16_IMM16)
1778         continue;
1779
1780       /* Get the section contents if we haven't done so already.  */
1781       if (contents == NULL)
1782         {
1783           /* Get cached copy if it exists.  */
1784           if (elf_section_data (sec)->this_hdr.contents != NULL)
1785             contents = elf_section_data (sec)->this_hdr.contents;
1786           /* Go get them off disk.  */
1787           else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1788             goto error_return;
1789         }
1790
1791       /* Read this BFD's local symbols if we haven't done so already.  */
1792       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1793         {
1794           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1795           if (isymbuf == NULL)
1796             isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1797                                             symtab_hdr->sh_info, 0,
1798                                             NULL, NULL, NULL);
1799           if (isymbuf == NULL)
1800             goto error_return;
1801         }
1802
1803       /* Get the value of the symbol referred to by the reloc.  */
1804       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1805         {
1806           /* A local symbol.  */
1807           Elf_Internal_Sym *isym;
1808           asection *sym_sec;
1809
1810           isym = isymbuf + ELF32_R_SYM (irel->r_info);
1811           if (isym->st_shndx == SHN_UNDEF)
1812             sym_sec = bfd_und_section_ptr;
1813           else if (isym->st_shndx == SHN_ABS)
1814             sym_sec = bfd_abs_section_ptr;
1815           else if (isym->st_shndx == SHN_COMMON)
1816             sym_sec = bfd_com_section_ptr;
1817           else
1818             sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1819           symval = (isym->st_value
1820                     + sym_sec->output_section->vma
1821                     + sym_sec->output_offset);
1822         }
1823       else
1824         {
1825           unsigned long indx;
1826           struct elf_link_hash_entry *h;
1827
1828           /* An external symbol.  */
1829           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1830           h = elf_sym_hashes (abfd)[indx];
1831           BFD_ASSERT (h != NULL);
1832
1833           if (h->root.type != bfd_link_hash_defined
1834               && h->root.type != bfd_link_hash_defweak)
1835             /* This appears to be a reference to an undefined
1836                symbol.  Just ignore it--it will be caught by the
1837                regular reloc processing.  */
1838             continue;
1839
1840           symval = (h->root.u.def.value
1841                     + h->root.u.def.section->output_section->vma
1842                     + h->root.u.def.section->output_offset);
1843         }
1844
1845       /* For simplicity of coding, we are going to modify the section
1846          contents, the section relocs, and the BFD symbol table.  We
1847          must tell the rest of the code not to free up this
1848          information.  It would be possible to instead create a table
1849          of changes which have to be made, as is done in coff-mips.c;
1850          that would be more work, but would require less memory when
1851          the linker is run.  */
1852
1853       /* Try to turn a 24  branch/call into a 16bit relative
1854          branch/call.  */
1855       if (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_DISP24)
1856         {
1857           bfd_vma value = symval;
1858
1859           /* Deal with pc-relative gunk.  */
1860           value -= (sec->output_section->vma + sec->output_offset);
1861           value -= irel->r_offset;
1862           value += irel->r_addend;
1863
1864           /* See if the value will fit in 16 bits, note the high value is
1865              0xfffe + 2 as the target will be two bytes closer if we are
1866              able to relax.  */
1867           if ((long) value < 0x10000 && (long) value > -0x10002)
1868             {
1869               unsigned int code;
1870
1871               /* Get the opcode.  */
1872               code = (unsigned int) bfd_get_32 (abfd, contents + irel->r_offset);
1873
1874               /* Verify it's a 'bcond' and fix the opcode.  */
1875               if ((code  & 0xffff) == 0x0010)
1876                 bfd_put_16 (abfd, 0x1800 | ((0xf & (code >> 20)) << 4), contents + irel->r_offset);
1877               else
1878                 continue;
1879
1880               /* Note that we've changed the relocs, section contents, etc.  */
1881               elf_section_data (sec)->relocs = internal_relocs;
1882               elf_section_data (sec)->this_hdr.contents = contents;
1883               symtab_hdr->contents = (unsigned char *) isymbuf;
1884
1885               /* Fix the relocation's type.  */
1886               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1887                                            R_CR16_DISP16);
1888
1889               /* Delete two bytes of data.  */
1890               if (!elf32_cr16_relax_delete_bytes (link_info, abfd, sec,
1891                                                    irel->r_offset + 2, 2))
1892                 goto error_return;
1893
1894               /* That will change things, so, we should relax again.
1895                  Note that this is not required, and it may be slow.  */
1896               *again = TRUE;
1897             }
1898         }
1899
1900       /* Try to turn a 16bit pc-relative branch into an
1901          8bit pc-relative branch.  */
1902       if (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_DISP16)
1903         {
1904           bfd_vma value = symval;
1905
1906           /* Deal with pc-relative gunk.  */
1907           value -= (sec->output_section->vma + sec->output_offset);
1908           value -= irel->r_offset;
1909           value += irel->r_addend;
1910
1911           /* See if the value will fit in 8 bits, note the high value is
1912              0xfc + 2 as the target will be two bytes closer if we are
1913              able to relax.  */
1914           /*if ((long) value < 0x1fa && (long) value > -0x100) REVISIT:range */
1915           if ((long) value < 0xfa && (long) value > -0x100)
1916             {
1917               unsigned short code;
1918
1919               /* Get the opcode.  */
1920               code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
1921
1922               /* Verify it's a 'bcond' and fix the opcode.  */
1923               if ((code & 0xff0f) == 0x1800)
1924                 bfd_put_16 (abfd, (code & 0xf0f0), contents + irel->r_offset);
1925               else
1926                 continue;
1927
1928               /* Note that we've changed the relocs, section contents, etc.  */
1929               elf_section_data (sec)->relocs = internal_relocs;
1930               elf_section_data (sec)->this_hdr.contents = contents;
1931               symtab_hdr->contents = (unsigned char *) isymbuf;
1932
1933               /* Fix the relocation's type.  */
1934               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1935                                            R_CR16_DISP8);
1936
1937               /* Delete two bytes of data.  */
1938               if (!elf32_cr16_relax_delete_bytes (link_info, abfd, sec,
1939                                                    irel->r_offset + 2, 2))
1940                 goto error_return;
1941
1942               /* That will change things, so, we should relax again.
1943                  Note that this is not required, and it may be slow.  */
1944               *again = TRUE;
1945             }
1946         }
1947
1948       /* Try to turn a 32-bit IMM address into a 20/16-bit IMM address */
1949       if (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_IMM32)
1950         {
1951           bfd_vma value = symval;
1952           unsigned short is_add_mov = 0;
1953           bfd_vma value1 = 0;
1954
1955           /* Get the existing value from the mcode */
1956           value1 = ((bfd_get_32 (abfd, contents + irel->r_offset + 2) >> 16)
1957                    |(((bfd_get_32 (abfd, contents + irel->r_offset + 2) & 0xffff) << 16)));
1958
1959           /* See if the value will fit in 20 bits.  */
1960           if ((long) (value + value1) < 0xfffff && (long) (value + value1) > 0)
1961             {
1962               unsigned short code;
1963
1964               /* Get the opcode.  */
1965               code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
1966
1967               /* Verify it's a 'arithmetic ADDD or MOVD instruction'.
1968                  For ADDD and MOVD only, convert to IMM32 -> IMM20.  */
1969
1970               if (((code & 0xfff0) == 0x0070) || ((code & 0xfff0) == 0x0020))
1971                  is_add_mov = 1;
1972
1973               if (is_add_mov)
1974                 {
1975                   /* Note that we've changed the relocs, section contents,
1976                      etc.  */
1977                   elf_section_data (sec)->relocs = internal_relocs;
1978                   elf_section_data (sec)->this_hdr.contents = contents;
1979                   symtab_hdr->contents = (unsigned char *) isymbuf;
1980
1981                   /* Fix the opcode.  */
1982                   if ((code & 0xfff0) == 0x0070) /* For movd.  */
1983                     bfd_put_8 (abfd, 0x05, contents + irel->r_offset + 1);
1984                   else                           /* code == 0x0020 for addd.  */
1985                     bfd_put_8 (abfd, 0x04, contents + irel->r_offset + 1);
1986
1987                   bfd_put_8 (abfd, (code & 0xf) << 4, contents + irel->r_offset);
1988
1989                   /* If existing value is nagavive adjust approriately
1990                      place the 16-20bits (ie 4 bit) in new opcode,
1991                      as the 0xffffxxxx, the higher 2 byte values removed. */
1992                   if (value1 & 0x80000000)
1993                     bfd_put_8 (abfd, (0x0f | (bfd_get_8(abfd, contents + irel->r_offset))), contents + irel->r_offset);
1994                   else
1995                     bfd_put_8 (abfd, (((value1 >> 16)&0xf) | (bfd_get_8(abfd, contents + irel->r_offset))), contents + irel->r_offset);
1996
1997                   /* Fix the relocation's type.  */
1998                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1999                                                R_CR16_IMM20);
2000
2001                   /* Delete two bytes of data.  */
2002                   if (!elf32_cr16_relax_delete_bytes (link_info, abfd, sec,
2003                                                       irel->r_offset + 2, 2))
2004                     goto error_return;
2005
2006                   /* That will change things, so, we should relax again.
2007                      Note that this is not required, and it may be slow.  */
2008                   *again = TRUE;
2009                 }
2010             }
2011
2012           /* See if the value will fit in 16 bits.  */
2013           if ((!is_add_mov)
2014               && ((long)(value + value1) < 0x7fff && (long)(value + value1) > 0))
2015             {
2016               unsigned short code;
2017
2018               /* Get the opcode.  */
2019               code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
2020
2021               /* Note that we've changed the relocs, section contents, etc.  */
2022               elf_section_data (sec)->relocs = internal_relocs;
2023               elf_section_data (sec)->this_hdr.contents = contents;
2024               symtab_hdr->contents = (unsigned char *) isymbuf;
2025
2026               /* Fix the opcode.  */
2027               if ((code & 0xf0) == 0x70)          /* For movd.  */
2028                 bfd_put_8 (abfd, 0x54, contents + irel->r_offset + 1);
2029               else if ((code & 0xf0) == 0x20)     /* For addd.  */
2030                 bfd_put_8 (abfd, 0x60, contents + irel->r_offset + 1);
2031               else if ((code & 0xf0) == 0x90)     /* For cmpd.  */
2032                 bfd_put_8 (abfd, 0x56, contents + irel->r_offset + 1);
2033               else
2034                 continue;
2035
2036               bfd_put_8 (abfd, 0xb0 | (code & 0xf), contents + irel->r_offset);
2037
2038               /* If existing value is nagavive adjust approriately
2039                  place the 12-16bits (ie 4 bit) in new opcode,
2040                  as the 0xfffffxxx, the higher 2 byte values removed. */
2041               if (value1 & 0x80000000)
2042                 bfd_put_8 (abfd, (0x0f | (bfd_get_8(abfd, contents + irel->r_offset))), contents + irel->r_offset);
2043               else
2044                 bfd_put_16 (abfd, value1, contents + irel->r_offset + 2);
2045
2046
2047               /* Fix the relocation's type.  */
2048               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2049                                            R_CR16_IMM16);
2050
2051               /* Delete two bytes of data.  */
2052               if (!elf32_cr16_relax_delete_bytes (link_info, abfd, sec,
2053                                                   irel->r_offset + 2, 2))
2054                 goto error_return;
2055
2056               /* That will change things, so, we should relax again.
2057                  Note that this is not required, and it may be slow.  */
2058               *again = TRUE;
2059             }
2060         }
2061
2062 #if 0
2063       /* Try to turn a 16bit immediate address into a 4bit
2064          immediate address.  */
2065       if ((ELF32_R_TYPE (irel->r_info) == (int) R_CR16_IMM20)
2066           || (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_IMM16))
2067         {
2068           bfd_vma value = symval;
2069           bfd_vma value1 = 0;
2070
2071           /* Get the existing value from the mcode */
2072           value1 = ((bfd_get_16 (abfd, contents + irel->r_offset + 2) & 0xffff));
2073
2074           if (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_IMM20)
2075             {
2076               value1 |= ((bfd_get_16 (abfd, contents + irel->r_offset + 1) & 0xf000) << 0x4);
2077             }
2078
2079           /* See if the value will fit in 4 bits.  */
2080           if ((((long) (value + value1)) < 0xf)
2081               && (((long) (value + value1)) > 0))
2082             {
2083               unsigned short code;
2084
2085               /* Get the opcode.  */
2086               code = (unsigned short) bfd_get_16 (abfd, contents + irel->r_offset);
2087
2088               /* Note that we've changed the relocs, section contents, etc.  */
2089               elf_section_data (sec)->relocs = internal_relocs;
2090               elf_section_data (sec)->this_hdr.contents = contents;
2091               symtab_hdr->contents = (unsigned char *) isymbuf;
2092
2093               /* Fix the opcode.  */
2094               if (((code & 0x0f00) == 0x0400) || ((code & 0x0f00) == 0x0500))
2095                 {
2096                   if ((code & 0x0f00) == 0x0400)      /* For movd imm20.  */
2097                     bfd_put_8 (abfd, 0x60, contents + irel->r_offset);
2098                   else                                /* For addd imm20.  */
2099                     bfd_put_8 (abfd, 0x54, contents + irel->r_offset);
2100                   bfd_put_8 (abfd, (code & 0xf0) >> 4, contents + irel->r_offset + 1);
2101                 }
2102               else
2103                 {
2104                   if ((code & 0xfff0) == 0x56b0)       /*  For cmpd imm16.  */
2105                     bfd_put_8 (abfd, 0x56, contents + irel->r_offset);
2106                   else if ((code & 0xfff0) == 0x54b0)  /*  For movd imm16.  */
2107                     bfd_put_8 (abfd, 0x54, contents + irel->r_offset);
2108                   else if ((code & 0xfff0) == 0x58b0)  /*  For movb imm16.  */
2109                     bfd_put_8 (abfd, 0x58, contents + irel->r_offset);
2110                   else if ((code & 0xfff0) == 0x5Ab0)  /*  For movw imm16.  */
2111                     bfd_put_8 (abfd, 0x5A, contents + irel->r_offset);
2112                   else if ((code & 0xfff0) == 0x60b0)  /*  For addd imm16.  */
2113                     bfd_put_8 (abfd, 0x60, contents + irel->r_offset);
2114                   else if ((code & 0xfff0) == 0x30b0)  /*  For addb imm16.  */
2115                     bfd_put_8 (abfd, 0x30, contents + irel->r_offset);
2116                   else if ((code & 0xfff0) == 0x2Cb0)  /*  For addub imm16.  */
2117                     bfd_put_8 (abfd, 0x2C, contents + irel->r_offset);
2118                   else if ((code & 0xfff0) == 0x32b0)  /*  For adduw imm16.  */
2119                     bfd_put_8 (abfd, 0x32, contents + irel->r_offset);
2120                   else if ((code & 0xfff0) == 0x38b0)  /*  For subb imm16.  */
2121                     bfd_put_8 (abfd, 0x38, contents + irel->r_offset);
2122                   else if ((code & 0xfff0) == 0x3Cb0)  /*  For subcb imm16.  */
2123                     bfd_put_8 (abfd, 0x3C, contents + irel->r_offset);
2124                   else if ((code & 0xfff0) == 0x3Fb0)  /*  For subcw imm16.  */
2125                     bfd_put_8 (abfd, 0x3F, contents + irel->r_offset);
2126                   else if ((code & 0xfff0) == 0x3Ab0)  /*  For subw imm16.  */
2127                     bfd_put_8 (abfd, 0x3A, contents + irel->r_offset);
2128                   else if ((code & 0xfff0) == 0x50b0)  /*  For cmpb imm16.  */
2129                     bfd_put_8 (abfd, 0x50, contents + irel->r_offset);
2130                   else if ((code & 0xfff0) == 0x52b0)  /*  For cmpw imm16.  */
2131                     bfd_put_8 (abfd, 0x52, contents + irel->r_offset);
2132                   else
2133                     continue;
2134
2135                   bfd_put_8 (abfd, (code & 0xf), contents + irel->r_offset + 1);
2136                 }
2137
2138               /* Fix the relocation's type.  */
2139               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2140                                            R_CR16_IMM4);
2141
2142               /* Delete two bytes of data.  */
2143               if (!elf32_cr16_relax_delete_bytes (link_info, abfd, sec,
2144                                                   irel->r_offset + 2, 2))
2145                 goto error_return;
2146
2147               /* That will change things, so, we should relax again.
2148                  Note that this is not required, and it may be slow.  */
2149               *again = TRUE;
2150             }
2151         }
2152 #endif
2153     }
2154
2155   if (isymbuf != NULL
2156       && symtab_hdr->contents != (unsigned char *) isymbuf)
2157     {
2158       if (! link_info->keep_memory)
2159         free (isymbuf);
2160       else
2161        /* Cache the symbols for elf_link_input_bfd.  */
2162        symtab_hdr->contents = (unsigned char *) isymbuf;
2163     }
2164
2165   if (contents != NULL
2166       && elf_section_data (sec)->this_hdr.contents != contents)
2167     {
2168       if (! link_info->keep_memory)
2169         free (contents);
2170       else
2171        /* Cache the section contents for elf_link_input_bfd.  */
2172        elf_section_data (sec)->this_hdr.contents = contents;
2173
2174     }
2175
2176   if (internal_relocs != NULL
2177       && elf_section_data (sec)->relocs != internal_relocs)
2178     free (internal_relocs);
2179
2180   return TRUE;
2181
2182  error_return:
2183   if (isymbuf != NULL
2184       && symtab_hdr->contents != (unsigned char *) isymbuf)
2185     free (isymbuf);
2186   if (contents != NULL
2187       && elf_section_data (sec)->this_hdr.contents != contents)
2188     free (contents);
2189   if (internal_relocs != NULL
2190       && elf_section_data (sec)->relocs != internal_relocs)
2191     free (internal_relocs);
2192
2193   return FALSE;
2194 }
2195
2196 static asection *
2197 elf32_cr16_gc_mark_hook (asection *sec,
2198                          struct bfd_link_info *info,
2199                          Elf_Internal_Rela *rel,
2200                          struct elf_link_hash_entry *h,
2201                          Elf_Internal_Sym *sym)
2202 {
2203   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2204 }
2205
2206 /* Create dynamic sections when linking against a dynamic object.  */
2207
2208 static bfd_boolean
2209 _bfd_cr16_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2210 {
2211   flagword   flags;
2212   asection * s;
2213   const struct elf_backend_data * bed = get_elf_backend_data (abfd);
2214   struct elf_link_hash_table *htab = elf_hash_table (info);
2215   int ptralign = 0;
2216
2217   switch (bed->s->arch_size)
2218     {
2219     case 16:
2220       ptralign = 1;
2221       break;
2222
2223     case 32:
2224       ptralign = 2;
2225       break;
2226
2227     default:
2228       bfd_set_error (bfd_error_bad_value);
2229       return FALSE;
2230     }
2231
2232   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2233      .rel[a].bss sections.  */
2234
2235   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2236            | SEC_LINKER_CREATED);
2237
2238   s = bfd_make_section_anyway_with_flags (abfd,
2239                                           (bed->default_use_rela_p
2240                                            ? ".rela.plt" : ".rel.plt"),
2241                                           flags | SEC_READONLY);
2242   htab->srelplt = s;
2243   if (s == NULL
2244       || ! bfd_set_section_alignment (abfd, s, ptralign))
2245     return FALSE;
2246
2247   if (! _bfd_cr16_elf_create_got_section (abfd, info))
2248     return FALSE;
2249
2250   if (bed->want_dynbss)
2251     {
2252       /* The .dynbss section is a place to put symbols which are defined
2253          by dynamic objects, are referenced by regular objects, and are
2254          not functions.  We must allocate space for them in the process
2255          image and use a R_*_COPY reloc to tell the dynamic linker to
2256          initialize them at run time.  The linker script puts the .dynbss
2257          section into the .bss section of the final image.  */
2258       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
2259                                               SEC_ALLOC | SEC_LINKER_CREATED);
2260       if (s == NULL)
2261         return FALSE;
2262
2263       /* The .rel[a].bss section holds copy relocs.  This section is not
2264          normally needed.  We need to create it here, though, so that the
2265          linker will map it to an output section.  We can't just create it
2266          only if we need it, because we will not know whether we need it
2267          until we have seen all the input files, and the first time the
2268          main linker code calls BFD after examining all the input files
2269          (size_dynamic_sections) the input sections have already been
2270          mapped to the output sections.  If the section turns out not to
2271          be needed, we can discard it later.  We will never need this
2272          section when generating a shared object, since they do not use
2273          copy relocs.  */
2274       if (! bfd_link_executable (info))
2275         {
2276           s = bfd_make_section_anyway_with_flags (abfd,
2277                                                   (bed->default_use_rela_p
2278                                                    ? ".rela.bss" : ".rel.bss"),
2279                                                   flags | SEC_READONLY);
2280           if (s == NULL
2281               || ! bfd_set_section_alignment (abfd, s, ptralign))
2282             return FALSE;
2283         }
2284     }
2285
2286   return TRUE;
2287 }
2288 \f
2289 /* Adjust a symbol defined by a dynamic object and referenced by a
2290    regular object.  The current definition is in some section of the
2291    dynamic object, but we're not including those sections.  We have to
2292    change the definition to something the rest of the link can
2293    understand.  */
2294
2295 static bfd_boolean
2296 _bfd_cr16_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
2297                                      struct elf_link_hash_entry * h)
2298 {
2299   bfd * dynobj;
2300   asection * s;
2301
2302   dynobj = elf_hash_table (info)->dynobj;
2303
2304   /* Make sure we know what is going on here.  */
2305   BFD_ASSERT (dynobj != NULL
2306               && (h->needs_plt
2307                   || h->is_weakalias
2308                   || (h->def_dynamic
2309                       && h->ref_regular
2310                       && !h->def_regular)));
2311
2312   /* If this is a function, put it in the procedure linkage table.  We
2313      will fill in the contents of the procedure linkage table later,
2314      when we know the address of the .got section.  */
2315   if (h->type == STT_FUNC
2316       || h->needs_plt)
2317     {
2318       if (! bfd_link_executable (info)
2319           && !h->def_dynamic
2320           && !h->ref_dynamic)
2321         {
2322           /* This case can occur if we saw a PLT reloc in an input
2323              file, but the symbol was never referred to by a dynamic
2324              object.  In such a case, we don't actually need to build
2325              a procedure linkage table, and we can just do a REL32
2326              reloc instead.  */
2327           BFD_ASSERT (h->needs_plt);
2328           return TRUE;
2329         }
2330
2331       /* Make sure this symbol is output as a dynamic symbol.  */
2332       if (h->dynindx == -1)
2333         {
2334           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2335             return FALSE;
2336         }
2337
2338       /* We also need to make an entry in the .got.plt section, which
2339          will be placed in the .got section by the linker script.  */
2340
2341       s = elf_hash_table (info)->sgotplt;
2342       BFD_ASSERT (s != NULL);
2343       s->size += 4;
2344
2345       /* We also need to make an entry in the .rela.plt section.  */
2346
2347       s = elf_hash_table (info)->srelplt;
2348       BFD_ASSERT (s != NULL);
2349       s->size += sizeof (Elf32_External_Rela);
2350
2351       return TRUE;
2352     }
2353
2354   /* If this is a weak symbol, and there is a real definition, the
2355      processor independent code will have arranged for us to see the
2356      real definition first, and we can just use the same value.  */
2357   if (h->is_weakalias)
2358     {
2359       struct elf_link_hash_entry *def = weakdef (h);
2360       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2361       h->root.u.def.section = def->root.u.def.section;
2362       h->root.u.def.value = def->root.u.def.value;
2363       return TRUE;
2364     }
2365
2366   /* This is a reference to a symbol defined by a dynamic object which
2367      is not a function.  */
2368
2369   /* If we are creating a shared library, we must presume that the
2370      only references to the symbol are via the global offset table.
2371      For such cases we need not do anything here; the relocations will
2372      be handled correctly by relocate_section.  */
2373   if (bfd_link_executable (info))
2374     return TRUE;
2375
2376   /* If there are no references to this symbol that do not use the
2377      GOT, we don't need to generate a copy reloc.  */
2378   if (!h->non_got_ref)
2379     return TRUE;
2380
2381   /* We must allocate the symbol in our .dynbss section, which will
2382      become part of the .bss section of the executable.  There will be
2383      an entry for this symbol in the .dynsym section.  The dynamic
2384      object will contain position independent code, so all references
2385      from the dynamic object to this symbol will go through the global
2386      offset table.  The dynamic linker will use the .dynsym entry to
2387      determine the address it must put in the global offset table, so
2388      both the dynamic object and the regular object will refer to the
2389      same memory location for the variable.  */
2390
2391   s = bfd_get_linker_section (dynobj, ".dynbss");
2392   BFD_ASSERT (s != NULL);
2393
2394   /* We must generate a R_CR16_COPY reloc to tell the dynamic linker to
2395      copy the initial value out of the dynamic object and into the
2396      runtime process image.  We need to remember the offset into the
2397      .rela.bss section we are going to use.  */
2398   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2399     {
2400       asection * srel;
2401
2402       srel = bfd_get_linker_section (dynobj, ".rela.bss");
2403       BFD_ASSERT (srel != NULL);
2404       srel->size += sizeof (Elf32_External_Rela);
2405       h->needs_copy = 1;
2406     }
2407
2408   return _bfd_elf_adjust_dynamic_copy (info, h, s);
2409 }
2410
2411 /* Set the sizes of the dynamic sections.  */
2412
2413 static bfd_boolean
2414 _bfd_cr16_elf_size_dynamic_sections (bfd * output_bfd,
2415                                      struct bfd_link_info * info)
2416 {
2417   bfd * dynobj;
2418   asection * s;
2419   bfd_boolean plt;
2420   bfd_boolean relocs;
2421   bfd_boolean reltext;
2422
2423   dynobj = elf_hash_table (info)->dynobj;
2424   BFD_ASSERT (dynobj != NULL);
2425
2426   if (elf_hash_table (info)->dynamic_sections_created)
2427     {
2428       /* Set the contents of the .interp section to the interpreter.  */
2429       if (bfd_link_executable (info) && !info->nointerp)
2430         {
2431 #if 0
2432           s = bfd_get_linker_section (dynobj, ".interp");
2433           BFD_ASSERT (s != NULL);
2434           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2435           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2436 #endif
2437         }
2438     }
2439   else
2440     {
2441       /* We may have created entries in the .rela.got section.
2442          However, if we are not creating the dynamic sections, we will
2443          not actually use these entries.  Reset the size of .rela.got,
2444          which will cause it to get stripped from the output file
2445          below.  */
2446       s = elf_hash_table (info)->srelgot;
2447       if (s != NULL)
2448         s->size = 0;
2449     }
2450
2451   /* The check_relocs and adjust_dynamic_symbol entry points have
2452      determined the sizes of the various dynamic sections.  Allocate
2453      memory for them.  */
2454   plt = FALSE;
2455   relocs = FALSE;
2456   reltext = FALSE;
2457   for (s = dynobj->sections; s != NULL; s = s->next)
2458     {
2459       const char * name;
2460
2461       if ((s->flags & SEC_LINKER_CREATED) == 0)
2462         continue;
2463
2464       /* It's OK to base decisions on the section name, because none
2465          of the dynobj section names depend upon the input files.  */
2466       name = bfd_get_section_name (dynobj, s);
2467
2468       if (strcmp (name, ".plt") == 0)
2469         {
2470           /* Remember whether there is a PLT.  */
2471           plt = s->size != 0;
2472         }
2473       else if (CONST_STRNEQ (name, ".rela"))
2474         {
2475           if (s->size != 0)
2476             {
2477               asection * target;
2478
2479               /* Remember whether there are any reloc sections other
2480                  than .rela.plt.  */
2481               if (strcmp (name, ".rela.plt") != 0)
2482                 {
2483                   const char * outname;
2484
2485                   relocs = TRUE;
2486
2487                   /* If this relocation section applies to a read only
2488                      section, then we probably need a DT_TEXTREL
2489                      entry.  The entries in the .rela.plt section
2490                      really apply to the .got section, which we
2491                      created ourselves and so know is not readonly.  */
2492                   outname = bfd_get_section_name (output_bfd,
2493                                                   s->output_section);
2494                   target = bfd_get_section_by_name (output_bfd, outname + 5);
2495                   if (target != NULL
2496                       && (target->flags & SEC_READONLY) != 0
2497                       && (target->flags & SEC_ALLOC) != 0)
2498                     reltext = TRUE;
2499                 }
2500
2501               /* We use the reloc_count field as a counter if we need
2502                  to copy relocs into the output file.  */
2503               s->reloc_count = 0;
2504             }
2505         }
2506       else if (! CONST_STRNEQ (name, ".got")
2507                && strcmp (name, ".dynbss") != 0)
2508         /* It's not one of our sections, so don't allocate space.  */
2509         continue;
2510
2511       if (s->size == 0)
2512         {
2513           /* If we don't need this section, strip it from the
2514              output file.  This is mostly to handle .rela.bss and
2515              .rela.plt.  We must create both sections in
2516              create_dynamic_sections, because they must be created
2517              before the linker maps input sections to output
2518              sections.  The linker does that before
2519              adjust_dynamic_symbol is called, and it is that
2520              function which decides whether anything needs to go
2521              into these sections.  */
2522           s->flags |= SEC_EXCLUDE;
2523           continue;
2524         }
2525
2526         if ((s->flags & SEC_HAS_CONTENTS) == 0)
2527           continue;
2528
2529       /* Allocate memory for the section contents.  We use bfd_zalloc
2530          here in case unused entries are not reclaimed before the
2531          section's contents are written out.  This should not happen,
2532          but this way if it does, we get a R_CR16_NONE reloc
2533          instead of garbage.  */
2534       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2535       if (s->contents == NULL)
2536         return FALSE;
2537     }
2538
2539   if (elf_hash_table (info)->dynamic_sections_created)
2540     {
2541       /* Add some entries to the .dynamic section.  We fill in the
2542          values later, in _bfd_cr16_elf_finish_dynamic_sections,
2543          but we must add the entries now so that we get the correct
2544          size for the .dynamic section.  The DT_DEBUG entry is filled
2545          in by the dynamic linker and used by the debugger.  */
2546       if (! bfd_link_executable (info))
2547         {
2548           if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
2549             return FALSE;
2550         }
2551
2552       if (plt)
2553         {
2554           if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
2555               || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2556               || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2557               || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
2558             return FALSE;
2559         }
2560
2561       if (relocs)
2562         {
2563           if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
2564               || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
2565               || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
2566                                               sizeof (Elf32_External_Rela)))
2567             return FALSE;
2568         }
2569
2570       if (reltext)
2571         {
2572           if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
2573             return FALSE;
2574         }
2575     }
2576
2577   return TRUE;
2578 }
2579
2580 /* Finish up dynamic symbol handling.  We set the contents of various
2581    dynamic sections here.  */
2582
2583 static bfd_boolean
2584 _bfd_cr16_elf_finish_dynamic_symbol (bfd * output_bfd,
2585                                      struct bfd_link_info * info,
2586                                      struct elf_link_hash_entry * h,
2587                                      Elf_Internal_Sym * sym)
2588 {
2589   bfd * dynobj;
2590
2591   dynobj = elf_hash_table (info)->dynobj;
2592
2593   if (h->got.offset != (bfd_vma) -1)
2594     {
2595       asection *        sgot;
2596       asection *        srel;
2597       Elf_Internal_Rela rel;
2598
2599       /* This symbol has an entry in the global offset table.  Set it up.  */
2600
2601       sgot = elf_hash_table (info)->sgot;
2602       srel = elf_hash_table (info)->srelgot;
2603       BFD_ASSERT (sgot != NULL && srel != NULL);
2604
2605       rel.r_offset = (sgot->output_section->vma
2606                       + sgot->output_offset
2607                       + (h->got.offset & ~1));
2608
2609       /* If this is a -Bsymbolic link, and the symbol is defined
2610          locally, we just want to emit a RELATIVE reloc.  Likewise if
2611          the symbol was forced to be local because of a version file.
2612          The entry in the global offset table will already have been
2613          initialized in the relocate_section function.  */
2614       if (bfd_link_executable (info)
2615           && (info->symbolic || h->dynindx == -1)
2616           && h->def_regular)
2617         {
2618           rel.r_info = ELF32_R_INFO (0, R_CR16_GOT_REGREL20);
2619           rel.r_addend = (h->root.u.def.value
2620                           + h->root.u.def.section->output_section->vma
2621                           + h->root.u.def.section->output_offset);
2622         }
2623       else
2624         {
2625           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
2626           rel.r_info = ELF32_R_INFO (h->dynindx, R_CR16_GOT_REGREL20);
2627           rel.r_addend = 0;
2628         }
2629
2630       bfd_elf32_swap_reloca_out (output_bfd, &rel,
2631                                  (bfd_byte *) ((Elf32_External_Rela *) srel->contents
2632                                                + srel->reloc_count));
2633       ++ srel->reloc_count;
2634     }
2635
2636   if (h->needs_copy)
2637     {
2638       asection *        s;
2639       Elf_Internal_Rela rel;
2640
2641       /* This symbol needs a copy reloc.  Set it up.  */
2642       BFD_ASSERT (h->dynindx != -1
2643                   && (h->root.type == bfd_link_hash_defined
2644                       || h->root.type == bfd_link_hash_defweak));
2645
2646       s = bfd_get_linker_section (dynobj, ".rela.bss");
2647       BFD_ASSERT (s != NULL);
2648
2649       rel.r_offset = (h->root.u.def.value
2650                       + h->root.u.def.section->output_section->vma
2651                       + h->root.u.def.section->output_offset);
2652       rel.r_info = ELF32_R_INFO (h->dynindx, R_CR16_GOT_REGREL20);
2653       rel.r_addend = 0;
2654       bfd_elf32_swap_reloca_out (output_bfd, &rel,
2655                                  (bfd_byte *) ((Elf32_External_Rela *) s->contents
2656                                                + s->reloc_count));
2657      ++ s->reloc_count;
2658     }
2659
2660   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
2661   if (h == elf_hash_table (info)->hdynamic
2662       || h == elf_hash_table (info)->hgot)
2663     sym->st_shndx = SHN_ABS;
2664
2665   return TRUE;
2666 }
2667
2668 /* Finish up the dynamic sections.  */
2669
2670 static bfd_boolean
2671 _bfd_cr16_elf_finish_dynamic_sections (bfd * output_bfd,
2672                                        struct bfd_link_info * info)
2673 {
2674   bfd *      dynobj;
2675   asection * sgot;
2676   asection * sdyn;
2677
2678   dynobj = elf_hash_table (info)->dynobj;
2679
2680   sgot = elf_hash_table (info)->sgotplt;
2681   BFD_ASSERT (sgot != NULL);
2682   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2683
2684   if (elf_hash_table (info)->dynamic_sections_created)
2685     {
2686       Elf32_External_Dyn * dyncon;
2687       Elf32_External_Dyn * dynconend;
2688
2689       BFD_ASSERT (sdyn != NULL);
2690
2691       dyncon = (Elf32_External_Dyn *) sdyn->contents;
2692       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2693
2694       for (; dyncon < dynconend; dyncon++)
2695         {
2696           Elf_Internal_Dyn dyn;
2697           asection * s;
2698
2699           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2700
2701           switch (dyn.d_tag)
2702             {
2703             default:
2704               break;
2705
2706             case DT_PLTGOT:
2707               s = elf_hash_table (info)->sgotplt;
2708               goto get_vma;
2709
2710             case DT_JMPREL:
2711               s = elf_hash_table (info)->srelplt;
2712             get_vma:
2713               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2714               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2715               break;
2716
2717             case DT_PLTRELSZ:
2718               s = elf_hash_table (info)->srelplt;
2719               dyn.d_un.d_val = s->size;
2720               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2721               break;
2722             }
2723         }
2724
2725     }
2726
2727   /* Fill in the first three entries in the global offset table.  */
2728   if (sgot->size > 0)
2729     {
2730       if (sdyn == NULL)
2731         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2732       else
2733         bfd_put_32 (output_bfd,
2734                     sdyn->output_section->vma + sdyn->output_offset,
2735                     sgot->contents);
2736     }
2737
2738   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2739
2740   return TRUE;
2741 }
2742
2743 /* Given a .data.rel section and a .emreloc in-memory section, store
2744    relocation information into the .emreloc section which can be
2745    used at runtime to relocate the section.  This is called by the
2746    linker when the --embedded-relocs switch is used.  This is called
2747    after the add_symbols entry point has been called for all the
2748    objects, and before the final_link entry point is called.  */
2749
2750 bfd_boolean
2751 bfd_cr16_elf32_create_embedded_relocs (bfd *abfd,
2752                                        struct bfd_link_info *info,
2753                                        asection *datasec,
2754                                        asection *relsec,
2755                                        char **errmsg)
2756 {
2757   Elf_Internal_Shdr *symtab_hdr;
2758   Elf_Internal_Sym *isymbuf = NULL;
2759   Elf_Internal_Rela *internal_relocs = NULL;
2760   Elf_Internal_Rela *irel, *irelend;
2761   bfd_byte *p;
2762   bfd_size_type amt;
2763
2764   BFD_ASSERT (! bfd_link_relocatable (info));
2765
2766   *errmsg = NULL;
2767
2768   if (datasec->reloc_count == 0)
2769     return TRUE;
2770
2771   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2772
2773   /* Get a copy of the native relocations.  */
2774   internal_relocs = (_bfd_elf_link_read_relocs
2775                      (abfd, datasec, NULL, NULL, info->keep_memory));
2776   if (internal_relocs == NULL)
2777     goto error_return;
2778
2779   amt = (bfd_size_type) datasec->reloc_count * 8;
2780   relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
2781   if (relsec->contents == NULL)
2782     goto error_return;
2783
2784   p = relsec->contents;
2785
2786   irelend = internal_relocs + datasec->reloc_count;
2787   for (irel = internal_relocs; irel < irelend; irel++, p += 8)
2788     {
2789       asection *targetsec;
2790
2791       /* We are going to write a four byte longword into the runtime
2792        reloc section.  The longword will be the address in the data
2793        section which must be relocated.  It is followed by the name
2794        of the target section NUL-padded or truncated to 8
2795        characters.  */
2796
2797       /* We can only relocate absolute longword relocs at run time.  */
2798       if (!((ELF32_R_TYPE (irel->r_info) == (int) R_CR16_NUM32a)
2799           || (ELF32_R_TYPE (irel->r_info) == (int) R_CR16_NUM32)))
2800         {
2801           *errmsg = _("unsupported relocation type");
2802           bfd_set_error (bfd_error_bad_value);
2803           goto error_return;
2804         }
2805
2806       /* Get the target section referred to by the reloc.  */
2807       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2808         {
2809           /* A local symbol.  */
2810           Elf_Internal_Sym *isym;
2811
2812           /* Read this BFD's local symbols if we haven't done so already.  */
2813           if (isymbuf == NULL)
2814             {
2815               isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2816               if (isymbuf == NULL)
2817                 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2818                                                 symtab_hdr->sh_info, 0,
2819                                                 NULL, NULL, NULL);
2820               if (isymbuf == NULL)
2821                 goto error_return;
2822             }
2823
2824           isym = isymbuf + ELF32_R_SYM (irel->r_info);
2825           targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2826         }
2827       else
2828         {
2829           unsigned long indx;
2830           struct elf_link_hash_entry *h;
2831
2832           /* An external symbol.  */
2833           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2834           h = elf_sym_hashes (abfd)[indx];
2835           BFD_ASSERT (h != NULL);
2836           if (h->root.type == bfd_link_hash_defined
2837               || h->root.type == bfd_link_hash_defweak)
2838             targetsec = h->root.u.def.section;
2839           else
2840             targetsec = NULL;
2841         }
2842
2843       bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2844       memset (p + 4, 0, 4);
2845       if ((ELF32_R_TYPE (irel->r_info) == (int) R_CR16_NUM32a)
2846           && (targetsec != NULL) )
2847          strncpy ((char *) p + 4, targetsec->output_section->name, 4);
2848     }
2849
2850   if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2851     free (isymbuf);
2852   if (internal_relocs != NULL
2853       && elf_section_data (datasec)->relocs != internal_relocs)
2854     free (internal_relocs);
2855   return TRUE;
2856
2857 error_return:
2858   if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2859     free (isymbuf);
2860   if (internal_relocs != NULL
2861       && elf_section_data (datasec)->relocs != internal_relocs)
2862     free (internal_relocs);
2863   return FALSE;
2864 }
2865
2866
2867 /* Classify relocation types, such that combreloc can sort them
2868    properly.  */
2869
2870 static enum elf_reloc_type_class
2871 _bfd_cr16_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2872                                 const asection *rel_sec ATTRIBUTE_UNUSED,
2873                                 const Elf_Internal_Rela *rela)
2874 {
2875   switch ((int) ELF32_R_TYPE (rela->r_info))
2876     {
2877     case R_CR16_GOT_REGREL20:
2878     case R_CR16_GOTC_REGREL20:
2879       return reloc_class_relative;
2880     default:
2881       return reloc_class_normal;
2882     }
2883 }
2884
2885 /* Definitions for setting CR16 target vector.  */
2886 #define TARGET_LITTLE_SYM                 cr16_elf32_vec
2887 #define TARGET_LITTLE_NAME                "elf32-cr16"
2888 #define ELF_ARCH                          bfd_arch_cr16
2889 #define ELF_MACHINE_CODE                  EM_CR16
2890 #define ELF_MACHINE_ALT1                  EM_CR16_OLD
2891 #define ELF_MAXPAGESIZE                   0x1
2892 #define elf_symbol_leading_char           '_'
2893
2894 #define bfd_elf32_bfd_reloc_type_lookup   elf_cr16_reloc_type_lookup
2895 #define bfd_elf32_bfd_reloc_name_lookup   elf_cr16_reloc_name_lookup
2896 #define elf_info_to_howto                 elf_cr16_info_to_howto
2897 #define elf_info_to_howto_rel             NULL
2898 #define elf_backend_relocate_section      elf32_cr16_relocate_section
2899 #define bfd_elf32_bfd_relax_section       elf32_cr16_relax_section
2900 #define bfd_elf32_bfd_get_relocated_section_contents \
2901                                 elf32_cr16_get_relocated_section_contents
2902 #define elf_backend_gc_mark_hook          elf32_cr16_gc_mark_hook
2903 #define elf_backend_can_gc_sections       1
2904 #define elf_backend_rela_normal           1
2905 #define elf_backend_check_relocs          cr16_elf_check_relocs
2906 /* So we can set bits in e_flags.  */
2907 #define elf_backend_final_write_processing \
2908                                  _bfd_cr16_elf_final_write_processing
2909 #define elf_backend_object_p     _bfd_cr16_elf_object_p
2910
2911 #define bfd_elf32_bfd_merge_private_bfd_data \
2912                                  _bfd_cr16_elf_merge_private_bfd_data
2913
2914
2915 #define bfd_elf32_bfd_link_hash_table_create \
2916                                   elf32_cr16_link_hash_table_create
2917
2918 #define elf_backend_create_dynamic_sections \
2919                                   _bfd_cr16_elf_create_dynamic_sections
2920 #define elf_backend_adjust_dynamic_symbol \
2921                                   _bfd_cr16_elf_adjust_dynamic_symbol
2922 #define elf_backend_size_dynamic_sections \
2923                                   _bfd_cr16_elf_size_dynamic_sections
2924 #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
2925 #define elf_backend_finish_dynamic_symbol \
2926                                    _bfd_cr16_elf_finish_dynamic_symbol
2927 #define elf_backend_finish_dynamic_sections \
2928                                    _bfd_cr16_elf_finish_dynamic_sections
2929
2930 #define elf_backend_reloc_type_class   _bfd_cr16_elf_reloc_type_class
2931
2932
2933 #define elf_backend_want_got_plt        1
2934 #define elf_backend_plt_readonly        1
2935 #define elf_backend_want_plt_sym        0
2936 #define elf_backend_got_header_size     12
2937 #define elf_backend_dtrel_excludes_plt  1
2938
2939 #include "elf32-target.h"