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