Home | History | Annotate | Line # | Download | only in bfd
      1 /* MIPS-specific support for ELF
      2    Copyright (C) 1993-2026 Free Software Foundation, Inc.
      3 
      4    Most of the information added by Ian Lance Taylor, Cygnus Support,
      5    <ian (at) cygnus.com>.
      6    N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
      7    <mark (at) codesourcery.com>
      8    Traditional MIPS targets support added by Koundinya.K, Dansk Data
      9    Elektronik & Operations Research Group. <kk (at) ddeorg.soft.net>
     10 
     11    This file is part of BFD, the Binary File Descriptor library.
     12 
     13    This program is free software; you can redistribute it and/or modify
     14    it under the terms of the GNU General Public License as published by
     15    the Free Software Foundation; either version 3 of the License, or
     16    (at your option) any later version.
     17 
     18    This program is distributed in the hope that it will be useful,
     19    but WITHOUT ANY WARRANTY; without even the implied warranty of
     20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     21    GNU General Public License for more details.
     22 
     23    You should have received a copy of the GNU General Public License
     24    along with this program; if not, write to the Free Software
     25    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     26    MA 02110-1301, USA.  */
     27 
     28 
     29 /* This file handles functionality common to the different MIPS ABI's.  */
     30 
     31 #include "sysdep.h"
     32 #include "bfd.h"
     33 #include "libbfd.h"
     34 #include "libiberty.h"
     35 #include "elf-bfd.h"
     36 #include "ecoff-bfd.h"
     37 #include "elfxx-mips.h"
     38 #include "elf/mips.h"
     39 #include "elf-vxworks.h"
     40 #include "dwarf2.h"
     41 
     42 /* Get the ECOFF swapping routines.  */
     43 #include "coff/sym.h"
     44 #include "coff/symconst.h"
     45 #include "coff/ecoff.h"
     46 #include "coff/mips.h"
     47 
     48 #include "hashtab.h"
     49 
     50 /* Types of TLS GOT entry.  */
     51 enum mips_got_tls_type {
     52   GOT_TLS_NONE,
     53   GOT_TLS_GD,
     54   GOT_TLS_LDM,
     55   GOT_TLS_IE
     56 };
     57 
     58 /* This structure is used to hold information about one GOT entry.
     59    There are four types of entry:
     60 
     61       (1) an absolute address
     62 	    requires: abfd == NULL
     63 	    fields: d.address
     64 
     65       (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
     66 	    requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
     67 	    fields: abfd, symndx, d.addend, tls_type
     68 
     69       (3) a SYMBOL address, where SYMBOL is not local to an input bfd
     70 	    requires: abfd != NULL, symndx == -1
     71 	    fields: d.h, tls_type
     72 
     73       (4) a TLS LDM slot
     74 	    requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
     75 	    fields: none; there's only one of these per GOT.  */
     76 struct mips_got_entry
     77 {
     78   /* One input bfd that needs the GOT entry.  */
     79   bfd *abfd;
     80   /* The index of the symbol, as stored in the relocation r_info, if
     81      we have a local symbol; -1 otherwise.  */
     82   long symndx;
     83   union
     84   {
     85     /* If abfd == NULL, an address that must be stored in the got.  */
     86     bfd_vma address;
     87     /* If abfd != NULL && symndx != -1, the addend of the relocation
     88        that should be added to the symbol value.  */
     89     bfd_vma addend;
     90     /* If abfd != NULL && symndx == -1, the hash table entry
     91        corresponding to a symbol in the GOT.  The symbol's entry
     92        is in the local area if h->global_got_area is GGA_NONE,
     93        otherwise it is in the global area.  */
     94     struct mips_elf_link_hash_entry *h;
     95   } d;
     96 
     97   /* The TLS type of this GOT entry.  An LDM GOT entry will be a local
     98      symbol entry with r_symndx == 0.  */
     99   unsigned char tls_type;
    100 
    101   /* True if we have filled in the GOT contents for a TLS entry,
    102      and created the associated relocations.  */
    103   unsigned char tls_initialized;
    104 
    105   /* The offset from the beginning of the .got section to the entry
    106      corresponding to this symbol+addend.  If it's a global symbol
    107      whose offset is yet to be decided, it's going to be -1.  */
    108   long gotidx;
    109 };
    110 
    111 /* This structure represents a GOT page reference from an input bfd.
    112    Each instance represents a symbol + ADDEND, where the representation
    113    of the symbol depends on whether it is local to the input bfd.
    114    If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
    115    Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
    116 
    117    Page references with SYMNDX >= 0 always become page references
    118    in the output.  Page references with SYMNDX < 0 only become page
    119    references if the symbol binds locally; in other cases, the page
    120    reference decays to a global GOT reference.  */
    121 struct mips_got_page_ref
    122 {
    123   long symndx;
    124   union
    125   {
    126     struct mips_elf_link_hash_entry *h;
    127     bfd *abfd;
    128   } u;
    129   bfd_vma addend;
    130 };
    131 
    132 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
    133    The structures form a non-overlapping list that is sorted by increasing
    134    MIN_ADDEND.  */
    135 struct mips_got_page_range
    136 {
    137   struct mips_got_page_range *next;
    138   bfd_signed_vma min_addend;
    139   bfd_signed_vma max_addend;
    140 };
    141 
    142 /* This structure describes the range of addends that are applied to page
    143    relocations against a given section.  */
    144 struct mips_got_page_entry
    145 {
    146   /* The section that these entries are based on.  */
    147   asection *sec;
    148   /* The ranges for this page entry.  */
    149   struct mips_got_page_range *ranges;
    150   /* The maximum number of page entries needed for RANGES.  */
    151   bfd_vma num_pages;
    152 };
    153 
    154 /* This structure is used to hold .got information when linking.  */
    155 
    156 struct mips_got_info
    157 {
    158   /* The number of global .got entries.  */
    159   unsigned int global_gotno;
    160   /* The number of global .got entries that are in the GGA_RELOC_ONLY area.  */
    161   unsigned int reloc_only_gotno;
    162   /* The number of .got slots used for TLS.  */
    163   unsigned int tls_gotno;
    164   /* The first unused TLS .got entry.  Used only during
    165      mips_elf_initialize_tls_index.  */
    166   unsigned int tls_assigned_gotno;
    167   /* The number of local .got entries, eventually including page entries.  */
    168   unsigned int local_gotno;
    169   /* The maximum number of page entries needed.  */
    170   unsigned int page_gotno;
    171   /* The number of relocations needed for the GOT entries.  */
    172   unsigned int relocs;
    173   /* The first unused local .got entry.  */
    174   unsigned int assigned_low_gotno;
    175   /* The last unused local .got entry.  */
    176   unsigned int assigned_high_gotno;
    177   /* A hash table holding members of the got.  */
    178   struct htab *got_entries;
    179   /* A hash table holding mips_got_page_ref structures.  */
    180   struct htab *got_page_refs;
    181   /* A hash table of mips_got_page_entry structures.  */
    182   struct htab *got_page_entries;
    183   /* In multi-got links, a pointer to the next got (err, rather, most
    184      of the time, it points to the previous got).  */
    185   struct mips_got_info *next;
    186 };
    187 
    188 /* Structure passed when merging bfds' gots.  */
    189 
    190 struct mips_elf_got_per_bfd_arg
    191 {
    192   /* The output bfd.  */
    193   bfd *obfd;
    194   /* The link information.  */
    195   struct bfd_link_info *info;
    196   /* A pointer to the primary got, i.e., the one that's going to get
    197      the implicit relocations from DT_MIPS_LOCAL_GOTNO and
    198      DT_MIPS_GOTSYM.  */
    199   struct mips_got_info *primary;
    200   /* A non-primary got we're trying to merge with other input bfd's
    201      gots.  */
    202   struct mips_got_info *current;
    203   /* The maximum number of got entries that can be addressed with a
    204      16-bit offset.  */
    205   unsigned int max_count;
    206   /* The maximum number of page entries needed by each got.  */
    207   unsigned int max_pages;
    208   /* The total number of global entries which will live in the
    209      primary got and be automatically relocated.  This includes
    210      those not referenced by the primary GOT but included in
    211      the "master" GOT.  */
    212   unsigned int global_count;
    213 };
    214 
    215 /* A structure used to pass information to htab_traverse callbacks
    216    when laying out the GOT.  */
    217 
    218 struct mips_elf_traverse_got_arg
    219 {
    220   struct bfd_link_info *info;
    221   struct mips_got_info *g;
    222   int value;
    223 };
    224 
    225 /* Used to store a REL high-part relocation, such as R_MIPS_HI16 or
    226    R_MIPS_GOT16.  REL is the relocation.  DATA nominally points to the
    227    start of the section  contents, but note that gas may use multiple
    228    chunks of memory for a section (with DATA + [offset,offset+frag_size)
    229    addressing a given frag).  A HI16 reloc might need a different "data"
    230    from a lo16.  OUTPUT_BFD is the output BFD for relocatable output or
    231    a NULL pointer otherwise.  */
    232 
    233 struct mips_hi16
    234 {
    235   struct mips_hi16 *next;
    236   bfd *output_bfd;
    237   bfd_byte *data;
    238   arelent rel;
    239 };
    240 
    241 struct _mips_elf_section_data
    242 {
    243   struct bfd_elf_section_data elf;
    244   union
    245   {
    246     bfd_byte *tdata;
    247   } u;
    248 
    249   struct mips_hi16 *mips_hi16_list;
    250 };
    251 
    252 #define mips_elf_section_data(sec) \
    253   ((struct _mips_elf_section_data *) elf_section_data (sec))
    254 
    255 #define is_mips_elf(bfd)				\
    256   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
    257    && elf_tdata (bfd) != NULL				\
    258    && elf_object_id (bfd) == MIPS_ELF_DATA)
    259 
    260 /* The ABI says that every symbol used by dynamic relocations must have
    261    a global GOT entry.  Among other things, this provides the dynamic
    262    linker with a free, directly-indexed cache.  The GOT can therefore
    263    contain symbols that are not referenced by GOT relocations themselves
    264    (in other words, it may have symbols that are not referenced by things
    265    like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
    266 
    267    GOT relocations are less likely to overflow if we put the associated
    268    GOT entries towards the beginning.  We therefore divide the global
    269    GOT entries into two areas: "normal" and "reloc-only".  Entries in
    270    the first area can be used for both dynamic relocations and GP-relative
    271    accesses, while those in the "reloc-only" area are for dynamic
    272    relocations only.
    273 
    274    These GGA_* ("Global GOT Area") values are organised so that lower
    275    values are more general than higher values.  Also, non-GGA_NONE
    276    values are ordered by the position of the area in the GOT.  */
    277 #define GGA_NORMAL 0
    278 #define GGA_RELOC_ONLY 1
    279 #define GGA_NONE 2
    280 
    281 /* Information about a non-PIC interface to a PIC function.  There are
    282    two ways of creating these interfaces.  The first is to add:
    283 
    284 	lui	$25,%hi(func)
    285 	addiu	$25,$25,%lo(func)
    286 
    287    immediately before a PIC function "func".  The second is to add:
    288 
    289 	lui	$25,%hi(func)
    290 	j	func
    291 	addiu	$25,$25,%lo(func)
    292 
    293    to a separate trampoline section.
    294 
    295    Stubs of the first kind go in a new section immediately before the
    296    target function.  Stubs of the second kind go in a single section
    297    pointed to by the hash table's "strampoline" field.  */
    298 struct mips_elf_la25_stub {
    299   /* The generated section that contains this stub.  */
    300   asection *stub_section;
    301 
    302   /* The offset of the stub from the start of STUB_SECTION.  */
    303   bfd_vma offset;
    304 
    305   /* One symbol for the original function.  Its location is available
    306      in H->root.root.u.def.  */
    307   struct mips_elf_link_hash_entry *h;
    308 };
    309 
    310 /* Macros for populating a mips_elf_la25_stub.  */
    311 
    312 #define LA25_LUI(VAL) (0x3c190000 | (VAL))	/* lui t9,VAL */
    313 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
    314 #define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
    315 #define LA25_ADDIU(VAL) (0x27390000 | (VAL))	/* addiu t9,t9,VAL */
    316 #define LA25_LUI_MICROMIPS(VAL)						\
    317   (0x41b90000 | (VAL))				/* lui t9,VAL */
    318 #define LA25_J_MICROMIPS(VAL)						\
    319   (0xd4000000 | (((VAL) >> 1) & 0x3ffffff))	/* j VAL */
    320 #define LA25_ADDIU_MICROMIPS(VAL)					\
    321   (0x33390000 | (VAL))				/* addiu t9,t9,VAL */
    322 
    323 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
    324    the dynamic symbols.  */
    325 
    326 struct mips_elf_hash_sort_data
    327 {
    328   /* The symbol in the global GOT with the lowest dynamic symbol table
    329      index.  */
    330   struct elf_link_hash_entry *low;
    331   /* The least dynamic symbol table index corresponding to a non-TLS
    332      symbol with a GOT entry.  */
    333   bfd_size_type min_got_dynindx;
    334   /* The greatest dynamic symbol table index corresponding to a symbol
    335      with a GOT entry that is not referenced (e.g., a dynamic symbol
    336      with dynamic relocations pointing to it from non-primary GOTs).  */
    337   bfd_size_type max_unref_got_dynindx;
    338   /* The greatest dynamic symbol table index corresponding to a local
    339      symbol.  */
    340   bfd_size_type max_local_dynindx;
    341   /* The greatest dynamic symbol table index corresponding to an external
    342      symbol without a GOT entry.  */
    343   bfd_size_type max_non_got_dynindx;
    344   /* If non-NULL, output BFD for .MIPS.xhash finalization.  */
    345   bfd *output_bfd;
    346   /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
    347      real final dynindx.  */
    348   bfd_byte *mipsxhash;
    349 };
    350 
    351 /* We make up to two PLT entries if needed, one for standard MIPS code
    352    and one for compressed code, either a MIPS16 or microMIPS one.  We
    353    keep a separate record of traditional lazy-binding stubs, for easier
    354    processing.  */
    355 
    356 struct plt_entry
    357 {
    358   /* Traditional SVR4 stub offset, or -1 if none.  */
    359   bfd_vma stub_offset;
    360 
    361   /* Standard PLT entry offset, or -1 if none.  */
    362   bfd_vma mips_offset;
    363 
    364   /* Compressed PLT entry offset, or -1 if none.  */
    365   bfd_vma comp_offset;
    366 
    367   /* The corresponding .got.plt index, or -1 if none.  */
    368   bfd_vma gotplt_index;
    369 
    370   /* Whether we need a standard PLT entry.  */
    371   unsigned int need_mips : 1;
    372 
    373   /* Whether we need a compressed PLT entry.  */
    374   unsigned int need_comp : 1;
    375 };
    376 
    377 /* The MIPS ELF linker needs additional information for each symbol in
    378    the global hash table.  */
    379 
    380 struct mips_elf_link_hash_entry
    381 {
    382   struct elf_link_hash_entry root;
    383 
    384   /* External symbol information.  */
    385   EXTR esym;
    386 
    387   /* The la25 stub we have created for ths symbol, if any.  */
    388   struct mips_elf_la25_stub *la25_stub;
    389 
    390   /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
    391      this symbol.  */
    392   unsigned int possibly_dynamic_relocs;
    393 
    394   /* If there is a stub that 32 bit functions should use to call this
    395      16 bit function, this points to the section containing the stub.  */
    396   asection *fn_stub;
    397 
    398   /* If there is a stub that 16 bit functions should use to call this
    399      32 bit function, this points to the section containing the stub.  */
    400   asection *call_stub;
    401 
    402   /* This is like the call_stub field, but it is used if the function
    403      being called returns a floating point value.  */
    404   asection *call_fp_stub;
    405 
    406   /* If non-zero, location in .MIPS.xhash to write real final dynindx.  */
    407   bfd_vma mipsxhash_loc;
    408 
    409   /* The highest GGA_* value that satisfies all references to this symbol.  */
    410   unsigned int global_got_area : 2;
    411 
    412   /* True if all GOT relocations against this symbol are for calls.  This is
    413      a looser condition than no_fn_stub below, because there may be other
    414      non-call non-GOT relocations against the symbol.  */
    415   unsigned int got_only_for_calls : 1;
    416 
    417   /* True if one of the relocations described by possibly_dynamic_relocs
    418      is against a readonly section.  */
    419   unsigned int readonly_reloc : 1;
    420 
    421   /* True if there is a relocation against this symbol that must be
    422      resolved by the static linker (in other words, if the relocation
    423      cannot possibly be made dynamic).  */
    424   unsigned int has_static_relocs : 1;
    425 
    426   /* True if we must not create a .MIPS.stubs entry for this symbol.
    427      This is set, for example, if there are relocations related to
    428      taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
    429      See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
    430   unsigned int no_fn_stub : 1;
    431 
    432   /* Whether we need the fn_stub; this is true if this symbol appears
    433      in any relocs other than a 16 bit call.  */
    434   unsigned int need_fn_stub : 1;
    435 
    436   /* True if this symbol is referenced by branch relocations from
    437      any non-PIC input file.  This is used to determine whether an
    438      la25 stub is required.  */
    439   unsigned int has_nonpic_branches : 1;
    440 
    441   /* Does this symbol need a traditional MIPS lazy-binding stub
    442      (as opposed to a PLT entry)?  */
    443   unsigned int needs_lazy_stub : 1;
    444 
    445   /* Does this symbol resolve to a PLT entry?  */
    446   unsigned int use_plt_entry : 1;
    447 };
    448 
    449 /* MIPS ELF linker hash table.  */
    450 
    451 struct mips_elf_link_hash_table
    452 {
    453   struct elf_link_hash_table root;
    454 
    455   /* The number of .rtproc entries.  */
    456   bfd_size_type procedure_count;
    457 
    458   /* The size of the .compact_rel section (if SGI_COMPAT).  */
    459   bfd_size_type compact_rel_size;
    460 
    461   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
    462      is set to the address of __rld_obj_head as in IRIX5 and IRIX6.  */
    463   bool use_rld_obj_head;
    464 
    465   /* The  __rld_map or __rld_obj_head symbol. */
    466   struct elf_link_hash_entry *rld_symbol;
    467 
    468   /* This is set if we see any mips16 stub sections.  */
    469   bool mips16_stubs_seen;
    470 
    471   /* True if we can generate copy relocs and PLTs.  */
    472   bool use_plts_and_copy_relocs;
    473 
    474   /* True if we can only use 32-bit microMIPS instructions.  */
    475   bool insn32;
    476 
    477   /* True if we suppress checks for invalid branches between ISA modes.  */
    478   bool ignore_branch_isa;
    479 
    480   /* True if we are targetting R6 compact branches.  */
    481   bool compact_branches;
    482 
    483   /* True if we already reported the small-data section overflow.  */
    484   bool small_data_overflow_reported;
    485 
    486   /* True if we use the special `__gnu_absolute_zero' symbol.  */
    487   bool use_absolute_zero;
    488 
    489   /* True if we have been configured for a GNU target.  */
    490   bool gnu_target;
    491 
    492   /* Shortcuts to some dynamic sections, or NULL if they are not
    493      being used.  */
    494   asection *srelplt2;
    495   asection *sstubs;
    496 
    497   /* The master GOT information.  */
    498   struct mips_got_info *got_info;
    499 
    500   /* The global symbol in the GOT with the lowest index in the dynamic
    501      symbol table.  */
    502   struct elf_link_hash_entry *global_gotsym;
    503 
    504   /* The size of the PLT header in bytes.  */
    505   bfd_vma plt_header_size;
    506 
    507   /* The size of a standard PLT entry in bytes.  */
    508   bfd_vma plt_mips_entry_size;
    509 
    510   /* The size of a compressed PLT entry in bytes.  */
    511   bfd_vma plt_comp_entry_size;
    512 
    513   /* The offset of the next standard PLT entry to create.  */
    514   bfd_vma plt_mips_offset;
    515 
    516   /* The offset of the next compressed PLT entry to create.  */
    517   bfd_vma plt_comp_offset;
    518 
    519   /* The index of the next .got.plt entry to create.  */
    520   bfd_vma plt_got_index;
    521 
    522   /* The number of functions that need a lazy-binding stub.  */
    523   bfd_vma lazy_stub_count;
    524 
    525   /* The size of a function stub entry in bytes.  */
    526   bfd_vma function_stub_size;
    527 
    528   /* The number of reserved entries at the beginning of the GOT.  */
    529   unsigned int reserved_gotno;
    530 
    531   /* The section used for mips_elf_la25_stub trampolines.
    532      See the comment above that structure for details.  */
    533   asection *strampoline;
    534 
    535   /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
    536      pairs.  */
    537   htab_t la25_stubs;
    538 
    539   /* A function FN (NAME, IS, OS) that creates a new input section
    540      called NAME and links it to output section OS.  If IS is nonnull,
    541      the new section should go immediately before it, otherwise it
    542      should go at the (current) beginning of OS.
    543 
    544      The function returns the new section on success, otherwise it
    545      returns null.  */
    546   asection *(*add_stub_section) (const char *, asection *, asection *);
    547 
    548   /* Is the PLT header compressed?  */
    549   unsigned int plt_header_is_comp : 1;
    550 };
    551 
    552 /* Get the MIPS ELF linker hash table from a link_info structure.  */
    553 
    554 #define mips_elf_hash_table(p) \
    555   ((is_elf_hash_table ((p)->hash)					\
    556     && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA)		\
    557    ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
    558 
    559 /* A structure used to communicate with htab_traverse callbacks.  */
    560 struct mips_htab_traverse_info
    561 {
    562   /* The usual link-wide information.  */
    563   struct bfd_link_info *info;
    564   bfd *output_bfd;
    565 
    566   /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
    567   bool error;
    568 };
    569 
    570 /* MIPS ELF private object data.  */
    571 
    572 struct mips_elf_obj_tdata
    573 {
    574   /* Generic ELF private object data.  */
    575   struct elf_obj_tdata root;
    576 
    577   /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output.  */
    578   bfd *abi_fp_bfd;
    579 
    580   /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output.  */
    581   bfd *abi_msa_bfd;
    582 
    583   /* The abiflags for this object.  */
    584   Elf_Internal_ABIFlags_v0 abiflags;
    585   bool abiflags_valid;
    586 
    587   /* The GOT requirements of input bfds.  */
    588   struct mips_got_info *got;
    589 
    590   /* Used by _bfd_mips_elf_find_nearest_line.  The structure could be
    591      included directly in this one, but there's no point to wasting
    592      the memory just for the infrequently called find_nearest_line.  */
    593   struct mips_elf_find_line *find_line_info;
    594 
    595   /* An array of stub sections indexed by symbol number.  */
    596   asection **local_stubs;
    597   asection **local_call_stubs;
    598 
    599   /* The Irix 5 support uses two virtual sections, which represent
    600      text/data symbols defined in dynamic objects.  */
    601   asection *elf_data_section;
    602   asection *elf_text_section;
    603 };
    604 
    605 /* Get MIPS ELF private object data from BFD's tdata.  */
    606 
    607 #define mips_elf_tdata(bfd) \
    608   ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
    609 
    610 #define TLS_RELOC_P(r_type) \
    611   (r_type == R_MIPS_TLS_DTPMOD32		\
    612    || r_type == R_MIPS_TLS_DTPMOD64		\
    613    || r_type == R_MIPS_TLS_DTPREL32		\
    614    || r_type == R_MIPS_TLS_DTPREL64		\
    615    || r_type == R_MIPS_TLS_GD			\
    616    || r_type == R_MIPS_TLS_LDM			\
    617    || r_type == R_MIPS_TLS_DTPREL_HI16		\
    618    || r_type == R_MIPS_TLS_DTPREL_LO16		\
    619    || r_type == R_MIPS_TLS_GOTTPREL		\
    620    || r_type == R_MIPS_TLS_TPREL32		\
    621    || r_type == R_MIPS_TLS_TPREL64		\
    622    || r_type == R_MIPS_TLS_TPREL_HI16		\
    623    || r_type == R_MIPS_TLS_TPREL_LO16		\
    624    || r_type == R_MIPS16_TLS_GD			\
    625    || r_type == R_MIPS16_TLS_LDM		\
    626    || r_type == R_MIPS16_TLS_DTPREL_HI16	\
    627    || r_type == R_MIPS16_TLS_DTPREL_LO16	\
    628    || r_type == R_MIPS16_TLS_GOTTPREL		\
    629    || r_type == R_MIPS16_TLS_TPREL_HI16		\
    630    || r_type == R_MIPS16_TLS_TPREL_LO16		\
    631    || r_type == R_MICROMIPS_TLS_GD		\
    632    || r_type == R_MICROMIPS_TLS_LDM		\
    633    || r_type == R_MICROMIPS_TLS_DTPREL_HI16	\
    634    || r_type == R_MICROMIPS_TLS_DTPREL_LO16	\
    635    || r_type == R_MICROMIPS_TLS_GOTTPREL	\
    636    || r_type == R_MICROMIPS_TLS_TPREL_HI16	\
    637    || r_type == R_MICROMIPS_TLS_TPREL_LO16)
    638 
    639 /* Structure used to pass information to mips_elf_output_extsym.  */
    640 
    641 struct extsym_info
    642 {
    643   bfd *abfd;
    644   struct bfd_link_info *info;
    645   struct ecoff_debug_info *debug;
    646   const struct ecoff_debug_swap *swap;
    647   bool failed;
    648 };
    649 
    650 /* The names of the runtime procedure table symbols used on IRIX5.  */
    651 
    652 static const char * const mips_elf_dynsym_rtproc_names[] =
    653 {
    654   "_procedure_table",
    655   "_procedure_string_table",
    656   "_procedure_table_size",
    657   NULL
    658 };
    659 
    660 /* These structures are used to generate the .compact_rel section on
    661    IRIX5.  */
    662 
    663 typedef struct
    664 {
    665   unsigned long id1;		/* Always one?  */
    666   unsigned long num;		/* Number of compact relocation entries.  */
    667   unsigned long id2;		/* Always two?  */
    668   unsigned long offset;		/* The file offset of the first relocation.  */
    669   unsigned long reserved0;	/* Zero?  */
    670   unsigned long reserved1;	/* Zero?  */
    671 } Elf32_compact_rel;
    672 
    673 typedef struct
    674 {
    675   bfd_byte id1[4];
    676   bfd_byte num[4];
    677   bfd_byte id2[4];
    678   bfd_byte offset[4];
    679   bfd_byte reserved0[4];
    680   bfd_byte reserved1[4];
    681 } Elf32_External_compact_rel;
    682 
    683 typedef struct
    684 {
    685   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
    686   unsigned int rtype : 4;	/* Relocation types. See below.  */
    687   unsigned int dist2to : 8;
    688   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
    689   unsigned long konst;		/* KONST field. See below.  */
    690   unsigned long vaddr;		/* VADDR to be relocated.  */
    691 } Elf32_crinfo;
    692 
    693 typedef struct
    694 {
    695   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
    696   unsigned int rtype : 4;	/* Relocation types. See below.  */
    697   unsigned int dist2to : 8;
    698   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
    699   unsigned long konst;		/* KONST field. See below.  */
    700 } Elf32_crinfo2;
    701 
    702 typedef struct
    703 {
    704   bfd_byte info[4];
    705   bfd_byte konst[4];
    706   bfd_byte vaddr[4];
    707 } Elf32_External_crinfo;
    708 
    709 typedef struct
    710 {
    711   bfd_byte info[4];
    712   bfd_byte konst[4];
    713 } Elf32_External_crinfo2;
    714 
    715 /* These are the constants used to swap the bitfields in a crinfo.  */
    716 
    717 #define CRINFO_CTYPE (0x1U)
    718 #define CRINFO_CTYPE_SH (31)
    719 #define CRINFO_RTYPE (0xfU)
    720 #define CRINFO_RTYPE_SH (27)
    721 #define CRINFO_DIST2TO (0xffU)
    722 #define CRINFO_DIST2TO_SH (19)
    723 #define CRINFO_RELVADDR (0x7ffffU)
    724 #define CRINFO_RELVADDR_SH (0)
    725 
    726 /* A compact relocation info has long (3 words) or short (2 words)
    727    formats.  A short format doesn't have VADDR field and relvaddr
    728    fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
    729 #define CRF_MIPS_LONG			1
    730 #define CRF_MIPS_SHORT			0
    731 
    732 /* There are 4 types of compact relocation at least. The value KONST
    733    has different meaning for each type:
    734 
    735    (type)		(konst)
    736    CT_MIPS_REL32	Address in data
    737    CT_MIPS_WORD		Address in word (XXX)
    738    CT_MIPS_GPHI_LO	GP - vaddr
    739    CT_MIPS_JMPAD	Address to jump
    740    */
    741 
    742 #define CRT_MIPS_REL32			0xa
    743 #define CRT_MIPS_WORD			0xb
    744 #define CRT_MIPS_GPHI_LO		0xc
    745 #define CRT_MIPS_JMPAD			0xd
    746 
    747 #define mips_elf_set_cr_format(x,format)	((x).ctype = (format))
    748 #define mips_elf_set_cr_type(x,type)		((x).rtype = (type))
    749 #define mips_elf_set_cr_dist2to(x,v)		((x).dist2to = (v))
    750 #define mips_elf_set_cr_relvaddr(x,d)		((x).relvaddr = (d)<<2)
    751 
    752 /* The structure of the runtime procedure descriptor created by the
    754    loader for use by the static exception system.  */
    755 
    756 typedef struct runtime_pdr {
    757 	bfd_vma	adr;		/* Memory address of start of procedure.  */
    758 	long	regmask;	/* Save register mask.  */
    759 	long	regoffset;	/* Save register offset.  */
    760 	long	fregmask;	/* Save floating point register mask.  */
    761 	long	fregoffset;	/* Save floating point register offset.  */
    762 	long	frameoffset;	/* Frame size.  */
    763 	short	framereg;	/* Frame pointer register.  */
    764 	short	pcreg;		/* Offset or reg of return pc.  */
    765 	long	irpss;		/* Index into the runtime string table.  */
    766 	long	reserved;
    767 	struct exception_info *exception_info;/* Pointer to exception array.  */
    768 } RPDR, *pRPDR;
    769 #define cbRPDR sizeof (RPDR)
    770 #define rpdNil ((pRPDR) 0)
    771 
    772 static struct mips_got_entry *mips_elf_create_local_got_entry
    774   (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
    775    struct mips_elf_link_hash_entry *, int);
    776 static bool mips_elf_sort_hash_table_f
    777   (struct mips_elf_link_hash_entry *, void *);
    778 static bfd_vma mips_elf_high
    779   (bfd_vma);
    780 static bool mips_elf_create_dynamic_relocation
    781   (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
    782    struct mips_elf_link_hash_entry *, asection *, bfd_vma,
    783    bfd_vma *, asection *);
    784 static bfd_vma mips_elf_adjust_gp
    785   (bfd *, struct mips_got_info *, bfd *);
    786 static bool mips_elf_free_hi16_list
    787   (bfd *, asection *, bool, struct bfd_link_info *);
    788 
    789 /* This will be used when we sort the dynamic relocation records.  */
    790 static bfd *reldyn_sorting_bfd;
    791 
    792 /* True if ABFD is for CPUs with load interlocking that include
    793    non-MIPS1 CPUs and R3900.  */
    794 #define LOAD_INTERLOCKS_P(abfd) \
    795   (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) \
    796    || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_3900))
    797 
    798 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
    799    This should be safe for all architectures.  We enable this predicate
    800    for RM9000 for now.  */
    801 #define JAL_TO_BAL_P(abfd) \
    802   ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_9000)
    803 
    804 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
    805    This should be safe for all architectures.  We enable this predicate for
    806    all CPUs.  */
    807 #define JALR_TO_BAL_P(abfd) 1
    808 
    809 /* True if ABFD is for CPUs that are faster if JR is converted to B.
    810    This should be safe for all architectures.  We enable this predicate for
    811    all CPUs.  */
    812 #define JR_TO_B_P(abfd) 1
    813 
    814 /* True if ABFD is a PIC object.  */
    815 #define PIC_OBJECT_P(abfd) \
    816   ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
    817 
    818 /* Nonzero if ABFD is using the O32 ABI.  */
    819 #define ABI_O32_P(abfd) \
    820   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
    821 
    822 /* Nonzero if ABFD is using the N32 ABI.  */
    823 #define ABI_N32_P(abfd) \
    824   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
    825 
    826 /* Nonzero if ABFD is using NewABI conventions.  */
    827 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
    828 
    829 /* Nonzero if ABFD has microMIPS code.  */
    830 #define MICROMIPS_P(abfd) \
    831   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
    832 
    833 /* Nonzero if ABFD is MIPS R6.  */
    834 #define MIPSR6_P(abfd) \
    835   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6 \
    836     || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
    837 
    838 /* The IRIX compatibility level we are striving for.  */
    839 #define IRIX_COMPAT(abfd) \
    840   (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
    841 
    842 /* Whether we are trying to be compatible with IRIX at all.  */
    843 #define SGI_COMPAT(abfd) \
    844   (IRIX_COMPAT (abfd) != ict_none)
    845 
    846 /* The name of the options section.  */
    847 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
    848   (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
    849 
    850 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
    851    Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
    852 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
    853   (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
    854 
    855 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section.  */
    856 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
    857   (strcmp (NAME, ".MIPS.abiflags") == 0)
    858 
    859 /* Whether the section is readonly.  */
    860 #define MIPS_ELF_READONLY_SECTION(sec) \
    861   ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))		\
    862    == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
    863 
    864 /* The name of the stub section.  */
    865 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
    866 
    867 /* The size of an external REL relocation.  */
    868 #define MIPS_ELF_REL_SIZE(abfd) \
    869   (get_elf_backend_data (abfd)->s->sizeof_rel)
    870 
    871 /* The size of an external RELA relocation.  */
    872 #define MIPS_ELF_RELA_SIZE(abfd) \
    873   (get_elf_backend_data (abfd)->s->sizeof_rela)
    874 
    875 /* The size of an external dynamic table entry.  */
    876 #define MIPS_ELF_DYN_SIZE(abfd) \
    877   (get_elf_backend_data (abfd)->s->sizeof_dyn)
    878 
    879 /* The size of a GOT entry.  */
    880 #define MIPS_ELF_GOT_SIZE(abfd) \
    881   (get_elf_backend_data (abfd)->s->arch_size / 8)
    882 
    883 /* The size of the .rld_map section. */
    884 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
    885   (get_elf_backend_data (abfd)->s->arch_size / 8)
    886 
    887 /* The size of a symbol-table entry.  */
    888 #define MIPS_ELF_SYM_SIZE(abfd) \
    889   (get_elf_backend_data (abfd)->s->sizeof_sym)
    890 
    891 /* The default alignment for sections, as a power of two.  */
    892 #define MIPS_ELF_LOG_FILE_ALIGN(abfd)				\
    893   (get_elf_backend_data (abfd)->s->log_file_align)
    894 
    895 /* Get word-sized data.  */
    896 #define MIPS_ELF_GET_WORD(abfd, ptr) \
    897   (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
    898 
    899 /* Put out word-sized data.  */
    900 #define MIPS_ELF_PUT_WORD(abfd, val, ptr)	\
    901   (ABI_64_P (abfd)				\
    902    ? bfd_put_64 (abfd, val, ptr)		\
    903    : bfd_put_32 (abfd, val, ptr))
    904 
    905 /* The opcode for word-sized loads (LW or LD).  */
    906 #define MIPS_ELF_LOAD_WORD(abfd) \
    907   (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
    908 
    909 /* Add a dynamic symbol table-entry.  */
    910 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)	\
    911   _bfd_elf_add_dynamic_entry (info, tag, val)
    912 
    913 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)			\
    914   (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
    915 
    916 /* The name of the dynamic relocation section.  */
    917 #define MIPS_ELF_REL_DYN_NAME(INFO) \
    918   (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
    919    ? ".rela.dyn" : ".rel.dyn")
    920 
    921 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
    922    from smaller values.  Start with zero, widen, *then* decrement.  */
    923 #define MINUS_ONE	(((bfd_vma)0) - 1)
    924 #define MINUS_TWO	(((bfd_vma)0) - 2)
    925 
    926 /* The value to write into got[1] for SVR4 targets, to identify it is
    927    a GNU object.  The dynamic linker can then use got[1] to store the
    928    module pointer.  */
    929 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
    930   ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
    931 
    932 /* The offset of $gp from the beginning of the .got section.  */
    933 #define ELF_MIPS_GP_OFFSET(INFO) \
    934   (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
    935    ? 0x0 : 0x7ff0)
    936 
    937 /* The maximum size of the GOT for it to be addressable using 16-bit
    938    offsets from $gp.  */
    939 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
    940 
    941 /* Instructions which appear in a stub.  */
    942 #define STUB_LW(abfd)							\
    943   ((ABI_64_P (abfd)							\
    944     ? 0xdf998010				/* ld t9,0x8010(gp) */	\
    945     : 0x8f998010))				/* lw t9,0x8010(gp) */
    946 #define STUB_MOVE 0x03e07825			/* or t7,ra,zero */
    947 #define STUB_LUI(VAL) (0x3c180000 + (VAL))	/* lui t8,VAL */
    948 #define STUB_JALR 0x0320f809			/* jalr ra,t9 */
    949 #define STUB_JALRC 0xf8190000			/* jalrc ra,t9 */
    950 #define STUB_ORI(VAL) (0x37180000 + (VAL))	/* ori t8,t8,VAL */
    951 #define STUB_LI16U(VAL) (0x34180000 + (VAL))	/* ori t8,zero,VAL unsigned */
    952 #define STUB_LI16S(abfd, VAL)						\
    953    ((ABI_64_P (abfd)							\
    954     ? (0x64180000 + (VAL))	/* daddiu t8,zero,VAL sign extended */	\
    955     : (0x24180000 + (VAL))))	/* addiu t8,zero,VAL sign extended */
    956 
    957 /* Likewise for the microMIPS ASE.  */
    958 #define STUB_LW_MICROMIPS(abfd)						\
    959   (ABI_64_P (abfd)							\
    960    ? 0xdf3c8010					/* ld t9,0x8010(gp) */	\
    961    : 0xff3c8010)				/* lw t9,0x8010(gp) */
    962 #define STUB_MOVE_MICROMIPS 0x0dff		/* move t7,ra */
    963 #define STUB_MOVE32_MICROMIPS 0x001f7a90	/* or t7,ra,zero */
    964 #define STUB_LUI_MICROMIPS(VAL)						\
    965    (0x41b80000 + (VAL))				/* lui t8,VAL */
    966 #define STUB_JALR_MICROMIPS 0x45d9		/* jalr t9 */
    967 #define STUB_JALR32_MICROMIPS 0x03f90f3c	/* jalr ra,t9 */
    968 #define STUB_ORI_MICROMIPS(VAL)						\
    969   (0x53180000 + (VAL))				/* ori t8,t8,VAL */
    970 #define STUB_LI16U_MICROMIPS(VAL)					\
    971   (0x53000000 + (VAL))				/* ori t8,zero,VAL unsigned */
    972 #define STUB_LI16S_MICROMIPS(abfd, VAL)					\
    973    (ABI_64_P (abfd)							\
    974     ? 0x5f000000 + (VAL)	/* daddiu t8,zero,VAL sign extended */	\
    975     : 0x33000000 + (VAL))	/* addiu t8,zero,VAL sign extended */
    976 
    977 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
    978 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
    979 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
    980 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
    981 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
    982 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
    983 
    984 /* The name of the dynamic interpreter.  This is put in the .interp
    985    section.  */
    986 
    987 #define ELF_DYNAMIC_INTERPRETER(abfd)		\
    988    (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1"	\
    989     : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1"	\
    990     : "/usr/lib/libc.so.1")
    991 
    992 #ifdef BFD64
    993 #define MNAME(bfd,pre,pos) \
    994   (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
    995 #define ELF_R_SYM(bfd, i)					\
    996   (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
    997 #define ELF_R_TYPE(bfd, i)					\
    998   (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
    999 #define ELF_R_INFO(bfd, s, t)					\
   1000   (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
   1001 #else
   1002 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
   1003 #define ELF_R_SYM(bfd, i)					\
   1004   (ELF32_R_SYM (i))
   1005 #define ELF_R_TYPE(bfd, i)					\
   1006   (ELF32_R_TYPE (i))
   1007 #define ELF_R_INFO(bfd, s, t)					\
   1008   (ELF32_R_INFO (s, t))
   1009 #endif
   1010 
   1011   /* The mips16 compiler uses a couple of special sections to handle
   1013      floating point arguments.
   1014 
   1015      Section names that look like .mips16.fn.FNNAME contain stubs that
   1016      copy floating point arguments from the fp regs to the gp regs and
   1017      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
   1018      call should be redirected to the stub instead.  If no 32 bit
   1019      function calls FNNAME, the stub should be discarded.  We need to
   1020      consider any reference to the function, not just a call, because
   1021      if the address of the function is taken we will need the stub,
   1022      since the address might be passed to a 32 bit function.
   1023 
   1024      Section names that look like .mips16.call.FNNAME contain stubs
   1025      that copy floating point arguments from the gp regs to the fp
   1026      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
   1027      then any 16 bit function that calls FNNAME should be redirected
   1028      to the stub instead.  If FNNAME is not a 32 bit function, the
   1029      stub should be discarded.
   1030 
   1031      .mips16.call.fp.FNNAME sections are similar, but contain stubs
   1032      which call FNNAME and then copy the return value from the fp regs
   1033      to the gp regs.  These stubs store the return value in $18 while
   1034      calling FNNAME; any function which might call one of these stubs
   1035      must arrange to save $18 around the call.  (This case is not
   1036      needed for 32 bit functions that call 16 bit functions, because
   1037      16 bit functions always return floating point values in both
   1038      $f0/$f1 and $2/$3.)
   1039 
   1040      Note that in all cases FNNAME might be defined statically.
   1041      Therefore, FNNAME is not used literally.  Instead, the relocation
   1042      information will indicate which symbol the section is for.
   1043 
   1044      We record any stubs that we find in the symbol table.  */
   1045 
   1046 #define FN_STUB ".mips16.fn."
   1047 #define CALL_STUB ".mips16.call."
   1048 #define CALL_FP_STUB ".mips16.call.fp."
   1049 
   1050 #define FN_STUB_P(name) startswith (name, FN_STUB)
   1051 #define CALL_STUB_P(name) startswith (name, CALL_STUB)
   1052 #define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
   1053 
   1054 /* The format of the first PLT entry in an O32 executable.  */
   1056 static const bfd_vma mips_o32_exec_plt0_entry[] =
   1057 {
   1058   0x3c1c0000,	/* lui $28, %hi(&GOTPLT[0])				*/
   1059   0x8f990000,	/* lw $25, %lo(&GOTPLT[0])($28)				*/
   1060   0x279c0000,	/* addiu $28, $28, %lo(&GOTPLT[0])			*/
   1061   0x031cc023,	/* subu $24, $24, $28					*/
   1062   0x03e07825,	/* or t7, ra, zero					*/
   1063   0x0018c082,	/* srl $24, $24, 2					*/
   1064   0x0320f809,	/* jalr $25						*/
   1065   0x2718fffe	/* subu $24, $24, 2					*/
   1066 };
   1067 
   1068 /* The format of the first PLT entry in an O32 executable using compact
   1069    jumps.  */
   1070 static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
   1071 {
   1072   0x3c1c0000,	/* lui $28, %hi(&GOTPLT[0])				*/
   1073   0x8f990000,	/* lw $25, %lo(&GOTPLT[0])($28)				*/
   1074   0x279c0000,	/* addiu $28, $28, %lo(&GOTPLT[0])			*/
   1075   0x031cc023,	/* subu $24, $24, $28					*/
   1076   0x03e07821,	/* move $15, $31	# 32-bit move (addu)		*/
   1077   0x0018c082,	/* srl $24, $24, 2					*/
   1078   0x2718fffe,	/* subu $24, $24, 2					*/
   1079   0xf8190000	/* jalrc $25						*/
   1080 };
   1081 
   1082 /* The format of the first PLT entry in an N32 executable.  Different
   1083    because gp ($28) is not available; we use t2 ($14) instead.  */
   1084 static const bfd_vma mips_n32_exec_plt0_entry[] =
   1085 {
   1086   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1087   0x8dd90000,	/* lw $25, %lo(&GOTPLT[0])($14)				*/
   1088   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1089   0x030ec023,	/* subu $24, $24, $14					*/
   1090   0x03e07825,	/* or t7, ra, zero					*/
   1091   0x0018c082,	/* srl $24, $24, 2					*/
   1092   0x0320f809,	/* jalr $25						*/
   1093   0x2718fffe	/* subu $24, $24, 2					*/
   1094 };
   1095 
   1096 /* The format of the first PLT entry in an N32 executable using compact
   1097    jumps.  Different because gp ($28) is not available; we use t2 ($14)
   1098    instead.  */
   1099 static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
   1100 {
   1101   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1102   0x8dd90000,	/* lw $25, %lo(&GOTPLT[0])($14)				*/
   1103   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1104   0x030ec023,	/* subu $24, $24, $14					*/
   1105   0x03e07821,	/* move $15, $31	# 32-bit move (addu)		*/
   1106   0x0018c082,	/* srl $24, $24, 2					*/
   1107   0x2718fffe,	/* subu $24, $24, 2					*/
   1108   0xf8190000	/* jalrc $25						*/
   1109 };
   1110 
   1111 /* The format of the first PLT entry in an N64 executable.  Different
   1112    from N32 because of the increased size of GOT entries.  */
   1113 static const bfd_vma mips_n64_exec_plt0_entry[] =
   1114 {
   1115   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1116   0xddd90000,	/* ld $25, %lo(&GOTPLT[0])($14)				*/
   1117   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1118   0x030ec023,	/* subu $24, $24, $14					*/
   1119   0x03e07825,	/* or t7, ra, zero					*/
   1120   0x0018c0c2,	/* srl $24, $24, 3					*/
   1121   0x0320f809,	/* jalr $25						*/
   1122   0x2718fffe	/* subu $24, $24, 2					*/
   1123 };
   1124 
   1125 /* The format of the first PLT entry in an N64 executable using compact
   1126    jumps.  Different from N32 because of the increased size of GOT
   1127    entries.  */
   1128 static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
   1129 {
   1130   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1131   0xddd90000,	/* ld $25, %lo(&GOTPLT[0])($14)				*/
   1132   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1133   0x030ec023,	/* subu $24, $24, $14					*/
   1134   0x03e0782d,	/* move $15, $31	# 64-bit move (daddu)		*/
   1135   0x0018c0c2,	/* srl $24, $24, 3					*/
   1136   0x2718fffe,	/* subu $24, $24, 2					*/
   1137   0xf8190000	/* jalrc $25						*/
   1138 };
   1139 
   1140 
   1141 /* The format of the microMIPS first PLT entry in an O32 executable.
   1142    We rely on v0 ($2) rather than t8 ($24) to contain the address
   1143    of the GOTPLT entry handled, so this stub may only be used when
   1144    all the subsequent PLT entries are microMIPS code too.
   1145 
   1146    The trailing NOP is for alignment and correct disassembly only.  */
   1147 static const bfd_vma micromips_o32_exec_plt0_entry[] =
   1148 {
   1149   0x7980, 0x0000,	/* addiupc $3, (&GOTPLT[0]) - .			*/
   1150   0xff23, 0x0000,	/* lw $25, 0($3)				*/
   1151   0x0535,		/* subu $2, $2, $3				*/
   1152   0x2525,		/* srl $2, $2, 2				*/
   1153   0x3302, 0xfffe,	/* subu $24, $2, 2				*/
   1154   0x0dff,		/* move $15, $31				*/
   1155   0x45f9,		/* jalrs $25					*/
   1156   0x0f83,		/* move $28, $3					*/
   1157   0x0c00		/* nop						*/
   1158 };
   1159 
   1160 /* The format of the microMIPS first PLT entry in an O32 executable
   1161    in the insn32 mode.  */
   1162 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
   1163 {
   1164   0x41bc, 0x0000,	/* lui $28, %hi(&GOTPLT[0])			*/
   1165   0xff3c, 0x0000,	/* lw $25, %lo(&GOTPLT[0])($28)			*/
   1166   0x339c, 0x0000,	/* addiu $28, $28, %lo(&GOTPLT[0])		*/
   1167   0x0398, 0xc1d0,	/* subu $24, $24, $28				*/
   1168   0x001f, 0x7a90,	/* or $15, $31, zero				*/
   1169   0x0318, 0x1040,	/* srl $24, $24, 2				*/
   1170   0x03f9, 0x0f3c,	/* jalr $25					*/
   1171   0x3318, 0xfffe	/* subu $24, $24, 2				*/
   1172 };
   1173 
   1174 /* The format of subsequent standard PLT entries.  */
   1175 static const bfd_vma mips_exec_plt_entry[] =
   1176 {
   1177   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
   1178   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
   1179   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
   1180   0x03200008	/* jr $25					*/
   1181 };
   1182 
   1183 static const bfd_vma mipsr6_exec_plt_entry[] =
   1184 {
   1185   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
   1186   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
   1187   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
   1188   0x03200009	/* jr $25					*/
   1189 };
   1190 
   1191 static const bfd_vma mipsr6_exec_plt_entry_compact[] =
   1192 {
   1193   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
   1194   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
   1195   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
   1196   0xd8190000	/* jic $25, 0					*/
   1197 };
   1198 
   1199 /* The format of subsequent MIPS16 o32 PLT entries.  We use v0 ($2)
   1200    and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
   1201    directly addressable.  */
   1202 static const bfd_vma mips16_o32_exec_plt_entry[] =
   1203 {
   1204   0xb203,		/* lw $2, 12($pc)			*/
   1205   0x9a60,		/* lw $3, 0($2)				*/
   1206   0x651a,		/* move $24, $2				*/
   1207   0xeb00,		/* jr $3				*/
   1208   0x653b,		/* move $25, $3				*/
   1209   0x6500,		/* nop					*/
   1210   0x0000, 0x0000	/* .word (.got.plt entry)		*/
   1211 };
   1212 
   1213 /* The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
   1214    as a temporary because t8 ($24) is not addressable with ADDIUPC.  */
   1215 static const bfd_vma micromips_o32_exec_plt_entry[] =
   1216 {
   1217   0x7900, 0x0000,	/* addiupc $2, (.got.plt entry) - .	*/
   1218   0xff22, 0x0000,	/* lw $25, 0($2)			*/
   1219   0x4599,		/* jr $25				*/
   1220   0x0f02		/* move $24, $2				*/
   1221 };
   1222 
   1223 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode.  */
   1224 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
   1225 {
   1226   0x41af, 0x0000,	/* lui $15, %hi(.got.plt entry)		*/
   1227   0xff2f, 0x0000,	/* lw $25, %lo(.got.plt entry)($15)	*/
   1228   0x0019, 0x0f3c,	/* jr $25				*/
   1229   0x330f, 0x0000	/* addiu $24, $15, %lo(.got.plt entry)	*/
   1230 };
   1231 
   1232 /* The format of the first PLT entry in a VxWorks executable.  */
   1233 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
   1234 {
   1235   0x3c190000,	/* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)		*/
   1236   0x27390000,	/* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_)	*/
   1237   0x8f390008,	/* lw t9, 8(t9)					*/
   1238   0x00000000,	/* nop						*/
   1239   0x03200008,	/* jr t9					*/
   1240   0x00000000	/* nop						*/
   1241 };
   1242 
   1243 /* The format of subsequent PLT entries.  */
   1244 static const bfd_vma mips_vxworks_exec_plt_entry[] =
   1245 {
   1246   0x10000000,	/* b .PLT_resolver			*/
   1247   0x24180000,	/* li t8, <pltindex>			*/
   1248   0x3c190000,	/* lui t9, %hi(<.got.plt slot>)		*/
   1249   0x27390000,	/* addiu t9, t9, %lo(<.got.plt slot>)	*/
   1250   0x8f390000,	/* lw t9, 0(t9)				*/
   1251   0x00000000,	/* nop					*/
   1252   0x03200008,	/* jr t9				*/
   1253   0x00000000	/* nop					*/
   1254 };
   1255 
   1256 /* The format of the first PLT entry in a VxWorks shared object.  */
   1257 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
   1258 {
   1259   0x8f990008,	/* lw t9, 8(gp)		*/
   1260   0x00000000,	/* nop			*/
   1261   0x03200008,	/* jr t9		*/
   1262   0x00000000,	/* nop			*/
   1263   0x00000000,	/* nop			*/
   1264   0x00000000	/* nop			*/
   1265 };
   1266 
   1267 /* The format of subsequent PLT entries.  */
   1268 static const bfd_vma mips_vxworks_shared_plt_entry[] =
   1269 {
   1270   0x10000000,	/* b .PLT_resolver	*/
   1271   0x24180000	/* li t8, <pltindex>	*/
   1272 };
   1273 
   1274 /* microMIPS 32-bit opcode helper installer.  */
   1276 
   1277 static void
   1278 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
   1279 {
   1280   bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
   1281   bfd_put_16 (abfd,  opcode	   & 0xffff, ptr + 2);
   1282 }
   1283 
   1284 /* microMIPS 32-bit opcode helper retriever.  */
   1285 
   1286 static bfd_vma
   1287 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
   1288 {
   1289   return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
   1290 }
   1291 
   1292 /* Look up an entry in a MIPS ELF linker hash table.  */
   1294 
   1295 #define mips_elf_link_hash_lookup(table, string, create, copy, follow)	\
   1296   ((struct mips_elf_link_hash_entry *)					\
   1297    elf_link_hash_lookup (&(table)->root, (string), (create),		\
   1298 			 (copy), (follow)))
   1299 
   1300 /* Traverse a MIPS ELF linker hash table.  */
   1301 
   1302 #define mips_elf_link_hash_traverse(table, func, info)			\
   1303   (elf_link_hash_traverse						\
   1304    (&(table)->root,							\
   1305     (bool (*) (struct elf_link_hash_entry *, void *)) (func),		\
   1306     (info)))
   1307 
   1308 /* Find the base offsets for thread-local storage in this object,
   1309    for GD/LD and IE/LE respectively.  */
   1310 
   1311 #define TP_OFFSET 0x7000
   1312 #define DTP_OFFSET 0x8000
   1313 
   1314 static bfd_vma
   1315 dtprel_base (struct bfd_link_info *info)
   1316 {
   1317   /* If tls_sec is NULL, we should have signalled an error already.  */
   1318   if (elf_hash_table (info)->tls_sec == NULL)
   1319     return 0;
   1320   return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
   1321 }
   1322 
   1323 static bfd_vma
   1324 tprel_base (struct bfd_link_info *info)
   1325 {
   1326   /* If tls_sec is NULL, we should have signalled an error already.  */
   1327   if (elf_hash_table (info)->tls_sec == NULL)
   1328     return 0;
   1329   return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
   1330 }
   1331 
   1332 /* Create an entry in a MIPS ELF linker hash table.  */
   1333 
   1334 static struct bfd_hash_entry *
   1335 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   1336 			    struct bfd_hash_table *table, const char *string)
   1337 {
   1338   struct mips_elf_link_hash_entry *ret =
   1339     (struct mips_elf_link_hash_entry *) entry;
   1340 
   1341   /* Allocate the structure if it has not already been allocated by a
   1342      subclass.  */
   1343   if (ret == NULL)
   1344     ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
   1345   if (ret == NULL)
   1346     return (struct bfd_hash_entry *) ret;
   1347 
   1348   /* Call the allocation method of the superclass.  */
   1349   ret = ((struct mips_elf_link_hash_entry *)
   1350 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
   1351 				     table, string));
   1352   if (ret != NULL)
   1353     {
   1354       /* Set local fields.  */
   1355       memset (&ret->esym, 0, sizeof (EXTR));
   1356       /* We use -2 as a marker to indicate that the information has
   1357 	 not been set.  -1 means there is no associated ifd.  */
   1358       ret->esym.ifd = -2;
   1359       ret->la25_stub = 0;
   1360       ret->possibly_dynamic_relocs = 0;
   1361       ret->fn_stub = NULL;
   1362       ret->call_stub = NULL;
   1363       ret->call_fp_stub = NULL;
   1364       ret->mipsxhash_loc = 0;
   1365       ret->global_got_area = GGA_NONE;
   1366       ret->got_only_for_calls = true;
   1367       ret->readonly_reloc = false;
   1368       ret->has_static_relocs = false;
   1369       ret->no_fn_stub = false;
   1370       ret->need_fn_stub = false;
   1371       ret->has_nonpic_branches = false;
   1372       ret->needs_lazy_stub = false;
   1373       ret->use_plt_entry = false;
   1374     }
   1375 
   1376   return (struct bfd_hash_entry *) ret;
   1377 }
   1378 
   1379 /* Allocate MIPS ELF private object data.  */
   1380 
   1381 bool
   1382 _bfd_mips_elf_mkobject (bfd *abfd)
   1383 {
   1384   return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata));
   1385 }
   1386 
   1387 /* MIPS ELF uses a special find_nearest_line routine in order the
   1388    handle the ECOFF debugging information.  */
   1389 
   1390 struct mips_elf_find_line
   1391 {
   1392   struct ecoff_debug_info d;
   1393   struct ecoff_find_line i;
   1394 };
   1395 
   1396 bool
   1397 _bfd_mips_elf_free_cached_info (bfd *abfd)
   1398 {
   1399   struct mips_elf_obj_tdata *tdata;
   1400 
   1401   if ((bfd_get_format (abfd) == bfd_object
   1402        || bfd_get_format (abfd) == bfd_core)
   1403       && (tdata = mips_elf_tdata (abfd)) != NULL)
   1404     {
   1405       BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
   1406       for (asection *sec = abfd->sections; sec; sec = sec->next)
   1407 	mips_elf_free_hi16_list (abfd, sec, false, NULL);
   1408       if (tdata->find_line_info != NULL)
   1409 	_bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
   1410     }
   1411   return _bfd_elf_free_cached_info (abfd);
   1412 }
   1413 
   1414 bool
   1415 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
   1416 {
   1417   struct _mips_elf_section_data *sdata;
   1418 
   1419   sdata = bfd_zalloc (abfd, sizeof (*sdata));
   1420   if (sdata == NULL)
   1421     return false;
   1422   sec->used_by_bfd = sdata;
   1423 
   1424   return _bfd_elf_new_section_hook (abfd, sec);
   1425 }
   1426 
   1427 /* Read ECOFF debugging information from a .mdebug section into a
   1429    ecoff_debug_info structure.  */
   1430 
   1431 bool
   1432 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
   1433 			       struct ecoff_debug_info *debug)
   1434 {
   1435   HDRR *symhdr;
   1436   const struct ecoff_debug_swap *swap;
   1437   char *ext_hdr;
   1438 
   1439   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   1440   memset (debug, 0, sizeof (*debug));
   1441 
   1442   ext_hdr = bfd_malloc (swap->external_hdr_size);
   1443   if (ext_hdr == NULL && swap->external_hdr_size != 0)
   1444     goto error_return;
   1445 
   1446   if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
   1447 				  swap->external_hdr_size))
   1448     goto error_return;
   1449 
   1450   symhdr = &debug->symbolic_header;
   1451   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
   1452   free (ext_hdr);
   1453   ext_hdr = NULL;
   1454 
   1455   /* The symbolic header contains absolute file offsets and sizes to
   1456      read.  */
   1457 #define READ(ptr, offset, count, size)					\
   1458   do									\
   1459     {									\
   1460       size_t amt;							\
   1461       debug->ptr = NULL;						\
   1462       if (symhdr->count == 0)						\
   1463 	break;								\
   1464       if (_bfd_mul_overflow (size, symhdr->count, &amt))		\
   1465 	{								\
   1466 	  bfd_set_error (bfd_error_file_too_big);			\
   1467 	  goto error_return;						\
   1468 	}								\
   1469       if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0)		\
   1470 	goto error_return;						\
   1471       debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt);		\
   1472       if (debug->ptr == NULL)						\
   1473 	goto error_return;						\
   1474       ((char *) debug->ptr)[amt] = 0;					\
   1475     } while (0)
   1476 
   1477   READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
   1478   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
   1479   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
   1480   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
   1481   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
   1482   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
   1483   READ (ss, cbSsOffset, issMax, sizeof (char));
   1484   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
   1485   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
   1486   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
   1487   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
   1488 #undef READ
   1489 
   1490   return true;
   1491 
   1492  error_return:
   1493   free (ext_hdr);
   1494   _bfd_ecoff_free_ecoff_debug_info (debug);
   1495   return false;
   1496 }
   1497 
   1498 /* Swap RPDR (runtime procedure table entry) for output.  */
   1500 
   1501 static void
   1502 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
   1503 {
   1504   H_PUT_S32 (abfd, in->adr, ex->p_adr);
   1505   H_PUT_32 (abfd, in->regmask, ex->p_regmask);
   1506   H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
   1507   H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
   1508   H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
   1509   H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
   1510 
   1511   H_PUT_16 (abfd, in->framereg, ex->p_framereg);
   1512   H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
   1513 
   1514   H_PUT_32 (abfd, in->irpss, ex->p_irpss);
   1515 }
   1516 
   1517 /* Create a runtime procedure table from the .mdebug section.  */
   1518 
   1519 static bool
   1520 mips_elf_create_procedure_table (void *handle, bfd *abfd,
   1521 				 struct bfd_link_info *info, asection *s,
   1522 				 struct ecoff_debug_info *debug)
   1523 {
   1524   const struct ecoff_debug_swap *swap;
   1525   HDRR *hdr = &debug->symbolic_header;
   1526   RPDR *rpdr, *rp;
   1527   struct rpdr_ext *erp;
   1528   void *rtproc;
   1529   struct pdr_ext *epdr;
   1530   struct sym_ext *esym;
   1531   char *ss, **sv;
   1532   char *str;
   1533   bfd_size_type size;
   1534   bfd_size_type count;
   1535   unsigned long sindex;
   1536   unsigned long i;
   1537   PDR pdr;
   1538   SYMR sym;
   1539   const char *no_name_func = _("static procedure (no name)");
   1540 
   1541   epdr = NULL;
   1542   rpdr = NULL;
   1543   esym = NULL;
   1544   ss = NULL;
   1545   sv = NULL;
   1546 
   1547   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   1548 
   1549   sindex = strlen (no_name_func) + 1;
   1550   count = hdr->ipdMax;
   1551   if (count > 0)
   1552     {
   1553       size = swap->external_pdr_size;
   1554 
   1555       epdr = bfd_malloc (size * count);
   1556       if (epdr == NULL)
   1557 	goto error_return;
   1558 
   1559       if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
   1560 	goto error_return;
   1561 
   1562       size = sizeof (RPDR);
   1563       rp = rpdr = bfd_malloc (size * count);
   1564       if (rpdr == NULL)
   1565 	goto error_return;
   1566 
   1567       size = sizeof (char *);
   1568       sv = bfd_malloc (size * count);
   1569       if (sv == NULL)
   1570 	goto error_return;
   1571 
   1572       count = hdr->isymMax;
   1573       size = swap->external_sym_size;
   1574       esym = bfd_malloc (size * count);
   1575       if (esym == NULL)
   1576 	goto error_return;
   1577 
   1578       if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
   1579 	goto error_return;
   1580 
   1581       count = hdr->issMax;
   1582       ss = bfd_malloc (count);
   1583       if (ss == NULL)
   1584 	goto error_return;
   1585       if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
   1586 	goto error_return;
   1587 
   1588       count = hdr->ipdMax;
   1589       for (i = 0; i < (unsigned long) count; i++, rp++)
   1590 	{
   1591 	  (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
   1592 	  (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
   1593 	  rp->adr = sym.value;
   1594 	  rp->regmask = pdr.regmask;
   1595 	  rp->regoffset = pdr.regoffset;
   1596 	  rp->fregmask = pdr.fregmask;
   1597 	  rp->fregoffset = pdr.fregoffset;
   1598 	  rp->frameoffset = pdr.frameoffset;
   1599 	  rp->framereg = pdr.framereg;
   1600 	  rp->pcreg = pdr.pcreg;
   1601 	  rp->irpss = sindex;
   1602 	  sv[i] = ss + sym.iss;
   1603 	  sindex += strlen (sv[i]) + 1;
   1604 	}
   1605     }
   1606 
   1607   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
   1608   size = BFD_ALIGN (size, 16);
   1609   rtproc = bfd_alloc (abfd, size);
   1610   if (rtproc == NULL)
   1611     {
   1612       mips_elf_hash_table (info)->procedure_count = 0;
   1613       goto error_return;
   1614     }
   1615 
   1616   mips_elf_hash_table (info)->procedure_count = count + 2;
   1617 
   1618   erp = rtproc;
   1619   memset (erp, 0, sizeof (struct rpdr_ext));
   1620   erp++;
   1621   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
   1622   strcpy (str, no_name_func);
   1623   str += strlen (no_name_func) + 1;
   1624   for (i = 0; i < count; i++)
   1625     {
   1626       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
   1627       strcpy (str, sv[i]);
   1628       str += strlen (sv[i]) + 1;
   1629     }
   1630   H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
   1631 
   1632   /* Set the size and contents of .rtproc section.  */
   1633   s->size = size;
   1634   s->contents = rtproc;
   1635 
   1636   /* Skip this section later on (I don't think this currently
   1637      matters, but someday it might).  */
   1638   s->map_head.link_order = NULL;
   1639 
   1640   free (epdr);
   1641   free (rpdr);
   1642   free (esym);
   1643   free (ss);
   1644   free (sv);
   1645   return true;
   1646 
   1647  error_return:
   1648   free (epdr);
   1649   free (rpdr);
   1650   free (esym);
   1651   free (ss);
   1652   free (sv);
   1653   return false;
   1654 }
   1655 
   1656 /* We're going to create a stub for H.  Create a symbol for the stub's
   1658    value and size, to help make the disassembly easier to read.  */
   1659 
   1660 static bool
   1661 mips_elf_create_stub_symbol (struct bfd_link_info *info,
   1662 			     struct mips_elf_link_hash_entry *h,
   1663 			     const char *prefix, asection *s, bfd_vma value,
   1664 			     bfd_vma size)
   1665 {
   1666   bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
   1667   struct bfd_link_hash_entry *bh;
   1668   struct elf_link_hash_entry *elfh;
   1669   char *name;
   1670   bool res;
   1671 
   1672   if (micromips_p)
   1673     value |= 1;
   1674 
   1675   /* Create a new symbol.  */
   1676   name = concat (prefix, h->root.root.root.string, NULL);
   1677   bh = NULL;
   1678   res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
   1679 					  BSF_LOCAL, s, value, NULL,
   1680 					  true, false, &bh);
   1681   free (name);
   1682   if (! res)
   1683     return false;
   1684 
   1685   /* Make it a local function.  */
   1686   elfh = (struct elf_link_hash_entry *) bh;
   1687   elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   1688   elfh->size = size;
   1689   elfh->forced_local = 1;
   1690   if (micromips_p)
   1691     elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
   1692   return true;
   1693 }
   1694 
   1695 /* We're about to redefine H.  Create a symbol to represent H's
   1696    current value and size, to help make the disassembly easier
   1697    to read.  */
   1698 
   1699 static bool
   1700 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
   1701 			       struct mips_elf_link_hash_entry *h,
   1702 			       const char *prefix)
   1703 {
   1704   struct bfd_link_hash_entry *bh;
   1705   struct elf_link_hash_entry *elfh;
   1706   char *name;
   1707   asection *s;
   1708   bfd_vma value;
   1709   bool res;
   1710 
   1711   /* Read the symbol's value.  */
   1712   BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
   1713 	      || h->root.root.type == bfd_link_hash_defweak);
   1714   s = h->root.root.u.def.section;
   1715   value = h->root.root.u.def.value;
   1716 
   1717   /* Create a new symbol.  */
   1718   name = concat (prefix, h->root.root.root.string, NULL);
   1719   bh = NULL;
   1720   res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
   1721 					  BSF_LOCAL, s, value, NULL,
   1722 					  true, false, &bh);
   1723   free (name);
   1724   if (! res)
   1725     return false;
   1726 
   1727   /* Make it local and copy the other attributes from H.  */
   1728   elfh = (struct elf_link_hash_entry *) bh;
   1729   elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
   1730   elfh->other = h->root.other;
   1731   elfh->size = h->root.size;
   1732   elfh->forced_local = 1;
   1733   return true;
   1734 }
   1735 
   1736 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
   1737    function rather than to a hard-float stub.  */
   1738 
   1739 static bool
   1740 section_allows_mips16_refs_p (asection *section)
   1741 {
   1742   const char *name;
   1743 
   1744   name = bfd_section_name (section);
   1745   return (FN_STUB_P (name)
   1746 	  || CALL_STUB_P (name)
   1747 	  || CALL_FP_STUB_P (name)
   1748 	  || strcmp (name, ".pdr") == 0);
   1749 }
   1750 
   1751 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
   1752    stub section of some kind.  Return the R_SYMNDX of the target
   1753    function, or 0 if we can't decide which function that is.  */
   1754 
   1755 static unsigned long
   1756 mips16_stub_symndx (elf_backend_data *bed,
   1757 		    asection *sec ATTRIBUTE_UNUSED,
   1758 		    const Elf_Internal_Rela *relocs,
   1759 		    const Elf_Internal_Rela *relend)
   1760 {
   1761   int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
   1762   const Elf_Internal_Rela *rel;
   1763 
   1764   /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
   1765      one in a compound relocation.  */
   1766   for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
   1767     if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
   1768       return ELF_R_SYM (sec->owner, rel->r_info);
   1769 
   1770   /* Otherwise trust the first relocation, whatever its kind.  This is
   1771      the traditional behavior.  */
   1772   if (relocs < relend)
   1773     return ELF_R_SYM (sec->owner, relocs->r_info);
   1774 
   1775   return 0;
   1776 }
   1777 
   1778 /* Check the mips16 stubs for a particular symbol, and see if we can
   1779    discard them.  */
   1780 
   1781 static void
   1782 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
   1783 			     struct mips_elf_link_hash_entry *h)
   1784 {
   1785   /* Dynamic symbols must use the standard call interface, in case other
   1786      objects try to call them.  */
   1787   if (h->fn_stub != NULL
   1788       && h->root.dynindx != -1)
   1789     {
   1790       mips_elf_create_shadow_symbol (info, h, ".mips16.");
   1791       h->need_fn_stub = true;
   1792     }
   1793 
   1794   if (h->fn_stub != NULL
   1795       && ! h->need_fn_stub)
   1796     {
   1797       /* We don't need the fn_stub; the only references to this symbol
   1798 	 are 16 bit calls.  Clobber the size to 0 to prevent it from
   1799 	 being included in the link.  */
   1800       h->fn_stub->size = 0;
   1801       h->fn_stub->flags &= ~SEC_RELOC;
   1802       h->fn_stub->reloc_count = 0;
   1803       h->fn_stub->flags |= SEC_EXCLUDE;
   1804       h->fn_stub->output_section = bfd_abs_section_ptr;
   1805     }
   1806 
   1807   if (h->call_stub != NULL
   1808       && ELF_ST_IS_MIPS16 (h->root.other))
   1809     {
   1810       /* We don't need the call_stub; this is a 16 bit function, so
   1811 	 calls from other 16 bit functions are OK.  Clobber the size
   1812 	 to 0 to prevent it from being included in the link.  */
   1813       h->call_stub->size = 0;
   1814       h->call_stub->flags &= ~SEC_RELOC;
   1815       h->call_stub->reloc_count = 0;
   1816       h->call_stub->flags |= SEC_EXCLUDE;
   1817       h->call_stub->output_section = bfd_abs_section_ptr;
   1818     }
   1819 
   1820   if (h->call_fp_stub != NULL
   1821       && ELF_ST_IS_MIPS16 (h->root.other))
   1822     {
   1823       /* We don't need the call_stub; this is a 16 bit function, so
   1824 	 calls from other 16 bit functions are OK.  Clobber the size
   1825 	 to 0 to prevent it from being included in the link.  */
   1826       h->call_fp_stub->size = 0;
   1827       h->call_fp_stub->flags &= ~SEC_RELOC;
   1828       h->call_fp_stub->reloc_count = 0;
   1829       h->call_fp_stub->flags |= SEC_EXCLUDE;
   1830       h->call_fp_stub->output_section = bfd_abs_section_ptr;
   1831     }
   1832 }
   1833 
   1834 /* Hashtable callbacks for mips_elf_la25_stubs.  */
   1835 
   1836 static hashval_t
   1837 mips_elf_la25_stub_hash (const void *entry_)
   1838 {
   1839   const struct mips_elf_la25_stub *entry;
   1840 
   1841   entry = (struct mips_elf_la25_stub *) entry_;
   1842   return entry->h->root.root.u.def.section->id
   1843     + entry->h->root.root.u.def.value;
   1844 }
   1845 
   1846 static int
   1847 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
   1848 {
   1849   const struct mips_elf_la25_stub *entry1, *entry2;
   1850 
   1851   entry1 = (struct mips_elf_la25_stub *) entry1_;
   1852   entry2 = (struct mips_elf_la25_stub *) entry2_;
   1853   return ((entry1->h->root.root.u.def.section
   1854 	   == entry2->h->root.root.u.def.section)
   1855 	  && (entry1->h->root.root.u.def.value
   1856 	      == entry2->h->root.root.u.def.value));
   1857 }
   1858 
   1859 /* Called by the linker to set up the la25 stub-creation code.  FN is
   1860    the linker's implementation of add_stub_function.  Return true on
   1861    success.  */
   1862 
   1863 bool
   1864 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
   1865 			  asection *(*fn) (const char *, asection *,
   1866 					   asection *))
   1867 {
   1868   struct mips_elf_link_hash_table *htab;
   1869 
   1870   htab = mips_elf_hash_table (info);
   1871   if (htab == NULL)
   1872     return false;
   1873 
   1874   htab->add_stub_section = fn;
   1875   htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
   1876 				      mips_elf_la25_stub_eq, NULL);
   1877   if (htab->la25_stubs == NULL)
   1878     return false;
   1879 
   1880   return true;
   1881 }
   1882 
   1883 /* Return true if H is a locally-defined PIC function, in the sense
   1884    that it or its fn_stub might need $25 to be valid on entry.
   1885    Note that MIPS16 functions set up $gp using PC-relative instructions,
   1886    so they themselves never need $25 to be valid.  Only non-MIPS16
   1887    entry points are of interest here.  */
   1888 
   1889 static bool
   1890 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
   1891 {
   1892   return ((h->root.root.type == bfd_link_hash_defined
   1893 	   || h->root.root.type == bfd_link_hash_defweak)
   1894 	  && h->root.def_regular
   1895 	  && !bfd_is_abs_section (h->root.root.u.def.section)
   1896 	  && !bfd_is_und_section (h->root.root.u.def.section)
   1897 	  && (!ELF_ST_IS_MIPS16 (h->root.other)
   1898 	      || (h->fn_stub && h->need_fn_stub))
   1899 	  && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
   1900 	      || ELF_ST_IS_MIPS_PIC (h->root.other)));
   1901 }
   1902 
   1903 /* Set *SEC to the input section that contains the target of STUB.
   1904    Return the offset of the target from the start of that section.  */
   1905 
   1906 static bfd_vma
   1907 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
   1908 			  asection **sec)
   1909 {
   1910   if (ELF_ST_IS_MIPS16 (stub->h->root.other))
   1911     {
   1912       BFD_ASSERT (stub->h->need_fn_stub);
   1913       *sec = stub->h->fn_stub;
   1914       return 0;
   1915     }
   1916   else
   1917     {
   1918       *sec = stub->h->root.root.u.def.section;
   1919       return stub->h->root.root.u.def.value;
   1920     }
   1921 }
   1922 
   1923 /* STUB describes an la25 stub that we have decided to implement
   1924    by inserting an LUI/ADDIU pair before the target function.
   1925    Create the section and redirect the function symbol to it.  */
   1926 
   1927 static bool
   1928 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
   1929 			 struct bfd_link_info *info)
   1930 {
   1931   struct mips_elf_link_hash_table *htab;
   1932   char *name;
   1933   asection *s, *input_section;
   1934   unsigned int align;
   1935 
   1936   htab = mips_elf_hash_table (info);
   1937   if (htab == NULL)
   1938     return false;
   1939 
   1940   /* Create a unique name for the new section.  */
   1941   name = bfd_malloc (11 + sizeof (".text.stub."));
   1942   if (name == NULL)
   1943     return false;
   1944   sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
   1945 
   1946   /* Create the section.  */
   1947   mips_elf_get_la25_target (stub, &input_section);
   1948   s = htab->add_stub_section (name, input_section,
   1949 			      input_section->output_section);
   1950   if (s == NULL)
   1951     return false;
   1952 
   1953   /* Make sure that any padding goes before the stub.  */
   1954   align = input_section->alignment_power;
   1955   if (!bfd_link_align_section (s, align))
   1956     return false;
   1957   if (align > 3)
   1958     s->size = (1 << align) - 8;
   1959 
   1960   /* Create a symbol for the stub.  */
   1961   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
   1962   stub->stub_section = s;
   1963   stub->offset = s->size;
   1964 
   1965   /* Allocate room for it.  */
   1966   s->size += 8;
   1967   return true;
   1968 }
   1969 
   1970 /* STUB describes an la25 stub that we have decided to implement
   1971    with a separate trampoline.  Allocate room for it and redirect
   1972    the function symbol to it.  */
   1973 
   1974 static bool
   1975 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
   1976 			      struct bfd_link_info *info)
   1977 {
   1978   struct mips_elf_link_hash_table *htab;
   1979   asection *s;
   1980 
   1981   htab = mips_elf_hash_table (info);
   1982   if (htab == NULL)
   1983     return false;
   1984 
   1985   /* Create a trampoline section, if we haven't already.  */
   1986   s = htab->strampoline;
   1987   if (s == NULL)
   1988     {
   1989       asection *input_section = stub->h->root.root.u.def.section;
   1990       s = htab->add_stub_section (".text", NULL,
   1991 				  input_section->output_section);
   1992       if (s == NULL || !bfd_link_align_section (s, 4))
   1993 	return false;
   1994       htab->strampoline = s;
   1995     }
   1996 
   1997   /* Create a symbol for the stub.  */
   1998   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
   1999   stub->stub_section = s;
   2000   stub->offset = s->size;
   2001 
   2002   /* Allocate room for it.  */
   2003   s->size += 16;
   2004   return true;
   2005 }
   2006 
   2007 /* H describes a symbol that needs an la25 stub.  Make sure that an
   2008    appropriate stub exists and point H at it.  */
   2009 
   2010 static bool
   2011 mips_elf_add_la25_stub (struct bfd_link_info *info,
   2012 			struct mips_elf_link_hash_entry *h)
   2013 {
   2014   struct mips_elf_link_hash_table *htab;
   2015   struct mips_elf_la25_stub search, *stub;
   2016   bool use_trampoline_p;
   2017   asection *s;
   2018   bfd_vma value;
   2019   void **slot;
   2020 
   2021   /* Describe the stub we want.  */
   2022   search.stub_section = NULL;
   2023   search.offset = 0;
   2024   search.h = h;
   2025 
   2026   /* See if we've already created an equivalent stub.  */
   2027   htab = mips_elf_hash_table (info);
   2028   if (htab == NULL)
   2029     return false;
   2030 
   2031   slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
   2032   if (slot == NULL)
   2033     return false;
   2034 
   2035   stub = (struct mips_elf_la25_stub *) *slot;
   2036   if (stub != NULL)
   2037     {
   2038       /* We can reuse the existing stub.  */
   2039       h->la25_stub = stub;
   2040       return true;
   2041     }
   2042 
   2043   /* Create a permanent copy of ENTRY and add it to the hash table.  */
   2044   stub = bfd_malloc (sizeof (search));
   2045   if (stub == NULL)
   2046     return false;
   2047   *stub = search;
   2048   *slot = stub;
   2049 
   2050   /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
   2051      of the section and if we would need no more than 2 nops.  */
   2052   value = mips_elf_get_la25_target (stub, &s);
   2053   if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
   2054     value &= ~1;
   2055   use_trampoline_p = (value != 0 || s->alignment_power > 4);
   2056 
   2057   h->la25_stub = stub;
   2058   return (use_trampoline_p
   2059 	  ? mips_elf_add_la25_trampoline (stub, info)
   2060 	  : mips_elf_add_la25_intro (stub, info));
   2061 }
   2062 
   2063 /* A mips_elf_link_hash_traverse callback that is called before sizing
   2064    sections.  DATA points to a mips_htab_traverse_info structure.  */
   2065 
   2066 static bool
   2067 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
   2068 {
   2069   struct mips_htab_traverse_info *hti;
   2070 
   2071   hti = (struct mips_htab_traverse_info *) data;
   2072   if (!bfd_link_relocatable (hti->info))
   2073     mips_elf_check_mips16_stubs (hti->info, h);
   2074 
   2075   if (mips_elf_local_pic_function_p (h))
   2076     {
   2077       /* PR 12845: If H is in a section that has been garbage
   2078 	 collected it will have its output section set to *ABS*.  */
   2079       if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
   2080 	return true;
   2081 
   2082       /* H is a function that might need $25 to be valid on entry.
   2083 	 If we're creating a non-PIC relocatable object, mark H as
   2084 	 being PIC.  If we're creating a non-relocatable object with
   2085 	 non-PIC branches and jumps to H, make sure that H has an la25
   2086 	 stub.  */
   2087       if (bfd_link_relocatable (hti->info))
   2088 	{
   2089 	  if (!PIC_OBJECT_P (hti->output_bfd))
   2090 	    h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
   2091 	}
   2092       else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
   2093 	{
   2094 	  hti->error = true;
   2095 	  return false;
   2096 	}
   2097     }
   2098   return true;
   2099 }
   2100 
   2101 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
   2103    Most mips16 instructions are 16 bits, but these instructions
   2104    are 32 bits.
   2105 
   2106    The format of these instructions is:
   2107 
   2108    +--------------+--------------------------------+
   2109    |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
   2110    +--------------+--------------------------------+
   2111    |		    Immediate  15:0		   |
   2112    +-----------------------------------------------+
   2113 
   2114    JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
   2115    Note that the immediate value in the first word is swapped.
   2116 
   2117    When producing a relocatable object file, R_MIPS16_26 is
   2118    handled mostly like R_MIPS_26.  In particular, the addend is
   2119    stored as a straight 26-bit value in a 32-bit instruction.
   2120    (gas makes life simpler for itself by never adjusting a
   2121    R_MIPS16_26 reloc to be against a section, so the addend is
   2122    always zero).  However, the 32 bit instruction is stored as 2
   2123    16-bit values, rather than a single 32-bit value.  In a
   2124    big-endian file, the result is the same; in a little-endian
   2125    file, the two 16-bit halves of the 32 bit value are swapped.
   2126    This is so that a disassembler can recognize the jal
   2127    instruction.
   2128 
   2129    When doing a final link, R_MIPS16_26 is treated as a 32 bit
   2130    instruction stored as two 16-bit values.  The addend A is the
   2131    contents of the targ26 field.  The calculation is the same as
   2132    R_MIPS_26.  When storing the calculated value, reorder the
   2133    immediate value as shown above, and don't forget to store the
   2134    value as two 16-bit values.
   2135 
   2136    To put it in MIPS ABI terms, the relocation field is T-targ26-16,
   2137    defined as
   2138 
   2139    big-endian:
   2140    +--------+----------------------+
   2141    |	    |			   |
   2142    |	    |	 targ26-16	   |
   2143    |31	  26|25			  0|
   2144    +--------+----------------------+
   2145 
   2146    little-endian:
   2147    +----------+------+-------------+
   2148    |	      |	     |		   |
   2149    |  sub1    |	     |	   sub2	   |
   2150    |0	     9|10  15|16	 31|
   2151    +----------+--------------------+
   2152    where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
   2153    ((sub1 << 16) | sub2)).
   2154 
   2155    When producing a relocatable object file, the calculation is
   2156    (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
   2157    When producing a fully linked file, the calculation is
   2158    let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
   2159    ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
   2160 
   2161    The table below lists the other MIPS16 instruction relocations.
   2162    Each one is calculated in the same way as the non-MIPS16 relocation
   2163    given on the right, but using the extended MIPS16 layout of 16-bit
   2164    immediate fields:
   2165 
   2166 	R_MIPS16_GPREL		R_MIPS_GPREL16
   2167 	R_MIPS16_GOT16		R_MIPS_GOT16
   2168 	R_MIPS16_CALL16		R_MIPS_CALL16
   2169 	R_MIPS16_HI16		R_MIPS_HI16
   2170 	R_MIPS16_LO16		R_MIPS_LO16
   2171 
   2172    A typical instruction will have a format like this:
   2173 
   2174    +--------------+--------------------------------+
   2175    |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
   2176    +--------------+--------------------------------+
   2177    |    Major     |   rx   |   ry   |   Imm  4:0   |
   2178    +--------------+--------------------------------+
   2179 
   2180    EXTEND is the five bit value 11110.  Major is the instruction
   2181    opcode.
   2182 
   2183    All we need to do here is shuffle the bits appropriately.
   2184    As above, the two 16-bit halves must be swapped on a
   2185    little-endian system.
   2186 
   2187    Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
   2188    relocatable field is shifted by 1 rather than 2 and the same bit
   2189    shuffling is done as with the relocations above.  */
   2190 
   2191 static inline bool
   2192 mips16_reloc_p (int r_type)
   2193 {
   2194   switch (r_type)
   2195     {
   2196     case R_MIPS16_26:
   2197     case R_MIPS16_GPREL:
   2198     case R_MIPS16_GOT16:
   2199     case R_MIPS16_CALL16:
   2200     case R_MIPS16_HI16:
   2201     case R_MIPS16_LO16:
   2202     case R_MIPS16_TLS_GD:
   2203     case R_MIPS16_TLS_LDM:
   2204     case R_MIPS16_TLS_DTPREL_HI16:
   2205     case R_MIPS16_TLS_DTPREL_LO16:
   2206     case R_MIPS16_TLS_GOTTPREL:
   2207     case R_MIPS16_TLS_TPREL_HI16:
   2208     case R_MIPS16_TLS_TPREL_LO16:
   2209     case R_MIPS16_PC16_S1:
   2210       return true;
   2211 
   2212     default:
   2213       return false;
   2214     }
   2215 }
   2216 
   2217 /* Check if a microMIPS reloc.  */
   2218 
   2219 static inline bool
   2220 micromips_reloc_p (unsigned int r_type)
   2221 {
   2222   return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
   2223 }
   2224 
   2225 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
   2226    on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1,
   2227    R_MICROMIPS_PC10_S1 and R_MICROMIPS_GPREL7_S2 relocs that apply to
   2228    16-bit instructions.  */
   2229 
   2230 static inline bool
   2231 micromips_reloc_shuffle_p (unsigned int r_type)
   2232 {
   2233   return (micromips_reloc_p (r_type)
   2234 	  && r_type != R_MICROMIPS_PC7_S1
   2235 	  && r_type != R_MICROMIPS_PC10_S1
   2236 	  && r_type != R_MICROMIPS_GPREL7_S2);
   2237 }
   2238 
   2239 static inline bool
   2240 got16_reloc_p (int r_type)
   2241 {
   2242   return (r_type == R_MIPS_GOT16
   2243 	  || r_type == R_MIPS16_GOT16
   2244 	  || r_type == R_MICROMIPS_GOT16);
   2245 }
   2246 
   2247 static inline bool
   2248 call16_reloc_p (int r_type)
   2249 {
   2250   return (r_type == R_MIPS_CALL16
   2251 	  || r_type == R_MIPS16_CALL16
   2252 	  || r_type == R_MICROMIPS_CALL16);
   2253 }
   2254 
   2255 static inline bool
   2256 got_disp_reloc_p (unsigned int r_type)
   2257 {
   2258   return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
   2259 }
   2260 
   2261 static inline bool
   2262 got_page_reloc_p (unsigned int r_type)
   2263 {
   2264   return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
   2265 }
   2266 
   2267 static inline bool
   2268 got_lo16_reloc_p (unsigned int r_type)
   2269 {
   2270   return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
   2271 }
   2272 
   2273 static inline bool
   2274 call_hi16_reloc_p (unsigned int r_type)
   2275 {
   2276   return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
   2277 }
   2278 
   2279 static inline bool
   2280 call_lo16_reloc_p (unsigned int r_type)
   2281 {
   2282   return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
   2283 }
   2284 
   2285 static inline bool
   2286 hi16_reloc_p (int r_type)
   2287 {
   2288   return (r_type == R_MIPS_HI16
   2289 	  || r_type == R_MIPS16_HI16
   2290 	  || r_type == R_MICROMIPS_HI16
   2291 	  || r_type == R_MIPS_PCHI16);
   2292 }
   2293 
   2294 static inline bool
   2295 lo16_reloc_p (int r_type)
   2296 {
   2297   return (r_type == R_MIPS_LO16
   2298 	  || r_type == R_MIPS16_LO16
   2299 	  || r_type == R_MICROMIPS_LO16
   2300 	  || r_type == R_MIPS_PCLO16);
   2301 }
   2302 
   2303 static inline bool
   2304 tls_hi16_reloc_p (int r_type)
   2305 {
   2306   return (r_type == R_MIPS_TLS_DTPREL_HI16
   2307 	  || r_type == R_MIPS_TLS_TPREL_HI16
   2308 	  || r_type == R_MIPS16_TLS_DTPREL_HI16
   2309 	  || r_type == R_MIPS16_TLS_TPREL_HI16
   2310 	  || r_type == R_MICROMIPS_TLS_DTPREL_HI16
   2311 	  || r_type == R_MICROMIPS_TLS_TPREL_HI16);
   2312 }
   2313 
   2314 static inline bool
   2315 mips16_call_reloc_p (int r_type)
   2316 {
   2317   return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
   2318 }
   2319 
   2320 static inline bool
   2321 jal_reloc_p (int r_type)
   2322 {
   2323   return (r_type == R_MIPS_26
   2324 	  || r_type == R_MIPS16_26
   2325 	  || r_type == R_MICROMIPS_26_S1);
   2326 }
   2327 
   2328 static inline bool
   2329 b_reloc_p (int r_type)
   2330 {
   2331   return (r_type == R_MIPS_PC26_S2
   2332 	  || r_type == R_MIPS_PC21_S2
   2333 	  || r_type == R_MIPS_PC16
   2334 	  || r_type == R_MIPS_GNU_REL16_S2
   2335 	  || r_type == R_MIPS16_PC16_S1
   2336 	  || r_type == R_MICROMIPS_PC16_S1
   2337 	  || r_type == R_MICROMIPS_PC10_S1
   2338 	  || r_type == R_MICROMIPS_PC7_S1);
   2339 }
   2340 
   2341 static inline bool
   2342 aligned_pcrel_reloc_p (int r_type)
   2343 {
   2344   return (r_type == R_MIPS_PC18_S3
   2345 	  || r_type == R_MIPS_PC19_S2);
   2346 }
   2347 
   2348 static inline bool
   2349 branch_reloc_p (int r_type)
   2350 {
   2351   return (r_type == R_MIPS_26
   2352 	  || r_type == R_MIPS_PC26_S2
   2353 	  || r_type == R_MIPS_PC21_S2
   2354 	  || r_type == R_MIPS_PC16
   2355 	  || r_type == R_MIPS_GNU_REL16_S2);
   2356 }
   2357 
   2358 static inline bool
   2359 mips16_branch_reloc_p (int r_type)
   2360 {
   2361   return (r_type == R_MIPS16_26
   2362 	  || r_type == R_MIPS16_PC16_S1);
   2363 }
   2364 
   2365 static inline bool
   2366 micromips_branch_reloc_p (int r_type)
   2367 {
   2368   return (r_type == R_MICROMIPS_26_S1
   2369 	  || r_type == R_MICROMIPS_PC16_S1
   2370 	  || r_type == R_MICROMIPS_PC10_S1
   2371 	  || r_type == R_MICROMIPS_PC7_S1);
   2372 }
   2373 
   2374 static inline bool
   2375 tls_gd_reloc_p (unsigned int r_type)
   2376 {
   2377   return (r_type == R_MIPS_TLS_GD
   2378 	  || r_type == R_MIPS16_TLS_GD
   2379 	  || r_type == R_MICROMIPS_TLS_GD);
   2380 }
   2381 
   2382 static inline bool
   2383 tls_ldm_reloc_p (unsigned int r_type)
   2384 {
   2385   return (r_type == R_MIPS_TLS_LDM
   2386 	  || r_type == R_MIPS16_TLS_LDM
   2387 	  || r_type == R_MICROMIPS_TLS_LDM);
   2388 }
   2389 
   2390 static inline bool
   2391 tls_gottprel_reloc_p (unsigned int r_type)
   2392 {
   2393   return (r_type == R_MIPS_TLS_GOTTPREL
   2394 	  || r_type == R_MIPS16_TLS_GOTTPREL
   2395 	  || r_type == R_MICROMIPS_TLS_GOTTPREL);
   2396 }
   2397 
   2398 static inline bool
   2399 needs_shuffle (int r_type)
   2400 {
   2401   return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
   2402 }
   2403 
   2404 void
   2405 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
   2406 			       bool jal_shuffle, bfd_byte *data)
   2407 {
   2408   bfd_vma first, second, val;
   2409 
   2410   if (!needs_shuffle (r_type))
   2411     return;
   2412 
   2413   /* Pick up the first and second halfwords of the instruction.  */
   2414   first = bfd_get_16 (abfd, data);
   2415   second = bfd_get_16 (abfd, data + 2);
   2416   if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
   2417     val = first << 16 | second;
   2418   else if (r_type != R_MIPS16_26)
   2419     val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
   2420 	   | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
   2421   else
   2422     val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
   2423 	   | ((first & 0x1f) << 21) | second);
   2424   bfd_put_32 (abfd, val, data);
   2425 }
   2426 
   2427 void
   2428 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
   2429 			     bool jal_shuffle, bfd_byte *data)
   2430 {
   2431   bfd_vma first, second, val;
   2432 
   2433   if (!needs_shuffle (r_type))
   2434     return;
   2435 
   2436   val = bfd_get_32 (abfd, data);
   2437   if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
   2438     {
   2439       second = val & 0xffff;
   2440       first = val >> 16;
   2441     }
   2442   else if (r_type != R_MIPS16_26)
   2443     {
   2444       second = ((val >> 11) & 0xffe0) | (val & 0x1f);
   2445       first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
   2446     }
   2447   else
   2448     {
   2449       second = val & 0xffff;
   2450       first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
   2451 	       | ((val >> 21) & 0x1f);
   2452     }
   2453   bfd_put_16 (abfd, second, data + 2);
   2454   bfd_put_16 (abfd, first, data);
   2455 }
   2456 
   2457 /* Perform reloc offset checking.
   2458    We can only use bfd_reloc_offset_in_range, which takes into account
   2459    the size of the field being relocated, when section contents will
   2460    be accessed because mips object files may use relocations that seem
   2461    to access beyond section limits.
   2462    gas/testsuite/gas/mips/dla-reloc.s is an example that puts
   2463    R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
   2464    section.  The R_MIPS_SUB applies to the addend for the next reloc
   2465    rather than the section contents.
   2466 
   2467    CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
   2468    CHECK_INPLACE to only check partial_inplace relocs, and
   2469    CHECK_SHUFFLE to only check relocs that shuffle/unshuffle.  */
   2470 
   2471 bool
   2472 _bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
   2473 				 arelent *reloc_entry, enum reloc_check check)
   2474 {
   2475   if (check == check_inplace && !reloc_entry->howto->partial_inplace)
   2476     return true;
   2477   if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
   2478     return true;
   2479   return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
   2480 				    input_section, reloc_entry->address);
   2481 }
   2482 
   2483 bfd_reloc_status_type
   2484 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
   2485 			       arelent *reloc_entry, asection *input_section,
   2486 			       bool relocatable, void *data, bfd_vma gp)
   2487 {
   2488   bfd_vma relocation;
   2489   bfd_signed_vma val;
   2490   bfd_reloc_status_type status;
   2491 
   2492   if (bfd_is_com_section (symbol->section))
   2493     relocation = 0;
   2494   else
   2495     relocation = symbol->value;
   2496 
   2497   if (symbol->section->output_section != NULL)
   2498     {
   2499       relocation += symbol->section->output_section->vma;
   2500       relocation += symbol->section->output_offset;
   2501     }
   2502 
   2503   /* Set val to the offset into the section or symbol.  */
   2504   val = reloc_entry->addend;
   2505 
   2506   _bfd_mips_elf_sign_extend (val, 16);
   2507 
   2508   /* Adjust val for the final section location and GP value.  If we
   2509      are producing relocatable output, we don't want to do this for
   2510      an external symbol.  */
   2511   if (! relocatable
   2512       || (symbol->flags & BSF_SECTION_SYM) != 0)
   2513     val += relocation - gp;
   2514 
   2515   if (reloc_entry->howto->partial_inplace)
   2516     {
   2517       if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
   2518 				      reloc_entry->address))
   2519 	return bfd_reloc_outofrange;
   2520 
   2521       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
   2522 				       (bfd_byte *) data
   2523 				       + reloc_entry->address);
   2524       if (status != bfd_reloc_ok)
   2525 	return status;
   2526     }
   2527   else
   2528     reloc_entry->addend = val;
   2529 
   2530   if (relocatable)
   2531     reloc_entry->address += input_section->output_offset;
   2532 
   2533   return bfd_reloc_ok;
   2534 }
   2535 
   2536 /* A howto special_function for REL *HI16 relocations.  We can only
   2537    calculate the correct value once we've seen the partnering
   2538    *LO16 relocation, so just save the information for later.
   2539 
   2540    The ABI requires that the *LO16 immediately follow the *HI16.
   2541    However, as a GNU extension, we permit an arbitrary number of
   2542    *HI16s to be associated with a single *LO16.  This significantly
   2543    simplies the relocation handling in gcc.  */
   2544 
   2545 bfd_reloc_status_type
   2546 _bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
   2547 			  asymbol *symbol ATTRIBUTE_UNUSED, void *data,
   2548 			  asection *input_section, bfd *output_bfd,
   2549 			  char **error_message ATTRIBUTE_UNUSED)
   2550 {
   2551   struct _mips_elf_section_data *sdata;
   2552   struct mips_hi16 *n;
   2553 
   2554   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2555     return bfd_reloc_outofrange;
   2556 
   2557   n = bfd_malloc (sizeof *n);
   2558   if (n == NULL)
   2559     return bfd_reloc_outofrange;
   2560 
   2561   sdata = mips_elf_section_data (input_section);
   2562   n->next = sdata->mips_hi16_list;
   2563   n->data = data;
   2564   n->output_bfd = output_bfd;
   2565   n->rel = *reloc_entry;
   2566   sdata->mips_hi16_list = n;
   2567 
   2568   if (output_bfd != NULL)
   2569     reloc_entry->address += input_section->output_offset;
   2570 
   2571   return bfd_reloc_ok;
   2572 }
   2573 
   2574 /* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
   2575    like any other 16-bit relocation when applied to global symbols, but is
   2576    treated in the same as R_MIPS_HI16 when applied to local symbols.  */
   2577 
   2578 bfd_reloc_status_type
   2579 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2580 			   void *data, asection *input_section,
   2581 			   bfd *output_bfd, char **error_message)
   2582 {
   2583   if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
   2584       || bfd_is_und_section (bfd_asymbol_section (symbol))
   2585       || bfd_is_com_section (bfd_asymbol_section (symbol)))
   2586     /* The relocation is against a global symbol.  */
   2587     return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2588 					input_section, output_bfd,
   2589 					error_message);
   2590 
   2591   return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
   2592 				   input_section, output_bfd, error_message);
   2593 }
   2594 
   2595 /* A helper function for REL high-part relocations that takes into account
   2596    local R_MIPS*_GOT16 relocations, which are something of a special case.
   2597    We want to install the addend in the same way as for a R_MIPS*_HI16
   2598    relocation (with a rightshift of 16).  However, since GOT16 relocations
   2599    can also be used with global symbols, their howto has a rightshift of 0.  */
   2600 
   2601 static bfd_reloc_status_type
   2602 _bfd_mips_elf_shr16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2603 			   void *data, asection *input_section,
   2604 			   bfd *output_bfd, char **error_message)
   2605 {
   2606   reloc_howto_type **howto = &reloc_entry->howto;
   2607 
   2608   if ((*howto)->type == R_MIPS_GOT16)
   2609     *howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
   2610   else if ((*howto)->type == R_MIPS16_GOT16)
   2611     *howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
   2612   else if ((*howto)->type == R_MICROMIPS_GOT16)
   2613     *howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
   2614 
   2615   return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2616 				      input_section, output_bfd,
   2617 				      error_message);
   2618 }
   2619 
   2620 /* A howto special_function for REL *LO16 relocations.  The *LO16 itself
   2621    is a straightforward 16 bit inplace relocation, but we must deal with
   2622    any partnering high-part relocations as well.  */
   2623 
   2624 bfd_reloc_status_type
   2625 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2626 			  void *data, asection *input_section,
   2627 			  bfd *output_bfd, char **error_message)
   2628 {
   2629   bfd_vma vallo;
   2630   bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
   2631   struct _mips_elf_section_data *sdata;
   2632 
   2633   if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
   2634 				  reloc_entry->address))
   2635     return bfd_reloc_outofrange;
   2636 
   2637   _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
   2638 				 location);
   2639   /* The high 16 bits of the addend are stored in the high insn, the
   2640      low 16 bits in the low insn, but there is a catch:  You can't
   2641      just concatenate the high and low parts.  The high part of the
   2642      addend is adjusted for the fact that the low part is sign
   2643      extended.  For example, an addend of 0x38000 would have 0x0004 in
   2644      the high part and 0x8000 (=0xff..f8000) in the low part.
   2645      To extract the actual addend, calculate
   2646      ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.  */
   2647   vallo = _bfd_mips_elf_sign_extend (bfd_get_32 (abfd, location) & 0xffff, 16);
   2648   _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
   2649 			       location);
   2650   /* Add in the separate addend, if any.  Since we are REL here this
   2651      will have been set and the in-place addend cleared if we have
   2652      been called from GAS via `bfd_install_relocation'.  */
   2653   vallo += reloc_entry->addend;
   2654 
   2655   sdata = mips_elf_section_data (input_section);
   2656   while (sdata->mips_hi16_list != NULL)
   2657     {
   2658       bfd_reloc_status_type ret;
   2659       struct mips_hi16 *hi;
   2660       bfd_vma addhi;
   2661       bfd_vma addlo;
   2662 
   2663       hi = sdata->mips_hi16_list;
   2664 
   2665       /* We will be applying (symbol + addend) & 0xffff to the low insn,
   2666 	 and we want to apply (symbol + addend + 0x8000) >> 16 to the
   2667 	 high insn (the +0x8000 adjusting for when the applied low part is
   2668 	 negative).  */
   2669       addhi = (hi->rel.addend + 0x8000) & ~(bfd_vma) 0xffff;
   2670       addlo = vallo;
   2671 
   2672       /* For a PC-relative relocation the PCLO16 part of the addend
   2673 	 is relative to its PC and not ours, so we need to adjust it.  */
   2674       if (hi->rel.howto->type == R_MIPS_PCHI16)
   2675 	addlo -= reloc_entry->address - hi->rel.address;
   2676 
   2677       hi->rel.addend = addhi + _bfd_mips_elf_sign_extend (addlo & 0xffff, 16);
   2678 
   2679       ret = _bfd_mips_elf_shr16_reloc (abfd, &hi->rel, symbol, hi->data,
   2680 				       input_section, hi->output_bfd,
   2681 				       error_message);
   2682 
   2683       sdata->mips_hi16_list = hi->next;
   2684       free (hi);
   2685 
   2686       if (ret != bfd_reloc_ok)
   2687 	return ret;
   2688     }
   2689 
   2690   return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2691 				      input_section, output_bfd,
   2692 				      error_message);
   2693 }
   2694 
   2695 /* A generic howto special_function.  This calculates and installs the
   2696    relocation itself, thus avoiding the oft-discussed problems in
   2697    bfd_perform_relocation and bfd_install_relocation.  */
   2698 
   2699 bfd_reloc_status_type
   2700 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   2701 			     asymbol *symbol, void *data ATTRIBUTE_UNUSED,
   2702 			     asection *input_section, bfd *output_bfd,
   2703 			     char **error_message ATTRIBUTE_UNUSED)
   2704 {
   2705   bfd_signed_vma val;
   2706   bfd_reloc_status_type status;
   2707   bool relocatable;
   2708 
   2709   relocatable = (output_bfd != NULL);
   2710 
   2711   if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
   2712 					(relocatable
   2713 					 ? check_inplace : check_std)))
   2714     return bfd_reloc_outofrange;
   2715 
   2716   /* Build up the field adjustment in VAL.  */
   2717   val = 0;
   2718   if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
   2719       && symbol->section->output_section != NULL)
   2720     {
   2721       /* Either we're calculating the final field value or we have a
   2722 	 relocation against a section symbol.  Add in the section's
   2723 	 offset or address.  */
   2724       val += symbol->section->output_section->vma;
   2725       val += symbol->section->output_offset;
   2726     }
   2727 
   2728   if (!relocatable)
   2729     {
   2730       /* We're calculating the final field value.  Add in the symbol's value
   2731 	 and, if pc-relative, subtract the address of the field itself.  */
   2732       val += symbol->value;
   2733       if (reloc_entry->howto->pc_relative)
   2734 	{
   2735 	  val -= input_section->output_section->vma;
   2736 	  val -= input_section->output_offset;
   2737 	  val -= reloc_entry->address;
   2738 	}
   2739     }
   2740 
   2741   /* VAL is now the final adjustment.  If we're keeping this relocation
   2742      in the output file, and if the relocation uses a separate addend,
   2743      we just need to add VAL to that addend.  Otherwise we need to add
   2744      VAL to the relocation field itself.  */
   2745   if (relocatable && !reloc_entry->howto->partial_inplace)
   2746     reloc_entry->addend += val;
   2747   else
   2748     {
   2749       bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
   2750 
   2751       /* Add in the separate addend, if any.  */
   2752       val += reloc_entry->addend;
   2753 
   2754       /* The high 16 bits of the addend are stored in the high insn, the
   2755 	 low 16 bits in the low insn, but there is a catch:  You can't
   2756 	 just concatenate the high and low parts.  The high part of the
   2757 	 addend is adjusted for the fact that the low part is sign
   2758 	 extended.  For example, an addend of 0x38000 would have 0x0004 in
   2759 	 the high part and 0x8000 (=0xff..f8000) in the low part.
   2760 	 We will be applying (symbol + addend) & 0xffff to the low insn,
   2761 	 and we want to apply (symbol + addend + 0x8000) >> 16 to the
   2762 	 high insn (the +0x8000 adjusting for when the applied low part is
   2763 	 negative).  Analogously for the higher parts of a 64-bit addend.  */
   2764       if (reloc_entry->howto->bitsize == 16
   2765 	  && reloc_entry->howto->rightshift % 16 == 0)
   2766 #ifdef BFD64
   2767 	val += 0x800080008000ULL >> (48 - reloc_entry->howto->rightshift);
   2768 #else
   2769 	{
   2770 	  if (reloc_entry->howto->rightshift <= 16)
   2771 	    val += 0x8000 >> (16 - reloc_entry->howto->rightshift);
   2772 	  else
   2773 	    abort ();
   2774 	}
   2775 #endif
   2776 
   2777       /* Add VAL to the relocation field.  */
   2778       _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
   2779 				     location);
   2780       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
   2781 				       location);
   2782       _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
   2783 				   location);
   2784 
   2785       if (status != bfd_reloc_ok)
   2786 	return status;
   2787     }
   2788 
   2789   if (relocatable)
   2790     reloc_entry->address += input_section->output_offset;
   2791 
   2792   return bfd_reloc_ok;
   2793 }
   2794 
   2795 /* Swap an entry in a .gptab section.  Note that these routines rely
   2797    on the equivalence of the two elements of the union.  */
   2798 
   2799 static void
   2800 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
   2801 			      Elf32_gptab *in)
   2802 {
   2803   in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
   2804   in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
   2805 }
   2806 
   2807 static void
   2808 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
   2809 			       Elf32_External_gptab *ex)
   2810 {
   2811   H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
   2812   H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
   2813 }
   2814 
   2815 static void
   2816 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
   2817 				Elf32_External_compact_rel *ex)
   2818 {
   2819   H_PUT_32 (abfd, in->id1, ex->id1);
   2820   H_PUT_32 (abfd, in->num, ex->num);
   2821   H_PUT_32 (abfd, in->id2, ex->id2);
   2822   H_PUT_32 (abfd, in->offset, ex->offset);
   2823   H_PUT_32 (abfd, in->reserved0, ex->reserved0);
   2824   H_PUT_32 (abfd, in->reserved1, ex->reserved1);
   2825 }
   2826 
   2827 static void
   2828 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
   2829 			   Elf32_External_crinfo *ex)
   2830 {
   2831   unsigned long l;
   2832 
   2833   l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
   2834        | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
   2835        | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
   2836        | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
   2837   H_PUT_32 (abfd, l, ex->info);
   2838   H_PUT_32 (abfd, in->konst, ex->konst);
   2839   H_PUT_32 (abfd, in->vaddr, ex->vaddr);
   2840 }
   2841 
   2842 /* A .reginfo section holds a single Elf32_RegInfo structure.  These
   2844    routines swap this structure in and out.  They are used outside of
   2845    BFD, so they are globally visible.  */
   2846 
   2847 void
   2848 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
   2849 				Elf32_RegInfo *in)
   2850 {
   2851   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
   2852   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
   2853   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
   2854   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
   2855   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
   2856   in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
   2857 }
   2858 
   2859 void
   2860 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
   2861 				 Elf32_External_RegInfo *ex)
   2862 {
   2863   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
   2864   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
   2865   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
   2866   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
   2867   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
   2868   H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
   2869 }
   2870 
   2871 /* In the 64 bit ABI, the .MIPS.options section holds register
   2872    information in an Elf64_Reginfo structure.  These routines swap
   2873    them in and out.  They are globally visible because they are used
   2874    outside of BFD.  These routines are here so that gas can call them
   2875    without worrying about whether the 64 bit ABI has been included.  */
   2876 
   2877 void
   2878 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
   2879 				Elf64_Internal_RegInfo *in)
   2880 {
   2881   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
   2882   in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
   2883   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
   2884   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
   2885   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
   2886   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
   2887   in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
   2888 }
   2889 
   2890 void
   2891 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
   2892 				 Elf64_External_RegInfo *ex)
   2893 {
   2894   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
   2895   H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
   2896   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
   2897   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
   2898   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
   2899   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
   2900   H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
   2901 }
   2902 
   2903 /* Swap in an options header.  */
   2904 
   2905 void
   2906 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
   2907 			      Elf_Internal_Options *in)
   2908 {
   2909   in->kind = H_GET_8 (abfd, ex->kind);
   2910   in->size = H_GET_8 (abfd, ex->size);
   2911   in->section = H_GET_16 (abfd, ex->section);
   2912   in->info = H_GET_32 (abfd, ex->info);
   2913 }
   2914 
   2915 /* Swap out an options header.  */
   2916 
   2917 void
   2918 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
   2919 			       Elf_External_Options *ex)
   2920 {
   2921   H_PUT_8 (abfd, in->kind, ex->kind);
   2922   H_PUT_8 (abfd, in->size, ex->size);
   2923   H_PUT_16 (abfd, in->section, ex->section);
   2924   H_PUT_32 (abfd, in->info, ex->info);
   2925 }
   2926 
   2927 /* Swap in an abiflags structure.  */
   2928 
   2929 void
   2930 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
   2931 				  const Elf_External_ABIFlags_v0 *ex,
   2932 				  Elf_Internal_ABIFlags_v0 *in)
   2933 {
   2934   in->version = H_GET_16 (abfd, ex->version);
   2935   in->isa_level = H_GET_8 (abfd, ex->isa_level);
   2936   in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
   2937   in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
   2938   in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
   2939   in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
   2940   in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
   2941   in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
   2942   in->ases = H_GET_32 (abfd, ex->ases);
   2943   in->flags1 = H_GET_32 (abfd, ex->flags1);
   2944   in->flags2 = H_GET_32 (abfd, ex->flags2);
   2945 }
   2946 
   2947 /* Swap out an abiflags structure.  */
   2948 
   2949 void
   2950 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
   2951 				   const Elf_Internal_ABIFlags_v0 *in,
   2952 				   Elf_External_ABIFlags_v0 *ex)
   2953 {
   2954   H_PUT_16 (abfd, in->version, ex->version);
   2955   H_PUT_8 (abfd, in->isa_level, ex->isa_level);
   2956   H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
   2957   H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
   2958   H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
   2959   H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
   2960   H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
   2961   H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
   2962   H_PUT_32 (abfd, in->ases, ex->ases);
   2963   H_PUT_32 (abfd, in->flags1, ex->flags1);
   2964   H_PUT_32 (abfd, in->flags2, ex->flags2);
   2965 }
   2966 
   2967 /* This function is called via qsort() to sort the dynamic relocation
   2969    entries by increasing r_symndx value.  */
   2970 
   2971 static int
   2972 sort_dynamic_relocs (const void *arg1, const void *arg2)
   2973 {
   2974   Elf_Internal_Rela int_reloc1;
   2975   Elf_Internal_Rela int_reloc2;
   2976   int diff;
   2977 
   2978   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
   2979   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
   2980 
   2981   diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
   2982   if (diff != 0)
   2983     return diff;
   2984 
   2985   if (int_reloc1.r_offset < int_reloc2.r_offset)
   2986     return -1;
   2987   if (int_reloc1.r_offset > int_reloc2.r_offset)
   2988     return 1;
   2989   return 0;
   2990 }
   2991 
   2992 /* Like sort_dynamic_relocs, but used for elf64 relocations.  */
   2993 
   2994 static int
   2995 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
   2996 			const void *arg2 ATTRIBUTE_UNUSED)
   2997 {
   2998 #ifdef BFD64
   2999   Elf_Internal_Rela int_reloc1[3];
   3000   Elf_Internal_Rela int_reloc2[3];
   3001 
   3002   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
   3003     (reldyn_sorting_bfd, arg1, int_reloc1);
   3004   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
   3005     (reldyn_sorting_bfd, arg2, int_reloc2);
   3006 
   3007   if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
   3008     return -1;
   3009   if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
   3010     return 1;
   3011 
   3012   if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
   3013     return -1;
   3014   if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
   3015     return 1;
   3016   return 0;
   3017 #else
   3018   abort ();
   3019 #endif
   3020 }
   3021 
   3022 
   3023 /* This routine is used to write out ECOFF debugging external symbol
   3024    information.  It is called via mips_elf_link_hash_traverse.  The
   3025    ECOFF external symbol information must match the ELF external
   3026    symbol information.  Unfortunately, at this point we don't know
   3027    whether a symbol is required by reloc information, so the two
   3028    tables may wind up being different.  We must sort out the external
   3029    symbol information before we can set the final size of the .mdebug
   3030    section, and we must set the size of the .mdebug section before we
   3031    can relocate any sections, and we can't know which symbols are
   3032    required by relocation until we relocate the sections.
   3033    Fortunately, it is relatively unlikely that any symbol will be
   3034    stripped but required by a reloc.  In particular, it can not happen
   3035    when generating a final executable.  */
   3036 
   3037 static bool
   3038 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
   3039 {
   3040   struct extsym_info *einfo = data;
   3041   bool strip;
   3042   asection *sec, *output_section;
   3043 
   3044   if (h->root.indx == -2)
   3045     strip = false;
   3046   else if ((h->root.def_dynamic
   3047 	    || h->root.ref_dynamic
   3048 	    || h->root.type == bfd_link_hash_new)
   3049 	   && !h->root.def_regular
   3050 	   && !h->root.ref_regular)
   3051     strip = true;
   3052   else if (einfo->info->strip == strip_all
   3053 	   || (einfo->info->strip == strip_some
   3054 	       && bfd_hash_lookup (einfo->info->keep_hash,
   3055 				   h->root.root.root.string,
   3056 				   false, false) == NULL))
   3057     strip = true;
   3058   else
   3059     strip = false;
   3060 
   3061   if (strip)
   3062     return true;
   3063 
   3064   if (h->esym.ifd == -2)
   3065     {
   3066       h->esym.jmptbl = 0;
   3067       h->esym.cobol_main = 0;
   3068       h->esym.weakext = 0;
   3069       h->esym.reserved = 0;
   3070       h->esym.ifd = ifdNil;
   3071       h->esym.asym.value = 0;
   3072       h->esym.asym.st = stGlobal;
   3073 
   3074       if (h->root.root.type == bfd_link_hash_undefined
   3075 	  || h->root.root.type == bfd_link_hash_undefweak)
   3076 	{
   3077 	  const char *name;
   3078 
   3079 	  /* Use undefined class.  Also, set class and type for some
   3080 	     special symbols.  */
   3081 	  name = h->root.root.root.string;
   3082 	  if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
   3083 	      || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
   3084 	    {
   3085 	      h->esym.asym.sc = scData;
   3086 	      h->esym.asym.st = stLabel;
   3087 	      h->esym.asym.value = 0;
   3088 	    }
   3089 	  else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
   3090 	    {
   3091 	      h->esym.asym.sc = scAbs;
   3092 	      h->esym.asym.st = stLabel;
   3093 	      h->esym.asym.value =
   3094 		mips_elf_hash_table (einfo->info)->procedure_count;
   3095 	    }
   3096 	  else
   3097 	    h->esym.asym.sc = scUndefined;
   3098 	}
   3099       else if (h->root.root.type != bfd_link_hash_defined
   3100 	  && h->root.root.type != bfd_link_hash_defweak)
   3101 	h->esym.asym.sc = scAbs;
   3102       else
   3103 	{
   3104 	  const char *name;
   3105 
   3106 	  sec = h->root.root.u.def.section;
   3107 	  output_section = sec->output_section;
   3108 
   3109 	  /* When making a shared library and symbol h is the one from
   3110 	     the another shared library, OUTPUT_SECTION may be null.  */
   3111 	  if (output_section == NULL)
   3112 	    h->esym.asym.sc = scUndefined;
   3113 	  else
   3114 	    {
   3115 	      name = bfd_section_name (output_section);
   3116 
   3117 	      if (strcmp (name, ".text") == 0)
   3118 		h->esym.asym.sc = scText;
   3119 	      else if (strcmp (name, ".data") == 0)
   3120 		h->esym.asym.sc = scData;
   3121 	      else if (strcmp (name, ".sdata") == 0)
   3122 		h->esym.asym.sc = scSData;
   3123 	      else if (strcmp (name, ".rodata") == 0
   3124 		       || strcmp (name, ".rdata") == 0)
   3125 		h->esym.asym.sc = scRData;
   3126 	      else if (strcmp (name, ".bss") == 0)
   3127 		h->esym.asym.sc = scBss;
   3128 	      else if (strcmp (name, ".sbss") == 0)
   3129 		h->esym.asym.sc = scSBss;
   3130 	      else if (strcmp (name, ".init") == 0)
   3131 		h->esym.asym.sc = scInit;
   3132 	      else if (strcmp (name, ".fini") == 0)
   3133 		h->esym.asym.sc = scFini;
   3134 	      else
   3135 		h->esym.asym.sc = scAbs;
   3136 	    }
   3137 	}
   3138 
   3139       h->esym.asym.reserved = 0;
   3140       h->esym.asym.index = indexNil;
   3141     }
   3142 
   3143   if (h->root.root.type == bfd_link_hash_common)
   3144     h->esym.asym.value = h->root.root.u.c.size;
   3145   else if (h->root.root.type == bfd_link_hash_defined
   3146 	   || h->root.root.type == bfd_link_hash_defweak)
   3147     {
   3148       if (h->esym.asym.sc == scCommon)
   3149 	h->esym.asym.sc = scBss;
   3150       else if (h->esym.asym.sc == scSCommon)
   3151 	h->esym.asym.sc = scSBss;
   3152 
   3153       sec = h->root.root.u.def.section;
   3154       output_section = sec->output_section;
   3155       if (output_section != NULL)
   3156 	h->esym.asym.value = (h->root.root.u.def.value
   3157 			      + sec->output_offset
   3158 			      + output_section->vma);
   3159       else
   3160 	h->esym.asym.value = 0;
   3161     }
   3162   else
   3163     {
   3164       struct mips_elf_link_hash_entry *hd = h;
   3165 
   3166       while (hd->root.root.type == bfd_link_hash_indirect)
   3167 	hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
   3168 
   3169       if (hd->needs_lazy_stub)
   3170 	{
   3171 	  BFD_ASSERT (hd->root.plt.plist != NULL);
   3172 	  BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
   3173 	  /* Set type and value for a symbol with a function stub.  */
   3174 	  h->esym.asym.st = stProc;
   3175 	  sec = hd->root.root.u.def.section;
   3176 	  if (sec == NULL)
   3177 	    h->esym.asym.value = 0;
   3178 	  else
   3179 	    {
   3180 	      output_section = sec->output_section;
   3181 	      if (output_section != NULL)
   3182 		h->esym.asym.value = (hd->root.plt.plist->stub_offset
   3183 				      + sec->output_offset
   3184 				      + output_section->vma);
   3185 	      else
   3186 		h->esym.asym.value = 0;
   3187 	    }
   3188 	}
   3189     }
   3190 
   3191   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
   3192 				      h->root.root.root.string,
   3193 				      &h->esym))
   3194     {
   3195       einfo->failed = true;
   3196       return false;
   3197     }
   3198 
   3199   return true;
   3200 }
   3201 
   3202 /* A comparison routine used to sort .gptab entries.  */
   3203 
   3204 static int
   3205 gptab_compare (const void *p1, const void *p2)
   3206 {
   3207   const Elf32_gptab *a1 = p1;
   3208   const Elf32_gptab *a2 = p2;
   3209 
   3210   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
   3211 }
   3212 
   3213 /* Functions to manage the got entry hash table.  */
   3215 
   3216 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
   3217    hash number.  */
   3218 
   3219 static inline hashval_t
   3220 mips_elf_hash_bfd_vma (bfd_vma addr)
   3221 {
   3222 #ifdef BFD64
   3223   return addr + (addr >> 32);
   3224 #else
   3225   return addr;
   3226 #endif
   3227 }
   3228 
   3229 static hashval_t
   3230 mips_elf_got_entry_hash (const void *entry_)
   3231 {
   3232   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
   3233 
   3234   return (entry->symndx
   3235 	  + ((entry->tls_type == GOT_TLS_LDM) << 18)
   3236 	  + (entry->tls_type == GOT_TLS_LDM ? 0
   3237 	     : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
   3238 	     : entry->symndx >= 0 ? (entry->abfd->id
   3239 				     + mips_elf_hash_bfd_vma (entry->d.addend))
   3240 	     : entry->d.h->root.root.root.hash));
   3241 }
   3242 
   3243 static int
   3244 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
   3245 {
   3246   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
   3247   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
   3248 
   3249   return (e1->symndx == e2->symndx
   3250 	  && e1->tls_type == e2->tls_type
   3251 	  && (e1->tls_type == GOT_TLS_LDM ? true
   3252 	      : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
   3253 	      : e1->symndx >= 0 ? (e1->abfd == e2->abfd
   3254 				   && e1->d.addend == e2->d.addend)
   3255 	      : e2->abfd && e1->d.h == e2->d.h));
   3256 }
   3257 
   3258 static hashval_t
   3259 mips_got_page_ref_hash (const void *ref_)
   3260 {
   3261   const struct mips_got_page_ref *ref;
   3262 
   3263   ref = (const struct mips_got_page_ref *) ref_;
   3264   return ((ref->symndx >= 0
   3265 	   ? (hashval_t) (ref->u.abfd->id + ref->symndx)
   3266 	   : ref->u.h->root.root.root.hash)
   3267 	  + mips_elf_hash_bfd_vma (ref->addend));
   3268 }
   3269 
   3270 static int
   3271 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
   3272 {
   3273   const struct mips_got_page_ref *ref1, *ref2;
   3274 
   3275   ref1 = (const struct mips_got_page_ref *) ref1_;
   3276   ref2 = (const struct mips_got_page_ref *) ref2_;
   3277   return (ref1->symndx == ref2->symndx
   3278 	  && (ref1->symndx < 0
   3279 	      ? ref1->u.h == ref2->u.h
   3280 	      : ref1->u.abfd == ref2->u.abfd)
   3281 	  && ref1->addend == ref2->addend);
   3282 }
   3283 
   3284 static hashval_t
   3285 mips_got_page_entry_hash (const void *entry_)
   3286 {
   3287   const struct mips_got_page_entry *entry;
   3288 
   3289   entry = (const struct mips_got_page_entry *) entry_;
   3290   return entry->sec->id;
   3291 }
   3292 
   3293 static int
   3294 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
   3295 {
   3296   const struct mips_got_page_entry *entry1, *entry2;
   3297 
   3298   entry1 = (const struct mips_got_page_entry *) entry1_;
   3299   entry2 = (const struct mips_got_page_entry *) entry2_;
   3300   return entry1->sec == entry2->sec;
   3301 }
   3302 
   3303 /* Create and return a new mips_got_info structure.  */
   3305 
   3306 static struct mips_got_info *
   3307 mips_elf_create_got_info (bfd *abfd)
   3308 {
   3309   struct mips_got_info *g;
   3310 
   3311   g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
   3312   if (g == NULL)
   3313     return NULL;
   3314 
   3315   g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
   3316 				    mips_elf_got_entry_eq, NULL);
   3317   if (g->got_entries == NULL)
   3318     return NULL;
   3319 
   3320   g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
   3321 				      mips_got_page_ref_eq, NULL);
   3322   if (g->got_page_refs == NULL)
   3323     return NULL;
   3324 
   3325   return g;
   3326 }
   3327 
   3328 /* Return the GOT info for input bfd ABFD, trying to create a new one if
   3329    CREATE_P and if ABFD doesn't already have a GOT.  */
   3330 
   3331 static struct mips_got_info *
   3332 mips_elf_bfd_got (bfd *abfd, bool create_p)
   3333 {
   3334   struct mips_elf_obj_tdata *tdata;
   3335 
   3336   if (!is_mips_elf (abfd))
   3337     return NULL;
   3338 
   3339   tdata = mips_elf_tdata (abfd);
   3340   if (!tdata->got && create_p)
   3341     tdata->got = mips_elf_create_got_info (abfd);
   3342   return tdata->got;
   3343 }
   3344 
   3345 /* Record that ABFD should use output GOT G.  */
   3346 
   3347 static void
   3348 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
   3349 {
   3350   struct mips_elf_obj_tdata *tdata;
   3351 
   3352   BFD_ASSERT (is_mips_elf (abfd));
   3353   tdata = mips_elf_tdata (abfd);
   3354   if (tdata->got)
   3355     {
   3356       /* The GOT structure itself and the hash table entries are
   3357 	 allocated to a bfd, but the hash tables aren't.  */
   3358       htab_delete (tdata->got->got_entries);
   3359       htab_delete (tdata->got->got_page_refs);
   3360       if (tdata->got->got_page_entries)
   3361 	htab_delete (tdata->got->got_page_entries);
   3362     }
   3363   tdata->got = g;
   3364 }
   3365 
   3366 /* Return the dynamic relocation section.  If it doesn't exist, try to
   3367    create a new it if CREATE_P, otherwise return NULL.  Also return NULL
   3368    if creation fails.  */
   3369 
   3370 static asection *
   3371 mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
   3372 {
   3373   const char *dname;
   3374   asection *sreloc;
   3375   bfd *dynobj;
   3376 
   3377   dname = MIPS_ELF_REL_DYN_NAME (info);
   3378   dynobj = elf_hash_table (info)->dynobj;
   3379   sreloc = bfd_get_linker_section (dynobj, dname);
   3380   if (sreloc == NULL && create_p)
   3381     {
   3382       sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
   3383 						   (SEC_ALLOC
   3384 						    | SEC_LOAD
   3385 						    | SEC_HAS_CONTENTS
   3386 						    | SEC_IN_MEMORY
   3387 						    | SEC_LINKER_CREATED
   3388 						    | SEC_READONLY));
   3389       if (sreloc == NULL
   3390 	  || !bfd_set_section_alignment (sreloc,
   3391 					 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
   3392 	return NULL;
   3393     }
   3394   return sreloc;
   3395 }
   3396 
   3397 /* Return the GOT_TLS_* type required by relocation type R_TYPE.  */
   3398 
   3399 static int
   3400 mips_elf_reloc_tls_type (unsigned int r_type)
   3401 {
   3402   if (tls_gd_reloc_p (r_type))
   3403     return GOT_TLS_GD;
   3404 
   3405   if (tls_ldm_reloc_p (r_type))
   3406     return GOT_TLS_LDM;
   3407 
   3408   if (tls_gottprel_reloc_p (r_type))
   3409     return GOT_TLS_IE;
   3410 
   3411   return GOT_TLS_NONE;
   3412 }
   3413 
   3414 /* Return the number of GOT slots needed for GOT TLS type TYPE.  */
   3415 
   3416 static int
   3417 mips_tls_got_entries (unsigned int type)
   3418 {
   3419   switch (type)
   3420     {
   3421     case GOT_TLS_GD:
   3422     case GOT_TLS_LDM:
   3423       return 2;
   3424 
   3425     case GOT_TLS_IE:
   3426       return 1;
   3427 
   3428     case GOT_TLS_NONE:
   3429       return 0;
   3430     }
   3431   abort ();
   3432 }
   3433 
   3434 /* Count the number of relocations needed for a TLS GOT entry, with
   3435    access types from TLS_TYPE, and symbol H (or a local symbol if H
   3436    is NULL).  */
   3437 
   3438 static int
   3439 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
   3440 		     struct elf_link_hash_entry *h)
   3441 {
   3442   int indx = 0;
   3443   bool need_relocs = false;
   3444   bool dyn = elf_hash_table (info)->dynamic_sections_created;
   3445 
   3446   if (h != NULL
   3447       && h->dynindx != -1
   3448       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
   3449       && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
   3450     indx = h->dynindx;
   3451 
   3452   if ((bfd_link_dll (info) || indx != 0)
   3453       && (h == NULL
   3454 	  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3455 	  || h->root.type != bfd_link_hash_undefweak))
   3456     need_relocs = true;
   3457 
   3458   if (!need_relocs)
   3459     return 0;
   3460 
   3461   switch (tls_type)
   3462     {
   3463     case GOT_TLS_GD:
   3464       return indx != 0 ? 2 : 1;
   3465 
   3466     case GOT_TLS_IE:
   3467       return 1;
   3468 
   3469     case GOT_TLS_LDM:
   3470       return bfd_link_dll (info) ? 1 : 0;
   3471 
   3472     default:
   3473       return 0;
   3474     }
   3475 }
   3476 
   3477 /* Add the number of GOT entries and TLS relocations required by ENTRY
   3478    to G.  */
   3479 
   3480 static void
   3481 mips_elf_count_got_entry (struct bfd_link_info *info,
   3482 			  struct mips_got_info *g,
   3483 			  struct mips_got_entry *entry)
   3484 {
   3485   if (entry->tls_type)
   3486     {
   3487       g->tls_gotno += mips_tls_got_entries (entry->tls_type);
   3488       g->relocs += mips_tls_got_relocs (info, entry->tls_type,
   3489 					entry->symndx < 0
   3490 					? &entry->d.h->root : NULL);
   3491     }
   3492   else if (entry->symndx >= 0)
   3493     g->local_gotno += 1;
   3494   else if (entry->d.h->global_got_area == GGA_NONE)
   3495     {
   3496       /* On VxWorks, calls can refer directly to the .got.plt entry,
   3497 	 in which case they won't have an entry in the regular GOT.
   3498 	 This is arranged for in `mips_elf_count_got_symbols' and we
   3499 	 need to refrain from counting these entries for the regular
   3500 	 GOT here.  */
   3501       if (mips_elf_hash_table (info)->root.target_os != is_vxworks
   3502 	  || entry->d.h->root.dynindx == -1
   3503 	  || !entry->d.h->got_only_for_calls
   3504 	  || entry->d.h->root.plt.plist->mips_offset == MINUS_ONE)
   3505 	g->local_gotno += 1;
   3506     }
   3507   else
   3508     g->global_gotno += 1;
   3509 }
   3510 
   3511 /* Output a simple dynamic relocation into SRELOC.  */
   3512 
   3513 static void
   3514 mips_elf_output_dynamic_relocation (bfd *output_bfd,
   3515 				    asection *sreloc,
   3516 				    unsigned long reloc_index,
   3517 				    unsigned long indx,
   3518 				    int r_type,
   3519 				    bfd_vma offset)
   3520 {
   3521   Elf_Internal_Rela rel[3];
   3522 
   3523   memset (rel, 0, sizeof (rel));
   3524 
   3525   rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
   3526   rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
   3527 
   3528   if (ABI_64_P (output_bfd))
   3529     {
   3530       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
   3531 	(output_bfd, &rel[0],
   3532 	 (sreloc->contents
   3533 	  + reloc_index * sizeof (Elf64_Mips_External_Rel)));
   3534     }
   3535   else
   3536     bfd_elf32_swap_reloc_out
   3537       (output_bfd, &rel[0],
   3538        (sreloc->contents
   3539 	+ reloc_index * sizeof (Elf32_External_Rel)));
   3540 }
   3541 
   3542 /* Initialize a set of TLS GOT entries for one symbol.  */
   3543 
   3544 static void
   3545 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
   3546 			       struct mips_got_entry *entry,
   3547 			       struct mips_elf_link_hash_entry *h,
   3548 			       bfd_vma value)
   3549 {
   3550   bool dyn = elf_hash_table (info)->dynamic_sections_created;
   3551   struct mips_elf_link_hash_table *htab;
   3552   int indx;
   3553   asection *sreloc, *sgot;
   3554   bfd_vma got_offset, got_offset2;
   3555   bool need_relocs = false;
   3556 
   3557   htab = mips_elf_hash_table (info);
   3558   if (htab == NULL)
   3559     return;
   3560 
   3561   sgot = htab->root.sgot;
   3562 
   3563   indx = 0;
   3564   if (h != NULL
   3565       && h->root.dynindx != -1
   3566       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
   3567       && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
   3568     indx = h->root.dynindx;
   3569 
   3570   if (entry->tls_initialized)
   3571     return;
   3572 
   3573   if ((bfd_link_dll (info) || indx != 0)
   3574       && (h == NULL
   3575 	  || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
   3576 	  || h->root.type != bfd_link_hash_undefweak))
   3577     need_relocs = true;
   3578 
   3579   /* MINUS_ONE means the symbol is not defined in this object.  It may not
   3580      be defined at all; assume that the value doesn't matter in that
   3581      case.  Otherwise complain if we would use the value.  */
   3582   BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
   3583 	      || h->root.root.type == bfd_link_hash_undefweak);
   3584 
   3585   /* Emit necessary relocations.  */
   3586   sreloc = mips_elf_rel_dyn_section (info, false);
   3587   got_offset = entry->gotidx;
   3588 
   3589   switch (entry->tls_type)
   3590     {
   3591     case GOT_TLS_GD:
   3592       /* General Dynamic.  */
   3593       got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
   3594 
   3595       if (need_relocs)
   3596 	{
   3597 	  mips_elf_output_dynamic_relocation
   3598 	    (abfd, sreloc, sreloc->reloc_count++, indx,
   3599 	     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
   3600 	     sgot->output_offset + sgot->output_section->vma + got_offset);
   3601 
   3602 	  if (indx)
   3603 	    mips_elf_output_dynamic_relocation
   3604 	      (abfd, sreloc, sreloc->reloc_count++, indx,
   3605 	       ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
   3606 	       sgot->output_offset + sgot->output_section->vma + got_offset2);
   3607 	  else
   3608 	    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
   3609 			       sgot->contents + got_offset2);
   3610 	}
   3611       else
   3612 	{
   3613 	  MIPS_ELF_PUT_WORD (abfd, 1,
   3614 			     sgot->contents + got_offset);
   3615 	  MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
   3616 			     sgot->contents + got_offset2);
   3617 	}
   3618       break;
   3619 
   3620     case GOT_TLS_IE:
   3621       /* Initial Exec model.  */
   3622       if (need_relocs)
   3623 	{
   3624 	  if (indx == 0)
   3625 	    MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
   3626 			       sgot->contents + got_offset);
   3627 	  else
   3628 	    MIPS_ELF_PUT_WORD (abfd, 0,
   3629 			       sgot->contents + got_offset);
   3630 
   3631 	  mips_elf_output_dynamic_relocation
   3632 	    (abfd, sreloc, sreloc->reloc_count++, indx,
   3633 	     ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
   3634 	     sgot->output_offset + sgot->output_section->vma + got_offset);
   3635 	}
   3636       else
   3637 	MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
   3638 			   sgot->contents + got_offset);
   3639       break;
   3640 
   3641     case GOT_TLS_LDM:
   3642       /* The initial offset is zero, and the LD offsets will include the
   3643 	 bias by DTP_OFFSET.  */
   3644       MIPS_ELF_PUT_WORD (abfd, 0,
   3645 			 sgot->contents + got_offset
   3646 			 + MIPS_ELF_GOT_SIZE (abfd));
   3647 
   3648       if (!bfd_link_dll (info))
   3649 	MIPS_ELF_PUT_WORD (abfd, 1,
   3650 			   sgot->contents + got_offset);
   3651       else
   3652 	mips_elf_output_dynamic_relocation
   3653 	  (abfd, sreloc, sreloc->reloc_count++, indx,
   3654 	   ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
   3655 	   sgot->output_offset + sgot->output_section->vma + got_offset);
   3656       break;
   3657 
   3658     default:
   3659       abort ();
   3660     }
   3661 
   3662   entry->tls_initialized = true;
   3663 }
   3664 
   3665 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
   3666    for global symbol H.  .got.plt comes before the GOT, so the offset
   3667    will be negative.  */
   3668 
   3669 static bfd_vma
   3670 mips_elf_gotplt_index (struct bfd_link_info *info,
   3671 		       struct elf_link_hash_entry *h)
   3672 {
   3673   bfd_vma got_address, got_value;
   3674   struct mips_elf_link_hash_table *htab;
   3675 
   3676   htab = mips_elf_hash_table (info);
   3677   BFD_ASSERT (htab != NULL);
   3678 
   3679   BFD_ASSERT (h->plt.plist != NULL);
   3680   BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
   3681 
   3682   /* Calculate the address of the associated .got.plt entry.  */
   3683   got_address = (htab->root.sgotplt->output_section->vma
   3684 		 + htab->root.sgotplt->output_offset
   3685 		 + (h->plt.plist->gotplt_index
   3686 		    * MIPS_ELF_GOT_SIZE (info->output_bfd)));
   3687 
   3688   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
   3689   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
   3690 	       + htab->root.hgot->root.u.def.section->output_offset
   3691 	       + htab->root.hgot->root.u.def.value);
   3692 
   3693   return got_address - got_value;
   3694 }
   3695 
   3696 /* Return the GOT offset for address VALUE.   If there is not yet a GOT
   3697    entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
   3698    create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
   3699    offset can be found.  */
   3700 
   3701 static bfd_vma
   3702 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3703 			  bfd_vma value, unsigned long r_symndx,
   3704 			  struct mips_elf_link_hash_entry *h, int r_type)
   3705 {
   3706   struct mips_elf_link_hash_table *htab;
   3707   struct mips_got_entry *entry;
   3708 
   3709   htab = mips_elf_hash_table (info);
   3710   BFD_ASSERT (htab != NULL);
   3711 
   3712   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
   3713 					   r_symndx, h, r_type);
   3714   if (!entry)
   3715     return MINUS_ONE;
   3716 
   3717   if (entry->tls_type)
   3718     mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
   3719   return entry->gotidx;
   3720 }
   3721 
   3722 /* Return the GOT index of global symbol H in the primary GOT.  */
   3723 
   3724 static bfd_vma
   3725 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
   3726 				   struct elf_link_hash_entry *h)
   3727 {
   3728   struct mips_elf_link_hash_table *htab;
   3729   long global_got_dynindx;
   3730   struct mips_got_info *g;
   3731   bfd_vma got_index;
   3732 
   3733   htab = mips_elf_hash_table (info);
   3734   BFD_ASSERT (htab != NULL);
   3735 
   3736   global_got_dynindx = 0;
   3737   if (htab->global_gotsym != NULL)
   3738     global_got_dynindx = htab->global_gotsym->dynindx;
   3739 
   3740   /* Once we determine the global GOT entry with the lowest dynamic
   3741      symbol table index, we must put all dynamic symbols with greater
   3742      indices into the primary GOT.  That makes it easy to calculate the
   3743      GOT offset.  */
   3744   BFD_ASSERT (h->dynindx >= global_got_dynindx);
   3745   g = mips_elf_bfd_got (obfd, false);
   3746   got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
   3747 	       * MIPS_ELF_GOT_SIZE (obfd));
   3748   BFD_ASSERT (got_index < htab->root.sgot->size);
   3749 
   3750   return got_index;
   3751 }
   3752 
   3753 /* Return the GOT index for the global symbol indicated by H, which is
   3754    referenced by a relocation of type R_TYPE in IBFD.  */
   3755 
   3756 static bfd_vma
   3757 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
   3758 			   struct elf_link_hash_entry *h, int r_type)
   3759 {
   3760   struct mips_elf_link_hash_table *htab;
   3761   struct mips_got_info *g;
   3762   struct mips_got_entry lookup, *entry;
   3763   bfd_vma gotidx;
   3764 
   3765   htab = mips_elf_hash_table (info);
   3766   BFD_ASSERT (htab != NULL);
   3767 
   3768   g = mips_elf_bfd_got (ibfd, false);
   3769   BFD_ASSERT (g);
   3770 
   3771   lookup.tls_type = mips_elf_reloc_tls_type (r_type);
   3772   if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
   3773     return mips_elf_primary_global_got_index (obfd, info, h);
   3774 
   3775   lookup.abfd = ibfd;
   3776   lookup.symndx = -1;
   3777   lookup.d.h = (struct mips_elf_link_hash_entry *) h;
   3778   entry = htab_find (g->got_entries, &lookup);
   3779   BFD_ASSERT (entry);
   3780 
   3781   gotidx = entry->gotidx;
   3782   BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
   3783 
   3784   if (lookup.tls_type)
   3785     {
   3786       bfd_vma value = MINUS_ONE;
   3787 
   3788       if ((h->root.type == bfd_link_hash_defined
   3789 	   || h->root.type == bfd_link_hash_defweak)
   3790 	  && h->root.u.def.section->output_section)
   3791 	value = (h->root.u.def.value
   3792 		 + h->root.u.def.section->output_offset
   3793 		 + h->root.u.def.section->output_section->vma);
   3794 
   3795       mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
   3796     }
   3797   return gotidx;
   3798 }
   3799 
   3800 /* Find a GOT page entry that points to within 32KB of VALUE.  These
   3801    entries are supposed to be placed at small offsets in the GOT, i.e.,
   3802    within 32KB of GP.  Return the index of the GOT entry, or -1 if no
   3803    entry could be created.  If OFFSETP is nonnull, use it to return the
   3804    offset of the GOT entry from VALUE.  */
   3805 
   3806 static bfd_vma
   3807 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3808 		   bfd_vma value, bfd_vma *offsetp)
   3809 {
   3810   bfd_vma page, got_index;
   3811   struct mips_got_entry *entry;
   3812 
   3813   page = (value + 0x8000) & ~(bfd_vma) 0xffff;
   3814   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
   3815 					   NULL, R_MIPS_GOT_PAGE);
   3816 
   3817   if (!entry)
   3818     return MINUS_ONE;
   3819 
   3820   got_index = entry->gotidx;
   3821 
   3822   if (offsetp)
   3823     *offsetp = value - entry->d.address;
   3824 
   3825   return got_index;
   3826 }
   3827 
   3828 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
   3829    EXTERNAL is true if the relocation was originally against a global
   3830    symbol that binds locally.  */
   3831 
   3832 static bfd_vma
   3833 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3834 		      bfd_vma value, bool external)
   3835 {
   3836   struct mips_got_entry *entry;
   3837 
   3838   /* GOT16 relocations against local symbols are followed by a LO16
   3839      relocation; those against global symbols are not.  Thus if the
   3840      symbol was originally local, the GOT16 relocation should load the
   3841      equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
   3842   if (! external)
   3843     value = mips_elf_high (value) << 16;
   3844 
   3845   /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
   3846      R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
   3847      same in all cases.  */
   3848   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
   3849 					   NULL, R_MIPS_GOT16);
   3850   if (entry)
   3851     return entry->gotidx;
   3852   else
   3853     return MINUS_ONE;
   3854 }
   3855 
   3856 /* Returns the offset for the entry at the INDEXth position
   3857    in the GOT.  */
   3858 
   3859 static bfd_vma
   3860 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
   3861 				bfd *input_bfd, bfd_vma got_index)
   3862 {
   3863   struct mips_elf_link_hash_table *htab;
   3864   asection *sgot;
   3865   bfd_vma gp;
   3866 
   3867   htab = mips_elf_hash_table (info);
   3868   BFD_ASSERT (htab != NULL);
   3869 
   3870   sgot = htab->root.sgot;
   3871   gp = _bfd_get_gp_value (output_bfd)
   3872     + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
   3873 
   3874   return sgot->output_section->vma + sgot->output_offset + got_index - gp;
   3875 }
   3876 
   3877 /* Create and return a local GOT entry for VALUE, which was calculated
   3878    from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
   3879    be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
   3880    instead.  */
   3881 
   3882 static struct mips_got_entry *
   3883 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
   3884 				 bfd *ibfd, bfd_vma value,
   3885 				 unsigned long r_symndx,
   3886 				 struct mips_elf_link_hash_entry *h,
   3887 				 int r_type)
   3888 {
   3889   struct mips_got_entry lookup, *entry;
   3890   void **loc;
   3891   struct mips_got_info *g;
   3892   struct mips_elf_link_hash_table *htab;
   3893   bfd_vma gotidx;
   3894 
   3895   htab = mips_elf_hash_table (info);
   3896   BFD_ASSERT (htab != NULL);
   3897 
   3898   g = mips_elf_bfd_got (ibfd, false);
   3899   if (g == NULL)
   3900     {
   3901       g = mips_elf_bfd_got (abfd, false);
   3902       BFD_ASSERT (g != NULL);
   3903     }
   3904 
   3905   /* This function shouldn't be called for symbols that live in the global
   3906      area of the GOT.  */
   3907   BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
   3908 
   3909   lookup.tls_type = mips_elf_reloc_tls_type (r_type);
   3910   if (lookup.tls_type)
   3911     {
   3912       lookup.abfd = ibfd;
   3913       if (tls_ldm_reloc_p (r_type))
   3914 	{
   3915 	  lookup.symndx = 0;
   3916 	  lookup.d.addend = 0;
   3917 	}
   3918       else if (h == NULL)
   3919 	{
   3920 	  lookup.symndx = r_symndx;
   3921 	  lookup.d.addend = 0;
   3922 	}
   3923       else
   3924 	{
   3925 	  lookup.symndx = -1;
   3926 	  lookup.d.h = h;
   3927 	}
   3928 
   3929       entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
   3930       BFD_ASSERT (entry);
   3931 
   3932       gotidx = entry->gotidx;
   3933       BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
   3934 
   3935       return entry;
   3936     }
   3937 
   3938   lookup.abfd = NULL;
   3939   lookup.symndx = -1;
   3940   lookup.d.address = value;
   3941   loc = htab_find_slot (g->got_entries, &lookup, INSERT);
   3942   if (!loc)
   3943     return NULL;
   3944 
   3945   entry = (struct mips_got_entry *) *loc;
   3946   if (entry)
   3947     return entry;
   3948 
   3949   if (g->assigned_low_gotno > g->assigned_high_gotno)
   3950     {
   3951       /* We didn't allocate enough space in the GOT.  */
   3952       _bfd_error_handler
   3953 	(_("not enough GOT space for local GOT entries"));
   3954       bfd_set_error (bfd_error_bad_value);
   3955       return NULL;
   3956     }
   3957 
   3958   entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
   3959   if (!entry)
   3960     return NULL;
   3961 
   3962   if (got16_reloc_p (r_type)
   3963       || call16_reloc_p (r_type)
   3964       || got_page_reloc_p (r_type)
   3965       || got_disp_reloc_p (r_type))
   3966     lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
   3967   else
   3968     lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
   3969 
   3970   *entry = lookup;
   3971   *loc = entry;
   3972 
   3973   MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
   3974 
   3975   /* These GOT entries need a dynamic relocation on VxWorks.  */
   3976   if (htab->root.target_os == is_vxworks)
   3977     {
   3978       Elf_Internal_Rela outrel;
   3979       asection *s;
   3980       bfd_byte *rloc;
   3981       bfd_vma got_address;
   3982 
   3983       s = mips_elf_rel_dyn_section (info, false);
   3984       got_address = (htab->root.sgot->output_section->vma
   3985 		     + htab->root.sgot->output_offset
   3986 		     + entry->gotidx);
   3987 
   3988       rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
   3989       outrel.r_offset = got_address;
   3990       outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
   3991       outrel.r_addend = value;
   3992       bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
   3993     }
   3994 
   3995   return entry;
   3996 }
   3997 
   3998 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
   3999    The number might be exact or a worst-case estimate, depending on how
   4000    much information is available to elf_backend_omit_section_dynsym at
   4001    the current linking stage.  */
   4002 
   4003 static bfd_size_type
   4004 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
   4005 {
   4006   bfd_size_type count;
   4007 
   4008   count = 0;
   4009   if (bfd_link_pic (info))
   4010     {
   4011       asection *p;
   4012       elf_backend_data *bed = get_elf_backend_data (output_bfd);
   4013 
   4014       for (p = output_bfd->sections; p ; p = p->next)
   4015 	if ((p->flags & SEC_EXCLUDE) == 0
   4016 	    && (p->flags & SEC_ALLOC) != 0
   4017 	    && elf_hash_table (info)->dynamic_relocs
   4018 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
   4019 	  ++count;
   4020     }
   4021   return count;
   4022 }
   4023 
   4024 /* Sort the dynamic symbol table so that symbols that need GOT entries
   4025    appear towards the end.  */
   4026 
   4027 static bool
   4028 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
   4029 {
   4030   struct mips_elf_link_hash_table *htab;
   4031   struct mips_elf_hash_sort_data hsd;
   4032   struct mips_got_info *g;
   4033 
   4034   htab = mips_elf_hash_table (info);
   4035   BFD_ASSERT (htab != NULL);
   4036 
   4037   if (htab->root.dynsymcount <= 1)
   4038     return true;
   4039 
   4040   g = htab->got_info;
   4041   if (g == NULL)
   4042     return true;
   4043 
   4044   hsd.low = NULL;
   4045   hsd.max_unref_got_dynindx
   4046     = hsd.min_got_dynindx
   4047     = (htab->root.dynsymcount - g->reloc_only_gotno);
   4048   /* Add 1 to local symbol indices to account for the mandatory NULL entry
   4049      at the head of the table; see `_bfd_elf_link_renumber_dynsyms'.  */
   4050   hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
   4051   hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
   4052   hsd.output_bfd = abfd;
   4053   if (htab->root.dynobj != NULL
   4054       && htab->root.dynamic_sections_created
   4055       && info->emit_gnu_hash)
   4056     {
   4057       asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
   4058       BFD_ASSERT (s != NULL);
   4059       hsd.mipsxhash = s->contents;
   4060       BFD_ASSERT (hsd.mipsxhash != NULL);
   4061     }
   4062   else
   4063     hsd.mipsxhash = NULL;
   4064   mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
   4065 
   4066   /* There should have been enough room in the symbol table to
   4067      accommodate both the GOT and non-GOT symbols.  */
   4068   BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
   4069   BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
   4070   BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
   4071   BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
   4072 
   4073   /* Now we know which dynamic symbol has the lowest dynamic symbol
   4074      table index in the GOT.  */
   4075   htab->global_gotsym = hsd.low;
   4076 
   4077   return true;
   4078 }
   4079 
   4080 /* If H needs a GOT entry, assign it the highest available dynamic
   4081    index.  Otherwise, assign it the lowest available dynamic
   4082    index.  */
   4083 
   4084 static bool
   4085 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
   4086 {
   4087   struct mips_elf_hash_sort_data *hsd = data;
   4088 
   4089   /* Symbols without dynamic symbol table entries aren't interesting
   4090      at all.  */
   4091   if (h->root.dynindx == -1)
   4092     return true;
   4093 
   4094   switch (h->global_got_area)
   4095     {
   4096     case GGA_NONE:
   4097       if (h->root.forced_local)
   4098 	h->root.dynindx = hsd->max_local_dynindx++;
   4099       else
   4100 	h->root.dynindx = hsd->max_non_got_dynindx++;
   4101       break;
   4102 
   4103     case GGA_NORMAL:
   4104       h->root.dynindx = --hsd->min_got_dynindx;
   4105       hsd->low = (struct elf_link_hash_entry *) h;
   4106       break;
   4107 
   4108     case GGA_RELOC_ONLY:
   4109       if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
   4110 	hsd->low = (struct elf_link_hash_entry *) h;
   4111       h->root.dynindx = hsd->max_unref_got_dynindx++;
   4112       break;
   4113     }
   4114 
   4115   /* Populate the .MIPS.xhash translation table entry with
   4116      the symbol dynindx.  */
   4117   if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
   4118     bfd_put_32 (hsd->output_bfd, h->root.dynindx,
   4119 		hsd->mipsxhash + h->mipsxhash_loc);
   4120 
   4121   return true;
   4122 }
   4123 
   4124 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
   4125    (which is owned by the caller and shouldn't be added to the
   4126    hash table directly).  */
   4127 
   4128 static bool
   4129 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
   4130 			   struct mips_got_entry *lookup)
   4131 {
   4132   struct mips_elf_link_hash_table *htab;
   4133   struct mips_got_entry *entry;
   4134   struct mips_got_info *g;
   4135   void **loc, **bfd_loc;
   4136 
   4137   /* Make sure there's a slot for this entry in the master GOT.  */
   4138   htab = mips_elf_hash_table (info);
   4139   g = htab->got_info;
   4140   loc = htab_find_slot (g->got_entries, lookup, INSERT);
   4141   if (!loc)
   4142     return false;
   4143 
   4144   /* Populate the entry if it isn't already.  */
   4145   entry = (struct mips_got_entry *) *loc;
   4146   if (!entry)
   4147     {
   4148       entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
   4149       if (!entry)
   4150 	return false;
   4151 
   4152       lookup->tls_initialized = false;
   4153       lookup->gotidx = -1;
   4154       *entry = *lookup;
   4155       *loc = entry;
   4156     }
   4157 
   4158   /* Reuse the same GOT entry for the BFD's GOT.  */
   4159   g = mips_elf_bfd_got (abfd, true);
   4160   if (!g)
   4161     return false;
   4162 
   4163   bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
   4164   if (!bfd_loc)
   4165     return false;
   4166 
   4167   if (!*bfd_loc)
   4168     *bfd_loc = entry;
   4169   return true;
   4170 }
   4171 
   4172 /* ABFD has a GOT relocation of type R_TYPE against H.  Reserve a GOT
   4173    entry for it.  FOR_CALL is true if the caller is only interested in
   4174    using the GOT entry for calls.  */
   4175 
   4176 static bool
   4177 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
   4178 				   bfd *abfd, struct bfd_link_info *info,
   4179 				   bool for_call, int r_type)
   4180 {
   4181   struct mips_elf_link_hash_table *htab;
   4182   struct mips_elf_link_hash_entry *hmips;
   4183   struct mips_got_entry entry;
   4184   unsigned char tls_type;
   4185 
   4186   htab = mips_elf_hash_table (info);
   4187   BFD_ASSERT (htab != NULL);
   4188 
   4189   hmips = (struct mips_elf_link_hash_entry *) h;
   4190   if (!for_call)
   4191     hmips->got_only_for_calls = false;
   4192 
   4193   /* A global symbol in the GOT must also be in the dynamic symbol
   4194      table.  */
   4195   if (h->dynindx == -1)
   4196     {
   4197       switch (ELF_ST_VISIBILITY (h->other))
   4198 	{
   4199 	case STV_INTERNAL:
   4200 	case STV_HIDDEN:
   4201 	  _bfd_mips_elf_hide_symbol (info, h, true);
   4202 	  break;
   4203 	}
   4204       if (!bfd_elf_link_record_dynamic_symbol (info, h))
   4205 	return false;
   4206     }
   4207 
   4208   tls_type = mips_elf_reloc_tls_type (r_type);
   4209   if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
   4210     hmips->global_got_area = GGA_NORMAL;
   4211 
   4212   entry.abfd = abfd;
   4213   entry.symndx = -1;
   4214   entry.d.h = (struct mips_elf_link_hash_entry *) h;
   4215   entry.tls_type = tls_type;
   4216   return mips_elf_record_got_entry (info, abfd, &entry);
   4217 }
   4218 
   4219 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
   4220    where SYMNDX is a local symbol.  Reserve a GOT entry for it.  */
   4221 
   4222 static bool
   4223 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
   4224 				  struct bfd_link_info *info, int r_type)
   4225 {
   4226   struct mips_elf_link_hash_table *htab;
   4227   struct mips_got_info *g;
   4228   struct mips_got_entry entry;
   4229 
   4230   htab = mips_elf_hash_table (info);
   4231   BFD_ASSERT (htab != NULL);
   4232 
   4233   g = htab->got_info;
   4234   BFD_ASSERT (g != NULL);
   4235 
   4236   entry.abfd = abfd;
   4237   entry.symndx = symndx;
   4238   entry.d.addend = addend;
   4239   entry.tls_type = mips_elf_reloc_tls_type (r_type);
   4240   return mips_elf_record_got_entry (info, abfd, &entry);
   4241 }
   4242 
   4243 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
   4244    H is the symbol's hash table entry, or null if SYMNDX is local
   4245    to ABFD.  */
   4246 
   4247 static bool
   4248 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
   4249 			      long symndx, struct elf_link_hash_entry *h,
   4250 			      bfd_signed_vma addend)
   4251 {
   4252   struct mips_elf_link_hash_table *htab;
   4253   struct mips_got_info *g1, *g2;
   4254   struct mips_got_page_ref lookup, *entry;
   4255   void **loc, **bfd_loc;
   4256 
   4257   htab = mips_elf_hash_table (info);
   4258   BFD_ASSERT (htab != NULL);
   4259 
   4260   g1 = htab->got_info;
   4261   BFD_ASSERT (g1 != NULL);
   4262 
   4263   if (h)
   4264     {
   4265       lookup.symndx = -1;
   4266       lookup.u.h = (struct mips_elf_link_hash_entry *) h;
   4267     }
   4268   else
   4269     {
   4270       lookup.symndx = symndx;
   4271       lookup.u.abfd = abfd;
   4272     }
   4273   lookup.addend = addend;
   4274   loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
   4275   if (loc == NULL)
   4276     return false;
   4277 
   4278   entry = (struct mips_got_page_ref *) *loc;
   4279   if (!entry)
   4280     {
   4281       entry = bfd_alloc (abfd, sizeof (*entry));
   4282       if (!entry)
   4283 	return false;
   4284 
   4285       *entry = lookup;
   4286       *loc = entry;
   4287     }
   4288 
   4289   /* Add the same entry to the BFD's GOT.  */
   4290   g2 = mips_elf_bfd_got (abfd, true);
   4291   if (!g2)
   4292     return false;
   4293 
   4294   bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
   4295   if (!bfd_loc)
   4296     return false;
   4297 
   4298   if (!*bfd_loc)
   4299     *bfd_loc = entry;
   4300 
   4301   return true;
   4302 }
   4303 
   4304 /* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
   4305 
   4306 static void
   4307 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
   4308 				       unsigned int n)
   4309 {
   4310   asection *s;
   4311   struct mips_elf_link_hash_table *htab;
   4312 
   4313   htab = mips_elf_hash_table (info);
   4314   BFD_ASSERT (htab != NULL);
   4315 
   4316   s = mips_elf_rel_dyn_section (info, false);
   4317   BFD_ASSERT (s != NULL);
   4318 
   4319   if (htab->root.target_os == is_vxworks)
   4320     s->size += n * MIPS_ELF_RELA_SIZE (abfd);
   4321   else
   4322     {
   4323       if (s->size == 0)
   4324 	{
   4325 	  /* Make room for a null element.  */
   4326 	  s->size += MIPS_ELF_REL_SIZE (abfd);
   4327 	  ++s->reloc_count;
   4328 	}
   4329       s->size += n * MIPS_ELF_REL_SIZE (abfd);
   4330     }
   4331 }
   4332 
   4333 /* A htab_traverse callback for GOT entries, with DATA pointing to a
   4335    mips_elf_traverse_got_arg structure.  Count the number of GOT
   4336    entries and TLS relocs.  Set DATA->value to true if we need
   4337    to resolve indirect or warning symbols and then recreate the GOT.  */
   4338 
   4339 static int
   4340 mips_elf_check_recreate_got (void **entryp, void *data)
   4341 {
   4342   struct mips_got_entry *entry;
   4343   struct mips_elf_traverse_got_arg *arg;
   4344 
   4345   entry = (struct mips_got_entry *) *entryp;
   4346   arg = (struct mips_elf_traverse_got_arg *) data;
   4347   if (entry->abfd != NULL && entry->symndx == -1)
   4348     {
   4349       struct mips_elf_link_hash_entry *h;
   4350 
   4351       h = entry->d.h;
   4352       if (h->root.root.type == bfd_link_hash_indirect
   4353 	  || h->root.root.type == bfd_link_hash_warning)
   4354 	{
   4355 	  arg->value = true;
   4356 	  return 0;
   4357 	}
   4358     }
   4359   mips_elf_count_got_entry (arg->info, arg->g, entry);
   4360   return 1;
   4361 }
   4362 
   4363 /* A htab_traverse callback for GOT entries, with DATA pointing to a
   4364    mips_elf_traverse_got_arg structure.  Add all entries to DATA->g,
   4365    converting entries for indirect and warning symbols into entries
   4366    for the target symbol.  Set DATA->g to null on error.  */
   4367 
   4368 static int
   4369 mips_elf_recreate_got (void **entryp, void *data)
   4370 {
   4371   struct mips_got_entry new_entry, *entry;
   4372   struct mips_elf_traverse_got_arg *arg;
   4373   void **slot;
   4374 
   4375   entry = (struct mips_got_entry *) *entryp;
   4376   arg = (struct mips_elf_traverse_got_arg *) data;
   4377   if (entry->abfd != NULL
   4378       && entry->symndx == -1
   4379       && (entry->d.h->root.root.type == bfd_link_hash_indirect
   4380 	  || entry->d.h->root.root.type == bfd_link_hash_warning))
   4381     {
   4382       struct mips_elf_link_hash_entry *h;
   4383 
   4384       new_entry = *entry;
   4385       entry = &new_entry;
   4386       h = entry->d.h;
   4387       do
   4388 	{
   4389 	  BFD_ASSERT (h->global_got_area == GGA_NONE);
   4390 	  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   4391 	}
   4392       while (h->root.root.type == bfd_link_hash_indirect
   4393 	     || h->root.root.type == bfd_link_hash_warning);
   4394       entry->d.h = h;
   4395     }
   4396   slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
   4397   if (slot == NULL)
   4398     {
   4399       arg->g = NULL;
   4400       return 0;
   4401     }
   4402   if (*slot == NULL)
   4403     {
   4404       if (entry == &new_entry)
   4405 	{
   4406 	  entry = bfd_alloc (entry->abfd, sizeof (*entry));
   4407 	  if (!entry)
   4408 	    {
   4409 	      arg->g = NULL;
   4410 	      return 0;
   4411 	    }
   4412 	  *entry = new_entry;
   4413 	}
   4414       *slot = entry;
   4415       mips_elf_count_got_entry (arg->info, arg->g, entry);
   4416     }
   4417   return 1;
   4418 }
   4419 
   4420 /* Return the maximum number of GOT page entries required for RANGE.  */
   4421 
   4422 static bfd_vma
   4423 mips_elf_pages_for_range (const struct mips_got_page_range *range)
   4424 {
   4425   return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
   4426 }
   4427 
   4428 /* Record that G requires a page entry that can reach SEC + ADDEND.  */
   4429 
   4430 static bool
   4431 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
   4432 				asection *sec, bfd_signed_vma addend)
   4433 {
   4434   struct mips_got_info *g = arg->g;
   4435   struct mips_got_page_entry lookup, *entry;
   4436   struct mips_got_page_range **range_ptr, *range;
   4437   bfd_vma old_pages, new_pages;
   4438   void **loc;
   4439 
   4440   /* Find the mips_got_page_entry hash table entry for this section.  */
   4441   lookup.sec = sec;
   4442   loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
   4443   if (loc == NULL)
   4444     return false;
   4445 
   4446   /* Create a mips_got_page_entry if this is the first time we've
   4447      seen the section.  */
   4448   entry = (struct mips_got_page_entry *) *loc;
   4449   if (!entry)
   4450     {
   4451       entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
   4452       if (!entry)
   4453 	return false;
   4454 
   4455       entry->sec = sec;
   4456       *loc = entry;
   4457     }
   4458 
   4459   /* Skip over ranges whose maximum extent cannot share a page entry
   4460      with ADDEND.  */
   4461   range_ptr = &entry->ranges;
   4462   while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
   4463     range_ptr = &(*range_ptr)->next;
   4464 
   4465   /* If we scanned to the end of the list, or found a range whose
   4466      minimum extent cannot share a page entry with ADDEND, create
   4467      a new singleton range.  */
   4468   range = *range_ptr;
   4469   if (!range || addend < range->min_addend - 0xffff)
   4470     {
   4471       range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
   4472       if (!range)
   4473 	return false;
   4474 
   4475       range->next = *range_ptr;
   4476       range->min_addend = addend;
   4477       range->max_addend = addend;
   4478 
   4479       *range_ptr = range;
   4480       entry->num_pages++;
   4481       g->page_gotno++;
   4482       return true;
   4483     }
   4484 
   4485   /* Remember how many pages the old range contributed.  */
   4486   old_pages = mips_elf_pages_for_range (range);
   4487 
   4488   /* Update the ranges.  */
   4489   if (addend < range->min_addend)
   4490     range->min_addend = addend;
   4491   else if (addend > range->max_addend)
   4492     {
   4493       if (range->next && addend >= range->next->min_addend - 0xffff)
   4494 	{
   4495 	  old_pages += mips_elf_pages_for_range (range->next);
   4496 	  range->max_addend = range->next->max_addend;
   4497 	  range->next = range->next->next;
   4498 	}
   4499       else
   4500 	range->max_addend = addend;
   4501     }
   4502 
   4503   /* Record any change in the total estimate.  */
   4504   new_pages = mips_elf_pages_for_range (range);
   4505   if (old_pages != new_pages)
   4506     {
   4507       entry->num_pages += new_pages - old_pages;
   4508       g->page_gotno += new_pages - old_pages;
   4509     }
   4510 
   4511   return true;
   4512 }
   4513 
   4514 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
   4515    and for which DATA points to a mips_elf_traverse_got_arg.  Work out
   4516    whether the page reference described by *REFP needs a GOT page entry,
   4517    and record that entry in DATA->g if so.  Set DATA->g to null on failure.  */
   4518 
   4519 static int
   4520 mips_elf_resolve_got_page_ref (void **refp, void *data)
   4521 {
   4522   struct mips_got_page_ref *ref;
   4523   struct mips_elf_traverse_got_arg *arg;
   4524   struct mips_elf_link_hash_table *htab;
   4525   asection *sec;
   4526   bfd_vma addend;
   4527 
   4528   ref = (struct mips_got_page_ref *) *refp;
   4529   arg = (struct mips_elf_traverse_got_arg *) data;
   4530   htab = mips_elf_hash_table (arg->info);
   4531 
   4532   if (ref->symndx < 0)
   4533     {
   4534       struct mips_elf_link_hash_entry *h;
   4535 
   4536       /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries.  */
   4537       h = ref->u.h;
   4538       if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
   4539 	return 1;
   4540 
   4541       /* Ignore undefined symbols; we'll issue an error later if
   4542 	 appropriate.  */
   4543       if (!((h->root.root.type == bfd_link_hash_defined
   4544 	     || h->root.root.type == bfd_link_hash_defweak)
   4545 	    && h->root.root.u.def.section))
   4546 	return 1;
   4547 
   4548       sec = h->root.root.u.def.section;
   4549       addend = h->root.root.u.def.value + ref->addend;
   4550     }
   4551   else
   4552     {
   4553       Elf_Internal_Sym *isym;
   4554 
   4555       /* Read in the symbol.  */
   4556       isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
   4557 				    ref->symndx);
   4558       if (isym == NULL)
   4559 	{
   4560 	  arg->g = NULL;
   4561 	  return 0;
   4562 	}
   4563 
   4564       /* Get the associated input section.  */
   4565       sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
   4566       if (sec == NULL)
   4567 	{
   4568 	  arg->g = NULL;
   4569 	  return 0;
   4570 	}
   4571 
   4572       /* If this is a mergable section, work out the section and offset
   4573 	 of the merged data.  For section symbols, the addend specifies
   4574 	 of the offset _of_ the first byte in the data, otherwise it
   4575 	 specifies the offset _from_ the first byte.  */
   4576       if (sec->flags & SEC_MERGE)
   4577 	{
   4578 	  if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   4579 	    addend = _bfd_merged_section_offset (ref->u.abfd, &sec,
   4580 						 isym->st_value + ref->addend);
   4581 	  else
   4582 	    addend = _bfd_merged_section_offset (ref->u.abfd, &sec,
   4583 						 isym->st_value) + ref->addend;
   4584 	}
   4585       else
   4586 	addend = isym->st_value + ref->addend;
   4587     }
   4588   if (!mips_elf_record_got_page_entry (arg, sec, addend))
   4589     {
   4590       arg->g = NULL;
   4591       return 0;
   4592     }
   4593   return 1;
   4594 }
   4595 
   4596 /* If any entries in G->got_entries are for indirect or warning symbols,
   4597    replace them with entries for the target symbol.  Convert g->got_page_refs
   4598    into got_page_entry structures and estimate the number of page entries
   4599    that they require.  */
   4600 
   4601 static bool
   4602 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
   4603 				    struct mips_got_info *g)
   4604 {
   4605   struct mips_elf_traverse_got_arg tga;
   4606   struct mips_got_info oldg;
   4607 
   4608   oldg = *g;
   4609 
   4610   tga.info = info;
   4611   tga.g = g;
   4612   tga.value = false;
   4613   htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
   4614   if (tga.value)
   4615     {
   4616       *g = oldg;
   4617       g->got_entries = htab_create (htab_size (oldg.got_entries),
   4618 				    mips_elf_got_entry_hash,
   4619 				    mips_elf_got_entry_eq, NULL);
   4620       if (!g->got_entries)
   4621 	return false;
   4622 
   4623       htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
   4624       if (!tga.g)
   4625 	return false;
   4626 
   4627       htab_delete (oldg.got_entries);
   4628     }
   4629 
   4630   g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
   4631 					 mips_got_page_entry_eq, NULL);
   4632   if (g->got_page_entries == NULL)
   4633     return false;
   4634 
   4635   tga.info = info;
   4636   tga.g = g;
   4637   htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
   4638 
   4639   return true;
   4640 }
   4641 
   4642 /* Return true if a GOT entry for H should live in the local rather than
   4643    global GOT area.  */
   4644 
   4645 static bool
   4646 mips_use_local_got_p (struct bfd_link_info *info,
   4647 		      struct mips_elf_link_hash_entry *h)
   4648 {
   4649   /* Symbols that aren't in the dynamic symbol table must live in the
   4650      local GOT.  This includes symbols that are completely undefined
   4651      and which therefore don't bind locally.  We'll report undefined
   4652      symbols later if appropriate.  */
   4653   if (h->root.dynindx == -1)
   4654     return true;
   4655 
   4656   /* Absolute symbols, if ever they need a GOT entry, cannot ever go
   4657      to the local GOT, as they would be implicitly relocated by the
   4658      base address by the dynamic loader.  */
   4659   if (bfd_is_abs_symbol (&h->root.root))
   4660     return false;
   4661 
   4662   /* Symbols that bind locally can (and in the case of forced-local
   4663      symbols, must) live in the local GOT.  */
   4664   if (h->got_only_for_calls
   4665       ? SYMBOL_CALLS_LOCAL (info, &h->root)
   4666       : SYMBOL_REFERENCES_LOCAL (info, &h->root))
   4667     return true;
   4668 
   4669   /* If this is an executable that must provide a definition of the symbol,
   4670      either though PLTs or copy relocations, then that address should go in
   4671      the local rather than global GOT.  */
   4672   if (bfd_link_executable (info) && h->has_static_relocs)
   4673     return true;
   4674 
   4675   return false;
   4676 }
   4677 
   4678 /* A mips_elf_link_hash_traverse callback for which DATA points to the
   4679    link_info structure.  Decide whether the hash entry needs an entry in
   4680    the global part of the primary GOT, setting global_got_area accordingly.
   4681    Count the number of global symbols that are in the primary GOT only
   4682    because they have relocations against them (reloc_only_gotno).  */
   4683 
   4684 static bool
   4685 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
   4686 {
   4687   struct bfd_link_info *info;
   4688   struct mips_elf_link_hash_table *htab;
   4689   struct mips_got_info *g;
   4690 
   4691   info = (struct bfd_link_info *) data;
   4692   htab = mips_elf_hash_table (info);
   4693   g = htab->got_info;
   4694   if (h->global_got_area != GGA_NONE)
   4695     {
   4696       /* Make a final decision about whether the symbol belongs in the
   4697 	 local or global GOT.  */
   4698       if (mips_use_local_got_p (info, h))
   4699 	/* The symbol belongs in the local GOT.  We no longer need this
   4700 	   entry if it was only used for relocations; those relocations
   4701 	   will be against the null or section symbol instead of H.  */
   4702 	h->global_got_area = GGA_NONE;
   4703       else if (htab->root.target_os == is_vxworks
   4704 	       && h->got_only_for_calls
   4705 	       && h->root.plt.plist->mips_offset != MINUS_ONE)
   4706 	/* On VxWorks, calls can refer directly to the .got.plt entry;
   4707 	   they don't need entries in the regular GOT.  .got.plt entries
   4708 	   will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
   4709 	h->global_got_area = GGA_NONE;
   4710       else if (h->global_got_area == GGA_RELOC_ONLY)
   4711 	{
   4712 	  g->reloc_only_gotno++;
   4713 	  g->global_gotno++;
   4714 	}
   4715     }
   4716   return 1;
   4717 }
   4718 
   4719 /* A htab_traverse callback for GOT entries.  Add each one to the GOT
   4721    given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
   4722 
   4723 static int
   4724 mips_elf_add_got_entry (void **entryp, void *data)
   4725 {
   4726   struct mips_got_entry *entry;
   4727   struct mips_elf_traverse_got_arg *arg;
   4728   void **slot;
   4729 
   4730   entry = (struct mips_got_entry *) *entryp;
   4731   arg = (struct mips_elf_traverse_got_arg *) data;
   4732   slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
   4733   if (!slot)
   4734     {
   4735       arg->g = NULL;
   4736       return 0;
   4737     }
   4738   if (!*slot)
   4739     {
   4740       *slot = entry;
   4741       mips_elf_count_got_entry (arg->info, arg->g, entry);
   4742     }
   4743   return 1;
   4744 }
   4745 
   4746 /* A htab_traverse callback for GOT page entries.  Add each one to the GOT
   4747    given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
   4748 
   4749 static int
   4750 mips_elf_add_got_page_entry (void **entryp, void *data)
   4751 {
   4752   struct mips_got_page_entry *entry;
   4753   struct mips_elf_traverse_got_arg *arg;
   4754   void **slot;
   4755 
   4756   entry = (struct mips_got_page_entry *) *entryp;
   4757   arg = (struct mips_elf_traverse_got_arg *) data;
   4758   slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
   4759   if (!slot)
   4760     {
   4761       arg->g = NULL;
   4762       return 0;
   4763     }
   4764   if (!*slot)
   4765     {
   4766       *slot = entry;
   4767       arg->g->page_gotno += entry->num_pages;
   4768     }
   4769   return 1;
   4770 }
   4771 
   4772 /* Consider merging FROM, which is ABFD's GOT, into TO.  Return -1 if
   4773    this would lead to overflow, 1 if they were merged successfully,
   4774    and 0 if a merge failed due to lack of memory.  (These values are chosen
   4775    so that nonnegative return values can be returned by a htab_traverse
   4776    callback.)  */
   4777 
   4778 static int
   4779 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
   4780 			 struct mips_got_info *to,
   4781 			 struct mips_elf_got_per_bfd_arg *arg)
   4782 {
   4783   struct mips_elf_traverse_got_arg tga;
   4784   unsigned int estimate;
   4785 
   4786   /* Work out how many page entries we would need for the combined GOT.  */
   4787   estimate = arg->max_pages;
   4788   if (estimate >= from->page_gotno + to->page_gotno)
   4789     estimate = from->page_gotno + to->page_gotno;
   4790 
   4791   /* And conservatively estimate how many local and TLS entries
   4792      would be needed.  */
   4793   estimate += from->local_gotno + to->local_gotno;
   4794   estimate += from->tls_gotno + to->tls_gotno;
   4795 
   4796   /* If we're merging with the primary got, any TLS relocations will
   4797      come after the full set of global entries.  Otherwise estimate those
   4798      conservatively as well.  */
   4799   if (to == arg->primary && from->tls_gotno + to->tls_gotno)
   4800     estimate += arg->global_count;
   4801   else
   4802     estimate += from->global_gotno + to->global_gotno;
   4803 
   4804   /* Bail out if the combined GOT might be too big.  */
   4805   if (estimate > arg->max_count)
   4806     return -1;
   4807 
   4808   /* Transfer the bfd's got information from FROM to TO.  */
   4809   tga.info = arg->info;
   4810   tga.g = to;
   4811   htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
   4812   if (!tga.g)
   4813     return 0;
   4814 
   4815   htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
   4816   if (!tga.g)
   4817     return 0;
   4818 
   4819   mips_elf_replace_bfd_got (abfd, to);
   4820   return 1;
   4821 }
   4822 
   4823 /* Attempt to merge GOT G, which belongs to ABFD.  Try to use as much
   4824    as possible of the primary got, since it doesn't require explicit
   4825    dynamic relocations, but don't use bfds that would reference global
   4826    symbols out of the addressable range.  Failing the primary got,
   4827    attempt to merge with the current got, or finish the current got
   4828    and then make make the new got current.  */
   4829 
   4830 static bool
   4831 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
   4832 		    struct mips_elf_got_per_bfd_arg *arg)
   4833 {
   4834   unsigned int estimate;
   4835   int result;
   4836 
   4837   if (!mips_elf_resolve_final_got_entries (arg->info, g))
   4838     return false;
   4839 
   4840   /* Work out the number of page, local and TLS entries.  */
   4841   estimate = arg->max_pages;
   4842   if (estimate > g->page_gotno)
   4843     estimate = g->page_gotno;
   4844   estimate += g->local_gotno + g->tls_gotno;
   4845 
   4846   /* We place TLS GOT entries after both locals and globals.  The globals
   4847      for the primary GOT may overflow the normal GOT size limit, so be
   4848      sure not to merge a GOT which requires TLS with the primary GOT in that
   4849      case.  This doesn't affect non-primary GOTs.  */
   4850   estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
   4851 
   4852   if (estimate <= arg->max_count)
   4853     {
   4854       /* If we don't have a primary GOT, use it as
   4855 	 a starting point for the primary GOT.  */
   4856       if (!arg->primary)
   4857 	{
   4858 	  arg->primary = g;
   4859 	  return true;
   4860 	}
   4861 
   4862       /* Try merging with the primary GOT.  */
   4863       result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
   4864       if (result >= 0)
   4865 	return result;
   4866     }
   4867 
   4868   /* If we can merge with the last-created got, do it.  */
   4869   if (arg->current)
   4870     {
   4871       result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
   4872       if (result >= 0)
   4873 	return result;
   4874     }
   4875 
   4876   /* Well, we couldn't merge, so create a new GOT.  Don't check if it
   4877      fits; if it turns out that it doesn't, we'll get relocation
   4878      overflows anyway.  */
   4879   g->next = arg->current;
   4880   arg->current = g;
   4881 
   4882   return true;
   4883 }
   4884 
   4885 /* ENTRYP is a hash table entry for a mips_got_entry.  Set its gotidx
   4886    to GOTIDX, duplicating the entry if it has already been assigned
   4887    an index in a different GOT.  */
   4888 
   4889 static bool
   4890 mips_elf_set_gotidx (void **entryp, long gotidx)
   4891 {
   4892   struct mips_got_entry *entry;
   4893 
   4894   entry = (struct mips_got_entry *) *entryp;
   4895   if (entry->gotidx > 0)
   4896     {
   4897       struct mips_got_entry *new_entry;
   4898 
   4899       new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
   4900       if (!new_entry)
   4901 	return false;
   4902 
   4903       *new_entry = *entry;
   4904       *entryp = new_entry;
   4905       entry = new_entry;
   4906     }
   4907   entry->gotidx = gotidx;
   4908   return true;
   4909 }
   4910 
   4911 /* Set the TLS GOT index for the GOT entry in ENTRYP.  DATA points to a
   4912    mips_elf_traverse_got_arg in which DATA->value is the size of one
   4913    GOT entry.  Set DATA->g to null on failure.  */
   4914 
   4915 static int
   4916 mips_elf_initialize_tls_index (void **entryp, void *data)
   4917 {
   4918   struct mips_got_entry *entry;
   4919   struct mips_elf_traverse_got_arg *arg;
   4920 
   4921   /* We're only interested in TLS symbols.  */
   4922   entry = (struct mips_got_entry *) *entryp;
   4923   if (entry->tls_type == GOT_TLS_NONE)
   4924     return 1;
   4925 
   4926   arg = (struct mips_elf_traverse_got_arg *) data;
   4927   if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
   4928     {
   4929       arg->g = NULL;
   4930       return 0;
   4931     }
   4932 
   4933   /* Account for the entries we've just allocated.  */
   4934   arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
   4935   return 1;
   4936 }
   4937 
   4938 /* A htab_traverse callback for GOT entries, where DATA points to a
   4939    mips_elf_traverse_got_arg.  Set the global_got_area of each global
   4940    symbol to DATA->value.  */
   4941 
   4942 static int
   4943 mips_elf_set_global_got_area (void **entryp, void *data)
   4944 {
   4945   struct mips_got_entry *entry;
   4946   struct mips_elf_traverse_got_arg *arg;
   4947 
   4948   entry = (struct mips_got_entry *) *entryp;
   4949   arg = (struct mips_elf_traverse_got_arg *) data;
   4950   if (entry->abfd != NULL
   4951       && entry->symndx == -1
   4952       && entry->d.h->global_got_area != GGA_NONE)
   4953     entry->d.h->global_got_area = arg->value;
   4954   return 1;
   4955 }
   4956 
   4957 /* A htab_traverse callback for secondary GOT entries, where DATA points
   4958    to a mips_elf_traverse_got_arg.  Assign GOT indices to global entries
   4959    and record the number of relocations they require.  DATA->value is
   4960    the size of one GOT entry.  Set DATA->g to null on failure.  */
   4961 
   4962 static int
   4963 mips_elf_set_global_gotidx (void **entryp, void *data)
   4964 {
   4965   struct mips_got_entry *entry;
   4966   struct mips_elf_traverse_got_arg *arg;
   4967 
   4968   entry = (struct mips_got_entry *) *entryp;
   4969   arg = (struct mips_elf_traverse_got_arg *) data;
   4970   if (entry->abfd != NULL
   4971       && entry->symndx == -1
   4972       && entry->d.h->global_got_area != GGA_NONE)
   4973     {
   4974       if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
   4975 	{
   4976 	  arg->g = NULL;
   4977 	  return 0;
   4978 	}
   4979       arg->g->assigned_low_gotno += 1;
   4980 
   4981       if (bfd_link_pic (arg->info)
   4982 	  || (elf_hash_table (arg->info)->dynamic_sections_created
   4983 	      && entry->d.h->root.def_dynamic
   4984 	      && !entry->d.h->root.def_regular))
   4985 	arg->g->relocs += 1;
   4986     }
   4987 
   4988   return 1;
   4989 }
   4990 
   4991 /* A htab_traverse callback for GOT entries for which DATA is the
   4992    bfd_link_info.  Forbid any global symbols from having traditional
   4993    lazy-binding stubs.  */
   4994 
   4995 static int
   4996 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
   4997 {
   4998   struct bfd_link_info *info;
   4999   struct mips_elf_link_hash_table *htab;
   5000   struct mips_got_entry *entry;
   5001 
   5002   entry = (struct mips_got_entry *) *entryp;
   5003   info = (struct bfd_link_info *) data;
   5004   htab = mips_elf_hash_table (info);
   5005   BFD_ASSERT (htab != NULL);
   5006 
   5007   if (entry->abfd != NULL
   5008       && entry->symndx == -1
   5009       && entry->d.h->needs_lazy_stub)
   5010     {
   5011       entry->d.h->needs_lazy_stub = false;
   5012       htab->lazy_stub_count--;
   5013     }
   5014 
   5015   return 1;
   5016 }
   5017 
   5018 /* Return the offset of an input bfd IBFD's GOT from the beginning of
   5019    the primary GOT.  */
   5020 static bfd_vma
   5021 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
   5022 {
   5023   if (!g->next)
   5024     return 0;
   5025 
   5026   g = mips_elf_bfd_got (ibfd, false);
   5027   if (! g)
   5028     return 0;
   5029 
   5030   BFD_ASSERT (g->next);
   5031 
   5032   g = g->next;
   5033 
   5034   return (g->local_gotno + g->global_gotno + g->tls_gotno)
   5035     * MIPS_ELF_GOT_SIZE (abfd);
   5036 }
   5037 
   5038 /* Turn a single GOT that is too big for 16-bit addressing into
   5039    a sequence of GOTs, each one 16-bit addressable.  */
   5040 
   5041 static bool
   5042 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
   5043 		    asection *got, bfd_size_type pages)
   5044 {
   5045   struct mips_elf_link_hash_table *htab;
   5046   struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
   5047   struct mips_elf_traverse_got_arg tga;
   5048   struct mips_got_info *g, *gg;
   5049   unsigned int assign, needed_relocs;
   5050   bfd *dynobj, *ibfd;
   5051 
   5052   dynobj = elf_hash_table (info)->dynobj;
   5053   htab = mips_elf_hash_table (info);
   5054   BFD_ASSERT (htab != NULL);
   5055 
   5056   g = htab->got_info;
   5057 
   5058   got_per_bfd_arg.obfd = abfd;
   5059   got_per_bfd_arg.info = info;
   5060   got_per_bfd_arg.current = NULL;
   5061   got_per_bfd_arg.primary = NULL;
   5062   got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
   5063 				/ MIPS_ELF_GOT_SIZE (abfd))
   5064 			       - htab->reserved_gotno);
   5065   got_per_bfd_arg.max_pages = pages;
   5066   /* The number of globals that will be included in the primary GOT.
   5067      See the calls to mips_elf_set_global_got_area below for more
   5068      information.  */
   5069   got_per_bfd_arg.global_count = g->global_gotno;
   5070 
   5071   /* Try to merge the GOTs of input bfds together, as long as they
   5072      don't seem to exceed the maximum GOT size, choosing one of them
   5073      to be the primary GOT.  */
   5074   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   5075     {
   5076       gg = mips_elf_bfd_got (ibfd, false);
   5077       if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
   5078 	return false;
   5079     }
   5080 
   5081   /* If we do not find any suitable primary GOT, create an empty one.  */
   5082   if (got_per_bfd_arg.primary == NULL)
   5083     g->next = mips_elf_create_got_info (abfd);
   5084   else
   5085     g->next = got_per_bfd_arg.primary;
   5086   g->next->next = got_per_bfd_arg.current;
   5087 
   5088   /* GG is now the master GOT, and G is the primary GOT.  */
   5089   gg = g;
   5090   g = g->next;
   5091 
   5092   /* Map the output bfd to the primary got.  That's what we're going
   5093      to use for bfds that use GOT16 or GOT_PAGE relocations that we
   5094      didn't mark in check_relocs, and we want a quick way to find it.
   5095      We can't just use gg->next because we're going to reverse the
   5096      list.  */
   5097   mips_elf_replace_bfd_got (abfd, g);
   5098 
   5099   /* Every symbol that is referenced in a dynamic relocation must be
   5100      present in the primary GOT, so arrange for them to appear after
   5101      those that are actually referenced.  */
   5102   gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
   5103   g->global_gotno = gg->global_gotno;
   5104 
   5105   tga.info = info;
   5106   tga.value = GGA_RELOC_ONLY;
   5107   htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
   5108   tga.value = GGA_NORMAL;
   5109   htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
   5110 
   5111   /* Now go through the GOTs assigning them offset ranges.
   5112      [assigned_low_gotno, local_gotno[ will be set to the range of local
   5113      entries in each GOT.  We can then compute the end of a GOT by
   5114      adding local_gotno to global_gotno.  We reverse the list and make
   5115      it circular since then we'll be able to quickly compute the
   5116      beginning of a GOT, by computing the end of its predecessor.  To
   5117      avoid special cases for the primary GOT, while still preserving
   5118      assertions that are valid for both single- and multi-got links,
   5119      we arrange for the main got struct to have the right number of
   5120      global entries, but set its local_gotno such that the initial
   5121      offset of the primary GOT is zero.  Remember that the primary GOT
   5122      will become the last item in the circular linked list, so it
   5123      points back to the master GOT.  */
   5124   gg->local_gotno = -g->global_gotno;
   5125   gg->global_gotno = g->global_gotno;
   5126   gg->tls_gotno = 0;
   5127   assign = 0;
   5128   gg->next = gg;
   5129 
   5130   do
   5131     {
   5132       struct mips_got_info *gn;
   5133 
   5134       assign += htab->reserved_gotno;
   5135       g->assigned_low_gotno = assign;
   5136       g->local_gotno += assign;
   5137       g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
   5138       g->assigned_high_gotno = g->local_gotno - 1;
   5139       assign = g->local_gotno + g->global_gotno + g->tls_gotno;
   5140 
   5141       /* Take g out of the direct list, and push it onto the reversed
   5142 	 list that gg points to.  g->next is guaranteed to be nonnull after
   5143 	 this operation, as required by mips_elf_initialize_tls_index. */
   5144       gn = g->next;
   5145       g->next = gg->next;
   5146       gg->next = g;
   5147 
   5148       /* Set up any TLS entries.  We always place the TLS entries after
   5149 	 all non-TLS entries.  */
   5150       g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
   5151       tga.g = g;
   5152       tga.value = MIPS_ELF_GOT_SIZE (abfd);
   5153       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
   5154       if (!tga.g)
   5155 	return false;
   5156       BFD_ASSERT (g->tls_assigned_gotno == assign);
   5157 
   5158       /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
   5159       g = gn;
   5160 
   5161       /* Forbid global symbols in every non-primary GOT from having
   5162 	 lazy-binding stubs.  */
   5163       if (g)
   5164 	htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
   5165     }
   5166   while (g);
   5167 
   5168   got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
   5169 
   5170   needed_relocs = 0;
   5171   for (g = gg->next; g && g->next != gg; g = g->next)
   5172     {
   5173       unsigned int save_assign;
   5174 
   5175       /* Assign offsets to global GOT entries and count how many
   5176 	 relocations they need.  */
   5177       save_assign = g->assigned_low_gotno;
   5178       g->assigned_low_gotno = g->local_gotno;
   5179       tga.info = info;
   5180       tga.value = MIPS_ELF_GOT_SIZE (abfd);
   5181       tga.g = g;
   5182       htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
   5183       if (!tga.g)
   5184 	return false;
   5185       BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
   5186       g->assigned_low_gotno = save_assign;
   5187 
   5188       if (bfd_link_pic (info))
   5189 	{
   5190 	  g->relocs += g->local_gotno - g->assigned_low_gotno;
   5191 	  BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
   5192 		      + g->next->global_gotno
   5193 		      + g->next->tls_gotno
   5194 		      + htab->reserved_gotno);
   5195 	}
   5196       needed_relocs += g->relocs;
   5197     }
   5198   needed_relocs += g->relocs;
   5199 
   5200   if (needed_relocs)
   5201     mips_elf_allocate_dynamic_relocations (dynobj, info,
   5202 					   needed_relocs);
   5203 
   5204   return true;
   5205 }
   5206 
   5207 
   5208 /* Returns the first relocation of type r_type found, beginning with
   5210    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
   5211 
   5212 static const Elf_Internal_Rela *
   5213 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
   5214 			  const Elf_Internal_Rela *relocation,
   5215 			  const Elf_Internal_Rela *relend)
   5216 {
   5217   unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
   5218 
   5219   while (relocation < relend)
   5220     {
   5221       if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
   5222 	  && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
   5223 	return relocation;
   5224 
   5225       ++relocation;
   5226     }
   5227 
   5228   /* We didn't find it.  */
   5229   return NULL;
   5230 }
   5231 
   5232 /* Return whether an input relocation is against a local symbol.  */
   5233 
   5234 static bool
   5235 mips_elf_local_relocation_p (bfd *input_bfd,
   5236 			     const Elf_Internal_Rela *relocation,
   5237 			     asection **local_sections)
   5238 {
   5239   unsigned long r_symndx;
   5240   Elf_Internal_Shdr *symtab_hdr;
   5241   size_t extsymoff;
   5242 
   5243   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
   5244   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   5245   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
   5246 
   5247   if (r_symndx < extsymoff)
   5248     return true;
   5249   if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
   5250     return true;
   5251 
   5252   return false;
   5253 }
   5254 
   5255 /* Sign-extend VALUE, which has the indicated number of BITS.  */
   5257 
   5258 bfd_vma
   5259 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
   5260 {
   5261   if (value & ((bfd_vma) 1 << (bits - 1)))
   5262     /* VALUE is negative.  */
   5263     value |= ((bfd_vma) - 1) << bits;
   5264 
   5265   return value;
   5266 }
   5267 
   5268 /* Return non-zero if the indicated VALUE has overflowed the maximum
   5269    range expressible by a signed number with the indicated number of
   5270    BITS.  */
   5271 
   5272 static bool
   5273 mips_elf_overflow_p (bfd_vma value, int bits)
   5274 {
   5275   bfd_signed_vma svalue = (bfd_signed_vma) value;
   5276 
   5277   if (svalue > (1 << (bits - 1)) - 1)
   5278     /* The value is too big.  */
   5279     return true;
   5280   else if (svalue < -(1 << (bits - 1)))
   5281     /* The value is too small.  */
   5282     return true;
   5283 
   5284   /* All is well.  */
   5285   return false;
   5286 }
   5287 
   5288 /* Calculate the %high function.  */
   5289 
   5290 static bfd_vma
   5291 mips_elf_high (bfd_vma value)
   5292 {
   5293   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
   5294 }
   5295 
   5296 /* Calculate the %higher function.  */
   5297 
   5298 static bfd_vma
   5299 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
   5300 {
   5301 #ifdef BFD64
   5302   return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
   5303 #else
   5304   abort ();
   5305   return MINUS_ONE;
   5306 #endif
   5307 }
   5308 
   5309 /* Calculate the %highest function.  */
   5310 
   5311 static bfd_vma
   5312 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
   5313 {
   5314 #ifdef BFD64
   5315   return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
   5316 #else
   5317   abort ();
   5318   return MINUS_ONE;
   5319 #endif
   5320 }
   5321 
   5322 /* Create the .compact_rel section.  */
   5324 
   5325 static bool
   5326 mips_elf_create_compact_rel_section
   5327   (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
   5328 {
   5329   flagword flags;
   5330   register asection *s;
   5331 
   5332   if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
   5333     {
   5334       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
   5335 	       | SEC_READONLY);
   5336 
   5337       s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
   5338       if (s == NULL
   5339 	  || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   5340 	return false;
   5341 
   5342       s->size = sizeof (Elf32_External_compact_rel);
   5343     }
   5344 
   5345   return true;
   5346 }
   5347 
   5348 /* Create the .got section to hold the global offset table.  */
   5349 
   5350 static bool
   5351 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
   5352 {
   5353   flagword flags;
   5354   register asection *s;
   5355   struct elf_link_hash_entry *h;
   5356   struct bfd_link_hash_entry *bh;
   5357   struct mips_elf_link_hash_table *htab;
   5358 
   5359   htab = mips_elf_hash_table (info);
   5360   BFD_ASSERT (htab != NULL);
   5361 
   5362   /* This function may be called more than once.  */
   5363   if (htab->root.sgot)
   5364     return true;
   5365 
   5366   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   5367 	   | SEC_LINKER_CREATED);
   5368 
   5369   /* We have to use an alignment of 2**4 here because this is hardcoded
   5370      in the function stub generation and in the linker script.  */
   5371   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   5372   if (s == NULL
   5373       || !bfd_set_section_alignment (s, 4))
   5374     return false;
   5375   htab->root.sgot = s;
   5376 
   5377   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
   5378      linker script because we don't want to define the symbol if we
   5379      are not creating a global offset table.  */
   5380   bh = NULL;
   5381   if (! (_bfd_generic_link_add_one_symbol
   5382 	 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
   5383 	  0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
   5384     return false;
   5385 
   5386   h = (struct elf_link_hash_entry *) bh;
   5387   h->non_elf = 0;
   5388   h->def_regular = 1;
   5389   h->type = STT_OBJECT;
   5390   h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
   5391   elf_hash_table (info)->hgot = h;
   5392 
   5393   if (bfd_link_pic (info)
   5394       && ! bfd_elf_link_record_dynamic_symbol (info, h))
   5395     return false;
   5396 
   5397   htab->got_info = mips_elf_create_got_info (abfd);
   5398   mips_elf_section_data (s)->elf.this_hdr.sh_flags
   5399     |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
   5400 
   5401   /* We also need a .got.plt section when generating PLTs.  */
   5402   s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
   5403 					  SEC_ALLOC | SEC_LOAD
   5404 					  | SEC_HAS_CONTENTS
   5405 					  | SEC_IN_MEMORY
   5406 					  | SEC_LINKER_CREATED);
   5407   if (s == NULL)
   5408     return false;
   5409   htab->root.sgotplt = s;
   5410 
   5411   return true;
   5412 }
   5413 
   5414 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
   5416    __GOTT_INDEX__ symbols.  These symbols are only special for
   5417    shared objects; they are not used in executables.  */
   5418 
   5419 static bool
   5420 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
   5421 {
   5422   return (mips_elf_hash_table (info)->root.target_os == is_vxworks
   5423 	  && bfd_link_pic (info)
   5424 	  && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
   5425 	      || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
   5426 }
   5427 
   5428 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
   5429    require an la25 stub.  See also mips_elf_local_pic_function_p,
   5430    which determines whether the destination function ever requires a
   5431    stub.  */
   5432 
   5433 static bool
   5434 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
   5435 				     bool target_is_16_bit_code_p)
   5436 {
   5437   /* We specifically ignore branches and jumps from EF_PIC objects,
   5438      where the onus is on the compiler or programmer to perform any
   5439      necessary initialization of $25.  Sometimes such initialization
   5440      is unnecessary; for example, -mno-shared functions do not use
   5441      the incoming value of $25, and may therefore be called directly.  */
   5442   if (PIC_OBJECT_P (input_bfd))
   5443     return false;
   5444 
   5445   switch (r_type)
   5446     {
   5447     case R_MIPS_26:
   5448     case R_MIPS_PC16:
   5449     case R_MIPS_PC21_S2:
   5450     case R_MIPS_PC26_S2:
   5451     case R_MICROMIPS_26_S1:
   5452     case R_MICROMIPS_PC7_S1:
   5453     case R_MICROMIPS_PC10_S1:
   5454     case R_MICROMIPS_PC16_S1:
   5455     case R_MICROMIPS_PC23_S2:
   5456       return true;
   5457 
   5458     case R_MIPS16_26:
   5459       return !target_is_16_bit_code_p;
   5460 
   5461     default:
   5462       return false;
   5463     }
   5464 }
   5465 
   5466 /* Obtain the field relocated by RELOCATION.  */
   5468 
   5469 static bfd_vma
   5470 mips_elf_obtain_contents (reloc_howto_type *howto,
   5471 			  const Elf_Internal_Rela *relocation,
   5472 			  bfd *input_bfd, bfd_byte *contents)
   5473 {
   5474   bfd_vma x = 0;
   5475   bfd_byte *location = contents + relocation->r_offset;
   5476   unsigned int size = bfd_get_reloc_size (howto);
   5477 
   5478   /* Obtain the bytes.  */
   5479   if (size != 0)
   5480     x = bfd_get (8 * size, input_bfd, location);
   5481 
   5482   return x;
   5483 }
   5484 
   5485 /* Store the field relocated by RELOCATION.  */
   5486 
   5487 static void
   5488 mips_elf_store_contents (reloc_howto_type *howto,
   5489 			 const Elf_Internal_Rela *relocation,
   5490 			 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
   5491 {
   5492   bfd_byte *location = contents + relocation->r_offset;
   5493   unsigned int size = bfd_get_reloc_size (howto);
   5494 
   5495   /* Put the value into the output.  */
   5496   if (size != 0)
   5497     bfd_put (8 * size, input_bfd, x, location);
   5498 }
   5499 
   5500 /* Try to patch a load from GOT instruction in CONTENTS pointed to by
   5501    RELOCATION described by HOWTO, with a move of 0 to the load target
   5502    register, returning TRUE if that is successful and FALSE otherwise.
   5503    If DOIT is FALSE, then only determine it patching is possible and
   5504    return status without actually changing CONTENTS.
   5505 */
   5506 
   5507 static bool
   5508 mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
   5509 			   const Elf_Internal_Rela *relocation,
   5510 			   reloc_howto_type *howto, bool doit)
   5511 {
   5512   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   5513   bfd_byte *location = contents + relocation->r_offset;
   5514   bool nullified = true;
   5515   bfd_vma x;
   5516 
   5517   _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
   5518 
   5519   /* Obtain the current value.  */
   5520   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
   5521 
   5522   /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
   5523      while RY is at bits [18:16] of the combined 32-bit instruction word.  */
   5524   if (mips16_reloc_p (r_type)
   5525       && (((x >> 22) & 0x3ff) == 0x3d3				/* LW */
   5526 	  || ((x >> 22) & 0x3ff) == 0x3c7))			/* LD */
   5527     x = (0x3cdU << 22) | (x & (7 << 16)) << 3;			/* LI */
   5528   else if (micromips_reloc_p (r_type)
   5529 	   && ((x >> 26) & 0x37) == 0x37)			/* LW/LD */
   5530     x = (0xc << 26) | (x & (0x1f << 21));			/* ADDIU */
   5531   else if (((x >> 26) & 0x3f) == 0x23				/* LW */
   5532 	   || ((x >> 26) & 0x3f) == 0x37)			/* LD */
   5533     x = (0x9 << 26) | (x & (0x1f << 16));			/* ADDIU */
   5534   else
   5535     nullified = false;
   5536 
   5537   /* Put the value into the output.  */
   5538   if (doit && nullified)
   5539     mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
   5540 
   5541   _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
   5542 
   5543   return nullified;
   5544 }
   5545 
   5546 /* Calculate the value produced by the RELOCATION (which comes from
   5547    the INPUT_BFD).  The ADDEND is the addend to use for this
   5548    RELOCATION; RELOCATION->R_ADDEND is ignored.
   5549 
   5550    The result of the relocation calculation is stored in VALUEP.
   5551    On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
   5552    is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
   5553 
   5554    This function returns bfd_reloc_continue if the caller need take no
   5555    further action regarding this relocation, bfd_reloc_notsupported if
   5556    something goes dramatically wrong, bfd_reloc_overflow if an
   5557    overflow occurs, and bfd_reloc_ok to indicate success.  */
   5558 
   5559 static bfd_reloc_status_type
   5560 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
   5561 			       asection *input_section, bfd_byte *contents,
   5562 			       struct bfd_link_info *info,
   5563 			       const Elf_Internal_Rela *relocation,
   5564 			       bfd_vma addend, reloc_howto_type *howto,
   5565 			       Elf_Internal_Sym *local_syms,
   5566 			       asection **local_sections, bfd_vma *valuep,
   5567 			       const char **namep,
   5568 			       bool *cross_mode_jump_p,
   5569 			       bool save_addend)
   5570 {
   5571   /* The eventual value we will return.  */
   5572   bfd_vma value;
   5573   /* The address of the symbol against which the relocation is
   5574      occurring.  */
   5575   bfd_vma symbol = 0;
   5576   /* The final GP value to be used for the relocatable, executable, or
   5577      shared object file being produced.  */
   5578   bfd_vma gp;
   5579   /* The place (section offset or address) of the storage unit being
   5580      relocated.  */
   5581   bfd_vma p;
   5582   /* The value of GP used to create the relocatable object.  */
   5583   bfd_vma gp0;
   5584   /* The offset into the global offset table at which the address of
   5585      the relocation entry symbol, adjusted by the addend, resides
   5586      during execution.  */
   5587   bfd_vma g = MINUS_ONE;
   5588   /* The section in which the symbol referenced by the relocation is
   5589      located.  */
   5590   asection *sec = NULL;
   5591   struct mips_elf_link_hash_entry *h = NULL;
   5592   /* TRUE if the symbol referred to by this relocation is a local
   5593      symbol.  */
   5594   bool local_p, was_local_p;
   5595   /* TRUE if the symbol referred to by this relocation is a section
   5596      symbol.  */
   5597   bool section_p = false;
   5598   /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
   5599   bool gp_disp_p = false;
   5600   /* TRUE if the symbol referred to by this relocation is
   5601      "__gnu_local_gp".  */
   5602   bool gnu_local_gp_p = false;
   5603   Elf_Internal_Shdr *symtab_hdr;
   5604   size_t extsymoff;
   5605   unsigned long r_symndx;
   5606   int r_type;
   5607   /* TRUE if overflow occurred during the calculation of the
   5608      relocation value.  */
   5609   bool overflowed_p;
   5610   /* TRUE if this relocation refers to a MIPS16 function.  */
   5611   bool target_is_16_bit_code_p = false;
   5612   bool target_is_micromips_code_p = false;
   5613   struct mips_elf_link_hash_table *htab;
   5614   bfd *dynobj;
   5615   bool resolved_to_zero;
   5616 
   5617   dynobj = elf_hash_table (info)->dynobj;
   5618   htab = mips_elf_hash_table (info);
   5619   BFD_ASSERT (htab != NULL);
   5620 
   5621   /* Parse the relocation.  */
   5622   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
   5623   r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   5624   p = (input_section->output_section->vma
   5625        + input_section->output_offset
   5626        + relocation->r_offset);
   5627 
   5628   /* Assume that there will be no overflow.  */
   5629   overflowed_p = false;
   5630 
   5631   /* Figure out whether or not the symbol is local, and get the offset
   5632      used in the array of hash table entries.  */
   5633   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   5634   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
   5635 					 local_sections);
   5636   was_local_p = local_p;
   5637   if (! elf_bad_symtab (input_bfd))
   5638     extsymoff = symtab_hdr->sh_info;
   5639   else
   5640     {
   5641       /* The symbol table does not follow the rule that local symbols
   5642 	 must come before globals.  */
   5643       extsymoff = 0;
   5644     }
   5645 
   5646   /* Figure out the value of the symbol.  */
   5647   if (local_p)
   5648     {
   5649       bool micromips_p = MICROMIPS_P (abfd);
   5650       Elf_Internal_Sym *sym;
   5651 
   5652       sym = local_syms + r_symndx;
   5653       sec = local_sections[r_symndx];
   5654 
   5655       section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
   5656 
   5657       symbol = sec->output_section->vma + sec->output_offset;
   5658       if (!section_p || (sec->flags & SEC_MERGE))
   5659 	symbol += sym->st_value;
   5660       if ((sec->flags & SEC_MERGE) && section_p)
   5661 	{
   5662 	  addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
   5663 	  addend -= symbol;
   5664 	  addend += sec->output_section->vma + sec->output_offset;
   5665 	}
   5666 
   5667       /* MIPS16/microMIPS text labels should be treated as odd.  */
   5668       if (ELF_ST_IS_COMPRESSED (sym->st_other))
   5669 	++symbol;
   5670 
   5671       /* Record the name of this symbol, for our caller.  */
   5672       *namep = bfd_elf_string_from_elf_section (input_bfd,
   5673 						symtab_hdr->sh_link,
   5674 						sym->st_name);
   5675       if (*namep == NULL || **namep == '\0')
   5676 	*namep = bfd_section_name (sec);
   5677 
   5678       /* For relocations against a section symbol and ones against no
   5679 	 symbol (absolute relocations) infer the ISA mode from the addend.  */
   5680       if (section_p || r_symndx == STN_UNDEF)
   5681 	{
   5682 	  target_is_16_bit_code_p = (addend & 1) && !micromips_p;
   5683 	  target_is_micromips_code_p = (addend & 1) && micromips_p;
   5684 	}
   5685       /* For relocations against an absolute symbol infer the ISA mode
   5686 	 from the value of the symbol plus addend.  */
   5687       else if (bfd_is_abs_section (sec))
   5688 	{
   5689 	  target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
   5690 	  target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
   5691 	}
   5692       /* Otherwise just use the regular symbol annotation available.  */
   5693       else
   5694 	{
   5695 	  target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
   5696 	  target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
   5697 	}
   5698     }
   5699   else
   5700     {
   5701       /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
   5702 
   5703       /* For global symbols we look up the symbol in the hash-table.  */
   5704       h = ((struct mips_elf_link_hash_entry *)
   5705 	   elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
   5706       /* Find the real hash-table entry for this symbol.  */
   5707       while (h->root.root.type == bfd_link_hash_indirect
   5708 	     || h->root.root.type == bfd_link_hash_warning)
   5709 	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   5710 
   5711       /* Record the name of this symbol, for our caller.  */
   5712       *namep = h->root.root.root.string;
   5713 
   5714       /* See if this is the special _gp_disp symbol.  Note that such a
   5715 	 symbol must always be a global symbol.  */
   5716       if (strcmp (*namep, "_gp_disp") == 0
   5717 	  && ! NEWABI_P (input_bfd))
   5718 	{
   5719 	  /* Relocations against _gp_disp are permitted only with
   5720 	     R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
   5721 	  if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
   5722 	    return bfd_reloc_notsupported;
   5723 
   5724 	  gp_disp_p = true;
   5725 	}
   5726       /* See if this is the special _gp symbol.  Note that such a
   5727 	 symbol must always be a global symbol.  */
   5728       else if (strcmp (*namep, "__gnu_local_gp") == 0)
   5729 	gnu_local_gp_p = true;
   5730 
   5731 
   5732       /* If this symbol is defined, calculate its address.  Note that
   5733 	 _gp_disp is a magic symbol, always implicitly defined by the
   5734 	 linker, so it's inappropriate to check to see whether or not
   5735 	 its defined.  */
   5736       else if ((h->root.root.type == bfd_link_hash_defined
   5737 		|| h->root.root.type == bfd_link_hash_defweak)
   5738 	       && h->root.root.u.def.section)
   5739 	{
   5740 	  sec = h->root.root.u.def.section;
   5741 	  if (sec->output_section)
   5742 	    symbol = (h->root.root.u.def.value
   5743 		      + sec->output_section->vma
   5744 		      + sec->output_offset);
   5745 	  else
   5746 	    symbol = h->root.root.u.def.value;
   5747 	}
   5748       else if (h->root.root.type == bfd_link_hash_undefweak)
   5749 	/* We allow relocations against undefined weak symbols, giving
   5750 	   it the value zero, so that you can undefined weak functions
   5751 	   and check to see if they exist by looking at their
   5752 	   addresses.  */
   5753 	symbol = 0;
   5754       else if (info->unresolved_syms_in_objects == RM_IGNORE
   5755 	       && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
   5756 	symbol = 0;
   5757       else if (strcmp (*namep, SGI_COMPAT (input_bfd)
   5758 		       ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
   5759 	{
   5760 	  /* If this is a dynamic link, we should have created a
   5761 	     _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
   5762 	     in _bfd_mips_elf_create_dynamic_sections.
   5763 	     Otherwise, we should define the symbol with a value of 0.
   5764 	     FIXME: It should probably get into the symbol table
   5765 	     somehow as well.  */
   5766 	  BFD_ASSERT (! bfd_link_pic (info));
   5767 	  BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
   5768 	  symbol = 0;
   5769 	}
   5770       else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
   5771 	{
   5772 	  /* This is an optional symbol - an Irix specific extension to the
   5773 	     ELF spec.  Ignore it for now.
   5774 	     XXX - FIXME - there is more to the spec for OPTIONAL symbols
   5775 	     than simply ignoring them, but we do not handle this for now.
   5776 	     For information see the "64-bit ELF Object File Specification"
   5777 	     which is available from here:
   5778 	     http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
   5779 	  symbol = 0;
   5780 	}
   5781       else
   5782 	{
   5783           bool reject_undefined
   5784 	    = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
   5785 		&& !info->warn_unresolved_syms)
   5786 	       || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
   5787 
   5788 	  info->callbacks->undefined_symbol
   5789 	    (info, h->root.root.root.string, input_bfd,
   5790 	     input_section, relocation->r_offset, reject_undefined);
   5791 
   5792 	  if (reject_undefined)
   5793 	    return bfd_reloc_undefined;
   5794 
   5795 	  symbol = 0;
   5796 	}
   5797 
   5798       target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
   5799       target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
   5800     }
   5801 
   5802   /* If this is a reference to a 16-bit function with a stub, we need
   5803      to redirect the relocation to the stub unless:
   5804 
   5805      (a) the relocation is for a MIPS16 JAL;
   5806 
   5807      (b) the relocation is for a MIPS16 PIC call, and there are no
   5808 	 non-MIPS16 uses of the GOT slot; or
   5809 
   5810      (c) the section allows direct references to MIPS16 functions.  */
   5811   if (r_type != R_MIPS16_26
   5812       && !bfd_link_relocatable (info)
   5813       && ((h != NULL
   5814 	   && h->fn_stub != NULL
   5815 	   && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
   5816 	  || (local_p
   5817 	      && mips_elf_tdata (input_bfd)->local_stubs != NULL
   5818 	      && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
   5819       && !section_allows_mips16_refs_p (input_section))
   5820     {
   5821       /* This is a 32- or 64-bit call to a 16-bit function.  We should
   5822 	 have already noticed that we were going to need the
   5823 	 stub.  */
   5824       if (local_p)
   5825 	{
   5826 	  sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
   5827 	  value = 0;
   5828 	}
   5829       else
   5830 	{
   5831 	  BFD_ASSERT (h->need_fn_stub);
   5832 	  if (h->la25_stub)
   5833 	    {
   5834 	      /* If a LA25 header for the stub itself exists, point to the
   5835 		 prepended LUI/ADDIU sequence.  */
   5836 	      sec = h->la25_stub->stub_section;
   5837 	      value = h->la25_stub->offset;
   5838 	    }
   5839 	  else
   5840 	    {
   5841 	      sec = h->fn_stub;
   5842 	      value = 0;
   5843 	    }
   5844 	}
   5845 
   5846       symbol = sec->output_section->vma + sec->output_offset + value;
   5847       /* The target is 16-bit, but the stub isn't.  */
   5848       target_is_16_bit_code_p = false;
   5849     }
   5850   /* If this is a MIPS16 call with a stub, that is made through the PLT or
   5851      to a standard MIPS function, we need to redirect the call to the stub.
   5852      Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
   5853      indirect calls should use an indirect stub instead.  */
   5854   else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
   5855 	   && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
   5856 	       || (local_p
   5857 		   && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
   5858 		   && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
   5859 	   && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
   5860     {
   5861       if (local_p)
   5862 	sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
   5863       else
   5864 	{
   5865 	  /* If both call_stub and call_fp_stub are defined, we can figure
   5866 	     out which one to use by checking which one appears in the input
   5867 	     file.  */
   5868 	  if (h->call_stub != NULL && h->call_fp_stub != NULL)
   5869 	    {
   5870 	      asection *o;
   5871 
   5872 	      sec = NULL;
   5873 	      for (o = input_bfd->sections; o != NULL; o = o->next)
   5874 		{
   5875 		  if (CALL_FP_STUB_P (bfd_section_name (o)))
   5876 		    {
   5877 		      sec = h->call_fp_stub;
   5878 		      break;
   5879 		    }
   5880 		}
   5881 	      if (sec == NULL)
   5882 		sec = h->call_stub;
   5883 	    }
   5884 	  else if (h->call_stub != NULL)
   5885 	    sec = h->call_stub;
   5886 	  else
   5887 	    sec = h->call_fp_stub;
   5888 	}
   5889 
   5890       BFD_ASSERT (sec->size > 0);
   5891       symbol = sec->output_section->vma + sec->output_offset;
   5892     }
   5893   /* If this is a direct call to a PIC function, redirect to the
   5894      non-PIC stub.  */
   5895   else if (h != NULL && h->la25_stub
   5896 	   && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
   5897 						   target_is_16_bit_code_p))
   5898     {
   5899 	symbol = (h->la25_stub->stub_section->output_section->vma
   5900 		  + h->la25_stub->stub_section->output_offset
   5901 		  + h->la25_stub->offset);
   5902 	if (ELF_ST_IS_MICROMIPS (h->root.other))
   5903 	  symbol |= 1;
   5904     }
   5905   /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
   5906      entry is used if a standard PLT entry has also been made.  In this
   5907      case the symbol will have been set by mips_elf_set_plt_sym_value
   5908      to point to the standard PLT entry, so redirect to the compressed
   5909      one.  */
   5910   else if ((mips16_branch_reloc_p (r_type)
   5911 	    || micromips_branch_reloc_p (r_type))
   5912 	   && !bfd_link_relocatable (info)
   5913 	   && h != NULL
   5914 	   && h->use_plt_entry
   5915 	   && h->root.plt.plist->comp_offset != MINUS_ONE
   5916 	   && h->root.plt.plist->mips_offset != MINUS_ONE)
   5917     {
   5918       bool micromips_p = MICROMIPS_P (abfd);
   5919 
   5920       sec = htab->root.splt;
   5921       symbol = (sec->output_section->vma
   5922 		+ sec->output_offset
   5923 		+ htab->plt_header_size
   5924 		+ htab->plt_mips_offset
   5925 		+ h->root.plt.plist->comp_offset
   5926 		+ 1);
   5927 
   5928       target_is_16_bit_code_p = !micromips_p;
   5929       target_is_micromips_code_p = micromips_p;
   5930     }
   5931 
   5932   /* Make sure MIPS16 and microMIPS are not used together.  */
   5933   if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
   5934       || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
   5935    {
   5936       _bfd_error_handler
   5937 	(_("MIPS16 and microMIPS functions cannot call each other"));
   5938       return bfd_reloc_notsupported;
   5939    }
   5940 
   5941   /* Calls from 16-bit code to 32-bit code and vice versa require the
   5942      mode change.  However, we can ignore calls to undefined weak symbols,
   5943      which should never be executed at runtime.  This exception is important
   5944      because the assembly writer may have "known" that any definition of the
   5945      symbol would be 16-bit code, and that direct jumps were therefore
   5946      acceptable.  */
   5947   *cross_mode_jump_p = (!bfd_link_relocatable (info)
   5948 			&& !(h && h->root.root.type == bfd_link_hash_undefweak)
   5949 			&& ((mips16_branch_reloc_p (r_type)
   5950 			     && !target_is_16_bit_code_p)
   5951 			    || (micromips_branch_reloc_p (r_type)
   5952 				&& !target_is_micromips_code_p)
   5953 			    || ((branch_reloc_p (r_type)
   5954 				 || r_type == R_MIPS_JALR)
   5955 				&& (target_is_16_bit_code_p
   5956 				    || target_is_micromips_code_p))));
   5957 
   5958   resolved_to_zero = (h != NULL
   5959 		      && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
   5960 
   5961   switch (r_type)
   5962     {
   5963     case R_MIPS16_CALL16:
   5964     case R_MIPS16_GOT16:
   5965     case R_MIPS_CALL16:
   5966     case R_MIPS_GOT16:
   5967     case R_MIPS_GOT_PAGE:
   5968     case R_MIPS_GOT_DISP:
   5969     case R_MIPS_GOT_LO16:
   5970     case R_MIPS_CALL_LO16:
   5971     case R_MICROMIPS_CALL16:
   5972     case R_MICROMIPS_GOT16:
   5973     case R_MICROMIPS_GOT_PAGE:
   5974     case R_MICROMIPS_GOT_DISP:
   5975     case R_MICROMIPS_GOT_LO16:
   5976     case R_MICROMIPS_CALL_LO16:
   5977       if (resolved_to_zero
   5978 	  && !bfd_link_relocatable (info)
   5979 	  && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
   5980 					relocation->r_offset)
   5981 	  && mips_elf_nullify_got_load (input_bfd, contents,
   5982 					relocation, howto, true))
   5983 	return bfd_reloc_continue;
   5984 
   5985       /* Fall through.  */
   5986     case R_MIPS_GOT_HI16:
   5987     case R_MIPS_CALL_HI16:
   5988     case R_MICROMIPS_GOT_HI16:
   5989     case R_MICROMIPS_CALL_HI16:
   5990       if (resolved_to_zero
   5991 	  && htab->use_absolute_zero
   5992 	  && bfd_link_pic (info))
   5993 	{
   5994 	  /* Redirect to the special `__gnu_absolute_zero' symbol.  */
   5995 	  h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
   5996 					 false, false, false);
   5997 	  BFD_ASSERT (h != NULL);
   5998 	}
   5999       break;
   6000     }
   6001 
   6002   local_p = (h == NULL || mips_use_local_got_p (info, h));
   6003 
   6004   gp0 = _bfd_get_gp_value (input_bfd);
   6005   gp = _bfd_get_gp_value (abfd);
   6006   if (htab->got_info)
   6007     gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
   6008 
   6009   if (gnu_local_gp_p)
   6010     symbol = gp;
   6011 
   6012   /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
   6013      to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
   6014      corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.  */
   6015   if (got_page_reloc_p (r_type) && !local_p)
   6016     {
   6017       r_type = (micromips_reloc_p (r_type)
   6018 		? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
   6019       addend = 0;
   6020     }
   6021 
   6022   /* If we haven't already determined the GOT offset, and we're going
   6023      to need it, get it now.  */
   6024   switch (r_type)
   6025     {
   6026     case R_MIPS16_CALL16:
   6027     case R_MIPS16_GOT16:
   6028     case R_MIPS_CALL16:
   6029     case R_MIPS_GOT16:
   6030     case R_MIPS_GOT_DISP:
   6031     case R_MIPS_GOT_HI16:
   6032     case R_MIPS_CALL_HI16:
   6033     case R_MIPS_GOT_LO16:
   6034     case R_MIPS_CALL_LO16:
   6035     case R_MICROMIPS_CALL16:
   6036     case R_MICROMIPS_GOT16:
   6037     case R_MICROMIPS_GOT_DISP:
   6038     case R_MICROMIPS_GOT_HI16:
   6039     case R_MICROMIPS_CALL_HI16:
   6040     case R_MICROMIPS_GOT_LO16:
   6041     case R_MICROMIPS_CALL_LO16:
   6042     case R_MIPS_TLS_GD:
   6043     case R_MIPS_TLS_GOTTPREL:
   6044     case R_MIPS_TLS_LDM:
   6045     case R_MIPS16_TLS_GD:
   6046     case R_MIPS16_TLS_GOTTPREL:
   6047     case R_MIPS16_TLS_LDM:
   6048     case R_MICROMIPS_TLS_GD:
   6049     case R_MICROMIPS_TLS_GOTTPREL:
   6050     case R_MICROMIPS_TLS_LDM:
   6051       /* Find the index into the GOT where this value is located.  */
   6052       if (tls_ldm_reloc_p (r_type))
   6053 	{
   6054 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
   6055 					0, 0, NULL, r_type);
   6056 	  if (g == MINUS_ONE)
   6057 	    return bfd_reloc_outofrange;
   6058 	}
   6059       else if (!local_p)
   6060 	{
   6061 	  /* On VxWorks, CALL relocations should refer to the .got.plt
   6062 	     entry, which is initialized to point at the PLT stub.  */
   6063 	  if (htab->root.target_os == is_vxworks
   6064 	      && (call_hi16_reloc_p (r_type)
   6065 		  || call_lo16_reloc_p (r_type)
   6066 		  || call16_reloc_p (r_type)))
   6067 	    {
   6068 	      BFD_ASSERT (addend == 0);
   6069 	      BFD_ASSERT (h->root.needs_plt);
   6070 	      g = mips_elf_gotplt_index (info, &h->root);
   6071 	    }
   6072 	  else
   6073 	    {
   6074 	      BFD_ASSERT (addend == 0);
   6075 	      g = mips_elf_global_got_index (abfd, info, input_bfd,
   6076 					     &h->root, r_type);
   6077 	      if (!TLS_RELOC_P (r_type)
   6078 		  && !elf_hash_table (info)->dynamic_sections_created)
   6079 		/* This is a static link.  We must initialize the GOT entry.  */
   6080 		MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
   6081 	    }
   6082 	}
   6083       else if (htab->root.target_os != is_vxworks
   6084 	       && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
   6085 	/* The calculation below does not involve "g".  */
   6086 	break;
   6087       else
   6088 	{
   6089 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
   6090 					symbol + addend, r_symndx, h, r_type);
   6091 	  if (g == MINUS_ONE)
   6092 	    return bfd_reloc_outofrange;
   6093 	}
   6094 
   6095       /* Convert GOT indices to actual offsets.  */
   6096       g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
   6097       break;
   6098     }
   6099 
   6100   /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
   6101      symbols are resolved by the loader.  Add them to .rela.dyn.  */
   6102   if (h != NULL && is_gott_symbol (info, &h->root))
   6103     {
   6104       Elf_Internal_Rela outrel;
   6105       bfd_byte *loc;
   6106       asection *s;
   6107 
   6108       s = mips_elf_rel_dyn_section (info, false);
   6109       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
   6110 
   6111       outrel.r_offset = (input_section->output_section->vma
   6112 			 + input_section->output_offset
   6113 			 + relocation->r_offset);
   6114       outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
   6115       outrel.r_addend = addend;
   6116       bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
   6117 
   6118       /* If we've written this relocation for a readonly section,
   6119 	 we need to set DF_TEXTREL again, so that we do not delete the
   6120 	 DT_TEXTREL tag.  */
   6121       if (MIPS_ELF_READONLY_SECTION (input_section))
   6122 	info->flags |= DF_TEXTREL;
   6123 
   6124       *valuep = 0;
   6125       return bfd_reloc_ok;
   6126     }
   6127 
   6128   /* Figure out what kind of relocation is being performed.  */
   6129   switch (r_type)
   6130     {
   6131     case R_MIPS_NONE:
   6132       return bfd_reloc_continue;
   6133 
   6134     case R_MIPS_16:
   6135       if (howto->partial_inplace)
   6136 	addend = _bfd_mips_elf_sign_extend (addend, 16);
   6137       value = symbol + addend;
   6138       overflowed_p = mips_elf_overflow_p (value, 16);
   6139       break;
   6140 
   6141     case R_MIPS_32:
   6142     case R_MIPS_REL32:
   6143     case R_MIPS_64:
   6144       if ((bfd_link_pic (info)
   6145 	   || (htab->root.dynamic_sections_created
   6146 	       && h != NULL
   6147 	       && h->root.def_dynamic
   6148 	       && !h->root.def_regular
   6149 	       && !h->has_static_relocs))
   6150 	  && r_symndx != STN_UNDEF
   6151 	  && (h == NULL
   6152 	      || h->root.root.type != bfd_link_hash_undefweak
   6153 	      || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
   6154 		  && !resolved_to_zero))
   6155 	  && (input_section->flags & SEC_ALLOC) != 0)
   6156 	{
   6157 	  /* If we're creating a shared library, then we can't know
   6158 	     where the symbol will end up.  So, we create a relocation
   6159 	     record in the output, and leave the job up to the dynamic
   6160 	     linker.  We must do the same for executable references to
   6161 	     shared library symbols, unless we've decided to use copy
   6162 	     relocs or PLTs instead.  */
   6163 	  value = addend;
   6164 	  if (!mips_elf_create_dynamic_relocation (abfd,
   6165 						   info,
   6166 						   relocation,
   6167 						   h,
   6168 						   sec,
   6169 						   symbol,
   6170 						   &value,
   6171 						   input_section))
   6172 	    return bfd_reloc_undefined;
   6173 	}
   6174       else
   6175 	{
   6176 	  if (r_type != R_MIPS_REL32)
   6177 	    value = symbol + addend;
   6178 	  else
   6179 	    value = addend;
   6180 	}
   6181       value &= howto->dst_mask;
   6182       break;
   6183 
   6184     case R_MIPS_PC32:
   6185       value = symbol + addend - p;
   6186       value &= howto->dst_mask;
   6187       break;
   6188 
   6189     case R_MIPS16_26:
   6190       /* The calculation for R_MIPS16_26 is just the same as for an
   6191 	 R_MIPS_26.  It's only the storage of the relocated field into
   6192 	 the output file that's different.  That's handled in
   6193 	 mips_elf_perform_relocation.  So, we just fall through to the
   6194 	 R_MIPS_26 case here.  */
   6195     case R_MIPS_26:
   6196     case R_MICROMIPS_26_S1:
   6197       {
   6198 	unsigned int shift;
   6199 
   6200 	/* Shift is 2, unusually, for microMIPS JALX.  */
   6201 	shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
   6202 
   6203 	if (howto->partial_inplace && !section_p)
   6204 	  value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
   6205 	else
   6206 	  value = addend;
   6207 	value += symbol;
   6208 
   6209 	/* Make sure the target of a jump is suitably aligned.  Bit 0 must
   6210 	   be the correct ISA mode selector except for weak undefined
   6211 	   symbols.  */
   6212 	if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6213 	    && (*cross_mode_jump_p
   6214 		? (value & 3) != (r_type == R_MIPS_26)
   6215 		: (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
   6216 	  return bfd_reloc_outofrange;
   6217 
   6218 	value >>= shift;
   6219 	if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6220 	  overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
   6221 	value &= howto->dst_mask;
   6222       }
   6223       break;
   6224 
   6225     case R_MIPS_TLS_DTPREL_HI16:
   6226     case R_MIPS16_TLS_DTPREL_HI16:
   6227     case R_MICROMIPS_TLS_DTPREL_HI16:
   6228       value = (mips_elf_high (addend + symbol - dtprel_base (info))
   6229 	       & howto->dst_mask);
   6230       break;
   6231 
   6232     case R_MIPS_TLS_DTPREL_LO16:
   6233     case R_MIPS_TLS_DTPREL32:
   6234     case R_MIPS_TLS_DTPREL64:
   6235     case R_MIPS16_TLS_DTPREL_LO16:
   6236     case R_MICROMIPS_TLS_DTPREL_LO16:
   6237       value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
   6238       break;
   6239 
   6240     case R_MIPS_TLS_TPREL_HI16:
   6241     case R_MIPS16_TLS_TPREL_HI16:
   6242     case R_MICROMIPS_TLS_TPREL_HI16:
   6243       value = (mips_elf_high (addend + symbol - tprel_base (info))
   6244 	       & howto->dst_mask);
   6245       break;
   6246 
   6247     case R_MIPS_TLS_TPREL_LO16:
   6248     case R_MIPS_TLS_TPREL32:
   6249     case R_MIPS_TLS_TPREL64:
   6250     case R_MIPS16_TLS_TPREL_LO16:
   6251     case R_MICROMIPS_TLS_TPREL_LO16:
   6252       value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
   6253       break;
   6254 
   6255     case R_MIPS_HI16:
   6256     case R_MIPS16_HI16:
   6257     case R_MICROMIPS_HI16:
   6258       if (!gp_disp_p)
   6259 	{
   6260 	  value = mips_elf_high (addend + symbol);
   6261 	  value &= howto->dst_mask;
   6262 	}
   6263       else
   6264 	{
   6265 	  /* For MIPS16 ABI code we generate this sequence
   6266 		0: li      $v0,%hi(_gp_disp)
   6267 		4: addiupc $v1,%lo(_gp_disp)
   6268 		8: sll     $v0,16
   6269 	       12: addu    $v0,$v1
   6270 	       14: move    $gp,$v0
   6271 	     So the offsets of hi and lo relocs are the same, but the
   6272 	     base $pc is that used by the ADDIUPC instruction at $t9 + 4.
   6273 	     ADDIUPC clears the low two bits of the instruction address,
   6274 	     so the base is ($t9 + 4) & ~3.  */
   6275 	  if (r_type == R_MIPS16_HI16)
   6276 	    value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
   6277 	  /* The microMIPS .cpload sequence uses the same assembly
   6278 	     instructions as the traditional psABI version, but the
   6279 	     incoming $t9 has the low bit set.  */
   6280 	  else if (r_type == R_MICROMIPS_HI16)
   6281 	    value = mips_elf_high (addend + gp - p - 1);
   6282 	  else
   6283 	    value = mips_elf_high (addend + gp - p);
   6284 	}
   6285       break;
   6286 
   6287     case R_MIPS_LO16:
   6288     case R_MIPS16_LO16:
   6289     case R_MICROMIPS_LO16:
   6290     case R_MICROMIPS_HI0_LO16:
   6291       if (!gp_disp_p)
   6292 	value = (symbol + addend) & howto->dst_mask;
   6293       else
   6294 	{
   6295 	  /* See the comment for R_MIPS16_HI16 above for the reason
   6296 	     for this conditional.  */
   6297 	  if (r_type == R_MIPS16_LO16)
   6298 	    value = addend + gp - (p & ~(bfd_vma) 0x3);
   6299 	  else if (r_type == R_MICROMIPS_LO16
   6300 		   || r_type == R_MICROMIPS_HI0_LO16)
   6301 	    value = addend + gp - p + 3;
   6302 	  else
   6303 	    value = addend + gp - p + 4;
   6304 	  /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
   6305 	     for overflow.  But, on, say, IRIX5, relocations against
   6306 	     _gp_disp are normally generated from the .cpload
   6307 	     pseudo-op.  It generates code that normally looks like
   6308 	     this:
   6309 
   6310 	       lui    $gp,%hi(_gp_disp)
   6311 	       addiu  $gp,$gp,%lo(_gp_disp)
   6312 	       addu   $gp,$gp,$t9
   6313 
   6314 	     Here $t9 holds the address of the function being called,
   6315 	     as required by the MIPS ELF ABI.  The R_MIPS_LO16
   6316 	     relocation can easily overflow in this situation, but the
   6317 	     R_MIPS_HI16 relocation will handle the overflow.
   6318 	     Therefore, we consider this a bug in the MIPS ABI, and do
   6319 	     not check for overflow here.  */
   6320 	}
   6321       break;
   6322 
   6323     case R_MIPS_LITERAL:
   6324     case R_MICROMIPS_LITERAL:
   6325       /* Because we don't merge literal sections, we can handle this
   6326 	 just like R_MIPS_GPREL16.  In the long run, we should merge
   6327 	 shared literals, and then we will need to additional work
   6328 	 here.  */
   6329 
   6330       /* Fall through.  */
   6331 
   6332     case R_MIPS16_GPREL:
   6333       /* The R_MIPS16_GPREL performs the same calculation as
   6334 	 R_MIPS_GPREL16, but stores the relocated bits in a different
   6335 	 order.  We don't need to do anything special here; the
   6336 	 differences are handled in mips_elf_perform_relocation.  */
   6337     case R_MIPS_GPREL16:
   6338     case R_MICROMIPS_GPREL7_S2:
   6339     case R_MICROMIPS_GPREL16:
   6340       {
   6341 	int bits = howto->bitsize + howto->rightshift;
   6342 	/* Only sign-extend the addend if it was extracted from the
   6343 	   instruction.  If the addend was separate, leave it alone,
   6344 	   otherwise we may lose significant bits.  */
   6345 	if (howto->partial_inplace)
   6346 	  addend = _bfd_mips_elf_sign_extend (addend, bits);
   6347 	value = symbol + addend - gp;
   6348 	/* If the symbol was local, any earlier relocatable links will
   6349 	   have adjusted its addend with the gp offset, so compensate
   6350 	   for that now.  Don't do it for symbols forced local in this
   6351 	   link, though, since they won't have had the gp offset applied
   6352 	   to them before.  */
   6353 	if (was_local_p)
   6354 	  value += gp0;
   6355 	if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6356 	  overflowed_p = mips_elf_overflow_p (value, bits);
   6357       }
   6358       break;
   6359 
   6360     case R_MIPS16_GOT16:
   6361     case R_MIPS16_CALL16:
   6362     case R_MIPS_GOT16:
   6363     case R_MIPS_CALL16:
   6364     case R_MICROMIPS_GOT16:
   6365     case R_MICROMIPS_CALL16:
   6366       /* VxWorks does not have separate local and global semantics for
   6367 	 R_MIPS*_GOT16; every relocation evaluates to "G".  */
   6368       if (htab->root.target_os != is_vxworks && local_p)
   6369 	{
   6370 	  value = mips_elf_got16_entry (abfd, input_bfd, info,
   6371 					symbol + addend, !was_local_p);
   6372 	  if (value == MINUS_ONE)
   6373 	    return bfd_reloc_outofrange;
   6374 	  value
   6375 	    = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
   6376 	  overflowed_p = mips_elf_overflow_p (value, 16);
   6377 	  break;
   6378 	}
   6379 
   6380       /* Fall through.  */
   6381 
   6382     case R_MIPS_TLS_GD:
   6383     case R_MIPS_TLS_GOTTPREL:
   6384     case R_MIPS_TLS_LDM:
   6385     case R_MIPS_GOT_DISP:
   6386     case R_MIPS16_TLS_GD:
   6387     case R_MIPS16_TLS_GOTTPREL:
   6388     case R_MIPS16_TLS_LDM:
   6389     case R_MICROMIPS_TLS_GD:
   6390     case R_MICROMIPS_TLS_GOTTPREL:
   6391     case R_MICROMIPS_TLS_LDM:
   6392     case R_MICROMIPS_GOT_DISP:
   6393       value = g;
   6394       overflowed_p = mips_elf_overflow_p (value, 16);
   6395       break;
   6396 
   6397     case R_MIPS_GPREL32:
   6398       value = (addend + symbol + gp0 - gp);
   6399       if (!save_addend)
   6400 	value &= howto->dst_mask;
   6401       break;
   6402 
   6403     case R_MIPS_PC16:
   6404     case R_MIPS_GNU_REL16_S2:
   6405       if (howto->partial_inplace)
   6406 	addend = _bfd_mips_elf_sign_extend (addend, 18);
   6407 
   6408       /* No need to exclude weak undefined symbols here as they resolve
   6409 	 to 0 and never set `*cross_mode_jump_p', so this alignment check
   6410 	 will never trigger for them.  */
   6411       if (*cross_mode_jump_p
   6412 	  ? ((symbol + addend) & 3) != 1
   6413 	  : ((symbol + addend) & 3) != 0)
   6414 	return bfd_reloc_outofrange;
   6415 
   6416       value = symbol + addend - p;
   6417       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6418 	overflowed_p = mips_elf_overflow_p (value, 18);
   6419       value >>= howto->rightshift;
   6420       value &= howto->dst_mask;
   6421       break;
   6422 
   6423     case R_MIPS16_PC16_S1:
   6424       if (howto->partial_inplace)
   6425 	addend = _bfd_mips_elf_sign_extend (addend, 17);
   6426 
   6427       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6428 	  && (*cross_mode_jump_p
   6429 	      ? ((symbol + addend) & 3) != 0
   6430 	      : ((symbol + addend) & 1) == 0))
   6431 	return bfd_reloc_outofrange;
   6432 
   6433       value = symbol + addend - p;
   6434       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6435 	overflowed_p = mips_elf_overflow_p (value, 17);
   6436       value >>= howto->rightshift;
   6437       value &= howto->dst_mask;
   6438       break;
   6439 
   6440     case R_MIPS_PC21_S2:
   6441       if (howto->partial_inplace)
   6442 	addend = _bfd_mips_elf_sign_extend (addend, 23);
   6443 
   6444       if ((symbol + addend) & 3)
   6445 	return bfd_reloc_outofrange;
   6446 
   6447       value = symbol + addend - p;
   6448       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6449 	overflowed_p = mips_elf_overflow_p (value, 23);
   6450       value >>= howto->rightshift;
   6451       value &= howto->dst_mask;
   6452       break;
   6453 
   6454     case R_MIPS_PC26_S2:
   6455       if (howto->partial_inplace)
   6456 	addend = _bfd_mips_elf_sign_extend (addend, 28);
   6457 
   6458       if ((symbol + addend) & 3)
   6459 	return bfd_reloc_outofrange;
   6460 
   6461       value = symbol + addend - p;
   6462       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6463 	overflowed_p = mips_elf_overflow_p (value, 28);
   6464       value >>= howto->rightshift;
   6465       value &= howto->dst_mask;
   6466       break;
   6467 
   6468     case R_MIPS_PC18_S3:
   6469       if (howto->partial_inplace)
   6470 	addend = _bfd_mips_elf_sign_extend (addend, 21);
   6471 
   6472       if ((symbol + addend) & 7)
   6473 	return bfd_reloc_outofrange;
   6474 
   6475       value = symbol + addend - ((p | 7) ^ 7);
   6476       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6477 	overflowed_p = mips_elf_overflow_p (value, 21);
   6478       value >>= howto->rightshift;
   6479       value &= howto->dst_mask;
   6480       break;
   6481 
   6482     case R_MIPS_PC19_S2:
   6483       if (howto->partial_inplace)
   6484 	addend = _bfd_mips_elf_sign_extend (addend, 21);
   6485 
   6486       if ((symbol + addend) & 3)
   6487 	return bfd_reloc_outofrange;
   6488 
   6489       value = symbol + addend - p;
   6490       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6491 	overflowed_p = mips_elf_overflow_p (value, 21);
   6492       value >>= howto->rightshift;
   6493       value &= howto->dst_mask;
   6494       break;
   6495 
   6496     case R_MIPS_PCHI16:
   6497       value = mips_elf_high (symbol + addend - p);
   6498       value &= howto->dst_mask;
   6499       break;
   6500 
   6501     case R_MIPS_PCLO16:
   6502       if (howto->partial_inplace)
   6503 	addend = _bfd_mips_elf_sign_extend (addend, 16);
   6504       value = symbol + addend - p;
   6505       value &= howto->dst_mask;
   6506       break;
   6507 
   6508     case R_MICROMIPS_PC7_S1:
   6509       if (howto->partial_inplace)
   6510 	addend = _bfd_mips_elf_sign_extend (addend, 8);
   6511 
   6512       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6513 	  && (*cross_mode_jump_p
   6514 	      ? ((symbol + addend + 2) & 3) != 0
   6515 	      : ((symbol + addend + 2) & 1) == 0))
   6516 	return bfd_reloc_outofrange;
   6517 
   6518       value = symbol + addend - p;
   6519       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6520 	overflowed_p = mips_elf_overflow_p (value, 8);
   6521       value >>= howto->rightshift;
   6522       value &= howto->dst_mask;
   6523       break;
   6524 
   6525     case R_MICROMIPS_PC10_S1:
   6526       if (howto->partial_inplace)
   6527 	addend = _bfd_mips_elf_sign_extend (addend, 11);
   6528 
   6529       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6530 	  && (*cross_mode_jump_p
   6531 	      ? ((symbol + addend + 2) & 3) != 0
   6532 	      : ((symbol + addend + 2) & 1) == 0))
   6533 	return bfd_reloc_outofrange;
   6534 
   6535       value = symbol + addend - p;
   6536       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6537 	overflowed_p = mips_elf_overflow_p (value, 11);
   6538       value >>= howto->rightshift;
   6539       value &= howto->dst_mask;
   6540       break;
   6541 
   6542     case R_MICROMIPS_PC16_S1:
   6543       if (howto->partial_inplace)
   6544 	addend = _bfd_mips_elf_sign_extend (addend, 17);
   6545 
   6546       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6547 	  && (*cross_mode_jump_p
   6548 	      ? ((symbol + addend) & 3) != 0
   6549 	      : ((symbol + addend) & 1) == 0))
   6550 	return bfd_reloc_outofrange;
   6551 
   6552       value = symbol + addend - p;
   6553       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6554 	overflowed_p = mips_elf_overflow_p (value, 17);
   6555       value >>= howto->rightshift;
   6556       value &= howto->dst_mask;
   6557       break;
   6558 
   6559     case R_MICROMIPS_PC23_S2:
   6560       if (howto->partial_inplace)
   6561 	addend = _bfd_mips_elf_sign_extend (addend, 25);
   6562       value = symbol + addend - ((p | 3) ^ 3);
   6563       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6564 	overflowed_p = mips_elf_overflow_p (value, 25);
   6565       value >>= howto->rightshift;
   6566       value &= howto->dst_mask;
   6567       break;
   6568 
   6569     case R_MIPS_GOT_HI16:
   6570     case R_MIPS_CALL_HI16:
   6571     case R_MICROMIPS_GOT_HI16:
   6572     case R_MICROMIPS_CALL_HI16:
   6573       /* We're allowed to handle these two relocations identically.
   6574 	 The dynamic linker is allowed to handle the CALL relocations
   6575 	 differently by creating a lazy evaluation stub.  */
   6576       value = g;
   6577       value = mips_elf_high (value);
   6578       value &= howto->dst_mask;
   6579       break;
   6580 
   6581     case R_MIPS_GOT_LO16:
   6582     case R_MIPS_CALL_LO16:
   6583     case R_MICROMIPS_GOT_LO16:
   6584     case R_MICROMIPS_CALL_LO16:
   6585       value = g & howto->dst_mask;
   6586       break;
   6587 
   6588     case R_MIPS_GOT_PAGE:
   6589     case R_MICROMIPS_GOT_PAGE:
   6590       value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
   6591       if (value == MINUS_ONE)
   6592 	return bfd_reloc_outofrange;
   6593       value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
   6594       overflowed_p = mips_elf_overflow_p (value, 16);
   6595       break;
   6596 
   6597     case R_MIPS_GOT_OFST:
   6598     case R_MICROMIPS_GOT_OFST:
   6599       if (local_p)
   6600 	mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
   6601       else
   6602 	value = addend;
   6603       overflowed_p = mips_elf_overflow_p (value, 16);
   6604       break;
   6605 
   6606     case R_MIPS_SUB:
   6607     case R_MICROMIPS_SUB:
   6608       value = symbol - addend;
   6609       value &= howto->dst_mask;
   6610       break;
   6611 
   6612     case R_MIPS_HIGHER:
   6613     case R_MICROMIPS_HIGHER:
   6614       value = mips_elf_higher (addend + symbol);
   6615       value &= howto->dst_mask;
   6616       break;
   6617 
   6618     case R_MIPS_HIGHEST:
   6619     case R_MICROMIPS_HIGHEST:
   6620       value = mips_elf_highest (addend + symbol);
   6621       value &= howto->dst_mask;
   6622       break;
   6623 
   6624     case R_MIPS_SCN_DISP:
   6625     case R_MICROMIPS_SCN_DISP:
   6626       value = symbol + addend - sec->output_offset;
   6627       value &= howto->dst_mask;
   6628       break;
   6629 
   6630     case R_MIPS_JALR:
   6631     case R_MICROMIPS_JALR:
   6632       /* This relocation is only a hint.  In some cases, we optimize
   6633 	 it into a bal instruction.  But we don't try to optimize
   6634 	 when the symbol does not resolve locally.  */
   6635       if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
   6636 	return bfd_reloc_continue;
   6637       /* We can't optimize cross-mode jumps either.  */
   6638       if (*cross_mode_jump_p)
   6639 	return bfd_reloc_continue;
   6640       value = symbol + addend;
   6641       /* Neither we can non-instruction-aligned targets.  */
   6642       if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
   6643 	return bfd_reloc_continue;
   6644       break;
   6645 
   6646     case R_MIPS_PJUMP:
   6647     case R_MIPS_GNU_VTINHERIT:
   6648     case R_MIPS_GNU_VTENTRY:
   6649       /* We don't do anything with these at present.  */
   6650       return bfd_reloc_continue;
   6651 
   6652     default:
   6653       /* An unrecognized relocation type.  */
   6654       return bfd_reloc_notsupported;
   6655     }
   6656 
   6657   /* Store the VALUE for our caller.  */
   6658   *valuep = value;
   6659   return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
   6660 }
   6661 
   6662 /* It has been determined that the result of the RELOCATION is the
   6663    VALUE.  Use HOWTO to place VALUE into the output file at the
   6664    appropriate position.  The SECTION is the section to which the
   6665    relocation applies.
   6666    CROSS_MODE_JUMP_P is true if the relocation field
   6667    is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
   6668 
   6669    Returns FALSE if anything goes wrong.  */
   6670 
   6671 static bool
   6672 mips_elf_perform_relocation (struct bfd_link_info *info,
   6673 			     reloc_howto_type *howto,
   6674 			     const Elf_Internal_Rela *relocation,
   6675 			     bfd_vma value, bfd *input_bfd,
   6676 			     asection *input_section, bfd_byte *contents,
   6677 			     bool cross_mode_jump_p)
   6678 {
   6679   bfd_vma x;
   6680   bfd_byte *location;
   6681   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   6682 
   6683   /* Figure out where the relocation is occurring.  */
   6684   location = contents + relocation->r_offset;
   6685 
   6686   _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
   6687 
   6688   /* Obtain the current value.  */
   6689   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
   6690 
   6691   /* Clear the field we are setting.  */
   6692   x &= ~howto->dst_mask;
   6693 
   6694   /* Set the field.  */
   6695   x |= (value & howto->dst_mask);
   6696 
   6697   /* Detect incorrect JALX usage.  If required, turn JAL or BAL into JALX.  */
   6698   if (!cross_mode_jump_p && jal_reloc_p (r_type))
   6699     {
   6700       bfd_vma opcode = x >> 26;
   6701 
   6702       if (r_type == R_MIPS16_26 ? opcode == 0x7
   6703 	  : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
   6704 	  : opcode == 0x1d)
   6705 	{
   6706 	  info->callbacks->einfo
   6707 	    (_("%X%H: unsupported JALX to the same ISA mode\n"),
   6708 	     input_bfd, input_section, relocation->r_offset);
   6709 	  return true;
   6710 	}
   6711     }
   6712   if (cross_mode_jump_p && jal_reloc_p (r_type))
   6713     {
   6714       bool ok;
   6715       bfd_vma opcode = x >> 26;
   6716       bfd_vma jalx_opcode;
   6717 
   6718       /* Check to see if the opcode is already JAL or JALX.  */
   6719       if (r_type == R_MIPS16_26)
   6720 	{
   6721 	  ok = ((opcode == 0x6) || (opcode == 0x7));
   6722 	  jalx_opcode = 0x7;
   6723 	}
   6724       else if (r_type == R_MICROMIPS_26_S1)
   6725 	{
   6726 	  ok = ((opcode == 0x3d) || (opcode == 0x3c));
   6727 	  jalx_opcode = 0x3c;
   6728 	}
   6729       else
   6730 	{
   6731 	  ok = ((opcode == 0x3) || (opcode == 0x1d));
   6732 	  jalx_opcode = 0x1d;
   6733 	}
   6734 
   6735       /* If the opcode is not JAL or JALX, there's a problem.  We cannot
   6736 	 convert J or JALS to JALX.  */
   6737       if (!ok)
   6738 	{
   6739 	  info->callbacks->einfo
   6740 	    (_("%X%H: unsupported jump between ISA modes; "
   6741 	       "consider recompiling with interlinking enabled\n"),
   6742 	     input_bfd, input_section, relocation->r_offset);
   6743 	  return true;
   6744 	}
   6745 
   6746       /* Make this the JALX opcode.  */
   6747       x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
   6748     }
   6749   else if (cross_mode_jump_p && b_reloc_p (r_type))
   6750     {
   6751       bool ok = false;
   6752       bfd_vma opcode = x >> 16;
   6753       bfd_vma jalx_opcode = 0;
   6754       bfd_vma sign_bit = 0;
   6755       bfd_vma addr;
   6756       bfd_vma dest;
   6757 
   6758       if (r_type == R_MICROMIPS_PC16_S1)
   6759 	{
   6760 	  ok = opcode == 0x4060;
   6761 	  jalx_opcode = 0x3c;
   6762 	  sign_bit = 0x10000;
   6763 	  value <<= 1;
   6764 	}
   6765       else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
   6766 	{
   6767 	  ok = opcode == 0x411;
   6768 	  jalx_opcode = 0x1d;
   6769 	  sign_bit = 0x20000;
   6770 	  value <<= 2;
   6771 	}
   6772 
   6773       if (ok && !bfd_link_pic (info))
   6774 	{
   6775 	  addr = (input_section->output_section->vma
   6776 		  + input_section->output_offset
   6777 		  + relocation->r_offset
   6778 		  + 4);
   6779 	  dest = (addr
   6780 		  + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
   6781 
   6782 	  if ((addr >> 28) << 28 != (dest >> 28) << 28)
   6783 	    {
   6784 	      info->callbacks->einfo
   6785 		(_("%X%H: cannot convert branch between ISA modes "
   6786 		   "to JALX: relocation out of range\n"),
   6787 		 input_bfd, input_section, relocation->r_offset);
   6788 	      return true;
   6789 	    }
   6790 
   6791 	  /* Make this the JALX opcode.  */
   6792 	  x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
   6793 	}
   6794       else if (!mips_elf_hash_table (info)->ignore_branch_isa)
   6795 	{
   6796 	  info->callbacks->einfo
   6797 	    (_("%X%H: unsupported branch between ISA modes\n"),
   6798 	     input_bfd, input_section, relocation->r_offset);
   6799 	  return true;
   6800 	}
   6801     }
   6802 
   6803   /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
   6804      range.  */
   6805   if (!bfd_link_relocatable (info)
   6806       && !cross_mode_jump_p
   6807       && ((JAL_TO_BAL_P (input_bfd)
   6808 	   && r_type == R_MIPS_26
   6809 	   && (x >> 26) == 0x3)			/* jal addr */
   6810 	  || (JALR_TO_BAL_P (input_bfd)
   6811 	      && r_type == R_MIPS_JALR
   6812 	      && x == 0x0320f809)		/* jalr t9 */
   6813 	  || (JR_TO_B_P (input_bfd)
   6814 	      && r_type == R_MIPS_JALR
   6815 	      && (x & ~1) == 0x03200008)))	/* jr t9 / jalr zero, t9 */
   6816     {
   6817       bfd_vma addr;
   6818       bfd_vma dest;
   6819       bfd_signed_vma off;
   6820 
   6821       addr = (input_section->output_section->vma
   6822 	      + input_section->output_offset
   6823 	      + relocation->r_offset
   6824 	      + 4);
   6825       if (r_type == R_MIPS_26)
   6826 	dest = (value << 2) | ((addr >> 28) << 28);
   6827       else
   6828 	dest = value;
   6829       off = dest - addr;
   6830       if (off <= 0x1ffff && off >= -0x20000)
   6831 	{
   6832 	  if ((x & ~1) == 0x03200008)		/* jr t9 / jalr zero, t9 */
   6833 	    x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
   6834 	  else
   6835 	    x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
   6836 	}
   6837     }
   6838 
   6839   /* Put the value into the output.  */
   6840   mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
   6841 
   6842   _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
   6843 			       location);
   6844 
   6845   return true;
   6846 }
   6847 
   6848 /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
   6850    is the original relocation, which is now being transformed into a
   6851    dynamic relocation.  The ADDENDP is adjusted if necessary; the
   6852    caller should store the result in place of the original addend.  */
   6853 
   6854 static bool
   6855 mips_elf_create_dynamic_relocation (bfd *output_bfd,
   6856 				    struct bfd_link_info *info,
   6857 				    const Elf_Internal_Rela *rel,
   6858 				    struct mips_elf_link_hash_entry *h,
   6859 				    asection *sec, bfd_vma symbol,
   6860 				    bfd_vma *addendp, asection *input_section)
   6861 {
   6862   Elf_Internal_Rela outrel[3];
   6863   asection *sreloc;
   6864   bfd *dynobj;
   6865   int r_type;
   6866   long indx;
   6867   bool defined_p;
   6868   struct mips_elf_link_hash_table *htab;
   6869 
   6870   htab = mips_elf_hash_table (info);
   6871   BFD_ASSERT (htab != NULL);
   6872 
   6873   r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   6874   dynobj = elf_hash_table (info)->dynobj;
   6875   sreloc = mips_elf_rel_dyn_section (info, false);
   6876   BFD_ASSERT (sreloc != NULL);
   6877   BFD_ASSERT (sreloc->contents != NULL);
   6878   BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
   6879 	      < sreloc->size);
   6880 
   6881   outrel[0].r_offset =
   6882     _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
   6883   if (ABI_64_P (output_bfd))
   6884     {
   6885       outrel[1].r_offset =
   6886 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
   6887       outrel[2].r_offset =
   6888 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
   6889     }
   6890 
   6891   if (outrel[0].r_offset == MINUS_ONE)
   6892     /* The relocation field has been deleted.  */
   6893     return true;
   6894 
   6895   if (outrel[0].r_offset == MINUS_TWO)
   6896     {
   6897       /* The relocation field has been converted into a relative value of
   6898 	 some sort.  Functions like _bfd_elf_write_section_eh_frame expect
   6899 	 the field to be fully relocated, so add in the symbol's value.  */
   6900       *addendp += symbol;
   6901       return true;
   6902     }
   6903 
   6904   /* We must now calculate the dynamic symbol table index to use
   6905      in the relocation.  */
   6906   if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
   6907     {
   6908       BFD_ASSERT (htab->root.target_os == is_vxworks
   6909 		  || h->global_got_area != GGA_NONE);
   6910       indx = h->root.dynindx;
   6911       if (SGI_COMPAT (output_bfd))
   6912 	defined_p = h->root.def_regular;
   6913       else
   6914 	/* ??? glibc's ld.so just adds the final GOT entry to the
   6915 	   relocation field.  It therefore treats relocs against
   6916 	   defined symbols in the same way as relocs against
   6917 	   undefined symbols.  */
   6918 	defined_p = false;
   6919     }
   6920   else
   6921     {
   6922       if (sec != NULL && bfd_is_abs_section (sec))
   6923 	indx = 0;
   6924       else if (sec == NULL || sec->owner == NULL)
   6925 	{
   6926 	  BFD_ASSERT (0);
   6927 	  bfd_set_error (bfd_error_bad_value);
   6928 	  return false;
   6929 	}
   6930       else
   6931 	{
   6932 	  indx = elf_section_data (sec->output_section)->dynindx;
   6933 	  if (indx == 0)
   6934 	    {
   6935 	      asection *osec = htab->root.text_index_section;
   6936 	      indx = elf_section_data (osec)->dynindx;
   6937 	    }
   6938 	  if (indx == 0)
   6939 	    abort ();
   6940 	}
   6941 
   6942       /* Instead of generating a relocation using the section
   6943 	 symbol, we may as well make it a fully relative
   6944 	 relocation.  We want to avoid generating relocations to
   6945 	 local symbols because we used to generate them
   6946 	 incorrectly, without adding the original symbol value,
   6947 	 which is mandated by the ABI for section symbols.  In
   6948 	 order to give dynamic loaders and applications time to
   6949 	 phase out the incorrect use, we refrain from emitting
   6950 	 section-relative relocations.  It's not like they're
   6951 	 useful, after all.  This should be a bit more efficient
   6952 	 as well.  */
   6953       /* ??? Although this behavior is compatible with glibc's ld.so,
   6954 	 the ABI says that relocations against STN_UNDEF should have
   6955 	 a symbol value of 0.  Irix rld honors this, so relocations
   6956 	 against STN_UNDEF have no effect.  */
   6957       if (!SGI_COMPAT (output_bfd))
   6958 	indx = 0;
   6959       defined_p = true;
   6960     }
   6961 
   6962   /* If the relocation was previously an absolute relocation and
   6963      this symbol will not be referred to by the relocation, we must
   6964      adjust it by the value we give it in the dynamic symbol table.
   6965      Otherwise leave the job up to the dynamic linker.  */
   6966   if (defined_p && r_type != R_MIPS_REL32)
   6967     *addendp += symbol;
   6968 
   6969   if (htab->root.target_os == is_vxworks)
   6970     /* VxWorks uses non-relative relocations for this.  */
   6971     outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
   6972   else
   6973     /* The relocation is always an REL32 relocation because we don't
   6974        know where the shared library will wind up at load-time.  */
   6975     outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
   6976 				   R_MIPS_REL32);
   6977 
   6978   /* For strict adherence to the ABI specification, we should
   6979      generate a R_MIPS_64 relocation record by itself before the
   6980      _REL32/_64 record as well, such that the addend is read in as
   6981      a 64-bit value (REL32 is a 32-bit relocation, after all).
   6982      However, since none of the existing ELF64 MIPS dynamic
   6983      loaders seems to care, we don't waste space with these
   6984      artificial relocations.  If this turns out to not be true,
   6985      mips_elf_allocate_dynamic_relocation() should be tweaked so
   6986      as to make room for a pair of dynamic relocations per
   6987      invocation if ABI_64_P, and here we should generate an
   6988      additional relocation record with R_MIPS_64 by itself for a
   6989      NULL symbol before this relocation record.  */
   6990   outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
   6991 				 ABI_64_P (output_bfd)
   6992 				 ? R_MIPS_64
   6993 				 : R_MIPS_NONE);
   6994   outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
   6995 
   6996   /* Adjust the output offset of the relocation to reference the
   6997      correct location in the output file.  */
   6998   outrel[0].r_offset += (input_section->output_section->vma
   6999 			 + input_section->output_offset);
   7000   outrel[1].r_offset += (input_section->output_section->vma
   7001 			 + input_section->output_offset);
   7002   outrel[2].r_offset += (input_section->output_section->vma
   7003 			 + input_section->output_offset);
   7004 
   7005   /* Put the relocation back out.  We have to use the special
   7006      relocation outputter in the 64-bit case since the 64-bit
   7007      relocation format is non-standard.  */
   7008   if (ABI_64_P (output_bfd))
   7009     {
   7010       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
   7011 	(output_bfd, &outrel[0],
   7012 	 (sreloc->contents
   7013 	  + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
   7014     }
   7015   else if (htab->root.target_os == is_vxworks)
   7016     {
   7017       /* VxWorks uses RELA rather than REL dynamic relocations.  */
   7018       outrel[0].r_addend = *addendp;
   7019       bfd_elf32_swap_reloca_out
   7020 	(output_bfd, &outrel[0],
   7021 	 (sreloc->contents
   7022 	  + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
   7023     }
   7024   else
   7025     bfd_elf32_swap_reloc_out
   7026       (output_bfd, &outrel[0],
   7027        (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
   7028 
   7029   /* We've now added another relocation.  */
   7030   ++sreloc->reloc_count;
   7031 
   7032   /* Make sure the output section is writable.  The dynamic linker
   7033      will be writing to it.  */
   7034   elf_section_data (input_section->output_section)->this_hdr.sh_flags
   7035     |= SHF_WRITE;
   7036 
   7037   /* On IRIX5, make an entry of compact relocation info.  */
   7038   if (IRIX_COMPAT (output_bfd) == ict_irix5)
   7039     {
   7040       asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
   7041       bfd_byte *cr;
   7042 
   7043       if (scpt)
   7044 	{
   7045 	  Elf32_crinfo cptrel;
   7046 
   7047 	  mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
   7048 	  cptrel.vaddr = (rel->r_offset
   7049 			  + input_section->output_section->vma
   7050 			  + input_section->output_offset);
   7051 	  if (r_type == R_MIPS_REL32)
   7052 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
   7053 	  else
   7054 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
   7055 	  mips_elf_set_cr_dist2to (cptrel, 0);
   7056 	  cptrel.konst = *addendp;
   7057 
   7058 	  cr = (scpt->contents
   7059 		+ sizeof (Elf32_External_compact_rel));
   7060 	  mips_elf_set_cr_relvaddr (cptrel, 0);
   7061 	  bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
   7062 				     ((Elf32_External_crinfo *) cr
   7063 				      + scpt->reloc_count));
   7064 	  ++scpt->reloc_count;
   7065 	}
   7066     }
   7067 
   7068   /* If we've written this relocation for a readonly section,
   7069      we need to set DF_TEXTREL again, so that we do not delete the
   7070      DT_TEXTREL tag.  */
   7071   if (MIPS_ELF_READONLY_SECTION (input_section))
   7072     info->flags |= DF_TEXTREL;
   7073 
   7074   return true;
   7075 }
   7076 
   7077 /* Return the MACH for a MIPS e_flags value.  */
   7079 
   7080 unsigned long
   7081 _bfd_elf_mips_mach (flagword flags)
   7082 {
   7083   switch (flags & EF_MIPS_MACH)
   7084     {
   7085     case EF_MIPS_MACH_3900:
   7086       return bfd_mach_mips3900;
   7087 
   7088     case EF_MIPS_MACH_4010:
   7089       return bfd_mach_mips4010;
   7090 
   7091     case EF_MIPS_MACH_ALLEGREX:
   7092       return bfd_mach_mips_allegrex;
   7093 
   7094     case EF_MIPS_MACH_4100:
   7095       return bfd_mach_mips4100;
   7096 
   7097     case EF_MIPS_MACH_4111:
   7098       return bfd_mach_mips4111;
   7099 
   7100     case EF_MIPS_MACH_4120:
   7101       return bfd_mach_mips4120;
   7102 
   7103     case EF_MIPS_MACH_4650:
   7104       return bfd_mach_mips4650;
   7105 
   7106     case EF_MIPS_MACH_5400:
   7107       return bfd_mach_mips5400;
   7108 
   7109     case EF_MIPS_MACH_5500:
   7110       return bfd_mach_mips5500;
   7111 
   7112     case EF_MIPS_MACH_5900:
   7113       return bfd_mach_mips5900;
   7114 
   7115     case EF_MIPS_MACH_9000:
   7116       return bfd_mach_mips9000;
   7117 
   7118     case EF_MIPS_MACH_SB1:
   7119       return bfd_mach_mips_sb1;
   7120 
   7121     case EF_MIPS_MACH_LS2E:
   7122       return bfd_mach_mips_loongson_2e;
   7123 
   7124     case EF_MIPS_MACH_LS2F:
   7125       return bfd_mach_mips_loongson_2f;
   7126 
   7127     case EF_MIPS_MACH_GS464:
   7128       return bfd_mach_mips_gs464;
   7129 
   7130     case EF_MIPS_MACH_GS464E:
   7131       return bfd_mach_mips_gs464e;
   7132 
   7133     case EF_MIPS_MACH_GS264E:
   7134       return bfd_mach_mips_gs264e;
   7135 
   7136     case EF_MIPS_MACH_OCTEON3:
   7137       return bfd_mach_mips_octeon3;
   7138 
   7139     case EF_MIPS_MACH_OCTEON2:
   7140       return bfd_mach_mips_octeon2;
   7141 
   7142     case EF_MIPS_MACH_OCTEON:
   7143       return bfd_mach_mips_octeon;
   7144 
   7145     case EF_MIPS_MACH_XLR:
   7146       return bfd_mach_mips_xlr;
   7147 
   7148     case EF_MIPS_MACH_IAMR2:
   7149       return bfd_mach_mips_interaptiv_mr2;
   7150 
   7151     default:
   7152       switch (flags & EF_MIPS_ARCH)
   7153 	{
   7154 	default:
   7155 	case EF_MIPS_ARCH_1:
   7156 	  return bfd_mach_mips3000;
   7157 
   7158 	case EF_MIPS_ARCH_2:
   7159 	  return bfd_mach_mips6000;
   7160 
   7161 	case EF_MIPS_ARCH_3:
   7162 	  return bfd_mach_mips4000;
   7163 
   7164 	case EF_MIPS_ARCH_4:
   7165 	  return bfd_mach_mips8000;
   7166 
   7167 	case EF_MIPS_ARCH_5:
   7168 	  return bfd_mach_mips5;
   7169 
   7170 	case EF_MIPS_ARCH_32:
   7171 	  return bfd_mach_mipsisa32;
   7172 
   7173 	case EF_MIPS_ARCH_64:
   7174 	  return bfd_mach_mipsisa64;
   7175 
   7176 	case EF_MIPS_ARCH_32R2:
   7177 	  return bfd_mach_mipsisa32r2;
   7178 
   7179 	case EF_MIPS_ARCH_64R2:
   7180 	  return bfd_mach_mipsisa64r2;
   7181 
   7182 	case EF_MIPS_ARCH_32R6:
   7183 	  return bfd_mach_mipsisa32r6;
   7184 
   7185 	case EF_MIPS_ARCH_64R6:
   7186 	  return bfd_mach_mipsisa64r6;
   7187 	}
   7188     }
   7189 
   7190   return 0;
   7191 }
   7192 
   7193 /* Return printable name for ABI.  */
   7194 
   7195 static inline char *
   7196 elf_mips_abi_name (bfd *abfd)
   7197 {
   7198   flagword flags;
   7199 
   7200   flags = elf_elfheader (abfd)->e_flags;
   7201   switch (flags & EF_MIPS_ABI)
   7202     {
   7203     case 0:
   7204       if (ABI_N32_P (abfd))
   7205 	return "N32";
   7206       else if (ABI_64_P (abfd))
   7207 	return "64";
   7208       else
   7209 	return "none";
   7210     case EF_MIPS_ABI_O32:
   7211       return "O32";
   7212     case EF_MIPS_ABI_O64:
   7213       return "O64";
   7214     case EF_MIPS_ABI_EABI32:
   7215       return "EABI32";
   7216     case EF_MIPS_ABI_EABI64:
   7217       return "EABI64";
   7218     default:
   7219       return "unknown abi";
   7220     }
   7221 }
   7222 
   7223 /* MIPS ELF uses two common sections.  One is the usual one, and the
   7225    other is for small objects.  All the small objects are kept
   7226    together, and then referenced via the gp pointer, which yields
   7227    faster assembler code.  This is what we use for the small common
   7228    section.  This approach is copied from ecoff.c.  */
   7229 static asection mips_elf_scom_section;
   7230 static const asymbol mips_elf_scom_symbol =
   7231   GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
   7232 static asection mips_elf_scom_section =
   7233   BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
   7234 		    ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
   7235 
   7236 /* MIPS ELF also uses an acommon section, which represents an
   7237    allocated common symbol which may be overridden by a
   7238    definition in a shared library.  */
   7239 static asection mips_elf_acom_section;
   7240 static const asymbol mips_elf_acom_symbol =
   7241   GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
   7242 static asection mips_elf_acom_section =
   7243   BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
   7244 		    ".acommon", 0, SEC_ALLOC);
   7245 
   7246 /* This is used for both the 32-bit and the 64-bit ABI.  */
   7247 
   7248 void
   7249 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
   7250 {
   7251   elf_symbol_type *elfsym;
   7252 
   7253   /* Handle the special MIPS section numbers that a symbol may use.  */
   7254   elfsym = (elf_symbol_type *) asym;
   7255   switch (elfsym->internal_elf_sym.st_shndx)
   7256     {
   7257     case SHN_MIPS_ACOMMON:
   7258       /* This section is used in a dynamically linked executable file.
   7259 	 It is an allocated common section.  The dynamic linker can
   7260 	 either resolve these symbols to something in a shared
   7261 	 library, or it can just leave them here.  For our purposes,
   7262 	 we can consider these symbols to be in a new section.  */
   7263       asym->section = &mips_elf_acom_section;
   7264       break;
   7265 
   7266     case SHN_COMMON:
   7267       /* Common symbols less than the GP size are automatically
   7268 	 treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
   7269       if (asym->value > elf_gp_size (abfd)
   7270 	  || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
   7271 	  || IRIX_COMPAT (abfd) == ict_irix6
   7272 	  || strcmp (asym->name, "__gnu_lto_slim") == 0)
   7273 	break;
   7274       /* Fall through.  */
   7275     case SHN_MIPS_SCOMMON:
   7276       asym->section = &mips_elf_scom_section;
   7277       asym->value = elfsym->internal_elf_sym.st_size;
   7278       break;
   7279 
   7280     case SHN_MIPS_SUNDEFINED:
   7281       asym->section = bfd_und_section_ptr;
   7282       break;
   7283 
   7284     case SHN_MIPS_TEXT:
   7285       {
   7286 	asection *section = bfd_get_section_by_name (abfd, ".text");
   7287 
   7288 	if (section != NULL)
   7289 	  {
   7290 	    asym->section = section;
   7291 	    /* MIPS_TEXT is a bit special, the address is not an offset
   7292 	       to the base of the .text section.  So subtract the section
   7293 	       base address to make it an offset.  */
   7294 	    asym->value -= section->vma;
   7295 	  }
   7296       }
   7297       break;
   7298 
   7299     case SHN_MIPS_DATA:
   7300       {
   7301 	asection *section = bfd_get_section_by_name (abfd, ".data");
   7302 
   7303 	if (section != NULL)
   7304 	  {
   7305 	    asym->section = section;
   7306 	    /* MIPS_DATA is a bit special, the address is not an offset
   7307 	       to the base of the .data section.  So subtract the section
   7308 	       base address to make it an offset.  */
   7309 	    asym->value -= section->vma;
   7310 	  }
   7311       }
   7312       break;
   7313     }
   7314 
   7315   /* If this is an odd-valued function symbol, assume it's a MIPS16
   7316      or microMIPS one.  */
   7317   if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
   7318       && (asym->value & 1) != 0)
   7319     {
   7320       asym->value--;
   7321       if (MICROMIPS_P (abfd))
   7322 	elfsym->internal_elf_sym.st_other
   7323 	  = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
   7324       else
   7325 	elfsym->internal_elf_sym.st_other
   7326 	  = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
   7327     }
   7328 }
   7329 
   7330 /* Implement elf_backend_eh_frame_address_size.  This differs from
   7332    the default in the way it handles EABI64.
   7333 
   7334    EABI64 was originally specified as an LP64 ABI, and that is what
   7335    -mabi=eabi normally gives on a 64-bit target.  However, gcc has
   7336    historically accepted the combination of -mabi=eabi and -mlong32,
   7337    and this ILP32 variation has become semi-official over time.
   7338    Both forms use elf32 and have pointer-sized FDE addresses.
   7339 
   7340    If an EABI object was generated by GCC 4.0 or above, it will have
   7341    an empty .gcc_compiled_longXX section, where XX is the size of longs
   7342    in bits.  Unfortunately, ILP32 objects generated by earlier compilers
   7343    have no special marking to distinguish them from LP64 objects.
   7344 
   7345    We don't want users of the official LP64 ABI to be punished for the
   7346    existence of the ILP32 variant, but at the same time, we don't want
   7347    to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
   7348    We therefore take the following approach:
   7349 
   7350       - If ABFD contains a .gcc_compiled_longXX section, use it to
   7351 	determine the pointer size.
   7352 
   7353       - Otherwise check the type of the first relocation.  Assume that
   7354 	the LP64 ABI is being used if the relocation is of type R_MIPS_64.
   7355 
   7356       - Otherwise punt.
   7357 
   7358    The second check is enough to detect LP64 objects generated by pre-4.0
   7359    compilers because, in the kind of output generated by those compilers,
   7360    the first relocation will be associated with either a CIE personality
   7361    routine or an FDE start address.  Furthermore, the compilers never
   7362    used a special (non-pointer) encoding for this ABI.
   7363 
   7364    Checking the relocation type should also be safe because there is no
   7365    reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
   7366    did so.  */
   7367 
   7368 unsigned int
   7369 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
   7370 {
   7371   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   7372     return 8;
   7373   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
   7374     {
   7375       bool long32_p, long64_p;
   7376 
   7377       long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
   7378       long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
   7379       if (long32_p && long64_p)
   7380 	return 0;
   7381       if (long32_p)
   7382 	return 4;
   7383       if (long64_p)
   7384 	return 8;
   7385 
   7386       if (sec->reloc_count > 0)
   7387 	{
   7388 	  /* Load the relocations for this section.  */
   7389 	  Elf_Internal_Rela *internal_relocs =
   7390 	    _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, true);
   7391 	  if (internal_relocs == NULL)
   7392 	    return 0;
   7393 
   7394 	  unsigned int size = 0;
   7395 	  if (ELF32_R_TYPE (internal_relocs[0].r_info) == R_MIPS_64)
   7396 	    size = 8;
   7397 
   7398 	  if (elf_section_data (sec)->relocs != internal_relocs)
   7399 	    free (internal_relocs);
   7400 
   7401 	  return size;
   7402 	}
   7403 
   7404       return 0;
   7405     }
   7406   return 4;
   7407 }
   7408 
   7409 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
   7411    relocations against two unnamed section symbols to resolve to the
   7412    same address.  For example, if we have code like:
   7413 
   7414 	lw	$4,%got_disp(.data)($gp)
   7415 	lw	$25,%got_disp(.text)($gp)
   7416 	jalr	$25
   7417 
   7418    then the linker will resolve both relocations to .data and the program
   7419    will jump there rather than to .text.
   7420 
   7421    We can work around this problem by giving names to local section symbols.
   7422    This is also what the MIPSpro tools do.  */
   7423 
   7424 bool
   7425 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
   7426 {
   7427   return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
   7428 }
   7429 
   7430 /* Work over a section just before writing it out.  This routine is
   7432    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
   7433    sections that need the SHF_MIPS_GPREL flag by name; there has to be
   7434    a better way.  */
   7435 
   7436 bool
   7437 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
   7438 {
   7439   if (hdr->sh_type == SHT_MIPS_REGINFO
   7440       && hdr->sh_size > 0)
   7441     {
   7442       bfd_byte buf[4];
   7443 
   7444       BFD_ASSERT (hdr->contents == NULL);
   7445 
   7446       if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
   7447 	{
   7448 	  _bfd_error_handler
   7449 	    (_("%pB: incorrect `.reginfo' section size; "
   7450 	       "expected %" PRIu64 ", got %" PRIu64),
   7451 	     abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
   7452 	     (uint64_t) hdr->sh_size);
   7453 	  bfd_set_error (bfd_error_bad_value);
   7454 	  return false;
   7455 	}
   7456 
   7457       if (bfd_seek (abfd,
   7458 		    hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
   7459 		    SEEK_SET) != 0)
   7460 	return false;
   7461       H_PUT_32 (abfd, elf_gp (abfd), buf);
   7462       if (bfd_write (buf, 4, abfd) != 4)
   7463 	return false;
   7464     }
   7465 
   7466   if (hdr->sh_type == SHT_MIPS_OPTIONS
   7467       && hdr->bfd_section != NULL
   7468       && mips_elf_section_data (hdr->bfd_section) != NULL
   7469       && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
   7470     {
   7471       bfd_byte *contents, *l, *lend;
   7472 
   7473       /* We stored the section contents in the tdata field in the
   7474 	 set_section_contents routine.  We save the section contents
   7475 	 so that we don't have to read them again.
   7476 	 At this point we know that elf_gp is set, so we can look
   7477 	 through the section contents to see if there is an
   7478 	 ODK_REGINFO structure.  */
   7479 
   7480       contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
   7481       l = contents;
   7482       lend = contents + hdr->sh_size;
   7483       while (l + sizeof (Elf_External_Options) <= lend)
   7484 	{
   7485 	  Elf_Internal_Options intopt;
   7486 
   7487 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
   7488 					&intopt);
   7489 	  if (intopt.size < sizeof (Elf_External_Options))
   7490 	    {
   7491 	      _bfd_error_handler
   7492 		/* xgettext:c-format */
   7493 		(_("%pB: warning: bad `%s' option size %u smaller than"
   7494 		   " its header"),
   7495 		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
   7496 	      break;
   7497 	    }
   7498 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
   7499 	    {
   7500 	      bfd_byte buf[8];
   7501 
   7502 	      if (bfd_seek (abfd,
   7503 			    (hdr->sh_offset
   7504 			     + (l - contents)
   7505 			     + sizeof (Elf_External_Options)
   7506 			     + (sizeof (Elf64_External_RegInfo) - 8)),
   7507 			     SEEK_SET) != 0)
   7508 		return false;
   7509 	      H_PUT_64 (abfd, elf_gp (abfd), buf);
   7510 	      if (bfd_write (buf, 8, abfd) != 8)
   7511 		return false;
   7512 	    }
   7513 	  else if (intopt.kind == ODK_REGINFO)
   7514 	    {
   7515 	      bfd_byte buf[4];
   7516 
   7517 	      if (bfd_seek (abfd,
   7518 			    (hdr->sh_offset
   7519 			     + (l - contents)
   7520 			     + sizeof (Elf_External_Options)
   7521 			     + (sizeof (Elf32_External_RegInfo) - 4)),
   7522 			    SEEK_SET) != 0)
   7523 		return false;
   7524 	      H_PUT_32 (abfd, elf_gp (abfd), buf);
   7525 	      if (bfd_write (buf, 4, abfd) != 4)
   7526 		return false;
   7527 	    }
   7528 	  l += intopt.size;
   7529 	}
   7530     }
   7531 
   7532   if (hdr->bfd_section != NULL)
   7533     {
   7534       const char *name = bfd_section_name (hdr->bfd_section);
   7535 
   7536       /* .sbss is not handled specially here because the GNU/Linux
   7537 	 prelinker can convert .sbss from NOBITS to PROGBITS and
   7538 	 changing it back to NOBITS breaks the binary.  The entry in
   7539 	 _bfd_mips_elf_special_sections will ensure the correct flags
   7540 	 are set on .sbss if BFD creates it without reading it from an
   7541 	 input file, and without special handling here the flags set
   7542 	 on it in an input file will be followed.  */
   7543       if (strcmp (name, ".sdata") == 0
   7544 	  || strcmp (name, ".lit8") == 0
   7545 	  || strcmp (name, ".lit4") == 0)
   7546 	hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
   7547       else if (strcmp (name, ".srdata") == 0)
   7548 	hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
   7549       else if (strcmp (name, ".compact_rel") == 0)
   7550 	hdr->sh_flags = 0;
   7551       else if (strcmp (name, ".rtproc") == 0)
   7552 	{
   7553 	  if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
   7554 	    {
   7555 	      unsigned int adjust;
   7556 
   7557 	      adjust = hdr->sh_size % hdr->sh_addralign;
   7558 	      if (adjust != 0)
   7559 		hdr->sh_size += hdr->sh_addralign - adjust;
   7560 	    }
   7561 	}
   7562     }
   7563 
   7564   return true;
   7565 }
   7566 
   7567 /* Handle a MIPS specific section when reading an object file.  This
   7568    is called when elfcode.h finds a section with an unknown type.
   7569    This routine supports both the 32-bit and 64-bit ELF ABI.  */
   7570 
   7571 bool
   7572 _bfd_mips_elf_section_from_shdr (bfd *abfd,
   7573 				 Elf_Internal_Shdr *hdr,
   7574 				 const char *name,
   7575 				 int shindex)
   7576 {
   7577   flagword flags = 0;
   7578 
   7579   /* There ought to be a place to keep ELF backend specific flags, but
   7580      at the moment there isn't one.  We just keep track of the
   7581      sections by their name, instead.  Fortunately, the ABI gives
   7582      suggested names for all the MIPS specific sections, so we will
   7583      probably get away with this.  */
   7584   switch (hdr->sh_type)
   7585     {
   7586     case SHT_MIPS_LIBLIST:
   7587       if (strcmp (name, ".liblist") != 0)
   7588 	return false;
   7589       break;
   7590     case SHT_MIPS_MSYM:
   7591       if (strcmp (name, ".msym") != 0)
   7592 	return false;
   7593       break;
   7594     case SHT_MIPS_CONFLICT:
   7595       if (strcmp (name, ".conflict") != 0)
   7596 	return false;
   7597       break;
   7598     case SHT_MIPS_GPTAB:
   7599       if (! startswith (name, ".gptab."))
   7600 	return false;
   7601       break;
   7602     case SHT_MIPS_UCODE:
   7603       if (strcmp (name, ".ucode") != 0)
   7604 	return false;
   7605       break;
   7606     case SHT_MIPS_DEBUG:
   7607       if (strcmp (name, ".mdebug") != 0)
   7608 	return false;
   7609       flags = SEC_DEBUGGING;
   7610       break;
   7611     case SHT_MIPS_REGINFO:
   7612       if (strcmp (name, ".reginfo") != 0
   7613 	  || hdr->sh_size != sizeof (Elf32_External_RegInfo))
   7614 	return false;
   7615       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
   7616       break;
   7617     case SHT_MIPS_IFACE:
   7618       if (strcmp (name, ".MIPS.interfaces") != 0)
   7619 	return false;
   7620       break;
   7621     case SHT_MIPS_CONTENT:
   7622       if (! startswith (name, ".MIPS.content"))
   7623 	return false;
   7624       break;
   7625     case SHT_MIPS_OPTIONS:
   7626       if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
   7627 	return false;
   7628       break;
   7629     case SHT_MIPS_ABIFLAGS:
   7630       if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
   7631 	return false;
   7632       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
   7633       break;
   7634     case SHT_MIPS_DWARF:
   7635       if (! startswith (name, ".debug_")
   7636          && ! startswith (name, ".gnu.debuglto_.debug_")
   7637          && ! startswith (name, ".zdebug_")
   7638          && ! startswith (name, ".gnu.debuglto_.zdebug_"))
   7639 	return false;
   7640       break;
   7641     case SHT_MIPS_SYMBOL_LIB:
   7642       if (strcmp (name, ".MIPS.symlib") != 0)
   7643 	return false;
   7644       break;
   7645     case SHT_MIPS_EVENTS:
   7646       if (! startswith (name, ".MIPS.events")
   7647 	  && ! startswith (name, ".MIPS.post_rel"))
   7648 	return false;
   7649       break;
   7650     case SHT_MIPS_XHASH:
   7651       if (strcmp (name, ".MIPS.xhash") != 0)
   7652 	return false;
   7653     default:
   7654       break;
   7655     }
   7656 
   7657   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   7658     return false;
   7659 
   7660   if (hdr->sh_flags & SHF_MIPS_GPREL)
   7661     flags |= SEC_SMALL_DATA;
   7662 
   7663   if (flags)
   7664     {
   7665       if (!bfd_set_section_flags (hdr->bfd_section,
   7666 				  (bfd_section_flags (hdr->bfd_section)
   7667 				   | flags)))
   7668 	return false;
   7669     }
   7670 
   7671   if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
   7672     {
   7673       Elf_External_ABIFlags_v0 ext;
   7674 
   7675       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
   7676 				      &ext, 0, sizeof ext))
   7677 	return false;
   7678       bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
   7679 					&mips_elf_tdata (abfd)->abiflags);
   7680       if (mips_elf_tdata (abfd)->abiflags.version != 0)
   7681 	return false;
   7682       mips_elf_tdata (abfd)->abiflags_valid = true;
   7683     }
   7684 
   7685   /* FIXME: We should record sh_info for a .gptab section.  */
   7686 
   7687   /* For a .reginfo section, set the gp value in the tdata information
   7688      from the contents of this section.  We need the gp value while
   7689      processing relocs, so we just get it now.  The .reginfo section
   7690      is not used in the 64-bit MIPS ELF ABI.  */
   7691   if (hdr->sh_type == SHT_MIPS_REGINFO)
   7692     {
   7693       Elf32_External_RegInfo ext;
   7694       Elf32_RegInfo s;
   7695 
   7696       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
   7697 				      &ext, 0, sizeof ext))
   7698 	return false;
   7699       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
   7700       elf_gp (abfd) = s.ri_gp_value;
   7701     }
   7702 
   7703   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
   7704      set the gp value based on what we find.  We may see both
   7705      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
   7706      they should agree.  */
   7707   if (hdr->sh_type == SHT_MIPS_OPTIONS)
   7708     {
   7709       bfd_byte *contents, *l, *lend;
   7710 
   7711       if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
   7712 	{
   7713 	  free (contents);
   7714 	  return false;
   7715 	}
   7716       l = contents;
   7717       lend = contents + hdr->sh_size;
   7718       while (l + sizeof (Elf_External_Options) <= lend)
   7719 	{
   7720 	  Elf_Internal_Options intopt;
   7721 
   7722 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
   7723 					&intopt);
   7724 	  if (intopt.size < sizeof (Elf_External_Options))
   7725 	    {
   7726 	    bad_opt:
   7727 	      _bfd_error_handler
   7728 		/* xgettext:c-format */
   7729 		(_("%pB: warning: truncated `%s' option"),
   7730 		 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
   7731 	      break;
   7732 	    }
   7733 	  if (intopt.kind == ODK_REGINFO)
   7734 	    {
   7735 	      if (ABI_64_P (abfd))
   7736 		{
   7737 		  Elf64_Internal_RegInfo intreg;
   7738 		  size_t needed = (sizeof (Elf_External_Options)
   7739 				   + sizeof (Elf64_External_RegInfo));
   7740 		  if (intopt.size < needed || (size_t) (lend - l) < needed)
   7741 		    goto bad_opt;
   7742 		  bfd_mips_elf64_swap_reginfo_in
   7743 		    (abfd,
   7744 		     ((Elf64_External_RegInfo *)
   7745 		      (l + sizeof (Elf_External_Options))),
   7746 		     &intreg);
   7747 		  elf_gp (abfd) = intreg.ri_gp_value;
   7748 		}
   7749 	      else
   7750 		{
   7751 		  Elf32_RegInfo intreg;
   7752 		  size_t needed = (sizeof (Elf_External_Options)
   7753 				   + sizeof (Elf32_External_RegInfo));
   7754 		  if (intopt.size < needed || (size_t) (lend - l) < needed)
   7755 		    goto bad_opt;
   7756 		  bfd_mips_elf32_swap_reginfo_in
   7757 		    (abfd,
   7758 		     ((Elf32_External_RegInfo *)
   7759 		      (l + sizeof (Elf_External_Options))),
   7760 		     &intreg);
   7761 		  elf_gp (abfd) = intreg.ri_gp_value;
   7762 		}
   7763 	    }
   7764 	  l += intopt.size;
   7765 	}
   7766       free (contents);
   7767     }
   7768 
   7769   return true;
   7770 }
   7771 
   7772 /* Set the correct type for a MIPS ELF section.  We do this by the
   7773    section name, which is a hack, but ought to work.  This routine is
   7774    used by both the 32-bit and the 64-bit ABI.  */
   7775 
   7776 bool
   7777 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
   7778 {
   7779   const char *name = bfd_section_name (sec);
   7780 
   7781   if (strcmp (name, ".liblist") == 0)
   7782     {
   7783       hdr->sh_type = SHT_MIPS_LIBLIST;
   7784       hdr->sh_info = sec->size / sizeof (Elf32_Lib);
   7785       /* The sh_link field is set in final_write_processing.  */
   7786     }
   7787   else if (strcmp (name, ".conflict") == 0)
   7788     hdr->sh_type = SHT_MIPS_CONFLICT;
   7789   else if (startswith (name, ".gptab."))
   7790     {
   7791       hdr->sh_type = SHT_MIPS_GPTAB;
   7792       hdr->sh_entsize = sizeof (Elf32_External_gptab);
   7793       /* The sh_info field is set in final_write_processing.  */
   7794     }
   7795   else if (strcmp (name, ".ucode") == 0)
   7796     hdr->sh_type = SHT_MIPS_UCODE;
   7797   else if (strcmp (name, ".mdebug") == 0)
   7798     {
   7799       hdr->sh_type = SHT_MIPS_DEBUG;
   7800       /* In a shared object on IRIX 5.3, the .mdebug section has an
   7801 	 entsize of 0.  FIXME: Does this matter?  */
   7802       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
   7803 	hdr->sh_entsize = 0;
   7804       else
   7805 	hdr->sh_entsize = 1;
   7806     }
   7807   else if (strcmp (name, ".reginfo") == 0)
   7808     {
   7809       hdr->sh_type = SHT_MIPS_REGINFO;
   7810       /* In a shared object on IRIX 5.3, the .reginfo section has an
   7811 	 entsize of 0x18.  FIXME: Does this matter?  */
   7812       if (SGI_COMPAT (abfd))
   7813 	{
   7814 	  if ((abfd->flags & DYNAMIC) != 0)
   7815 	    hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
   7816 	  else
   7817 	    hdr->sh_entsize = 1;
   7818 	}
   7819       else
   7820 	hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
   7821     }
   7822   else if (SGI_COMPAT (abfd)
   7823 	   && (strcmp (name, ".hash") == 0
   7824 	       || strcmp (name, ".dynamic") == 0
   7825 	       || strcmp (name, ".dynstr") == 0))
   7826     {
   7827       if (SGI_COMPAT (abfd))
   7828 	hdr->sh_entsize = 0;
   7829 #if 0
   7830       /* This isn't how the IRIX6 linker behaves.  */
   7831       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
   7832 #endif
   7833     }
   7834   else if (strcmp (name, ".got") == 0
   7835 	   || strcmp (name, ".srdata") == 0
   7836 	   || strcmp (name, ".sdata") == 0
   7837 	   || strcmp (name, ".sbss") == 0
   7838 	   || strcmp (name, ".lit4") == 0
   7839 	   || strcmp (name, ".lit8") == 0)
   7840     hdr->sh_flags |= SHF_MIPS_GPREL;
   7841   else if (strcmp (name, ".MIPS.interfaces") == 0)
   7842     {
   7843       hdr->sh_type = SHT_MIPS_IFACE;
   7844       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7845     }
   7846   else if (startswith (name, ".MIPS.content"))
   7847     {
   7848       hdr->sh_type = SHT_MIPS_CONTENT;
   7849       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7850       /* The sh_info field is set in final_write_processing.  */
   7851     }
   7852   else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
   7853     {
   7854       hdr->sh_type = SHT_MIPS_OPTIONS;
   7855       hdr->sh_entsize = 1;
   7856       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7857     }
   7858   else if (startswith (name, ".MIPS.abiflags"))
   7859     {
   7860       hdr->sh_type = SHT_MIPS_ABIFLAGS;
   7861       hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
   7862     }
   7863   else if (startswith (name, ".debug_")
   7864 	   || startswith (name, ".gnu.debuglto_.debug_")
   7865 	   || startswith (name, ".zdebug_")
   7866 	   || startswith (name, ".gnu.debuglto_.zdebug_"))
   7867     {
   7868       hdr->sh_type = SHT_MIPS_DWARF;
   7869 
   7870       /* Irix facilities such as libexc expect a single .debug_frame
   7871 	 per executable, the system ones have NOSTRIP set and the linker
   7872 	 doesn't merge sections with different flags so ...  */
   7873       if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
   7874 	hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7875     }
   7876   else if (strcmp (name, ".MIPS.symlib") == 0)
   7877     {
   7878       hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
   7879       /* The sh_link and sh_info fields are set in
   7880 	 final_write_processing.  */
   7881     }
   7882   else if (startswith (name, ".MIPS.events")
   7883 	   || startswith (name, ".MIPS.post_rel"))
   7884     {
   7885       hdr->sh_type = SHT_MIPS_EVENTS;
   7886       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7887       /* The sh_link field is set in final_write_processing.  */
   7888     }
   7889   else if (strcmp (name, ".msym") == 0)
   7890     {
   7891       hdr->sh_type = SHT_MIPS_MSYM;
   7892       hdr->sh_flags |= SHF_ALLOC;
   7893       hdr->sh_entsize = 8;
   7894     }
   7895   else if (strcmp (name, ".MIPS.xhash") == 0)
   7896     {
   7897       hdr->sh_type = SHT_MIPS_XHASH;
   7898       hdr->sh_flags |= SHF_ALLOC;
   7899       hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
   7900     }
   7901 
   7902   /* The generic elf_fake_sections will set up REL_HDR using the default
   7903    kind of relocations.  We used to set up a second header for the
   7904    non-default kind of relocations here, but only NewABI would use
   7905    these, and the IRIX ld doesn't like resulting empty RELA sections.
   7906    Thus we create those header only on demand now.  */
   7907 
   7908   return true;
   7909 }
   7910 
   7911 /* Given a BFD section, try to locate the corresponding ELF section
   7912    index.  This is used by both the 32-bit and the 64-bit ABI.
   7913    Actually, it's not clear to me that the 64-bit ABI supports these,
   7914    but for non-PIC objects we will certainly want support for at least
   7915    the .scommon section.  */
   7916 
   7917 bool
   7918 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
   7919 					asection *sec, int *retval)
   7920 {
   7921   if (strcmp (bfd_section_name (sec), ".scommon") == 0)
   7922     {
   7923       *retval = SHN_MIPS_SCOMMON;
   7924       return true;
   7925     }
   7926   if (strcmp (bfd_section_name (sec), ".acommon") == 0)
   7927     {
   7928       *retval = SHN_MIPS_ACOMMON;
   7929       return true;
   7930     }
   7931   return false;
   7932 }
   7933 
   7934 /* Hook called by the linker routine which adds symbols from an object
   7936    file.  We must handle the special MIPS section numbers here.  */
   7937 
   7938 bool
   7939 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
   7940 			       Elf_Internal_Sym *sym, const char **namep,
   7941 			       flagword *flagsp ATTRIBUTE_UNUSED,
   7942 			       asection **secp, bfd_vma *valp)
   7943 {
   7944   if (SGI_COMPAT (abfd)
   7945       && (abfd->flags & DYNAMIC) != 0
   7946       && strcmp (*namep, "_rld_new_interface") == 0)
   7947     {
   7948       /* Skip IRIX5 rld entry name.  */
   7949       *namep = NULL;
   7950       return true;
   7951     }
   7952 
   7953   /* Shared objects may have a dynamic symbol '_gp_disp' defined as
   7954      a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
   7955      by setting a DT_NEEDED for the shared object.  Since _gp_disp is
   7956      a magic symbol resolved by the linker, we ignore this bogus definition
   7957      of _gp_disp.  New ABI objects do not suffer from this problem so this
   7958      is not done for them. */
   7959   if (!NEWABI_P(abfd)
   7960       && (sym->st_shndx == SHN_ABS)
   7961       && (strcmp (*namep, "_gp_disp") == 0))
   7962     {
   7963       *namep = NULL;
   7964       return true;
   7965     }
   7966 
   7967   switch (sym->st_shndx)
   7968     {
   7969     case SHN_COMMON:
   7970       /* Common symbols less than the GP size are automatically
   7971 	 treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
   7972       if (sym->st_size > elf_gp_size (abfd)
   7973 	  || ELF_ST_TYPE (sym->st_info) == STT_TLS
   7974 	  || IRIX_COMPAT (abfd) == ict_irix6
   7975 	  || strcmp (*namep, "__gnu_lto_slim") == 0)
   7976 	break;
   7977       /* Fall through.  */
   7978     case SHN_MIPS_SCOMMON:
   7979       *secp = bfd_make_section_old_way (abfd, ".scommon");
   7980       (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
   7981       *valp = sym->st_size;
   7982       break;
   7983 
   7984     case SHN_MIPS_TEXT:
   7985       /* This section is used in a shared object.  */
   7986       if (mips_elf_tdata (abfd)->elf_text_section == NULL)
   7987 	{
   7988 	  asymbol *elf_text_symbol;
   7989 	  asection *elf_text_section;
   7990 	  size_t amt = sizeof (asection);
   7991 
   7992 	  elf_text_section = bfd_zalloc (abfd, amt);
   7993 	  if (elf_text_section == NULL)
   7994 	    return false;
   7995 
   7996 	  amt = sizeof (asymbol);
   7997 	  elf_text_symbol = bfd_zalloc (abfd, amt);
   7998 	  if (elf_text_symbol == NULL)
   7999 	    return false;
   8000 
   8001 	  /* Initialize the section.  */
   8002 
   8003 	  mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
   8004 
   8005 	  elf_text_section->symbol = elf_text_symbol;
   8006 	  elf_text_section->name = ".text";
   8007 	  elf_text_section->flags = SEC_NO_FLAGS;
   8008 	  elf_text_section->output_section = NULL;
   8009 	  elf_text_section->owner = abfd;
   8010 	  elf_text_symbol->name = ".text";
   8011 	  elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
   8012 	  elf_text_symbol->section = elf_text_section;
   8013 	}
   8014       /* This code used to do *secp = bfd_und_section_ptr if
   8015 	 bfd_link_pic (info).  I don't know why, and that doesn't make sense,
   8016 	 so I took it out.  */
   8017       *secp = mips_elf_tdata (abfd)->elf_text_section;
   8018       break;
   8019 
   8020     case SHN_MIPS_ACOMMON:
   8021       /* Fall through. XXX Can we treat this as allocated data?  */
   8022     case SHN_MIPS_DATA:
   8023       /* This section is used in a shared object.  */
   8024       if (mips_elf_tdata (abfd)->elf_data_section == NULL)
   8025 	{
   8026 	  asymbol *elf_data_symbol;
   8027 	  asection *elf_data_section;
   8028 	  size_t amt = sizeof (asection);
   8029 
   8030 	  elf_data_section = bfd_zalloc (abfd, amt);
   8031 	  if (elf_data_section == NULL)
   8032 	    return false;
   8033 
   8034 	  amt = sizeof (asymbol);
   8035 	  elf_data_symbol = bfd_zalloc (abfd, amt);
   8036 	  if (elf_data_symbol == NULL)
   8037 	    return false;
   8038 
   8039 	  /* Initialize the section.  */
   8040 
   8041 	  mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
   8042 
   8043 	  elf_data_section->symbol = elf_data_symbol;
   8044 	  elf_data_section->name = ".data";
   8045 	  elf_data_section->flags = SEC_NO_FLAGS;
   8046 	  elf_data_section->output_section = NULL;
   8047 	  elf_data_section->owner = abfd;
   8048 	  elf_data_symbol->name = ".data";
   8049 	  elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
   8050 	  elf_data_symbol->section = elf_data_section;
   8051 	}
   8052       /* This code used to do *secp = bfd_und_section_ptr if
   8053 	 bfd_link_pic (info).  I don't know why, and that doesn't make sense,
   8054 	 so I took it out.  */
   8055       *secp = mips_elf_tdata (abfd)->elf_data_section;
   8056       break;
   8057 
   8058     case SHN_MIPS_SUNDEFINED:
   8059       *secp = bfd_und_section_ptr;
   8060       break;
   8061     }
   8062 
   8063   if (SGI_COMPAT (abfd)
   8064       && ! bfd_link_pic (info)
   8065       && info->output_bfd->xvec == abfd->xvec
   8066       && strcmp (*namep, "__rld_obj_head") == 0)
   8067     {
   8068       struct elf_link_hash_entry *h;
   8069       struct bfd_link_hash_entry *bh;
   8070 
   8071       /* Mark __rld_obj_head as dynamic.  */
   8072       bh = NULL;
   8073       if (! (_bfd_generic_link_add_one_symbol
   8074 	     (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
   8075 	      get_elf_backend_data (abfd)->collect, &bh)))
   8076 	return false;
   8077 
   8078       h = (struct elf_link_hash_entry *) bh;
   8079       h->non_elf = 0;
   8080       h->def_regular = 1;
   8081       h->type = STT_OBJECT;
   8082 
   8083       if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8084 	return false;
   8085 
   8086       mips_elf_hash_table (info)->use_rld_obj_head = true;
   8087       mips_elf_hash_table (info)->rld_symbol = h;
   8088     }
   8089 
   8090   /* If this is a mips16 text symbol, add 1 to the value to make it
   8091      odd.  This will cause something like .word SYM to come up with
   8092      the right value when it is loaded into the PC.  */
   8093   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   8094     ++*valp;
   8095 
   8096   return true;
   8097 }
   8098 
   8099 /* This hook function is called before the linker writes out a global
   8100    symbol.  We mark symbols as small common if appropriate.  This is
   8101    also where we undo the increment of the value for a mips16 symbol.  */
   8102 
   8103 int
   8104 _bfd_mips_elf_link_output_symbol_hook
   8105   (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   8106    const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
   8107    asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
   8108 {
   8109   /* If we see a common symbol, which implies a relocatable link, then
   8110      if a symbol was small common in an input file, mark it as small
   8111      common in the output file.  */
   8112   if (sym->st_shndx == SHN_COMMON
   8113       && strcmp (input_sec->name, ".scommon") == 0)
   8114     sym->st_shndx = SHN_MIPS_SCOMMON;
   8115 
   8116   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   8117     sym->st_value &= ~1;
   8118 
   8119   return 1;
   8120 }
   8121 
   8122 /* Functions for the dynamic linker.  */
   8124 
   8125 /* Create dynamic sections when linking against a dynamic object.  */
   8126 
   8127 bool
   8128 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   8129 {
   8130   struct elf_link_hash_entry *h;
   8131   struct bfd_link_hash_entry *bh;
   8132   flagword flags;
   8133   register asection *s;
   8134   const char * const *namep;
   8135   struct mips_elf_link_hash_table *htab;
   8136 
   8137   htab = mips_elf_hash_table (info);
   8138   BFD_ASSERT (htab != NULL);
   8139 
   8140   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   8141 	   | SEC_LINKER_CREATED | SEC_READONLY);
   8142 
   8143   /* The psABI requires a read-only .dynamic section, but the VxWorks
   8144      EABI doesn't.  */
   8145   if (htab->root.target_os != is_vxworks)
   8146     {
   8147       s = bfd_get_linker_section (abfd, ".dynamic");
   8148       if (s != NULL)
   8149 	{
   8150 	  if (!bfd_set_section_flags (s, flags))
   8151 	    return false;
   8152 	}
   8153     }
   8154 
   8155   /* We need to create .got section.  */
   8156   if (!mips_elf_create_got_section (abfd, info))
   8157     return false;
   8158 
   8159   if (! mips_elf_rel_dyn_section (info, true))
   8160     return false;
   8161 
   8162   /* Create .stub section.  */
   8163   s = bfd_make_section_anyway_with_flags (abfd,
   8164 					  MIPS_ELF_STUB_SECTION_NAME (abfd),
   8165 					  flags | SEC_CODE);
   8166   if (s == NULL
   8167       || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   8168     return false;
   8169   htab->sstubs = s;
   8170 
   8171   if (!mips_elf_hash_table (info)->use_rld_obj_head
   8172       && bfd_link_executable (info)
   8173       && bfd_get_linker_section (abfd, ".rld_map") == NULL)
   8174     {
   8175       s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
   8176 					      flags &~ (flagword) SEC_READONLY);
   8177       if (s == NULL
   8178 	  || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   8179 	return false;
   8180     }
   8181 
   8182   /* Create .MIPS.xhash section.  */
   8183   if (info->emit_gnu_hash)
   8184     s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
   8185 					    flags | SEC_READONLY);
   8186 
   8187   /* On IRIX5, we adjust add some additional symbols and change the
   8188      alignments of several sections.  There is no ABI documentation
   8189      indicating that this is necessary on IRIX6, nor any evidence that
   8190      the linker takes such action.  */
   8191   if (IRIX_COMPAT (abfd) == ict_irix5)
   8192     {
   8193       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
   8194 	{
   8195 	  bh = NULL;
   8196 	  if (! (_bfd_generic_link_add_one_symbol
   8197 		 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
   8198 		  NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
   8199 	    return false;
   8200 
   8201 	  h = (struct elf_link_hash_entry *) bh;
   8202 	  h->mark = 1;
   8203 	  h->non_elf = 0;
   8204 	  h->def_regular = 1;
   8205 	  h->type = STT_SECTION;
   8206 
   8207 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8208 	    return false;
   8209 	}
   8210 
   8211       /* We need to create a .compact_rel section.  */
   8212       if (SGI_COMPAT (abfd))
   8213 	{
   8214 	  if (!mips_elf_create_compact_rel_section (abfd, info))
   8215 	    return false;
   8216 	}
   8217 
   8218       /* Change alignments of some sections.  */
   8219       s = bfd_get_linker_section (abfd, ".hash");
   8220       if (s != NULL)
   8221 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   8222 
   8223       s = bfd_get_linker_section (abfd, ".dynsym");
   8224       if (s != NULL)
   8225 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   8226 
   8227       s = bfd_get_linker_section (abfd, ".dynstr");
   8228       if (s != NULL)
   8229 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   8230 
   8231       /* ??? */
   8232       s = bfd_get_section_by_name (abfd, ".reginfo");
   8233       if (s != NULL)
   8234 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   8235 
   8236       s = bfd_get_linker_section (abfd, ".dynamic");
   8237       if (s != NULL)
   8238 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   8239     }
   8240 
   8241   if (bfd_link_executable (info))
   8242     {
   8243       const char *name;
   8244 
   8245       name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
   8246       bh = NULL;
   8247       if (!(_bfd_generic_link_add_one_symbol
   8248 	    (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
   8249 	     NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
   8250 	return false;
   8251 
   8252       h = (struct elf_link_hash_entry *) bh;
   8253       h->non_elf = 0;
   8254       h->def_regular = 1;
   8255       h->type = STT_SECTION;
   8256 
   8257       if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8258 	return false;
   8259 
   8260       if (! mips_elf_hash_table (info)->use_rld_obj_head)
   8261 	{
   8262 	  /* __rld_map is a four byte word located in the .data section
   8263 	     and is filled in by the rtld to contain a pointer to
   8264 	     the _r_debug structure. Its symbol value will be set in
   8265 	     _bfd_mips_elf_finish_dynamic_symbol.  */
   8266 	  s = bfd_get_linker_section (abfd, ".rld_map");
   8267 	  BFD_ASSERT (s != NULL);
   8268 
   8269 	  name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
   8270 	  bh = NULL;
   8271 	  if (!(_bfd_generic_link_add_one_symbol
   8272 		(info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
   8273 		 get_elf_backend_data (abfd)->collect, &bh)))
   8274 	    return false;
   8275 
   8276 	  h = (struct elf_link_hash_entry *) bh;
   8277 	  h->non_elf = 0;
   8278 	  h->def_regular = 1;
   8279 	  h->type = STT_OBJECT;
   8280 
   8281 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8282 	    return false;
   8283 	  mips_elf_hash_table (info)->rld_symbol = h;
   8284 	}
   8285     }
   8286 
   8287   /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
   8288      Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol.  */
   8289   if (!_bfd_elf_create_dynamic_sections (abfd, info))
   8290     return false;
   8291 
   8292   /* Do the usual VxWorks handling.  */
   8293   if (htab->root.target_os == is_vxworks
   8294       && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
   8295     return false;
   8296 
   8297   return true;
   8298 }
   8299 
   8300 /* Return true if relocation REL against section SEC is a REL rather than
   8302    RELA relocation.  RELOCS is the first relocation in the section and
   8303    ABFD is the bfd that contains SEC.  */
   8304 
   8305 static bool
   8306 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
   8307 			   const Elf_Internal_Rela *relocs,
   8308 			   const Elf_Internal_Rela *rel)
   8309 {
   8310   Elf_Internal_Shdr *rel_hdr;
   8311   elf_backend_data *bed;
   8312 
   8313   /* To determine which flavor of relocation this is, we depend on the
   8314      fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
   8315   rel_hdr = elf_section_data (sec)->rel.hdr;
   8316   if (rel_hdr == NULL)
   8317     return false;
   8318   bed = get_elf_backend_data (abfd);
   8319   return ((size_t) (rel - relocs)
   8320 	  < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
   8321 }
   8322 
   8323 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
   8324    HOWTO is the relocation's howto and CONTENTS points to the contents
   8325    of the section that REL is against.  */
   8326 
   8327 static bfd_vma
   8328 mips_elf_read_rel_addend (bfd *abfd, asection *sec,
   8329 			  const Elf_Internal_Rela *rel,
   8330 			  reloc_howto_type *howto, bfd_byte *contents)
   8331 {
   8332   bfd_byte *location;
   8333   unsigned int r_type;
   8334   bfd_vma addend;
   8335   bfd_vma bytes;
   8336 
   8337   if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
   8338     return 0;
   8339 
   8340   r_type = ELF_R_TYPE (abfd, rel->r_info);
   8341   location = contents + rel->r_offset;
   8342 
   8343   /* Get the addend, which is stored in the input file.  */
   8344   _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
   8345   bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
   8346   _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
   8347 
   8348   addend = bytes & howto->src_mask;
   8349 
   8350   /* Shift is 2, unusually, for microMIPS JALX.  Adjust the addend
   8351      accordingly.  */
   8352   if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
   8353     addend <<= 1;
   8354 
   8355   return addend;
   8356 }
   8357 
   8358 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
   8359    and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
   8360    and update *ADDEND with the final addend.  Return true on success
   8361    or false if the LO16 could not be found.  RELEND is the exclusive
   8362    upper bound on the relocations for REL's section.  */
   8363 
   8364 static bool
   8365 mips_elf_add_lo16_rel_addend (bfd *abfd,
   8366 			      asection *sec,
   8367 			      const Elf_Internal_Rela *rel,
   8368 			      const Elf_Internal_Rela *relend,
   8369 			      bfd_byte *contents, bfd_vma *addend)
   8370 {
   8371   unsigned int r_type, lo16_type;
   8372   const Elf_Internal_Rela *lo16_relocation;
   8373   reloc_howto_type *lo16_howto;
   8374   bfd_vma l;
   8375 
   8376   r_type = ELF_R_TYPE (abfd, rel->r_info);
   8377   switch (r_type)
   8378     {
   8379     case R_MIPS_HI16:
   8380     case R_MIPS_GOT16:
   8381       lo16_type = R_MIPS_LO16;
   8382       break;
   8383     case R_MIPS_PCHI16:
   8384       lo16_type = R_MIPS_PCLO16;
   8385       break;
   8386     case R_MIPS_TLS_DTPREL_HI16:
   8387       lo16_type = R_MIPS_TLS_DTPREL_LO16;
   8388       break;
   8389     case R_MIPS_TLS_TPREL_HI16:
   8390       lo16_type = R_MIPS_TLS_TPREL_LO16;
   8391       break;
   8392     case R_MIPS16_HI16:
   8393     case R_MIPS16_GOT16:
   8394       lo16_type = R_MIPS16_LO16;
   8395       break;
   8396     case R_MIPS16_TLS_DTPREL_HI16:
   8397       lo16_type = R_MIPS16_TLS_DTPREL_LO16;
   8398       break;
   8399     case R_MIPS16_TLS_TPREL_HI16:
   8400       lo16_type = R_MIPS16_TLS_TPREL_LO16;
   8401       break;
   8402     case R_MICROMIPS_HI16:
   8403     case R_MICROMIPS_GOT16:
   8404       lo16_type = R_MICROMIPS_LO16;
   8405       break;
   8406     case R_MICROMIPS_TLS_DTPREL_HI16:
   8407       lo16_type = R_MICROMIPS_TLS_DTPREL_LO16;
   8408       break;
   8409     case R_MICROMIPS_TLS_TPREL_HI16:
   8410       lo16_type = R_MICROMIPS_TLS_TPREL_LO16;
   8411       break;
   8412     default:
   8413       abort ();
   8414     }
   8415 
   8416   /* The combined value is the sum of the HI16 addend, left-shifted by
   8417      sixteen bits, and the LO16 addend, sign extended.  (Usually, the
   8418      code does a `lui' of the HI16 value, and then an `addiu' of the
   8419      LO16 value.)  */
   8420   *addend <<= 16;
   8421 
   8422   /* Scan ahead to find a matching LO16 relocation.
   8423 
   8424      According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
   8425      be immediately following.  However, for the IRIX6 ABI, the next
   8426      relocation may be a composed relocation consisting of several
   8427      relocations for the same address.  In that case, the R_MIPS_LO16
   8428      relocation may occur as one of these.  We permit a similar
   8429      extension in general, as that is useful for GCC.
   8430 
   8431      In some cases GCC dead code elimination removes the LO16 but keeps
   8432      the corresponding HI16.  This is strictly speaking a violation of
   8433      the ABI but not immediately harmful.  */
   8434   lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
   8435   if (lo16_relocation == NULL)
   8436     return false;
   8437 
   8438   /* Obtain the addend kept there.  */
   8439   lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
   8440   l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
   8441 				contents);
   8442 
   8443   l <<= lo16_howto->rightshift;
   8444   /* For a PC-relative relocation the PCLO16 part of the addend
   8445      is relative to its PC and not ours, so we need to adjust it.  */
   8446   if (r_type == R_MIPS_PCHI16)
   8447     l = (l - (lo16_relocation->r_offset - rel->r_offset)) & 0xffff;
   8448   l = _bfd_mips_elf_sign_extend (l, 16);
   8449 
   8450   *addend += l;
   8451   return true;
   8452 }
   8453 
   8454 /* Try to read the contents of section SEC in bfd ABFD.  Return true and
   8455    store the contents in *CONTENTS on success.  Assume that *CONTENTS
   8456    already holds the contents if it is nonull on entry.  */
   8457 
   8458 static bool
   8459 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
   8460 {
   8461   if (*contents)
   8462     return true;
   8463 
   8464   /* Get cached copy if it exists.  */
   8465   if (elf_section_data (sec)->this_hdr.contents != NULL)
   8466     {
   8467       *contents = elf_section_data (sec)->this_hdr.contents;
   8468       return true;
   8469     }
   8470 
   8471   return bfd_malloc_and_get_section (abfd, sec, contents);
   8472 }
   8473 
   8474 /* Make a new PLT record to keep internal data.  */
   8475 
   8476 static struct plt_entry *
   8477 mips_elf_make_plt_record (bfd *abfd)
   8478 {
   8479   struct plt_entry *entry;
   8480 
   8481   entry = bfd_zalloc (abfd, sizeof (*entry));
   8482   if (entry == NULL)
   8483     return NULL;
   8484 
   8485   entry->stub_offset = MINUS_ONE;
   8486   entry->mips_offset = MINUS_ONE;
   8487   entry->comp_offset = MINUS_ONE;
   8488   entry->gotplt_index = MINUS_ONE;
   8489   return entry;
   8490 }
   8491 
   8492 /* Define the special `__gnu_absolute_zero' symbol.  We only need this
   8493    for PIC code, as otherwise there is no load-time relocation involved
   8494    and local GOT entries whose value is zero at static link time will
   8495    retain their value at load time.  */
   8496 
   8497 static bool
   8498 mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
   8499 			       struct mips_elf_link_hash_table *htab,
   8500 			       unsigned int r_type)
   8501 {
   8502   union
   8503     {
   8504       struct elf_link_hash_entry *eh;
   8505       struct bfd_link_hash_entry *bh;
   8506     }
   8507   hzero;
   8508 
   8509   BFD_ASSERT (!htab->use_absolute_zero);
   8510   BFD_ASSERT (bfd_link_pic (info));
   8511 
   8512   hzero.bh = NULL;
   8513   if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
   8514 					 BSF_GLOBAL, bfd_abs_section_ptr, 0,
   8515 					 NULL, false, false, &hzero.bh))
   8516     return false;
   8517 
   8518   BFD_ASSERT (hzero.bh != NULL);
   8519   hzero.eh->size = 0;
   8520   hzero.eh->type = STT_NOTYPE;
   8521   hzero.eh->other = STV_PROTECTED;
   8522   hzero.eh->def_regular = 1;
   8523   hzero.eh->non_elf = 0;
   8524 
   8525   if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
   8526     return false;
   8527 
   8528   htab->use_absolute_zero = true;
   8529 
   8530   return true;
   8531 }
   8532 
   8533 /* Look through the relocs for a section during the first phase, and
   8534    allocate space in the global offset table and record the need for
   8535    standard MIPS and compressed procedure linkage table entries.  */
   8536 
   8537 bool
   8538 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   8539 			    asection *sec, const Elf_Internal_Rela *relocs)
   8540 {
   8541   const char *name;
   8542   bfd *dynobj;
   8543   Elf_Internal_Shdr *symtab_hdr;
   8544   struct elf_link_hash_entry **sym_hashes;
   8545   size_t extsymoff;
   8546   const Elf_Internal_Rela *rel;
   8547   const Elf_Internal_Rela *rel_end;
   8548   asection *sreloc;
   8549   elf_backend_data *bed;
   8550   struct mips_elf_link_hash_table *htab;
   8551   bfd_byte *contents;
   8552   bfd_vma addend;
   8553   reloc_howto_type *howto;
   8554 
   8555   if (bfd_link_relocatable (info))
   8556     return true;
   8557 
   8558   htab = mips_elf_hash_table (info);
   8559   BFD_ASSERT (htab != NULL);
   8560 
   8561   dynobj = elf_hash_table (info)->dynobj;
   8562   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8563   sym_hashes = elf_sym_hashes (abfd);
   8564   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
   8565 
   8566   bed = get_elf_backend_data (abfd);
   8567   rel_end = relocs + sec->reloc_count;
   8568 
   8569   /* Check for the mips16 stub sections.  */
   8570 
   8571   name = bfd_section_name (sec);
   8572   if (FN_STUB_P (name))
   8573     {
   8574       unsigned long r_symndx;
   8575 
   8576       /* Look at the relocation information to figure out which symbol
   8577 	 this is for.  */
   8578 
   8579       r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
   8580       if (r_symndx == 0)
   8581 	{
   8582 	  _bfd_error_handler
   8583 	    /* xgettext:c-format */
   8584 	    (_("%pB: warning: cannot determine the target function for"
   8585 	       " stub section `%s'"),
   8586 	     abfd, name);
   8587 	  bfd_set_error (bfd_error_bad_value);
   8588 	  return false;
   8589 	}
   8590 
   8591       if (r_symndx < extsymoff
   8592 	  || sym_hashes[r_symndx - extsymoff] == NULL)
   8593 	{
   8594 	  asection *o;
   8595 
   8596 	  /* This stub is for a local symbol.  This stub will only be
   8597 	     needed if there is some relocation in this BFD, other
   8598 	     than a 16 bit function call, which refers to this symbol.  */
   8599 	  for (o = abfd->sections; o != NULL; o = o->next)
   8600 	    {
   8601 	      Elf_Internal_Rela *sec_relocs;
   8602 	      const Elf_Internal_Rela *r, *rend;
   8603 
   8604 	      /* We can ignore stub sections when looking for relocs.  */
   8605 	      if ((o->flags & SEC_RELOC) == 0
   8606 		  || o->reloc_count == 0
   8607 		  || section_allows_mips16_refs_p (o))
   8608 		continue;
   8609 
   8610 	      sec_relocs
   8611 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   8612 					     info->keep_memory);
   8613 	      if (sec_relocs == NULL)
   8614 		return false;
   8615 
   8616 	      rend = sec_relocs + o->reloc_count;
   8617 	      for (r = sec_relocs; r < rend; r++)
   8618 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
   8619 		    && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
   8620 		  break;
   8621 
   8622 	      if (elf_section_data (o)->relocs != sec_relocs)
   8623 		free (sec_relocs);
   8624 
   8625 	      if (r < rend)
   8626 		break;
   8627 	    }
   8628 
   8629 	  if (o == NULL)
   8630 	    {
   8631 	      /* There is no non-call reloc for this stub, so we do
   8632 		 not need it.  Since this function is called before
   8633 		 the linker maps input sections to output sections, we
   8634 		 can easily discard it by setting the SEC_EXCLUDE
   8635 		 flag.  */
   8636 	      sec->flags |= SEC_EXCLUDE;
   8637 	      return true;
   8638 	    }
   8639 
   8640 	  /* Record this stub in an array of local symbol stubs for
   8641 	     this BFD.  */
   8642 	  if (mips_elf_tdata (abfd)->local_stubs == NULL)
   8643 	    {
   8644 	      unsigned long symcount;
   8645 	      asection **n;
   8646 	      bfd_size_type amt;
   8647 
   8648 	      if (elf_bad_symtab (abfd))
   8649 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
   8650 	      else
   8651 		symcount = symtab_hdr->sh_info;
   8652 	      amt = symcount * sizeof (asection *);
   8653 	      n = bfd_zalloc (abfd, amt);
   8654 	      if (n == NULL)
   8655 		return false;
   8656 	      mips_elf_tdata (abfd)->local_stubs = n;
   8657 	    }
   8658 
   8659 	  sec->flags |= SEC_KEEP;
   8660 	  mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
   8661 
   8662 	  /* We don't need to set mips16_stubs_seen in this case.
   8663 	     That flag is used to see whether we need to look through
   8664 	     the global symbol table for stubs.  We don't need to set
   8665 	     it here, because we just have a local stub.  */
   8666 	}
   8667       else
   8668 	{
   8669 	  struct mips_elf_link_hash_entry *h;
   8670 
   8671 	  h = ((struct mips_elf_link_hash_entry *)
   8672 	       sym_hashes[r_symndx - extsymoff]);
   8673 
   8674 	  while (h->root.root.type == bfd_link_hash_indirect
   8675 		 || h->root.root.type == bfd_link_hash_warning)
   8676 	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   8677 
   8678 	  /* H is the symbol this stub is for.  */
   8679 
   8680 	  /* If we already have an appropriate stub for this function, we
   8681 	     don't need another one, so we can discard this one.  Since
   8682 	     this function is called before the linker maps input sections
   8683 	     to output sections, we can easily discard it by setting the
   8684 	     SEC_EXCLUDE flag.  */
   8685 	  if (h->fn_stub != NULL)
   8686 	    {
   8687 	      sec->flags |= SEC_EXCLUDE;
   8688 	      return true;
   8689 	    }
   8690 
   8691 	  sec->flags |= SEC_KEEP;
   8692 	  h->fn_stub = sec;
   8693 	  mips_elf_hash_table (info)->mips16_stubs_seen = true;
   8694 	}
   8695     }
   8696   else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
   8697     {
   8698       unsigned long r_symndx;
   8699       struct mips_elf_link_hash_entry *h;
   8700       asection **loc;
   8701 
   8702       /* Look at the relocation information to figure out which symbol
   8703 	 this is for.  */
   8704 
   8705       r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
   8706       if (r_symndx == 0)
   8707 	{
   8708 	  _bfd_error_handler
   8709 	    /* xgettext:c-format */
   8710 	    (_("%pB: warning: cannot determine the target function for"
   8711 	       " stub section `%s'"),
   8712 	     abfd, name);
   8713 	  bfd_set_error (bfd_error_bad_value);
   8714 	  return false;
   8715 	}
   8716 
   8717       if (r_symndx < extsymoff
   8718 	  || sym_hashes[r_symndx - extsymoff] == NULL)
   8719 	{
   8720 	  asection *o;
   8721 
   8722 	  /* This stub is for a local symbol.  This stub will only be
   8723 	     needed if there is some relocation (R_MIPS16_26) in this BFD
   8724 	     that refers to this symbol.  */
   8725 	  for (o = abfd->sections; o != NULL; o = o->next)
   8726 	    {
   8727 	      Elf_Internal_Rela *sec_relocs;
   8728 	      const Elf_Internal_Rela *r, *rend;
   8729 
   8730 	      /* We can ignore stub sections when looking for relocs.  */
   8731 	      if ((o->flags & SEC_RELOC) == 0
   8732 		  || o->reloc_count == 0
   8733 		  || section_allows_mips16_refs_p (o))
   8734 		continue;
   8735 
   8736 	      sec_relocs
   8737 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   8738 					     info->keep_memory);
   8739 	      if (sec_relocs == NULL)
   8740 		return false;
   8741 
   8742 	      rend = sec_relocs + o->reloc_count;
   8743 	      for (r = sec_relocs; r < rend; r++)
   8744 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
   8745 		    && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
   8746 		    break;
   8747 
   8748 	      if (elf_section_data (o)->relocs != sec_relocs)
   8749 		free (sec_relocs);
   8750 
   8751 	      if (r < rend)
   8752 		break;
   8753 	    }
   8754 
   8755 	  if (o == NULL)
   8756 	    {
   8757 	      /* There is no non-call reloc for this stub, so we do
   8758 		 not need it.  Since this function is called before
   8759 		 the linker maps input sections to output sections, we
   8760 		 can easily discard it by setting the SEC_EXCLUDE
   8761 		 flag.  */
   8762 	      sec->flags |= SEC_EXCLUDE;
   8763 	      return true;
   8764 	    }
   8765 
   8766 	  /* Record this stub in an array of local symbol call_stubs for
   8767 	     this BFD.  */
   8768 	  if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
   8769 	    {
   8770 	      unsigned long symcount;
   8771 	      asection **n;
   8772 	      bfd_size_type amt;
   8773 
   8774 	      if (elf_bad_symtab (abfd))
   8775 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
   8776 	      else
   8777 		symcount = symtab_hdr->sh_info;
   8778 	      amt = symcount * sizeof (asection *);
   8779 	      n = bfd_zalloc (abfd, amt);
   8780 	      if (n == NULL)
   8781 		return false;
   8782 	      mips_elf_tdata (abfd)->local_call_stubs = n;
   8783 	    }
   8784 
   8785 	  sec->flags |= SEC_KEEP;
   8786 	  mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
   8787 
   8788 	  /* We don't need to set mips16_stubs_seen in this case.
   8789 	     That flag is used to see whether we need to look through
   8790 	     the global symbol table for stubs.  We don't need to set
   8791 	     it here, because we just have a local stub.  */
   8792 	}
   8793       else
   8794 	{
   8795 	  h = ((struct mips_elf_link_hash_entry *)
   8796 	       sym_hashes[r_symndx - extsymoff]);
   8797 
   8798 	  /* H is the symbol this stub is for.  */
   8799 
   8800 	  if (CALL_FP_STUB_P (name))
   8801 	    loc = &h->call_fp_stub;
   8802 	  else
   8803 	    loc = &h->call_stub;
   8804 
   8805 	  /* If we already have an appropriate stub for this function, we
   8806 	     don't need another one, so we can discard this one.  Since
   8807 	     this function is called before the linker maps input sections
   8808 	     to output sections, we can easily discard it by setting the
   8809 	     SEC_EXCLUDE flag.  */
   8810 	  if (*loc != NULL)
   8811 	    {
   8812 	      sec->flags |= SEC_EXCLUDE;
   8813 	      return true;
   8814 	    }
   8815 
   8816 	  sec->flags |= SEC_KEEP;
   8817 	  *loc = sec;
   8818 	  mips_elf_hash_table (info)->mips16_stubs_seen = true;
   8819 	}
   8820     }
   8821 
   8822   sreloc = NULL;
   8823   contents = NULL;
   8824   for (rel = relocs; rel < rel_end; ++rel)
   8825     {
   8826       unsigned long r_symndx;
   8827       unsigned int r_type;
   8828       struct elf_link_hash_entry *h;
   8829       bool can_make_dynamic_p;
   8830       bool call_reloc_p;
   8831       bool constrain_symbol_p;
   8832 
   8833       r_symndx = ELF_R_SYM (abfd, rel->r_info);
   8834       r_type = ELF_R_TYPE (abfd, rel->r_info);
   8835 
   8836       if (r_symndx < extsymoff)
   8837 	h = NULL;
   8838       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
   8839 	{
   8840 	  _bfd_error_handler
   8841 	    /* xgettext:c-format */
   8842 	    (_("%pB: malformed reloc detected for section %s"),
   8843 	     abfd, name);
   8844 	  bfd_set_error (bfd_error_bad_value);
   8845 	  return false;
   8846 	}
   8847       else
   8848 	{
   8849 	  h = sym_hashes[r_symndx - extsymoff];
   8850 	  if (h != NULL)
   8851 	    {
   8852 	      while (h->root.type == bfd_link_hash_indirect
   8853 		     || h->root.type == bfd_link_hash_warning)
   8854 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8855 	    }
   8856 	}
   8857 
   8858       /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
   8859 	 relocation into a dynamic one.  */
   8860       can_make_dynamic_p = false;
   8861 
   8862       /* Set CALL_RELOC_P to true if the relocation is for a call,
   8863 	 and if pointer equality therefore doesn't matter.  */
   8864       call_reloc_p = false;
   8865 
   8866       /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
   8867 	 into account when deciding how to define the symbol.  */
   8868       constrain_symbol_p = true;
   8869 
   8870       switch (r_type)
   8871 	{
   8872 	case R_MIPS_CALL16:
   8873 	case R_MIPS_CALL_HI16:
   8874 	case R_MIPS_CALL_LO16:
   8875 	case R_MIPS16_CALL16:
   8876 	case R_MICROMIPS_CALL16:
   8877 	case R_MICROMIPS_CALL_HI16:
   8878 	case R_MICROMIPS_CALL_LO16:
   8879 	  call_reloc_p = true;
   8880 	  /* Fall through.  */
   8881 
   8882 	case R_MIPS_GOT16:
   8883 	case R_MIPS_GOT_LO16:
   8884 	case R_MIPS_GOT_PAGE:
   8885 	case R_MIPS_GOT_DISP:
   8886 	case R_MIPS16_GOT16:
   8887 	case R_MICROMIPS_GOT16:
   8888 	case R_MICROMIPS_GOT_LO16:
   8889 	case R_MICROMIPS_GOT_PAGE:
   8890 	case R_MICROMIPS_GOT_DISP:
   8891 	  /* If we have a symbol that will resolve to zero at static link
   8892 	     time and it is used by a GOT relocation applied to code we
   8893 	     cannot relax to an immediate zero load, then we will be using
   8894 	     the special `__gnu_absolute_zero' symbol whose value is zero
   8895 	     at dynamic load time.  We ignore HI16-type GOT relocations at
   8896 	     this stage, because their handling will depend entirely on
   8897 	     the corresponding LO16-type GOT relocation.  */
   8898 	  if (!call_hi16_reloc_p (r_type)
   8899 	      && h != NULL
   8900 	      && bfd_link_pic (info)
   8901 	      && !htab->use_absolute_zero
   8902 	      && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   8903 	    {
   8904 	      bool rel_reloc;
   8905 
   8906 	      if (!mips_elf_get_section_contents (abfd, sec, &contents))
   8907 		return false;
   8908 
   8909 	      rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
   8910 	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
   8911 	      if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
   8912 		if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
   8913 						false))
   8914 		  if (!mips_elf_define_absolute_zero (abfd, info, htab,
   8915 						      r_type))
   8916 		    return false;
   8917 	    }
   8918 
   8919 	  /* Fall through.  */
   8920 	case R_MIPS_GOT_HI16:
   8921 	case R_MIPS_GOT_OFST:
   8922 	case R_MIPS_TLS_GOTTPREL:
   8923 	case R_MIPS_TLS_GD:
   8924 	case R_MIPS_TLS_LDM:
   8925 	case R_MIPS16_TLS_GOTTPREL:
   8926 	case R_MIPS16_TLS_GD:
   8927 	case R_MIPS16_TLS_LDM:
   8928 	case R_MICROMIPS_GOT_HI16:
   8929 	case R_MICROMIPS_GOT_OFST:
   8930 	case R_MICROMIPS_TLS_GOTTPREL:
   8931 	case R_MICROMIPS_TLS_GD:
   8932 	case R_MICROMIPS_TLS_LDM:
   8933 	  if (dynobj == NULL)
   8934 	    elf_hash_table (info)->dynobj = dynobj = abfd;
   8935 	  if (!mips_elf_create_got_section (dynobj, info))
   8936 	    return false;
   8937 	  if (htab->root.target_os == is_vxworks
   8938 	      && !bfd_link_pic (info))
   8939 	    {
   8940 	      _bfd_error_handler
   8941 		/* xgettext:c-format */
   8942 		(_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
   8943 		 abfd, (uint64_t) rel->r_offset);
   8944 	      bfd_set_error (bfd_error_bad_value);
   8945 	      return false;
   8946 	    }
   8947 	  can_make_dynamic_p = true;
   8948 	  break;
   8949 
   8950 	case R_MIPS_NONE:
   8951 	case R_MIPS_JALR:
   8952 	case R_MICROMIPS_JALR:
   8953 	  /* These relocations have empty fields and are purely there to
   8954 	     provide link information.  The symbol value doesn't matter.  */
   8955 	  constrain_symbol_p = false;
   8956 	  break;
   8957 
   8958 	case R_MIPS_GPREL16:
   8959 	case R_MIPS_GPREL32:
   8960 	case R_MIPS16_GPREL:
   8961 	case R_MICROMIPS_GPREL16:
   8962 	  /* GP-relative relocations always resolve to a definition in a
   8963 	     regular input file, ignoring the one-definition rule.  This is
   8964 	     important for the GP setup sequence in NewABI code, which
   8965 	     always resolves to a local function even if other relocations
   8966 	     against the symbol wouldn't.  */
   8967 	  constrain_symbol_p = false;
   8968 	  break;
   8969 
   8970 	case R_MIPS_32:
   8971 	case R_MIPS_REL32:
   8972 	case R_MIPS_64:
   8973 	  /* In VxWorks executables, references to external symbols
   8974 	     must be handled using copy relocs or PLT entries; it is not
   8975 	     possible to convert this relocation into a dynamic one.
   8976 
   8977 	     For executables that use PLTs and copy-relocs, we have a
   8978 	     choice between converting the relocation into a dynamic
   8979 	     one or using copy relocations or PLT entries.  It is
   8980 	     usually better to do the former, unless the relocation is
   8981 	     against a read-only section.  */
   8982 	  if ((bfd_link_pic (info)
   8983 	       || (h != NULL
   8984 		   && htab->root.target_os != is_vxworks
   8985 		   && strcmp (h->root.root.string, "__gnu_local_gp") != 0
   8986 		   && !(!info->nocopyreloc
   8987 			&& !PIC_OBJECT_P (abfd)
   8988 			&& MIPS_ELF_READONLY_SECTION (sec))))
   8989 	      && (sec->flags & SEC_ALLOC) != 0)
   8990 	    {
   8991 	      can_make_dynamic_p = true;
   8992 	      if (dynobj == NULL)
   8993 		elf_hash_table (info)->dynobj = dynobj = abfd;
   8994 	    }
   8995 	  break;
   8996 
   8997 	case R_MIPS_26:
   8998 	case R_MIPS_PC16:
   8999 	case R_MIPS_PC21_S2:
   9000 	case R_MIPS_PC26_S2:
   9001 	case R_MIPS16_26:
   9002 	case R_MIPS16_PC16_S1:
   9003 	case R_MICROMIPS_26_S1:
   9004 	case R_MICROMIPS_PC7_S1:
   9005 	case R_MICROMIPS_PC10_S1:
   9006 	case R_MICROMIPS_PC16_S1:
   9007 	case R_MICROMIPS_PC23_S2:
   9008 	  call_reloc_p = true;
   9009 	  break;
   9010 	}
   9011 
   9012       if (h)
   9013 	{
   9014 	  if (constrain_symbol_p)
   9015 	    {
   9016 	      if (!can_make_dynamic_p)
   9017 		((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
   9018 
   9019 	      if (!call_reloc_p)
   9020 		h->pointer_equality_needed = 1;
   9021 
   9022 	      /* We must not create a stub for a symbol that has
   9023 		 relocations related to taking the function's address.
   9024 		 This doesn't apply to VxWorks, where CALL relocs refer
   9025 		 to a .got.plt entry instead of a normal .got entry.  */
   9026 	      if (htab->root.target_os != is_vxworks
   9027 		  && (!can_make_dynamic_p || !call_reloc_p))
   9028 		((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
   9029 	    }
   9030 
   9031 	  /* Relocations against the special VxWorks __GOTT_BASE__ and
   9032 	     __GOTT_INDEX__ symbols must be left to the loader.  Allocate
   9033 	     room for them in .rela.dyn.  */
   9034 	  if (is_gott_symbol (info, h))
   9035 	    {
   9036 	      if (sreloc == NULL)
   9037 		{
   9038 		  sreloc = mips_elf_rel_dyn_section (info, true);
   9039 		  if (sreloc == NULL)
   9040 		    return false;
   9041 		}
   9042 	      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   9043 	      if (MIPS_ELF_READONLY_SECTION (sec))
   9044 		/* We tell the dynamic linker that there are
   9045 		   relocations against the text segment.  */
   9046 		info->flags |= DF_TEXTREL;
   9047 	    }
   9048 	}
   9049       else if (call_lo16_reloc_p (r_type)
   9050 	       || got_lo16_reloc_p (r_type)
   9051 	       || got_disp_reloc_p (r_type)
   9052 	       || (got16_reloc_p (r_type)
   9053 		   && htab->root.target_os == is_vxworks))
   9054 	{
   9055 	  /* We may need a local GOT entry for this relocation.  We
   9056 	     don't count R_MIPS_GOT_PAGE because we can estimate the
   9057 	     maximum number of pages needed by looking at the size of
   9058 	     the segment.  Similar comments apply to R_MIPS*_GOT16 and
   9059 	     R_MIPS*_CALL16, except on VxWorks, where GOT relocations
   9060 	     always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
   9061 	     R_MIPS_CALL_HI16 because these are always followed by an
   9062 	     R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
   9063 	  if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
   9064 						 rel->r_addend, info, r_type))
   9065 	    return false;
   9066 	}
   9067 
   9068       if (h != NULL
   9069 	  && mips_elf_relocation_needs_la25_stub (abfd, r_type,
   9070 						  ELF_ST_IS_MIPS16 (h->other)))
   9071 	((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
   9072 
   9073       switch (r_type)
   9074 	{
   9075 	case R_MIPS_CALL16:
   9076 	case R_MIPS16_CALL16:
   9077 	case R_MICROMIPS_CALL16:
   9078 	  if (h == NULL)
   9079 	    {
   9080 	      _bfd_error_handler
   9081 		/* xgettext:c-format */
   9082 		(_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
   9083 		 abfd, (uint64_t) rel->r_offset);
   9084 	      bfd_set_error (bfd_error_bad_value);
   9085 	      return false;
   9086 	    }
   9087 	  /* Fall through.  */
   9088 
   9089 	case R_MIPS_CALL_HI16:
   9090 	case R_MIPS_CALL_LO16:
   9091 	case R_MICROMIPS_CALL_HI16:
   9092 	case R_MICROMIPS_CALL_LO16:
   9093 	  if (h != NULL)
   9094 	    {
   9095 	      /* Make sure there is room in the regular GOT to hold the
   9096 		 function's address.  We may eliminate it in favour of
   9097 		 a .got.plt entry later; see mips_elf_count_got_symbols.  */
   9098 	      if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
   9099 						      r_type))
   9100 		return false;
   9101 
   9102 	      /* We need a stub, not a plt entry for the undefined
   9103 		 function.  But we record it as if it needs plt.  See
   9104 		 _bfd_elf_adjust_dynamic_symbol.  */
   9105 	      h->needs_plt = 1;
   9106 	      h->type = STT_FUNC;
   9107 	    }
   9108 	  break;
   9109 
   9110 	case R_MIPS_GOT_PAGE:
   9111 	case R_MICROMIPS_GOT_PAGE:
   9112 	case R_MIPS16_GOT16:
   9113 	case R_MIPS_GOT16:
   9114 	case R_MIPS_GOT_HI16:
   9115 	case R_MIPS_GOT_LO16:
   9116 	case R_MICROMIPS_GOT16:
   9117 	case R_MICROMIPS_GOT_HI16:
   9118 	case R_MICROMIPS_GOT_LO16:
   9119 	  if (!h || got_page_reloc_p (r_type))
   9120 	    {
   9121 	      /* This relocation needs (or may need, if h != NULL) a
   9122 		 page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
   9123 		 know for sure until we know whether the symbol is
   9124 		 preemptible.  */
   9125 	      if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
   9126 		{
   9127 		  if (!mips_elf_get_section_contents (abfd, sec, &contents))
   9128 		    return false;
   9129 		  howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
   9130 		  addend = mips_elf_read_rel_addend (abfd, sec, rel,
   9131 						     howto, contents);
   9132 		  if (got16_reloc_p (r_type))
   9133 		    mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
   9134 						  contents, &addend);
   9135 		  else
   9136 		    addend <<= howto->rightshift;
   9137 		}
   9138 	      else
   9139 		addend = rel->r_addend;
   9140 	      if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
   9141 						 h, addend))
   9142 		return false;
   9143 
   9144 	      if (h)
   9145 		{
   9146 		  struct mips_elf_link_hash_entry *hmips =
   9147 		    (struct mips_elf_link_hash_entry *) h;
   9148 
   9149 		  /* This symbol is definitely not overridable.  */
   9150 		  if (hmips->root.def_regular
   9151 		      && ! (bfd_link_pic (info) && ! info->symbolic
   9152 			    && ! hmips->root.forced_local))
   9153 		    h = NULL;
   9154 		}
   9155 	    }
   9156 	  /* If this is a global, overridable symbol, GOT_PAGE will
   9157 	     decay to GOT_DISP, so we'll need a GOT entry for it.  */
   9158 	  /* Fall through.  */
   9159 
   9160 	case R_MIPS_GOT_DISP:
   9161 	case R_MICROMIPS_GOT_DISP:
   9162 	  if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
   9163 						       false, r_type))
   9164 	    return false;
   9165 	  break;
   9166 
   9167 	case R_MIPS_TLS_GOTTPREL:
   9168 	case R_MIPS16_TLS_GOTTPREL:
   9169 	case R_MICROMIPS_TLS_GOTTPREL:
   9170 	  if (bfd_link_pic (info))
   9171 	    info->flags |= DF_STATIC_TLS;
   9172 	  /* Fall through */
   9173 
   9174 	case R_MIPS_TLS_LDM:
   9175 	case R_MIPS16_TLS_LDM:
   9176 	case R_MICROMIPS_TLS_LDM:
   9177 	  if (tls_ldm_reloc_p (r_type))
   9178 	    {
   9179 	      r_symndx = STN_UNDEF;
   9180 	      h = NULL;
   9181 	    }
   9182 	  /* Fall through */
   9183 
   9184 	case R_MIPS_TLS_GD:
   9185 	case R_MIPS16_TLS_GD:
   9186 	case R_MICROMIPS_TLS_GD:
   9187 	  /* This symbol requires a global offset table entry, or two
   9188 	     for TLS GD relocations.  */
   9189 	  if (h != NULL)
   9190 	    {
   9191 	      if (!mips_elf_record_global_got_symbol (h, abfd, info,
   9192 						      false, r_type))
   9193 		return false;
   9194 	    }
   9195 	  else
   9196 	    {
   9197 	      if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
   9198 						     rel->r_addend,
   9199 						     info, r_type))
   9200 		return false;
   9201 	    }
   9202 	  break;
   9203 
   9204 	case R_MIPS_32:
   9205 	case R_MIPS_REL32:
   9206 	case R_MIPS_64:
   9207 	  /* In VxWorks executables, references to external symbols
   9208 	     are handled using copy relocs or PLT stubs, so there's
   9209 	     no need to add a .rela.dyn entry for this relocation.  */
   9210 	  if (can_make_dynamic_p)
   9211 	    {
   9212 	      if (sreloc == NULL)
   9213 		{
   9214 		  sreloc = mips_elf_rel_dyn_section (info, true);
   9215 		  if (sreloc == NULL)
   9216 		    return false;
   9217 		}
   9218 	      if (bfd_link_pic (info) && h == NULL)
   9219 		{
   9220 		  /* When creating a shared object, we must copy these
   9221 		     reloc types into the output file as R_MIPS_REL32
   9222 		     relocs.  Make room for this reloc in .rel(a).dyn.  */
   9223 		  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   9224 		  /* In the N32 and 64-bit ABIs there may be multiple
   9225 		     consecutive relocations for the same offset.  If we have
   9226 		     a R_MIPS_GPREL32 followed by a R_MIPS_64 then that
   9227 		     relocation is complete and needs no futher adjustment.
   9228 
   9229 		     Silently ignore absolute relocations in the .eh_frame
   9230 		     section, they will be dropped latter.
   9231 		   */
   9232 		  if ((rel == relocs
   9233 		      || rel[-1].r_offset != rel->r_offset
   9234 		      || r_type != R_MIPS_64
   9235 		      || ELF_R_TYPE(abfd, rel[-1].r_info) != R_MIPS_GPREL32)
   9236 		      && MIPS_ELF_READONLY_SECTION (sec)
   9237 		      && !((r_type == R_MIPS_32 || r_type == R_MIPS_64)
   9238 		           && strcmp(sec->name, ".eh_frame") == 0))
   9239 		    {
   9240 		      /* We tell the dynamic linker that there are
   9241 		         relocations against the text segment.  */
   9242 		      info->flags |= DF_TEXTREL;
   9243 		      info->callbacks->warning
   9244 			(info,
   9245 			 _("relocation emitted against readonly section"),
   9246 			 NULL, abfd, sec, rel->r_offset);
   9247 		    }
   9248 		}
   9249 	      else
   9250 		{
   9251 		  struct mips_elf_link_hash_entry *hmips;
   9252 
   9253 		  /* For a shared object, we must copy this relocation
   9254 		     unless the symbol turns out to be undefined and
   9255 		     weak with non-default visibility, in which case
   9256 		     it will be left as zero.
   9257 
   9258 		     We could elide R_MIPS_REL32 for locally binding symbols
   9259 		     in shared libraries, but do not yet do so.
   9260 
   9261 		     For an executable, we only need to copy this
   9262 		     reloc if the symbol is defined in a dynamic
   9263 		     object.  */
   9264 		  hmips = (struct mips_elf_link_hash_entry *) h;
   9265 		  ++hmips->possibly_dynamic_relocs;
   9266 		  if (MIPS_ELF_READONLY_SECTION (sec))
   9267 		    /* We need it to tell the dynamic linker if there
   9268 		       are relocations against the text segment.  */
   9269 		    hmips->readonly_reloc = true;
   9270 		}
   9271 	    }
   9272 
   9273 	  if (SGI_COMPAT (abfd))
   9274 	    mips_elf_hash_table (info)->compact_rel_size +=
   9275 	      sizeof (Elf32_External_crinfo);
   9276 	  break;
   9277 
   9278 	case R_MIPS_26:
   9279 	case R_MIPS_GPREL16:
   9280 	case R_MIPS_LITERAL:
   9281 	case R_MIPS_GPREL32:
   9282 	case R_MICROMIPS_26_S1:
   9283 	case R_MICROMIPS_GPREL16:
   9284 	case R_MICROMIPS_LITERAL:
   9285 	case R_MICROMIPS_GPREL7_S2:
   9286 	  if (SGI_COMPAT (abfd))
   9287 	    mips_elf_hash_table (info)->compact_rel_size +=
   9288 	      sizeof (Elf32_External_crinfo);
   9289 	  break;
   9290 
   9291 	  /* This relocation describes the C++ object vtable hierarchy.
   9292 	     Reconstruct it for later use during GC.  */
   9293 	case R_MIPS_GNU_VTINHERIT:
   9294 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   9295 	    return false;
   9296 	  break;
   9297 
   9298 	  /* This relocation describes which C++ vtable entries are actually
   9299 	     used.  Record for later use during GC.  */
   9300 	case R_MIPS_GNU_VTENTRY:
   9301 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
   9302 	    return false;
   9303 	  break;
   9304 
   9305 	default:
   9306 	  break;
   9307 	}
   9308 
   9309       /* Record the need for a PLT entry.  At this point we don't know
   9310 	 yet if we are going to create a PLT in the first place, but
   9311 	 we only record whether the relocation requires a standard MIPS
   9312 	 or a compressed code entry anyway.  If we don't make a PLT after
   9313 	 all, then we'll just ignore these arrangements.  Likewise if
   9314 	 a PLT entry is not created because the symbol is satisfied
   9315 	 locally.  */
   9316       if (h != NULL
   9317 	  && (branch_reloc_p (r_type)
   9318 	      || mips16_branch_reloc_p (r_type)
   9319 	      || micromips_branch_reloc_p (r_type))
   9320 	  && !SYMBOL_CALLS_LOCAL (info, h))
   9321 	{
   9322 	  if (h->plt.plist == NULL)
   9323 	    h->plt.plist = mips_elf_make_plt_record (abfd);
   9324 	  if (h->plt.plist == NULL)
   9325 	    return false;
   9326 
   9327 	  if (branch_reloc_p (r_type))
   9328 	    h->plt.plist->need_mips = true;
   9329 	  else
   9330 	    h->plt.plist->need_comp = true;
   9331 	}
   9332 
   9333       /* See if this reloc would need to refer to a MIPS16 hard-float stub,
   9334 	 if there is one.  We only need to handle global symbols here;
   9335 	 we decide whether to keep or delete stubs for local symbols
   9336 	 when processing the stub's relocations.  */
   9337       if (h != NULL
   9338 	  && !mips16_call_reloc_p (r_type)
   9339 	  && !section_allows_mips16_refs_p (sec))
   9340 	{
   9341 	  struct mips_elf_link_hash_entry *mh;
   9342 
   9343 	  mh = (struct mips_elf_link_hash_entry *) h;
   9344 	  mh->need_fn_stub = true;
   9345 	}
   9346 
   9347       /* Refuse some position-dependent relocations when creating a
   9348 	 shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
   9349 	 not PIC, but we can create dynamic relocations and the result
   9350 	 will be fine.  Also do not refuse R_MIPS_LO16, which can be
   9351 	 combined with R_MIPS_GOT16.  */
   9352       if (bfd_link_pic (info))
   9353 	{
   9354 	  switch (r_type)
   9355 	    {
   9356 	    case R_MIPS_TLS_TPREL_HI16:
   9357 	    case R_MIPS16_TLS_TPREL_HI16:
   9358 	    case R_MICROMIPS_TLS_TPREL_HI16:
   9359 	    case R_MIPS_TLS_TPREL_LO16:
   9360 	    case R_MIPS16_TLS_TPREL_LO16:
   9361 	    case R_MICROMIPS_TLS_TPREL_LO16:
   9362 	      /* These are okay in PIE, but not in a shared library.  */
   9363 	      if (bfd_link_executable (info))
   9364 		break;
   9365 
   9366 	      /* FALLTHROUGH */
   9367 
   9368 	    case R_MIPS16_HI16:
   9369 	    case R_MIPS_HI16:
   9370 	    case R_MIPS_HIGHER:
   9371 	    case R_MIPS_HIGHEST:
   9372 	    case R_MICROMIPS_HI16:
   9373 	    case R_MICROMIPS_HIGHER:
   9374 	    case R_MICROMIPS_HIGHEST:
   9375 	      /* Don't refuse a high part relocation if it's against
   9376 		 no symbol (e.g. part of a compound relocation).  */
   9377 	      if (r_symndx == STN_UNDEF)
   9378 		break;
   9379 
   9380 	      /* Likewise an absolute symbol.  */
   9381 	      if (h != NULL && bfd_is_abs_symbol (&h->root))
   9382 		break;
   9383 
   9384 	      /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
   9385 		 and has a special meaning.  */
   9386 	      if (!NEWABI_P (abfd) && h != NULL
   9387 		  && strcmp (h->root.root.string, "_gp_disp") == 0)
   9388 		break;
   9389 
   9390 	      /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
   9391 	      if (is_gott_symbol (info, h))
   9392 		break;
   9393 
   9394 	      /* FALLTHROUGH */
   9395 
   9396 	    case R_MIPS16_26:
   9397 	    case R_MIPS_26:
   9398 	    case R_MICROMIPS_26_S1:
   9399 	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
   9400 	      /* An error for unsupported relocations is raised as part
   9401 		 of the above search, so we can skip the following.  */
   9402 	      if (howto != NULL)
   9403 		info->callbacks->einfo
   9404 		  /* xgettext:c-format */
   9405 		  (_("%X%H: relocation %s against `%s' cannot be used"
   9406 		     " when making a shared object; recompile with -fPIC\n"),
   9407 		   abfd, sec, rel->r_offset, howto->name,
   9408 		   (h) ? h->root.root.string : "a local symbol");
   9409 	      break;
   9410 	    default:
   9411 	      break;
   9412 	    }
   9413 	}
   9414     }
   9415 
   9416   return true;
   9417 }
   9418 
   9419 /* Allocate space for global sym dynamic relocs.  */
   9421 
   9422 static bool
   9423 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   9424 {
   9425   struct bfd_link_info *info = inf;
   9426   bfd *dynobj;
   9427   struct mips_elf_link_hash_entry *hmips;
   9428   struct mips_elf_link_hash_table *htab;
   9429 
   9430   htab = mips_elf_hash_table (info);
   9431   BFD_ASSERT (htab != NULL);
   9432 
   9433   dynobj = elf_hash_table (info)->dynobj;
   9434   hmips = (struct mips_elf_link_hash_entry *) h;
   9435 
   9436   /* VxWorks executables are handled elsewhere; we only need to
   9437      allocate relocations in shared objects.  */
   9438   if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
   9439     return true;
   9440 
   9441   /* Ignore indirect symbols.  All relocations against such symbols
   9442      will be redirected to the target symbol.  */
   9443   if (h->root.type == bfd_link_hash_indirect)
   9444     return true;
   9445 
   9446   /* If this symbol is defined in a dynamic object, or we are creating
   9447      a shared library, we will need to copy any R_MIPS_32 or
   9448      R_MIPS_REL32 relocs against it into the output file.  */
   9449   if (! bfd_link_relocatable (info)
   9450       && hmips->possibly_dynamic_relocs != 0
   9451       && (h->root.type == bfd_link_hash_defweak
   9452 	  || (!h->def_regular && !ELF_COMMON_DEF_P (h))
   9453 	  || bfd_link_pic (info)))
   9454     {
   9455       bool do_copy = true;
   9456 
   9457       if (h->root.type == bfd_link_hash_undefweak)
   9458 	{
   9459 	  /* Do not copy relocations for undefined weak symbols that
   9460 	     we are not going to export.  */
   9461 	  if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   9462 	    do_copy = false;
   9463 
   9464 	  /* Make sure undefined weak symbols are output as a dynamic
   9465 	     symbol in PIEs.  */
   9466 	  else if (h->dynindx == -1 && !h->forced_local)
   9467 	    {
   9468 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   9469 		return false;
   9470 	    }
   9471 	}
   9472 
   9473       if (do_copy)
   9474 	{
   9475 	  /* Even though we don't directly need a GOT entry for this symbol,
   9476 	     the SVR4 psABI requires it to have a dynamic symbol table
   9477 	     index greater that DT_MIPS_GOTSYM if there are dynamic
   9478 	     relocations against it.
   9479 
   9480 	     VxWorks does not enforce the same mapping between the GOT
   9481 	     and the symbol table, so the same requirement does not
   9482 	     apply there.  */
   9483 	  if (htab->root.target_os != is_vxworks)
   9484 	    {
   9485 	      if (hmips->global_got_area > GGA_RELOC_ONLY)
   9486 		hmips->global_got_area = GGA_RELOC_ONLY;
   9487 	      hmips->got_only_for_calls = false;
   9488 	    }
   9489 
   9490 	  mips_elf_allocate_dynamic_relocations
   9491 	    (dynobj, info, hmips->possibly_dynamic_relocs);
   9492 	  if (hmips->readonly_reloc)
   9493 	    /* We tell the dynamic linker that there are relocations
   9494 	       against the text segment.  */
   9495 	    info->flags |= DF_TEXTREL;
   9496 	}
   9497     }
   9498 
   9499   return true;
   9500 }
   9501 
   9502 /* Adjust a symbol defined by a dynamic object and referenced by a
   9503    regular object.  The current definition is in some section of the
   9504    dynamic object, but we're not including those sections.  We have to
   9505    change the definition to something the rest of the link can
   9506    understand.  */
   9507 
   9508 bool
   9509 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   9510 				     struct elf_link_hash_entry *h)
   9511 {
   9512   bfd *dynobj;
   9513   struct mips_elf_link_hash_entry *hmips;
   9514   struct mips_elf_link_hash_table *htab;
   9515   asection *s, *srel;
   9516 
   9517   htab = mips_elf_hash_table (info);
   9518   BFD_ASSERT (htab != NULL);
   9519 
   9520   dynobj = elf_hash_table (info)->dynobj;
   9521   hmips = (struct mips_elf_link_hash_entry *) h;
   9522 
   9523   /* Make sure we know what is going on here.  */
   9524   if (dynobj == NULL
   9525       || (! h->needs_plt
   9526 	  && ! h->is_weakalias
   9527 	  && (! h->def_dynamic
   9528 	      || ! h->ref_regular
   9529 	      || h->def_regular)))
   9530     {
   9531       if (h->type == STT_GNU_IFUNC)
   9532 	_bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
   9533 			    h->root.root.string);
   9534       else
   9535 	_bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
   9536 			    h->root.root.string);
   9537       return true;
   9538     }
   9539 
   9540   hmips = (struct mips_elf_link_hash_entry *) h;
   9541 
   9542   /* If there are call relocations against an externally-defined symbol,
   9543      see whether we can create a MIPS lazy-binding stub for it.  We can
   9544      only do this if all references to the function are through call
   9545      relocations, and in that case, the traditional lazy-binding stubs
   9546      are much more efficient than PLT entries.
   9547 
   9548      Traditional stubs are only available on SVR4 psABI-based systems;
   9549      VxWorks always uses PLTs instead.  */
   9550   if (htab->root.target_os != is_vxworks
   9551       && h->needs_plt
   9552       && !hmips->no_fn_stub)
   9553     {
   9554       if (! elf_hash_table (info)->dynamic_sections_created)
   9555 	return true;
   9556 
   9557       /* If this symbol is not defined in a regular file, then set
   9558 	 the symbol to the stub location.  This is required to make
   9559 	 function pointers compare as equal between the normal
   9560 	 executable and the shared library.  */
   9561       if (!h->def_regular
   9562 	  && !bfd_is_abs_section (htab->sstubs->output_section))
   9563 	{
   9564 	  hmips->needs_lazy_stub = true;
   9565 	  htab->lazy_stub_count++;
   9566 	  return true;
   9567 	}
   9568     }
   9569   /* As above, VxWorks requires PLT entries for externally-defined
   9570      functions that are only accessed through call relocations.
   9571 
   9572      Both VxWorks and non-VxWorks targets also need PLT entries if there
   9573      are static-only relocations against an externally-defined function.
   9574      This can technically occur for shared libraries if there are
   9575      branches to the symbol, although it is unlikely that this will be
   9576      used in practice due to the short ranges involved.  It can occur
   9577      for any relative or absolute relocation in executables; in that
   9578      case, the PLT entry becomes the function's canonical address.  */
   9579   else if (((h->needs_plt && !hmips->no_fn_stub)
   9580 	    || (h->type == STT_FUNC && hmips->has_static_relocs))
   9581 	   && htab->use_plts_and_copy_relocs
   9582 	   && !SYMBOL_CALLS_LOCAL (info, h)
   9583 	   && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   9584 		&& h->root.type == bfd_link_hash_undefweak))
   9585     {
   9586       bool micromips_p = MICROMIPS_P (info->output_bfd);
   9587       bool newabi_p = NEWABI_P (info->output_bfd);
   9588 
   9589       /* If this is the first symbol to need a PLT entry, then make some
   9590 	 basic setup.  Also work out PLT entry sizes.  We'll need them
   9591 	 for PLT offset calculations.  */
   9592       if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
   9593 	{
   9594 	  BFD_ASSERT (htab->root.sgotplt->size == 0);
   9595 	  BFD_ASSERT (htab->plt_got_index == 0);
   9596 
   9597 	  /* If we're using the PLT additions to the psABI, each PLT
   9598 	     entry is 16 bytes and the PLT0 entry is 32 bytes.
   9599 	     Encourage better cache usage by aligning.  We do this
   9600 	     lazily to avoid pessimizing traditional objects.  */
   9601 	  if (htab->root.target_os != is_vxworks
   9602 	      && !bfd_link_align_section (htab->root.splt, 5))
   9603 	    return false;
   9604 
   9605 	  /* Make sure that .got.plt is word-aligned.  We do this lazily
   9606 	     for the same reason as above.  */
   9607 	  if (!bfd_link_align_section (htab->root.sgotplt,
   9608 				       MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
   9609 	    return false;
   9610 
   9611 	  /* On non-VxWorks targets, the first two entries in .got.plt
   9612 	     are reserved.  */
   9613 	  if (htab->root.target_os != is_vxworks)
   9614 	    htab->plt_got_index
   9615 	      += (get_elf_backend_data (dynobj)->got_header_size
   9616 		  / MIPS_ELF_GOT_SIZE (dynobj));
   9617 
   9618 	  /* On VxWorks, also allocate room for the header's
   9619 	     .rela.plt.unloaded entries.  */
   9620 	  if (htab->root.target_os == is_vxworks
   9621 	      && !bfd_link_pic (info))
   9622 	    htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
   9623 
   9624 	  /* Now work out the sizes of individual PLT entries.  */
   9625 	  if (htab->root.target_os == is_vxworks
   9626 	      && bfd_link_pic (info))
   9627 	    htab->plt_mips_entry_size
   9628 	      = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
   9629 	  else if (htab->root.target_os == is_vxworks)
   9630 	    htab->plt_mips_entry_size
   9631 	      = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
   9632 	  else if (newabi_p)
   9633 	    htab->plt_mips_entry_size
   9634 	      = 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9635 	  else if (!micromips_p)
   9636 	    {
   9637 	      htab->plt_mips_entry_size
   9638 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9639 	      htab->plt_comp_entry_size
   9640 		= 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
   9641 	    }
   9642 	  else if (htab->insn32)
   9643 	    {
   9644 	      htab->plt_mips_entry_size
   9645 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9646 	      htab->plt_comp_entry_size
   9647 		= 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
   9648 	    }
   9649 	  else
   9650 	    {
   9651 	      htab->plt_mips_entry_size
   9652 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9653 	      htab->plt_comp_entry_size
   9654 		= 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
   9655 	    }
   9656 	}
   9657 
   9658       if (h->plt.plist == NULL)
   9659 	h->plt.plist = mips_elf_make_plt_record (dynobj);
   9660       if (h->plt.plist == NULL)
   9661 	return false;
   9662 
   9663       /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
   9664 	 n32 or n64, so always use a standard entry there.
   9665 
   9666 	 If the symbol has a MIPS16 call stub and gets a PLT entry, then
   9667 	 all MIPS16 calls will go via that stub, and there is no benefit
   9668 	 to having a MIPS16 entry.  And in the case of call_stub a
   9669 	 standard entry actually has to be used as the stub ends with a J
   9670 	 instruction.  */
   9671       if (newabi_p
   9672 	  || htab->root.target_os == is_vxworks
   9673 	  || hmips->call_stub
   9674 	  || hmips->call_fp_stub)
   9675 	{
   9676 	  h->plt.plist->need_mips = true;
   9677 	  h->plt.plist->need_comp = false;
   9678 	}
   9679 
   9680       /* Otherwise, if there are no direct calls to the function, we
   9681 	 have a free choice of whether to use standard or compressed
   9682 	 entries.  Prefer microMIPS entries if the object is known to
   9683 	 contain microMIPS code, so that it becomes possible to create
   9684 	 pure microMIPS binaries.  Prefer standard entries otherwise,
   9685 	 because MIPS16 ones are no smaller and are usually slower.  */
   9686       if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
   9687 	{
   9688 	  if (micromips_p)
   9689 	    h->plt.plist->need_comp = true;
   9690 	  else
   9691 	    h->plt.plist->need_mips = true;
   9692 	}
   9693 
   9694       if (h->plt.plist->need_mips)
   9695 	{
   9696 	  h->plt.plist->mips_offset = htab->plt_mips_offset;
   9697 	  htab->plt_mips_offset += htab->plt_mips_entry_size;
   9698 	}
   9699       if (h->plt.plist->need_comp)
   9700 	{
   9701 	  h->plt.plist->comp_offset = htab->plt_comp_offset;
   9702 	  htab->plt_comp_offset += htab->plt_comp_entry_size;
   9703 	}
   9704 
   9705       /* Reserve the corresponding .got.plt entry now too.  */
   9706       h->plt.plist->gotplt_index = htab->plt_got_index++;
   9707 
   9708       /* If the output file has no definition of the symbol, set the
   9709 	 symbol's value to the address of the stub.  */
   9710       if (!bfd_link_pic (info) && !h->def_regular)
   9711 	hmips->use_plt_entry = true;
   9712 
   9713       /* Make room for the R_MIPS_JUMP_SLOT relocation.  */
   9714       htab->root.srelplt->size += (htab->root.target_os == is_vxworks
   9715 				   ? MIPS_ELF_RELA_SIZE (dynobj)
   9716 				   : MIPS_ELF_REL_SIZE (dynobj));
   9717 
   9718       /* Make room for the .rela.plt.unloaded relocations.  */
   9719       if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
   9720 	htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
   9721 
   9722       /* All relocations against this symbol that could have been made
   9723 	 dynamic will now refer to the PLT entry instead.  */
   9724       hmips->possibly_dynamic_relocs = 0;
   9725 
   9726       return true;
   9727     }
   9728 
   9729   /* If this is a weak symbol, and there is a real definition, the
   9730      processor independent code will have arranged for us to see the
   9731      real definition first, and we can just use the same value.  */
   9732   if (h->is_weakalias)
   9733     {
   9734       struct elf_link_hash_entry *def = weakdef (h);
   9735       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   9736       h->root.u.def.section = def->root.u.def.section;
   9737       h->root.u.def.value = def->root.u.def.value;
   9738       return true;
   9739     }
   9740 
   9741   /* Otherwise, there is nothing further to do for symbols defined
   9742      in regular objects.  */
   9743   if (h->def_regular)
   9744     return true;
   9745 
   9746   /* There's also nothing more to do if we'll convert all relocations
   9747      against this symbol into dynamic relocations.  */
   9748   if (!hmips->has_static_relocs)
   9749     return true;
   9750 
   9751   /* We're now relying on copy relocations.  Complain if we have
   9752      some that we can't convert.  */
   9753   if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
   9754     {
   9755       _bfd_error_handler (_("non-dynamic relocations refer to "
   9756 			    "dynamic symbol %s"),
   9757 			  h->root.root.string);
   9758       bfd_set_error (bfd_error_bad_value);
   9759       return false;
   9760     }
   9761 
   9762   /* We must allocate the symbol in our .dynbss section, which will
   9763      become part of the .bss section of the executable.  There will be
   9764      an entry for this symbol in the .dynsym section.  The dynamic
   9765      object will contain position independent code, so all references
   9766      from the dynamic object to this symbol will go through the global
   9767      offset table.  The dynamic linker will use the .dynsym entry to
   9768      determine the address it must put in the global offset table, so
   9769      both the dynamic object and the regular object will refer to the
   9770      same memory location for the variable.  */
   9771 
   9772   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
   9773     {
   9774       s = htab->root.sdynrelro;
   9775       srel = htab->root.sreldynrelro;
   9776     }
   9777   else
   9778     {
   9779       s = htab->root.sdynbss;
   9780       srel = htab->root.srelbss;
   9781     }
   9782   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
   9783     {
   9784       if (htab->root.target_os == is_vxworks)
   9785 	srel->size += sizeof (Elf32_External_Rela);
   9786       else
   9787 	mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   9788       h->needs_copy = 1;
   9789     }
   9790 
   9791   /* All relocations against this symbol that could have been made
   9792      dynamic will now refer to the local copy instead.  */
   9793   hmips->possibly_dynamic_relocs = 0;
   9794 
   9795   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   9796 }
   9797 
   9798 /* If the link uses a GOT, lay it out and work out its size.  */
   9800 
   9801 static bool
   9802 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
   9803 {
   9804   bfd *dynobj;
   9805   asection *s;
   9806   struct mips_got_info *g;
   9807   bfd_size_type loadable_size = 0;
   9808   bfd_size_type page_gotno;
   9809   bfd *ibfd;
   9810   struct mips_elf_traverse_got_arg tga;
   9811   struct mips_elf_link_hash_table *htab;
   9812 
   9813   htab = mips_elf_hash_table (info);
   9814   BFD_ASSERT (htab != NULL);
   9815 
   9816   s = htab->root.sgot;
   9817   if (s == NULL)
   9818     return true;
   9819 
   9820   dynobj = elf_hash_table (info)->dynobj;
   9821   g = htab->got_info;
   9822 
   9823   /* Allocate room for the reserved entries.  VxWorks always reserves
   9824      3 entries; other objects only reserve 2 entries.  */
   9825   BFD_ASSERT (g->assigned_low_gotno == 0);
   9826   if (htab->root.target_os == is_vxworks)
   9827     htab->reserved_gotno = 3;
   9828   else
   9829     htab->reserved_gotno = 2;
   9830   g->local_gotno += htab->reserved_gotno;
   9831   g->assigned_low_gotno = htab->reserved_gotno;
   9832 
   9833   /* Decide which symbols need to go in the global part of the GOT and
   9834      count the number of reloc-only GOT symbols.  */
   9835   mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
   9836 
   9837   if (!mips_elf_resolve_final_got_entries (info, g))
   9838     return false;
   9839 
   9840   /* Calculate the total loadable size of the output.  That
   9841      will give us the maximum number of GOT_PAGE entries
   9842      required.  */
   9843   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   9844     {
   9845       asection *subsection;
   9846 
   9847       for (subsection = ibfd->sections;
   9848 	   subsection;
   9849 	   subsection = subsection->next)
   9850 	{
   9851 	  if ((subsection->flags & SEC_ALLOC) == 0)
   9852 	    continue;
   9853 	  loadable_size += ((subsection->size + 0xf)
   9854 			    &~ (bfd_size_type) 0xf);
   9855 	}
   9856     }
   9857 
   9858   if (htab->root.target_os == is_vxworks)
   9859     /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
   9860        relocations against local symbols evaluate to "G", and the EABI does
   9861        not include R_MIPS_GOT_PAGE.  */
   9862     page_gotno = 0;
   9863   else
   9864     /* Assume there are two loadable segments consisting of contiguous
   9865        sections.  Is 5 enough?  */
   9866     page_gotno = (loadable_size >> 16) + 5;
   9867 
   9868   /* Choose the smaller of the two page estimates; both are intended to be
   9869      conservative.  */
   9870   if (page_gotno > g->page_gotno)
   9871     page_gotno = g->page_gotno;
   9872 
   9873   g->local_gotno += page_gotno;
   9874   g->assigned_high_gotno = g->local_gotno - 1;
   9875 
   9876   s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9877   s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9878   s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9879 
   9880   /* VxWorks does not support multiple GOTs.  It initializes $gp to
   9881      __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
   9882      dynamic loader.  */
   9883   if (htab->root.target_os != is_vxworks
   9884       && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
   9885     {
   9886       if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
   9887 	return false;
   9888     }
   9889   else
   9890     {
   9891       /* Record that all bfds use G.  This also has the effect of freeing
   9892 	 the per-bfd GOTs, which we no longer need.  */
   9893       for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   9894 	if (mips_elf_bfd_got (ibfd, false))
   9895 	  mips_elf_replace_bfd_got (ibfd, g);
   9896       mips_elf_replace_bfd_got (output_bfd, g);
   9897 
   9898       /* Set up TLS entries.  */
   9899       g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
   9900       tga.info = info;
   9901       tga.g = g;
   9902       tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
   9903       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
   9904       if (!tga.g)
   9905 	return false;
   9906       BFD_ASSERT (g->tls_assigned_gotno
   9907 		  == g->global_gotno + g->local_gotno + g->tls_gotno);
   9908 
   9909       /* Each VxWorks GOT entry needs an explicit relocation.  */
   9910       if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
   9911 	g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
   9912 
   9913       /* Allocate room for the TLS relocations.  */
   9914       if (g->relocs)
   9915 	mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
   9916     }
   9917 
   9918   return true;
   9919 }
   9920 
   9921 /* Estimate the size of the .MIPS.stubs section.  */
   9922 
   9923 static void
   9924 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
   9925 {
   9926   struct mips_elf_link_hash_table *htab;
   9927   bfd_size_type dynsymcount;
   9928 
   9929   htab = mips_elf_hash_table (info);
   9930   BFD_ASSERT (htab != NULL);
   9931 
   9932   if (htab->lazy_stub_count == 0)
   9933     return;
   9934 
   9935   /* IRIX rld assumes that a function stub isn't at the end of the .text
   9936      section, so add a dummy entry to the end.  */
   9937   htab->lazy_stub_count++;
   9938 
   9939   /* Get a worst-case estimate of the number of dynamic symbols needed.
   9940      At this point, dynsymcount does not account for section symbols
   9941      and count_section_dynsyms may overestimate the number that will
   9942      be needed.  */
   9943   dynsymcount = (elf_hash_table (info)->dynsymcount
   9944 		 + count_section_dynsyms (output_bfd, info));
   9945 
   9946   /* Determine the size of one stub entry.  There's no disadvantage
   9947      from using microMIPS code here, so for the sake of pure-microMIPS
   9948      binaries we prefer it whenever there's any microMIPS code in
   9949      output produced at all.  This has a benefit of stubs being
   9950      shorter by 4 bytes each too, unless in the insn32 mode.  */
   9951   if (!MICROMIPS_P (output_bfd))
   9952     htab->function_stub_size = (dynsymcount > 0x10000
   9953 				? MIPS_FUNCTION_STUB_BIG_SIZE
   9954 				: MIPS_FUNCTION_STUB_NORMAL_SIZE);
   9955   else if (htab->insn32)
   9956     htab->function_stub_size = (dynsymcount > 0x10000
   9957 				? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
   9958 				: MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
   9959   else
   9960     htab->function_stub_size = (dynsymcount > 0x10000
   9961 				? MICROMIPS_FUNCTION_STUB_BIG_SIZE
   9962 				: MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
   9963 
   9964   htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
   9965 }
   9966 
   9967 /* A mips_elf_link_hash_traverse callback for which DATA points to a
   9968    mips_htab_traverse_info.  If H needs a traditional MIPS lazy-binding
   9969    stub, allocate an entry in the stubs section.  */
   9970 
   9971 static bool
   9972 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
   9973 {
   9974   struct mips_htab_traverse_info *hti = data;
   9975   struct mips_elf_link_hash_table *htab;
   9976   struct bfd_link_info *info;
   9977   bfd *output_bfd;
   9978 
   9979   info = hti->info;
   9980   output_bfd = hti->output_bfd;
   9981   htab = mips_elf_hash_table (info);
   9982   BFD_ASSERT (htab != NULL);
   9983 
   9984   if (h->needs_lazy_stub)
   9985     {
   9986       bool micromips_p = MICROMIPS_P (output_bfd);
   9987       unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   9988       bfd_vma isa_bit = micromips_p;
   9989 
   9990       BFD_ASSERT (htab->root.dynobj != NULL);
   9991       if (h->root.plt.plist == NULL)
   9992 	h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
   9993       if (h->root.plt.plist == NULL)
   9994 	{
   9995 	  hti->error = true;
   9996 	  return false;
   9997 	}
   9998       h->root.root.u.def.section = htab->sstubs;
   9999       h->root.root.u.def.value = htab->sstubs->size + isa_bit;
   10000       h->root.plt.plist->stub_offset = htab->sstubs->size;
   10001       h->root.other = other;
   10002       htab->sstubs->size += htab->function_stub_size;
   10003     }
   10004   return true;
   10005 }
   10006 
   10007 /* Allocate offsets in the stubs section to each symbol that needs one.
   10008    Set the final size of the .MIPS.stub section.  */
   10009 
   10010 static bool
   10011 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
   10012 {
   10013   bfd *output_bfd = info->output_bfd;
   10014   bool micromips_p = MICROMIPS_P (output_bfd);
   10015   unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   10016   bfd_vma isa_bit = micromips_p;
   10017   struct mips_elf_link_hash_table *htab;
   10018   struct mips_htab_traverse_info hti;
   10019   struct elf_link_hash_entry *h;
   10020   bfd *dynobj;
   10021 
   10022   htab = mips_elf_hash_table (info);
   10023   BFD_ASSERT (htab != NULL);
   10024 
   10025   if (htab->lazy_stub_count == 0)
   10026     return true;
   10027 
   10028   htab->sstubs->size = 0;
   10029   hti.info = info;
   10030   hti.output_bfd = output_bfd;
   10031   hti.error = false;
   10032   mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
   10033   if (hti.error)
   10034     return false;
   10035   htab->sstubs->size += htab->function_stub_size;
   10036   BFD_ASSERT (htab->sstubs->size
   10037 	      == htab->lazy_stub_count * htab->function_stub_size);
   10038 
   10039   dynobj = elf_hash_table (info)->dynobj;
   10040   BFD_ASSERT (dynobj != NULL);
   10041   h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
   10042   if (h == NULL)
   10043     return false;
   10044   h->root.u.def.value = isa_bit;
   10045   h->other = other;
   10046   h->type = STT_FUNC;
   10047 
   10048   return true;
   10049 }
   10050 
   10051 /* A mips_elf_link_hash_traverse callback for which DATA points to a
   10052    bfd_link_info.  If H uses the address of a PLT entry as the value
   10053    of the symbol, then set the entry in the symbol table now.  Prefer
   10054    a standard MIPS PLT entry.  */
   10055 
   10056 static bool
   10057 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
   10058 {
   10059   struct bfd_link_info *info = data;
   10060   bool micromips_p = MICROMIPS_P (info->output_bfd);
   10061   struct mips_elf_link_hash_table *htab;
   10062   unsigned int other;
   10063   bfd_vma isa_bit;
   10064   bfd_vma val;
   10065 
   10066   htab = mips_elf_hash_table (info);
   10067   BFD_ASSERT (htab != NULL);
   10068 
   10069   if (h->use_plt_entry)
   10070     {
   10071       BFD_ASSERT (h->root.plt.plist != NULL);
   10072       BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
   10073 		  || h->root.plt.plist->comp_offset != MINUS_ONE);
   10074 
   10075       val = htab->plt_header_size;
   10076       if (h->root.plt.plist->mips_offset != MINUS_ONE)
   10077 	{
   10078 	  isa_bit = 0;
   10079 	  val += h->root.plt.plist->mips_offset;
   10080 	  other = 0;
   10081 	}
   10082       else
   10083 	{
   10084 	  isa_bit = 1;
   10085 	  val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
   10086 	  other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
   10087 	}
   10088       val += isa_bit;
   10089       /* For VxWorks, point at the PLT load stub rather than the lazy
   10090 	 resolution stub; this stub will become the canonical function
   10091 	 address.  */
   10092       if (htab->root.target_os == is_vxworks)
   10093 	val += 8;
   10094 
   10095       h->root.root.u.def.section = htab->root.splt;
   10096       h->root.root.u.def.value = val;
   10097       h->root.other = other;
   10098     }
   10099 
   10100   return true;
   10101 }
   10102 
   10103 /* Set the sizes of the dynamic sections, some mips non-dynamic sections,
   10104    and check for any mips16 stub sections that we can discard.  */
   10105 
   10106 bool
   10107 _bfd_mips_elf_late_size_sections (bfd *output_bfd,
   10108 				  struct bfd_link_info *info)
   10109 {
   10110   bfd *dynobj;
   10111   asection *s, *sreldyn;
   10112   bool reltext;
   10113   struct mips_elf_link_hash_table *htab;
   10114   struct mips_htab_traverse_info hti;
   10115 
   10116   htab = mips_elf_hash_table (info);
   10117   BFD_ASSERT (htab != NULL);
   10118 
   10119   /* The .reginfo section has a fixed size.  */
   10120   s = bfd_get_section_by_name (output_bfd, ".reginfo");
   10121   if (s != NULL)
   10122     {
   10123       bfd_set_section_size (s, sizeof (Elf32_External_RegInfo));
   10124       s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
   10125     }
   10126 
   10127   /* The .MIPS.abiflags section has a fixed size.  */
   10128   s = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
   10129   if (s != NULL)
   10130     {
   10131       bfd_set_section_size (s, sizeof (Elf_External_ABIFlags_v0));
   10132       s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
   10133     }
   10134 
   10135   hti.info = info;
   10136   hti.output_bfd = output_bfd;
   10137   hti.error = false;
   10138   mips_elf_link_hash_traverse (htab, mips_elf_check_symbols, &hti);
   10139   if (hti.error)
   10140     return false;
   10141 
   10142   dynobj = htab->root.dynobj;
   10143   if (dynobj == NULL)
   10144     return true;
   10145 
   10146   if (htab->root.dynamic_sections_created)
   10147     {
   10148       /* Set the contents of the .interp section to the interpreter.  */
   10149       if (bfd_link_executable (info) && !info->nointerp)
   10150 	{
   10151 	  s = htab->root.interp;
   10152 	  BFD_ASSERT (s != NULL);
   10153 	  s->size
   10154 	    = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
   10155 	  s->contents
   10156 	    = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
   10157 	  s->alloced = 1;
   10158 	}
   10159 
   10160       /* Figure out the size of the PLT header if we know that we
   10161 	 are using it.  For the sake of cache alignment always use
   10162 	 a standard header whenever any standard entries are present
   10163 	 even if microMIPS entries are present as well.  This also
   10164 	 lets the microMIPS header rely on the value of $v0 only set
   10165 	 by microMIPS entries, for a small size reduction.
   10166 
   10167 	 Set symbol table entry values for symbols that use the
   10168 	 address of their PLT entry now that we can calculate it.
   10169 
   10170 	 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
   10171 	 haven't already in _bfd_elf_create_dynamic_sections.  */
   10172       if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
   10173 	{
   10174 	  bool micromips_p = (MICROMIPS_P (output_bfd)
   10175 				     && !htab->plt_mips_offset);
   10176 	  unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   10177 	  bfd_vma isa_bit = micromips_p;
   10178 	  struct elf_link_hash_entry *h;
   10179 	  bfd_vma size;
   10180 
   10181 	  BFD_ASSERT (htab->use_plts_and_copy_relocs);
   10182 	  BFD_ASSERT (htab->root.sgotplt->size == 0);
   10183 	  BFD_ASSERT (htab->root.splt->size == 0);
   10184 
   10185 	  if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
   10186 	    size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
   10187 	  else if (htab->root.target_os == is_vxworks)
   10188 	    size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
   10189 	  else if (ABI_64_P (output_bfd))
   10190 	    size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
   10191 	  else if (ABI_N32_P (output_bfd))
   10192 	    size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
   10193 	  else if (!micromips_p)
   10194 	    size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
   10195 	  else if (htab->insn32)
   10196 	    size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
   10197 	  else
   10198 	    size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
   10199 
   10200 	  htab->plt_header_is_comp = micromips_p;
   10201 	  htab->plt_header_size = size;
   10202 	  htab->root.splt->size = (size
   10203 				   + htab->plt_mips_offset
   10204 				   + htab->plt_comp_offset);
   10205 	  htab->root.sgotplt->size = (htab->plt_got_index
   10206 				      * MIPS_ELF_GOT_SIZE (dynobj));
   10207 
   10208 	  mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
   10209 
   10210 	  if (htab->root.hplt == NULL)
   10211 	    {
   10212 	      h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
   10213 					       "_PROCEDURE_LINKAGE_TABLE_");
   10214 	      htab->root.hplt = h;
   10215 	      if (h == NULL)
   10216 		return false;
   10217 	    }
   10218 
   10219 	  h = htab->root.hplt;
   10220 	  h->root.u.def.value = isa_bit;
   10221 	  h->other = other;
   10222 	  h->type = STT_FUNC;
   10223 	}
   10224     }
   10225 
   10226   /* Allocate space for global sym dynamic relocs.  */
   10227   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
   10228 
   10229   mips_elf_estimate_stub_size (output_bfd, info);
   10230 
   10231   if (!mips_elf_lay_out_got (output_bfd, info))
   10232     return false;
   10233 
   10234   mips_elf_lay_out_lazy_stubs (info);
   10235 
   10236   /* The check_relocs and adjust_dynamic_symbol entry points have
   10237      determined the sizes of the various dynamic sections.  Allocate
   10238      memory for them.  */
   10239   reltext = false;
   10240   for (s = dynobj->sections; s != NULL; s = s->next)
   10241     {
   10242       const char *name;
   10243 
   10244       /* It's OK to base decisions on the section name, because none
   10245 	 of the dynobj section names depend upon the input files.  */
   10246       name = bfd_section_name (s);
   10247 
   10248       if ((s->flags & SEC_LINKER_CREATED) == 0)
   10249 	continue;
   10250 
   10251       if (startswith (name, ".rel"))
   10252 	{
   10253 	  if (s->size != 0)
   10254 	    {
   10255 	      const char *outname;
   10256 	      asection *target;
   10257 
   10258 	      /* If this relocation section applies to a read only
   10259 		 section, then we probably need a DT_TEXTREL entry.
   10260 		 If the relocation section is .rel(a).dyn, we always
   10261 		 assert a DT_TEXTREL entry rather than testing whether
   10262 		 there exists a relocation to a read only section or
   10263 		 not.  */
   10264 	      outname = bfd_section_name (s->output_section);
   10265 	      target = bfd_get_section_by_name (output_bfd, outname + 4);
   10266 	      if ((target != NULL
   10267 		   && (target->flags & SEC_READONLY) != 0
   10268 		   && (target->flags & SEC_ALLOC) != 0)
   10269 		  || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
   10270 		reltext = true;
   10271 
   10272 	      /* We use the reloc_count field as a counter if we need
   10273 		 to copy relocs into the output file.  */
   10274 	      if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
   10275 		s->reloc_count = 0;
   10276 
   10277 	      /* If combreloc is enabled, elf_link_sort_relocs() will
   10278 		 sort relocations, but in a different way than we do,
   10279 		 and before we're done creating relocations.  Also, it
   10280 		 will move them around between input sections'
   10281 		 relocation's contents, so our sorting would be
   10282 		 broken, so don't let it run.  */
   10283 	      info->combreloc = 0;
   10284 	    }
   10285 	}
   10286       else if (bfd_link_executable (info)
   10287 	       && !htab->use_rld_obj_head
   10288 	       && startswith (name, ".rld_map"))
   10289 	{
   10290 	  /* We add a room for __rld_map.  It will be filled in by the
   10291 	     rtld to contain a pointer to the _r_debug structure.  */
   10292 	  s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
   10293 	}
   10294       else if (SGI_COMPAT (output_bfd)
   10295 	       && startswith (name, ".compact_rel"))
   10296 	s->size += htab->compact_rel_size;
   10297       else if (s == htab->root.splt)
   10298 	{
   10299 	  /* If the last PLT entry has a branch delay slot, allocate
   10300 	     room for an extra nop to fill the delay slot.  This is
   10301 	     for CPUs without load interlocking.  */
   10302 	  if (! LOAD_INTERLOCKS_P (output_bfd)
   10303 	      && htab->root.target_os != is_vxworks
   10304 	      && s->size > 0)
   10305 	    s->size += 4;
   10306 	}
   10307       else if (! startswith (name, ".init")
   10308 	       && s != htab->root.sgot
   10309 	       && s != htab->root.sgotplt
   10310 	       && s != htab->sstubs
   10311 	       && s != htab->root.sdynbss
   10312 	       && s != htab->root.sdynrelro)
   10313 	{
   10314 	  /* It's not one of our sections, so don't allocate space.  */
   10315 	  continue;
   10316 	}
   10317 
   10318       if (s->size == 0)
   10319 	{
   10320 	  s->flags |= SEC_EXCLUDE;
   10321 	  continue;
   10322 	}
   10323 
   10324       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   10325 	continue;
   10326 
   10327       /* Allocate memory for the section contents.  */
   10328       s->contents = bfd_zalloc (dynobj, s->size);
   10329       if (s->contents == NULL)
   10330 	{
   10331 	  bfd_set_error (bfd_error_no_memory);
   10332 	  return false;
   10333 	}
   10334       s->alloced = 1;
   10335     }
   10336 
   10337   if (htab->root.dynamic_sections_created)
   10338     {
   10339       /* Add some entries to the .dynamic section.  We fill in the
   10340 	 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
   10341 	 must add the entries now so that we get the correct size for
   10342 	 the .dynamic section.  */
   10343 
   10344       /* SGI object has the equivalence of DT_DEBUG in the
   10345 	 DT_MIPS_RLD_MAP entry.  This must come first because glibc
   10346 	 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
   10347 	 may only look at the first one they see.  */
   10348       if (!bfd_link_pic (info)
   10349 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
   10350 	return false;
   10351 
   10352       if (bfd_link_executable (info)
   10353 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
   10354 	return false;
   10355 
   10356       /* The DT_DEBUG entry may be filled in by the dynamic linker and
   10357 	 used by the debugger.  */
   10358       if (bfd_link_executable (info)
   10359 	  && !SGI_COMPAT (output_bfd)
   10360 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
   10361 	return false;
   10362 
   10363       if (reltext
   10364 	  && (SGI_COMPAT (output_bfd)
   10365 	      || htab->root.target_os == is_vxworks))
   10366 	info->flags |= DF_TEXTREL;
   10367 
   10368       if ((info->flags & DF_TEXTREL) != 0)
   10369 	{
   10370 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
   10371 	    return false;
   10372 
   10373 	  /* Clear the DF_TEXTREL flag.  It will be set again if we
   10374 	     write out an actual text relocation; we may not, because
   10375 	     at this point we do not know whether e.g. any .eh_frame
   10376 	     absolute relocations have been converted to PC-relative.  */
   10377 	  info->flags &= ~DF_TEXTREL;
   10378 	}
   10379 
   10380       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
   10381 	return false;
   10382 
   10383       sreldyn = mips_elf_rel_dyn_section (info, false);
   10384       if (htab->root.target_os == is_vxworks)
   10385 	{
   10386 	  /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
   10387 	     use any of the DT_MIPS_* tags.  */
   10388 	  if (sreldyn && sreldyn->size > 0)
   10389 	    {
   10390 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
   10391 		return false;
   10392 
   10393 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
   10394 		return false;
   10395 
   10396 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
   10397 		return false;
   10398 	    }
   10399 	}
   10400       else
   10401 	{
   10402 	  if (sreldyn && sreldyn->size > 0
   10403 	      && !bfd_is_abs_section (sreldyn->output_section))
   10404 	    {
   10405 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
   10406 		return false;
   10407 
   10408 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
   10409 		return false;
   10410 
   10411 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
   10412 		return false;
   10413 	    }
   10414 
   10415 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
   10416 	    return false;
   10417 
   10418 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
   10419 	    return false;
   10420 
   10421 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
   10422 	    return false;
   10423 
   10424 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
   10425 	    return false;
   10426 
   10427 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
   10428 	    return false;
   10429 
   10430 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
   10431 	    return false;
   10432 
   10433 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
   10434 	    return false;
   10435 
   10436 	  if (info->emit_gnu_hash
   10437 	      && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
   10438 	    return false;
   10439 
   10440 	  if (IRIX_COMPAT (dynobj) == ict_irix5
   10441 	      && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
   10442 	    return false;
   10443 
   10444 	  if (IRIX_COMPAT (dynobj) == ict_irix6
   10445 	      && (bfd_get_section_by_name
   10446 		  (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
   10447 	      && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
   10448 	    return false;
   10449 	}
   10450       if (htab->root.splt->size > 0)
   10451 	{
   10452 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
   10453 	    return false;
   10454 
   10455 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
   10456 	    return false;
   10457 
   10458 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
   10459 	    return false;
   10460 
   10461 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
   10462 	    return false;
   10463 	}
   10464       if (htab->root.target_os == is_vxworks
   10465 	  && !elf_vxworks_add_dynamic_entries (output_bfd, info))
   10466 	return false;
   10467     }
   10468 
   10469   return true;
   10470 }
   10471 
   10472 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
   10474    Adjust its R_ADDEND field so that it is correct for the output file.
   10475    LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
   10476    and sections respectively; both use symbol indexes.  */
   10477 
   10478 static void
   10479 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
   10480 			bfd *input_bfd, Elf_Internal_Sym *local_syms,
   10481 			asection **local_sections, Elf_Internal_Rela *rel)
   10482 {
   10483   unsigned int r_type, r_symndx;
   10484   Elf_Internal_Sym *sym;
   10485   asection *sec;
   10486 
   10487   if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
   10488     {
   10489       r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   10490       if (gprel16_reloc_p (r_type)
   10491 	  || r_type == R_MIPS_GPREL32
   10492 	  || literal_reloc_p (r_type))
   10493 	{
   10494 	  rel->r_addend += _bfd_get_gp_value (input_bfd);
   10495 	  rel->r_addend -= _bfd_get_gp_value (output_bfd);
   10496 	}
   10497 
   10498       r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
   10499       sym = local_syms + r_symndx;
   10500 
   10501       /* Adjust REL's addend to account for section merging.  */
   10502       if (!bfd_link_relocatable (info))
   10503 	{
   10504 	  sec = local_sections[r_symndx];
   10505 	  _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   10506 	}
   10507 
   10508       /* This would normally be done by the rela_normal code in elflink.c.  */
   10509       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   10510 	rel->r_addend += local_sections[r_symndx]->output_offset;
   10511     }
   10512 }
   10513 
   10514 /* Handle relocations against symbols from removed linkonce sections,
   10515    or sections discarded by a linker script.  We use this wrapper around
   10516    RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
   10517    on 64-bit ELF targets.  In this case for any relocation handled, which
   10518    always be the first in a triplet, the remaining two have to be processed
   10519    together with the first, even if they are R_MIPS_NONE.  It is the symbol
   10520    index referred by the first reloc that applies to all the three and the
   10521    remaining two never refer to an object symbol.  And it is the final
   10522    relocation (the last non-null one) that determines the output field of
   10523    the whole relocation so retrieve the corresponding howto structure for
   10524    the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
   10525 
   10526    Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
   10527    and therefore requires to be pasted in a loop.  It also defines a block
   10528    and does not protect any of its arguments, hence the extra brackets.  */
   10529 
   10530 static void
   10531 mips_reloc_against_discarded_section (bfd *output_bfd,
   10532 				      struct bfd_link_info *info,
   10533 				      bfd *input_bfd, asection *input_section,
   10534 				      Elf_Internal_Rela **rel,
   10535 				      const Elf_Internal_Rela **relend,
   10536 				      bool rel_reloc,
   10537 				      reloc_howto_type *howto,
   10538 				      bfd_byte *contents)
   10539 {
   10540   elf_backend_data *bed = get_elf_backend_data (output_bfd);
   10541   int count = bed->s->int_rels_per_ext_rel;
   10542   unsigned int r_type;
   10543   int i;
   10544 
   10545   for (i = count - 1; i > 0; i--)
   10546     {
   10547       r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
   10548       if (r_type != R_MIPS_NONE)
   10549 	{
   10550 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
   10551 	  break;
   10552 	}
   10553     }
   10554   do
   10555     {
   10556        RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   10557 					(*rel), count, (*relend), R_MIPS_NONE,
   10558 					howto, i, contents);
   10559     }
   10560   while (0);
   10561 }
   10562 
   10563 /* Relocate a MIPS ELF section.  */
   10564 
   10565 int
   10566 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
   10567 				bfd *input_bfd, asection *input_section,
   10568 				bfd_byte *contents, Elf_Internal_Rela *relocs,
   10569 				Elf_Internal_Sym *local_syms,
   10570 				asection **local_sections)
   10571 {
   10572   Elf_Internal_Rela *rel;
   10573   const Elf_Internal_Rela *relend;
   10574   bfd_vma addend = 0;
   10575   bool use_saved_addend_p = false;
   10576 
   10577   relend = relocs + input_section->reloc_count;
   10578   for (rel = relocs; rel < relend; ++rel)
   10579     {
   10580       const char *name;
   10581       bfd_vma value = 0;
   10582       reloc_howto_type *howto;
   10583       bool cross_mode_jump_p = false;
   10584       /* TRUE if the relocation is a RELA relocation, rather than a
   10585 	 REL relocation.  */
   10586       bool rela_relocation_p = true;
   10587       unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   10588       const char *msg;
   10589       unsigned long r_symndx;
   10590       asection *sec;
   10591       Elf_Internal_Shdr *symtab_hdr;
   10592       struct elf_link_hash_entry *h;
   10593       bool rel_reloc;
   10594 
   10595       rel_reloc = mips_elf_rel_relocation_p (input_bfd, input_section,
   10596 					     relocs, rel);
   10597       /* Find the relocation howto for this relocation.  */
   10598       howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
   10599 
   10600       r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
   10601       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   10602       if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
   10603 	{
   10604 	  sec = local_sections[r_symndx];
   10605 	  h = NULL;
   10606 	}
   10607       else
   10608 	{
   10609 	  unsigned long extsymoff;
   10610 
   10611 	  extsymoff = 0;
   10612 	  if (!elf_bad_symtab (input_bfd))
   10613 	    extsymoff = symtab_hdr->sh_info;
   10614 	  h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
   10615 	  while (h->root.type == bfd_link_hash_indirect
   10616 		 || h->root.type == bfd_link_hash_warning)
   10617 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10618 
   10619 	  sec = NULL;
   10620 	  if (h->root.type == bfd_link_hash_defined
   10621 	      || h->root.type == bfd_link_hash_defweak)
   10622 	    sec = h->root.u.def.section;
   10623 	}
   10624 
   10625       if (sec != NULL && discarded_section (sec))
   10626 	{
   10627 	  mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
   10628 						input_section, &rel, &relend,
   10629 						rel_reloc, howto, contents);
   10630 	  continue;
   10631 	}
   10632 
   10633       if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
   10634 	{
   10635 	  /* Some 32-bit code uses R_MIPS_64.  In particular, people use
   10636 	     64-bit code, but make sure all their addresses are in the
   10637 	     lowermost or uppermost 32-bit section of the 64-bit address
   10638 	     space.  Thus, when they use an R_MIPS_64 they mean what is
   10639 	     usually meant by R_MIPS_32, with the exception that the
   10640 	     stored value is sign-extended to 64 bits.  */
   10641 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
   10642 
   10643 	  /* On big-endian systems, we need to lie about the position
   10644 	     of the reloc.  */
   10645 	  if (bfd_big_endian (input_bfd))
   10646 	    rel->r_offset += 4;
   10647 	}
   10648 
   10649       if (!use_saved_addend_p)
   10650 	{
   10651 	  /* If these relocations were originally of the REL variety,
   10652 	     we must pull the addend out of the field that will be
   10653 	     relocated.  Otherwise, we simply use the contents of the
   10654 	     RELA relocation.  */
   10655 	  if (mips_elf_rel_relocation_p (input_bfd, input_section,
   10656 					 relocs, rel))
   10657 	    {
   10658 	      rela_relocation_p = false;
   10659 	      addend = mips_elf_read_rel_addend (input_bfd, input_section,
   10660 						 rel, howto, contents);
   10661 	      if (hi16_reloc_p (r_type)
   10662 		  || (got16_reloc_p (r_type)
   10663 		      && mips_elf_local_relocation_p (input_bfd, rel,
   10664 						      local_sections))
   10665 		  || tls_hi16_reloc_p (r_type))
   10666 		{
   10667 		  if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
   10668 						     rel, relend,
   10669 						     contents, &addend))
   10670 		    {
   10671 		      if (h)
   10672 			name = h->root.root.string;
   10673 		      else
   10674 			name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   10675 						 local_syms + r_symndx,
   10676 						 sec);
   10677 		      /* xgettext:c-format */
   10678 		      msg = bfd_asprintf (_("can't find matching LO16 reloc"
   10679 					    " against `%s' for %s at %#" PRIx64
   10680 					    " in section `%s'"),
   10681 					  name, howto->name,
   10682 					  (uint64_t) rel->r_offset,
   10683 					  input_section->name);
   10684 		      if (msg == NULL)
   10685 			return false;
   10686 		      info->callbacks->warning
   10687 			(info, msg, name, input_bfd, input_section,
   10688 			 rel->r_offset);
   10689 		    }
   10690 		}
   10691 	      else
   10692 		addend <<= howto->rightshift;
   10693 	    }
   10694 	  else
   10695 	    addend = rel->r_addend;
   10696 	  mips_elf_adjust_addend (output_bfd, info, input_bfd,
   10697 				  local_syms, local_sections, rel);
   10698 	}
   10699 
   10700       if (bfd_link_relocatable (info))
   10701 	{
   10702 	  if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
   10703 	      && bfd_big_endian (input_bfd))
   10704 	    rel->r_offset -= 4;
   10705 
   10706 	  if (!rela_relocation_p && rel->r_addend)
   10707 	    {
   10708 	      addend += rel->r_addend;
   10709 	      if (hi16_reloc_p (r_type)
   10710 		  || got16_reloc_p (r_type)
   10711 		  || tls_hi16_reloc_p (r_type))
   10712 		addend = mips_elf_high (addend);
   10713 	      else if (r_type == R_MIPS_HIGHER)
   10714 		addend = mips_elf_higher (addend);
   10715 	      else if (r_type == R_MIPS_HIGHEST)
   10716 		addend = mips_elf_highest (addend);
   10717 	      else
   10718 		addend >>= howto->rightshift;
   10719 
   10720 	      /* We use the source mask, rather than the destination
   10721 		 mask because the place to which we are writing will be
   10722 		 source of the addend in the final link.  */
   10723 	      addend &= howto->src_mask;
   10724 
   10725 	      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
   10726 		/* See the comment above about using R_MIPS_64 in the 32-bit
   10727 		   ABI.  Here, we need to update the addend.  It would be
   10728 		   possible to get away with just using the R_MIPS_32 reloc
   10729 		   but for endianness.  */
   10730 		{
   10731 		  bfd_vma sign_bits;
   10732 		  bfd_vma low_bits;
   10733 		  bfd_vma high_bits;
   10734 
   10735 		  if (addend & ((bfd_vma) 1 << 31))
   10736 #ifdef BFD64
   10737 		    sign_bits = ((bfd_vma) 1 << 32) - 1;
   10738 #else
   10739 		    sign_bits = -1;
   10740 #endif
   10741 		  else
   10742 		    sign_bits = 0;
   10743 
   10744 		  /* If we don't know that we have a 64-bit type,
   10745 		     do two separate stores.  */
   10746 		  if (bfd_big_endian (input_bfd))
   10747 		    {
   10748 		      /* Store the sign-bits (which are most significant)
   10749 			 first.  */
   10750 		      low_bits = sign_bits;
   10751 		      high_bits = addend;
   10752 		    }
   10753 		  else
   10754 		    {
   10755 		      low_bits = addend;
   10756 		      high_bits = sign_bits;
   10757 		    }
   10758 		  bfd_put_32 (input_bfd, low_bits,
   10759 			      contents + rel->r_offset);
   10760 		  bfd_put_32 (input_bfd, high_bits,
   10761 			      contents + rel->r_offset + 4);
   10762 		  continue;
   10763 		}
   10764 
   10765 	      if (! mips_elf_perform_relocation (info, howto, rel, addend,
   10766 						 input_bfd, input_section,
   10767 						 contents, false))
   10768 		return false;
   10769 	    }
   10770 
   10771 	  /* Go on to the next relocation.  */
   10772 	  continue;
   10773 	}
   10774 
   10775       /* In the N32 and 64-bit ABIs there may be multiple consecutive
   10776 	 relocations for the same offset.  In that case we are
   10777 	 supposed to treat the output of each relocation as the addend
   10778 	 for the next.  */
   10779       if (rel + 1 < relend
   10780 	  && rel->r_offset == rel[1].r_offset
   10781 	  && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
   10782 	use_saved_addend_p = true;
   10783       else
   10784 	use_saved_addend_p = false;
   10785 
   10786       /* Figure out what value we are supposed to relocate.  */
   10787       switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
   10788 					     input_section, contents,
   10789 					     info, rel, addend, howto,
   10790 					     local_syms, local_sections,
   10791 					     &value, &name, &cross_mode_jump_p,
   10792 					     use_saved_addend_p))
   10793 	{
   10794 	case bfd_reloc_continue:
   10795 	  /* There's nothing to do.  */
   10796 	  continue;
   10797 
   10798 	case bfd_reloc_undefined:
   10799 	  /* mips_elf_calculate_relocation already called the
   10800 	     undefined_symbol callback.  There's no real point in
   10801 	     trying to perform the relocation at this point, so we
   10802 	     just skip ahead to the next relocation.  */
   10803 	  continue;
   10804 
   10805 	case bfd_reloc_notsupported:
   10806 	  msg = _("internal error: unsupported relocation error");
   10807 	  info->callbacks->warning
   10808 	    (info, msg, name, input_bfd, input_section, rel->r_offset);
   10809 	  return false;
   10810 
   10811 	case bfd_reloc_overflow:
   10812 	  if (use_saved_addend_p)
   10813 	    /* Ignore overflow until we reach the last relocation for
   10814 	       a given location.  */
   10815 	    ;
   10816 	  else
   10817 	    {
   10818 	      struct mips_elf_link_hash_table *htab;
   10819 
   10820 	      htab = mips_elf_hash_table (info);
   10821 	      BFD_ASSERT (htab != NULL);
   10822 	      BFD_ASSERT (name != NULL);
   10823 	      if (!htab->small_data_overflow_reported
   10824 		  && (gprel16_reloc_p (howto->type)
   10825 		      || literal_reloc_p (howto->type)))
   10826 		{
   10827 		  msg = _("small-data section too large;"
   10828 			  " lower small-data size limit (see option -G)");
   10829 
   10830 		  htab->small_data_overflow_reported = true;
   10831 		  (*info->callbacks->einfo) ("%P: %s\n", msg);
   10832 		}
   10833 	      (*info->callbacks->reloc_overflow)
   10834 		(info, NULL, name, howto->name, (bfd_vma) 0,
   10835 		 input_bfd, input_section, rel->r_offset);
   10836 	    }
   10837 	  break;
   10838 
   10839 	case bfd_reloc_ok:
   10840 	  break;
   10841 
   10842 	case bfd_reloc_outofrange:
   10843 	  msg = NULL;
   10844 	  if (jal_reloc_p (howto->type))
   10845 	    msg = (cross_mode_jump_p
   10846 		   ? _("cannot convert a jump to JALX "
   10847 		       "for a non-word-aligned address")
   10848 		   : (howto->type == R_MIPS16_26
   10849 		      ? _("jump to a non-word-aligned address")
   10850 		      : _("jump to a non-instruction-aligned address")));
   10851 	  else if (b_reloc_p (howto->type))
   10852 	    msg = (cross_mode_jump_p
   10853 		   ? _("cannot convert a branch to JALX "
   10854 		       "for a non-word-aligned address")
   10855 		   : _("branch to a non-instruction-aligned address"));
   10856 	  else if (aligned_pcrel_reloc_p (howto->type))
   10857 	    msg = _("PC-relative load from unaligned address");
   10858 	  if (msg)
   10859 	    {
   10860 	      info->callbacks->einfo
   10861 		("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
   10862 	      break;
   10863 	    }
   10864 	  /* Fall through.  */
   10865 
   10866 	default:
   10867 	  abort ();
   10868 	  break;
   10869 	}
   10870 
   10871       /* If we've got another relocation for the address, keep going
   10872 	 until we reach the last one.  */
   10873       if (use_saved_addend_p)
   10874 	{
   10875 	  addend = value;
   10876 	  continue;
   10877 	}
   10878 
   10879       if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
   10880 	/* See the comment above about using R_MIPS_64 in the 32-bit
   10881 	   ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
   10882 	   that calculated the right value.  Now, however, we
   10883 	   sign-extend the 32-bit result to 64-bits, and store it as a
   10884 	   64-bit value.  We are especially generous here in that we
   10885 	   go to extreme lengths to support this usage on systems with
   10886 	   only a 32-bit VMA.  */
   10887 	{
   10888 	  bfd_vma sign_bits;
   10889 	  bfd_vma low_bits;
   10890 	  bfd_vma high_bits;
   10891 
   10892 	  if (value & ((bfd_vma) 1 << 31))
   10893 #ifdef BFD64
   10894 	    sign_bits = ((bfd_vma) 1 << 32) - 1;
   10895 #else
   10896 	    sign_bits = -1;
   10897 #endif
   10898 	  else
   10899 	    sign_bits = 0;
   10900 
   10901 	  /* If we don't know that we have a 64-bit type,
   10902 	     do two separate stores.  */
   10903 	  if (bfd_big_endian (input_bfd))
   10904 	    {
   10905 	      /* Undo what we did above.  */
   10906 	      rel->r_offset -= 4;
   10907 	      /* Store the sign-bits (which are most significant)
   10908 		 first.  */
   10909 	      low_bits = sign_bits;
   10910 	      high_bits = value;
   10911 	    }
   10912 	  else
   10913 	    {
   10914 	      low_bits = value;
   10915 	      high_bits = sign_bits;
   10916 	    }
   10917 	  bfd_put_32 (input_bfd, low_bits,
   10918 		      contents + rel->r_offset);
   10919 	  bfd_put_32 (input_bfd, high_bits,
   10920 		      contents + rel->r_offset + 4);
   10921 	  continue;
   10922 	}
   10923 
   10924       /* Actually perform the relocation.  */
   10925       if (! mips_elf_perform_relocation (info, howto, rel, value,
   10926 					 input_bfd, input_section,
   10927 					 contents, cross_mode_jump_p))
   10928 	return false;
   10929     }
   10930 
   10931   return true;
   10932 }
   10933 
   10934 /* A function that iterates over each entry in la25_stubs and fills
   10936    in the code for each one.  DATA points to a mips_htab_traverse_info.  */
   10937 
   10938 static int
   10939 mips_elf_create_la25_stub (void **slot, void *data)
   10940 {
   10941   struct mips_htab_traverse_info *hti;
   10942   struct mips_elf_link_hash_table *htab;
   10943   struct mips_elf_la25_stub *stub;
   10944   asection *s;
   10945   bfd_byte *loc;
   10946   bfd_vma offset, target, target_high, target_low;
   10947   bfd_vma branch_pc;
   10948   bfd_signed_vma pcrel_offset = 0;
   10949 
   10950   stub = (struct mips_elf_la25_stub *) *slot;
   10951   hti = (struct mips_htab_traverse_info *) data;
   10952   htab = mips_elf_hash_table (hti->info);
   10953   BFD_ASSERT (htab != NULL);
   10954 
   10955   /* Create the section contents, if we haven't already.  */
   10956   s = stub->stub_section;
   10957   loc = s->contents;
   10958   if (loc == NULL)
   10959     {
   10960       loc = bfd_malloc (s->size);
   10961       if (loc == NULL)
   10962 	{
   10963 	  hti->error = true;
   10964 	  return false;
   10965 	}
   10966       s->contents = loc;
   10967     }
   10968 
   10969   /* Work out where in the section this stub should go.  */
   10970   offset = stub->offset;
   10971 
   10972   /* We add 8 here to account for the LUI/ADDIU instructions
   10973      before the branch instruction.  This cannot be moved down to
   10974      where pcrel_offset is calculated as 's' is updated in
   10975      mips_elf_get_la25_target.  */
   10976   branch_pc = s->output_section->vma + s->output_offset + offset + 8;
   10977 
   10978   /* Work out the target address.  */
   10979   target = mips_elf_get_la25_target (stub, &s);
   10980   target += s->output_section->vma + s->output_offset;
   10981 
   10982   target_high = ((target + 0x8000) >> 16) & 0xffff;
   10983   target_low = (target & 0xffff);
   10984 
   10985   /* Calculate the PC of the compact branch instruction (for the case where
   10986      compact branches are used for either microMIPSR6 or MIPSR6 with
   10987      compact branches.  Add 4-bytes to account for BC using the PC of the
   10988      next instruction as the base.  */
   10989   pcrel_offset = target - (branch_pc + 4);
   10990 
   10991   if (stub->stub_section != htab->strampoline)
   10992     {
   10993       /* This is a simple LUI/ADDIU stub.  Zero out the beginning
   10994 	 of the section and write the two instructions at the end.  */
   10995       memset (loc, 0, offset);
   10996       loc += offset;
   10997       if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
   10998 	{
   10999 	  bfd_put_micromips_32 (hti->output_bfd,
   11000 				LA25_LUI_MICROMIPS (target_high),
   11001 				loc);
   11002 	  bfd_put_micromips_32 (hti->output_bfd,
   11003 				LA25_ADDIU_MICROMIPS (target_low),
   11004 				loc + 4);
   11005 	}
   11006       else
   11007 	{
   11008 	  bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
   11009 	  bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
   11010 	}
   11011     }
   11012   else
   11013     {
   11014       /* This is trampoline.  */
   11015       loc += offset;
   11016       if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
   11017 	{
   11018 	  bfd_put_micromips_32 (hti->output_bfd,
   11019 				LA25_LUI_MICROMIPS (target_high), loc);
   11020 	  bfd_put_micromips_32 (hti->output_bfd,
   11021 				LA25_J_MICROMIPS (target), loc + 4);
   11022 	  bfd_put_micromips_32 (hti->output_bfd,
   11023 				LA25_ADDIU_MICROMIPS (target_low), loc + 8);
   11024 	  bfd_put_32 (hti->output_bfd, 0, loc + 12);
   11025 	}
   11026       else
   11027 	{
   11028 	  bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
   11029 	  if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
   11030 	    {
   11031 	      bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
   11032 	      bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
   11033 	    }
   11034 	  else
   11035 	    {
   11036 	      bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
   11037 	      bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
   11038 	    }
   11039 	  bfd_put_32 (hti->output_bfd, 0, loc + 12);
   11040 	}
   11041     }
   11042   return true;
   11043 }
   11044 
   11045 /* If NAME is one of the special IRIX6 symbols defined by the linker,
   11046    adjust it appropriately now.  */
   11047 
   11048 static void
   11049 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
   11050 				      const char *name, Elf_Internal_Sym *sym)
   11051 {
   11052   /* The linker script takes care of providing names and values for
   11053      these, but we must place them into the right sections.  */
   11054   static const char* const text_section_symbols[] = {
   11055     "_ftext",
   11056     "_etext",
   11057     "__dso_displacement",
   11058     "__elf_header",
   11059     "__program_header_table",
   11060     NULL
   11061   };
   11062 
   11063   static const char* const data_section_symbols[] = {
   11064     "_fdata",
   11065     "_edata",
   11066     "_end",
   11067     "_fbss",
   11068     NULL
   11069   };
   11070 
   11071   const char* const *p;
   11072   int i;
   11073 
   11074   for (i = 0; i < 2; ++i)
   11075     for (p = (i == 0) ? text_section_symbols : data_section_symbols;
   11076 	 *p;
   11077 	 ++p)
   11078       if (strcmp (*p, name) == 0)
   11079 	{
   11080 	  /* All of these symbols are given type STT_SECTION by the
   11081 	     IRIX6 linker.  */
   11082 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   11083 	  sym->st_other = STO_PROTECTED;
   11084 
   11085 	  /* The IRIX linker puts these symbols in special sections.  */
   11086 	  if (i == 0)
   11087 	    sym->st_shndx = SHN_MIPS_TEXT;
   11088 	  else
   11089 	    sym->st_shndx = SHN_MIPS_DATA;
   11090 
   11091 	  break;
   11092 	}
   11093 }
   11094 
   11095 /* Finish up dynamic symbol handling.  We set the contents of various
   11096    dynamic sections here.  */
   11097 
   11098 bool
   11099 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
   11100 				     struct bfd_link_info *info,
   11101 				     struct elf_link_hash_entry *h,
   11102 				     Elf_Internal_Sym *sym)
   11103 {
   11104   bfd *dynobj;
   11105   asection *sgot;
   11106   struct mips_got_info *g, *gg;
   11107   const char *name;
   11108   int idx;
   11109   struct mips_elf_link_hash_table *htab;
   11110   struct mips_elf_link_hash_entry *hmips;
   11111 
   11112   htab = mips_elf_hash_table (info);
   11113   BFD_ASSERT (htab != NULL);
   11114   dynobj = elf_hash_table (info)->dynobj;
   11115   hmips = (struct mips_elf_link_hash_entry *) h;
   11116 
   11117   BFD_ASSERT (htab->root.target_os != is_vxworks);
   11118 
   11119   if (h->plt.plist != NULL
   11120       && (h->plt.plist->mips_offset != MINUS_ONE
   11121 	  || h->plt.plist->comp_offset != MINUS_ONE))
   11122     {
   11123       /* We've decided to create a PLT entry for this symbol.  */
   11124       bfd_byte *loc;
   11125       bfd_vma header_address, got_address;
   11126       bfd_vma got_address_high, got_address_low, load;
   11127       bfd_vma got_index;
   11128       bfd_vma isa_bit;
   11129 
   11130       got_index = h->plt.plist->gotplt_index;
   11131 
   11132       BFD_ASSERT (htab->use_plts_and_copy_relocs);
   11133       BFD_ASSERT (h->dynindx != -1);
   11134       BFD_ASSERT (htab->root.splt != NULL);
   11135       BFD_ASSERT (got_index != MINUS_ONE);
   11136       BFD_ASSERT (!h->def_regular);
   11137 
   11138       /* Calculate the address of the PLT header.  */
   11139       isa_bit = htab->plt_header_is_comp;
   11140       header_address = (htab->root.splt->output_section->vma
   11141 			+ htab->root.splt->output_offset + isa_bit);
   11142 
   11143       /* Calculate the address of the .got.plt entry.  */
   11144       got_address = (htab->root.sgotplt->output_section->vma
   11145 		     + htab->root.sgotplt->output_offset
   11146 		     + got_index * MIPS_ELF_GOT_SIZE (dynobj));
   11147 
   11148       got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
   11149       got_address_low = got_address & 0xffff;
   11150 
   11151       /* The PLT sequence is not safe for N64 if .got.plt entry's address
   11152 	 cannot be loaded in two instructions.  */
   11153       if (ABI_64_P (output_bfd)
   11154 	  && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
   11155 	{
   11156 	  _bfd_error_handler
   11157 	    /* xgettext:c-format */
   11158 	    (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
   11159 	       "supported; consider using `-Ttext-segment=...'"),
   11160 	     output_bfd,
   11161 	     htab->root.sgotplt->output_section,
   11162 	     (int64_t) got_address);
   11163 	  bfd_set_error (bfd_error_no_error);
   11164 	  return false;
   11165 	}
   11166 
   11167       /* Initially point the .got.plt entry at the PLT header.  */
   11168       loc = (htab->root.sgotplt->contents
   11169 	     + got_index * MIPS_ELF_GOT_SIZE (dynobj));
   11170       if (ABI_64_P (output_bfd))
   11171 	bfd_put_64 (output_bfd, header_address, loc);
   11172       else
   11173 	bfd_put_32 (output_bfd, header_address, loc);
   11174 
   11175       /* Now handle the PLT itself.  First the standard entry (the order
   11176 	 does not matter, we just have to pick one).  */
   11177       if (h->plt.plist->mips_offset != MINUS_ONE)
   11178 	{
   11179 	  const bfd_vma *plt_entry;
   11180 	  bfd_vma plt_offset;
   11181 
   11182 	  plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
   11183 
   11184 	  BFD_ASSERT (plt_offset <= htab->root.splt->size);
   11185 
   11186 	  /* Find out where the .plt entry should go.  */
   11187 	  loc = htab->root.splt->contents + plt_offset;
   11188 
   11189 	  /* Pick the load opcode.  */
   11190 	  load = MIPS_ELF_LOAD_WORD (output_bfd);
   11191 
   11192 	  /* Fill in the PLT entry itself.  */
   11193 
   11194 	  if (MIPSR6_P (output_bfd))
   11195 	    plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
   11196 					       : mipsr6_exec_plt_entry;
   11197 	  else
   11198 	    plt_entry = mips_exec_plt_entry;
   11199 	  bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
   11200 	  bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
   11201 		      loc + 4);
   11202 
   11203 	  if (! LOAD_INTERLOCKS_P (output_bfd)
   11204 	      || (MIPSR6_P (output_bfd) && htab->compact_branches))
   11205 	    {
   11206 	      bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
   11207 	      bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   11208 	    }
   11209 	  else
   11210 	    {
   11211 	      bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
   11212 	      bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
   11213 			  loc + 12);
   11214 	    }
   11215 	}
   11216 
   11217       /* Now the compressed entry.  They come after any standard ones.  */
   11218       if (h->plt.plist->comp_offset != MINUS_ONE)
   11219 	{
   11220 	  bfd_vma plt_offset;
   11221 
   11222 	  plt_offset = (htab->plt_header_size + htab->plt_mips_offset
   11223 			+ h->plt.plist->comp_offset);
   11224 
   11225 	  BFD_ASSERT (plt_offset <= htab->root.splt->size);
   11226 
   11227 	  /* Find out where the .plt entry should go.  */
   11228 	  loc = htab->root.splt->contents + plt_offset;
   11229 
   11230 	  /* Fill in the PLT entry itself.  */
   11231 	  if (!MICROMIPS_P (output_bfd))
   11232 	    {
   11233 	      const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
   11234 
   11235 	      bfd_put_16 (output_bfd, plt_entry[0], loc);
   11236 	      bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
   11237 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   11238 	      bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
   11239 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   11240 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   11241 	      bfd_put_32 (output_bfd, got_address, loc + 12);
   11242 	    }
   11243 	  else if (htab->insn32)
   11244 	    {
   11245 	      const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
   11246 
   11247 	      bfd_put_16 (output_bfd, plt_entry[0], loc);
   11248 	      bfd_put_16 (output_bfd, got_address_high, loc + 2);
   11249 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   11250 	      bfd_put_16 (output_bfd, got_address_low, loc + 6);
   11251 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   11252 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   11253 	      bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
   11254 	      bfd_put_16 (output_bfd, got_address_low, loc + 14);
   11255 	    }
   11256 	  else
   11257 	    {
   11258 	      const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
   11259 	      bfd_signed_vma gotpc_offset;
   11260 	      bfd_vma loc_address;
   11261 
   11262 	      BFD_ASSERT (got_address % 4 == 0);
   11263 
   11264 	      loc_address = (htab->root.splt->output_section->vma
   11265 			     + htab->root.splt->output_offset + plt_offset);
   11266 	      gotpc_offset = got_address - ((loc_address | 3) ^ 3);
   11267 
   11268 	      /* ADDIUPC has a span of +/-16MB, check we're in range.  */
   11269 	      if (gotpc_offset + 0x1000000 >= 0x2000000)
   11270 		{
   11271 		  _bfd_error_handler
   11272 		    /* xgettext:c-format */
   11273 		    (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
   11274 		       "beyond the range of ADDIUPC"),
   11275 		     output_bfd,
   11276 		     htab->root.sgotplt->output_section,
   11277 		     (int64_t) gotpc_offset,
   11278 		     htab->root.splt->output_section);
   11279 		  bfd_set_error (bfd_error_no_error);
   11280 		  return false;
   11281 		}
   11282 	      bfd_put_16 (output_bfd,
   11283 			  plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
   11284 	      bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
   11285 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   11286 	      bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
   11287 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   11288 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   11289 	    }
   11290 	}
   11291 
   11292       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
   11293       mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
   11294 					  got_index - 2, h->dynindx,
   11295 					  R_MIPS_JUMP_SLOT, got_address);
   11296 
   11297       /* We distinguish between PLT entries and lazy-binding stubs by
   11298 	 giving the former an st_other value of STO_MIPS_PLT.  Set the
   11299 	 flag and leave the value if there are any relocations in the
   11300 	 binary where pointer equality matters.  */
   11301       sym->st_shndx = SHN_UNDEF;
   11302       if (h->pointer_equality_needed)
   11303 	sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
   11304       else
   11305 	{
   11306 	  sym->st_value = 0;
   11307 	  sym->st_other = 0;
   11308 	}
   11309     }
   11310 
   11311   if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
   11312     {
   11313       /* We've decided to create a lazy-binding stub.  */
   11314       bool micromips_p = MICROMIPS_P (output_bfd);
   11315       unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   11316       bfd_vma stub_size = htab->function_stub_size;
   11317       bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
   11318       bfd_vma isa_bit = micromips_p;
   11319       bfd_vma stub_big_size;
   11320 
   11321       if (!micromips_p)
   11322 	stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
   11323       else if (htab->insn32)
   11324 	stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
   11325       else
   11326 	stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
   11327 
   11328       /* This symbol has a stub.  Set it up.  */
   11329 
   11330       BFD_ASSERT (h->dynindx != -1);
   11331 
   11332       BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
   11333 
   11334       /* Values up to 2^31 - 1 are allowed.  Larger values would cause
   11335 	 sign extension at runtime in the stub, resulting in a negative
   11336 	 index value.  */
   11337       if (h->dynindx & ~0x7fffffff)
   11338 	{
   11339 	  _bfd_error_handler
   11340 	    (_("%pB: cannot handle more than %d dynamic symbols"),
   11341 	     output_bfd, 0x7fffffff);
   11342 	  bfd_set_error (bfd_error_bad_value);
   11343 	  return false;
   11344 	}
   11345 
   11346       /* Fill the stub.  */
   11347       if (micromips_p)
   11348 	{
   11349 	  idx = 0;
   11350 	  bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
   11351 				stub + idx);
   11352 	  idx += 4;
   11353 	  if (htab->insn32)
   11354 	    {
   11355 	      bfd_put_micromips_32 (output_bfd,
   11356 				    STUB_MOVE32_MICROMIPS, stub + idx);
   11357 	      idx += 4;
   11358 	    }
   11359 	  else
   11360 	    {
   11361 	      bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
   11362 	      idx += 2;
   11363 	    }
   11364 	  if (stub_size == stub_big_size)
   11365 	    {
   11366 	      long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
   11367 
   11368 	      bfd_put_micromips_32 (output_bfd,
   11369 				    STUB_LUI_MICROMIPS (dynindx_hi),
   11370 				    stub + idx);
   11371 	      idx += 4;
   11372 	    }
   11373 	  if (htab->insn32)
   11374 	    {
   11375 	      bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
   11376 				    stub + idx);
   11377 	      idx += 4;
   11378 	    }
   11379 	  else
   11380 	    {
   11381 	      bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
   11382 	      idx += 2;
   11383 	    }
   11384 
   11385 	  /* If a large stub is not required and sign extension is not a
   11386 	     problem, then use legacy code in the stub.  */
   11387 	  if (stub_size == stub_big_size)
   11388 	    bfd_put_micromips_32 (output_bfd,
   11389 				  STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
   11390 				  stub + idx);
   11391 	  else if (h->dynindx & ~0x7fff)
   11392 	    bfd_put_micromips_32 (output_bfd,
   11393 				  STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
   11394 				  stub + idx);
   11395 	  else
   11396 	    bfd_put_micromips_32 (output_bfd,
   11397 				  STUB_LI16S_MICROMIPS (output_bfd,
   11398 							h->dynindx),
   11399 				  stub + idx);
   11400 	}
   11401       else
   11402 	{
   11403 	  idx = 0;
   11404 	  bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
   11405 	  idx += 4;
   11406 	  bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
   11407 	  idx += 4;
   11408 	  if (stub_size == stub_big_size)
   11409 	    {
   11410 	      bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
   11411 			  stub + idx);
   11412 	      idx += 4;
   11413 	    }
   11414 
   11415 	  if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
   11416 	    {
   11417 	      bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
   11418 	      idx += 4;
   11419 	    }
   11420 
   11421 	  /* If a large stub is not required and sign extension is not a
   11422 	     problem, then use legacy code in the stub.  */
   11423 	  if (stub_size == stub_big_size)
   11424 	    bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
   11425 			stub + idx);
   11426 	  else if (h->dynindx & ~0x7fff)
   11427 	    bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
   11428 			stub + idx);
   11429 	  else
   11430 	    bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
   11431 			stub + idx);
   11432 	  idx += 4;
   11433 
   11434 	  if (MIPSR6_P (output_bfd) && htab->compact_branches)
   11435 	    bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
   11436 	}
   11437 
   11438       BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
   11439       memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
   11440 	      stub, stub_size);
   11441 
   11442       /* Mark the symbol as undefined.  stub_offset != -1 occurs
   11443 	 only for the referenced symbol.  */
   11444       sym->st_shndx = SHN_UNDEF;
   11445 
   11446       /* The run-time linker uses the st_value field of the symbol
   11447 	 to reset the global offset table entry for this external
   11448 	 to its stub address when unlinking a shared object.  */
   11449       sym->st_value = (htab->sstubs->output_section->vma
   11450 		       + htab->sstubs->output_offset
   11451 		       + h->plt.plist->stub_offset
   11452 		       + isa_bit);
   11453       sym->st_other = other;
   11454     }
   11455 
   11456   /* If we have a MIPS16 function with a stub, the dynamic symbol must
   11457      refer to the stub, since only the stub uses the standard calling
   11458      conventions.  */
   11459   if (h->dynindx != -1 && hmips->fn_stub != NULL)
   11460     {
   11461       BFD_ASSERT (hmips->need_fn_stub);
   11462       sym->st_value = (hmips->fn_stub->output_section->vma
   11463 		       + hmips->fn_stub->output_offset);
   11464       sym->st_size = hmips->fn_stub->size;
   11465       sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
   11466     }
   11467 
   11468   BFD_ASSERT (h->dynindx != -1
   11469 	      || h->forced_local);
   11470 
   11471   sgot = htab->root.sgot;
   11472   g = htab->got_info;
   11473   BFD_ASSERT (g != NULL);
   11474 
   11475   /* Run through the global symbol table, creating GOT entries for all
   11476      the symbols that need them.  */
   11477   if (hmips->global_got_area != GGA_NONE)
   11478     {
   11479       bfd_vma offset;
   11480       bfd_vma value;
   11481 
   11482       value = sym->st_value;
   11483       offset = mips_elf_primary_global_got_index (output_bfd, info, h);
   11484       MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
   11485     }
   11486 
   11487   if (hmips->global_got_area != GGA_NONE && g->next)
   11488     {
   11489       struct mips_got_entry e, *p;
   11490       bfd_vma entry;
   11491       bfd_vma offset;
   11492 
   11493       gg = g;
   11494 
   11495       e.abfd = output_bfd;
   11496       e.symndx = -1;
   11497       e.d.h = hmips;
   11498       e.tls_type = GOT_TLS_NONE;
   11499 
   11500       for (g = g->next; g->next != gg; g = g->next)
   11501 	{
   11502 	  if (g->got_entries
   11503 	      && (p = (struct mips_got_entry *) htab_find (g->got_entries,
   11504 							   &e)))
   11505 	    {
   11506 	      offset = p->gotidx;
   11507 	      BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
   11508 	      if (bfd_link_pic (info)
   11509 		  || (elf_hash_table (info)->dynamic_sections_created
   11510 		      && p->d.h != NULL
   11511 		      && p->d.h->root.def_dynamic
   11512 		      && !p->d.h->root.def_regular))
   11513 		{
   11514 		  /* Create an R_MIPS_REL32 relocation for this entry.  Due to
   11515 		     the various compatibility problems, it's easier to mock
   11516 		     up an R_MIPS_32 or R_MIPS_64 relocation and leave
   11517 		     mips_elf_create_dynamic_relocation to calculate the
   11518 		     appropriate addend.  */
   11519 		  Elf_Internal_Rela rel[3];
   11520 
   11521 		  memset (rel, 0, sizeof (rel));
   11522 		  if (ABI_64_P (output_bfd))
   11523 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
   11524 		  else
   11525 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
   11526 		  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
   11527 
   11528 		  entry = 0;
   11529 		  if (! (mips_elf_create_dynamic_relocation
   11530 			 (output_bfd, info, rel,
   11531 			  e.d.h, NULL, sym->st_value, &entry, sgot)))
   11532 		    return false;
   11533 		}
   11534 	      else
   11535 		entry = sym->st_value;
   11536 	      MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
   11537 	    }
   11538 	}
   11539     }
   11540 
   11541   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
   11542   name = h->root.root.string;
   11543   if (h == elf_hash_table (info)->hdynamic
   11544       || h == elf_hash_table (info)->hgot)
   11545     sym->st_shndx = SHN_ABS;
   11546   else if (strcmp (name, "_DYNAMIC_LINK") == 0
   11547 	   || strcmp (name, "_DYNAMIC_LINKING") == 0)
   11548     {
   11549       sym->st_shndx = SHN_ABS;
   11550       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   11551       sym->st_value = 1;
   11552     }
   11553   else if (SGI_COMPAT (output_bfd))
   11554     {
   11555       if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
   11556 	  || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
   11557 	{
   11558 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   11559 	  sym->st_other = STO_PROTECTED;
   11560 	  sym->st_value = 0;
   11561 	  sym->st_shndx = SHN_MIPS_DATA;
   11562 	}
   11563       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
   11564 	{
   11565 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   11566 	  sym->st_other = STO_PROTECTED;
   11567 	  sym->st_value = mips_elf_hash_table (info)->procedure_count;
   11568 	  sym->st_shndx = SHN_ABS;
   11569 	}
   11570       else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
   11571 	{
   11572 	  if (h->type == STT_FUNC)
   11573 	    sym->st_shndx = SHN_MIPS_TEXT;
   11574 	  else if (h->type == STT_OBJECT)
   11575 	    sym->st_shndx = SHN_MIPS_DATA;
   11576 	}
   11577     }
   11578 
   11579   /* Emit a copy reloc, if needed.  */
   11580   if (h->needs_copy)
   11581     {
   11582       asection *s;
   11583       bfd_vma symval;
   11584 
   11585       BFD_ASSERT (h->dynindx != -1);
   11586       BFD_ASSERT (htab->use_plts_and_copy_relocs);
   11587 
   11588       s = mips_elf_rel_dyn_section (info, false);
   11589       symval = (h->root.u.def.section->output_section->vma
   11590 		+ h->root.u.def.section->output_offset
   11591 		+ h->root.u.def.value);
   11592       mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
   11593 					  h->dynindx, R_MIPS_COPY, symval);
   11594     }
   11595 
   11596   /* Handle the IRIX6-specific symbols.  */
   11597   if (IRIX_COMPAT (output_bfd) == ict_irix6)
   11598     mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
   11599 
   11600   /* Keep dynamic compressed symbols odd.  This allows the dynamic linker
   11601      to treat compressed symbols like any other.  */
   11602   if (ELF_ST_IS_MIPS16 (sym->st_other))
   11603     {
   11604       BFD_ASSERT (sym->st_value & 1);
   11605       sym->st_other -= STO_MIPS16;
   11606     }
   11607   else if (ELF_ST_IS_MICROMIPS (sym->st_other))
   11608     {
   11609       BFD_ASSERT (sym->st_value & 1);
   11610       sym->st_other -= STO_MICROMIPS;
   11611     }
   11612 
   11613   return true;
   11614 }
   11615 
   11616 /* Likewise, for VxWorks.  */
   11617 
   11618 bool
   11619 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
   11620 					 struct bfd_link_info *info,
   11621 					 struct elf_link_hash_entry *h,
   11622 					 Elf_Internal_Sym *sym)
   11623 {
   11624   bfd *dynobj;
   11625   asection *sgot;
   11626   struct mips_got_info *g;
   11627   struct mips_elf_link_hash_table *htab;
   11628   struct mips_elf_link_hash_entry *hmips;
   11629 
   11630   htab = mips_elf_hash_table (info);
   11631   BFD_ASSERT (htab != NULL);
   11632   dynobj = elf_hash_table (info)->dynobj;
   11633   hmips = (struct mips_elf_link_hash_entry *) h;
   11634 
   11635   if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
   11636     {
   11637       bfd_byte *loc;
   11638       bfd_vma plt_address, got_address, got_offset, branch_offset;
   11639       Elf_Internal_Rela rel;
   11640       static const bfd_vma *plt_entry;
   11641       bfd_vma gotplt_index;
   11642       bfd_vma plt_offset;
   11643 
   11644       plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
   11645       gotplt_index = h->plt.plist->gotplt_index;
   11646 
   11647       BFD_ASSERT (h->dynindx != -1);
   11648       BFD_ASSERT (htab->root.splt != NULL);
   11649       BFD_ASSERT (gotplt_index != MINUS_ONE);
   11650       BFD_ASSERT (plt_offset <= htab->root.splt->size);
   11651 
   11652       /* Calculate the address of the .plt entry.  */
   11653       plt_address = (htab->root.splt->output_section->vma
   11654 		     + htab->root.splt->output_offset
   11655 		     + plt_offset);
   11656 
   11657       /* Calculate the address of the .got.plt entry.  */
   11658       got_address = (htab->root.sgotplt->output_section->vma
   11659 		     + htab->root.sgotplt->output_offset
   11660 		     + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
   11661 
   11662       /* Calculate the offset of the .got.plt entry from
   11663 	 _GLOBAL_OFFSET_TABLE_.  */
   11664       got_offset = mips_elf_gotplt_index (info, h);
   11665 
   11666       /* Calculate the offset for the branch at the start of the PLT
   11667 	 entry.  The branch jumps to the beginning of .plt.  */
   11668       branch_offset = -(plt_offset / 4 + 1) & 0xffff;
   11669 
   11670       /* Fill in the initial value of the .got.plt entry.  */
   11671       bfd_put_32 (output_bfd, plt_address,
   11672 		  (htab->root.sgotplt->contents
   11673 		   + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
   11674 
   11675       /* Find out where the .plt entry should go.  */
   11676       loc = htab->root.splt->contents + plt_offset;
   11677 
   11678       if (bfd_link_pic (info))
   11679 	{
   11680 	  plt_entry = mips_vxworks_shared_plt_entry;
   11681 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
   11682 	  bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
   11683 	}
   11684       else
   11685 	{
   11686 	  bfd_vma got_address_high, got_address_low;
   11687 
   11688 	  plt_entry = mips_vxworks_exec_plt_entry;
   11689 	  got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
   11690 	  got_address_low = got_address & 0xffff;
   11691 
   11692 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
   11693 	  bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
   11694 	  bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
   11695 	  bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
   11696 	  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11697 	  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11698 	  bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
   11699 	  bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
   11700 
   11701 	  loc = (htab->srelplt2->contents
   11702 		 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
   11703 
   11704 	  /* Emit a relocation for the .got.plt entry.  */
   11705 	  rel.r_offset = got_address;
   11706 	  rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
   11707 	  rel.r_addend = plt_offset;
   11708 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11709 
   11710 	  /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
   11711 	  loc += sizeof (Elf32_External_Rela);
   11712 	  rel.r_offset = plt_address + 8;
   11713 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11714 	  rel.r_addend = got_offset;
   11715 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11716 
   11717 	  /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
   11718 	  loc += sizeof (Elf32_External_Rela);
   11719 	  rel.r_offset += 4;
   11720 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11721 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11722 	}
   11723 
   11724       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
   11725       loc = (htab->root.srelplt->contents
   11726 	     + gotplt_index * sizeof (Elf32_External_Rela));
   11727       rel.r_offset = got_address;
   11728       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
   11729       rel.r_addend = 0;
   11730       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11731 
   11732       if (!h->def_regular)
   11733 	sym->st_shndx = SHN_UNDEF;
   11734     }
   11735 
   11736   BFD_ASSERT (h->dynindx != -1 || h->forced_local);
   11737 
   11738   sgot = htab->root.sgot;
   11739   g = htab->got_info;
   11740   BFD_ASSERT (g != NULL);
   11741 
   11742   /* See if this symbol has an entry in the GOT.  */
   11743   if (hmips->global_got_area != GGA_NONE)
   11744     {
   11745       bfd_vma offset;
   11746       Elf_Internal_Rela outrel;
   11747       bfd_byte *loc;
   11748       asection *s;
   11749 
   11750       /* Install the symbol value in the GOT.   */
   11751       offset = mips_elf_primary_global_got_index (output_bfd, info, h);
   11752       MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
   11753 
   11754       /* Add a dynamic relocation for it.  */
   11755       s = mips_elf_rel_dyn_section (info, false);
   11756       loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
   11757       outrel.r_offset = (sgot->output_section->vma
   11758 			 + sgot->output_offset
   11759 			 + offset);
   11760       outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
   11761       outrel.r_addend = 0;
   11762       bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
   11763     }
   11764 
   11765   /* Emit a copy reloc, if needed.  */
   11766   if (h->needs_copy)
   11767     {
   11768       Elf_Internal_Rela rel;
   11769       asection *srel;
   11770       bfd_byte *loc;
   11771 
   11772       BFD_ASSERT (h->dynindx != -1);
   11773 
   11774       rel.r_offset = (h->root.u.def.section->output_section->vma
   11775 		      + h->root.u.def.section->output_offset
   11776 		      + h->root.u.def.value);
   11777       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
   11778       rel.r_addend = 0;
   11779       if (h->root.u.def.section == htab->root.sdynrelro)
   11780 	srel = htab->root.sreldynrelro;
   11781       else
   11782 	srel = htab->root.srelbss;
   11783       loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
   11784       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11785       ++srel->reloc_count;
   11786     }
   11787 
   11788   /* If this is a mips16/microMIPS symbol, force the value to be even.  */
   11789   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   11790     sym->st_value &= ~1;
   11791 
   11792   return true;
   11793 }
   11794 
   11795 /* Write out a plt0 entry to the beginning of .plt.  */
   11796 
   11797 static bool
   11798 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
   11799 {
   11800   bfd_byte *loc;
   11801   bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
   11802   static const bfd_vma *plt_entry;
   11803   struct mips_elf_link_hash_table *htab;
   11804 
   11805   htab = mips_elf_hash_table (info);
   11806   BFD_ASSERT (htab != NULL);
   11807 
   11808   if (ABI_64_P (output_bfd))
   11809     plt_entry = (htab->compact_branches
   11810 		 ? mipsr6_n64_exec_plt0_entry_compact
   11811 		 : mips_n64_exec_plt0_entry);
   11812   else if (ABI_N32_P (output_bfd))
   11813     plt_entry = (htab->compact_branches
   11814 		 ? mipsr6_n32_exec_plt0_entry_compact
   11815 		 : mips_n32_exec_plt0_entry);
   11816   else if (!htab->plt_header_is_comp)
   11817     plt_entry = (htab->compact_branches
   11818 		 ? mipsr6_o32_exec_plt0_entry_compact
   11819 		 : mips_o32_exec_plt0_entry);
   11820   else if (htab->insn32)
   11821     plt_entry = micromips_insn32_o32_exec_plt0_entry;
   11822   else
   11823     plt_entry = micromips_o32_exec_plt0_entry;
   11824 
   11825   /* Calculate the value of .got.plt.  */
   11826   gotplt_value = (htab->root.sgotplt->output_section->vma
   11827 		  + htab->root.sgotplt->output_offset);
   11828   gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
   11829   gotplt_value_low = gotplt_value & 0xffff;
   11830 
   11831   /* The PLT sequence is not safe for N64 if .got.plt's address can
   11832      not be loaded in two instructions.  */
   11833   if (ABI_64_P (output_bfd)
   11834       && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
   11835     {
   11836       _bfd_error_handler
   11837 	/* xgettext:c-format */
   11838 	(_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
   11839 	   "supported; consider using `-Ttext-segment=...'"),
   11840 	 output_bfd,
   11841 	 htab->root.sgotplt->output_section,
   11842 	 (int64_t) gotplt_value);
   11843       bfd_set_error (bfd_error_no_error);
   11844       return false;
   11845     }
   11846 
   11847   /* Install the PLT header.  */
   11848   loc = htab->root.splt->contents;
   11849   if (plt_entry == micromips_o32_exec_plt0_entry)
   11850     {
   11851       bfd_vma gotpc_offset;
   11852       bfd_vma loc_address;
   11853       size_t i;
   11854 
   11855       BFD_ASSERT (gotplt_value % 4 == 0);
   11856 
   11857       loc_address = (htab->root.splt->output_section->vma
   11858 		     + htab->root.splt->output_offset);
   11859       gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
   11860 
   11861       /* ADDIUPC has a span of +/-16MB, check we're in range.  */
   11862       if (gotpc_offset + 0x1000000 >= 0x2000000)
   11863 	{
   11864 	  _bfd_error_handler
   11865 	    /* xgettext:c-format */
   11866 	    (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
   11867 	       "beyond the range of ADDIUPC"),
   11868 	     output_bfd,
   11869 	     htab->root.sgotplt->output_section,
   11870 	     (int64_t) gotpc_offset,
   11871 	     htab->root.splt->output_section);
   11872 	  bfd_set_error (bfd_error_no_error);
   11873 	  return false;
   11874 	}
   11875       bfd_put_16 (output_bfd,
   11876 		  plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
   11877       bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
   11878       for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
   11879 	bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
   11880     }
   11881   else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
   11882     {
   11883       size_t i;
   11884 
   11885       bfd_put_16 (output_bfd, plt_entry[0], loc);
   11886       bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
   11887       bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   11888       bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
   11889       bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   11890       bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
   11891       for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
   11892 	bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
   11893     }
   11894   else
   11895     {
   11896       bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
   11897       bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
   11898       bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
   11899       bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   11900       bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11901       bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11902       bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
   11903       bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
   11904     }
   11905 
   11906   return true;
   11907 }
   11908 
   11909 /* Install the PLT header for a VxWorks executable and finalize the
   11910    contents of .rela.plt.unloaded.  */
   11911 
   11912 static void
   11913 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
   11914 {
   11915   Elf_Internal_Rela rela;
   11916   bfd_byte *loc;
   11917   bfd_vma got_value, got_value_high, got_value_low, plt_address;
   11918   static const bfd_vma *plt_entry;
   11919   struct mips_elf_link_hash_table *htab;
   11920 
   11921   htab = mips_elf_hash_table (info);
   11922   BFD_ASSERT (htab != NULL);
   11923 
   11924   plt_entry = mips_vxworks_exec_plt0_entry;
   11925 
   11926   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
   11927   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
   11928 	       + htab->root.hgot->root.u.def.section->output_offset
   11929 	       + htab->root.hgot->root.u.def.value);
   11930 
   11931   got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
   11932   got_value_low = got_value & 0xffff;
   11933 
   11934   /* Calculate the address of the PLT header.  */
   11935   plt_address = (htab->root.splt->output_section->vma
   11936 		 + htab->root.splt->output_offset);
   11937 
   11938   /* Install the PLT header.  */
   11939   loc = htab->root.splt->contents;
   11940   bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
   11941   bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
   11942   bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
   11943   bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   11944   bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11945   bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11946 
   11947   /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
   11948   loc = htab->srelplt2->contents;
   11949   rela.r_offset = plt_address;
   11950   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11951   rela.r_addend = 0;
   11952   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   11953   loc += sizeof (Elf32_External_Rela);
   11954 
   11955   /* Output the relocation for the following addiu of
   11956      %lo(_GLOBAL_OFFSET_TABLE_).  */
   11957   rela.r_offset += 4;
   11958   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11959   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   11960   loc += sizeof (Elf32_External_Rela);
   11961 
   11962   /* Fix up the remaining relocations.  They may have the wrong
   11963      symbol index for _G_O_T_ or _P_L_T_ depending on the order
   11964      in which symbols were output.  */
   11965   while (loc < htab->srelplt2->contents + htab->srelplt2->size)
   11966     {
   11967       Elf_Internal_Rela rel;
   11968 
   11969       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11970       rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
   11971       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11972       loc += sizeof (Elf32_External_Rela);
   11973 
   11974       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11975       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11976       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11977       loc += sizeof (Elf32_External_Rela);
   11978 
   11979       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11980       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11981       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11982       loc += sizeof (Elf32_External_Rela);
   11983     }
   11984 }
   11985 
   11986 /* Install the PLT header for a VxWorks shared library.  */
   11987 
   11988 static void
   11989 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
   11990 {
   11991   unsigned int i;
   11992   struct mips_elf_link_hash_table *htab;
   11993 
   11994   htab = mips_elf_hash_table (info);
   11995   BFD_ASSERT (htab != NULL);
   11996 
   11997   /* We just need to copy the entry byte-by-byte.  */
   11998   for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
   11999     bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
   12000 		htab->root.splt->contents + i * 4);
   12001 }
   12002 
   12003 /* Finish up the dynamic sections.  */
   12004 
   12005 bool
   12006 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
   12007 				       struct bfd_link_info *info,
   12008 				       bfd_byte *buf ATTRIBUTE_UNUSED)
   12009 {
   12010   bfd *dynobj;
   12011   asection *sdyn;
   12012   asection *sgot;
   12013   struct mips_got_info *gg, *g;
   12014   struct mips_elf_link_hash_table *htab;
   12015 
   12016   htab = mips_elf_hash_table (info);
   12017   BFD_ASSERT (htab != NULL);
   12018 
   12019   dynobj = elf_hash_table (info)->dynobj;
   12020 
   12021   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   12022 
   12023   sgot = htab->root.sgot;
   12024   gg = htab->got_info;
   12025 
   12026   if (elf_hash_table (info)->dynamic_sections_created)
   12027     {
   12028       bfd_byte *b;
   12029       int dyn_to_skip = 0, dyn_skipped = 0;
   12030 
   12031       BFD_ASSERT (sdyn != NULL);
   12032       BFD_ASSERT (gg != NULL);
   12033 
   12034       g = mips_elf_bfd_got (output_bfd, false);
   12035       BFD_ASSERT (g != NULL);
   12036 
   12037       for (b = sdyn->contents;
   12038 	   b < sdyn->contents + sdyn->size;
   12039 	   b += MIPS_ELF_DYN_SIZE (dynobj))
   12040 	{
   12041 	  Elf_Internal_Dyn dyn;
   12042 	  const char *name;
   12043 	  size_t elemsize;
   12044 	  asection *s;
   12045 	  bool swap_out_p;
   12046 
   12047 	  /* Read in the current dynamic entry.  */
   12048 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
   12049 
   12050 	  /* Assume that we're going to modify it and write it out.  */
   12051 	  swap_out_p = true;
   12052 
   12053 	  switch (dyn.d_tag)
   12054 	    {
   12055 	    case DT_RELENT:
   12056 	      dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
   12057 	      break;
   12058 
   12059 	    case DT_RELAENT:
   12060 	      BFD_ASSERT (htab->root.target_os == is_vxworks);
   12061 	      dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
   12062 	      break;
   12063 
   12064 	    case DT_STRSZ:
   12065 	      /* Rewrite DT_STRSZ.  */
   12066 	      dyn.d_un.d_val =
   12067 		_bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   12068 	      break;
   12069 
   12070 	    case DT_PLTGOT:
   12071 	      s = htab->root.sgot;
   12072 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   12073 	      break;
   12074 
   12075 	    case DT_MIPS_PLTGOT:
   12076 	      s = htab->root.sgotplt;
   12077 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   12078 	      break;
   12079 
   12080 	    case DT_MIPS_RLD_VERSION:
   12081 	      dyn.d_un.d_val = 1; /* XXX */
   12082 	      break;
   12083 
   12084 	    case DT_MIPS_FLAGS:
   12085 	      dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
   12086 	      break;
   12087 
   12088 	    case DT_MIPS_TIME_STAMP:
   12089 	      {
   12090 		time_t t;
   12091 		time (&t);
   12092 		dyn.d_un.d_val = t;
   12093 	      }
   12094 	      break;
   12095 
   12096 	    case DT_MIPS_ICHECKSUM:
   12097 	      /* XXX FIXME: */
   12098 	      swap_out_p = false;
   12099 	      break;
   12100 
   12101 	    case DT_MIPS_IVERSION:
   12102 	      /* XXX FIXME: */
   12103 	      swap_out_p = false;
   12104 	      break;
   12105 
   12106 	    case DT_MIPS_BASE_ADDRESS:
   12107 	      s = output_bfd->sections;
   12108 	      BFD_ASSERT (s != NULL);
   12109 	      dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
   12110 	      break;
   12111 
   12112 	    case DT_MIPS_LOCAL_GOTNO:
   12113 	      dyn.d_un.d_val = g->local_gotno;
   12114 	      break;
   12115 
   12116 	    case DT_MIPS_UNREFEXTNO:
   12117 	      /* The index into the dynamic symbol table which is the
   12118 		 entry of the first external symbol that is not
   12119 		 referenced within the same object.  */
   12120 	      dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
   12121 	      break;
   12122 
   12123 	    case DT_MIPS_GOTSYM:
   12124 	      if (htab->global_gotsym)
   12125 		{
   12126 		  dyn.d_un.d_val = htab->global_gotsym->dynindx;
   12127 		  break;
   12128 		}
   12129 	      /* In case if we don't have global got symbols we default
   12130 		 to setting DT_MIPS_GOTSYM to the same value as
   12131 		 DT_MIPS_SYMTABNO.  */
   12132 	      /* Fall through.  */
   12133 
   12134 	    case DT_MIPS_SYMTABNO:
   12135 	      name = ".dynsym";
   12136 	      elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
   12137 	      s = bfd_get_linker_section (dynobj, name);
   12138 
   12139 	      if (s != NULL)
   12140 		dyn.d_un.d_val = s->size / elemsize;
   12141 	      else
   12142 		dyn.d_un.d_val = 0;
   12143 	      break;
   12144 
   12145 	    case DT_MIPS_HIPAGENO:
   12146 	      dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
   12147 	      break;
   12148 
   12149 	    case DT_MIPS_RLD_MAP:
   12150 	      {
   12151 		struct elf_link_hash_entry *h;
   12152 		h = mips_elf_hash_table (info)->rld_symbol;
   12153 		if (!h)
   12154 		  {
   12155 		    dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   12156 		    swap_out_p = false;
   12157 		    break;
   12158 		  }
   12159 		s = h->root.u.def.section;
   12160 
   12161 		/* The MIPS_RLD_MAP tag stores the absolute address of the
   12162 		   debug pointer.  */
   12163 		dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
   12164 				  + h->root.u.def.value);
   12165 	      }
   12166 	      break;
   12167 
   12168 	    case DT_MIPS_RLD_MAP_REL:
   12169 	      {
   12170 		struct elf_link_hash_entry *h;
   12171 		bfd_vma dt_addr, rld_addr;
   12172 		h = mips_elf_hash_table (info)->rld_symbol;
   12173 		if (!h)
   12174 		  {
   12175 		    dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   12176 		    swap_out_p = false;
   12177 		    break;
   12178 		  }
   12179 		s = h->root.u.def.section;
   12180 
   12181 		/* The MIPS_RLD_MAP_REL tag stores the offset to the debug
   12182 		   pointer, relative to the address of the tag.  */
   12183 		dt_addr = (sdyn->output_section->vma + sdyn->output_offset
   12184 			   + (b - sdyn->contents));
   12185 		rld_addr = (s->output_section->vma + s->output_offset
   12186 			    + h->root.u.def.value);
   12187 		dyn.d_un.d_ptr = rld_addr - dt_addr;
   12188 	      }
   12189 	      break;
   12190 
   12191 	    case DT_MIPS_OPTIONS:
   12192 	      s = (bfd_get_section_by_name
   12193 		   (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
   12194 	      dyn.d_un.d_ptr = s->vma;
   12195 	      break;
   12196 
   12197 	    case DT_PLTREL:
   12198 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   12199 	      if (htab->root.target_os == is_vxworks)
   12200 		dyn.d_un.d_val = DT_RELA;
   12201 	      else
   12202 		dyn.d_un.d_val = DT_REL;
   12203 	      break;
   12204 
   12205 	    case DT_PLTRELSZ:
   12206 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   12207 	      dyn.d_un.d_val = htab->root.srelplt->size;
   12208 	      break;
   12209 
   12210 	    case DT_JMPREL:
   12211 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   12212 	      dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
   12213 				+ htab->root.srelplt->output_offset);
   12214 	      break;
   12215 
   12216 	    case DT_TEXTREL:
   12217 	      /* If we didn't need any text relocations after all, delete
   12218 		 the dynamic tag.  */
   12219 	      if (!(info->flags & DF_TEXTREL))
   12220 		{
   12221 		  dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   12222 		  swap_out_p = false;
   12223 		}
   12224 	      break;
   12225 
   12226 	    case DT_FLAGS:
   12227 	      /* If we didn't need any text relocations after all, clear
   12228 		 DF_TEXTREL from DT_FLAGS.  */
   12229 	      if (!(info->flags & DF_TEXTREL))
   12230 		dyn.d_un.d_val &= ~DF_TEXTREL;
   12231 	      else
   12232 		swap_out_p = false;
   12233 	      break;
   12234 
   12235 	    case DT_MIPS_XHASH:
   12236 	      name = ".MIPS.xhash";
   12237 	      s = bfd_get_linker_section (dynobj, name);
   12238 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   12239 	      break;
   12240 
   12241 	    default:
   12242 	      swap_out_p = false;
   12243 	      if (htab->root.target_os == is_vxworks
   12244 		  && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
   12245 		swap_out_p = true;
   12246 	      break;
   12247 	    }
   12248 
   12249 	  if (swap_out_p || dyn_skipped)
   12250 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
   12251 	      (dynobj, &dyn, b - dyn_skipped);
   12252 
   12253 	  if (dyn_to_skip)
   12254 	    {
   12255 	      dyn_skipped += dyn_to_skip;
   12256 	      dyn_to_skip = 0;
   12257 	    }
   12258 	}
   12259 
   12260       /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
   12261       if (dyn_skipped > 0)
   12262 	memset (b - dyn_skipped, 0, dyn_skipped);
   12263     }
   12264 
   12265   if (sgot != NULL && sgot->size > 0
   12266       && !bfd_is_abs_section (sgot->output_section))
   12267     {
   12268       if (htab->root.target_os == is_vxworks)
   12269 	{
   12270 	  /* The first entry of the global offset table points to the
   12271 	     ".dynamic" section.  The second is initialized by the
   12272 	     loader and contains the shared library identifier.
   12273 	     The third is also initialized by the loader and points
   12274 	     to the lazy resolution stub.  */
   12275 	  MIPS_ELF_PUT_WORD (output_bfd,
   12276 			     sdyn->output_offset + sdyn->output_section->vma,
   12277 			     sgot->contents);
   12278 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
   12279 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
   12280 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
   12281 			     sgot->contents
   12282 			     + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
   12283 	}
   12284       else
   12285 	{
   12286 	  /* The first entry of the global offset table will be filled at
   12287 	     runtime. The second entry will be used by some runtime loaders.
   12288 	     This isn't the case of IRIX rld.  */
   12289 	  MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
   12290 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
   12291 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
   12292 	}
   12293 
   12294       elf_section_data (sgot->output_section)->this_hdr.sh_entsize
   12295 	 = MIPS_ELF_GOT_SIZE (output_bfd);
   12296     }
   12297 
   12298   /* Generate dynamic relocations for the non-primary gots.  */
   12299   if (gg != NULL && gg->next)
   12300     {
   12301       Elf_Internal_Rela rel[3];
   12302       bfd_vma addend = 0;
   12303 
   12304       memset (rel, 0, sizeof (rel));
   12305       rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
   12306 
   12307       for (g = gg->next; g->next != gg; g = g->next)
   12308 	{
   12309 	  bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
   12310 	    + g->next->tls_gotno;
   12311 
   12312 	  MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
   12313 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
   12314 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
   12315 			     sgot->contents
   12316 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
   12317 
   12318 	  if (! bfd_link_pic (info))
   12319 	    continue;
   12320 
   12321 	  for (; got_index < g->local_gotno; got_index++)
   12322 	    {
   12323 	      if (got_index >= g->assigned_low_gotno
   12324 		  && got_index <= g->assigned_high_gotno)
   12325 		continue;
   12326 
   12327 	      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
   12328 		= got_index * MIPS_ELF_GOT_SIZE (output_bfd);
   12329 	      if (!(mips_elf_create_dynamic_relocation
   12330 		    (output_bfd, info, rel, NULL,
   12331 		     bfd_abs_section_ptr,
   12332 		     0, &addend, sgot)))
   12333 		return false;
   12334 	      BFD_ASSERT (addend == 0);
   12335 	    }
   12336 	}
   12337     }
   12338 
   12339   /* The generation of dynamic relocations for the non-primary gots
   12340      adds more dynamic relocations.  We cannot count them until
   12341      here.  */
   12342 
   12343   if (elf_hash_table (info)->dynamic_sections_created)
   12344     {
   12345       bfd_byte *b;
   12346       bool swap_out_p;
   12347 
   12348       BFD_ASSERT (sdyn != NULL);
   12349 
   12350       for (b = sdyn->contents;
   12351 	   b < sdyn->contents + sdyn->size;
   12352 	   b += MIPS_ELF_DYN_SIZE (dynobj))
   12353 	{
   12354 	  Elf_Internal_Dyn dyn;
   12355 	  asection *s;
   12356 
   12357 	  /* Read in the current dynamic entry.  */
   12358 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
   12359 
   12360 	  /* Assume that we're going to modify it and write it out.  */
   12361 	  swap_out_p = true;
   12362 
   12363 	  switch (dyn.d_tag)
   12364 	    {
   12365 	    case DT_RELSZ:
   12366 	      /* Reduce DT_RELSZ to account for any relocations we
   12367 		 decided not to make.  This is for the n64 irix rld,
   12368 		 which doesn't seem to apply any relocations if there
   12369 		 are trailing null entries.  */
   12370 	      s = mips_elf_rel_dyn_section (info, false);
   12371 	      dyn.d_un.d_val = (s->reloc_count
   12372 				* (ABI_64_P (output_bfd)
   12373 				   ? sizeof (Elf64_Mips_External_Rel)
   12374 				   : sizeof (Elf32_External_Rel)));
   12375 	      /* Adjust the section size too.  Tools like the prelinker
   12376 		 can reasonably expect the values to the same.  */
   12377 	      BFD_ASSERT (!bfd_is_abs_section (s->output_section));
   12378 	      elf_section_data (s->output_section)->this_hdr.sh_size
   12379 		= dyn.d_un.d_val;
   12380 	      break;
   12381 
   12382 	    default:
   12383 	      swap_out_p = false;
   12384 	      break;
   12385 	    }
   12386 
   12387 	  if (swap_out_p)
   12388 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
   12389 	      (dynobj, &dyn, b);
   12390 	}
   12391     }
   12392 
   12393   {
   12394     asection *s;
   12395     Elf32_compact_rel cpt;
   12396 
   12397     if (SGI_COMPAT (output_bfd))
   12398       {
   12399 	/* Write .compact_rel section out.  */
   12400 	s = bfd_get_linker_section (dynobj, ".compact_rel");
   12401 	if (s != NULL)
   12402 	  {
   12403 	    cpt.id1 = 1;
   12404 	    cpt.num = s->reloc_count;
   12405 	    cpt.id2 = 2;
   12406 	    cpt.offset = (s->output_section->filepos
   12407 			  + sizeof (Elf32_External_compact_rel));
   12408 	    cpt.reserved0 = 0;
   12409 	    cpt.reserved1 = 0;
   12410 	    bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
   12411 					    ((Elf32_External_compact_rel *)
   12412 					     s->contents));
   12413 
   12414 	    /* Clean up a dummy stub function entry in .text.  */
   12415 	    if (htab->sstubs != NULL
   12416 		&& htab->sstubs->contents != NULL)
   12417 	      {
   12418 		file_ptr dummy_offset;
   12419 
   12420 		BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
   12421 		dummy_offset = htab->sstubs->size - htab->function_stub_size;
   12422 		memset (htab->sstubs->contents + dummy_offset, 0,
   12423 			htab->function_stub_size);
   12424 	      }
   12425 	  }
   12426       }
   12427 
   12428     /* The psABI says that the dynamic relocations must be sorted in
   12429        increasing order of r_symndx.  The VxWorks EABI doesn't require
   12430        this, and because the code below handles REL rather than RELA
   12431        relocations, using it for VxWorks would be outright harmful.  */
   12432     if (htab->root.target_os != is_vxworks)
   12433       {
   12434 	s = mips_elf_rel_dyn_section (info, false);
   12435 	if (s != NULL
   12436 	    && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
   12437 	  {
   12438 	    reldyn_sorting_bfd = output_bfd;
   12439 
   12440 	    if (ABI_64_P (output_bfd))
   12441 	      qsort ((Elf64_External_Rel *) s->contents + 1,
   12442 		     s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
   12443 		     sort_dynamic_relocs_64);
   12444 	    else
   12445 	      qsort ((Elf32_External_Rel *) s->contents + 1,
   12446 		     s->reloc_count - 1, sizeof (Elf32_External_Rel),
   12447 		     sort_dynamic_relocs);
   12448 	  }
   12449       }
   12450   }
   12451 
   12452   if (htab->root.splt && htab->root.splt->size > 0)
   12453     {
   12454       if (htab->root.target_os == is_vxworks)
   12455 	{
   12456 	  if (bfd_link_pic (info))
   12457 	    mips_vxworks_finish_shared_plt (output_bfd, info);
   12458 	  else
   12459 	    mips_vxworks_finish_exec_plt (output_bfd, info);
   12460 	}
   12461       else
   12462 	{
   12463 	  BFD_ASSERT (!bfd_link_pic (info));
   12464 	  if (!mips_finish_exec_plt (output_bfd, info))
   12465 	    return false;
   12466 	}
   12467     }
   12468   return true;
   12469 }
   12470 
   12471 
   12472 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
   12473 
   12474 static void
   12475 mips_set_isa_flags (bfd *abfd)
   12476 {
   12477   flagword val;
   12478 
   12479   switch (bfd_get_mach (abfd))
   12480     {
   12481     default:
   12482       if (ABI_N32_P (abfd) || ABI_64_P (abfd))
   12483         val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_64R6 : EF_MIPS_ARCH_3;
   12484       else
   12485         val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_32R6 : EF_MIPS_ARCH_1;
   12486       break;
   12487 
   12488     case bfd_mach_mips3000:
   12489       val = EF_MIPS_ARCH_1;
   12490       break;
   12491 
   12492     case bfd_mach_mips3900:
   12493       val = EF_MIPS_ARCH_1 | EF_MIPS_MACH_3900;
   12494       break;
   12495 
   12496     case bfd_mach_mips6000:
   12497       val = EF_MIPS_ARCH_2;
   12498       break;
   12499 
   12500     case bfd_mach_mips4010:
   12501       val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_4010;
   12502       break;
   12503 
   12504     case bfd_mach_mips_allegrex:
   12505       val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_ALLEGREX;
   12506       break;
   12507 
   12508     case bfd_mach_mips4000:
   12509     case bfd_mach_mips4300:
   12510     case bfd_mach_mips4400:
   12511     case bfd_mach_mips4600:
   12512       val = EF_MIPS_ARCH_3;
   12513       break;
   12514 
   12515     case bfd_mach_mips4100:
   12516       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4100;
   12517       break;
   12518 
   12519     case bfd_mach_mips4111:
   12520       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4111;
   12521       break;
   12522 
   12523     case bfd_mach_mips4120:
   12524       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4120;
   12525       break;
   12526 
   12527     case bfd_mach_mips4650:
   12528       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4650;
   12529       break;
   12530 
   12531     case bfd_mach_mips5400:
   12532       val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5400;
   12533       break;
   12534 
   12535     case bfd_mach_mips5500:
   12536       val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5500;
   12537       break;
   12538 
   12539     case bfd_mach_mips5900:
   12540       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_5900;
   12541       break;
   12542 
   12543     case bfd_mach_mips9000:
   12544       val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_9000;
   12545       break;
   12546 
   12547     case bfd_mach_mips5000:
   12548     case bfd_mach_mips7000:
   12549     case bfd_mach_mips8000:
   12550     case bfd_mach_mips10000:
   12551     case bfd_mach_mips12000:
   12552     case bfd_mach_mips14000:
   12553     case bfd_mach_mips16000:
   12554       val = EF_MIPS_ARCH_4;
   12555       break;
   12556 
   12557     case bfd_mach_mips5:
   12558       val = EF_MIPS_ARCH_5;
   12559       break;
   12560 
   12561     case bfd_mach_mips_loongson_2e:
   12562       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2E;
   12563       break;
   12564 
   12565     case bfd_mach_mips_loongson_2f:
   12566       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2F;
   12567       break;
   12568 
   12569     case bfd_mach_mips_sb1:
   12570       val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_SB1;
   12571       break;
   12572 
   12573     case bfd_mach_mips_gs464:
   12574       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464;
   12575       break;
   12576 
   12577     case bfd_mach_mips_gs464e:
   12578       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464E;
   12579       break;
   12580 
   12581     case bfd_mach_mips_gs264e:
   12582       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS264E;
   12583       break;
   12584 
   12585     case bfd_mach_mips_octeon:
   12586     case bfd_mach_mips_octeonp:
   12587       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON;
   12588       break;
   12589 
   12590     case bfd_mach_mips_octeon3:
   12591       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON3;
   12592       break;
   12593 
   12594     case bfd_mach_mips_xlr:
   12595       val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_XLR;
   12596       break;
   12597 
   12598     case bfd_mach_mips_octeon2:
   12599       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON2;
   12600       break;
   12601 
   12602     case bfd_mach_mipsisa32:
   12603       val = EF_MIPS_ARCH_32;
   12604       break;
   12605 
   12606     case bfd_mach_mipsisa64:
   12607       val = EF_MIPS_ARCH_64;
   12608       break;
   12609 
   12610     case bfd_mach_mipsisa32r2:
   12611     case bfd_mach_mipsisa32r3:
   12612     case bfd_mach_mipsisa32r5:
   12613       val = EF_MIPS_ARCH_32R2;
   12614       break;
   12615 
   12616     case bfd_mach_mips_interaptiv_mr2:
   12617       val = EF_MIPS_ARCH_32R2 | EF_MIPS_MACH_IAMR2;
   12618       break;
   12619 
   12620     case bfd_mach_mipsisa64r2:
   12621     case bfd_mach_mipsisa64r3:
   12622     case bfd_mach_mipsisa64r5:
   12623       val = EF_MIPS_ARCH_64R2;
   12624       break;
   12625 
   12626     case bfd_mach_mipsisa32r6:
   12627       val = EF_MIPS_ARCH_32R6;
   12628       break;
   12629 
   12630     case bfd_mach_mipsisa64r6:
   12631       val = EF_MIPS_ARCH_64R6;
   12632       break;
   12633     }
   12634   elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
   12635   elf_elfheader (abfd)->e_flags |= val;
   12636 
   12637 }
   12638 
   12639 
   12640 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
   12641    Don't do so for code sections.  We want to keep ordering of HI16/LO16
   12642    as is.  On the other hand, elf-eh-frame.c processing requires .eh_frame
   12643    relocs to be sorted.  */
   12644 
   12645 bool
   12646 _bfd_mips_elf_sort_relocs_p (asection *sec)
   12647 {
   12648   return (sec->flags & SEC_CODE) == 0;
   12649 }
   12650 
   12651 
   12652 /* The final processing done just before writing out a MIPS ELF object
   12653    file.  This gets the MIPS architecture right based on the machine
   12654    number.  This is used by both the 32-bit and the 64-bit ABI.  */
   12655 
   12656 void
   12657 _bfd_mips_final_write_processing (bfd *abfd)
   12658 {
   12659   unsigned int i;
   12660   Elf_Internal_Shdr **hdrpp;
   12661   const char *name;
   12662   asection *sec;
   12663 
   12664   /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
   12665      is nonzero.  This is for compatibility with old objects, which used
   12666      a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
   12667   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
   12668     mips_set_isa_flags (abfd);
   12669 
   12670   /* Set the sh_info field for .gptab sections and other appropriate
   12671      info for each special section.  */
   12672   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
   12673        i < elf_numsections (abfd);
   12674        i++, hdrpp++)
   12675     {
   12676       switch ((*hdrpp)->sh_type)
   12677 	{
   12678 	case SHT_MIPS_MSYM:
   12679 	case SHT_MIPS_LIBLIST:
   12680 	  sec = bfd_get_section_by_name (abfd, ".dynstr");
   12681 	  if (sec != NULL)
   12682 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12683 	  break;
   12684 
   12685 	case SHT_MIPS_GPTAB:
   12686 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12687 	  name = bfd_section_name ((*hdrpp)->bfd_section);
   12688 	  if (startswith (name, ".gptab."))
   12689 	    {
   12690 	      sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
   12691 	      if (sec != NULL)
   12692 		(*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
   12693 	    }
   12694 	  break;
   12695 
   12696 	case SHT_MIPS_CONTENT:
   12697 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12698 	  name = bfd_section_name ((*hdrpp)->bfd_section);
   12699 	  if (startswith (name, ".MIPS.content"))
   12700 	    {
   12701 	      sec = bfd_get_section_by_name (abfd,
   12702 					     name + sizeof ".MIPS.content" - 1);
   12703 	      if (sec != NULL)
   12704 		(*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12705 	    }
   12706 	  break;
   12707 
   12708 	case SHT_MIPS_SYMBOL_LIB:
   12709 	  sec = bfd_get_section_by_name (abfd, ".dynsym");
   12710 	  if (sec != NULL)
   12711 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12712 	  sec = bfd_get_section_by_name (abfd, ".liblist");
   12713 	  if (sec != NULL)
   12714 	    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
   12715 	  break;
   12716 
   12717 	case SHT_MIPS_EVENTS:
   12718 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12719 	  name = bfd_section_name ((*hdrpp)->bfd_section);
   12720 	  if (startswith (name, ".MIPS.events"))
   12721 	    sec = bfd_get_section_by_name (abfd,
   12722 					   name + sizeof ".MIPS.events" - 1);
   12723 	  else if (startswith (name, ".MIPS.post_rel"))
   12724 	    sec = bfd_get_section_by_name (abfd,
   12725 					   name + sizeof ".MIPS.post_rel" - 1);
   12726 	  else
   12727 	    sec = NULL;
   12728 	  if (sec != NULL)
   12729 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12730 	  break;
   12731 
   12732 	case SHT_MIPS_XHASH:
   12733 	  sec = bfd_get_section_by_name (abfd, ".dynsym");
   12734 	  if (sec != NULL)
   12735 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12736 	}
   12737     }
   12738 }
   12739 
   12740 bool
   12741 _bfd_mips_elf_final_write_processing (bfd *abfd)
   12742 {
   12743   _bfd_mips_final_write_processing (abfd);
   12744   return _bfd_elf_final_write_processing (abfd);
   12745 }
   12746 
   12747 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
   12749    segments.  */
   12750 
   12751 int
   12752 _bfd_mips_elf_additional_program_headers (bfd *abfd,
   12753 					  struct bfd_link_info *info ATTRIBUTE_UNUSED)
   12754 {
   12755   asection *s;
   12756   int ret = 0;
   12757 
   12758   /* See if we need a PT_MIPS_REGINFO segment.  */
   12759   s = bfd_get_section_by_name (abfd, ".reginfo");
   12760   if (s && (s->flags & SEC_LOAD))
   12761     ++ret;
   12762 
   12763   /* See if we need a PT_MIPS_ABIFLAGS segment.  */
   12764   if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
   12765     ++ret;
   12766 
   12767   /* See if we need a PT_MIPS_OPTIONS segment.  */
   12768   if (IRIX_COMPAT (abfd) == ict_irix6
   12769       && bfd_get_section_by_name (abfd,
   12770 				  MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
   12771     ++ret;
   12772 
   12773   /* See if we need a PT_MIPS_RTPROC segment.  */
   12774   if (IRIX_COMPAT (abfd) == ict_irix5
   12775       && bfd_get_section_by_name (abfd, ".dynamic")
   12776       && bfd_get_section_by_name (abfd, ".mdebug"))
   12777     ++ret;
   12778 
   12779   /* Allocate a PT_NULL header in dynamic objects.  See
   12780      _bfd_mips_elf_modify_segment_map for details.  */
   12781   if (!SGI_COMPAT (abfd)
   12782       && bfd_get_section_by_name (abfd, ".dynamic"))
   12783     ++ret;
   12784 
   12785   return ret;
   12786 }
   12787 
   12788 /* Modify the segment map for an IRIX5 executable.  */
   12789 
   12790 bool
   12791 _bfd_mips_elf_modify_segment_map (bfd *abfd,
   12792 				  struct bfd_link_info *info)
   12793 {
   12794   asection *s;
   12795   struct elf_segment_map *m, **pm;
   12796   size_t amt;
   12797 
   12798   /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
   12799      segment.  */
   12800   s = bfd_get_section_by_name (abfd, ".reginfo");
   12801   if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12802     {
   12803       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12804 	if (m->p_type == PT_MIPS_REGINFO)
   12805 	  break;
   12806       if (m == NULL)
   12807 	{
   12808 	  amt = sizeof *m;
   12809 	  m = bfd_zalloc (abfd, amt);
   12810 	  if (m == NULL)
   12811 	    return false;
   12812 
   12813 	  m->p_type = PT_MIPS_REGINFO;
   12814 	  m->count = 1;
   12815 	  m->sections[0] = s;
   12816 
   12817 	  /* We want to put it after the PHDR and INTERP segments.  */
   12818 	  pm = &elf_seg_map (abfd);
   12819 	  while (*pm != NULL
   12820 		 && ((*pm)->p_type == PT_PHDR
   12821 		     || (*pm)->p_type == PT_INTERP))
   12822 	    pm = &(*pm)->next;
   12823 
   12824 	  m->next = *pm;
   12825 	  *pm = m;
   12826 	}
   12827     }
   12828 
   12829   /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
   12830      segment.  */
   12831   s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
   12832   if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12833     {
   12834       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12835 	if (m->p_type == PT_MIPS_ABIFLAGS)
   12836 	  break;
   12837       if (m == NULL)
   12838 	{
   12839 	  amt = sizeof *m;
   12840 	  m = bfd_zalloc (abfd, amt);
   12841 	  if (m == NULL)
   12842 	    return false;
   12843 
   12844 	  m->p_type = PT_MIPS_ABIFLAGS;
   12845 	  m->count = 1;
   12846 	  m->sections[0] = s;
   12847 
   12848 	  /* We want to put it after the PHDR and INTERP segments.  */
   12849 	  pm = &elf_seg_map (abfd);
   12850 	  while (*pm != NULL
   12851 		 && ((*pm)->p_type == PT_PHDR
   12852 		     || (*pm)->p_type == PT_INTERP))
   12853 	    pm = &(*pm)->next;
   12854 
   12855 	  m->next = *pm;
   12856 	  *pm = m;
   12857 	}
   12858     }
   12859 
   12860   /* For IRIX 6, we don't have .mdebug sections, nor does anything but
   12861      .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
   12862      PT_MIPS_OPTIONS segment immediately following the program header
   12863      table.  */
   12864   if (NEWABI_P (abfd)
   12865       /* On non-IRIX6 new abi, we'll have already created a segment
   12866 	 for this section, so don't create another.  I'm not sure this
   12867 	 is not also the case for IRIX 6, but I can't test it right
   12868 	 now.  */
   12869       && IRIX_COMPAT (abfd) == ict_irix6)
   12870     {
   12871       for (s = abfd->sections; s; s = s->next)
   12872 	if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
   12873 	  break;
   12874 
   12875       if (s)
   12876 	{
   12877 	  struct elf_segment_map *options_segment;
   12878 
   12879 	  pm = &elf_seg_map (abfd);
   12880 	  while (*pm != NULL
   12881 		 && ((*pm)->p_type == PT_PHDR
   12882 		     || (*pm)->p_type == PT_INTERP))
   12883 	    pm = &(*pm)->next;
   12884 
   12885 	  if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
   12886 	    {
   12887 	      amt = sizeof (struct elf_segment_map);
   12888 	      options_segment = bfd_zalloc (abfd, amt);
   12889 	      options_segment->next = *pm;
   12890 	      options_segment->p_type = PT_MIPS_OPTIONS;
   12891 	      options_segment->p_flags = PF_R;
   12892 	      options_segment->p_flags_valid = true;
   12893 	      options_segment->count = 1;
   12894 	      options_segment->sections[0] = s;
   12895 	      *pm = options_segment;
   12896 	    }
   12897 	}
   12898     }
   12899   else
   12900     {
   12901       if (IRIX_COMPAT (abfd) == ict_irix5)
   12902 	{
   12903 	  /* If there are .dynamic and .mdebug sections, we make a room
   12904 	     for the RTPROC header.  FIXME: Rewrite without section names.  */
   12905 	  if (bfd_get_section_by_name (abfd, ".interp") == NULL
   12906 	      && bfd_get_section_by_name (abfd, ".dynamic") != NULL
   12907 	      && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
   12908 	    {
   12909 	      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12910 		if (m->p_type == PT_MIPS_RTPROC)
   12911 		  break;
   12912 	      if (m == NULL)
   12913 		{
   12914 		  amt = sizeof *m;
   12915 		  m = bfd_zalloc (abfd, amt);
   12916 		  if (m == NULL)
   12917 		    return false;
   12918 
   12919 		  m->p_type = PT_MIPS_RTPROC;
   12920 
   12921 		  s = bfd_get_section_by_name (abfd, ".rtproc");
   12922 		  if (s == NULL)
   12923 		    {
   12924 		      m->count = 0;
   12925 		      m->p_flags = 0;
   12926 		      m->p_flags_valid = 1;
   12927 		    }
   12928 		  else
   12929 		    {
   12930 		      m->count = 1;
   12931 		      m->sections[0] = s;
   12932 		    }
   12933 
   12934 		  /* We want to put it after the DYNAMIC segment.  */
   12935 		  pm = &elf_seg_map (abfd);
   12936 		  while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
   12937 		    pm = &(*pm)->next;
   12938 		  if (*pm != NULL)
   12939 		    pm = &(*pm)->next;
   12940 
   12941 		  m->next = *pm;
   12942 		  *pm = m;
   12943 		}
   12944 	    }
   12945 	}
   12946       /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
   12947 	 .dynstr, .dynsym, and .hash sections, and everything in
   12948 	 between.  */
   12949       for (pm = &elf_seg_map (abfd); *pm != NULL;
   12950 	   pm = &(*pm)->next)
   12951 	if ((*pm)->p_type == PT_DYNAMIC)
   12952 	  break;
   12953       m = *pm;
   12954       /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
   12955 	 glibc's dynamic linker has traditionally derived the number of
   12956 	 tags from the p_filesz field, and sometimes allocates stack
   12957 	 arrays of that size.  An overly-big PT_DYNAMIC segment can
   12958 	 be actively harmful in such cases.  Making PT_DYNAMIC contain
   12959 	 other sections can also make life hard for the prelinker,
   12960 	 which might move one of the other sections to a different
   12961 	 PT_LOAD segment.  */
   12962       if (SGI_COMPAT (abfd)
   12963 	  && m != NULL
   12964 	  && m->count == 1
   12965 	  && strcmp (m->sections[0]->name, ".dynamic") == 0)
   12966 	{
   12967 	  static const char *sec_names[] =
   12968 	  {
   12969 	    ".dynamic", ".dynstr", ".dynsym", ".hash"
   12970 	  };
   12971 	  bfd_vma low, high;
   12972 	  unsigned int i, c;
   12973 	  struct elf_segment_map *n;
   12974 
   12975 	  low = ~(bfd_vma) 0;
   12976 	  high = 0;
   12977 	  for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
   12978 	    {
   12979 	      s = bfd_get_section_by_name (abfd, sec_names[i]);
   12980 	      if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12981 		{
   12982 		  bfd_size_type sz;
   12983 
   12984 		  if (low > s->vma)
   12985 		    low = s->vma;
   12986 		  sz = s->size;
   12987 		  if (high < s->vma + sz)
   12988 		    high = s->vma + sz;
   12989 		}
   12990 	    }
   12991 
   12992 	  c = 0;
   12993 	  for (s = abfd->sections; s != NULL; s = s->next)
   12994 	    if ((s->flags & SEC_LOAD) != 0
   12995 		&& s->vma >= low
   12996 		&& s->vma + s->size <= high)
   12997 	      ++c;
   12998 
   12999 	  amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
   13000 	  n = bfd_zalloc (abfd, amt);
   13001 	  if (n == NULL)
   13002 	    return false;
   13003 	  *n = *m;
   13004 	  n->count = c;
   13005 
   13006 	  i = 0;
   13007 	  for (s = abfd->sections; s != NULL; s = s->next)
   13008 	    {
   13009 	      if ((s->flags & SEC_LOAD) != 0
   13010 		  && s->vma >= low
   13011 		  && s->vma + s->size <= high)
   13012 		{
   13013 		  n->sections[i] = s;
   13014 		  ++i;
   13015 		}
   13016 	    }
   13017 
   13018 	  *pm = n;
   13019 	}
   13020     }
   13021 
   13022   /* Allocate a spare program header in dynamic objects so that tools
   13023      like the prelinker can add an extra PT_LOAD entry.
   13024 
   13025      If the prelinker needs to make room for a new PT_LOAD entry, its
   13026      standard procedure is to move the first (read-only) sections into
   13027      the new (writable) segment.  However, the MIPS ABI requires
   13028      .dynamic to be in a read-only segment, and the section will often
   13029      start within sizeof (ElfNN_Phdr) bytes of the last program header.
   13030 
   13031      Although the prelinker could in principle move .dynamic to a
   13032      writable segment, it seems better to allocate a spare program
   13033      header instead, and avoid the need to move any sections.
   13034      There is a long tradition of allocating spare dynamic tags,
   13035      so allocating a spare program header seems like a natural
   13036      extension.
   13037 
   13038      If INFO is NULL, we may be copying an already prelinked binary
   13039      with objcopy or strip, so do not add this header.  */
   13040   if (info != NULL
   13041       && !SGI_COMPAT (abfd)
   13042       && bfd_get_section_by_name (abfd, ".dynamic"))
   13043     {
   13044       for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
   13045 	if ((*pm)->p_type == PT_NULL)
   13046 	  break;
   13047       if (*pm == NULL)
   13048 	{
   13049 	  m = bfd_zalloc (abfd, sizeof (*m));
   13050 	  if (m == NULL)
   13051 	    return false;
   13052 
   13053 	  m->p_type = PT_NULL;
   13054 	  *pm = m;
   13055 	}
   13056     }
   13057 
   13058   return true;
   13059 }
   13060 
   13061 /* Return the section that should be marked against GC for a given
   13063    relocation.  */
   13064 
   13065 asection *
   13066 _bfd_mips_elf_gc_mark_hook (asection *sec,
   13067 			    struct bfd_link_info *info,
   13068 			    struct elf_reloc_cookie *cookie,
   13069 			    struct elf_link_hash_entry *h,
   13070 			    unsigned int symndx)
   13071 {
   13072   /* ??? Do mips16 stub sections need to be handled special?  */
   13073 
   13074   if (h != NULL)
   13075     switch (ELF_R_TYPE (sec->owner, cookie->rel->r_info))
   13076       {
   13077       case R_MIPS_GNU_VTINHERIT:
   13078       case R_MIPS_GNU_VTENTRY:
   13079 	return NULL;
   13080       }
   13081 
   13082   return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
   13083 }
   13084 
   13085 /* Prevent .MIPS.abiflags from being discarded with --gc-sections.  */
   13086 
   13087 bool
   13088 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
   13089 				      elf_gc_mark_hook_fn gc_mark_hook)
   13090 {
   13091   bfd *sub;
   13092 
   13093   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
   13094 
   13095   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   13096     {
   13097       asection *o;
   13098 
   13099       if (! is_mips_elf (sub))
   13100 	continue;
   13101 
   13102       for (o = sub->sections; o != NULL; o = o->next)
   13103 	if (!o->gc_mark
   13104 	    && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
   13105 	  {
   13106 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   13107 	      return false;
   13108 	  }
   13109     }
   13110 
   13111   return true;
   13112 }
   13113 
   13114 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
   13116    hiding the old indirect symbol.  Process additional relocation
   13117    information.  Also called for weakdefs, in which case we just let
   13118    _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
   13119 
   13120 void
   13121 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
   13122 				    struct elf_link_hash_entry *dir,
   13123 				    struct elf_link_hash_entry *ind)
   13124 {
   13125   struct mips_elf_link_hash_entry *dirmips, *indmips;
   13126 
   13127   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   13128 
   13129   dirmips = (struct mips_elf_link_hash_entry *) dir;
   13130   indmips = (struct mips_elf_link_hash_entry *) ind;
   13131   /* Any absolute non-dynamic relocations against an indirect or weak
   13132      definition will be against the target symbol.  */
   13133   if (indmips->has_static_relocs)
   13134     dirmips->has_static_relocs = true;
   13135 
   13136   if (ind->root.type != bfd_link_hash_indirect)
   13137     return;
   13138 
   13139   dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
   13140   if (indmips->readonly_reloc)
   13141     dirmips->readonly_reloc = true;
   13142   if (indmips->no_fn_stub)
   13143     dirmips->no_fn_stub = true;
   13144   if (indmips->fn_stub)
   13145     {
   13146       dirmips->fn_stub = indmips->fn_stub;
   13147       indmips->fn_stub = NULL;
   13148     }
   13149   if (indmips->need_fn_stub)
   13150     {
   13151       dirmips->need_fn_stub = true;
   13152       indmips->need_fn_stub = false;
   13153     }
   13154   if (indmips->call_stub)
   13155     {
   13156       dirmips->call_stub = indmips->call_stub;
   13157       indmips->call_stub = NULL;
   13158     }
   13159   if (indmips->call_fp_stub)
   13160     {
   13161       dirmips->call_fp_stub = indmips->call_fp_stub;
   13162       indmips->call_fp_stub = NULL;
   13163     }
   13164   if (indmips->global_got_area < dirmips->global_got_area)
   13165     dirmips->global_got_area = indmips->global_got_area;
   13166   if (indmips->global_got_area < GGA_NONE)
   13167     indmips->global_got_area = GGA_NONE;
   13168   if (indmips->has_nonpic_branches)
   13169     dirmips->has_nonpic_branches = true;
   13170 }
   13171 
   13172 /* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
   13173    to hide it.  It has to remain global (it will also be protected) so as to
   13174    be assigned a global GOT entry, which will then remain unchanged at load
   13175    time.  */
   13176 
   13177 void
   13178 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
   13179 			   struct elf_link_hash_entry *entry,
   13180 			   bool force_local)
   13181 {
   13182   struct mips_elf_link_hash_table *htab;
   13183 
   13184   htab = mips_elf_hash_table (info);
   13185   BFD_ASSERT (htab != NULL);
   13186   if (htab->use_absolute_zero
   13187       && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
   13188     return;
   13189 
   13190   _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
   13191 }
   13192 
   13193 #define PDR_SIZE 32
   13195 
   13196 bool
   13197 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
   13198 			    struct bfd_link_info *info)
   13199 {
   13200   asection *o;
   13201   bool ret = false;
   13202   unsigned char *tdata;
   13203   size_t i, skip;
   13204 
   13205   o = bfd_get_section_by_name (abfd, ".pdr");
   13206   if (! o)
   13207     return false;
   13208   if (o->size == 0)
   13209     return false;
   13210   if (o->size % PDR_SIZE != 0)
   13211     return false;
   13212   if (o->output_section != NULL
   13213       && bfd_is_abs_section (o->output_section))
   13214     return false;
   13215 
   13216   tdata = bfd_zmalloc (o->size / PDR_SIZE);
   13217   if (! tdata)
   13218     return false;
   13219 
   13220   cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   13221 					    info->keep_memory);
   13222   if (!cookie->rels)
   13223     {
   13224       free (tdata);
   13225       return false;
   13226     }
   13227 
   13228   cookie->rel = cookie->rels;
   13229   cookie->relend = cookie->rels + o->reloc_count;
   13230 
   13231   for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
   13232     {
   13233       if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
   13234 	{
   13235 	  tdata[i] = 1;
   13236 	  skip ++;
   13237 	}
   13238     }
   13239 
   13240   if (skip != 0)
   13241     {
   13242       mips_elf_section_data (o)->u.tdata = tdata;
   13243       if (o->rawsize == 0)
   13244 	o->rawsize = o->size;
   13245       o->size -= skip * PDR_SIZE;
   13246       ret = true;
   13247     }
   13248   else
   13249     free (tdata);
   13250 
   13251   if (! info->keep_memory)
   13252     free (cookie->rels);
   13253 
   13254   return ret;
   13255 }
   13256 
   13257 bool
   13258 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
   13259 {
   13260   if (strcmp (sec->name, ".pdr") == 0)
   13261     return true;
   13262   return false;
   13263 }
   13264 
   13265 bool
   13266 _bfd_mips_elf_write_section (bfd *output_bfd,
   13267 			     struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   13268 			     asection *sec, bfd_byte *contents)
   13269 {
   13270   bfd_byte *to, *from, *end;
   13271   int i;
   13272 
   13273   if (strcmp (sec->name, ".pdr") != 0)
   13274     return false;
   13275 
   13276   if (mips_elf_section_data (sec)->u.tdata == NULL)
   13277     return false;
   13278 
   13279   to = contents;
   13280   end = contents + sec->size;
   13281   for (from = contents, i = 0;
   13282        from < end;
   13283        from += PDR_SIZE, i++)
   13284     {
   13285       if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
   13286 	continue;
   13287       if (to != from)
   13288 	memcpy (to, from, PDR_SIZE);
   13289       to += PDR_SIZE;
   13290     }
   13291   bfd_set_section_contents (output_bfd, sec->output_section, contents,
   13292 			    sec->output_offset, sec->size);
   13293   return true;
   13294 }
   13295 
   13296 /* microMIPS code retains local labels for linker relaxation.  Omit them
   13298    from output by default for clarity.  */
   13299 
   13300 bool
   13301 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
   13302 {
   13303   return _bfd_elf_is_local_label_name (abfd, sym->name);
   13304 }
   13305 
   13306 /* Helper for `mips_elf_free_hi16_list'.  Resolve an orphan REL high-part
   13307    relocation according to HI.  Return TRUE if succeeded, otherwise FALSE.  */
   13308 
   13309 static bool
   13310 _bfd_mips_elf_orphan_shr16_reloc (bfd *abfd, asection *sec,
   13311 				  struct mips_hi16 *hi,
   13312 				  struct bfd_link_info *info)
   13313 {
   13314   asymbol *symbol = *hi->rel.sym_ptr_ptr;
   13315   const char *name = hi->rel.howto->name;
   13316   char *error_message = NULL;
   13317   bfd_reloc_status_type r;
   13318 
   13319   r = _bfd_mips_elf_shr16_reloc (abfd, &hi->rel, symbol, hi->data,
   13320 				 sec, hi->output_bfd, &error_message);
   13321   if (r == bfd_reloc_ok)
   13322     {
   13323       if (info != NULL)
   13324 	{
   13325 	  /* xgettext:c-format */
   13326 	  error_message = bfd_asprintf (_("can't find matching LO16 reloc"
   13327 					  " against `%s' for %s at %#" PRIx64
   13328 					  " in section `%s'"),
   13329 					symbol->name, name,
   13330 					(uint64_t) hi->rel.address, sec->name);
   13331 	  if (error_message == NULL)
   13332 	    return false;
   13333 	  info->callbacks->warning
   13334 	    (info, error_message, symbol->name, sec->owner, sec,
   13335 	     hi->rel.address);
   13336 	}
   13337       return true;
   13338     }
   13339 
   13340   if (info != NULL)
   13341     _bfd_link_reloc_status_error (abfd, info, sec, &hi->rel, error_message, r);
   13342   else
   13343     _bfd_error_handler (_("%pB(%pA+%#" PRIx64 "): %s relocation error"),
   13344 			abfd, sec, (uint64_t) hi->rel.address,
   13345 			hi->rel.howto->name);
   13346   bfd_set_error (bfd_error_bad_value);
   13347   return false;
   13348 }
   13349 
   13350 /* Resolve any outstanding orphan REL high-part relocations if INSTALL
   13351    is TRUE, and release their occupied memory.  */
   13352 
   13353 static bool
   13354 mips_elf_free_hi16_list (bfd *abfd, asection *sec, bool install,
   13355 			 struct bfd_link_info *info)
   13356 {
   13357   bfd_error_type error_tag = bfd_get_error ();
   13358   struct _mips_elf_section_data *sdata;
   13359   bool status = true;
   13360 
   13361   BFD_ASSERT (is_mips_elf (abfd));
   13362   sdata = mips_elf_section_data (sec);
   13363   while (sdata->mips_hi16_list != NULL)
   13364     {
   13365       struct mips_hi16 *hi = sdata->mips_hi16_list;
   13366 
   13367       if (install)
   13368 	status &= _bfd_mips_elf_orphan_shr16_reloc (abfd, sec, hi, info);
   13369       if (!status && error_tag == bfd_error_no_error)
   13370 	error_tag = bfd_get_error ();
   13371 
   13372       sdata->mips_hi16_list = hi->next;
   13373       free (hi);
   13374     }
   13375 
   13376   bfd_set_error (error_tag);
   13377   return status;
   13378 }
   13379 
   13380 /* Resolve any outstanding orphan REL high-part relocations before
   13381    calling the generic BFD handler.  */
   13382 
   13383 bool
   13384 _bfd_mips_elf_finalize_section_relocs (bfd *abfd, asection *asect,
   13385 				       arelent **location, unsigned int count)
   13386 {
   13387   if (!mips_elf_free_hi16_list (abfd, asect, true, NULL))
   13388     return false;
   13389   return _bfd_generic_finalize_section_relocs (abfd, asect, location, count);
   13390 }
   13391 
   13392 bool
   13393 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
   13394 				 asection *section, bfd_vma offset,
   13395 				 const char **filename_ptr,
   13396 				 const char **functionname_ptr,
   13397 				 unsigned int *line_ptr,
   13398 				 unsigned int *discriminator_ptr)
   13399 {
   13400   asection *msec;
   13401 
   13402   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   13403 				     filename_ptr, functionname_ptr,
   13404 				     line_ptr, discriminator_ptr,
   13405 				     dwarf_debug_sections,
   13406 				     &elf_tdata (abfd)->dwarf2_find_line_info)
   13407       == 1)
   13408     return true;
   13409 
   13410   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   13411 				     filename_ptr, functionname_ptr,
   13412 				     line_ptr))
   13413     {
   13414       if (!*functionname_ptr)
   13415 	_bfd_elf_find_function (abfd, symbols, section, offset,
   13416 				*filename_ptr ? NULL : filename_ptr,
   13417 				functionname_ptr);
   13418       return true;
   13419     }
   13420 
   13421   msec = bfd_get_section_by_name (abfd, ".mdebug");
   13422   if (msec != NULL)
   13423     {
   13424       flagword origflags;
   13425       struct mips_elf_find_line *fi;
   13426       const struct ecoff_debug_swap * const swap =
   13427 	get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   13428 
   13429       /* If we are called during a link, mips_elf_final_link may have
   13430 	 cleared the SEC_HAS_CONTENTS field.  We force it back on here
   13431 	 if appropriate (which it normally will be).  */
   13432       origflags = msec->flags;
   13433       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
   13434 	msec->flags |= SEC_HAS_CONTENTS;
   13435 
   13436       fi = mips_elf_tdata (abfd)->find_line_info;
   13437       if (fi == NULL)
   13438 	{
   13439 	  bfd_size_type external_fdr_size;
   13440 	  char *fraw_src;
   13441 	  char *fraw_end;
   13442 	  struct fdr *fdr_ptr;
   13443 	  bfd_size_type amt = sizeof (struct mips_elf_find_line);
   13444 
   13445 	  fi = bfd_zalloc (abfd, amt);
   13446 	  if (fi == NULL)
   13447 	    {
   13448 	      msec->flags = origflags;
   13449 	      return false;
   13450 	    }
   13451 
   13452 	  if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
   13453 	    {
   13454 	      msec->flags = origflags;
   13455 	      return false;
   13456 	    }
   13457 
   13458 	  /* Swap in the FDR information.  */
   13459 	  amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
   13460 	  fi->d.fdr = bfd_alloc (abfd, amt);
   13461 	  if (fi->d.fdr == NULL)
   13462 	    {
   13463 	      _bfd_ecoff_free_ecoff_debug_info (&fi->d);
   13464 	      msec->flags = origflags;
   13465 	      return false;
   13466 	    }
   13467 	  external_fdr_size = swap->external_fdr_size;
   13468 	  fdr_ptr = fi->d.fdr;
   13469 	  fraw_src = (char *) fi->d.external_fdr;
   13470 	  fraw_end = (fraw_src
   13471 		      + fi->d.symbolic_header.ifdMax * external_fdr_size);
   13472 	  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
   13473 	    (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
   13474 
   13475 	  mips_elf_tdata (abfd)->find_line_info = fi;
   13476 	}
   13477 
   13478       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
   13479 				  &fi->i, filename_ptr, functionname_ptr,
   13480 				  line_ptr))
   13481 	{
   13482 	  msec->flags = origflags;
   13483 	  return true;
   13484 	}
   13485 
   13486       msec->flags = origflags;
   13487     }
   13488 
   13489   /* Fall back on the generic ELF find_nearest_line routine.  */
   13490 
   13491   return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
   13492 				     filename_ptr, functionname_ptr,
   13493 				     line_ptr, discriminator_ptr);
   13494 }
   13495 
   13496 bool
   13497 _bfd_mips_elf_find_inliner_info (bfd *abfd,
   13498 				 const char **filename_ptr,
   13499 				 const char **functionname_ptr,
   13500 				 unsigned int *line_ptr)
   13501 {
   13502   bool found;
   13503   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   13504 					 functionname_ptr, line_ptr,
   13505 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   13506   return found;
   13507 }
   13508 
   13509 
   13510 /* When are writing out the .options or .MIPS.options section,
   13512    remember the bytes we are writing out, so that we can install the
   13513    GP value in the section_processing routine.  */
   13514 
   13515 bool
   13516 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
   13517 				    const void *location,
   13518 				    file_ptr offset, bfd_size_type count)
   13519 {
   13520   if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
   13521     {
   13522       bfd_byte *c;
   13523 
   13524       if (elf_section_data (section) == NULL)
   13525 	{
   13526 	  size_t amt = sizeof (struct bfd_elf_section_data);
   13527 	  section->used_by_bfd = bfd_zalloc (abfd, amt);
   13528 	  if (elf_section_data (section) == NULL)
   13529 	    return false;
   13530 	}
   13531       c = mips_elf_section_data (section)->u.tdata;
   13532       if (c == NULL)
   13533 	{
   13534 	  c = bfd_zalloc (abfd, section->size);
   13535 	  if (c == NULL)
   13536 	    return false;
   13537 	  mips_elf_section_data (section)->u.tdata = c;
   13538 	}
   13539 
   13540       memcpy (c + offset, location, count);
   13541     }
   13542 
   13543   return _bfd_elf_set_section_contents (abfd, section, location, offset,
   13544 					count);
   13545 }
   13546 
   13547 /* This is almost identical to bfd_generic_get_... except that some
   13548    MIPS relocations need to be handled specially.  Sigh.  */
   13549 
   13550 bfd_byte *
   13551 _bfd_elf_mips_get_relocated_section_contents
   13552   (bfd *abfd,
   13553    struct bfd_link_info *link_info,
   13554    struct bfd_link_order *link_order,
   13555    bfd_byte *data,
   13556    bool relocatable,
   13557    asymbol **symbols)
   13558 {
   13559   bfd *input_bfd = link_order->u.indirect.section->owner;
   13560   asection *input_section = link_order->u.indirect.section;
   13561   long reloc_size;
   13562   arelent **reloc_vector;
   13563   long reloc_count = 0;
   13564   bool install = true;
   13565 
   13566   reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
   13567   if (reloc_size < 0)
   13568     return NULL;
   13569 
   13570   /* Read in the section.  */
   13571   bfd_byte *orig_data = data;
   13572   if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
   13573     return NULL;
   13574 
   13575   if (data == NULL)
   13576     return NULL;
   13577 
   13578   if (reloc_size == 0)
   13579     return data;
   13580 
   13581   reloc_vector = (arelent **) bfd_malloc (reloc_size);
   13582   if (reloc_vector != NULL)
   13583     reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
   13584 					  reloc_vector, symbols);
   13585 
   13586   if (reloc_vector == NULL || reloc_count < 0)
   13587     {
   13588       install = false;
   13589       if (orig_data == NULL)
   13590 	free (data);
   13591       data = NULL;
   13592       goto out;
   13593     }
   13594 
   13595   if (reloc_count > 0)
   13596     {
   13597       arelent **parent;
   13598       /* for mips */
   13599       int gp_found;
   13600       bfd_vma gp = 0x12345678;	/* initialize just to shut gcc up */
   13601 
   13602       {
   13603 	struct bfd_hash_entry *h;
   13604 	struct bfd_link_hash_entry *lh;
   13605 	/* Skip all this stuff if we aren't mixing formats.  */
   13606 	if (abfd && input_bfd
   13607 	    && abfd->xvec == input_bfd->xvec)
   13608 	  lh = 0;
   13609 	else
   13610 	  {
   13611 	    h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
   13612 	    lh = (struct bfd_link_hash_entry *) h;
   13613 	  }
   13614       lookup:
   13615 	if (lh)
   13616 	  {
   13617 	    switch (lh->type)
   13618 	      {
   13619 	      case bfd_link_hash_undefined:
   13620 	      case bfd_link_hash_undefweak:
   13621 	      case bfd_link_hash_common:
   13622 		gp_found = 0;
   13623 		break;
   13624 	      case bfd_link_hash_defined:
   13625 	      case bfd_link_hash_defweak:
   13626 		gp_found = 1;
   13627 		gp = lh->u.def.value;
   13628 		break;
   13629 	      case bfd_link_hash_indirect:
   13630 	      case bfd_link_hash_warning:
   13631 		lh = lh->u.i.link;
   13632 		/* @@FIXME  ignoring warning for now */
   13633 		goto lookup;
   13634 	      case bfd_link_hash_new:
   13635 	      default:
   13636 		abort ();
   13637 	      }
   13638 	  }
   13639 	else
   13640 	  gp_found = 0;
   13641       }
   13642       /* end mips */
   13643 
   13644       for (parent = reloc_vector; *parent != NULL; parent++)
   13645 	{
   13646 	  char *error_message = NULL;
   13647 	  asymbol *symbol;
   13648 	  bfd_reloc_status_type r;
   13649 
   13650 	  symbol = *(*parent)->sym_ptr_ptr;
   13651 	  /* PR ld/19628: A specially crafted input file
   13652 	     can result in a NULL symbol pointer here.  */
   13653 	  if (symbol == NULL)
   13654 	    {
   13655 	      link_info->callbacks->einfo
   13656 		/* xgettext:c-format */
   13657 		(_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
   13658 		 abfd, input_section, (* parent)->address);
   13659 	      install = false;
   13660 	      goto out;
   13661 	    }
   13662 
   13663 	  /* Zap reloc field when the symbol is from a discarded
   13664 	     section, ignoring any addend.  Do the same when called
   13665 	     from bfd_simple_get_relocated_section_contents for
   13666 	     undefined symbols in debug sections.  This is to keep
   13667 	     debug info reasonably sane, in particular so that
   13668 	     DW_FORM_ref_addr to another file's .debug_info isn't
   13669 	     confused with an offset into the current file's
   13670 	     .debug_info.  */
   13671 	  if ((symbol->section != NULL && discarded_section (symbol->section))
   13672 	      || (symbol->section == bfd_und_section_ptr
   13673 		  && (input_section->flags & SEC_DEBUGGING) != 0
   13674 		  && link_info->input_bfds == link_info->output_bfd))
   13675 	    {
   13676 	      bfd_vma off;
   13677 	      static reloc_howto_type none_howto
   13678 		= HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
   13679 			 "unused", false, 0, 0, false);
   13680 
   13681 	      off = ((*parent)->address
   13682 		     * bfd_octets_per_byte (input_bfd, input_section));
   13683 	      _bfd_clear_contents ((*parent)->howto, input_bfd,
   13684 				   input_section, data, off);
   13685 	      (*parent)->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
   13686 	      (*parent)->addend = 0;
   13687 	      (*parent)->howto = &none_howto;
   13688 	      r = bfd_reloc_ok;
   13689 	    }
   13690 
   13691 	  /* Specific to MIPS: Deal with relocation types that require
   13692 	     knowing the gp of the output bfd.  */
   13693 
   13694 	  /* If we've managed to find the gp and have a special
   13695 	     function for the relocation then go ahead, else default
   13696 	     to the generic handling.  */
   13697 	  else if (gp_found
   13698 		   && ((*parent)->howto->special_function
   13699 		       == _bfd_mips_elf32_gprel16_reloc))
   13700 	    r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
   13701 					       input_section, relocatable,
   13702 					       data, gp);
   13703 	  else
   13704 	    r = bfd_perform_relocation (input_bfd,
   13705 					*parent,
   13706 					data,
   13707 					input_section,
   13708 					relocatable ? abfd : NULL,
   13709 					&error_message);
   13710 
   13711 	  if (relocatable)
   13712 	    {
   13713 	      asection *os = input_section->output_section;
   13714 
   13715 	      /* A partial link, so keep the relocs.  */
   13716 	      os->orelocation[os->reloc_count] = *parent;
   13717 	      os->reloc_count++;
   13718 	    }
   13719 
   13720 	  if (r != bfd_reloc_ok)
   13721 	    {
   13722 	      _bfd_link_reloc_status_error (abfd, link_info, input_section,
   13723 					    *parent, error_message, r);
   13724 	      if (r == bfd_reloc_outofrange || r == bfd_reloc_notsupported)
   13725 		{
   13726 		  install = false;
   13727 		  goto out;
   13728 		}
   13729 	    }
   13730 	}
   13731     }
   13732 
   13733  out:
   13734   mips_elf_free_hi16_list (input_bfd, input_section, install, link_info);
   13735   free (reloc_vector);
   13736   return data;
   13737 }
   13738 
   13739 static bool
   13741 mips_elf_relax_delete_bytes (bfd *abfd,
   13742 			     asection *sec, bfd_vma addr, int count)
   13743 {
   13744   Elf_Internal_Shdr *symtab_hdr;
   13745   unsigned int sec_shndx;
   13746   bfd_byte *contents;
   13747   Elf_Internal_Rela *irel, *irelend;
   13748   Elf_Internal_Sym *isym;
   13749   Elf_Internal_Sym *isymend;
   13750   struct elf_link_hash_entry **sym_hashes;
   13751   struct elf_link_hash_entry **end_hashes;
   13752   struct elf_link_hash_entry **start_hashes;
   13753   unsigned int symcount;
   13754 
   13755   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   13756   contents = elf_section_data (sec)->this_hdr.contents;
   13757 
   13758   irel = elf_section_data (sec)->relocs;
   13759   irelend = irel + sec->reloc_count;
   13760 
   13761   /* Actually delete the bytes.  */
   13762   memmove (contents + addr, contents + addr + count,
   13763 	   (size_t) (sec->size - addr - count));
   13764   sec->size -= count;
   13765 
   13766   /* Adjust all the relocs.  */
   13767   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
   13768     {
   13769       /* Get the new reloc address.  */
   13770       if (irel->r_offset > addr)
   13771 	irel->r_offset -= count;
   13772     }
   13773 
   13774   BFD_ASSERT (addr % 2 == 0);
   13775   BFD_ASSERT (count % 2 == 0);
   13776 
   13777   /* Adjust the local symbols defined in this section.  */
   13778   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13779   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
   13780   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
   13781     if (isym->st_shndx == sec_shndx && isym->st_value > addr)
   13782       isym->st_value -= count;
   13783 
   13784   /* Now adjust the global symbols defined in this section.  */
   13785   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   13786 	      - symtab_hdr->sh_info);
   13787   sym_hashes = start_hashes = elf_sym_hashes (abfd);
   13788   end_hashes = sym_hashes + symcount;
   13789 
   13790   for (; sym_hashes < end_hashes; sym_hashes++)
   13791     {
   13792       struct elf_link_hash_entry *sym_hash = *sym_hashes;
   13793 
   13794       if ((sym_hash->root.type == bfd_link_hash_defined
   13795 	   || sym_hash->root.type == bfd_link_hash_defweak)
   13796 	  && sym_hash->root.u.def.section == sec)
   13797 	{
   13798 	  bfd_vma value = sym_hash->root.u.def.value;
   13799 
   13800 	  if (ELF_ST_IS_MICROMIPS (sym_hash->other))
   13801 	    value &= MINUS_TWO;
   13802 	  if (value > addr)
   13803 	    sym_hash->root.u.def.value -= count;
   13804 	}
   13805     }
   13806 
   13807   return true;
   13808 }
   13809 
   13810 
   13811 /* Opcodes needed for microMIPS relaxation as found in
   13812    opcodes/micromips-opc.c.  */
   13813 
   13814 struct opcode_descriptor {
   13815   unsigned long match;
   13816   unsigned long mask;
   13817 };
   13818 
   13819 /* The $ra register aka $31.  */
   13820 
   13821 #define RA 31
   13822 
   13823 /* 32-bit instruction format register fields.  */
   13824 
   13825 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
   13826 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
   13827 
   13828 /* Check if a 5-bit register index can be abbreviated to 3 bits.  */
   13829 
   13830 #define OP16_VALID_REG(r) \
   13831   ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
   13832 
   13833 
   13834 /* 32-bit and 16-bit branches.  */
   13835 
   13836 static const struct opcode_descriptor b_insns_32[] = {
   13837   { /* "b",	"p",		*/ 0x40400000, 0xffff0000 }, /* bgez 0 */
   13838   { /* "b",	"p",		*/ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
   13839   { 0, 0 }  /* End marker for find_match().  */
   13840 };
   13841 
   13842 static const struct opcode_descriptor bc_insn_32 =
   13843   { /* "bc(1|2)(ft)", "N,p",	*/ 0x42800000, 0xfec30000 };
   13844 
   13845 static const struct opcode_descriptor bz_insn_32 =
   13846   { /* "b(g|l)(e|t)z", "s,p",	*/ 0x40000000, 0xff200000 };
   13847 
   13848 static const struct opcode_descriptor bzal_insn_32 =
   13849   { /* "b(ge|lt)zal", "s,p",	*/ 0x40200000, 0xffa00000 };
   13850 
   13851 static const struct opcode_descriptor beq_insn_32 =
   13852   { /* "b(eq|ne)", "s,t,p",	*/ 0x94000000, 0xdc000000 };
   13853 
   13854 static const struct opcode_descriptor b_insn_16 =
   13855   { /* "b",	"mD",		*/ 0xcc00,     0xfc00 };
   13856 
   13857 static const struct opcode_descriptor bz_insn_16 =
   13858   { /* "b(eq|ne)z", "md,mE",	*/ 0x8c00,     0xdc00 };
   13859 
   13860 
   13861 /* 32-bit and 16-bit branch EQ and NE zero.  */
   13862 
   13863 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
   13864    eq and second the ne.  This convention is used when replacing a
   13865    32-bit BEQ/BNE with the 16-bit version.  */
   13866 
   13867 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
   13868 
   13869 static const struct opcode_descriptor bz_rs_insns_32[] = {
   13870   { /* "beqz",	"s,p",		*/ 0x94000000, 0xffe00000 },
   13871   { /* "bnez",	"s,p",		*/ 0xb4000000, 0xffe00000 },
   13872   { 0, 0 }  /* End marker for find_match().  */
   13873 };
   13874 
   13875 static const struct opcode_descriptor bz_rt_insns_32[] = {
   13876   { /* "beqz",	"t,p",		*/ 0x94000000, 0xfc01f000 },
   13877   { /* "bnez",	"t,p",		*/ 0xb4000000, 0xfc01f000 },
   13878   { 0, 0 }  /* End marker for find_match().  */
   13879 };
   13880 
   13881 static const struct opcode_descriptor bzc_insns_32[] = {
   13882   { /* "beqzc",	"s,p",		*/ 0x40e00000, 0xffe00000 },
   13883   { /* "bnezc",	"s,p",		*/ 0x40a00000, 0xffe00000 },
   13884   { 0, 0 }  /* End marker for find_match().  */
   13885 };
   13886 
   13887 static const struct opcode_descriptor bz_insns_16[] = {
   13888   { /* "beqz",	"md,mE",	*/ 0x8c00,     0xfc00 },
   13889   { /* "bnez",	"md,mE",	*/ 0xac00,     0xfc00 },
   13890   { 0, 0 }  /* End marker for find_match().  */
   13891 };
   13892 
   13893 /* Switch between a 5-bit register index and its 3-bit shorthand.  */
   13894 
   13895 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
   13896 #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
   13897 
   13898 
   13899 /* 32-bit instructions with a delay slot.  */
   13900 
   13901 static const struct opcode_descriptor jal_insn_32_bd16 =
   13902   { /* "jals",	"a",		*/ 0x74000000, 0xfc000000 };
   13903 
   13904 static const struct opcode_descriptor jal_insn_32_bd32 =
   13905   { /* "jal",	"a",		*/ 0xf4000000, 0xfc000000 };
   13906 
   13907 static const struct opcode_descriptor jal_x_insn_32_bd32 =
   13908   { /* "jal[x]", "a",		*/ 0xf0000000, 0xf8000000 };
   13909 
   13910 static const struct opcode_descriptor j_insn_32 =
   13911   { /* "j",	"a",		*/ 0xd4000000, 0xfc000000 };
   13912 
   13913 static const struct opcode_descriptor jalr_insn_32 =
   13914   { /* "jalr[.hb]", "t,s",	*/ 0x00000f3c, 0xfc00efff };
   13915 
   13916 /* This table can be compacted, because no opcode replacement is made.  */
   13917 
   13918 static const struct opcode_descriptor ds_insns_32_bd16[] = {
   13919   { /* "jals",	"a",		*/ 0x74000000, 0xfc000000 },
   13920 
   13921   { /* "jalrs[.hb]", "t,s",	*/ 0x00004f3c, 0xfc00efff },
   13922   { /* "b(ge|lt)zals", "s,p",	*/ 0x42200000, 0xffa00000 },
   13923 
   13924   { /* "b(g|l)(e|t)z", "s,p",	*/ 0x40000000, 0xff200000 },
   13925   { /* "b(eq|ne)", "s,t,p",	*/ 0x94000000, 0xdc000000 },
   13926   { /* "j",	"a",		*/ 0xd4000000, 0xfc000000 },
   13927   { 0, 0 }  /* End marker for find_match().  */
   13928 };
   13929 
   13930 /* This table can be compacted, because no opcode replacement is made.  */
   13931 
   13932 static const struct opcode_descriptor ds_insns_32_bd32[] = {
   13933   { /* "jal[x]", "a",		*/ 0xf0000000, 0xf8000000 },
   13934 
   13935   { /* "jalr[.hb]", "t,s",	*/ 0x00000f3c, 0xfc00efff },
   13936   { /* "b(ge|lt)zal", "s,p",	*/ 0x40200000, 0xffa00000 },
   13937   { 0, 0 }  /* End marker for find_match().  */
   13938 };
   13939 
   13940 
   13941 /* 16-bit instructions with a delay slot.  */
   13942 
   13943 static const struct opcode_descriptor jalr_insn_16_bd16 =
   13944   { /* "jalrs",	"my,mj",	*/ 0x45e0,     0xffe0 };
   13945 
   13946 static const struct opcode_descriptor jalr_insn_16_bd32 =
   13947   { /* "jalr",	"my,mj",	*/ 0x45c0,     0xffe0 };
   13948 
   13949 static const struct opcode_descriptor jr_insn_16 =
   13950   { /* "jr",	"mj",		*/ 0x4580,     0xffe0 };
   13951 
   13952 #define JR16_REG(opcode) ((opcode) & 0x1f)
   13953 
   13954 /* This table can be compacted, because no opcode replacement is made.  */
   13955 
   13956 static const struct opcode_descriptor ds_insns_16_bd16[] = {
   13957   { /* "jalrs",	"my,mj",	*/ 0x45e0,     0xffe0 },
   13958 
   13959   { /* "b",	"mD",		*/ 0xcc00,     0xfc00 },
   13960   { /* "b(eq|ne)z", "md,mE",	*/ 0x8c00,     0xdc00 },
   13961   { /* "jr",	"mj",		*/ 0x4580,     0xffe0 },
   13962   { 0, 0 }  /* End marker for find_match().  */
   13963 };
   13964 
   13965 
   13966 /* LUI instruction.  */
   13967 
   13968 static const struct opcode_descriptor lui_insn =
   13969  { /* "lui",	"s,u",		*/ 0x41a00000, 0xffe00000 };
   13970 
   13971 
   13972 /* ADDIU instruction.  */
   13973 
   13974 static const struct opcode_descriptor addiu_insn =
   13975   { /* "addiu",	"t,r,j",	*/ 0x30000000, 0xfc000000 };
   13976 
   13977 static const struct opcode_descriptor addiupc_insn =
   13978   { /* "addiu",	"mb,$pc,mQ",	*/ 0x78000000, 0xfc000000 };
   13979 
   13980 #define ADDIUPC_REG_FIELD(r) \
   13981   (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
   13982 
   13983 
   13984 /* Relaxable instructions in a JAL delay slot: MOVE.  */
   13985 
   13986 /* The 16-bit move has rd in 9:5 and rs in 4:0.  The 32-bit moves
   13987    (ADDU, OR) have rd in 15:11 and rs in 10:16.  */
   13988 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
   13989 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
   13990 
   13991 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
   13992 #define MOVE16_RS_FIELD(r) (((r) & 0x1f)     )
   13993 
   13994 static const struct opcode_descriptor move_insns_32[] = {
   13995   { /* "move",	"d,s",		*/ 0x00000290, 0xffe007ff }, /* or   d,s,$0 */
   13996   { /* "move",	"d,s",		*/ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
   13997   { 0, 0 }  /* End marker for find_match().  */
   13998 };
   13999 
   14000 static const struct opcode_descriptor move_insn_16 =
   14001   { /* "move",	"mp,mj",	*/ 0x0c00,     0xfc00 };
   14002 
   14003 
   14004 /* NOP instructions.  */
   14005 
   14006 static const struct opcode_descriptor nop_insn_32 =
   14007   { /* "nop",	"",		*/ 0x00000000, 0xffffffff };
   14008 
   14009 static const struct opcode_descriptor nop_insn_16 =
   14010   { /* "nop",	"",		*/ 0x0c00,     0xffff };
   14011 
   14012 
   14013 /* Instruction match support.  */
   14014 
   14015 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
   14016 
   14017 static int
   14018 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
   14019 {
   14020   unsigned long indx;
   14021 
   14022   for (indx = 0; insn[indx].mask != 0; indx++)
   14023     if (MATCH (opcode, insn[indx]))
   14024       return indx;
   14025 
   14026   return -1;
   14027 }
   14028 
   14029 
   14030 /* Branch and delay slot decoding support.  */
   14031 
   14032 /* If PTR points to what *might* be a 16-bit branch or jump, then
   14033    return the minimum length of its delay slot, otherwise return 0.
   14034    Non-zero results are not definitive as we might be checking against
   14035    the second half of another instruction.  */
   14036 
   14037 static int
   14038 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
   14039 {
   14040   unsigned long opcode;
   14041   int bdsize;
   14042 
   14043   opcode = bfd_get_16 (abfd, ptr);
   14044   if (MATCH (opcode, jalr_insn_16_bd32) != 0)
   14045     /* 16-bit branch/jump with a 32-bit delay slot.  */
   14046     bdsize = 4;
   14047   else if (MATCH (opcode, jalr_insn_16_bd16) != 0
   14048 	   || find_match (opcode, ds_insns_16_bd16) >= 0)
   14049     /* 16-bit branch/jump with a 16-bit delay slot.  */
   14050     bdsize = 2;
   14051   else
   14052     /* No delay slot.  */
   14053     bdsize = 0;
   14054 
   14055   return bdsize;
   14056 }
   14057 
   14058 /* If PTR points to what *might* be a 32-bit branch or jump, then
   14059    return the minimum length of its delay slot, otherwise return 0.
   14060    Non-zero results are not definitive as we might be checking against
   14061    the second half of another instruction.  */
   14062 
   14063 static int
   14064 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
   14065 {
   14066   unsigned long opcode;
   14067   int bdsize;
   14068 
   14069   opcode = bfd_get_micromips_32 (abfd, ptr);
   14070   if (find_match (opcode, ds_insns_32_bd32) >= 0)
   14071     /* 32-bit branch/jump with a 32-bit delay slot.  */
   14072     bdsize = 4;
   14073   else if (find_match (opcode, ds_insns_32_bd16) >= 0)
   14074     /* 32-bit branch/jump with a 16-bit delay slot.  */
   14075     bdsize = 2;
   14076   else
   14077     /* No delay slot.  */
   14078     bdsize = 0;
   14079 
   14080   return bdsize;
   14081 }
   14082 
   14083 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
   14084    that doesn't fiddle with REG, then return TRUE, otherwise FALSE.  */
   14085 
   14086 static bool
   14087 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
   14088 {
   14089   unsigned long opcode;
   14090 
   14091   opcode = bfd_get_16 (abfd, ptr);
   14092   if (MATCH (opcode, b_insn_16)
   14093 						/* B16  */
   14094       || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
   14095 						/* JR16  */
   14096       || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
   14097 						/* BEQZ16, BNEZ16  */
   14098       || (MATCH (opcode, jalr_insn_16_bd32)
   14099 						/* JALR16  */
   14100 	  && reg != JR16_REG (opcode) && reg != RA))
   14101     return true;
   14102 
   14103   return false;
   14104 }
   14105 
   14106 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
   14107    then return TRUE, otherwise FALSE.  */
   14108 
   14109 static bool
   14110 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
   14111 {
   14112   unsigned long opcode;
   14113 
   14114   opcode = bfd_get_micromips_32 (abfd, ptr);
   14115   if (MATCH (opcode, j_insn_32)
   14116 						/* J  */
   14117       || MATCH (opcode, bc_insn_32)
   14118 						/* BC1F, BC1T, BC2F, BC2T  */
   14119       || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
   14120 						/* JAL, JALX  */
   14121       || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
   14122 						/* BGEZ, BGTZ, BLEZ, BLTZ  */
   14123       || (MATCH (opcode, bzal_insn_32)
   14124 						/* BGEZAL, BLTZAL  */
   14125 	  && reg != OP32_SREG (opcode) && reg != RA)
   14126       || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
   14127 						/* JALR, JALR.HB, BEQ, BNE  */
   14128 	  && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
   14129     return true;
   14130 
   14131   return false;
   14132 }
   14133 
   14134 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
   14135    IRELEND) at OFFSET indicate that there must be a compact branch there,
   14136    then return TRUE, otherwise FALSE.  */
   14137 
   14138 static bool
   14139 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
   14140 		     const Elf_Internal_Rela *internal_relocs,
   14141 		     const Elf_Internal_Rela *irelend)
   14142 {
   14143   const Elf_Internal_Rela *irel;
   14144   unsigned long opcode;
   14145 
   14146   opcode = bfd_get_micromips_32 (abfd, ptr);
   14147   if (find_match (opcode, bzc_insns_32) < 0)
   14148     return false;
   14149 
   14150   for (irel = internal_relocs; irel < irelend; irel++)
   14151     if (irel->r_offset == offset
   14152 	&& ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
   14153       return true;
   14154 
   14155   return false;
   14156 }
   14157 
   14158 /* Bitsize checking.  */
   14159 #define IS_BITSIZE(val, N)						\
   14160   (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1)))		\
   14161     - (1ULL << ((N) - 1))) == (val))
   14162 
   14163 
   14164 bool
   14166 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
   14167 			     struct bfd_link_info *link_info,
   14168 			     bool *again)
   14169 {
   14170   bool insn32 = mips_elf_hash_table (link_info)->insn32;
   14171   Elf_Internal_Shdr *symtab_hdr;
   14172   Elf_Internal_Rela *internal_relocs;
   14173   Elf_Internal_Rela *irel, *irelend;
   14174   bfd_byte *contents = NULL;
   14175   Elf_Internal_Sym *isymbuf = NULL;
   14176 
   14177   /* Assume nothing changes.  */
   14178   *again = false;
   14179 
   14180   /* We don't have to do anything for a relocatable link, if
   14181      this section does not have relocs, or if this is not a
   14182      code section.  */
   14183 
   14184   if (bfd_link_relocatable (link_info)
   14185       || sec->reloc_count == 0
   14186       || (sec->flags & SEC_RELOC) == 0
   14187       || (sec->flags & SEC_HAS_CONTENTS) == 0
   14188       || (sec->flags & SEC_CODE) == 0)
   14189     return true;
   14190 
   14191   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   14192 
   14193   /* Get a copy of the native relocations.  */
   14194   internal_relocs = (_bfd_elf_link_read_relocs
   14195 		     (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
   14196 		      link_info->keep_memory));
   14197   if (internal_relocs == NULL)
   14198     goto error_return;
   14199 
   14200   /* Walk through them looking for relaxing opportunities.  */
   14201   irelend = internal_relocs + sec->reloc_count;
   14202   for (irel = internal_relocs; irel < irelend; irel++)
   14203     {
   14204       unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
   14205       unsigned int r_type = ELF32_R_TYPE (irel->r_info);
   14206       bool target_is_micromips_code_p;
   14207       unsigned long opcode;
   14208       bfd_vma symval;
   14209       bfd_vma pcrval;
   14210       bfd_byte *ptr;
   14211       int fndopc;
   14212 
   14213       /* The number of bytes to delete for relaxation and from where
   14214 	 to delete these bytes starting at irel->r_offset.  */
   14215       int delcnt = 0;
   14216       int deloff = 0;
   14217 
   14218       /* If this isn't something that can be relaxed, then ignore
   14219 	 this reloc.  */
   14220       if (r_type != R_MICROMIPS_HI16
   14221 	  && r_type != R_MICROMIPS_PC16_S1
   14222 	  && r_type != R_MICROMIPS_26_S1)
   14223 	continue;
   14224 
   14225       /* Get the section contents if we haven't done so already.  */
   14226       if (contents == NULL)
   14227 	{
   14228 	  /* Get cached copy if it exists.  */
   14229 	  if (elf_section_data (sec)->this_hdr.contents != NULL)
   14230 	    contents = elf_section_data (sec)->this_hdr.contents;
   14231 	  /* Go get them off disk.  */
   14232 	  else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
   14233 	    goto error_return;
   14234 	}
   14235       ptr = contents + irel->r_offset;
   14236 
   14237       /* Read this BFD's local symbols if we haven't done so already.  */
   14238       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
   14239 	{
   14240 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   14241 	  if (isymbuf == NULL)
   14242 	    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   14243 					    symtab_hdr->sh_info, 0,
   14244 					    NULL, NULL, NULL);
   14245 	  if (isymbuf == NULL)
   14246 	    goto error_return;
   14247 	}
   14248 
   14249       /* Get the value of the symbol referred to by the reloc.  */
   14250       if (r_symndx < symtab_hdr->sh_info)
   14251 	{
   14252 	  /* A local symbol.  */
   14253 	  Elf_Internal_Sym *isym;
   14254 	  asection *sym_sec;
   14255 
   14256 	  isym = isymbuf + r_symndx;
   14257 	  if (isym->st_shndx == SHN_UNDEF)
   14258 	    sym_sec = bfd_und_section_ptr;
   14259 	  else if (isym->st_shndx == SHN_ABS)
   14260 	    sym_sec = bfd_abs_section_ptr;
   14261 	  else if (isym->st_shndx == SHN_COMMON)
   14262 	    sym_sec = bfd_com_section_ptr;
   14263 	  else
   14264 	    sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   14265 	  symval = (isym->st_value
   14266 		    + sym_sec->output_section->vma
   14267 		    + sym_sec->output_offset);
   14268 	  target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
   14269 	}
   14270       else
   14271 	{
   14272 	  unsigned long indx;
   14273 	  struct elf_link_hash_entry *h;
   14274 
   14275 	  /* An external symbol.  */
   14276 	  indx = r_symndx - symtab_hdr->sh_info;
   14277 	  h = elf_sym_hashes (abfd)[indx];
   14278 	  BFD_ASSERT (h != NULL);
   14279 
   14280 	  if (h->root.type != bfd_link_hash_defined
   14281 	      && h->root.type != bfd_link_hash_defweak)
   14282 	    /* This appears to be a reference to an undefined
   14283 	       symbol.  Just ignore it -- it will be caught by the
   14284 	       regular reloc processing.  */
   14285 	    continue;
   14286 
   14287 	  symval = (h->root.u.def.value
   14288 		    + h->root.u.def.section->output_section->vma
   14289 		    + h->root.u.def.section->output_offset);
   14290 	  target_is_micromips_code_p = (!h->needs_plt
   14291 					&& ELF_ST_IS_MICROMIPS (h->other));
   14292 	}
   14293 
   14294 
   14295       /* For simplicity of coding, we are going to modify the
   14296 	 section contents, the section relocs, and the BFD symbol
   14297 	 table.  We must tell the rest of the code not to free up this
   14298 	 information.  It would be possible to instead create a table
   14299 	 of changes which have to be made, as is done in coff-mips.c;
   14300 	 that would be more work, but would require less memory when
   14301 	 the linker is run.  */
   14302 
   14303       /* Only 32-bit instructions relaxed.  */
   14304       if (irel->r_offset + 4 > sec->size)
   14305 	continue;
   14306 
   14307       opcode = bfd_get_micromips_32 (abfd, ptr);
   14308 
   14309       /* This is the pc-relative distance from the instruction the
   14310 	 relocation is applied to, to the symbol referred.  */
   14311       pcrval = (symval
   14312 		- (sec->output_section->vma + sec->output_offset)
   14313 		- irel->r_offset);
   14314 
   14315       /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
   14316 	 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
   14317 	 R_MICROMIPS_PC23_S2.  The R_MICROMIPS_PC23_S2 condition is
   14318 
   14319 	   (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
   14320 
   14321 	 where pcrval has first to be adjusted to apply against the LO16
   14322 	 location (we make the adjustment later on, when we have figured
   14323 	 out the offset).  */
   14324       if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
   14325 	{
   14326 	  bool bzc = false;
   14327 	  unsigned long nextopc;
   14328 	  unsigned long reg;
   14329 	  bfd_vma offset;
   14330 
   14331 	  /* Give up if the previous reloc was a HI16 against this symbol
   14332 	     too.  */
   14333 	  if (irel > internal_relocs
   14334 	      && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
   14335 	      && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
   14336 	    continue;
   14337 
   14338 	  /* Or if the next reloc is not a LO16 against this symbol.  */
   14339 	  if (irel + 1 >= irelend
   14340 	      || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
   14341 	      || ELF32_R_SYM (irel[1].r_info) != r_symndx)
   14342 	    continue;
   14343 
   14344 	  /* Or if the second next reloc is a LO16 against this symbol too.  */
   14345 	  if (irel + 2 >= irelend
   14346 	      && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
   14347 	      && ELF32_R_SYM (irel[2].r_info) == r_symndx)
   14348 	    continue;
   14349 
   14350 	  /* See if the LUI instruction *might* be in a branch delay slot.
   14351 	     We check whether what looks like a 16-bit branch or jump is
   14352 	     actually an immediate argument to a compact branch, and let
   14353 	     it through if so.  */
   14354 	  if (irel->r_offset >= 2
   14355 	      && check_br16_dslot (abfd, ptr - 2)
   14356 	      && !(irel->r_offset >= 4
   14357 		   && (bzc = check_relocated_bzc (abfd,
   14358 						  ptr - 4, irel->r_offset - 4,
   14359 						  internal_relocs, irelend))))
   14360 	    continue;
   14361 	  if (irel->r_offset >= 4
   14362 	      && !bzc
   14363 	      && check_br32_dslot (abfd, ptr - 4))
   14364 	    continue;
   14365 
   14366 	  reg = OP32_SREG (opcode);
   14367 
   14368 	  /* We only relax adjacent instructions or ones separated with
   14369 	     a branch or jump that has a delay slot.  The branch or jump
   14370 	     must not fiddle with the register used to hold the address.
   14371 	     Subtract 4 for the LUI itself.  */
   14372 	  offset = irel[1].r_offset - irel[0].r_offset;
   14373 	  switch (offset - 4)
   14374 	    {
   14375 	    case 0:
   14376 	      break;
   14377 	    case 2:
   14378 	      if (check_br16 (abfd, ptr + 4, reg))
   14379 		break;
   14380 	      continue;
   14381 	    case 4:
   14382 	      if (check_br32 (abfd, ptr + 4, reg))
   14383 		break;
   14384 	      continue;
   14385 	    default:
   14386 	      continue;
   14387 	    }
   14388 
   14389 	  nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
   14390 
   14391 	  /* Give up unless the same register is used with both
   14392 	     relocations.  */
   14393 	  if (OP32_SREG (nextopc) != reg)
   14394 	    continue;
   14395 
   14396 	  /* Now adjust pcrval, subtracting the offset to the LO16 reloc
   14397 	     and rounding up to take masking of the two LSBs into account.  */
   14398 	  pcrval = ((pcrval - offset + 3) | 3) ^ 3;
   14399 
   14400 	  /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16.  */
   14401 	  if (IS_BITSIZE (symval, 16))
   14402 	    {
   14403 	      /* Fix the relocation's type.  */
   14404 	      irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
   14405 
   14406 	      /* Instructions using R_MICROMIPS_LO16 have the base or
   14407 		 source register in bits 20:16.  This register becomes $0
   14408 		 (zero) as the result of the R_MICROMIPS_HI16 being 0.  */
   14409 	      nextopc &= ~0x001f0000;
   14410 	      bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
   14411 			  contents + irel[1].r_offset);
   14412 	    }
   14413 
   14414 	  /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
   14415 	     We add 4 to take LUI deletion into account while checking
   14416 	     the PC-relative distance.  */
   14417 	  else if (symval % 4 == 0
   14418 		   && IS_BITSIZE (pcrval + 4, 25)
   14419 		   && MATCH (nextopc, addiu_insn)
   14420 		   && OP32_TREG (nextopc) == OP32_SREG (nextopc)
   14421 		   && OP16_VALID_REG (OP32_TREG (nextopc)))
   14422 	    {
   14423 	      /* Fix the relocation's type.  */
   14424 	      irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
   14425 
   14426 	      /* Replace ADDIU with the ADDIUPC version.  */
   14427 	      nextopc = (addiupc_insn.match
   14428 			 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
   14429 
   14430 	      bfd_put_micromips_32 (abfd, nextopc,
   14431 				    contents + irel[1].r_offset);
   14432 	    }
   14433 
   14434 	  /* Can't do anything, give up, sigh...  */
   14435 	  else
   14436 	    continue;
   14437 
   14438 	  /* Fix the relocation's type.  */
   14439 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
   14440 
   14441 	  /* Delete the LUI instruction: 4 bytes at irel->r_offset.  */
   14442 	  delcnt = 4;
   14443 	  deloff = 0;
   14444 	}
   14445 
   14446       /* Compact branch relaxation -- due to the multitude of macros
   14447 	 employed by the compiler/assembler, compact branches are not
   14448 	 always generated.  Obviously, this can/will be fixed elsewhere,
   14449 	 but there is no drawback in double checking it here.  */
   14450       else if (r_type == R_MICROMIPS_PC16_S1
   14451 	       && irel->r_offset + 5 < sec->size
   14452 	       && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
   14453 		   || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
   14454 	       && ((!insn32
   14455 		    && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
   14456 					nop_insn_16) ? 2 : 0))
   14457 		   || (irel->r_offset + 7 < sec->size
   14458 		       && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
   14459 								 ptr + 4),
   14460 					   nop_insn_32) ? 4 : 0))))
   14461 	{
   14462 	  unsigned long reg;
   14463 
   14464 	  reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
   14465 
   14466 	  /* Replace BEQZ/BNEZ with the compact version.  */
   14467 	  opcode = (bzc_insns_32[fndopc].match
   14468 		    | BZC32_REG_FIELD (reg)
   14469 		    | (opcode & 0xffff));		/* Addend value.  */
   14470 
   14471 	  bfd_put_micromips_32 (abfd, opcode, ptr);
   14472 
   14473 	  /* Delete the delay slot NOP: two or four bytes from
   14474 	     irel->offset + 4; delcnt has already been set above.  */
   14475 	  deloff = 4;
   14476 	}
   14477 
   14478       /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1.  We need
   14479 	 to check the distance from the next instruction, so subtract 2.  */
   14480       else if (!insn32
   14481 	       && r_type == R_MICROMIPS_PC16_S1
   14482 	       && IS_BITSIZE (pcrval - 2, 11)
   14483 	       && find_match (opcode, b_insns_32) >= 0)
   14484 	{
   14485 	  /* Fix the relocation's type.  */
   14486 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
   14487 
   14488 	  /* Replace the 32-bit opcode with a 16-bit opcode.  */
   14489 	  bfd_put_16 (abfd,
   14490 		      (b_insn_16.match
   14491 		       | (opcode & 0x3ff)),		/* Addend value.  */
   14492 		      ptr);
   14493 
   14494 	  /* Delete 2 bytes from irel->r_offset + 2.  */
   14495 	  delcnt = 2;
   14496 	  deloff = 2;
   14497 	}
   14498 
   14499       /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1.  We need
   14500 	 to check the distance from the next instruction, so subtract 2.  */
   14501       else if (!insn32
   14502 	       && r_type == R_MICROMIPS_PC16_S1
   14503 	       && IS_BITSIZE (pcrval - 2, 8)
   14504 	       && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
   14505 		    && OP16_VALID_REG (OP32_SREG (opcode)))
   14506 		   || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
   14507 		       && OP16_VALID_REG (OP32_TREG (opcode)))))
   14508 	{
   14509 	  unsigned long reg;
   14510 
   14511 	  reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
   14512 
   14513 	  /* Fix the relocation's type.  */
   14514 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
   14515 
   14516 	  /* Replace the 32-bit opcode with a 16-bit opcode.  */
   14517 	  bfd_put_16 (abfd,
   14518 		      (bz_insns_16[fndopc].match
   14519 		       | BZ16_REG_FIELD (reg)
   14520 		       | (opcode & 0x7f)),		/* Addend value.  */
   14521 		      ptr);
   14522 
   14523 	  /* Delete 2 bytes from irel->r_offset + 2.  */
   14524 	  delcnt = 2;
   14525 	  deloff = 2;
   14526 	}
   14527 
   14528       /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets.  */
   14529       else if (!insn32
   14530 	       && r_type == R_MICROMIPS_26_S1
   14531 	       && target_is_micromips_code_p
   14532 	       && irel->r_offset + 7 < sec->size
   14533 	       && MATCH (opcode, jal_insn_32_bd32))
   14534 	{
   14535 	  unsigned long n32opc;
   14536 	  bool relaxed = false;
   14537 
   14538 	  n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
   14539 
   14540 	  if (MATCH (n32opc, nop_insn_32))
   14541 	    {
   14542 	      /* Replace delay slot 32-bit NOP with a 16-bit NOP.  */
   14543 	      bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
   14544 
   14545 	      relaxed = true;
   14546 	    }
   14547 	  else if (find_match (n32opc, move_insns_32) >= 0)
   14548 	    {
   14549 	      /* Replace delay slot 32-bit MOVE with 16-bit MOVE.  */
   14550 	      bfd_put_16 (abfd,
   14551 			  (move_insn_16.match
   14552 			   | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
   14553 			   | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
   14554 			  ptr + 4);
   14555 
   14556 	      relaxed = true;
   14557 	    }
   14558 	  /* Other 32-bit instructions relaxable to 16-bit
   14559 	     instructions will be handled here later.  */
   14560 
   14561 	  if (relaxed)
   14562 	    {
   14563 	      /* JAL with 32-bit delay slot that is changed to a JALS
   14564 		 with 16-bit delay slot.  */
   14565 	      bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
   14566 
   14567 	      /* Delete 2 bytes from irel->r_offset + 6.  */
   14568 	      delcnt = 2;
   14569 	      deloff = 6;
   14570 	    }
   14571 	}
   14572 
   14573       if (delcnt != 0)
   14574 	{
   14575 	  /* Note that we've changed the relocs, section contents, etc.  */
   14576 	  elf_section_data (sec)->relocs = internal_relocs;
   14577 	  elf_section_data (sec)->this_hdr.contents = contents;
   14578 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   14579 
   14580 	  /* Delete bytes depending on the delcnt and deloff.  */
   14581 	  if (!mips_elf_relax_delete_bytes (abfd, sec,
   14582 					    irel->r_offset + deloff, delcnt))
   14583 	    goto error_return;
   14584 
   14585 	  /* That will change things, so we should relax again.
   14586 	     Note that this is not required, and it may be slow.  */
   14587 	  *again = true;
   14588 	}
   14589     }
   14590 
   14591   if (isymbuf != NULL
   14592       && symtab_hdr->contents != (unsigned char *) isymbuf)
   14593     {
   14594       if (! link_info->keep_memory)
   14595 	free (isymbuf);
   14596       else
   14597 	{
   14598 	  /* Cache the symbols for elf_link_input_bfd.  */
   14599 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   14600 	}
   14601     }
   14602 
   14603   if (contents != NULL
   14604       && elf_section_data (sec)->this_hdr.contents != contents)
   14605     {
   14606       if (! link_info->keep_memory)
   14607 	free (contents);
   14608       else
   14609 	{
   14610 	  /* Cache the section contents for elf_link_input_bfd.  */
   14611 	  elf_section_data (sec)->this_hdr.contents = contents;
   14612 	}
   14613     }
   14614 
   14615   if (elf_section_data (sec)->relocs != internal_relocs)
   14616     free (internal_relocs);
   14617 
   14618   return true;
   14619 
   14620  error_return:
   14621   if (symtab_hdr->contents != (unsigned char *) isymbuf)
   14622     free (isymbuf);
   14623   if (elf_section_data (sec)->this_hdr.contents != contents)
   14624     free (contents);
   14625   if (elf_section_data (sec)->relocs != internal_relocs)
   14626     free (internal_relocs);
   14627 
   14628   return false;
   14629 }
   14630 
   14631 /* Create a MIPS ELF linker hash table.  */
   14633 
   14634 struct bfd_link_hash_table *
   14635 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
   14636 {
   14637   struct mips_elf_link_hash_table *ret;
   14638   size_t amt = sizeof (struct mips_elf_link_hash_table);
   14639 
   14640   ret = bfd_zmalloc (amt);
   14641   if (ret == NULL)
   14642     return NULL;
   14643 
   14644   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
   14645 				      mips_elf_link_hash_newfunc,
   14646 				      sizeof (struct mips_elf_link_hash_entry)))
   14647     {
   14648       free (ret);
   14649       return NULL;
   14650     }
   14651   ret->root.init_plt_refcount.plist = NULL;
   14652   ret->root.init_plt_offset.plist = NULL;
   14653 
   14654   return &ret->root.root;
   14655 }
   14656 
   14657 /* Likewise, but indicate that the target is VxWorks.  */
   14658 
   14659 struct bfd_link_hash_table *
   14660 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
   14661 {
   14662   struct bfd_link_hash_table *ret;
   14663 
   14664   ret = _bfd_mips_elf_link_hash_table_create (abfd);
   14665   if (ret)
   14666     {
   14667       struct mips_elf_link_hash_table *htab;
   14668 
   14669       htab = (struct mips_elf_link_hash_table *) ret;
   14670       htab->use_plts_and_copy_relocs = true;
   14671     }
   14672   return ret;
   14673 }
   14674 
   14675 /* A function that the linker calls if we are allowed to use PLTs
   14676    and copy relocs.  */
   14677 
   14678 void
   14679 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
   14680 {
   14681   mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
   14682 }
   14683 
   14684 /* A function that the linker calls to select between all or only
   14685    32-bit microMIPS instructions, and between making or ignoring
   14686    branch relocation checks for invalid transitions between ISA modes.
   14687    Also record whether we have been configured for a GNU target.  */
   14688 
   14689 void
   14690 _bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
   14691 			    bool ignore_branch_isa,
   14692 			    bool gnu_target)
   14693 {
   14694   mips_elf_hash_table (info)->insn32 = insn32;
   14695   mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
   14696   mips_elf_hash_table (info)->gnu_target = gnu_target;
   14697 }
   14698 
   14699 /* A function that the linker calls to enable use of compact branches in
   14700    linker generated code for MIPSR6.  */
   14701 
   14702 void
   14703 _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
   14704 {
   14705   mips_elf_hash_table (info)->compact_branches = on;
   14706 }
   14707 
   14708 
   14709 /* Structure for saying that BFD machine EXTENSION extends BASE.  */
   14711 
   14712 struct mips_mach_extension
   14713 {
   14714   unsigned long extension, base;
   14715 };
   14716 
   14717 /* An array that maps 64-bit architectures to the corresponding 32-bit
   14718    architectures.  */
   14719 static const struct mips_mach_extension mips_mach_32_64[] =
   14720 {
   14721   { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
   14722   { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
   14723   { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
   14724   { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
   14725   { bfd_mach_mipsisa64,   bfd_mach_mipsisa32 }
   14726 };
   14727 
   14728 /* An array describing how BFD machines relate to one another.  The entries
   14729    are ordered topologically with MIPS I extensions listed last.  */
   14730 
   14731 static const struct mips_mach_extension mips_mach_extensions[] =
   14732 {
   14733   /* MIPS64r2 extensions.  */
   14734   { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
   14735   { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
   14736   { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
   14737   { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
   14738   { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
   14739   { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
   14740   { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
   14741 
   14742   /* MIPS64 extensions.  */
   14743   { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
   14744   { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
   14745   { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
   14746 
   14747   /* MIPS V extensions.  */
   14748   { bfd_mach_mipsisa64, bfd_mach_mips5 },
   14749 
   14750   /* R10000 extensions.  */
   14751   { bfd_mach_mips12000, bfd_mach_mips10000 },
   14752   { bfd_mach_mips14000, bfd_mach_mips10000 },
   14753   { bfd_mach_mips16000, bfd_mach_mips10000 },
   14754 
   14755   /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
   14756      vr5400 ISA, but doesn't include the multimedia stuff.  It seems
   14757      better to allow vr5400 and vr5500 code to be merged anyway, since
   14758      many libraries will just use the core ISA.  Perhaps we could add
   14759      some sort of ASE flag if this ever proves a problem.  */
   14760   { bfd_mach_mips5500, bfd_mach_mips5400 },
   14761   { bfd_mach_mips5400, bfd_mach_mips5000 },
   14762 
   14763   /* MIPS IV extensions.  */
   14764   { bfd_mach_mips5, bfd_mach_mips8000 },
   14765   { bfd_mach_mips10000, bfd_mach_mips8000 },
   14766   { bfd_mach_mips5000, bfd_mach_mips8000 },
   14767   { bfd_mach_mips7000, bfd_mach_mips8000 },
   14768   { bfd_mach_mips9000, bfd_mach_mips8000 },
   14769 
   14770   /* VR4100 extensions.  */
   14771   { bfd_mach_mips4120, bfd_mach_mips4100 },
   14772   { bfd_mach_mips4111, bfd_mach_mips4100 },
   14773 
   14774   /* MIPS III extensions.  */
   14775   { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
   14776   { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
   14777   { bfd_mach_mips8000, bfd_mach_mips4000 },
   14778   { bfd_mach_mips4650, bfd_mach_mips4000 },
   14779   { bfd_mach_mips4600, bfd_mach_mips4000 },
   14780   { bfd_mach_mips4400, bfd_mach_mips4000 },
   14781   { bfd_mach_mips4300, bfd_mach_mips4000 },
   14782   { bfd_mach_mips4100, bfd_mach_mips4000 },
   14783   { bfd_mach_mips5900, bfd_mach_mips4000 },
   14784 
   14785   /* MIPS32r3 extensions.  */
   14786   { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
   14787 
   14788   /* MIPS32r2 extensions.  */
   14789   { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
   14790 
   14791   /* MIPS32 extensions.  */
   14792   { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
   14793 
   14794   /* MIPS II extensions.  */
   14795   { bfd_mach_mips4000, bfd_mach_mips6000 },
   14796   { bfd_mach_mipsisa32, bfd_mach_mips6000 },
   14797   { bfd_mach_mips4010, bfd_mach_mips6000 },
   14798   { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
   14799 
   14800   /* MIPS I extensions.  */
   14801   { bfd_mach_mips6000, bfd_mach_mips3000 },
   14802   { bfd_mach_mips3900, bfd_mach_mips3000 }
   14803 };
   14804 
   14805 /* Return true if bfd machine EXTENSION is the same as BASE, or if
   14806    EXTENSION is the 64-bit equivalent of a 32-bit BASE.  */
   14807 
   14808 static bool
   14809 mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
   14810 {
   14811   size_t i;
   14812 
   14813   if (extension == base)
   14814     return true;
   14815 
   14816   for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
   14817     if (extension == mips_mach_32_64[i].extension)
   14818       return base == mips_mach_32_64[i].base;
   14819 
   14820   return false;
   14821 }
   14822 
   14823 static bool
   14824 mips_mach_extends_p (unsigned long base, unsigned long extension)
   14825 {
   14826   size_t i;
   14827 
   14828   if (mips_mach_extends_32_64 (base, extension))
   14829     return true;
   14830 
   14831   for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
   14832     if (extension == mips_mach_extensions[i].extension)
   14833       {
   14834 	extension = mips_mach_extensions[i].base;
   14835 	if (mips_mach_extends_32_64 (base, extension))
   14836 	  return true;
   14837       }
   14838 
   14839   return false;
   14840 }
   14841 
   14842 /* Return the BFD mach for each .MIPS.abiflags ISA Extension.  */
   14843 
   14844 static unsigned long
   14845 bfd_mips_isa_ext_mach (unsigned int isa_ext)
   14846 {
   14847   switch (isa_ext)
   14848     {
   14849     case AFL_EXT_3900:	      return bfd_mach_mips3900;
   14850     case AFL_EXT_4010:	      return bfd_mach_mips4010;
   14851     case AFL_EXT_4100:	      return bfd_mach_mips4100;
   14852     case AFL_EXT_4111:	      return bfd_mach_mips4111;
   14853     case AFL_EXT_4120:	      return bfd_mach_mips4120;
   14854     case AFL_EXT_4650:	      return bfd_mach_mips4650;
   14855     case AFL_EXT_5400:	      return bfd_mach_mips5400;
   14856     case AFL_EXT_5500:	      return bfd_mach_mips5500;
   14857     case AFL_EXT_5900:	      return bfd_mach_mips5900;
   14858     case AFL_EXT_10000:	      return bfd_mach_mips10000;
   14859     case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
   14860     case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
   14861     case AFL_EXT_SB1:	      return bfd_mach_mips_sb1;
   14862     case AFL_EXT_OCTEON:      return bfd_mach_mips_octeon;
   14863     case AFL_EXT_OCTEONP:     return bfd_mach_mips_octeonp;
   14864     case AFL_EXT_OCTEON2:     return bfd_mach_mips_octeon2;
   14865     case AFL_EXT_XLR:	      return bfd_mach_mips_xlr;
   14866     default:		      return bfd_mach_mips3000;
   14867     }
   14868 }
   14869 
   14870 /* Return the .MIPS.abiflags value representing each ISA Extension.  */
   14871 
   14872 unsigned int
   14873 bfd_mips_isa_ext (bfd *abfd)
   14874 {
   14875   switch (bfd_get_mach (abfd))
   14876     {
   14877     case bfd_mach_mips3900:	    return AFL_EXT_3900;
   14878     case bfd_mach_mips4010:	    return AFL_EXT_4010;
   14879     case bfd_mach_mips4100:	    return AFL_EXT_4100;
   14880     case bfd_mach_mips4111:	    return AFL_EXT_4111;
   14881     case bfd_mach_mips4120:	    return AFL_EXT_4120;
   14882     case bfd_mach_mips4650:	    return AFL_EXT_4650;
   14883     case bfd_mach_mips5400:	    return AFL_EXT_5400;
   14884     case bfd_mach_mips5500:	    return AFL_EXT_5500;
   14885     case bfd_mach_mips5900:	    return AFL_EXT_5900;
   14886     case bfd_mach_mips10000:	    return AFL_EXT_10000;
   14887     case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
   14888     case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
   14889     case bfd_mach_mips_sb1:	    return AFL_EXT_SB1;
   14890     case bfd_mach_mips_octeon:	    return AFL_EXT_OCTEON;
   14891     case bfd_mach_mips_octeonp:	    return AFL_EXT_OCTEONP;
   14892     case bfd_mach_mips_octeon3:	    return AFL_EXT_OCTEON3;
   14893     case bfd_mach_mips_octeon2:	    return AFL_EXT_OCTEON2;
   14894     case bfd_mach_mips_xlr:	    return AFL_EXT_XLR;
   14895     case bfd_mach_mips_interaptiv_mr2:
   14896       return AFL_EXT_INTERAPTIV_MR2;
   14897     default:			    return 0;
   14898     }
   14899 }
   14900 
   14901 /* Encode ISA level and revision as a single value.  */
   14902 #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
   14903 
   14904 /* Decode a single value into level and revision.  */
   14905 #define ISA_LEVEL(LEVREV)  ((LEVREV) >> 3)
   14906 #define ISA_REV(LEVREV)    ((LEVREV) & 0x7)
   14907 
   14908 /* Update the isa_level, isa_rev, isa_ext fields of abiflags.  */
   14909 
   14910 static void
   14911 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
   14912 {
   14913   int new_isa = 0;
   14914   switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
   14915     {
   14916     case EF_MIPS_ARCH_1:    new_isa = LEVEL_REV (1, 0); break;
   14917     case EF_MIPS_ARCH_2:    new_isa = LEVEL_REV (2, 0); break;
   14918     case EF_MIPS_ARCH_3:    new_isa = LEVEL_REV (3, 0); break;
   14919     case EF_MIPS_ARCH_4:    new_isa = LEVEL_REV (4, 0); break;
   14920     case EF_MIPS_ARCH_5:    new_isa = LEVEL_REV (5, 0); break;
   14921     case EF_MIPS_ARCH_32:   new_isa = LEVEL_REV (32, 1); break;
   14922     case EF_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
   14923     case EF_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
   14924     case EF_MIPS_ARCH_64:   new_isa = LEVEL_REV (64, 1); break;
   14925     case EF_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
   14926     case EF_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
   14927     default:
   14928       _bfd_error_handler
   14929 	/* xgettext:c-format */
   14930 	(_("%pB: unknown architecture %s"),
   14931 	 abfd, bfd_printable_name (abfd));
   14932     }
   14933 
   14934   if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
   14935     {
   14936       abiflags->isa_level = ISA_LEVEL (new_isa);
   14937       abiflags->isa_rev = ISA_REV (new_isa);
   14938     }
   14939 
   14940   /* Update the isa_ext if ABFD describes a further extension.  */
   14941   if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
   14942 			   bfd_get_mach (abfd)))
   14943     abiflags->isa_ext = bfd_mips_isa_ext (abfd);
   14944 }
   14945 
   14946 /* Return true if the given ELF header flags describe a 32-bit binary.  */
   14947 
   14948 static bool
   14949 mips_32bit_flags_p (flagword flags)
   14950 {
   14951   return ((flags & EF_MIPS_32BITMODE) != 0
   14952 	  || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32
   14953 	  || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32
   14954 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1
   14955 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2
   14956 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32
   14957 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2
   14958 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6);
   14959 }
   14960 
   14961 /* Infer the content of the ABI flags based on the elf header.  */
   14962 
   14963 static void
   14964 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
   14965 {
   14966   obj_attribute *in_attr;
   14967 
   14968   memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
   14969   update_mips_abiflags_isa (abfd, abiflags);
   14970 
   14971   if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
   14972     abiflags->gpr_size = AFL_REG_32;
   14973   else
   14974     abiflags->gpr_size = AFL_REG_64;
   14975 
   14976   abiflags->cpr1_size = AFL_REG_NONE;
   14977 
   14978   in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
   14979   abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   14980 
   14981   if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
   14982       || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
   14983       || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
   14984 	  && abiflags->gpr_size == AFL_REG_32))
   14985     abiflags->cpr1_size = AFL_REG_32;
   14986   else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
   14987 	   || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
   14988 	   || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
   14989     abiflags->cpr1_size = AFL_REG_64;
   14990 
   14991   abiflags->cpr2_size = AFL_REG_NONE;
   14992 
   14993   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
   14994     abiflags->ases |= AFL_ASE_MDMX;
   14995   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
   14996     abiflags->ases |= AFL_ASE_MIPS16;
   14997   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
   14998     abiflags->ases |= AFL_ASE_MICROMIPS;
   14999 
   15000   if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
   15001       && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
   15002       && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
   15003       && abiflags->isa_level >= 32
   15004       && abiflags->ases != AFL_ASE_LOONGSON_EXT)
   15005     abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
   15006 }
   15007 
   15008 /* We need to use a special link routine to handle the .reginfo and
   15009    the .mdebug sections.  We need to merge all instances of these
   15010    sections together, not write them all out sequentially.  */
   15011 
   15012 bool
   15013 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   15014 {
   15015   asection *o;
   15016   struct bfd_link_order *p;
   15017   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
   15018   asection *rtproc_sec, *abiflags_sec;
   15019   Elf32_RegInfo reginfo;
   15020   struct ecoff_debug_info debug;
   15021   struct mips_htab_traverse_info hti;
   15022   elf_backend_data *bed = get_elf_backend_data (abfd);
   15023   const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
   15024   HDRR *symhdr = &debug.symbolic_header;
   15025   void *mdebug_handle = NULL;
   15026   asection *s;
   15027   EXTR esym;
   15028   unsigned int i;
   15029   bfd_size_type amt;
   15030   struct mips_elf_link_hash_table *htab;
   15031 
   15032   static const char * const secname[] =
   15033   {
   15034     ".text", ".init", ".fini", ".data",
   15035     ".rodata", ".sdata", ".sbss", ".bss"
   15036   };
   15037   static const int sc[] =
   15038   {
   15039     scText, scInit, scFini, scData,
   15040     scRData, scSData, scSBss, scBss
   15041   };
   15042 
   15043   htab = mips_elf_hash_table (info);
   15044   BFD_ASSERT (htab != NULL);
   15045 
   15046   /* Sort the dynamic symbols so that those with GOT entries come after
   15047      those without.  */
   15048   if (!mips_elf_sort_hash_table (abfd, info))
   15049     return false;
   15050 
   15051   /* Create any scheduled LA25 stubs.  */
   15052   hti.info = info;
   15053   hti.output_bfd = abfd;
   15054   hti.error = false;
   15055   htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
   15056   if (hti.error)
   15057     return false;
   15058 
   15059   /* Get a value for the GP register.  */
   15060   if (elf_gp (abfd) == 0)
   15061     {
   15062       struct bfd_link_hash_entry *h;
   15063 
   15064       h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
   15065       if (h != NULL && h->type == bfd_link_hash_defined)
   15066 	elf_gp (abfd) = (h->u.def.value
   15067 			 + h->u.def.section->output_section->vma
   15068 			 + h->u.def.section->output_offset);
   15069       else if (htab->root.target_os == is_vxworks
   15070 	       && (h = bfd_link_hash_lookup (info->hash,
   15071 					     "_GLOBAL_OFFSET_TABLE_",
   15072 					     false, false, true))
   15073 	       && h->type == bfd_link_hash_defined)
   15074 	elf_gp (abfd) = (h->u.def.section->output_section->vma
   15075 			 + h->u.def.section->output_offset
   15076 			 + h->u.def.value);
   15077       else if (bfd_link_relocatable (info))
   15078 	{
   15079 	  bfd_vma lo = MINUS_ONE;
   15080 
   15081 	  /* Find the GP-relative section with the lowest offset.  */
   15082 	  for (o = abfd->sections; o != NULL; o = o->next)
   15083 	    if (o->vma < lo
   15084 		&& (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
   15085 	      lo = o->vma;
   15086 
   15087 	  /* And calculate GP relative to that.  */
   15088 	  elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
   15089 	}
   15090       else
   15091 	{
   15092 	  /* If the relocate_section function needs to do a reloc
   15093 	     involving the GP value, it should make a reloc_dangerous
   15094 	     callback to warn that GP is not defined.  */
   15095 	}
   15096     }
   15097 
   15098   /* Go through the sections and collect the .reginfo and .mdebug
   15099      information.  */
   15100   abiflags_sec = NULL;
   15101   reginfo_sec = NULL;
   15102   mdebug_sec = NULL;
   15103   gptab_data_sec = NULL;
   15104   gptab_bss_sec = NULL;
   15105   for (o = abfd->sections; o != NULL; o = o->next)
   15106     {
   15107       if (strcmp (o->name, ".MIPS.abiflags") == 0)
   15108 	{
   15109 	  /* We have found the .MIPS.abiflags section in the output file.
   15110 	     Look through all the link_orders comprising it and remove them.
   15111 	     The data is merged in _bfd_mips_elf_merge_private_bfd_data.  */
   15112 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   15113 	    {
   15114 	      asection *input_section;
   15115 
   15116 	      if (p->type != bfd_indirect_link_order)
   15117 		{
   15118 		  if (p->type == bfd_data_link_order)
   15119 		    continue;
   15120 		  abort ();
   15121 		}
   15122 
   15123 	      input_section = p->u.indirect.section;
   15124 
   15125 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   15126 		 elf_link_input_bfd ignores this section.  */
   15127 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   15128 	    }
   15129 
   15130 	  /* Size has been set in _bfd_mips_elf_late_size_sections.  */
   15131 	  BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
   15132 
   15133 	  /* Skip this section later on (I don't think this currently
   15134 	     matters, but someday it might).  */
   15135 	  o->map_head.link_order = NULL;
   15136 
   15137 	  abiflags_sec = o;
   15138 	}
   15139 
   15140       if (strcmp (o->name, ".reginfo") == 0)
   15141 	{
   15142 	  memset (&reginfo, 0, sizeof reginfo);
   15143 
   15144 	  /* We have found the .reginfo section in the output file.
   15145 	     Look through all the link_orders comprising it and merge
   15146 	     the information together.  */
   15147 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   15148 	    {
   15149 	      asection *input_section;
   15150 	      bfd *input_bfd;
   15151 	      Elf32_External_RegInfo ext;
   15152 	      Elf32_RegInfo sub;
   15153 	      bfd_size_type sz;
   15154 
   15155 	      if (p->type != bfd_indirect_link_order)
   15156 		{
   15157 		  if (p->type == bfd_data_link_order)
   15158 		    continue;
   15159 		  abort ();
   15160 		}
   15161 
   15162 	      input_section = p->u.indirect.section;
   15163 	      input_bfd = input_section->owner;
   15164 
   15165 	      sz = (input_section->size < sizeof (ext)
   15166 		    ? input_section->size : sizeof (ext));
   15167 	      memset (&ext, 0, sizeof (ext));
   15168 	      if (! bfd_get_section_contents (input_bfd, input_section,
   15169 					      &ext, 0, sz))
   15170 		return false;
   15171 
   15172 	      bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
   15173 
   15174 	      reginfo.ri_gprmask |= sub.ri_gprmask;
   15175 	      reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
   15176 	      reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
   15177 	      reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
   15178 	      reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
   15179 
   15180 	      /* ri_gp_value is set by the function
   15181 		 `_bfd_mips_elf_section_processing' when the section is
   15182 		 finally written out.  */
   15183 
   15184 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   15185 		 elf_link_input_bfd ignores this section.  */
   15186 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   15187 	    }
   15188 
   15189 	  /* Size has been set in _bfd_mips_elf_late_size_sections.  */
   15190 	  BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
   15191 
   15192 	  /* Skip this section later on (I don't think this currently
   15193 	     matters, but someday it might).  */
   15194 	  o->map_head.link_order = NULL;
   15195 
   15196 	  reginfo_sec = o;
   15197 	}
   15198 
   15199       if (strcmp (o->name, ".mdebug") == 0)
   15200 	{
   15201 	  struct extsym_info einfo;
   15202 	  bfd_vma last;
   15203 
   15204 	  /* We have found the .mdebug section in the output file.
   15205 	     Look through all the link_orders comprising it and merge
   15206 	     the information together.  */
   15207 	  symhdr->magic = swap->sym_magic;
   15208 	  /* FIXME: What should the version stamp be?  */
   15209 	  symhdr->vstamp = 0;
   15210 	  symhdr->ilineMax = 0;
   15211 	  symhdr->cbLine = 0;
   15212 	  symhdr->idnMax = 0;
   15213 	  symhdr->ipdMax = 0;
   15214 	  symhdr->isymMax = 0;
   15215 	  symhdr->ioptMax = 0;
   15216 	  symhdr->iauxMax = 0;
   15217 	  symhdr->issMax = 0;
   15218 	  symhdr->issExtMax = 0;
   15219 	  symhdr->ifdMax = 0;
   15220 	  symhdr->crfd = 0;
   15221 	  symhdr->iextMax = 0;
   15222 
   15223 	  /* We accumulate the debugging information itself in the
   15224 	     debug_info structure.  */
   15225 	  debug.alloc_syments = false;
   15226 	  debug.line = NULL;
   15227 	  debug.external_dnr = NULL;
   15228 	  debug.external_pdr = NULL;
   15229 	  debug.external_sym = NULL;
   15230 	  debug.external_opt = NULL;
   15231 	  debug.external_aux = NULL;
   15232 	  debug.ss = NULL;
   15233 	  debug.ssext = debug.ssext_end = NULL;
   15234 	  debug.external_fdr = NULL;
   15235 	  debug.external_rfd = NULL;
   15236 	  debug.external_ext = debug.external_ext_end = NULL;
   15237 
   15238 	  mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
   15239 	  if (mdebug_handle == NULL)
   15240 	    return false;
   15241 
   15242 	  esym.jmptbl = 0;
   15243 	  esym.cobol_main = 0;
   15244 	  esym.weakext = 0;
   15245 	  esym.reserved = 0;
   15246 	  esym.ifd = ifdNil;
   15247 	  esym.asym.iss = issNil;
   15248 	  esym.asym.st = stLocal;
   15249 	  esym.asym.reserved = 0;
   15250 	  esym.asym.index = indexNil;
   15251 	  last = 0;
   15252 	  for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
   15253 	    {
   15254 	      esym.asym.sc = sc[i];
   15255 	      s = bfd_get_section_by_name (abfd, secname[i]);
   15256 	      if (s != NULL)
   15257 		{
   15258 		  esym.asym.value = s->vma;
   15259 		  last = s->vma + s->size;
   15260 		}
   15261 	      else
   15262 		esym.asym.value = last;
   15263 	      if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
   15264 						 secname[i], &esym))
   15265 		return false;
   15266 	    }
   15267 
   15268 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   15269 	    {
   15270 	      asection *input_section;
   15271 	      bfd *input_bfd;
   15272 	      const struct ecoff_debug_swap *input_swap;
   15273 	      struct ecoff_debug_info input_debug;
   15274 	      char *eraw_src;
   15275 	      char *eraw_end;
   15276 
   15277 	      if (p->type != bfd_indirect_link_order)
   15278 		{
   15279 		  if (p->type == bfd_data_link_order)
   15280 		    continue;
   15281 		  abort ();
   15282 		}
   15283 
   15284 	      input_section = p->u.indirect.section;
   15285 	      input_bfd = input_section->owner;
   15286 
   15287 	      if (!is_mips_elf (input_bfd))
   15288 		{
   15289 		  /* I don't know what a non MIPS ELF bfd would be
   15290 		     doing with a .mdebug section, but I don't really
   15291 		     want to deal with it.  */
   15292 		  continue;
   15293 		}
   15294 
   15295 	      input_swap = (get_elf_backend_data (input_bfd)
   15296 			    ->elf_backend_ecoff_debug_swap);
   15297 
   15298 	      BFD_ASSERT (p->size == input_section->size);
   15299 
   15300 	      /* The ECOFF linking code expects that we have already
   15301 		 read in the debugging information and set up an
   15302 		 ecoff_debug_info structure, so we do that now.  */
   15303 	      if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
   15304 						   &input_debug))
   15305 		return false;
   15306 
   15307 	      if (! (bfd_ecoff_debug_accumulate
   15308 		     (mdebug_handle, abfd, &debug, swap, input_bfd,
   15309 		      &input_debug, input_swap, info)))
   15310 		{
   15311 		  _bfd_ecoff_free_ecoff_debug_info (&input_debug);
   15312 		  return false;
   15313 		}
   15314 
   15315 	      /* Loop through the external symbols.  For each one with
   15316 		 interesting information, try to find the symbol in
   15317 		 the linker global hash table and save the information
   15318 		 for the output external symbols.  */
   15319 	      eraw_src = input_debug.external_ext;
   15320 	      eraw_end = (eraw_src
   15321 			  + (input_debug.symbolic_header.iextMax
   15322 			     * input_swap->external_ext_size));
   15323 	      for (;
   15324 		   eraw_src < eraw_end;
   15325 		   eraw_src += input_swap->external_ext_size)
   15326 		{
   15327 		  EXTR ext;
   15328 		  const char *name;
   15329 		  struct mips_elf_link_hash_entry *h;
   15330 
   15331 		  (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
   15332 		  if (ext.asym.sc == scNil
   15333 		      || ext.asym.sc == scUndefined
   15334 		      || ext.asym.sc == scSUndefined)
   15335 		    continue;
   15336 
   15337 		  name = input_debug.ssext + ext.asym.iss;
   15338 		  h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
   15339 						 name, false, false, true);
   15340 		  if (h == NULL || h->esym.ifd != -2)
   15341 		    continue;
   15342 
   15343 		  if (ext.ifd != -1)
   15344 		    {
   15345 		      BFD_ASSERT (ext.ifd
   15346 				  < input_debug.symbolic_header.ifdMax);
   15347 		      ext.ifd = input_debug.ifdmap[ext.ifd];
   15348 		    }
   15349 
   15350 		  h->esym = ext;
   15351 		}
   15352 
   15353 	      /* Free up the information we just read.  */
   15354 	      _bfd_ecoff_free_ecoff_debug_info (&input_debug);
   15355 
   15356 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   15357 		 elf_link_input_bfd ignores this section.  */
   15358 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   15359 	    }
   15360 
   15361 	  if (SGI_COMPAT (abfd) && bfd_link_pic (info))
   15362 	    {
   15363 	      /* Create .rtproc section.  */
   15364 	      rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
   15365 	      if (rtproc_sec == NULL)
   15366 		{
   15367 		  flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
   15368 				    | SEC_LINKER_CREATED | SEC_READONLY);
   15369 
   15370 		  rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
   15371 								   ".rtproc",
   15372 								   flags);
   15373 		  if (rtproc_sec == NULL
   15374 		      || !bfd_set_section_alignment (rtproc_sec, 4))
   15375 		    return false;
   15376 		}
   15377 
   15378 	      if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
   15379 						     info, rtproc_sec,
   15380 						     &debug))
   15381 		return false;
   15382 	    }
   15383 
   15384 	  /* Build the external symbol information.  */
   15385 	  einfo.abfd = abfd;
   15386 	  einfo.info = info;
   15387 	  einfo.debug = &debug;
   15388 	  einfo.swap = swap;
   15389 	  einfo.failed = false;
   15390 	  mips_elf_link_hash_traverse (mips_elf_hash_table (info),
   15391 				       mips_elf_output_extsym, &einfo);
   15392 	  if (einfo.failed)
   15393 	    return false;
   15394 
   15395 	  /* Set the size of the .mdebug section.  */
   15396 	  o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
   15397 
   15398 	  /* Skip this section later on (I don't think this currently
   15399 	     matters, but someday it might).  */
   15400 	  o->map_head.link_order = NULL;
   15401 
   15402 	  mdebug_sec = o;
   15403 	}
   15404 
   15405       if (startswith (o->name, ".gptab."))
   15406 	{
   15407 	  const char *subname;
   15408 	  unsigned int c;
   15409 	  Elf32_gptab *tab;
   15410 	  Elf32_External_gptab *ext_tab;
   15411 	  unsigned int j;
   15412 
   15413 	  /* The .gptab.sdata and .gptab.sbss sections hold
   15414 	     information describing how the small data area would
   15415 	     change depending upon the -G switch.  These sections
   15416 	     not used in executables files.  */
   15417 	  if (! bfd_link_relocatable (info))
   15418 	    {
   15419 	      for (p = o->map_head.link_order; p != NULL; p = p->next)
   15420 		{
   15421 		  asection *input_section;
   15422 
   15423 		  if (p->type != bfd_indirect_link_order)
   15424 		    {
   15425 		      if (p->type == bfd_data_link_order)
   15426 			continue;
   15427 		      abort ();
   15428 		    }
   15429 
   15430 		  input_section = p->u.indirect.section;
   15431 
   15432 		  /* Hack: reset the SEC_HAS_CONTENTS flag so that
   15433 		     elf_link_input_bfd ignores this section.  */
   15434 		  input_section->flags &= ~SEC_HAS_CONTENTS;
   15435 		}
   15436 
   15437 	      /* Skip this section later on (I don't think this
   15438 		 currently matters, but someday it might).  */
   15439 	      o->map_head.link_order = NULL;
   15440 
   15441 	      /* Really remove the section.  */
   15442 	      bfd_section_list_remove (abfd, o);
   15443 	      --abfd->section_count;
   15444 
   15445 	      continue;
   15446 	    }
   15447 
   15448 	  /* There is one gptab for initialized data, and one for
   15449 	     uninitialized data.  */
   15450 	  if (strcmp (o->name, ".gptab.sdata") == 0)
   15451 	    gptab_data_sec = o;
   15452 	  else if (strcmp (o->name, ".gptab.sbss") == 0)
   15453 	    gptab_bss_sec = o;
   15454 	  else
   15455 	    {
   15456 	      _bfd_error_handler
   15457 		/* xgettext:c-format */
   15458 		(_("%pB: illegal section name `%pA'"), abfd, o);
   15459 	      bfd_set_error (bfd_error_nonrepresentable_section);
   15460 	      return false;
   15461 	    }
   15462 
   15463 	  /* The linker script always combines .gptab.data and
   15464 	     .gptab.sdata into .gptab.sdata, and likewise for
   15465 	     .gptab.bss and .gptab.sbss.  It is possible that there is
   15466 	     no .sdata or .sbss section in the output file, in which
   15467 	     case we must change the name of the output section.  */
   15468 	  subname = o->name + sizeof ".gptab" - 1;
   15469 	  if (bfd_get_section_by_name (abfd, subname) == NULL)
   15470 	    {
   15471 	      if (o == gptab_data_sec)
   15472 		o->name = ".gptab.data";
   15473 	      else
   15474 		o->name = ".gptab.bss";
   15475 	      subname = o->name + sizeof ".gptab" - 1;
   15476 	      BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
   15477 	    }
   15478 
   15479 	  /* Set up the first entry.  */
   15480 	  c = 1;
   15481 	  amt = c * sizeof (Elf32_gptab);
   15482 	  tab = bfd_malloc (amt);
   15483 	  if (tab == NULL)
   15484 	    return false;
   15485 	  tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
   15486 	  tab[0].gt_header.gt_unused = 0;
   15487 
   15488 	  /* Combine the input sections.  */
   15489 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   15490 	    {
   15491 	      asection *input_section;
   15492 	      bfd *input_bfd;
   15493 	      bfd_size_type size;
   15494 	      unsigned long last;
   15495 	      bfd_size_type gpentry;
   15496 
   15497 	      if (p->type != bfd_indirect_link_order)
   15498 		{
   15499 		  if (p->type == bfd_data_link_order)
   15500 		    continue;
   15501 		  abort ();
   15502 		}
   15503 
   15504 	      input_section = p->u.indirect.section;
   15505 	      input_bfd = input_section->owner;
   15506 
   15507 	      /* Combine the gptab entries for this input section one
   15508 		 by one.  We know that the input gptab entries are
   15509 		 sorted by ascending -G value.  */
   15510 	      size = input_section->size;
   15511 	      last = 0;
   15512 	      for (gpentry = sizeof (Elf32_External_gptab);
   15513 		   gpentry < size;
   15514 		   gpentry += sizeof (Elf32_External_gptab))
   15515 		{
   15516 		  Elf32_External_gptab ext_gptab;
   15517 		  Elf32_gptab int_gptab;
   15518 		  unsigned long val;
   15519 		  unsigned long add;
   15520 		  bool exact;
   15521 		  unsigned int look;
   15522 
   15523 		  if (! (bfd_get_section_contents
   15524 			 (input_bfd, input_section, &ext_gptab, gpentry,
   15525 			  sizeof (Elf32_External_gptab))))
   15526 		    {
   15527 		      free (tab);
   15528 		      return false;
   15529 		    }
   15530 
   15531 		  bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
   15532 						&int_gptab);
   15533 		  val = int_gptab.gt_entry.gt_g_value;
   15534 		  add = int_gptab.gt_entry.gt_bytes - last;
   15535 
   15536 		  exact = false;
   15537 		  for (look = 1; look < c; look++)
   15538 		    {
   15539 		      if (tab[look].gt_entry.gt_g_value >= val)
   15540 			tab[look].gt_entry.gt_bytes += add;
   15541 
   15542 		      if (tab[look].gt_entry.gt_g_value == val)
   15543 			exact = true;
   15544 		    }
   15545 
   15546 		  if (! exact)
   15547 		    {
   15548 		      Elf32_gptab *new_tab;
   15549 		      unsigned int max;
   15550 
   15551 		      /* We need a new table entry.  */
   15552 		      amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
   15553 		      new_tab = bfd_realloc (tab, amt);
   15554 		      if (new_tab == NULL)
   15555 			{
   15556 			  free (tab);
   15557 			  return false;
   15558 			}
   15559 		      tab = new_tab;
   15560 		      tab[c].gt_entry.gt_g_value = val;
   15561 		      tab[c].gt_entry.gt_bytes = add;
   15562 
   15563 		      /* Merge in the size for the next smallest -G
   15564 			 value, since that will be implied by this new
   15565 			 value.  */
   15566 		      max = 0;
   15567 		      for (look = 1; look < c; look++)
   15568 			{
   15569 			  if (tab[look].gt_entry.gt_g_value < val
   15570 			      && (max == 0
   15571 				  || (tab[look].gt_entry.gt_g_value
   15572 				      > tab[max].gt_entry.gt_g_value)))
   15573 			    max = look;
   15574 			}
   15575 		      if (max != 0)
   15576 			tab[c].gt_entry.gt_bytes +=
   15577 			  tab[max].gt_entry.gt_bytes;
   15578 
   15579 		      ++c;
   15580 		    }
   15581 
   15582 		  last = int_gptab.gt_entry.gt_bytes;
   15583 		}
   15584 
   15585 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   15586 		 elf_link_input_bfd ignores this section.  */
   15587 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   15588 	    }
   15589 
   15590 	  /* The table must be sorted by -G value.  */
   15591 	  if (c > 2)
   15592 	    qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
   15593 
   15594 	  /* Swap out the table.  */
   15595 	  amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
   15596 	  ext_tab = bfd_alloc (abfd, amt);
   15597 	  if (ext_tab == NULL)
   15598 	    {
   15599 	      free (tab);
   15600 	      return false;
   15601 	    }
   15602 
   15603 	  for (j = 0; j < c; j++)
   15604 	    bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
   15605 	  free (tab);
   15606 
   15607 	  o->size = c * sizeof (Elf32_External_gptab);
   15608 	  o->contents = (bfd_byte *) ext_tab;
   15609 	  o->alloced = 1;
   15610 
   15611 	  /* Skip this section later on (I don't think this currently
   15612 	     matters, but someday it might).  */
   15613 	  o->map_head.link_order = NULL;
   15614 	}
   15615     }
   15616 
   15617   /* Invoke the regular ELF backend linker to do all the work.  */
   15618   if (!_bfd_elf_final_link (abfd, info))
   15619     return false;
   15620 
   15621   /* Now write out the computed sections.  */
   15622 
   15623   if (abiflags_sec != NULL)
   15624     {
   15625       Elf_External_ABIFlags_v0 ext;
   15626       Elf_Internal_ABIFlags_v0 *abiflags;
   15627 
   15628       abiflags = &mips_elf_tdata (abfd)->abiflags;
   15629 
   15630       /* Set up the abiflags if no valid input sections were found.  */
   15631       if (!mips_elf_tdata (abfd)->abiflags_valid)
   15632 	{
   15633 	  infer_mips_abiflags (abfd, abiflags);
   15634 	  mips_elf_tdata (abfd)->abiflags_valid = true;
   15635 	}
   15636       bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
   15637       if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
   15638 	return false;
   15639     }
   15640 
   15641   if (reginfo_sec != NULL)
   15642     {
   15643       Elf32_External_RegInfo ext;
   15644 
   15645       bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
   15646       if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
   15647 	return false;
   15648     }
   15649 
   15650   if (mdebug_sec != NULL)
   15651     {
   15652       BFD_ASSERT (abfd->output_has_begun);
   15653       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
   15654 					       swap, info,
   15655 					       mdebug_sec->filepos))
   15656 	return false;
   15657 
   15658       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
   15659     }
   15660 
   15661   if (gptab_data_sec != NULL)
   15662     {
   15663       if (! bfd_set_section_contents (abfd, gptab_data_sec,
   15664 				      gptab_data_sec->contents,
   15665 				      0, gptab_data_sec->size))
   15666 	return false;
   15667     }
   15668 
   15669   if (gptab_bss_sec != NULL)
   15670     {
   15671       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
   15672 				      gptab_bss_sec->contents,
   15673 				      0, gptab_bss_sec->size))
   15674 	return false;
   15675     }
   15676 
   15677   if (SGI_COMPAT (abfd))
   15678     {
   15679       rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
   15680       if (rtproc_sec != NULL)
   15681 	{
   15682 	  if (! bfd_set_section_contents (abfd, rtproc_sec,
   15683 					  rtproc_sec->contents,
   15684 					  0, rtproc_sec->size))
   15685 	    return false;
   15686 	}
   15687     }
   15688 
   15689   return true;
   15690 }
   15691 
   15692 /* Merge object file header flags from IBFD into OBFD.  Raise an error
   15694    if there are conflicting settings.  */
   15695 
   15696 static bool
   15697 mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
   15698 {
   15699   bfd *obfd = info->output_bfd;
   15700   struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
   15701   flagword old_flags;
   15702   flagword new_flags;
   15703   bool ok;
   15704 
   15705   new_flags = elf_elfheader (ibfd)->e_flags;
   15706   elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
   15707   old_flags = elf_elfheader (obfd)->e_flags;
   15708 
   15709   /* Check flag compatibility.  */
   15710 
   15711   new_flags &= ~EF_MIPS_NOREORDER;
   15712   old_flags &= ~EF_MIPS_NOREORDER;
   15713 
   15714   /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
   15715      doesn't seem to matter.  */
   15716   new_flags &= ~EF_MIPS_XGOT;
   15717   old_flags &= ~EF_MIPS_XGOT;
   15718 
   15719   /* MIPSpro generates ucode info in n64 objects.  Again, we should
   15720      just be able to ignore this.  */
   15721   new_flags &= ~EF_MIPS_UCODE;
   15722   old_flags &= ~EF_MIPS_UCODE;
   15723 
   15724   /* DSOs should only be linked with CPIC code.  */
   15725   if ((ibfd->flags & DYNAMIC) != 0)
   15726     new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
   15727 
   15728   if (new_flags == old_flags)
   15729     return true;
   15730 
   15731   ok = true;
   15732 
   15733   if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
   15734       != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
   15735     {
   15736       _bfd_error_handler
   15737 	(_("%pB: warning: linking abicalls files with non-abicalls files"),
   15738 	 ibfd);
   15739       ok = true;
   15740     }
   15741 
   15742   if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
   15743     elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
   15744   if (! (new_flags & EF_MIPS_PIC))
   15745     elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
   15746 
   15747   new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
   15748   old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
   15749 
   15750   /* Compare the ISAs.  */
   15751   if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
   15752     {
   15753       _bfd_error_handler
   15754 	(_("%pB: linking 32-bit code with 64-bit code"),
   15755 	 ibfd);
   15756       ok = false;
   15757     }
   15758   else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
   15759     {
   15760       /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
   15761       if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
   15762 	{
   15763 	  /* Copy the architecture info from IBFD to OBFD.  Also copy
   15764 	     the 32-bit flag (if set) so that we continue to recognise
   15765 	     OBFD as a 32-bit binary.  */
   15766 	  bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
   15767 	  elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
   15768 	  elf_elfheader (obfd)->e_flags
   15769 	    |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15770 
   15771 	  /* Update the ABI flags isa_level, isa_rev, isa_ext fields.  */
   15772 	  update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
   15773 
   15774 	  /* Copy across the ABI flags if OBFD doesn't use them
   15775 	     and if that was what caused us to treat IBFD as 32-bit.  */
   15776 	  if ((old_flags & EF_MIPS_ABI) == 0
   15777 	      && mips_32bit_flags_p (new_flags)
   15778 	      && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
   15779 	    elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
   15780 	}
   15781       else
   15782 	{
   15783 	  /* The ISAs aren't compatible.  */
   15784 	  _bfd_error_handler
   15785 	    /* xgettext:c-format */
   15786 	    (_("%pB: linking %s module with previous %s modules"),
   15787 	     ibfd,
   15788 	     bfd_printable_name (ibfd),
   15789 	     bfd_printable_name (obfd));
   15790 	  ok = false;
   15791 	}
   15792     }
   15793 
   15794   new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15795   old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15796 
   15797   /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
   15798      does set EI_CLASS differently from any 32-bit ABI.  */
   15799   if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
   15800       || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   15801 	  != elf_elfheader (obfd)->e_ident[EI_CLASS]))
   15802     {
   15803       /* Only error if both are set (to different values).  */
   15804       if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
   15805 	  || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   15806 	      != elf_elfheader (obfd)->e_ident[EI_CLASS]))
   15807 	{
   15808 	  _bfd_error_handler
   15809 	    /* xgettext:c-format */
   15810 	    (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
   15811 	     ibfd,
   15812 	     elf_mips_abi_name (ibfd),
   15813 	     elf_mips_abi_name (obfd));
   15814 	  ok = false;
   15815 	}
   15816       new_flags &= ~EF_MIPS_ABI;
   15817       old_flags &= ~EF_MIPS_ABI;
   15818     }
   15819 
   15820   /* Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
   15821      and allow arbitrary mixing of the remaining ASEs (retain the union).  */
   15822   if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
   15823     {
   15824       int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
   15825       int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
   15826       int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
   15827       int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
   15828       int micro_mis = old_m16 && new_micro;
   15829       int m16_mis = old_micro && new_m16;
   15830 
   15831       if (m16_mis || micro_mis)
   15832 	{
   15833 	  _bfd_error_handler
   15834 	    /* xgettext:c-format */
   15835 	    (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
   15836 	     ibfd,
   15837 	     m16_mis ? "MIPS16" : "microMIPS",
   15838 	     m16_mis ? "microMIPS" : "MIPS16");
   15839 	  ok = false;
   15840 	}
   15841 
   15842       elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
   15843 
   15844       new_flags &= ~ EF_MIPS_ARCH_ASE;
   15845       old_flags &= ~ EF_MIPS_ARCH_ASE;
   15846     }
   15847 
   15848   /* Compare NaN encodings.  */
   15849   if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
   15850     {
   15851       /* xgettext:c-format */
   15852       _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
   15853 			  ibfd,
   15854 			  (new_flags & EF_MIPS_NAN2008
   15855 			   ? "-mnan=2008" : "-mnan=legacy"),
   15856 			  (old_flags & EF_MIPS_NAN2008
   15857 			   ? "-mnan=2008" : "-mnan=legacy"));
   15858       ok = false;
   15859       new_flags &= ~EF_MIPS_NAN2008;
   15860       old_flags &= ~EF_MIPS_NAN2008;
   15861     }
   15862 
   15863   /* Compare FP64 state.  */
   15864   if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
   15865     {
   15866       /* xgettext:c-format */
   15867       _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
   15868 			  ibfd,
   15869 			  (new_flags & EF_MIPS_FP64
   15870 			   ? "-mfp64" : "-mfp32"),
   15871 			  (old_flags & EF_MIPS_FP64
   15872 			   ? "-mfp64" : "-mfp32"));
   15873       ok = false;
   15874       new_flags &= ~EF_MIPS_FP64;
   15875       old_flags &= ~EF_MIPS_FP64;
   15876     }
   15877 
   15878   /* Warn about any other mismatches */
   15879   if (new_flags != old_flags)
   15880     {
   15881       /* xgettext:c-format */
   15882       _bfd_error_handler
   15883 	(_("%pB: uses different e_flags (%#x) fields than previous modules "
   15884 	   "(%#x)"),
   15885 	 ibfd, new_flags, old_flags);
   15886       ok = false;
   15887     }
   15888 
   15889   return ok;
   15890 }
   15891 
   15892 /* Merge object attributes from IBFD into OBFD.  Raise an error if
   15893    there are conflicting attributes.  */
   15894 static bool
   15895 mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
   15896 {
   15897   bfd *obfd = info->output_bfd;
   15898   obj_attribute *in_attr;
   15899   obj_attribute *out_attr;
   15900   bfd *abi_fp_bfd;
   15901   bfd *abi_msa_bfd;
   15902 
   15903   abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
   15904   in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   15905   if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
   15906     mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   15907 
   15908   abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
   15909   if (!abi_msa_bfd
   15910       && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
   15911     mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
   15912 
   15913   if (!elf_known_obj_attributes_proc (obfd)[0].i)
   15914     {
   15915       /* This is the first object.  Copy the attributes.  */
   15916       _bfd_elf_copy_obj_attributes (ibfd, obfd);
   15917 
   15918       /* Use the Tag_null value to indicate the attributes have been
   15919 	 initialized.  */
   15920       elf_known_obj_attributes_proc (obfd)[0].i = 1;
   15921 
   15922       return true;
   15923     }
   15924 
   15925   /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
   15926      non-conflicting ones.  */
   15927   out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   15928   if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
   15929     {
   15930       int out_fp, in_fp;
   15931 
   15932       out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
   15933       in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   15934       out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
   15935       if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
   15936 	out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
   15937       else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
   15938 	       && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
   15939 		   || in_fp == Val_GNU_MIPS_ABI_FP_64
   15940 		   || in_fp == Val_GNU_MIPS_ABI_FP_64A))
   15941 	{
   15942 	  mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   15943 	  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   15944 	}
   15945       else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
   15946 	       && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
   15947 		   || out_fp == Val_GNU_MIPS_ABI_FP_64
   15948 		   || out_fp == Val_GNU_MIPS_ABI_FP_64A))
   15949 	/* Keep the current setting.  */;
   15950       else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
   15951 	       && in_fp == Val_GNU_MIPS_ABI_FP_64)
   15952 	{
   15953 	  mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   15954 	  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   15955 	}
   15956       else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
   15957 	       && out_fp == Val_GNU_MIPS_ABI_FP_64)
   15958 	/* Keep the current setting.  */;
   15959       else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
   15960 	{
   15961 	  const char *out_string, *in_string;
   15962 
   15963 	  out_string = _bfd_mips_fp_abi_string (out_fp);
   15964 	  in_string = _bfd_mips_fp_abi_string (in_fp);
   15965 	  /* First warn about cases involving unrecognised ABIs.  */
   15966 	  if (!out_string && !in_string)
   15967 	    /* xgettext:c-format */
   15968 	    _bfd_error_handler
   15969 	      (_("warning: %pB uses unknown floating point ABI %d "
   15970 		 "(set by %pB), %pB uses unknown floating point ABI %d"),
   15971 	       obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
   15972 	  else if (!out_string)
   15973 	    _bfd_error_handler
   15974 	      /* xgettext:c-format */
   15975 	      (_("warning: %pB uses unknown floating point ABI %d "
   15976 		 "(set by %pB), %pB uses %s"),
   15977 	       obfd, out_fp, abi_fp_bfd, ibfd, in_string);
   15978 	  else if (!in_string)
   15979 	    _bfd_error_handler
   15980 	      /* xgettext:c-format */
   15981 	      (_("warning: %pB uses %s (set by %pB), "
   15982 		 "%pB uses unknown floating point ABI %d"),
   15983 	       obfd, out_string, abi_fp_bfd, ibfd, in_fp);
   15984 	  else
   15985 	    {
   15986 	      /* If one of the bfds is soft-float, the other must be
   15987 		 hard-float.  The exact choice of hard-float ABI isn't
   15988 		 really relevant to the error message.  */
   15989 	      if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
   15990 		out_string = "-mhard-float";
   15991 	      else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
   15992 		in_string = "-mhard-float";
   15993 	      _bfd_error_handler
   15994 		/* xgettext:c-format */
   15995 		(_("warning: %pB uses %s (set by %pB), %pB uses %s"),
   15996 		 obfd, out_string, abi_fp_bfd, ibfd, in_string);
   15997 	    }
   15998 	}
   15999     }
   16000 
   16001   /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
   16002      non-conflicting ones.  */
   16003   if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
   16004     {
   16005       out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
   16006       if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
   16007 	out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
   16008       else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
   16009 	switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
   16010 	  {
   16011 	  case Val_GNU_MIPS_ABI_MSA_128:
   16012 	    _bfd_error_handler
   16013 	      /* xgettext:c-format */
   16014 	      (_("warning: %pB uses %s (set by %pB), "
   16015 		 "%pB uses unknown MSA ABI %d"),
   16016 	       obfd, "-mmsa", abi_msa_bfd,
   16017 	       ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
   16018 	    break;
   16019 
   16020 	  default:
   16021 	    switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
   16022 	      {
   16023 	      case Val_GNU_MIPS_ABI_MSA_128:
   16024 		_bfd_error_handler
   16025 		  /* xgettext:c-format */
   16026 		  (_("warning: %pB uses unknown MSA ABI %d "
   16027 		     "(set by %pB), %pB uses %s"),
   16028 		     obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
   16029 		   abi_msa_bfd, ibfd, "-mmsa");
   16030 		  break;
   16031 
   16032 	      default:
   16033 		_bfd_error_handler
   16034 		  /* xgettext:c-format */
   16035 		  (_("warning: %pB uses unknown MSA ABI %d "
   16036 		     "(set by %pB), %pB uses unknown MSA ABI %d"),
   16037 		   obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
   16038 		   abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
   16039 		break;
   16040 	      }
   16041 	  }
   16042     }
   16043 
   16044   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   16045   return _bfd_elf_merge_object_attributes (ibfd, info);
   16046 }
   16047 
   16048 /* Merge object ABI flags from IBFD into OBFD.  Raise an error if
   16049    there are conflicting settings.  */
   16050 
   16051 static bool
   16052 mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
   16053 {
   16054   obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   16055   struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
   16056   struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
   16057 
   16058   /* Update the output abiflags fp_abi using the computed fp_abi.  */
   16059   out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
   16060 
   16061 #define max(a, b) ((a) > (b) ? (a) : (b))
   16062   /* Merge abiflags.  */
   16063   out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
   16064 				       in_tdata->abiflags.isa_level);
   16065   out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
   16066 				     in_tdata->abiflags.isa_rev);
   16067   out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
   16068 				      in_tdata->abiflags.gpr_size);
   16069   out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
   16070 				       in_tdata->abiflags.cpr1_size);
   16071   out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
   16072 				       in_tdata->abiflags.cpr2_size);
   16073 #undef max
   16074   out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
   16075   out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
   16076 
   16077   return true;
   16078 }
   16079 
   16080 /* Merge backend specific data from an object file to the output
   16081    object file when linking.  */
   16082 
   16083 bool
   16084 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   16085 {
   16086   bfd *obfd = info->output_bfd;
   16087   struct mips_elf_obj_tdata *out_tdata;
   16088   struct mips_elf_obj_tdata *in_tdata;
   16089   bool null_input_bfd = true;
   16090   asection *sec;
   16091   bool ok;
   16092 
   16093   /* Check if we have the same endianness.  */
   16094   if (! _bfd_generic_verify_endian_match (ibfd, info))
   16095     {
   16096       _bfd_error_handler
   16097 	(_("%pB: endianness incompatible with that of the selected emulation"),
   16098 	 ibfd);
   16099       return false;
   16100     }
   16101 
   16102   if (!is_mips_elf (ibfd))
   16103     return true;
   16104 
   16105   in_tdata = mips_elf_tdata (ibfd);
   16106   out_tdata = mips_elf_tdata (obfd);
   16107 
   16108   if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
   16109     {
   16110       _bfd_error_handler
   16111 	(_("%pB: ABI is incompatible with that of the selected emulation"),
   16112 	 ibfd);
   16113       return false;
   16114     }
   16115 
   16116   /* Check to see if the input BFD actually contains any sections.  If not,
   16117      then it has no attributes, and its flags may not have been initialized
   16118      either, but it cannot actually cause any incompatibility.  */
   16119   /* FIXME: This excludes any input shared library from consideration.  */
   16120   for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   16121     {
   16122       /* Ignore synthetic sections and empty .text, .data and .bss sections
   16123 	 which are automatically generated by gas.  Also ignore fake
   16124 	 (s)common sections, since merely defining a common symbol does
   16125 	 not affect compatibility.  */
   16126       if ((sec->flags & SEC_IS_COMMON) == 0
   16127 	  && strcmp (sec->name, ".reginfo")
   16128 	  && strcmp (sec->name, ".mdebug")
   16129 	  && (sec->size != 0
   16130 	      || (strcmp (sec->name, ".text")
   16131 		  && strcmp (sec->name, ".data")
   16132 		  && strcmp (sec->name, ".bss"))))
   16133 	{
   16134 	  null_input_bfd = false;
   16135 	  break;
   16136 	}
   16137     }
   16138   if (null_input_bfd)
   16139     return true;
   16140 
   16141   /* Populate abiflags using existing information.  */
   16142   if (in_tdata->abiflags_valid)
   16143     {
   16144       obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   16145       Elf_Internal_ABIFlags_v0 in_abiflags;
   16146       Elf_Internal_ABIFlags_v0 abiflags;
   16147 
   16148       /* Set up the FP ABI attribute from the abiflags if it is not already
   16149 	 set.  */
   16150       if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
   16151 	in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
   16152 
   16153       infer_mips_abiflags (ibfd, &abiflags);
   16154       in_abiflags = in_tdata->abiflags;
   16155 
   16156       /* It is not possible to infer the correct ISA revision
   16157 	 for R3 or R5 so drop down to R2 for the checks.  */
   16158       if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
   16159 	in_abiflags.isa_rev = 2;
   16160 
   16161       if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
   16162 	  < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
   16163 	_bfd_error_handler
   16164 	  (_("%pB: warning: inconsistent ISA between e_flags and "
   16165 	     ".MIPS.abiflags"), ibfd);
   16166       if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
   16167 	  && in_abiflags.fp_abi != abiflags.fp_abi)
   16168 	_bfd_error_handler
   16169 	  (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
   16170 	     ".MIPS.abiflags"), ibfd);
   16171       if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
   16172 	_bfd_error_handler
   16173 	  (_("%pB: warning: inconsistent ASEs between e_flags and "
   16174 	     ".MIPS.abiflags"), ibfd);
   16175       /* The isa_ext is allowed to be an extension of what can be inferred
   16176 	 from e_flags.  */
   16177       if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
   16178 				bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
   16179 	_bfd_error_handler
   16180 	  (_("%pB: warning: inconsistent ISA extensions between e_flags and "
   16181 	     ".MIPS.abiflags"), ibfd);
   16182       if (in_abiflags.flags2 != 0)
   16183 	_bfd_error_handler
   16184 	  (_("%pB: warning: unexpected flag in the flags2 field of "
   16185 	     ".MIPS.abiflags (0x%lx)"), ibfd,
   16186 	   in_abiflags.flags2);
   16187     }
   16188   else
   16189     {
   16190       infer_mips_abiflags (ibfd, &in_tdata->abiflags);
   16191       in_tdata->abiflags_valid = true;
   16192     }
   16193 
   16194   if (!out_tdata->abiflags_valid)
   16195     {
   16196       /* Copy input abiflags if output abiflags are not already valid.  */
   16197       out_tdata->abiflags = in_tdata->abiflags;
   16198       out_tdata->abiflags_valid = true;
   16199     }
   16200 
   16201   if (! elf_flags_init (obfd))
   16202     {
   16203       elf_flags_init (obfd) = true;
   16204       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   16205       elf_elfheader (obfd)->e_ident[EI_CLASS]
   16206 	= elf_elfheader (ibfd)->e_ident[EI_CLASS];
   16207 
   16208       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   16209 	  && (bfd_get_arch_info (obfd)->the_default
   16210 	      || mips_mach_extends_p (bfd_get_mach (obfd),
   16211 				      bfd_get_mach (ibfd))))
   16212 	{
   16213 	  if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
   16214 				   bfd_get_mach (ibfd)))
   16215 	    return false;
   16216 
   16217 	  /* Update the ABI flags isa_level, isa_rev and isa_ext fields.  */
   16218 	  update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
   16219 	}
   16220 
   16221       ok = true;
   16222     }
   16223   else
   16224     ok = mips_elf_merge_obj_e_flags (ibfd, info);
   16225 
   16226   ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
   16227 
   16228   ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
   16229 
   16230   if (!ok)
   16231     {
   16232       bfd_set_error (bfd_error_bad_value);
   16233       return false;
   16234     }
   16235 
   16236   return true;
   16237 }
   16238 
   16239 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
   16240 
   16241 bool
   16242 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
   16243 {
   16244   BFD_ASSERT (!elf_flags_init (abfd)
   16245 	      || elf_elfheader (abfd)->e_flags == flags);
   16246 
   16247   elf_elfheader (abfd)->e_flags = flags;
   16248   elf_flags_init (abfd) = true;
   16249   return true;
   16250 }
   16251 
   16252 char *
   16253 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
   16254 {
   16255   switch (dtag)
   16256     {
   16257     default: return "";
   16258     case DT_MIPS_RLD_VERSION:
   16259       return "MIPS_RLD_VERSION";
   16260     case DT_MIPS_TIME_STAMP:
   16261       return "MIPS_TIME_STAMP";
   16262     case DT_MIPS_ICHECKSUM:
   16263       return "MIPS_ICHECKSUM";
   16264     case DT_MIPS_IVERSION:
   16265       return "MIPS_IVERSION";
   16266     case DT_MIPS_FLAGS:
   16267       return "MIPS_FLAGS";
   16268     case DT_MIPS_BASE_ADDRESS:
   16269       return "MIPS_BASE_ADDRESS";
   16270     case DT_MIPS_MSYM:
   16271       return "MIPS_MSYM";
   16272     case DT_MIPS_CONFLICT:
   16273       return "MIPS_CONFLICT";
   16274     case DT_MIPS_LIBLIST:
   16275       return "MIPS_LIBLIST";
   16276     case DT_MIPS_LOCAL_GOTNO:
   16277       return "MIPS_LOCAL_GOTNO";
   16278     case DT_MIPS_CONFLICTNO:
   16279       return "MIPS_CONFLICTNO";
   16280     case DT_MIPS_LIBLISTNO:
   16281       return "MIPS_LIBLISTNO";
   16282     case DT_MIPS_SYMTABNO:
   16283       return "MIPS_SYMTABNO";
   16284     case DT_MIPS_UNREFEXTNO:
   16285       return "MIPS_UNREFEXTNO";
   16286     case DT_MIPS_GOTSYM:
   16287       return "MIPS_GOTSYM";
   16288     case DT_MIPS_HIPAGENO:
   16289       return "MIPS_HIPAGENO";
   16290     case DT_MIPS_RLD_MAP:
   16291       return "MIPS_RLD_MAP";
   16292     case DT_MIPS_RLD_MAP_REL:
   16293       return "MIPS_RLD_MAP_REL";
   16294     case DT_MIPS_DELTA_CLASS:
   16295       return "MIPS_DELTA_CLASS";
   16296     case DT_MIPS_DELTA_CLASS_NO:
   16297       return "MIPS_DELTA_CLASS_NO";
   16298     case DT_MIPS_DELTA_INSTANCE:
   16299       return "MIPS_DELTA_INSTANCE";
   16300     case DT_MIPS_DELTA_INSTANCE_NO:
   16301       return "MIPS_DELTA_INSTANCE_NO";
   16302     case DT_MIPS_DELTA_RELOC:
   16303       return "MIPS_DELTA_RELOC";
   16304     case DT_MIPS_DELTA_RELOC_NO:
   16305       return "MIPS_DELTA_RELOC_NO";
   16306     case DT_MIPS_DELTA_SYM:
   16307       return "MIPS_DELTA_SYM";
   16308     case DT_MIPS_DELTA_SYM_NO:
   16309       return "MIPS_DELTA_SYM_NO";
   16310     case DT_MIPS_DELTA_CLASSSYM:
   16311       return "MIPS_DELTA_CLASSSYM";
   16312     case DT_MIPS_DELTA_CLASSSYM_NO:
   16313       return "MIPS_DELTA_CLASSSYM_NO";
   16314     case DT_MIPS_CXX_FLAGS:
   16315       return "MIPS_CXX_FLAGS";
   16316     case DT_MIPS_PIXIE_INIT:
   16317       return "MIPS_PIXIE_INIT";
   16318     case DT_MIPS_SYMBOL_LIB:
   16319       return "MIPS_SYMBOL_LIB";
   16320     case DT_MIPS_LOCALPAGE_GOTIDX:
   16321       return "MIPS_LOCALPAGE_GOTIDX";
   16322     case DT_MIPS_LOCAL_GOTIDX:
   16323       return "MIPS_LOCAL_GOTIDX";
   16324     case DT_MIPS_HIDDEN_GOTIDX:
   16325       return "MIPS_HIDDEN_GOTIDX";
   16326     case DT_MIPS_PROTECTED_GOTIDX:
   16327       return "MIPS_PROTECTED_GOT_IDX";
   16328     case DT_MIPS_OPTIONS:
   16329       return "MIPS_OPTIONS";
   16330     case DT_MIPS_INTERFACE:
   16331       return "MIPS_INTERFACE";
   16332     case DT_MIPS_DYNSTR_ALIGN:
   16333       return "DT_MIPS_DYNSTR_ALIGN";
   16334     case DT_MIPS_INTERFACE_SIZE:
   16335       return "DT_MIPS_INTERFACE_SIZE";
   16336     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
   16337       return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
   16338     case DT_MIPS_PERF_SUFFIX:
   16339       return "DT_MIPS_PERF_SUFFIX";
   16340     case DT_MIPS_COMPACT_SIZE:
   16341       return "DT_MIPS_COMPACT_SIZE";
   16342     case DT_MIPS_GP_VALUE:
   16343       return "DT_MIPS_GP_VALUE";
   16344     case DT_MIPS_AUX_DYNAMIC:
   16345       return "DT_MIPS_AUX_DYNAMIC";
   16346     case DT_MIPS_PLTGOT:
   16347       return "DT_MIPS_PLTGOT";
   16348     case DT_MIPS_RWPLT:
   16349       return "DT_MIPS_RWPLT";
   16350     case DT_MIPS_XHASH:
   16351       return "DT_MIPS_XHASH";
   16352     }
   16353 }
   16354 
   16355 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
   16356    not known.  */
   16357 
   16358 const char *
   16359 _bfd_mips_fp_abi_string (int fp)
   16360 {
   16361   switch (fp)
   16362     {
   16363       /* These strings aren't translated because they're simply
   16364 	 option lists.  */
   16365     case Val_GNU_MIPS_ABI_FP_DOUBLE:
   16366       return "-mdouble-float";
   16367 
   16368     case Val_GNU_MIPS_ABI_FP_SINGLE:
   16369       return "-msingle-float";
   16370 
   16371     case Val_GNU_MIPS_ABI_FP_SOFT:
   16372       return "-msoft-float";
   16373 
   16374     case Val_GNU_MIPS_ABI_FP_OLD_64:
   16375       return _("-mips32r2 -mfp64 (12 callee-saved)");
   16376 
   16377     case Val_GNU_MIPS_ABI_FP_XX:
   16378       return "-mfpxx";
   16379 
   16380     case Val_GNU_MIPS_ABI_FP_64:
   16381       return "-mgp32 -mfp64";
   16382 
   16383     case Val_GNU_MIPS_ABI_FP_64A:
   16384       return "-mgp32 -mfp64 -mno-odd-spreg";
   16385 
   16386     default:
   16387       return 0;
   16388     }
   16389 }
   16390 
   16391 static void
   16392 print_mips_ases (FILE *file, unsigned int mask)
   16393 {
   16394   if (mask & AFL_ASE_DSP)
   16395     fputs ("\n\tDSP ASE", file);
   16396   if (mask & AFL_ASE_DSPR2)
   16397     fputs ("\n\tDSP R2 ASE", file);
   16398   if (mask & AFL_ASE_DSPR3)
   16399     fputs ("\n\tDSP R3 ASE", file);
   16400   if (mask & AFL_ASE_EVA)
   16401     fputs ("\n\tEnhanced VA Scheme", file);
   16402   if (mask & AFL_ASE_MCU)
   16403     fputs ("\n\tMCU (MicroController) ASE", file);
   16404   if (mask & AFL_ASE_MDMX)
   16405     fputs ("\n\tMDMX ASE", file);
   16406   if (mask & AFL_ASE_MIPS3D)
   16407     fputs ("\n\tMIPS-3D ASE", file);
   16408   if (mask & AFL_ASE_MT)
   16409     fputs ("\n\tMT ASE", file);
   16410   if (mask & AFL_ASE_SMARTMIPS)
   16411     fputs ("\n\tSmartMIPS ASE", file);
   16412   if (mask & AFL_ASE_VIRT)
   16413     fputs ("\n\tVZ ASE", file);
   16414   if (mask & AFL_ASE_MSA)
   16415     fputs ("\n\tMSA ASE", file);
   16416   if (mask & AFL_ASE_MIPS16)
   16417     fputs ("\n\tMIPS16 ASE", file);
   16418   if (mask & AFL_ASE_MICROMIPS)
   16419     fputs ("\n\tMICROMIPS ASE", file);
   16420   if (mask & AFL_ASE_XPA)
   16421     fputs ("\n\tXPA ASE", file);
   16422   if (mask & AFL_ASE_MIPS16E2)
   16423     fputs ("\n\tMIPS16e2 ASE", file);
   16424   if (mask & AFL_ASE_CRC)
   16425     fputs ("\n\tCRC ASE", file);
   16426   if (mask & AFL_ASE_GINV)
   16427     fputs ("\n\tGINV ASE", file);
   16428   if (mask & AFL_ASE_LOONGSON_MMI)
   16429     fputs ("\n\tLoongson MMI ASE", file);
   16430   if (mask & AFL_ASE_LOONGSON_CAM)
   16431     fputs ("\n\tLoongson CAM ASE", file);
   16432   if (mask & AFL_ASE_LOONGSON_EXT)
   16433     fputs ("\n\tLoongson EXT ASE", file);
   16434   if (mask & AFL_ASE_LOONGSON_EXT2)
   16435     fputs ("\n\tLoongson EXT2 ASE", file);
   16436   if (mask == 0)
   16437     fprintf (file, "\n\t%s", _("None"));
   16438   else if ((mask & ~AFL_ASE_MASK) != 0)
   16439     fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
   16440 }
   16441 
   16442 static void
   16443 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
   16444 {
   16445   switch (isa_ext)
   16446     {
   16447     case 0:
   16448       fputs (_("None"), file);
   16449       break;
   16450     case AFL_EXT_XLR:
   16451       fputs ("RMI XLR", file);
   16452       break;
   16453     case AFL_EXT_OCTEON3:
   16454       fputs ("Cavium Networks Octeon3", file);
   16455       break;
   16456     case AFL_EXT_OCTEON2:
   16457       fputs ("Cavium Networks Octeon2", file);
   16458       break;
   16459     case AFL_EXT_OCTEONP:
   16460       fputs ("Cavium Networks OcteonP", file);
   16461       break;
   16462     case AFL_EXT_OCTEON:
   16463       fputs ("Cavium Networks Octeon", file);
   16464       break;
   16465     case AFL_EXT_5900:
   16466       fputs ("Toshiba R5900", file);
   16467       break;
   16468     case AFL_EXT_4650:
   16469       fputs ("MIPS R4650", file);
   16470       break;
   16471     case AFL_EXT_4010:
   16472       fputs ("LSI R4010", file);
   16473       break;
   16474     case AFL_EXT_4100:
   16475       fputs ("NEC VR4100", file);
   16476       break;
   16477     case AFL_EXT_3900:
   16478       fputs ("Toshiba R3900", file);
   16479       break;
   16480     case AFL_EXT_10000:
   16481       fputs ("MIPS R10000", file);
   16482       break;
   16483     case AFL_EXT_SB1:
   16484       fputs ("Broadcom SB-1", file);
   16485       break;
   16486     case AFL_EXT_4111:
   16487       fputs ("NEC VR4111/VR4181", file);
   16488       break;
   16489     case AFL_EXT_4120:
   16490       fputs ("NEC VR4120", file);
   16491       break;
   16492     case AFL_EXT_5400:
   16493       fputs ("NEC VR5400", file);
   16494       break;
   16495     case AFL_EXT_5500:
   16496       fputs ("NEC VR5500", file);
   16497       break;
   16498     case AFL_EXT_LOONGSON_2E:
   16499       fputs ("ST Microelectronics Loongson 2E", file);
   16500       break;
   16501     case AFL_EXT_LOONGSON_2F:
   16502       fputs ("ST Microelectronics Loongson 2F", file);
   16503       break;
   16504     case AFL_EXT_INTERAPTIV_MR2:
   16505       fputs ("Imagination interAptiv MR2", file);
   16506       break;
   16507     default:
   16508       fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
   16509       break;
   16510     }
   16511 }
   16512 
   16513 static void
   16514 print_mips_fp_abi_value (FILE *file, int val)
   16515 {
   16516   switch (val)
   16517     {
   16518     case Val_GNU_MIPS_ABI_FP_ANY:
   16519       fprintf (file, _("Hard or soft float\n"));
   16520       break;
   16521     case Val_GNU_MIPS_ABI_FP_DOUBLE:
   16522       fprintf (file, _("Hard float (double precision)\n"));
   16523       break;
   16524     case Val_GNU_MIPS_ABI_FP_SINGLE:
   16525       fprintf (file, _("Hard float (single precision)\n"));
   16526       break;
   16527     case Val_GNU_MIPS_ABI_FP_SOFT:
   16528       fprintf (file, _("Soft float\n"));
   16529       break;
   16530     case Val_GNU_MIPS_ABI_FP_OLD_64:
   16531       fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
   16532       break;
   16533     case Val_GNU_MIPS_ABI_FP_XX:
   16534       fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
   16535       break;
   16536     case Val_GNU_MIPS_ABI_FP_64:
   16537       fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
   16538       break;
   16539     case Val_GNU_MIPS_ABI_FP_64A:
   16540       fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
   16541       break;
   16542     default:
   16543       fprintf (file, "??? (%d)\n", val);
   16544       break;
   16545     }
   16546 }
   16547 
   16548 static int
   16549 get_mips_reg_size (int reg_size)
   16550 {
   16551   return (reg_size == AFL_REG_NONE) ? 0
   16552 	 : (reg_size == AFL_REG_32) ? 32
   16553 	 : (reg_size == AFL_REG_64) ? 64
   16554 	 : (reg_size == AFL_REG_128) ? 128
   16555 	 : -1;
   16556 }
   16557 
   16558 bool
   16559 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
   16560 {
   16561   FILE *file = ptr;
   16562 
   16563   BFD_ASSERT (abfd != NULL && ptr != NULL);
   16564 
   16565   /* Print normal ELF private data.  */
   16566   _bfd_elf_print_private_bfd_data (abfd, ptr);
   16567 
   16568   /* xgettext:c-format */
   16569   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
   16570 
   16571   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
   16572     fprintf (file, _(" [abi=O32]"));
   16573   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O64)
   16574     fprintf (file, _(" [abi=O64]"));
   16575   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32)
   16576     fprintf (file, _(" [abi=EABI32]"));
   16577   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
   16578     fprintf (file, _(" [abi=EABI64]"));
   16579   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
   16580     fprintf (file, _(" [abi unknown]"));
   16581   else if (ABI_N32_P (abfd))
   16582     fprintf (file, _(" [abi=N32]"));
   16583   else if (ABI_64_P (abfd))
   16584     fprintf (file, _(" [abi=64]"));
   16585   else
   16586     fprintf (file, _(" [no abi set]"));
   16587 
   16588   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1)
   16589     fprintf (file, " [mips1]");
   16590   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2)
   16591     fprintf (file, " [mips2]");
   16592   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_3)
   16593     fprintf (file, " [mips3]");
   16594   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_4)
   16595     fprintf (file, " [mips4]");
   16596   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_5)
   16597     fprintf (file, " [mips5]");
   16598   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32)
   16599     fprintf (file, " [mips32]");
   16600   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64)
   16601     fprintf (file, " [mips64]");
   16602   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2)
   16603     fprintf (file, " [mips32r2]");
   16604   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R2)
   16605     fprintf (file, " [mips64r2]");
   16606   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6)
   16607     fprintf (file, " [mips32r6]");
   16608   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
   16609     fprintf (file, " [mips64r6]");
   16610   else
   16611     fprintf (file, _(" [unknown ISA]"));
   16612 
   16613   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
   16614     fprintf (file, " [mdmx]");
   16615 
   16616   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
   16617     fprintf (file, " [mips16]");
   16618 
   16619   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
   16620     fprintf (file, " [micromips]");
   16621 
   16622   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
   16623     fprintf (file, " [nan2008]");
   16624 
   16625   if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
   16626     fprintf (file, " [old fp64]");
   16627 
   16628   if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
   16629     fprintf (file, " [32bitmode]");
   16630   else
   16631     fprintf (file, _(" [not 32bitmode]"));
   16632 
   16633   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
   16634     fprintf (file, " [noreorder]");
   16635 
   16636   if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
   16637     fprintf (file, " [PIC]");
   16638 
   16639   if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
   16640     fprintf (file, " [CPIC]");
   16641 
   16642   if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
   16643     fprintf (file, " [XGOT]");
   16644 
   16645   if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
   16646     fprintf (file, " [UCODE]");
   16647 
   16648   fputc ('\n', file);
   16649 
   16650   if (mips_elf_tdata (abfd)->abiflags_valid)
   16651     {
   16652       Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
   16653       fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
   16654       fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
   16655       if (abiflags->isa_rev > 1)
   16656 	fprintf (file, "r%d", abiflags->isa_rev);
   16657       fprintf (file, "\nGPR size: %d",
   16658 	       get_mips_reg_size (abiflags->gpr_size));
   16659       fprintf (file, "\nCPR1 size: %d",
   16660 	       get_mips_reg_size (abiflags->cpr1_size));
   16661       fprintf (file, "\nCPR2 size: %d",
   16662 	       get_mips_reg_size (abiflags->cpr2_size));
   16663       fputs ("\nFP ABI: ", file);
   16664       print_mips_fp_abi_value (file, abiflags->fp_abi);
   16665       fputs ("ISA Extension: ", file);
   16666       print_mips_isa_ext (file, abiflags->isa_ext);
   16667       fputs ("\nASEs:", file);
   16668       print_mips_ases (file, abiflags->ases);
   16669       fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
   16670       fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
   16671       fputc ('\n', file);
   16672     }
   16673 
   16674   return true;
   16675 }
   16676 
   16677 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
   16678 {
   16679   { STRING_COMMA_LEN (".lit4"),	  0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   16680   { STRING_COMMA_LEN (".lit8"),	  0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   16681   { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
   16682   { STRING_COMMA_LEN (".sbss"),	 -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   16683   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   16684   { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
   16685   { STRING_COMMA_LEN (".MIPS.xhash"),  0, SHT_MIPS_XHASH,   SHF_ALLOC },
   16686   { NULL,		      0,  0, 0,		     0 }
   16687 };
   16688 
   16689 /* Merge non visibility st_other attributes.  Ensure that the
   16690    STO_OPTIONAL flag is copied into h->other, even if this is not a
   16691    definiton of the symbol.  */
   16692 void
   16693 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
   16694 				      unsigned int st_other,
   16695 				      bool definition,
   16696 				      bool dynamic ATTRIBUTE_UNUSED)
   16697 {
   16698   if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
   16699     {
   16700       unsigned char other;
   16701 
   16702       other = (definition ? st_other : h->other);
   16703       other &= ~ELF_ST_VISIBILITY (-1);
   16704       h->other = other | ELF_ST_VISIBILITY (h->other);
   16705     }
   16706 
   16707   if (!definition
   16708       && ELF_MIPS_IS_OPTIONAL (st_other))
   16709     h->other |= STO_OPTIONAL;
   16710 }
   16711 
   16712 /* Decide whether an undefined symbol is special and can be ignored.
   16713    This is the case for OPTIONAL symbols on IRIX.  */
   16714 bool
   16715 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
   16716 {
   16717   return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
   16718 }
   16719 
   16720 bool
   16721 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
   16722 {
   16723   return (sym->st_shndx == SHN_COMMON
   16724 	  || sym->st_shndx == SHN_MIPS_ACOMMON
   16725 	  || sym->st_shndx == SHN_MIPS_SCOMMON);
   16726 }
   16727 
   16728 /* Return address for Ith PLT stub in section PLT, for relocation REL
   16729    or (bfd_vma) -1 if it should not be included.  */
   16730 
   16731 bfd_vma
   16732 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
   16733 			   const arelent *rel ATTRIBUTE_UNUSED)
   16734 {
   16735   return (plt->vma
   16736 	  + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
   16737 	  + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
   16738 }
   16739 
   16740 /* Build a table of synthetic symbols to represent the PLT.  As with MIPS16
   16741    and microMIPS PLT slots we may have a many-to-one mapping between .plt
   16742    and .got.plt and also the slots may be of a different size each we walk
   16743    the PLT manually fetching instructions and matching them against known
   16744    patterns.  To make things easier standard MIPS slots, if any, always come
   16745    first.  As we don't create proper ELF symbols we use the UDATA.I member
   16746    of ASYMBOL to carry ISA annotation.  The encoding used is the same as
   16747    with the ST_OTHER member of the ELF symbol.  */
   16748 
   16749 long
   16750 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
   16751 				    long symcount ATTRIBUTE_UNUSED,
   16752 				    asymbol **syms ATTRIBUTE_UNUSED,
   16753 				    long dynsymcount, asymbol **dynsyms,
   16754 				    asymbol **ret)
   16755 {
   16756   static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
   16757   static const char microsuffix[] = "@micromipsplt";
   16758   static const char m16suffix[] = "@mips16plt";
   16759   static const char mipssuffix[] = "@plt";
   16760 
   16761   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   16762   elf_backend_data *bed = get_elf_backend_data (abfd);
   16763   bool micromips_p = MICROMIPS_P (abfd);
   16764   Elf_Internal_Shdr *hdr;
   16765   bfd_byte *plt_data;
   16766   bfd_vma plt_offset;
   16767   unsigned int other;
   16768   bfd_vma entry_size;
   16769   bfd_vma plt0_size;
   16770   asection *relplt;
   16771   bfd_vma opcode;
   16772   asection *plt;
   16773   asymbol *send;
   16774   size_t size;
   16775   char *names;
   16776   long counti;
   16777   arelent *p;
   16778   asymbol *s;
   16779   char *nend;
   16780   long count;
   16781   long pi;
   16782   long i;
   16783   long n;
   16784 
   16785   *ret = NULL;
   16786 
   16787   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
   16788     return 0;
   16789 
   16790   relplt = bfd_get_section_by_name (abfd, ".rel.plt");
   16791   if (relplt == NULL)
   16792     return 0;
   16793 
   16794   hdr = &elf_section_data (relplt)->this_hdr;
   16795   if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
   16796     return 0;
   16797 
   16798   plt = bfd_get_section_by_name (abfd, ".plt");
   16799   if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
   16800     return 0;
   16801 
   16802   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   16803   if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
   16804     return -1;
   16805   p = relplt->relocation;
   16806 
   16807   /* Calculating the exact amount of space required for symbols would
   16808      require two passes over the PLT, so just pessimise assuming two
   16809      PLT slots per relocation.  */
   16810   count = NUM_SHDR_ENTRIES (hdr);
   16811   counti = count * bed->s->int_rels_per_ext_rel;
   16812   size = 2 * count * sizeof (asymbol);
   16813   size += count * (sizeof (mipssuffix) +
   16814 		   (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
   16815   for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
   16816     size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
   16817 
   16818   /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too.  */
   16819   size += sizeof (asymbol) + sizeof (pltname);
   16820 
   16821   if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
   16822     return -1;
   16823 
   16824   if (plt->size < 16)
   16825     return -1;
   16826 
   16827   s = *ret = bfd_malloc (size);
   16828   if (s == NULL)
   16829     return -1;
   16830   send = s + 2 * count + 1;
   16831 
   16832   names = (char *) send;
   16833   nend = (char *) s + size;
   16834   n = 0;
   16835 
   16836   opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
   16837   if (opcode == 0x3302fffe)
   16838     {
   16839       if (!micromips_p)
   16840 	return -1;
   16841       plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
   16842       other = STO_MICROMIPS;
   16843     }
   16844   else if (opcode == 0x0398c1d0)
   16845     {
   16846       if (!micromips_p)
   16847 	return -1;
   16848       plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
   16849       other = STO_MICROMIPS;
   16850     }
   16851   else
   16852     {
   16853       plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
   16854       other = 0;
   16855     }
   16856 
   16857   s->the_bfd = abfd;
   16858   s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
   16859   s->section = plt;
   16860   s->value = 0;
   16861   s->name = names;
   16862   s->udata.i = other;
   16863   memcpy (names, pltname, sizeof (pltname));
   16864   names += sizeof (pltname);
   16865   ++s, ++n;
   16866 
   16867   pi = 0;
   16868   for (plt_offset = plt0_size;
   16869        plt_offset + 8 <= plt->size && s < send;
   16870        plt_offset += entry_size)
   16871     {
   16872       bfd_vma gotplt_addr;
   16873       const char *suffix;
   16874       bfd_vma gotplt_hi;
   16875       bfd_vma gotplt_lo;
   16876       size_t suffixlen;
   16877 
   16878       opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
   16879 
   16880       /* Check if the second word matches the expected MIPS16 instruction.  */
   16881       if (opcode == 0x651aeb00)
   16882 	{
   16883 	  if (micromips_p)
   16884 	    return -1;
   16885 	  /* Truncated table???  */
   16886 	  if (plt_offset + 16 > plt->size)
   16887 	    break;
   16888 	  gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
   16889 	  entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
   16890 	  suffixlen = sizeof (m16suffix);
   16891 	  suffix = m16suffix;
   16892 	  other = STO_MIPS16;
   16893 	}
   16894       /* Likewise the expected microMIPS instruction (no insn32 mode).  */
   16895       else if (opcode == 0xff220000)
   16896 	{
   16897 	  if (!micromips_p)
   16898 	    return -1;
   16899 	  gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
   16900 	  gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
   16901 	  gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
   16902 	  gotplt_lo <<= 2;
   16903 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16904 	  gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
   16905 	  entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
   16906 	  suffixlen = sizeof (microsuffix);
   16907 	  suffix = microsuffix;
   16908 	  other = STO_MICROMIPS;
   16909 	}
   16910       /* Likewise the expected microMIPS instruction (insn32 mode).  */
   16911       else if ((opcode & 0xffff0000) == 0xff2f0000)
   16912 	{
   16913 	  gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
   16914 	  gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
   16915 	  gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
   16916 	  gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
   16917 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16918 	  entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
   16919 	  suffixlen = sizeof (microsuffix);
   16920 	  suffix = microsuffix;
   16921 	  other = STO_MICROMIPS;
   16922 	}
   16923       /* Otherwise assume standard MIPS code.  */
   16924       else
   16925 	{
   16926 	  gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
   16927 	  gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
   16928 	  gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
   16929 	  gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
   16930 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16931 	  entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
   16932 	  suffixlen = sizeof (mipssuffix);
   16933 	  suffix = mipssuffix;
   16934 	  other = 0;
   16935 	}
   16936       /* Truncated table???  */
   16937       if (plt_offset + entry_size > plt->size)
   16938 	break;
   16939 
   16940       for (i = 0;
   16941 	   i < count && p[pi].address != gotplt_addr;
   16942 	   i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
   16943 
   16944       if (i < count)
   16945 	{
   16946 	  size_t namelen;
   16947 	  size_t len;
   16948 
   16949 	  *s = **p[pi].sym_ptr_ptr;
   16950 	  /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   16951 	     we are defining a symbol, ensure one of them is set.  */
   16952 	  if ((s->flags & BSF_LOCAL) == 0)
   16953 	    s->flags |= BSF_GLOBAL;
   16954 	  s->flags |= BSF_SYNTHETIC;
   16955 	  s->section = plt;
   16956 	  s->value = plt_offset;
   16957 	  s->name = names;
   16958 	  s->udata.i = other;
   16959 
   16960 	  len = strlen ((*p[pi].sym_ptr_ptr)->name);
   16961 	  namelen = len + suffixlen;
   16962 	  if (names + namelen > nend)
   16963 	    break;
   16964 
   16965 	  memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
   16966 	  names += len;
   16967 	  memcpy (names, suffix, suffixlen);
   16968 	  names += suffixlen;
   16969 
   16970 	  ++s, ++n;
   16971 	  pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
   16972 	}
   16973     }
   16974 
   16975   free (plt_data);
   16976 
   16977   return n;
   16978 }
   16979 
   16980 /* Return the ABI flags associated with ABFD if available.  */
   16981 
   16982 Elf_Internal_ABIFlags_v0 *
   16983 bfd_mips_elf_get_abiflags (bfd *abfd)
   16984 {
   16985   struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
   16986 
   16987   return tdata->abiflags_valid ? &tdata->abiflags : NULL;
   16988 }
   16989 
   16990 /* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
   16991    field.  Taken from `libc-abis.h' generated at GNU libc build time.
   16992    Using a MIPS_ prefix as other libc targets use different values.  */
   16993 enum
   16994 {
   16995   MIPS_LIBC_ABI_DEFAULT = 0,
   16996   MIPS_LIBC_ABI_MIPS_PLT,
   16997   MIPS_LIBC_ABI_UNIQUE,
   16998   MIPS_LIBC_ABI_MIPS_O32_FP64,
   16999   MIPS_LIBC_ABI_ABSOLUTE,
   17000   MIPS_LIBC_ABI_XHASH,
   17001   MIPS_LIBC_ABI_MAX
   17002 };
   17003 
   17004 bool
   17005 _bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
   17006 {
   17007   struct mips_elf_link_hash_table *htab = NULL;
   17008   Elf_Internal_Ehdr *i_ehdrp;
   17009 
   17010   if (!_bfd_elf_init_file_header (abfd, link_info))
   17011     return false;
   17012 
   17013   i_ehdrp = elf_elfheader (abfd);
   17014   if (link_info)
   17015     {
   17016       htab = mips_elf_hash_table (link_info);
   17017       BFD_ASSERT (htab != NULL);
   17018     }
   17019 
   17020   if (htab != NULL
   17021       && htab->use_plts_and_copy_relocs
   17022       && htab->root.target_os != is_vxworks)
   17023     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
   17024 
   17025   if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
   17026       || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
   17027     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
   17028 
   17029   /* Mark that we need support for absolute symbols in the dynamic loader.  */
   17030   if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
   17031     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
   17032 
   17033   /* Mark that we need support for .MIPS.xhash in the dynamic linker,
   17034      if it is the only hash section that will be created.  */
   17035   if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
   17036     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
   17037   return true;
   17038 }
   17039 
   17040 int
   17041 _bfd_mips_elf_compact_eh_encoding
   17042   (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
   17043 {
   17044   return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
   17045 }
   17046 
   17047 /* Return the opcode for can't unwind.  */
   17048 
   17049 int
   17050 _bfd_mips_elf_cant_unwind_opcode
   17051   (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
   17052 {
   17053   return COMPACT_EH_CANT_UNWIND_OPCODE;
   17054 }
   17055 
   17056 /* Record a position XLAT_LOC in the xlat translation table, associated with
   17057    the hash entry H.  The entry in the translation table will later be
   17058    populated with the real symbol dynindx.  */
   17059 
   17060 void
   17061 _bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
   17062 				   bfd_vma xlat_loc)
   17063 {
   17064   struct mips_elf_link_hash_entry *hmips;
   17065 
   17066   hmips = (struct mips_elf_link_hash_entry *) h;
   17067   hmips->mipsxhash_loc = xlat_loc;
   17068 }
   17069