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