3911976a252ea538b9246ce79f62ed981da7a967
[platform/upstream/binutils.git] / bfd / elf64-mmix.c
1 /* MMIX-specific support for 64-bit ELF.
2    Copyright 2001, 2002 Free Software Foundation, Inc.
3    Contributed by Hans-Peter Nilsson <hp@bitrange.com>
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 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* No specific ABI or "processor-specific supplement" defined.  */
22
23 /* TODO:
24    - Linker relaxation.
25    - On-demand register allocation (from R_MMIX_BASE_PLUS_OFFSET).  */
26
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "libbfd.h"
30 #include "elf-bfd.h"
31 #include "elf/mmix.h"
32 #include "opcode/mmix.h"
33
34 #define MINUS_ONE       (((bfd_vma) 0) - 1)
35
36 /* Put these everywhere in new code.  */
37 #define FATAL_DEBUG                                             \
38  _bfd_abort (__FILE__, __LINE__,                                \
39              "Internal: Non-debugged code (test-case missing)")
40
41 #define BAD_CASE(x)                             \
42  _bfd_abort (__FILE__, __LINE__,                \
43              "bad case for " #x)
44
45 static boolean mmix_elf_link_output_symbol_hook
46   PARAMS ((bfd *, struct bfd_link_info *, const char *,
47            Elf_Internal_Sym *, asection *));
48
49 static bfd_reloc_status_type mmix_elf_reloc
50   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
51
52 static reloc_howto_type *bfd_elf64_bfd_reloc_type_lookup
53   PARAMS ((bfd *, bfd_reloc_code_real_type));
54
55 static void mmix_info_to_howto_rela
56   PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
57
58 static int mmix_elf_sort_relocs PARAMS ((const PTR, const PTR));
59
60 static boolean mmix_elf_check_relocs
61   PARAMS ((bfd *, struct bfd_link_info *, asection *,
62            const Elf_Internal_Rela *));
63
64 static boolean mmix_elf_relocate_section
65   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
66            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
67
68 static asection * mmix_elf_gc_mark_hook
69   PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
70            struct elf_link_hash_entry *, Elf_Internal_Sym *));
71
72 static bfd_reloc_status_type mmix_final_link_relocate
73   PARAMS ((reloc_howto_type *, asection *, bfd_byte *,
74            bfd_vma, bfd_signed_vma, bfd_vma, const char *, asection *));
75
76 static bfd_reloc_status_type mmix_elf_perform_relocation
77   PARAMS ((asection *, reloc_howto_type *, PTR, bfd_vma, bfd_vma));
78
79 static boolean mmix_elf_section_from_bfd_section
80   PARAMS ((bfd *, Elf64_Internal_Shdr *, asection *, int *));
81
82 static boolean mmix_elf_add_symbol_hook
83   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
84            const char **, flagword *, asection **, bfd_vma *));
85
86 static boolean mmix_elf_is_local_label_name
87   PARAMS ((bfd *, const char *));
88
89 extern boolean mmix_elf_final_link PARAMS ((bfd *, struct bfd_link_info *));
90
91 extern void mmix_elf_symbol_processing PARAMS ((bfd *, asymbol *));
92
93 /* Watch out: this currently needs to have elements with the same index as
94    their R_MMIX_ number.  */
95 static reloc_howto_type elf_mmix_howto_table[] =
96  {
97   /* This reloc does nothing.  */
98   HOWTO (R_MMIX_NONE,           /* type */
99          0,                     /* rightshift */
100          2,                     /* size (0 = byte, 1 = short, 2 = long) */
101          32,                    /* bitsize */
102          false,                 /* pc_relative */
103          0,                     /* bitpos */
104          complain_overflow_bitfield, /* complain_on_overflow */
105          bfd_elf_generic_reloc, /* special_function */
106          "R_MMIX_NONE",         /* name */
107          false,                 /* partial_inplace */
108          0,                     /* src_mask */
109          0,                     /* dst_mask */
110          false),                /* pcrel_offset */
111
112   /* An 8 bit absolute relocation.  */
113   HOWTO (R_MMIX_8,              /* type */
114          0,                     /* rightshift */
115          0,                     /* size (0 = byte, 1 = short, 2 = long) */
116          8,                     /* bitsize */
117          false,                 /* pc_relative */
118          0,                     /* bitpos */
119          complain_overflow_bitfield, /* complain_on_overflow */
120          bfd_elf_generic_reloc, /* special_function */
121          "R_MMIX_8",            /* name */
122          false,                 /* partial_inplace */
123          0xff,                  /* src_mask */
124          0xff,                  /* dst_mask */
125          false),                /* pcrel_offset */
126
127   /* An 16 bit absolute relocation.  */
128   HOWTO (R_MMIX_16,             /* type */
129          0,                     /* rightshift */
130          1,                     /* size (0 = byte, 1 = short, 2 = long) */
131          16,                    /* bitsize */
132          false,                 /* pc_relative */
133          0,                     /* bitpos */
134          complain_overflow_bitfield, /* complain_on_overflow */
135          bfd_elf_generic_reloc, /* special_function */
136          "R_MMIX_16",           /* name */
137          false,                 /* partial_inplace */
138          0xffff,                /* src_mask */
139          0xffff,                /* dst_mask */
140          false),                /* pcrel_offset */
141
142   /* An 24 bit absolute relocation.  */
143   HOWTO (R_MMIX_24,             /* type */
144          0,                     /* rightshift */
145          2,                     /* size (0 = byte, 1 = short, 2 = long) */
146          24,                    /* bitsize */
147          false,                 /* pc_relative */
148          0,                     /* bitpos */
149          complain_overflow_bitfield, /* complain_on_overflow */
150          bfd_elf_generic_reloc, /* special_function */
151          "R_MMIX_24",           /* name */
152          false,                 /* partial_inplace */
153          0xffffff,              /* src_mask */
154          0xffffff,              /* dst_mask */
155          false),                /* pcrel_offset */
156
157   /* A 32 bit absolute relocation.  */
158   HOWTO (R_MMIX_32,             /* type */
159          0,                     /* rightshift */
160          2,                     /* size (0 = byte, 1 = short, 2 = long) */
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_MMIX_32",           /* name */
167          false,                 /* partial_inplace */
168          0xffffffff,            /* src_mask */
169          0xffffffff,            /* dst_mask */
170          false),                /* pcrel_offset */
171
172   /* 64 bit relocation.  */
173   HOWTO (R_MMIX_64,             /* type */
174          0,                     /* rightshift */
175          4,                     /* size (0 = byte, 1 = short, 2 = long) */
176          64,                    /* bitsize */
177          false,                 /* pc_relative */
178          0,                     /* bitpos */
179          complain_overflow_bitfield, /* complain_on_overflow */
180          bfd_elf_generic_reloc, /* special_function */
181          "R_MMIX_64",           /* name */
182          false,                 /* partial_inplace */
183          MINUS_ONE,             /* src_mask */
184          MINUS_ONE,             /* dst_mask */
185          false),                /* pcrel_offset */
186
187   /* An 8 bit PC-relative relocation.  */
188   HOWTO (R_MMIX_PC_8,           /* type */
189          0,                     /* rightshift */
190          0,                     /* size (0 = byte, 1 = short, 2 = long) */
191          8,                     /* bitsize */
192          true,                  /* pc_relative */
193          0,                     /* bitpos */
194          complain_overflow_bitfield, /* complain_on_overflow */
195          bfd_elf_generic_reloc, /* special_function */
196          "R_MMIX_PC_8",         /* name */
197          false,                 /* partial_inplace */
198          0xff,                  /* src_mask */
199          0xff,                  /* dst_mask */
200          true),                 /* pcrel_offset */
201
202   /* An 16 bit PC-relative relocation.  */
203   HOWTO (R_MMIX_PC_16,          /* type */
204          0,                     /* rightshift */
205          1,                     /* size (0 = byte, 1 = short, 2 = long) */
206          16,                    /* bitsize */
207          true,                  /* pc_relative */
208          0,                     /* bitpos */
209          complain_overflow_bitfield, /* complain_on_overflow */
210          bfd_elf_generic_reloc, /* special_function */
211          "R_MMIX_PC_16",        /* name */
212          false,                 /* partial_inplace */
213          0xffff,                /* src_mask */
214          0xffff,                /* dst_mask */
215          true),                 /* pcrel_offset */
216
217   /* An 24 bit PC-relative relocation.  */
218   HOWTO (R_MMIX_PC_24,          /* type */
219          0,                     /* rightshift */
220          2,                     /* size (0 = byte, 1 = short, 2 = long) */
221          24,                    /* bitsize */
222          true,                  /* pc_relative */
223          0,                     /* bitpos */
224          complain_overflow_bitfield, /* complain_on_overflow */
225          bfd_elf_generic_reloc, /* special_function */
226          "R_MMIX_PC_24",        /* name */
227          false,                 /* partial_inplace */
228          0xffffff,              /* src_mask */
229          0xffffff,              /* dst_mask */
230          true),                 /* pcrel_offset */
231
232   /* A 32 bit absolute PC-relative relocation.  */
233   HOWTO (R_MMIX_PC_32,          /* type */
234          0,                     /* rightshift */
235          2,                     /* size (0 = byte, 1 = short, 2 = long) */
236          32,                    /* bitsize */
237          true,                  /* pc_relative */
238          0,                     /* bitpos */
239          complain_overflow_bitfield, /* complain_on_overflow */
240          bfd_elf_generic_reloc, /* special_function */
241          "R_MMIX_PC_32",        /* name */
242          false,                 /* partial_inplace */
243          0xffffffff,            /* src_mask */
244          0xffffffff,            /* dst_mask */
245          true),                 /* pcrel_offset */
246
247   /* 64 bit PC-relative relocation.  */
248   HOWTO (R_MMIX_PC_64,          /* type */
249          0,                     /* rightshift */
250          4,                     /* size (0 = byte, 1 = short, 2 = long) */
251          64,                    /* bitsize */
252          true,                  /* pc_relative */
253          0,                     /* bitpos */
254          complain_overflow_bitfield, /* complain_on_overflow */
255          bfd_elf_generic_reloc, /* special_function */
256          "R_MMIX_PC_64",        /* name */
257          false,                 /* partial_inplace */
258          MINUS_ONE,             /* src_mask */
259          MINUS_ONE,             /* dst_mask */
260          true),                 /* pcrel_offset */
261
262   /* GNU extension to record C++ vtable hierarchy.  */
263   HOWTO (R_MMIX_GNU_VTINHERIT, /* type */
264          0,                     /* rightshift */
265          0,                     /* size (0 = byte, 1 = short, 2 = long) */
266          0,                     /* bitsize */
267          false,                 /* pc_relative */
268          0,                     /* bitpos */
269          complain_overflow_dont, /* complain_on_overflow */
270          NULL,                  /* special_function */
271          "R_MMIX_GNU_VTINHERIT", /* name */
272          false,                 /* partial_inplace */
273          0,                     /* src_mask */
274          0,                     /* dst_mask */
275          true),                 /* pcrel_offset */
276
277   /* GNU extension to record C++ vtable member usage.  */
278   HOWTO (R_MMIX_GNU_VTENTRY,    /* type */
279          0,                     /* rightshift */
280          0,                     /* size (0 = byte, 1 = short, 2 = long) */
281          0,                     /* bitsize */
282          false,                 /* pc_relative */
283          0,                     /* bitpos */
284          complain_overflow_dont, /* complain_on_overflow */
285          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
286          "R_MMIX_GNU_VTENTRY", /* name */
287          false,                 /* partial_inplace */
288          0,                     /* src_mask */
289          0,                     /* dst_mask */
290          false),                /* pcrel_offset */
291
292   /* The GETA relocation is supposed to get any address that could
293      possibly be reached by the GETA instruction.  It can silently expand
294      to get a 64-bit operand, but will complain if any of the two least
295      significant bits are set.  The howto members reflect a simple GETA.  */
296   HOWTO (R_MMIX_GETA,           /* type */
297          2,                     /* rightshift */
298          2,                     /* size (0 = byte, 1 = short, 2 = long) */
299          19,                    /* bitsize */
300          true,                  /* pc_relative */
301          0,                     /* bitpos */
302          complain_overflow_signed, /* complain_on_overflow */
303          mmix_elf_reloc,        /* special_function */
304          "R_MMIX_GETA",         /* name */
305          false,                 /* partial_inplace */
306          0x0100ffff,            /* src_mask */
307          0x0100ffff,            /* dst_mask */
308          true),                 /* pcrel_offset */
309
310   HOWTO (R_MMIX_GETA_1,         /* type */
311          2,                     /* rightshift */
312          2,                     /* size (0 = byte, 1 = short, 2 = long) */
313          19,                    /* bitsize */
314          true,                  /* pc_relative */
315          0,                     /* bitpos */
316          complain_overflow_signed, /* complain_on_overflow */
317          mmix_elf_reloc,        /* special_function */
318          "R_MMIX_GETA_1",               /* name */
319          false,                 /* partial_inplace */
320          0x0100ffff,            /* src_mask */
321          0x0100ffff,            /* dst_mask */
322          true),                 /* pcrel_offset */
323
324   HOWTO (R_MMIX_GETA_2,         /* type */
325          2,                     /* rightshift */
326          2,                     /* size (0 = byte, 1 = short, 2 = long) */
327          19,                    /* bitsize */
328          true,                  /* pc_relative */
329          0,                     /* bitpos */
330          complain_overflow_signed, /* complain_on_overflow */
331          mmix_elf_reloc,        /* special_function */
332          "R_MMIX_GETA_2",               /* name */
333          false,                 /* partial_inplace */
334          0x0100ffff,            /* src_mask */
335          0x0100ffff,            /* dst_mask */
336          true),                 /* pcrel_offset */
337
338   HOWTO (R_MMIX_GETA_3,         /* type */
339          2,                     /* rightshift */
340          2,                     /* size (0 = byte, 1 = short, 2 = long) */
341          19,                    /* bitsize */
342          true,                  /* pc_relative */
343          0,                     /* bitpos */
344          complain_overflow_signed, /* complain_on_overflow */
345          mmix_elf_reloc,        /* special_function */
346          "R_MMIX_GETA_3",               /* name */
347          false,                 /* partial_inplace */
348          0x0100ffff,            /* src_mask */
349          0x0100ffff,            /* dst_mask */
350          true),                 /* pcrel_offset */
351
352   /* The conditional branches are supposed to reach any (code) address.
353      It can silently expand to a 64-bit operand, but will emit an error if
354      any of the two least significant bits are set.  The howto members
355      reflect a simple branch.  */
356   HOWTO (R_MMIX_CBRANCH,        /* type */
357          2,                     /* rightshift */
358          2,                     /* size (0 = byte, 1 = short, 2 = long) */
359          19,                    /* bitsize */
360          true,                  /* pc_relative */
361          0,                     /* bitpos */
362          complain_overflow_signed, /* complain_on_overflow */
363          mmix_elf_reloc,        /* special_function */
364          "R_MMIX_CBRANCH",      /* name */
365          false,                 /* partial_inplace */
366          0x0100ffff,            /* src_mask */
367          0x0100ffff,            /* dst_mask */
368          true),                 /* pcrel_offset */
369
370   HOWTO (R_MMIX_CBRANCH_J,      /* type */
371          2,                     /* rightshift */
372          2,                     /* size (0 = byte, 1 = short, 2 = long) */
373          19,                    /* bitsize */
374          true,                  /* pc_relative */
375          0,                     /* bitpos */
376          complain_overflow_signed, /* complain_on_overflow */
377          mmix_elf_reloc,        /* special_function */
378          "R_MMIX_CBRANCH_J",    /* name */
379          false,                 /* partial_inplace */
380          0x0100ffff,            /* src_mask */
381          0x0100ffff,            /* dst_mask */
382          true),                 /* pcrel_offset */
383
384   HOWTO (R_MMIX_CBRANCH_1,      /* type */
385          2,                     /* rightshift */
386          2,                     /* size (0 = byte, 1 = short, 2 = long) */
387          19,                    /* bitsize */
388          true,                  /* pc_relative */
389          0,                     /* bitpos */
390          complain_overflow_signed, /* complain_on_overflow */
391          mmix_elf_reloc,        /* special_function */
392          "R_MMIX_CBRANCH_1",    /* name */
393          false,                 /* partial_inplace */
394          0x0100ffff,            /* src_mask */
395          0x0100ffff,            /* dst_mask */
396          true),                 /* pcrel_offset */
397
398   HOWTO (R_MMIX_CBRANCH_2,      /* type */
399          2,                     /* rightshift */
400          2,                     /* size (0 = byte, 1 = short, 2 = long) */
401          19,                    /* bitsize */
402          true,                  /* pc_relative */
403          0,                     /* bitpos */
404          complain_overflow_signed, /* complain_on_overflow */
405          mmix_elf_reloc,        /* special_function */
406          "R_MMIX_CBRANCH_2",    /* name */
407          false,                 /* partial_inplace */
408          0x0100ffff,            /* src_mask */
409          0x0100ffff,            /* dst_mask */
410          true),                 /* pcrel_offset */
411
412   HOWTO (R_MMIX_CBRANCH_3,      /* type */
413          2,                     /* rightshift */
414          2,                     /* size (0 = byte, 1 = short, 2 = long) */
415          19,                    /* bitsize */
416          true,                  /* pc_relative */
417          0,                     /* bitpos */
418          complain_overflow_signed, /* complain_on_overflow */
419          mmix_elf_reloc,        /* special_function */
420          "R_MMIX_CBRANCH_3",    /* name */
421          false,                 /* partial_inplace */
422          0x0100ffff,            /* src_mask */
423          0x0100ffff,            /* dst_mask */
424          true),                 /* pcrel_offset */
425
426   /* The PUSHJ instruction can reach any (code) address, as long as it's
427      the beginning of a function (no usable restriction).  It can silently
428      expand to a 64-bit operand, but will emit an error if any of the two
429      least significant bits are set.  The howto members reflect a simple
430      PUSHJ.  */
431   HOWTO (R_MMIX_PUSHJ,          /* type */
432          2,                     /* rightshift */
433          2,                     /* size (0 = byte, 1 = short, 2 = long) */
434          19,                    /* bitsize */
435          true,                  /* pc_relative */
436          0,                     /* bitpos */
437          complain_overflow_signed, /* complain_on_overflow */
438          mmix_elf_reloc,        /* special_function */
439          "R_MMIX_PUSHJ",        /* name */
440          false,                 /* partial_inplace */
441          0x0100ffff,            /* src_mask */
442          0x0100ffff,            /* dst_mask */
443          true),                 /* pcrel_offset */
444
445   HOWTO (R_MMIX_PUSHJ_1,        /* type */
446          2,                     /* rightshift */
447          2,                     /* size (0 = byte, 1 = short, 2 = long) */
448          19,                    /* bitsize */
449          true,                  /* pc_relative */
450          0,                     /* bitpos */
451          complain_overflow_signed, /* complain_on_overflow */
452          mmix_elf_reloc,        /* special_function */
453          "R_MMIX_PUSHJ_1",      /* name */
454          false,                 /* partial_inplace */
455          0x0100ffff,            /* src_mask */
456          0x0100ffff,            /* dst_mask */
457          true),                 /* pcrel_offset */
458
459   HOWTO (R_MMIX_PUSHJ_2,        /* type */
460          2,                     /* rightshift */
461          2,                     /* size (0 = byte, 1 = short, 2 = long) */
462          19,                    /* bitsize */
463          true,                  /* pc_relative */
464          0,                     /* bitpos */
465          complain_overflow_signed, /* complain_on_overflow */
466          mmix_elf_reloc,        /* special_function */
467          "R_MMIX_PUSHJ_2",      /* name */
468          false,                 /* partial_inplace */
469          0x0100ffff,            /* src_mask */
470          0x0100ffff,            /* dst_mask */
471          true),                 /* pcrel_offset */
472
473   HOWTO (R_MMIX_PUSHJ_3,        /* type */
474          2,                     /* rightshift */
475          2,                     /* size (0 = byte, 1 = short, 2 = long) */
476          19,                    /* bitsize */
477          true,                  /* pc_relative */
478          0,                     /* bitpos */
479          complain_overflow_signed, /* complain_on_overflow */
480          mmix_elf_reloc,        /* special_function */
481          "R_MMIX_PUSHJ_3",      /* name */
482          false,                 /* partial_inplace */
483          0x0100ffff,            /* src_mask */
484          0x0100ffff,            /* dst_mask */
485          true),                 /* pcrel_offset */
486
487   /* A JMP is supposed to reach any (code) address.  By itself, it can
488      reach +-64M; the expansion can reach all 64 bits.  Note that the 64M
489      limit is soon reached if you link the program in wildly different
490      memory segments.  The howto members reflect a trivial JMP.  */
491   HOWTO (R_MMIX_JMP,            /* type */
492          2,                     /* rightshift */
493          2,                     /* size (0 = byte, 1 = short, 2 = long) */
494          27,                    /* bitsize */
495          true,                  /* pc_relative */
496          0,                     /* bitpos */
497          complain_overflow_signed, /* complain_on_overflow */
498          mmix_elf_reloc,        /* special_function */
499          "R_MMIX_JMP",          /* name */
500          false,                 /* partial_inplace */
501          0x1ffffff,             /* src_mask */
502          0x1ffffff,             /* dst_mask */
503          true),                 /* pcrel_offset */
504
505   HOWTO (R_MMIX_JMP_1,          /* type */
506          2,                     /* rightshift */
507          2,                     /* size (0 = byte, 1 = short, 2 = long) */
508          27,                    /* bitsize */
509          true,                  /* pc_relative */
510          0,                     /* bitpos */
511          complain_overflow_signed, /* complain_on_overflow */
512          mmix_elf_reloc,        /* special_function */
513          "R_MMIX_JMP_1",        /* name */
514          false,                 /* partial_inplace */
515          0x1ffffff,             /* src_mask */
516          0x1ffffff,             /* dst_mask */
517          true),                 /* pcrel_offset */
518
519   HOWTO (R_MMIX_JMP_2,          /* type */
520          2,                     /* rightshift */
521          2,                     /* size (0 = byte, 1 = short, 2 = long) */
522          27,                    /* bitsize */
523          true,                  /* pc_relative */
524          0,                     /* bitpos */
525          complain_overflow_signed, /* complain_on_overflow */
526          mmix_elf_reloc,        /* special_function */
527          "R_MMIX_JMP_2",        /* name */
528          false,                 /* partial_inplace */
529          0x1ffffff,             /* src_mask */
530          0x1ffffff,             /* dst_mask */
531          true),                 /* pcrel_offset */
532
533   HOWTO (R_MMIX_JMP_3,          /* type */
534          2,                     /* rightshift */
535          2,                     /* size (0 = byte, 1 = short, 2 = long) */
536          27,                    /* bitsize */
537          true,                  /* pc_relative */
538          0,                     /* bitpos */
539          complain_overflow_signed, /* complain_on_overflow */
540          mmix_elf_reloc,        /* special_function */
541          "R_MMIX_JMP_3",        /* name */
542          false,                 /* partial_inplace */
543          0x1ffffff,             /* src_mask */
544          0x1ffffff,             /* dst_mask */
545          true),                 /* pcrel_offset */
546
547   /* When we don't emit link-time-relaxable code from the assembler, or
548      when relaxation has done all it can do, these relocs are used.  For
549      GETA/PUSHJ/branches.  */
550   HOWTO (R_MMIX_ADDR19,         /* type */
551          2,                     /* rightshift */
552          2,                     /* size (0 = byte, 1 = short, 2 = long) */
553          19,                    /* bitsize */
554          true,                  /* pc_relative */
555          0,                     /* bitpos */
556          complain_overflow_signed, /* complain_on_overflow */
557          mmix_elf_reloc,        /* special_function */
558          "R_MMIX_ADDR19",       /* name */
559          false,                 /* partial_inplace */
560          0x0100ffff,            /* src_mask */
561          0x0100ffff,            /* dst_mask */
562          true),                 /* pcrel_offset */
563
564   /* For JMP.  */
565   HOWTO (R_MMIX_ADDR27,         /* type */
566          2,                     /* rightshift */
567          2,                     /* size (0 = byte, 1 = short, 2 = long) */
568          27,                    /* bitsize */
569          true,                  /* pc_relative */
570          0,                     /* bitpos */
571          complain_overflow_signed, /* complain_on_overflow */
572          mmix_elf_reloc,        /* special_function */
573          "R_MMIX_ADDR27",       /* name */
574          false,                 /* partial_inplace */
575          0x1ffffff,             /* src_mask */
576          0x1ffffff,             /* dst_mask */
577          true),                 /* pcrel_offset */
578
579   /* A general register or the value 0..255.  If a value, then the
580      instruction (offset -3) needs adjusting.  */
581   HOWTO (R_MMIX_REG_OR_BYTE,    /* type */
582          0,                     /* rightshift */
583          1,                     /* size (0 = byte, 1 = short, 2 = long) */
584          8,                     /* bitsize */
585          false,                 /* pc_relative */
586          0,                     /* bitpos */
587          complain_overflow_bitfield, /* complain_on_overflow */
588          mmix_elf_reloc,        /* special_function */
589          "R_MMIX_REG_OR_BYTE",  /* name */
590          false,                 /* partial_inplace */
591          0xff,                  /* src_mask */
592          0xff,                  /* dst_mask */
593          false),                /* pcrel_offset */
594
595   /* A general register.  */
596   HOWTO (R_MMIX_REG,            /* type */
597          0,                     /* rightshift */
598          1,                     /* size (0 = byte, 1 = short, 2 = long) */
599          8,                     /* bitsize */
600          false,                 /* pc_relative */
601          0,                     /* bitpos */
602          complain_overflow_bitfield, /* complain_on_overflow */
603          mmix_elf_reloc,        /* special_function */
604          "R_MMIX_REG",          /* name */
605          false,                 /* partial_inplace */
606          0xff,                  /* src_mask */
607          0xff,                  /* dst_mask */
608          false),                /* pcrel_offset */
609
610   /* A register plus an index, corresponding to the relocation expression.
611      The sizes must correspond to the valid range of the expression, while
612      the bitmasks correspond to what we store in the image.  */
613   HOWTO (R_MMIX_BASE_PLUS_OFFSET,       /* type */
614          0,                     /* rightshift */
615          4,                     /* size (0 = byte, 1 = short, 2 = long) */
616          64,                    /* bitsize */
617          false,                 /* pc_relative */
618          0,                     /* bitpos */
619          complain_overflow_bitfield, /* complain_on_overflow */
620          mmix_elf_reloc,        /* special_function */
621          "R_MMIX_BASE_PLUS_OFFSET", /* name */
622          false,                 /* partial_inplace */
623          0xffff,                /* src_mask */
624          0xffff,                /* dst_mask */
625          false),                /* pcrel_offset */
626
627   /* A "magic" relocation for a LOCAL expression, asserting that the
628      expression is less than the number of global registers.  No actual
629      modification of the contents is done.  Implementing this as a
630      relocation was less intrusive than e.g. putting such expressions in a
631      section to discard *after* relocation.  */
632   HOWTO (R_MMIX_LOCAL,          /* type */
633          0,                     /* rightshift */
634          0,                     /* size (0 = byte, 1 = short, 2 = long) */
635          0,                     /* bitsize */
636          false,                 /* pc_relative */
637          0,                     /* bitpos */
638          complain_overflow_dont, /* complain_on_overflow */
639          mmix_elf_reloc,        /* special_function */
640          "R_MMIX_LOCAL",        /* name */
641          false,                 /* partial_inplace */
642          0,                     /* src_mask */
643          0,                     /* dst_mask */
644          false),                /* pcrel_offset */
645  };
646
647
648 /* Map BFD reloc types to MMIX ELF reloc types.  */
649
650 struct mmix_reloc_map
651   {
652     bfd_reloc_code_real_type bfd_reloc_val;
653     enum elf_mmix_reloc_type elf_reloc_val;
654   };
655
656
657 static const struct mmix_reloc_map mmix_reloc_map[] =
658   {
659     {BFD_RELOC_NONE, R_MMIX_NONE},
660     {BFD_RELOC_8, R_MMIX_8},
661     {BFD_RELOC_16, R_MMIX_16},
662     {BFD_RELOC_24, R_MMIX_24},
663     {BFD_RELOC_32, R_MMIX_32},
664     {BFD_RELOC_64, R_MMIX_64},
665     {BFD_RELOC_8_PCREL, R_MMIX_PC_8},
666     {BFD_RELOC_16_PCREL, R_MMIX_PC_16},
667     {BFD_RELOC_24_PCREL, R_MMIX_PC_24},
668     {BFD_RELOC_32_PCREL, R_MMIX_PC_32},
669     {BFD_RELOC_64_PCREL, R_MMIX_PC_64},
670     {BFD_RELOC_VTABLE_INHERIT, R_MMIX_GNU_VTINHERIT},
671     {BFD_RELOC_VTABLE_ENTRY, R_MMIX_GNU_VTENTRY},
672     {BFD_RELOC_MMIX_GETA, R_MMIX_GETA},
673     {BFD_RELOC_MMIX_CBRANCH, R_MMIX_CBRANCH},
674     {BFD_RELOC_MMIX_PUSHJ, R_MMIX_PUSHJ},
675     {BFD_RELOC_MMIX_JMP, R_MMIX_JMP},
676     {BFD_RELOC_MMIX_ADDR19, R_MMIX_ADDR19},
677     {BFD_RELOC_MMIX_ADDR27, R_MMIX_ADDR27},
678     {BFD_RELOC_MMIX_REG_OR_BYTE, R_MMIX_REG_OR_BYTE},
679     {BFD_RELOC_MMIX_REG, R_MMIX_REG},
680     {BFD_RELOC_MMIX_BASE_PLUS_OFFSET, R_MMIX_BASE_PLUS_OFFSET},
681     {BFD_RELOC_MMIX_LOCAL, R_MMIX_LOCAL}
682   };
683
684 static reloc_howto_type *
685 bfd_elf64_bfd_reloc_type_lookup (abfd, code)
686      bfd *abfd ATTRIBUTE_UNUSED;
687      bfd_reloc_code_real_type code;
688 {
689   unsigned int i;
690
691   for (i = 0;
692        i < sizeof (mmix_reloc_map) / sizeof (mmix_reloc_map[0]);
693        i++)
694     {
695       if (mmix_reloc_map[i].bfd_reloc_val == code)
696         return &elf_mmix_howto_table[mmix_reloc_map[i].elf_reloc_val];
697     }
698
699   return NULL;
700 }
701
702
703 /* This function performs the actual bitfiddling and sanity check for a
704    final relocation.  Each relocation gets its *worst*-case expansion
705    in size when it arrives here; any reduction in size should have been
706    caught in linker relaxation earlier.  When we get here, the relocation
707    looks like the smallest instruction with SWYM:s (nop:s) appended to the
708    max size.  We fill in those nop:s.
709
710    R_MMIX_GETA: (FIXME: Relaxation should break this up in 1, 2, 3 tetra)
711     GETA $N,foo
712    ->
713     SETL $N,foo & 0xffff
714     INCML $N,(foo >> 16) & 0xffff
715     INCMH $N,(foo >> 32) & 0xffff
716     INCH $N,(foo >> 48) & 0xffff
717
718    R_MMIX_CBRANCH: (FIXME: Relaxation should break this up, but
719    condbranches needing relaxation might be rare enough to not be
720    worthwhile.)
721     [P]Bcc $N,foo
722    ->
723     [~P]B~cc $N,.+20
724     SETL $255,foo & ...
725     INCML ...
726     INCMH ...
727     INCH ...
728     GO $255,$255,0
729
730    R_MMIX_PUSHJ: (FIXME: Relaxation...)
731     PUSHJ $N,foo
732    ->
733     SETL $255,foo & ...
734     INCML ...
735     INCMH ...
736     INCH ...
737     PUSHGO $N,$255,0
738
739    R_MMIX_JMP: (FIXME: Relaxation...)
740     JMP foo
741    ->
742     SETL $255,foo & ...
743     INCML ...
744     INCMH ...
745     INCH ...
746     GO $255,$255,0
747
748    R_MMIX_ADDR19 and R_MMIX_ADDR27 are just filled in.  */
749
750 static bfd_reloc_status_type
751 mmix_elf_perform_relocation (isec, howto, datap, addr, value)
752      asection *isec;
753      reloc_howto_type *howto;
754      PTR datap;
755      bfd_vma addr ATTRIBUTE_UNUSED;
756      bfd_vma value;
757 {
758   bfd *abfd = isec->owner;
759   bfd_reloc_status_type flag = bfd_reloc_ok;
760   bfd_reloc_status_type r;
761   int offs = 0;
762   int reg = 255;
763
764   /* The worst case bits are all similar SETL/INCML/INCMH/INCH sequences.
765      We handle the differences here and the common sequence later.  */
766   switch (howto->type)
767     {
768     case R_MMIX_GETA:
769       offs = 0;
770       reg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
771
772       /* We change to an absolute value.  */
773       value += addr;
774       break;
775
776     case R_MMIX_CBRANCH:
777       {
778         int in1 = bfd_get_16 (abfd, (bfd_byte *) datap) << 16;
779
780         /* Invert the condition and prediction bit, and set the offset
781            to five instructions ahead.
782
783            We *can* do better if we want to.  If the branch is found to be
784            within limits, we could leave the branch as is; there'll just
785            be a bunch of NOP:s after it.  But we shouldn't see this
786            sequence often enough that it's worth doing it.  */
787
788         bfd_put_32 (abfd,
789                     (((in1 ^ ((PRED_INV_BIT | COND_INV_BIT) << 24)) & ~0xffff)
790                      | (24/4)),
791                     (bfd_byte *) datap);
792
793         /* Put a "GO $255,$255,0" after the common sequence.  */
794         bfd_put_32 (abfd,
795                     ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24) | 0xffff00,
796                     (bfd_byte *) datap + 20);
797
798         /* Common sequence starts at offset 4.  */
799         offs = 4;
800
801         /* We change to an absolute value.  */
802         value += addr;
803       }
804       break;
805
806     case R_MMIX_PUSHJ:
807       {
808         int inreg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
809
810         /* Put a "PUSHGO $N,$255,0" after the common sequence.  */
811         bfd_put_32 (abfd,
812                     ((PUSHGO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
813                     | (inreg << 16)
814                     | 0xff00,
815                     (bfd_byte *) datap + 16);
816
817         /* We change to an absolute value.  */
818         value += addr;
819       }
820       break;
821
822     case R_MMIX_JMP:
823       /* This one is a little special.  If we get here on a non-relaxing
824          link, and the destination is actually in range, we don't need to
825          execute the nops.
826          If so, we fall through to the bit-fiddling relocs.
827
828          FIXME: bfd_check_overflow seems broken; the relocation is
829          rightshifted before testing, so supply a zero rightshift.  */
830
831       if (! ((value & 3) == 0
832              && (r = bfd_check_overflow (complain_overflow_signed,
833                                          howto->bitsize,
834                                          0,
835                                          bfd_arch_bits_per_address (abfd),
836                                          value)) == bfd_reloc_ok))
837         {
838           /* If the relocation doesn't fit in a JMP, we let the NOP:s be
839              modified below, and put a "GO $255,$255,0" after the
840              address-loading sequence.  */
841           bfd_put_32 (abfd,
842                       ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
843                       | 0xffff00,
844                       (bfd_byte *) datap + 16);
845
846           /* We change to an absolute value.  */
847           value += addr;
848           break;
849         }
850       /* FALLTHROUGH. */
851     case R_MMIX_ADDR19:
852     case R_MMIX_ADDR27:
853       /* These must be in range, or else we emit an error.  */
854       if ((value & 3) == 0
855           /* Note rightshift 0; see above.  */
856           && (r = bfd_check_overflow (complain_overflow_signed,
857                                       howto->bitsize,
858                                       0,
859                                       bfd_arch_bits_per_address (abfd),
860                                       value)) == bfd_reloc_ok)
861         {
862           bfd_vma in1
863             = bfd_get_32 (abfd, (bfd_byte *) datap);
864           bfd_vma highbit;
865
866           if ((bfd_signed_vma) value < 0)
867             {
868               highbit = (1 << 24);
869               value += (1 << (howto->bitsize - 1));
870             }
871           else
872             highbit = 0;
873
874           value >>= 2;
875
876           bfd_put_32 (abfd,
877                       (in1 & ~howto->src_mask)
878                       | highbit
879                       | (value & howto->dst_mask),
880                       (bfd_byte *) datap);
881
882           return bfd_reloc_ok;
883         }
884       else
885         return bfd_reloc_overflow;
886
887     case R_MMIX_REG_OR_BYTE:
888     case R_MMIX_REG:
889       if (value > 255)
890         return bfd_reloc_overflow;
891       bfd_put_8 (abfd, value, datap);
892       return bfd_reloc_ok;
893
894     default:
895       BAD_CASE (howto->type);
896     }
897
898   /* This code adds the common SETL/INCML/INCMH/INCH worst-case
899      sequence.  */
900
901   /* Lowest two bits must be 0.  We return bfd_reloc_overflow for
902      everything that looks strange.  */
903   if (value & 3)
904     flag = bfd_reloc_overflow;
905
906   bfd_put_32 (abfd,
907               (SETL_INSN_BYTE << 24) | (value & 0xffff) | (reg << 16),
908               (bfd_byte *) datap + offs);
909   bfd_put_32 (abfd,
910               (INCML_INSN_BYTE << 24) | ((value >> 16) & 0xffff) | (reg << 16),
911               (bfd_byte *) datap + offs + 4);
912   bfd_put_32 (abfd,
913               (INCMH_INSN_BYTE << 24) | ((value >> 32) & 0xffff) | (reg << 16),
914               (bfd_byte *) datap + offs + 8);
915   bfd_put_32 (abfd,
916               (INCH_INSN_BYTE << 24) | ((value >> 48) & 0xffff) | (reg << 16),
917               (bfd_byte *) datap + offs + 12);
918
919   return flag;
920 }
921
922 /* Set the howto pointer for an MMIX ELF reloc (type RELA).  */
923
924 static void
925 mmix_info_to_howto_rela (abfd, cache_ptr, dst)
926      bfd *abfd ATTRIBUTE_UNUSED;
927      arelent *cache_ptr;
928      Elf64_Internal_Rela *dst;
929 {
930   unsigned int r_type;
931
932   r_type = ELF64_R_TYPE (dst->r_info);
933   BFD_ASSERT (r_type < (unsigned int) R_MMIX_max);
934   cache_ptr->howto = &elf_mmix_howto_table[r_type];
935 }
936
937 /* Any MMIX-specific relocation gets here at assembly time or when linking
938    to other formats (such as mmo); this is the relocation function from
939    the reloc_table.  We don't get here for final pure ELF linking.  */
940
941 static bfd_reloc_status_type
942 mmix_elf_reloc (abfd, reloc_entry, symbol, data, input_section,
943                 output_bfd, error_message)
944      bfd *abfd;
945      arelent *reloc_entry;
946      asymbol *symbol;
947      PTR data;
948      asection *input_section;
949      bfd *output_bfd;
950      char **error_message ATTRIBUTE_UNUSED;
951 {
952   bfd_vma relocation;
953   bfd_reloc_status_type r;
954   asection *reloc_target_output_section;
955   bfd_reloc_status_type flag = bfd_reloc_ok;
956   bfd_vma output_base = 0;
957   bfd_vma addr;
958
959   r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
960                              input_section, output_bfd, error_message);
961
962   /* If that was all that was needed (i.e. this isn't a final link, only
963      some segment adjustments), we're done.  */
964   if (r != bfd_reloc_continue)
965     return r;
966
967   if (bfd_is_und_section (symbol->section)
968       && (symbol->flags & BSF_WEAK) == 0
969       && output_bfd == (bfd *) NULL)
970     return bfd_reloc_undefined;
971
972   /* Is the address of the relocation really within the section?  */
973   if (reloc_entry->address > input_section->_cooked_size)
974     return bfd_reloc_outofrange;
975
976   /* Work out which section the relocation is targetted at and the
977      initial relocation command value.  */
978
979   /* Get symbol value.  (Common symbols are special.)  */
980   if (bfd_is_com_section (symbol->section))
981     relocation = 0;
982   else
983     relocation = symbol->value;
984
985   reloc_target_output_section = bfd_get_output_section (symbol);
986
987   /* Here the variable relocation holds the final address of the symbol we
988      are relocating against, plus any addend.  */
989   if (output_bfd)
990     output_base = 0;
991   else
992     output_base = reloc_target_output_section->vma;
993
994   relocation += output_base + symbol->section->output_offset;
995
996   /* Get position of relocation.  */
997   addr = (reloc_entry->address + input_section->output_section->vma
998           + input_section->output_offset);
999   if (output_bfd != (bfd *) NULL)
1000     {
1001       /* Add in supplied addend.  */
1002       relocation += reloc_entry->addend;
1003
1004       /* This is a partial relocation, and we want to apply the
1005          relocation to the reloc entry rather than the raw data.
1006          Modify the reloc inplace to reflect what we now know.  */
1007       reloc_entry->addend = relocation;
1008       reloc_entry->address += input_section->output_offset;
1009       return flag;
1010     }
1011
1012   return mmix_final_link_relocate (reloc_entry->howto, input_section,
1013                                    data, reloc_entry->address,
1014                                    reloc_entry->addend, relocation,
1015                                    bfd_asymbol_name (symbol),
1016                                    reloc_target_output_section);
1017 }
1018 \f
1019 /* Relocate an MMIX ELF section.  Modified from elf32-fr30.c; look to it
1020    for guidance if you're thinking of copying this.  */
1021
1022 static boolean
1023 mmix_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1024                            contents, relocs, local_syms, local_sections)
1025      bfd *output_bfd ATTRIBUTE_UNUSED;
1026      struct bfd_link_info *info;
1027      bfd *input_bfd;
1028      asection *input_section;
1029      bfd_byte *contents;
1030      Elf_Internal_Rela *relocs;
1031      Elf_Internal_Sym *local_syms;
1032      asection **local_sections;
1033 {
1034   Elf_Internal_Shdr *symtab_hdr;
1035   struct elf_link_hash_entry **sym_hashes;
1036   Elf_Internal_Rela *rel;
1037   Elf_Internal_Rela *relend;
1038
1039   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1040   sym_hashes = elf_sym_hashes (input_bfd);
1041   relend = relocs + input_section->reloc_count;
1042
1043   for (rel = relocs; rel < relend; rel ++)
1044     {
1045       reloc_howto_type *howto;
1046       unsigned long r_symndx;
1047       Elf_Internal_Sym *sym;
1048       asection *sec;
1049       struct elf_link_hash_entry *h;
1050       bfd_vma relocation;
1051       bfd_reloc_status_type r;
1052       const char *name = NULL;
1053       int r_type;
1054       boolean undefined_signalled = false;
1055
1056       r_type = ELF64_R_TYPE (rel->r_info);
1057
1058       if (r_type == R_MMIX_GNU_VTINHERIT
1059           || r_type == R_MMIX_GNU_VTENTRY)
1060         continue;
1061
1062       r_symndx = ELF64_R_SYM (rel->r_info);
1063
1064       if (info->relocateable)
1065         {
1066           /* This is a relocateable link.  We don't have to change
1067              anything, unless the reloc is against a section symbol,
1068              in which case we have to adjust according to where the
1069              section symbol winds up in the output section.  */
1070           if (r_symndx < symtab_hdr->sh_info)
1071             {
1072               sym = local_syms + r_symndx;
1073
1074               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1075                 {
1076                   sec = local_sections [r_symndx];
1077                   rel->r_addend += sec->output_offset + sym->st_value;
1078                 }
1079             }
1080
1081           continue;
1082         }
1083
1084       /* This is a final link.  */
1085       howto = elf_mmix_howto_table + ELF64_R_TYPE (rel->r_info);
1086       h = NULL;
1087       sym = NULL;
1088       sec = NULL;
1089
1090       if (r_symndx < symtab_hdr->sh_info)
1091         {
1092           sym = local_syms + r_symndx;
1093           sec = local_sections [r_symndx];
1094           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1095
1096           name = bfd_elf_string_from_elf_section
1097             (input_bfd, symtab_hdr->sh_link, sym->st_name);
1098           name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1099         }
1100       else
1101         {
1102           h = sym_hashes [r_symndx - symtab_hdr->sh_info];
1103
1104           while (h->root.type == bfd_link_hash_indirect
1105                  || h->root.type == bfd_link_hash_warning)
1106             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1107
1108           name = h->root.root.string;
1109
1110           if (h->root.type == bfd_link_hash_defined
1111               || h->root.type == bfd_link_hash_defweak)
1112             {
1113               sec = h->root.u.def.section;
1114               relocation = (h->root.u.def.value
1115                             + sec->output_section->vma
1116                             + sec->output_offset);
1117             }
1118           else if (h->root.type == bfd_link_hash_undefweak)
1119             relocation = 0;
1120           else if (info->shared
1121                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1122             relocation = 0;
1123           else
1124             {
1125               /* The test on undefined_signalled is redundant at the
1126                  moment, but kept for symmetry.  */
1127               if (! undefined_signalled
1128                   && ! ((*info->callbacks->undefined_symbol)
1129                         (info, h->root.root.string, input_bfd,
1130                          input_section, rel->r_offset, true)))
1131                 return false;
1132               undefined_signalled = true;
1133               relocation = 0;
1134             }
1135         }
1136
1137       r = mmix_final_link_relocate (howto, input_section,
1138                                     contents, rel->r_offset,
1139                                     rel->r_addend, relocation, name, sec);
1140
1141       if (r != bfd_reloc_ok)
1142         {
1143           boolean check_ok = true;
1144           const char * msg = (const char *) NULL;
1145
1146           switch (r)
1147             {
1148             case bfd_reloc_overflow:
1149               check_ok = info->callbacks->reloc_overflow
1150                 (info, name, howto->name, (bfd_vma) 0,
1151                  input_bfd, input_section, rel->r_offset);
1152               break;
1153
1154             case bfd_reloc_undefined:
1155               /* We may have sent this message above.  */
1156               if (! undefined_signalled)
1157                 check_ok = info->callbacks->undefined_symbol
1158                   (info, name, input_bfd, input_section, rel->r_offset,
1159                    true);
1160               undefined_signalled = true;
1161               break;
1162
1163             case bfd_reloc_outofrange:
1164               msg = _("internal error: out of range error");
1165               break;
1166
1167             case bfd_reloc_notsupported:
1168               msg = _("internal error: unsupported relocation error");
1169               break;
1170
1171             case bfd_reloc_dangerous:
1172               msg = _("internal error: dangerous relocation");
1173               break;
1174
1175             default:
1176               msg = _("internal error: unknown error");
1177               break;
1178             }
1179
1180           if (msg)
1181             check_ok = info->callbacks->warning
1182               (info, msg, name, input_bfd, input_section, rel->r_offset);
1183
1184           if (! check_ok)
1185             return false;
1186         }
1187     }
1188
1189   return true;
1190 }
1191 \f
1192 /* Perform a single relocation.  By default we use the standard BFD
1193    routines.  A few relocs we have to do ourselves.  */
1194
1195 static bfd_reloc_status_type
1196 mmix_final_link_relocate (howto, input_section, contents,
1197                           r_offset, r_addend, relocation, symname, symsec)
1198      reloc_howto_type *howto;
1199      asection *input_section;
1200      bfd_byte *contents;
1201      bfd_vma r_offset;
1202      bfd_signed_vma r_addend;
1203      bfd_vma relocation;
1204      const char *symname;
1205      asection *symsec;
1206 {
1207   bfd_reloc_status_type r = bfd_reloc_ok;
1208   bfd_vma addr
1209     = (input_section->output_section->vma
1210        + input_section->output_offset
1211        + r_offset);
1212   bfd_signed_vma srel
1213     = (bfd_signed_vma) relocation + r_addend;
1214
1215   switch (howto->type)
1216     {
1217       /* All these are PC-relative.  */
1218     case R_MMIX_PUSHJ:
1219     case R_MMIX_CBRANCH:
1220     case R_MMIX_ADDR19:
1221     case R_MMIX_GETA:
1222     case R_MMIX_ADDR27:
1223     case R_MMIX_JMP:
1224       contents += r_offset;
1225
1226       srel -= (input_section->output_section->vma
1227                + input_section->output_offset
1228                + r_offset);
1229
1230       r = mmix_elf_perform_relocation (input_section, howto, contents,
1231                                        addr, srel);
1232       break;
1233
1234     case R_MMIX_REG_OR_BYTE:
1235     case R_MMIX_REG:
1236       /* For now, we handle these alike.  They must refer to an register
1237          symbol, which is either relative to the register section and in
1238          the range 0..255, or is in the register contents section with vma
1239          regno * 8.  */
1240
1241       /* FIXME: A better way to check for reg contents section?
1242          FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
1243       if (symsec == NULL)
1244         return bfd_reloc_undefined;
1245
1246       if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1247                   MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1248         {
1249           if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1250             {
1251               /* The bfd_reloc_outofrange return value, though intuitively
1252                  a better value, will not get us an error.  */
1253               return bfd_reloc_overflow;
1254             }
1255           srel /= 8;
1256         }
1257       else if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1258                        MMIX_REG_SECTION_NAME) == 0)
1259         {
1260           if (srel < 0 || srel > 255)
1261             /* The bfd_reloc_outofrange return value, though intuitively a
1262                better value, will not get us an error.  */
1263             return bfd_reloc_overflow;
1264         }
1265       else
1266         {
1267           (*_bfd_error_handler)
1268             (_("%s: register relocation against non-register symbol: %s in %s"),
1269              bfd_get_filename (input_section->owner),
1270              symname == NULL || *symname == 0 ? _("(unknown)") : symname,
1271              bfd_get_section_name (symsec->owner, symsec));
1272
1273           /* The bfd_reloc_outofrange return value, though intuitively a
1274              better value, will not get us an error.  */
1275           return bfd_reloc_overflow;
1276         }
1277       contents += r_offset;
1278       r = mmix_elf_perform_relocation (input_section, howto, contents,
1279                                        addr, srel);
1280       break;
1281
1282     case R_MMIX_LOCAL:
1283       /* This isn't a real relocation, it's just an assertion that the
1284          final relocation value corresponds to a local register.  We
1285          ignore the actual relocation; nothing is changed.  */
1286       {
1287         asection *regsec
1288           = bfd_get_section_by_name (input_section->output_section->owner,
1289                                      MMIX_REG_CONTENTS_SECTION_NAME);
1290         bfd_vma first_global;
1291
1292         /* Check that this is an absolute value, or a reference to the
1293            register contents section or the register (symbol) section.
1294            Absolute numbers can get here as undefined section.  Undefined
1295            symbols are signalled elsewhere, so there's no conflict in us
1296            accidentally handling it.  */
1297         if (!bfd_is_abs_section (symsec)
1298             && !bfd_is_und_section (symsec)
1299             && strcmp (bfd_get_section_name (symsec->owner, symsec),
1300                        MMIX_REG_CONTENTS_SECTION_NAME) != 0
1301             && strcmp (bfd_get_section_name (symsec->owner, symsec),
1302                        MMIX_REG_SECTION_NAME) != 0)
1303         {
1304           (*_bfd_error_handler)
1305             (_("%s: directive LOCAL valid only with a register or absolute value"),
1306              bfd_get_filename (input_section->owner));
1307
1308           return bfd_reloc_overflow;
1309         }
1310
1311       /* If we don't have a register contents section, then $255 is the
1312          first global register.  */
1313       if (regsec == NULL)
1314         first_global = 255;
1315       else
1316         {
1317           first_global = bfd_get_section_vma (abfd, regsec) / 8;
1318           if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1319                       MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1320             {
1321               if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1322                 /* The bfd_reloc_outofrange return value, though
1323                    intuitively a better value, will not get us an error.  */
1324                 return bfd_reloc_overflow;
1325               srel /= 8;
1326             }
1327         }
1328
1329         if ((bfd_vma) srel >= first_global)
1330           {
1331             /* FIXME: Better error message.  */
1332             (*_bfd_error_handler)
1333               (_("%s: LOCAL directive: Register $%ld is not a local register.  First global register is $%ld."),
1334                bfd_get_filename (input_section->owner), (long) srel, (long) first_global);
1335
1336             return bfd_reloc_overflow;
1337           }
1338       }
1339       r = bfd_reloc_ok;
1340       break;
1341
1342     default:
1343       r = _bfd_final_link_relocate (howto, input_section->owner, input_section,
1344                                     contents, r_offset,
1345                                     relocation, r_addend);
1346     }
1347
1348   return r;
1349 }
1350 \f
1351 /* Return the section that should be marked against GC for a given
1352    relocation.  */
1353
1354 static asection *
1355 mmix_elf_gc_mark_hook (abfd, info, rel, h, sym)
1356      bfd *abfd;
1357      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1358      Elf_Internal_Rela *rel;
1359      struct elf_link_hash_entry *h;
1360      Elf_Internal_Sym *sym;
1361 {
1362   if (h != NULL)
1363     {
1364       switch (ELF64_R_TYPE (rel->r_info))
1365         {
1366         case R_MMIX_GNU_VTINHERIT:
1367         case R_MMIX_GNU_VTENTRY:
1368           break;
1369
1370         default:
1371           switch (h->root.type)
1372             {
1373             case bfd_link_hash_defined:
1374             case bfd_link_hash_defweak:
1375               return h->root.u.def.section;
1376
1377             case bfd_link_hash_common:
1378               return h->root.u.c.p->section;
1379
1380             default:
1381               break;
1382             }
1383         }
1384     }
1385   else
1386     {
1387       return bfd_section_from_elf_index (abfd, sym->st_shndx);
1388     }
1389
1390   return NULL;
1391 }
1392 \f
1393 /* Sort register relocs to come before expanding relocs.  */
1394
1395 static int
1396 mmix_elf_sort_relocs (p1, p2)
1397      const PTR p1;
1398      const PTR p2;
1399 {
1400   const Elf_Internal_Rela *r1 = (const Elf_Internal_Rela *) p1;
1401   const Elf_Internal_Rela *r2 = (const Elf_Internal_Rela *) p2;
1402   int r1_is_reg, r2_is_reg;
1403
1404   /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
1405      insns.  */
1406   if ((r1->r_offset & ~(bfd_vma) 3) > (r2->r_offset & ~(bfd_vma) 3))
1407     return 1;
1408   else if ((r1->r_offset & ~(bfd_vma) 3) < (r2->r_offset & ~(bfd_vma) 3))
1409     return -1;
1410
1411   r1_is_reg
1412     = (ELF64_R_TYPE (r1->r_info) == R_MMIX_REG_OR_BYTE
1413        || ELF64_R_TYPE (r1->r_info) == R_MMIX_REG);
1414   r2_is_reg
1415     = (ELF64_R_TYPE (r2->r_info) == R_MMIX_REG_OR_BYTE
1416        || ELF64_R_TYPE (r2->r_info) == R_MMIX_REG);
1417   if (r1_is_reg != r2_is_reg)
1418     return r2_is_reg - r1_is_reg;
1419
1420   /* Neither or both are register relocs.  Then sort on full offset.  */
1421   if (r1->r_offset > r2->r_offset)
1422     return 1;
1423   else if (r1->r_offset < r2->r_offset)
1424     return -1;
1425   return 0;
1426 }
1427
1428 /* Look through the relocs for a section during the first phase.  */
1429
1430 static boolean
1431 mmix_elf_check_relocs (abfd, info, sec, relocs)
1432      bfd *abfd;
1433      struct bfd_link_info *info;
1434      asection *sec;
1435      const Elf_Internal_Rela *relocs;
1436 {
1437   Elf_Internal_Shdr *symtab_hdr;
1438   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
1439   const Elf_Internal_Rela *rel;
1440   const Elf_Internal_Rela *rel_end;
1441
1442   if (info->relocateable)
1443     return true;
1444
1445   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1446   sym_hashes = elf_sym_hashes (abfd);
1447   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf64_External_Sym);
1448   if (!elf_bad_symtab (abfd))
1449     sym_hashes_end -= symtab_hdr->sh_info;
1450
1451   /* First we sort the relocs so that any register relocs come before
1452      expansion-relocs to the same insn.  FIXME: Not done for mmo.  */
1453   qsort ((PTR) relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1454          mmix_elf_sort_relocs);
1455
1456   rel_end = relocs + sec->reloc_count;
1457   for (rel = relocs; rel < rel_end; rel++)
1458     {
1459       struct elf_link_hash_entry *h;
1460       unsigned long r_symndx;
1461
1462       r_symndx = ELF64_R_SYM (rel->r_info);
1463       if (r_symndx < symtab_hdr->sh_info)
1464         h = NULL;
1465       else
1466         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1467
1468       switch (ELF64_R_TYPE (rel->r_info))
1469         {
1470         /* This relocation describes the C++ object vtable hierarchy.
1471            Reconstruct it for later use during GC.  */
1472         case R_MMIX_GNU_VTINHERIT:
1473           if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1474             return false;
1475           break;
1476
1477         /* This relocation describes which C++ vtable entries are actually
1478            used.  Record for later use during GC.  */
1479         case R_MMIX_GNU_VTENTRY:
1480           if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1481             return false;
1482           break;
1483         }
1484     }
1485
1486   return true;
1487 }
1488 \f
1489 /* Change symbols relative to the reg contents section to instead be to
1490    the register section, and scale them down to correspond to the register
1491    number.  */
1492
1493 static boolean
1494 mmix_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
1495      bfd *abfd ATTRIBUTE_UNUSED;
1496      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1497      const char *name ATTRIBUTE_UNUSED;
1498      Elf_Internal_Sym *sym;
1499      asection *input_sec;
1500 {
1501   if (input_sec != NULL
1502       && input_sec->name != NULL
1503       && ELF_ST_TYPE (sym->st_info) != STT_SECTION
1504       && strcmp (input_sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1505     {
1506       sym->st_value /= 8;
1507       sym->st_shndx = SHN_REGISTER;
1508     }
1509
1510   return true;
1511 }
1512
1513 /* We fake a register section that holds values that are register numbers.
1514    Having a SHN_REGISTER and register section translates better to other
1515    formats (e.g. mmo) than for example a STT_REGISTER attribute.
1516    This section faking is based on a construct in elf32-mips.c.  */
1517 static asection mmix_elf_reg_section;
1518 static asymbol mmix_elf_reg_section_symbol;
1519 static asymbol *mmix_elf_reg_section_symbol_ptr;
1520
1521 /* Handle the special MIPS section numbers that a symbol may use.
1522    This is used for both the 32-bit and the 64-bit ABI.  */
1523
1524 void
1525 mmix_elf_symbol_processing (abfd, asym)
1526      bfd *abfd ATTRIBUTE_UNUSED;
1527      asymbol *asym;
1528 {
1529   elf_symbol_type *elfsym;
1530
1531   elfsym = (elf_symbol_type *) asym;
1532   switch (elfsym->internal_elf_sym.st_shndx)
1533     {
1534     case SHN_REGISTER:
1535       if (mmix_elf_reg_section.name == NULL)
1536         {
1537           /* Initialize the register section.  */
1538           mmix_elf_reg_section.name = MMIX_REG_SECTION_NAME;
1539           mmix_elf_reg_section.flags = SEC_NO_FLAGS;
1540           mmix_elf_reg_section.output_section = &mmix_elf_reg_section;
1541           mmix_elf_reg_section.symbol = &mmix_elf_reg_section_symbol;
1542           mmix_elf_reg_section.symbol_ptr_ptr = &mmix_elf_reg_section_symbol_ptr;
1543           mmix_elf_reg_section_symbol.name = MMIX_REG_SECTION_NAME;
1544           mmix_elf_reg_section_symbol.flags = BSF_SECTION_SYM;
1545           mmix_elf_reg_section_symbol.section = &mmix_elf_reg_section;
1546           mmix_elf_reg_section_symbol_ptr = &mmix_elf_reg_section_symbol;
1547         }
1548       asym->section = &mmix_elf_reg_section;
1549       break;
1550
1551     default:
1552       break;
1553     }
1554 }
1555
1556 /* Given a BFD section, try to locate the corresponding ELF section
1557    index.  */
1558
1559 static boolean
1560 mmix_elf_section_from_bfd_section (abfd, hdr, sec, retval)
1561      bfd *                 abfd ATTRIBUTE_UNUSED;
1562      Elf64_Internal_Shdr * hdr ATTRIBUTE_UNUSED;
1563      asection *            sec;
1564      int *                 retval;
1565 {
1566   if (strcmp (bfd_get_section_name (abfd, sec), MMIX_REG_SECTION_NAME) == 0)
1567     *retval = SHN_REGISTER;
1568   else
1569     return false;
1570
1571   return true;
1572 }
1573
1574 /* Hook called by the linker routine which adds symbols from an object
1575    file.  We must handle the special SHN_REGISTER section number here.
1576
1577    We also check that we only have *one* each of the section-start
1578    symbols, since otherwise having two with the same value would cause
1579    them to be "merged", but with the contents serialized.  */
1580
1581 boolean
1582 mmix_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1583      bfd *abfd;
1584      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1585      const Elf_Internal_Sym *sym;
1586      const char **namep ATTRIBUTE_UNUSED;
1587      flagword *flagsp ATTRIBUTE_UNUSED;
1588      asection **secp;
1589      bfd_vma *valp ATTRIBUTE_UNUSED;
1590 {
1591   if (sym->st_shndx == SHN_REGISTER)
1592     *secp = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
1593   else if ((*namep)[0] == '_' && (*namep)[1] == '_' && (*namep)[2] == '.'
1594            && strncmp (*namep, MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
1595                        strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)) == 0)
1596     {
1597       /* See if we have another one.  */
1598       struct elf_link_hash_entry *h
1599         = (struct elf_link_hash_entry *) bfd_link_hash_lookup (info->hash,
1600                                                                *namep,
1601                                                                false,
1602                                                                false, false);
1603
1604       if (h != NULL && h->root.type != bfd_link_hash_undefined)
1605         {
1606           /* How do we get the asymbol (or really: the filename) from h?
1607              h->root.u.def.section->owner is NULL.  */
1608           ((*_bfd_error_handler)
1609            (_("%s: Error: multiple definition of `%s'; start of %s is set in a earlier linked file\n"),
1610             bfd_get_filename (abfd), *namep,
1611             *namep + strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)));
1612            bfd_set_error (bfd_error_bad_value);
1613            return false;
1614         }
1615     }
1616
1617   return true;
1618 }
1619
1620 /* We consider symbols matching "L.*:[0-9]+" to be local symbols.  */
1621
1622 boolean
1623 mmix_elf_is_local_label_name (abfd, name)
1624      bfd *abfd;
1625      const char *name;
1626 {
1627   const char *colpos;
1628   int digits;
1629
1630   /* Also include the default local-label definition.  */
1631   if (_bfd_elf_is_local_label_name (abfd, name))
1632     return true;
1633
1634   if (*name != 'L')
1635     return false;
1636
1637   /* If there's no ":", or more than one, it's not a local symbol.  */
1638   colpos = strchr (name, ':');
1639   if (colpos == NULL || strchr (colpos + 1, ':') != NULL)
1640     return false;
1641
1642   /* Check that there are remaining characters and that they are digits.  */
1643   if (colpos[1] == 0)
1644     return false;
1645
1646   digits = strspn (colpos + 1, "0123456789");
1647   return digits != 0 && colpos[1 + digits] == 0;
1648 }
1649
1650 /* We get rid of the register section here.  */
1651
1652 boolean
1653 mmix_elf_final_link (abfd, info)
1654      bfd *abfd;
1655      struct bfd_link_info *info;
1656 {
1657   /* We never output a register section, though we create one for
1658      temporary measures.  Check that nobody entered contents into it.  */
1659   asection *reg_section;
1660   asection **secpp;
1661
1662   reg_section = bfd_get_section_by_name (abfd, MMIX_REG_SECTION_NAME);
1663
1664   if (reg_section != NULL)
1665     {
1666       /* FIXME: Pass error state gracefully.  */
1667       if (bfd_get_section_flags (abfd, reg_section) & SEC_HAS_CONTENTS)
1668         _bfd_abort (__FILE__, __LINE__, _("Register section has contents\n"));
1669
1670       /* Really remove the section.  */
1671       for (secpp = &abfd->sections;
1672            *secpp != reg_section;
1673            secpp = &(*secpp)->next)
1674         ;
1675       bfd_section_list_remove (abfd, secpp);
1676       --abfd->section_count;
1677     }
1678
1679   if (! bfd_elf64_bfd_final_link (abfd, info))
1680     return false;
1681
1682   return true;
1683 }
1684 \f
1685 #define ELF_ARCH                bfd_arch_mmix
1686 #define ELF_MACHINE_CODE        EM_MMIX
1687
1688 /* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
1689    However, that's too much for something somewhere in the linker part of
1690    BFD; perhaps the start-address has to be a non-zero multiple of this
1691    number, or larger than this number.  The symptom is that the linker
1692    complains: "warning: allocated section `.text' not in segment".  We
1693    settle for 64k; the page-size used in examples is 8k.
1694    #define ELF_MAXPAGESIZE 0x10000
1695
1696    Unfortunately, this causes excessive padding in the supposedly small
1697    for-education programs that are the expected usage (where people would
1698    inspect output).  We stick to 256 bytes just to have *some* default
1699    alignment.  */
1700 #define ELF_MAXPAGESIZE 0x100
1701
1702 #define TARGET_BIG_SYM          bfd_elf64_mmix_vec
1703 #define TARGET_BIG_NAME         "elf64-mmix"
1704
1705 #define elf_info_to_howto_rel           NULL
1706 #define elf_info_to_howto               mmix_info_to_howto_rela
1707 #define elf_backend_relocate_section    mmix_elf_relocate_section
1708 #define elf_backend_gc_mark_hook        mmix_elf_gc_mark_hook
1709 #define elf_backend_link_output_symbol_hook \
1710         mmix_elf_link_output_symbol_hook
1711 #define elf_backend_add_symbol_hook     mmix_elf_add_symbol_hook
1712
1713 #define elf_backend_check_relocs        mmix_elf_check_relocs
1714 #define elf_backend_symbol_processing   mmix_elf_symbol_processing
1715
1716 #define bfd_elf64_bfd_is_local_label_name \
1717         mmix_elf_is_local_label_name
1718
1719 #define elf_backend_may_use_rel_p       0
1720 #define elf_backend_may_use_rela_p      1
1721 #define elf_backend_default_use_rela_p  1
1722
1723 #define elf_backend_can_gc_sections     1
1724 #define elf_backend_section_from_bfd_section \
1725         mmix_elf_section_from_bfd_section
1726
1727 #define bfd_elf64_bfd_final_link        mmix_elf_final_link
1728
1729 #include "elf64-target.h"