OSDN Git Service

2002-04-01 Nathan Williams <nathanw@wasabisystems.com>
[pf3gnuchains/pf3gnuchains4x.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
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "libbfd.h"
29 #include "elf-bfd.h"
30 #include "elf/mmix.h"
31 #include "opcode/mmix.h"
32
33 #define MINUS_ONE       (((bfd_vma) 0) - 1)
34
35 /* Put these everywhere in new code.  */
36 #define FATAL_DEBUG                                             \
37  _bfd_abort (__FILE__, __LINE__,                                \
38              "Internal: Non-debugged code (test-case missing)")
39
40 #define BAD_CASE(x)                             \
41  _bfd_abort (__FILE__, __LINE__,                \
42              "bad case for " #x)
43
44 /* For each section containing a base-plus-offset (BPO) reloc, we attach
45    this struct as elf_section_data (section)->tdata, which is otherwise
46    NULL.  */
47 struct bpo_reloc_section_info
48   {
49     /* The base is 1; this is the first number in this section.  */
50     size_t first_base_plus_offset_reloc;
51
52     /* Number of BPO-relocs in this section.  */
53     size_t n_bpo_relocs_this_section;
54
55     /* Running index, used at relocation time.  */
56     size_t bpo_index;
57
58     /* We don't have access to the bfd_link_info struct in
59        mmix_final_link_relocate.  What we really want to get at is the
60        global single struct greg_relocation, so we stash it here.  */
61     asection *bpo_greg_section;
62   };
63
64 /* Helper struct (in global context) for the one below.
65    There's one of these created for every BPO reloc.  */
66 struct bpo_reloc_request
67   {
68     bfd_vma value;
69
70     /* Valid after relaxation.  The base is 0; the first register number
71        must be added.  The offset is in range 0..255.  */
72     size_t regindex;
73     size_t offset;
74
75     /* The order number for this BPO reloc, corresponding to the order in
76        which BPO relocs were found.  Used to create an index after reloc
77        requests are sorted.  */
78     size_t bpo_reloc_no;
79
80     /* Set when the value is computed.  Better than coding "guard values"
81        into the other members.  Is false only for BPO relocs in a GC:ed
82        section.  */
83     boolean valid;
84   };
85
86 /* We attach this as elf_section_data (sec)->tdata in the linker-allocated
87    greg contents section (MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME),
88    which is linked into the register contents section
89    (MMIX_REG_CONTENTS_SECTION_NAME).  This section is created by the
90    linker; using the same hook as for usual with BPO relocs does not
91    collide.  */
92 struct bpo_greg_section_info
93   {
94     /* After GC, this reflects the number of remaining, non-excluded
95        BPO-relocs.  */
96     size_t n_bpo_relocs;
97
98     /* This is the number of allocated bpo_reloc_requests; the size of
99        sorted_indexes.  Valid after the check.*relocs functions are called
100        for all incoming sections.  It includes the number of BPO relocs in
101        sections that were GC:ed.  */
102     size_t n_max_bpo_relocs;
103
104     /* A counter used to find out when to fold the BPO gregs, since we
105        don't have a single "after-relaxation" hook.  */
106     size_t n_remaining_bpo_relocs_this_relaxation_round;
107
108     /* The number of linker-allocated GREGs resulting from BPO relocs.
109        This is an approximation after _bfd_mmix_allocated_gregs_init and
110        supposedly accurate after mmix_elf_relax_section is called for all
111        incoming non-collected sections.  */
112     size_t n_allocated_bpo_gregs;
113
114     /* Index into reloc_request[], sorted on increasing "value", secondary
115        by increasing index for strict sorting order.  */
116     size_t *bpo_reloc_indexes;
117
118     /* An array of all relocations, with the "value" member filled in by
119        the relaxation function.  */
120     struct bpo_reloc_request *reloc_request;
121   };
122
123 static boolean mmix_elf_link_output_symbol_hook
124   PARAMS ((bfd *, struct bfd_link_info *, const char *,
125            Elf_Internal_Sym *, asection *));
126
127 static bfd_reloc_status_type mmix_elf_reloc
128   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
129
130 static reloc_howto_type *bfd_elf64_bfd_reloc_type_lookup
131   PARAMS ((bfd *, bfd_reloc_code_real_type));
132
133 static void mmix_info_to_howto_rela
134   PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
135
136 static int mmix_elf_sort_relocs PARAMS ((const PTR, const PTR));
137
138 static boolean mmix_elf_check_relocs
139   PARAMS ((bfd *, struct bfd_link_info *, asection *,
140            const Elf_Internal_Rela *));
141
142 static boolean mmix_elf_check_common_relocs
143   PARAMS ((bfd *, struct bfd_link_info *, asection *,
144            const Elf_Internal_Rela *));
145
146 static boolean mmix_elf_relocate_section
147   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
148            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
149
150 static asection * mmix_elf_gc_mark_hook
151   PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
152            struct elf_link_hash_entry *, Elf_Internal_Sym *));
153
154 static boolean mmix_elf_gc_sweep_hook
155   PARAMS ((bfd *, struct bfd_link_info *, asection *,
156            const Elf_Internal_Rela *));
157
158 static bfd_reloc_status_type mmix_final_link_relocate
159   PARAMS ((reloc_howto_type *, asection *, bfd_byte *,
160            bfd_vma, bfd_signed_vma, bfd_vma, const char *, asection *));
161
162 static bfd_reloc_status_type mmix_elf_perform_relocation
163   PARAMS ((asection *, reloc_howto_type *, PTR, bfd_vma, bfd_vma));
164
165 static boolean mmix_elf_section_from_bfd_section
166   PARAMS ((bfd *, asection *, int *));
167
168 static boolean mmix_elf_add_symbol_hook
169   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
170            const char **, flagword *, asection **, bfd_vma *));
171
172 static boolean mmix_elf_is_local_label_name
173   PARAMS ((bfd *, const char *));
174
175 static int bpo_reloc_request_sort_fn PARAMS ((const PTR, const PTR));
176
177 static boolean mmix_elf_relax_section
178   PARAMS ((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
179            boolean *again));
180
181 extern boolean mmix_elf_final_link PARAMS ((bfd *, struct bfd_link_info *));
182
183 extern void mmix_elf_symbol_processing PARAMS ((bfd *, asymbol *));
184
185 /* Only intended to be called from a debugger.  */
186 extern void mmix_dump_bpo_gregs
187   PARAMS ((struct bfd_link_info *, bfd_error_handler_type));
188
189 /* Watch out: this currently needs to have elements with the same index as
190    their R_MMIX_ number.  */
191 static reloc_howto_type elf_mmix_howto_table[] =
192  {
193   /* This reloc does nothing.  */
194   HOWTO (R_MMIX_NONE,           /* type */
195          0,                     /* rightshift */
196          2,                     /* size (0 = byte, 1 = short, 2 = long) */
197          32,                    /* bitsize */
198          false,                 /* pc_relative */
199          0,                     /* bitpos */
200          complain_overflow_bitfield, /* complain_on_overflow */
201          bfd_elf_generic_reloc, /* special_function */
202          "R_MMIX_NONE",         /* name */
203          false,                 /* partial_inplace */
204          0,                     /* src_mask */
205          0,                     /* dst_mask */
206          false),                /* pcrel_offset */
207
208   /* An 8 bit absolute relocation.  */
209   HOWTO (R_MMIX_8,              /* type */
210          0,                     /* rightshift */
211          0,                     /* size (0 = byte, 1 = short, 2 = long) */
212          8,                     /* bitsize */
213          false,                 /* pc_relative */
214          0,                     /* bitpos */
215          complain_overflow_bitfield, /* complain_on_overflow */
216          bfd_elf_generic_reloc, /* special_function */
217          "R_MMIX_8",            /* name */
218          false,                 /* partial_inplace */
219          0,                     /* src_mask */
220          0xff,                  /* dst_mask */
221          false),                /* pcrel_offset */
222
223   /* An 16 bit absolute relocation.  */
224   HOWTO (R_MMIX_16,             /* type */
225          0,                     /* rightshift */
226          1,                     /* size (0 = byte, 1 = short, 2 = long) */
227          16,                    /* bitsize */
228          false,                 /* pc_relative */
229          0,                     /* bitpos */
230          complain_overflow_bitfield, /* complain_on_overflow */
231          bfd_elf_generic_reloc, /* special_function */
232          "R_MMIX_16",           /* name */
233          false,                 /* partial_inplace */
234          0,                     /* src_mask */
235          0xffff,                /* dst_mask */
236          false),                /* pcrel_offset */
237
238   /* An 24 bit absolute relocation.  */
239   HOWTO (R_MMIX_24,             /* type */
240          0,                     /* rightshift */
241          2,                     /* size (0 = byte, 1 = short, 2 = long) */
242          24,                    /* bitsize */
243          false,                 /* pc_relative */
244          0,                     /* bitpos */
245          complain_overflow_bitfield, /* complain_on_overflow */
246          bfd_elf_generic_reloc, /* special_function */
247          "R_MMIX_24",           /* name */
248          false,                 /* partial_inplace */
249          ~0xffffff,             /* src_mask */
250          0xffffff,              /* dst_mask */
251          false),                /* pcrel_offset */
252
253   /* A 32 bit absolute relocation.  */
254   HOWTO (R_MMIX_32,             /* type */
255          0,                     /* rightshift */
256          2,                     /* size (0 = byte, 1 = short, 2 = long) */
257          32,                    /* bitsize */
258          false,                 /* pc_relative */
259          0,                     /* bitpos */
260          complain_overflow_bitfield, /* complain_on_overflow */
261          bfd_elf_generic_reloc, /* special_function */
262          "R_MMIX_32",           /* name */
263          false,                 /* partial_inplace */
264          0,                     /* src_mask */
265          0xffffffff,            /* dst_mask */
266          false),                /* pcrel_offset */
267
268   /* 64 bit relocation.  */
269   HOWTO (R_MMIX_64,             /* type */
270          0,                     /* rightshift */
271          4,                     /* size (0 = byte, 1 = short, 2 = long) */
272          64,                    /* bitsize */
273          false,                 /* pc_relative */
274          0,                     /* bitpos */
275          complain_overflow_bitfield, /* complain_on_overflow */
276          bfd_elf_generic_reloc, /* special_function */
277          "R_MMIX_64",           /* name */
278          false,                 /* partial_inplace */
279          0,                     /* src_mask */
280          MINUS_ONE,             /* dst_mask */
281          false),                /* pcrel_offset */
282
283   /* An 8 bit PC-relative relocation.  */
284   HOWTO (R_MMIX_PC_8,           /* type */
285          0,                     /* rightshift */
286          0,                     /* size (0 = byte, 1 = short, 2 = long) */
287          8,                     /* bitsize */
288          true,                  /* pc_relative */
289          0,                     /* bitpos */
290          complain_overflow_bitfield, /* complain_on_overflow */
291          bfd_elf_generic_reloc, /* special_function */
292          "R_MMIX_PC_8",         /* name */
293          false,                 /* partial_inplace */
294          0,                     /* src_mask */
295          0xff,                  /* dst_mask */
296          true),                 /* pcrel_offset */
297
298   /* An 16 bit PC-relative relocation.  */
299   HOWTO (R_MMIX_PC_16,          /* type */
300          0,                     /* rightshift */
301          1,                     /* size (0 = byte, 1 = short, 2 = long) */
302          16,                    /* bitsize */
303          true,                  /* pc_relative */
304          0,                     /* bitpos */
305          complain_overflow_bitfield, /* complain_on_overflow */
306          bfd_elf_generic_reloc, /* special_function */
307          "R_MMIX_PC_16",        /* name */
308          false,                 /* partial_inplace */
309          0,                     /* src_mask */
310          0xffff,                /* dst_mask */
311          true),                 /* pcrel_offset */
312
313   /* An 24 bit PC-relative relocation.  */
314   HOWTO (R_MMIX_PC_24,          /* type */
315          0,                     /* rightshift */
316          2,                     /* size (0 = byte, 1 = short, 2 = long) */
317          24,                    /* bitsize */
318          true,                  /* pc_relative */
319          0,                     /* bitpos */
320          complain_overflow_bitfield, /* complain_on_overflow */
321          bfd_elf_generic_reloc, /* special_function */
322          "R_MMIX_PC_24",        /* name */
323          false,                 /* partial_inplace */
324          ~0xffffff,             /* src_mask */
325          0xffffff,              /* dst_mask */
326          true),                 /* pcrel_offset */
327
328   /* A 32 bit absolute PC-relative relocation.  */
329   HOWTO (R_MMIX_PC_32,          /* type */
330          0,                     /* rightshift */
331          2,                     /* size (0 = byte, 1 = short, 2 = long) */
332          32,                    /* bitsize */
333          true,                  /* pc_relative */
334          0,                     /* bitpos */
335          complain_overflow_bitfield, /* complain_on_overflow */
336          bfd_elf_generic_reloc, /* special_function */
337          "R_MMIX_PC_32",        /* name */
338          false,                 /* partial_inplace */
339          0,                     /* src_mask */
340          0xffffffff,            /* dst_mask */
341          true),                 /* pcrel_offset */
342
343   /* 64 bit PC-relative relocation.  */
344   HOWTO (R_MMIX_PC_64,          /* type */
345          0,                     /* rightshift */
346          4,                     /* size (0 = byte, 1 = short, 2 = long) */
347          64,                    /* bitsize */
348          true,                  /* pc_relative */
349          0,                     /* bitpos */
350          complain_overflow_bitfield, /* complain_on_overflow */
351          bfd_elf_generic_reloc, /* special_function */
352          "R_MMIX_PC_64",        /* name */
353          false,                 /* partial_inplace */
354          0,                     /* src_mask */
355          MINUS_ONE,             /* dst_mask */
356          true),                 /* pcrel_offset */
357
358   /* GNU extension to record C++ vtable hierarchy.  */
359   HOWTO (R_MMIX_GNU_VTINHERIT, /* type */
360          0,                     /* rightshift */
361          0,                     /* size (0 = byte, 1 = short, 2 = long) */
362          0,                     /* bitsize */
363          false,                 /* pc_relative */
364          0,                     /* bitpos */
365          complain_overflow_dont, /* complain_on_overflow */
366          NULL,                  /* special_function */
367          "R_MMIX_GNU_VTINHERIT", /* name */
368          false,                 /* partial_inplace */
369          0,                     /* src_mask */
370          0,                     /* dst_mask */
371          true),                 /* pcrel_offset */
372
373   /* GNU extension to record C++ vtable member usage.  */
374   HOWTO (R_MMIX_GNU_VTENTRY,    /* type */
375          0,                     /* rightshift */
376          0,                     /* size (0 = byte, 1 = short, 2 = long) */
377          0,                     /* bitsize */
378          false,                 /* pc_relative */
379          0,                     /* bitpos */
380          complain_overflow_dont, /* complain_on_overflow */
381          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
382          "R_MMIX_GNU_VTENTRY", /* name */
383          false,                 /* partial_inplace */
384          0,                     /* src_mask */
385          0,                     /* dst_mask */
386          false),                /* pcrel_offset */
387
388   /* The GETA relocation is supposed to get any address that could
389      possibly be reached by the GETA instruction.  It can silently expand
390      to get a 64-bit operand, but will complain if any of the two least
391      significant bits are set.  The howto members reflect a simple GETA.  */
392   HOWTO (R_MMIX_GETA,           /* type */
393          2,                     /* rightshift */
394          2,                     /* size (0 = byte, 1 = short, 2 = long) */
395          19,                    /* bitsize */
396          true,                  /* pc_relative */
397          0,                     /* bitpos */
398          complain_overflow_signed, /* complain_on_overflow */
399          mmix_elf_reloc,        /* special_function */
400          "R_MMIX_GETA",         /* name */
401          false,                 /* partial_inplace */
402          ~0x0100ffff,           /* src_mask */
403          0x0100ffff,            /* dst_mask */
404          true),                 /* pcrel_offset */
405
406   HOWTO (R_MMIX_GETA_1,         /* type */
407          2,                     /* rightshift */
408          2,                     /* size (0 = byte, 1 = short, 2 = long) */
409          19,                    /* bitsize */
410          true,                  /* pc_relative */
411          0,                     /* bitpos */
412          complain_overflow_signed, /* complain_on_overflow */
413          mmix_elf_reloc,        /* special_function */
414          "R_MMIX_GETA_1",               /* name */
415          false,                 /* partial_inplace */
416          ~0x0100ffff,           /* src_mask */
417          0x0100ffff,            /* dst_mask */
418          true),                 /* pcrel_offset */
419
420   HOWTO (R_MMIX_GETA_2,         /* type */
421          2,                     /* rightshift */
422          2,                     /* size (0 = byte, 1 = short, 2 = long) */
423          19,                    /* bitsize */
424          true,                  /* pc_relative */
425          0,                     /* bitpos */
426          complain_overflow_signed, /* complain_on_overflow */
427          mmix_elf_reloc,        /* special_function */
428          "R_MMIX_GETA_2",               /* name */
429          false,                 /* partial_inplace */
430          ~0x0100ffff,           /* src_mask */
431          0x0100ffff,            /* dst_mask */
432          true),                 /* pcrel_offset */
433
434   HOWTO (R_MMIX_GETA_3,         /* type */
435          2,                     /* rightshift */
436          2,                     /* size (0 = byte, 1 = short, 2 = long) */
437          19,                    /* bitsize */
438          true,                  /* pc_relative */
439          0,                     /* bitpos */
440          complain_overflow_signed, /* complain_on_overflow */
441          mmix_elf_reloc,        /* special_function */
442          "R_MMIX_GETA_3",               /* name */
443          false,                 /* partial_inplace */
444          ~0x0100ffff,           /* src_mask */
445          0x0100ffff,            /* dst_mask */
446          true),                 /* pcrel_offset */
447
448   /* The conditional branches are supposed to reach any (code) address.
449      It can silently expand to a 64-bit operand, but will emit an error if
450      any of the two least significant bits are set.  The howto members
451      reflect a simple branch.  */
452   HOWTO (R_MMIX_CBRANCH,        /* type */
453          2,                     /* rightshift */
454          2,                     /* size (0 = byte, 1 = short, 2 = long) */
455          19,                    /* bitsize */
456          true,                  /* pc_relative */
457          0,                     /* bitpos */
458          complain_overflow_signed, /* complain_on_overflow */
459          mmix_elf_reloc,        /* special_function */
460          "R_MMIX_CBRANCH",      /* name */
461          false,                 /* partial_inplace */
462          ~0x0100ffff,           /* src_mask */
463          0x0100ffff,            /* dst_mask */
464          true),                 /* pcrel_offset */
465
466   HOWTO (R_MMIX_CBRANCH_J,      /* type */
467          2,                     /* rightshift */
468          2,                     /* size (0 = byte, 1 = short, 2 = long) */
469          19,                    /* bitsize */
470          true,                  /* pc_relative */
471          0,                     /* bitpos */
472          complain_overflow_signed, /* complain_on_overflow */
473          mmix_elf_reloc,        /* special_function */
474          "R_MMIX_CBRANCH_J",    /* name */
475          false,                 /* partial_inplace */
476          ~0x0100ffff,           /* src_mask */
477          0x0100ffff,            /* dst_mask */
478          true),                 /* pcrel_offset */
479
480   HOWTO (R_MMIX_CBRANCH_1,      /* type */
481          2,                     /* rightshift */
482          2,                     /* size (0 = byte, 1 = short, 2 = long) */
483          19,                    /* bitsize */
484          true,                  /* pc_relative */
485          0,                     /* bitpos */
486          complain_overflow_signed, /* complain_on_overflow */
487          mmix_elf_reloc,        /* special_function */
488          "R_MMIX_CBRANCH_1",    /* name */
489          false,                 /* partial_inplace */
490          ~0x0100ffff,           /* src_mask */
491          0x0100ffff,            /* dst_mask */
492          true),                 /* pcrel_offset */
493
494   HOWTO (R_MMIX_CBRANCH_2,      /* type */
495          2,                     /* rightshift */
496          2,                     /* size (0 = byte, 1 = short, 2 = long) */
497          19,                    /* bitsize */
498          true,                  /* pc_relative */
499          0,                     /* bitpos */
500          complain_overflow_signed, /* complain_on_overflow */
501          mmix_elf_reloc,        /* special_function */
502          "R_MMIX_CBRANCH_2",    /* name */
503          false,                 /* partial_inplace */
504          ~0x0100ffff,           /* src_mask */
505          0x0100ffff,            /* dst_mask */
506          true),                 /* pcrel_offset */
507
508   HOWTO (R_MMIX_CBRANCH_3,      /* type */
509          2,                     /* rightshift */
510          2,                     /* size (0 = byte, 1 = short, 2 = long) */
511          19,                    /* bitsize */
512          true,                  /* pc_relative */
513          0,                     /* bitpos */
514          complain_overflow_signed, /* complain_on_overflow */
515          mmix_elf_reloc,        /* special_function */
516          "R_MMIX_CBRANCH_3",    /* name */
517          false,                 /* partial_inplace */
518          ~0x0100ffff,           /* src_mask */
519          0x0100ffff,            /* dst_mask */
520          true),                 /* pcrel_offset */
521
522   /* The PUSHJ instruction can reach any (code) address, as long as it's
523      the beginning of a function (no usable restriction).  It can silently
524      expand to a 64-bit operand, but will emit an error if any of the two
525      least significant bits are set.  The howto members reflect a simple
526      PUSHJ.  */
527   HOWTO (R_MMIX_PUSHJ,          /* type */
528          2,                     /* rightshift */
529          2,                     /* size (0 = byte, 1 = short, 2 = long) */
530          19,                    /* bitsize */
531          true,                  /* pc_relative */
532          0,                     /* bitpos */
533          complain_overflow_signed, /* complain_on_overflow */
534          mmix_elf_reloc,        /* special_function */
535          "R_MMIX_PUSHJ",        /* name */
536          false,                 /* partial_inplace */
537          ~0x0100ffff,           /* src_mask */
538          0x0100ffff,            /* dst_mask */
539          true),                 /* pcrel_offset */
540
541   HOWTO (R_MMIX_PUSHJ_1,        /* type */
542          2,                     /* rightshift */
543          2,                     /* size (0 = byte, 1 = short, 2 = long) */
544          19,                    /* bitsize */
545          true,                  /* pc_relative */
546          0,                     /* bitpos */
547          complain_overflow_signed, /* complain_on_overflow */
548          mmix_elf_reloc,        /* special_function */
549          "R_MMIX_PUSHJ_1",      /* name */
550          false,                 /* partial_inplace */
551          ~0x0100ffff,           /* src_mask */
552          0x0100ffff,            /* dst_mask */
553          true),                 /* pcrel_offset */
554
555   HOWTO (R_MMIX_PUSHJ_2,        /* type */
556          2,                     /* rightshift */
557          2,                     /* size (0 = byte, 1 = short, 2 = long) */
558          19,                    /* bitsize */
559          true,                  /* pc_relative */
560          0,                     /* bitpos */
561          complain_overflow_signed, /* complain_on_overflow */
562          mmix_elf_reloc,        /* special_function */
563          "R_MMIX_PUSHJ_2",      /* name */
564          false,                 /* partial_inplace */
565          ~0x0100ffff,           /* src_mask */
566          0x0100ffff,            /* dst_mask */
567          true),                 /* pcrel_offset */
568
569   HOWTO (R_MMIX_PUSHJ_3,        /* type */
570          2,                     /* rightshift */
571          2,                     /* size (0 = byte, 1 = short, 2 = long) */
572          19,                    /* bitsize */
573          true,                  /* pc_relative */
574          0,                     /* bitpos */
575          complain_overflow_signed, /* complain_on_overflow */
576          mmix_elf_reloc,        /* special_function */
577          "R_MMIX_PUSHJ_3",      /* name */
578          false,                 /* partial_inplace */
579          ~0x0100ffff,           /* src_mask */
580          0x0100ffff,            /* dst_mask */
581          true),                 /* pcrel_offset */
582
583   /* A JMP is supposed to reach any (code) address.  By itself, it can
584      reach +-64M; the expansion can reach all 64 bits.  Note that the 64M
585      limit is soon reached if you link the program in wildly different
586      memory segments.  The howto members reflect a trivial JMP.  */
587   HOWTO (R_MMIX_JMP,            /* type */
588          2,                     /* rightshift */
589          2,                     /* size (0 = byte, 1 = short, 2 = long) */
590          27,                    /* bitsize */
591          true,                  /* pc_relative */
592          0,                     /* bitpos */
593          complain_overflow_signed, /* complain_on_overflow */
594          mmix_elf_reloc,        /* special_function */
595          "R_MMIX_JMP",          /* name */
596          false,                 /* partial_inplace */
597          ~0x1ffffff,            /* src_mask */
598          0x1ffffff,             /* dst_mask */
599          true),                 /* pcrel_offset */
600
601   HOWTO (R_MMIX_JMP_1,          /* type */
602          2,                     /* rightshift */
603          2,                     /* size (0 = byte, 1 = short, 2 = long) */
604          27,                    /* bitsize */
605          true,                  /* pc_relative */
606          0,                     /* bitpos */
607          complain_overflow_signed, /* complain_on_overflow */
608          mmix_elf_reloc,        /* special_function */
609          "R_MMIX_JMP_1",        /* name */
610          false,                 /* partial_inplace */
611          ~0x1ffffff,            /* src_mask */
612          0x1ffffff,             /* dst_mask */
613          true),                 /* pcrel_offset */
614
615   HOWTO (R_MMIX_JMP_2,          /* type */
616          2,                     /* rightshift */
617          2,                     /* size (0 = byte, 1 = short, 2 = long) */
618          27,                    /* bitsize */
619          true,                  /* pc_relative */
620          0,                     /* bitpos */
621          complain_overflow_signed, /* complain_on_overflow */
622          mmix_elf_reloc,        /* special_function */
623          "R_MMIX_JMP_2",        /* name */
624          false,                 /* partial_inplace */
625          ~0x1ffffff,            /* src_mask */
626          0x1ffffff,             /* dst_mask */
627          true),                 /* pcrel_offset */
628
629   HOWTO (R_MMIX_JMP_3,          /* type */
630          2,                     /* rightshift */
631          2,                     /* size (0 = byte, 1 = short, 2 = long) */
632          27,                    /* bitsize */
633          true,                  /* pc_relative */
634          0,                     /* bitpos */
635          complain_overflow_signed, /* complain_on_overflow */
636          mmix_elf_reloc,        /* special_function */
637          "R_MMIX_JMP_3",        /* name */
638          false,                 /* partial_inplace */
639          ~0x1ffffff,            /* src_mask */
640          0x1ffffff,             /* dst_mask */
641          true),                 /* pcrel_offset */
642
643   /* When we don't emit link-time-relaxable code from the assembler, or
644      when relaxation has done all it can do, these relocs are used.  For
645      GETA/PUSHJ/branches.  */
646   HOWTO (R_MMIX_ADDR19,         /* type */
647          2,                     /* rightshift */
648          2,                     /* size (0 = byte, 1 = short, 2 = long) */
649          19,                    /* bitsize */
650          true,                  /* pc_relative */
651          0,                     /* bitpos */
652          complain_overflow_signed, /* complain_on_overflow */
653          mmix_elf_reloc,        /* special_function */
654          "R_MMIX_ADDR19",       /* name */
655          false,                 /* partial_inplace */
656          ~0x0100ffff,           /* src_mask */
657          0x0100ffff,            /* dst_mask */
658          true),                 /* pcrel_offset */
659
660   /* For JMP.  */
661   HOWTO (R_MMIX_ADDR27,         /* type */
662          2,                     /* rightshift */
663          2,                     /* size (0 = byte, 1 = short, 2 = long) */
664          27,                    /* bitsize */
665          true,                  /* pc_relative */
666          0,                     /* bitpos */
667          complain_overflow_signed, /* complain_on_overflow */
668          mmix_elf_reloc,        /* special_function */
669          "R_MMIX_ADDR27",       /* name */
670          false,                 /* partial_inplace */
671          ~0x1ffffff,            /* src_mask */
672          0x1ffffff,             /* dst_mask */
673          true),                 /* pcrel_offset */
674
675   /* A general register or the value 0..255.  If a value, then the
676      instruction (offset -3) needs adjusting.  */
677   HOWTO (R_MMIX_REG_OR_BYTE,    /* type */
678          0,                     /* rightshift */
679          1,                     /* size (0 = byte, 1 = short, 2 = long) */
680          8,                     /* bitsize */
681          false,                 /* pc_relative */
682          0,                     /* bitpos */
683          complain_overflow_bitfield, /* complain_on_overflow */
684          mmix_elf_reloc,        /* special_function */
685          "R_MMIX_REG_OR_BYTE",  /* name */
686          false,                 /* partial_inplace */
687          0,                     /* src_mask */
688          0xff,                  /* dst_mask */
689          false),                /* pcrel_offset */
690
691   /* A general register.  */
692   HOWTO (R_MMIX_REG,            /* type */
693          0,                     /* rightshift */
694          1,                     /* size (0 = byte, 1 = short, 2 = long) */
695          8,                     /* bitsize */
696          false,                 /* pc_relative */
697          0,                     /* bitpos */
698          complain_overflow_bitfield, /* complain_on_overflow */
699          mmix_elf_reloc,        /* special_function */
700          "R_MMIX_REG",          /* name */
701          false,                 /* partial_inplace */
702          0,                     /* src_mask */
703          0xff,                  /* dst_mask */
704          false),                /* pcrel_offset */
705
706   /* A register plus an index, corresponding to the relocation expression.
707      The sizes must correspond to the valid range of the expression, while
708      the bitmasks correspond to what we store in the image.  */
709   HOWTO (R_MMIX_BASE_PLUS_OFFSET,       /* type */
710          0,                     /* rightshift */
711          4,                     /* size (0 = byte, 1 = short, 2 = long) */
712          64,                    /* bitsize */
713          false,                 /* pc_relative */
714          0,                     /* bitpos */
715          complain_overflow_bitfield, /* complain_on_overflow */
716          mmix_elf_reloc,        /* special_function */
717          "R_MMIX_BASE_PLUS_OFFSET", /* name */
718          false,                 /* partial_inplace */
719          0,                     /* src_mask */
720          0xffff,                /* dst_mask */
721          false),                /* pcrel_offset */
722
723   /* A "magic" relocation for a LOCAL expression, asserting that the
724      expression is less than the number of global registers.  No actual
725      modification of the contents is done.  Implementing this as a
726      relocation was less intrusive than e.g. putting such expressions in a
727      section to discard *after* relocation.  */
728   HOWTO (R_MMIX_LOCAL,          /* type */
729          0,                     /* rightshift */
730          0,                     /* size (0 = byte, 1 = short, 2 = long) */
731          0,                     /* bitsize */
732          false,                 /* pc_relative */
733          0,                     /* bitpos */
734          complain_overflow_dont, /* complain_on_overflow */
735          mmix_elf_reloc,        /* special_function */
736          "R_MMIX_LOCAL",        /* name */
737          false,                 /* partial_inplace */
738          0,                     /* src_mask */
739          0,                     /* dst_mask */
740          false),                /* pcrel_offset */
741  };
742
743
744 /* Map BFD reloc types to MMIX ELF reloc types.  */
745
746 struct mmix_reloc_map
747   {
748     bfd_reloc_code_real_type bfd_reloc_val;
749     enum elf_mmix_reloc_type elf_reloc_val;
750   };
751
752
753 static const struct mmix_reloc_map mmix_reloc_map[] =
754   {
755     {BFD_RELOC_NONE, R_MMIX_NONE},
756     {BFD_RELOC_8, R_MMIX_8},
757     {BFD_RELOC_16, R_MMIX_16},
758     {BFD_RELOC_24, R_MMIX_24},
759     {BFD_RELOC_32, R_MMIX_32},
760     {BFD_RELOC_64, R_MMIX_64},
761     {BFD_RELOC_8_PCREL, R_MMIX_PC_8},
762     {BFD_RELOC_16_PCREL, R_MMIX_PC_16},
763     {BFD_RELOC_24_PCREL, R_MMIX_PC_24},
764     {BFD_RELOC_32_PCREL, R_MMIX_PC_32},
765     {BFD_RELOC_64_PCREL, R_MMIX_PC_64},
766     {BFD_RELOC_VTABLE_INHERIT, R_MMIX_GNU_VTINHERIT},
767     {BFD_RELOC_VTABLE_ENTRY, R_MMIX_GNU_VTENTRY},
768     {BFD_RELOC_MMIX_GETA, R_MMIX_GETA},
769     {BFD_RELOC_MMIX_CBRANCH, R_MMIX_CBRANCH},
770     {BFD_RELOC_MMIX_PUSHJ, R_MMIX_PUSHJ},
771     {BFD_RELOC_MMIX_JMP, R_MMIX_JMP},
772     {BFD_RELOC_MMIX_ADDR19, R_MMIX_ADDR19},
773     {BFD_RELOC_MMIX_ADDR27, R_MMIX_ADDR27},
774     {BFD_RELOC_MMIX_REG_OR_BYTE, R_MMIX_REG_OR_BYTE},
775     {BFD_RELOC_MMIX_REG, R_MMIX_REG},
776     {BFD_RELOC_MMIX_BASE_PLUS_OFFSET, R_MMIX_BASE_PLUS_OFFSET},
777     {BFD_RELOC_MMIX_LOCAL, R_MMIX_LOCAL}
778   };
779
780 static reloc_howto_type *
781 bfd_elf64_bfd_reloc_type_lookup (abfd, code)
782      bfd *abfd ATTRIBUTE_UNUSED;
783      bfd_reloc_code_real_type code;
784 {
785   unsigned int i;
786
787   for (i = 0;
788        i < sizeof (mmix_reloc_map) / sizeof (mmix_reloc_map[0]);
789        i++)
790     {
791       if (mmix_reloc_map[i].bfd_reloc_val == code)
792         return &elf_mmix_howto_table[mmix_reloc_map[i].elf_reloc_val];
793     }
794
795   return NULL;
796 }
797
798
799 /* This function performs the actual bitfiddling and sanity check for a
800    final relocation.  Each relocation gets its *worst*-case expansion
801    in size when it arrives here; any reduction in size should have been
802    caught in linker relaxation earlier.  When we get here, the relocation
803    looks like the smallest instruction with SWYM:s (nop:s) appended to the
804    max size.  We fill in those nop:s.
805
806    R_MMIX_GETA: (FIXME: Relaxation should break this up in 1, 2, 3 tetra)
807     GETA $N,foo
808    ->
809     SETL $N,foo & 0xffff
810     INCML $N,(foo >> 16) & 0xffff
811     INCMH $N,(foo >> 32) & 0xffff
812     INCH $N,(foo >> 48) & 0xffff
813
814    R_MMIX_CBRANCH: (FIXME: Relaxation should break this up, but
815    condbranches needing relaxation might be rare enough to not be
816    worthwhile.)
817     [P]Bcc $N,foo
818    ->
819     [~P]B~cc $N,.+20
820     SETL $255,foo & ...
821     INCML ...
822     INCMH ...
823     INCH ...
824     GO $255,$255,0
825
826    R_MMIX_PUSHJ: (FIXME: Relaxation...)
827     PUSHJ $N,foo
828    ->
829     SETL $255,foo & ...
830     INCML ...
831     INCMH ...
832     INCH ...
833     PUSHGO $N,$255,0
834
835    R_MMIX_JMP: (FIXME: Relaxation...)
836     JMP foo
837    ->
838     SETL $255,foo & ...
839     INCML ...
840     INCMH ...
841     INCH ...
842     GO $255,$255,0
843
844    R_MMIX_ADDR19 and R_MMIX_ADDR27 are just filled in.  */
845
846 static bfd_reloc_status_type
847 mmix_elf_perform_relocation (isec, howto, datap, addr, value)
848      asection *isec;
849      reloc_howto_type *howto;
850      PTR datap;
851      bfd_vma addr ATTRIBUTE_UNUSED;
852      bfd_vma value;
853 {
854   bfd *abfd = isec->owner;
855   bfd_reloc_status_type flag = bfd_reloc_ok;
856   bfd_reloc_status_type r;
857   int offs = 0;
858   int reg = 255;
859
860   /* The worst case bits are all similar SETL/INCML/INCMH/INCH sequences.
861      We handle the differences here and the common sequence later.  */
862   switch (howto->type)
863     {
864     case R_MMIX_GETA:
865       offs = 0;
866       reg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
867
868       /* We change to an absolute value.  */
869       value += addr;
870       break;
871
872     case R_MMIX_CBRANCH:
873       {
874         int in1 = bfd_get_16 (abfd, (bfd_byte *) datap) << 16;
875
876         /* Invert the condition and prediction bit, and set the offset
877            to five instructions ahead.
878
879            We *can* do better if we want to.  If the branch is found to be
880            within limits, we could leave the branch as is; there'll just
881            be a bunch of NOP:s after it.  But we shouldn't see this
882            sequence often enough that it's worth doing it.  */
883
884         bfd_put_32 (abfd,
885                     (((in1 ^ ((PRED_INV_BIT | COND_INV_BIT) << 24)) & ~0xffff)
886                      | (24/4)),
887                     (bfd_byte *) datap);
888
889         /* Put a "GO $255,$255,0" after the common sequence.  */
890         bfd_put_32 (abfd,
891                     ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24) | 0xffff00,
892                     (bfd_byte *) datap + 20);
893
894         /* Common sequence starts at offset 4.  */
895         offs = 4;
896
897         /* We change to an absolute value.  */
898         value += addr;
899       }
900       break;
901
902     case R_MMIX_PUSHJ:
903       {
904         int inreg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
905
906         /* Put a "PUSHGO $N,$255,0" after the common sequence.  */
907         bfd_put_32 (abfd,
908                     ((PUSHGO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
909                     | (inreg << 16)
910                     | 0xff00,
911                     (bfd_byte *) datap + 16);
912
913         /* We change to an absolute value.  */
914         value += addr;
915       }
916       break;
917
918     case R_MMIX_JMP:
919       /* This one is a little special.  If we get here on a non-relaxing
920          link, and the destination is actually in range, we don't need to
921          execute the nops.
922          If so, we fall through to the bit-fiddling relocs.
923
924          FIXME: bfd_check_overflow seems broken; the relocation is
925          rightshifted before testing, so supply a zero rightshift.  */
926
927       if (! ((value & 3) == 0
928              && (r = bfd_check_overflow (complain_overflow_signed,
929                                          howto->bitsize,
930                                          0,
931                                          bfd_arch_bits_per_address (abfd),
932                                          value)) == bfd_reloc_ok))
933         {
934           /* If the relocation doesn't fit in a JMP, we let the NOP:s be
935              modified below, and put a "GO $255,$255,0" after the
936              address-loading sequence.  */
937           bfd_put_32 (abfd,
938                       ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
939                       | 0xffff00,
940                       (bfd_byte *) datap + 16);
941
942           /* We change to an absolute value.  */
943           value += addr;
944           break;
945         }
946       /* FALLTHROUGH. */
947     case R_MMIX_ADDR19:
948     case R_MMIX_ADDR27:
949       /* These must be in range, or else we emit an error.  */
950       if ((value & 3) == 0
951           /* Note rightshift 0; see above.  */
952           && (r = bfd_check_overflow (complain_overflow_signed,
953                                       howto->bitsize,
954                                       0,
955                                       bfd_arch_bits_per_address (abfd),
956                                       value)) == bfd_reloc_ok)
957         {
958           bfd_vma in1
959             = bfd_get_32 (abfd, (bfd_byte *) datap);
960           bfd_vma highbit;
961
962           if ((bfd_signed_vma) value < 0)
963             {
964               highbit = (1 << 24);
965               value += (1 << (howto->bitsize - 1));
966             }
967           else
968             highbit = 0;
969
970           value >>= 2;
971
972           bfd_put_32 (abfd,
973                       (in1 & howto->src_mask)
974                       | highbit
975                       | (value & howto->dst_mask),
976                       (bfd_byte *) datap);
977
978           return bfd_reloc_ok;
979         }
980       else
981         return bfd_reloc_overflow;
982
983     case R_MMIX_BASE_PLUS_OFFSET:
984       {
985         struct bpo_reloc_section_info *bpodata
986           = (struct bpo_reloc_section_info *)
987           elf_section_data (isec)->tdata;
988         asection *bpo_greg_section
989           = bpodata->bpo_greg_section;
990         struct bpo_greg_section_info *gregdata
991           = (struct bpo_greg_section_info *)
992           elf_section_data (bpo_greg_section)->tdata;
993         size_t bpo_index
994           = gregdata->bpo_reloc_indexes[bpodata->bpo_index++];
995
996         /* A consistency check: The value we now have in "relocation" must
997            be the same as the value we stored for that relocation.  It
998            doesn't cost much, so can be left in at all times.  */
999         if (value != gregdata->reloc_request[bpo_index].value)
1000           {
1001             (*_bfd_error_handler)
1002               (_("%s: Internal inconsistency error for value for\n\
1003  linker-allocated global register: linked: 0x%lx%08lx != relaxed: 0x%lx%08lx\n"),
1004                bfd_get_filename (isec->owner),
1005                (unsigned long) (value >> 32), (unsigned long) value,
1006                (unsigned long) (gregdata->reloc_request[bpo_index].value
1007                                 >> 32),
1008                (unsigned long) gregdata->reloc_request[bpo_index].value);
1009             bfd_set_error (bfd_error_bad_value);
1010             return bfd_reloc_overflow;
1011           }
1012
1013         /* Then store the register number and offset for that register
1014            into datap and datap + 1 respectively.  */
1015         bfd_put_8 (abfd,
1016                    gregdata->reloc_request[bpo_index].regindex
1017                    + bpo_greg_section->output_section->vma / 8,
1018                    datap);
1019         bfd_put_8 (abfd,
1020                    gregdata->reloc_request[bpo_index].offset,
1021                    ((unsigned char *) datap) + 1);
1022         return bfd_reloc_ok;
1023       }
1024
1025     case R_MMIX_REG_OR_BYTE:
1026     case R_MMIX_REG:
1027       if (value > 255)
1028         return bfd_reloc_overflow;
1029       bfd_put_8 (abfd, value, datap);
1030       return bfd_reloc_ok;
1031
1032     default:
1033       BAD_CASE (howto->type);
1034     }
1035
1036   /* This code adds the common SETL/INCML/INCMH/INCH worst-case
1037      sequence.  */
1038
1039   /* Lowest two bits must be 0.  We return bfd_reloc_overflow for
1040      everything that looks strange.  */
1041   if (value & 3)
1042     flag = bfd_reloc_overflow;
1043
1044   bfd_put_32 (abfd,
1045               (SETL_INSN_BYTE << 24) | (value & 0xffff) | (reg << 16),
1046               (bfd_byte *) datap + offs);
1047   bfd_put_32 (abfd,
1048               (INCML_INSN_BYTE << 24) | ((value >> 16) & 0xffff) | (reg << 16),
1049               (bfd_byte *) datap + offs + 4);
1050   bfd_put_32 (abfd,
1051               (INCMH_INSN_BYTE << 24) | ((value >> 32) & 0xffff) | (reg << 16),
1052               (bfd_byte *) datap + offs + 8);
1053   bfd_put_32 (abfd,
1054               (INCH_INSN_BYTE << 24) | ((value >> 48) & 0xffff) | (reg << 16),
1055               (bfd_byte *) datap + offs + 12);
1056
1057   return flag;
1058 }
1059
1060 /* Set the howto pointer for an MMIX ELF reloc (type RELA).  */
1061
1062 static void
1063 mmix_info_to_howto_rela (abfd, cache_ptr, dst)
1064      bfd *abfd ATTRIBUTE_UNUSED;
1065      arelent *cache_ptr;
1066      Elf64_Internal_Rela *dst;
1067 {
1068   unsigned int r_type;
1069
1070   r_type = ELF64_R_TYPE (dst->r_info);
1071   BFD_ASSERT (r_type < (unsigned int) R_MMIX_max);
1072   cache_ptr->howto = &elf_mmix_howto_table[r_type];
1073 }
1074
1075 /* Any MMIX-specific relocation gets here at assembly time or when linking
1076    to other formats (such as mmo); this is the relocation function from
1077    the reloc_table.  We don't get here for final pure ELF linking.  */
1078
1079 static bfd_reloc_status_type
1080 mmix_elf_reloc (abfd, reloc_entry, symbol, data, input_section,
1081                 output_bfd, error_message)
1082      bfd *abfd;
1083      arelent *reloc_entry;
1084      asymbol *symbol;
1085      PTR data;
1086      asection *input_section;
1087      bfd *output_bfd;
1088      char **error_message ATTRIBUTE_UNUSED;
1089 {
1090   bfd_vma relocation;
1091   bfd_reloc_status_type r;
1092   asection *reloc_target_output_section;
1093   bfd_reloc_status_type flag = bfd_reloc_ok;
1094   bfd_vma output_base = 0;
1095   bfd_vma addr;
1096
1097   r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1098                              input_section, output_bfd, error_message);
1099
1100   /* If that was all that was needed (i.e. this isn't a final link, only
1101      some segment adjustments), we're done.  */
1102   if (r != bfd_reloc_continue)
1103     return r;
1104
1105   if (bfd_is_und_section (symbol->section)
1106       && (symbol->flags & BSF_WEAK) == 0
1107       && output_bfd == (bfd *) NULL)
1108     return bfd_reloc_undefined;
1109
1110   /* Is the address of the relocation really within the section?  */
1111   if (reloc_entry->address > input_section->_cooked_size)
1112     return bfd_reloc_outofrange;
1113
1114   /* Work out which section the relocation is targetted at and the
1115      initial relocation command value.  */
1116
1117   /* Get symbol value.  (Common symbols are special.)  */
1118   if (bfd_is_com_section (symbol->section))
1119     relocation = 0;
1120   else
1121     relocation = symbol->value;
1122
1123   reloc_target_output_section = bfd_get_output_section (symbol);
1124
1125   /* Here the variable relocation holds the final address of the symbol we
1126      are relocating against, plus any addend.  */
1127   if (output_bfd)
1128     output_base = 0;
1129   else
1130     output_base = reloc_target_output_section->vma;
1131
1132   relocation += output_base + symbol->section->output_offset;
1133
1134   /* Get position of relocation.  */
1135   addr = (reloc_entry->address + input_section->output_section->vma
1136           + input_section->output_offset);
1137   if (output_bfd != (bfd *) NULL)
1138     {
1139       /* Add in supplied addend.  */
1140       relocation += reloc_entry->addend;
1141
1142       /* This is a partial relocation, and we want to apply the
1143          relocation to the reloc entry rather than the raw data.
1144          Modify the reloc inplace to reflect what we now know.  */
1145       reloc_entry->addend = relocation;
1146       reloc_entry->address += input_section->output_offset;
1147       return flag;
1148     }
1149
1150   return mmix_final_link_relocate (reloc_entry->howto, input_section,
1151                                    data, reloc_entry->address,
1152                                    reloc_entry->addend, relocation,
1153                                    bfd_asymbol_name (symbol),
1154                                    reloc_target_output_section);
1155 }
1156 \f
1157 /* Relocate an MMIX ELF section.  Modified from elf32-fr30.c; look to it
1158    for guidance if you're thinking of copying this.  */
1159
1160 static boolean
1161 mmix_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1162                            contents, relocs, local_syms, local_sections)
1163      bfd *output_bfd ATTRIBUTE_UNUSED;
1164      struct bfd_link_info *info;
1165      bfd *input_bfd;
1166      asection *input_section;
1167      bfd_byte *contents;
1168      Elf_Internal_Rela *relocs;
1169      Elf_Internal_Sym *local_syms;
1170      asection **local_sections;
1171 {
1172   Elf_Internal_Shdr *symtab_hdr;
1173   struct elf_link_hash_entry **sym_hashes;
1174   Elf_Internal_Rela *rel;
1175   Elf_Internal_Rela *relend;
1176
1177   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1178   sym_hashes = elf_sym_hashes (input_bfd);
1179   relend = relocs + input_section->reloc_count;
1180
1181   for (rel = relocs; rel < relend; rel ++)
1182     {
1183       reloc_howto_type *howto;
1184       unsigned long r_symndx;
1185       Elf_Internal_Sym *sym;
1186       asection *sec;
1187       struct elf_link_hash_entry *h;
1188       bfd_vma relocation;
1189       bfd_reloc_status_type r;
1190       const char *name = NULL;
1191       int r_type;
1192       boolean undefined_signalled = false;
1193
1194       r_type = ELF64_R_TYPE (rel->r_info);
1195
1196       if (r_type == R_MMIX_GNU_VTINHERIT
1197           || r_type == R_MMIX_GNU_VTENTRY)
1198         continue;
1199
1200       r_symndx = ELF64_R_SYM (rel->r_info);
1201
1202       if (info->relocateable)
1203         {
1204           /* This is a relocateable link.  We don't have to change
1205              anything, unless the reloc is against a section symbol,
1206              in which case we have to adjust according to where the
1207              section symbol winds up in the output section.  */
1208           if (r_symndx < symtab_hdr->sh_info)
1209             {
1210               sym = local_syms + r_symndx;
1211
1212               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1213                 {
1214                   sec = local_sections [r_symndx];
1215                   rel->r_addend += sec->output_offset + sym->st_value;
1216                 }
1217             }
1218
1219           continue;
1220         }
1221
1222       /* This is a final link.  */
1223       howto = elf_mmix_howto_table + ELF64_R_TYPE (rel->r_info);
1224       h = NULL;
1225       sym = NULL;
1226       sec = NULL;
1227
1228       if (r_symndx < symtab_hdr->sh_info)
1229         {
1230           sym = local_syms + r_symndx;
1231           sec = local_sections [r_symndx];
1232           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1233
1234           name = bfd_elf_string_from_elf_section
1235             (input_bfd, symtab_hdr->sh_link, sym->st_name);
1236           name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1237         }
1238       else
1239         {
1240           h = sym_hashes [r_symndx - symtab_hdr->sh_info];
1241
1242           while (h->root.type == bfd_link_hash_indirect
1243                  || h->root.type == bfd_link_hash_warning)
1244             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1245
1246           name = h->root.root.string;
1247
1248           if (h->root.type == bfd_link_hash_defined
1249               || h->root.type == bfd_link_hash_defweak)
1250             {
1251               sec = h->root.u.def.section;
1252               relocation = (h->root.u.def.value
1253                             + sec->output_section->vma
1254                             + sec->output_offset);
1255             }
1256           else if (h->root.type == bfd_link_hash_undefweak)
1257             relocation = 0;
1258           else if (info->shared
1259                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1260             relocation = 0;
1261           else
1262             {
1263               /* The test on undefined_signalled is redundant at the
1264                  moment, but kept for symmetry.  */
1265               if (! undefined_signalled
1266                   && ! ((*info->callbacks->undefined_symbol)
1267                         (info, h->root.root.string, input_bfd,
1268                          input_section, rel->r_offset, true)))
1269                 return false;
1270               undefined_signalled = true;
1271               relocation = 0;
1272             }
1273         }
1274
1275       r = mmix_final_link_relocate (howto, input_section,
1276                                     contents, rel->r_offset,
1277                                     rel->r_addend, relocation, name, sec);
1278
1279       if (r != bfd_reloc_ok)
1280         {
1281           boolean check_ok = true;
1282           const char * msg = (const char *) NULL;
1283
1284           switch (r)
1285             {
1286             case bfd_reloc_overflow:
1287               check_ok = info->callbacks->reloc_overflow
1288                 (info, name, howto->name, (bfd_vma) 0,
1289                  input_bfd, input_section, rel->r_offset);
1290               break;
1291
1292             case bfd_reloc_undefined:
1293               /* We may have sent this message above.  */
1294               if (! undefined_signalled)
1295                 check_ok = info->callbacks->undefined_symbol
1296                   (info, name, input_bfd, input_section, rel->r_offset,
1297                    true);
1298               undefined_signalled = true;
1299               break;
1300
1301             case bfd_reloc_outofrange:
1302               msg = _("internal error: out of range error");
1303               break;
1304
1305             case bfd_reloc_notsupported:
1306               msg = _("internal error: unsupported relocation error");
1307               break;
1308
1309             case bfd_reloc_dangerous:
1310               msg = _("internal error: dangerous relocation");
1311               break;
1312
1313             default:
1314               msg = _("internal error: unknown error");
1315               break;
1316             }
1317
1318           if (msg)
1319             check_ok = info->callbacks->warning
1320               (info, msg, name, input_bfd, input_section, rel->r_offset);
1321
1322           if (! check_ok)
1323             return false;
1324         }
1325     }
1326
1327   return true;
1328 }
1329 \f
1330 /* Perform a single relocation.  By default we use the standard BFD
1331    routines.  A few relocs we have to do ourselves.  */
1332
1333 static bfd_reloc_status_type
1334 mmix_final_link_relocate (howto, input_section, contents,
1335                           r_offset, r_addend, relocation, symname, symsec)
1336      reloc_howto_type *howto;
1337      asection *input_section;
1338      bfd_byte *contents;
1339      bfd_vma r_offset;
1340      bfd_signed_vma r_addend;
1341      bfd_vma relocation;
1342      const char *symname;
1343      asection *symsec;
1344 {
1345   bfd_reloc_status_type r = bfd_reloc_ok;
1346   bfd_vma addr
1347     = (input_section->output_section->vma
1348        + input_section->output_offset
1349        + r_offset);
1350   bfd_signed_vma srel
1351     = (bfd_signed_vma) relocation + r_addend;
1352
1353   switch (howto->type)
1354     {
1355       /* All these are PC-relative.  */
1356     case R_MMIX_PUSHJ:
1357     case R_MMIX_CBRANCH:
1358     case R_MMIX_ADDR19:
1359     case R_MMIX_GETA:
1360     case R_MMIX_ADDR27:
1361     case R_MMIX_JMP:
1362       contents += r_offset;
1363
1364       srel -= (input_section->output_section->vma
1365                + input_section->output_offset
1366                + r_offset);
1367
1368       r = mmix_elf_perform_relocation (input_section, howto, contents,
1369                                        addr, srel);
1370       break;
1371
1372     case R_MMIX_BASE_PLUS_OFFSET:
1373       if (symsec == NULL)
1374         return bfd_reloc_undefined;
1375
1376       /* Check that we're not relocating against a register symbol.  */
1377       if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1378                   MMIX_REG_CONTENTS_SECTION_NAME) == 0
1379           || strcmp (bfd_get_section_name (symsec->owner, symsec),
1380                      MMIX_REG_SECTION_NAME) == 0)
1381         {
1382           /* Note: This is separated out into two messages in order
1383              to ease the translation into other languages.  */
1384           if (symname == NULL || *symname == 0)
1385             (*_bfd_error_handler)
1386               (_("%s: base-plus-offset relocation against register symbol: (unknown) in %s"),
1387                bfd_get_filename (input_section->owner),
1388                bfd_get_section_name (symsec->owner, symsec));
1389           else
1390             (*_bfd_error_handler)
1391               (_("%s: base-plus-offset relocation against register symbol: %s in %s"),
1392                bfd_get_filename (input_section->owner), symname,
1393                bfd_get_section_name (symsec->owner, symsec));
1394           return bfd_reloc_overflow;
1395         }
1396       goto do_mmix_reloc;
1397
1398     case R_MMIX_REG_OR_BYTE:
1399     case R_MMIX_REG:
1400       /* For now, we handle these alike.  They must refer to an register
1401          symbol, which is either relative to the register section and in
1402          the range 0..255, or is in the register contents section with vma
1403          regno * 8.  */
1404
1405       /* FIXME: A better way to check for reg contents section?
1406          FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
1407       if (symsec == NULL)
1408         return bfd_reloc_undefined;
1409
1410       if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1411                   MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1412         {
1413           if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1414             {
1415               /* The bfd_reloc_outofrange return value, though intuitively
1416                  a better value, will not get us an error.  */
1417               return bfd_reloc_overflow;
1418             }
1419           srel /= 8;
1420         }
1421       else if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1422                        MMIX_REG_SECTION_NAME) == 0)
1423         {
1424           if (srel < 0 || srel > 255)
1425             /* The bfd_reloc_outofrange return value, though intuitively a
1426                better value, will not get us an error.  */
1427             return bfd_reloc_overflow;
1428         }
1429       else
1430         {
1431           /* Note: This is separated out into two messages in order
1432              to ease the translation into other languages.  */
1433           if (symname == NULL || *symname == 0)
1434             (*_bfd_error_handler)
1435               (_("%s: register relocation against non-register symbol: (unknown) in %s"),
1436                bfd_get_filename (input_section->owner),
1437                bfd_get_section_name (symsec->owner, symsec));
1438           else
1439             (*_bfd_error_handler)
1440               (_("%s: register relocation against non-register symbol: %s in %s"),
1441                bfd_get_filename (input_section->owner), symname,
1442                bfd_get_section_name (symsec->owner, symsec));
1443
1444           /* The bfd_reloc_outofrange return value, though intuitively a
1445              better value, will not get us an error.  */
1446           return bfd_reloc_overflow;
1447         }
1448     do_mmix_reloc:
1449       contents += r_offset;
1450       r = mmix_elf_perform_relocation (input_section, howto, contents,
1451                                        addr, srel);
1452       break;
1453
1454     case R_MMIX_LOCAL:
1455       /* This isn't a real relocation, it's just an assertion that the
1456          final relocation value corresponds to a local register.  We
1457          ignore the actual relocation; nothing is changed.  */
1458       {
1459         asection *regsec
1460           = bfd_get_section_by_name (input_section->output_section->owner,
1461                                      MMIX_REG_CONTENTS_SECTION_NAME);
1462         bfd_vma first_global;
1463
1464         /* Check that this is an absolute value, or a reference to the
1465            register contents section or the register (symbol) section.
1466            Absolute numbers can get here as undefined section.  Undefined
1467            symbols are signalled elsewhere, so there's no conflict in us
1468            accidentally handling it.  */
1469         if (!bfd_is_abs_section (symsec)
1470             && !bfd_is_und_section (symsec)
1471             && strcmp (bfd_get_section_name (symsec->owner, symsec),
1472                        MMIX_REG_CONTENTS_SECTION_NAME) != 0
1473             && strcmp (bfd_get_section_name (symsec->owner, symsec),
1474                        MMIX_REG_SECTION_NAME) != 0)
1475         {
1476           (*_bfd_error_handler)
1477             (_("%s: directive LOCAL valid only with a register or absolute value"),
1478              bfd_get_filename (input_section->owner));
1479
1480           return bfd_reloc_overflow;
1481         }
1482
1483       /* If we don't have a register contents section, then $255 is the
1484          first global register.  */
1485       if (regsec == NULL)
1486         first_global = 255;
1487       else
1488         {
1489           first_global = bfd_get_section_vma (abfd, regsec) / 8;
1490           if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1491                       MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1492             {
1493               if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1494                 /* The bfd_reloc_outofrange return value, though
1495                    intuitively a better value, will not get us an error.  */
1496                 return bfd_reloc_overflow;
1497               srel /= 8;
1498             }
1499         }
1500
1501         if ((bfd_vma) srel >= first_global)
1502           {
1503             /* FIXME: Better error message.  */
1504             (*_bfd_error_handler)
1505               (_("%s: LOCAL directive: Register $%ld is not a local register.  First global register is $%ld."),
1506                bfd_get_filename (input_section->owner), (long) srel, (long) first_global);
1507
1508             return bfd_reloc_overflow;
1509           }
1510       }
1511       r = bfd_reloc_ok;
1512       break;
1513
1514     default:
1515       r = _bfd_final_link_relocate (howto, input_section->owner, input_section,
1516                                     contents, r_offset,
1517                                     relocation, r_addend);
1518     }
1519
1520   return r;
1521 }
1522 \f
1523 /* Return the section that should be marked against GC for a given
1524    relocation.  */
1525
1526 static asection *
1527 mmix_elf_gc_mark_hook (abfd, info, rel, h, sym)
1528      bfd *abfd;
1529      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1530      Elf_Internal_Rela *rel;
1531      struct elf_link_hash_entry *h;
1532      Elf_Internal_Sym *sym;
1533 {
1534   if (h != NULL)
1535     {
1536       switch (ELF64_R_TYPE (rel->r_info))
1537         {
1538         case R_MMIX_GNU_VTINHERIT:
1539         case R_MMIX_GNU_VTENTRY:
1540           break;
1541
1542         default:
1543           switch (h->root.type)
1544             {
1545             case bfd_link_hash_defined:
1546             case bfd_link_hash_defweak:
1547               return h->root.u.def.section;
1548
1549             case bfd_link_hash_common:
1550               return h->root.u.c.p->section;
1551
1552             default:
1553               break;
1554             }
1555         }
1556     }
1557   else
1558     {
1559       return bfd_section_from_elf_index (abfd, sym->st_shndx);
1560     }
1561
1562   return NULL;
1563 }
1564
1565 /* Update relocation info for a GC-excluded section.  We could supposedly
1566    perform the allocation after GC, but there's no suitable hook between
1567    GC (or section merge) and the point when all input sections must be
1568    present.  Better to waste some memory and (perhaps) a little time.  */
1569
1570 static boolean
1571 mmix_elf_gc_sweep_hook (abfd, info, sec, relocs)
1572      bfd *abfd ATTRIBUTE_UNUSED;
1573      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1574      asection *sec ATTRIBUTE_UNUSED;
1575      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1576 {
1577   struct bpo_reloc_section_info *bpodata
1578     = (struct bpo_reloc_section_info *)
1579     elf_section_data (sec)->tdata;
1580   asection *allocated_gregs_section;
1581
1582   /* If no bpodata here, we have nothing to do.  */
1583   if (bpodata == NULL)
1584     return true;
1585
1586   allocated_gregs_section = bpodata->bpo_greg_section;
1587
1588   ((struct bpo_greg_section_info *)
1589    elf_section_data (allocated_gregs_section)->tdata)
1590     ->n_bpo_relocs
1591     -= bpodata->n_bpo_relocs_this_section;
1592
1593   return true;
1594 }
1595 \f
1596 /* Sort register relocs to come before expanding relocs.  */
1597
1598 static int
1599 mmix_elf_sort_relocs (p1, p2)
1600      const PTR p1;
1601      const PTR p2;
1602 {
1603   const Elf_Internal_Rela *r1 = (const Elf_Internal_Rela *) p1;
1604   const Elf_Internal_Rela *r2 = (const Elf_Internal_Rela *) p2;
1605   int r1_is_reg, r2_is_reg;
1606
1607   /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
1608      insns.  */
1609   if ((r1->r_offset & ~(bfd_vma) 3) > (r2->r_offset & ~(bfd_vma) 3))
1610     return 1;
1611   else if ((r1->r_offset & ~(bfd_vma) 3) < (r2->r_offset & ~(bfd_vma) 3))
1612     return -1;
1613
1614   r1_is_reg
1615     = (ELF64_R_TYPE (r1->r_info) == R_MMIX_REG_OR_BYTE
1616        || ELF64_R_TYPE (r1->r_info) == R_MMIX_REG);
1617   r2_is_reg
1618     = (ELF64_R_TYPE (r2->r_info) == R_MMIX_REG_OR_BYTE
1619        || ELF64_R_TYPE (r2->r_info) == R_MMIX_REG);
1620   if (r1_is_reg != r2_is_reg)
1621     return r2_is_reg - r1_is_reg;
1622
1623   /* Neither or both are register relocs.  Then sort on full offset.  */
1624   if (r1->r_offset > r2->r_offset)
1625     return 1;
1626   else if (r1->r_offset < r2->r_offset)
1627     return -1;
1628   return 0;
1629 }
1630
1631 /* Subset of mmix_elf_check_relocs, common to ELF and mmo linking.  */
1632
1633 static boolean
1634 mmix_elf_check_common_relocs  (abfd, info, sec, relocs)
1635      bfd *abfd;
1636      struct bfd_link_info *info;
1637      asection *sec;
1638      const Elf_Internal_Rela *relocs;
1639 {
1640   bfd *bpo_greg_owner = NULL;
1641   asection *allocated_gregs_section = NULL;
1642   struct bpo_greg_section_info *gregdata = NULL;
1643   struct bpo_reloc_section_info *bpodata = NULL;
1644   const Elf_Internal_Rela *rel;
1645   const Elf_Internal_Rela *rel_end;
1646
1647   if (info->relocateable)
1648     return true;
1649
1650   /* We currently have to abuse this COFF-specific member, since there's
1651      no target-machine-dedicated member.  There's no alternative outside
1652      the bfd_link_info struct; we can't specialize a hash-table since
1653      they're different between ELF and mmo.  */
1654   bpo_greg_owner = (bfd *) info->base_file;
1655
1656   rel_end = relocs + sec->reloc_count;
1657   for (rel = relocs; rel < rel_end; rel++)
1658     {
1659       switch (ELF64_R_TYPE (rel->r_info))
1660         {
1661           /* This relocation causes a GREG allocation.  We need to count
1662              them, and we need to create a section for them, so we need an
1663              object to fake as the owner of that section.  We can't use
1664              the ELF dynobj for this, since the ELF bits assume lots of
1665              DSO-related stuff if that member is non-NULL.  */
1666         case R_MMIX_BASE_PLUS_OFFSET:
1667           if (bpo_greg_owner == NULL)
1668             {
1669               bpo_greg_owner = abfd;
1670               info->base_file = (PTR) bpo_greg_owner;
1671             }
1672
1673           if (allocated_gregs_section == NULL)
1674             allocated_gregs_section
1675               = bfd_get_section_by_name (bpo_greg_owner,
1676                                          MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
1677
1678           if (allocated_gregs_section == NULL)
1679             {
1680               allocated_gregs_section
1681                 = bfd_make_section (bpo_greg_owner,
1682                                     MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
1683               /* Setting both SEC_ALLOC and SEC_LOAD means the section is
1684                  treated like any other section, and we'd get errors for
1685                  address overlap with the text section.  Let's set none of
1686                  those flags, as that is what currently happens for usual
1687                  GREG allocations, and that works.  */
1688               if (allocated_gregs_section == NULL
1689                   || !bfd_set_section_flags (bpo_greg_owner,
1690                                              allocated_gregs_section,
1691                                              (SEC_HAS_CONTENTS
1692                                               | SEC_IN_MEMORY
1693                                               | SEC_LINKER_CREATED))
1694                   || !bfd_set_section_alignment (bpo_greg_owner,
1695                                                  allocated_gregs_section,
1696                                                  3))
1697                 return false;
1698
1699               gregdata = (struct bpo_greg_section_info *)
1700                 bfd_zalloc (bpo_greg_owner, sizeof (struct bpo_greg_section_info));
1701               if (gregdata == NULL)
1702                 return false;
1703               elf_section_data (allocated_gregs_section)->tdata = gregdata;
1704             }
1705           else if (gregdata == NULL)
1706             gregdata = elf_section_data (allocated_gregs_section)->tdata;
1707
1708           /* Get ourselves some auxiliary info for the BPO-relocs.  */
1709           if (bpodata == NULL)
1710             {
1711               /* No use doing a separate iteration pass to find the upper
1712                  limit - just use the number of relocs.  */
1713               bpodata = (struct bpo_reloc_section_info *)
1714                 bfd_alloc (bpo_greg_owner,
1715                            sizeof (struct bpo_reloc_section_info)
1716                            * (sec->reloc_count + 1));
1717               if (bpodata == NULL)
1718                 return false;
1719               elf_section_data (sec)->tdata = bpodata;
1720               bpodata->first_base_plus_offset_reloc
1721                 = bpodata->bpo_index
1722                 = gregdata->n_max_bpo_relocs;
1723               bpodata->bpo_greg_section
1724                 = allocated_gregs_section;
1725               bpodata->n_bpo_relocs_this_section = 0;
1726             }
1727
1728           bpodata->n_bpo_relocs_this_section++;
1729           gregdata->n_max_bpo_relocs++;
1730
1731           /* We don't get another chance to set this before GC; we've not
1732              set up set up any hook that runs before GC.  */
1733           gregdata->n_bpo_relocs
1734             = gregdata->n_max_bpo_relocs;
1735           break;
1736         }
1737     }
1738
1739   return true;
1740 }
1741
1742 /* Look through the relocs for a section during the first phase.  */
1743
1744 static boolean
1745 mmix_elf_check_relocs (abfd, info, sec, relocs)
1746      bfd *abfd;
1747      struct bfd_link_info *info;
1748      asection *sec;
1749      const Elf_Internal_Rela *relocs;
1750 {
1751   Elf_Internal_Shdr *symtab_hdr;
1752   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
1753   const Elf_Internal_Rela *rel;
1754   const Elf_Internal_Rela *rel_end;
1755
1756   if (info->relocateable)
1757     return true;
1758
1759   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1760   sym_hashes = elf_sym_hashes (abfd);
1761   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf64_External_Sym);
1762   if (!elf_bad_symtab (abfd))
1763     sym_hashes_end -= symtab_hdr->sh_info;
1764
1765   /* First we sort the relocs so that any register relocs come before
1766      expansion-relocs to the same insn.  FIXME: Not done for mmo.  */
1767   qsort ((PTR) relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1768          mmix_elf_sort_relocs);
1769
1770   /* Do the common part.  */
1771   if (!mmix_elf_check_common_relocs (abfd, info, sec, relocs))
1772     return false;
1773
1774   rel_end = relocs + sec->reloc_count;
1775   for (rel = relocs; rel < rel_end; rel++)
1776     {
1777       struct elf_link_hash_entry *h;
1778       unsigned long r_symndx;
1779
1780       r_symndx = ELF64_R_SYM (rel->r_info);
1781       if (r_symndx < symtab_hdr->sh_info)
1782         h = NULL;
1783       else
1784         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1785
1786       switch (ELF64_R_TYPE (rel->r_info))
1787         {
1788         /* This relocation describes the C++ object vtable hierarchy.
1789            Reconstruct it for later use during GC.  */
1790         case R_MMIX_GNU_VTINHERIT:
1791           if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1792             return false;
1793           break;
1794
1795         /* This relocation describes which C++ vtable entries are actually
1796            used.  Record for later use during GC.  */
1797         case R_MMIX_GNU_VTENTRY:
1798           if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1799             return false;
1800           break;
1801         }
1802     }
1803
1804   return true;
1805 }
1806
1807 /* Wrapper for mmix_elf_check_common_relocs, called when linking to mmo.
1808    Copied from elf_link_add_object_symbols.  */
1809
1810 boolean
1811 _bfd_mmix_check_all_relocs (abfd, info)
1812      bfd *abfd;
1813      struct bfd_link_info *info;
1814 {
1815   asection *o;
1816
1817   for (o = abfd->sections; o != NULL; o = o->next)
1818     {
1819       Elf_Internal_Rela *internal_relocs;
1820       boolean ok;
1821
1822       if ((o->flags & SEC_RELOC) == 0
1823           || o->reloc_count == 0
1824           || ((info->strip == strip_all || info->strip == strip_debugger)
1825               && (o->flags & SEC_DEBUGGING) != 0)
1826           || bfd_is_abs_section (o->output_section))
1827         continue;
1828
1829       internal_relocs
1830         = _bfd_elf64_link_read_relocs (abfd, o, (PTR) NULL,
1831                                        (Elf_Internal_Rela *) NULL,
1832                                        info->keep_memory);
1833       if (internal_relocs == NULL)
1834         return false;
1835
1836       ok = mmix_elf_check_common_relocs (abfd, info, o, internal_relocs);
1837
1838       if (! info->keep_memory)
1839         free (internal_relocs);
1840
1841       if (! ok)
1842         return false;
1843     }
1844
1845   return true;
1846 }
1847 \f
1848 /* Change symbols relative to the reg contents section to instead be to
1849    the register section, and scale them down to correspond to the register
1850    number.  */
1851
1852 static boolean
1853 mmix_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
1854      bfd *abfd ATTRIBUTE_UNUSED;
1855      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1856      const char *name ATTRIBUTE_UNUSED;
1857      Elf_Internal_Sym *sym;
1858      asection *input_sec;
1859 {
1860   if (input_sec != NULL
1861       && input_sec->name != NULL
1862       && ELF_ST_TYPE (sym->st_info) != STT_SECTION
1863       && strcmp (input_sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1864     {
1865       sym->st_value /= 8;
1866       sym->st_shndx = SHN_REGISTER;
1867     }
1868
1869   return true;
1870 }
1871
1872 /* We fake a register section that holds values that are register numbers.
1873    Having a SHN_REGISTER and register section translates better to other
1874    formats (e.g. mmo) than for example a STT_REGISTER attribute.
1875    This section faking is based on a construct in elf32-mips.c.  */
1876 static asection mmix_elf_reg_section;
1877 static asymbol mmix_elf_reg_section_symbol;
1878 static asymbol *mmix_elf_reg_section_symbol_ptr;
1879
1880 /* Handle the special MIPS section numbers that a symbol may use.
1881    This is used for both the 32-bit and the 64-bit ABI.  */
1882
1883 void
1884 mmix_elf_symbol_processing (abfd, asym)
1885      bfd *abfd ATTRIBUTE_UNUSED;
1886      asymbol *asym;
1887 {
1888   elf_symbol_type *elfsym;
1889
1890   elfsym = (elf_symbol_type *) asym;
1891   switch (elfsym->internal_elf_sym.st_shndx)
1892     {
1893     case SHN_REGISTER:
1894       if (mmix_elf_reg_section.name == NULL)
1895         {
1896           /* Initialize the register section.  */
1897           mmix_elf_reg_section.name = MMIX_REG_SECTION_NAME;
1898           mmix_elf_reg_section.flags = SEC_NO_FLAGS;
1899           mmix_elf_reg_section.output_section = &mmix_elf_reg_section;
1900           mmix_elf_reg_section.symbol = &mmix_elf_reg_section_symbol;
1901           mmix_elf_reg_section.symbol_ptr_ptr = &mmix_elf_reg_section_symbol_ptr;
1902           mmix_elf_reg_section_symbol.name = MMIX_REG_SECTION_NAME;
1903           mmix_elf_reg_section_symbol.flags = BSF_SECTION_SYM;
1904           mmix_elf_reg_section_symbol.section = &mmix_elf_reg_section;
1905           mmix_elf_reg_section_symbol_ptr = &mmix_elf_reg_section_symbol;
1906         }
1907       asym->section = &mmix_elf_reg_section;
1908       break;
1909
1910     default:
1911       break;
1912     }
1913 }
1914
1915 /* Given a BFD section, try to locate the corresponding ELF section
1916    index.  */
1917
1918 static boolean
1919 mmix_elf_section_from_bfd_section (abfd, sec, retval)
1920      bfd *                 abfd ATTRIBUTE_UNUSED;
1921      asection *            sec;
1922      int *                 retval;
1923 {
1924   if (strcmp (bfd_get_section_name (abfd, sec), MMIX_REG_SECTION_NAME) == 0)
1925     *retval = SHN_REGISTER;
1926   else
1927     return false;
1928
1929   return true;
1930 }
1931
1932 /* Hook called by the linker routine which adds symbols from an object
1933    file.  We must handle the special SHN_REGISTER section number here.
1934
1935    We also check that we only have *one* each of the section-start
1936    symbols, since otherwise having two with the same value would cause
1937    them to be "merged", but with the contents serialized.  */
1938
1939 boolean
1940 mmix_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1941      bfd *abfd;
1942      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1943      const Elf_Internal_Sym *sym;
1944      const char **namep ATTRIBUTE_UNUSED;
1945      flagword *flagsp ATTRIBUTE_UNUSED;
1946      asection **secp;
1947      bfd_vma *valp ATTRIBUTE_UNUSED;
1948 {
1949   if (sym->st_shndx == SHN_REGISTER)
1950     *secp = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
1951   else if ((*namep)[0] == '_' && (*namep)[1] == '_' && (*namep)[2] == '.'
1952            && strncmp (*namep, MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
1953                        strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)) == 0)
1954     {
1955       /* See if we have another one.  */
1956       struct elf_link_hash_entry *h
1957         = (struct elf_link_hash_entry *) bfd_link_hash_lookup (info->hash,
1958                                                                *namep,
1959                                                                false,
1960                                                                false, false);
1961
1962       if (h != NULL && h->root.type != bfd_link_hash_undefined)
1963         {
1964           /* How do we get the asymbol (or really: the filename) from h?
1965              h->root.u.def.section->owner is NULL.  */
1966           ((*_bfd_error_handler)
1967            (_("%s: Error: multiple definition of `%s'; start of %s is set in a earlier linked file\n"),
1968             bfd_get_filename (abfd), *namep,
1969             *namep + strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)));
1970            bfd_set_error (bfd_error_bad_value);
1971            return false;
1972         }
1973     }
1974
1975   return true;
1976 }
1977
1978 /* We consider symbols matching "L.*:[0-9]+" to be local symbols.  */
1979
1980 boolean
1981 mmix_elf_is_local_label_name (abfd, name)
1982      bfd *abfd;
1983      const char *name;
1984 {
1985   const char *colpos;
1986   int digits;
1987
1988   /* Also include the default local-label definition.  */
1989   if (_bfd_elf_is_local_label_name (abfd, name))
1990     return true;
1991
1992   if (*name != 'L')
1993     return false;
1994
1995   /* If there's no ":", or more than one, it's not a local symbol.  */
1996   colpos = strchr (name, ':');
1997   if (colpos == NULL || strchr (colpos + 1, ':') != NULL)
1998     return false;
1999
2000   /* Check that there are remaining characters and that they are digits.  */
2001   if (colpos[1] == 0)
2002     return false;
2003
2004   digits = strspn (colpos + 1, "0123456789");
2005   return digits != 0 && colpos[1 + digits] == 0;
2006 }
2007
2008 /* We get rid of the register section here.  */
2009
2010 boolean
2011 mmix_elf_final_link (abfd, info)
2012      bfd *abfd;
2013      struct bfd_link_info *info;
2014 {
2015   /* We never output a register section, though we create one for
2016      temporary measures.  Check that nobody entered contents into it.  */
2017   asection *reg_section;
2018   asection **secpp;
2019
2020   reg_section = bfd_get_section_by_name (abfd, MMIX_REG_SECTION_NAME);
2021
2022   if (reg_section != NULL)
2023     {
2024       /* FIXME: Pass error state gracefully.  */
2025       if (bfd_get_section_flags (abfd, reg_section) & SEC_HAS_CONTENTS)
2026         _bfd_abort (__FILE__, __LINE__, _("Register section has contents\n"));
2027
2028       /* Really remove the section.  */
2029       for (secpp = &abfd->sections;
2030            *secpp != reg_section;
2031            secpp = &(*secpp)->next)
2032         ;
2033       bfd_section_list_remove (abfd, secpp);
2034       --abfd->section_count;
2035     }
2036
2037   if (! bfd_elf64_bfd_final_link (abfd, info))
2038     return false;
2039
2040   /* Since this section is marked SEC_LINKER_CREATED, it isn't output by
2041      the regular linker machinery.  We do it here, like other targets with
2042      special sections.  */
2043   if (info->base_file != NULL)
2044     {
2045       asection *greg_section
2046         = bfd_get_section_by_name ((bfd *) info->base_file,
2047                                    MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2048       if (!bfd_set_section_contents (abfd,
2049                                      greg_section->output_section,
2050                                      greg_section->contents,
2051                                      (file_ptr) greg_section->output_offset,
2052                                      greg_section->_cooked_size))
2053         return false;
2054     }
2055   return true;
2056 }
2057
2058 /* Initialize stuff for the linker-generated GREGs to match
2059    R_MMIX_BASE_PLUS_OFFSET relocs seen by the linker.  */
2060
2061 boolean
2062 _bfd_mmix_prepare_linker_allocated_gregs (abfd, info)
2063      bfd *abfd ATTRIBUTE_UNUSED;
2064      struct bfd_link_info *info;
2065 {
2066   asection *bpo_gregs_section;
2067   bfd *bpo_greg_owner;
2068   struct bpo_greg_section_info *gregdata;
2069   size_t n_gregs;
2070   bfd_vma gregs_size;
2071   size_t i;
2072   size_t *bpo_reloc_indexes;
2073
2074   /* The bpo_greg_owner bfd is supposed to have been set by
2075      mmix_elf_check_relocs when the first R_MMIX_BASE_PLUS_OFFSET is seen.
2076      If there is no such object, there was no R_MMIX_BASE_PLUS_OFFSET.  */
2077   bpo_greg_owner = (bfd *) info->base_file;
2078   if (bpo_greg_owner == NULL)
2079     return true;
2080
2081   bpo_gregs_section
2082     = bfd_get_section_by_name (bpo_greg_owner,
2083                                MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2084
2085   if (bpo_gregs_section == NULL)
2086     return true;
2087
2088   /* We use the target-data handle in the ELF section data.  */
2089   gregdata = (struct bpo_greg_section_info *)
2090     elf_section_data (bpo_gregs_section)->tdata;
2091   if (gregdata == NULL)
2092     return false;
2093
2094   n_gregs = gregdata->n_bpo_relocs;
2095   gregdata->n_allocated_bpo_gregs = n_gregs;
2096
2097   /* When this reaches zero during relaxation, all entries have been
2098      filled in and the size of the linker gregs can be calculated.  */
2099   gregdata->n_remaining_bpo_relocs_this_relaxation_round = n_gregs;
2100
2101   /* Set the zeroth-order estimate for the GREGs size.  */
2102   gregs_size = n_gregs * 8;
2103
2104   if (!bfd_set_section_size (bpo_greg_owner, bpo_gregs_section, gregs_size))
2105     return false;
2106
2107   /* Allocate and set up the GREG arrays.  They're filled in at relaxation
2108      time.  Note that we must use the max number ever noted for the array,
2109      since the index numbers were created before GC.  */
2110   gregdata->reloc_request
2111     = bfd_zalloc (bpo_greg_owner,
2112                   sizeof (struct bpo_reloc_request)
2113                   * gregdata->n_max_bpo_relocs);
2114
2115   gregdata->bpo_reloc_indexes
2116     = bpo_reloc_indexes
2117     = bfd_alloc (bpo_greg_owner,
2118                  gregdata->n_max_bpo_relocs
2119                  * sizeof (size_t));
2120   if (bpo_reloc_indexes == NULL)
2121     return false;
2122
2123   /* The default order is an identity mapping.  */
2124   for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2125     {
2126       bpo_reloc_indexes[i] = i;
2127       gregdata->reloc_request[i].bpo_reloc_no = i;
2128     }
2129
2130   return true;
2131 }
2132 \f
2133 /* Fill in contents in the linker allocated gregs.  Everything is
2134    calculated at this point; we just move the contents into place here.  */
2135
2136 boolean
2137 _bfd_mmix_finalize_linker_allocated_gregs (abfd, link_info)
2138      bfd *abfd ATTRIBUTE_UNUSED;
2139      struct bfd_link_info *link_info;
2140 {
2141   asection *bpo_gregs_section;
2142   bfd *bpo_greg_owner;
2143   struct bpo_greg_section_info *gregdata;
2144   size_t n_gregs;
2145   size_t i, j;
2146   size_t lastreg;
2147   bfd_byte *contents;
2148
2149   /* The bpo_greg_owner bfd is supposed to have been set by mmix_elf_check_relocs
2150      when the first R_MMIX_BASE_PLUS_OFFSET is seen.  If there is no such
2151      object, there was no R_MMIX_BASE_PLUS_OFFSET.  */
2152   bpo_greg_owner = (bfd *) link_info->base_file;
2153   if (bpo_greg_owner == NULL)
2154     return true;
2155
2156   bpo_gregs_section
2157     = bfd_get_section_by_name (bpo_greg_owner,
2158                                MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2159
2160   /* This can't happen without DSO handling.  When DSOs are handled
2161      without any R_MMIX_BASE_PLUS_OFFSET seen, there will be no such
2162      section.  */
2163   if (bpo_gregs_section == NULL)
2164     return true;
2165
2166   /* We use the target-data handle in the ELF section data.  */
2167
2168   gregdata = (struct bpo_greg_section_info *)
2169     elf_section_data (bpo_gregs_section)->tdata;
2170   if (gregdata == NULL)
2171     return false;
2172
2173   n_gregs = gregdata->n_allocated_bpo_gregs;
2174
2175   bpo_gregs_section->contents
2176     = contents = bfd_alloc (bpo_greg_owner, bpo_gregs_section->_cooked_size);
2177   if (contents == NULL)
2178     return false;
2179
2180   /* Sanity check: If these numbers mismatch, some relocation has not been
2181      accounted for and the rest of gregdata is probably inconsistent.
2182      It's a bug, but it's more helpful to identify it than segfaulting
2183      below.  */
2184   if (gregdata->n_remaining_bpo_relocs_this_relaxation_round
2185       != gregdata->n_bpo_relocs)
2186     {
2187       (*_bfd_error_handler)
2188         (_("Internal inconsistency: remaining %u != max %u.\n\
2189   Please report this bug."),
2190          gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2191          gregdata->n_bpo_relocs);
2192       return false;
2193     }
2194
2195   for (lastreg = 255, i = 0, j = 0; j < n_gregs; i++)
2196     if (gregdata->reloc_request[i].regindex != lastreg)
2197       {
2198         bfd_put_64 (bpo_greg_owner, gregdata->reloc_request[i].value,
2199                     contents + j * 8);
2200         lastreg = gregdata->reloc_request[i].regindex;
2201         j++;
2202       }
2203
2204   return true;
2205 }
2206
2207 /* Sort valid relocs to come before non-valid relocs, then on increasing
2208    value.  */
2209
2210 static int
2211 bpo_reloc_request_sort_fn (p1, p2)
2212      const PTR p1;
2213      const PTR p2;
2214 {
2215   const struct bpo_reloc_request *r1 = (const struct bpo_reloc_request *) p1;
2216   const struct bpo_reloc_request *r2 = (const struct bpo_reloc_request *) p2;
2217
2218   /* Primary function is validity; non-valid relocs sorted after valid
2219      ones.  */
2220   if (r1->valid != r2->valid)
2221     return r2->valid - r1->valid;
2222
2223   /* Then sort on value.  Don't simplify and return just the difference of
2224      the values: the upper bits of the 64-bit value would be truncated on
2225      a host with 32-bit ints.  */
2226   if (r1->value != r2->value)
2227     return r1->value > r2->value ? 1 : -1;
2228
2229   /* As a last re-sort, use the address so we get a stable sort.  */
2230   return r1 > r2 ? 1 : (r1 < r2 ? -1 : 0);
2231 }
2232
2233 /* For debug use only.  Dumps the global register allocations resulting
2234    from base-plus-offset relocs.  */
2235
2236 void
2237 mmix_dump_bpo_gregs (link_info, pf)
2238      struct bfd_link_info *link_info;
2239      bfd_error_handler_type pf;
2240 {
2241   bfd *bpo_greg_owner;
2242   asection *bpo_gregs_section;
2243   struct bpo_greg_section_info *gregdata;
2244   unsigned int i;
2245
2246   if (link_info == NULL || link_info->base_file == NULL)
2247     return;
2248
2249   bpo_greg_owner = (bfd *) link_info->base_file;
2250
2251   bpo_gregs_section
2252     = bfd_get_section_by_name (bpo_greg_owner,
2253                                MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2254
2255   if (bpo_gregs_section == NULL)
2256     return;
2257
2258   gregdata = (struct bpo_greg_section_info *)
2259     elf_section_data (bpo_gregs_section)->tdata;
2260   if (gregdata == NULL)
2261     return;
2262
2263   if (pf == NULL)
2264     pf = _bfd_error_handler;
2265
2266   /* These format strings are not translated.  They are for debug purposes
2267      only and never displayed to an end user.  Should they escape, we
2268      surely want them in original.  */
2269   (*pf) (" n_bpo_relocs: %u\n n_max_bpo_relocs: %u\n n_remain...round: %u\n\
2270  n_allocated_bpo_gregs: %u\n", gregdata->n_bpo_relocs,
2271      gregdata->n_max_bpo_relocs,
2272      gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2273      gregdata->n_allocated_bpo_gregs);
2274
2275   if (gregdata->reloc_request)
2276     for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2277       (*pf) ("%4u (%4u)/%4u#%u: 0x%08lx%08lx  r: %3u o: %3u\n",
2278              i,
2279              gregdata->bpo_reloc_indexes != NULL
2280              ? gregdata->bpo_reloc_indexes[i] : -1,
2281              gregdata->reloc_request[i].bpo_reloc_no,
2282              gregdata->reloc_request[i].valid,
2283
2284              (unsigned long) (gregdata->reloc_request[i].value >> 32),
2285              (unsigned long) gregdata->reloc_request[i].value,
2286              gregdata->reloc_request[i].regindex,
2287              gregdata->reloc_request[i].offset);
2288 }
2289
2290 /* This links all R_MMIX_BASE_PLUS_OFFSET relocs into a special array, and
2291    when the last such reloc is done, an index-array is sorted according to
2292    the values and iterated over to produce register numbers (indexed by 0
2293    from the first allocated register number) and offsets for use in real
2294    relocation.
2295
2296    Symbol- and reloc-reading infrastructure copied from elf-m10200.c.  */
2297
2298 static boolean
2299 mmix_elf_relax_section (abfd, sec, link_info, again)
2300      bfd *abfd;
2301      asection *sec;
2302      struct bfd_link_info *link_info;
2303      boolean *again;
2304 {
2305   Elf_Internal_Shdr *symtab_hdr;
2306   Elf_Internal_Shdr *shndx_hdr;
2307   Elf_Internal_Rela *internal_relocs;
2308   Elf_Internal_Rela *free_relocs = NULL;
2309   Elf_Internal_Rela *irel, *irelend;
2310   asection *bpo_gregs_section = NULL;
2311   struct bpo_greg_section_info *gregdata;
2312   struct bpo_reloc_section_info *bpodata
2313     = (struct bpo_reloc_section_info *)
2314     elf_section_data (sec)->tdata;
2315   size_t bpono;
2316   bfd *bpo_greg_owner;
2317   Elf64_External_Sym *extsyms = NULL;
2318   Elf64_External_Sym *free_extsyms = NULL;
2319   Elf_External_Sym_Shndx *shndx_buf = NULL;
2320
2321   /* Assume nothing changes.  */
2322   *again = false;
2323
2324   /* If this is the first time we have been called for this section,
2325      initialize the cooked size.  */
2326   if (sec->_cooked_size == 0)
2327     sec->_cooked_size = sec->_raw_size;
2328
2329   /* We don't have to do anything for a relocateable link, if
2330      this section does not have relocs, or if this is not a
2331      code section.  */
2332   if (link_info->relocateable
2333       || (sec->flags & SEC_RELOC) == 0
2334       || sec->reloc_count == 0
2335       || (sec->flags & SEC_CODE) == 0
2336       || (sec->flags & SEC_LINKER_CREATED) != 0
2337       /* If no R_MMIX_BASE_PLUS_OFFSET relocs, then nothing to do.  */
2338       || bpodata == NULL)
2339     return true;
2340
2341   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2342   shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2343
2344   bpo_greg_owner = (bfd *) link_info->base_file;
2345   bpo_gregs_section = bpodata->bpo_greg_section;
2346   gregdata = (struct bpo_greg_section_info *)
2347     elf_section_data (bpo_gregs_section)->tdata;
2348
2349   bpono = bpodata->first_base_plus_offset_reloc;
2350
2351   /* Get a copy of the native relocations.  */
2352   internal_relocs
2353     = _bfd_elf64_link_read_relocs (abfd, sec, (PTR) NULL,
2354                                    (Elf_Internal_Rela *) NULL,
2355                                    link_info->keep_memory);
2356   if (internal_relocs == NULL)
2357     goto error_return;
2358   if (! link_info->keep_memory)
2359     free_relocs = internal_relocs;
2360
2361   /* Walk through them looking for relaxing opportunities.  */
2362   irelend = internal_relocs + sec->reloc_count;
2363   for (irel = internal_relocs; irel < irelend; irel++)
2364     {
2365       bfd_vma symval;
2366
2367       if (ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_BASE_PLUS_OFFSET)
2368         continue;
2369
2370       /* Read this BFD's symbols if we haven't done so already.  */
2371       if (extsyms == NULL)
2372         {
2373           /* Get cached copy if it exists.  */
2374           if (symtab_hdr->contents != NULL)
2375             extsyms = (Elf64_External_Sym *) symtab_hdr->contents;
2376           else
2377             {
2378               /* Go get them off disk.  */
2379               bfd_size_type amt;
2380
2381               amt = symtab_hdr->sh_info;
2382               amt *= sizeof (Elf64_External_Sym);
2383               extsyms = (Elf64_External_Sym *) bfd_malloc (amt);
2384               if (extsyms == NULL)
2385                 goto error_return;
2386               free_extsyms = extsyms;
2387               if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2388                   || bfd_bread ((PTR) extsyms, amt, abfd) != amt)
2389                 goto error_return;
2390               symtab_hdr->contents = (bfd_byte *) extsyms;
2391             }
2392
2393           /* If >64k sections, this presumable happens.  No test-case.  */
2394           if (shndx_hdr->sh_size != 0)
2395             {
2396               bfd_size_type amt;
2397
2398               amt = symtab_hdr->sh_info;
2399               amt *= sizeof (Elf_External_Sym_Shndx);
2400               shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
2401               if (shndx_buf == NULL)
2402                 goto error_return;
2403               if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
2404                   || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
2405                 goto error_return;
2406               shndx_hdr->contents = (bfd_byte *) shndx_buf;
2407             }
2408         }
2409
2410       /* Get the value of the symbol referred to by the reloc.  */
2411       if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2412         {
2413           /* A local symbol.  */
2414           Elf64_External_Sym *esym;
2415           Elf_External_Sym_Shndx *shndx;
2416           Elf_Internal_Sym isym;
2417           asection *sym_sec;
2418
2419           esym = extsyms + ELF64_R_SYM (irel->r_info);
2420           shndx = shndx_buf + (shndx_buf
2421                                ? ELF64_R_SYM (irel->r_info) : 0);
2422           bfd_elf64_swap_symbol_in (abfd, esym, shndx, &isym);
2423
2424           if (isym.st_shndx == SHN_UNDEF)
2425             sym_sec = bfd_und_section_ptr;
2426           else if (isym.st_shndx == SHN_ABS)
2427             sym_sec = bfd_abs_section_ptr;
2428           else if (isym.st_shndx == SHN_COMMON)
2429             sym_sec = bfd_com_section_ptr;
2430           else
2431             sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
2432           symval = (isym.st_value
2433                     + sym_sec->output_section->vma
2434                     + sym_sec->output_offset);
2435         }
2436       else
2437         {
2438           unsigned long indx;
2439           struct elf_link_hash_entry *h;
2440
2441           /* An external symbol.  */
2442           indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2443           h = elf_sym_hashes (abfd)[indx];
2444           BFD_ASSERT (h != NULL);
2445           if (h->root.type != bfd_link_hash_defined
2446               && h->root.type != bfd_link_hash_defweak)
2447             {
2448               /* This appears to be a reference to an undefined symbol.
2449                  Just ignore it--it will be caught by the regular reloc
2450                  processing.  We need to keep BPO reloc accounting
2451                  consistent, though.  */
2452               gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2453               bpono++;
2454               continue;
2455             }
2456
2457           symval = (h->root.u.def.value
2458                     + h->root.u.def.section->output_section->vma
2459                     + h->root.u.def.section->output_offset);
2460         }
2461
2462       gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono]].value
2463         = symval + irel->r_addend;
2464       gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono++]].valid = true;
2465       gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2466     }
2467
2468   /* Check if that was the last BPO-reloc.  If so, sort the values and
2469      calculate how many registers we need to cover them.  Set the size of
2470      the linker gregs, and if the number of registers changed, indicate
2471      that we need to relax some more because we have more work to do.  */
2472   if (gregdata->n_remaining_bpo_relocs_this_relaxation_round == 0)
2473     {
2474       size_t i;
2475       bfd_vma prev_base;
2476       size_t regindex;
2477
2478       /* First, reset the remaining relocs for the next round.  */
2479       gregdata->n_remaining_bpo_relocs_this_relaxation_round
2480         = gregdata->n_bpo_relocs;
2481
2482       qsort ((PTR) gregdata->reloc_request,
2483              gregdata->n_max_bpo_relocs,
2484              sizeof (struct bpo_reloc_request),
2485              bpo_reloc_request_sort_fn);
2486
2487       /* Recalculate indexes.  When we find a change (however unlikely
2488          after the initial iteration), we know we need to relax again,
2489          since items in the GREG-array are sorted by increasing value and
2490          stored in the relaxation phase.  */
2491       for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2492         if (gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2493             != i)
2494           {
2495             gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2496               = i;
2497             *again = true;
2498           }
2499
2500       /* Allocate register numbers (indexing from 0).  Stop at the first
2501          non-valid reloc.  */
2502       for (i = 0, regindex = 0, prev_base = gregdata->reloc_request[0].value;
2503            i < gregdata->n_bpo_relocs;
2504            i++)
2505         {
2506           if (gregdata->reloc_request[i].value > prev_base + 255)
2507             {
2508               regindex++;
2509               prev_base = gregdata->reloc_request[i].value;
2510             }
2511           gregdata->reloc_request[i].regindex = regindex;
2512           gregdata->reloc_request[i].offset
2513             = gregdata->reloc_request[i].value - prev_base;
2514         }
2515
2516       /* If it's not the same as the last time, we need to relax again,
2517          because the size of the section has changed.  I'm not sure we
2518          actually need to do any adjustments since the shrinking happens
2519          at the start of this section, but better safe than sorry.  */
2520       if (gregdata->n_allocated_bpo_gregs != regindex + 1)
2521         {
2522           gregdata->n_allocated_bpo_gregs = regindex + 1;
2523           *again = true;
2524         }
2525
2526       bpo_gregs_section->_cooked_size = (regindex + 1) * 8;
2527     }
2528
2529   if (free_relocs != NULL)
2530     free (free_relocs);
2531
2532   if (shndx_buf != NULL)
2533     {
2534       shndx_hdr->contents = NULL;
2535       free (shndx_buf);
2536     }
2537
2538   if (free_extsyms != NULL)
2539     {
2540       if (! link_info->keep_memory)
2541         {
2542           symtab_hdr->contents = NULL;
2543           free (free_extsyms);
2544         }
2545     }
2546
2547   return true;
2548
2549  error_return:
2550   if (free_relocs != NULL)
2551     free (free_relocs);
2552   if (shndx_buf != NULL)
2553     {
2554       shndx_hdr->contents = NULL;
2555       free (shndx_buf);
2556     }
2557   if (free_extsyms != NULL)
2558     {
2559       symtab_hdr->contents = NULL;
2560       free (free_extsyms);
2561     }
2562
2563   return false;
2564 }
2565 \f
2566 #define ELF_ARCH                bfd_arch_mmix
2567 #define ELF_MACHINE_CODE        EM_MMIX
2568
2569 /* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
2570    However, that's too much for something somewhere in the linker part of
2571    BFD; perhaps the start-address has to be a non-zero multiple of this
2572    number, or larger than this number.  The symptom is that the linker
2573    complains: "warning: allocated section `.text' not in segment".  We
2574    settle for 64k; the page-size used in examples is 8k.
2575    #define ELF_MAXPAGESIZE 0x10000
2576
2577    Unfortunately, this causes excessive padding in the supposedly small
2578    for-education programs that are the expected usage (where people would
2579    inspect output).  We stick to 256 bytes just to have *some* default
2580    alignment.  */
2581 #define ELF_MAXPAGESIZE 0x100
2582
2583 #define TARGET_BIG_SYM          bfd_elf64_mmix_vec
2584 #define TARGET_BIG_NAME         "elf64-mmix"
2585
2586 #define elf_info_to_howto_rel           NULL
2587 #define elf_info_to_howto               mmix_info_to_howto_rela
2588 #define elf_backend_relocate_section    mmix_elf_relocate_section
2589 #define elf_backend_gc_mark_hook        mmix_elf_gc_mark_hook
2590 #define elf_backend_gc_sweep_hook       mmix_elf_gc_sweep_hook
2591
2592 #define elf_backend_link_output_symbol_hook \
2593         mmix_elf_link_output_symbol_hook
2594 #define elf_backend_add_symbol_hook     mmix_elf_add_symbol_hook
2595
2596 #define elf_backend_check_relocs        mmix_elf_check_relocs
2597 #define elf_backend_symbol_processing   mmix_elf_symbol_processing
2598
2599 #define bfd_elf64_bfd_is_local_label_name \
2600         mmix_elf_is_local_label_name
2601
2602 #define elf_backend_may_use_rel_p       0
2603 #define elf_backend_may_use_rela_p      1
2604 #define elf_backend_default_use_rela_p  1
2605
2606 #define elf_backend_can_gc_sections     1
2607 #define elf_backend_section_from_bfd_section \
2608         mmix_elf_section_from_bfd_section
2609
2610 #define bfd_elf64_bfd_final_link        mmix_elf_final_link
2611 #define bfd_elf64_bfd_relax_section     mmix_elf_relax_section
2612
2613 #include "elf64-target.h"