Back to index

cell-binutils  2.17cvs20070401
elf64-mmix.c
Go to the documentation of this file.
00001 /* MMIX-specific support for 64-bit ELF.
00002    Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007
00003    Free Software Foundation, Inc.
00004    Contributed by Hans-Peter Nilsson <hp@bitrange.com>
00005 
00006 This file is part of BFD, the Binary File Descriptor library.
00007 
00008 This program is free software; you can redistribute it and/or modify
00009 it under the terms of the GNU General Public License as published by
00010 the Free Software Foundation; either version 2 of the License, or
00011 (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 /* No specific ABI or "processor-specific supplement" defined.  */
00023 
00024 /* TODO:
00025    - "Traditional" linker relaxation (shrinking whole sections).
00026    - Merge reloc stubs jumping to same location.
00027    - GETA stub relaxation (call a stub for out of range new
00028      R_MMIX_GETA_STUBBABLE).  */
00029 
00030 #include "bfd.h"
00031 #include "sysdep.h"
00032 #include "libbfd.h"
00033 #include "elf-bfd.h"
00034 #include "elf/mmix.h"
00035 #include "opcode/mmix.h"
00036 
00037 #define MINUS_ONE    (((bfd_vma) 0) - 1)
00038 
00039 #define MAX_PUSHJ_STUB_SIZE (5 * 4)
00040 
00041 /* Put these everywhere in new code.  */
00042 #define FATAL_DEBUG                                     \
00043  _bfd_abort (__FILE__, __LINE__,                        \
00044             "Internal: Non-debugged code (test-case missing)")
00045 
00046 #define BAD_CASE(x)                       \
00047  _bfd_abort (__FILE__, __LINE__,          \
00048             "bad case for " #x)
00049 
00050 struct _mmix_elf_section_data
00051 {
00052   struct bfd_elf_section_data elf;
00053   union
00054   {
00055     struct bpo_reloc_section_info *reloc;
00056     struct bpo_greg_section_info *greg;
00057   } bpo;
00058 
00059   struct pushj_stub_info
00060   {
00061     /* Maximum number of stubs needed for this section.  */
00062     bfd_size_type n_pushj_relocs;
00063 
00064     /* Size of stubs after a mmix_elf_relax_section round.  */
00065     bfd_size_type stubs_size_sum;
00066 
00067     /* Per-reloc stubs_size_sum information.  The stubs_size_sum member is the sum
00068        of these.  Allocated in mmix_elf_check_common_relocs.  */
00069     bfd_size_type *stub_size;
00070 
00071     /* Offset of next stub during relocation.  Somewhat redundant with the
00072        above: error coverage is easier and we don't have to reset the
00073        stubs_size_sum for relocation.  */
00074     bfd_size_type stub_offset;
00075   } pjs;
00076 };
00077 
00078 #define mmix_elf_section_data(sec) \
00079   ((struct _mmix_elf_section_data *) elf_section_data (sec))
00080 
00081 /* For each section containing a base-plus-offset (BPO) reloc, we attach
00082    this struct as mmix_elf_section_data (section)->bpo, which is otherwise
00083    NULL.  */
00084 struct bpo_reloc_section_info
00085   {
00086     /* The base is 1; this is the first number in this section.  */
00087     size_t first_base_plus_offset_reloc;
00088 
00089     /* Number of BPO-relocs in this section.  */
00090     size_t n_bpo_relocs_this_section;
00091 
00092     /* Running index, used at relocation time.  */
00093     size_t bpo_index;
00094 
00095     /* We don't have access to the bfd_link_info struct in
00096        mmix_final_link_relocate.  What we really want to get at is the
00097        global single struct greg_relocation, so we stash it here.  */
00098     asection *bpo_greg_section;
00099   };
00100 
00101 /* Helper struct (in global context) for the one below.
00102    There's one of these created for every BPO reloc.  */
00103 struct bpo_reloc_request
00104   {
00105     bfd_vma value;
00106 
00107     /* Valid after relaxation.  The base is 0; the first register number
00108        must be added.  The offset is in range 0..255.  */
00109     size_t regindex;
00110     size_t offset;
00111 
00112     /* The order number for this BPO reloc, corresponding to the order in
00113        which BPO relocs were found.  Used to create an index after reloc
00114        requests are sorted.  */
00115     size_t bpo_reloc_no;
00116 
00117     /* Set when the value is computed.  Better than coding "guard values"
00118        into the other members.  Is FALSE only for BPO relocs in a GC:ed
00119        section.  */
00120     bfd_boolean valid;
00121   };
00122 
00123 /* We attach this as mmix_elf_section_data (sec)->bpo in the linker-allocated
00124    greg contents section (MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME),
00125    which is linked into the register contents section
00126    (MMIX_REG_CONTENTS_SECTION_NAME).  This section is created by the
00127    linker; using the same hook as for usual with BPO relocs does not
00128    collide.  */
00129 struct bpo_greg_section_info
00130   {
00131     /* After GC, this reflects the number of remaining, non-excluded
00132        BPO-relocs.  */
00133     size_t n_bpo_relocs;
00134 
00135     /* This is the number of allocated bpo_reloc_requests; the size of
00136        sorted_indexes.  Valid after the check.*relocs functions are called
00137        for all incoming sections.  It includes the number of BPO relocs in
00138        sections that were GC:ed.  */
00139     size_t n_max_bpo_relocs;
00140 
00141     /* A counter used to find out when to fold the BPO gregs, since we
00142        don't have a single "after-relaxation" hook.  */
00143     size_t n_remaining_bpo_relocs_this_relaxation_round;
00144 
00145     /* The number of linker-allocated GREGs resulting from BPO relocs.
00146        This is an approximation after _bfd_mmix_before_linker_allocation
00147        and supposedly accurate after mmix_elf_relax_section is called for
00148        all incoming non-collected sections.  */
00149     size_t n_allocated_bpo_gregs;
00150 
00151     /* Index into reloc_request[], sorted on increasing "value", secondary
00152        by increasing index for strict sorting order.  */
00153     size_t *bpo_reloc_indexes;
00154 
00155     /* An array of all relocations, with the "value" member filled in by
00156        the relaxation function.  */
00157     struct bpo_reloc_request *reloc_request;
00158   };
00159 
00160 static bfd_boolean mmix_elf_link_output_symbol_hook
00161   PARAMS ((struct bfd_link_info *, const char *, Elf_Internal_Sym *,
00162           asection *, struct elf_link_hash_entry *));
00163 
00164 static bfd_reloc_status_type mmix_elf_reloc
00165   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
00166 
00167 static reloc_howto_type *bfd_elf64_bfd_reloc_type_lookup
00168   PARAMS ((bfd *, bfd_reloc_code_real_type));
00169 
00170 static void mmix_info_to_howto_rela
00171   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
00172 
00173 static int mmix_elf_sort_relocs PARAMS ((const PTR, const PTR));
00174 
00175 static bfd_boolean mmix_elf_new_section_hook
00176   PARAMS ((bfd *, asection *));
00177 
00178 static bfd_boolean mmix_elf_check_relocs
00179   PARAMS ((bfd *, struct bfd_link_info *, asection *,
00180           const Elf_Internal_Rela *));
00181 
00182 static bfd_boolean mmix_elf_check_common_relocs
00183   PARAMS ((bfd *, struct bfd_link_info *, asection *,
00184           const Elf_Internal_Rela *));
00185 
00186 static bfd_boolean mmix_elf_relocate_section
00187   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
00188           Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
00189 
00190 static bfd_reloc_status_type mmix_final_link_relocate
00191   PARAMS ((reloc_howto_type *, asection *, bfd_byte *,
00192           bfd_vma, bfd_signed_vma, bfd_vma, const char *, asection *));
00193 
00194 static bfd_reloc_status_type mmix_elf_perform_relocation
00195   PARAMS ((asection *, reloc_howto_type *, PTR, bfd_vma, bfd_vma));
00196 
00197 static bfd_boolean mmix_elf_section_from_bfd_section
00198   PARAMS ((bfd *, asection *, int *));
00199 
00200 static bfd_boolean mmix_elf_add_symbol_hook
00201   PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
00202           const char **, flagword *, asection **, bfd_vma *));
00203 
00204 static bfd_boolean mmix_elf_is_local_label_name
00205   PARAMS ((bfd *, const char *));
00206 
00207 static int bpo_reloc_request_sort_fn PARAMS ((const PTR, const PTR));
00208 
00209 static bfd_boolean mmix_elf_relax_section
00210   PARAMS ((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
00211           bfd_boolean *again));
00212 
00213 extern bfd_boolean mmix_elf_final_link PARAMS ((bfd *, struct bfd_link_info *));
00214 
00215 extern void mmix_elf_symbol_processing PARAMS ((bfd *, asymbol *));
00216 
00217 /* Only intended to be called from a debugger.  */
00218 extern void mmix_dump_bpo_gregs
00219   PARAMS ((struct bfd_link_info *, bfd_error_handler_type));
00220 
00221 static void
00222 mmix_set_relaxable_size
00223   PARAMS ((bfd *, asection *, void *));
00224 
00225 
00226 /* Watch out: this currently needs to have elements with the same index as
00227    their R_MMIX_ number.  */
00228 static reloc_howto_type elf_mmix_howto_table[] =
00229  {
00230   /* This reloc does nothing.  */
00231   HOWTO (R_MMIX_NONE,              /* type */
00232         0,                  /* rightshift */
00233         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00234         32,                 /* bitsize */
00235         FALSE,                     /* pc_relative */
00236         0,                  /* bitpos */
00237         complain_overflow_bitfield, /* complain_on_overflow */
00238         bfd_elf_generic_reloc,     /* special_function */
00239         "R_MMIX_NONE",             /* name */
00240         FALSE,                     /* partial_inplace */
00241         0,                  /* src_mask */
00242         0,                  /* dst_mask */
00243         FALSE),             /* pcrel_offset */
00244 
00245   /* An 8 bit absolute relocation.  */
00246   HOWTO (R_MMIX_8,          /* type */
00247         0,                  /* rightshift */
00248         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00249         8,                  /* bitsize */
00250         FALSE,                     /* pc_relative */
00251         0,                  /* bitpos */
00252         complain_overflow_bitfield, /* complain_on_overflow */
00253         bfd_elf_generic_reloc,     /* special_function */
00254         "R_MMIX_8",         /* name */
00255         FALSE,                     /* partial_inplace */
00256         0,                  /* src_mask */
00257         0xff,               /* dst_mask */
00258         FALSE),             /* pcrel_offset */
00259 
00260   /* An 16 bit absolute relocation.  */
00261   HOWTO (R_MMIX_16,         /* type */
00262         0,                  /* rightshift */
00263         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00264         16,                 /* bitsize */
00265         FALSE,                     /* pc_relative */
00266         0,                  /* bitpos */
00267         complain_overflow_bitfield, /* complain_on_overflow */
00268         bfd_elf_generic_reloc,     /* special_function */
00269         "R_MMIX_16",        /* name */
00270         FALSE,                     /* partial_inplace */
00271         0,                  /* src_mask */
00272         0xffff,             /* dst_mask */
00273         FALSE),             /* pcrel_offset */
00274 
00275   /* An 24 bit absolute relocation.  */
00276   HOWTO (R_MMIX_24,         /* type */
00277         0,                  /* rightshift */
00278         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00279         24,                 /* bitsize */
00280         FALSE,                     /* pc_relative */
00281         0,                  /* bitpos */
00282         complain_overflow_bitfield, /* complain_on_overflow */
00283         bfd_elf_generic_reloc,     /* special_function */
00284         "R_MMIX_24",        /* name */
00285         FALSE,                     /* partial_inplace */
00286         ~0xffffff,          /* src_mask */
00287         0xffffff,           /* dst_mask */
00288         FALSE),             /* pcrel_offset */
00289 
00290   /* A 32 bit absolute relocation.  */
00291   HOWTO (R_MMIX_32,         /* type */
00292         0,                  /* rightshift */
00293         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00294         32,                 /* bitsize */
00295         FALSE,                     /* pc_relative */
00296         0,                  /* bitpos */
00297         complain_overflow_bitfield, /* complain_on_overflow */
00298         bfd_elf_generic_reloc,     /* special_function */
00299         "R_MMIX_32",        /* name */
00300         FALSE,                     /* partial_inplace */
00301         0,                  /* src_mask */
00302         0xffffffff,         /* dst_mask */
00303         FALSE),             /* pcrel_offset */
00304 
00305   /* 64 bit relocation.  */
00306   HOWTO (R_MMIX_64,         /* type */
00307         0,                  /* rightshift */
00308         4,                  /* size (0 = byte, 1 = short, 2 = long) */
00309         64,                 /* bitsize */
00310         FALSE,                     /* pc_relative */
00311         0,                  /* bitpos */
00312         complain_overflow_bitfield, /* complain_on_overflow */
00313         bfd_elf_generic_reloc,     /* special_function */
00314         "R_MMIX_64",        /* name */
00315         FALSE,                     /* partial_inplace */
00316         0,                  /* src_mask */
00317         MINUS_ONE,          /* dst_mask */
00318         FALSE),             /* pcrel_offset */
00319 
00320   /* An 8 bit PC-relative relocation.  */
00321   HOWTO (R_MMIX_PC_8,              /* type */
00322         0,                  /* rightshift */
00323         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00324         8,                  /* bitsize */
00325         TRUE,               /* pc_relative */
00326         0,                  /* bitpos */
00327         complain_overflow_bitfield, /* complain_on_overflow */
00328         bfd_elf_generic_reloc,     /* special_function */
00329         "R_MMIX_PC_8",             /* name */
00330         FALSE,                     /* partial_inplace */
00331         0,                  /* src_mask */
00332         0xff,               /* dst_mask */
00333         TRUE),                     /* pcrel_offset */
00334 
00335   /* An 16 bit PC-relative relocation.  */
00336   HOWTO (R_MMIX_PC_16,             /* type */
00337         0,                  /* rightshift */
00338         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00339         16,                 /* bitsize */
00340         TRUE,               /* pc_relative */
00341         0,                  /* bitpos */
00342         complain_overflow_bitfield, /* complain_on_overflow */
00343         bfd_elf_generic_reloc,     /* special_function */
00344         "R_MMIX_PC_16",     /* name */
00345         FALSE,                     /* partial_inplace */
00346         0,                  /* src_mask */
00347         0xffff,             /* dst_mask */
00348         TRUE),                     /* pcrel_offset */
00349 
00350   /* An 24 bit PC-relative relocation.  */
00351   HOWTO (R_MMIX_PC_24,             /* type */
00352         0,                  /* rightshift */
00353         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00354         24,                 /* bitsize */
00355         TRUE,               /* pc_relative */
00356         0,                  /* bitpos */
00357         complain_overflow_bitfield, /* complain_on_overflow */
00358         bfd_elf_generic_reloc,     /* special_function */
00359         "R_MMIX_PC_24",     /* name */
00360         FALSE,                     /* partial_inplace */
00361         ~0xffffff,          /* src_mask */
00362         0xffffff,           /* dst_mask */
00363         TRUE),                     /* pcrel_offset */
00364 
00365   /* A 32 bit absolute PC-relative relocation.  */
00366   HOWTO (R_MMIX_PC_32,             /* type */
00367         0,                  /* rightshift */
00368         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00369         32,                 /* bitsize */
00370         TRUE,               /* pc_relative */
00371         0,                  /* bitpos */
00372         complain_overflow_bitfield, /* complain_on_overflow */
00373         bfd_elf_generic_reloc,     /* special_function */
00374         "R_MMIX_PC_32",     /* name */
00375         FALSE,                     /* partial_inplace */
00376         0,                  /* src_mask */
00377         0xffffffff,         /* dst_mask */
00378         TRUE),                     /* pcrel_offset */
00379 
00380   /* 64 bit PC-relative relocation.  */
00381   HOWTO (R_MMIX_PC_64,             /* type */
00382         0,                  /* rightshift */
00383         4,                  /* size (0 = byte, 1 = short, 2 = long) */
00384         64,                 /* bitsize */
00385         TRUE,               /* pc_relative */
00386         0,                  /* bitpos */
00387         complain_overflow_bitfield, /* complain_on_overflow */
00388         bfd_elf_generic_reloc,     /* special_function */
00389         "R_MMIX_PC_64",     /* name */
00390         FALSE,                     /* partial_inplace */
00391         0,                  /* src_mask */
00392         MINUS_ONE,          /* dst_mask */
00393         TRUE),                     /* pcrel_offset */
00394 
00395   /* GNU extension to record C++ vtable hierarchy.  */
00396   HOWTO (R_MMIX_GNU_VTINHERIT, /* type */
00397         0,                  /* rightshift */
00398         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00399         0,                  /* bitsize */
00400         FALSE,                     /* pc_relative */
00401         0,                  /* bitpos */
00402         complain_overflow_dont, /* complain_on_overflow */
00403         NULL,               /* special_function */
00404         "R_MMIX_GNU_VTINHERIT", /* name */
00405         FALSE,                     /* partial_inplace */
00406         0,                  /* src_mask */
00407         0,                  /* dst_mask */
00408         TRUE),                     /* pcrel_offset */
00409 
00410   /* GNU extension to record C++ vtable member usage.  */
00411   HOWTO (R_MMIX_GNU_VTENTRY,       /* type */
00412         0,                  /* rightshift */
00413         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00414         0,                  /* bitsize */
00415         FALSE,                     /* pc_relative */
00416         0,                  /* bitpos */
00417         complain_overflow_dont, /* complain_on_overflow */
00418         _bfd_elf_rel_vtable_reloc_fn,     /* special_function */
00419         "R_MMIX_GNU_VTENTRY", /* name */
00420         FALSE,                     /* partial_inplace */
00421         0,                  /* src_mask */
00422         0,                  /* dst_mask */
00423         FALSE),             /* pcrel_offset */
00424 
00425   /* The GETA relocation is supposed to get any address that could
00426      possibly be reached by the GETA instruction.  It can silently expand
00427      to get a 64-bit operand, but will complain if any of the two least
00428      significant bits are set.  The howto members reflect a simple GETA.  */
00429   HOWTO (R_MMIX_GETA,              /* type */
00430         2,                  /* rightshift */
00431         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00432         19,                 /* bitsize */
00433         TRUE,               /* pc_relative */
00434         0,                  /* bitpos */
00435         complain_overflow_signed, /* complain_on_overflow */
00436         mmix_elf_reloc,     /* special_function */
00437         "R_MMIX_GETA",             /* name */
00438         FALSE,                     /* partial_inplace */
00439         ~0x0100ffff,        /* src_mask */
00440         0x0100ffff,         /* dst_mask */
00441         TRUE),                     /* pcrel_offset */
00442 
00443   HOWTO (R_MMIX_GETA_1,            /* type */
00444         2,                  /* rightshift */
00445         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00446         19,                 /* bitsize */
00447         TRUE,               /* pc_relative */
00448         0,                  /* bitpos */
00449         complain_overflow_signed, /* complain_on_overflow */
00450         mmix_elf_reloc,     /* special_function */
00451         "R_MMIX_GETA_1",           /* name */
00452         FALSE,                     /* partial_inplace */
00453         ~0x0100ffff,        /* src_mask */
00454         0x0100ffff,         /* dst_mask */
00455         TRUE),                     /* pcrel_offset */
00456 
00457   HOWTO (R_MMIX_GETA_2,            /* type */
00458         2,                  /* rightshift */
00459         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00460         19,                 /* bitsize */
00461         TRUE,               /* pc_relative */
00462         0,                  /* bitpos */
00463         complain_overflow_signed, /* complain_on_overflow */
00464         mmix_elf_reloc,     /* special_function */
00465         "R_MMIX_GETA_2",           /* name */
00466         FALSE,                     /* partial_inplace */
00467         ~0x0100ffff,        /* src_mask */
00468         0x0100ffff,         /* dst_mask */
00469         TRUE),                     /* pcrel_offset */
00470 
00471   HOWTO (R_MMIX_GETA_3,            /* type */
00472         2,                  /* rightshift */
00473         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00474         19,                 /* bitsize */
00475         TRUE,               /* pc_relative */
00476         0,                  /* bitpos */
00477         complain_overflow_signed, /* complain_on_overflow */
00478         mmix_elf_reloc,     /* special_function */
00479         "R_MMIX_GETA_3",           /* name */
00480         FALSE,                     /* partial_inplace */
00481         ~0x0100ffff,        /* src_mask */
00482         0x0100ffff,         /* dst_mask */
00483         TRUE),                     /* pcrel_offset */
00484 
00485   /* The conditional branches are supposed to reach any (code) address.
00486      It can silently expand to a 64-bit operand, but will emit an error if
00487      any of the two least significant bits are set.  The howto members
00488      reflect a simple branch.  */
00489   HOWTO (R_MMIX_CBRANCH,    /* type */
00490         2,                  /* rightshift */
00491         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00492         19,                 /* bitsize */
00493         TRUE,               /* pc_relative */
00494         0,                  /* bitpos */
00495         complain_overflow_signed, /* complain_on_overflow */
00496         mmix_elf_reloc,     /* special_function */
00497         "R_MMIX_CBRANCH",   /* name */
00498         FALSE,                     /* partial_inplace */
00499         ~0x0100ffff,        /* src_mask */
00500         0x0100ffff,         /* dst_mask */
00501         TRUE),                            /* pcrel_offset */
00502 
00503   HOWTO (R_MMIX_CBRANCH_J,  /* type */
00504         2,                  /* rightshift */
00505         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00506         19,                 /* bitsize */
00507         TRUE,               /* pc_relative */
00508         0,                  /* bitpos */
00509         complain_overflow_signed, /* complain_on_overflow */
00510         mmix_elf_reloc,     /* special_function */
00511         "R_MMIX_CBRANCH_J", /* name */
00512         FALSE,                     /* partial_inplace */
00513         ~0x0100ffff,        /* src_mask */
00514         0x0100ffff,         /* dst_mask */
00515         TRUE),                     /* pcrel_offset */
00516 
00517   HOWTO (R_MMIX_CBRANCH_1,  /* type */
00518         2,                  /* rightshift */
00519         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00520         19,                 /* bitsize */
00521         TRUE,               /* pc_relative */
00522         0,                  /* bitpos */
00523         complain_overflow_signed, /* complain_on_overflow */
00524         mmix_elf_reloc,     /* special_function */
00525         "R_MMIX_CBRANCH_1", /* name */
00526         FALSE,                     /* partial_inplace */
00527         ~0x0100ffff,        /* src_mask */
00528         0x0100ffff,         /* dst_mask */
00529         TRUE),                     /* pcrel_offset */
00530 
00531   HOWTO (R_MMIX_CBRANCH_2,  /* type */
00532         2,                  /* rightshift */
00533         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00534         19,                 /* bitsize */
00535         TRUE,               /* pc_relative */
00536         0,                  /* bitpos */
00537         complain_overflow_signed, /* complain_on_overflow */
00538         mmix_elf_reloc,     /* special_function */
00539         "R_MMIX_CBRANCH_2", /* name */
00540         FALSE,                     /* partial_inplace */
00541         ~0x0100ffff,        /* src_mask */
00542         0x0100ffff,         /* dst_mask */
00543         TRUE),                     /* pcrel_offset */
00544 
00545   HOWTO (R_MMIX_CBRANCH_3,  /* type */
00546         2,                  /* rightshift */
00547         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00548         19,                 /* bitsize */
00549         TRUE,               /* pc_relative */
00550         0,                  /* bitpos */
00551         complain_overflow_signed, /* complain_on_overflow */
00552         mmix_elf_reloc,     /* special_function */
00553         "R_MMIX_CBRANCH_3", /* name */
00554         FALSE,                     /* partial_inplace */
00555         ~0x0100ffff,        /* src_mask */
00556         0x0100ffff,         /* dst_mask */
00557         TRUE),                     /* pcrel_offset */
00558 
00559   /* The PUSHJ instruction can reach any (code) address, as long as it's
00560      the beginning of a function (no usable restriction).  It can silently
00561      expand to a 64-bit operand, but will emit an error if any of the two
00562      least significant bits are set.  It can also expand into a call to a
00563      stub; see R_MMIX_PUSHJ_STUBBABLE.  The howto members reflect a simple
00564      PUSHJ.  */
00565   HOWTO (R_MMIX_PUSHJ,             /* type */
00566         2,                  /* rightshift */
00567         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00568         19,                 /* bitsize */
00569         TRUE,               /* pc_relative */
00570         0,                  /* bitpos */
00571         complain_overflow_signed, /* complain_on_overflow */
00572         mmix_elf_reloc,     /* special_function */
00573         "R_MMIX_PUSHJ",     /* name */
00574         FALSE,                     /* partial_inplace */
00575         ~0x0100ffff,        /* src_mask */
00576         0x0100ffff,         /* dst_mask */
00577         TRUE),                     /* pcrel_offset */
00578 
00579   HOWTO (R_MMIX_PUSHJ_1,    /* type */
00580         2,                  /* rightshift */
00581         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00582         19,                 /* bitsize */
00583         TRUE,               /* pc_relative */
00584         0,                  /* bitpos */
00585         complain_overflow_signed, /* complain_on_overflow */
00586         mmix_elf_reloc,     /* special_function */
00587         "R_MMIX_PUSHJ_1",   /* name */
00588         FALSE,                     /* partial_inplace */
00589         ~0x0100ffff,        /* src_mask */
00590         0x0100ffff,         /* dst_mask */
00591         TRUE),                     /* pcrel_offset */
00592 
00593   HOWTO (R_MMIX_PUSHJ_2,    /* type */
00594         2,                  /* rightshift */
00595         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00596         19,                 /* bitsize */
00597         TRUE,               /* pc_relative */
00598         0,                  /* bitpos */
00599         complain_overflow_signed, /* complain_on_overflow */
00600         mmix_elf_reloc,     /* special_function */
00601         "R_MMIX_PUSHJ_2",   /* name */
00602         FALSE,                     /* partial_inplace */
00603         ~0x0100ffff,        /* src_mask */
00604         0x0100ffff,         /* dst_mask */
00605         TRUE),                     /* pcrel_offset */
00606 
00607   HOWTO (R_MMIX_PUSHJ_3,    /* type */
00608         2,                  /* rightshift */
00609         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00610         19,                 /* bitsize */
00611         TRUE,               /* pc_relative */
00612         0,                  /* bitpos */
00613         complain_overflow_signed, /* complain_on_overflow */
00614         mmix_elf_reloc,     /* special_function */
00615         "R_MMIX_PUSHJ_3",   /* name */
00616         FALSE,                     /* partial_inplace */
00617         ~0x0100ffff,        /* src_mask */
00618         0x0100ffff,         /* dst_mask */
00619         TRUE),                     /* pcrel_offset */
00620 
00621   /* A JMP is supposed to reach any (code) address.  By itself, it can
00622      reach +-64M; the expansion can reach all 64 bits.  Note that the 64M
00623      limit is soon reached if you link the program in wildly different
00624      memory segments.  The howto members reflect a trivial JMP.  */
00625   HOWTO (R_MMIX_JMP,        /* type */
00626         2,                  /* rightshift */
00627         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00628         27,                 /* bitsize */
00629         TRUE,               /* pc_relative */
00630         0,                  /* bitpos */
00631         complain_overflow_signed, /* complain_on_overflow */
00632         mmix_elf_reloc,     /* special_function */
00633         "R_MMIX_JMP",              /* name */
00634         FALSE,                     /* partial_inplace */
00635         ~0x1ffffff,         /* src_mask */
00636         0x1ffffff,          /* dst_mask */
00637         TRUE),                     /* pcrel_offset */
00638 
00639   HOWTO (R_MMIX_JMP_1,             /* type */
00640         2,                  /* rightshift */
00641         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00642         27,                 /* bitsize */
00643         TRUE,               /* pc_relative */
00644         0,                  /* bitpos */
00645         complain_overflow_signed, /* complain_on_overflow */
00646         mmix_elf_reloc,     /* special_function */
00647         "R_MMIX_JMP_1",     /* name */
00648         FALSE,                     /* partial_inplace */
00649         ~0x1ffffff,         /* src_mask */
00650         0x1ffffff,          /* dst_mask */
00651         TRUE),                     /* pcrel_offset */
00652 
00653   HOWTO (R_MMIX_JMP_2,             /* type */
00654         2,                  /* rightshift */
00655         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00656         27,                 /* bitsize */
00657         TRUE,               /* pc_relative */
00658         0,                  /* bitpos */
00659         complain_overflow_signed, /* complain_on_overflow */
00660         mmix_elf_reloc,     /* special_function */
00661         "R_MMIX_JMP_2",     /* name */
00662         FALSE,                     /* partial_inplace */
00663         ~0x1ffffff,         /* src_mask */
00664         0x1ffffff,          /* dst_mask */
00665         TRUE),                     /* pcrel_offset */
00666 
00667   HOWTO (R_MMIX_JMP_3,             /* type */
00668         2,                  /* rightshift */
00669         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00670         27,                 /* bitsize */
00671         TRUE,               /* pc_relative */
00672         0,                  /* bitpos */
00673         complain_overflow_signed, /* complain_on_overflow */
00674         mmix_elf_reloc,     /* special_function */
00675         "R_MMIX_JMP_3",     /* name */
00676         FALSE,                     /* partial_inplace */
00677         ~0x1ffffff,         /* src_mask */
00678         0x1ffffff,          /* dst_mask */
00679         TRUE),                     /* pcrel_offset */
00680 
00681   /* When we don't emit link-time-relaxable code from the assembler, or
00682      when relaxation has done all it can do, these relocs are used.  For
00683      GETA/PUSHJ/branches.  */
00684   HOWTO (R_MMIX_ADDR19,            /* type */
00685         2,                  /* rightshift */
00686         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00687         19,                 /* bitsize */
00688         TRUE,               /* pc_relative */
00689         0,                  /* bitpos */
00690         complain_overflow_signed, /* complain_on_overflow */
00691         mmix_elf_reloc,     /* special_function */
00692         "R_MMIX_ADDR19",    /* name */
00693         FALSE,                     /* partial_inplace */
00694         ~0x0100ffff,        /* src_mask */
00695         0x0100ffff,         /* dst_mask */
00696         TRUE),                     /* pcrel_offset */
00697 
00698   /* For JMP.  */
00699   HOWTO (R_MMIX_ADDR27,            /* type */
00700         2,                  /* rightshift */
00701         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00702         27,                 /* bitsize */
00703         TRUE,               /* pc_relative */
00704         0,                  /* bitpos */
00705         complain_overflow_signed, /* complain_on_overflow */
00706         mmix_elf_reloc,     /* special_function */
00707         "R_MMIX_ADDR27",    /* name */
00708         FALSE,                     /* partial_inplace */
00709         ~0x1ffffff,         /* src_mask */
00710         0x1ffffff,          /* dst_mask */
00711         TRUE),                     /* pcrel_offset */
00712 
00713   /* A general register or the value 0..255.  If a value, then the
00714      instruction (offset -3) needs adjusting.  */
00715   HOWTO (R_MMIX_REG_OR_BYTE,       /* type */
00716         0,                  /* rightshift */
00717         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00718         8,                  /* bitsize */
00719         FALSE,                     /* pc_relative */
00720         0,                  /* bitpos */
00721         complain_overflow_bitfield, /* complain_on_overflow */
00722         mmix_elf_reloc,     /* special_function */
00723         "R_MMIX_REG_OR_BYTE",      /* name */
00724         FALSE,                     /* partial_inplace */
00725         0,                  /* src_mask */
00726         0xff,               /* dst_mask */
00727         FALSE),             /* pcrel_offset */
00728 
00729   /* A general register.  */
00730   HOWTO (R_MMIX_REG,        /* type */
00731         0,                  /* rightshift */
00732         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00733         8,                  /* bitsize */
00734         FALSE,                     /* pc_relative */
00735         0,                  /* bitpos */
00736         complain_overflow_bitfield, /* complain_on_overflow */
00737         mmix_elf_reloc,     /* special_function */
00738         "R_MMIX_REG",              /* name */
00739         FALSE,                     /* partial_inplace */
00740         0,                  /* src_mask */
00741         0xff,               /* dst_mask */
00742         FALSE),             /* pcrel_offset */
00743 
00744   /* A register plus an index, corresponding to the relocation expression.
00745      The sizes must correspond to the valid range of the expression, while
00746      the bitmasks correspond to what we store in the image.  */
00747   HOWTO (R_MMIX_BASE_PLUS_OFFSET,  /* type */
00748         0,                  /* rightshift */
00749         4,                  /* size (0 = byte, 1 = short, 2 = long) */
00750         64,                 /* bitsize */
00751         FALSE,                     /* pc_relative */
00752         0,                  /* bitpos */
00753         complain_overflow_bitfield, /* complain_on_overflow */
00754         mmix_elf_reloc,     /* special_function */
00755         "R_MMIX_BASE_PLUS_OFFSET", /* name */
00756         FALSE,                     /* partial_inplace */
00757         0,                  /* src_mask */
00758         0xffff,             /* dst_mask */
00759         FALSE),             /* pcrel_offset */
00760 
00761   /* A "magic" relocation for a LOCAL expression, asserting that the
00762      expression is less than the number of global registers.  No actual
00763      modification of the contents is done.  Implementing this as a
00764      relocation was less intrusive than e.g. putting such expressions in a
00765      section to discard *after* relocation.  */
00766   HOWTO (R_MMIX_LOCAL,             /* type */
00767         0,                  /* rightshift */
00768         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00769         0,                  /* bitsize */
00770         FALSE,                     /* pc_relative */
00771         0,                  /* bitpos */
00772         complain_overflow_dont, /* complain_on_overflow */
00773         mmix_elf_reloc,     /* special_function */
00774         "R_MMIX_LOCAL",     /* name */
00775         FALSE,                     /* partial_inplace */
00776         0,                  /* src_mask */
00777         0,                  /* dst_mask */
00778         FALSE),             /* pcrel_offset */
00779 
00780   HOWTO (R_MMIX_PUSHJ_STUBBABLE, /* type */
00781         2,                  /* rightshift */
00782         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00783         19,                 /* bitsize */
00784         TRUE,               /* pc_relative */
00785         0,                  /* bitpos */
00786         complain_overflow_signed, /* complain_on_overflow */
00787         mmix_elf_reloc,     /* special_function */
00788         "R_MMIX_PUSHJ_STUBBABLE", /* name */
00789         FALSE,                     /* partial_inplace */
00790         ~0x0100ffff,        /* src_mask */
00791         0x0100ffff,         /* dst_mask */
00792         TRUE)               /* pcrel_offset */
00793  };
00794 
00795 
00796 /* Map BFD reloc types to MMIX ELF reloc types.  */
00797 
00798 struct mmix_reloc_map
00799   {
00800     bfd_reloc_code_real_type bfd_reloc_val;
00801     enum elf_mmix_reloc_type elf_reloc_val;
00802   };
00803 
00804 
00805 static const struct mmix_reloc_map mmix_reloc_map[] =
00806   {
00807     {BFD_RELOC_NONE, R_MMIX_NONE},
00808     {BFD_RELOC_8, R_MMIX_8},
00809     {BFD_RELOC_16, R_MMIX_16},
00810     {BFD_RELOC_24, R_MMIX_24},
00811     {BFD_RELOC_32, R_MMIX_32},
00812     {BFD_RELOC_64, R_MMIX_64},
00813     {BFD_RELOC_8_PCREL, R_MMIX_PC_8},
00814     {BFD_RELOC_16_PCREL, R_MMIX_PC_16},
00815     {BFD_RELOC_24_PCREL, R_MMIX_PC_24},
00816     {BFD_RELOC_32_PCREL, R_MMIX_PC_32},
00817     {BFD_RELOC_64_PCREL, R_MMIX_PC_64},
00818     {BFD_RELOC_VTABLE_INHERIT, R_MMIX_GNU_VTINHERIT},
00819     {BFD_RELOC_VTABLE_ENTRY, R_MMIX_GNU_VTENTRY},
00820     {BFD_RELOC_MMIX_GETA, R_MMIX_GETA},
00821     {BFD_RELOC_MMIX_CBRANCH, R_MMIX_CBRANCH},
00822     {BFD_RELOC_MMIX_PUSHJ, R_MMIX_PUSHJ},
00823     {BFD_RELOC_MMIX_JMP, R_MMIX_JMP},
00824     {BFD_RELOC_MMIX_ADDR19, R_MMIX_ADDR19},
00825     {BFD_RELOC_MMIX_ADDR27, R_MMIX_ADDR27},
00826     {BFD_RELOC_MMIX_REG_OR_BYTE, R_MMIX_REG_OR_BYTE},
00827     {BFD_RELOC_MMIX_REG, R_MMIX_REG},
00828     {BFD_RELOC_MMIX_BASE_PLUS_OFFSET, R_MMIX_BASE_PLUS_OFFSET},
00829     {BFD_RELOC_MMIX_LOCAL, R_MMIX_LOCAL},
00830     {BFD_RELOC_MMIX_PUSHJ_STUBBABLE, R_MMIX_PUSHJ_STUBBABLE}
00831   };
00832 
00833 static reloc_howto_type *
00834 bfd_elf64_bfd_reloc_type_lookup (abfd, code)
00835      bfd *abfd ATTRIBUTE_UNUSED;
00836      bfd_reloc_code_real_type code;
00837 {
00838   unsigned int i;
00839 
00840   for (i = 0;
00841        i < sizeof (mmix_reloc_map) / sizeof (mmix_reloc_map[0]);
00842        i++)
00843     {
00844       if (mmix_reloc_map[i].bfd_reloc_val == code)
00845        return &elf_mmix_howto_table[mmix_reloc_map[i].elf_reloc_val];
00846     }
00847 
00848   return NULL;
00849 }
00850 
00851 static reloc_howto_type *
00852 bfd_elf64_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00853                              const char *r_name)
00854 {
00855   unsigned int i;
00856 
00857   for (i = 0;
00858        i < sizeof (elf_mmix_howto_table) / sizeof (elf_mmix_howto_table[0]);
00859        i++)
00860     if (elf_mmix_howto_table[i].name != NULL
00861        && strcasecmp (elf_mmix_howto_table[i].name, r_name) == 0)
00862       return &elf_mmix_howto_table[i];
00863 
00864   return NULL;
00865 }
00866 
00867 static bfd_boolean
00868 mmix_elf_new_section_hook (abfd, sec)
00869      bfd *abfd;
00870      asection *sec;
00871 {
00872   if (!sec->used_by_bfd)
00873     {
00874       struct _mmix_elf_section_data *sdata;
00875       bfd_size_type amt = sizeof (*sdata);
00876 
00877       sdata = bfd_zalloc (abfd, amt);
00878       if (sdata == NULL)
00879        return FALSE;
00880       sec->used_by_bfd = sdata;
00881     }
00882 
00883   return _bfd_elf_new_section_hook (abfd, sec);
00884 }
00885 
00886 
00887 /* This function performs the actual bitfiddling and sanity check for a
00888    final relocation.  Each relocation gets its *worst*-case expansion
00889    in size when it arrives here; any reduction in size should have been
00890    caught in linker relaxation earlier.  When we get here, the relocation
00891    looks like the smallest instruction with SWYM:s (nop:s) appended to the
00892    max size.  We fill in those nop:s.
00893 
00894    R_MMIX_GETA: (FIXME: Relaxation should break this up in 1, 2, 3 tetra)
00895     GETA $N,foo
00896    ->
00897     SETL $N,foo & 0xffff
00898     INCML $N,(foo >> 16) & 0xffff
00899     INCMH $N,(foo >> 32) & 0xffff
00900     INCH $N,(foo >> 48) & 0xffff
00901 
00902    R_MMIX_CBRANCH: (FIXME: Relaxation should break this up, but
00903    condbranches needing relaxation might be rare enough to not be
00904    worthwhile.)
00905     [P]Bcc $N,foo
00906    ->
00907     [~P]B~cc $N,.+20
00908     SETL $255,foo & ...
00909     INCML ...
00910     INCMH ...
00911     INCH ...
00912     GO $255,$255,0
00913 
00914    R_MMIX_PUSHJ: (FIXME: Relaxation...)
00915     PUSHJ $N,foo
00916    ->
00917     SETL $255,foo & ...
00918     INCML ...
00919     INCMH ...
00920     INCH ...
00921     PUSHGO $N,$255,0
00922 
00923    R_MMIX_JMP: (FIXME: Relaxation...)
00924     JMP foo
00925    ->
00926     SETL $255,foo & ...
00927     INCML ...
00928     INCMH ...
00929     INCH ...
00930     GO $255,$255,0
00931 
00932    R_MMIX_ADDR19 and R_MMIX_ADDR27 are just filled in.  */
00933 
00934 static bfd_reloc_status_type
00935 mmix_elf_perform_relocation (isec, howto, datap, addr, value)
00936      asection *isec;
00937      reloc_howto_type *howto;
00938      PTR datap;
00939      bfd_vma addr;
00940      bfd_vma value;
00941 {
00942   bfd *abfd = isec->owner;
00943   bfd_reloc_status_type flag = bfd_reloc_ok;
00944   bfd_reloc_status_type r;
00945   int offs = 0;
00946   int reg = 255;
00947 
00948   /* The worst case bits are all similar SETL/INCML/INCMH/INCH sequences.
00949      We handle the differences here and the common sequence later.  */
00950   switch (howto->type)
00951     {
00952     case R_MMIX_GETA:
00953       offs = 0;
00954       reg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
00955 
00956       /* We change to an absolute value.  */
00957       value += addr;
00958       break;
00959 
00960     case R_MMIX_CBRANCH:
00961       {
00962        int in1 = bfd_get_16 (abfd, (bfd_byte *) datap) << 16;
00963 
00964        /* Invert the condition and prediction bit, and set the offset
00965           to five instructions ahead.
00966 
00967           We *can* do better if we want to.  If the branch is found to be
00968           within limits, we could leave the branch as is; there'll just
00969           be a bunch of NOP:s after it.  But we shouldn't see this
00970           sequence often enough that it's worth doing it.  */
00971 
00972        bfd_put_32 (abfd,
00973                   (((in1 ^ ((PRED_INV_BIT | COND_INV_BIT) << 24)) & ~0xffff)
00974                    | (24/4)),
00975                   (bfd_byte *) datap);
00976 
00977        /* Put a "GO $255,$255,0" after the common sequence.  */
00978        bfd_put_32 (abfd,
00979                   ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24) | 0xffff00,
00980                   (bfd_byte *) datap + 20);
00981 
00982        /* Common sequence starts at offset 4.  */
00983        offs = 4;
00984 
00985        /* We change to an absolute value.  */
00986        value += addr;
00987       }
00988       break;
00989 
00990     case R_MMIX_PUSHJ_STUBBABLE:
00991       /* If the address fits, we're fine.  */
00992       if ((value & 3) == 0
00993          /* Note rightshift 0; see R_MMIX_JMP case below.  */
00994          && (r = bfd_check_overflow (complain_overflow_signed,
00995                                   howto->bitsize,
00996                                   0,
00997                                   bfd_arch_bits_per_address (abfd),
00998                                   value)) == bfd_reloc_ok)
00999        goto pcrel_mmix_reloc_fits;
01000       else
01001        {
01002          bfd_size_type size = isec->rawsize ? isec->rawsize : isec->size;
01003 
01004          /* We have the bytes at the PUSHJ insn and need to get the
01005             position for the stub.  There's supposed to be room allocated
01006             for the stub.  */
01007          bfd_byte *stubcontents
01008            = ((bfd_byte *) datap
01009               - (addr - (isec->output_section->vma + isec->output_offset))
01010               + size
01011               + mmix_elf_section_data (isec)->pjs.stub_offset);
01012          bfd_vma stubaddr;
01013 
01014          /* The address doesn't fit, so redirect the PUSHJ to the
01015             location of the stub.  */
01016          r = mmix_elf_perform_relocation (isec,
01017                                       &elf_mmix_howto_table
01018                                       [R_MMIX_ADDR19],
01019                                       datap,
01020                                       addr,
01021                                       isec->output_section->vma
01022                                       + isec->output_offset
01023                                       + size
01024                                       + (mmix_elf_section_data (isec)
01025                                          ->pjs.stub_offset)
01026                                       - addr);
01027          if (r != bfd_reloc_ok)
01028            return r;
01029 
01030          stubaddr
01031            = (isec->output_section->vma
01032               + isec->output_offset
01033               + size
01034               + mmix_elf_section_data (isec)->pjs.stub_offset);
01035 
01036          /* We generate a simple JMP if that suffices, else the whole 5
01037             insn stub.  */
01038          if (bfd_check_overflow (complain_overflow_signed,
01039                               elf_mmix_howto_table[R_MMIX_ADDR27].bitsize,
01040                               0,
01041                               bfd_arch_bits_per_address (abfd),
01042                               addr + value - stubaddr) == bfd_reloc_ok)
01043            {
01044              bfd_put_32 (abfd, JMP_INSN_BYTE << 24, stubcontents);
01045              r = mmix_elf_perform_relocation (isec,
01046                                           &elf_mmix_howto_table
01047                                           [R_MMIX_ADDR27],
01048                                           stubcontents,
01049                                           stubaddr,
01050                                           value + addr - stubaddr);
01051              mmix_elf_section_data (isec)->pjs.stub_offset += 4;
01052 
01053              if (size + mmix_elf_section_data (isec)->pjs.stub_offset
01054                 > isec->size)
01055               abort ();
01056 
01057              return r;
01058            }
01059          else
01060            {
01061              /* Put a "GO $255,0" after the common sequence.  */
01062              bfd_put_32 (abfd,
01063                        ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
01064                        | 0xff00, (bfd_byte *) stubcontents + 16);
01065 
01066              /* Prepare for the general code to set the first part of the
01067                linker stub, and */
01068              value += addr;
01069              datap = stubcontents;
01070              mmix_elf_section_data (isec)->pjs.stub_offset
01071               += MAX_PUSHJ_STUB_SIZE;
01072            }
01073        }
01074       break;
01075 
01076     case R_MMIX_PUSHJ:
01077       {
01078        int inreg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
01079 
01080        /* Put a "PUSHGO $N,$255,0" after the common sequence.  */
01081        bfd_put_32 (abfd,
01082                   ((PUSHGO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
01083                   | (inreg << 16)
01084                   | 0xff00,
01085                   (bfd_byte *) datap + 16);
01086 
01087        /* We change to an absolute value.  */
01088        value += addr;
01089       }
01090       break;
01091 
01092     case R_MMIX_JMP:
01093       /* This one is a little special.  If we get here on a non-relaxing
01094         link, and the destination is actually in range, we don't need to
01095         execute the nops.
01096         If so, we fall through to the bit-fiddling relocs.
01097 
01098         FIXME: bfd_check_overflow seems broken; the relocation is
01099         rightshifted before testing, so supply a zero rightshift.  */
01100 
01101       if (! ((value & 3) == 0
01102             && (r = bfd_check_overflow (complain_overflow_signed,
01103                                     howto->bitsize,
01104                                     0,
01105                                     bfd_arch_bits_per_address (abfd),
01106                                     value)) == bfd_reloc_ok))
01107        {
01108          /* If the relocation doesn't fit in a JMP, we let the NOP:s be
01109             modified below, and put a "GO $255,$255,0" after the
01110             address-loading sequence.  */
01111          bfd_put_32 (abfd,
01112                     ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
01113                     | 0xffff00,
01114                     (bfd_byte *) datap + 16);
01115 
01116          /* We change to an absolute value.  */
01117          value += addr;
01118          break;
01119        }
01120       /* FALLTHROUGH.  */
01121     case R_MMIX_ADDR19:
01122     case R_MMIX_ADDR27:
01123     pcrel_mmix_reloc_fits:
01124       /* These must be in range, or else we emit an error.  */
01125       if ((value & 3) == 0
01126          /* Note rightshift 0; see above.  */
01127          && (r = bfd_check_overflow (complain_overflow_signed,
01128                                   howto->bitsize,
01129                                   0,
01130                                   bfd_arch_bits_per_address (abfd),
01131                                   value)) == bfd_reloc_ok)
01132        {
01133          bfd_vma in1
01134            = bfd_get_32 (abfd, (bfd_byte *) datap);
01135          bfd_vma highbit;
01136 
01137          if ((bfd_signed_vma) value < 0)
01138            {
01139              highbit = 1 << 24;
01140              value += (1 << (howto->bitsize - 1));
01141            }
01142          else
01143            highbit = 0;
01144 
01145          value >>= 2;
01146 
01147          bfd_put_32 (abfd,
01148                     (in1 & howto->src_mask)
01149                     | highbit
01150                     | (value & howto->dst_mask),
01151                     (bfd_byte *) datap);
01152 
01153          return bfd_reloc_ok;
01154        }
01155       else
01156        return bfd_reloc_overflow;
01157 
01158     case R_MMIX_BASE_PLUS_OFFSET:
01159       {
01160        struct bpo_reloc_section_info *bpodata
01161          = mmix_elf_section_data (isec)->bpo.reloc;
01162        asection *bpo_greg_section
01163          = bpodata->bpo_greg_section;
01164        struct bpo_greg_section_info *gregdata
01165          = mmix_elf_section_data (bpo_greg_section)->bpo.greg;
01166        size_t bpo_index
01167          = gregdata->bpo_reloc_indexes[bpodata->bpo_index++];
01168 
01169        /* A consistency check: The value we now have in "relocation" must
01170           be the same as the value we stored for that relocation.  It
01171           doesn't cost much, so can be left in at all times.  */
01172        if (value != gregdata->reloc_request[bpo_index].value)
01173          {
01174            (*_bfd_error_handler)
01175              (_("%s: Internal inconsistency error for value for\n\
01176  linker-allocated global register: linked: 0x%lx%08lx != relaxed: 0x%lx%08lx\n"),
01177               bfd_get_filename (isec->owner),
01178               (unsigned long) (value >> 32), (unsigned long) value,
01179               (unsigned long) (gregdata->reloc_request[bpo_index].value
01180                             >> 32),
01181               (unsigned long) gregdata->reloc_request[bpo_index].value);
01182            bfd_set_error (bfd_error_bad_value);
01183            return bfd_reloc_overflow;
01184          }
01185 
01186        /* Then store the register number and offset for that register
01187           into datap and datap + 1 respectively.  */
01188        bfd_put_8 (abfd,
01189                  gregdata->reloc_request[bpo_index].regindex
01190                  + bpo_greg_section->output_section->vma / 8,
01191                  datap);
01192        bfd_put_8 (abfd,
01193                  gregdata->reloc_request[bpo_index].offset,
01194                  ((unsigned char *) datap) + 1);
01195        return bfd_reloc_ok;
01196       }
01197 
01198     case R_MMIX_REG_OR_BYTE:
01199     case R_MMIX_REG:
01200       if (value > 255)
01201        return bfd_reloc_overflow;
01202       bfd_put_8 (abfd, value, datap);
01203       return bfd_reloc_ok;
01204 
01205     default:
01206       BAD_CASE (howto->type);
01207     }
01208 
01209   /* This code adds the common SETL/INCML/INCMH/INCH worst-case
01210      sequence.  */
01211 
01212   /* Lowest two bits must be 0.  We return bfd_reloc_overflow for
01213      everything that looks strange.  */
01214   if (value & 3)
01215     flag = bfd_reloc_overflow;
01216 
01217   bfd_put_32 (abfd,
01218              (SETL_INSN_BYTE << 24) | (value & 0xffff) | (reg << 16),
01219              (bfd_byte *) datap + offs);
01220   bfd_put_32 (abfd,
01221              (INCML_INSN_BYTE << 24) | ((value >> 16) & 0xffff) | (reg << 16),
01222              (bfd_byte *) datap + offs + 4);
01223   bfd_put_32 (abfd,
01224              (INCMH_INSN_BYTE << 24) | ((value >> 32) & 0xffff) | (reg << 16),
01225              (bfd_byte *) datap + offs + 8);
01226   bfd_put_32 (abfd,
01227              (INCH_INSN_BYTE << 24) | ((value >> 48) & 0xffff) | (reg << 16),
01228              (bfd_byte *) datap + offs + 12);
01229 
01230   return flag;
01231 }
01232 
01233 /* Set the howto pointer for an MMIX ELF reloc (type RELA).  */
01234 
01235 static void
01236 mmix_info_to_howto_rela (abfd, cache_ptr, dst)
01237      bfd *abfd ATTRIBUTE_UNUSED;
01238      arelent *cache_ptr;
01239      Elf_Internal_Rela *dst;
01240 {
01241   unsigned int r_type;
01242 
01243   r_type = ELF64_R_TYPE (dst->r_info);
01244   BFD_ASSERT (r_type < (unsigned int) R_MMIX_max);
01245   cache_ptr->howto = &elf_mmix_howto_table[r_type];
01246 }
01247 
01248 /* Any MMIX-specific relocation gets here at assembly time or when linking
01249    to other formats (such as mmo); this is the relocation function from
01250    the reloc_table.  We don't get here for final pure ELF linking.  */
01251 
01252 static bfd_reloc_status_type
01253 mmix_elf_reloc (abfd, reloc_entry, symbol, data, input_section,
01254               output_bfd, error_message)
01255      bfd *abfd;
01256      arelent *reloc_entry;
01257      asymbol *symbol;
01258      PTR data;
01259      asection *input_section;
01260      bfd *output_bfd;
01261      char **error_message ATTRIBUTE_UNUSED;
01262 {
01263   bfd_vma relocation;
01264   bfd_reloc_status_type r;
01265   asection *reloc_target_output_section;
01266   bfd_reloc_status_type flag = bfd_reloc_ok;
01267   bfd_vma output_base = 0;
01268   bfd_vma addr;
01269 
01270   r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
01271                           input_section, output_bfd, error_message);
01272 
01273   /* If that was all that was needed (i.e. this isn't a final link, only
01274      some segment adjustments), we're done.  */
01275   if (r != bfd_reloc_continue)
01276     return r;
01277 
01278   if (bfd_is_und_section (symbol->section)
01279       && (symbol->flags & BSF_WEAK) == 0
01280       && output_bfd == (bfd *) NULL)
01281     return bfd_reloc_undefined;
01282 
01283   /* Is the address of the relocation really within the section?  */
01284   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
01285     return bfd_reloc_outofrange;
01286 
01287   /* Work out which section the relocation is targeted at and the
01288      initial relocation command value.  */
01289 
01290   /* Get symbol value.  (Common symbols are special.)  */
01291   if (bfd_is_com_section (symbol->section))
01292     relocation = 0;
01293   else
01294     relocation = symbol->value;
01295 
01296   reloc_target_output_section = bfd_get_output_section (symbol);
01297 
01298   /* Here the variable relocation holds the final address of the symbol we
01299      are relocating against, plus any addend.  */
01300   if (output_bfd)
01301     output_base = 0;
01302   else
01303     output_base = reloc_target_output_section->vma;
01304 
01305   relocation += output_base + symbol->section->output_offset;
01306 
01307   /* Get position of relocation.  */
01308   addr = (reloc_entry->address + input_section->output_section->vma
01309          + input_section->output_offset);
01310   if (output_bfd != (bfd *) NULL)
01311     {
01312       /* Add in supplied addend.  */
01313       relocation += reloc_entry->addend;
01314 
01315       /* This is a partial relocation, and we want to apply the
01316         relocation to the reloc entry rather than the raw data.
01317         Modify the reloc inplace to reflect what we now know.  */
01318       reloc_entry->addend = relocation;
01319       reloc_entry->address += input_section->output_offset;
01320       return flag;
01321     }
01322 
01323   return mmix_final_link_relocate (reloc_entry->howto, input_section,
01324                                data, reloc_entry->address,
01325                                reloc_entry->addend, relocation,
01326                                bfd_asymbol_name (symbol),
01327                                reloc_target_output_section);
01328 }
01329 
01330 /* Relocate an MMIX ELF section.  Modified from elf32-fr30.c; look to it
01331    for guidance if you're thinking of copying this.  */
01332 
01333 static bfd_boolean
01334 mmix_elf_relocate_section (output_bfd, info, input_bfd, input_section,
01335                         contents, relocs, local_syms, local_sections)
01336      bfd *output_bfd ATTRIBUTE_UNUSED;
01337      struct bfd_link_info *info;
01338      bfd *input_bfd;
01339      asection *input_section;
01340      bfd_byte *contents;
01341      Elf_Internal_Rela *relocs;
01342      Elf_Internal_Sym *local_syms;
01343      asection **local_sections;
01344 {
01345   Elf_Internal_Shdr *symtab_hdr;
01346   struct elf_link_hash_entry **sym_hashes;
01347   Elf_Internal_Rela *rel;
01348   Elf_Internal_Rela *relend;
01349   bfd_size_type size;
01350   size_t pjsno = 0;
01351 
01352   size = input_section->rawsize ? input_section->rawsize : input_section->size;
01353   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
01354   sym_hashes = elf_sym_hashes (input_bfd);
01355   relend = relocs + input_section->reloc_count;
01356 
01357   /* Zero the stub area before we start.  */
01358   if (input_section->rawsize != 0
01359       && input_section->size > input_section->rawsize)
01360     memset (contents + input_section->rawsize, 0,
01361            input_section->size - input_section->rawsize);
01362 
01363   for (rel = relocs; rel < relend; rel ++)
01364     {
01365       reloc_howto_type *howto;
01366       unsigned long r_symndx;
01367       Elf_Internal_Sym *sym;
01368       asection *sec;
01369       struct elf_link_hash_entry *h;
01370       bfd_vma relocation;
01371       bfd_reloc_status_type r;
01372       const char *name = NULL;
01373       int r_type;
01374       bfd_boolean undefined_signalled = FALSE;
01375 
01376       r_type = ELF64_R_TYPE (rel->r_info);
01377 
01378       if (r_type == R_MMIX_GNU_VTINHERIT
01379          || r_type == R_MMIX_GNU_VTENTRY)
01380        continue;
01381 
01382       r_symndx = ELF64_R_SYM (rel->r_info);
01383 
01384       howto = elf_mmix_howto_table + ELF64_R_TYPE (rel->r_info);
01385       h = NULL;
01386       sym = NULL;
01387       sec = NULL;
01388 
01389       if (r_symndx < symtab_hdr->sh_info)
01390        {
01391          sym = local_syms + r_symndx;
01392          sec = local_sections [r_symndx];
01393          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
01394 
01395          name = bfd_elf_string_from_elf_section (input_bfd,
01396                                             symtab_hdr->sh_link,
01397                                             sym->st_name);
01398          if (name == NULL)
01399            name = bfd_section_name (input_bfd, sec);
01400        }
01401       else
01402        {
01403          bfd_boolean unresolved_reloc;
01404 
01405          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
01406                                r_symndx, symtab_hdr, sym_hashes,
01407                                h, sec, relocation,
01408                                unresolved_reloc, undefined_signalled);
01409          name = h->root.root.string;
01410        }
01411 
01412       if (sec != NULL && elf_discarded_section (sec))
01413        {
01414          /* For relocs against symbols from removed linkonce sections,
01415             or sections discarded by a linker script, we just want the
01416             section contents zeroed.  Avoid any special processing.  */
01417          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
01418          rel->r_info = 0;
01419          rel->r_addend = 0;
01420          continue;
01421        }
01422 
01423       if (info->relocatable)
01424        {
01425          /* This is a relocatable link.  For most relocs we don't have to
01426             change anything, unless the reloc is against a section
01427             symbol, in which case we have to adjust according to where
01428             the section symbol winds up in the output section.  */
01429          if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
01430            rel->r_addend += sec->output_offset;
01431 
01432          /* For PUSHJ stub relocs however, we may need to change the
01433             reloc and the section contents, if the reloc doesn't reach
01434             beyond the end of the output section and previous stubs.
01435             Then we change the section contents to be a PUSHJ to the end
01436             of the input section plus stubs (we can do that without using
01437             a reloc), and then we change the reloc to be a R_MMIX_PUSHJ
01438             at the stub location.  */
01439          if (r_type == R_MMIX_PUSHJ_STUBBABLE)
01440            {
01441              /* We've already checked whether we need a stub; use that
01442                knowledge.  */
01443              if (mmix_elf_section_data (input_section)->pjs.stub_size[pjsno]
01444                 != 0)
01445               {
01446                 Elf_Internal_Rela relcpy;
01447 
01448                 if (mmix_elf_section_data (input_section)
01449                     ->pjs.stub_size[pjsno] != MAX_PUSHJ_STUB_SIZE)
01450                   abort ();
01451 
01452                 /* There's already a PUSHJ insn there, so just fill in
01453                    the offset bits to the stub.  */
01454                 if (mmix_final_link_relocate (elf_mmix_howto_table
01455                                           + R_MMIX_ADDR19,
01456                                           input_section,
01457                                           contents,
01458                                           rel->r_offset,
01459                                           0,
01460                                           input_section
01461                                           ->output_section->vma
01462                                           + input_section->output_offset
01463                                           + size
01464                                           + mmix_elf_section_data (input_section)
01465                                           ->pjs.stub_offset,
01466                                           NULL, NULL) != bfd_reloc_ok)
01467                   return FALSE;
01468 
01469                 /* Put a JMP insn at the stub; it goes with the
01470                    R_MMIX_JMP reloc.  */
01471                 bfd_put_32 (output_bfd, JMP_INSN_BYTE << 24,
01472                            contents
01473                            + size
01474                            + mmix_elf_section_data (input_section)
01475                            ->pjs.stub_offset);
01476 
01477                 /* Change the reloc to be at the stub, and to a full
01478                    R_MMIX_JMP reloc.  */
01479                 rel->r_info = ELF64_R_INFO (r_symndx, R_MMIX_JMP);
01480                 rel->r_offset
01481                   = (size
01482                      + mmix_elf_section_data (input_section)
01483                      ->pjs.stub_offset);
01484 
01485                 mmix_elf_section_data (input_section)->pjs.stub_offset
01486                   += MAX_PUSHJ_STUB_SIZE;
01487 
01488                 /* Shift this reloc to the end of the relocs to maintain
01489                    the r_offset sorted reloc order.  */
01490                 relcpy = *rel;
01491                 memmove (rel, rel + 1, (char *) relend - (char *) rel);
01492                 relend[-1] = relcpy;
01493 
01494                 /* Back up one reloc, or else we'd skip the next reloc
01495                  in turn.  */
01496                 rel--;
01497               }
01498 
01499              pjsno++;
01500            }
01501          continue;
01502        }
01503 
01504       r = mmix_final_link_relocate (howto, input_section,
01505                                 contents, rel->r_offset,
01506                                 rel->r_addend, relocation, name, sec);
01507 
01508       if (r != bfd_reloc_ok)
01509        {
01510          bfd_boolean check_ok = TRUE;
01511          const char * msg = (const char *) NULL;
01512 
01513          switch (r)
01514            {
01515            case bfd_reloc_overflow:
01516              check_ok = info->callbacks->reloc_overflow
01517               (info, (h ? &h->root : NULL), name, howto->name,
01518                (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
01519              break;
01520 
01521            case bfd_reloc_undefined:
01522              /* We may have sent this message above.  */
01523              if (! undefined_signalled)
01524               check_ok = info->callbacks->undefined_symbol
01525                 (info, name, input_bfd, input_section, rel->r_offset,
01526                  TRUE);
01527              undefined_signalled = TRUE;
01528              break;
01529 
01530            case bfd_reloc_outofrange:
01531              msg = _("internal error: out of range error");
01532              break;
01533 
01534            case bfd_reloc_notsupported:
01535              msg = _("internal error: unsupported relocation error");
01536              break;
01537 
01538            case bfd_reloc_dangerous:
01539              msg = _("internal error: dangerous relocation");
01540              break;
01541 
01542            default:
01543              msg = _("internal error: unknown error");
01544              break;
01545            }
01546 
01547          if (msg)
01548            check_ok = info->callbacks->warning
01549              (info, msg, name, input_bfd, input_section, rel->r_offset);
01550 
01551          if (! check_ok)
01552            return FALSE;
01553        }
01554     }
01555 
01556   return TRUE;
01557 }
01558 
01559 /* Perform a single relocation.  By default we use the standard BFD
01560    routines.  A few relocs we have to do ourselves.  */
01561 
01562 static bfd_reloc_status_type
01563 mmix_final_link_relocate (howto, input_section, contents,
01564                        r_offset, r_addend, relocation, symname, symsec)
01565      reloc_howto_type *howto;
01566      asection *input_section;
01567      bfd_byte *contents;
01568      bfd_vma r_offset;
01569      bfd_signed_vma r_addend;
01570      bfd_vma relocation;
01571      const char *symname;
01572      asection *symsec;
01573 {
01574   bfd_reloc_status_type r = bfd_reloc_ok;
01575   bfd_vma addr
01576     = (input_section->output_section->vma
01577        + input_section->output_offset
01578        + r_offset);
01579   bfd_signed_vma srel
01580     = (bfd_signed_vma) relocation + r_addend;
01581 
01582   switch (howto->type)
01583     {
01584       /* All these are PC-relative.  */
01585     case R_MMIX_PUSHJ_STUBBABLE:
01586     case R_MMIX_PUSHJ:
01587     case R_MMIX_CBRANCH:
01588     case R_MMIX_ADDR19:
01589     case R_MMIX_GETA:
01590     case R_MMIX_ADDR27:
01591     case R_MMIX_JMP:
01592       contents += r_offset;
01593 
01594       srel -= (input_section->output_section->vma
01595               + input_section->output_offset
01596               + r_offset);
01597 
01598       r = mmix_elf_perform_relocation (input_section, howto, contents,
01599                                    addr, srel);
01600       break;
01601 
01602     case R_MMIX_BASE_PLUS_OFFSET:
01603       if (symsec == NULL)
01604        return bfd_reloc_undefined;
01605 
01606       /* Check that we're not relocating against a register symbol.  */
01607       if (strcmp (bfd_get_section_name (symsec->owner, symsec),
01608                 MMIX_REG_CONTENTS_SECTION_NAME) == 0
01609          || strcmp (bfd_get_section_name (symsec->owner, symsec),
01610                    MMIX_REG_SECTION_NAME) == 0)
01611        {
01612          /* Note: This is separated out into two messages in order
01613             to ease the translation into other languages.  */
01614          if (symname == NULL || *symname == 0)
01615            (*_bfd_error_handler)
01616              (_("%s: base-plus-offset relocation against register symbol: (unknown) in %s"),
01617               bfd_get_filename (input_section->owner),
01618               bfd_get_section_name (symsec->owner, symsec));
01619          else
01620            (*_bfd_error_handler)
01621              (_("%s: base-plus-offset relocation against register symbol: %s in %s"),
01622               bfd_get_filename (input_section->owner), symname,
01623               bfd_get_section_name (symsec->owner, symsec));
01624          return bfd_reloc_overflow;
01625        }
01626       goto do_mmix_reloc;
01627 
01628     case R_MMIX_REG_OR_BYTE:
01629     case R_MMIX_REG:
01630       /* For now, we handle these alike.  They must refer to an register
01631         symbol, which is either relative to the register section and in
01632         the range 0..255, or is in the register contents section with vma
01633         regno * 8.  */
01634 
01635       /* FIXME: A better way to check for reg contents section?
01636         FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
01637       if (symsec == NULL)
01638        return bfd_reloc_undefined;
01639 
01640       if (strcmp (bfd_get_section_name (symsec->owner, symsec),
01641                 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
01642        {
01643          if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
01644            {
01645              /* The bfd_reloc_outofrange return value, though intuitively
01646                a better value, will not get us an error.  */
01647              return bfd_reloc_overflow;
01648            }
01649          srel /= 8;
01650        }
01651       else if (strcmp (bfd_get_section_name (symsec->owner, symsec),
01652                      MMIX_REG_SECTION_NAME) == 0)
01653        {
01654          if (srel < 0 || srel > 255)
01655            /* The bfd_reloc_outofrange return value, though intuitively a
01656               better value, will not get us an error.  */
01657            return bfd_reloc_overflow;
01658        }
01659       else
01660        {
01661          /* Note: This is separated out into two messages in order
01662             to ease the translation into other languages.  */
01663          if (symname == NULL || *symname == 0)
01664            (*_bfd_error_handler)
01665              (_("%s: register relocation against non-register symbol: (unknown) in %s"),
01666               bfd_get_filename (input_section->owner),
01667               bfd_get_section_name (symsec->owner, symsec));
01668          else
01669            (*_bfd_error_handler)
01670              (_("%s: register relocation against non-register symbol: %s in %s"),
01671               bfd_get_filename (input_section->owner), symname,
01672               bfd_get_section_name (symsec->owner, symsec));
01673 
01674          /* The bfd_reloc_outofrange return value, though intuitively a
01675             better value, will not get us an error.  */
01676          return bfd_reloc_overflow;
01677        }
01678     do_mmix_reloc:
01679       contents += r_offset;
01680       r = mmix_elf_perform_relocation (input_section, howto, contents,
01681                                    addr, srel);
01682       break;
01683 
01684     case R_MMIX_LOCAL:
01685       /* This isn't a real relocation, it's just an assertion that the
01686         final relocation value corresponds to a local register.  We
01687         ignore the actual relocation; nothing is changed.  */
01688       {
01689        asection *regsec
01690          = bfd_get_section_by_name (input_section->output_section->owner,
01691                                  MMIX_REG_CONTENTS_SECTION_NAME);
01692        bfd_vma first_global;
01693 
01694        /* Check that this is an absolute value, or a reference to the
01695           register contents section or the register (symbol) section.
01696           Absolute numbers can get here as undefined section.  Undefined
01697           symbols are signalled elsewhere, so there's no conflict in us
01698           accidentally handling it.  */
01699        if (!bfd_is_abs_section (symsec)
01700            && !bfd_is_und_section (symsec)
01701            && strcmp (bfd_get_section_name (symsec->owner, symsec),
01702                      MMIX_REG_CONTENTS_SECTION_NAME) != 0
01703            && strcmp (bfd_get_section_name (symsec->owner, symsec),
01704                      MMIX_REG_SECTION_NAME) != 0)
01705        {
01706          (*_bfd_error_handler)
01707            (_("%s: directive LOCAL valid only with a register or absolute value"),
01708             bfd_get_filename (input_section->owner));
01709 
01710          return bfd_reloc_overflow;
01711        }
01712 
01713       /* If we don't have a register contents section, then $255 is the
01714         first global register.  */
01715       if (regsec == NULL)
01716        first_global = 255;
01717       else
01718        {
01719          first_global = bfd_get_section_vma (abfd, regsec) / 8;
01720          if (strcmp (bfd_get_section_name (symsec->owner, symsec),
01721                     MMIX_REG_CONTENTS_SECTION_NAME) == 0)
01722            {
01723              if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
01724               /* The bfd_reloc_outofrange return value, though
01725                  intuitively a better value, will not get us an error.  */
01726               return bfd_reloc_overflow;
01727              srel /= 8;
01728            }
01729        }
01730 
01731        if ((bfd_vma) srel >= first_global)
01732          {
01733            /* FIXME: Better error message.  */
01734            (*_bfd_error_handler)
01735              (_("%s: LOCAL directive: Register $%ld is not a local register.  First global register is $%ld."),
01736               bfd_get_filename (input_section->owner), (long) srel, (long) first_global);
01737 
01738            return bfd_reloc_overflow;
01739          }
01740       }
01741       r = bfd_reloc_ok;
01742       break;
01743 
01744     default:
01745       r = _bfd_final_link_relocate (howto, input_section->owner, input_section,
01746                                 contents, r_offset,
01747                                 relocation, r_addend);
01748     }
01749 
01750   return r;
01751 }
01752 
01753 /* Return the section that should be marked against GC for a given
01754    relocation.  */
01755 
01756 static asection *
01757 mmix_elf_gc_mark_hook (asection *sec,
01758                      struct bfd_link_info *info,
01759                      Elf_Internal_Rela *rel,
01760                      struct elf_link_hash_entry *h,
01761                      Elf_Internal_Sym *sym)
01762 {
01763   if (h != NULL)
01764     switch (ELF64_R_TYPE (rel->r_info))
01765       {
01766       case R_MMIX_GNU_VTINHERIT:
01767       case R_MMIX_GNU_VTENTRY:
01768        return NULL;
01769       }
01770 
01771   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
01772 }
01773 
01774 /* Update relocation info for a GC-excluded section.  We could supposedly
01775    perform the allocation after GC, but there's no suitable hook between
01776    GC (or section merge) and the point when all input sections must be
01777    present.  Better to waste some memory and (perhaps) a little time.  */
01778 
01779 static bfd_boolean
01780 mmix_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
01781                      struct bfd_link_info *info ATTRIBUTE_UNUSED,
01782                      asection *sec,
01783                      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
01784 {
01785   struct bpo_reloc_section_info *bpodata
01786     = mmix_elf_section_data (sec)->bpo.reloc;
01787   asection *allocated_gregs_section;
01788 
01789   /* If no bpodata here, we have nothing to do.  */
01790   if (bpodata == NULL)
01791     return TRUE;
01792 
01793   allocated_gregs_section = bpodata->bpo_greg_section;
01794 
01795   mmix_elf_section_data (allocated_gregs_section)->bpo.greg->n_bpo_relocs
01796     -= bpodata->n_bpo_relocs_this_section;
01797 
01798   return TRUE;
01799 }
01800 
01801 /* Sort register relocs to come before expanding relocs.  */
01802 
01803 static int
01804 mmix_elf_sort_relocs (p1, p2)
01805      const PTR p1;
01806      const PTR p2;
01807 {
01808   const Elf_Internal_Rela *r1 = (const Elf_Internal_Rela *) p1;
01809   const Elf_Internal_Rela *r2 = (const Elf_Internal_Rela *) p2;
01810   int r1_is_reg, r2_is_reg;
01811 
01812   /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
01813      insns.  */
01814   if ((r1->r_offset & ~(bfd_vma) 3) > (r2->r_offset & ~(bfd_vma) 3))
01815     return 1;
01816   else if ((r1->r_offset & ~(bfd_vma) 3) < (r2->r_offset & ~(bfd_vma) 3))
01817     return -1;
01818 
01819   r1_is_reg
01820     = (ELF64_R_TYPE (r1->r_info) == R_MMIX_REG_OR_BYTE
01821        || ELF64_R_TYPE (r1->r_info) == R_MMIX_REG);
01822   r2_is_reg
01823     = (ELF64_R_TYPE (r2->r_info) == R_MMIX_REG_OR_BYTE
01824        || ELF64_R_TYPE (r2->r_info) == R_MMIX_REG);
01825   if (r1_is_reg != r2_is_reg)
01826     return r2_is_reg - r1_is_reg;
01827 
01828   /* Neither or both are register relocs.  Then sort on full offset.  */
01829   if (r1->r_offset > r2->r_offset)
01830     return 1;
01831   else if (r1->r_offset < r2->r_offset)
01832     return -1;
01833   return 0;
01834 }
01835 
01836 /* Subset of mmix_elf_check_relocs, common to ELF and mmo linking.  */
01837 
01838 static bfd_boolean
01839 mmix_elf_check_common_relocs  (abfd, info, sec, relocs)
01840      bfd *abfd;
01841      struct bfd_link_info *info;
01842      asection *sec;
01843      const Elf_Internal_Rela *relocs;
01844 {
01845   bfd *bpo_greg_owner = NULL;
01846   asection *allocated_gregs_section = NULL;
01847   struct bpo_greg_section_info *gregdata = NULL;
01848   struct bpo_reloc_section_info *bpodata = NULL;
01849   const Elf_Internal_Rela *rel;
01850   const Elf_Internal_Rela *rel_end;
01851 
01852   /* We currently have to abuse this COFF-specific member, since there's
01853      no target-machine-dedicated member.  There's no alternative outside
01854      the bfd_link_info struct; we can't specialize a hash-table since
01855      they're different between ELF and mmo.  */
01856   bpo_greg_owner = (bfd *) info->base_file;
01857 
01858   rel_end = relocs + sec->reloc_count;
01859   for (rel = relocs; rel < rel_end; rel++)
01860     {
01861       switch (ELF64_R_TYPE (rel->r_info))
01862         {
01863          /* This relocation causes a GREG allocation.  We need to count
01864             them, and we need to create a section for them, so we need an
01865             object to fake as the owner of that section.  We can't use
01866             the ELF dynobj for this, since the ELF bits assume lots of
01867             DSO-related stuff if that member is non-NULL.  */
01868        case R_MMIX_BASE_PLUS_OFFSET:
01869          /* We don't do anything with this reloc for a relocatable link.  */
01870          if (info->relocatable)
01871            break;
01872 
01873          if (bpo_greg_owner == NULL)
01874            {
01875              bpo_greg_owner = abfd;
01876              info->base_file = (PTR) bpo_greg_owner;
01877            }
01878 
01879          if (allocated_gregs_section == NULL)
01880            allocated_gregs_section
01881              = bfd_get_section_by_name (bpo_greg_owner,
01882                                     MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
01883 
01884          if (allocated_gregs_section == NULL)
01885            {
01886              allocated_gregs_section
01887               = bfd_make_section_with_flags (bpo_greg_owner,
01888                                           MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME,
01889                                           (SEC_HAS_CONTENTS
01890                                           | SEC_IN_MEMORY
01891                                           | SEC_LINKER_CREATED));
01892              /* Setting both SEC_ALLOC and SEC_LOAD means the section is
01893                treated like any other section, and we'd get errors for
01894                address overlap with the text section.  Let's set none of
01895                those flags, as that is what currently happens for usual
01896                GREG allocations, and that works.  */
01897              if (allocated_gregs_section == NULL
01898                 || !bfd_set_section_alignment (bpo_greg_owner,
01899                                            allocated_gregs_section,
01900                                            3))
01901               return FALSE;
01902 
01903              gregdata = (struct bpo_greg_section_info *)
01904               bfd_zalloc (bpo_greg_owner, sizeof (struct bpo_greg_section_info));
01905              if (gregdata == NULL)
01906               return FALSE;
01907              mmix_elf_section_data (allocated_gregs_section)->bpo.greg
01908               = gregdata;
01909            }
01910          else if (gregdata == NULL)
01911            gregdata
01912              = mmix_elf_section_data (allocated_gregs_section)->bpo.greg;
01913 
01914          /* Get ourselves some auxiliary info for the BPO-relocs.  */
01915          if (bpodata == NULL)
01916            {
01917              /* No use doing a separate iteration pass to find the upper
01918                limit - just use the number of relocs.  */
01919              bpodata = (struct bpo_reloc_section_info *)
01920               bfd_alloc (bpo_greg_owner,
01921                         sizeof (struct bpo_reloc_section_info)
01922                         * (sec->reloc_count + 1));
01923              if (bpodata == NULL)
01924               return FALSE;
01925              mmix_elf_section_data (sec)->bpo.reloc = bpodata;
01926              bpodata->first_base_plus_offset_reloc
01927               = bpodata->bpo_index
01928               = gregdata->n_max_bpo_relocs;
01929              bpodata->bpo_greg_section
01930               = allocated_gregs_section;
01931              bpodata->n_bpo_relocs_this_section = 0;
01932            }
01933 
01934          bpodata->n_bpo_relocs_this_section++;
01935          gregdata->n_max_bpo_relocs++;
01936 
01937          /* We don't get another chance to set this before GC; we've not
01938             set up any hook that runs before GC.  */
01939          gregdata->n_bpo_relocs
01940            = gregdata->n_max_bpo_relocs;
01941          break;
01942 
01943        case R_MMIX_PUSHJ_STUBBABLE:
01944          mmix_elf_section_data (sec)->pjs.n_pushj_relocs++;
01945          break;
01946        }
01947     }
01948 
01949   /* Allocate per-reloc stub storage and initialize it to the max stub
01950      size.  */
01951   if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs != 0)
01952     {
01953       size_t i;
01954 
01955       mmix_elf_section_data (sec)->pjs.stub_size
01956        = bfd_alloc (abfd, mmix_elf_section_data (sec)->pjs.n_pushj_relocs
01957                    * sizeof (mmix_elf_section_data (sec)
01958                             ->pjs.stub_size[0]));
01959       if (mmix_elf_section_data (sec)->pjs.stub_size == NULL)
01960        return FALSE;
01961 
01962       for (i = 0; i < mmix_elf_section_data (sec)->pjs.n_pushj_relocs; i++)
01963        mmix_elf_section_data (sec)->pjs.stub_size[i] = MAX_PUSHJ_STUB_SIZE;
01964     }
01965 
01966   return TRUE;
01967 }
01968 
01969 /* Look through the relocs for a section during the first phase.  */
01970 
01971 static bfd_boolean
01972 mmix_elf_check_relocs (abfd, info, sec, relocs)
01973      bfd *abfd;
01974      struct bfd_link_info *info;
01975      asection *sec;
01976      const Elf_Internal_Rela *relocs;
01977 {
01978   Elf_Internal_Shdr *symtab_hdr;
01979   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
01980   const Elf_Internal_Rela *rel;
01981   const Elf_Internal_Rela *rel_end;
01982 
01983   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
01984   sym_hashes = elf_sym_hashes (abfd);
01985   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf64_External_Sym);
01986   if (!elf_bad_symtab (abfd))
01987     sym_hashes_end -= symtab_hdr->sh_info;
01988 
01989   /* First we sort the relocs so that any register relocs come before
01990      expansion-relocs to the same insn.  FIXME: Not done for mmo.  */
01991   qsort ((PTR) relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
01992         mmix_elf_sort_relocs);
01993 
01994   /* Do the common part.  */
01995   if (!mmix_elf_check_common_relocs (abfd, info, sec, relocs))
01996     return FALSE;
01997 
01998   if (info->relocatable)
01999     return TRUE;
02000 
02001   rel_end = relocs + sec->reloc_count;
02002   for (rel = relocs; rel < rel_end; rel++)
02003     {
02004       struct elf_link_hash_entry *h;
02005       unsigned long r_symndx;
02006 
02007       r_symndx = ELF64_R_SYM (rel->r_info);
02008       if (r_symndx < symtab_hdr->sh_info)
02009         h = NULL;
02010       else
02011        {
02012          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
02013          while (h->root.type == bfd_link_hash_indirect
02014                || h->root.type == bfd_link_hash_warning)
02015            h = (struct elf_link_hash_entry *) h->root.u.i.link;
02016        }
02017 
02018       switch (ELF64_R_TYPE (rel->r_info))
02019        {
02020         /* This relocation describes the C++ object vtable hierarchy.
02021            Reconstruct it for later use during GC.  */
02022         case R_MMIX_GNU_VTINHERIT:
02023           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
02024             return FALSE;
02025           break;
02026 
02027         /* This relocation describes which C++ vtable entries are actually
02028            used.  Record for later use during GC.  */
02029         case R_MMIX_GNU_VTENTRY:
02030           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
02031             return FALSE;
02032           break;
02033        }
02034     }
02035 
02036   return TRUE;
02037 }
02038 
02039 /* Wrapper for mmix_elf_check_common_relocs, called when linking to mmo.
02040    Copied from elf_link_add_object_symbols.  */
02041 
02042 bfd_boolean
02043 _bfd_mmix_check_all_relocs (abfd, info)
02044      bfd *abfd;
02045      struct bfd_link_info *info;
02046 {
02047   asection *o;
02048 
02049   for (o = abfd->sections; o != NULL; o = o->next)
02050     {
02051       Elf_Internal_Rela *internal_relocs;
02052       bfd_boolean ok;
02053 
02054       if ((o->flags & SEC_RELOC) == 0
02055          || o->reloc_count == 0
02056          || ((info->strip == strip_all || info->strip == strip_debugger)
02057              && (o->flags & SEC_DEBUGGING) != 0)
02058          || bfd_is_abs_section (o->output_section))
02059        continue;
02060 
02061       internal_relocs
02062        = _bfd_elf_link_read_relocs (abfd, o, (PTR) NULL,
02063                                  (Elf_Internal_Rela *) NULL,
02064                                  info->keep_memory);
02065       if (internal_relocs == NULL)
02066        return FALSE;
02067 
02068       ok = mmix_elf_check_common_relocs (abfd, info, o, internal_relocs);
02069 
02070       if (! info->keep_memory)
02071        free (internal_relocs);
02072 
02073       if (! ok)
02074        return FALSE;
02075     }
02076 
02077   return TRUE;
02078 }
02079 
02080 /* Change symbols relative to the reg contents section to instead be to
02081    the register section, and scale them down to correspond to the register
02082    number.  */
02083 
02084 static bfd_boolean
02085 mmix_elf_link_output_symbol_hook (info, name, sym, input_sec, h)
02086      struct bfd_link_info *info ATTRIBUTE_UNUSED;
02087      const char *name ATTRIBUTE_UNUSED;
02088      Elf_Internal_Sym *sym;
02089      asection *input_sec;
02090      struct elf_link_hash_entry *h ATTRIBUTE_UNUSED;
02091 {
02092   if (input_sec != NULL
02093       && input_sec->name != NULL
02094       && ELF_ST_TYPE (sym->st_info) != STT_SECTION
02095       && strcmp (input_sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
02096     {
02097       sym->st_value /= 8;
02098       sym->st_shndx = SHN_REGISTER;
02099     }
02100 
02101   return TRUE;
02102 }
02103 
02104 /* We fake a register section that holds values that are register numbers.
02105    Having a SHN_REGISTER and register section translates better to other
02106    formats (e.g. mmo) than for example a STT_REGISTER attribute.
02107    This section faking is based on a construct in elf32-mips.c.  */
02108 static asection mmix_elf_reg_section;
02109 static asymbol mmix_elf_reg_section_symbol;
02110 static asymbol *mmix_elf_reg_section_symbol_ptr;
02111 
02112 /* Handle the special section numbers that a symbol may use.  */
02113 
02114 void
02115 mmix_elf_symbol_processing (abfd, asym)
02116      bfd *abfd ATTRIBUTE_UNUSED;
02117      asymbol *asym;
02118 {
02119   elf_symbol_type *elfsym;
02120 
02121   elfsym = (elf_symbol_type *) asym;
02122   switch (elfsym->internal_elf_sym.st_shndx)
02123     {
02124     case SHN_REGISTER:
02125       if (mmix_elf_reg_section.name == NULL)
02126        {
02127          /* Initialize the register section.  */
02128          mmix_elf_reg_section.name = MMIX_REG_SECTION_NAME;
02129          mmix_elf_reg_section.flags = SEC_NO_FLAGS;
02130          mmix_elf_reg_section.output_section = &mmix_elf_reg_section;
02131          mmix_elf_reg_section.symbol = &mmix_elf_reg_section_symbol;
02132          mmix_elf_reg_section.symbol_ptr_ptr = &mmix_elf_reg_section_symbol_ptr;
02133          mmix_elf_reg_section_symbol.name = MMIX_REG_SECTION_NAME;
02134          mmix_elf_reg_section_symbol.flags = BSF_SECTION_SYM;
02135          mmix_elf_reg_section_symbol.section = &mmix_elf_reg_section;
02136          mmix_elf_reg_section_symbol_ptr = &mmix_elf_reg_section_symbol;
02137        }
02138       asym->section = &mmix_elf_reg_section;
02139       break;
02140 
02141     default:
02142       break;
02143     }
02144 }
02145 
02146 /* Given a BFD section, try to locate the corresponding ELF section
02147    index.  */
02148 
02149 static bfd_boolean
02150 mmix_elf_section_from_bfd_section (abfd, sec, retval)
02151      bfd *                 abfd ATTRIBUTE_UNUSED;
02152      asection *            sec;
02153      int *                 retval;
02154 {
02155   if (strcmp (bfd_get_section_name (abfd, sec), MMIX_REG_SECTION_NAME) == 0)
02156     *retval = SHN_REGISTER;
02157   else
02158     return FALSE;
02159 
02160   return TRUE;
02161 }
02162 
02163 /* Hook called by the linker routine which adds symbols from an object
02164    file.  We must handle the special SHN_REGISTER section number here.
02165 
02166    We also check that we only have *one* each of the section-start
02167    symbols, since otherwise having two with the same value would cause
02168    them to be "merged", but with the contents serialized.  */
02169 
02170 bfd_boolean
02171 mmix_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
02172      bfd *abfd;
02173      struct bfd_link_info *info ATTRIBUTE_UNUSED;
02174      Elf_Internal_Sym *sym;
02175      const char **namep ATTRIBUTE_UNUSED;
02176      flagword *flagsp ATTRIBUTE_UNUSED;
02177      asection **secp;
02178      bfd_vma *valp ATTRIBUTE_UNUSED;
02179 {
02180   if (sym->st_shndx == SHN_REGISTER)
02181     {
02182       *secp = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
02183       (*secp)->flags |= SEC_LINKER_CREATED;
02184     }
02185   else if ((*namep)[0] == '_' && (*namep)[1] == '_' && (*namep)[2] == '.'
02186           && CONST_STRNEQ (*namep, MMIX_LOC_SECTION_START_SYMBOL_PREFIX))
02187     {
02188       /* See if we have another one.  */
02189       struct bfd_link_hash_entry *h = bfd_link_hash_lookup (info->hash,
02190                                                      *namep,
02191                                                      FALSE,
02192                                                      FALSE,
02193                                                      FALSE);
02194 
02195       if (h != NULL && h->type != bfd_link_hash_undefined)
02196        {
02197          /* How do we get the asymbol (or really: the filename) from h?
02198             h->u.def.section->owner is NULL.  */
02199          ((*_bfd_error_handler)
02200           (_("%s: Error: multiple definition of `%s'; start of %s is set in a earlier linked file\n"),
02201            bfd_get_filename (abfd), *namep,
02202            *namep + strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)));
02203           bfd_set_error (bfd_error_bad_value);
02204           return FALSE;
02205        }
02206     }
02207 
02208   return TRUE;
02209 }
02210 
02211 /* We consider symbols matching "L.*:[0-9]+" to be local symbols.  */
02212 
02213 bfd_boolean
02214 mmix_elf_is_local_label_name (abfd, name)
02215      bfd *abfd;
02216      const char *name;
02217 {
02218   const char *colpos;
02219   int digits;
02220 
02221   /* Also include the default local-label definition.  */
02222   if (_bfd_elf_is_local_label_name (abfd, name))
02223     return TRUE;
02224 
02225   if (*name != 'L')
02226     return FALSE;
02227 
02228   /* If there's no ":", or more than one, it's not a local symbol.  */
02229   colpos = strchr (name, ':');
02230   if (colpos == NULL || strchr (colpos + 1, ':') != NULL)
02231     return FALSE;
02232 
02233   /* Check that there are remaining characters and that they are digits.  */
02234   if (colpos[1] == 0)
02235     return FALSE;
02236 
02237   digits = strspn (colpos + 1, "0123456789");
02238   return digits != 0 && colpos[1 + digits] == 0;
02239 }
02240 
02241 /* We get rid of the register section here.  */
02242 
02243 bfd_boolean
02244 mmix_elf_final_link (abfd, info)
02245      bfd *abfd;
02246      struct bfd_link_info *info;
02247 {
02248   /* We never output a register section, though we create one for
02249      temporary measures.  Check that nobody entered contents into it.  */
02250   asection *reg_section;
02251 
02252   reg_section = bfd_get_section_by_name (abfd, MMIX_REG_SECTION_NAME);
02253 
02254   if (reg_section != NULL)
02255     {
02256       /* FIXME: Pass error state gracefully.  */
02257       if (bfd_get_section_flags (abfd, reg_section) & SEC_HAS_CONTENTS)
02258        _bfd_abort (__FILE__, __LINE__, _("Register section has contents\n"));
02259 
02260       /* Really remove the section, if it hasn't already been done.  */
02261       if (!bfd_section_removed_from_list (abfd, reg_section))
02262        {
02263          bfd_section_list_remove (abfd, reg_section);
02264          --abfd->section_count;
02265        }
02266     }
02267 
02268   if (! bfd_elf_final_link (abfd, info))
02269     return FALSE;
02270 
02271   /* Since this section is marked SEC_LINKER_CREATED, it isn't output by
02272      the regular linker machinery.  We do it here, like other targets with
02273      special sections.  */
02274   if (info->base_file != NULL)
02275     {
02276       asection *greg_section
02277        = bfd_get_section_by_name ((bfd *) info->base_file,
02278                                MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
02279       if (!bfd_set_section_contents (abfd,
02280                                  greg_section->output_section,
02281                                  greg_section->contents,
02282                                  (file_ptr) greg_section->output_offset,
02283                                  greg_section->size))
02284        return FALSE;
02285     }
02286   return TRUE;
02287 }
02288 
02289 /* We need to include the maximum size of PUSHJ-stubs in the initial
02290    section size.  This is expected to shrink during linker relaxation.  */
02291 
02292 static void
02293 mmix_set_relaxable_size (abfd, sec, ptr)
02294      bfd *abfd ATTRIBUTE_UNUSED;
02295      asection *sec;
02296      void *ptr;
02297 {
02298   struct bfd_link_info *info = ptr;
02299 
02300   /* Make sure we only do this for section where we know we want this,
02301      otherwise we might end up resetting the size of COMMONs.  */
02302   if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0)
02303     return;
02304 
02305   sec->rawsize = sec->size;
02306   sec->size += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
02307               * MAX_PUSHJ_STUB_SIZE);
02308 
02309   /* For use in relocatable link, we start with a max stubs size.  See
02310      mmix_elf_relax_section.  */
02311   if (info->relocatable && sec->output_section)
02312     mmix_elf_section_data (sec->output_section)->pjs.stubs_size_sum
02313       += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
02314          * MAX_PUSHJ_STUB_SIZE);
02315 }
02316 
02317 /* Initialize stuff for the linker-generated GREGs to match
02318    R_MMIX_BASE_PLUS_OFFSET relocs seen by the linker.  */
02319 
02320 bfd_boolean
02321 _bfd_mmix_before_linker_allocation (abfd, info)
02322      bfd *abfd ATTRIBUTE_UNUSED;
02323      struct bfd_link_info *info;
02324 {
02325   asection *bpo_gregs_section;
02326   bfd *bpo_greg_owner;
02327   struct bpo_greg_section_info *gregdata;
02328   size_t n_gregs;
02329   bfd_vma gregs_size;
02330   size_t i;
02331   size_t *bpo_reloc_indexes;
02332   bfd *ibfd;
02333 
02334   /* Set the initial size of sections.  */
02335   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
02336     bfd_map_over_sections (ibfd, mmix_set_relaxable_size, info);
02337 
02338   /* The bpo_greg_owner bfd is supposed to have been set by
02339      mmix_elf_check_relocs when the first R_MMIX_BASE_PLUS_OFFSET is seen.
02340      If there is no such object, there was no R_MMIX_BASE_PLUS_OFFSET.  */
02341   bpo_greg_owner = (bfd *) info->base_file;
02342   if (bpo_greg_owner == NULL)
02343     return TRUE;
02344 
02345   bpo_gregs_section
02346     = bfd_get_section_by_name (bpo_greg_owner,
02347                             MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
02348 
02349   if (bpo_gregs_section == NULL)
02350     return TRUE;
02351 
02352   /* We use the target-data handle in the ELF section data.  */
02353   gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
02354   if (gregdata == NULL)
02355     return FALSE;
02356 
02357   n_gregs = gregdata->n_bpo_relocs;
02358   gregdata->n_allocated_bpo_gregs = n_gregs;
02359 
02360   /* When this reaches zero during relaxation, all entries have been
02361      filled in and the size of the linker gregs can be calculated.  */
02362   gregdata->n_remaining_bpo_relocs_this_relaxation_round = n_gregs;
02363 
02364   /* Set the zeroth-order estimate for the GREGs size.  */
02365   gregs_size = n_gregs * 8;
02366 
02367   if (!bfd_set_section_size (bpo_greg_owner, bpo_gregs_section, gregs_size))
02368     return FALSE;
02369 
02370   /* Allocate and set up the GREG arrays.  They're filled in at relaxation
02371      time.  Note that we must use the max number ever noted for the array,
02372      since the index numbers were created before GC.  */
02373   gregdata->reloc_request
02374     = bfd_zalloc (bpo_greg_owner,
02375                 sizeof (struct bpo_reloc_request)
02376                 * gregdata->n_max_bpo_relocs);
02377 
02378   gregdata->bpo_reloc_indexes
02379     = bpo_reloc_indexes
02380     = bfd_alloc (bpo_greg_owner,
02381                gregdata->n_max_bpo_relocs
02382                * sizeof (size_t));
02383   if (bpo_reloc_indexes == NULL)
02384     return FALSE;
02385 
02386   /* The default order is an identity mapping.  */
02387   for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
02388     {
02389       bpo_reloc_indexes[i] = i;
02390       gregdata->reloc_request[i].bpo_reloc_no = i;
02391     }
02392 
02393   return TRUE;
02394 }
02395 
02396 /* Fill in contents in the linker allocated gregs.  Everything is
02397    calculated at this point; we just move the contents into place here.  */
02398 
02399 bfd_boolean
02400 _bfd_mmix_after_linker_allocation (abfd, link_info)
02401      bfd *abfd ATTRIBUTE_UNUSED;
02402      struct bfd_link_info *link_info;
02403 {
02404   asection *bpo_gregs_section;
02405   bfd *bpo_greg_owner;
02406   struct bpo_greg_section_info *gregdata;
02407   size_t n_gregs;
02408   size_t i, j;
02409   size_t lastreg;
02410   bfd_byte *contents;
02411 
02412   /* The bpo_greg_owner bfd is supposed to have been set by mmix_elf_check_relocs
02413      when the first R_MMIX_BASE_PLUS_OFFSET is seen.  If there is no such
02414      object, there was no R_MMIX_BASE_PLUS_OFFSET.  */
02415   bpo_greg_owner = (bfd *) link_info->base_file;
02416   if (bpo_greg_owner == NULL)
02417     return TRUE;
02418 
02419   bpo_gregs_section
02420     = bfd_get_section_by_name (bpo_greg_owner,
02421                             MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
02422 
02423   /* This can't happen without DSO handling.  When DSOs are handled
02424      without any R_MMIX_BASE_PLUS_OFFSET seen, there will be no such
02425      section.  */
02426   if (bpo_gregs_section == NULL)
02427     return TRUE;
02428 
02429   /* We use the target-data handle in the ELF section data.  */
02430 
02431   gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
02432   if (gregdata == NULL)
02433     return FALSE;
02434 
02435   n_gregs = gregdata->n_allocated_bpo_gregs;
02436 
02437   bpo_gregs_section->contents
02438     = contents = bfd_alloc (bpo_greg_owner, bpo_gregs_section->size);
02439   if (contents == NULL)
02440     return FALSE;
02441 
02442   /* Sanity check: If these numbers mismatch, some relocation has not been
02443      accounted for and the rest of gregdata is probably inconsistent.
02444      It's a bug, but it's more helpful to identify it than segfaulting
02445      below.  */
02446   if (gregdata->n_remaining_bpo_relocs_this_relaxation_round
02447       != gregdata->n_bpo_relocs)
02448     {
02449       (*_bfd_error_handler)
02450        (_("Internal inconsistency: remaining %u != max %u.\n\
02451   Please report this bug."),
02452         gregdata->n_remaining_bpo_relocs_this_relaxation_round,
02453         gregdata->n_bpo_relocs);
02454       return FALSE;
02455     }
02456 
02457   for (lastreg = 255, i = 0, j = 0; j < n_gregs; i++)
02458     if (gregdata->reloc_request[i].regindex != lastreg)
02459       {
02460        bfd_put_64 (bpo_greg_owner, gregdata->reloc_request[i].value,
02461                   contents + j * 8);
02462        lastreg = gregdata->reloc_request[i].regindex;
02463        j++;
02464       }
02465 
02466   return TRUE;
02467 }
02468 
02469 /* Sort valid relocs to come before non-valid relocs, then on increasing
02470    value.  */
02471 
02472 static int
02473 bpo_reloc_request_sort_fn (p1, p2)
02474      const PTR p1;
02475      const PTR p2;
02476 {
02477   const struct bpo_reloc_request *r1 = (const struct bpo_reloc_request *) p1;
02478   const struct bpo_reloc_request *r2 = (const struct bpo_reloc_request *) p2;
02479 
02480   /* Primary function is validity; non-valid relocs sorted after valid
02481      ones.  */
02482   if (r1->valid != r2->valid)
02483     return r2->valid - r1->valid;
02484 
02485   /* Then sort on value.  Don't simplify and return just the difference of
02486      the values: the upper bits of the 64-bit value would be truncated on
02487      a host with 32-bit ints.  */
02488   if (r1->value != r2->value)
02489     return r1->value > r2->value ? 1 : -1;
02490 
02491   /* As a last re-sort, use the relocation number, so we get a stable
02492      sort.  The *addresses* aren't stable since items are swapped during
02493      sorting.  It depends on the qsort implementation if this actually
02494      happens.  */
02495   return r1->bpo_reloc_no > r2->bpo_reloc_no
02496     ? 1 : (r1->bpo_reloc_no < r2->bpo_reloc_no ? -1 : 0);
02497 }
02498 
02499 /* For debug use only.  Dumps the global register allocations resulting
02500    from base-plus-offset relocs.  */
02501 
02502 void
02503 mmix_dump_bpo_gregs (link_info, pf)
02504      struct bfd_link_info *link_info;
02505      bfd_error_handler_type pf;
02506 {
02507   bfd *bpo_greg_owner;
02508   asection *bpo_gregs_section;
02509   struct bpo_greg_section_info *gregdata;
02510   unsigned int i;
02511 
02512   if (link_info == NULL || link_info->base_file == NULL)
02513     return;
02514 
02515   bpo_greg_owner = (bfd *) link_info->base_file;
02516 
02517   bpo_gregs_section
02518     = bfd_get_section_by_name (bpo_greg_owner,
02519                             MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
02520 
02521   if (bpo_gregs_section == NULL)
02522     return;
02523 
02524   gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
02525   if (gregdata == NULL)
02526     return;
02527 
02528   if (pf == NULL)
02529     pf = _bfd_error_handler;
02530 
02531   /* These format strings are not translated.  They are for debug purposes
02532      only and never displayed to an end user.  Should they escape, we
02533      surely want them in original.  */
02534   (*pf) (" n_bpo_relocs: %u\n n_max_bpo_relocs: %u\n n_remain...round: %u\n\
02535  n_allocated_bpo_gregs: %u\n", gregdata->n_bpo_relocs,
02536      gregdata->n_max_bpo_relocs,
02537      gregdata->n_remaining_bpo_relocs_this_relaxation_round,
02538      gregdata->n_allocated_bpo_gregs);
02539 
02540   if (gregdata->reloc_request)
02541     for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
02542       (*pf) ("%4u (%4u)/%4u#%u: 0x%08lx%08lx  r: %3u o: %3u\n",
02543             i,
02544             (gregdata->bpo_reloc_indexes != NULL
02545              ? gregdata->bpo_reloc_indexes[i] : (size_t) -1),
02546             gregdata->reloc_request[i].bpo_reloc_no,
02547             gregdata->reloc_request[i].valid,
02548 
02549             (unsigned long) (gregdata->reloc_request[i].value >> 32),
02550             (unsigned long) gregdata->reloc_request[i].value,
02551             gregdata->reloc_request[i].regindex,
02552             gregdata->reloc_request[i].offset);
02553 }
02554 
02555 /* This links all R_MMIX_BASE_PLUS_OFFSET relocs into a special array, and
02556    when the last such reloc is done, an index-array is sorted according to
02557    the values and iterated over to produce register numbers (indexed by 0
02558    from the first allocated register number) and offsets for use in real
02559    relocation.
02560 
02561    PUSHJ stub accounting is also done here.
02562 
02563    Symbol- and reloc-reading infrastructure copied from elf-m10200.c.  */
02564 
02565 static bfd_boolean
02566 mmix_elf_relax_section (abfd, sec, link_info, again)
02567      bfd *abfd;
02568      asection *sec;
02569      struct bfd_link_info *link_info;
02570      bfd_boolean *again;
02571 {
02572   Elf_Internal_Shdr *symtab_hdr;
02573   Elf_Internal_Rela *internal_relocs;
02574   Elf_Internal_Rela *irel, *irelend;
02575   asection *bpo_gregs_section = NULL;
02576   struct bpo_greg_section_info *gregdata;
02577   struct bpo_reloc_section_info *bpodata
02578     = mmix_elf_section_data (sec)->bpo.reloc;
02579   /* The initialization is to quiet compiler warnings.  The value is to
02580      spot a missing actual initialization.  */
02581   size_t bpono = (size_t) -1;
02582   size_t pjsno = 0;
02583   bfd *bpo_greg_owner;
02584   Elf_Internal_Sym *isymbuf = NULL;
02585   bfd_size_type size = sec->rawsize ? sec->rawsize : sec->size;
02586 
02587   mmix_elf_section_data (sec)->pjs.stubs_size_sum = 0;
02588 
02589   /* Assume nothing changes.  */
02590   *again = FALSE;
02591 
02592   /* We don't have to do anything if this section does not have relocs, or
02593      if this is not a code section.  */
02594   if ((sec->flags & SEC_RELOC) == 0
02595       || sec->reloc_count == 0
02596       || (sec->flags & SEC_CODE) == 0
02597       || (sec->flags & SEC_LINKER_CREATED) != 0
02598       /* If no R_MMIX_BASE_PLUS_OFFSET relocs and no PUSHJ-stub relocs,
02599          then nothing to do.  */
02600       || (bpodata == NULL
02601          && mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0))
02602     return TRUE;
02603 
02604   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
02605 
02606   bpo_greg_owner = (bfd *) link_info->base_file;
02607 
02608   if (bpodata != NULL)
02609     {
02610       bpo_gregs_section = bpodata->bpo_greg_section;
02611       gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
02612       bpono = bpodata->first_base_plus_offset_reloc;
02613     }
02614   else
02615     gregdata = NULL;
02616 
02617   /* Get a copy of the native relocations.  */
02618   internal_relocs
02619     = _bfd_elf_link_read_relocs (abfd, sec, (PTR) NULL,
02620                              (Elf_Internal_Rela *) NULL,
02621                              link_info->keep_memory);
02622   if (internal_relocs == NULL)
02623     goto error_return;
02624 
02625   /* Walk through them looking for relaxing opportunities.  */
02626   irelend = internal_relocs + sec->reloc_count;
02627   for (irel = internal_relocs; irel < irelend; irel++)
02628     {
02629       bfd_vma symval;
02630       struct elf_link_hash_entry *h = NULL;
02631 
02632       /* We only process two relocs.  */
02633       if (ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_BASE_PLUS_OFFSET
02634          && ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_PUSHJ_STUBBABLE)
02635        continue;
02636 
02637       /* We process relocs in a distinctly different way when this is a
02638         relocatable link (for one, we don't look at symbols), so we avoid
02639         mixing its code with that for the "normal" relaxation.  */
02640       if (link_info->relocatable)
02641        {
02642          /* The only transformation in a relocatable link is to generate
02643             a full stub at the location of the stub calculated for the
02644             input section, if the relocated stub location, the end of the
02645             output section plus earlier stubs, cannot be reached.  Thus
02646             relocatable linking can only lead to worse code, but it still
02647             works.  */
02648          if (ELF64_R_TYPE (irel->r_info) == R_MMIX_PUSHJ_STUBBABLE)
02649            {
02650              /* If we can reach the end of the output-section and beyond
02651                any current stubs, then we don't need a stub for this
02652                reloc.  The relaxed order of output stub allocation may
02653                not exactly match the straightforward order, so we always
02654                assume presence of output stubs, which will allow
02655                relaxation only on relocations indifferent to the
02656                presence of output stub allocations for other relocations
02657                and thus the order of output stub allocation.  */
02658              if (bfd_check_overflow (complain_overflow_signed,
02659                                   19,
02660                                   0,
02661                                   bfd_arch_bits_per_address (abfd),
02662                                   /* Output-stub location.  */
02663                                   sec->output_section->rawsize
02664                                   + (mmix_elf_section_data (sec
02665                                                         ->output_section)
02666                                     ->pjs.stubs_size_sum)
02667                                   /* Location of this PUSHJ reloc.  */
02668                                   - (sec->output_offset + irel->r_offset)
02669                                   /* Don't count *this* stub twice.  */
02670                                   - (mmix_elf_section_data (sec)
02671                                     ->pjs.stub_size[pjsno]
02672                                     + MAX_PUSHJ_STUB_SIZE))
02673                 == bfd_reloc_ok)
02674               mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
02675 
02676              mmix_elf_section_data (sec)->pjs.stubs_size_sum
02677               += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
02678 
02679              pjsno++;
02680            }
02681 
02682          continue;
02683        }
02684 
02685       /* Get the value of the symbol referred to by the reloc.  */
02686       if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
02687        {
02688          /* A local symbol.  */
02689          Elf_Internal_Sym *isym;
02690          asection *sym_sec;
02691 
02692          /* Read this BFD's local symbols if we haven't already.  */
02693          if (isymbuf == NULL)
02694            {
02695              isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
02696              if (isymbuf == NULL)
02697               isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
02698                                           symtab_hdr->sh_info, 0,
02699                                           NULL, NULL, NULL);
02700              if (isymbuf == 0)
02701               goto error_return;
02702            }
02703 
02704          isym = isymbuf + ELF64_R_SYM (irel->r_info);
02705          if (isym->st_shndx == SHN_UNDEF)
02706            sym_sec = bfd_und_section_ptr;
02707          else if (isym->st_shndx == SHN_ABS)
02708            sym_sec = bfd_abs_section_ptr;
02709          else if (isym->st_shndx == SHN_COMMON)
02710            sym_sec = bfd_com_section_ptr;
02711          else
02712            sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
02713          symval = (isym->st_value
02714                   + sym_sec->output_section->vma
02715                   + sym_sec->output_offset);
02716        }
02717       else
02718        {
02719          unsigned long indx;
02720 
02721          /* An external symbol.  */
02722          indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
02723          h = elf_sym_hashes (abfd)[indx];
02724          BFD_ASSERT (h != NULL);
02725          if (h->root.type != bfd_link_hash_defined
02726              && h->root.type != bfd_link_hash_defweak)
02727            {
02728              /* This appears to be a reference to an undefined symbol.  Just
02729                ignore it--it will be caught by the regular reloc processing.
02730                We need to keep BPO reloc accounting consistent, though
02731                else we'll abort instead of emitting an error message.  */
02732              if (ELF64_R_TYPE (irel->r_info) == R_MMIX_BASE_PLUS_OFFSET
02733                 && gregdata != NULL)
02734               {
02735                 gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
02736                 bpono++;
02737               }
02738              continue;
02739            }
02740 
02741          symval = (h->root.u.def.value
02742                   + h->root.u.def.section->output_section->vma
02743                   + h->root.u.def.section->output_offset);
02744        }
02745 
02746       if (ELF64_R_TYPE (irel->r_info) == (int) R_MMIX_PUSHJ_STUBBABLE)
02747        {
02748          bfd_vma value = symval + irel->r_addend;
02749          bfd_vma dot
02750            = (sec->output_section->vma
02751               + sec->output_offset
02752               + irel->r_offset);
02753          bfd_vma stubaddr
02754            = (sec->output_section->vma
02755               + sec->output_offset
02756               + size
02757               + mmix_elf_section_data (sec)->pjs.stubs_size_sum);
02758 
02759          if ((value & 3) == 0
02760              && bfd_check_overflow (complain_overflow_signed,
02761                                  19,
02762                                  0,
02763                                  bfd_arch_bits_per_address (abfd),
02764                                  value - dot
02765                                  - (value > dot
02766                                    ? mmix_elf_section_data (sec)
02767                                    ->pjs.stub_size[pjsno]
02768                                    : 0))
02769              == bfd_reloc_ok)
02770            /* If the reloc fits, no stub is needed.  */
02771            mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
02772          else
02773            /* Maybe we can get away with just a JMP insn?  */
02774            if ((value & 3) == 0
02775               && bfd_check_overflow (complain_overflow_signed,
02776                                    27,
02777                                    0,
02778                                    bfd_arch_bits_per_address (abfd),
02779                                    value - stubaddr
02780                                    - (value > dot
02781                                      ? mmix_elf_section_data (sec)
02782                                      ->pjs.stub_size[pjsno] - 4
02783                                      : 0))
02784               == bfd_reloc_ok)
02785              /* Yep, account for a stub consisting of a single JMP insn.  */
02786              mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 4;
02787          else
02788            /* Nope, go for the full insn stub.  It doesn't seem useful to
02789               emit the intermediate sizes; those will only be useful for
02790               a >64M program assuming contiguous code.  */
02791            mmix_elf_section_data (sec)->pjs.stub_size[pjsno]
02792              = MAX_PUSHJ_STUB_SIZE;
02793 
02794          mmix_elf_section_data (sec)->pjs.stubs_size_sum
02795            += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
02796          pjsno++;
02797          continue;
02798        }
02799 
02800       /* We're looking at a R_MMIX_BASE_PLUS_OFFSET reloc.  */
02801 
02802       gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono]].value
02803        = symval + irel->r_addend;
02804       gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono++]].valid = TRUE;
02805       gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
02806     }
02807 
02808   /* Check if that was the last BPO-reloc.  If so, sort the values and
02809      calculate how many registers we need to cover them.  Set the size of
02810      the linker gregs, and if the number of registers changed, indicate
02811      that we need to relax some more because we have more work to do.  */
02812   if (gregdata != NULL
02813       && gregdata->n_remaining_bpo_relocs_this_relaxation_round == 0)
02814     {
02815       size_t i;
02816       bfd_vma prev_base;
02817       size_t regindex;
02818 
02819       /* First, reset the remaining relocs for the next round.  */
02820       gregdata->n_remaining_bpo_relocs_this_relaxation_round
02821        = gregdata->n_bpo_relocs;
02822 
02823       qsort ((PTR) gregdata->reloc_request,
02824             gregdata->n_max_bpo_relocs,
02825             sizeof (struct bpo_reloc_request),
02826             bpo_reloc_request_sort_fn);
02827 
02828       /* Recalculate indexes.  When we find a change (however unlikely
02829         after the initial iteration), we know we need to relax again,
02830         since items in the GREG-array are sorted by increasing value and
02831         stored in the relaxation phase.  */
02832       for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
02833        if (gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
02834            != i)
02835          {
02836            gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
02837              = i;
02838            *again = TRUE;
02839          }
02840 
02841       /* Allocate register numbers (indexing from 0).  Stop at the first
02842         non-valid reloc.  */
02843       for (i = 0, regindex = 0, prev_base = gregdata->reloc_request[0].value;
02844           i < gregdata->n_bpo_relocs;
02845           i++)
02846        {
02847          if (gregdata->reloc_request[i].value > prev_base + 255)
02848            {
02849              regindex++;
02850              prev_base = gregdata->reloc_request[i].value;
02851            }
02852          gregdata->reloc_request[i].regindex = regindex;
02853          gregdata->reloc_request[i].offset
02854            = gregdata->reloc_request[i].value - prev_base;
02855        }
02856 
02857       /* If it's not the same as the last time, we need to relax again,
02858         because the size of the section has changed.  I'm not sure we
02859         actually need to do any adjustments since the shrinking happens
02860         at the start of this section, but better safe than sorry.  */
02861       if (gregdata->n_allocated_bpo_gregs != regindex + 1)
02862        {
02863          gregdata->n_allocated_bpo_gregs = regindex + 1;
02864          *again = TRUE;
02865        }
02866 
02867       bpo_gregs_section->size = (regindex + 1) * 8;
02868     }
02869 
02870   if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
02871     {
02872       if (! link_info->keep_memory)
02873        free (isymbuf);
02874       else
02875        {
02876          /* Cache the symbols for elf_link_input_bfd.  */
02877          symtab_hdr->contents = (unsigned char *) isymbuf;
02878        }
02879     }
02880 
02881   if (internal_relocs != NULL
02882       && elf_section_data (sec)->relocs != internal_relocs)
02883     free (internal_relocs);
02884 
02885   if (sec->size < size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
02886     abort ();
02887 
02888   if (sec->size > size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
02889     {
02890       sec->size = size + mmix_elf_section_data (sec)->pjs.stubs_size_sum;
02891       *again = TRUE;
02892     }
02893 
02894   return TRUE;
02895 
02896  error_return:
02897   if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
02898     free (isymbuf);
02899   if (internal_relocs != NULL
02900       && elf_section_data (sec)->relocs != internal_relocs)
02901     free (internal_relocs);
02902   return FALSE;
02903 }
02904 
02905 #define ELF_ARCH            bfd_arch_mmix
02906 #define ELF_MACHINE_CODE    EM_MMIX
02907 
02908 /* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
02909    However, that's too much for something somewhere in the linker part of
02910    BFD; perhaps the start-address has to be a non-zero multiple of this
02911    number, or larger than this number.  The symptom is that the linker
02912    complains: "warning: allocated section `.text' not in segment".  We
02913    settle for 64k; the page-size used in examples is 8k.
02914    #define ELF_MAXPAGESIZE 0x10000
02915 
02916    Unfortunately, this causes excessive padding in the supposedly small
02917    for-education programs that are the expected usage (where people would
02918    inspect output).  We stick to 256 bytes just to have *some* default
02919    alignment.  */
02920 #define ELF_MAXPAGESIZE 0x100
02921 
02922 #define TARGET_BIG_SYM             bfd_elf64_mmix_vec
02923 #define TARGET_BIG_NAME            "elf64-mmix"
02924 
02925 #define elf_info_to_howto_rel             NULL
02926 #define elf_info_to_howto          mmix_info_to_howto_rela
02927 #define elf_backend_relocate_section      mmix_elf_relocate_section
02928 #define elf_backend_gc_mark_hook   mmix_elf_gc_mark_hook
02929 #define elf_backend_gc_sweep_hook  mmix_elf_gc_sweep_hook
02930 
02931 #define elf_backend_link_output_symbol_hook \
02932        mmix_elf_link_output_symbol_hook
02933 #define elf_backend_add_symbol_hook       mmix_elf_add_symbol_hook
02934 
02935 #define elf_backend_check_relocs   mmix_elf_check_relocs
02936 #define elf_backend_symbol_processing     mmix_elf_symbol_processing
02937 #define elf_backend_omit_section_dynsym \
02938   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
02939 
02940 #define bfd_elf64_bfd_is_local_label_name \
02941        mmix_elf_is_local_label_name
02942 
02943 #define elf_backend_may_use_rel_p  0
02944 #define elf_backend_may_use_rela_p 1
02945 #define elf_backend_default_use_rela_p    1
02946 
02947 #define elf_backend_can_gc_sections       1
02948 #define elf_backend_section_from_bfd_section \
02949        mmix_elf_section_from_bfd_section
02950 
02951 #define bfd_elf64_new_section_hook mmix_elf_new_section_hook
02952 #define bfd_elf64_bfd_final_link   mmix_elf_final_link
02953 #define bfd_elf64_bfd_relax_section       mmix_elf_relax_section
02954 
02955 #include "elf64-target.h"