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