Home | History | Annotate | Line # | Download | only in bfd
elfxx-mips.c revision 1.1.1.10.2.1
      1 /* MIPS-specific support for ELF
      2    Copyright (C) 1993-2024 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 struct _mips_elf_section_data
    226 {
    227   struct bfd_elf_section_data elf;
    228   union
    229   {
    230     bfd_byte *tdata;
    231   } u;
    232 };
    233 
    234 #define mips_elf_section_data(sec) \
    235   ((struct _mips_elf_section_data *) elf_section_data (sec))
    236 
    237 #define is_mips_elf(bfd)				\
    238   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
    239    && elf_tdata (bfd) != NULL				\
    240    && elf_object_id (bfd) == MIPS_ELF_DATA)
    241 
    242 /* The ABI says that every symbol used by dynamic relocations must have
    243    a global GOT entry.  Among other things, this provides the dynamic
    244    linker with a free, directly-indexed cache.  The GOT can therefore
    245    contain symbols that are not referenced by GOT relocations themselves
    246    (in other words, it may have symbols that are not referenced by things
    247    like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
    248 
    249    GOT relocations are less likely to overflow if we put the associated
    250    GOT entries towards the beginning.  We therefore divide the global
    251    GOT entries into two areas: "normal" and "reloc-only".  Entries in
    252    the first area can be used for both dynamic relocations and GP-relative
    253    accesses, while those in the "reloc-only" area are for dynamic
    254    relocations only.
    255 
    256    These GGA_* ("Global GOT Area") values are organised so that lower
    257    values are more general than higher values.  Also, non-GGA_NONE
    258    values are ordered by the position of the area in the GOT.  */
    259 #define GGA_NORMAL 0
    260 #define GGA_RELOC_ONLY 1
    261 #define GGA_NONE 2
    262 
    263 /* Information about a non-PIC interface to a PIC function.  There are
    264    two ways of creating these interfaces.  The first is to add:
    265 
    266 	lui	$25,%hi(func)
    267 	addiu	$25,$25,%lo(func)
    268 
    269    immediately before a PIC function "func".  The second is to add:
    270 
    271 	lui	$25,%hi(func)
    272 	j	func
    273 	addiu	$25,$25,%lo(func)
    274 
    275    to a separate trampoline section.
    276 
    277    Stubs of the first kind go in a new section immediately before the
    278    target function.  Stubs of the second kind go in a single section
    279    pointed to by the hash table's "strampoline" field.  */
    280 struct mips_elf_la25_stub {
    281   /* The generated section that contains this stub.  */
    282   asection *stub_section;
    283 
    284   /* The offset of the stub from the start of STUB_SECTION.  */
    285   bfd_vma offset;
    286 
    287   /* One symbol for the original function.  Its location is available
    288      in H->root.root.u.def.  */
    289   struct mips_elf_link_hash_entry *h;
    290 };
    291 
    292 /* Macros for populating a mips_elf_la25_stub.  */
    293 
    294 #define LA25_LUI(VAL) (0x3c190000 | (VAL))	/* lui t9,VAL */
    295 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
    296 #define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
    297 #define LA25_ADDIU(VAL) (0x27390000 | (VAL))	/* addiu t9,t9,VAL */
    298 #define LA25_LUI_MICROMIPS(VAL)						\
    299   (0x41b90000 | (VAL))				/* lui t9,VAL */
    300 #define LA25_J_MICROMIPS(VAL)						\
    301   (0xd4000000 | (((VAL) >> 1) & 0x3ffffff))	/* j VAL */
    302 #define LA25_ADDIU_MICROMIPS(VAL)					\
    303   (0x33390000 | (VAL))				/* addiu t9,t9,VAL */
    304 
    305 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
    306    the dynamic symbols.  */
    307 
    308 struct mips_elf_hash_sort_data
    309 {
    310   /* The symbol in the global GOT with the lowest dynamic symbol table
    311      index.  */
    312   struct elf_link_hash_entry *low;
    313   /* The least dynamic symbol table index corresponding to a non-TLS
    314      symbol with a GOT entry.  */
    315   bfd_size_type min_got_dynindx;
    316   /* The greatest dynamic symbol table index corresponding to a symbol
    317      with a GOT entry that is not referenced (e.g., a dynamic symbol
    318      with dynamic relocations pointing to it from non-primary GOTs).  */
    319   bfd_size_type max_unref_got_dynindx;
    320   /* The greatest dynamic symbol table index corresponding to a local
    321      symbol.  */
    322   bfd_size_type max_local_dynindx;
    323   /* The greatest dynamic symbol table index corresponding to an external
    324      symbol without a GOT entry.  */
    325   bfd_size_type max_non_got_dynindx;
    326   /* If non-NULL, output BFD for .MIPS.xhash finalization.  */
    327   bfd *output_bfd;
    328   /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
    329      real final dynindx.  */
    330   bfd_byte *mipsxhash;
    331 };
    332 
    333 /* We make up to two PLT entries if needed, one for standard MIPS code
    334    and one for compressed code, either a MIPS16 or microMIPS one.  We
    335    keep a separate record of traditional lazy-binding stubs, for easier
    336    processing.  */
    337 
    338 struct plt_entry
    339 {
    340   /* Traditional SVR4 stub offset, or -1 if none.  */
    341   bfd_vma stub_offset;
    342 
    343   /* Standard PLT entry offset, or -1 if none.  */
    344   bfd_vma mips_offset;
    345 
    346   /* Compressed PLT entry offset, or -1 if none.  */
    347   bfd_vma comp_offset;
    348 
    349   /* The corresponding .got.plt index, or -1 if none.  */
    350   bfd_vma gotplt_index;
    351 
    352   /* Whether we need a standard PLT entry.  */
    353   unsigned int need_mips : 1;
    354 
    355   /* Whether we need a compressed PLT entry.  */
    356   unsigned int need_comp : 1;
    357 };
    358 
    359 /* The MIPS ELF linker needs additional information for each symbol in
    360    the global hash table.  */
    361 
    362 struct mips_elf_link_hash_entry
    363 {
    364   struct elf_link_hash_entry root;
    365 
    366   /* External symbol information.  */
    367   EXTR esym;
    368 
    369   /* The la25 stub we have created for ths symbol, if any.  */
    370   struct mips_elf_la25_stub *la25_stub;
    371 
    372   /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
    373      this symbol.  */
    374   unsigned int possibly_dynamic_relocs;
    375 
    376   /* If there is a stub that 32 bit functions should use to call this
    377      16 bit function, this points to the section containing the stub.  */
    378   asection *fn_stub;
    379 
    380   /* If there is a stub that 16 bit functions should use to call this
    381      32 bit function, this points to the section containing the stub.  */
    382   asection *call_stub;
    383 
    384   /* This is like the call_stub field, but it is used if the function
    385      being called returns a floating point value.  */
    386   asection *call_fp_stub;
    387 
    388   /* If non-zero, location in .MIPS.xhash to write real final dynindx.  */
    389   bfd_vma mipsxhash_loc;
    390 
    391   /* The highest GGA_* value that satisfies all references to this symbol.  */
    392   unsigned int global_got_area : 2;
    393 
    394   /* True if all GOT relocations against this symbol are for calls.  This is
    395      a looser condition than no_fn_stub below, because there may be other
    396      non-call non-GOT relocations against the symbol.  */
    397   unsigned int got_only_for_calls : 1;
    398 
    399   /* True if one of the relocations described by possibly_dynamic_relocs
    400      is against a readonly section.  */
    401   unsigned int readonly_reloc : 1;
    402 
    403   /* True if there is a relocation against this symbol that must be
    404      resolved by the static linker (in other words, if the relocation
    405      cannot possibly be made dynamic).  */
    406   unsigned int has_static_relocs : 1;
    407 
    408   /* True if we must not create a .MIPS.stubs entry for this symbol.
    409      This is set, for example, if there are relocations related to
    410      taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
    411      See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
    412   unsigned int no_fn_stub : 1;
    413 
    414   /* Whether we need the fn_stub; this is true if this symbol appears
    415      in any relocs other than a 16 bit call.  */
    416   unsigned int need_fn_stub : 1;
    417 
    418   /* True if this symbol is referenced by branch relocations from
    419      any non-PIC input file.  This is used to determine whether an
    420      la25 stub is required.  */
    421   unsigned int has_nonpic_branches : 1;
    422 
    423   /* Does this symbol need a traditional MIPS lazy-binding stub
    424      (as opposed to a PLT entry)?  */
    425   unsigned int needs_lazy_stub : 1;
    426 
    427   /* Does this symbol resolve to a PLT entry?  */
    428   unsigned int use_plt_entry : 1;
    429 };
    430 
    431 /* MIPS ELF linker hash table.  */
    432 
    433 struct mips_elf_link_hash_table
    434 {
    435   struct elf_link_hash_table root;
    436 
    437   /* The number of .rtproc entries.  */
    438   bfd_size_type procedure_count;
    439 
    440   /* The size of the .compact_rel section (if SGI_COMPAT).  */
    441   bfd_size_type compact_rel_size;
    442 
    443   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
    444      is set to the address of __rld_obj_head as in IRIX5 and IRIX6.  */
    445   bool use_rld_obj_head;
    446 
    447   /* The  __rld_map or __rld_obj_head symbol. */
    448   struct elf_link_hash_entry *rld_symbol;
    449 
    450   /* This is set if we see any mips16 stub sections.  */
    451   bool mips16_stubs_seen;
    452 
    453   /* True if we can generate copy relocs and PLTs.  */
    454   bool use_plts_and_copy_relocs;
    455 
    456   /* True if we can only use 32-bit microMIPS instructions.  */
    457   bool insn32;
    458 
    459   /* True if we suppress checks for invalid branches between ISA modes.  */
    460   bool ignore_branch_isa;
    461 
    462   /* True if we are targetting R6 compact branches.  */
    463   bool compact_branches;
    464 
    465   /* True if we already reported the small-data section overflow.  */
    466   bool small_data_overflow_reported;
    467 
    468   /* True if we use the special `__gnu_absolute_zero' symbol.  */
    469   bool use_absolute_zero;
    470 
    471   /* True if we have been configured for a GNU target.  */
    472   bool gnu_target;
    473 
    474   /* Shortcuts to some dynamic sections, or NULL if they are not
    475      being used.  */
    476   asection *srelplt2;
    477   asection *sstubs;
    478 
    479   /* The master GOT information.  */
    480   struct mips_got_info *got_info;
    481 
    482   /* The global symbol in the GOT with the lowest index in the dynamic
    483      symbol table.  */
    484   struct elf_link_hash_entry *global_gotsym;
    485 
    486   /* The size of the PLT header in bytes.  */
    487   bfd_vma plt_header_size;
    488 
    489   /* The size of a standard PLT entry in bytes.  */
    490   bfd_vma plt_mips_entry_size;
    491 
    492   /* The size of a compressed PLT entry in bytes.  */
    493   bfd_vma plt_comp_entry_size;
    494 
    495   /* The offset of the next standard PLT entry to create.  */
    496   bfd_vma plt_mips_offset;
    497 
    498   /* The offset of the next compressed PLT entry to create.  */
    499   bfd_vma plt_comp_offset;
    500 
    501   /* The index of the next .got.plt entry to create.  */
    502   bfd_vma plt_got_index;
    503 
    504   /* The number of functions that need a lazy-binding stub.  */
    505   bfd_vma lazy_stub_count;
    506 
    507   /* The size of a function stub entry in bytes.  */
    508   bfd_vma function_stub_size;
    509 
    510   /* The number of reserved entries at the beginning of the GOT.  */
    511   unsigned int reserved_gotno;
    512 
    513   /* The section used for mips_elf_la25_stub trampolines.
    514      See the comment above that structure for details.  */
    515   asection *strampoline;
    516 
    517   /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
    518      pairs.  */
    519   htab_t la25_stubs;
    520 
    521   /* A function FN (NAME, IS, OS) that creates a new input section
    522      called NAME and links it to output section OS.  If IS is nonnull,
    523      the new section should go immediately before it, otherwise it
    524      should go at the (current) beginning of OS.
    525 
    526      The function returns the new section on success, otherwise it
    527      returns null.  */
    528   asection *(*add_stub_section) (const char *, asection *, asection *);
    529 
    530   /* Is the PLT header compressed?  */
    531   unsigned int plt_header_is_comp : 1;
    532 };
    533 
    534 /* Get the MIPS ELF linker hash table from a link_info structure.  */
    535 
    536 #define mips_elf_hash_table(p) \
    537   ((is_elf_hash_table ((p)->hash)					\
    538     && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA)		\
    539    ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
    540 
    541 /* A structure used to communicate with htab_traverse callbacks.  */
    542 struct mips_htab_traverse_info
    543 {
    544   /* The usual link-wide information.  */
    545   struct bfd_link_info *info;
    546   bfd *output_bfd;
    547 
    548   /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
    549   bool error;
    550 };
    551 
    552 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
    553    R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
    554    that contains the relocation field and DATA points to the start of
    555    INPUT_SECTION.  */
    556 
    557 struct mips_hi16
    558 {
    559   struct mips_hi16 *next;
    560   bfd_byte *data;
    561   asection *input_section;
    562   arelent rel;
    563 };
    564 
    565 /* MIPS ELF private object data.  */
    566 
    567 struct mips_elf_obj_tdata
    568 {
    569   /* Generic ELF private object data.  */
    570   struct elf_obj_tdata root;
    571 
    572   /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output.  */
    573   bfd *abi_fp_bfd;
    574 
    575   /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output.  */
    576   bfd *abi_msa_bfd;
    577 
    578   /* The abiflags for this object.  */
    579   Elf_Internal_ABIFlags_v0 abiflags;
    580   bool abiflags_valid;
    581 
    582   /* The GOT requirements of input bfds.  */
    583   struct mips_got_info *got;
    584 
    585   /* Used by _bfd_mips_elf_find_nearest_line.  The structure could be
    586      included directly in this one, but there's no point to wasting
    587      the memory just for the infrequently called find_nearest_line.  */
    588   struct mips_elf_find_line *find_line_info;
    589 
    590   /* An array of stub sections indexed by symbol number.  */
    591   asection **local_stubs;
    592   asection **local_call_stubs;
    593 
    594   /* The Irix 5 support uses two virtual sections, which represent
    595      text/data symbols defined in dynamic objects.  */
    596   asymbol *elf_data_symbol;
    597   asymbol *elf_text_symbol;
    598   asection *elf_data_section;
    599   asection *elf_text_section;
    600 
    601   struct mips_hi16 *mips_hi16_list;
    602 };
    603 
    604 /* Get MIPS ELF private object data from BFD's tdata.  */
    605 
    606 #define mips_elf_tdata(bfd) \
    607   ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
    608 
    609 #define TLS_RELOC_P(r_type) \
    610   (r_type == R_MIPS_TLS_DTPMOD32		\
    611    || r_type == R_MIPS_TLS_DTPMOD64		\
    612    || r_type == R_MIPS_TLS_DTPREL32		\
    613    || r_type == R_MIPS_TLS_DTPREL64		\
    614    || r_type == R_MIPS_TLS_GD			\
    615    || r_type == R_MIPS_TLS_LDM			\
    616    || r_type == R_MIPS_TLS_DTPREL_HI16		\
    617    || r_type == R_MIPS_TLS_DTPREL_LO16		\
    618    || r_type == R_MIPS_TLS_GOTTPREL		\
    619    || r_type == R_MIPS_TLS_TPREL32		\
    620    || r_type == R_MIPS_TLS_TPREL64		\
    621    || r_type == R_MIPS_TLS_TPREL_HI16		\
    622    || r_type == R_MIPS_TLS_TPREL_LO16		\
    623    || r_type == R_MIPS16_TLS_GD			\
    624    || r_type == R_MIPS16_TLS_LDM		\
    625    || r_type == R_MIPS16_TLS_DTPREL_HI16	\
    626    || r_type == R_MIPS16_TLS_DTPREL_LO16	\
    627    || r_type == R_MIPS16_TLS_GOTTPREL		\
    628    || r_type == R_MIPS16_TLS_TPREL_HI16		\
    629    || r_type == R_MIPS16_TLS_TPREL_LO16		\
    630    || r_type == R_MICROMIPS_TLS_GD		\
    631    || r_type == R_MICROMIPS_TLS_LDM		\
    632    || r_type == R_MICROMIPS_TLS_DTPREL_HI16	\
    633    || r_type == R_MICROMIPS_TLS_DTPREL_LO16	\
    634    || r_type == R_MICROMIPS_TLS_GOTTPREL	\
    635    || r_type == R_MICROMIPS_TLS_TPREL_HI16	\
    636    || r_type == R_MICROMIPS_TLS_TPREL_LO16)
    637 
    638 /* Structure used to pass information to mips_elf_output_extsym.  */
    639 
    640 struct extsym_info
    641 {
    642   bfd *abfd;
    643   struct bfd_link_info *info;
    644   struct ecoff_debug_info *debug;
    645   const struct ecoff_debug_swap *swap;
    646   bool failed;
    647 };
    648 
    649 /* The names of the runtime procedure table symbols used on IRIX5.  */
    650 
    651 static const char * const mips_elf_dynsym_rtproc_names[] =
    652 {
    653   "_procedure_table",
    654   "_procedure_string_table",
    655   "_procedure_table_size",
    656   NULL
    657 };
    658 
    659 /* These structures are used to generate the .compact_rel section on
    660    IRIX5.  */
    661 
    662 typedef struct
    663 {
    664   unsigned long id1;		/* Always one?  */
    665   unsigned long num;		/* Number of compact relocation entries.  */
    666   unsigned long id2;		/* Always two?  */
    667   unsigned long offset;		/* The file offset of the first relocation.  */
    668   unsigned long reserved0;	/* Zero?  */
    669   unsigned long reserved1;	/* Zero?  */
    670 } Elf32_compact_rel;
    671 
    672 typedef struct
    673 {
    674   bfd_byte id1[4];
    675   bfd_byte num[4];
    676   bfd_byte id2[4];
    677   bfd_byte offset[4];
    678   bfd_byte reserved0[4];
    679   bfd_byte reserved1[4];
    680 } Elf32_External_compact_rel;
    681 
    682 typedef struct
    683 {
    684   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
    685   unsigned int rtype : 4;	/* Relocation types. See below.  */
    686   unsigned int dist2to : 8;
    687   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
    688   unsigned long konst;		/* KONST field. See below.  */
    689   unsigned long vaddr;		/* VADDR to be relocated.  */
    690 } Elf32_crinfo;
    691 
    692 typedef struct
    693 {
    694   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
    695   unsigned int rtype : 4;	/* Relocation types. See below.  */
    696   unsigned int dist2to : 8;
    697   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
    698   unsigned long konst;		/* KONST field. See below.  */
    699 } Elf32_crinfo2;
    700 
    701 typedef struct
    702 {
    703   bfd_byte info[4];
    704   bfd_byte konst[4];
    705   bfd_byte vaddr[4];
    706 } Elf32_External_crinfo;
    707 
    708 typedef struct
    709 {
    710   bfd_byte info[4];
    711   bfd_byte konst[4];
    712 } Elf32_External_crinfo2;
    713 
    714 /* These are the constants used to swap the bitfields in a crinfo.  */
    715 
    716 #define CRINFO_CTYPE (0x1U)
    717 #define CRINFO_CTYPE_SH (31)
    718 #define CRINFO_RTYPE (0xfU)
    719 #define CRINFO_RTYPE_SH (27)
    720 #define CRINFO_DIST2TO (0xffU)
    721 #define CRINFO_DIST2TO_SH (19)
    722 #define CRINFO_RELVADDR (0x7ffffU)
    723 #define CRINFO_RELVADDR_SH (0)
    724 
    725 /* A compact relocation info has long (3 words) or short (2 words)
    726    formats.  A short format doesn't have VADDR field and relvaddr
    727    fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
    728 #define CRF_MIPS_LONG			1
    729 #define CRF_MIPS_SHORT			0
    730 
    731 /* There are 4 types of compact relocation at least. The value KONST
    732    has different meaning for each type:
    733 
    734    (type)		(konst)
    735    CT_MIPS_REL32	Address in data
    736    CT_MIPS_WORD		Address in word (XXX)
    737    CT_MIPS_GPHI_LO	GP - vaddr
    738    CT_MIPS_JMPAD	Address to jump
    739    */
    740 
    741 #define CRT_MIPS_REL32			0xa
    742 #define CRT_MIPS_WORD			0xb
    743 #define CRT_MIPS_GPHI_LO		0xc
    744 #define CRT_MIPS_JMPAD			0xd
    745 
    746 #define mips_elf_set_cr_format(x,format)	((x).ctype = (format))
    747 #define mips_elf_set_cr_type(x,type)		((x).rtype = (type))
    748 #define mips_elf_set_cr_dist2to(x,v)		((x).dist2to = (v))
    749 #define mips_elf_set_cr_relvaddr(x,d)		((x).relvaddr = (d)<<2)
    750 
    751 /* The structure of the runtime procedure descriptor created by the
    753    loader for use by the static exception system.  */
    754 
    755 typedef struct runtime_pdr {
    756 	bfd_vma	adr;		/* Memory address of start of procedure.  */
    757 	long	regmask;	/* Save register mask.  */
    758 	long	regoffset;	/* Save register offset.  */
    759 	long	fregmask;	/* Save floating point register mask.  */
    760 	long	fregoffset;	/* Save floating point register offset.  */
    761 	long	frameoffset;	/* Frame size.  */
    762 	short	framereg;	/* Frame pointer register.  */
    763 	short	pcreg;		/* Offset or reg of return pc.  */
    764 	long	irpss;		/* Index into the runtime string table.  */
    765 	long	reserved;
    766 	struct exception_info *exception_info;/* Pointer to exception array.  */
    767 } RPDR, *pRPDR;
    768 #define cbRPDR sizeof (RPDR)
    769 #define rpdNil ((pRPDR) 0)
    770 
    771 static struct mips_got_entry *mips_elf_create_local_got_entry
    773   (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
    774    struct mips_elf_link_hash_entry *, int);
    775 static bool mips_elf_sort_hash_table_f
    776   (struct mips_elf_link_hash_entry *, void *);
    777 static bfd_vma mips_elf_high
    778   (bfd_vma);
    779 static bool mips_elf_create_dynamic_relocation
    780   (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
    781    struct mips_elf_link_hash_entry *, asection *, bfd_vma,
    782    bfd_vma *, asection *);
    783 static bfd_vma mips_elf_adjust_gp
    784   (bfd *, struct mips_got_info *, bfd *);
    785 
    786 /* This will be used when we sort the dynamic relocation records.  */
    787 static bfd *reldyn_sorting_bfd;
    788 
    789 /* True if ABFD is for CPUs with load interlocking that include
    790    non-MIPS1 CPUs and R3900.  */
    791 #define LOAD_INTERLOCKS_P(abfd) \
    792   (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) \
    793    || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_3900))
    794 
    795 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
    796    This should be safe for all architectures.  We enable this predicate
    797    for RM9000 for now.  */
    798 #define JAL_TO_BAL_P(abfd) \
    799   ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_9000)
    800 
    801 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
    802    This should be safe for all architectures.  We enable this predicate for
    803    all CPUs.  */
    804 #define JALR_TO_BAL_P(abfd) 1
    805 
    806 /* True if ABFD is for CPUs that are faster if JR is converted to B.
    807    This should be safe for all architectures.  We enable this predicate for
    808    all CPUs.  */
    809 #define JR_TO_B_P(abfd) 1
    810 
    811 /* True if ABFD is a PIC object.  */
    812 #define PIC_OBJECT_P(abfd) \
    813   ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
    814 
    815 /* Nonzero if ABFD is using the O32 ABI.  */
    816 #define ABI_O32_P(abfd) \
    817   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
    818 
    819 /* Nonzero if ABFD is using the N32 ABI.  */
    820 #define ABI_N32_P(abfd) \
    821   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
    822 
    823 /* Nonzero if ABFD is using the N64 ABI.  */
    824 #define ABI_64_P(abfd) \
    825   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
    826 
    827 /* Nonzero if ABFD is using NewABI conventions.  */
    828 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
    829 
    830 /* Nonzero if ABFD has microMIPS code.  */
    831 #define MICROMIPS_P(abfd) \
    832   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
    833 
    834 /* Nonzero if ABFD is MIPS R6.  */
    835 #define MIPSR6_P(abfd) \
    836   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6 \
    837     || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
    838 
    839 /* The IRIX compatibility level we are striving for.  */
    840 #define IRIX_COMPAT(abfd) \
    841   (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
    842 
    843 /* Whether we are trying to be compatible with IRIX at all.  */
    844 #define SGI_COMPAT(abfd) \
    845   (IRIX_COMPAT (abfd) != ict_none)
    846 
    847 /* The name of the options section.  */
    848 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
    849   (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
    850 
    851 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
    852    Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
    853 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
    854   (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
    855 
    856 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section.  */
    857 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
    858   (strcmp (NAME, ".MIPS.abiflags") == 0)
    859 
    860 /* Whether the section is readonly.  */
    861 #define MIPS_ELF_READONLY_SECTION(sec) \
    862   ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))		\
    863    == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
    864 
    865 /* The name of the stub section.  */
    866 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
    867 
    868 /* The size of an external REL relocation.  */
    869 #define MIPS_ELF_REL_SIZE(abfd) \
    870   (get_elf_backend_data (abfd)->s->sizeof_rel)
    871 
    872 /* The size of an external RELA relocation.  */
    873 #define MIPS_ELF_RELA_SIZE(abfd) \
    874   (get_elf_backend_data (abfd)->s->sizeof_rela)
    875 
    876 /* The size of an external dynamic table entry.  */
    877 #define MIPS_ELF_DYN_SIZE(abfd) \
    878   (get_elf_backend_data (abfd)->s->sizeof_dyn)
    879 
    880 /* The size of a GOT entry.  */
    881 #define MIPS_ELF_GOT_SIZE(abfd) \
    882   (get_elf_backend_data (abfd)->s->arch_size / 8)
    883 
    884 /* The size of the .rld_map section. */
    885 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
    886   (get_elf_backend_data (abfd)->s->arch_size / 8)
    887 
    888 /* The size of a symbol-table entry.  */
    889 #define MIPS_ELF_SYM_SIZE(abfd) \
    890   (get_elf_backend_data (abfd)->s->sizeof_sym)
    891 
    892 /* The default alignment for sections, as a power of two.  */
    893 #define MIPS_ELF_LOG_FILE_ALIGN(abfd)				\
    894   (get_elf_backend_data (abfd)->s->log_file_align)
    895 
    896 /* Get word-sized data.  */
    897 #define MIPS_ELF_GET_WORD(abfd, ptr) \
    898   (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
    899 
    900 /* Put out word-sized data.  */
    901 #define MIPS_ELF_PUT_WORD(abfd, val, ptr)	\
    902   (ABI_64_P (abfd)				\
    903    ? bfd_put_64 (abfd, val, ptr)		\
    904    : bfd_put_32 (abfd, val, ptr))
    905 
    906 /* The opcode for word-sized loads (LW or LD).  */
    907 #define MIPS_ELF_LOAD_WORD(abfd) \
    908   (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
    909 
    910 /* Add a dynamic symbol table-entry.  */
    911 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)	\
    912   _bfd_elf_add_dynamic_entry (info, tag, val)
    913 
    914 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)			\
    915   (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
    916 
    917 /* The name of the dynamic relocation section.  */
    918 #define MIPS_ELF_REL_DYN_NAME(INFO) \
    919   (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
    920    ? ".rela.dyn" : ".rel.dyn")
    921 
    922 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
    923    from smaller values.  Start with zero, widen, *then* decrement.  */
    924 #define MINUS_ONE	(((bfd_vma)0) - 1)
    925 #define MINUS_TWO	(((bfd_vma)0) - 2)
    926 
    927 /* The value to write into got[1] for SVR4 targets, to identify it is
    928    a GNU object.  The dynamic linker can then use got[1] to store the
    929    module pointer.  */
    930 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
    931   ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
    932 
    933 /* The offset of $gp from the beginning of the .got section.  */
    934 #define ELF_MIPS_GP_OFFSET(INFO) \
    935   (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
    936    ? 0x0 : 0x7ff0)
    937 
    938 /* The maximum size of the GOT for it to be addressable using 16-bit
    939    offsets from $gp.  */
    940 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
    941 
    942 /* Instructions which appear in a stub.  */
    943 #define STUB_LW(abfd)							\
    944   ((ABI_64_P (abfd)							\
    945     ? 0xdf998010				/* ld t9,0x8010(gp) */	\
    946     : 0x8f998010))				/* lw t9,0x8010(gp) */
    947 #define STUB_MOVE 0x03e07825			/* or t7,ra,zero */
    948 #define STUB_LUI(VAL) (0x3c180000 + (VAL))	/* lui t8,VAL */
    949 #define STUB_JALR 0x0320f809			/* jalr ra,t9 */
    950 #define STUB_JALRC 0xf8190000			/* jalrc ra,t9 */
    951 #define STUB_ORI(VAL) (0x37180000 + (VAL))	/* ori t8,t8,VAL */
    952 #define STUB_LI16U(VAL) (0x34180000 + (VAL))	/* ori t8,zero,VAL unsigned */
    953 #define STUB_LI16S(abfd, VAL)						\
    954    ((ABI_64_P (abfd)							\
    955     ? (0x64180000 + (VAL))	/* daddiu t8,zero,VAL sign extended */	\
    956     : (0x24180000 + (VAL))))	/* addiu t8,zero,VAL sign extended */
    957 
    958 /* Likewise for the microMIPS ASE.  */
    959 #define STUB_LW_MICROMIPS(abfd)						\
    960   (ABI_64_P (abfd)							\
    961    ? 0xdf3c8010					/* ld t9,0x8010(gp) */	\
    962    : 0xff3c8010)				/* lw t9,0x8010(gp) */
    963 #define STUB_MOVE_MICROMIPS 0x0dff		/* move t7,ra */
    964 #define STUB_MOVE32_MICROMIPS 0x001f7a90	/* or t7,ra,zero */
    965 #define STUB_LUI_MICROMIPS(VAL)						\
    966    (0x41b80000 + (VAL))				/* lui t8,VAL */
    967 #define STUB_JALR_MICROMIPS 0x45d9		/* jalr t9 */
    968 #define STUB_JALR32_MICROMIPS 0x03f90f3c	/* jalr ra,t9 */
    969 #define STUB_ORI_MICROMIPS(VAL)						\
    970   (0x53180000 + (VAL))				/* ori t8,t8,VAL */
    971 #define STUB_LI16U_MICROMIPS(VAL)					\
    972   (0x53000000 + (VAL))				/* ori t8,zero,VAL unsigned */
    973 #define STUB_LI16S_MICROMIPS(abfd, VAL)					\
    974    (ABI_64_P (abfd)							\
    975     ? 0x5f000000 + (VAL)	/* daddiu t8,zero,VAL sign extended */	\
    976     : 0x33000000 + (VAL))	/* addiu t8,zero,VAL sign extended */
    977 
    978 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
    979 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
    980 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
    981 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
    982 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
    983 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
    984 
    985 /* The name of the dynamic interpreter.  This is put in the .interp
    986    section.  */
    987 
    988 #define ELF_DYNAMIC_INTERPRETER(abfd)		\
    989    (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1"	\
    990     : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1"	\
    991     : "/usr/lib/libc.so.1")
    992 
    993 #ifdef BFD64
    994 #define MNAME(bfd,pre,pos) \
    995   (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
    996 #define ELF_R_SYM(bfd, i)					\
    997   (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
    998 #define ELF_R_TYPE(bfd, i)					\
    999   (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
   1000 #define ELF_R_INFO(bfd, s, t)					\
   1001   (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
   1002 #else
   1003 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
   1004 #define ELF_R_SYM(bfd, i)					\
   1005   (ELF32_R_SYM (i))
   1006 #define ELF_R_TYPE(bfd, i)					\
   1007   (ELF32_R_TYPE (i))
   1008 #define ELF_R_INFO(bfd, s, t)					\
   1009   (ELF32_R_INFO (s, t))
   1010 #endif
   1011 
   1012   /* The mips16 compiler uses a couple of special sections to handle
   1014      floating point arguments.
   1015 
   1016      Section names that look like .mips16.fn.FNNAME contain stubs that
   1017      copy floating point arguments from the fp regs to the gp regs and
   1018      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
   1019      call should be redirected to the stub instead.  If no 32 bit
   1020      function calls FNNAME, the stub should be discarded.  We need to
   1021      consider any reference to the function, not just a call, because
   1022      if the address of the function is taken we will need the stub,
   1023      since the address might be passed to a 32 bit function.
   1024 
   1025      Section names that look like .mips16.call.FNNAME contain stubs
   1026      that copy floating point arguments from the gp regs to the fp
   1027      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
   1028      then any 16 bit function that calls FNNAME should be redirected
   1029      to the stub instead.  If FNNAME is not a 32 bit function, the
   1030      stub should be discarded.
   1031 
   1032      .mips16.call.fp.FNNAME sections are similar, but contain stubs
   1033      which call FNNAME and then copy the return value from the fp regs
   1034      to the gp regs.  These stubs store the return value in $18 while
   1035      calling FNNAME; any function which might call one of these stubs
   1036      must arrange to save $18 around the call.  (This case is not
   1037      needed for 32 bit functions that call 16 bit functions, because
   1038      16 bit functions always return floating point values in both
   1039      $f0/$f1 and $2/$3.)
   1040 
   1041      Note that in all cases FNNAME might be defined statically.
   1042      Therefore, FNNAME is not used literally.  Instead, the relocation
   1043      information will indicate which symbol the section is for.
   1044 
   1045      We record any stubs that we find in the symbol table.  */
   1046 
   1047 #define FN_STUB ".mips16.fn."
   1048 #define CALL_STUB ".mips16.call."
   1049 #define CALL_FP_STUB ".mips16.call.fp."
   1050 
   1051 #define FN_STUB_P(name) startswith (name, FN_STUB)
   1052 #define CALL_STUB_P(name) startswith (name, CALL_STUB)
   1053 #define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
   1054 
   1055 /* The format of the first PLT entry in an O32 executable.  */
   1057 static const bfd_vma mips_o32_exec_plt0_entry[] =
   1058 {
   1059   0x3c1c0000,	/* lui $28, %hi(&GOTPLT[0])				*/
   1060   0x8f990000,	/* lw $25, %lo(&GOTPLT[0])($28)				*/
   1061   0x279c0000,	/* addiu $28, $28, %lo(&GOTPLT[0])			*/
   1062   0x031cc023,	/* subu $24, $24, $28					*/
   1063   0x03e07825,	/* or t7, ra, zero					*/
   1064   0x0018c082,	/* srl $24, $24, 2					*/
   1065   0x0320f809,	/* jalr $25						*/
   1066   0x2718fffe	/* subu $24, $24, 2					*/
   1067 };
   1068 
   1069 /* The format of the first PLT entry in an O32 executable using compact
   1070    jumps.  */
   1071 static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
   1072 {
   1073   0x3c1c0000,	/* lui $28, %hi(&GOTPLT[0])				*/
   1074   0x8f990000,	/* lw $25, %lo(&GOTPLT[0])($28)				*/
   1075   0x279c0000,	/* addiu $28, $28, %lo(&GOTPLT[0])			*/
   1076   0x031cc023,	/* subu $24, $24, $28					*/
   1077   0x03e07821,	/* move $15, $31	# 32-bit move (addu)		*/
   1078   0x0018c082,	/* srl $24, $24, 2					*/
   1079   0x2718fffe,	/* subu $24, $24, 2					*/
   1080   0xf8190000	/* jalrc $25						*/
   1081 };
   1082 
   1083 /* The format of the first PLT entry in an N32 executable.  Different
   1084    because gp ($28) is not available; we use t2 ($14) instead.  */
   1085 static const bfd_vma mips_n32_exec_plt0_entry[] =
   1086 {
   1087   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1088   0x8dd90000,	/* lw $25, %lo(&GOTPLT[0])($14)				*/
   1089   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1090   0x030ec023,	/* subu $24, $24, $14					*/
   1091   0x03e07825,	/* or t7, ra, zero					*/
   1092   0x0018c082,	/* srl $24, $24, 2					*/
   1093   0x0320f809,	/* jalr $25						*/
   1094   0x2718fffe	/* subu $24, $24, 2					*/
   1095 };
   1096 
   1097 /* The format of the first PLT entry in an N32 executable using compact
   1098    jumps.  Different because gp ($28) is not available; we use t2 ($14)
   1099    instead.  */
   1100 static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
   1101 {
   1102   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1103   0x8dd90000,	/* lw $25, %lo(&GOTPLT[0])($14)				*/
   1104   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1105   0x030ec023,	/* subu $24, $24, $14					*/
   1106   0x03e07821,	/* move $15, $31	# 32-bit move (addu)		*/
   1107   0x0018c082,	/* srl $24, $24, 2					*/
   1108   0x2718fffe,	/* subu $24, $24, 2					*/
   1109   0xf8190000	/* jalrc $25						*/
   1110 };
   1111 
   1112 /* The format of the first PLT entry in an N64 executable.  Different
   1113    from N32 because of the increased size of GOT entries.  */
   1114 static const bfd_vma mips_n64_exec_plt0_entry[] =
   1115 {
   1116   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1117   0xddd90000,	/* ld $25, %lo(&GOTPLT[0])($14)				*/
   1118   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1119   0x030ec023,	/* subu $24, $24, $14					*/
   1120   0x03e07825,	/* or t7, ra, zero					*/
   1121   0x0018c0c2,	/* srl $24, $24, 3					*/
   1122   0x0320f809,	/* jalr $25						*/
   1123   0x2718fffe	/* subu $24, $24, 2					*/
   1124 };
   1125 
   1126 /* The format of the first PLT entry in an N64 executable using compact
   1127    jumps.  Different from N32 because of the increased size of GOT
   1128    entries.  */
   1129 static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
   1130 {
   1131   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1132   0xddd90000,	/* ld $25, %lo(&GOTPLT[0])($14)				*/
   1133   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1134   0x030ec023,	/* subu $24, $24, $14					*/
   1135   0x03e0782d,	/* move $15, $31	# 64-bit move (daddu)		*/
   1136   0x0018c0c2,	/* srl $24, $24, 3					*/
   1137   0x2718fffe,	/* subu $24, $24, 2					*/
   1138   0xf8190000	/* jalrc $25						*/
   1139 };
   1140 
   1141 
   1142 /* The format of the microMIPS first PLT entry in an O32 executable.
   1143    We rely on v0 ($2) rather than t8 ($24) to contain the address
   1144    of the GOTPLT entry handled, so this stub may only be used when
   1145    all the subsequent PLT entries are microMIPS code too.
   1146 
   1147    The trailing NOP is for alignment and correct disassembly only.  */
   1148 static const bfd_vma micromips_o32_exec_plt0_entry[] =
   1149 {
   1150   0x7980, 0x0000,	/* addiupc $3, (&GOTPLT[0]) - .			*/
   1151   0xff23, 0x0000,	/* lw $25, 0($3)				*/
   1152   0x0535,		/* subu $2, $2, $3				*/
   1153   0x2525,		/* srl $2, $2, 2				*/
   1154   0x3302, 0xfffe,	/* subu $24, $2, 2				*/
   1155   0x0dff,		/* move $15, $31				*/
   1156   0x45f9,		/* jalrs $25					*/
   1157   0x0f83,		/* move $28, $3					*/
   1158   0x0c00		/* nop						*/
   1159 };
   1160 
   1161 /* The format of the microMIPS first PLT entry in an O32 executable
   1162    in the insn32 mode.  */
   1163 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
   1164 {
   1165   0x41bc, 0x0000,	/* lui $28, %hi(&GOTPLT[0])			*/
   1166   0xff3c, 0x0000,	/* lw $25, %lo(&GOTPLT[0])($28)			*/
   1167   0x339c, 0x0000,	/* addiu $28, $28, %lo(&GOTPLT[0])		*/
   1168   0x0398, 0xc1d0,	/* subu $24, $24, $28				*/
   1169   0x001f, 0x7a90,	/* or $15, $31, zero				*/
   1170   0x0318, 0x1040,	/* srl $24, $24, 2				*/
   1171   0x03f9, 0x0f3c,	/* jalr $25					*/
   1172   0x3318, 0xfffe	/* subu $24, $24, 2				*/
   1173 };
   1174 
   1175 /* The format of subsequent standard PLT entries.  */
   1176 static const bfd_vma mips_exec_plt_entry[] =
   1177 {
   1178   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
   1179   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
   1180   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
   1181   0x03200008	/* jr $25					*/
   1182 };
   1183 
   1184 static const bfd_vma mipsr6_exec_plt_entry[] =
   1185 {
   1186   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
   1187   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
   1188   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
   1189   0x03200009	/* jr $25					*/
   1190 };
   1191 
   1192 static const bfd_vma mipsr6_exec_plt_entry_compact[] =
   1193 {
   1194   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
   1195   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
   1196   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
   1197   0xd8190000	/* jic $25, 0					*/
   1198 };
   1199 
   1200 /* The format of subsequent MIPS16 o32 PLT entries.  We use v0 ($2)
   1201    and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
   1202    directly addressable.  */
   1203 static const bfd_vma mips16_o32_exec_plt_entry[] =
   1204 {
   1205   0xb203,		/* lw $2, 12($pc)			*/
   1206   0x9a60,		/* lw $3, 0($2)				*/
   1207   0x651a,		/* move $24, $2				*/
   1208   0xeb00,		/* jr $3				*/
   1209   0x653b,		/* move $25, $3				*/
   1210   0x6500,		/* nop					*/
   1211   0x0000, 0x0000	/* .word (.got.plt entry)		*/
   1212 };
   1213 
   1214 /* The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
   1215    as a temporary because t8 ($24) is not addressable with ADDIUPC.  */
   1216 static const bfd_vma micromips_o32_exec_plt_entry[] =
   1217 {
   1218   0x7900, 0x0000,	/* addiupc $2, (.got.plt entry) - .	*/
   1219   0xff22, 0x0000,	/* lw $25, 0($2)			*/
   1220   0x4599,		/* jr $25				*/
   1221   0x0f02		/* move $24, $2				*/
   1222 };
   1223 
   1224 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode.  */
   1225 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
   1226 {
   1227   0x41af, 0x0000,	/* lui $15, %hi(.got.plt entry)		*/
   1228   0xff2f, 0x0000,	/* lw $25, %lo(.got.plt entry)($15)	*/
   1229   0x0019, 0x0f3c,	/* jr $25				*/
   1230   0x330f, 0x0000	/* addiu $24, $15, %lo(.got.plt entry)	*/
   1231 };
   1232 
   1233 /* The format of the first PLT entry in a VxWorks executable.  */
   1234 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
   1235 {
   1236   0x3c190000,	/* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)		*/
   1237   0x27390000,	/* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_)	*/
   1238   0x8f390008,	/* lw t9, 8(t9)					*/
   1239   0x00000000,	/* nop						*/
   1240   0x03200008,	/* jr t9					*/
   1241   0x00000000	/* nop						*/
   1242 };
   1243 
   1244 /* The format of subsequent PLT entries.  */
   1245 static const bfd_vma mips_vxworks_exec_plt_entry[] =
   1246 {
   1247   0x10000000,	/* b .PLT_resolver			*/
   1248   0x24180000,	/* li t8, <pltindex>			*/
   1249   0x3c190000,	/* lui t9, %hi(<.got.plt slot>)		*/
   1250   0x27390000,	/* addiu t9, t9, %lo(<.got.plt slot>)	*/
   1251   0x8f390000,	/* lw t9, 0(t9)				*/
   1252   0x00000000,	/* nop					*/
   1253   0x03200008,	/* jr t9				*/
   1254   0x00000000	/* nop					*/
   1255 };
   1256 
   1257 /* The format of the first PLT entry in a VxWorks shared object.  */
   1258 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
   1259 {
   1260   0x8f990008,	/* lw t9, 8(gp)		*/
   1261   0x00000000,	/* nop			*/
   1262   0x03200008,	/* jr t9		*/
   1263   0x00000000,	/* nop			*/
   1264   0x00000000,	/* nop			*/
   1265   0x00000000	/* nop			*/
   1266 };
   1267 
   1268 /* The format of subsequent PLT entries.  */
   1269 static const bfd_vma mips_vxworks_shared_plt_entry[] =
   1270 {
   1271   0x10000000,	/* b .PLT_resolver	*/
   1272   0x24180000	/* li t8, <pltindex>	*/
   1273 };
   1274 
   1275 /* microMIPS 32-bit opcode helper installer.  */
   1277 
   1278 static void
   1279 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
   1280 {
   1281   bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
   1282   bfd_put_16 (abfd,  opcode	   & 0xffff, ptr + 2);
   1283 }
   1284 
   1285 /* microMIPS 32-bit opcode helper retriever.  */
   1286 
   1287 static bfd_vma
   1288 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
   1289 {
   1290   return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
   1291 }
   1292 
   1293 /* Look up an entry in a MIPS ELF linker hash table.  */
   1295 
   1296 #define mips_elf_link_hash_lookup(table, string, create, copy, follow)	\
   1297   ((struct mips_elf_link_hash_entry *)					\
   1298    elf_link_hash_lookup (&(table)->root, (string), (create),		\
   1299 			 (copy), (follow)))
   1300 
   1301 /* Traverse a MIPS ELF linker hash table.  */
   1302 
   1303 #define mips_elf_link_hash_traverse(table, func, info)			\
   1304   (elf_link_hash_traverse						\
   1305    (&(table)->root,							\
   1306     (bool (*) (struct elf_link_hash_entry *, void *)) (func),		\
   1307     (info)))
   1308 
   1309 /* Find the base offsets for thread-local storage in this object,
   1310    for GD/LD and IE/LE respectively.  */
   1311 
   1312 #define TP_OFFSET 0x7000
   1313 #define DTP_OFFSET 0x8000
   1314 
   1315 static bfd_vma
   1316 dtprel_base (struct bfd_link_info *info)
   1317 {
   1318   /* If tls_sec is NULL, we should have signalled an error already.  */
   1319   if (elf_hash_table (info)->tls_sec == NULL)
   1320     return 0;
   1321   return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
   1322 }
   1323 
   1324 static bfd_vma
   1325 tprel_base (struct bfd_link_info *info)
   1326 {
   1327   /* If tls_sec is NULL, we should have signalled an error already.  */
   1328   if (elf_hash_table (info)->tls_sec == NULL)
   1329     return 0;
   1330   return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
   1331 }
   1332 
   1333 /* Create an entry in a MIPS ELF linker hash table.  */
   1334 
   1335 static struct bfd_hash_entry *
   1336 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   1337 			    struct bfd_hash_table *table, const char *string)
   1338 {
   1339   struct mips_elf_link_hash_entry *ret =
   1340     (struct mips_elf_link_hash_entry *) entry;
   1341 
   1342   /* Allocate the structure if it has not already been allocated by a
   1343      subclass.  */
   1344   if (ret == NULL)
   1345     ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
   1346   if (ret == NULL)
   1347     return (struct bfd_hash_entry *) ret;
   1348 
   1349   /* Call the allocation method of the superclass.  */
   1350   ret = ((struct mips_elf_link_hash_entry *)
   1351 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
   1352 				     table, string));
   1353   if (ret != NULL)
   1354     {
   1355       /* Set local fields.  */
   1356       memset (&ret->esym, 0, sizeof (EXTR));
   1357       /* We use -2 as a marker to indicate that the information has
   1358 	 not been set.  -1 means there is no associated ifd.  */
   1359       ret->esym.ifd = -2;
   1360       ret->la25_stub = 0;
   1361       ret->possibly_dynamic_relocs = 0;
   1362       ret->fn_stub = NULL;
   1363       ret->call_stub = NULL;
   1364       ret->call_fp_stub = NULL;
   1365       ret->mipsxhash_loc = 0;
   1366       ret->global_got_area = GGA_NONE;
   1367       ret->got_only_for_calls = true;
   1368       ret->readonly_reloc = false;
   1369       ret->has_static_relocs = false;
   1370       ret->no_fn_stub = false;
   1371       ret->need_fn_stub = false;
   1372       ret->has_nonpic_branches = false;
   1373       ret->needs_lazy_stub = false;
   1374       ret->use_plt_entry = false;
   1375     }
   1376 
   1377   return (struct bfd_hash_entry *) ret;
   1378 }
   1379 
   1380 /* Allocate MIPS ELF private object data.  */
   1381 
   1382 bool
   1383 _bfd_mips_elf_mkobject (bfd *abfd)
   1384 {
   1385   return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
   1386 				  MIPS_ELF_DATA);
   1387 }
   1388 
   1389 /* MIPS ELF uses a special find_nearest_line routine in order the
   1390    handle the ECOFF debugging information.  */
   1391 
   1392 struct mips_elf_find_line
   1393 {
   1394   struct ecoff_debug_info d;
   1395   struct ecoff_find_line i;
   1396 };
   1397 
   1398 bool
   1399 _bfd_mips_elf_free_cached_info (bfd *abfd)
   1400 {
   1401   struct mips_elf_obj_tdata *tdata;
   1402 
   1403   if ((bfd_get_format (abfd) == bfd_object
   1404        || bfd_get_format (abfd) == bfd_core)
   1405       && (tdata = mips_elf_tdata (abfd)) != NULL)
   1406     {
   1407       BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
   1408       while (tdata->mips_hi16_list != NULL)
   1409 	{
   1410 	  struct mips_hi16 *hi = tdata->mips_hi16_list;
   1411 	  tdata->mips_hi16_list = hi->next;
   1412 	  free (hi);
   1413 	}
   1414       if (tdata->find_line_info != NULL)
   1415 	_bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
   1416     }
   1417   return _bfd_elf_free_cached_info (abfd);
   1418 }
   1419 
   1420 bool
   1421 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
   1422 {
   1423   if (!sec->used_by_bfd)
   1424     {
   1425       struct _mips_elf_section_data *sdata;
   1426       size_t amt = sizeof (*sdata);
   1427 
   1428       sdata = bfd_zalloc (abfd, amt);
   1429       if (sdata == NULL)
   1430 	return false;
   1431       sec->used_by_bfd = sdata;
   1432     }
   1433 
   1434   return _bfd_elf_new_section_hook (abfd, sec);
   1435 }
   1436 
   1437 /* Read ECOFF debugging information from a .mdebug section into a
   1439    ecoff_debug_info structure.  */
   1440 
   1441 bool
   1442 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
   1443 			       struct ecoff_debug_info *debug)
   1444 {
   1445   HDRR *symhdr;
   1446   const struct ecoff_debug_swap *swap;
   1447   char *ext_hdr;
   1448 
   1449   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   1450   memset (debug, 0, sizeof (*debug));
   1451 
   1452   ext_hdr = bfd_malloc (swap->external_hdr_size);
   1453   if (ext_hdr == NULL && swap->external_hdr_size != 0)
   1454     goto error_return;
   1455 
   1456   if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
   1457 				  swap->external_hdr_size))
   1458     goto error_return;
   1459 
   1460   symhdr = &debug->symbolic_header;
   1461   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
   1462   free (ext_hdr);
   1463   ext_hdr = NULL;
   1464 
   1465   /* The symbolic header contains absolute file offsets and sizes to
   1466      read.  */
   1467 #define READ(ptr, offset, count, size)					\
   1468   do									\
   1469     {									\
   1470       size_t amt;							\
   1471       debug->ptr = NULL;						\
   1472       if (symhdr->count == 0)						\
   1473 	break;								\
   1474       if (_bfd_mul_overflow (size, symhdr->count, &amt))		\
   1475 	{								\
   1476 	  bfd_set_error (bfd_error_file_too_big);			\
   1477 	  goto error_return;						\
   1478 	}								\
   1479       if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0)		\
   1480 	goto error_return;						\
   1481       debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt);		\
   1482       if (debug->ptr == NULL)						\
   1483 	goto error_return;						\
   1484       ((char *) debug->ptr)[amt] = 0;					\
   1485     } while (0)
   1486 
   1487   READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
   1488   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
   1489   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
   1490   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
   1491   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
   1492   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
   1493   READ (ss, cbSsOffset, issMax, sizeof (char));
   1494   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
   1495   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
   1496   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
   1497   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
   1498 #undef READ
   1499 
   1500   return true;
   1501 
   1502  error_return:
   1503   free (ext_hdr);
   1504   _bfd_ecoff_free_ecoff_debug_info (debug);
   1505   return false;
   1506 }
   1507 
   1508 /* Swap RPDR (runtime procedure table entry) for output.  */
   1510 
   1511 static void
   1512 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
   1513 {
   1514   H_PUT_S32 (abfd, in->adr, ex->p_adr);
   1515   H_PUT_32 (abfd, in->regmask, ex->p_regmask);
   1516   H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
   1517   H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
   1518   H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
   1519   H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
   1520 
   1521   H_PUT_16 (abfd, in->framereg, ex->p_framereg);
   1522   H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
   1523 
   1524   H_PUT_32 (abfd, in->irpss, ex->p_irpss);
   1525 }
   1526 
   1527 /* Create a runtime procedure table from the .mdebug section.  */
   1528 
   1529 static bool
   1530 mips_elf_create_procedure_table (void *handle, bfd *abfd,
   1531 				 struct bfd_link_info *info, asection *s,
   1532 				 struct ecoff_debug_info *debug)
   1533 {
   1534   const struct ecoff_debug_swap *swap;
   1535   HDRR *hdr = &debug->symbolic_header;
   1536   RPDR *rpdr, *rp;
   1537   struct rpdr_ext *erp;
   1538   void *rtproc;
   1539   struct pdr_ext *epdr;
   1540   struct sym_ext *esym;
   1541   char *ss, **sv;
   1542   char *str;
   1543   bfd_size_type size;
   1544   bfd_size_type count;
   1545   unsigned long sindex;
   1546   unsigned long i;
   1547   PDR pdr;
   1548   SYMR sym;
   1549   const char *no_name_func = _("static procedure (no name)");
   1550 
   1551   epdr = NULL;
   1552   rpdr = NULL;
   1553   esym = NULL;
   1554   ss = NULL;
   1555   sv = NULL;
   1556 
   1557   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   1558 
   1559   sindex = strlen (no_name_func) + 1;
   1560   count = hdr->ipdMax;
   1561   if (count > 0)
   1562     {
   1563       size = swap->external_pdr_size;
   1564 
   1565       epdr = bfd_malloc (size * count);
   1566       if (epdr == NULL)
   1567 	goto error_return;
   1568 
   1569       if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
   1570 	goto error_return;
   1571 
   1572       size = sizeof (RPDR);
   1573       rp = rpdr = bfd_malloc (size * count);
   1574       if (rpdr == NULL)
   1575 	goto error_return;
   1576 
   1577       size = sizeof (char *);
   1578       sv = bfd_malloc (size * count);
   1579       if (sv == NULL)
   1580 	goto error_return;
   1581 
   1582       count = hdr->isymMax;
   1583       size = swap->external_sym_size;
   1584       esym = bfd_malloc (size * count);
   1585       if (esym == NULL)
   1586 	goto error_return;
   1587 
   1588       if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
   1589 	goto error_return;
   1590 
   1591       count = hdr->issMax;
   1592       ss = bfd_malloc (count);
   1593       if (ss == NULL)
   1594 	goto error_return;
   1595       if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
   1596 	goto error_return;
   1597 
   1598       count = hdr->ipdMax;
   1599       for (i = 0; i < (unsigned long) count; i++, rp++)
   1600 	{
   1601 	  (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
   1602 	  (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
   1603 	  rp->adr = sym.value;
   1604 	  rp->regmask = pdr.regmask;
   1605 	  rp->regoffset = pdr.regoffset;
   1606 	  rp->fregmask = pdr.fregmask;
   1607 	  rp->fregoffset = pdr.fregoffset;
   1608 	  rp->frameoffset = pdr.frameoffset;
   1609 	  rp->framereg = pdr.framereg;
   1610 	  rp->pcreg = pdr.pcreg;
   1611 	  rp->irpss = sindex;
   1612 	  sv[i] = ss + sym.iss;
   1613 	  sindex += strlen (sv[i]) + 1;
   1614 	}
   1615     }
   1616 
   1617   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
   1618   size = BFD_ALIGN (size, 16);
   1619   rtproc = bfd_alloc (abfd, size);
   1620   if (rtproc == NULL)
   1621     {
   1622       mips_elf_hash_table (info)->procedure_count = 0;
   1623       goto error_return;
   1624     }
   1625 
   1626   mips_elf_hash_table (info)->procedure_count = count + 2;
   1627 
   1628   erp = rtproc;
   1629   memset (erp, 0, sizeof (struct rpdr_ext));
   1630   erp++;
   1631   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
   1632   strcpy (str, no_name_func);
   1633   str += strlen (no_name_func) + 1;
   1634   for (i = 0; i < count; i++)
   1635     {
   1636       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
   1637       strcpy (str, sv[i]);
   1638       str += strlen (sv[i]) + 1;
   1639     }
   1640   H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
   1641 
   1642   /* Set the size and contents of .rtproc section.  */
   1643   s->size = size;
   1644   s->contents = rtproc;
   1645 
   1646   /* Skip this section later on (I don't think this currently
   1647      matters, but someday it might).  */
   1648   s->map_head.link_order = NULL;
   1649 
   1650   free (epdr);
   1651   free (rpdr);
   1652   free (esym);
   1653   free (ss);
   1654   free (sv);
   1655   return true;
   1656 
   1657  error_return:
   1658   free (epdr);
   1659   free (rpdr);
   1660   free (esym);
   1661   free (ss);
   1662   free (sv);
   1663   return false;
   1664 }
   1665 
   1666 /* We're going to create a stub for H.  Create a symbol for the stub's
   1668    value and size, to help make the disassembly easier to read.  */
   1669 
   1670 static bool
   1671 mips_elf_create_stub_symbol (struct bfd_link_info *info,
   1672 			     struct mips_elf_link_hash_entry *h,
   1673 			     const char *prefix, asection *s, bfd_vma value,
   1674 			     bfd_vma size)
   1675 {
   1676   bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
   1677   struct bfd_link_hash_entry *bh;
   1678   struct elf_link_hash_entry *elfh;
   1679   char *name;
   1680   bool res;
   1681 
   1682   if (micromips_p)
   1683     value |= 1;
   1684 
   1685   /* Create a new symbol.  */
   1686   name = concat (prefix, h->root.root.root.string, NULL);
   1687   bh = NULL;
   1688   res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
   1689 					  BSF_LOCAL, s, value, NULL,
   1690 					  true, false, &bh);
   1691   free (name);
   1692   if (! res)
   1693     return false;
   1694 
   1695   /* Make it a local function.  */
   1696   elfh = (struct elf_link_hash_entry *) bh;
   1697   elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   1698   elfh->size = size;
   1699   elfh->forced_local = 1;
   1700   if (micromips_p)
   1701     elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
   1702   return true;
   1703 }
   1704 
   1705 /* We're about to redefine H.  Create a symbol to represent H's
   1706    current value and size, to help make the disassembly easier
   1707    to read.  */
   1708 
   1709 static bool
   1710 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
   1711 			       struct mips_elf_link_hash_entry *h,
   1712 			       const char *prefix)
   1713 {
   1714   struct bfd_link_hash_entry *bh;
   1715   struct elf_link_hash_entry *elfh;
   1716   char *name;
   1717   asection *s;
   1718   bfd_vma value;
   1719   bool res;
   1720 
   1721   /* Read the symbol's value.  */
   1722   BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
   1723 	      || h->root.root.type == bfd_link_hash_defweak);
   1724   s = h->root.root.u.def.section;
   1725   value = h->root.root.u.def.value;
   1726 
   1727   /* Create a new symbol.  */
   1728   name = concat (prefix, h->root.root.root.string, NULL);
   1729   bh = NULL;
   1730   res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
   1731 					  BSF_LOCAL, s, value, NULL,
   1732 					  true, false, &bh);
   1733   free (name);
   1734   if (! res)
   1735     return false;
   1736 
   1737   /* Make it local and copy the other attributes from H.  */
   1738   elfh = (struct elf_link_hash_entry *) bh;
   1739   elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
   1740   elfh->other = h->root.other;
   1741   elfh->size = h->root.size;
   1742   elfh->forced_local = 1;
   1743   return true;
   1744 }
   1745 
   1746 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
   1747    function rather than to a hard-float stub.  */
   1748 
   1749 static bool
   1750 section_allows_mips16_refs_p (asection *section)
   1751 {
   1752   const char *name;
   1753 
   1754   name = bfd_section_name (section);
   1755   return (FN_STUB_P (name)
   1756 	  || CALL_STUB_P (name)
   1757 	  || CALL_FP_STUB_P (name)
   1758 	  || strcmp (name, ".pdr") == 0);
   1759 }
   1760 
   1761 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
   1762    stub section of some kind.  Return the R_SYMNDX of the target
   1763    function, or 0 if we can't decide which function that is.  */
   1764 
   1765 static unsigned long
   1766 mips16_stub_symndx (const struct elf_backend_data *bed,
   1767 		    asection *sec ATTRIBUTE_UNUSED,
   1768 		    const Elf_Internal_Rela *relocs,
   1769 		    const Elf_Internal_Rela *relend)
   1770 {
   1771   int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
   1772   const Elf_Internal_Rela *rel;
   1773 
   1774   /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
   1775      one in a compound relocation.  */
   1776   for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
   1777     if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
   1778       return ELF_R_SYM (sec->owner, rel->r_info);
   1779 
   1780   /* Otherwise trust the first relocation, whatever its kind.  This is
   1781      the traditional behavior.  */
   1782   if (relocs < relend)
   1783     return ELF_R_SYM (sec->owner, relocs->r_info);
   1784 
   1785   return 0;
   1786 }
   1787 
   1788 /* Check the mips16 stubs for a particular symbol, and see if we can
   1789    discard them.  */
   1790 
   1791 static void
   1792 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
   1793 			     struct mips_elf_link_hash_entry *h)
   1794 {
   1795   /* Dynamic symbols must use the standard call interface, in case other
   1796      objects try to call them.  */
   1797   if (h->fn_stub != NULL
   1798       && h->root.dynindx != -1)
   1799     {
   1800       mips_elf_create_shadow_symbol (info, h, ".mips16.");
   1801       h->need_fn_stub = true;
   1802     }
   1803 
   1804   if (h->fn_stub != NULL
   1805       && ! h->need_fn_stub)
   1806     {
   1807       /* We don't need the fn_stub; the only references to this symbol
   1808 	 are 16 bit calls.  Clobber the size to 0 to prevent it from
   1809 	 being included in the link.  */
   1810       h->fn_stub->size = 0;
   1811       h->fn_stub->flags &= ~SEC_RELOC;
   1812       h->fn_stub->reloc_count = 0;
   1813       h->fn_stub->flags |= SEC_EXCLUDE;
   1814       h->fn_stub->output_section = bfd_abs_section_ptr;
   1815     }
   1816 
   1817   if (h->call_stub != NULL
   1818       && ELF_ST_IS_MIPS16 (h->root.other))
   1819     {
   1820       /* We don't need the call_stub; this is a 16 bit function, so
   1821 	 calls from other 16 bit functions are OK.  Clobber the size
   1822 	 to 0 to prevent it from being included in the link.  */
   1823       h->call_stub->size = 0;
   1824       h->call_stub->flags &= ~SEC_RELOC;
   1825       h->call_stub->reloc_count = 0;
   1826       h->call_stub->flags |= SEC_EXCLUDE;
   1827       h->call_stub->output_section = bfd_abs_section_ptr;
   1828     }
   1829 
   1830   if (h->call_fp_stub != NULL
   1831       && ELF_ST_IS_MIPS16 (h->root.other))
   1832     {
   1833       /* We don't need the call_stub; this is a 16 bit function, so
   1834 	 calls from other 16 bit functions are OK.  Clobber the size
   1835 	 to 0 to prevent it from being included in the link.  */
   1836       h->call_fp_stub->size = 0;
   1837       h->call_fp_stub->flags &= ~SEC_RELOC;
   1838       h->call_fp_stub->reloc_count = 0;
   1839       h->call_fp_stub->flags |= SEC_EXCLUDE;
   1840       h->call_fp_stub->output_section = bfd_abs_section_ptr;
   1841     }
   1842 }
   1843 
   1844 /* Hashtable callbacks for mips_elf_la25_stubs.  */
   1845 
   1846 static hashval_t
   1847 mips_elf_la25_stub_hash (const void *entry_)
   1848 {
   1849   const struct mips_elf_la25_stub *entry;
   1850 
   1851   entry = (struct mips_elf_la25_stub *) entry_;
   1852   return entry->h->root.root.u.def.section->id
   1853     + entry->h->root.root.u.def.value;
   1854 }
   1855 
   1856 static int
   1857 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
   1858 {
   1859   const struct mips_elf_la25_stub *entry1, *entry2;
   1860 
   1861   entry1 = (struct mips_elf_la25_stub *) entry1_;
   1862   entry2 = (struct mips_elf_la25_stub *) entry2_;
   1863   return ((entry1->h->root.root.u.def.section
   1864 	   == entry2->h->root.root.u.def.section)
   1865 	  && (entry1->h->root.root.u.def.value
   1866 	      == entry2->h->root.root.u.def.value));
   1867 }
   1868 
   1869 /* Called by the linker to set up the la25 stub-creation code.  FN is
   1870    the linker's implementation of add_stub_function.  Return true on
   1871    success.  */
   1872 
   1873 bool
   1874 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
   1875 			  asection *(*fn) (const char *, asection *,
   1876 					   asection *))
   1877 {
   1878   struct mips_elf_link_hash_table *htab;
   1879 
   1880   htab = mips_elf_hash_table (info);
   1881   if (htab == NULL)
   1882     return false;
   1883 
   1884   htab->add_stub_section = fn;
   1885   htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
   1886 				      mips_elf_la25_stub_eq, NULL);
   1887   if (htab->la25_stubs == NULL)
   1888     return false;
   1889 
   1890   return true;
   1891 }
   1892 
   1893 /* Return true if H is a locally-defined PIC function, in the sense
   1894    that it or its fn_stub might need $25 to be valid on entry.
   1895    Note that MIPS16 functions set up $gp using PC-relative instructions,
   1896    so they themselves never need $25 to be valid.  Only non-MIPS16
   1897    entry points are of interest here.  */
   1898 
   1899 static bool
   1900 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
   1901 {
   1902   return ((h->root.root.type == bfd_link_hash_defined
   1903 	   || h->root.root.type == bfd_link_hash_defweak)
   1904 	  && h->root.def_regular
   1905 	  && !bfd_is_abs_section (h->root.root.u.def.section)
   1906 	  && !bfd_is_und_section (h->root.root.u.def.section)
   1907 	  && (!ELF_ST_IS_MIPS16 (h->root.other)
   1908 	      || (h->fn_stub && h->need_fn_stub))
   1909 	  && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
   1910 	      || ELF_ST_IS_MIPS_PIC (h->root.other)));
   1911 }
   1912 
   1913 /* Set *SEC to the input section that contains the target of STUB.
   1914    Return the offset of the target from the start of that section.  */
   1915 
   1916 static bfd_vma
   1917 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
   1918 			  asection **sec)
   1919 {
   1920   if (ELF_ST_IS_MIPS16 (stub->h->root.other))
   1921     {
   1922       BFD_ASSERT (stub->h->need_fn_stub);
   1923       *sec = stub->h->fn_stub;
   1924       return 0;
   1925     }
   1926   else
   1927     {
   1928       *sec = stub->h->root.root.u.def.section;
   1929       return stub->h->root.root.u.def.value;
   1930     }
   1931 }
   1932 
   1933 /* STUB describes an la25 stub that we have decided to implement
   1934    by inserting an LUI/ADDIU pair before the target function.
   1935    Create the section and redirect the function symbol to it.  */
   1936 
   1937 static bool
   1938 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
   1939 			 struct bfd_link_info *info)
   1940 {
   1941   struct mips_elf_link_hash_table *htab;
   1942   char *name;
   1943   asection *s, *input_section;
   1944   unsigned int align;
   1945 
   1946   htab = mips_elf_hash_table (info);
   1947   if (htab == NULL)
   1948     return false;
   1949 
   1950   /* Create a unique name for the new section.  */
   1951   name = bfd_malloc (11 + sizeof (".text.stub."));
   1952   if (name == NULL)
   1953     return false;
   1954   sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
   1955 
   1956   /* Create the section.  */
   1957   mips_elf_get_la25_target (stub, &input_section);
   1958   s = htab->add_stub_section (name, input_section,
   1959 			      input_section->output_section);
   1960   if (s == NULL)
   1961     return false;
   1962 
   1963   /* Make sure that any padding goes before the stub.  */
   1964   align = input_section->alignment_power;
   1965   if (!bfd_set_section_alignment (s, align))
   1966     return false;
   1967   if (align > 3)
   1968     s->size = (1 << align) - 8;
   1969 
   1970   /* Create a symbol for the stub.  */
   1971   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
   1972   stub->stub_section = s;
   1973   stub->offset = s->size;
   1974 
   1975   /* Allocate room for it.  */
   1976   s->size += 8;
   1977   return true;
   1978 }
   1979 
   1980 /* STUB describes an la25 stub that we have decided to implement
   1981    with a separate trampoline.  Allocate room for it and redirect
   1982    the function symbol to it.  */
   1983 
   1984 static bool
   1985 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
   1986 			      struct bfd_link_info *info)
   1987 {
   1988   struct mips_elf_link_hash_table *htab;
   1989   asection *s;
   1990 
   1991   htab = mips_elf_hash_table (info);
   1992   if (htab == NULL)
   1993     return false;
   1994 
   1995   /* Create a trampoline section, if we haven't already.  */
   1996   s = htab->strampoline;
   1997   if (s == NULL)
   1998     {
   1999       asection *input_section = stub->h->root.root.u.def.section;
   2000       s = htab->add_stub_section (".text", NULL,
   2001 				  input_section->output_section);
   2002       if (s == NULL || !bfd_set_section_alignment (s, 4))
   2003 	return false;
   2004       htab->strampoline = s;
   2005     }
   2006 
   2007   /* Create a symbol for the stub.  */
   2008   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
   2009   stub->stub_section = s;
   2010   stub->offset = s->size;
   2011 
   2012   /* Allocate room for it.  */
   2013   s->size += 16;
   2014   return true;
   2015 }
   2016 
   2017 /* H describes a symbol that needs an la25 stub.  Make sure that an
   2018    appropriate stub exists and point H at it.  */
   2019 
   2020 static bool
   2021 mips_elf_add_la25_stub (struct bfd_link_info *info,
   2022 			struct mips_elf_link_hash_entry *h)
   2023 {
   2024   struct mips_elf_link_hash_table *htab;
   2025   struct mips_elf_la25_stub search, *stub;
   2026   bool use_trampoline_p;
   2027   asection *s;
   2028   bfd_vma value;
   2029   void **slot;
   2030 
   2031   /* Describe the stub we want.  */
   2032   search.stub_section = NULL;
   2033   search.offset = 0;
   2034   search.h = h;
   2035 
   2036   /* See if we've already created an equivalent stub.  */
   2037   htab = mips_elf_hash_table (info);
   2038   if (htab == NULL)
   2039     return false;
   2040 
   2041   slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
   2042   if (slot == NULL)
   2043     return false;
   2044 
   2045   stub = (struct mips_elf_la25_stub *) *slot;
   2046   if (stub != NULL)
   2047     {
   2048       /* We can reuse the existing stub.  */
   2049       h->la25_stub = stub;
   2050       return true;
   2051     }
   2052 
   2053   /* Create a permanent copy of ENTRY and add it to the hash table.  */
   2054   stub = bfd_malloc (sizeof (search));
   2055   if (stub == NULL)
   2056     return false;
   2057   *stub = search;
   2058   *slot = stub;
   2059 
   2060   /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
   2061      of the section and if we would need no more than 2 nops.  */
   2062   value = mips_elf_get_la25_target (stub, &s);
   2063   if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
   2064     value &= ~1;
   2065   use_trampoline_p = (value != 0 || s->alignment_power > 4);
   2066 
   2067   h->la25_stub = stub;
   2068   return (use_trampoline_p
   2069 	  ? mips_elf_add_la25_trampoline (stub, info)
   2070 	  : mips_elf_add_la25_intro (stub, info));
   2071 }
   2072 
   2073 /* A mips_elf_link_hash_traverse callback that is called before sizing
   2074    sections.  DATA points to a mips_htab_traverse_info structure.  */
   2075 
   2076 static bool
   2077 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
   2078 {
   2079   struct mips_htab_traverse_info *hti;
   2080 
   2081   hti = (struct mips_htab_traverse_info *) data;
   2082   if (!bfd_link_relocatable (hti->info))
   2083     mips_elf_check_mips16_stubs (hti->info, h);
   2084 
   2085   if (mips_elf_local_pic_function_p (h))
   2086     {
   2087       /* PR 12845: If H is in a section that has been garbage
   2088 	 collected it will have its output section set to *ABS*.  */
   2089       if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
   2090 	return true;
   2091 
   2092       /* H is a function that might need $25 to be valid on entry.
   2093 	 If we're creating a non-PIC relocatable object, mark H as
   2094 	 being PIC.  If we're creating a non-relocatable object with
   2095 	 non-PIC branches and jumps to H, make sure that H has an la25
   2096 	 stub.  */
   2097       if (bfd_link_relocatable (hti->info))
   2098 	{
   2099 	  if (!PIC_OBJECT_P (hti->output_bfd))
   2100 	    h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
   2101 	}
   2102       else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
   2103 	{
   2104 	  hti->error = true;
   2105 	  return false;
   2106 	}
   2107     }
   2108   return true;
   2109 }
   2110 
   2111 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
   2113    Most mips16 instructions are 16 bits, but these instructions
   2114    are 32 bits.
   2115 
   2116    The format of these instructions is:
   2117 
   2118    +--------------+--------------------------------+
   2119    |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
   2120    +--------------+--------------------------------+
   2121    |		    Immediate  15:0		   |
   2122    +-----------------------------------------------+
   2123 
   2124    JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
   2125    Note that the immediate value in the first word is swapped.
   2126 
   2127    When producing a relocatable object file, R_MIPS16_26 is
   2128    handled mostly like R_MIPS_26.  In particular, the addend is
   2129    stored as a straight 26-bit value in a 32-bit instruction.
   2130    (gas makes life simpler for itself by never adjusting a
   2131    R_MIPS16_26 reloc to be against a section, so the addend is
   2132    always zero).  However, the 32 bit instruction is stored as 2
   2133    16-bit values, rather than a single 32-bit value.  In a
   2134    big-endian file, the result is the same; in a little-endian
   2135    file, the two 16-bit halves of the 32 bit value are swapped.
   2136    This is so that a disassembler can recognize the jal
   2137    instruction.
   2138 
   2139    When doing a final link, R_MIPS16_26 is treated as a 32 bit
   2140    instruction stored as two 16-bit values.  The addend A is the
   2141    contents of the targ26 field.  The calculation is the same as
   2142    R_MIPS_26.  When storing the calculated value, reorder the
   2143    immediate value as shown above, and don't forget to store the
   2144    value as two 16-bit values.
   2145 
   2146    To put it in MIPS ABI terms, the relocation field is T-targ26-16,
   2147    defined as
   2148 
   2149    big-endian:
   2150    +--------+----------------------+
   2151    |	    |			   |
   2152    |	    |	 targ26-16	   |
   2153    |31	  26|25			  0|
   2154    +--------+----------------------+
   2155 
   2156    little-endian:
   2157    +----------+------+-------------+
   2158    |	      |	     |		   |
   2159    |  sub1    |	     |	   sub2	   |
   2160    |0	     9|10  15|16	 31|
   2161    +----------+--------------------+
   2162    where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
   2163    ((sub1 << 16) | sub2)).
   2164 
   2165    When producing a relocatable object file, the calculation is
   2166    (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
   2167    When producing a fully linked file, the calculation is
   2168    let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
   2169    ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
   2170 
   2171    The table below lists the other MIPS16 instruction relocations.
   2172    Each one is calculated in the same way as the non-MIPS16 relocation
   2173    given on the right, but using the extended MIPS16 layout of 16-bit
   2174    immediate fields:
   2175 
   2176 	R_MIPS16_GPREL		R_MIPS_GPREL16
   2177 	R_MIPS16_GOT16		R_MIPS_GOT16
   2178 	R_MIPS16_CALL16		R_MIPS_CALL16
   2179 	R_MIPS16_HI16		R_MIPS_HI16
   2180 	R_MIPS16_LO16		R_MIPS_LO16
   2181 
   2182    A typical instruction will have a format like this:
   2183 
   2184    +--------------+--------------------------------+
   2185    |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
   2186    +--------------+--------------------------------+
   2187    |    Major     |   rx   |   ry   |   Imm  4:0   |
   2188    +--------------+--------------------------------+
   2189 
   2190    EXTEND is the five bit value 11110.  Major is the instruction
   2191    opcode.
   2192 
   2193    All we need to do here is shuffle the bits appropriately.
   2194    As above, the two 16-bit halves must be swapped on a
   2195    little-endian system.
   2196 
   2197    Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
   2198    relocatable field is shifted by 1 rather than 2 and the same bit
   2199    shuffling is done as with the relocations above.  */
   2200 
   2201 static inline bool
   2202 mips16_reloc_p (int r_type)
   2203 {
   2204   switch (r_type)
   2205     {
   2206     case R_MIPS16_26:
   2207     case R_MIPS16_GPREL:
   2208     case R_MIPS16_GOT16:
   2209     case R_MIPS16_CALL16:
   2210     case R_MIPS16_HI16:
   2211     case R_MIPS16_LO16:
   2212     case R_MIPS16_TLS_GD:
   2213     case R_MIPS16_TLS_LDM:
   2214     case R_MIPS16_TLS_DTPREL_HI16:
   2215     case R_MIPS16_TLS_DTPREL_LO16:
   2216     case R_MIPS16_TLS_GOTTPREL:
   2217     case R_MIPS16_TLS_TPREL_HI16:
   2218     case R_MIPS16_TLS_TPREL_LO16:
   2219     case R_MIPS16_PC16_S1:
   2220       return true;
   2221 
   2222     default:
   2223       return false;
   2224     }
   2225 }
   2226 
   2227 /* Check if a microMIPS reloc.  */
   2228 
   2229 static inline bool
   2230 micromips_reloc_p (unsigned int r_type)
   2231 {
   2232   return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
   2233 }
   2234 
   2235 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
   2236    on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1,
   2237    R_MICROMIPS_PC10_S1 and R_MICROMIPS_GPREL7_S2 relocs that apply to
   2238    16-bit instructions.  */
   2239 
   2240 static inline bool
   2241 micromips_reloc_shuffle_p (unsigned int r_type)
   2242 {
   2243   return (micromips_reloc_p (r_type)
   2244 	  && r_type != R_MICROMIPS_PC7_S1
   2245 	  && r_type != R_MICROMIPS_PC10_S1
   2246 	  && r_type != R_MICROMIPS_GPREL7_S2);
   2247 }
   2248 
   2249 static inline bool
   2250 got16_reloc_p (int r_type)
   2251 {
   2252   return (r_type == R_MIPS_GOT16
   2253 	  || r_type == R_MIPS16_GOT16
   2254 	  || r_type == R_MICROMIPS_GOT16);
   2255 }
   2256 
   2257 static inline bool
   2258 call16_reloc_p (int r_type)
   2259 {
   2260   return (r_type == R_MIPS_CALL16
   2261 	  || r_type == R_MIPS16_CALL16
   2262 	  || r_type == R_MICROMIPS_CALL16);
   2263 }
   2264 
   2265 static inline bool
   2266 got_disp_reloc_p (unsigned int r_type)
   2267 {
   2268   return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
   2269 }
   2270 
   2271 static inline bool
   2272 got_page_reloc_p (unsigned int r_type)
   2273 {
   2274   return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
   2275 }
   2276 
   2277 static inline bool
   2278 got_lo16_reloc_p (unsigned int r_type)
   2279 {
   2280   return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
   2281 }
   2282 
   2283 static inline bool
   2284 call_hi16_reloc_p (unsigned int r_type)
   2285 {
   2286   return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
   2287 }
   2288 
   2289 static inline bool
   2290 call_lo16_reloc_p (unsigned int r_type)
   2291 {
   2292   return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
   2293 }
   2294 
   2295 static inline bool
   2296 hi16_reloc_p (int r_type)
   2297 {
   2298   return (r_type == R_MIPS_HI16
   2299 	  || r_type == R_MIPS16_HI16
   2300 	  || r_type == R_MICROMIPS_HI16
   2301 	  || r_type == R_MIPS_PCHI16);
   2302 }
   2303 
   2304 static inline bool
   2305 lo16_reloc_p (int r_type)
   2306 {
   2307   return (r_type == R_MIPS_LO16
   2308 	  || r_type == R_MIPS16_LO16
   2309 	  || r_type == R_MICROMIPS_LO16
   2310 	  || r_type == R_MIPS_PCLO16);
   2311 }
   2312 
   2313 static inline bool
   2314 mips16_call_reloc_p (int r_type)
   2315 {
   2316   return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
   2317 }
   2318 
   2319 static inline bool
   2320 jal_reloc_p (int r_type)
   2321 {
   2322   return (r_type == R_MIPS_26
   2323 	  || r_type == R_MIPS16_26
   2324 	  || r_type == R_MICROMIPS_26_S1);
   2325 }
   2326 
   2327 static inline bool
   2328 b_reloc_p (int r_type)
   2329 {
   2330   return (r_type == R_MIPS_PC26_S2
   2331 	  || r_type == R_MIPS_PC21_S2
   2332 	  || r_type == R_MIPS_PC16
   2333 	  || r_type == R_MIPS_GNU_REL16_S2
   2334 	  || r_type == R_MIPS16_PC16_S1
   2335 	  || r_type == R_MICROMIPS_PC16_S1
   2336 	  || r_type == R_MICROMIPS_PC10_S1
   2337 	  || r_type == R_MICROMIPS_PC7_S1);
   2338 }
   2339 
   2340 static inline bool
   2341 aligned_pcrel_reloc_p (int r_type)
   2342 {
   2343   return (r_type == R_MIPS_PC18_S3
   2344 	  || r_type == R_MIPS_PC19_S2);
   2345 }
   2346 
   2347 static inline bool
   2348 branch_reloc_p (int r_type)
   2349 {
   2350   return (r_type == R_MIPS_26
   2351 	  || r_type == R_MIPS_PC26_S2
   2352 	  || r_type == R_MIPS_PC21_S2
   2353 	  || r_type == R_MIPS_PC16
   2354 	  || r_type == R_MIPS_GNU_REL16_S2);
   2355 }
   2356 
   2357 static inline bool
   2358 mips16_branch_reloc_p (int r_type)
   2359 {
   2360   return (r_type == R_MIPS16_26
   2361 	  || r_type == R_MIPS16_PC16_S1);
   2362 }
   2363 
   2364 static inline bool
   2365 micromips_branch_reloc_p (int r_type)
   2366 {
   2367   return (r_type == R_MICROMIPS_26_S1
   2368 	  || r_type == R_MICROMIPS_PC16_S1
   2369 	  || r_type == R_MICROMIPS_PC10_S1
   2370 	  || r_type == R_MICROMIPS_PC7_S1);
   2371 }
   2372 
   2373 static inline bool
   2374 tls_gd_reloc_p (unsigned int r_type)
   2375 {
   2376   return (r_type == R_MIPS_TLS_GD
   2377 	  || r_type == R_MIPS16_TLS_GD
   2378 	  || r_type == R_MICROMIPS_TLS_GD);
   2379 }
   2380 
   2381 static inline bool
   2382 tls_ldm_reloc_p (unsigned int r_type)
   2383 {
   2384   return (r_type == R_MIPS_TLS_LDM
   2385 	  || r_type == R_MIPS16_TLS_LDM
   2386 	  || r_type == R_MICROMIPS_TLS_LDM);
   2387 }
   2388 
   2389 static inline bool
   2390 tls_gottprel_reloc_p (unsigned int r_type)
   2391 {
   2392   return (r_type == R_MIPS_TLS_GOTTPREL
   2393 	  || r_type == R_MIPS16_TLS_GOTTPREL
   2394 	  || r_type == R_MICROMIPS_TLS_GOTTPREL);
   2395 }
   2396 
   2397 static inline bool
   2398 needs_shuffle (int r_type)
   2399 {
   2400   return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
   2401 }
   2402 
   2403 void
   2404 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
   2405 			       bool jal_shuffle, bfd_byte *data)
   2406 {
   2407   bfd_vma first, second, val;
   2408 
   2409   if (!needs_shuffle (r_type))
   2410     return;
   2411 
   2412   /* Pick up the first and second halfwords of the instruction.  */
   2413   first = bfd_get_16 (abfd, data);
   2414   second = bfd_get_16 (abfd, data + 2);
   2415   if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
   2416     val = first << 16 | second;
   2417   else if (r_type != R_MIPS16_26)
   2418     val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
   2419 	   | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
   2420   else
   2421     val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
   2422 	   | ((first & 0x1f) << 21) | second);
   2423   bfd_put_32 (abfd, val, data);
   2424 }
   2425 
   2426 void
   2427 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
   2428 			     bool jal_shuffle, bfd_byte *data)
   2429 {
   2430   bfd_vma first, second, val;
   2431 
   2432   if (!needs_shuffle (r_type))
   2433     return;
   2434 
   2435   val = bfd_get_32 (abfd, data);
   2436   if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
   2437     {
   2438       second = val & 0xffff;
   2439       first = val >> 16;
   2440     }
   2441   else if (r_type != R_MIPS16_26)
   2442     {
   2443       second = ((val >> 11) & 0xffe0) | (val & 0x1f);
   2444       first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
   2445     }
   2446   else
   2447     {
   2448       second = val & 0xffff;
   2449       first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
   2450 	       | ((val >> 21) & 0x1f);
   2451     }
   2452   bfd_put_16 (abfd, second, data + 2);
   2453   bfd_put_16 (abfd, first, data);
   2454 }
   2455 
   2456 /* Perform reloc offset checking.
   2457    We can only use bfd_reloc_offset_in_range, which takes into account
   2458    the size of the field being relocated, when section contents will
   2459    be accessed because mips object files may use relocations that seem
   2460    to access beyond section limits.
   2461    gas/testsuite/gas/mips/dla-reloc.s is an example that puts
   2462    R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
   2463    section.  The R_MIPS_SUB applies to the addend for the next reloc
   2464    rather than the section contents.
   2465 
   2466    CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
   2467    CHECK_INPLACE to only check partial_inplace relocs, and
   2468    CHECK_SHUFFLE to only check relocs that shuffle/unshuffle.  */
   2469 
   2470 bool
   2471 _bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
   2472 				 arelent *reloc_entry, enum reloc_check check)
   2473 {
   2474   if (check == check_inplace && !reloc_entry->howto->partial_inplace)
   2475     return true;
   2476   if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
   2477     return true;
   2478   return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
   2479 				    input_section, reloc_entry->address);
   2480 }
   2481 
   2482 bfd_reloc_status_type
   2483 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
   2484 			       arelent *reloc_entry, asection *input_section,
   2485 			       bool relocatable, void *data, bfd_vma gp)
   2486 {
   2487   bfd_vma relocation;
   2488   bfd_signed_vma val;
   2489   bfd_reloc_status_type status;
   2490 
   2491   if (bfd_is_com_section (symbol->section))
   2492     relocation = 0;
   2493   else
   2494     relocation = symbol->value;
   2495 
   2496   if (symbol->section->output_section != NULL)
   2497     {
   2498       relocation += symbol->section->output_section->vma;
   2499       relocation += symbol->section->output_offset;
   2500     }
   2501 
   2502   /* Set val to the offset into the section or symbol.  */
   2503   val = reloc_entry->addend;
   2504 
   2505   _bfd_mips_elf_sign_extend (val, 16);
   2506 
   2507   /* Adjust val for the final section location and GP value.  If we
   2508      are producing relocatable output, we don't want to do this for
   2509      an external symbol.  */
   2510   if (! relocatable
   2511       || (symbol->flags & BSF_SECTION_SYM) != 0)
   2512     val += relocation - gp;
   2513 
   2514   if (reloc_entry->howto->partial_inplace)
   2515     {
   2516       if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
   2517 				      reloc_entry->address))
   2518 	return bfd_reloc_outofrange;
   2519 
   2520       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
   2521 				       (bfd_byte *) data
   2522 				       + reloc_entry->address);
   2523       if (status != bfd_reloc_ok)
   2524 	return status;
   2525     }
   2526   else
   2527     reloc_entry->addend = val;
   2528 
   2529   if (relocatable)
   2530     reloc_entry->address += input_section->output_offset;
   2531 
   2532   return bfd_reloc_ok;
   2533 }
   2534 
   2535 /* A howto special_function for REL *HI16 relocations.  We can only
   2536    calculate the correct value once we've seen the partnering
   2537    *LO16 relocation, so just save the information for later.
   2538 
   2539    The ABI requires that the *LO16 immediately follow the *HI16.
   2540    However, as a GNU extension, we permit an arbitrary number of
   2541    *HI16s to be associated with a single *LO16.  This significantly
   2542    simplies the relocation handling in gcc.  */
   2543 
   2544 bfd_reloc_status_type
   2545 _bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
   2546 			  asymbol *symbol ATTRIBUTE_UNUSED, void *data,
   2547 			  asection *input_section, bfd *output_bfd,
   2548 			  char **error_message ATTRIBUTE_UNUSED)
   2549 {
   2550   struct mips_hi16 *n;
   2551   struct mips_elf_obj_tdata *tdata;
   2552 
   2553   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2554     return bfd_reloc_outofrange;
   2555 
   2556   n = bfd_malloc (sizeof *n);
   2557   if (n == NULL)
   2558     return bfd_reloc_outofrange;
   2559 
   2560   tdata = mips_elf_tdata (abfd);
   2561   n->next = tdata->mips_hi16_list;
   2562   n->data = data;
   2563   n->input_section = input_section;
   2564   n->rel = *reloc_entry;
   2565   tdata->mips_hi16_list = n;
   2566 
   2567   if (output_bfd != NULL)
   2568     reloc_entry->address += input_section->output_offset;
   2569 
   2570   return bfd_reloc_ok;
   2571 }
   2572 
   2573 /* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
   2574    like any other 16-bit relocation when applied to global symbols, but is
   2575    treated in the same as R_MIPS_HI16 when applied to local symbols.  */
   2576 
   2577 bfd_reloc_status_type
   2578 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2579 			   void *data, asection *input_section,
   2580 			   bfd *output_bfd, char **error_message)
   2581 {
   2582   if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
   2583       || bfd_is_und_section (bfd_asymbol_section (symbol))
   2584       || bfd_is_com_section (bfd_asymbol_section (symbol)))
   2585     /* The relocation is against a global symbol.  */
   2586     return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2587 					input_section, output_bfd,
   2588 					error_message);
   2589 
   2590   return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
   2591 				   input_section, output_bfd, error_message);
   2592 }
   2593 
   2594 /* A howto special_function for REL *LO16 relocations.  The *LO16 itself
   2595    is a straightforward 16 bit inplace relocation, but we must deal with
   2596    any partnering high-part relocations as well.  */
   2597 
   2598 bfd_reloc_status_type
   2599 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2600 			  void *data, asection *input_section,
   2601 			  bfd *output_bfd, char **error_message)
   2602 {
   2603   bfd_vma vallo;
   2604   bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
   2605   struct mips_elf_obj_tdata *tdata;
   2606 
   2607   if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
   2608 				  reloc_entry->address))
   2609     return bfd_reloc_outofrange;
   2610 
   2611   _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
   2612 				 location);
   2613   /* The high 16 bits of the addend are stored in the high insn, the
   2614      low 16 bits in the low insn, but there is a catch:  You can't
   2615      just concatenate the high and low parts.  The high part of the
   2616      addend is adjusted for the fact that the low part is sign
   2617      extended.  For example, an addend of 0x38000 would have 0x0004 in
   2618      the high part and 0x8000 (=0xff..f8000) in the low part.
   2619      To extract the actual addend, calculate (a)
   2620      ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.
   2621      We will be applying (symbol + addend) & 0xffff to the low insn,
   2622      and we want to apply (b) (symbol + addend + 0x8000) >> 16 to the
   2623      high insn (the +0x8000 adjusting for when the applied low part is
   2624      negative).  Substituting (a) into (b) and recognising that
   2625      (hi & 0xffff) is already in the high insn gives a high part
   2626      addend adjustment of (lo & 0xffff) ^ 0x8000.  */
   2627   vallo = (bfd_get_32 (abfd, location) & 0xffff) ^ 0x8000;
   2628   _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
   2629 			       location);
   2630 
   2631   tdata = mips_elf_tdata (abfd);
   2632   while (tdata->mips_hi16_list != NULL)
   2633     {
   2634       bfd_reloc_status_type ret;
   2635       struct mips_hi16 *hi;
   2636 
   2637       hi = tdata->mips_hi16_list;
   2638 
   2639       /* R_MIPS*_GOT16 relocations are something of a special case.  We
   2640 	 want to install the addend in the same way as for a R_MIPS*_HI16
   2641 	 relocation (with a rightshift of 16).  However, since GOT16
   2642 	 relocations can also be used with global symbols, their howto
   2643 	 has a rightshift of 0.  */
   2644       if (hi->rel.howto->type == R_MIPS_GOT16)
   2645 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
   2646       else if (hi->rel.howto->type == R_MIPS16_GOT16)
   2647 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
   2648       else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
   2649 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
   2650 
   2651       hi->rel.addend += vallo;
   2652 
   2653       ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
   2654 					 hi->input_section, output_bfd,
   2655 					 error_message);
   2656       if (ret != bfd_reloc_ok)
   2657 	return ret;
   2658 
   2659       tdata->mips_hi16_list = hi->next;
   2660       free (hi);
   2661     }
   2662 
   2663   return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2664 				      input_section, output_bfd,
   2665 				      error_message);
   2666 }
   2667 
   2668 /* A generic howto special_function.  This calculates and installs the
   2669    relocation itself, thus avoiding the oft-discussed problems in
   2670    bfd_perform_relocation and bfd_install_relocation.  */
   2671 
   2672 bfd_reloc_status_type
   2673 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   2674 			     asymbol *symbol, void *data ATTRIBUTE_UNUSED,
   2675 			     asection *input_section, bfd *output_bfd,
   2676 			     char **error_message ATTRIBUTE_UNUSED)
   2677 {
   2678   bfd_signed_vma val;
   2679   bfd_reloc_status_type status;
   2680   bool relocatable;
   2681 
   2682   relocatable = (output_bfd != NULL);
   2683 
   2684   if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
   2685 					(relocatable
   2686 					 ? check_inplace : check_std)))
   2687     return bfd_reloc_outofrange;
   2688 
   2689   /* Build up the field adjustment in VAL.  */
   2690   val = 0;
   2691   if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
   2692       && symbol->section->output_section != NULL)
   2693     {
   2694       /* Either we're calculating the final field value or we have a
   2695 	 relocation against a section symbol.  Add in the section's
   2696 	 offset or address.  */
   2697       val += symbol->section->output_section->vma;
   2698       val += symbol->section->output_offset;
   2699     }
   2700 
   2701   if (!relocatable)
   2702     {
   2703       /* We're calculating the final field value.  Add in the symbol's value
   2704 	 and, if pc-relative, subtract the address of the field itself.  */
   2705       val += symbol->value;
   2706       if (reloc_entry->howto->pc_relative)
   2707 	{
   2708 	  val -= input_section->output_section->vma;
   2709 	  val -= input_section->output_offset;
   2710 	  val -= reloc_entry->address;
   2711 	}
   2712     }
   2713 
   2714   /* VAL is now the final adjustment.  If we're keeping this relocation
   2715      in the output file, and if the relocation uses a separate addend,
   2716      we just need to add VAL to that addend.  Otherwise we need to add
   2717      VAL to the relocation field itself.  */
   2718   if (relocatable && !reloc_entry->howto->partial_inplace)
   2719     reloc_entry->addend += val;
   2720   else
   2721     {
   2722       bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
   2723 
   2724       /* Add in the separate addend, if any.  */
   2725       val += reloc_entry->addend;
   2726 
   2727       /* Add VAL to the relocation field.  */
   2728       _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
   2729 				     location);
   2730       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
   2731 				       location);
   2732       _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
   2733 				   location);
   2734 
   2735       if (status != bfd_reloc_ok)
   2736 	return status;
   2737     }
   2738 
   2739   if (relocatable)
   2740     reloc_entry->address += input_section->output_offset;
   2741 
   2742   return bfd_reloc_ok;
   2743 }
   2744 
   2745 /* Swap an entry in a .gptab section.  Note that these routines rely
   2747    on the equivalence of the two elements of the union.  */
   2748 
   2749 static void
   2750 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
   2751 			      Elf32_gptab *in)
   2752 {
   2753   in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
   2754   in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
   2755 }
   2756 
   2757 static void
   2758 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
   2759 			       Elf32_External_gptab *ex)
   2760 {
   2761   H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
   2762   H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
   2763 }
   2764 
   2765 static void
   2766 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
   2767 				Elf32_External_compact_rel *ex)
   2768 {
   2769   H_PUT_32 (abfd, in->id1, ex->id1);
   2770   H_PUT_32 (abfd, in->num, ex->num);
   2771   H_PUT_32 (abfd, in->id2, ex->id2);
   2772   H_PUT_32 (abfd, in->offset, ex->offset);
   2773   H_PUT_32 (abfd, in->reserved0, ex->reserved0);
   2774   H_PUT_32 (abfd, in->reserved1, ex->reserved1);
   2775 }
   2776 
   2777 static void
   2778 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
   2779 			   Elf32_External_crinfo *ex)
   2780 {
   2781   unsigned long l;
   2782 
   2783   l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
   2784        | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
   2785        | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
   2786        | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
   2787   H_PUT_32 (abfd, l, ex->info);
   2788   H_PUT_32 (abfd, in->konst, ex->konst);
   2789   H_PUT_32 (abfd, in->vaddr, ex->vaddr);
   2790 }
   2791 
   2792 /* A .reginfo section holds a single Elf32_RegInfo structure.  These
   2794    routines swap this structure in and out.  They are used outside of
   2795    BFD, so they are globally visible.  */
   2796 
   2797 void
   2798 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
   2799 				Elf32_RegInfo *in)
   2800 {
   2801   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
   2802   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
   2803   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
   2804   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
   2805   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
   2806   in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
   2807 }
   2808 
   2809 void
   2810 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
   2811 				 Elf32_External_RegInfo *ex)
   2812 {
   2813   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
   2814   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
   2815   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
   2816   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
   2817   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
   2818   H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
   2819 }
   2820 
   2821 /* In the 64 bit ABI, the .MIPS.options section holds register
   2822    information in an Elf64_Reginfo structure.  These routines swap
   2823    them in and out.  They are globally visible because they are used
   2824    outside of BFD.  These routines are here so that gas can call them
   2825    without worrying about whether the 64 bit ABI has been included.  */
   2826 
   2827 void
   2828 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
   2829 				Elf64_Internal_RegInfo *in)
   2830 {
   2831   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
   2832   in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
   2833   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
   2834   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
   2835   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
   2836   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
   2837   in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
   2838 }
   2839 
   2840 void
   2841 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
   2842 				 Elf64_External_RegInfo *ex)
   2843 {
   2844   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
   2845   H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
   2846   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
   2847   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
   2848   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
   2849   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
   2850   H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
   2851 }
   2852 
   2853 /* Swap in an options header.  */
   2854 
   2855 void
   2856 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
   2857 			      Elf_Internal_Options *in)
   2858 {
   2859   in->kind = H_GET_8 (abfd, ex->kind);
   2860   in->size = H_GET_8 (abfd, ex->size);
   2861   in->section = H_GET_16 (abfd, ex->section);
   2862   in->info = H_GET_32 (abfd, ex->info);
   2863 }
   2864 
   2865 /* Swap out an options header.  */
   2866 
   2867 void
   2868 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
   2869 			       Elf_External_Options *ex)
   2870 {
   2871   H_PUT_8 (abfd, in->kind, ex->kind);
   2872   H_PUT_8 (abfd, in->size, ex->size);
   2873   H_PUT_16 (abfd, in->section, ex->section);
   2874   H_PUT_32 (abfd, in->info, ex->info);
   2875 }
   2876 
   2877 /* Swap in an abiflags structure.  */
   2878 
   2879 void
   2880 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
   2881 				  const Elf_External_ABIFlags_v0 *ex,
   2882 				  Elf_Internal_ABIFlags_v0 *in)
   2883 {
   2884   in->version = H_GET_16 (abfd, ex->version);
   2885   in->isa_level = H_GET_8 (abfd, ex->isa_level);
   2886   in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
   2887   in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
   2888   in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
   2889   in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
   2890   in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
   2891   in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
   2892   in->ases = H_GET_32 (abfd, ex->ases);
   2893   in->flags1 = H_GET_32 (abfd, ex->flags1);
   2894   in->flags2 = H_GET_32 (abfd, ex->flags2);
   2895 }
   2896 
   2897 /* Swap out an abiflags structure.  */
   2898 
   2899 void
   2900 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
   2901 				   const Elf_Internal_ABIFlags_v0 *in,
   2902 				   Elf_External_ABIFlags_v0 *ex)
   2903 {
   2904   H_PUT_16 (abfd, in->version, ex->version);
   2905   H_PUT_8 (abfd, in->isa_level, ex->isa_level);
   2906   H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
   2907   H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
   2908   H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
   2909   H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
   2910   H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
   2911   H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
   2912   H_PUT_32 (abfd, in->ases, ex->ases);
   2913   H_PUT_32 (abfd, in->flags1, ex->flags1);
   2914   H_PUT_32 (abfd, in->flags2, ex->flags2);
   2915 }
   2916 
   2917 /* This function is called via qsort() to sort the dynamic relocation
   2919    entries by increasing r_symndx value.  */
   2920 
   2921 static int
   2922 sort_dynamic_relocs (const void *arg1, const void *arg2)
   2923 {
   2924   Elf_Internal_Rela int_reloc1;
   2925   Elf_Internal_Rela int_reloc2;
   2926   int diff;
   2927 
   2928   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
   2929   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
   2930 
   2931   diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
   2932   if (diff != 0)
   2933     return diff;
   2934 
   2935   if (int_reloc1.r_offset < int_reloc2.r_offset)
   2936     return -1;
   2937   if (int_reloc1.r_offset > int_reloc2.r_offset)
   2938     return 1;
   2939   return 0;
   2940 }
   2941 
   2942 /* Like sort_dynamic_relocs, but used for elf64 relocations.  */
   2943 
   2944 static int
   2945 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
   2946 			const void *arg2 ATTRIBUTE_UNUSED)
   2947 {
   2948 #ifdef BFD64
   2949   Elf_Internal_Rela int_reloc1[3];
   2950   Elf_Internal_Rela int_reloc2[3];
   2951 
   2952   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
   2953     (reldyn_sorting_bfd, arg1, int_reloc1);
   2954   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
   2955     (reldyn_sorting_bfd, arg2, int_reloc2);
   2956 
   2957   if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
   2958     return -1;
   2959   if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
   2960     return 1;
   2961 
   2962   if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
   2963     return -1;
   2964   if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
   2965     return 1;
   2966   return 0;
   2967 #else
   2968   abort ();
   2969 #endif
   2970 }
   2971 
   2972 
   2973 /* This routine is used to write out ECOFF debugging external symbol
   2974    information.  It is called via mips_elf_link_hash_traverse.  The
   2975    ECOFF external symbol information must match the ELF external
   2976    symbol information.  Unfortunately, at this point we don't know
   2977    whether a symbol is required by reloc information, so the two
   2978    tables may wind up being different.  We must sort out the external
   2979    symbol information before we can set the final size of the .mdebug
   2980    section, and we must set the size of the .mdebug section before we
   2981    can relocate any sections, and we can't know which symbols are
   2982    required by relocation until we relocate the sections.
   2983    Fortunately, it is relatively unlikely that any symbol will be
   2984    stripped but required by a reloc.  In particular, it can not happen
   2985    when generating a final executable.  */
   2986 
   2987 static bool
   2988 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
   2989 {
   2990   struct extsym_info *einfo = data;
   2991   bool strip;
   2992   asection *sec, *output_section;
   2993 
   2994   if (h->root.indx == -2)
   2995     strip = false;
   2996   else if ((h->root.def_dynamic
   2997 	    || h->root.ref_dynamic
   2998 	    || h->root.type == bfd_link_hash_new)
   2999 	   && !h->root.def_regular
   3000 	   && !h->root.ref_regular)
   3001     strip = true;
   3002   else if (einfo->info->strip == strip_all
   3003 	   || (einfo->info->strip == strip_some
   3004 	       && bfd_hash_lookup (einfo->info->keep_hash,
   3005 				   h->root.root.root.string,
   3006 				   false, false) == NULL))
   3007     strip = true;
   3008   else
   3009     strip = false;
   3010 
   3011   if (strip)
   3012     return true;
   3013 
   3014   if (h->esym.ifd == -2)
   3015     {
   3016       h->esym.jmptbl = 0;
   3017       h->esym.cobol_main = 0;
   3018       h->esym.weakext = 0;
   3019       h->esym.reserved = 0;
   3020       h->esym.ifd = ifdNil;
   3021       h->esym.asym.value = 0;
   3022       h->esym.asym.st = stGlobal;
   3023 
   3024       if (h->root.root.type == bfd_link_hash_undefined
   3025 	  || h->root.root.type == bfd_link_hash_undefweak)
   3026 	{
   3027 	  const char *name;
   3028 
   3029 	  /* Use undefined class.  Also, set class and type for some
   3030 	     special symbols.  */
   3031 	  name = h->root.root.root.string;
   3032 	  if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
   3033 	      || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
   3034 	    {
   3035 	      h->esym.asym.sc = scData;
   3036 	      h->esym.asym.st = stLabel;
   3037 	      h->esym.asym.value = 0;
   3038 	    }
   3039 	  else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
   3040 	    {
   3041 	      h->esym.asym.sc = scAbs;
   3042 	      h->esym.asym.st = stLabel;
   3043 	      h->esym.asym.value =
   3044 		mips_elf_hash_table (einfo->info)->procedure_count;
   3045 	    }
   3046 	  else
   3047 	    h->esym.asym.sc = scUndefined;
   3048 	}
   3049       else if (h->root.root.type != bfd_link_hash_defined
   3050 	  && h->root.root.type != bfd_link_hash_defweak)
   3051 	h->esym.asym.sc = scAbs;
   3052       else
   3053 	{
   3054 	  const char *name;
   3055 
   3056 	  sec = h->root.root.u.def.section;
   3057 	  output_section = sec->output_section;
   3058 
   3059 	  /* When making a shared library and symbol h is the one from
   3060 	     the another shared library, OUTPUT_SECTION may be null.  */
   3061 	  if (output_section == NULL)
   3062 	    h->esym.asym.sc = scUndefined;
   3063 	  else
   3064 	    {
   3065 	      name = bfd_section_name (output_section);
   3066 
   3067 	      if (strcmp (name, ".text") == 0)
   3068 		h->esym.asym.sc = scText;
   3069 	      else if (strcmp (name, ".data") == 0)
   3070 		h->esym.asym.sc = scData;
   3071 	      else if (strcmp (name, ".sdata") == 0)
   3072 		h->esym.asym.sc = scSData;
   3073 	      else if (strcmp (name, ".rodata") == 0
   3074 		       || strcmp (name, ".rdata") == 0)
   3075 		h->esym.asym.sc = scRData;
   3076 	      else if (strcmp (name, ".bss") == 0)
   3077 		h->esym.asym.sc = scBss;
   3078 	      else if (strcmp (name, ".sbss") == 0)
   3079 		h->esym.asym.sc = scSBss;
   3080 	      else if (strcmp (name, ".init") == 0)
   3081 		h->esym.asym.sc = scInit;
   3082 	      else if (strcmp (name, ".fini") == 0)
   3083 		h->esym.asym.sc = scFini;
   3084 	      else
   3085 		h->esym.asym.sc = scAbs;
   3086 	    }
   3087 	}
   3088 
   3089       h->esym.asym.reserved = 0;
   3090       h->esym.asym.index = indexNil;
   3091     }
   3092 
   3093   if (h->root.root.type == bfd_link_hash_common)
   3094     h->esym.asym.value = h->root.root.u.c.size;
   3095   else if (h->root.root.type == bfd_link_hash_defined
   3096 	   || h->root.root.type == bfd_link_hash_defweak)
   3097     {
   3098       if (h->esym.asym.sc == scCommon)
   3099 	h->esym.asym.sc = scBss;
   3100       else if (h->esym.asym.sc == scSCommon)
   3101 	h->esym.asym.sc = scSBss;
   3102 
   3103       sec = h->root.root.u.def.section;
   3104       output_section = sec->output_section;
   3105       if (output_section != NULL)
   3106 	h->esym.asym.value = (h->root.root.u.def.value
   3107 			      + sec->output_offset
   3108 			      + output_section->vma);
   3109       else
   3110 	h->esym.asym.value = 0;
   3111     }
   3112   else
   3113     {
   3114       struct mips_elf_link_hash_entry *hd = h;
   3115 
   3116       while (hd->root.root.type == bfd_link_hash_indirect)
   3117 	hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
   3118 
   3119       if (hd->needs_lazy_stub)
   3120 	{
   3121 	  BFD_ASSERT (hd->root.plt.plist != NULL);
   3122 	  BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
   3123 	  /* Set type and value for a symbol with a function stub.  */
   3124 	  h->esym.asym.st = stProc;
   3125 	  sec = hd->root.root.u.def.section;
   3126 	  if (sec == NULL)
   3127 	    h->esym.asym.value = 0;
   3128 	  else
   3129 	    {
   3130 	      output_section = sec->output_section;
   3131 	      if (output_section != NULL)
   3132 		h->esym.asym.value = (hd->root.plt.plist->stub_offset
   3133 				      + sec->output_offset
   3134 				      + output_section->vma);
   3135 	      else
   3136 		h->esym.asym.value = 0;
   3137 	    }
   3138 	}
   3139     }
   3140 
   3141   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
   3142 				      h->root.root.root.string,
   3143 				      &h->esym))
   3144     {
   3145       einfo->failed = true;
   3146       return false;
   3147     }
   3148 
   3149   return true;
   3150 }
   3151 
   3152 /* A comparison routine used to sort .gptab entries.  */
   3153 
   3154 static int
   3155 gptab_compare (const void *p1, const void *p2)
   3156 {
   3157   const Elf32_gptab *a1 = p1;
   3158   const Elf32_gptab *a2 = p2;
   3159 
   3160   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
   3161 }
   3162 
   3163 /* Functions to manage the got entry hash table.  */
   3165 
   3166 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
   3167    hash number.  */
   3168 
   3169 static inline hashval_t
   3170 mips_elf_hash_bfd_vma (bfd_vma addr)
   3171 {
   3172 #ifdef BFD64
   3173   return addr + (addr >> 32);
   3174 #else
   3175   return addr;
   3176 #endif
   3177 }
   3178 
   3179 static hashval_t
   3180 mips_elf_got_entry_hash (const void *entry_)
   3181 {
   3182   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
   3183 
   3184   return (entry->symndx
   3185 	  + ((entry->tls_type == GOT_TLS_LDM) << 18)
   3186 	  + (entry->tls_type == GOT_TLS_LDM ? 0
   3187 	     : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
   3188 	     : entry->symndx >= 0 ? (entry->abfd->id
   3189 				     + mips_elf_hash_bfd_vma (entry->d.addend))
   3190 	     : entry->d.h->root.root.root.hash));
   3191 }
   3192 
   3193 static int
   3194 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
   3195 {
   3196   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
   3197   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
   3198 
   3199   return (e1->symndx == e2->symndx
   3200 	  && e1->tls_type == e2->tls_type
   3201 	  && (e1->tls_type == GOT_TLS_LDM ? true
   3202 	      : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
   3203 	      : e1->symndx >= 0 ? (e1->abfd == e2->abfd
   3204 				   && e1->d.addend == e2->d.addend)
   3205 	      : e2->abfd && e1->d.h == e2->d.h));
   3206 }
   3207 
   3208 static hashval_t
   3209 mips_got_page_ref_hash (const void *ref_)
   3210 {
   3211   const struct mips_got_page_ref *ref;
   3212 
   3213   ref = (const struct mips_got_page_ref *) ref_;
   3214   return ((ref->symndx >= 0
   3215 	   ? (hashval_t) (ref->u.abfd->id + ref->symndx)
   3216 	   : ref->u.h->root.root.root.hash)
   3217 	  + mips_elf_hash_bfd_vma (ref->addend));
   3218 }
   3219 
   3220 static int
   3221 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
   3222 {
   3223   const struct mips_got_page_ref *ref1, *ref2;
   3224 
   3225   ref1 = (const struct mips_got_page_ref *) ref1_;
   3226   ref2 = (const struct mips_got_page_ref *) ref2_;
   3227   return (ref1->symndx == ref2->symndx
   3228 	  && (ref1->symndx < 0
   3229 	      ? ref1->u.h == ref2->u.h
   3230 	      : ref1->u.abfd == ref2->u.abfd)
   3231 	  && ref1->addend == ref2->addend);
   3232 }
   3233 
   3234 static hashval_t
   3235 mips_got_page_entry_hash (const void *entry_)
   3236 {
   3237   const struct mips_got_page_entry *entry;
   3238 
   3239   entry = (const struct mips_got_page_entry *) entry_;
   3240   return entry->sec->id;
   3241 }
   3242 
   3243 static int
   3244 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
   3245 {
   3246   const struct mips_got_page_entry *entry1, *entry2;
   3247 
   3248   entry1 = (const struct mips_got_page_entry *) entry1_;
   3249   entry2 = (const struct mips_got_page_entry *) entry2_;
   3250   return entry1->sec == entry2->sec;
   3251 }
   3252 
   3253 /* Create and return a new mips_got_info structure.  */
   3255 
   3256 static struct mips_got_info *
   3257 mips_elf_create_got_info (bfd *abfd)
   3258 {
   3259   struct mips_got_info *g;
   3260 
   3261   g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
   3262   if (g == NULL)
   3263     return NULL;
   3264 
   3265   g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
   3266 				    mips_elf_got_entry_eq, NULL);
   3267   if (g->got_entries == NULL)
   3268     return NULL;
   3269 
   3270   g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
   3271 				      mips_got_page_ref_eq, NULL);
   3272   if (g->got_page_refs == NULL)
   3273     return NULL;
   3274 
   3275   return g;
   3276 }
   3277 
   3278 /* Return the GOT info for input bfd ABFD, trying to create a new one if
   3279    CREATE_P and if ABFD doesn't already have a GOT.  */
   3280 
   3281 static struct mips_got_info *
   3282 mips_elf_bfd_got (bfd *abfd, bool create_p)
   3283 {
   3284   struct mips_elf_obj_tdata *tdata;
   3285 
   3286   if (!is_mips_elf (abfd))
   3287     return NULL;
   3288 
   3289   tdata = mips_elf_tdata (abfd);
   3290   if (!tdata->got && create_p)
   3291     tdata->got = mips_elf_create_got_info (abfd);
   3292   return tdata->got;
   3293 }
   3294 
   3295 /* Record that ABFD should use output GOT G.  */
   3296 
   3297 static void
   3298 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
   3299 {
   3300   struct mips_elf_obj_tdata *tdata;
   3301 
   3302   BFD_ASSERT (is_mips_elf (abfd));
   3303   tdata = mips_elf_tdata (abfd);
   3304   if (tdata->got)
   3305     {
   3306       /* The GOT structure itself and the hash table entries are
   3307 	 allocated to a bfd, but the hash tables aren't.  */
   3308       htab_delete (tdata->got->got_entries);
   3309       htab_delete (tdata->got->got_page_refs);
   3310       if (tdata->got->got_page_entries)
   3311 	htab_delete (tdata->got->got_page_entries);
   3312     }
   3313   tdata->got = g;
   3314 }
   3315 
   3316 /* Return the dynamic relocation section.  If it doesn't exist, try to
   3317    create a new it if CREATE_P, otherwise return NULL.  Also return NULL
   3318    if creation fails.  */
   3319 
   3320 static asection *
   3321 mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
   3322 {
   3323   const char *dname;
   3324   asection *sreloc;
   3325   bfd *dynobj;
   3326 
   3327   dname = MIPS_ELF_REL_DYN_NAME (info);
   3328   dynobj = elf_hash_table (info)->dynobj;
   3329   sreloc = bfd_get_linker_section (dynobj, dname);
   3330   if (sreloc == NULL && create_p)
   3331     {
   3332       sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
   3333 						   (SEC_ALLOC
   3334 						    | SEC_LOAD
   3335 						    | SEC_HAS_CONTENTS
   3336 						    | SEC_IN_MEMORY
   3337 						    | SEC_LINKER_CREATED
   3338 						    | SEC_READONLY));
   3339       if (sreloc == NULL
   3340 	  || !bfd_set_section_alignment (sreloc,
   3341 					 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
   3342 	return NULL;
   3343     }
   3344   return sreloc;
   3345 }
   3346 
   3347 /* Return the GOT_TLS_* type required by relocation type R_TYPE.  */
   3348 
   3349 static int
   3350 mips_elf_reloc_tls_type (unsigned int r_type)
   3351 {
   3352   if (tls_gd_reloc_p (r_type))
   3353     return GOT_TLS_GD;
   3354 
   3355   if (tls_ldm_reloc_p (r_type))
   3356     return GOT_TLS_LDM;
   3357 
   3358   if (tls_gottprel_reloc_p (r_type))
   3359     return GOT_TLS_IE;
   3360 
   3361   return GOT_TLS_NONE;
   3362 }
   3363 
   3364 /* Return the number of GOT slots needed for GOT TLS type TYPE.  */
   3365 
   3366 static int
   3367 mips_tls_got_entries (unsigned int type)
   3368 {
   3369   switch (type)
   3370     {
   3371     case GOT_TLS_GD:
   3372     case GOT_TLS_LDM:
   3373       return 2;
   3374 
   3375     case GOT_TLS_IE:
   3376       return 1;
   3377 
   3378     case GOT_TLS_NONE:
   3379       return 0;
   3380     }
   3381   abort ();
   3382 }
   3383 
   3384 /* Count the number of relocations needed for a TLS GOT entry, with
   3385    access types from TLS_TYPE, and symbol H (or a local symbol if H
   3386    is NULL).  */
   3387 
   3388 static int
   3389 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
   3390 		     struct elf_link_hash_entry *h)
   3391 {
   3392   int indx = 0;
   3393   bool need_relocs = false;
   3394   bool dyn = elf_hash_table (info)->dynamic_sections_created;
   3395 
   3396   if (h != NULL
   3397       && h->dynindx != -1
   3398       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
   3399       && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
   3400     indx = h->dynindx;
   3401 
   3402   if ((bfd_link_dll (info) || indx != 0)
   3403       && (h == NULL
   3404 	  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3405 	  || h->root.type != bfd_link_hash_undefweak))
   3406     need_relocs = true;
   3407 
   3408   if (!need_relocs)
   3409     return 0;
   3410 
   3411   switch (tls_type)
   3412     {
   3413     case GOT_TLS_GD:
   3414       return indx != 0 ? 2 : 1;
   3415 
   3416     case GOT_TLS_IE:
   3417       return 1;
   3418 
   3419     case GOT_TLS_LDM:
   3420       return bfd_link_dll (info) ? 1 : 0;
   3421 
   3422     default:
   3423       return 0;
   3424     }
   3425 }
   3426 
   3427 /* Add the number of GOT entries and TLS relocations required by ENTRY
   3428    to G.  */
   3429 
   3430 static void
   3431 mips_elf_count_got_entry (struct bfd_link_info *info,
   3432 			  struct mips_got_info *g,
   3433 			  struct mips_got_entry *entry)
   3434 {
   3435   if (entry->tls_type)
   3436     {
   3437       g->tls_gotno += mips_tls_got_entries (entry->tls_type);
   3438       g->relocs += mips_tls_got_relocs (info, entry->tls_type,
   3439 					entry->symndx < 0
   3440 					? &entry->d.h->root : NULL);
   3441     }
   3442   else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
   3443     g->local_gotno += 1;
   3444   else
   3445     g->global_gotno += 1;
   3446 }
   3447 
   3448 /* Output a simple dynamic relocation into SRELOC.  */
   3449 
   3450 static void
   3451 mips_elf_output_dynamic_relocation (bfd *output_bfd,
   3452 				    asection *sreloc,
   3453 				    unsigned long reloc_index,
   3454 				    unsigned long indx,
   3455 				    int r_type,
   3456 				    bfd_vma offset)
   3457 {
   3458   Elf_Internal_Rela rel[3];
   3459 
   3460   memset (rel, 0, sizeof (rel));
   3461 
   3462   rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
   3463   rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
   3464 
   3465   if (ABI_64_P (output_bfd))
   3466     {
   3467       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
   3468 	(output_bfd, &rel[0],
   3469 	 (sreloc->contents
   3470 	  + reloc_index * sizeof (Elf64_Mips_External_Rel)));
   3471     }
   3472   else
   3473     bfd_elf32_swap_reloc_out
   3474       (output_bfd, &rel[0],
   3475        (sreloc->contents
   3476 	+ reloc_index * sizeof (Elf32_External_Rel)));
   3477 }
   3478 
   3479 /* Initialize a set of TLS GOT entries for one symbol.  */
   3480 
   3481 static void
   3482 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
   3483 			       struct mips_got_entry *entry,
   3484 			       struct mips_elf_link_hash_entry *h,
   3485 			       bfd_vma value)
   3486 {
   3487   bool dyn = elf_hash_table (info)->dynamic_sections_created;
   3488   struct mips_elf_link_hash_table *htab;
   3489   int indx;
   3490   asection *sreloc, *sgot;
   3491   bfd_vma got_offset, got_offset2;
   3492   bool need_relocs = false;
   3493 
   3494   htab = mips_elf_hash_table (info);
   3495   if (htab == NULL)
   3496     return;
   3497 
   3498   sgot = htab->root.sgot;
   3499 
   3500   indx = 0;
   3501   if (h != NULL
   3502       && h->root.dynindx != -1
   3503       && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
   3504       && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
   3505     indx = h->root.dynindx;
   3506 
   3507   if (entry->tls_initialized)
   3508     return;
   3509 
   3510   if ((bfd_link_dll (info) || indx != 0)
   3511       && (h == NULL
   3512 	  || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
   3513 	  || h->root.type != bfd_link_hash_undefweak))
   3514     need_relocs = true;
   3515 
   3516   /* MINUS_ONE means the symbol is not defined in this object.  It may not
   3517      be defined at all; assume that the value doesn't matter in that
   3518      case.  Otherwise complain if we would use the value.  */
   3519   BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
   3520 	      || h->root.root.type == bfd_link_hash_undefweak);
   3521 
   3522   /* Emit necessary relocations.  */
   3523   sreloc = mips_elf_rel_dyn_section (info, false);
   3524   got_offset = entry->gotidx;
   3525 
   3526   switch (entry->tls_type)
   3527     {
   3528     case GOT_TLS_GD:
   3529       /* General Dynamic.  */
   3530       got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
   3531 
   3532       if (need_relocs)
   3533 	{
   3534 	  mips_elf_output_dynamic_relocation
   3535 	    (abfd, sreloc, sreloc->reloc_count++, indx,
   3536 	     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
   3537 	     sgot->output_offset + sgot->output_section->vma + got_offset);
   3538 
   3539 	  if (indx)
   3540 	    mips_elf_output_dynamic_relocation
   3541 	      (abfd, sreloc, sreloc->reloc_count++, indx,
   3542 	       ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
   3543 	       sgot->output_offset + sgot->output_section->vma + got_offset2);
   3544 	  else
   3545 	    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
   3546 			       sgot->contents + got_offset2);
   3547 	}
   3548       else
   3549 	{
   3550 	  MIPS_ELF_PUT_WORD (abfd, 1,
   3551 			     sgot->contents + got_offset);
   3552 	  MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
   3553 			     sgot->contents + got_offset2);
   3554 	}
   3555       break;
   3556 
   3557     case GOT_TLS_IE:
   3558       /* Initial Exec model.  */
   3559       if (need_relocs)
   3560 	{
   3561 	  if (indx == 0)
   3562 	    MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
   3563 			       sgot->contents + got_offset);
   3564 	  else
   3565 	    MIPS_ELF_PUT_WORD (abfd, 0,
   3566 			       sgot->contents + got_offset);
   3567 
   3568 	  mips_elf_output_dynamic_relocation
   3569 	    (abfd, sreloc, sreloc->reloc_count++, indx,
   3570 	     ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
   3571 	     sgot->output_offset + sgot->output_section->vma + got_offset);
   3572 	}
   3573       else
   3574 	MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
   3575 			   sgot->contents + got_offset);
   3576       break;
   3577 
   3578     case GOT_TLS_LDM:
   3579       /* The initial offset is zero, and the LD offsets will include the
   3580 	 bias by DTP_OFFSET.  */
   3581       MIPS_ELF_PUT_WORD (abfd, 0,
   3582 			 sgot->contents + got_offset
   3583 			 + MIPS_ELF_GOT_SIZE (abfd));
   3584 
   3585       if (!bfd_link_dll (info))
   3586 	MIPS_ELF_PUT_WORD (abfd, 1,
   3587 			   sgot->contents + got_offset);
   3588       else
   3589 	mips_elf_output_dynamic_relocation
   3590 	  (abfd, sreloc, sreloc->reloc_count++, indx,
   3591 	   ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
   3592 	   sgot->output_offset + sgot->output_section->vma + got_offset);
   3593       break;
   3594 
   3595     default:
   3596       abort ();
   3597     }
   3598 
   3599   entry->tls_initialized = true;
   3600 }
   3601 
   3602 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
   3603    for global symbol H.  .got.plt comes before the GOT, so the offset
   3604    will be negative.  */
   3605 
   3606 static bfd_vma
   3607 mips_elf_gotplt_index (struct bfd_link_info *info,
   3608 		       struct elf_link_hash_entry *h)
   3609 {
   3610   bfd_vma got_address, got_value;
   3611   struct mips_elf_link_hash_table *htab;
   3612 
   3613   htab = mips_elf_hash_table (info);
   3614   BFD_ASSERT (htab != NULL);
   3615 
   3616   BFD_ASSERT (h->plt.plist != NULL);
   3617   BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
   3618 
   3619   /* Calculate the address of the associated .got.plt entry.  */
   3620   got_address = (htab->root.sgotplt->output_section->vma
   3621 		 + htab->root.sgotplt->output_offset
   3622 		 + (h->plt.plist->gotplt_index
   3623 		    * MIPS_ELF_GOT_SIZE (info->output_bfd)));
   3624 
   3625   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
   3626   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
   3627 	       + htab->root.hgot->root.u.def.section->output_offset
   3628 	       + htab->root.hgot->root.u.def.value);
   3629 
   3630   return got_address - got_value;
   3631 }
   3632 
   3633 /* Return the GOT offset for address VALUE.   If there is not yet a GOT
   3634    entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
   3635    create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
   3636    offset can be found.  */
   3637 
   3638 static bfd_vma
   3639 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3640 			  bfd_vma value, unsigned long r_symndx,
   3641 			  struct mips_elf_link_hash_entry *h, int r_type)
   3642 {
   3643   struct mips_elf_link_hash_table *htab;
   3644   struct mips_got_entry *entry;
   3645 
   3646   htab = mips_elf_hash_table (info);
   3647   BFD_ASSERT (htab != NULL);
   3648 
   3649   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
   3650 					   r_symndx, h, r_type);
   3651   if (!entry)
   3652     return MINUS_ONE;
   3653 
   3654   if (entry->tls_type)
   3655     mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
   3656   return entry->gotidx;
   3657 }
   3658 
   3659 /* Return the GOT index of global symbol H in the primary GOT.  */
   3660 
   3661 static bfd_vma
   3662 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
   3663 				   struct elf_link_hash_entry *h)
   3664 {
   3665   struct mips_elf_link_hash_table *htab;
   3666   long global_got_dynindx;
   3667   struct mips_got_info *g;
   3668   bfd_vma got_index;
   3669 
   3670   htab = mips_elf_hash_table (info);
   3671   BFD_ASSERT (htab != NULL);
   3672 
   3673   global_got_dynindx = 0;
   3674   if (htab->global_gotsym != NULL)
   3675     global_got_dynindx = htab->global_gotsym->dynindx;
   3676 
   3677   /* Once we determine the global GOT entry with the lowest dynamic
   3678      symbol table index, we must put all dynamic symbols with greater
   3679      indices into the primary GOT.  That makes it easy to calculate the
   3680      GOT offset.  */
   3681   BFD_ASSERT (h->dynindx >= global_got_dynindx);
   3682   g = mips_elf_bfd_got (obfd, false);
   3683   got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
   3684 	       * MIPS_ELF_GOT_SIZE (obfd));
   3685   BFD_ASSERT (got_index < htab->root.sgot->size);
   3686 
   3687   return got_index;
   3688 }
   3689 
   3690 /* Return the GOT index for the global symbol indicated by H, which is
   3691    referenced by a relocation of type R_TYPE in IBFD.  */
   3692 
   3693 static bfd_vma
   3694 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
   3695 			   struct elf_link_hash_entry *h, int r_type)
   3696 {
   3697   struct mips_elf_link_hash_table *htab;
   3698   struct mips_got_info *g;
   3699   struct mips_got_entry lookup, *entry;
   3700   bfd_vma gotidx;
   3701 
   3702   htab = mips_elf_hash_table (info);
   3703   BFD_ASSERT (htab != NULL);
   3704 
   3705   g = mips_elf_bfd_got (ibfd, false);
   3706   BFD_ASSERT (g);
   3707 
   3708   lookup.tls_type = mips_elf_reloc_tls_type (r_type);
   3709   if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
   3710     return mips_elf_primary_global_got_index (obfd, info, h);
   3711 
   3712   lookup.abfd = ibfd;
   3713   lookup.symndx = -1;
   3714   lookup.d.h = (struct mips_elf_link_hash_entry *) h;
   3715   entry = htab_find (g->got_entries, &lookup);
   3716   BFD_ASSERT (entry);
   3717 
   3718   gotidx = entry->gotidx;
   3719   BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
   3720 
   3721   if (lookup.tls_type)
   3722     {
   3723       bfd_vma value = MINUS_ONE;
   3724 
   3725       if ((h->root.type == bfd_link_hash_defined
   3726 	   || h->root.type == bfd_link_hash_defweak)
   3727 	  && h->root.u.def.section->output_section)
   3728 	value = (h->root.u.def.value
   3729 		 + h->root.u.def.section->output_offset
   3730 		 + h->root.u.def.section->output_section->vma);
   3731 
   3732       mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
   3733     }
   3734   return gotidx;
   3735 }
   3736 
   3737 /* Find a GOT page entry that points to within 32KB of VALUE.  These
   3738    entries are supposed to be placed at small offsets in the GOT, i.e.,
   3739    within 32KB of GP.  Return the index of the GOT entry, or -1 if no
   3740    entry could be created.  If OFFSETP is nonnull, use it to return the
   3741    offset of the GOT entry from VALUE.  */
   3742 
   3743 static bfd_vma
   3744 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3745 		   bfd_vma value, bfd_vma *offsetp)
   3746 {
   3747   bfd_vma page, got_index;
   3748   struct mips_got_entry *entry;
   3749 
   3750   page = (value + 0x8000) & ~(bfd_vma) 0xffff;
   3751   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
   3752 					   NULL, R_MIPS_GOT_PAGE);
   3753 
   3754   if (!entry)
   3755     return MINUS_ONE;
   3756 
   3757   got_index = entry->gotidx;
   3758 
   3759   if (offsetp)
   3760     *offsetp = value - entry->d.address;
   3761 
   3762   return got_index;
   3763 }
   3764 
   3765 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
   3766    EXTERNAL is true if the relocation was originally against a global
   3767    symbol that binds locally.  */
   3768 
   3769 static bfd_vma
   3770 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3771 		      bfd_vma value, bool external)
   3772 {
   3773   struct mips_got_entry *entry;
   3774 
   3775   /* GOT16 relocations against local symbols are followed by a LO16
   3776      relocation; those against global symbols are not.  Thus if the
   3777      symbol was originally local, the GOT16 relocation should load the
   3778      equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
   3779   if (! external)
   3780     value = mips_elf_high (value) << 16;
   3781 
   3782   /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
   3783      R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
   3784      same in all cases.  */
   3785   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
   3786 					   NULL, R_MIPS_GOT16);
   3787   if (entry)
   3788     return entry->gotidx;
   3789   else
   3790     return MINUS_ONE;
   3791 }
   3792 
   3793 /* Returns the offset for the entry at the INDEXth position
   3794    in the GOT.  */
   3795 
   3796 static bfd_vma
   3797 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
   3798 				bfd *input_bfd, bfd_vma got_index)
   3799 {
   3800   struct mips_elf_link_hash_table *htab;
   3801   asection *sgot;
   3802   bfd_vma gp;
   3803 
   3804   htab = mips_elf_hash_table (info);
   3805   BFD_ASSERT (htab != NULL);
   3806 
   3807   sgot = htab->root.sgot;
   3808   gp = _bfd_get_gp_value (output_bfd)
   3809     + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
   3810 
   3811   return sgot->output_section->vma + sgot->output_offset + got_index - gp;
   3812 }
   3813 
   3814 /* Create and return a local GOT entry for VALUE, which was calculated
   3815    from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
   3816    be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
   3817    instead.  */
   3818 
   3819 static struct mips_got_entry *
   3820 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
   3821 				 bfd *ibfd, bfd_vma value,
   3822 				 unsigned long r_symndx,
   3823 				 struct mips_elf_link_hash_entry *h,
   3824 				 int r_type)
   3825 {
   3826   struct mips_got_entry lookup, *entry;
   3827   void **loc;
   3828   struct mips_got_info *g;
   3829   struct mips_elf_link_hash_table *htab;
   3830   bfd_vma gotidx;
   3831 
   3832   htab = mips_elf_hash_table (info);
   3833   BFD_ASSERT (htab != NULL);
   3834 
   3835   g = mips_elf_bfd_got (ibfd, false);
   3836   if (g == NULL)
   3837     {
   3838       g = mips_elf_bfd_got (abfd, false);
   3839       BFD_ASSERT (g != NULL);
   3840     }
   3841 
   3842   /* This function shouldn't be called for symbols that live in the global
   3843      area of the GOT.  */
   3844   BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
   3845 
   3846   lookup.tls_type = mips_elf_reloc_tls_type (r_type);
   3847   if (lookup.tls_type)
   3848     {
   3849       lookup.abfd = ibfd;
   3850       if (tls_ldm_reloc_p (r_type))
   3851 	{
   3852 	  lookup.symndx = 0;
   3853 	  lookup.d.addend = 0;
   3854 	}
   3855       else if (h == NULL)
   3856 	{
   3857 	  lookup.symndx = r_symndx;
   3858 	  lookup.d.addend = 0;
   3859 	}
   3860       else
   3861 	{
   3862 	  lookup.symndx = -1;
   3863 	  lookup.d.h = h;
   3864 	}
   3865 
   3866       entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
   3867       BFD_ASSERT (entry);
   3868 
   3869       gotidx = entry->gotidx;
   3870       BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
   3871 
   3872       return entry;
   3873     }
   3874 
   3875   lookup.abfd = NULL;
   3876   lookup.symndx = -1;
   3877   lookup.d.address = value;
   3878   loc = htab_find_slot (g->got_entries, &lookup, INSERT);
   3879   if (!loc)
   3880     return NULL;
   3881 
   3882   entry = (struct mips_got_entry *) *loc;
   3883   if (entry)
   3884     return entry;
   3885 
   3886   if (g->assigned_low_gotno > g->assigned_high_gotno)
   3887     {
   3888       /* We didn't allocate enough space in the GOT.  */
   3889       _bfd_error_handler
   3890 	(_("not enough GOT space for local GOT entries"));
   3891       bfd_set_error (bfd_error_bad_value);
   3892       return NULL;
   3893     }
   3894 
   3895   entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
   3896   if (!entry)
   3897     return NULL;
   3898 
   3899   if (got16_reloc_p (r_type)
   3900       || call16_reloc_p (r_type)
   3901       || got_page_reloc_p (r_type)
   3902       || got_disp_reloc_p (r_type))
   3903     lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
   3904   else
   3905     lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
   3906 
   3907   *entry = lookup;
   3908   *loc = entry;
   3909 
   3910   MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
   3911 
   3912   /* These GOT entries need a dynamic relocation on VxWorks.  */
   3913   if (htab->root.target_os == is_vxworks)
   3914     {
   3915       Elf_Internal_Rela outrel;
   3916       asection *s;
   3917       bfd_byte *rloc;
   3918       bfd_vma got_address;
   3919 
   3920       s = mips_elf_rel_dyn_section (info, false);
   3921       got_address = (htab->root.sgot->output_section->vma
   3922 		     + htab->root.sgot->output_offset
   3923 		     + entry->gotidx);
   3924 
   3925       rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
   3926       outrel.r_offset = got_address;
   3927       outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
   3928       outrel.r_addend = value;
   3929       bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
   3930     }
   3931 
   3932   return entry;
   3933 }
   3934 
   3935 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
   3936    The number might be exact or a worst-case estimate, depending on how
   3937    much information is available to elf_backend_omit_section_dynsym at
   3938    the current linking stage.  */
   3939 
   3940 static bfd_size_type
   3941 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
   3942 {
   3943   bfd_size_type count;
   3944 
   3945   count = 0;
   3946   if (bfd_link_pic (info))
   3947     {
   3948       asection *p;
   3949       const struct elf_backend_data *bed;
   3950 
   3951       bed = get_elf_backend_data (output_bfd);
   3952       for (p = output_bfd->sections; p ; p = p->next)
   3953 	if ((p->flags & SEC_EXCLUDE) == 0
   3954 	    && (p->flags & SEC_ALLOC) != 0
   3955 	    && elf_hash_table (info)->dynamic_relocs
   3956 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
   3957 	  ++count;
   3958     }
   3959   return count;
   3960 }
   3961 
   3962 /* Sort the dynamic symbol table so that symbols that need GOT entries
   3963    appear towards the end.  */
   3964 
   3965 static bool
   3966 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
   3967 {
   3968   struct mips_elf_link_hash_table *htab;
   3969   struct mips_elf_hash_sort_data hsd;
   3970   struct mips_got_info *g;
   3971 
   3972   htab = mips_elf_hash_table (info);
   3973   BFD_ASSERT (htab != NULL);
   3974 
   3975   if (htab->root.dynsymcount == 0)
   3976     return true;
   3977 
   3978   g = htab->got_info;
   3979   if (g == NULL)
   3980     return true;
   3981 
   3982   hsd.low = NULL;
   3983   hsd.max_unref_got_dynindx
   3984     = hsd.min_got_dynindx
   3985     = (htab->root.dynsymcount - g->reloc_only_gotno);
   3986   /* Add 1 to local symbol indices to account for the mandatory NULL entry
   3987      at the head of the table; see `_bfd_elf_link_renumber_dynsyms'.  */
   3988   hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
   3989   hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
   3990   hsd.output_bfd = abfd;
   3991   if (htab->root.dynobj != NULL
   3992       && htab->root.dynamic_sections_created
   3993       && info->emit_gnu_hash)
   3994     {
   3995       asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
   3996       BFD_ASSERT (s != NULL);
   3997       hsd.mipsxhash = s->contents;
   3998       BFD_ASSERT (hsd.mipsxhash != NULL);
   3999     }
   4000   else
   4001     hsd.mipsxhash = NULL;
   4002   mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
   4003 
   4004   /* There should have been enough room in the symbol table to
   4005      accommodate both the GOT and non-GOT symbols.  */
   4006   BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
   4007   BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
   4008   BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
   4009   BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
   4010 
   4011   /* Now we know which dynamic symbol has the lowest dynamic symbol
   4012      table index in the GOT.  */
   4013   htab->global_gotsym = hsd.low;
   4014 
   4015   return true;
   4016 }
   4017 
   4018 /* If H needs a GOT entry, assign it the highest available dynamic
   4019    index.  Otherwise, assign it the lowest available dynamic
   4020    index.  */
   4021 
   4022 static bool
   4023 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
   4024 {
   4025   struct mips_elf_hash_sort_data *hsd = data;
   4026 
   4027   /* Symbols without dynamic symbol table entries aren't interesting
   4028      at all.  */
   4029   if (h->root.dynindx == -1)
   4030     return true;
   4031 
   4032   switch (h->global_got_area)
   4033     {
   4034     case GGA_NONE:
   4035       if (h->root.forced_local)
   4036 	h->root.dynindx = hsd->max_local_dynindx++;
   4037       else
   4038 	h->root.dynindx = hsd->max_non_got_dynindx++;
   4039       break;
   4040 
   4041     case GGA_NORMAL:
   4042       h->root.dynindx = --hsd->min_got_dynindx;
   4043       hsd->low = (struct elf_link_hash_entry *) h;
   4044       break;
   4045 
   4046     case GGA_RELOC_ONLY:
   4047       if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
   4048 	hsd->low = (struct elf_link_hash_entry *) h;
   4049       h->root.dynindx = hsd->max_unref_got_dynindx++;
   4050       break;
   4051     }
   4052 
   4053   /* Populate the .MIPS.xhash translation table entry with
   4054      the symbol dynindx.  */
   4055   if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
   4056     bfd_put_32 (hsd->output_bfd, h->root.dynindx,
   4057 		hsd->mipsxhash + h->mipsxhash_loc);
   4058 
   4059   return true;
   4060 }
   4061 
   4062 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
   4063    (which is owned by the caller and shouldn't be added to the
   4064    hash table directly).  */
   4065 
   4066 static bool
   4067 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
   4068 			   struct mips_got_entry *lookup)
   4069 {
   4070   struct mips_elf_link_hash_table *htab;
   4071   struct mips_got_entry *entry;
   4072   struct mips_got_info *g;
   4073   void **loc, **bfd_loc;
   4074 
   4075   /* Make sure there's a slot for this entry in the master GOT.  */
   4076   htab = mips_elf_hash_table (info);
   4077   g = htab->got_info;
   4078   loc = htab_find_slot (g->got_entries, lookup, INSERT);
   4079   if (!loc)
   4080     return false;
   4081 
   4082   /* Populate the entry if it isn't already.  */
   4083   entry = (struct mips_got_entry *) *loc;
   4084   if (!entry)
   4085     {
   4086       entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
   4087       if (!entry)
   4088 	return false;
   4089 
   4090       lookup->tls_initialized = false;
   4091       lookup->gotidx = -1;
   4092       *entry = *lookup;
   4093       *loc = entry;
   4094     }
   4095 
   4096   /* Reuse the same GOT entry for the BFD's GOT.  */
   4097   g = mips_elf_bfd_got (abfd, true);
   4098   if (!g)
   4099     return false;
   4100 
   4101   bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
   4102   if (!bfd_loc)
   4103     return false;
   4104 
   4105   if (!*bfd_loc)
   4106     *bfd_loc = entry;
   4107   return true;
   4108 }
   4109 
   4110 /* ABFD has a GOT relocation of type R_TYPE against H.  Reserve a GOT
   4111    entry for it.  FOR_CALL is true if the caller is only interested in
   4112    using the GOT entry for calls.  */
   4113 
   4114 static bool
   4115 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
   4116 				   bfd *abfd, struct bfd_link_info *info,
   4117 				   bool for_call, int r_type)
   4118 {
   4119   struct mips_elf_link_hash_table *htab;
   4120   struct mips_elf_link_hash_entry *hmips;
   4121   struct mips_got_entry entry;
   4122   unsigned char tls_type;
   4123 
   4124   htab = mips_elf_hash_table (info);
   4125   BFD_ASSERT (htab != NULL);
   4126 
   4127   hmips = (struct mips_elf_link_hash_entry *) h;
   4128   if (!for_call)
   4129     hmips->got_only_for_calls = false;
   4130 
   4131   /* A global symbol in the GOT must also be in the dynamic symbol
   4132      table.  */
   4133   if (h->dynindx == -1)
   4134     {
   4135       switch (ELF_ST_VISIBILITY (h->other))
   4136 	{
   4137 	case STV_INTERNAL:
   4138 	case STV_HIDDEN:
   4139 	  _bfd_mips_elf_hide_symbol (info, h, true);
   4140 	  break;
   4141 	}
   4142       if (!bfd_elf_link_record_dynamic_symbol (info, h))
   4143 	return false;
   4144     }
   4145 
   4146   tls_type = mips_elf_reloc_tls_type (r_type);
   4147   if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
   4148     hmips->global_got_area = GGA_NORMAL;
   4149 
   4150   entry.abfd = abfd;
   4151   entry.symndx = -1;
   4152   entry.d.h = (struct mips_elf_link_hash_entry *) h;
   4153   entry.tls_type = tls_type;
   4154   return mips_elf_record_got_entry (info, abfd, &entry);
   4155 }
   4156 
   4157 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
   4158    where SYMNDX is a local symbol.  Reserve a GOT entry for it.  */
   4159 
   4160 static bool
   4161 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
   4162 				  struct bfd_link_info *info, int r_type)
   4163 {
   4164   struct mips_elf_link_hash_table *htab;
   4165   struct mips_got_info *g;
   4166   struct mips_got_entry entry;
   4167 
   4168   htab = mips_elf_hash_table (info);
   4169   BFD_ASSERT (htab != NULL);
   4170 
   4171   g = htab->got_info;
   4172   BFD_ASSERT (g != NULL);
   4173 
   4174   entry.abfd = abfd;
   4175   entry.symndx = symndx;
   4176   entry.d.addend = addend;
   4177   entry.tls_type = mips_elf_reloc_tls_type (r_type);
   4178   return mips_elf_record_got_entry (info, abfd, &entry);
   4179 }
   4180 
   4181 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
   4182    H is the symbol's hash table entry, or null if SYMNDX is local
   4183    to ABFD.  */
   4184 
   4185 static bool
   4186 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
   4187 			      long symndx, struct elf_link_hash_entry *h,
   4188 			      bfd_signed_vma addend)
   4189 {
   4190   struct mips_elf_link_hash_table *htab;
   4191   struct mips_got_info *g1, *g2;
   4192   struct mips_got_page_ref lookup, *entry;
   4193   void **loc, **bfd_loc;
   4194 
   4195   htab = mips_elf_hash_table (info);
   4196   BFD_ASSERT (htab != NULL);
   4197 
   4198   g1 = htab->got_info;
   4199   BFD_ASSERT (g1 != NULL);
   4200 
   4201   if (h)
   4202     {
   4203       lookup.symndx = -1;
   4204       lookup.u.h = (struct mips_elf_link_hash_entry *) h;
   4205     }
   4206   else
   4207     {
   4208       lookup.symndx = symndx;
   4209       lookup.u.abfd = abfd;
   4210     }
   4211   lookup.addend = addend;
   4212   loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
   4213   if (loc == NULL)
   4214     return false;
   4215 
   4216   entry = (struct mips_got_page_ref *) *loc;
   4217   if (!entry)
   4218     {
   4219       entry = bfd_alloc (abfd, sizeof (*entry));
   4220       if (!entry)
   4221 	return false;
   4222 
   4223       *entry = lookup;
   4224       *loc = entry;
   4225     }
   4226 
   4227   /* Add the same entry to the BFD's GOT.  */
   4228   g2 = mips_elf_bfd_got (abfd, true);
   4229   if (!g2)
   4230     return false;
   4231 
   4232   bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
   4233   if (!bfd_loc)
   4234     return false;
   4235 
   4236   if (!*bfd_loc)
   4237     *bfd_loc = entry;
   4238 
   4239   return true;
   4240 }
   4241 
   4242 /* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
   4243 
   4244 static void
   4245 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
   4246 				       unsigned int n)
   4247 {
   4248   asection *s;
   4249   struct mips_elf_link_hash_table *htab;
   4250 
   4251   htab = mips_elf_hash_table (info);
   4252   BFD_ASSERT (htab != NULL);
   4253 
   4254   s = mips_elf_rel_dyn_section (info, false);
   4255   BFD_ASSERT (s != NULL);
   4256 
   4257   if (htab->root.target_os == is_vxworks)
   4258     s->size += n * MIPS_ELF_RELA_SIZE (abfd);
   4259   else
   4260     {
   4261       if (s->size == 0)
   4262 	{
   4263 	  /* Make room for a null element.  */
   4264 	  s->size += MIPS_ELF_REL_SIZE (abfd);
   4265 	  ++s->reloc_count;
   4266 	}
   4267       s->size += n * MIPS_ELF_REL_SIZE (abfd);
   4268     }
   4269 }
   4270 
   4271 /* A htab_traverse callback for GOT entries, with DATA pointing to a
   4273    mips_elf_traverse_got_arg structure.  Count the number of GOT
   4274    entries and TLS relocs.  Set DATA->value to true if we need
   4275    to resolve indirect or warning symbols and then recreate the GOT.  */
   4276 
   4277 static int
   4278 mips_elf_check_recreate_got (void **entryp, void *data)
   4279 {
   4280   struct mips_got_entry *entry;
   4281   struct mips_elf_traverse_got_arg *arg;
   4282 
   4283   entry = (struct mips_got_entry *) *entryp;
   4284   arg = (struct mips_elf_traverse_got_arg *) data;
   4285   if (entry->abfd != NULL && entry->symndx == -1)
   4286     {
   4287       struct mips_elf_link_hash_entry *h;
   4288 
   4289       h = entry->d.h;
   4290       if (h->root.root.type == bfd_link_hash_indirect
   4291 	  || h->root.root.type == bfd_link_hash_warning)
   4292 	{
   4293 	  arg->value = true;
   4294 	  return 0;
   4295 	}
   4296     }
   4297   mips_elf_count_got_entry (arg->info, arg->g, entry);
   4298   return 1;
   4299 }
   4300 
   4301 /* A htab_traverse callback for GOT entries, with DATA pointing to a
   4302    mips_elf_traverse_got_arg structure.  Add all entries to DATA->g,
   4303    converting entries for indirect and warning symbols into entries
   4304    for the target symbol.  Set DATA->g to null on error.  */
   4305 
   4306 static int
   4307 mips_elf_recreate_got (void **entryp, void *data)
   4308 {
   4309   struct mips_got_entry new_entry, *entry;
   4310   struct mips_elf_traverse_got_arg *arg;
   4311   void **slot;
   4312 
   4313   entry = (struct mips_got_entry *) *entryp;
   4314   arg = (struct mips_elf_traverse_got_arg *) data;
   4315   if (entry->abfd != NULL
   4316       && entry->symndx == -1
   4317       && (entry->d.h->root.root.type == bfd_link_hash_indirect
   4318 	  || entry->d.h->root.root.type == bfd_link_hash_warning))
   4319     {
   4320       struct mips_elf_link_hash_entry *h;
   4321 
   4322       new_entry = *entry;
   4323       entry = &new_entry;
   4324       h = entry->d.h;
   4325       do
   4326 	{
   4327 	  BFD_ASSERT (h->global_got_area == GGA_NONE);
   4328 	  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   4329 	}
   4330       while (h->root.root.type == bfd_link_hash_indirect
   4331 	     || h->root.root.type == bfd_link_hash_warning);
   4332       entry->d.h = h;
   4333     }
   4334   slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
   4335   if (slot == NULL)
   4336     {
   4337       arg->g = NULL;
   4338       return 0;
   4339     }
   4340   if (*slot == NULL)
   4341     {
   4342       if (entry == &new_entry)
   4343 	{
   4344 	  entry = bfd_alloc (entry->abfd, sizeof (*entry));
   4345 	  if (!entry)
   4346 	    {
   4347 	      arg->g = NULL;
   4348 	      return 0;
   4349 	    }
   4350 	  *entry = new_entry;
   4351 	}
   4352       *slot = entry;
   4353       mips_elf_count_got_entry (arg->info, arg->g, entry);
   4354     }
   4355   return 1;
   4356 }
   4357 
   4358 /* Return the maximum number of GOT page entries required for RANGE.  */
   4359 
   4360 static bfd_vma
   4361 mips_elf_pages_for_range (const struct mips_got_page_range *range)
   4362 {
   4363   return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
   4364 }
   4365 
   4366 /* Record that G requires a page entry that can reach SEC + ADDEND.  */
   4367 
   4368 static bool
   4369 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
   4370 				asection *sec, bfd_signed_vma addend)
   4371 {
   4372   struct mips_got_info *g = arg->g;
   4373   struct mips_got_page_entry lookup, *entry;
   4374   struct mips_got_page_range **range_ptr, *range;
   4375   bfd_vma old_pages, new_pages;
   4376   void **loc;
   4377 
   4378   /* Find the mips_got_page_entry hash table entry for this section.  */
   4379   lookup.sec = sec;
   4380   loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
   4381   if (loc == NULL)
   4382     return false;
   4383 
   4384   /* Create a mips_got_page_entry if this is the first time we've
   4385      seen the section.  */
   4386   entry = (struct mips_got_page_entry *) *loc;
   4387   if (!entry)
   4388     {
   4389       entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
   4390       if (!entry)
   4391 	return false;
   4392 
   4393       entry->sec = sec;
   4394       *loc = entry;
   4395     }
   4396 
   4397   /* Skip over ranges whose maximum extent cannot share a page entry
   4398      with ADDEND.  */
   4399   range_ptr = &entry->ranges;
   4400   while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
   4401     range_ptr = &(*range_ptr)->next;
   4402 
   4403   /* If we scanned to the end of the list, or found a range whose
   4404      minimum extent cannot share a page entry with ADDEND, create
   4405      a new singleton range.  */
   4406   range = *range_ptr;
   4407   if (!range || addend < range->min_addend - 0xffff)
   4408     {
   4409       range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
   4410       if (!range)
   4411 	return false;
   4412 
   4413       range->next = *range_ptr;
   4414       range->min_addend = addend;
   4415       range->max_addend = addend;
   4416 
   4417       *range_ptr = range;
   4418       entry->num_pages++;
   4419       g->page_gotno++;
   4420       return true;
   4421     }
   4422 
   4423   /* Remember how many pages the old range contributed.  */
   4424   old_pages = mips_elf_pages_for_range (range);
   4425 
   4426   /* Update the ranges.  */
   4427   if (addend < range->min_addend)
   4428     range->min_addend = addend;
   4429   else if (addend > range->max_addend)
   4430     {
   4431       if (range->next && addend >= range->next->min_addend - 0xffff)
   4432 	{
   4433 	  old_pages += mips_elf_pages_for_range (range->next);
   4434 	  range->max_addend = range->next->max_addend;
   4435 	  range->next = range->next->next;
   4436 	}
   4437       else
   4438 	range->max_addend = addend;
   4439     }
   4440 
   4441   /* Record any change in the total estimate.  */
   4442   new_pages = mips_elf_pages_for_range (range);
   4443   if (old_pages != new_pages)
   4444     {
   4445       entry->num_pages += new_pages - old_pages;
   4446       g->page_gotno += new_pages - old_pages;
   4447     }
   4448 
   4449   return true;
   4450 }
   4451 
   4452 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
   4453    and for which DATA points to a mips_elf_traverse_got_arg.  Work out
   4454    whether the page reference described by *REFP needs a GOT page entry,
   4455    and record that entry in DATA->g if so.  Set DATA->g to null on failure.  */
   4456 
   4457 static int
   4458 mips_elf_resolve_got_page_ref (void **refp, void *data)
   4459 {
   4460   struct mips_got_page_ref *ref;
   4461   struct mips_elf_traverse_got_arg *arg;
   4462   struct mips_elf_link_hash_table *htab;
   4463   asection *sec;
   4464   bfd_vma addend;
   4465 
   4466   ref = (struct mips_got_page_ref *) *refp;
   4467   arg = (struct mips_elf_traverse_got_arg *) data;
   4468   htab = mips_elf_hash_table (arg->info);
   4469 
   4470   if (ref->symndx < 0)
   4471     {
   4472       struct mips_elf_link_hash_entry *h;
   4473 
   4474       /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries.  */
   4475       h = ref->u.h;
   4476       if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
   4477 	return 1;
   4478 
   4479       /* Ignore undefined symbols; we'll issue an error later if
   4480 	 appropriate.  */
   4481       if (!((h->root.root.type == bfd_link_hash_defined
   4482 	     || h->root.root.type == bfd_link_hash_defweak)
   4483 	    && h->root.root.u.def.section))
   4484 	return 1;
   4485 
   4486       sec = h->root.root.u.def.section;
   4487       addend = h->root.root.u.def.value + ref->addend;
   4488     }
   4489   else
   4490     {
   4491       Elf_Internal_Sym *isym;
   4492 
   4493       /* Read in the symbol.  */
   4494       isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
   4495 				    ref->symndx);
   4496       if (isym == NULL)
   4497 	{
   4498 	  arg->g = NULL;
   4499 	  return 0;
   4500 	}
   4501 
   4502       /* Get the associated input section.  */
   4503       sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
   4504       if (sec == NULL)
   4505 	{
   4506 	  arg->g = NULL;
   4507 	  return 0;
   4508 	}
   4509 
   4510       /* If this is a mergable section, work out the section and offset
   4511 	 of the merged data.  For section symbols, the addend specifies
   4512 	 of the offset _of_ the first byte in the data, otherwise it
   4513 	 specifies the offset _from_ the first byte.  */
   4514       if (sec->flags & SEC_MERGE)
   4515 	{
   4516 	  void *secinfo;
   4517 
   4518 	  secinfo = elf_section_data (sec)->sec_info;
   4519 	  if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   4520 	    addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
   4521 						 isym->st_value + ref->addend);
   4522 	  else
   4523 	    addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
   4524 						 isym->st_value) + ref->addend;
   4525 	}
   4526       else
   4527 	addend = isym->st_value + ref->addend;
   4528     }
   4529   if (!mips_elf_record_got_page_entry (arg, sec, addend))
   4530     {
   4531       arg->g = NULL;
   4532       return 0;
   4533     }
   4534   return 1;
   4535 }
   4536 
   4537 /* If any entries in G->got_entries are for indirect or warning symbols,
   4538    replace them with entries for the target symbol.  Convert g->got_page_refs
   4539    into got_page_entry structures and estimate the number of page entries
   4540    that they require.  */
   4541 
   4542 static bool
   4543 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
   4544 				    struct mips_got_info *g)
   4545 {
   4546   struct mips_elf_traverse_got_arg tga;
   4547   struct mips_got_info oldg;
   4548 
   4549   oldg = *g;
   4550 
   4551   tga.info = info;
   4552   tga.g = g;
   4553   tga.value = false;
   4554   htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
   4555   if (tga.value)
   4556     {
   4557       *g = oldg;
   4558       g->got_entries = htab_create (htab_size (oldg.got_entries),
   4559 				    mips_elf_got_entry_hash,
   4560 				    mips_elf_got_entry_eq, NULL);
   4561       if (!g->got_entries)
   4562 	return false;
   4563 
   4564       htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
   4565       if (!tga.g)
   4566 	return false;
   4567 
   4568       htab_delete (oldg.got_entries);
   4569     }
   4570 
   4571   g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
   4572 					 mips_got_page_entry_eq, NULL);
   4573   if (g->got_page_entries == NULL)
   4574     return false;
   4575 
   4576   tga.info = info;
   4577   tga.g = g;
   4578   htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
   4579 
   4580   return true;
   4581 }
   4582 
   4583 /* Return true if a GOT entry for H should live in the local rather than
   4584    global GOT area.  */
   4585 
   4586 static bool
   4587 mips_use_local_got_p (struct bfd_link_info *info,
   4588 		      struct mips_elf_link_hash_entry *h)
   4589 {
   4590   /* Symbols that aren't in the dynamic symbol table must live in the
   4591      local GOT.  This includes symbols that are completely undefined
   4592      and which therefore don't bind locally.  We'll report undefined
   4593      symbols later if appropriate.  */
   4594   if (h->root.dynindx == -1)
   4595     return true;
   4596 
   4597   /* Absolute symbols, if ever they need a GOT entry, cannot ever go
   4598      to the local GOT, as they would be implicitly relocated by the
   4599      base address by the dynamic loader.  */
   4600   if (bfd_is_abs_symbol (&h->root.root))
   4601     return false;
   4602 
   4603   /* Symbols that bind locally can (and in the case of forced-local
   4604      symbols, must) live in the local GOT.  */
   4605   if (h->got_only_for_calls
   4606       ? SYMBOL_CALLS_LOCAL (info, &h->root)
   4607       : SYMBOL_REFERENCES_LOCAL (info, &h->root))
   4608     return true;
   4609 
   4610   /* If this is an executable that must provide a definition of the symbol,
   4611      either though PLTs or copy relocations, then that address should go in
   4612      the local rather than global GOT.  */
   4613   if (bfd_link_executable (info) && h->has_static_relocs)
   4614     return true;
   4615 
   4616   return false;
   4617 }
   4618 
   4619 /* A mips_elf_link_hash_traverse callback for which DATA points to the
   4620    link_info structure.  Decide whether the hash entry needs an entry in
   4621    the global part of the primary GOT, setting global_got_area accordingly.
   4622    Count the number of global symbols that are in the primary GOT only
   4623    because they have relocations against them (reloc_only_gotno).  */
   4624 
   4625 static bool
   4626 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
   4627 {
   4628   struct bfd_link_info *info;
   4629   struct mips_elf_link_hash_table *htab;
   4630   struct mips_got_info *g;
   4631 
   4632   info = (struct bfd_link_info *) data;
   4633   htab = mips_elf_hash_table (info);
   4634   g = htab->got_info;
   4635   if (h->global_got_area != GGA_NONE)
   4636     {
   4637       /* Make a final decision about whether the symbol belongs in the
   4638 	 local or global GOT.  */
   4639       if (mips_use_local_got_p (info, h))
   4640 	/* The symbol belongs in the local GOT.  We no longer need this
   4641 	   entry if it was only used for relocations; those relocations
   4642 	   will be against the null or section symbol instead of H.  */
   4643 	h->global_got_area = GGA_NONE;
   4644       else if (htab->root.target_os == is_vxworks
   4645 	       && h->got_only_for_calls
   4646 	       && h->root.plt.plist->mips_offset != MINUS_ONE)
   4647 	/* On VxWorks, calls can refer directly to the .got.plt entry;
   4648 	   they don't need entries in the regular GOT.  .got.plt entries
   4649 	   will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
   4650 	h->global_got_area = GGA_NONE;
   4651       else if (h->global_got_area == GGA_RELOC_ONLY)
   4652 	{
   4653 	  g->reloc_only_gotno++;
   4654 	  g->global_gotno++;
   4655 	}
   4656     }
   4657   return 1;
   4658 }
   4659 
   4660 /* A htab_traverse callback for GOT entries.  Add each one to the GOT
   4662    given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
   4663 
   4664 static int
   4665 mips_elf_add_got_entry (void **entryp, void *data)
   4666 {
   4667   struct mips_got_entry *entry;
   4668   struct mips_elf_traverse_got_arg *arg;
   4669   void **slot;
   4670 
   4671   entry = (struct mips_got_entry *) *entryp;
   4672   arg = (struct mips_elf_traverse_got_arg *) data;
   4673   slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
   4674   if (!slot)
   4675     {
   4676       arg->g = NULL;
   4677       return 0;
   4678     }
   4679   if (!*slot)
   4680     {
   4681       *slot = entry;
   4682       mips_elf_count_got_entry (arg->info, arg->g, entry);
   4683     }
   4684   return 1;
   4685 }
   4686 
   4687 /* A htab_traverse callback for GOT page entries.  Add each one to the GOT
   4688    given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
   4689 
   4690 static int
   4691 mips_elf_add_got_page_entry (void **entryp, void *data)
   4692 {
   4693   struct mips_got_page_entry *entry;
   4694   struct mips_elf_traverse_got_arg *arg;
   4695   void **slot;
   4696 
   4697   entry = (struct mips_got_page_entry *) *entryp;
   4698   arg = (struct mips_elf_traverse_got_arg *) data;
   4699   slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
   4700   if (!slot)
   4701     {
   4702       arg->g = NULL;
   4703       return 0;
   4704     }
   4705   if (!*slot)
   4706     {
   4707       *slot = entry;
   4708       arg->g->page_gotno += entry->num_pages;
   4709     }
   4710   return 1;
   4711 }
   4712 
   4713 /* Consider merging FROM, which is ABFD's GOT, into TO.  Return -1 if
   4714    this would lead to overflow, 1 if they were merged successfully,
   4715    and 0 if a merge failed due to lack of memory.  (These values are chosen
   4716    so that nonnegative return values can be returned by a htab_traverse
   4717    callback.)  */
   4718 
   4719 static int
   4720 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
   4721 			 struct mips_got_info *to,
   4722 			 struct mips_elf_got_per_bfd_arg *arg)
   4723 {
   4724   struct mips_elf_traverse_got_arg tga;
   4725   unsigned int estimate;
   4726 
   4727   /* Work out how many page entries we would need for the combined GOT.  */
   4728   estimate = arg->max_pages;
   4729   if (estimate >= from->page_gotno + to->page_gotno)
   4730     estimate = from->page_gotno + to->page_gotno;
   4731 
   4732   /* And conservatively estimate how many local and TLS entries
   4733      would be needed.  */
   4734   estimate += from->local_gotno + to->local_gotno;
   4735   estimate += from->tls_gotno + to->tls_gotno;
   4736 
   4737   /* If we're merging with the primary got, any TLS relocations will
   4738      come after the full set of global entries.  Otherwise estimate those
   4739      conservatively as well.  */
   4740   if (to == arg->primary && from->tls_gotno + to->tls_gotno)
   4741     estimate += arg->global_count;
   4742   else
   4743     estimate += from->global_gotno + to->global_gotno;
   4744 
   4745   /* Bail out if the combined GOT might be too big.  */
   4746   if (estimate > arg->max_count)
   4747     return -1;
   4748 
   4749   /* Transfer the bfd's got information from FROM to TO.  */
   4750   tga.info = arg->info;
   4751   tga.g = to;
   4752   htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
   4753   if (!tga.g)
   4754     return 0;
   4755 
   4756   htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
   4757   if (!tga.g)
   4758     return 0;
   4759 
   4760   mips_elf_replace_bfd_got (abfd, to);
   4761   return 1;
   4762 }
   4763 
   4764 /* Attempt to merge GOT G, which belongs to ABFD.  Try to use as much
   4765    as possible of the primary got, since it doesn't require explicit
   4766    dynamic relocations, but don't use bfds that would reference global
   4767    symbols out of the addressable range.  Failing the primary got,
   4768    attempt to merge with the current got, or finish the current got
   4769    and then make make the new got current.  */
   4770 
   4771 static bool
   4772 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
   4773 		    struct mips_elf_got_per_bfd_arg *arg)
   4774 {
   4775   unsigned int estimate;
   4776   int result;
   4777 
   4778   if (!mips_elf_resolve_final_got_entries (arg->info, g))
   4779     return false;
   4780 
   4781   /* Work out the number of page, local and TLS entries.  */
   4782   estimate = arg->max_pages;
   4783   if (estimate > g->page_gotno)
   4784     estimate = g->page_gotno;
   4785   estimate += g->local_gotno + g->tls_gotno;
   4786 
   4787   /* We place TLS GOT entries after both locals and globals.  The globals
   4788      for the primary GOT may overflow the normal GOT size limit, so be
   4789      sure not to merge a GOT which requires TLS with the primary GOT in that
   4790      case.  This doesn't affect non-primary GOTs.  */
   4791   estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
   4792 
   4793   if (estimate <= arg->max_count)
   4794     {
   4795       /* If we don't have a primary GOT, use it as
   4796 	 a starting point for the primary GOT.  */
   4797       if (!arg->primary)
   4798 	{
   4799 	  arg->primary = g;
   4800 	  return true;
   4801 	}
   4802 
   4803       /* Try merging with the primary GOT.  */
   4804       result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
   4805       if (result >= 0)
   4806 	return result;
   4807     }
   4808 
   4809   /* If we can merge with the last-created got, do it.  */
   4810   if (arg->current)
   4811     {
   4812       result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
   4813       if (result >= 0)
   4814 	return result;
   4815     }
   4816 
   4817   /* Well, we couldn't merge, so create a new GOT.  Don't check if it
   4818      fits; if it turns out that it doesn't, we'll get relocation
   4819      overflows anyway.  */
   4820   g->next = arg->current;
   4821   arg->current = g;
   4822 
   4823   return true;
   4824 }
   4825 
   4826 /* ENTRYP is a hash table entry for a mips_got_entry.  Set its gotidx
   4827    to GOTIDX, duplicating the entry if it has already been assigned
   4828    an index in a different GOT.  */
   4829 
   4830 static bool
   4831 mips_elf_set_gotidx (void **entryp, long gotidx)
   4832 {
   4833   struct mips_got_entry *entry;
   4834 
   4835   entry = (struct mips_got_entry *) *entryp;
   4836   if (entry->gotidx > 0)
   4837     {
   4838       struct mips_got_entry *new_entry;
   4839 
   4840       new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
   4841       if (!new_entry)
   4842 	return false;
   4843 
   4844       *new_entry = *entry;
   4845       *entryp = new_entry;
   4846       entry = new_entry;
   4847     }
   4848   entry->gotidx = gotidx;
   4849   return true;
   4850 }
   4851 
   4852 /* Set the TLS GOT index for the GOT entry in ENTRYP.  DATA points to a
   4853    mips_elf_traverse_got_arg in which DATA->value is the size of one
   4854    GOT entry.  Set DATA->g to null on failure.  */
   4855 
   4856 static int
   4857 mips_elf_initialize_tls_index (void **entryp, void *data)
   4858 {
   4859   struct mips_got_entry *entry;
   4860   struct mips_elf_traverse_got_arg *arg;
   4861 
   4862   /* We're only interested in TLS symbols.  */
   4863   entry = (struct mips_got_entry *) *entryp;
   4864   if (entry->tls_type == GOT_TLS_NONE)
   4865     return 1;
   4866 
   4867   arg = (struct mips_elf_traverse_got_arg *) data;
   4868   if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
   4869     {
   4870       arg->g = NULL;
   4871       return 0;
   4872     }
   4873 
   4874   /* Account for the entries we've just allocated.  */
   4875   arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
   4876   return 1;
   4877 }
   4878 
   4879 /* A htab_traverse callback for GOT entries, where DATA points to a
   4880    mips_elf_traverse_got_arg.  Set the global_got_area of each global
   4881    symbol to DATA->value.  */
   4882 
   4883 static int
   4884 mips_elf_set_global_got_area (void **entryp, void *data)
   4885 {
   4886   struct mips_got_entry *entry;
   4887   struct mips_elf_traverse_got_arg *arg;
   4888 
   4889   entry = (struct mips_got_entry *) *entryp;
   4890   arg = (struct mips_elf_traverse_got_arg *) data;
   4891   if (entry->abfd != NULL
   4892       && entry->symndx == -1
   4893       && entry->d.h->global_got_area != GGA_NONE)
   4894     entry->d.h->global_got_area = arg->value;
   4895   return 1;
   4896 }
   4897 
   4898 /* A htab_traverse callback for secondary GOT entries, where DATA points
   4899    to a mips_elf_traverse_got_arg.  Assign GOT indices to global entries
   4900    and record the number of relocations they require.  DATA->value is
   4901    the size of one GOT entry.  Set DATA->g to null on failure.  */
   4902 
   4903 static int
   4904 mips_elf_set_global_gotidx (void **entryp, void *data)
   4905 {
   4906   struct mips_got_entry *entry;
   4907   struct mips_elf_traverse_got_arg *arg;
   4908 
   4909   entry = (struct mips_got_entry *) *entryp;
   4910   arg = (struct mips_elf_traverse_got_arg *) data;
   4911   if (entry->abfd != NULL
   4912       && entry->symndx == -1
   4913       && entry->d.h->global_got_area != GGA_NONE)
   4914     {
   4915       if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
   4916 	{
   4917 	  arg->g = NULL;
   4918 	  return 0;
   4919 	}
   4920       arg->g->assigned_low_gotno += 1;
   4921 
   4922       if (bfd_link_pic (arg->info)
   4923 	  || (elf_hash_table (arg->info)->dynamic_sections_created
   4924 	      && entry->d.h->root.def_dynamic
   4925 	      && !entry->d.h->root.def_regular))
   4926 	arg->g->relocs += 1;
   4927     }
   4928 
   4929   return 1;
   4930 }
   4931 
   4932 /* A htab_traverse callback for GOT entries for which DATA is the
   4933    bfd_link_info.  Forbid any global symbols from having traditional
   4934    lazy-binding stubs.  */
   4935 
   4936 static int
   4937 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
   4938 {
   4939   struct bfd_link_info *info;
   4940   struct mips_elf_link_hash_table *htab;
   4941   struct mips_got_entry *entry;
   4942 
   4943   entry = (struct mips_got_entry *) *entryp;
   4944   info = (struct bfd_link_info *) data;
   4945   htab = mips_elf_hash_table (info);
   4946   BFD_ASSERT (htab != NULL);
   4947 
   4948   if (entry->abfd != NULL
   4949       && entry->symndx == -1
   4950       && entry->d.h->needs_lazy_stub)
   4951     {
   4952       entry->d.h->needs_lazy_stub = false;
   4953       htab->lazy_stub_count--;
   4954     }
   4955 
   4956   return 1;
   4957 }
   4958 
   4959 /* Return the offset of an input bfd IBFD's GOT from the beginning of
   4960    the primary GOT.  */
   4961 static bfd_vma
   4962 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
   4963 {
   4964   if (!g->next)
   4965     return 0;
   4966 
   4967   g = mips_elf_bfd_got (ibfd, false);
   4968   if (! g)
   4969     return 0;
   4970 
   4971   BFD_ASSERT (g->next);
   4972 
   4973   g = g->next;
   4974 
   4975   return (g->local_gotno + g->global_gotno + g->tls_gotno)
   4976     * MIPS_ELF_GOT_SIZE (abfd);
   4977 }
   4978 
   4979 /* Turn a single GOT that is too big for 16-bit addressing into
   4980    a sequence of GOTs, each one 16-bit addressable.  */
   4981 
   4982 static bool
   4983 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
   4984 		    asection *got, bfd_size_type pages)
   4985 {
   4986   struct mips_elf_link_hash_table *htab;
   4987   struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
   4988   struct mips_elf_traverse_got_arg tga;
   4989   struct mips_got_info *g, *gg;
   4990   unsigned int assign, needed_relocs;
   4991   bfd *dynobj, *ibfd;
   4992 
   4993   dynobj = elf_hash_table (info)->dynobj;
   4994   htab = mips_elf_hash_table (info);
   4995   BFD_ASSERT (htab != NULL);
   4996 
   4997   g = htab->got_info;
   4998 
   4999   got_per_bfd_arg.obfd = abfd;
   5000   got_per_bfd_arg.info = info;
   5001   got_per_bfd_arg.current = NULL;
   5002   got_per_bfd_arg.primary = NULL;
   5003   got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
   5004 				/ MIPS_ELF_GOT_SIZE (abfd))
   5005 			       - htab->reserved_gotno);
   5006   got_per_bfd_arg.max_pages = pages;
   5007   /* The number of globals that will be included in the primary GOT.
   5008      See the calls to mips_elf_set_global_got_area below for more
   5009      information.  */
   5010   got_per_bfd_arg.global_count = g->global_gotno;
   5011 
   5012   /* Try to merge the GOTs of input bfds together, as long as they
   5013      don't seem to exceed the maximum GOT size, choosing one of them
   5014      to be the primary GOT.  */
   5015   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   5016     {
   5017       gg = mips_elf_bfd_got (ibfd, false);
   5018       if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
   5019 	return false;
   5020     }
   5021 
   5022   /* If we do not find any suitable primary GOT, create an empty one.  */
   5023   if (got_per_bfd_arg.primary == NULL)
   5024     g->next = mips_elf_create_got_info (abfd);
   5025   else
   5026     g->next = got_per_bfd_arg.primary;
   5027   g->next->next = got_per_bfd_arg.current;
   5028 
   5029   /* GG is now the master GOT, and G is the primary GOT.  */
   5030   gg = g;
   5031   g = g->next;
   5032 
   5033   /* Map the output bfd to the primary got.  That's what we're going
   5034      to use for bfds that use GOT16 or GOT_PAGE relocations that we
   5035      didn't mark in check_relocs, and we want a quick way to find it.
   5036      We can't just use gg->next because we're going to reverse the
   5037      list.  */
   5038   mips_elf_replace_bfd_got (abfd, g);
   5039 
   5040   /* Every symbol that is referenced in a dynamic relocation must be
   5041      present in the primary GOT, so arrange for them to appear after
   5042      those that are actually referenced.  */
   5043   gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
   5044   g->global_gotno = gg->global_gotno;
   5045 
   5046   tga.info = info;
   5047   tga.value = GGA_RELOC_ONLY;
   5048   htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
   5049   tga.value = GGA_NORMAL;
   5050   htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
   5051 
   5052   /* Now go through the GOTs assigning them offset ranges.
   5053      [assigned_low_gotno, local_gotno[ will be set to the range of local
   5054      entries in each GOT.  We can then compute the end of a GOT by
   5055      adding local_gotno to global_gotno.  We reverse the list and make
   5056      it circular since then we'll be able to quickly compute the
   5057      beginning of a GOT, by computing the end of its predecessor.  To
   5058      avoid special cases for the primary GOT, while still preserving
   5059      assertions that are valid for both single- and multi-got links,
   5060      we arrange for the main got struct to have the right number of
   5061      global entries, but set its local_gotno such that the initial
   5062      offset of the primary GOT is zero.  Remember that the primary GOT
   5063      will become the last item in the circular linked list, so it
   5064      points back to the master GOT.  */
   5065   gg->local_gotno = -g->global_gotno;
   5066   gg->global_gotno = g->global_gotno;
   5067   gg->tls_gotno = 0;
   5068   assign = 0;
   5069   gg->next = gg;
   5070 
   5071   do
   5072     {
   5073       struct mips_got_info *gn;
   5074 
   5075       assign += htab->reserved_gotno;
   5076       g->assigned_low_gotno = assign;
   5077       g->local_gotno += assign;
   5078       g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
   5079       g->assigned_high_gotno = g->local_gotno - 1;
   5080       assign = g->local_gotno + g->global_gotno + g->tls_gotno;
   5081 
   5082       /* Take g out of the direct list, and push it onto the reversed
   5083 	 list that gg points to.  g->next is guaranteed to be nonnull after
   5084 	 this operation, as required by mips_elf_initialize_tls_index. */
   5085       gn = g->next;
   5086       g->next = gg->next;
   5087       gg->next = g;
   5088 
   5089       /* Set up any TLS entries.  We always place the TLS entries after
   5090 	 all non-TLS entries.  */
   5091       g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
   5092       tga.g = g;
   5093       tga.value = MIPS_ELF_GOT_SIZE (abfd);
   5094       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
   5095       if (!tga.g)
   5096 	return false;
   5097       BFD_ASSERT (g->tls_assigned_gotno == assign);
   5098 
   5099       /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
   5100       g = gn;
   5101 
   5102       /* Forbid global symbols in every non-primary GOT from having
   5103 	 lazy-binding stubs.  */
   5104       if (g)
   5105 	htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
   5106     }
   5107   while (g);
   5108 
   5109   got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
   5110 
   5111   needed_relocs = 0;
   5112   for (g = gg->next; g && g->next != gg; g = g->next)
   5113     {
   5114       unsigned int save_assign;
   5115 
   5116       /* Assign offsets to global GOT entries and count how many
   5117 	 relocations they need.  */
   5118       save_assign = g->assigned_low_gotno;
   5119       g->assigned_low_gotno = g->local_gotno;
   5120       tga.info = info;
   5121       tga.value = MIPS_ELF_GOT_SIZE (abfd);
   5122       tga.g = g;
   5123       htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
   5124       if (!tga.g)
   5125 	return false;
   5126       BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
   5127       g->assigned_low_gotno = save_assign;
   5128 
   5129       if (bfd_link_pic (info))
   5130 	{
   5131 	  g->relocs += g->local_gotno - g->assigned_low_gotno;
   5132 	  BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
   5133 		      + g->next->global_gotno
   5134 		      + g->next->tls_gotno
   5135 		      + htab->reserved_gotno);
   5136 	}
   5137       needed_relocs += g->relocs;
   5138     }
   5139   needed_relocs += g->relocs;
   5140 
   5141   if (needed_relocs)
   5142     mips_elf_allocate_dynamic_relocations (dynobj, info,
   5143 					   needed_relocs);
   5144 
   5145   return true;
   5146 }
   5147 
   5148 
   5149 /* Returns the first relocation of type r_type found, beginning with
   5151    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
   5152 
   5153 static const Elf_Internal_Rela *
   5154 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
   5155 			  const Elf_Internal_Rela *relocation,
   5156 			  const Elf_Internal_Rela *relend)
   5157 {
   5158   unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
   5159 
   5160   while (relocation < relend)
   5161     {
   5162       if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
   5163 	  && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
   5164 	return relocation;
   5165 
   5166       ++relocation;
   5167     }
   5168 
   5169   /* We didn't find it.  */
   5170   return NULL;
   5171 }
   5172 
   5173 /* Return whether an input relocation is against a local symbol.  */
   5174 
   5175 static bool
   5176 mips_elf_local_relocation_p (bfd *input_bfd,
   5177 			     const Elf_Internal_Rela *relocation,
   5178 			     asection **local_sections)
   5179 {
   5180   unsigned long r_symndx;
   5181   Elf_Internal_Shdr *symtab_hdr;
   5182   size_t extsymoff;
   5183 
   5184   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
   5185   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   5186   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
   5187 
   5188   if (r_symndx < extsymoff)
   5189     return true;
   5190   if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
   5191     return true;
   5192 
   5193   return false;
   5194 }
   5195 
   5196 /* Sign-extend VALUE, which has the indicated number of BITS.  */
   5198 
   5199 bfd_vma
   5200 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
   5201 {
   5202   if (value & ((bfd_vma) 1 << (bits - 1)))
   5203     /* VALUE is negative.  */
   5204     value |= ((bfd_vma) - 1) << bits;
   5205 
   5206   return value;
   5207 }
   5208 
   5209 /* Return non-zero if the indicated VALUE has overflowed the maximum
   5210    range expressible by a signed number with the indicated number of
   5211    BITS.  */
   5212 
   5213 static bool
   5214 mips_elf_overflow_p (bfd_vma value, int bits)
   5215 {
   5216   bfd_signed_vma svalue = (bfd_signed_vma) value;
   5217 
   5218   if (svalue > (1 << (bits - 1)) - 1)
   5219     /* The value is too big.  */
   5220     return true;
   5221   else if (svalue < -(1 << (bits - 1)))
   5222     /* The value is too small.  */
   5223     return true;
   5224 
   5225   /* All is well.  */
   5226   return false;
   5227 }
   5228 
   5229 /* Calculate the %high function.  */
   5230 
   5231 static bfd_vma
   5232 mips_elf_high (bfd_vma value)
   5233 {
   5234   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
   5235 }
   5236 
   5237 /* Calculate the %higher function.  */
   5238 
   5239 static bfd_vma
   5240 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
   5241 {
   5242 #ifdef BFD64
   5243   return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
   5244 #else
   5245   abort ();
   5246   return MINUS_ONE;
   5247 #endif
   5248 }
   5249 
   5250 /* Calculate the %highest function.  */
   5251 
   5252 static bfd_vma
   5253 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
   5254 {
   5255 #ifdef BFD64
   5256   return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
   5257 #else
   5258   abort ();
   5259   return MINUS_ONE;
   5260 #endif
   5261 }
   5262 
   5263 /* Create the .compact_rel section.  */
   5265 
   5266 static bool
   5267 mips_elf_create_compact_rel_section
   5268   (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
   5269 {
   5270   flagword flags;
   5271   register asection *s;
   5272 
   5273   if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
   5274     {
   5275       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
   5276 	       | SEC_READONLY);
   5277 
   5278       s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
   5279       if (s == NULL
   5280 	  || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   5281 	return false;
   5282 
   5283       s->size = sizeof (Elf32_External_compact_rel);
   5284     }
   5285 
   5286   return true;
   5287 }
   5288 
   5289 /* Create the .got section to hold the global offset table.  */
   5290 
   5291 static bool
   5292 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
   5293 {
   5294   flagword flags;
   5295   register asection *s;
   5296   struct elf_link_hash_entry *h;
   5297   struct bfd_link_hash_entry *bh;
   5298   struct mips_elf_link_hash_table *htab;
   5299 
   5300   htab = mips_elf_hash_table (info);
   5301   BFD_ASSERT (htab != NULL);
   5302 
   5303   /* This function may be called more than once.  */
   5304   if (htab->root.sgot)
   5305     return true;
   5306 
   5307   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   5308 	   | SEC_LINKER_CREATED);
   5309 
   5310   /* We have to use an alignment of 2**4 here because this is hardcoded
   5311      in the function stub generation and in the linker script.  */
   5312   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   5313   if (s == NULL
   5314       || !bfd_set_section_alignment (s, 4))
   5315     return false;
   5316   htab->root.sgot = s;
   5317 
   5318   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
   5319      linker script because we don't want to define the symbol if we
   5320      are not creating a global offset table.  */
   5321   bh = NULL;
   5322   if (! (_bfd_generic_link_add_one_symbol
   5323 	 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
   5324 	  0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
   5325     return false;
   5326 
   5327   h = (struct elf_link_hash_entry *) bh;
   5328   h->non_elf = 0;
   5329   h->def_regular = 1;
   5330   h->type = STT_OBJECT;
   5331   h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
   5332   elf_hash_table (info)->hgot = h;
   5333 
   5334   if (bfd_link_pic (info)
   5335       && ! bfd_elf_link_record_dynamic_symbol (info, h))
   5336     return false;
   5337 
   5338   htab->got_info = mips_elf_create_got_info (abfd);
   5339   mips_elf_section_data (s)->elf.this_hdr.sh_flags
   5340     |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
   5341 
   5342   /* We also need a .got.plt section when generating PLTs.  */
   5343   s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
   5344 					  SEC_ALLOC | SEC_LOAD
   5345 					  | SEC_HAS_CONTENTS
   5346 					  | SEC_IN_MEMORY
   5347 					  | SEC_LINKER_CREATED);
   5348   if (s == NULL)
   5349     return false;
   5350   htab->root.sgotplt = s;
   5351 
   5352   return true;
   5353 }
   5354 
   5355 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
   5357    __GOTT_INDEX__ symbols.  These symbols are only special for
   5358    shared objects; they are not used in executables.  */
   5359 
   5360 static bool
   5361 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
   5362 {
   5363   return (mips_elf_hash_table (info)->root.target_os == is_vxworks
   5364 	  && bfd_link_pic (info)
   5365 	  && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
   5366 	      || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
   5367 }
   5368 
   5369 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
   5370    require an la25 stub.  See also mips_elf_local_pic_function_p,
   5371    which determines whether the destination function ever requires a
   5372    stub.  */
   5373 
   5374 static bool
   5375 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
   5376 				     bool target_is_16_bit_code_p)
   5377 {
   5378   /* We specifically ignore branches and jumps from EF_PIC objects,
   5379      where the onus is on the compiler or programmer to perform any
   5380      necessary initialization of $25.  Sometimes such initialization
   5381      is unnecessary; for example, -mno-shared functions do not use
   5382      the incoming value of $25, and may therefore be called directly.  */
   5383   if (PIC_OBJECT_P (input_bfd))
   5384     return false;
   5385 
   5386   switch (r_type)
   5387     {
   5388     case R_MIPS_26:
   5389     case R_MIPS_PC16:
   5390     case R_MIPS_PC21_S2:
   5391     case R_MIPS_PC26_S2:
   5392     case R_MICROMIPS_26_S1:
   5393     case R_MICROMIPS_PC7_S1:
   5394     case R_MICROMIPS_PC10_S1:
   5395     case R_MICROMIPS_PC16_S1:
   5396     case R_MICROMIPS_PC23_S2:
   5397       return true;
   5398 
   5399     case R_MIPS16_26:
   5400       return !target_is_16_bit_code_p;
   5401 
   5402     default:
   5403       return false;
   5404     }
   5405 }
   5406 
   5407 /* Obtain the field relocated by RELOCATION.  */
   5409 
   5410 static bfd_vma
   5411 mips_elf_obtain_contents (reloc_howto_type *howto,
   5412 			  const Elf_Internal_Rela *relocation,
   5413 			  bfd *input_bfd, bfd_byte *contents)
   5414 {
   5415   bfd_vma x = 0;
   5416   bfd_byte *location = contents + relocation->r_offset;
   5417   unsigned int size = bfd_get_reloc_size (howto);
   5418 
   5419   /* Obtain the bytes.  */
   5420   if (size != 0)
   5421     x = bfd_get (8 * size, input_bfd, location);
   5422 
   5423   return x;
   5424 }
   5425 
   5426 /* Store the field relocated by RELOCATION.  */
   5427 
   5428 static void
   5429 mips_elf_store_contents (reloc_howto_type *howto,
   5430 			 const Elf_Internal_Rela *relocation,
   5431 			 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
   5432 {
   5433   bfd_byte *location = contents + relocation->r_offset;
   5434   unsigned int size = bfd_get_reloc_size (howto);
   5435 
   5436   /* Put the value into the output.  */
   5437   if (size != 0)
   5438     bfd_put (8 * size, input_bfd, x, location);
   5439 }
   5440 
   5441 /* Try to patch a load from GOT instruction in CONTENTS pointed to by
   5442    RELOCATION described by HOWTO, with a move of 0 to the load target
   5443    register, returning TRUE if that is successful and FALSE otherwise.
   5444    If DOIT is FALSE, then only determine it patching is possible and
   5445    return status without actually changing CONTENTS.
   5446 */
   5447 
   5448 static bool
   5449 mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
   5450 			   const Elf_Internal_Rela *relocation,
   5451 			   reloc_howto_type *howto, bool doit)
   5452 {
   5453   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   5454   bfd_byte *location = contents + relocation->r_offset;
   5455   bool nullified = true;
   5456   bfd_vma x;
   5457 
   5458   _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
   5459 
   5460   /* Obtain the current value.  */
   5461   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
   5462 
   5463   /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
   5464      while RY is at bits [18:16] of the combined 32-bit instruction word.  */
   5465   if (mips16_reloc_p (r_type)
   5466       && (((x >> 22) & 0x3ff) == 0x3d3				/* LW */
   5467 	  || ((x >> 22) & 0x3ff) == 0x3c7))			/* LD */
   5468     x = (0x3cdU << 22) | (x & (7 << 16)) << 3;			/* LI */
   5469   else if (micromips_reloc_p (r_type)
   5470 	   && ((x >> 26) & 0x37) == 0x37)			/* LW/LD */
   5471     x = (0xc << 26) | (x & (0x1f << 21));			/* ADDIU */
   5472   else if (((x >> 26) & 0x3f) == 0x23				/* LW */
   5473 	   || ((x >> 26) & 0x3f) == 0x37)			/* LD */
   5474     x = (0x9 << 26) | (x & (0x1f << 16));			/* ADDIU */
   5475   else
   5476     nullified = false;
   5477 
   5478   /* Put the value into the output.  */
   5479   if (doit && nullified)
   5480     mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
   5481 
   5482   _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
   5483 
   5484   return nullified;
   5485 }
   5486 
   5487 /* Calculate the value produced by the RELOCATION (which comes from
   5488    the INPUT_BFD).  The ADDEND is the addend to use for this
   5489    RELOCATION; RELOCATION->R_ADDEND is ignored.
   5490 
   5491    The result of the relocation calculation is stored in VALUEP.
   5492    On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
   5493    is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
   5494 
   5495    This function returns bfd_reloc_continue if the caller need take no
   5496    further action regarding this relocation, bfd_reloc_notsupported if
   5497    something goes dramatically wrong, bfd_reloc_overflow if an
   5498    overflow occurs, and bfd_reloc_ok to indicate success.  */
   5499 
   5500 static bfd_reloc_status_type
   5501 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
   5502 			       asection *input_section, bfd_byte *contents,
   5503 			       struct bfd_link_info *info,
   5504 			       const Elf_Internal_Rela *relocation,
   5505 			       bfd_vma addend, reloc_howto_type *howto,
   5506 			       Elf_Internal_Sym *local_syms,
   5507 			       asection **local_sections, bfd_vma *valuep,
   5508 			       const char **namep,
   5509 			       bool *cross_mode_jump_p,
   5510 			       bool save_addend)
   5511 {
   5512   /* The eventual value we will return.  */
   5513   bfd_vma value;
   5514   /* The address of the symbol against which the relocation is
   5515      occurring.  */
   5516   bfd_vma symbol = 0;
   5517   /* The final GP value to be used for the relocatable, executable, or
   5518      shared object file being produced.  */
   5519   bfd_vma gp;
   5520   /* The place (section offset or address) of the storage unit being
   5521      relocated.  */
   5522   bfd_vma p;
   5523   /* The value of GP used to create the relocatable object.  */
   5524   bfd_vma gp0;
   5525   /* The offset into the global offset table at which the address of
   5526      the relocation entry symbol, adjusted by the addend, resides
   5527      during execution.  */
   5528   bfd_vma g = MINUS_ONE;
   5529   /* The section in which the symbol referenced by the relocation is
   5530      located.  */
   5531   asection *sec = NULL;
   5532   struct mips_elf_link_hash_entry *h = NULL;
   5533   /* TRUE if the symbol referred to by this relocation is a local
   5534      symbol.  */
   5535   bool local_p, was_local_p;
   5536   /* TRUE if the symbol referred to by this relocation is a section
   5537      symbol.  */
   5538   bool section_p = false;
   5539   /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
   5540   bool gp_disp_p = false;
   5541   /* TRUE if the symbol referred to by this relocation is
   5542      "__gnu_local_gp".  */
   5543   bool gnu_local_gp_p = false;
   5544   Elf_Internal_Shdr *symtab_hdr;
   5545   size_t extsymoff;
   5546   unsigned long r_symndx;
   5547   int r_type;
   5548   /* TRUE if overflow occurred during the calculation of the
   5549      relocation value.  */
   5550   bool overflowed_p;
   5551   /* TRUE if this relocation refers to a MIPS16 function.  */
   5552   bool target_is_16_bit_code_p = false;
   5553   bool target_is_micromips_code_p = false;
   5554   struct mips_elf_link_hash_table *htab;
   5555   bfd *dynobj;
   5556   bool resolved_to_zero;
   5557 
   5558   dynobj = elf_hash_table (info)->dynobj;
   5559   htab = mips_elf_hash_table (info);
   5560   BFD_ASSERT (htab != NULL);
   5561 
   5562   /* Parse the relocation.  */
   5563   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
   5564   r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   5565   p = (input_section->output_section->vma
   5566        + input_section->output_offset
   5567        + relocation->r_offset);
   5568 
   5569   /* Assume that there will be no overflow.  */
   5570   overflowed_p = false;
   5571 
   5572   /* Figure out whether or not the symbol is local, and get the offset
   5573      used in the array of hash table entries.  */
   5574   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   5575   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
   5576 					 local_sections);
   5577   was_local_p = local_p;
   5578   if (! elf_bad_symtab (input_bfd))
   5579     extsymoff = symtab_hdr->sh_info;
   5580   else
   5581     {
   5582       /* The symbol table does not follow the rule that local symbols
   5583 	 must come before globals.  */
   5584       extsymoff = 0;
   5585     }
   5586 
   5587   /* Figure out the value of the symbol.  */
   5588   if (local_p)
   5589     {
   5590       bool micromips_p = MICROMIPS_P (abfd);
   5591       Elf_Internal_Sym *sym;
   5592 
   5593       sym = local_syms + r_symndx;
   5594       sec = local_sections[r_symndx];
   5595 
   5596       section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
   5597 
   5598       symbol = sec->output_section->vma + sec->output_offset;
   5599       if (!section_p || (sec->flags & SEC_MERGE))
   5600 	symbol += sym->st_value;
   5601       if ((sec->flags & SEC_MERGE) && section_p)
   5602 	{
   5603 	  addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
   5604 	  addend -= symbol;
   5605 	  addend += sec->output_section->vma + sec->output_offset;
   5606 	}
   5607 
   5608       /* MIPS16/microMIPS text labels should be treated as odd.  */
   5609       if (ELF_ST_IS_COMPRESSED (sym->st_other))
   5610 	++symbol;
   5611 
   5612       /* Record the name of this symbol, for our caller.  */
   5613       *namep = bfd_elf_string_from_elf_section (input_bfd,
   5614 						symtab_hdr->sh_link,
   5615 						sym->st_name);
   5616       if (*namep == NULL || **namep == '\0')
   5617 	*namep = bfd_section_name (sec);
   5618 
   5619       /* For relocations against a section symbol and ones against no
   5620 	 symbol (absolute relocations) infer the ISA mode from the addend.  */
   5621       if (section_p || r_symndx == STN_UNDEF)
   5622 	{
   5623 	  target_is_16_bit_code_p = (addend & 1) && !micromips_p;
   5624 	  target_is_micromips_code_p = (addend & 1) && micromips_p;
   5625 	}
   5626       /* For relocations against an absolute symbol infer the ISA mode
   5627 	 from the value of the symbol plus addend.  */
   5628       else if (bfd_is_abs_section (sec))
   5629 	{
   5630 	  target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
   5631 	  target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
   5632 	}
   5633       /* Otherwise just use the regular symbol annotation available.  */
   5634       else
   5635 	{
   5636 	  target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
   5637 	  target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
   5638 	}
   5639     }
   5640   else
   5641     {
   5642       /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
   5643 
   5644       /* For global symbols we look up the symbol in the hash-table.  */
   5645       h = ((struct mips_elf_link_hash_entry *)
   5646 	   elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
   5647       /* Find the real hash-table entry for this symbol.  */
   5648       while (h->root.root.type == bfd_link_hash_indirect
   5649 	     || h->root.root.type == bfd_link_hash_warning)
   5650 	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   5651 
   5652       /* Record the name of this symbol, for our caller.  */
   5653       *namep = h->root.root.root.string;
   5654 
   5655       /* See if this is the special _gp_disp symbol.  Note that such a
   5656 	 symbol must always be a global symbol.  */
   5657       if (strcmp (*namep, "_gp_disp") == 0
   5658 	  && ! NEWABI_P (input_bfd))
   5659 	{
   5660 	  /* Relocations against _gp_disp are permitted only with
   5661 	     R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
   5662 	  if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
   5663 	    return bfd_reloc_notsupported;
   5664 
   5665 	  gp_disp_p = true;
   5666 	}
   5667       /* See if this is the special _gp symbol.  Note that such a
   5668 	 symbol must always be a global symbol.  */
   5669       else if (strcmp (*namep, "__gnu_local_gp") == 0)
   5670 	gnu_local_gp_p = true;
   5671 
   5672 
   5673       /* If this symbol is defined, calculate its address.  Note that
   5674 	 _gp_disp is a magic symbol, always implicitly defined by the
   5675 	 linker, so it's inappropriate to check to see whether or not
   5676 	 its defined.  */
   5677       else if ((h->root.root.type == bfd_link_hash_defined
   5678 		|| h->root.root.type == bfd_link_hash_defweak)
   5679 	       && h->root.root.u.def.section)
   5680 	{
   5681 	  sec = h->root.root.u.def.section;
   5682 	  if (sec->output_section)
   5683 	    symbol = (h->root.root.u.def.value
   5684 		      + sec->output_section->vma
   5685 		      + sec->output_offset);
   5686 	  else
   5687 	    symbol = h->root.root.u.def.value;
   5688 	}
   5689       else if (h->root.root.type == bfd_link_hash_undefweak)
   5690 	/* We allow relocations against undefined weak symbols, giving
   5691 	   it the value zero, so that you can undefined weak functions
   5692 	   and check to see if they exist by looking at their
   5693 	   addresses.  */
   5694 	symbol = 0;
   5695       else if (info->unresolved_syms_in_objects == RM_IGNORE
   5696 	       && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
   5697 	symbol = 0;
   5698       else if (strcmp (*namep, SGI_COMPAT (input_bfd)
   5699 		       ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
   5700 	{
   5701 	  /* If this is a dynamic link, we should have created a
   5702 	     _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
   5703 	     in _bfd_mips_elf_create_dynamic_sections.
   5704 	     Otherwise, we should define the symbol with a value of 0.
   5705 	     FIXME: It should probably get into the symbol table
   5706 	     somehow as well.  */
   5707 	  BFD_ASSERT (! bfd_link_pic (info));
   5708 	  BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
   5709 	  symbol = 0;
   5710 	}
   5711       else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
   5712 	{
   5713 	  /* This is an optional symbol - an Irix specific extension to the
   5714 	     ELF spec.  Ignore it for now.
   5715 	     XXX - FIXME - there is more to the spec for OPTIONAL symbols
   5716 	     than simply ignoring them, but we do not handle this for now.
   5717 	     For information see the "64-bit ELF Object File Specification"
   5718 	     which is available from here:
   5719 	     http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
   5720 	  symbol = 0;
   5721 	}
   5722       else
   5723 	{
   5724           bool reject_undefined
   5725 	    = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
   5726 		&& !info->warn_unresolved_syms)
   5727 	       || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
   5728 
   5729 	  info->callbacks->undefined_symbol
   5730 	    (info, h->root.root.root.string, input_bfd,
   5731 	     input_section, relocation->r_offset, reject_undefined);
   5732 
   5733 	  if (reject_undefined)
   5734 	    return bfd_reloc_undefined;
   5735 
   5736 	  symbol = 0;
   5737 	}
   5738 
   5739       target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
   5740       target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
   5741     }
   5742 
   5743   /* If this is a reference to a 16-bit function with a stub, we need
   5744      to redirect the relocation to the stub unless:
   5745 
   5746      (a) the relocation is for a MIPS16 JAL;
   5747 
   5748      (b) the relocation is for a MIPS16 PIC call, and there are no
   5749 	 non-MIPS16 uses of the GOT slot; or
   5750 
   5751      (c) the section allows direct references to MIPS16 functions.  */
   5752   if (r_type != R_MIPS16_26
   5753       && !bfd_link_relocatable (info)
   5754       && ((h != NULL
   5755 	   && h->fn_stub != NULL
   5756 	   && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
   5757 	  || (local_p
   5758 	      && mips_elf_tdata (input_bfd)->local_stubs != NULL
   5759 	      && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
   5760       && !section_allows_mips16_refs_p (input_section))
   5761     {
   5762       /* This is a 32- or 64-bit call to a 16-bit function.  We should
   5763 	 have already noticed that we were going to need the
   5764 	 stub.  */
   5765       if (local_p)
   5766 	{
   5767 	  sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
   5768 	  value = 0;
   5769 	}
   5770       else
   5771 	{
   5772 	  BFD_ASSERT (h->need_fn_stub);
   5773 	  if (h->la25_stub)
   5774 	    {
   5775 	      /* If a LA25 header for the stub itself exists, point to the
   5776 		 prepended LUI/ADDIU sequence.  */
   5777 	      sec = h->la25_stub->stub_section;
   5778 	      value = h->la25_stub->offset;
   5779 	    }
   5780 	  else
   5781 	    {
   5782 	      sec = h->fn_stub;
   5783 	      value = 0;
   5784 	    }
   5785 	}
   5786 
   5787       symbol = sec->output_section->vma + sec->output_offset + value;
   5788       /* The target is 16-bit, but the stub isn't.  */
   5789       target_is_16_bit_code_p = false;
   5790     }
   5791   /* If this is a MIPS16 call with a stub, that is made through the PLT or
   5792      to a standard MIPS function, we need to redirect the call to the stub.
   5793      Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
   5794      indirect calls should use an indirect stub instead.  */
   5795   else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
   5796 	   && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
   5797 	       || (local_p
   5798 		   && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
   5799 		   && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
   5800 	   && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
   5801     {
   5802       if (local_p)
   5803 	sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
   5804       else
   5805 	{
   5806 	  /* If both call_stub and call_fp_stub are defined, we can figure
   5807 	     out which one to use by checking which one appears in the input
   5808 	     file.  */
   5809 	  if (h->call_stub != NULL && h->call_fp_stub != NULL)
   5810 	    {
   5811 	      asection *o;
   5812 
   5813 	      sec = NULL;
   5814 	      for (o = input_bfd->sections; o != NULL; o = o->next)
   5815 		{
   5816 		  if (CALL_FP_STUB_P (bfd_section_name (o)))
   5817 		    {
   5818 		      sec = h->call_fp_stub;
   5819 		      break;
   5820 		    }
   5821 		}
   5822 	      if (sec == NULL)
   5823 		sec = h->call_stub;
   5824 	    }
   5825 	  else if (h->call_stub != NULL)
   5826 	    sec = h->call_stub;
   5827 	  else
   5828 	    sec = h->call_fp_stub;
   5829 	}
   5830 
   5831       BFD_ASSERT (sec->size > 0);
   5832       symbol = sec->output_section->vma + sec->output_offset;
   5833     }
   5834   /* If this is a direct call to a PIC function, redirect to the
   5835      non-PIC stub.  */
   5836   else if (h != NULL && h->la25_stub
   5837 	   && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
   5838 						   target_is_16_bit_code_p))
   5839     {
   5840 	symbol = (h->la25_stub->stub_section->output_section->vma
   5841 		  + h->la25_stub->stub_section->output_offset
   5842 		  + h->la25_stub->offset);
   5843 	if (ELF_ST_IS_MICROMIPS (h->root.other))
   5844 	  symbol |= 1;
   5845     }
   5846   /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
   5847      entry is used if a standard PLT entry has also been made.  In this
   5848      case the symbol will have been set by mips_elf_set_plt_sym_value
   5849      to point to the standard PLT entry, so redirect to the compressed
   5850      one.  */
   5851   else if ((mips16_branch_reloc_p (r_type)
   5852 	    || micromips_branch_reloc_p (r_type))
   5853 	   && !bfd_link_relocatable (info)
   5854 	   && h != NULL
   5855 	   && h->use_plt_entry
   5856 	   && h->root.plt.plist->comp_offset != MINUS_ONE
   5857 	   && h->root.plt.plist->mips_offset != MINUS_ONE)
   5858     {
   5859       bool micromips_p = MICROMIPS_P (abfd);
   5860 
   5861       sec = htab->root.splt;
   5862       symbol = (sec->output_section->vma
   5863 		+ sec->output_offset
   5864 		+ htab->plt_header_size
   5865 		+ htab->plt_mips_offset
   5866 		+ h->root.plt.plist->comp_offset
   5867 		+ 1);
   5868 
   5869       target_is_16_bit_code_p = !micromips_p;
   5870       target_is_micromips_code_p = micromips_p;
   5871     }
   5872 
   5873   /* Make sure MIPS16 and microMIPS are not used together.  */
   5874   if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
   5875       || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
   5876    {
   5877       _bfd_error_handler
   5878 	(_("MIPS16 and microMIPS functions cannot call each other"));
   5879       return bfd_reloc_notsupported;
   5880    }
   5881 
   5882   /* Calls from 16-bit code to 32-bit code and vice versa require the
   5883      mode change.  However, we can ignore calls to undefined weak symbols,
   5884      which should never be executed at runtime.  This exception is important
   5885      because the assembly writer may have "known" that any definition of the
   5886      symbol would be 16-bit code, and that direct jumps were therefore
   5887      acceptable.  */
   5888   *cross_mode_jump_p = (!bfd_link_relocatable (info)
   5889 			&& !(h && h->root.root.type == bfd_link_hash_undefweak)
   5890 			&& ((mips16_branch_reloc_p (r_type)
   5891 			     && !target_is_16_bit_code_p)
   5892 			    || (micromips_branch_reloc_p (r_type)
   5893 				&& !target_is_micromips_code_p)
   5894 			    || ((branch_reloc_p (r_type)
   5895 				 || r_type == R_MIPS_JALR)
   5896 				&& (target_is_16_bit_code_p
   5897 				    || target_is_micromips_code_p))));
   5898 
   5899   resolved_to_zero = (h != NULL
   5900 		      && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
   5901 
   5902   switch (r_type)
   5903     {
   5904     case R_MIPS16_CALL16:
   5905     case R_MIPS16_GOT16:
   5906     case R_MIPS_CALL16:
   5907     case R_MIPS_GOT16:
   5908     case R_MIPS_GOT_PAGE:
   5909     case R_MIPS_GOT_DISP:
   5910     case R_MIPS_GOT_LO16:
   5911     case R_MIPS_CALL_LO16:
   5912     case R_MICROMIPS_CALL16:
   5913     case R_MICROMIPS_GOT16:
   5914     case R_MICROMIPS_GOT_PAGE:
   5915     case R_MICROMIPS_GOT_DISP:
   5916     case R_MICROMIPS_GOT_LO16:
   5917     case R_MICROMIPS_CALL_LO16:
   5918       if (resolved_to_zero
   5919 	  && !bfd_link_relocatable (info)
   5920 	  && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
   5921 					relocation->r_offset)
   5922 	  && mips_elf_nullify_got_load (input_bfd, contents,
   5923 					relocation, howto, true))
   5924 	return bfd_reloc_continue;
   5925 
   5926       /* Fall through.  */
   5927     case R_MIPS_GOT_HI16:
   5928     case R_MIPS_CALL_HI16:
   5929     case R_MICROMIPS_GOT_HI16:
   5930     case R_MICROMIPS_CALL_HI16:
   5931       if (resolved_to_zero
   5932 	  && htab->use_absolute_zero
   5933 	  && bfd_link_pic (info))
   5934 	{
   5935 	  /* Redirect to the special `__gnu_absolute_zero' symbol.  */
   5936 	  h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
   5937 					 false, false, false);
   5938 	  BFD_ASSERT (h != NULL);
   5939 	}
   5940       break;
   5941     }
   5942 
   5943   local_p = (h == NULL || mips_use_local_got_p (info, h));
   5944 
   5945   gp0 = _bfd_get_gp_value (input_bfd);
   5946   gp = _bfd_get_gp_value (abfd);
   5947   if (htab->got_info)
   5948     gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
   5949 
   5950   if (gnu_local_gp_p)
   5951     symbol = gp;
   5952 
   5953   /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
   5954      to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
   5955      corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.  */
   5956   if (got_page_reloc_p (r_type) && !local_p)
   5957     {
   5958       r_type = (micromips_reloc_p (r_type)
   5959 		? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
   5960       addend = 0;
   5961     }
   5962 
   5963   /* If we haven't already determined the GOT offset, and we're going
   5964      to need it, get it now.  */
   5965   switch (r_type)
   5966     {
   5967     case R_MIPS16_CALL16:
   5968     case R_MIPS16_GOT16:
   5969     case R_MIPS_CALL16:
   5970     case R_MIPS_GOT16:
   5971     case R_MIPS_GOT_DISP:
   5972     case R_MIPS_GOT_HI16:
   5973     case R_MIPS_CALL_HI16:
   5974     case R_MIPS_GOT_LO16:
   5975     case R_MIPS_CALL_LO16:
   5976     case R_MICROMIPS_CALL16:
   5977     case R_MICROMIPS_GOT16:
   5978     case R_MICROMIPS_GOT_DISP:
   5979     case R_MICROMIPS_GOT_HI16:
   5980     case R_MICROMIPS_CALL_HI16:
   5981     case R_MICROMIPS_GOT_LO16:
   5982     case R_MICROMIPS_CALL_LO16:
   5983     case R_MIPS_TLS_GD:
   5984     case R_MIPS_TLS_GOTTPREL:
   5985     case R_MIPS_TLS_LDM:
   5986     case R_MIPS16_TLS_GD:
   5987     case R_MIPS16_TLS_GOTTPREL:
   5988     case R_MIPS16_TLS_LDM:
   5989     case R_MICROMIPS_TLS_GD:
   5990     case R_MICROMIPS_TLS_GOTTPREL:
   5991     case R_MICROMIPS_TLS_LDM:
   5992       /* Find the index into the GOT where this value is located.  */
   5993       if (tls_ldm_reloc_p (r_type))
   5994 	{
   5995 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
   5996 					0, 0, NULL, r_type);
   5997 	  if (g == MINUS_ONE)
   5998 	    return bfd_reloc_outofrange;
   5999 	}
   6000       else if (!local_p)
   6001 	{
   6002 	  /* On VxWorks, CALL relocations should refer to the .got.plt
   6003 	     entry, which is initialized to point at the PLT stub.  */
   6004 	  if (htab->root.target_os == is_vxworks
   6005 	      && (call_hi16_reloc_p (r_type)
   6006 		  || call_lo16_reloc_p (r_type)
   6007 		  || call16_reloc_p (r_type)))
   6008 	    {
   6009 	      BFD_ASSERT (addend == 0);
   6010 	      BFD_ASSERT (h->root.needs_plt);
   6011 	      g = mips_elf_gotplt_index (info, &h->root);
   6012 	    }
   6013 	  else
   6014 	    {
   6015 	      BFD_ASSERT (addend == 0);
   6016 	      g = mips_elf_global_got_index (abfd, info, input_bfd,
   6017 					     &h->root, r_type);
   6018 	      if (!TLS_RELOC_P (r_type)
   6019 		  && !elf_hash_table (info)->dynamic_sections_created)
   6020 		/* This is a static link.  We must initialize the GOT entry.  */
   6021 		MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
   6022 	    }
   6023 	}
   6024       else if (htab->root.target_os != is_vxworks
   6025 	       && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
   6026 	/* The calculation below does not involve "g".  */
   6027 	break;
   6028       else
   6029 	{
   6030 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
   6031 					symbol + addend, r_symndx, h, r_type);
   6032 	  if (g == MINUS_ONE)
   6033 	    return bfd_reloc_outofrange;
   6034 	}
   6035 
   6036       /* Convert GOT indices to actual offsets.  */
   6037       g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
   6038       break;
   6039     }
   6040 
   6041   /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
   6042      symbols are resolved by the loader.  Add them to .rela.dyn.  */
   6043   if (h != NULL && is_gott_symbol (info, &h->root))
   6044     {
   6045       Elf_Internal_Rela outrel;
   6046       bfd_byte *loc;
   6047       asection *s;
   6048 
   6049       s = mips_elf_rel_dyn_section (info, false);
   6050       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
   6051 
   6052       outrel.r_offset = (input_section->output_section->vma
   6053 			 + input_section->output_offset
   6054 			 + relocation->r_offset);
   6055       outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
   6056       outrel.r_addend = addend;
   6057       bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
   6058 
   6059       /* If we've written this relocation for a readonly section,
   6060 	 we need to set DF_TEXTREL again, so that we do not delete the
   6061 	 DT_TEXTREL tag.  */
   6062       if (MIPS_ELF_READONLY_SECTION (input_section))
   6063 	info->flags |= DF_TEXTREL;
   6064 
   6065       *valuep = 0;
   6066       return bfd_reloc_ok;
   6067     }
   6068 
   6069   /* Figure out what kind of relocation is being performed.  */
   6070   switch (r_type)
   6071     {
   6072     case R_MIPS_NONE:
   6073       return bfd_reloc_continue;
   6074 
   6075     case R_MIPS_16:
   6076       if (howto->partial_inplace)
   6077 	addend = _bfd_mips_elf_sign_extend (addend, 16);
   6078       value = symbol + addend;
   6079       overflowed_p = mips_elf_overflow_p (value, 16);
   6080       break;
   6081 
   6082     case R_MIPS_32:
   6083     case R_MIPS_REL32:
   6084     case R_MIPS_64:
   6085       if ((bfd_link_pic (info)
   6086 	   || (htab->root.dynamic_sections_created
   6087 	       && h != NULL
   6088 	       && h->root.def_dynamic
   6089 	       && !h->root.def_regular
   6090 	       && !h->has_static_relocs))
   6091 	  && r_symndx != STN_UNDEF
   6092 	  && (h == NULL
   6093 	      || h->root.root.type != bfd_link_hash_undefweak
   6094 	      || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
   6095 		  && !resolved_to_zero))
   6096 	  && (input_section->flags & SEC_ALLOC) != 0)
   6097 	{
   6098 	  /* If we're creating a shared library, then we can't know
   6099 	     where the symbol will end up.  So, we create a relocation
   6100 	     record in the output, and leave the job up to the dynamic
   6101 	     linker.  We must do the same for executable references to
   6102 	     shared library symbols, unless we've decided to use copy
   6103 	     relocs or PLTs instead.  */
   6104 	  value = addend;
   6105 	  if (!mips_elf_create_dynamic_relocation (abfd,
   6106 						   info,
   6107 						   relocation,
   6108 						   h,
   6109 						   sec,
   6110 						   symbol,
   6111 						   &value,
   6112 						   input_section))
   6113 	    return bfd_reloc_undefined;
   6114 	}
   6115       else
   6116 	{
   6117 	  if (r_type != R_MIPS_REL32)
   6118 	    value = symbol + addend;
   6119 	  else
   6120 	    value = addend;
   6121 	}
   6122       value &= howto->dst_mask;
   6123       break;
   6124 
   6125     case R_MIPS_PC32:
   6126       value = symbol + addend - p;
   6127       value &= howto->dst_mask;
   6128       break;
   6129 
   6130     case R_MIPS16_26:
   6131       /* The calculation for R_MIPS16_26 is just the same as for an
   6132 	 R_MIPS_26.  It's only the storage of the relocated field into
   6133 	 the output file that's different.  That's handled in
   6134 	 mips_elf_perform_relocation.  So, we just fall through to the
   6135 	 R_MIPS_26 case here.  */
   6136     case R_MIPS_26:
   6137     case R_MICROMIPS_26_S1:
   6138       {
   6139 	unsigned int shift;
   6140 
   6141 	/* Shift is 2, unusually, for microMIPS JALX.  */
   6142 	shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
   6143 
   6144 	if (howto->partial_inplace && !section_p)
   6145 	  value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
   6146 	else
   6147 	  value = addend;
   6148 	value += symbol;
   6149 
   6150 	/* Make sure the target of a jump is suitably aligned.  Bit 0 must
   6151 	   be the correct ISA mode selector except for weak undefined
   6152 	   symbols.  */
   6153 	if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6154 	    && (*cross_mode_jump_p
   6155 		? (value & 3) != (r_type == R_MIPS_26)
   6156 		: (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
   6157 	  return bfd_reloc_outofrange;
   6158 
   6159 	value >>= shift;
   6160 	if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6161 	  overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
   6162 	value &= howto->dst_mask;
   6163       }
   6164       break;
   6165 
   6166     case R_MIPS_TLS_DTPREL_HI16:
   6167     case R_MIPS16_TLS_DTPREL_HI16:
   6168     case R_MICROMIPS_TLS_DTPREL_HI16:
   6169       value = (mips_elf_high (addend + symbol - dtprel_base (info))
   6170 	       & howto->dst_mask);
   6171       break;
   6172 
   6173     case R_MIPS_TLS_DTPREL_LO16:
   6174     case R_MIPS_TLS_DTPREL32:
   6175     case R_MIPS_TLS_DTPREL64:
   6176     case R_MIPS16_TLS_DTPREL_LO16:
   6177     case R_MICROMIPS_TLS_DTPREL_LO16:
   6178       value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
   6179       break;
   6180 
   6181     case R_MIPS_TLS_TPREL_HI16:
   6182     case R_MIPS16_TLS_TPREL_HI16:
   6183     case R_MICROMIPS_TLS_TPREL_HI16:
   6184       value = (mips_elf_high (addend + symbol - tprel_base (info))
   6185 	       & howto->dst_mask);
   6186       break;
   6187 
   6188     case R_MIPS_TLS_TPREL_LO16:
   6189     case R_MIPS_TLS_TPREL32:
   6190     case R_MIPS_TLS_TPREL64:
   6191     case R_MIPS16_TLS_TPREL_LO16:
   6192     case R_MICROMIPS_TLS_TPREL_LO16:
   6193       value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
   6194       break;
   6195 
   6196     case R_MIPS_HI16:
   6197     case R_MIPS16_HI16:
   6198     case R_MICROMIPS_HI16:
   6199       if (!gp_disp_p)
   6200 	{
   6201 	  value = mips_elf_high (addend + symbol);
   6202 	  value &= howto->dst_mask;
   6203 	}
   6204       else
   6205 	{
   6206 	  /* For MIPS16 ABI code we generate this sequence
   6207 		0: li      $v0,%hi(_gp_disp)
   6208 		4: addiupc $v1,%lo(_gp_disp)
   6209 		8: sll     $v0,16
   6210 	       12: addu    $v0,$v1
   6211 	       14: move    $gp,$v0
   6212 	     So the offsets of hi and lo relocs are the same, but the
   6213 	     base $pc is that used by the ADDIUPC instruction at $t9 + 4.
   6214 	     ADDIUPC clears the low two bits of the instruction address,
   6215 	     so the base is ($t9 + 4) & ~3.  */
   6216 	  if (r_type == R_MIPS16_HI16)
   6217 	    value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
   6218 	  /* The microMIPS .cpload sequence uses the same assembly
   6219 	     instructions as the traditional psABI version, but the
   6220 	     incoming $t9 has the low bit set.  */
   6221 	  else if (r_type == R_MICROMIPS_HI16)
   6222 	    value = mips_elf_high (addend + gp - p - 1);
   6223 	  else
   6224 	    value = mips_elf_high (addend + gp - p);
   6225 	}
   6226       break;
   6227 
   6228     case R_MIPS_LO16:
   6229     case R_MIPS16_LO16:
   6230     case R_MICROMIPS_LO16:
   6231     case R_MICROMIPS_HI0_LO16:
   6232       if (!gp_disp_p)
   6233 	value = (symbol + addend) & howto->dst_mask;
   6234       else
   6235 	{
   6236 	  /* See the comment for R_MIPS16_HI16 above for the reason
   6237 	     for this conditional.  */
   6238 	  if (r_type == R_MIPS16_LO16)
   6239 	    value = addend + gp - (p & ~(bfd_vma) 0x3);
   6240 	  else if (r_type == R_MICROMIPS_LO16
   6241 		   || r_type == R_MICROMIPS_HI0_LO16)
   6242 	    value = addend + gp - p + 3;
   6243 	  else
   6244 	    value = addend + gp - p + 4;
   6245 	  /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
   6246 	     for overflow.  But, on, say, IRIX5, relocations against
   6247 	     _gp_disp are normally generated from the .cpload
   6248 	     pseudo-op.  It generates code that normally looks like
   6249 	     this:
   6250 
   6251 	       lui    $gp,%hi(_gp_disp)
   6252 	       addiu  $gp,$gp,%lo(_gp_disp)
   6253 	       addu   $gp,$gp,$t9
   6254 
   6255 	     Here $t9 holds the address of the function being called,
   6256 	     as required by the MIPS ELF ABI.  The R_MIPS_LO16
   6257 	     relocation can easily overflow in this situation, but the
   6258 	     R_MIPS_HI16 relocation will handle the overflow.
   6259 	     Therefore, we consider this a bug in the MIPS ABI, and do
   6260 	     not check for overflow here.  */
   6261 	}
   6262       break;
   6263 
   6264     case R_MIPS_LITERAL:
   6265     case R_MICROMIPS_LITERAL:
   6266       /* Because we don't merge literal sections, we can handle this
   6267 	 just like R_MIPS_GPREL16.  In the long run, we should merge
   6268 	 shared literals, and then we will need to additional work
   6269 	 here.  */
   6270 
   6271       /* Fall through.  */
   6272 
   6273     case R_MIPS16_GPREL:
   6274       /* The R_MIPS16_GPREL performs the same calculation as
   6275 	 R_MIPS_GPREL16, but stores the relocated bits in a different
   6276 	 order.  We don't need to do anything special here; the
   6277 	 differences are handled in mips_elf_perform_relocation.  */
   6278     case R_MIPS_GPREL16:
   6279     case R_MICROMIPS_GPREL7_S2:
   6280     case R_MICROMIPS_GPREL16:
   6281       {
   6282 	int bits = howto->bitsize + howto->rightshift;
   6283 	/* Only sign-extend the addend if it was extracted from the
   6284 	   instruction.  If the addend was separate, leave it alone,
   6285 	   otherwise we may lose significant bits.  */
   6286 	if (howto->partial_inplace)
   6287 	  addend = _bfd_mips_elf_sign_extend (addend, bits);
   6288 	value = symbol + addend - gp;
   6289 	/* If the symbol was local, any earlier relocatable links will
   6290 	   have adjusted its addend with the gp offset, so compensate
   6291 	   for that now.  Don't do it for symbols forced local in this
   6292 	   link, though, since they won't have had the gp offset applied
   6293 	   to them before.  */
   6294 	if (was_local_p)
   6295 	  value += gp0;
   6296 	if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6297 	  overflowed_p = mips_elf_overflow_p (value, bits);
   6298       }
   6299       break;
   6300 
   6301     case R_MIPS16_GOT16:
   6302     case R_MIPS16_CALL16:
   6303     case R_MIPS_GOT16:
   6304     case R_MIPS_CALL16:
   6305     case R_MICROMIPS_GOT16:
   6306     case R_MICROMIPS_CALL16:
   6307       /* VxWorks does not have separate local and global semantics for
   6308 	 R_MIPS*_GOT16; every relocation evaluates to "G".  */
   6309       if (htab->root.target_os != is_vxworks && local_p)
   6310 	{
   6311 	  value = mips_elf_got16_entry (abfd, input_bfd, info,
   6312 					symbol + addend, !was_local_p);
   6313 	  if (value == MINUS_ONE)
   6314 	    return bfd_reloc_outofrange;
   6315 	  value
   6316 	    = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
   6317 	  overflowed_p = mips_elf_overflow_p (value, 16);
   6318 	  break;
   6319 	}
   6320 
   6321       /* Fall through.  */
   6322 
   6323     case R_MIPS_TLS_GD:
   6324     case R_MIPS_TLS_GOTTPREL:
   6325     case R_MIPS_TLS_LDM:
   6326     case R_MIPS_GOT_DISP:
   6327     case R_MIPS16_TLS_GD:
   6328     case R_MIPS16_TLS_GOTTPREL:
   6329     case R_MIPS16_TLS_LDM:
   6330     case R_MICROMIPS_TLS_GD:
   6331     case R_MICROMIPS_TLS_GOTTPREL:
   6332     case R_MICROMIPS_TLS_LDM:
   6333     case R_MICROMIPS_GOT_DISP:
   6334       value = g;
   6335       overflowed_p = mips_elf_overflow_p (value, 16);
   6336       break;
   6337 
   6338     case R_MIPS_GPREL32:
   6339       value = (addend + symbol + gp0 - gp);
   6340       if (!save_addend)
   6341 	value &= howto->dst_mask;
   6342       break;
   6343 
   6344     case R_MIPS_PC16:
   6345     case R_MIPS_GNU_REL16_S2:
   6346       if (howto->partial_inplace)
   6347 	addend = _bfd_mips_elf_sign_extend (addend, 18);
   6348 
   6349       /* No need to exclude weak undefined symbols here as they resolve
   6350 	 to 0 and never set `*cross_mode_jump_p', so this alignment check
   6351 	 will never trigger for them.  */
   6352       if (*cross_mode_jump_p
   6353 	  ? ((symbol + addend) & 3) != 1
   6354 	  : ((symbol + addend) & 3) != 0)
   6355 	return bfd_reloc_outofrange;
   6356 
   6357       value = symbol + addend - p;
   6358       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6359 	overflowed_p = mips_elf_overflow_p (value, 18);
   6360       value >>= howto->rightshift;
   6361       value &= howto->dst_mask;
   6362       break;
   6363 
   6364     case R_MIPS16_PC16_S1:
   6365       if (howto->partial_inplace)
   6366 	addend = _bfd_mips_elf_sign_extend (addend, 17);
   6367 
   6368       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6369 	  && (*cross_mode_jump_p
   6370 	      ? ((symbol + addend) & 3) != 0
   6371 	      : ((symbol + addend) & 1) == 0))
   6372 	return bfd_reloc_outofrange;
   6373 
   6374       value = symbol + addend - p;
   6375       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6376 	overflowed_p = mips_elf_overflow_p (value, 17);
   6377       value >>= howto->rightshift;
   6378       value &= howto->dst_mask;
   6379       break;
   6380 
   6381     case R_MIPS_PC21_S2:
   6382       if (howto->partial_inplace)
   6383 	addend = _bfd_mips_elf_sign_extend (addend, 23);
   6384 
   6385       if ((symbol + addend) & 3)
   6386 	return bfd_reloc_outofrange;
   6387 
   6388       value = symbol + addend - p;
   6389       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6390 	overflowed_p = mips_elf_overflow_p (value, 23);
   6391       value >>= howto->rightshift;
   6392       value &= howto->dst_mask;
   6393       break;
   6394 
   6395     case R_MIPS_PC26_S2:
   6396       if (howto->partial_inplace)
   6397 	addend = _bfd_mips_elf_sign_extend (addend, 28);
   6398 
   6399       if ((symbol + addend) & 3)
   6400 	return bfd_reloc_outofrange;
   6401 
   6402       value = symbol + addend - p;
   6403       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6404 	overflowed_p = mips_elf_overflow_p (value, 28);
   6405       value >>= howto->rightshift;
   6406       value &= howto->dst_mask;
   6407       break;
   6408 
   6409     case R_MIPS_PC18_S3:
   6410       if (howto->partial_inplace)
   6411 	addend = _bfd_mips_elf_sign_extend (addend, 21);
   6412 
   6413       if ((symbol + addend) & 7)
   6414 	return bfd_reloc_outofrange;
   6415 
   6416       value = symbol + addend - ((p | 7) ^ 7);
   6417       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6418 	overflowed_p = mips_elf_overflow_p (value, 21);
   6419       value >>= howto->rightshift;
   6420       value &= howto->dst_mask;
   6421       break;
   6422 
   6423     case R_MIPS_PC19_S2:
   6424       if (howto->partial_inplace)
   6425 	addend = _bfd_mips_elf_sign_extend (addend, 21);
   6426 
   6427       if ((symbol + addend) & 3)
   6428 	return bfd_reloc_outofrange;
   6429 
   6430       value = symbol + addend - p;
   6431       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6432 	overflowed_p = mips_elf_overflow_p (value, 21);
   6433       value >>= howto->rightshift;
   6434       value &= howto->dst_mask;
   6435       break;
   6436 
   6437     case R_MIPS_PCHI16:
   6438       value = mips_elf_high (symbol + addend - p);
   6439       value &= howto->dst_mask;
   6440       break;
   6441 
   6442     case R_MIPS_PCLO16:
   6443       if (howto->partial_inplace)
   6444 	addend = _bfd_mips_elf_sign_extend (addend, 16);
   6445       value = symbol + addend - p;
   6446       value &= howto->dst_mask;
   6447       break;
   6448 
   6449     case R_MICROMIPS_PC7_S1:
   6450       if (howto->partial_inplace)
   6451 	addend = _bfd_mips_elf_sign_extend (addend, 8);
   6452 
   6453       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6454 	  && (*cross_mode_jump_p
   6455 	      ? ((symbol + addend + 2) & 3) != 0
   6456 	      : ((symbol + addend + 2) & 1) == 0))
   6457 	return bfd_reloc_outofrange;
   6458 
   6459       value = symbol + addend - p;
   6460       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6461 	overflowed_p = mips_elf_overflow_p (value, 8);
   6462       value >>= howto->rightshift;
   6463       value &= howto->dst_mask;
   6464       break;
   6465 
   6466     case R_MICROMIPS_PC10_S1:
   6467       if (howto->partial_inplace)
   6468 	addend = _bfd_mips_elf_sign_extend (addend, 11);
   6469 
   6470       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6471 	  && (*cross_mode_jump_p
   6472 	      ? ((symbol + addend + 2) & 3) != 0
   6473 	      : ((symbol + addend + 2) & 1) == 0))
   6474 	return bfd_reloc_outofrange;
   6475 
   6476       value = symbol + addend - p;
   6477       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6478 	overflowed_p = mips_elf_overflow_p (value, 11);
   6479       value >>= howto->rightshift;
   6480       value &= howto->dst_mask;
   6481       break;
   6482 
   6483     case R_MICROMIPS_PC16_S1:
   6484       if (howto->partial_inplace)
   6485 	addend = _bfd_mips_elf_sign_extend (addend, 17);
   6486 
   6487       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6488 	  && (*cross_mode_jump_p
   6489 	      ? ((symbol + addend) & 3) != 0
   6490 	      : ((symbol + addend) & 1) == 0))
   6491 	return bfd_reloc_outofrange;
   6492 
   6493       value = symbol + addend - p;
   6494       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6495 	overflowed_p = mips_elf_overflow_p (value, 17);
   6496       value >>= howto->rightshift;
   6497       value &= howto->dst_mask;
   6498       break;
   6499 
   6500     case R_MICROMIPS_PC23_S2:
   6501       if (howto->partial_inplace)
   6502 	addend = _bfd_mips_elf_sign_extend (addend, 25);
   6503       value = symbol + addend - ((p | 3) ^ 3);
   6504       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6505 	overflowed_p = mips_elf_overflow_p (value, 25);
   6506       value >>= howto->rightshift;
   6507       value &= howto->dst_mask;
   6508       break;
   6509 
   6510     case R_MIPS_GOT_HI16:
   6511     case R_MIPS_CALL_HI16:
   6512     case R_MICROMIPS_GOT_HI16:
   6513     case R_MICROMIPS_CALL_HI16:
   6514       /* We're allowed to handle these two relocations identically.
   6515 	 The dynamic linker is allowed to handle the CALL relocations
   6516 	 differently by creating a lazy evaluation stub.  */
   6517       value = g;
   6518       value = mips_elf_high (value);
   6519       value &= howto->dst_mask;
   6520       break;
   6521 
   6522     case R_MIPS_GOT_LO16:
   6523     case R_MIPS_CALL_LO16:
   6524     case R_MICROMIPS_GOT_LO16:
   6525     case R_MICROMIPS_CALL_LO16:
   6526       value = g & howto->dst_mask;
   6527       break;
   6528 
   6529     case R_MIPS_GOT_PAGE:
   6530     case R_MICROMIPS_GOT_PAGE:
   6531       value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
   6532       if (value == MINUS_ONE)
   6533 	return bfd_reloc_outofrange;
   6534       value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
   6535       overflowed_p = mips_elf_overflow_p (value, 16);
   6536       break;
   6537 
   6538     case R_MIPS_GOT_OFST:
   6539     case R_MICROMIPS_GOT_OFST:
   6540       if (local_p)
   6541 	mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
   6542       else
   6543 	value = addend;
   6544       overflowed_p = mips_elf_overflow_p (value, 16);
   6545       break;
   6546 
   6547     case R_MIPS_SUB:
   6548     case R_MICROMIPS_SUB:
   6549       value = symbol - addend;
   6550       value &= howto->dst_mask;
   6551       break;
   6552 
   6553     case R_MIPS_HIGHER:
   6554     case R_MICROMIPS_HIGHER:
   6555       value = mips_elf_higher (addend + symbol);
   6556       value &= howto->dst_mask;
   6557       break;
   6558 
   6559     case R_MIPS_HIGHEST:
   6560     case R_MICROMIPS_HIGHEST:
   6561       value = mips_elf_highest (addend + symbol);
   6562       value &= howto->dst_mask;
   6563       break;
   6564 
   6565     case R_MIPS_SCN_DISP:
   6566     case R_MICROMIPS_SCN_DISP:
   6567       value = symbol + addend - sec->output_offset;
   6568       value &= howto->dst_mask;
   6569       break;
   6570 
   6571     case R_MIPS_JALR:
   6572     case R_MICROMIPS_JALR:
   6573       /* This relocation is only a hint.  In some cases, we optimize
   6574 	 it into a bal instruction.  But we don't try to optimize
   6575 	 when the symbol does not resolve locally.  */
   6576       if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
   6577 	return bfd_reloc_continue;
   6578       /* We can't optimize cross-mode jumps either.  */
   6579       if (*cross_mode_jump_p)
   6580 	return bfd_reloc_continue;
   6581       value = symbol + addend;
   6582       /* Neither we can non-instruction-aligned targets.  */
   6583       if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
   6584 	return bfd_reloc_continue;
   6585       break;
   6586 
   6587     case R_MIPS_PJUMP:
   6588     case R_MIPS_GNU_VTINHERIT:
   6589     case R_MIPS_GNU_VTENTRY:
   6590       /* We don't do anything with these at present.  */
   6591       return bfd_reloc_continue;
   6592 
   6593     default:
   6594       /* An unrecognized relocation type.  */
   6595       return bfd_reloc_notsupported;
   6596     }
   6597 
   6598   /* Store the VALUE for our caller.  */
   6599   *valuep = value;
   6600   return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
   6601 }
   6602 
   6603 /* It has been determined that the result of the RELOCATION is the
   6604    VALUE.  Use HOWTO to place VALUE into the output file at the
   6605    appropriate position.  The SECTION is the section to which the
   6606    relocation applies.
   6607    CROSS_MODE_JUMP_P is true if the relocation field
   6608    is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
   6609 
   6610    Returns FALSE if anything goes wrong.  */
   6611 
   6612 static bool
   6613 mips_elf_perform_relocation (struct bfd_link_info *info,
   6614 			     reloc_howto_type *howto,
   6615 			     const Elf_Internal_Rela *relocation,
   6616 			     bfd_vma value, bfd *input_bfd,
   6617 			     asection *input_section, bfd_byte *contents,
   6618 			     bool cross_mode_jump_p)
   6619 {
   6620   bfd_vma x;
   6621   bfd_byte *location;
   6622   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   6623 
   6624   /* Figure out where the relocation is occurring.  */
   6625   location = contents + relocation->r_offset;
   6626 
   6627   _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
   6628 
   6629   /* Obtain the current value.  */
   6630   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
   6631 
   6632   /* Clear the field we are setting.  */
   6633   x &= ~howto->dst_mask;
   6634 
   6635   /* Set the field.  */
   6636   x |= (value & howto->dst_mask);
   6637 
   6638   /* Detect incorrect JALX usage.  If required, turn JAL or BAL into JALX.  */
   6639   if (!cross_mode_jump_p && jal_reloc_p (r_type))
   6640     {
   6641       bfd_vma opcode = x >> 26;
   6642 
   6643       if (r_type == R_MIPS16_26 ? opcode == 0x7
   6644 	  : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
   6645 	  : opcode == 0x1d)
   6646 	{
   6647 	  info->callbacks->einfo
   6648 	    (_("%X%H: unsupported JALX to the same ISA mode\n"),
   6649 	     input_bfd, input_section, relocation->r_offset);
   6650 	  return true;
   6651 	}
   6652     }
   6653   if (cross_mode_jump_p && jal_reloc_p (r_type))
   6654     {
   6655       bool ok;
   6656       bfd_vma opcode = x >> 26;
   6657       bfd_vma jalx_opcode;
   6658 
   6659       /* Check to see if the opcode is already JAL or JALX.  */
   6660       if (r_type == R_MIPS16_26)
   6661 	{
   6662 	  ok = ((opcode == 0x6) || (opcode == 0x7));
   6663 	  jalx_opcode = 0x7;
   6664 	}
   6665       else if (r_type == R_MICROMIPS_26_S1)
   6666 	{
   6667 	  ok = ((opcode == 0x3d) || (opcode == 0x3c));
   6668 	  jalx_opcode = 0x3c;
   6669 	}
   6670       else
   6671 	{
   6672 	  ok = ((opcode == 0x3) || (opcode == 0x1d));
   6673 	  jalx_opcode = 0x1d;
   6674 	}
   6675 
   6676       /* If the opcode is not JAL or JALX, there's a problem.  We cannot
   6677 	 convert J or JALS to JALX.  */
   6678       if (!ok)
   6679 	{
   6680 	  info->callbacks->einfo
   6681 	    (_("%X%H: unsupported jump between ISA modes; "
   6682 	       "consider recompiling with interlinking enabled\n"),
   6683 	     input_bfd, input_section, relocation->r_offset);
   6684 	  return true;
   6685 	}
   6686 
   6687       /* Make this the JALX opcode.  */
   6688       x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
   6689     }
   6690   else if (cross_mode_jump_p && b_reloc_p (r_type))
   6691     {
   6692       bool ok = false;
   6693       bfd_vma opcode = x >> 16;
   6694       bfd_vma jalx_opcode = 0;
   6695       bfd_vma sign_bit = 0;
   6696       bfd_vma addr;
   6697       bfd_vma dest;
   6698 
   6699       if (r_type == R_MICROMIPS_PC16_S1)
   6700 	{
   6701 	  ok = opcode == 0x4060;
   6702 	  jalx_opcode = 0x3c;
   6703 	  sign_bit = 0x10000;
   6704 	  value <<= 1;
   6705 	}
   6706       else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
   6707 	{
   6708 	  ok = opcode == 0x411;
   6709 	  jalx_opcode = 0x1d;
   6710 	  sign_bit = 0x20000;
   6711 	  value <<= 2;
   6712 	}
   6713 
   6714       if (ok && !bfd_link_pic (info))
   6715 	{
   6716 	  addr = (input_section->output_section->vma
   6717 		  + input_section->output_offset
   6718 		  + relocation->r_offset
   6719 		  + 4);
   6720 	  dest = (addr
   6721 		  + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
   6722 
   6723 	  if ((addr >> 28) << 28 != (dest >> 28) << 28)
   6724 	    {
   6725 	      info->callbacks->einfo
   6726 		(_("%X%H: cannot convert branch between ISA modes "
   6727 		   "to JALX: relocation out of range\n"),
   6728 		 input_bfd, input_section, relocation->r_offset);
   6729 	      return true;
   6730 	    }
   6731 
   6732 	  /* Make this the JALX opcode.  */
   6733 	  x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
   6734 	}
   6735       else if (!mips_elf_hash_table (info)->ignore_branch_isa)
   6736 	{
   6737 	  info->callbacks->einfo
   6738 	    (_("%X%H: unsupported branch between ISA modes\n"),
   6739 	     input_bfd, input_section, relocation->r_offset);
   6740 	  return true;
   6741 	}
   6742     }
   6743 
   6744   /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
   6745      range.  */
   6746   if (!bfd_link_relocatable (info)
   6747       && !cross_mode_jump_p
   6748       && ((JAL_TO_BAL_P (input_bfd)
   6749 	   && r_type == R_MIPS_26
   6750 	   && (x >> 26) == 0x3)			/* jal addr */
   6751 	  || (JALR_TO_BAL_P (input_bfd)
   6752 	      && r_type == R_MIPS_JALR
   6753 	      && x == 0x0320f809)		/* jalr t9 */
   6754 	  || (JR_TO_B_P (input_bfd)
   6755 	      && r_type == R_MIPS_JALR
   6756 	      && (x & ~1) == 0x03200008)))	/* jr t9 / jalr zero, t9 */
   6757     {
   6758       bfd_vma addr;
   6759       bfd_vma dest;
   6760       bfd_signed_vma off;
   6761 
   6762       addr = (input_section->output_section->vma
   6763 	      + input_section->output_offset
   6764 	      + relocation->r_offset
   6765 	      + 4);
   6766       if (r_type == R_MIPS_26)
   6767 	dest = (value << 2) | ((addr >> 28) << 28);
   6768       else
   6769 	dest = value;
   6770       off = dest - addr;
   6771       if (off <= 0x1ffff && off >= -0x20000)
   6772 	{
   6773 	  if ((x & ~1) == 0x03200008)		/* jr t9 / jalr zero, t9 */
   6774 	    x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
   6775 	  else
   6776 	    x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
   6777 	}
   6778     }
   6779 
   6780   /* Put the value into the output.  */
   6781   mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
   6782 
   6783   _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
   6784 			       location);
   6785 
   6786   return true;
   6787 }
   6788 
   6789 /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
   6791    is the original relocation, which is now being transformed into a
   6792    dynamic relocation.  The ADDENDP is adjusted if necessary; the
   6793    caller should store the result in place of the original addend.  */
   6794 
   6795 static bool
   6796 mips_elf_create_dynamic_relocation (bfd *output_bfd,
   6797 				    struct bfd_link_info *info,
   6798 				    const Elf_Internal_Rela *rel,
   6799 				    struct mips_elf_link_hash_entry *h,
   6800 				    asection *sec, bfd_vma symbol,
   6801 				    bfd_vma *addendp, asection *input_section)
   6802 {
   6803   Elf_Internal_Rela outrel[3];
   6804   asection *sreloc;
   6805   bfd *dynobj;
   6806   int r_type;
   6807   long indx;
   6808   bool defined_p;
   6809   struct mips_elf_link_hash_table *htab;
   6810 
   6811   htab = mips_elf_hash_table (info);
   6812   BFD_ASSERT (htab != NULL);
   6813 
   6814   r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   6815   dynobj = elf_hash_table (info)->dynobj;
   6816   sreloc = mips_elf_rel_dyn_section (info, false);
   6817   BFD_ASSERT (sreloc != NULL);
   6818   BFD_ASSERT (sreloc->contents != NULL);
   6819   BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
   6820 	      < sreloc->size);
   6821 
   6822   outrel[0].r_offset =
   6823     _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
   6824   if (ABI_64_P (output_bfd))
   6825     {
   6826       outrel[1].r_offset =
   6827 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
   6828       outrel[2].r_offset =
   6829 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
   6830     }
   6831 
   6832   if (outrel[0].r_offset == MINUS_ONE)
   6833     /* The relocation field has been deleted.  */
   6834     return true;
   6835 
   6836   if (outrel[0].r_offset == MINUS_TWO)
   6837     {
   6838       /* The relocation field has been converted into a relative value of
   6839 	 some sort.  Functions like _bfd_elf_write_section_eh_frame expect
   6840 	 the field to be fully relocated, so add in the symbol's value.  */
   6841       *addendp += symbol;
   6842       return true;
   6843     }
   6844 
   6845   /* We must now calculate the dynamic symbol table index to use
   6846      in the relocation.  */
   6847   if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
   6848     {
   6849       BFD_ASSERT (htab->root.target_os == is_vxworks
   6850 		  || h->global_got_area != GGA_NONE);
   6851       indx = h->root.dynindx;
   6852       if (SGI_COMPAT (output_bfd))
   6853 	defined_p = h->root.def_regular;
   6854       else
   6855 	/* ??? glibc's ld.so just adds the final GOT entry to the
   6856 	   relocation field.  It therefore treats relocs against
   6857 	   defined symbols in the same way as relocs against
   6858 	   undefined symbols.  */
   6859 	defined_p = false;
   6860     }
   6861   else
   6862     {
   6863       if (sec != NULL && bfd_is_abs_section (sec))
   6864 	indx = 0;
   6865       else if (sec == NULL || sec->owner == NULL)
   6866 	{
   6867 	  BFD_ASSERT (0);
   6868 	  bfd_set_error (bfd_error_bad_value);
   6869 	  return false;
   6870 	}
   6871       else
   6872 	{
   6873 	  indx = elf_section_data (sec->output_section)->dynindx;
   6874 	  if (indx == 0)
   6875 	    {
   6876 	      asection *osec = htab->root.text_index_section;
   6877 	      indx = elf_section_data (osec)->dynindx;
   6878 	    }
   6879 	  if (indx == 0)
   6880 	    abort ();
   6881 	}
   6882 
   6883       /* Instead of generating a relocation using the section
   6884 	 symbol, we may as well make it a fully relative
   6885 	 relocation.  We want to avoid generating relocations to
   6886 	 local symbols because we used to generate them
   6887 	 incorrectly, without adding the original symbol value,
   6888 	 which is mandated by the ABI for section symbols.  In
   6889 	 order to give dynamic loaders and applications time to
   6890 	 phase out the incorrect use, we refrain from emitting
   6891 	 section-relative relocations.  It's not like they're
   6892 	 useful, after all.  This should be a bit more efficient
   6893 	 as well.  */
   6894       /* ??? Although this behavior is compatible with glibc's ld.so,
   6895 	 the ABI says that relocations against STN_UNDEF should have
   6896 	 a symbol value of 0.  Irix rld honors this, so relocations
   6897 	 against STN_UNDEF have no effect.  */
   6898       if (!SGI_COMPAT (output_bfd))
   6899 	indx = 0;
   6900       defined_p = true;
   6901     }
   6902 
   6903   /* If the relocation was previously an absolute relocation and
   6904      this symbol will not be referred to by the relocation, we must
   6905      adjust it by the value we give it in the dynamic symbol table.
   6906      Otherwise leave the job up to the dynamic linker.  */
   6907   if (defined_p && r_type != R_MIPS_REL32)
   6908     *addendp += symbol;
   6909 
   6910   if (htab->root.target_os == is_vxworks)
   6911     /* VxWorks uses non-relative relocations for this.  */
   6912     outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
   6913   else
   6914     /* The relocation is always an REL32 relocation because we don't
   6915        know where the shared library will wind up at load-time.  */
   6916     outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
   6917 				   R_MIPS_REL32);
   6918 
   6919   /* For strict adherence to the ABI specification, we should
   6920      generate a R_MIPS_64 relocation record by itself before the
   6921      _REL32/_64 record as well, such that the addend is read in as
   6922      a 64-bit value (REL32 is a 32-bit relocation, after all).
   6923      However, since none of the existing ELF64 MIPS dynamic
   6924      loaders seems to care, we don't waste space with these
   6925      artificial relocations.  If this turns out to not be true,
   6926      mips_elf_allocate_dynamic_relocation() should be tweaked so
   6927      as to make room for a pair of dynamic relocations per
   6928      invocation if ABI_64_P, and here we should generate an
   6929      additional relocation record with R_MIPS_64 by itself for a
   6930      NULL symbol before this relocation record.  */
   6931   outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
   6932 				 ABI_64_P (output_bfd)
   6933 				 ? R_MIPS_64
   6934 				 : R_MIPS_NONE);
   6935   outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
   6936 
   6937   /* Adjust the output offset of the relocation to reference the
   6938      correct location in the output file.  */
   6939   outrel[0].r_offset += (input_section->output_section->vma
   6940 			 + input_section->output_offset);
   6941   outrel[1].r_offset += (input_section->output_section->vma
   6942 			 + input_section->output_offset);
   6943   outrel[2].r_offset += (input_section->output_section->vma
   6944 			 + input_section->output_offset);
   6945 
   6946   /* Put the relocation back out.  We have to use the special
   6947      relocation outputter in the 64-bit case since the 64-bit
   6948      relocation format is non-standard.  */
   6949   if (ABI_64_P (output_bfd))
   6950     {
   6951       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
   6952 	(output_bfd, &outrel[0],
   6953 	 (sreloc->contents
   6954 	  + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
   6955     }
   6956   else if (htab->root.target_os == is_vxworks)
   6957     {
   6958       /* VxWorks uses RELA rather than REL dynamic relocations.  */
   6959       outrel[0].r_addend = *addendp;
   6960       bfd_elf32_swap_reloca_out
   6961 	(output_bfd, &outrel[0],
   6962 	 (sreloc->contents
   6963 	  + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
   6964     }
   6965   else
   6966     bfd_elf32_swap_reloc_out
   6967       (output_bfd, &outrel[0],
   6968        (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
   6969 
   6970   /* We've now added another relocation.  */
   6971   ++sreloc->reloc_count;
   6972 
   6973   /* Make sure the output section is writable.  The dynamic linker
   6974      will be writing to it.  */
   6975   elf_section_data (input_section->output_section)->this_hdr.sh_flags
   6976     |= SHF_WRITE;
   6977 
   6978   /* On IRIX5, make an entry of compact relocation info.  */
   6979   if (IRIX_COMPAT (output_bfd) == ict_irix5)
   6980     {
   6981       asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
   6982       bfd_byte *cr;
   6983 
   6984       if (scpt)
   6985 	{
   6986 	  Elf32_crinfo cptrel;
   6987 
   6988 	  mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
   6989 	  cptrel.vaddr = (rel->r_offset
   6990 			  + input_section->output_section->vma
   6991 			  + input_section->output_offset);
   6992 	  if (r_type == R_MIPS_REL32)
   6993 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
   6994 	  else
   6995 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
   6996 	  mips_elf_set_cr_dist2to (cptrel, 0);
   6997 	  cptrel.konst = *addendp;
   6998 
   6999 	  cr = (scpt->contents
   7000 		+ sizeof (Elf32_External_compact_rel));
   7001 	  mips_elf_set_cr_relvaddr (cptrel, 0);
   7002 	  bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
   7003 				     ((Elf32_External_crinfo *) cr
   7004 				      + scpt->reloc_count));
   7005 	  ++scpt->reloc_count;
   7006 	}
   7007     }
   7008 
   7009   /* If we've written this relocation for a readonly section,
   7010      we need to set DF_TEXTREL again, so that we do not delete the
   7011      DT_TEXTREL tag.  */
   7012   if (MIPS_ELF_READONLY_SECTION (input_section))
   7013     info->flags |= DF_TEXTREL;
   7014 
   7015   return true;
   7016 }
   7017 
   7018 /* Return the MACH for a MIPS e_flags value.  */
   7020 
   7021 unsigned long
   7022 _bfd_elf_mips_mach (flagword flags)
   7023 {
   7024   switch (flags & EF_MIPS_MACH)
   7025     {
   7026     case EF_MIPS_MACH_3900:
   7027       return bfd_mach_mips3900;
   7028 
   7029     case EF_MIPS_MACH_4010:
   7030       return bfd_mach_mips4010;
   7031 
   7032     case EF_MIPS_MACH_ALLEGREX:
   7033       return bfd_mach_mips_allegrex;
   7034 
   7035     case EF_MIPS_MACH_4100:
   7036       return bfd_mach_mips4100;
   7037 
   7038     case EF_MIPS_MACH_4111:
   7039       return bfd_mach_mips4111;
   7040 
   7041     case EF_MIPS_MACH_4120:
   7042       return bfd_mach_mips4120;
   7043 
   7044     case EF_MIPS_MACH_4650:
   7045       return bfd_mach_mips4650;
   7046 
   7047     case EF_MIPS_MACH_5400:
   7048       return bfd_mach_mips5400;
   7049 
   7050     case EF_MIPS_MACH_5500:
   7051       return bfd_mach_mips5500;
   7052 
   7053     case EF_MIPS_MACH_5900:
   7054       return bfd_mach_mips5900;
   7055 
   7056     case EF_MIPS_MACH_9000:
   7057       return bfd_mach_mips9000;
   7058 
   7059     case EF_MIPS_MACH_SB1:
   7060       return bfd_mach_mips_sb1;
   7061 
   7062     case EF_MIPS_MACH_LS2E:
   7063       return bfd_mach_mips_loongson_2e;
   7064 
   7065     case EF_MIPS_MACH_LS2F:
   7066       return bfd_mach_mips_loongson_2f;
   7067 
   7068     case EF_MIPS_MACH_GS464:
   7069       return bfd_mach_mips_gs464;
   7070 
   7071     case EF_MIPS_MACH_GS464E:
   7072       return bfd_mach_mips_gs464e;
   7073 
   7074     case EF_MIPS_MACH_GS264E:
   7075       return bfd_mach_mips_gs264e;
   7076 
   7077     case EF_MIPS_MACH_OCTEON3:
   7078       return bfd_mach_mips_octeon3;
   7079 
   7080     case EF_MIPS_MACH_OCTEON2:
   7081       return bfd_mach_mips_octeon2;
   7082 
   7083     case EF_MIPS_MACH_OCTEON:
   7084       return bfd_mach_mips_octeon;
   7085 
   7086     case EF_MIPS_MACH_XLR:
   7087       return bfd_mach_mips_xlr;
   7088 
   7089     case EF_MIPS_MACH_IAMR2:
   7090       return bfd_mach_mips_interaptiv_mr2;
   7091 
   7092     default:
   7093       switch (flags & EF_MIPS_ARCH)
   7094 	{
   7095 	default:
   7096 	case EF_MIPS_ARCH_1:
   7097 	  return bfd_mach_mips3000;
   7098 
   7099 	case EF_MIPS_ARCH_2:
   7100 	  return bfd_mach_mips6000;
   7101 
   7102 	case EF_MIPS_ARCH_3:
   7103 	  return bfd_mach_mips4000;
   7104 
   7105 	case EF_MIPS_ARCH_4:
   7106 	  return bfd_mach_mips8000;
   7107 
   7108 	case EF_MIPS_ARCH_5:
   7109 	  return bfd_mach_mips5;
   7110 
   7111 	case EF_MIPS_ARCH_32:
   7112 	  return bfd_mach_mipsisa32;
   7113 
   7114 	case EF_MIPS_ARCH_64:
   7115 	  return bfd_mach_mipsisa64;
   7116 
   7117 	case EF_MIPS_ARCH_32R2:
   7118 	  return bfd_mach_mipsisa32r2;
   7119 
   7120 	case EF_MIPS_ARCH_64R2:
   7121 	  return bfd_mach_mipsisa64r2;
   7122 
   7123 	case EF_MIPS_ARCH_32R6:
   7124 	  return bfd_mach_mipsisa32r6;
   7125 
   7126 	case EF_MIPS_ARCH_64R6:
   7127 	  return bfd_mach_mipsisa64r6;
   7128 	}
   7129     }
   7130 
   7131   return 0;
   7132 }
   7133 
   7134 /* Return printable name for ABI.  */
   7135 
   7136 static inline char *
   7137 elf_mips_abi_name (bfd *abfd)
   7138 {
   7139   flagword flags;
   7140 
   7141   flags = elf_elfheader (abfd)->e_flags;
   7142   switch (flags & EF_MIPS_ABI)
   7143     {
   7144     case 0:
   7145       if (ABI_N32_P (abfd))
   7146 	return "N32";
   7147       else if (ABI_64_P (abfd))
   7148 	return "64";
   7149       else
   7150 	return "none";
   7151     case EF_MIPS_ABI_O32:
   7152       return "O32";
   7153     case EF_MIPS_ABI_O64:
   7154       return "O64";
   7155     case EF_MIPS_ABI_EABI32:
   7156       return "EABI32";
   7157     case EF_MIPS_ABI_EABI64:
   7158       return "EABI64";
   7159     default:
   7160       return "unknown abi";
   7161     }
   7162 }
   7163 
   7164 /* MIPS ELF uses two common sections.  One is the usual one, and the
   7166    other is for small objects.  All the small objects are kept
   7167    together, and then referenced via the gp pointer, which yields
   7168    faster assembler code.  This is what we use for the small common
   7169    section.  This approach is copied from ecoff.c.  */
   7170 static asection mips_elf_scom_section;
   7171 static const asymbol mips_elf_scom_symbol =
   7172   GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
   7173 static asection mips_elf_scom_section =
   7174   BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
   7175 		    ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
   7176 
   7177 /* MIPS ELF also uses an acommon section, which represents an
   7178    allocated common symbol which may be overridden by a
   7179    definition in a shared library.  */
   7180 static asection mips_elf_acom_section;
   7181 static const asymbol mips_elf_acom_symbol =
   7182   GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
   7183 static asection mips_elf_acom_section =
   7184   BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
   7185 		    ".acommon", 0, SEC_ALLOC);
   7186 
   7187 /* This is used for both the 32-bit and the 64-bit ABI.  */
   7188 
   7189 void
   7190 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
   7191 {
   7192   elf_symbol_type *elfsym;
   7193 
   7194   /* Handle the special MIPS section numbers that a symbol may use.  */
   7195   elfsym = (elf_symbol_type *) asym;
   7196   switch (elfsym->internal_elf_sym.st_shndx)
   7197     {
   7198     case SHN_MIPS_ACOMMON:
   7199       /* This section is used in a dynamically linked executable file.
   7200 	 It is an allocated common section.  The dynamic linker can
   7201 	 either resolve these symbols to something in a shared
   7202 	 library, or it can just leave them here.  For our purposes,
   7203 	 we can consider these symbols to be in a new section.  */
   7204       asym->section = &mips_elf_acom_section;
   7205       break;
   7206 
   7207     case SHN_COMMON:
   7208       /* Common symbols less than the GP size are automatically
   7209 	 treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
   7210       if (asym->value > elf_gp_size (abfd)
   7211 	  || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
   7212 	  || IRIX_COMPAT (abfd) == ict_irix6
   7213 	  || strcmp (asym->name, "__gnu_lto_slim") == 0)
   7214 	break;
   7215       /* Fall through.  */
   7216     case SHN_MIPS_SCOMMON:
   7217       asym->section = &mips_elf_scom_section;
   7218       asym->value = elfsym->internal_elf_sym.st_size;
   7219       break;
   7220 
   7221     case SHN_MIPS_SUNDEFINED:
   7222       asym->section = bfd_und_section_ptr;
   7223       break;
   7224 
   7225     case SHN_MIPS_TEXT:
   7226       {
   7227 	asection *section = bfd_get_section_by_name (abfd, ".text");
   7228 
   7229 	if (section != NULL)
   7230 	  {
   7231 	    asym->section = section;
   7232 	    /* MIPS_TEXT is a bit special, the address is not an offset
   7233 	       to the base of the .text section.  So subtract the section
   7234 	       base address to make it an offset.  */
   7235 	    asym->value -= section->vma;
   7236 	  }
   7237       }
   7238       break;
   7239 
   7240     case SHN_MIPS_DATA:
   7241       {
   7242 	asection *section = bfd_get_section_by_name (abfd, ".data");
   7243 
   7244 	if (section != NULL)
   7245 	  {
   7246 	    asym->section = section;
   7247 	    /* MIPS_DATA is a bit special, the address is not an offset
   7248 	       to the base of the .data section.  So subtract the section
   7249 	       base address to make it an offset.  */
   7250 	    asym->value -= section->vma;
   7251 	  }
   7252       }
   7253       break;
   7254     }
   7255 
   7256   /* If this is an odd-valued function symbol, assume it's a MIPS16
   7257      or microMIPS one.  */
   7258   if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
   7259       && (asym->value & 1) != 0)
   7260     {
   7261       asym->value--;
   7262       if (MICROMIPS_P (abfd))
   7263 	elfsym->internal_elf_sym.st_other
   7264 	  = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
   7265       else
   7266 	elfsym->internal_elf_sym.st_other
   7267 	  = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
   7268     }
   7269 }
   7270 
   7271 /* Implement elf_backend_eh_frame_address_size.  This differs from
   7273    the default in the way it handles EABI64.
   7274 
   7275    EABI64 was originally specified as an LP64 ABI, and that is what
   7276    -mabi=eabi normally gives on a 64-bit target.  However, gcc has
   7277    historically accepted the combination of -mabi=eabi and -mlong32,
   7278    and this ILP32 variation has become semi-official over time.
   7279    Both forms use elf32 and have pointer-sized FDE addresses.
   7280 
   7281    If an EABI object was generated by GCC 4.0 or above, it will have
   7282    an empty .gcc_compiled_longXX section, where XX is the size of longs
   7283    in bits.  Unfortunately, ILP32 objects generated by earlier compilers
   7284    have no special marking to distinguish them from LP64 objects.
   7285 
   7286    We don't want users of the official LP64 ABI to be punished for the
   7287    existence of the ILP32 variant, but at the same time, we don't want
   7288    to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
   7289    We therefore take the following approach:
   7290 
   7291       - If ABFD contains a .gcc_compiled_longXX section, use it to
   7292 	determine the pointer size.
   7293 
   7294       - Otherwise check the type of the first relocation.  Assume that
   7295 	the LP64 ABI is being used if the relocation is of type R_MIPS_64.
   7296 
   7297       - Otherwise punt.
   7298 
   7299    The second check is enough to detect LP64 objects generated by pre-4.0
   7300    compilers because, in the kind of output generated by those compilers,
   7301    the first relocation will be associated with either a CIE personality
   7302    routine or an FDE start address.  Furthermore, the compilers never
   7303    used a special (non-pointer) encoding for this ABI.
   7304 
   7305    Checking the relocation type should also be safe because there is no
   7306    reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
   7307    did so.  */
   7308 
   7309 unsigned int
   7310 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
   7311 {
   7312   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   7313     return 8;
   7314   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
   7315     {
   7316       bool long32_p, long64_p;
   7317 
   7318       long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
   7319       long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
   7320       if (long32_p && long64_p)
   7321 	return 0;
   7322       if (long32_p)
   7323 	return 4;
   7324       if (long64_p)
   7325 	return 8;
   7326 
   7327       if (sec->reloc_count > 0)
   7328 	{
   7329 	  /* Load the relocations for this section.  */
   7330 	  Elf_Internal_Rela *internal_relocs =
   7331 	    _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, true);
   7332 	  if (internal_relocs == NULL)
   7333 	    return 0;
   7334 
   7335 	  unsigned int size = 0;
   7336 	  if (ELF32_R_TYPE (internal_relocs[0].r_info) == R_MIPS_64)
   7337 	    size = 8;
   7338 
   7339 	  if (elf_section_data (sec)->relocs != internal_relocs)
   7340 	    free (internal_relocs);
   7341 
   7342 	  return size;
   7343 	}
   7344 
   7345       return 0;
   7346     }
   7347   return 4;
   7348 }
   7349 
   7350 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
   7352    relocations against two unnamed section symbols to resolve to the
   7353    same address.  For example, if we have code like:
   7354 
   7355 	lw	$4,%got_disp(.data)($gp)
   7356 	lw	$25,%got_disp(.text)($gp)
   7357 	jalr	$25
   7358 
   7359    then the linker will resolve both relocations to .data and the program
   7360    will jump there rather than to .text.
   7361 
   7362    We can work around this problem by giving names to local section symbols.
   7363    This is also what the MIPSpro tools do.  */
   7364 
   7365 bool
   7366 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
   7367 {
   7368   return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
   7369 }
   7370 
   7371 /* Work over a section just before writing it out.  This routine is
   7373    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
   7374    sections that need the SHF_MIPS_GPREL flag by name; there has to be
   7375    a better way.  */
   7376 
   7377 bool
   7378 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
   7379 {
   7380   if (hdr->sh_type == SHT_MIPS_REGINFO
   7381       && hdr->sh_size > 0)
   7382     {
   7383       bfd_byte buf[4];
   7384 
   7385       BFD_ASSERT (hdr->contents == NULL);
   7386 
   7387       if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
   7388 	{
   7389 	  _bfd_error_handler
   7390 	    (_("%pB: incorrect `.reginfo' section size; "
   7391 	       "expected %" PRIu64 ", got %" PRIu64),
   7392 	     abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
   7393 	     (uint64_t) hdr->sh_size);
   7394 	  bfd_set_error (bfd_error_bad_value);
   7395 	  return false;
   7396 	}
   7397 
   7398       if (bfd_seek (abfd,
   7399 		    hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
   7400 		    SEEK_SET) != 0)
   7401 	return false;
   7402       H_PUT_32 (abfd, elf_gp (abfd), buf);
   7403       if (bfd_write (buf, 4, abfd) != 4)
   7404 	return false;
   7405     }
   7406 
   7407   if (hdr->sh_type == SHT_MIPS_OPTIONS
   7408       && hdr->bfd_section != NULL
   7409       && mips_elf_section_data (hdr->bfd_section) != NULL
   7410       && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
   7411     {
   7412       bfd_byte *contents, *l, *lend;
   7413 
   7414       /* We stored the section contents in the tdata field in the
   7415 	 set_section_contents routine.  We save the section contents
   7416 	 so that we don't have to read them again.
   7417 	 At this point we know that elf_gp is set, so we can look
   7418 	 through the section contents to see if there is an
   7419 	 ODK_REGINFO structure.  */
   7420 
   7421       contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
   7422       l = contents;
   7423       lend = contents + hdr->sh_size;
   7424       while (l + sizeof (Elf_External_Options) <= lend)
   7425 	{
   7426 	  Elf_Internal_Options intopt;
   7427 
   7428 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
   7429 					&intopt);
   7430 	  if (intopt.size < sizeof (Elf_External_Options))
   7431 	    {
   7432 	      _bfd_error_handler
   7433 		/* xgettext:c-format */
   7434 		(_("%pB: warning: bad `%s' option size %u smaller than"
   7435 		   " its header"),
   7436 		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
   7437 	      break;
   7438 	    }
   7439 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
   7440 	    {
   7441 	      bfd_byte buf[8];
   7442 
   7443 	      if (bfd_seek (abfd,
   7444 			    (hdr->sh_offset
   7445 			     + (l - contents)
   7446 			     + sizeof (Elf_External_Options)
   7447 			     + (sizeof (Elf64_External_RegInfo) - 8)),
   7448 			     SEEK_SET) != 0)
   7449 		return false;
   7450 	      H_PUT_64 (abfd, elf_gp (abfd), buf);
   7451 	      if (bfd_write (buf, 8, abfd) != 8)
   7452 		return false;
   7453 	    }
   7454 	  else if (intopt.kind == ODK_REGINFO)
   7455 	    {
   7456 	      bfd_byte buf[4];
   7457 
   7458 	      if (bfd_seek (abfd,
   7459 			    (hdr->sh_offset
   7460 			     + (l - contents)
   7461 			     + sizeof (Elf_External_Options)
   7462 			     + (sizeof (Elf32_External_RegInfo) - 4)),
   7463 			    SEEK_SET) != 0)
   7464 		return false;
   7465 	      H_PUT_32 (abfd, elf_gp (abfd), buf);
   7466 	      if (bfd_write (buf, 4, abfd) != 4)
   7467 		return false;
   7468 	    }
   7469 	  l += intopt.size;
   7470 	}
   7471     }
   7472 
   7473   if (hdr->bfd_section != NULL)
   7474     {
   7475       const char *name = bfd_section_name (hdr->bfd_section);
   7476 
   7477       /* .sbss is not handled specially here because the GNU/Linux
   7478 	 prelinker can convert .sbss from NOBITS to PROGBITS and
   7479 	 changing it back to NOBITS breaks the binary.  The entry in
   7480 	 _bfd_mips_elf_special_sections will ensure the correct flags
   7481 	 are set on .sbss if BFD creates it without reading it from an
   7482 	 input file, and without special handling here the flags set
   7483 	 on it in an input file will be followed.  */
   7484       if (strcmp (name, ".sdata") == 0
   7485 	  || strcmp (name, ".lit8") == 0
   7486 	  || strcmp (name, ".lit4") == 0)
   7487 	hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
   7488       else if (strcmp (name, ".srdata") == 0)
   7489 	hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
   7490       else if (strcmp (name, ".compact_rel") == 0)
   7491 	hdr->sh_flags = 0;
   7492       else if (strcmp (name, ".rtproc") == 0)
   7493 	{
   7494 	  if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
   7495 	    {
   7496 	      unsigned int adjust;
   7497 
   7498 	      adjust = hdr->sh_size % hdr->sh_addralign;
   7499 	      if (adjust != 0)
   7500 		hdr->sh_size += hdr->sh_addralign - adjust;
   7501 	    }
   7502 	}
   7503     }
   7504 
   7505   return true;
   7506 }
   7507 
   7508 /* Handle a MIPS specific section when reading an object file.  This
   7509    is called when elfcode.h finds a section with an unknown type.
   7510    This routine supports both the 32-bit and 64-bit ELF ABI.  */
   7511 
   7512 bool
   7513 _bfd_mips_elf_section_from_shdr (bfd *abfd,
   7514 				 Elf_Internal_Shdr *hdr,
   7515 				 const char *name,
   7516 				 int shindex)
   7517 {
   7518   flagword flags = 0;
   7519 
   7520   /* There ought to be a place to keep ELF backend specific flags, but
   7521      at the moment there isn't one.  We just keep track of the
   7522      sections by their name, instead.  Fortunately, the ABI gives
   7523      suggested names for all the MIPS specific sections, so we will
   7524      probably get away with this.  */
   7525   switch (hdr->sh_type)
   7526     {
   7527     case SHT_MIPS_LIBLIST:
   7528       if (strcmp (name, ".liblist") != 0)
   7529 	return false;
   7530       break;
   7531     case SHT_MIPS_MSYM:
   7532       if (strcmp (name, ".msym") != 0)
   7533 	return false;
   7534       break;
   7535     case SHT_MIPS_CONFLICT:
   7536       if (strcmp (name, ".conflict") != 0)
   7537 	return false;
   7538       break;
   7539     case SHT_MIPS_GPTAB:
   7540       if (! startswith (name, ".gptab."))
   7541 	return false;
   7542       break;
   7543     case SHT_MIPS_UCODE:
   7544       if (strcmp (name, ".ucode") != 0)
   7545 	return false;
   7546       break;
   7547     case SHT_MIPS_DEBUG:
   7548       if (strcmp (name, ".mdebug") != 0)
   7549 	return false;
   7550       flags = SEC_DEBUGGING;
   7551       break;
   7552     case SHT_MIPS_REGINFO:
   7553       if (strcmp (name, ".reginfo") != 0
   7554 	  || hdr->sh_size != sizeof (Elf32_External_RegInfo))
   7555 	return false;
   7556       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
   7557       break;
   7558     case SHT_MIPS_IFACE:
   7559       if (strcmp (name, ".MIPS.interfaces") != 0)
   7560 	return false;
   7561       break;
   7562     case SHT_MIPS_CONTENT:
   7563       if (! startswith (name, ".MIPS.content"))
   7564 	return false;
   7565       break;
   7566     case SHT_MIPS_OPTIONS:
   7567       if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
   7568 	return false;
   7569       break;
   7570     case SHT_MIPS_ABIFLAGS:
   7571       if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
   7572 	return false;
   7573       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
   7574       break;
   7575     case SHT_MIPS_DWARF:
   7576       if (! startswith (name, ".debug_")
   7577          && ! startswith (name, ".gnu.debuglto_.debug_")
   7578          && ! startswith (name, ".zdebug_")
   7579          && ! startswith (name, ".gnu.debuglto_.zdebug_"))
   7580 	return false;
   7581       break;
   7582     case SHT_MIPS_SYMBOL_LIB:
   7583       if (strcmp (name, ".MIPS.symlib") != 0)
   7584 	return false;
   7585       break;
   7586     case SHT_MIPS_EVENTS:
   7587       if (! startswith (name, ".MIPS.events")
   7588 	  && ! startswith (name, ".MIPS.post_rel"))
   7589 	return false;
   7590       break;
   7591     case SHT_MIPS_XHASH:
   7592       if (strcmp (name, ".MIPS.xhash") != 0)
   7593 	return false;
   7594     default:
   7595       break;
   7596     }
   7597 
   7598   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   7599     return false;
   7600 
   7601   if (hdr->sh_flags & SHF_MIPS_GPREL)
   7602     flags |= SEC_SMALL_DATA;
   7603 
   7604   if (flags)
   7605     {
   7606       if (!bfd_set_section_flags (hdr->bfd_section,
   7607 				  (bfd_section_flags (hdr->bfd_section)
   7608 				   | flags)))
   7609 	return false;
   7610     }
   7611 
   7612   if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
   7613     {
   7614       Elf_External_ABIFlags_v0 ext;
   7615 
   7616       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
   7617 				      &ext, 0, sizeof ext))
   7618 	return false;
   7619       bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
   7620 					&mips_elf_tdata (abfd)->abiflags);
   7621       if (mips_elf_tdata (abfd)->abiflags.version != 0)
   7622 	return false;
   7623       mips_elf_tdata (abfd)->abiflags_valid = true;
   7624     }
   7625 
   7626   /* FIXME: We should record sh_info for a .gptab section.  */
   7627 
   7628   /* For a .reginfo section, set the gp value in the tdata information
   7629      from the contents of this section.  We need the gp value while
   7630      processing relocs, so we just get it now.  The .reginfo section
   7631      is not used in the 64-bit MIPS ELF ABI.  */
   7632   if (hdr->sh_type == SHT_MIPS_REGINFO)
   7633     {
   7634       Elf32_External_RegInfo ext;
   7635       Elf32_RegInfo s;
   7636 
   7637       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
   7638 				      &ext, 0, sizeof ext))
   7639 	return false;
   7640       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
   7641       elf_gp (abfd) = s.ri_gp_value;
   7642     }
   7643 
   7644   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
   7645      set the gp value based on what we find.  We may see both
   7646      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
   7647      they should agree.  */
   7648   if (hdr->sh_type == SHT_MIPS_OPTIONS)
   7649     {
   7650       bfd_byte *contents, *l, *lend;
   7651 
   7652       if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
   7653 	{
   7654 	  free (contents);
   7655 	  return false;
   7656 	}
   7657       l = contents;
   7658       lend = contents + hdr->sh_size;
   7659       while (l + sizeof (Elf_External_Options) <= lend)
   7660 	{
   7661 	  Elf_Internal_Options intopt;
   7662 
   7663 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
   7664 					&intopt);
   7665 	  if (intopt.size < sizeof (Elf_External_Options))
   7666 	    {
   7667 	    bad_opt:
   7668 	      _bfd_error_handler
   7669 		/* xgettext:c-format */
   7670 		(_("%pB: warning: truncated `%s' option"),
   7671 		 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
   7672 	      break;
   7673 	    }
   7674 	  if (intopt.kind == ODK_REGINFO)
   7675 	    {
   7676 	      if (ABI_64_P (abfd))
   7677 		{
   7678 		  Elf64_Internal_RegInfo intreg;
   7679 		  size_t needed = (sizeof (Elf_External_Options)
   7680 				   + sizeof (Elf64_External_RegInfo));
   7681 		  if (intopt.size < needed || (size_t) (lend - l) < needed)
   7682 		    goto bad_opt;
   7683 		  bfd_mips_elf64_swap_reginfo_in
   7684 		    (abfd,
   7685 		     ((Elf64_External_RegInfo *)
   7686 		      (l + sizeof (Elf_External_Options))),
   7687 		     &intreg);
   7688 		  elf_gp (abfd) = intreg.ri_gp_value;
   7689 		}
   7690 	      else
   7691 		{
   7692 		  Elf32_RegInfo intreg;
   7693 		  size_t needed = (sizeof (Elf_External_Options)
   7694 				   + sizeof (Elf32_External_RegInfo));
   7695 		  if (intopt.size < needed || (size_t) (lend - l) < needed)
   7696 		    goto bad_opt;
   7697 		  bfd_mips_elf32_swap_reginfo_in
   7698 		    (abfd,
   7699 		     ((Elf32_External_RegInfo *)
   7700 		      (l + sizeof (Elf_External_Options))),
   7701 		     &intreg);
   7702 		  elf_gp (abfd) = intreg.ri_gp_value;
   7703 		}
   7704 	    }
   7705 	  l += intopt.size;
   7706 	}
   7707       free (contents);
   7708     }
   7709 
   7710   return true;
   7711 }
   7712 
   7713 /* Set the correct type for a MIPS ELF section.  We do this by the
   7714    section name, which is a hack, but ought to work.  This routine is
   7715    used by both the 32-bit and the 64-bit ABI.  */
   7716 
   7717 bool
   7718 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
   7719 {
   7720   const char *name = bfd_section_name (sec);
   7721 
   7722   if (strcmp (name, ".liblist") == 0)
   7723     {
   7724       hdr->sh_type = SHT_MIPS_LIBLIST;
   7725       hdr->sh_info = sec->size / sizeof (Elf32_Lib);
   7726       /* The sh_link field is set in final_write_processing.  */
   7727     }
   7728   else if (strcmp (name, ".conflict") == 0)
   7729     hdr->sh_type = SHT_MIPS_CONFLICT;
   7730   else if (startswith (name, ".gptab."))
   7731     {
   7732       hdr->sh_type = SHT_MIPS_GPTAB;
   7733       hdr->sh_entsize = sizeof (Elf32_External_gptab);
   7734       /* The sh_info field is set in final_write_processing.  */
   7735     }
   7736   else if (strcmp (name, ".ucode") == 0)
   7737     hdr->sh_type = SHT_MIPS_UCODE;
   7738   else if (strcmp (name, ".mdebug") == 0)
   7739     {
   7740       hdr->sh_type = SHT_MIPS_DEBUG;
   7741       /* In a shared object on IRIX 5.3, the .mdebug section has an
   7742 	 entsize of 0.  FIXME: Does this matter?  */
   7743       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
   7744 	hdr->sh_entsize = 0;
   7745       else
   7746 	hdr->sh_entsize = 1;
   7747     }
   7748   else if (strcmp (name, ".reginfo") == 0)
   7749     {
   7750       hdr->sh_type = SHT_MIPS_REGINFO;
   7751       /* In a shared object on IRIX 5.3, the .reginfo section has an
   7752 	 entsize of 0x18.  FIXME: Does this matter?  */
   7753       if (SGI_COMPAT (abfd))
   7754 	{
   7755 	  if ((abfd->flags & DYNAMIC) != 0)
   7756 	    hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
   7757 	  else
   7758 	    hdr->sh_entsize = 1;
   7759 	}
   7760       else
   7761 	hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
   7762     }
   7763   else if (SGI_COMPAT (abfd)
   7764 	   && (strcmp (name, ".hash") == 0
   7765 	       || strcmp (name, ".dynamic") == 0
   7766 	       || strcmp (name, ".dynstr") == 0))
   7767     {
   7768       if (SGI_COMPAT (abfd))
   7769 	hdr->sh_entsize = 0;
   7770 #if 0
   7771       /* This isn't how the IRIX6 linker behaves.  */
   7772       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
   7773 #endif
   7774     }
   7775   else if (strcmp (name, ".got") == 0
   7776 	   || strcmp (name, ".srdata") == 0
   7777 	   || strcmp (name, ".sdata") == 0
   7778 	   || strcmp (name, ".sbss") == 0
   7779 	   || strcmp (name, ".lit4") == 0
   7780 	   || strcmp (name, ".lit8") == 0)
   7781     hdr->sh_flags |= SHF_MIPS_GPREL;
   7782   else if (strcmp (name, ".MIPS.interfaces") == 0)
   7783     {
   7784       hdr->sh_type = SHT_MIPS_IFACE;
   7785       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7786     }
   7787   else if (startswith (name, ".MIPS.content"))
   7788     {
   7789       hdr->sh_type = SHT_MIPS_CONTENT;
   7790       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7791       /* The sh_info field is set in final_write_processing.  */
   7792     }
   7793   else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
   7794     {
   7795       hdr->sh_type = SHT_MIPS_OPTIONS;
   7796       hdr->sh_entsize = 1;
   7797       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7798     }
   7799   else if (startswith (name, ".MIPS.abiflags"))
   7800     {
   7801       hdr->sh_type = SHT_MIPS_ABIFLAGS;
   7802       hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
   7803     }
   7804   else if (startswith (name, ".debug_")
   7805 	   || startswith (name, ".gnu.debuglto_.debug_")
   7806 	   || startswith (name, ".zdebug_")
   7807 	   || startswith (name, ".gnu.debuglto_.zdebug_"))
   7808     {
   7809       hdr->sh_type = SHT_MIPS_DWARF;
   7810 
   7811       /* Irix facilities such as libexc expect a single .debug_frame
   7812 	 per executable, the system ones have NOSTRIP set and the linker
   7813 	 doesn't merge sections with different flags so ...  */
   7814       if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
   7815 	hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7816     }
   7817   else if (strcmp (name, ".MIPS.symlib") == 0)
   7818     {
   7819       hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
   7820       /* The sh_link and sh_info fields are set in
   7821 	 final_write_processing.  */
   7822     }
   7823   else if (startswith (name, ".MIPS.events")
   7824 	   || startswith (name, ".MIPS.post_rel"))
   7825     {
   7826       hdr->sh_type = SHT_MIPS_EVENTS;
   7827       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7828       /* The sh_link field is set in final_write_processing.  */
   7829     }
   7830   else if (strcmp (name, ".msym") == 0)
   7831     {
   7832       hdr->sh_type = SHT_MIPS_MSYM;
   7833       hdr->sh_flags |= SHF_ALLOC;
   7834       hdr->sh_entsize = 8;
   7835     }
   7836   else if (strcmp (name, ".MIPS.xhash") == 0)
   7837     {
   7838       hdr->sh_type = SHT_MIPS_XHASH;
   7839       hdr->sh_flags |= SHF_ALLOC;
   7840       hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
   7841     }
   7842 
   7843   /* The generic elf_fake_sections will set up REL_HDR using the default
   7844    kind of relocations.  We used to set up a second header for the
   7845    non-default kind of relocations here, but only NewABI would use
   7846    these, and the IRIX ld doesn't like resulting empty RELA sections.
   7847    Thus we create those header only on demand now.  */
   7848 
   7849   return true;
   7850 }
   7851 
   7852 /* Given a BFD section, try to locate the corresponding ELF section
   7853    index.  This is used by both the 32-bit and the 64-bit ABI.
   7854    Actually, it's not clear to me that the 64-bit ABI supports these,
   7855    but for non-PIC objects we will certainly want support for at least
   7856    the .scommon section.  */
   7857 
   7858 bool
   7859 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
   7860 					asection *sec, int *retval)
   7861 {
   7862   if (strcmp (bfd_section_name (sec), ".scommon") == 0)
   7863     {
   7864       *retval = SHN_MIPS_SCOMMON;
   7865       return true;
   7866     }
   7867   if (strcmp (bfd_section_name (sec), ".acommon") == 0)
   7868     {
   7869       *retval = SHN_MIPS_ACOMMON;
   7870       return true;
   7871     }
   7872   return false;
   7873 }
   7874 
   7875 /* Hook called by the linker routine which adds symbols from an object
   7877    file.  We must handle the special MIPS section numbers here.  */
   7878 
   7879 bool
   7880 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
   7881 			       Elf_Internal_Sym *sym, const char **namep,
   7882 			       flagword *flagsp ATTRIBUTE_UNUSED,
   7883 			       asection **secp, bfd_vma *valp)
   7884 {
   7885   if (SGI_COMPAT (abfd)
   7886       && (abfd->flags & DYNAMIC) != 0
   7887       && strcmp (*namep, "_rld_new_interface") == 0)
   7888     {
   7889       /* Skip IRIX5 rld entry name.  */
   7890       *namep = NULL;
   7891       return true;
   7892     }
   7893 
   7894   /* Shared objects may have a dynamic symbol '_gp_disp' defined as
   7895      a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
   7896      by setting a DT_NEEDED for the shared object.  Since _gp_disp is
   7897      a magic symbol resolved by the linker, we ignore this bogus definition
   7898      of _gp_disp.  New ABI objects do not suffer from this problem so this
   7899      is not done for them. */
   7900   if (!NEWABI_P(abfd)
   7901       && (sym->st_shndx == SHN_ABS)
   7902       && (strcmp (*namep, "_gp_disp") == 0))
   7903     {
   7904       *namep = NULL;
   7905       return true;
   7906     }
   7907 
   7908   switch (sym->st_shndx)
   7909     {
   7910     case SHN_COMMON:
   7911       /* Common symbols less than the GP size are automatically
   7912 	 treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
   7913       if (sym->st_size > elf_gp_size (abfd)
   7914 	  || ELF_ST_TYPE (sym->st_info) == STT_TLS
   7915 	  || IRIX_COMPAT (abfd) == ict_irix6
   7916 	  || strcmp (*namep, "__gnu_lto_slim") == 0)
   7917 	break;
   7918       /* Fall through.  */
   7919     case SHN_MIPS_SCOMMON:
   7920       *secp = bfd_make_section_old_way (abfd, ".scommon");
   7921       (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
   7922       *valp = sym->st_size;
   7923       break;
   7924 
   7925     case SHN_MIPS_TEXT:
   7926       /* This section is used in a shared object.  */
   7927       if (mips_elf_tdata (abfd)->elf_text_section == NULL)
   7928 	{
   7929 	  asymbol *elf_text_symbol;
   7930 	  asection *elf_text_section;
   7931 	  size_t amt = sizeof (asection);
   7932 
   7933 	  elf_text_section = bfd_zalloc (abfd, amt);
   7934 	  if (elf_text_section == NULL)
   7935 	    return false;
   7936 
   7937 	  amt = sizeof (asymbol);
   7938 	  elf_text_symbol = bfd_zalloc (abfd, amt);
   7939 	  if (elf_text_symbol == NULL)
   7940 	    return false;
   7941 
   7942 	  /* Initialize the section.  */
   7943 
   7944 	  mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
   7945 	  mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
   7946 
   7947 	  elf_text_section->symbol = elf_text_symbol;
   7948 	  elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
   7949 
   7950 	  elf_text_section->name = ".text";
   7951 	  elf_text_section->flags = SEC_NO_FLAGS;
   7952 	  elf_text_section->output_section = NULL;
   7953 	  elf_text_section->owner = abfd;
   7954 	  elf_text_symbol->name = ".text";
   7955 	  elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
   7956 	  elf_text_symbol->section = elf_text_section;
   7957 	}
   7958       /* This code used to do *secp = bfd_und_section_ptr if
   7959 	 bfd_link_pic (info).  I don't know why, and that doesn't make sense,
   7960 	 so I took it out.  */
   7961       *secp = mips_elf_tdata (abfd)->elf_text_section;
   7962       break;
   7963 
   7964     case SHN_MIPS_ACOMMON:
   7965       /* Fall through. XXX Can we treat this as allocated data?  */
   7966     case SHN_MIPS_DATA:
   7967       /* This section is used in a shared object.  */
   7968       if (mips_elf_tdata (abfd)->elf_data_section == NULL)
   7969 	{
   7970 	  asymbol *elf_data_symbol;
   7971 	  asection *elf_data_section;
   7972 	  size_t amt = sizeof (asection);
   7973 
   7974 	  elf_data_section = bfd_zalloc (abfd, amt);
   7975 	  if (elf_data_section == NULL)
   7976 	    return false;
   7977 
   7978 	  amt = sizeof (asymbol);
   7979 	  elf_data_symbol = bfd_zalloc (abfd, amt);
   7980 	  if (elf_data_symbol == NULL)
   7981 	    return false;
   7982 
   7983 	  /* Initialize the section.  */
   7984 
   7985 	  mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
   7986 	  mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
   7987 
   7988 	  elf_data_section->symbol = elf_data_symbol;
   7989 	  elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
   7990 
   7991 	  elf_data_section->name = ".data";
   7992 	  elf_data_section->flags = SEC_NO_FLAGS;
   7993 	  elf_data_section->output_section = NULL;
   7994 	  elf_data_section->owner = abfd;
   7995 	  elf_data_symbol->name = ".data";
   7996 	  elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
   7997 	  elf_data_symbol->section = elf_data_section;
   7998 	}
   7999       /* This code used to do *secp = bfd_und_section_ptr if
   8000 	 bfd_link_pic (info).  I don't know why, and that doesn't make sense,
   8001 	 so I took it out.  */
   8002       *secp = mips_elf_tdata (abfd)->elf_data_section;
   8003       break;
   8004 
   8005     case SHN_MIPS_SUNDEFINED:
   8006       *secp = bfd_und_section_ptr;
   8007       break;
   8008     }
   8009 
   8010   if (SGI_COMPAT (abfd)
   8011       && ! bfd_link_pic (info)
   8012       && info->output_bfd->xvec == abfd->xvec
   8013       && strcmp (*namep, "__rld_obj_head") == 0)
   8014     {
   8015       struct elf_link_hash_entry *h;
   8016       struct bfd_link_hash_entry *bh;
   8017 
   8018       /* Mark __rld_obj_head as dynamic.  */
   8019       bh = NULL;
   8020       if (! (_bfd_generic_link_add_one_symbol
   8021 	     (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
   8022 	      get_elf_backend_data (abfd)->collect, &bh)))
   8023 	return false;
   8024 
   8025       h = (struct elf_link_hash_entry *) bh;
   8026       h->non_elf = 0;
   8027       h->def_regular = 1;
   8028       h->type = STT_OBJECT;
   8029 
   8030       if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8031 	return false;
   8032 
   8033       mips_elf_hash_table (info)->use_rld_obj_head = true;
   8034       mips_elf_hash_table (info)->rld_symbol = h;
   8035     }
   8036 
   8037   /* If this is a mips16 text symbol, add 1 to the value to make it
   8038      odd.  This will cause something like .word SYM to come up with
   8039      the right value when it is loaded into the PC.  */
   8040   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   8041     ++*valp;
   8042 
   8043   return true;
   8044 }
   8045 
   8046 /* This hook function is called before the linker writes out a global
   8047    symbol.  We mark symbols as small common if appropriate.  This is
   8048    also where we undo the increment of the value for a mips16 symbol.  */
   8049 
   8050 int
   8051 _bfd_mips_elf_link_output_symbol_hook
   8052   (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   8053    const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
   8054    asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
   8055 {
   8056   /* If we see a common symbol, which implies a relocatable link, then
   8057      if a symbol was small common in an input file, mark it as small
   8058      common in the output file.  */
   8059   if (sym->st_shndx == SHN_COMMON
   8060       && strcmp (input_sec->name, ".scommon") == 0)
   8061     sym->st_shndx = SHN_MIPS_SCOMMON;
   8062 
   8063   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   8064     sym->st_value &= ~1;
   8065 
   8066   return 1;
   8067 }
   8068 
   8069 /* Functions for the dynamic linker.  */
   8071 
   8072 /* Create dynamic sections when linking against a dynamic object.  */
   8073 
   8074 bool
   8075 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   8076 {
   8077   struct elf_link_hash_entry *h;
   8078   struct bfd_link_hash_entry *bh;
   8079   flagword flags;
   8080   register asection *s;
   8081   const char * const *namep;
   8082   struct mips_elf_link_hash_table *htab;
   8083 
   8084   htab = mips_elf_hash_table (info);
   8085   BFD_ASSERT (htab != NULL);
   8086 
   8087   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   8088 	   | SEC_LINKER_CREATED | SEC_READONLY);
   8089 
   8090   /* The psABI requires a read-only .dynamic section, but the VxWorks
   8091      EABI doesn't.  */
   8092   if (htab->root.target_os != is_vxworks)
   8093     {
   8094       s = bfd_get_linker_section (abfd, ".dynamic");
   8095       if (s != NULL)
   8096 	{
   8097 	  if (!bfd_set_section_flags (s, flags))
   8098 	    return false;
   8099 	}
   8100     }
   8101 
   8102   /* We need to create .got section.  */
   8103   if (!mips_elf_create_got_section (abfd, info))
   8104     return false;
   8105 
   8106   if (! mips_elf_rel_dyn_section (info, true))
   8107     return false;
   8108 
   8109   /* Create .stub section.  */
   8110   s = bfd_make_section_anyway_with_flags (abfd,
   8111 					  MIPS_ELF_STUB_SECTION_NAME (abfd),
   8112 					  flags | SEC_CODE);
   8113   if (s == NULL
   8114       || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   8115     return false;
   8116   htab->sstubs = s;
   8117 
   8118   if (!mips_elf_hash_table (info)->use_rld_obj_head
   8119       && bfd_link_executable (info)
   8120       && bfd_get_linker_section (abfd, ".rld_map") == NULL)
   8121     {
   8122       s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
   8123 					      flags &~ (flagword) SEC_READONLY);
   8124       if (s == NULL
   8125 	  || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   8126 	return false;
   8127     }
   8128 
   8129   /* Create .MIPS.xhash section.  */
   8130   if (info->emit_gnu_hash)
   8131     s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
   8132 					    flags | SEC_READONLY);
   8133 
   8134   /* On IRIX5, we adjust add some additional symbols and change the
   8135      alignments of several sections.  There is no ABI documentation
   8136      indicating that this is necessary on IRIX6, nor any evidence that
   8137      the linker takes such action.  */
   8138   if (IRIX_COMPAT (abfd) == ict_irix5)
   8139     {
   8140       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
   8141 	{
   8142 	  bh = NULL;
   8143 	  if (! (_bfd_generic_link_add_one_symbol
   8144 		 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
   8145 		  NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
   8146 	    return false;
   8147 
   8148 	  h = (struct elf_link_hash_entry *) bh;
   8149 	  h->mark = 1;
   8150 	  h->non_elf = 0;
   8151 	  h->def_regular = 1;
   8152 	  h->type = STT_SECTION;
   8153 
   8154 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8155 	    return false;
   8156 	}
   8157 
   8158       /* We need to create a .compact_rel section.  */
   8159       if (SGI_COMPAT (abfd))
   8160 	{
   8161 	  if (!mips_elf_create_compact_rel_section (abfd, info))
   8162 	    return false;
   8163 	}
   8164 
   8165       /* Change alignments of some sections.  */
   8166       s = bfd_get_linker_section (abfd, ".hash");
   8167       if (s != NULL)
   8168 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   8169 
   8170       s = bfd_get_linker_section (abfd, ".dynsym");
   8171       if (s != NULL)
   8172 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   8173 
   8174       s = bfd_get_linker_section (abfd, ".dynstr");
   8175       if (s != NULL)
   8176 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   8177 
   8178       /* ??? */
   8179       s = bfd_get_section_by_name (abfd, ".reginfo");
   8180       if (s != NULL)
   8181 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   8182 
   8183       s = bfd_get_linker_section (abfd, ".dynamic");
   8184       if (s != NULL)
   8185 	bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   8186     }
   8187 
   8188   if (bfd_link_executable (info))
   8189     {
   8190       const char *name;
   8191 
   8192       name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
   8193       bh = NULL;
   8194       if (!(_bfd_generic_link_add_one_symbol
   8195 	    (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
   8196 	     NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
   8197 	return false;
   8198 
   8199       h = (struct elf_link_hash_entry *) bh;
   8200       h->non_elf = 0;
   8201       h->def_regular = 1;
   8202       h->type = STT_SECTION;
   8203 
   8204       if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8205 	return false;
   8206 
   8207       if (! mips_elf_hash_table (info)->use_rld_obj_head)
   8208 	{
   8209 	  /* __rld_map is a four byte word located in the .data section
   8210 	     and is filled in by the rtld to contain a pointer to
   8211 	     the _r_debug structure. Its symbol value will be set in
   8212 	     _bfd_mips_elf_finish_dynamic_symbol.  */
   8213 	  s = bfd_get_linker_section (abfd, ".rld_map");
   8214 	  BFD_ASSERT (s != NULL);
   8215 
   8216 	  name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
   8217 	  bh = NULL;
   8218 	  if (!(_bfd_generic_link_add_one_symbol
   8219 		(info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
   8220 		 get_elf_backend_data (abfd)->collect, &bh)))
   8221 	    return false;
   8222 
   8223 	  h = (struct elf_link_hash_entry *) bh;
   8224 	  h->non_elf = 0;
   8225 	  h->def_regular = 1;
   8226 	  h->type = STT_OBJECT;
   8227 
   8228 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8229 	    return false;
   8230 	  mips_elf_hash_table (info)->rld_symbol = h;
   8231 	}
   8232     }
   8233 
   8234   /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
   8235      Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol.  */
   8236   if (!_bfd_elf_create_dynamic_sections (abfd, info))
   8237     return false;
   8238 
   8239   /* Do the usual VxWorks handling.  */
   8240   if (htab->root.target_os == is_vxworks
   8241       && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
   8242     return false;
   8243 
   8244   return true;
   8245 }
   8246 
   8247 /* Return true if relocation REL against section SEC is a REL rather than
   8249    RELA relocation.  RELOCS is the first relocation in the section and
   8250    ABFD is the bfd that contains SEC.  */
   8251 
   8252 static bool
   8253 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
   8254 			   const Elf_Internal_Rela *relocs,
   8255 			   const Elf_Internal_Rela *rel)
   8256 {
   8257   Elf_Internal_Shdr *rel_hdr;
   8258   const struct elf_backend_data *bed;
   8259 
   8260   /* To determine which flavor of relocation this is, we depend on the
   8261      fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
   8262   rel_hdr = elf_section_data (sec)->rel.hdr;
   8263   if (rel_hdr == NULL)
   8264     return false;
   8265   bed = get_elf_backend_data (abfd);
   8266   return ((size_t) (rel - relocs)
   8267 	  < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
   8268 }
   8269 
   8270 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
   8271    HOWTO is the relocation's howto and CONTENTS points to the contents
   8272    of the section that REL is against.  */
   8273 
   8274 static bfd_vma
   8275 mips_elf_read_rel_addend (bfd *abfd, asection *sec,
   8276 			  const Elf_Internal_Rela *rel,
   8277 			  reloc_howto_type *howto, bfd_byte *contents)
   8278 {
   8279   bfd_byte *location;
   8280   unsigned int r_type;
   8281   bfd_vma addend;
   8282   bfd_vma bytes;
   8283 
   8284   if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
   8285     return 0;
   8286 
   8287   r_type = ELF_R_TYPE (abfd, rel->r_info);
   8288   location = contents + rel->r_offset;
   8289 
   8290   /* Get the addend, which is stored in the input file.  */
   8291   _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
   8292   bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
   8293   _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
   8294 
   8295   addend = bytes & howto->src_mask;
   8296 
   8297   /* Shift is 2, unusually, for microMIPS JALX.  Adjust the addend
   8298      accordingly.  */
   8299   if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
   8300     addend <<= 1;
   8301 
   8302   return addend;
   8303 }
   8304 
   8305 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
   8306    and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
   8307    and update *ADDEND with the final addend.  Return true on success
   8308    or false if the LO16 could not be found.  RELEND is the exclusive
   8309    upper bound on the relocations for REL's section.  */
   8310 
   8311 static bool
   8312 mips_elf_add_lo16_rel_addend (bfd *abfd,
   8313 			      asection *sec,
   8314 			      const Elf_Internal_Rela *rel,
   8315 			      const Elf_Internal_Rela *relend,
   8316 			      bfd_byte *contents, bfd_vma *addend)
   8317 {
   8318   unsigned int r_type, lo16_type;
   8319   const Elf_Internal_Rela *lo16_relocation;
   8320   reloc_howto_type *lo16_howto;
   8321   bfd_vma l;
   8322 
   8323   r_type = ELF_R_TYPE (abfd, rel->r_info);
   8324   if (mips16_reloc_p (r_type))
   8325     lo16_type = R_MIPS16_LO16;
   8326   else if (micromips_reloc_p (r_type))
   8327     lo16_type = R_MICROMIPS_LO16;
   8328   else if (r_type == R_MIPS_PCHI16)
   8329     lo16_type = R_MIPS_PCLO16;
   8330   else
   8331     lo16_type = R_MIPS_LO16;
   8332 
   8333   /* The combined value is the sum of the HI16 addend, left-shifted by
   8334      sixteen bits, and the LO16 addend, sign extended.  (Usually, the
   8335      code does a `lui' of the HI16 value, and then an `addiu' of the
   8336      LO16 value.)
   8337 
   8338      Scan ahead to find a matching LO16 relocation.
   8339 
   8340      According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
   8341      be immediately following.  However, for the IRIX6 ABI, the next
   8342      relocation may be a composed relocation consisting of several
   8343      relocations for the same address.  In that case, the R_MIPS_LO16
   8344      relocation may occur as one of these.  We permit a similar
   8345      extension in general, as that is useful for GCC.
   8346 
   8347      In some cases GCC dead code elimination removes the LO16 but keeps
   8348      the corresponding HI16.  This is strictly speaking a violation of
   8349      the ABI but not immediately harmful.  */
   8350   lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
   8351   if (lo16_relocation == NULL)
   8352     return false;
   8353 
   8354   /* Obtain the addend kept there.  */
   8355   lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
   8356   l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
   8357 				contents);
   8358 
   8359   l <<= lo16_howto->rightshift;
   8360   l = _bfd_mips_elf_sign_extend (l, 16);
   8361 
   8362   *addend <<= 16;
   8363   *addend += l;
   8364   return true;
   8365 }
   8366 
   8367 /* Try to read the contents of section SEC in bfd ABFD.  Return true and
   8368    store the contents in *CONTENTS on success.  Assume that *CONTENTS
   8369    already holds the contents if it is nonull on entry.  */
   8370 
   8371 static bool
   8372 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
   8373 {
   8374   if (*contents)
   8375     return true;
   8376 
   8377   /* Get cached copy if it exists.  */
   8378   if (elf_section_data (sec)->this_hdr.contents != NULL)
   8379     {
   8380       *contents = elf_section_data (sec)->this_hdr.contents;
   8381       return true;
   8382     }
   8383 
   8384   return bfd_malloc_and_get_section (abfd, sec, contents);
   8385 }
   8386 
   8387 /* Make a new PLT record to keep internal data.  */
   8388 
   8389 static struct plt_entry *
   8390 mips_elf_make_plt_record (bfd *abfd)
   8391 {
   8392   struct plt_entry *entry;
   8393 
   8394   entry = bfd_zalloc (abfd, sizeof (*entry));
   8395   if (entry == NULL)
   8396     return NULL;
   8397 
   8398   entry->stub_offset = MINUS_ONE;
   8399   entry->mips_offset = MINUS_ONE;
   8400   entry->comp_offset = MINUS_ONE;
   8401   entry->gotplt_index = MINUS_ONE;
   8402   return entry;
   8403 }
   8404 
   8405 /* Define the special `__gnu_absolute_zero' symbol.  We only need this
   8406    for PIC code, as otherwise there is no load-time relocation involved
   8407    and local GOT entries whose value is zero at static link time will
   8408    retain their value at load time.  */
   8409 
   8410 static bool
   8411 mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
   8412 			       struct mips_elf_link_hash_table *htab,
   8413 			       unsigned int r_type)
   8414 {
   8415   union
   8416     {
   8417       struct elf_link_hash_entry *eh;
   8418       struct bfd_link_hash_entry *bh;
   8419     }
   8420   hzero;
   8421 
   8422   BFD_ASSERT (!htab->use_absolute_zero);
   8423   BFD_ASSERT (bfd_link_pic (info));
   8424 
   8425   hzero.bh = NULL;
   8426   if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
   8427 					 BSF_GLOBAL, bfd_abs_section_ptr, 0,
   8428 					 NULL, false, false, &hzero.bh))
   8429     return false;
   8430 
   8431   BFD_ASSERT (hzero.bh != NULL);
   8432   hzero.eh->size = 0;
   8433   hzero.eh->type = STT_NOTYPE;
   8434   hzero.eh->other = STV_PROTECTED;
   8435   hzero.eh->def_regular = 1;
   8436   hzero.eh->non_elf = 0;
   8437 
   8438   if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
   8439     return false;
   8440 
   8441   htab->use_absolute_zero = true;
   8442 
   8443   return true;
   8444 }
   8445 
   8446 /* Look through the relocs for a section during the first phase, and
   8447    allocate space in the global offset table and record the need for
   8448    standard MIPS and compressed procedure linkage table entries.  */
   8449 
   8450 bool
   8451 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   8452 			    asection *sec, const Elf_Internal_Rela *relocs)
   8453 {
   8454   const char *name;
   8455   bfd *dynobj;
   8456   Elf_Internal_Shdr *symtab_hdr;
   8457   struct elf_link_hash_entry **sym_hashes;
   8458   size_t extsymoff;
   8459   const Elf_Internal_Rela *rel;
   8460   const Elf_Internal_Rela *rel_end;
   8461   asection *sreloc;
   8462   const struct elf_backend_data *bed;
   8463   struct mips_elf_link_hash_table *htab;
   8464   bfd_byte *contents;
   8465   bfd_vma addend;
   8466   reloc_howto_type *howto;
   8467 
   8468   if (bfd_link_relocatable (info))
   8469     return true;
   8470 
   8471   htab = mips_elf_hash_table (info);
   8472   BFD_ASSERT (htab != NULL);
   8473 
   8474   dynobj = elf_hash_table (info)->dynobj;
   8475   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8476   sym_hashes = elf_sym_hashes (abfd);
   8477   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
   8478 
   8479   bed = get_elf_backend_data (abfd);
   8480   rel_end = relocs + sec->reloc_count;
   8481 
   8482   /* Check for the mips16 stub sections.  */
   8483 
   8484   name = bfd_section_name (sec);
   8485   if (FN_STUB_P (name))
   8486     {
   8487       unsigned long r_symndx;
   8488 
   8489       /* Look at the relocation information to figure out which symbol
   8490 	 this is for.  */
   8491 
   8492       r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
   8493       if (r_symndx == 0)
   8494 	{
   8495 	  _bfd_error_handler
   8496 	    /* xgettext:c-format */
   8497 	    (_("%pB: warning: cannot determine the target function for"
   8498 	       " stub section `%s'"),
   8499 	     abfd, name);
   8500 	  bfd_set_error (bfd_error_bad_value);
   8501 	  return false;
   8502 	}
   8503 
   8504       if (r_symndx < extsymoff
   8505 	  || sym_hashes[r_symndx - extsymoff] == NULL)
   8506 	{
   8507 	  asection *o;
   8508 
   8509 	  /* This stub is for a local symbol.  This stub will only be
   8510 	     needed if there is some relocation in this BFD, other
   8511 	     than a 16 bit function call, which refers to this symbol.  */
   8512 	  for (o = abfd->sections; o != NULL; o = o->next)
   8513 	    {
   8514 	      Elf_Internal_Rela *sec_relocs;
   8515 	      const Elf_Internal_Rela *r, *rend;
   8516 
   8517 	      /* We can ignore stub sections when looking for relocs.  */
   8518 	      if ((o->flags & SEC_RELOC) == 0
   8519 		  || o->reloc_count == 0
   8520 		  || section_allows_mips16_refs_p (o))
   8521 		continue;
   8522 
   8523 	      sec_relocs
   8524 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   8525 					     info->keep_memory);
   8526 	      if (sec_relocs == NULL)
   8527 		return false;
   8528 
   8529 	      rend = sec_relocs + o->reloc_count;
   8530 	      for (r = sec_relocs; r < rend; r++)
   8531 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
   8532 		    && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
   8533 		  break;
   8534 
   8535 	      if (elf_section_data (o)->relocs != sec_relocs)
   8536 		free (sec_relocs);
   8537 
   8538 	      if (r < rend)
   8539 		break;
   8540 	    }
   8541 
   8542 	  if (o == NULL)
   8543 	    {
   8544 	      /* There is no non-call reloc for this stub, so we do
   8545 		 not need it.  Since this function is called before
   8546 		 the linker maps input sections to output sections, we
   8547 		 can easily discard it by setting the SEC_EXCLUDE
   8548 		 flag.  */
   8549 	      sec->flags |= SEC_EXCLUDE;
   8550 	      return true;
   8551 	    }
   8552 
   8553 	  /* Record this stub in an array of local symbol stubs for
   8554 	     this BFD.  */
   8555 	  if (mips_elf_tdata (abfd)->local_stubs == NULL)
   8556 	    {
   8557 	      unsigned long symcount;
   8558 	      asection **n;
   8559 	      bfd_size_type amt;
   8560 
   8561 	      if (elf_bad_symtab (abfd))
   8562 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
   8563 	      else
   8564 		symcount = symtab_hdr->sh_info;
   8565 	      amt = symcount * sizeof (asection *);
   8566 	      n = bfd_zalloc (abfd, amt);
   8567 	      if (n == NULL)
   8568 		return false;
   8569 	      mips_elf_tdata (abfd)->local_stubs = n;
   8570 	    }
   8571 
   8572 	  sec->flags |= SEC_KEEP;
   8573 	  mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
   8574 
   8575 	  /* We don't need to set mips16_stubs_seen in this case.
   8576 	     That flag is used to see whether we need to look through
   8577 	     the global symbol table for stubs.  We don't need to set
   8578 	     it here, because we just have a local stub.  */
   8579 	}
   8580       else
   8581 	{
   8582 	  struct mips_elf_link_hash_entry *h;
   8583 
   8584 	  h = ((struct mips_elf_link_hash_entry *)
   8585 	       sym_hashes[r_symndx - extsymoff]);
   8586 
   8587 	  while (h->root.root.type == bfd_link_hash_indirect
   8588 		 || h->root.root.type == bfd_link_hash_warning)
   8589 	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   8590 
   8591 	  /* H is the symbol this stub is for.  */
   8592 
   8593 	  /* If we already have an appropriate stub for this function, we
   8594 	     don't need another one, so we can discard this one.  Since
   8595 	     this function is called before the linker maps input sections
   8596 	     to output sections, we can easily discard it by setting the
   8597 	     SEC_EXCLUDE flag.  */
   8598 	  if (h->fn_stub != NULL)
   8599 	    {
   8600 	      sec->flags |= SEC_EXCLUDE;
   8601 	      return true;
   8602 	    }
   8603 
   8604 	  sec->flags |= SEC_KEEP;
   8605 	  h->fn_stub = sec;
   8606 	  mips_elf_hash_table (info)->mips16_stubs_seen = true;
   8607 	}
   8608     }
   8609   else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
   8610     {
   8611       unsigned long r_symndx;
   8612       struct mips_elf_link_hash_entry *h;
   8613       asection **loc;
   8614 
   8615       /* Look at the relocation information to figure out which symbol
   8616 	 this is for.  */
   8617 
   8618       r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
   8619       if (r_symndx == 0)
   8620 	{
   8621 	  _bfd_error_handler
   8622 	    /* xgettext:c-format */
   8623 	    (_("%pB: warning: cannot determine the target function for"
   8624 	       " stub section `%s'"),
   8625 	     abfd, name);
   8626 	  bfd_set_error (bfd_error_bad_value);
   8627 	  return false;
   8628 	}
   8629 
   8630       if (r_symndx < extsymoff
   8631 	  || sym_hashes[r_symndx - extsymoff] == NULL)
   8632 	{
   8633 	  asection *o;
   8634 
   8635 	  /* This stub is for a local symbol.  This stub will only be
   8636 	     needed if there is some relocation (R_MIPS16_26) in this BFD
   8637 	     that refers to this symbol.  */
   8638 	  for (o = abfd->sections; o != NULL; o = o->next)
   8639 	    {
   8640 	      Elf_Internal_Rela *sec_relocs;
   8641 	      const Elf_Internal_Rela *r, *rend;
   8642 
   8643 	      /* We can ignore stub sections when looking for relocs.  */
   8644 	      if ((o->flags & SEC_RELOC) == 0
   8645 		  || o->reloc_count == 0
   8646 		  || section_allows_mips16_refs_p (o))
   8647 		continue;
   8648 
   8649 	      sec_relocs
   8650 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   8651 					     info->keep_memory);
   8652 	      if (sec_relocs == NULL)
   8653 		return false;
   8654 
   8655 	      rend = sec_relocs + o->reloc_count;
   8656 	      for (r = sec_relocs; r < rend; r++)
   8657 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
   8658 		    && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
   8659 		    break;
   8660 
   8661 	      if (elf_section_data (o)->relocs != sec_relocs)
   8662 		free (sec_relocs);
   8663 
   8664 	      if (r < rend)
   8665 		break;
   8666 	    }
   8667 
   8668 	  if (o == NULL)
   8669 	    {
   8670 	      /* There is no non-call reloc for this stub, so we do
   8671 		 not need it.  Since this function is called before
   8672 		 the linker maps input sections to output sections, we
   8673 		 can easily discard it by setting the SEC_EXCLUDE
   8674 		 flag.  */
   8675 	      sec->flags |= SEC_EXCLUDE;
   8676 	      return true;
   8677 	    }
   8678 
   8679 	  /* Record this stub in an array of local symbol call_stubs for
   8680 	     this BFD.  */
   8681 	  if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
   8682 	    {
   8683 	      unsigned long symcount;
   8684 	      asection **n;
   8685 	      bfd_size_type amt;
   8686 
   8687 	      if (elf_bad_symtab (abfd))
   8688 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
   8689 	      else
   8690 		symcount = symtab_hdr->sh_info;
   8691 	      amt = symcount * sizeof (asection *);
   8692 	      n = bfd_zalloc (abfd, amt);
   8693 	      if (n == NULL)
   8694 		return false;
   8695 	      mips_elf_tdata (abfd)->local_call_stubs = n;
   8696 	    }
   8697 
   8698 	  sec->flags |= SEC_KEEP;
   8699 	  mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
   8700 
   8701 	  /* We don't need to set mips16_stubs_seen in this case.
   8702 	     That flag is used to see whether we need to look through
   8703 	     the global symbol table for stubs.  We don't need to set
   8704 	     it here, because we just have a local stub.  */
   8705 	}
   8706       else
   8707 	{
   8708 	  h = ((struct mips_elf_link_hash_entry *)
   8709 	       sym_hashes[r_symndx - extsymoff]);
   8710 
   8711 	  /* H is the symbol this stub is for.  */
   8712 
   8713 	  if (CALL_FP_STUB_P (name))
   8714 	    loc = &h->call_fp_stub;
   8715 	  else
   8716 	    loc = &h->call_stub;
   8717 
   8718 	  /* If we already have an appropriate stub for this function, we
   8719 	     don't need another one, so we can discard this one.  Since
   8720 	     this function is called before the linker maps input sections
   8721 	     to output sections, we can easily discard it by setting the
   8722 	     SEC_EXCLUDE flag.  */
   8723 	  if (*loc != NULL)
   8724 	    {
   8725 	      sec->flags |= SEC_EXCLUDE;
   8726 	      return true;
   8727 	    }
   8728 
   8729 	  sec->flags |= SEC_KEEP;
   8730 	  *loc = sec;
   8731 	  mips_elf_hash_table (info)->mips16_stubs_seen = true;
   8732 	}
   8733     }
   8734 
   8735   sreloc = NULL;
   8736   contents = NULL;
   8737   for (rel = relocs; rel < rel_end; ++rel)
   8738     {
   8739       unsigned long r_symndx;
   8740       unsigned int r_type;
   8741       struct elf_link_hash_entry *h;
   8742       bool can_make_dynamic_p;
   8743       bool call_reloc_p;
   8744       bool constrain_symbol_p;
   8745 
   8746       r_symndx = ELF_R_SYM (abfd, rel->r_info);
   8747       r_type = ELF_R_TYPE (abfd, rel->r_info);
   8748 
   8749       if (r_symndx < extsymoff)
   8750 	h = NULL;
   8751       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
   8752 	{
   8753 	  _bfd_error_handler
   8754 	    /* xgettext:c-format */
   8755 	    (_("%pB: malformed reloc detected for section %s"),
   8756 	     abfd, name);
   8757 	  bfd_set_error (bfd_error_bad_value);
   8758 	  return false;
   8759 	}
   8760       else
   8761 	{
   8762 	  h = sym_hashes[r_symndx - extsymoff];
   8763 	  if (h != NULL)
   8764 	    {
   8765 	      while (h->root.type == bfd_link_hash_indirect
   8766 		     || h->root.type == bfd_link_hash_warning)
   8767 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8768 	    }
   8769 	}
   8770 
   8771       /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
   8772 	 relocation into a dynamic one.  */
   8773       can_make_dynamic_p = false;
   8774 
   8775       /* Set CALL_RELOC_P to true if the relocation is for a call,
   8776 	 and if pointer equality therefore doesn't matter.  */
   8777       call_reloc_p = false;
   8778 
   8779       /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
   8780 	 into account when deciding how to define the symbol.  */
   8781       constrain_symbol_p = true;
   8782 
   8783       switch (r_type)
   8784 	{
   8785 	case R_MIPS_CALL16:
   8786 	case R_MIPS_CALL_HI16:
   8787 	case R_MIPS_CALL_LO16:
   8788 	case R_MIPS16_CALL16:
   8789 	case R_MICROMIPS_CALL16:
   8790 	case R_MICROMIPS_CALL_HI16:
   8791 	case R_MICROMIPS_CALL_LO16:
   8792 	  call_reloc_p = true;
   8793 	  /* Fall through.  */
   8794 
   8795 	case R_MIPS_GOT16:
   8796 	case R_MIPS_GOT_LO16:
   8797 	case R_MIPS_GOT_PAGE:
   8798 	case R_MIPS_GOT_DISP:
   8799 	case R_MIPS16_GOT16:
   8800 	case R_MICROMIPS_GOT16:
   8801 	case R_MICROMIPS_GOT_LO16:
   8802 	case R_MICROMIPS_GOT_PAGE:
   8803 	case R_MICROMIPS_GOT_DISP:
   8804 	  /* If we have a symbol that will resolve to zero at static link
   8805 	     time and it is used by a GOT relocation applied to code we
   8806 	     cannot relax to an immediate zero load, then we will be using
   8807 	     the special `__gnu_absolute_zero' symbol whose value is zero
   8808 	     at dynamic load time.  We ignore HI16-type GOT relocations at
   8809 	     this stage, because their handling will depend entirely on
   8810 	     the corresponding LO16-type GOT relocation.  */
   8811 	  if (!call_hi16_reloc_p (r_type)
   8812 	      && h != NULL
   8813 	      && bfd_link_pic (info)
   8814 	      && !htab->use_absolute_zero
   8815 	      && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   8816 	    {
   8817 	      bool rel_reloc;
   8818 
   8819 	      if (!mips_elf_get_section_contents (abfd, sec, &contents))
   8820 		return false;
   8821 
   8822 	      rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
   8823 	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
   8824 	      if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
   8825 		if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
   8826 						false))
   8827 		  if (!mips_elf_define_absolute_zero (abfd, info, htab,
   8828 						      r_type))
   8829 		    return false;
   8830 	    }
   8831 
   8832 	  /* Fall through.  */
   8833 	case R_MIPS_GOT_HI16:
   8834 	case R_MIPS_GOT_OFST:
   8835 	case R_MIPS_TLS_GOTTPREL:
   8836 	case R_MIPS_TLS_GD:
   8837 	case R_MIPS_TLS_LDM:
   8838 	case R_MIPS16_TLS_GOTTPREL:
   8839 	case R_MIPS16_TLS_GD:
   8840 	case R_MIPS16_TLS_LDM:
   8841 	case R_MICROMIPS_GOT_HI16:
   8842 	case R_MICROMIPS_GOT_OFST:
   8843 	case R_MICROMIPS_TLS_GOTTPREL:
   8844 	case R_MICROMIPS_TLS_GD:
   8845 	case R_MICROMIPS_TLS_LDM:
   8846 	  if (dynobj == NULL)
   8847 	    elf_hash_table (info)->dynobj = dynobj = abfd;
   8848 	  if (!mips_elf_create_got_section (dynobj, info))
   8849 	    return false;
   8850 	  if (htab->root.target_os == is_vxworks
   8851 	      && !bfd_link_pic (info))
   8852 	    {
   8853 	      _bfd_error_handler
   8854 		/* xgettext:c-format */
   8855 		(_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
   8856 		 abfd, (uint64_t) rel->r_offset);
   8857 	      bfd_set_error (bfd_error_bad_value);
   8858 	      return false;
   8859 	    }
   8860 	  can_make_dynamic_p = true;
   8861 	  break;
   8862 
   8863 	case R_MIPS_NONE:
   8864 	case R_MIPS_JALR:
   8865 	case R_MICROMIPS_JALR:
   8866 	  /* These relocations have empty fields and are purely there to
   8867 	     provide link information.  The symbol value doesn't matter.  */
   8868 	  constrain_symbol_p = false;
   8869 	  break;
   8870 
   8871 	case R_MIPS_GPREL16:
   8872 	case R_MIPS_GPREL32:
   8873 	case R_MIPS16_GPREL:
   8874 	case R_MICROMIPS_GPREL16:
   8875 	  /* GP-relative relocations always resolve to a definition in a
   8876 	     regular input file, ignoring the one-definition rule.  This is
   8877 	     important for the GP setup sequence in NewABI code, which
   8878 	     always resolves to a local function even if other relocations
   8879 	     against the symbol wouldn't.  */
   8880 	  constrain_symbol_p = false;
   8881 	  break;
   8882 
   8883 	case R_MIPS_32:
   8884 	case R_MIPS_REL32:
   8885 	case R_MIPS_64:
   8886 	  /* In VxWorks executables, references to external symbols
   8887 	     must be handled using copy relocs or PLT entries; it is not
   8888 	     possible to convert this relocation into a dynamic one.
   8889 
   8890 	     For executables that use PLTs and copy-relocs, we have a
   8891 	     choice between converting the relocation into a dynamic
   8892 	     one or using copy relocations or PLT entries.  It is
   8893 	     usually better to do the former, unless the relocation is
   8894 	     against a read-only section.  */
   8895 	  if ((bfd_link_pic (info)
   8896 	       || (h != NULL
   8897 		   && htab->root.target_os != is_vxworks
   8898 		   && strcmp (h->root.root.string, "__gnu_local_gp") != 0
   8899 		   && !(!info->nocopyreloc
   8900 			&& !PIC_OBJECT_P (abfd)
   8901 			&& MIPS_ELF_READONLY_SECTION (sec))))
   8902 	      && (sec->flags & SEC_ALLOC) != 0)
   8903 	    {
   8904 	      can_make_dynamic_p = true;
   8905 	      if (dynobj == NULL)
   8906 		elf_hash_table (info)->dynobj = dynobj = abfd;
   8907 	    }
   8908 	  break;
   8909 
   8910 	case R_MIPS_26:
   8911 	case R_MIPS_PC16:
   8912 	case R_MIPS_PC21_S2:
   8913 	case R_MIPS_PC26_S2:
   8914 	case R_MIPS16_26:
   8915 	case R_MIPS16_PC16_S1:
   8916 	case R_MICROMIPS_26_S1:
   8917 	case R_MICROMIPS_PC7_S1:
   8918 	case R_MICROMIPS_PC10_S1:
   8919 	case R_MICROMIPS_PC16_S1:
   8920 	case R_MICROMIPS_PC23_S2:
   8921 	  call_reloc_p = true;
   8922 	  break;
   8923 	}
   8924 
   8925       if (h)
   8926 	{
   8927 	  if (constrain_symbol_p)
   8928 	    {
   8929 	      if (!can_make_dynamic_p)
   8930 		((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
   8931 
   8932 	      if (!call_reloc_p)
   8933 		h->pointer_equality_needed = 1;
   8934 
   8935 	      /* We must not create a stub for a symbol that has
   8936 		 relocations related to taking the function's address.
   8937 		 This doesn't apply to VxWorks, where CALL relocs refer
   8938 		 to a .got.plt entry instead of a normal .got entry.  */
   8939 	      if (htab->root.target_os != is_vxworks
   8940 		  && (!can_make_dynamic_p || !call_reloc_p))
   8941 		((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
   8942 	    }
   8943 
   8944 	  /* Relocations against the special VxWorks __GOTT_BASE__ and
   8945 	     __GOTT_INDEX__ symbols must be left to the loader.  Allocate
   8946 	     room for them in .rela.dyn.  */
   8947 	  if (is_gott_symbol (info, h))
   8948 	    {
   8949 	      if (sreloc == NULL)
   8950 		{
   8951 		  sreloc = mips_elf_rel_dyn_section (info, true);
   8952 		  if (sreloc == NULL)
   8953 		    return false;
   8954 		}
   8955 	      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   8956 	      if (MIPS_ELF_READONLY_SECTION (sec))
   8957 		/* We tell the dynamic linker that there are
   8958 		   relocations against the text segment.  */
   8959 		info->flags |= DF_TEXTREL;
   8960 	    }
   8961 	}
   8962       else if (call_lo16_reloc_p (r_type)
   8963 	       || got_lo16_reloc_p (r_type)
   8964 	       || got_disp_reloc_p (r_type)
   8965 	       || (got16_reloc_p (r_type)
   8966 		   && htab->root.target_os == is_vxworks))
   8967 	{
   8968 	  /* We may need a local GOT entry for this relocation.  We
   8969 	     don't count R_MIPS_GOT_PAGE because we can estimate the
   8970 	     maximum number of pages needed by looking at the size of
   8971 	     the segment.  Similar comments apply to R_MIPS*_GOT16 and
   8972 	     R_MIPS*_CALL16, except on VxWorks, where GOT relocations
   8973 	     always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
   8974 	     R_MIPS_CALL_HI16 because these are always followed by an
   8975 	     R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
   8976 	  if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
   8977 						 rel->r_addend, info, r_type))
   8978 	    return false;
   8979 	}
   8980 
   8981       if (h != NULL
   8982 	  && mips_elf_relocation_needs_la25_stub (abfd, r_type,
   8983 						  ELF_ST_IS_MIPS16 (h->other)))
   8984 	((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
   8985 
   8986       switch (r_type)
   8987 	{
   8988 	case R_MIPS_CALL16:
   8989 	case R_MIPS16_CALL16:
   8990 	case R_MICROMIPS_CALL16:
   8991 	  if (h == NULL)
   8992 	    {
   8993 	      _bfd_error_handler
   8994 		/* xgettext:c-format */
   8995 		(_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
   8996 		 abfd, (uint64_t) rel->r_offset);
   8997 	      bfd_set_error (bfd_error_bad_value);
   8998 	      return false;
   8999 	    }
   9000 	  /* Fall through.  */
   9001 
   9002 	case R_MIPS_CALL_HI16:
   9003 	case R_MIPS_CALL_LO16:
   9004 	case R_MICROMIPS_CALL_HI16:
   9005 	case R_MICROMIPS_CALL_LO16:
   9006 	  if (h != NULL)
   9007 	    {
   9008 	      /* Make sure there is room in the regular GOT to hold the
   9009 		 function's address.  We may eliminate it in favour of
   9010 		 a .got.plt entry later; see mips_elf_count_got_symbols.  */
   9011 	      if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
   9012 						      r_type))
   9013 		return false;
   9014 
   9015 	      /* We need a stub, not a plt entry for the undefined
   9016 		 function.  But we record it as if it needs plt.  See
   9017 		 _bfd_elf_adjust_dynamic_symbol.  */
   9018 	      h->needs_plt = 1;
   9019 	      h->type = STT_FUNC;
   9020 	    }
   9021 	  break;
   9022 
   9023 	case R_MIPS_GOT_PAGE:
   9024 	case R_MICROMIPS_GOT_PAGE:
   9025 	case R_MIPS16_GOT16:
   9026 	case R_MIPS_GOT16:
   9027 	case R_MIPS_GOT_HI16:
   9028 	case R_MIPS_GOT_LO16:
   9029 	case R_MICROMIPS_GOT16:
   9030 	case R_MICROMIPS_GOT_HI16:
   9031 	case R_MICROMIPS_GOT_LO16:
   9032 	  if (!h || got_page_reloc_p (r_type))
   9033 	    {
   9034 	      /* This relocation needs (or may need, if h != NULL) a
   9035 		 page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
   9036 		 know for sure until we know whether the symbol is
   9037 		 preemptible.  */
   9038 	      if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
   9039 		{
   9040 		  if (!mips_elf_get_section_contents (abfd, sec, &contents))
   9041 		    return false;
   9042 		  howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
   9043 		  addend = mips_elf_read_rel_addend (abfd, sec, rel,
   9044 						     howto, contents);
   9045 		  if (got16_reloc_p (r_type))
   9046 		    mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
   9047 						  contents, &addend);
   9048 		  else
   9049 		    addend <<= howto->rightshift;
   9050 		}
   9051 	      else
   9052 		addend = rel->r_addend;
   9053 	      if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
   9054 						 h, addend))
   9055 		return false;
   9056 
   9057 	      if (h)
   9058 		{
   9059 		  struct mips_elf_link_hash_entry *hmips =
   9060 		    (struct mips_elf_link_hash_entry *) h;
   9061 
   9062 		  /* This symbol is definitely not overridable.  */
   9063 		  if (hmips->root.def_regular
   9064 		      && ! (bfd_link_pic (info) && ! info->symbolic
   9065 			    && ! hmips->root.forced_local))
   9066 		    h = NULL;
   9067 		}
   9068 	    }
   9069 	  /* If this is a global, overridable symbol, GOT_PAGE will
   9070 	     decay to GOT_DISP, so we'll need a GOT entry for it.  */
   9071 	  /* Fall through.  */
   9072 
   9073 	case R_MIPS_GOT_DISP:
   9074 	case R_MICROMIPS_GOT_DISP:
   9075 	  if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
   9076 						       false, r_type))
   9077 	    return false;
   9078 	  break;
   9079 
   9080 	case R_MIPS_TLS_GOTTPREL:
   9081 	case R_MIPS16_TLS_GOTTPREL:
   9082 	case R_MICROMIPS_TLS_GOTTPREL:
   9083 	  if (bfd_link_pic (info))
   9084 	    info->flags |= DF_STATIC_TLS;
   9085 	  /* Fall through */
   9086 
   9087 	case R_MIPS_TLS_LDM:
   9088 	case R_MIPS16_TLS_LDM:
   9089 	case R_MICROMIPS_TLS_LDM:
   9090 	  if (tls_ldm_reloc_p (r_type))
   9091 	    {
   9092 	      r_symndx = STN_UNDEF;
   9093 	      h = NULL;
   9094 	    }
   9095 	  /* Fall through */
   9096 
   9097 	case R_MIPS_TLS_GD:
   9098 	case R_MIPS16_TLS_GD:
   9099 	case R_MICROMIPS_TLS_GD:
   9100 	  /* This symbol requires a global offset table entry, or two
   9101 	     for TLS GD relocations.  */
   9102 	  if (h != NULL)
   9103 	    {
   9104 	      if (!mips_elf_record_global_got_symbol (h, abfd, info,
   9105 						      false, r_type))
   9106 		return false;
   9107 	    }
   9108 	  else
   9109 	    {
   9110 	      if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
   9111 						     rel->r_addend,
   9112 						     info, r_type))
   9113 		return false;
   9114 	    }
   9115 	  break;
   9116 
   9117 	case R_MIPS_32:
   9118 	case R_MIPS_REL32:
   9119 	case R_MIPS_64:
   9120 	  /* In VxWorks executables, references to external symbols
   9121 	     are handled using copy relocs or PLT stubs, so there's
   9122 	     no need to add a .rela.dyn entry for this relocation.  */
   9123 	  if (can_make_dynamic_p)
   9124 	    {
   9125 	      if (sreloc == NULL)
   9126 		{
   9127 		  sreloc = mips_elf_rel_dyn_section (info, true);
   9128 		  if (sreloc == NULL)
   9129 		    return false;
   9130 		}
   9131 	      if (bfd_link_pic (info) && h == NULL)
   9132 		{
   9133 		  /* When creating a shared object, we must copy these
   9134 		     reloc types into the output file as R_MIPS_REL32
   9135 		     relocs.  Make room for this reloc in .rel(a).dyn.  */
   9136 		  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   9137 		  if (MIPS_ELF_READONLY_SECTION (sec))
   9138 		    /* We tell the dynamic linker that there are
   9139 		       relocations against the text segment.  */
   9140 		    info->flags |= DF_TEXTREL;
   9141 		}
   9142 	      else
   9143 		{
   9144 		  struct mips_elf_link_hash_entry *hmips;
   9145 
   9146 		  /* For a shared object, we must copy this relocation
   9147 		     unless the symbol turns out to be undefined and
   9148 		     weak with non-default visibility, in which case
   9149 		     it will be left as zero.
   9150 
   9151 		     We could elide R_MIPS_REL32 for locally binding symbols
   9152 		     in shared libraries, but do not yet do so.
   9153 
   9154 		     For an executable, we only need to copy this
   9155 		     reloc if the symbol is defined in a dynamic
   9156 		     object.  */
   9157 		  hmips = (struct mips_elf_link_hash_entry *) h;
   9158 		  ++hmips->possibly_dynamic_relocs;
   9159 		  if (MIPS_ELF_READONLY_SECTION (sec))
   9160 		    /* We need it to tell the dynamic linker if there
   9161 		       are relocations against the text segment.  */
   9162 		    hmips->readonly_reloc = true;
   9163 		}
   9164 	    }
   9165 
   9166 	  if (SGI_COMPAT (abfd))
   9167 	    mips_elf_hash_table (info)->compact_rel_size +=
   9168 	      sizeof (Elf32_External_crinfo);
   9169 	  break;
   9170 
   9171 	case R_MIPS_26:
   9172 	case R_MIPS_GPREL16:
   9173 	case R_MIPS_LITERAL:
   9174 	case R_MIPS_GPREL32:
   9175 	case R_MICROMIPS_26_S1:
   9176 	case R_MICROMIPS_GPREL16:
   9177 	case R_MICROMIPS_LITERAL:
   9178 	case R_MICROMIPS_GPREL7_S2:
   9179 	  if (SGI_COMPAT (abfd))
   9180 	    mips_elf_hash_table (info)->compact_rel_size +=
   9181 	      sizeof (Elf32_External_crinfo);
   9182 	  break;
   9183 
   9184 	  /* This relocation describes the C++ object vtable hierarchy.
   9185 	     Reconstruct it for later use during GC.  */
   9186 	case R_MIPS_GNU_VTINHERIT:
   9187 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   9188 	    return false;
   9189 	  break;
   9190 
   9191 	  /* This relocation describes which C++ vtable entries are actually
   9192 	     used.  Record for later use during GC.  */
   9193 	case R_MIPS_GNU_VTENTRY:
   9194 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
   9195 	    return false;
   9196 	  break;
   9197 
   9198 	default:
   9199 	  break;
   9200 	}
   9201 
   9202       /* Record the need for a PLT entry.  At this point we don't know
   9203 	 yet if we are going to create a PLT in the first place, but
   9204 	 we only record whether the relocation requires a standard MIPS
   9205 	 or a compressed code entry anyway.  If we don't make a PLT after
   9206 	 all, then we'll just ignore these arrangements.  Likewise if
   9207 	 a PLT entry is not created because the symbol is satisfied
   9208 	 locally.  */
   9209       if (h != NULL
   9210 	  && (branch_reloc_p (r_type)
   9211 	      || mips16_branch_reloc_p (r_type)
   9212 	      || micromips_branch_reloc_p (r_type))
   9213 	  && !SYMBOL_CALLS_LOCAL (info, h))
   9214 	{
   9215 	  if (h->plt.plist == NULL)
   9216 	    h->plt.plist = mips_elf_make_plt_record (abfd);
   9217 	  if (h->plt.plist == NULL)
   9218 	    return false;
   9219 
   9220 	  if (branch_reloc_p (r_type))
   9221 	    h->plt.plist->need_mips = true;
   9222 	  else
   9223 	    h->plt.plist->need_comp = true;
   9224 	}
   9225 
   9226       /* See if this reloc would need to refer to a MIPS16 hard-float stub,
   9227 	 if there is one.  We only need to handle global symbols here;
   9228 	 we decide whether to keep or delete stubs for local symbols
   9229 	 when processing the stub's relocations.  */
   9230       if (h != NULL
   9231 	  && !mips16_call_reloc_p (r_type)
   9232 	  && !section_allows_mips16_refs_p (sec))
   9233 	{
   9234 	  struct mips_elf_link_hash_entry *mh;
   9235 
   9236 	  mh = (struct mips_elf_link_hash_entry *) h;
   9237 	  mh->need_fn_stub = true;
   9238 	}
   9239 
   9240       /* Refuse some position-dependent relocations when creating a
   9241 	 shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
   9242 	 not PIC, but we can create dynamic relocations and the result
   9243 	 will be fine.  Also do not refuse R_MIPS_LO16, which can be
   9244 	 combined with R_MIPS_GOT16.  */
   9245       if (bfd_link_pic (info))
   9246 	{
   9247 	  switch (r_type)
   9248 	    {
   9249 	    case R_MIPS_TLS_TPREL_HI16:
   9250 	    case R_MIPS16_TLS_TPREL_HI16:
   9251 	    case R_MICROMIPS_TLS_TPREL_HI16:
   9252 	    case R_MIPS_TLS_TPREL_LO16:
   9253 	    case R_MIPS16_TLS_TPREL_LO16:
   9254 	    case R_MICROMIPS_TLS_TPREL_LO16:
   9255 	      /* These are okay in PIE, but not in a shared library.  */
   9256 	      if (bfd_link_executable (info))
   9257 		break;
   9258 
   9259 	      /* FALLTHROUGH */
   9260 
   9261 	    case R_MIPS16_HI16:
   9262 	    case R_MIPS_HI16:
   9263 	    case R_MIPS_HIGHER:
   9264 	    case R_MIPS_HIGHEST:
   9265 	    case R_MICROMIPS_HI16:
   9266 	    case R_MICROMIPS_HIGHER:
   9267 	    case R_MICROMIPS_HIGHEST:
   9268 	      /* Don't refuse a high part relocation if it's against
   9269 		 no symbol (e.g. part of a compound relocation).  */
   9270 	      if (r_symndx == STN_UNDEF)
   9271 		break;
   9272 
   9273 	      /* Likewise an absolute symbol.  */
   9274 	      if (h != NULL && bfd_is_abs_symbol (&h->root))
   9275 		break;
   9276 
   9277 	      /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
   9278 		 and has a special meaning.  */
   9279 	      if (!NEWABI_P (abfd) && h != NULL
   9280 		  && strcmp (h->root.root.string, "_gp_disp") == 0)
   9281 		break;
   9282 
   9283 	      /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
   9284 	      if (is_gott_symbol (info, h))
   9285 		break;
   9286 
   9287 	      /* FALLTHROUGH */
   9288 
   9289 	    case R_MIPS16_26:
   9290 	    case R_MIPS_26:
   9291 	    case R_MICROMIPS_26_S1:
   9292 	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
   9293 	      /* An error for unsupported relocations is raised as part
   9294 		 of the above search, so we can skip the following.  */
   9295 	      if (howto != NULL)
   9296 		info->callbacks->einfo
   9297 		  /* xgettext:c-format */
   9298 		  (_("%X%H: relocation %s against `%s' cannot be used"
   9299 		     " when making a shared object; recompile with -fPIC\n"),
   9300 		   abfd, sec, rel->r_offset, howto->name,
   9301 		   (h) ? h->root.root.string : "a local symbol");
   9302 	      break;
   9303 	    default:
   9304 	      break;
   9305 	    }
   9306 	}
   9307     }
   9308 
   9309   return true;
   9310 }
   9311 
   9312 /* Allocate space for global sym dynamic relocs.  */
   9314 
   9315 static bool
   9316 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   9317 {
   9318   struct bfd_link_info *info = inf;
   9319   bfd *dynobj;
   9320   struct mips_elf_link_hash_entry *hmips;
   9321   struct mips_elf_link_hash_table *htab;
   9322 
   9323   htab = mips_elf_hash_table (info);
   9324   BFD_ASSERT (htab != NULL);
   9325 
   9326   dynobj = elf_hash_table (info)->dynobj;
   9327   hmips = (struct mips_elf_link_hash_entry *) h;
   9328 
   9329   /* VxWorks executables are handled elsewhere; we only need to
   9330      allocate relocations in shared objects.  */
   9331   if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
   9332     return true;
   9333 
   9334   /* Ignore indirect symbols.  All relocations against such symbols
   9335      will be redirected to the target symbol.  */
   9336   if (h->root.type == bfd_link_hash_indirect)
   9337     return true;
   9338 
   9339   /* If this symbol is defined in a dynamic object, or we are creating
   9340      a shared library, we will need to copy any R_MIPS_32 or
   9341      R_MIPS_REL32 relocs against it into the output file.  */
   9342   if (! bfd_link_relocatable (info)
   9343       && hmips->possibly_dynamic_relocs != 0
   9344       && (h->root.type == bfd_link_hash_defweak
   9345 	  || (!h->def_regular && !ELF_COMMON_DEF_P (h))
   9346 	  || bfd_link_pic (info)))
   9347     {
   9348       bool do_copy = true;
   9349 
   9350       if (h->root.type == bfd_link_hash_undefweak)
   9351 	{
   9352 	  /* Do not copy relocations for undefined weak symbols that
   9353 	     we are not going to export.  */
   9354 	  if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   9355 	    do_copy = false;
   9356 
   9357 	  /* Make sure undefined weak symbols are output as a dynamic
   9358 	     symbol in PIEs.  */
   9359 	  else if (h->dynindx == -1 && !h->forced_local)
   9360 	    {
   9361 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   9362 		return false;
   9363 	    }
   9364 	}
   9365 
   9366       if (do_copy)
   9367 	{
   9368 	  /* Even though we don't directly need a GOT entry for this symbol,
   9369 	     the SVR4 psABI requires it to have a dynamic symbol table
   9370 	     index greater that DT_MIPS_GOTSYM if there are dynamic
   9371 	     relocations against it.
   9372 
   9373 	     VxWorks does not enforce the same mapping between the GOT
   9374 	     and the symbol table, so the same requirement does not
   9375 	     apply there.  */
   9376 	  if (htab->root.target_os != is_vxworks)
   9377 	    {
   9378 	      if (hmips->global_got_area > GGA_RELOC_ONLY)
   9379 		hmips->global_got_area = GGA_RELOC_ONLY;
   9380 	      hmips->got_only_for_calls = false;
   9381 	    }
   9382 
   9383 	  mips_elf_allocate_dynamic_relocations
   9384 	    (dynobj, info, hmips->possibly_dynamic_relocs);
   9385 	  if (hmips->readonly_reloc)
   9386 	    /* We tell the dynamic linker that there are relocations
   9387 	       against the text segment.  */
   9388 	    info->flags |= DF_TEXTREL;
   9389 	}
   9390     }
   9391 
   9392   return true;
   9393 }
   9394 
   9395 /* Adjust a symbol defined by a dynamic object and referenced by a
   9396    regular object.  The current definition is in some section of the
   9397    dynamic object, but we're not including those sections.  We have to
   9398    change the definition to something the rest of the link can
   9399    understand.  */
   9400 
   9401 bool
   9402 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   9403 				     struct elf_link_hash_entry *h)
   9404 {
   9405   bfd *dynobj;
   9406   struct mips_elf_link_hash_entry *hmips;
   9407   struct mips_elf_link_hash_table *htab;
   9408   asection *s, *srel;
   9409 
   9410   htab = mips_elf_hash_table (info);
   9411   BFD_ASSERT (htab != NULL);
   9412 
   9413   dynobj = elf_hash_table (info)->dynobj;
   9414   hmips = (struct mips_elf_link_hash_entry *) h;
   9415 
   9416   /* Make sure we know what is going on here.  */
   9417   if (dynobj == NULL
   9418       || (! h->needs_plt
   9419 	  && ! h->is_weakalias
   9420 	  && (! h->def_dynamic
   9421 	      || ! h->ref_regular
   9422 	      || h->def_regular)))
   9423     {
   9424       if (h->type == STT_GNU_IFUNC)
   9425 	_bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
   9426 			    h->root.root.string);
   9427       else
   9428 	_bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
   9429 			    h->root.root.string);
   9430       return true;
   9431     }
   9432 
   9433   hmips = (struct mips_elf_link_hash_entry *) h;
   9434 
   9435   /* If there are call relocations against an externally-defined symbol,
   9436      see whether we can create a MIPS lazy-binding stub for it.  We can
   9437      only do this if all references to the function are through call
   9438      relocations, and in that case, the traditional lazy-binding stubs
   9439      are much more efficient than PLT entries.
   9440 
   9441      Traditional stubs are only available on SVR4 psABI-based systems;
   9442      VxWorks always uses PLTs instead.  */
   9443   if (htab->root.target_os != is_vxworks
   9444       && h->needs_plt
   9445       && !hmips->no_fn_stub)
   9446     {
   9447       if (! elf_hash_table (info)->dynamic_sections_created)
   9448 	return true;
   9449 
   9450       /* If this symbol is not defined in a regular file, then set
   9451 	 the symbol to the stub location.  This is required to make
   9452 	 function pointers compare as equal between the normal
   9453 	 executable and the shared library.  */
   9454       if (!h->def_regular
   9455 	  && !bfd_is_abs_section (htab->sstubs->output_section))
   9456 	{
   9457 	  hmips->needs_lazy_stub = true;
   9458 	  htab->lazy_stub_count++;
   9459 	  return true;
   9460 	}
   9461     }
   9462   /* As above, VxWorks requires PLT entries for externally-defined
   9463      functions that are only accessed through call relocations.
   9464 
   9465      Both VxWorks and non-VxWorks targets also need PLT entries if there
   9466      are static-only relocations against an externally-defined function.
   9467      This can technically occur for shared libraries if there are
   9468      branches to the symbol, although it is unlikely that this will be
   9469      used in practice due to the short ranges involved.  It can occur
   9470      for any relative or absolute relocation in executables; in that
   9471      case, the PLT entry becomes the function's canonical address.  */
   9472   else if (((h->needs_plt && !hmips->no_fn_stub)
   9473 	    || (h->type == STT_FUNC && hmips->has_static_relocs))
   9474 	   && htab->use_plts_and_copy_relocs
   9475 	   && !SYMBOL_CALLS_LOCAL (info, h)
   9476 	   && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   9477 		&& h->root.type == bfd_link_hash_undefweak))
   9478     {
   9479       bool micromips_p = MICROMIPS_P (info->output_bfd);
   9480       bool newabi_p = NEWABI_P (info->output_bfd);
   9481 
   9482       /* If this is the first symbol to need a PLT entry, then make some
   9483 	 basic setup.  Also work out PLT entry sizes.  We'll need them
   9484 	 for PLT offset calculations.  */
   9485       if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
   9486 	{
   9487 	  BFD_ASSERT (htab->root.sgotplt->size == 0);
   9488 	  BFD_ASSERT (htab->plt_got_index == 0);
   9489 
   9490 	  /* If we're using the PLT additions to the psABI, each PLT
   9491 	     entry is 16 bytes and the PLT0 entry is 32 bytes.
   9492 	     Encourage better cache usage by aligning.  We do this
   9493 	     lazily to avoid pessimizing traditional objects.  */
   9494 	  if (htab->root.target_os != is_vxworks
   9495 	      && !bfd_set_section_alignment (htab->root.splt, 5))
   9496 	    return false;
   9497 
   9498 	  /* Make sure that .got.plt is word-aligned.  We do this lazily
   9499 	     for the same reason as above.  */
   9500 	  if (!bfd_set_section_alignment (htab->root.sgotplt,
   9501 					  MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
   9502 	    return false;
   9503 
   9504 	  /* On non-VxWorks targets, the first two entries in .got.plt
   9505 	     are reserved.  */
   9506 	  if (htab->root.target_os != is_vxworks)
   9507 	    htab->plt_got_index
   9508 	      += (get_elf_backend_data (dynobj)->got_header_size
   9509 		  / MIPS_ELF_GOT_SIZE (dynobj));
   9510 
   9511 	  /* On VxWorks, also allocate room for the header's
   9512 	     .rela.plt.unloaded entries.  */
   9513 	  if (htab->root.target_os == is_vxworks
   9514 	      && !bfd_link_pic (info))
   9515 	    htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
   9516 
   9517 	  /* Now work out the sizes of individual PLT entries.  */
   9518 	  if (htab->root.target_os == is_vxworks
   9519 	      && bfd_link_pic (info))
   9520 	    htab->plt_mips_entry_size
   9521 	      = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
   9522 	  else if (htab->root.target_os == is_vxworks)
   9523 	    htab->plt_mips_entry_size
   9524 	      = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
   9525 	  else if (newabi_p)
   9526 	    htab->plt_mips_entry_size
   9527 	      = 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9528 	  else if (!micromips_p)
   9529 	    {
   9530 	      htab->plt_mips_entry_size
   9531 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9532 	      htab->plt_comp_entry_size
   9533 		= 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
   9534 	    }
   9535 	  else if (htab->insn32)
   9536 	    {
   9537 	      htab->plt_mips_entry_size
   9538 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9539 	      htab->plt_comp_entry_size
   9540 		= 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
   9541 	    }
   9542 	  else
   9543 	    {
   9544 	      htab->plt_mips_entry_size
   9545 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9546 	      htab->plt_comp_entry_size
   9547 		= 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
   9548 	    }
   9549 	}
   9550 
   9551       if (h->plt.plist == NULL)
   9552 	h->plt.plist = mips_elf_make_plt_record (dynobj);
   9553       if (h->plt.plist == NULL)
   9554 	return false;
   9555 
   9556       /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
   9557 	 n32 or n64, so always use a standard entry there.
   9558 
   9559 	 If the symbol has a MIPS16 call stub and gets a PLT entry, then
   9560 	 all MIPS16 calls will go via that stub, and there is no benefit
   9561 	 to having a MIPS16 entry.  And in the case of call_stub a
   9562 	 standard entry actually has to be used as the stub ends with a J
   9563 	 instruction.  */
   9564       if (newabi_p
   9565 	  || htab->root.target_os == is_vxworks
   9566 	  || hmips->call_stub
   9567 	  || hmips->call_fp_stub)
   9568 	{
   9569 	  h->plt.plist->need_mips = true;
   9570 	  h->plt.plist->need_comp = false;
   9571 	}
   9572 
   9573       /* Otherwise, if there are no direct calls to the function, we
   9574 	 have a free choice of whether to use standard or compressed
   9575 	 entries.  Prefer microMIPS entries if the object is known to
   9576 	 contain microMIPS code, so that it becomes possible to create
   9577 	 pure microMIPS binaries.  Prefer standard entries otherwise,
   9578 	 because MIPS16 ones are no smaller and are usually slower.  */
   9579       if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
   9580 	{
   9581 	  if (micromips_p)
   9582 	    h->plt.plist->need_comp = true;
   9583 	  else
   9584 	    h->plt.plist->need_mips = true;
   9585 	}
   9586 
   9587       if (h->plt.plist->need_mips)
   9588 	{
   9589 	  h->plt.plist->mips_offset = htab->plt_mips_offset;
   9590 	  htab->plt_mips_offset += htab->plt_mips_entry_size;
   9591 	}
   9592       if (h->plt.plist->need_comp)
   9593 	{
   9594 	  h->plt.plist->comp_offset = htab->plt_comp_offset;
   9595 	  htab->plt_comp_offset += htab->plt_comp_entry_size;
   9596 	}
   9597 
   9598       /* Reserve the corresponding .got.plt entry now too.  */
   9599       h->plt.plist->gotplt_index = htab->plt_got_index++;
   9600 
   9601       /* If the output file has no definition of the symbol, set the
   9602 	 symbol's value to the address of the stub.  */
   9603       if (!bfd_link_pic (info) && !h->def_regular)
   9604 	hmips->use_plt_entry = true;
   9605 
   9606       /* Make room for the R_MIPS_JUMP_SLOT relocation.  */
   9607       htab->root.srelplt->size += (htab->root.target_os == is_vxworks
   9608 				   ? MIPS_ELF_RELA_SIZE (dynobj)
   9609 				   : MIPS_ELF_REL_SIZE (dynobj));
   9610 
   9611       /* Make room for the .rela.plt.unloaded relocations.  */
   9612       if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
   9613 	htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
   9614 
   9615       /* All relocations against this symbol that could have been made
   9616 	 dynamic will now refer to the PLT entry instead.  */
   9617       hmips->possibly_dynamic_relocs = 0;
   9618 
   9619       return true;
   9620     }
   9621 
   9622   /* If this is a weak symbol, and there is a real definition, the
   9623      processor independent code will have arranged for us to see the
   9624      real definition first, and we can just use the same value.  */
   9625   if (h->is_weakalias)
   9626     {
   9627       struct elf_link_hash_entry *def = weakdef (h);
   9628       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   9629       h->root.u.def.section = def->root.u.def.section;
   9630       h->root.u.def.value = def->root.u.def.value;
   9631       return true;
   9632     }
   9633 
   9634   /* Otherwise, there is nothing further to do for symbols defined
   9635      in regular objects.  */
   9636   if (h->def_regular)
   9637     return true;
   9638 
   9639   /* There's also nothing more to do if we'll convert all relocations
   9640      against this symbol into dynamic relocations.  */
   9641   if (!hmips->has_static_relocs)
   9642     return true;
   9643 
   9644   /* We're now relying on copy relocations.  Complain if we have
   9645      some that we can't convert.  */
   9646   if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
   9647     {
   9648       _bfd_error_handler (_("non-dynamic relocations refer to "
   9649 			    "dynamic symbol %s"),
   9650 			  h->root.root.string);
   9651       bfd_set_error (bfd_error_bad_value);
   9652       return false;
   9653     }
   9654 
   9655   /* We must allocate the symbol in our .dynbss section, which will
   9656      become part of the .bss section of the executable.  There will be
   9657      an entry for this symbol in the .dynsym section.  The dynamic
   9658      object will contain position independent code, so all references
   9659      from the dynamic object to this symbol will go through the global
   9660      offset table.  The dynamic linker will use the .dynsym entry to
   9661      determine the address it must put in the global offset table, so
   9662      both the dynamic object and the regular object will refer to the
   9663      same memory location for the variable.  */
   9664 
   9665   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
   9666     {
   9667       s = htab->root.sdynrelro;
   9668       srel = htab->root.sreldynrelro;
   9669     }
   9670   else
   9671     {
   9672       s = htab->root.sdynbss;
   9673       srel = htab->root.srelbss;
   9674     }
   9675   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
   9676     {
   9677       if (htab->root.target_os == is_vxworks)
   9678 	srel->size += sizeof (Elf32_External_Rela);
   9679       else
   9680 	mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   9681       h->needs_copy = 1;
   9682     }
   9683 
   9684   /* All relocations against this symbol that could have been made
   9685      dynamic will now refer to the local copy instead.  */
   9686   hmips->possibly_dynamic_relocs = 0;
   9687 
   9688   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   9689 }
   9690 
   9691 /* If the link uses a GOT, lay it out and work out its size.  */
   9693 
   9694 static bool
   9695 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
   9696 {
   9697   bfd *dynobj;
   9698   asection *s;
   9699   struct mips_got_info *g;
   9700   bfd_size_type loadable_size = 0;
   9701   bfd_size_type page_gotno;
   9702   bfd *ibfd;
   9703   struct mips_elf_traverse_got_arg tga;
   9704   struct mips_elf_link_hash_table *htab;
   9705 
   9706   htab = mips_elf_hash_table (info);
   9707   BFD_ASSERT (htab != NULL);
   9708 
   9709   s = htab->root.sgot;
   9710   if (s == NULL)
   9711     return true;
   9712 
   9713   dynobj = elf_hash_table (info)->dynobj;
   9714   g = htab->got_info;
   9715 
   9716   /* Allocate room for the reserved entries.  VxWorks always reserves
   9717      3 entries; other objects only reserve 2 entries.  */
   9718   BFD_ASSERT (g->assigned_low_gotno == 0);
   9719   if (htab->root.target_os == is_vxworks)
   9720     htab->reserved_gotno = 3;
   9721   else
   9722     htab->reserved_gotno = 2;
   9723   g->local_gotno += htab->reserved_gotno;
   9724   g->assigned_low_gotno = htab->reserved_gotno;
   9725 
   9726   /* Decide which symbols need to go in the global part of the GOT and
   9727      count the number of reloc-only GOT symbols.  */
   9728   mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
   9729 
   9730   if (!mips_elf_resolve_final_got_entries (info, g))
   9731     return false;
   9732 
   9733   /* Calculate the total loadable size of the output.  That
   9734      will give us the maximum number of GOT_PAGE entries
   9735      required.  */
   9736   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   9737     {
   9738       asection *subsection;
   9739 
   9740       for (subsection = ibfd->sections;
   9741 	   subsection;
   9742 	   subsection = subsection->next)
   9743 	{
   9744 	  if ((subsection->flags & SEC_ALLOC) == 0)
   9745 	    continue;
   9746 	  loadable_size += ((subsection->size + 0xf)
   9747 			    &~ (bfd_size_type) 0xf);
   9748 	}
   9749     }
   9750 
   9751   if (htab->root.target_os == is_vxworks)
   9752     /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
   9753        relocations against local symbols evaluate to "G", and the EABI does
   9754        not include R_MIPS_GOT_PAGE.  */
   9755     page_gotno = 0;
   9756   else
   9757     /* Assume there are two loadable segments consisting of contiguous
   9758        sections.  Is 5 enough?  */
   9759     page_gotno = (loadable_size >> 16) + 5;
   9760 
   9761   /* Choose the smaller of the two page estimates; both are intended to be
   9762      conservative.  */
   9763   if (page_gotno > g->page_gotno)
   9764     page_gotno = g->page_gotno;
   9765 
   9766   g->local_gotno += page_gotno;
   9767   g->assigned_high_gotno = g->local_gotno - 1;
   9768 
   9769   s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9770   s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9771   s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9772 
   9773   /* VxWorks does not support multiple GOTs.  It initializes $gp to
   9774      __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
   9775      dynamic loader.  */
   9776   if (htab->root.target_os != is_vxworks
   9777       && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
   9778     {
   9779       if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
   9780 	return false;
   9781     }
   9782   else
   9783     {
   9784       /* Record that all bfds use G.  This also has the effect of freeing
   9785 	 the per-bfd GOTs, which we no longer need.  */
   9786       for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   9787 	if (mips_elf_bfd_got (ibfd, false))
   9788 	  mips_elf_replace_bfd_got (ibfd, g);
   9789       mips_elf_replace_bfd_got (output_bfd, g);
   9790 
   9791       /* Set up TLS entries.  */
   9792       g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
   9793       tga.info = info;
   9794       tga.g = g;
   9795       tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
   9796       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
   9797       if (!tga.g)
   9798 	return false;
   9799       BFD_ASSERT (g->tls_assigned_gotno
   9800 		  == g->global_gotno + g->local_gotno + g->tls_gotno);
   9801 
   9802       /* Each VxWorks GOT entry needs an explicit relocation.  */
   9803       if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
   9804 	g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
   9805 
   9806       /* Allocate room for the TLS relocations.  */
   9807       if (g->relocs)
   9808 	mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
   9809     }
   9810 
   9811   return true;
   9812 }
   9813 
   9814 /* Estimate the size of the .MIPS.stubs section.  */
   9815 
   9816 static void
   9817 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
   9818 {
   9819   struct mips_elf_link_hash_table *htab;
   9820   bfd_size_type dynsymcount;
   9821 
   9822   htab = mips_elf_hash_table (info);
   9823   BFD_ASSERT (htab != NULL);
   9824 
   9825   if (htab->lazy_stub_count == 0)
   9826     return;
   9827 
   9828   /* IRIX rld assumes that a function stub isn't at the end of the .text
   9829      section, so add a dummy entry to the end.  */
   9830   htab->lazy_stub_count++;
   9831 
   9832   /* Get a worst-case estimate of the number of dynamic symbols needed.
   9833      At this point, dynsymcount does not account for section symbols
   9834      and count_section_dynsyms may overestimate the number that will
   9835      be needed.  */
   9836   dynsymcount = (elf_hash_table (info)->dynsymcount
   9837 		 + count_section_dynsyms (output_bfd, info));
   9838 
   9839   /* Determine the size of one stub entry.  There's no disadvantage
   9840      from using microMIPS code here, so for the sake of pure-microMIPS
   9841      binaries we prefer it whenever there's any microMIPS code in
   9842      output produced at all.  This has a benefit of stubs being
   9843      shorter by 4 bytes each too, unless in the insn32 mode.  */
   9844   if (!MICROMIPS_P (output_bfd))
   9845     htab->function_stub_size = (dynsymcount > 0x10000
   9846 				? MIPS_FUNCTION_STUB_BIG_SIZE
   9847 				: MIPS_FUNCTION_STUB_NORMAL_SIZE);
   9848   else if (htab->insn32)
   9849     htab->function_stub_size = (dynsymcount > 0x10000
   9850 				? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
   9851 				: MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
   9852   else
   9853     htab->function_stub_size = (dynsymcount > 0x10000
   9854 				? MICROMIPS_FUNCTION_STUB_BIG_SIZE
   9855 				: MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
   9856 
   9857   htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
   9858 }
   9859 
   9860 /* A mips_elf_link_hash_traverse callback for which DATA points to a
   9861    mips_htab_traverse_info.  If H needs a traditional MIPS lazy-binding
   9862    stub, allocate an entry in the stubs section.  */
   9863 
   9864 static bool
   9865 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
   9866 {
   9867   struct mips_htab_traverse_info *hti = data;
   9868   struct mips_elf_link_hash_table *htab;
   9869   struct bfd_link_info *info;
   9870   bfd *output_bfd;
   9871 
   9872   info = hti->info;
   9873   output_bfd = hti->output_bfd;
   9874   htab = mips_elf_hash_table (info);
   9875   BFD_ASSERT (htab != NULL);
   9876 
   9877   if (h->needs_lazy_stub)
   9878     {
   9879       bool micromips_p = MICROMIPS_P (output_bfd);
   9880       unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   9881       bfd_vma isa_bit = micromips_p;
   9882 
   9883       BFD_ASSERT (htab->root.dynobj != NULL);
   9884       if (h->root.plt.plist == NULL)
   9885 	h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
   9886       if (h->root.plt.plist == NULL)
   9887 	{
   9888 	  hti->error = true;
   9889 	  return false;
   9890 	}
   9891       h->root.root.u.def.section = htab->sstubs;
   9892       h->root.root.u.def.value = htab->sstubs->size + isa_bit;
   9893       h->root.plt.plist->stub_offset = htab->sstubs->size;
   9894       h->root.other = other;
   9895       htab->sstubs->size += htab->function_stub_size;
   9896     }
   9897   return true;
   9898 }
   9899 
   9900 /* Allocate offsets in the stubs section to each symbol that needs one.
   9901    Set the final size of the .MIPS.stub section.  */
   9902 
   9903 static bool
   9904 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
   9905 {
   9906   bfd *output_bfd = info->output_bfd;
   9907   bool micromips_p = MICROMIPS_P (output_bfd);
   9908   unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   9909   bfd_vma isa_bit = micromips_p;
   9910   struct mips_elf_link_hash_table *htab;
   9911   struct mips_htab_traverse_info hti;
   9912   struct elf_link_hash_entry *h;
   9913   bfd *dynobj;
   9914 
   9915   htab = mips_elf_hash_table (info);
   9916   BFD_ASSERT (htab != NULL);
   9917 
   9918   if (htab->lazy_stub_count == 0)
   9919     return true;
   9920 
   9921   htab->sstubs->size = 0;
   9922   hti.info = info;
   9923   hti.output_bfd = output_bfd;
   9924   hti.error = false;
   9925   mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
   9926   if (hti.error)
   9927     return false;
   9928   htab->sstubs->size += htab->function_stub_size;
   9929   BFD_ASSERT (htab->sstubs->size
   9930 	      == htab->lazy_stub_count * htab->function_stub_size);
   9931 
   9932   dynobj = elf_hash_table (info)->dynobj;
   9933   BFD_ASSERT (dynobj != NULL);
   9934   h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
   9935   if (h == NULL)
   9936     return false;
   9937   h->root.u.def.value = isa_bit;
   9938   h->other = other;
   9939   h->type = STT_FUNC;
   9940 
   9941   return true;
   9942 }
   9943 
   9944 /* A mips_elf_link_hash_traverse callback for which DATA points to a
   9945    bfd_link_info.  If H uses the address of a PLT entry as the value
   9946    of the symbol, then set the entry in the symbol table now.  Prefer
   9947    a standard MIPS PLT entry.  */
   9948 
   9949 static bool
   9950 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
   9951 {
   9952   struct bfd_link_info *info = data;
   9953   bool micromips_p = MICROMIPS_P (info->output_bfd);
   9954   struct mips_elf_link_hash_table *htab;
   9955   unsigned int other;
   9956   bfd_vma isa_bit;
   9957   bfd_vma val;
   9958 
   9959   htab = mips_elf_hash_table (info);
   9960   BFD_ASSERT (htab != NULL);
   9961 
   9962   if (h->use_plt_entry)
   9963     {
   9964       BFD_ASSERT (h->root.plt.plist != NULL);
   9965       BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
   9966 		  || h->root.plt.plist->comp_offset != MINUS_ONE);
   9967 
   9968       val = htab->plt_header_size;
   9969       if (h->root.plt.plist->mips_offset != MINUS_ONE)
   9970 	{
   9971 	  isa_bit = 0;
   9972 	  val += h->root.plt.plist->mips_offset;
   9973 	  other = 0;
   9974 	}
   9975       else
   9976 	{
   9977 	  isa_bit = 1;
   9978 	  val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
   9979 	  other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
   9980 	}
   9981       val += isa_bit;
   9982       /* For VxWorks, point at the PLT load stub rather than the lazy
   9983 	 resolution stub; this stub will become the canonical function
   9984 	 address.  */
   9985       if (htab->root.target_os == is_vxworks)
   9986 	val += 8;
   9987 
   9988       h->root.root.u.def.section = htab->root.splt;
   9989       h->root.root.u.def.value = val;
   9990       h->root.other = other;
   9991     }
   9992 
   9993   return true;
   9994 }
   9995 
   9996 /* Set the sizes of the dynamic sections, some mips non-dynamic sections,
   9997    and check for any mips16 stub sections that we can discard.  */
   9998 
   9999 bool
   10000 _bfd_mips_elf_late_size_sections (bfd *output_bfd,
   10001 				  struct bfd_link_info *info)
   10002 {
   10003   bfd *dynobj;
   10004   asection *s, *sreldyn;
   10005   bool reltext;
   10006   struct mips_elf_link_hash_table *htab;
   10007   struct mips_htab_traverse_info hti;
   10008 
   10009   htab = mips_elf_hash_table (info);
   10010   BFD_ASSERT (htab != NULL);
   10011 
   10012   /* The .reginfo section has a fixed size.  */
   10013   s = bfd_get_section_by_name (output_bfd, ".reginfo");
   10014   if (s != NULL)
   10015     {
   10016       bfd_set_section_size (s, sizeof (Elf32_External_RegInfo));
   10017       s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
   10018     }
   10019 
   10020   /* The .MIPS.abiflags section has a fixed size.  */
   10021   s = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
   10022   if (s != NULL)
   10023     {
   10024       bfd_set_section_size (s, sizeof (Elf_External_ABIFlags_v0));
   10025       s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
   10026     }
   10027 
   10028   hti.info = info;
   10029   hti.output_bfd = output_bfd;
   10030   hti.error = false;
   10031   mips_elf_link_hash_traverse (htab, mips_elf_check_symbols, &hti);
   10032   if (hti.error)
   10033     return false;
   10034 
   10035   dynobj = htab->root.dynobj;
   10036   if (dynobj == NULL)
   10037     return true;
   10038 
   10039   if (htab->root.dynamic_sections_created)
   10040     {
   10041       /* Set the contents of the .interp section to the interpreter.  */
   10042       if (bfd_link_executable (info) && !info->nointerp)
   10043 	{
   10044 	  s = bfd_get_linker_section (dynobj, ".interp");
   10045 	  BFD_ASSERT (s != NULL);
   10046 	  s->size
   10047 	    = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
   10048 	  s->contents
   10049 	    = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
   10050 	}
   10051 
   10052       /* Figure out the size of the PLT header if we know that we
   10053 	 are using it.  For the sake of cache alignment always use
   10054 	 a standard header whenever any standard entries are present
   10055 	 even if microMIPS entries are present as well.  This also
   10056 	 lets the microMIPS header rely on the value of $v0 only set
   10057 	 by microMIPS entries, for a small size reduction.
   10058 
   10059 	 Set symbol table entry values for symbols that use the
   10060 	 address of their PLT entry now that we can calculate it.
   10061 
   10062 	 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
   10063 	 haven't already in _bfd_elf_create_dynamic_sections.  */
   10064       if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
   10065 	{
   10066 	  bool micromips_p = (MICROMIPS_P (output_bfd)
   10067 				     && !htab->plt_mips_offset);
   10068 	  unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   10069 	  bfd_vma isa_bit = micromips_p;
   10070 	  struct elf_link_hash_entry *h;
   10071 	  bfd_vma size;
   10072 
   10073 	  BFD_ASSERT (htab->use_plts_and_copy_relocs);
   10074 	  BFD_ASSERT (htab->root.sgotplt->size == 0);
   10075 	  BFD_ASSERT (htab->root.splt->size == 0);
   10076 
   10077 	  if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
   10078 	    size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
   10079 	  else if (htab->root.target_os == is_vxworks)
   10080 	    size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
   10081 	  else if (ABI_64_P (output_bfd))
   10082 	    size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
   10083 	  else if (ABI_N32_P (output_bfd))
   10084 	    size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
   10085 	  else if (!micromips_p)
   10086 	    size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
   10087 	  else if (htab->insn32)
   10088 	    size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
   10089 	  else
   10090 	    size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
   10091 
   10092 	  htab->plt_header_is_comp = micromips_p;
   10093 	  htab->plt_header_size = size;
   10094 	  htab->root.splt->size = (size
   10095 				   + htab->plt_mips_offset
   10096 				   + htab->plt_comp_offset);
   10097 	  htab->root.sgotplt->size = (htab->plt_got_index
   10098 				      * MIPS_ELF_GOT_SIZE (dynobj));
   10099 
   10100 	  mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
   10101 
   10102 	  if (htab->root.hplt == NULL)
   10103 	    {
   10104 	      h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
   10105 					       "_PROCEDURE_LINKAGE_TABLE_");
   10106 	      htab->root.hplt = h;
   10107 	      if (h == NULL)
   10108 		return false;
   10109 	    }
   10110 
   10111 	  h = htab->root.hplt;
   10112 	  h->root.u.def.value = isa_bit;
   10113 	  h->other = other;
   10114 	  h->type = STT_FUNC;
   10115 	}
   10116     }
   10117 
   10118   /* Allocate space for global sym dynamic relocs.  */
   10119   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
   10120 
   10121   mips_elf_estimate_stub_size (output_bfd, info);
   10122 
   10123   if (!mips_elf_lay_out_got (output_bfd, info))
   10124     return false;
   10125 
   10126   mips_elf_lay_out_lazy_stubs (info);
   10127 
   10128   /* The check_relocs and adjust_dynamic_symbol entry points have
   10129      determined the sizes of the various dynamic sections.  Allocate
   10130      memory for them.  */
   10131   reltext = false;
   10132   for (s = dynobj->sections; s != NULL; s = s->next)
   10133     {
   10134       const char *name;
   10135 
   10136       /* It's OK to base decisions on the section name, because none
   10137 	 of the dynobj section names depend upon the input files.  */
   10138       name = bfd_section_name (s);
   10139 
   10140       if ((s->flags & SEC_LINKER_CREATED) == 0)
   10141 	continue;
   10142 
   10143       if (startswith (name, ".rel"))
   10144 	{
   10145 	  if (s->size != 0)
   10146 	    {
   10147 	      const char *outname;
   10148 	      asection *target;
   10149 
   10150 	      /* If this relocation section applies to a read only
   10151 		 section, then we probably need a DT_TEXTREL entry.
   10152 		 If the relocation section is .rel(a).dyn, we always
   10153 		 assert a DT_TEXTREL entry rather than testing whether
   10154 		 there exists a relocation to a read only section or
   10155 		 not.  */
   10156 	      outname = bfd_section_name (s->output_section);
   10157 	      target = bfd_get_section_by_name (output_bfd, outname + 4);
   10158 	      if ((target != NULL
   10159 		   && (target->flags & SEC_READONLY) != 0
   10160 		   && (target->flags & SEC_ALLOC) != 0)
   10161 		  || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
   10162 		reltext = true;
   10163 
   10164 	      /* We use the reloc_count field as a counter if we need
   10165 		 to copy relocs into the output file.  */
   10166 	      if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
   10167 		s->reloc_count = 0;
   10168 
   10169 	      /* If combreloc is enabled, elf_link_sort_relocs() will
   10170 		 sort relocations, but in a different way than we do,
   10171 		 and before we're done creating relocations.  Also, it
   10172 		 will move them around between input sections'
   10173 		 relocation's contents, so our sorting would be
   10174 		 broken, so don't let it run.  */
   10175 	      info->combreloc = 0;
   10176 	    }
   10177 	}
   10178       else if (bfd_link_executable (info)
   10179 	       && !htab->use_rld_obj_head
   10180 	       && startswith (name, ".rld_map"))
   10181 	{
   10182 	  /* We add a room for __rld_map.  It will be filled in by the
   10183 	     rtld to contain a pointer to the _r_debug structure.  */
   10184 	  s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
   10185 	}
   10186       else if (SGI_COMPAT (output_bfd)
   10187 	       && startswith (name, ".compact_rel"))
   10188 	s->size += htab->compact_rel_size;
   10189       else if (s == htab->root.splt)
   10190 	{
   10191 	  /* If the last PLT entry has a branch delay slot, allocate
   10192 	     room for an extra nop to fill the delay slot.  This is
   10193 	     for CPUs without load interlocking.  */
   10194 	  if (! LOAD_INTERLOCKS_P (output_bfd)
   10195 	      && htab->root.target_os != is_vxworks
   10196 	      && s->size > 0)
   10197 	    s->size += 4;
   10198 	}
   10199       else if (! startswith (name, ".init")
   10200 	       && s != htab->root.sgot
   10201 	       && s != htab->root.sgotplt
   10202 	       && s != htab->sstubs
   10203 	       && s != htab->root.sdynbss
   10204 	       && s != htab->root.sdynrelro)
   10205 	{
   10206 	  /* It's not one of our sections, so don't allocate space.  */
   10207 	  continue;
   10208 	}
   10209 
   10210       if (s->size == 0)
   10211 	{
   10212 	  s->flags |= SEC_EXCLUDE;
   10213 	  continue;
   10214 	}
   10215 
   10216       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   10217 	continue;
   10218 
   10219       /* Allocate memory for the section contents.  */
   10220       s->contents = bfd_zalloc (dynobj, s->size);
   10221       if (s->contents == NULL)
   10222 	{
   10223 	  bfd_set_error (bfd_error_no_memory);
   10224 	  return false;
   10225 	}
   10226     }
   10227 
   10228   if (htab->root.dynamic_sections_created)
   10229     {
   10230       /* Add some entries to the .dynamic section.  We fill in the
   10231 	 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
   10232 	 must add the entries now so that we get the correct size for
   10233 	 the .dynamic section.  */
   10234 
   10235       /* SGI object has the equivalence of DT_DEBUG in the
   10236 	 DT_MIPS_RLD_MAP entry.  This must come first because glibc
   10237 	 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
   10238 	 may only look at the first one they see.  */
   10239       if (!bfd_link_pic (info)
   10240 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
   10241 	return false;
   10242 
   10243       if (bfd_link_executable (info)
   10244 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
   10245 	return false;
   10246 
   10247       /* The DT_DEBUG entry may be filled in by the dynamic linker and
   10248 	 used by the debugger.  */
   10249       if (bfd_link_executable (info)
   10250 	  && !SGI_COMPAT (output_bfd)
   10251 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
   10252 	return false;
   10253 
   10254       if (reltext
   10255 	  && (SGI_COMPAT (output_bfd)
   10256 	      || htab->root.target_os == is_vxworks))
   10257 	info->flags |= DF_TEXTREL;
   10258 
   10259       if ((info->flags & DF_TEXTREL) != 0)
   10260 	{
   10261 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
   10262 	    return false;
   10263 
   10264 	  /* Clear the DF_TEXTREL flag.  It will be set again if we
   10265 	     write out an actual text relocation; we may not, because
   10266 	     at this point we do not know whether e.g. any .eh_frame
   10267 	     absolute relocations have been converted to PC-relative.  */
   10268 	  info->flags &= ~DF_TEXTREL;
   10269 	}
   10270 
   10271       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
   10272 	return false;
   10273 
   10274       sreldyn = mips_elf_rel_dyn_section (info, false);
   10275       if (htab->root.target_os == is_vxworks)
   10276 	{
   10277 	  /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
   10278 	     use any of the DT_MIPS_* tags.  */
   10279 	  if (sreldyn && sreldyn->size > 0)
   10280 	    {
   10281 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
   10282 		return false;
   10283 
   10284 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
   10285 		return false;
   10286 
   10287 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
   10288 		return false;
   10289 	    }
   10290 	}
   10291       else
   10292 	{
   10293 	  if (sreldyn && sreldyn->size > 0
   10294 	      && !bfd_is_abs_section (sreldyn->output_section))
   10295 	    {
   10296 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
   10297 		return false;
   10298 
   10299 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
   10300 		return false;
   10301 
   10302 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
   10303 		return false;
   10304 	    }
   10305 
   10306 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
   10307 	    return false;
   10308 
   10309 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
   10310 	    return false;
   10311 
   10312 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
   10313 	    return false;
   10314 
   10315 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
   10316 	    return false;
   10317 
   10318 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
   10319 	    return false;
   10320 
   10321 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
   10322 	    return false;
   10323 
   10324 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
   10325 	    return false;
   10326 
   10327 	  if (info->emit_gnu_hash
   10328 	      && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
   10329 	    return false;
   10330 
   10331 	  if (IRIX_COMPAT (dynobj) == ict_irix5
   10332 	      && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
   10333 	    return false;
   10334 
   10335 	  if (IRIX_COMPAT (dynobj) == ict_irix6
   10336 	      && (bfd_get_section_by_name
   10337 		  (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
   10338 	      && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
   10339 	    return false;
   10340 	}
   10341       if (htab->root.splt->size > 0)
   10342 	{
   10343 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
   10344 	    return false;
   10345 
   10346 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
   10347 	    return false;
   10348 
   10349 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
   10350 	    return false;
   10351 
   10352 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
   10353 	    return false;
   10354 	}
   10355       if (htab->root.target_os == is_vxworks
   10356 	  && !elf_vxworks_add_dynamic_entries (output_bfd, info))
   10357 	return false;
   10358     }
   10359 
   10360   return true;
   10361 }
   10362 
   10363 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
   10365    Adjust its R_ADDEND field so that it is correct for the output file.
   10366    LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
   10367    and sections respectively; both use symbol indexes.  */
   10368 
   10369 static void
   10370 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
   10371 			bfd *input_bfd, Elf_Internal_Sym *local_syms,
   10372 			asection **local_sections, Elf_Internal_Rela *rel)
   10373 {
   10374   unsigned int r_type, r_symndx;
   10375   Elf_Internal_Sym *sym;
   10376   asection *sec;
   10377 
   10378   if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
   10379     {
   10380       r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   10381       if (gprel16_reloc_p (r_type)
   10382 	  || r_type == R_MIPS_GPREL32
   10383 	  || literal_reloc_p (r_type))
   10384 	{
   10385 	  rel->r_addend += _bfd_get_gp_value (input_bfd);
   10386 	  rel->r_addend -= _bfd_get_gp_value (output_bfd);
   10387 	}
   10388 
   10389       r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
   10390       sym = local_syms + r_symndx;
   10391 
   10392       /* Adjust REL's addend to account for section merging.  */
   10393       if (!bfd_link_relocatable (info))
   10394 	{
   10395 	  sec = local_sections[r_symndx];
   10396 	  _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   10397 	}
   10398 
   10399       /* This would normally be done by the rela_normal code in elflink.c.  */
   10400       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   10401 	rel->r_addend += local_sections[r_symndx]->output_offset;
   10402     }
   10403 }
   10404 
   10405 /* Handle relocations against symbols from removed linkonce sections,
   10406    or sections discarded by a linker script.  We use this wrapper around
   10407    RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
   10408    on 64-bit ELF targets.  In this case for any relocation handled, which
   10409    always be the first in a triplet, the remaining two have to be processed
   10410    together with the first, even if they are R_MIPS_NONE.  It is the symbol
   10411    index referred by the first reloc that applies to all the three and the
   10412    remaining two never refer to an object symbol.  And it is the final
   10413    relocation (the last non-null one) that determines the output field of
   10414    the whole relocation so retrieve the corresponding howto structure for
   10415    the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
   10416 
   10417    Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
   10418    and therefore requires to be pasted in a loop.  It also defines a block
   10419    and does not protect any of its arguments, hence the extra brackets.  */
   10420 
   10421 static void
   10422 mips_reloc_against_discarded_section (bfd *output_bfd,
   10423 				      struct bfd_link_info *info,
   10424 				      bfd *input_bfd, asection *input_section,
   10425 				      Elf_Internal_Rela **rel,
   10426 				      const Elf_Internal_Rela **relend,
   10427 				      bool rel_reloc,
   10428 				      reloc_howto_type *howto,
   10429 				      bfd_byte *contents)
   10430 {
   10431   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   10432   int count = bed->s->int_rels_per_ext_rel;
   10433   unsigned int r_type;
   10434   int i;
   10435 
   10436   for (i = count - 1; i > 0; i--)
   10437     {
   10438       r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
   10439       if (r_type != R_MIPS_NONE)
   10440 	{
   10441 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
   10442 	  break;
   10443 	}
   10444     }
   10445   do
   10446     {
   10447        RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   10448 					(*rel), count, (*relend),
   10449 					howto, i, contents);
   10450     }
   10451   while (0);
   10452 }
   10453 
   10454 /* Relocate a MIPS ELF section.  */
   10455 
   10456 int
   10457 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
   10458 				bfd *input_bfd, asection *input_section,
   10459 				bfd_byte *contents, Elf_Internal_Rela *relocs,
   10460 				Elf_Internal_Sym *local_syms,
   10461 				asection **local_sections)
   10462 {
   10463   Elf_Internal_Rela *rel;
   10464   const Elf_Internal_Rela *relend;
   10465   bfd_vma addend = 0;
   10466   bool use_saved_addend_p = false;
   10467 
   10468   relend = relocs + input_section->reloc_count;
   10469   for (rel = relocs; rel < relend; ++rel)
   10470     {
   10471       const char *name;
   10472       bfd_vma value = 0;
   10473       reloc_howto_type *howto;
   10474       bool cross_mode_jump_p = false;
   10475       /* TRUE if the relocation is a RELA relocation, rather than a
   10476 	 REL relocation.  */
   10477       bool rela_relocation_p = true;
   10478       unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   10479       const char *msg;
   10480       unsigned long r_symndx;
   10481       asection *sec;
   10482       Elf_Internal_Shdr *symtab_hdr;
   10483       struct elf_link_hash_entry *h;
   10484       bool rel_reloc;
   10485 
   10486       rel_reloc = (NEWABI_P (input_bfd)
   10487 		   && mips_elf_rel_relocation_p (input_bfd, input_section,
   10488 						 relocs, rel));
   10489       /* Find the relocation howto for this relocation.  */
   10490       howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
   10491 
   10492       r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
   10493       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   10494       if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
   10495 	{
   10496 	  sec = local_sections[r_symndx];
   10497 	  h = NULL;
   10498 	}
   10499       else
   10500 	{
   10501 	  unsigned long extsymoff;
   10502 
   10503 	  extsymoff = 0;
   10504 	  if (!elf_bad_symtab (input_bfd))
   10505 	    extsymoff = symtab_hdr->sh_info;
   10506 	  h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
   10507 	  while (h->root.type == bfd_link_hash_indirect
   10508 		 || h->root.type == bfd_link_hash_warning)
   10509 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10510 
   10511 	  sec = NULL;
   10512 	  if (h->root.type == bfd_link_hash_defined
   10513 	      || h->root.type == bfd_link_hash_defweak)
   10514 	    sec = h->root.u.def.section;
   10515 	}
   10516 
   10517       if (sec != NULL && discarded_section (sec))
   10518 	{
   10519 	  mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
   10520 						input_section, &rel, &relend,
   10521 						rel_reloc, howto, contents);
   10522 	  continue;
   10523 	}
   10524 
   10525       if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
   10526 	{
   10527 	  /* Some 32-bit code uses R_MIPS_64.  In particular, people use
   10528 	     64-bit code, but make sure all their addresses are in the
   10529 	     lowermost or uppermost 32-bit section of the 64-bit address
   10530 	     space.  Thus, when they use an R_MIPS_64 they mean what is
   10531 	     usually meant by R_MIPS_32, with the exception that the
   10532 	     stored value is sign-extended to 64 bits.  */
   10533 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
   10534 
   10535 	  /* On big-endian systems, we need to lie about the position
   10536 	     of the reloc.  */
   10537 	  if (bfd_big_endian (input_bfd))
   10538 	    rel->r_offset += 4;
   10539 	}
   10540 
   10541       if (!use_saved_addend_p)
   10542 	{
   10543 	  /* If these relocations were originally of the REL variety,
   10544 	     we must pull the addend out of the field that will be
   10545 	     relocated.  Otherwise, we simply use the contents of the
   10546 	     RELA relocation.  */
   10547 	  if (mips_elf_rel_relocation_p (input_bfd, input_section,
   10548 					 relocs, rel))
   10549 	    {
   10550 	      rela_relocation_p = false;
   10551 	      addend = mips_elf_read_rel_addend (input_bfd, input_section,
   10552 						 rel, howto, contents);
   10553 	      if (hi16_reloc_p (r_type)
   10554 		  || (got16_reloc_p (r_type)
   10555 		      && mips_elf_local_relocation_p (input_bfd, rel,
   10556 						      local_sections)))
   10557 		{
   10558 		  if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
   10559 						     rel, relend,
   10560 						     contents, &addend))
   10561 		    {
   10562 		      if (h)
   10563 			name = h->root.root.string;
   10564 		      else
   10565 			name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   10566 						 local_syms + r_symndx,
   10567 						 sec);
   10568 		      _bfd_error_handler
   10569 			/* xgettext:c-format */
   10570 			(_("%pB: can't find matching LO16 reloc against `%s'"
   10571 			   " for %s at %#" PRIx64 " in section `%pA'"),
   10572 			 input_bfd, name,
   10573 			 howto->name, (uint64_t) rel->r_offset, input_section);
   10574 		    }
   10575 		}
   10576 	      else
   10577 		addend <<= howto->rightshift;
   10578 	    }
   10579 	  else
   10580 	    addend = rel->r_addend;
   10581 	  mips_elf_adjust_addend (output_bfd, info, input_bfd,
   10582 				  local_syms, local_sections, rel);
   10583 	}
   10584 
   10585       if (bfd_link_relocatable (info))
   10586 	{
   10587 	  if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
   10588 	      && bfd_big_endian (input_bfd))
   10589 	    rel->r_offset -= 4;
   10590 
   10591 	  if (!rela_relocation_p && rel->r_addend)
   10592 	    {
   10593 	      addend += rel->r_addend;
   10594 	      if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
   10595 		addend = mips_elf_high (addend);
   10596 	      else if (r_type == R_MIPS_HIGHER)
   10597 		addend = mips_elf_higher (addend);
   10598 	      else if (r_type == R_MIPS_HIGHEST)
   10599 		addend = mips_elf_highest (addend);
   10600 	      else
   10601 		addend >>= howto->rightshift;
   10602 
   10603 	      /* We use the source mask, rather than the destination
   10604 		 mask because the place to which we are writing will be
   10605 		 source of the addend in the final link.  */
   10606 	      addend &= howto->src_mask;
   10607 
   10608 	      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
   10609 		/* See the comment above about using R_MIPS_64 in the 32-bit
   10610 		   ABI.  Here, we need to update the addend.  It would be
   10611 		   possible to get away with just using the R_MIPS_32 reloc
   10612 		   but for endianness.  */
   10613 		{
   10614 		  bfd_vma sign_bits;
   10615 		  bfd_vma low_bits;
   10616 		  bfd_vma high_bits;
   10617 
   10618 		  if (addend & ((bfd_vma) 1 << 31))
   10619 #ifdef BFD64
   10620 		    sign_bits = ((bfd_vma) 1 << 32) - 1;
   10621 #else
   10622 		    sign_bits = -1;
   10623 #endif
   10624 		  else
   10625 		    sign_bits = 0;
   10626 
   10627 		  /* If we don't know that we have a 64-bit type,
   10628 		     do two separate stores.  */
   10629 		  if (bfd_big_endian (input_bfd))
   10630 		    {
   10631 		      /* Store the sign-bits (which are most significant)
   10632 			 first.  */
   10633 		      low_bits = sign_bits;
   10634 		      high_bits = addend;
   10635 		    }
   10636 		  else
   10637 		    {
   10638 		      low_bits = addend;
   10639 		      high_bits = sign_bits;
   10640 		    }
   10641 		  bfd_put_32 (input_bfd, low_bits,
   10642 			      contents + rel->r_offset);
   10643 		  bfd_put_32 (input_bfd, high_bits,
   10644 			      contents + rel->r_offset + 4);
   10645 		  continue;
   10646 		}
   10647 
   10648 	      if (! mips_elf_perform_relocation (info, howto, rel, addend,
   10649 						 input_bfd, input_section,
   10650 						 contents, false))
   10651 		return false;
   10652 	    }
   10653 
   10654 	  /* Go on to the next relocation.  */
   10655 	  continue;
   10656 	}
   10657 
   10658       /* In the N32 and 64-bit ABIs there may be multiple consecutive
   10659 	 relocations for the same offset.  In that case we are
   10660 	 supposed to treat the output of each relocation as the addend
   10661 	 for the next.  */
   10662       if (rel + 1 < relend
   10663 	  && rel->r_offset == rel[1].r_offset
   10664 	  && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
   10665 	use_saved_addend_p = true;
   10666       else
   10667 	use_saved_addend_p = false;
   10668 
   10669       /* Figure out what value we are supposed to relocate.  */
   10670       switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
   10671 					     input_section, contents,
   10672 					     info, rel, addend, howto,
   10673 					     local_syms, local_sections,
   10674 					     &value, &name, &cross_mode_jump_p,
   10675 					     use_saved_addend_p))
   10676 	{
   10677 	case bfd_reloc_continue:
   10678 	  /* There's nothing to do.  */
   10679 	  continue;
   10680 
   10681 	case bfd_reloc_undefined:
   10682 	  /* mips_elf_calculate_relocation already called the
   10683 	     undefined_symbol callback.  There's no real point in
   10684 	     trying to perform the relocation at this point, so we
   10685 	     just skip ahead to the next relocation.  */
   10686 	  continue;
   10687 
   10688 	case bfd_reloc_notsupported:
   10689 	  msg = _("internal error: unsupported relocation error");
   10690 	  info->callbacks->warning
   10691 	    (info, msg, name, input_bfd, input_section, rel->r_offset);
   10692 	  return false;
   10693 
   10694 	case bfd_reloc_overflow:
   10695 	  if (use_saved_addend_p)
   10696 	    /* Ignore overflow until we reach the last relocation for
   10697 	       a given location.  */
   10698 	    ;
   10699 	  else
   10700 	    {
   10701 	      struct mips_elf_link_hash_table *htab;
   10702 
   10703 	      htab = mips_elf_hash_table (info);
   10704 	      BFD_ASSERT (htab != NULL);
   10705 	      BFD_ASSERT (name != NULL);
   10706 	      if (!htab->small_data_overflow_reported
   10707 		  && (gprel16_reloc_p (howto->type)
   10708 		      || literal_reloc_p (howto->type)))
   10709 		{
   10710 		  msg = _("small-data section too large;"
   10711 			  " lower small-data size limit (see option -G)");
   10712 
   10713 		  htab->small_data_overflow_reported = true;
   10714 		  (*info->callbacks->einfo) ("%P: %s\n", msg);
   10715 		}
   10716 	      (*info->callbacks->reloc_overflow)
   10717 		(info, NULL, name, howto->name, (bfd_vma) 0,
   10718 		 input_bfd, input_section, rel->r_offset);
   10719 	    }
   10720 	  break;
   10721 
   10722 	case bfd_reloc_ok:
   10723 	  break;
   10724 
   10725 	case bfd_reloc_outofrange:
   10726 	  msg = NULL;
   10727 	  if (jal_reloc_p (howto->type))
   10728 	    msg = (cross_mode_jump_p
   10729 		   ? _("cannot convert a jump to JALX "
   10730 		       "for a non-word-aligned address")
   10731 		   : (howto->type == R_MIPS16_26
   10732 		      ? _("jump to a non-word-aligned address")
   10733 		      : _("jump to a non-instruction-aligned address")));
   10734 	  else if (b_reloc_p (howto->type))
   10735 	    msg = (cross_mode_jump_p
   10736 		   ? _("cannot convert a branch to JALX "
   10737 		       "for a non-word-aligned address")
   10738 		   : _("branch to a non-instruction-aligned address"));
   10739 	  else if (aligned_pcrel_reloc_p (howto->type))
   10740 	    msg = _("PC-relative load from unaligned address");
   10741 	  if (msg)
   10742 	    {
   10743 	      info->callbacks->einfo
   10744 		("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
   10745 	      break;
   10746 	    }
   10747 	  /* Fall through.  */
   10748 
   10749 	default:
   10750 	  abort ();
   10751 	  break;
   10752 	}
   10753 
   10754       /* If we've got another relocation for the address, keep going
   10755 	 until we reach the last one.  */
   10756       if (use_saved_addend_p)
   10757 	{
   10758 	  addend = value;
   10759 	  continue;
   10760 	}
   10761 
   10762       if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
   10763 	/* See the comment above about using R_MIPS_64 in the 32-bit
   10764 	   ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
   10765 	   that calculated the right value.  Now, however, we
   10766 	   sign-extend the 32-bit result to 64-bits, and store it as a
   10767 	   64-bit value.  We are especially generous here in that we
   10768 	   go to extreme lengths to support this usage on systems with
   10769 	   only a 32-bit VMA.  */
   10770 	{
   10771 	  bfd_vma sign_bits;
   10772 	  bfd_vma low_bits;
   10773 	  bfd_vma high_bits;
   10774 
   10775 	  if (value & ((bfd_vma) 1 << 31))
   10776 #ifdef BFD64
   10777 	    sign_bits = ((bfd_vma) 1 << 32) - 1;
   10778 #else
   10779 	    sign_bits = -1;
   10780 #endif
   10781 	  else
   10782 	    sign_bits = 0;
   10783 
   10784 	  /* If we don't know that we have a 64-bit type,
   10785 	     do two separate stores.  */
   10786 	  if (bfd_big_endian (input_bfd))
   10787 	    {
   10788 	      /* Undo what we did above.  */
   10789 	      rel->r_offset -= 4;
   10790 	      /* Store the sign-bits (which are most significant)
   10791 		 first.  */
   10792 	      low_bits = sign_bits;
   10793 	      high_bits = value;
   10794 	    }
   10795 	  else
   10796 	    {
   10797 	      low_bits = value;
   10798 	      high_bits = sign_bits;
   10799 	    }
   10800 	  bfd_put_32 (input_bfd, low_bits,
   10801 		      contents + rel->r_offset);
   10802 	  bfd_put_32 (input_bfd, high_bits,
   10803 		      contents + rel->r_offset + 4);
   10804 	  continue;
   10805 	}
   10806 
   10807       /* Actually perform the relocation.  */
   10808       if (! mips_elf_perform_relocation (info, howto, rel, value,
   10809 					 input_bfd, input_section,
   10810 					 contents, cross_mode_jump_p))
   10811 	return false;
   10812     }
   10813 
   10814   return true;
   10815 }
   10816 
   10817 /* A function that iterates over each entry in la25_stubs and fills
   10819    in the code for each one.  DATA points to a mips_htab_traverse_info.  */
   10820 
   10821 static int
   10822 mips_elf_create_la25_stub (void **slot, void *data)
   10823 {
   10824   struct mips_htab_traverse_info *hti;
   10825   struct mips_elf_link_hash_table *htab;
   10826   struct mips_elf_la25_stub *stub;
   10827   asection *s;
   10828   bfd_byte *loc;
   10829   bfd_vma offset, target, target_high, target_low;
   10830   bfd_vma branch_pc;
   10831   bfd_signed_vma pcrel_offset = 0;
   10832 
   10833   stub = (struct mips_elf_la25_stub *) *slot;
   10834   hti = (struct mips_htab_traverse_info *) data;
   10835   htab = mips_elf_hash_table (hti->info);
   10836   BFD_ASSERT (htab != NULL);
   10837 
   10838   /* Create the section contents, if we haven't already.  */
   10839   s = stub->stub_section;
   10840   loc = s->contents;
   10841   if (loc == NULL)
   10842     {
   10843       loc = bfd_malloc (s->size);
   10844       if (loc == NULL)
   10845 	{
   10846 	  hti->error = true;
   10847 	  return false;
   10848 	}
   10849       s->contents = loc;
   10850     }
   10851 
   10852   /* Work out where in the section this stub should go.  */
   10853   offset = stub->offset;
   10854 
   10855   /* We add 8 here to account for the LUI/ADDIU instructions
   10856      before the branch instruction.  This cannot be moved down to
   10857      where pcrel_offset is calculated as 's' is updated in
   10858      mips_elf_get_la25_target.  */
   10859   branch_pc = s->output_section->vma + s->output_offset + offset + 8;
   10860 
   10861   /* Work out the target address.  */
   10862   target = mips_elf_get_la25_target (stub, &s);
   10863   target += s->output_section->vma + s->output_offset;
   10864 
   10865   target_high = ((target + 0x8000) >> 16) & 0xffff;
   10866   target_low = (target & 0xffff);
   10867 
   10868   /* Calculate the PC of the compact branch instruction (for the case where
   10869      compact branches are used for either microMIPSR6 or MIPSR6 with
   10870      compact branches.  Add 4-bytes to account for BC using the PC of the
   10871      next instruction as the base.  */
   10872   pcrel_offset = target - (branch_pc + 4);
   10873 
   10874   if (stub->stub_section != htab->strampoline)
   10875     {
   10876       /* This is a simple LUI/ADDIU stub.  Zero out the beginning
   10877 	 of the section and write the two instructions at the end.  */
   10878       memset (loc, 0, offset);
   10879       loc += offset;
   10880       if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
   10881 	{
   10882 	  bfd_put_micromips_32 (hti->output_bfd,
   10883 				LA25_LUI_MICROMIPS (target_high),
   10884 				loc);
   10885 	  bfd_put_micromips_32 (hti->output_bfd,
   10886 				LA25_ADDIU_MICROMIPS (target_low),
   10887 				loc + 4);
   10888 	}
   10889       else
   10890 	{
   10891 	  bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
   10892 	  bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
   10893 	}
   10894     }
   10895   else
   10896     {
   10897       /* This is trampoline.  */
   10898       loc += offset;
   10899       if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
   10900 	{
   10901 	  bfd_put_micromips_32 (hti->output_bfd,
   10902 				LA25_LUI_MICROMIPS (target_high), loc);
   10903 	  bfd_put_micromips_32 (hti->output_bfd,
   10904 				LA25_J_MICROMIPS (target), loc + 4);
   10905 	  bfd_put_micromips_32 (hti->output_bfd,
   10906 				LA25_ADDIU_MICROMIPS (target_low), loc + 8);
   10907 	  bfd_put_32 (hti->output_bfd, 0, loc + 12);
   10908 	}
   10909       else
   10910 	{
   10911 	  bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
   10912 	  if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
   10913 	    {
   10914 	      bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
   10915 	      bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
   10916 	    }
   10917 	  else
   10918 	    {
   10919 	      bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
   10920 	      bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
   10921 	    }
   10922 	  bfd_put_32 (hti->output_bfd, 0, loc + 12);
   10923 	}
   10924     }
   10925   return true;
   10926 }
   10927 
   10928 /* If NAME is one of the special IRIX6 symbols defined by the linker,
   10929    adjust it appropriately now.  */
   10930 
   10931 static void
   10932 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
   10933 				      const char *name, Elf_Internal_Sym *sym)
   10934 {
   10935   /* The linker script takes care of providing names and values for
   10936      these, but we must place them into the right sections.  */
   10937   static const char* const text_section_symbols[] = {
   10938     "_ftext",
   10939     "_etext",
   10940     "__dso_displacement",
   10941     "__elf_header",
   10942     "__program_header_table",
   10943     NULL
   10944   };
   10945 
   10946   static const char* const data_section_symbols[] = {
   10947     "_fdata",
   10948     "_edata",
   10949     "_end",
   10950     "_fbss",
   10951     NULL
   10952   };
   10953 
   10954   const char* const *p;
   10955   int i;
   10956 
   10957   for (i = 0; i < 2; ++i)
   10958     for (p = (i == 0) ? text_section_symbols : data_section_symbols;
   10959 	 *p;
   10960 	 ++p)
   10961       if (strcmp (*p, name) == 0)
   10962 	{
   10963 	  /* All of these symbols are given type STT_SECTION by the
   10964 	     IRIX6 linker.  */
   10965 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   10966 	  sym->st_other = STO_PROTECTED;
   10967 
   10968 	  /* The IRIX linker puts these symbols in special sections.  */
   10969 	  if (i == 0)
   10970 	    sym->st_shndx = SHN_MIPS_TEXT;
   10971 	  else
   10972 	    sym->st_shndx = SHN_MIPS_DATA;
   10973 
   10974 	  break;
   10975 	}
   10976 }
   10977 
   10978 /* Finish up dynamic symbol handling.  We set the contents of various
   10979    dynamic sections here.  */
   10980 
   10981 bool
   10982 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
   10983 				     struct bfd_link_info *info,
   10984 				     struct elf_link_hash_entry *h,
   10985 				     Elf_Internal_Sym *sym)
   10986 {
   10987   bfd *dynobj;
   10988   asection *sgot;
   10989   struct mips_got_info *g, *gg;
   10990   const char *name;
   10991   int idx;
   10992   struct mips_elf_link_hash_table *htab;
   10993   struct mips_elf_link_hash_entry *hmips;
   10994 
   10995   htab = mips_elf_hash_table (info);
   10996   BFD_ASSERT (htab != NULL);
   10997   dynobj = elf_hash_table (info)->dynobj;
   10998   hmips = (struct mips_elf_link_hash_entry *) h;
   10999 
   11000   BFD_ASSERT (htab->root.target_os != is_vxworks);
   11001 
   11002   if (h->plt.plist != NULL
   11003       && (h->plt.plist->mips_offset != MINUS_ONE
   11004 	  || h->plt.plist->comp_offset != MINUS_ONE))
   11005     {
   11006       /* We've decided to create a PLT entry for this symbol.  */
   11007       bfd_byte *loc;
   11008       bfd_vma header_address, got_address;
   11009       bfd_vma got_address_high, got_address_low, load;
   11010       bfd_vma got_index;
   11011       bfd_vma isa_bit;
   11012 
   11013       got_index = h->plt.plist->gotplt_index;
   11014 
   11015       BFD_ASSERT (htab->use_plts_and_copy_relocs);
   11016       BFD_ASSERT (h->dynindx != -1);
   11017       BFD_ASSERT (htab->root.splt != NULL);
   11018       BFD_ASSERT (got_index != MINUS_ONE);
   11019       BFD_ASSERT (!h->def_regular);
   11020 
   11021       /* Calculate the address of the PLT header.  */
   11022       isa_bit = htab->plt_header_is_comp;
   11023       header_address = (htab->root.splt->output_section->vma
   11024 			+ htab->root.splt->output_offset + isa_bit);
   11025 
   11026       /* Calculate the address of the .got.plt entry.  */
   11027       got_address = (htab->root.sgotplt->output_section->vma
   11028 		     + htab->root.sgotplt->output_offset
   11029 		     + got_index * MIPS_ELF_GOT_SIZE (dynobj));
   11030 
   11031       got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
   11032       got_address_low = got_address & 0xffff;
   11033 
   11034       /* The PLT sequence is not safe for N64 if .got.plt entry's address
   11035 	 cannot be loaded in two instructions.  */
   11036       if (ABI_64_P (output_bfd)
   11037 	  && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
   11038 	{
   11039 	  _bfd_error_handler
   11040 	    /* xgettext:c-format */
   11041 	    (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
   11042 	       "supported; consider using `-Ttext-segment=...'"),
   11043 	     output_bfd,
   11044 	     htab->root.sgotplt->output_section,
   11045 	     (int64_t) got_address);
   11046 	  bfd_set_error (bfd_error_no_error);
   11047 	  return false;
   11048 	}
   11049 
   11050       /* Initially point the .got.plt entry at the PLT header.  */
   11051       loc = (htab->root.sgotplt->contents
   11052 	     + got_index * MIPS_ELF_GOT_SIZE (dynobj));
   11053       if (ABI_64_P (output_bfd))
   11054 	bfd_put_64 (output_bfd, header_address, loc);
   11055       else
   11056 	bfd_put_32 (output_bfd, header_address, loc);
   11057 
   11058       /* Now handle the PLT itself.  First the standard entry (the order
   11059 	 does not matter, we just have to pick one).  */
   11060       if (h->plt.plist->mips_offset != MINUS_ONE)
   11061 	{
   11062 	  const bfd_vma *plt_entry;
   11063 	  bfd_vma plt_offset;
   11064 
   11065 	  plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
   11066 
   11067 	  BFD_ASSERT (plt_offset <= htab->root.splt->size);
   11068 
   11069 	  /* Find out where the .plt entry should go.  */
   11070 	  loc = htab->root.splt->contents + plt_offset;
   11071 
   11072 	  /* Pick the load opcode.  */
   11073 	  load = MIPS_ELF_LOAD_WORD (output_bfd);
   11074 
   11075 	  /* Fill in the PLT entry itself.  */
   11076 
   11077 	  if (MIPSR6_P (output_bfd))
   11078 	    plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
   11079 					       : mipsr6_exec_plt_entry;
   11080 	  else
   11081 	    plt_entry = mips_exec_plt_entry;
   11082 	  bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
   11083 	  bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
   11084 		      loc + 4);
   11085 
   11086 	  if (! LOAD_INTERLOCKS_P (output_bfd)
   11087 	      || (MIPSR6_P (output_bfd) && htab->compact_branches))
   11088 	    {
   11089 	      bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
   11090 	      bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   11091 	    }
   11092 	  else
   11093 	    {
   11094 	      bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
   11095 	      bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
   11096 			  loc + 12);
   11097 	    }
   11098 	}
   11099 
   11100       /* Now the compressed entry.  They come after any standard ones.  */
   11101       if (h->plt.plist->comp_offset != MINUS_ONE)
   11102 	{
   11103 	  bfd_vma plt_offset;
   11104 
   11105 	  plt_offset = (htab->plt_header_size + htab->plt_mips_offset
   11106 			+ h->plt.plist->comp_offset);
   11107 
   11108 	  BFD_ASSERT (plt_offset <= htab->root.splt->size);
   11109 
   11110 	  /* Find out where the .plt entry should go.  */
   11111 	  loc = htab->root.splt->contents + plt_offset;
   11112 
   11113 	  /* Fill in the PLT entry itself.  */
   11114 	  if (!MICROMIPS_P (output_bfd))
   11115 	    {
   11116 	      const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
   11117 
   11118 	      bfd_put_16 (output_bfd, plt_entry[0], loc);
   11119 	      bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
   11120 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   11121 	      bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
   11122 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   11123 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   11124 	      bfd_put_32 (output_bfd, got_address, loc + 12);
   11125 	    }
   11126 	  else if (htab->insn32)
   11127 	    {
   11128 	      const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
   11129 
   11130 	      bfd_put_16 (output_bfd, plt_entry[0], loc);
   11131 	      bfd_put_16 (output_bfd, got_address_high, loc + 2);
   11132 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   11133 	      bfd_put_16 (output_bfd, got_address_low, loc + 6);
   11134 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   11135 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   11136 	      bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
   11137 	      bfd_put_16 (output_bfd, got_address_low, loc + 14);
   11138 	    }
   11139 	  else
   11140 	    {
   11141 	      const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
   11142 	      bfd_signed_vma gotpc_offset;
   11143 	      bfd_vma loc_address;
   11144 
   11145 	      BFD_ASSERT (got_address % 4 == 0);
   11146 
   11147 	      loc_address = (htab->root.splt->output_section->vma
   11148 			     + htab->root.splt->output_offset + plt_offset);
   11149 	      gotpc_offset = got_address - ((loc_address | 3) ^ 3);
   11150 
   11151 	      /* ADDIUPC has a span of +/-16MB, check we're in range.  */
   11152 	      if (gotpc_offset + 0x1000000 >= 0x2000000)
   11153 		{
   11154 		  _bfd_error_handler
   11155 		    /* xgettext:c-format */
   11156 		    (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
   11157 		       "beyond the range of ADDIUPC"),
   11158 		     output_bfd,
   11159 		     htab->root.sgotplt->output_section,
   11160 		     (int64_t) gotpc_offset,
   11161 		     htab->root.splt->output_section);
   11162 		  bfd_set_error (bfd_error_no_error);
   11163 		  return false;
   11164 		}
   11165 	      bfd_put_16 (output_bfd,
   11166 			  plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
   11167 	      bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
   11168 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   11169 	      bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
   11170 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   11171 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   11172 	    }
   11173 	}
   11174 
   11175       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
   11176       mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
   11177 					  got_index - 2, h->dynindx,
   11178 					  R_MIPS_JUMP_SLOT, got_address);
   11179 
   11180       /* We distinguish between PLT entries and lazy-binding stubs by
   11181 	 giving the former an st_other value of STO_MIPS_PLT.  Set the
   11182 	 flag and leave the value if there are any relocations in the
   11183 	 binary where pointer equality matters.  */
   11184       sym->st_shndx = SHN_UNDEF;
   11185       if (h->pointer_equality_needed)
   11186 	sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
   11187       else
   11188 	{
   11189 	  sym->st_value = 0;
   11190 	  sym->st_other = 0;
   11191 	}
   11192     }
   11193 
   11194   if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
   11195     {
   11196       /* We've decided to create a lazy-binding stub.  */
   11197       bool micromips_p = MICROMIPS_P (output_bfd);
   11198       unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   11199       bfd_vma stub_size = htab->function_stub_size;
   11200       bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
   11201       bfd_vma isa_bit = micromips_p;
   11202       bfd_vma stub_big_size;
   11203 
   11204       if (!micromips_p)
   11205 	stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
   11206       else if (htab->insn32)
   11207 	stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
   11208       else
   11209 	stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
   11210 
   11211       /* This symbol has a stub.  Set it up.  */
   11212 
   11213       BFD_ASSERT (h->dynindx != -1);
   11214 
   11215       BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
   11216 
   11217       /* Values up to 2^31 - 1 are allowed.  Larger values would cause
   11218 	 sign extension at runtime in the stub, resulting in a negative
   11219 	 index value.  */
   11220       if (h->dynindx & ~0x7fffffff)
   11221 	{
   11222 	  _bfd_error_handler
   11223 	    (_("%pB: cannot handle more than %d dynamic symbols"),
   11224 	     output_bfd, 0x7fffffff);
   11225 	  bfd_set_error (bfd_error_bad_value);
   11226 	  return false;
   11227 	}
   11228 
   11229       /* Fill the stub.  */
   11230       if (micromips_p)
   11231 	{
   11232 	  idx = 0;
   11233 	  bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
   11234 				stub + idx);
   11235 	  idx += 4;
   11236 	  if (htab->insn32)
   11237 	    {
   11238 	      bfd_put_micromips_32 (output_bfd,
   11239 				    STUB_MOVE32_MICROMIPS, stub + idx);
   11240 	      idx += 4;
   11241 	    }
   11242 	  else
   11243 	    {
   11244 	      bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
   11245 	      idx += 2;
   11246 	    }
   11247 	  if (stub_size == stub_big_size)
   11248 	    {
   11249 	      long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
   11250 
   11251 	      bfd_put_micromips_32 (output_bfd,
   11252 				    STUB_LUI_MICROMIPS (dynindx_hi),
   11253 				    stub + idx);
   11254 	      idx += 4;
   11255 	    }
   11256 	  if (htab->insn32)
   11257 	    {
   11258 	      bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
   11259 				    stub + idx);
   11260 	      idx += 4;
   11261 	    }
   11262 	  else
   11263 	    {
   11264 	      bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
   11265 	      idx += 2;
   11266 	    }
   11267 
   11268 	  /* If a large stub is not required and sign extension is not a
   11269 	     problem, then use legacy code in the stub.  */
   11270 	  if (stub_size == stub_big_size)
   11271 	    bfd_put_micromips_32 (output_bfd,
   11272 				  STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
   11273 				  stub + idx);
   11274 	  else if (h->dynindx & ~0x7fff)
   11275 	    bfd_put_micromips_32 (output_bfd,
   11276 				  STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
   11277 				  stub + idx);
   11278 	  else
   11279 	    bfd_put_micromips_32 (output_bfd,
   11280 				  STUB_LI16S_MICROMIPS (output_bfd,
   11281 							h->dynindx),
   11282 				  stub + idx);
   11283 	}
   11284       else
   11285 	{
   11286 	  idx = 0;
   11287 	  bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
   11288 	  idx += 4;
   11289 	  bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
   11290 	  idx += 4;
   11291 	  if (stub_size == stub_big_size)
   11292 	    {
   11293 	      bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
   11294 			  stub + idx);
   11295 	      idx += 4;
   11296 	    }
   11297 
   11298 	  if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
   11299 	    {
   11300 	      bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
   11301 	      idx += 4;
   11302 	    }
   11303 
   11304 	  /* If a large stub is not required and sign extension is not a
   11305 	     problem, then use legacy code in the stub.  */
   11306 	  if (stub_size == stub_big_size)
   11307 	    bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
   11308 			stub + idx);
   11309 	  else if (h->dynindx & ~0x7fff)
   11310 	    bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
   11311 			stub + idx);
   11312 	  else
   11313 	    bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
   11314 			stub + idx);
   11315 	  idx += 4;
   11316 
   11317 	  if (MIPSR6_P (output_bfd) && htab->compact_branches)
   11318 	    bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
   11319 	}
   11320 
   11321       BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
   11322       memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
   11323 	      stub, stub_size);
   11324 
   11325       /* Mark the symbol as undefined.  stub_offset != -1 occurs
   11326 	 only for the referenced symbol.  */
   11327       sym->st_shndx = SHN_UNDEF;
   11328 
   11329       /* The run-time linker uses the st_value field of the symbol
   11330 	 to reset the global offset table entry for this external
   11331 	 to its stub address when unlinking a shared object.  */
   11332       sym->st_value = (htab->sstubs->output_section->vma
   11333 		       + htab->sstubs->output_offset
   11334 		       + h->plt.plist->stub_offset
   11335 		       + isa_bit);
   11336       sym->st_other = other;
   11337     }
   11338 
   11339   /* If we have a MIPS16 function with a stub, the dynamic symbol must
   11340      refer to the stub, since only the stub uses the standard calling
   11341      conventions.  */
   11342   if (h->dynindx != -1 && hmips->fn_stub != NULL)
   11343     {
   11344       BFD_ASSERT (hmips->need_fn_stub);
   11345       sym->st_value = (hmips->fn_stub->output_section->vma
   11346 		       + hmips->fn_stub->output_offset);
   11347       sym->st_size = hmips->fn_stub->size;
   11348       sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
   11349     }
   11350 
   11351   BFD_ASSERT (h->dynindx != -1
   11352 	      || h->forced_local);
   11353 
   11354   sgot = htab->root.sgot;
   11355   g = htab->got_info;
   11356   BFD_ASSERT (g != NULL);
   11357 
   11358   /* Run through the global symbol table, creating GOT entries for all
   11359      the symbols that need them.  */
   11360   if (hmips->global_got_area != GGA_NONE)
   11361     {
   11362       bfd_vma offset;
   11363       bfd_vma value;
   11364 
   11365       value = sym->st_value;
   11366       offset = mips_elf_primary_global_got_index (output_bfd, info, h);
   11367       MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
   11368     }
   11369 
   11370   if (hmips->global_got_area != GGA_NONE && g->next)
   11371     {
   11372       struct mips_got_entry e, *p;
   11373       bfd_vma entry;
   11374       bfd_vma offset;
   11375 
   11376       gg = g;
   11377 
   11378       e.abfd = output_bfd;
   11379       e.symndx = -1;
   11380       e.d.h = hmips;
   11381       e.tls_type = GOT_TLS_NONE;
   11382 
   11383       for (g = g->next; g->next != gg; g = g->next)
   11384 	{
   11385 	  if (g->got_entries
   11386 	      && (p = (struct mips_got_entry *) htab_find (g->got_entries,
   11387 							   &e)))
   11388 	    {
   11389 	      offset = p->gotidx;
   11390 	      BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
   11391 	      if (bfd_link_pic (info)
   11392 		  || (elf_hash_table (info)->dynamic_sections_created
   11393 		      && p->d.h != NULL
   11394 		      && p->d.h->root.def_dynamic
   11395 		      && !p->d.h->root.def_regular))
   11396 		{
   11397 		  /* Create an R_MIPS_REL32 relocation for this entry.  Due to
   11398 		     the various compatibility problems, it's easier to mock
   11399 		     up an R_MIPS_32 or R_MIPS_64 relocation and leave
   11400 		     mips_elf_create_dynamic_relocation to calculate the
   11401 		     appropriate addend.  */
   11402 		  Elf_Internal_Rela rel[3];
   11403 
   11404 		  memset (rel, 0, sizeof (rel));
   11405 		  if (ABI_64_P (output_bfd))
   11406 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
   11407 		  else
   11408 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
   11409 		  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
   11410 
   11411 		  entry = 0;
   11412 		  if (! (mips_elf_create_dynamic_relocation
   11413 			 (output_bfd, info, rel,
   11414 			  e.d.h, NULL, sym->st_value, &entry, sgot)))
   11415 		    return false;
   11416 		}
   11417 	      else
   11418 		entry = sym->st_value;
   11419 	      MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
   11420 	    }
   11421 	}
   11422     }
   11423 
   11424   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
   11425   name = h->root.root.string;
   11426   if (h == elf_hash_table (info)->hdynamic
   11427       || h == elf_hash_table (info)->hgot)
   11428     sym->st_shndx = SHN_ABS;
   11429   else if (strcmp (name, "_DYNAMIC_LINK") == 0
   11430 	   || strcmp (name, "_DYNAMIC_LINKING") == 0)
   11431     {
   11432       sym->st_shndx = SHN_ABS;
   11433       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   11434       sym->st_value = 1;
   11435     }
   11436   else if (SGI_COMPAT (output_bfd))
   11437     {
   11438       if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
   11439 	  || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
   11440 	{
   11441 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   11442 	  sym->st_other = STO_PROTECTED;
   11443 	  sym->st_value = 0;
   11444 	  sym->st_shndx = SHN_MIPS_DATA;
   11445 	}
   11446       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
   11447 	{
   11448 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   11449 	  sym->st_other = STO_PROTECTED;
   11450 	  sym->st_value = mips_elf_hash_table (info)->procedure_count;
   11451 	  sym->st_shndx = SHN_ABS;
   11452 	}
   11453       else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
   11454 	{
   11455 	  if (h->type == STT_FUNC)
   11456 	    sym->st_shndx = SHN_MIPS_TEXT;
   11457 	  else if (h->type == STT_OBJECT)
   11458 	    sym->st_shndx = SHN_MIPS_DATA;
   11459 	}
   11460     }
   11461 
   11462   /* Emit a copy reloc, if needed.  */
   11463   if (h->needs_copy)
   11464     {
   11465       asection *s;
   11466       bfd_vma symval;
   11467 
   11468       BFD_ASSERT (h->dynindx != -1);
   11469       BFD_ASSERT (htab->use_plts_and_copy_relocs);
   11470 
   11471       s = mips_elf_rel_dyn_section (info, false);
   11472       symval = (h->root.u.def.section->output_section->vma
   11473 		+ h->root.u.def.section->output_offset
   11474 		+ h->root.u.def.value);
   11475       mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
   11476 					  h->dynindx, R_MIPS_COPY, symval);
   11477     }
   11478 
   11479   /* Handle the IRIX6-specific symbols.  */
   11480   if (IRIX_COMPAT (output_bfd) == ict_irix6)
   11481     mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
   11482 
   11483   /* Keep dynamic compressed symbols odd.  This allows the dynamic linker
   11484      to treat compressed symbols like any other.  */
   11485   if (ELF_ST_IS_MIPS16 (sym->st_other))
   11486     {
   11487       BFD_ASSERT (sym->st_value & 1);
   11488       sym->st_other -= STO_MIPS16;
   11489     }
   11490   else if (ELF_ST_IS_MICROMIPS (sym->st_other))
   11491     {
   11492       BFD_ASSERT (sym->st_value & 1);
   11493       sym->st_other -= STO_MICROMIPS;
   11494     }
   11495 
   11496   return true;
   11497 }
   11498 
   11499 /* Likewise, for VxWorks.  */
   11500 
   11501 bool
   11502 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
   11503 					 struct bfd_link_info *info,
   11504 					 struct elf_link_hash_entry *h,
   11505 					 Elf_Internal_Sym *sym)
   11506 {
   11507   bfd *dynobj;
   11508   asection *sgot;
   11509   struct mips_got_info *g;
   11510   struct mips_elf_link_hash_table *htab;
   11511   struct mips_elf_link_hash_entry *hmips;
   11512 
   11513   htab = mips_elf_hash_table (info);
   11514   BFD_ASSERT (htab != NULL);
   11515   dynobj = elf_hash_table (info)->dynobj;
   11516   hmips = (struct mips_elf_link_hash_entry *) h;
   11517 
   11518   if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
   11519     {
   11520       bfd_byte *loc;
   11521       bfd_vma plt_address, got_address, got_offset, branch_offset;
   11522       Elf_Internal_Rela rel;
   11523       static const bfd_vma *plt_entry;
   11524       bfd_vma gotplt_index;
   11525       bfd_vma plt_offset;
   11526 
   11527       plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
   11528       gotplt_index = h->plt.plist->gotplt_index;
   11529 
   11530       BFD_ASSERT (h->dynindx != -1);
   11531       BFD_ASSERT (htab->root.splt != NULL);
   11532       BFD_ASSERT (gotplt_index != MINUS_ONE);
   11533       BFD_ASSERT (plt_offset <= htab->root.splt->size);
   11534 
   11535       /* Calculate the address of the .plt entry.  */
   11536       plt_address = (htab->root.splt->output_section->vma
   11537 		     + htab->root.splt->output_offset
   11538 		     + plt_offset);
   11539 
   11540       /* Calculate the address of the .got.plt entry.  */
   11541       got_address = (htab->root.sgotplt->output_section->vma
   11542 		     + htab->root.sgotplt->output_offset
   11543 		     + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
   11544 
   11545       /* Calculate the offset of the .got.plt entry from
   11546 	 _GLOBAL_OFFSET_TABLE_.  */
   11547       got_offset = mips_elf_gotplt_index (info, h);
   11548 
   11549       /* Calculate the offset for the branch at the start of the PLT
   11550 	 entry.  The branch jumps to the beginning of .plt.  */
   11551       branch_offset = -(plt_offset / 4 + 1) & 0xffff;
   11552 
   11553       /* Fill in the initial value of the .got.plt entry.  */
   11554       bfd_put_32 (output_bfd, plt_address,
   11555 		  (htab->root.sgotplt->contents
   11556 		   + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
   11557 
   11558       /* Find out where the .plt entry should go.  */
   11559       loc = htab->root.splt->contents + plt_offset;
   11560 
   11561       if (bfd_link_pic (info))
   11562 	{
   11563 	  plt_entry = mips_vxworks_shared_plt_entry;
   11564 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
   11565 	  bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
   11566 	}
   11567       else
   11568 	{
   11569 	  bfd_vma got_address_high, got_address_low;
   11570 
   11571 	  plt_entry = mips_vxworks_exec_plt_entry;
   11572 	  got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
   11573 	  got_address_low = got_address & 0xffff;
   11574 
   11575 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
   11576 	  bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
   11577 	  bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
   11578 	  bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
   11579 	  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11580 	  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11581 	  bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
   11582 	  bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
   11583 
   11584 	  loc = (htab->srelplt2->contents
   11585 		 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
   11586 
   11587 	  /* Emit a relocation for the .got.plt entry.  */
   11588 	  rel.r_offset = got_address;
   11589 	  rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
   11590 	  rel.r_addend = plt_offset;
   11591 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11592 
   11593 	  /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
   11594 	  loc += sizeof (Elf32_External_Rela);
   11595 	  rel.r_offset = plt_address + 8;
   11596 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11597 	  rel.r_addend = got_offset;
   11598 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11599 
   11600 	  /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
   11601 	  loc += sizeof (Elf32_External_Rela);
   11602 	  rel.r_offset += 4;
   11603 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11604 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11605 	}
   11606 
   11607       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
   11608       loc = (htab->root.srelplt->contents
   11609 	     + gotplt_index * sizeof (Elf32_External_Rela));
   11610       rel.r_offset = got_address;
   11611       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
   11612       rel.r_addend = 0;
   11613       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11614 
   11615       if (!h->def_regular)
   11616 	sym->st_shndx = SHN_UNDEF;
   11617     }
   11618 
   11619   BFD_ASSERT (h->dynindx != -1 || h->forced_local);
   11620 
   11621   sgot = htab->root.sgot;
   11622   g = htab->got_info;
   11623   BFD_ASSERT (g != NULL);
   11624 
   11625   /* See if this symbol has an entry in the GOT.  */
   11626   if (hmips->global_got_area != GGA_NONE)
   11627     {
   11628       bfd_vma offset;
   11629       Elf_Internal_Rela outrel;
   11630       bfd_byte *loc;
   11631       asection *s;
   11632 
   11633       /* Install the symbol value in the GOT.   */
   11634       offset = mips_elf_primary_global_got_index (output_bfd, info, h);
   11635       MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
   11636 
   11637       /* Add a dynamic relocation for it.  */
   11638       s = mips_elf_rel_dyn_section (info, false);
   11639       loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
   11640       outrel.r_offset = (sgot->output_section->vma
   11641 			 + sgot->output_offset
   11642 			 + offset);
   11643       outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
   11644       outrel.r_addend = 0;
   11645       bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
   11646     }
   11647 
   11648   /* Emit a copy reloc, if needed.  */
   11649   if (h->needs_copy)
   11650     {
   11651       Elf_Internal_Rela rel;
   11652       asection *srel;
   11653       bfd_byte *loc;
   11654 
   11655       BFD_ASSERT (h->dynindx != -1);
   11656 
   11657       rel.r_offset = (h->root.u.def.section->output_section->vma
   11658 		      + h->root.u.def.section->output_offset
   11659 		      + h->root.u.def.value);
   11660       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
   11661       rel.r_addend = 0;
   11662       if (h->root.u.def.section == htab->root.sdynrelro)
   11663 	srel = htab->root.sreldynrelro;
   11664       else
   11665 	srel = htab->root.srelbss;
   11666       loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
   11667       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11668       ++srel->reloc_count;
   11669     }
   11670 
   11671   /* If this is a mips16/microMIPS symbol, force the value to be even.  */
   11672   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   11673     sym->st_value &= ~1;
   11674 
   11675   return true;
   11676 }
   11677 
   11678 /* Write out a plt0 entry to the beginning of .plt.  */
   11679 
   11680 static bool
   11681 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
   11682 {
   11683   bfd_byte *loc;
   11684   bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
   11685   static const bfd_vma *plt_entry;
   11686   struct mips_elf_link_hash_table *htab;
   11687 
   11688   htab = mips_elf_hash_table (info);
   11689   BFD_ASSERT (htab != NULL);
   11690 
   11691   if (ABI_64_P (output_bfd))
   11692     plt_entry = (htab->compact_branches
   11693 		 ? mipsr6_n64_exec_plt0_entry_compact
   11694 		 : mips_n64_exec_plt0_entry);
   11695   else if (ABI_N32_P (output_bfd))
   11696     plt_entry = (htab->compact_branches
   11697 		 ? mipsr6_n32_exec_plt0_entry_compact
   11698 		 : mips_n32_exec_plt0_entry);
   11699   else if (!htab->plt_header_is_comp)
   11700     plt_entry = (htab->compact_branches
   11701 		 ? mipsr6_o32_exec_plt0_entry_compact
   11702 		 : mips_o32_exec_plt0_entry);
   11703   else if (htab->insn32)
   11704     plt_entry = micromips_insn32_o32_exec_plt0_entry;
   11705   else
   11706     plt_entry = micromips_o32_exec_plt0_entry;
   11707 
   11708   /* Calculate the value of .got.plt.  */
   11709   gotplt_value = (htab->root.sgotplt->output_section->vma
   11710 		  + htab->root.sgotplt->output_offset);
   11711   gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
   11712   gotplt_value_low = gotplt_value & 0xffff;
   11713 
   11714   /* The PLT sequence is not safe for N64 if .got.plt's address can
   11715      not be loaded in two instructions.  */
   11716   if (ABI_64_P (output_bfd)
   11717       && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
   11718     {
   11719       _bfd_error_handler
   11720 	/* xgettext:c-format */
   11721 	(_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
   11722 	   "supported; consider using `-Ttext-segment=...'"),
   11723 	 output_bfd,
   11724 	 htab->root.sgotplt->output_section,
   11725 	 (int64_t) gotplt_value);
   11726       bfd_set_error (bfd_error_no_error);
   11727       return false;
   11728     }
   11729 
   11730   /* Install the PLT header.  */
   11731   loc = htab->root.splt->contents;
   11732   if (plt_entry == micromips_o32_exec_plt0_entry)
   11733     {
   11734       bfd_vma gotpc_offset;
   11735       bfd_vma loc_address;
   11736       size_t i;
   11737 
   11738       BFD_ASSERT (gotplt_value % 4 == 0);
   11739 
   11740       loc_address = (htab->root.splt->output_section->vma
   11741 		     + htab->root.splt->output_offset);
   11742       gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
   11743 
   11744       /* ADDIUPC has a span of +/-16MB, check we're in range.  */
   11745       if (gotpc_offset + 0x1000000 >= 0x2000000)
   11746 	{
   11747 	  _bfd_error_handler
   11748 	    /* xgettext:c-format */
   11749 	    (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
   11750 	       "beyond the range of ADDIUPC"),
   11751 	     output_bfd,
   11752 	     htab->root.sgotplt->output_section,
   11753 	     (int64_t) gotpc_offset,
   11754 	     htab->root.splt->output_section);
   11755 	  bfd_set_error (bfd_error_no_error);
   11756 	  return false;
   11757 	}
   11758       bfd_put_16 (output_bfd,
   11759 		  plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
   11760       bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
   11761       for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
   11762 	bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
   11763     }
   11764   else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
   11765     {
   11766       size_t i;
   11767 
   11768       bfd_put_16 (output_bfd, plt_entry[0], loc);
   11769       bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
   11770       bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   11771       bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
   11772       bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   11773       bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
   11774       for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
   11775 	bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
   11776     }
   11777   else
   11778     {
   11779       bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
   11780       bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
   11781       bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
   11782       bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   11783       bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11784       bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11785       bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
   11786       bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
   11787     }
   11788 
   11789   return true;
   11790 }
   11791 
   11792 /* Install the PLT header for a VxWorks executable and finalize the
   11793    contents of .rela.plt.unloaded.  */
   11794 
   11795 static void
   11796 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
   11797 {
   11798   Elf_Internal_Rela rela;
   11799   bfd_byte *loc;
   11800   bfd_vma got_value, got_value_high, got_value_low, plt_address;
   11801   static const bfd_vma *plt_entry;
   11802   struct mips_elf_link_hash_table *htab;
   11803 
   11804   htab = mips_elf_hash_table (info);
   11805   BFD_ASSERT (htab != NULL);
   11806 
   11807   plt_entry = mips_vxworks_exec_plt0_entry;
   11808 
   11809   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
   11810   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
   11811 	       + htab->root.hgot->root.u.def.section->output_offset
   11812 	       + htab->root.hgot->root.u.def.value);
   11813 
   11814   got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
   11815   got_value_low = got_value & 0xffff;
   11816 
   11817   /* Calculate the address of the PLT header.  */
   11818   plt_address = (htab->root.splt->output_section->vma
   11819 		 + htab->root.splt->output_offset);
   11820 
   11821   /* Install the PLT header.  */
   11822   loc = htab->root.splt->contents;
   11823   bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
   11824   bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
   11825   bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
   11826   bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   11827   bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11828   bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11829 
   11830   /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
   11831   loc = htab->srelplt2->contents;
   11832   rela.r_offset = plt_address;
   11833   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11834   rela.r_addend = 0;
   11835   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   11836   loc += sizeof (Elf32_External_Rela);
   11837 
   11838   /* Output the relocation for the following addiu of
   11839      %lo(_GLOBAL_OFFSET_TABLE_).  */
   11840   rela.r_offset += 4;
   11841   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11842   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   11843   loc += sizeof (Elf32_External_Rela);
   11844 
   11845   /* Fix up the remaining relocations.  They may have the wrong
   11846      symbol index for _G_O_T_ or _P_L_T_ depending on the order
   11847      in which symbols were output.  */
   11848   while (loc < htab->srelplt2->contents + htab->srelplt2->size)
   11849     {
   11850       Elf_Internal_Rela rel;
   11851 
   11852       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11853       rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
   11854       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11855       loc += sizeof (Elf32_External_Rela);
   11856 
   11857       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11858       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11859       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11860       loc += sizeof (Elf32_External_Rela);
   11861 
   11862       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11863       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11864       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11865       loc += sizeof (Elf32_External_Rela);
   11866     }
   11867 }
   11868 
   11869 /* Install the PLT header for a VxWorks shared library.  */
   11870 
   11871 static void
   11872 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
   11873 {
   11874   unsigned int i;
   11875   struct mips_elf_link_hash_table *htab;
   11876 
   11877   htab = mips_elf_hash_table (info);
   11878   BFD_ASSERT (htab != NULL);
   11879 
   11880   /* We just need to copy the entry byte-by-byte.  */
   11881   for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
   11882     bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
   11883 		htab->root.splt->contents + i * 4);
   11884 }
   11885 
   11886 /* Finish up the dynamic sections.  */
   11887 
   11888 bool
   11889 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
   11890 				       struct bfd_link_info *info)
   11891 {
   11892   bfd *dynobj;
   11893   asection *sdyn;
   11894   asection *sgot;
   11895   struct mips_got_info *gg, *g;
   11896   struct mips_elf_link_hash_table *htab;
   11897 
   11898   htab = mips_elf_hash_table (info);
   11899   BFD_ASSERT (htab != NULL);
   11900 
   11901   dynobj = elf_hash_table (info)->dynobj;
   11902 
   11903   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   11904 
   11905   sgot = htab->root.sgot;
   11906   gg = htab->got_info;
   11907 
   11908   if (elf_hash_table (info)->dynamic_sections_created)
   11909     {
   11910       bfd_byte *b;
   11911       int dyn_to_skip = 0, dyn_skipped = 0;
   11912 
   11913       BFD_ASSERT (sdyn != NULL);
   11914       BFD_ASSERT (gg != NULL);
   11915 
   11916       g = mips_elf_bfd_got (output_bfd, false);
   11917       BFD_ASSERT (g != NULL);
   11918 
   11919       for (b = sdyn->contents;
   11920 	   b < sdyn->contents + sdyn->size;
   11921 	   b += MIPS_ELF_DYN_SIZE (dynobj))
   11922 	{
   11923 	  Elf_Internal_Dyn dyn;
   11924 	  const char *name;
   11925 	  size_t elemsize;
   11926 	  asection *s;
   11927 	  bool swap_out_p;
   11928 
   11929 	  /* Read in the current dynamic entry.  */
   11930 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
   11931 
   11932 	  /* Assume that we're going to modify it and write it out.  */
   11933 	  swap_out_p = true;
   11934 
   11935 	  switch (dyn.d_tag)
   11936 	    {
   11937 	    case DT_RELENT:
   11938 	      dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
   11939 	      break;
   11940 
   11941 	    case DT_RELAENT:
   11942 	      BFD_ASSERT (htab->root.target_os == is_vxworks);
   11943 	      dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
   11944 	      break;
   11945 
   11946 	    case DT_STRSZ:
   11947 	      /* Rewrite DT_STRSZ.  */
   11948 	      dyn.d_un.d_val =
   11949 		_bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   11950 	      break;
   11951 
   11952 	    case DT_PLTGOT:
   11953 	      s = htab->root.sgot;
   11954 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   11955 	      break;
   11956 
   11957 	    case DT_MIPS_PLTGOT:
   11958 	      s = htab->root.sgotplt;
   11959 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   11960 	      break;
   11961 
   11962 	    case DT_MIPS_RLD_VERSION:
   11963 	      dyn.d_un.d_val = 1; /* XXX */
   11964 	      break;
   11965 
   11966 	    case DT_MIPS_FLAGS:
   11967 	      dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
   11968 	      break;
   11969 
   11970 	    case DT_MIPS_TIME_STAMP:
   11971 	      {
   11972 		time_t t;
   11973 		time (&t);
   11974 		dyn.d_un.d_val = t;
   11975 	      }
   11976 	      break;
   11977 
   11978 	    case DT_MIPS_ICHECKSUM:
   11979 	      /* XXX FIXME: */
   11980 	      swap_out_p = false;
   11981 	      break;
   11982 
   11983 	    case DT_MIPS_IVERSION:
   11984 	      /* XXX FIXME: */
   11985 	      swap_out_p = false;
   11986 	      break;
   11987 
   11988 	    case DT_MIPS_BASE_ADDRESS:
   11989 	      s = output_bfd->sections;
   11990 	      BFD_ASSERT (s != NULL);
   11991 	      dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
   11992 	      break;
   11993 
   11994 	    case DT_MIPS_LOCAL_GOTNO:
   11995 	      dyn.d_un.d_val = g->local_gotno;
   11996 	      break;
   11997 
   11998 	    case DT_MIPS_UNREFEXTNO:
   11999 	      /* The index into the dynamic symbol table which is the
   12000 		 entry of the first external symbol that is not
   12001 		 referenced within the same object.  */
   12002 	      dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
   12003 	      break;
   12004 
   12005 	    case DT_MIPS_GOTSYM:
   12006 	      if (htab->global_gotsym)
   12007 		{
   12008 		  dyn.d_un.d_val = htab->global_gotsym->dynindx;
   12009 		  break;
   12010 		}
   12011 	      /* In case if we don't have global got symbols we default
   12012 		 to setting DT_MIPS_GOTSYM to the same value as
   12013 		 DT_MIPS_SYMTABNO.  */
   12014 	      /* Fall through.  */
   12015 
   12016 	    case DT_MIPS_SYMTABNO:
   12017 	      name = ".dynsym";
   12018 	      elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
   12019 	      s = bfd_get_linker_section (dynobj, name);
   12020 
   12021 	      if (s != NULL)
   12022 		dyn.d_un.d_val = s->size / elemsize;
   12023 	      else
   12024 		dyn.d_un.d_val = 0;
   12025 	      break;
   12026 
   12027 	    case DT_MIPS_HIPAGENO:
   12028 	      dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
   12029 	      break;
   12030 
   12031 	    case DT_MIPS_RLD_MAP:
   12032 	      {
   12033 		struct elf_link_hash_entry *h;
   12034 		h = mips_elf_hash_table (info)->rld_symbol;
   12035 		if (!h)
   12036 		  {
   12037 		    dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   12038 		    swap_out_p = false;
   12039 		    break;
   12040 		  }
   12041 		s = h->root.u.def.section;
   12042 
   12043 		/* The MIPS_RLD_MAP tag stores the absolute address of the
   12044 		   debug pointer.  */
   12045 		dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
   12046 				  + h->root.u.def.value);
   12047 	      }
   12048 	      break;
   12049 
   12050 	    case DT_MIPS_RLD_MAP_REL:
   12051 	      {
   12052 		struct elf_link_hash_entry *h;
   12053 		bfd_vma dt_addr, rld_addr;
   12054 		h = mips_elf_hash_table (info)->rld_symbol;
   12055 		if (!h)
   12056 		  {
   12057 		    dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   12058 		    swap_out_p = false;
   12059 		    break;
   12060 		  }
   12061 		s = h->root.u.def.section;
   12062 
   12063 		/* The MIPS_RLD_MAP_REL tag stores the offset to the debug
   12064 		   pointer, relative to the address of the tag.  */
   12065 		dt_addr = (sdyn->output_section->vma + sdyn->output_offset
   12066 			   + (b - sdyn->contents));
   12067 		rld_addr = (s->output_section->vma + s->output_offset
   12068 			    + h->root.u.def.value);
   12069 		dyn.d_un.d_ptr = rld_addr - dt_addr;
   12070 	      }
   12071 	      break;
   12072 
   12073 	    case DT_MIPS_OPTIONS:
   12074 	      s = (bfd_get_section_by_name
   12075 		   (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
   12076 	      dyn.d_un.d_ptr = s->vma;
   12077 	      break;
   12078 
   12079 	    case DT_PLTREL:
   12080 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   12081 	      if (htab->root.target_os == is_vxworks)
   12082 		dyn.d_un.d_val = DT_RELA;
   12083 	      else
   12084 		dyn.d_un.d_val = DT_REL;
   12085 	      break;
   12086 
   12087 	    case DT_PLTRELSZ:
   12088 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   12089 	      dyn.d_un.d_val = htab->root.srelplt->size;
   12090 	      break;
   12091 
   12092 	    case DT_JMPREL:
   12093 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   12094 	      dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
   12095 				+ htab->root.srelplt->output_offset);
   12096 	      break;
   12097 
   12098 	    case DT_TEXTREL:
   12099 	      /* If we didn't need any text relocations after all, delete
   12100 		 the dynamic tag.  */
   12101 	      if (!(info->flags & DF_TEXTREL))
   12102 		{
   12103 		  dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   12104 		  swap_out_p = false;
   12105 		}
   12106 	      break;
   12107 
   12108 	    case DT_FLAGS:
   12109 	      /* If we didn't need any text relocations after all, clear
   12110 		 DF_TEXTREL from DT_FLAGS.  */
   12111 	      if (!(info->flags & DF_TEXTREL))
   12112 		dyn.d_un.d_val &= ~DF_TEXTREL;
   12113 	      else
   12114 		swap_out_p = false;
   12115 	      break;
   12116 
   12117 	    case DT_MIPS_XHASH:
   12118 	      name = ".MIPS.xhash";
   12119 	      s = bfd_get_linker_section (dynobj, name);
   12120 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   12121 	      break;
   12122 
   12123 	    default:
   12124 	      swap_out_p = false;
   12125 	      if (htab->root.target_os == is_vxworks
   12126 		  && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
   12127 		swap_out_p = true;
   12128 	      break;
   12129 	    }
   12130 
   12131 	  if (swap_out_p || dyn_skipped)
   12132 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
   12133 	      (dynobj, &dyn, b - dyn_skipped);
   12134 
   12135 	  if (dyn_to_skip)
   12136 	    {
   12137 	      dyn_skipped += dyn_to_skip;
   12138 	      dyn_to_skip = 0;
   12139 	    }
   12140 	}
   12141 
   12142       /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
   12143       if (dyn_skipped > 0)
   12144 	memset (b - dyn_skipped, 0, dyn_skipped);
   12145     }
   12146 
   12147   if (sgot != NULL && sgot->size > 0
   12148       && !bfd_is_abs_section (sgot->output_section))
   12149     {
   12150       if (htab->root.target_os == is_vxworks)
   12151 	{
   12152 	  /* The first entry of the global offset table points to the
   12153 	     ".dynamic" section.  The second is initialized by the
   12154 	     loader and contains the shared library identifier.
   12155 	     The third is also initialized by the loader and points
   12156 	     to the lazy resolution stub.  */
   12157 	  MIPS_ELF_PUT_WORD (output_bfd,
   12158 			     sdyn->output_offset + sdyn->output_section->vma,
   12159 			     sgot->contents);
   12160 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
   12161 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
   12162 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
   12163 			     sgot->contents
   12164 			     + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
   12165 	}
   12166       else
   12167 	{
   12168 	  /* The first entry of the global offset table will be filled at
   12169 	     runtime. The second entry will be used by some runtime loaders.
   12170 	     This isn't the case of IRIX rld.  */
   12171 	  MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
   12172 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
   12173 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
   12174 	}
   12175 
   12176       elf_section_data (sgot->output_section)->this_hdr.sh_entsize
   12177 	 = MIPS_ELF_GOT_SIZE (output_bfd);
   12178     }
   12179 
   12180   /* Generate dynamic relocations for the non-primary gots.  */
   12181   if (gg != NULL && gg->next)
   12182     {
   12183       Elf_Internal_Rela rel[3];
   12184       bfd_vma addend = 0;
   12185 
   12186       memset (rel, 0, sizeof (rel));
   12187       rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
   12188 
   12189       for (g = gg->next; g->next != gg; g = g->next)
   12190 	{
   12191 	  bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
   12192 	    + g->next->tls_gotno;
   12193 
   12194 	  MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
   12195 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
   12196 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
   12197 			     sgot->contents
   12198 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
   12199 
   12200 	  if (! bfd_link_pic (info))
   12201 	    continue;
   12202 
   12203 	  for (; got_index < g->local_gotno; got_index++)
   12204 	    {
   12205 	      if (got_index >= g->assigned_low_gotno
   12206 		  && got_index <= g->assigned_high_gotno)
   12207 		continue;
   12208 
   12209 	      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
   12210 		= got_index * MIPS_ELF_GOT_SIZE (output_bfd);
   12211 	      if (!(mips_elf_create_dynamic_relocation
   12212 		    (output_bfd, info, rel, NULL,
   12213 		     bfd_abs_section_ptr,
   12214 		     0, &addend, sgot)))
   12215 		return false;
   12216 	      BFD_ASSERT (addend == 0);
   12217 	    }
   12218 	}
   12219     }
   12220 
   12221   /* The generation of dynamic relocations for the non-primary gots
   12222      adds more dynamic relocations.  We cannot count them until
   12223      here.  */
   12224 
   12225   if (elf_hash_table (info)->dynamic_sections_created)
   12226     {
   12227       bfd_byte *b;
   12228       bool swap_out_p;
   12229 
   12230       BFD_ASSERT (sdyn != NULL);
   12231 
   12232       for (b = sdyn->contents;
   12233 	   b < sdyn->contents + sdyn->size;
   12234 	   b += MIPS_ELF_DYN_SIZE (dynobj))
   12235 	{
   12236 	  Elf_Internal_Dyn dyn;
   12237 	  asection *s;
   12238 
   12239 	  /* Read in the current dynamic entry.  */
   12240 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
   12241 
   12242 	  /* Assume that we're going to modify it and write it out.  */
   12243 	  swap_out_p = true;
   12244 
   12245 	  switch (dyn.d_tag)
   12246 	    {
   12247 	    case DT_RELSZ:
   12248 	      /* Reduce DT_RELSZ to account for any relocations we
   12249 		 decided not to make.  This is for the n64 irix rld,
   12250 		 which doesn't seem to apply any relocations if there
   12251 		 are trailing null entries.  */
   12252 	      s = mips_elf_rel_dyn_section (info, false);
   12253 	      dyn.d_un.d_val = (s->reloc_count
   12254 				* (ABI_64_P (output_bfd)
   12255 				   ? sizeof (Elf64_Mips_External_Rel)
   12256 				   : sizeof (Elf32_External_Rel)));
   12257 	      /* Adjust the section size too.  Tools like the prelinker
   12258 		 can reasonably expect the values to the same.  */
   12259 	      BFD_ASSERT (!bfd_is_abs_section (s->output_section));
   12260 	      elf_section_data (s->output_section)->this_hdr.sh_size
   12261 		= dyn.d_un.d_val;
   12262 	      break;
   12263 
   12264 	    default:
   12265 	      swap_out_p = false;
   12266 	      break;
   12267 	    }
   12268 
   12269 	  if (swap_out_p)
   12270 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
   12271 	      (dynobj, &dyn, b);
   12272 	}
   12273     }
   12274 
   12275   {
   12276     asection *s;
   12277     Elf32_compact_rel cpt;
   12278 
   12279     if (SGI_COMPAT (output_bfd))
   12280       {
   12281 	/* Write .compact_rel section out.  */
   12282 	s = bfd_get_linker_section (dynobj, ".compact_rel");
   12283 	if (s != NULL)
   12284 	  {
   12285 	    cpt.id1 = 1;
   12286 	    cpt.num = s->reloc_count;
   12287 	    cpt.id2 = 2;
   12288 	    cpt.offset = (s->output_section->filepos
   12289 			  + sizeof (Elf32_External_compact_rel));
   12290 	    cpt.reserved0 = 0;
   12291 	    cpt.reserved1 = 0;
   12292 	    bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
   12293 					    ((Elf32_External_compact_rel *)
   12294 					     s->contents));
   12295 
   12296 	    /* Clean up a dummy stub function entry in .text.  */
   12297 	    if (htab->sstubs != NULL
   12298 		&& htab->sstubs->contents != NULL)
   12299 	      {
   12300 		file_ptr dummy_offset;
   12301 
   12302 		BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
   12303 		dummy_offset = htab->sstubs->size - htab->function_stub_size;
   12304 		memset (htab->sstubs->contents + dummy_offset, 0,
   12305 			htab->function_stub_size);
   12306 	      }
   12307 	  }
   12308       }
   12309 
   12310     /* The psABI says that the dynamic relocations must be sorted in
   12311        increasing order of r_symndx.  The VxWorks EABI doesn't require
   12312        this, and because the code below handles REL rather than RELA
   12313        relocations, using it for VxWorks would be outright harmful.  */
   12314     if (htab->root.target_os != is_vxworks)
   12315       {
   12316 	s = mips_elf_rel_dyn_section (info, false);
   12317 	if (s != NULL
   12318 	    && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
   12319 	  {
   12320 	    reldyn_sorting_bfd = output_bfd;
   12321 
   12322 	    if (ABI_64_P (output_bfd))
   12323 	      qsort ((Elf64_External_Rel *) s->contents + 1,
   12324 		     s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
   12325 		     sort_dynamic_relocs_64);
   12326 	    else
   12327 	      qsort ((Elf32_External_Rel *) s->contents + 1,
   12328 		     s->reloc_count - 1, sizeof (Elf32_External_Rel),
   12329 		     sort_dynamic_relocs);
   12330 	  }
   12331       }
   12332   }
   12333 
   12334   if (htab->root.splt && htab->root.splt->size > 0)
   12335     {
   12336       if (htab->root.target_os == is_vxworks)
   12337 	{
   12338 	  if (bfd_link_pic (info))
   12339 	    mips_vxworks_finish_shared_plt (output_bfd, info);
   12340 	  else
   12341 	    mips_vxworks_finish_exec_plt (output_bfd, info);
   12342 	}
   12343       else
   12344 	{
   12345 	  BFD_ASSERT (!bfd_link_pic (info));
   12346 	  if (!mips_finish_exec_plt (output_bfd, info))
   12347 	    return false;
   12348 	}
   12349     }
   12350   return true;
   12351 }
   12352 
   12353 
   12354 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
   12355 
   12356 static void
   12357 mips_set_isa_flags (bfd *abfd)
   12358 {
   12359   flagword val;
   12360 
   12361   switch (bfd_get_mach (abfd))
   12362     {
   12363     default:
   12364       if (ABI_N32_P (abfd) || ABI_64_P (abfd))
   12365         val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_64R6 : EF_MIPS_ARCH_3;
   12366       else
   12367         val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_32R6 : EF_MIPS_ARCH_1;
   12368       break;
   12369 
   12370     case bfd_mach_mips3000:
   12371       val = EF_MIPS_ARCH_1;
   12372       break;
   12373 
   12374     case bfd_mach_mips3900:
   12375       val = EF_MIPS_ARCH_1 | EF_MIPS_MACH_3900;
   12376       break;
   12377 
   12378     case bfd_mach_mips6000:
   12379       val = EF_MIPS_ARCH_2;
   12380       break;
   12381 
   12382     case bfd_mach_mips4010:
   12383       val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_4010;
   12384       break;
   12385 
   12386     case bfd_mach_mips_allegrex:
   12387       val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_ALLEGREX;
   12388       break;
   12389 
   12390     case bfd_mach_mips4000:
   12391     case bfd_mach_mips4300:
   12392     case bfd_mach_mips4400:
   12393     case bfd_mach_mips4600:
   12394       val = EF_MIPS_ARCH_3;
   12395       break;
   12396 
   12397     case bfd_mach_mips4100:
   12398       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4100;
   12399       break;
   12400 
   12401     case bfd_mach_mips4111:
   12402       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4111;
   12403       break;
   12404 
   12405     case bfd_mach_mips4120:
   12406       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4120;
   12407       break;
   12408 
   12409     case bfd_mach_mips4650:
   12410       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4650;
   12411       break;
   12412 
   12413     case bfd_mach_mips5400:
   12414       val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5400;
   12415       break;
   12416 
   12417     case bfd_mach_mips5500:
   12418       val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5500;
   12419       break;
   12420 
   12421     case bfd_mach_mips5900:
   12422       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_5900;
   12423       break;
   12424 
   12425     case bfd_mach_mips9000:
   12426       val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_9000;
   12427       break;
   12428 
   12429     case bfd_mach_mips5000:
   12430     case bfd_mach_mips7000:
   12431     case bfd_mach_mips8000:
   12432     case bfd_mach_mips10000:
   12433     case bfd_mach_mips12000:
   12434     case bfd_mach_mips14000:
   12435     case bfd_mach_mips16000:
   12436       val = EF_MIPS_ARCH_4;
   12437       break;
   12438 
   12439     case bfd_mach_mips5:
   12440       val = EF_MIPS_ARCH_5;
   12441       break;
   12442 
   12443     case bfd_mach_mips_loongson_2e:
   12444       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2E;
   12445       break;
   12446 
   12447     case bfd_mach_mips_loongson_2f:
   12448       val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2F;
   12449       break;
   12450 
   12451     case bfd_mach_mips_sb1:
   12452       val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_SB1;
   12453       break;
   12454 
   12455     case bfd_mach_mips_gs464:
   12456       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464;
   12457       break;
   12458 
   12459     case bfd_mach_mips_gs464e:
   12460       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464E;
   12461       break;
   12462 
   12463     case bfd_mach_mips_gs264e:
   12464       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS264E;
   12465       break;
   12466 
   12467     case bfd_mach_mips_octeon:
   12468     case bfd_mach_mips_octeonp:
   12469       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON;
   12470       break;
   12471 
   12472     case bfd_mach_mips_octeon3:
   12473       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON3;
   12474       break;
   12475 
   12476     case bfd_mach_mips_xlr:
   12477       val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_XLR;
   12478       break;
   12479 
   12480     case bfd_mach_mips_octeon2:
   12481       val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON2;
   12482       break;
   12483 
   12484     case bfd_mach_mipsisa32:
   12485       val = EF_MIPS_ARCH_32;
   12486       break;
   12487 
   12488     case bfd_mach_mipsisa64:
   12489       val = EF_MIPS_ARCH_64;
   12490       break;
   12491 
   12492     case bfd_mach_mipsisa32r2:
   12493     case bfd_mach_mipsisa32r3:
   12494     case bfd_mach_mipsisa32r5:
   12495       val = EF_MIPS_ARCH_32R2;
   12496       break;
   12497 
   12498     case bfd_mach_mips_interaptiv_mr2:
   12499       val = EF_MIPS_ARCH_32R2 | EF_MIPS_MACH_IAMR2;
   12500       break;
   12501 
   12502     case bfd_mach_mipsisa64r2:
   12503     case bfd_mach_mipsisa64r3:
   12504     case bfd_mach_mipsisa64r5:
   12505       val = EF_MIPS_ARCH_64R2;
   12506       break;
   12507 
   12508     case bfd_mach_mipsisa32r6:
   12509       val = EF_MIPS_ARCH_32R6;
   12510       break;
   12511 
   12512     case bfd_mach_mipsisa64r6:
   12513       val = EF_MIPS_ARCH_64R6;
   12514       break;
   12515     }
   12516   elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
   12517   elf_elfheader (abfd)->e_flags |= val;
   12518 
   12519 }
   12520 
   12521 
   12522 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
   12523    Don't do so for code sections.  We want to keep ordering of HI16/LO16
   12524    as is.  On the other hand, elf-eh-frame.c processing requires .eh_frame
   12525    relocs to be sorted.  */
   12526 
   12527 bool
   12528 _bfd_mips_elf_sort_relocs_p (asection *sec)
   12529 {
   12530   return (sec->flags & SEC_CODE) == 0;
   12531 }
   12532 
   12533 
   12534 /* The final processing done just before writing out a MIPS ELF object
   12535    file.  This gets the MIPS architecture right based on the machine
   12536    number.  This is used by both the 32-bit and the 64-bit ABI.  */
   12537 
   12538 void
   12539 _bfd_mips_final_write_processing (bfd *abfd)
   12540 {
   12541   unsigned int i;
   12542   Elf_Internal_Shdr **hdrpp;
   12543   const char *name;
   12544   asection *sec;
   12545 
   12546   /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
   12547      is nonzero.  This is for compatibility with old objects, which used
   12548      a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
   12549   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
   12550     mips_set_isa_flags (abfd);
   12551 
   12552   /* Set the sh_info field for .gptab sections and other appropriate
   12553      info for each special section.  */
   12554   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
   12555        i < elf_numsections (abfd);
   12556        i++, hdrpp++)
   12557     {
   12558       switch ((*hdrpp)->sh_type)
   12559 	{
   12560 	case SHT_MIPS_MSYM:
   12561 	case SHT_MIPS_LIBLIST:
   12562 	  sec = bfd_get_section_by_name (abfd, ".dynstr");
   12563 	  if (sec != NULL)
   12564 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12565 	  break;
   12566 
   12567 	case SHT_MIPS_GPTAB:
   12568 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12569 	  name = bfd_section_name ((*hdrpp)->bfd_section);
   12570 	  if (startswith (name, ".gptab."))
   12571 	    {
   12572 	      sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
   12573 	      if (sec != NULL)
   12574 		(*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
   12575 	    }
   12576 	  break;
   12577 
   12578 	case SHT_MIPS_CONTENT:
   12579 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12580 	  name = bfd_section_name ((*hdrpp)->bfd_section);
   12581 	  if (startswith (name, ".MIPS.content"))
   12582 	    {
   12583 	      sec = bfd_get_section_by_name (abfd,
   12584 					     name + sizeof ".MIPS.content" - 1);
   12585 	      if (sec != NULL)
   12586 		(*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12587 	    }
   12588 	  break;
   12589 
   12590 	case SHT_MIPS_SYMBOL_LIB:
   12591 	  sec = bfd_get_section_by_name (abfd, ".dynsym");
   12592 	  if (sec != NULL)
   12593 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12594 	  sec = bfd_get_section_by_name (abfd, ".liblist");
   12595 	  if (sec != NULL)
   12596 	    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
   12597 	  break;
   12598 
   12599 	case SHT_MIPS_EVENTS:
   12600 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12601 	  name = bfd_section_name ((*hdrpp)->bfd_section);
   12602 	  if (startswith (name, ".MIPS.events"))
   12603 	    sec = bfd_get_section_by_name (abfd,
   12604 					   name + sizeof ".MIPS.events" - 1);
   12605 	  else if (startswith (name, ".MIPS.post_rel"))
   12606 	    sec = bfd_get_section_by_name (abfd,
   12607 					   name + sizeof ".MIPS.post_rel" - 1);
   12608 	  else
   12609 	    sec = NULL;
   12610 	  if (sec != NULL)
   12611 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12612 	  break;
   12613 
   12614 	case SHT_MIPS_XHASH:
   12615 	  sec = bfd_get_section_by_name (abfd, ".dynsym");
   12616 	  if (sec != NULL)
   12617 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12618 	}
   12619     }
   12620 }
   12621 
   12622 bool
   12623 _bfd_mips_elf_final_write_processing (bfd *abfd)
   12624 {
   12625   _bfd_mips_final_write_processing (abfd);
   12626   return _bfd_elf_final_write_processing (abfd);
   12627 }
   12628 
   12629 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
   12631    segments.  */
   12632 
   12633 int
   12634 _bfd_mips_elf_additional_program_headers (bfd *abfd,
   12635 					  struct bfd_link_info *info ATTRIBUTE_UNUSED)
   12636 {
   12637   asection *s;
   12638   int ret = 0;
   12639 
   12640   /* See if we need a PT_MIPS_REGINFO segment.  */
   12641   s = bfd_get_section_by_name (abfd, ".reginfo");
   12642   if (s && (s->flags & SEC_LOAD))
   12643     ++ret;
   12644 
   12645   /* See if we need a PT_MIPS_ABIFLAGS segment.  */
   12646   if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
   12647     ++ret;
   12648 
   12649   /* See if we need a PT_MIPS_OPTIONS segment.  */
   12650   if (IRIX_COMPAT (abfd) == ict_irix6
   12651       && bfd_get_section_by_name (abfd,
   12652 				  MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
   12653     ++ret;
   12654 
   12655   /* See if we need a PT_MIPS_RTPROC segment.  */
   12656   if (IRIX_COMPAT (abfd) == ict_irix5
   12657       && bfd_get_section_by_name (abfd, ".dynamic")
   12658       && bfd_get_section_by_name (abfd, ".mdebug"))
   12659     ++ret;
   12660 
   12661   /* Allocate a PT_NULL header in dynamic objects.  See
   12662      _bfd_mips_elf_modify_segment_map for details.  */
   12663   if (!SGI_COMPAT (abfd)
   12664       && bfd_get_section_by_name (abfd, ".dynamic"))
   12665     ++ret;
   12666 
   12667   return ret;
   12668 }
   12669 
   12670 /* Modify the segment map for an IRIX5 executable.  */
   12671 
   12672 bool
   12673 _bfd_mips_elf_modify_segment_map (bfd *abfd,
   12674 				  struct bfd_link_info *info)
   12675 {
   12676   asection *s;
   12677   struct elf_segment_map *m, **pm;
   12678   size_t amt;
   12679 
   12680   /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
   12681      segment.  */
   12682   s = bfd_get_section_by_name (abfd, ".reginfo");
   12683   if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12684     {
   12685       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12686 	if (m->p_type == PT_MIPS_REGINFO)
   12687 	  break;
   12688       if (m == NULL)
   12689 	{
   12690 	  amt = sizeof *m;
   12691 	  m = bfd_zalloc (abfd, amt);
   12692 	  if (m == NULL)
   12693 	    return false;
   12694 
   12695 	  m->p_type = PT_MIPS_REGINFO;
   12696 	  m->count = 1;
   12697 	  m->sections[0] = s;
   12698 
   12699 	  /* We want to put it after the PHDR and INTERP segments.  */
   12700 	  pm = &elf_seg_map (abfd);
   12701 	  while (*pm != NULL
   12702 		 && ((*pm)->p_type == PT_PHDR
   12703 		     || (*pm)->p_type == PT_INTERP))
   12704 	    pm = &(*pm)->next;
   12705 
   12706 	  m->next = *pm;
   12707 	  *pm = m;
   12708 	}
   12709     }
   12710 
   12711   /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
   12712      segment.  */
   12713   s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
   12714   if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12715     {
   12716       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12717 	if (m->p_type == PT_MIPS_ABIFLAGS)
   12718 	  break;
   12719       if (m == NULL)
   12720 	{
   12721 	  amt = sizeof *m;
   12722 	  m = bfd_zalloc (abfd, amt);
   12723 	  if (m == NULL)
   12724 	    return false;
   12725 
   12726 	  m->p_type = PT_MIPS_ABIFLAGS;
   12727 	  m->count = 1;
   12728 	  m->sections[0] = s;
   12729 
   12730 	  /* We want to put it after the PHDR and INTERP segments.  */
   12731 	  pm = &elf_seg_map (abfd);
   12732 	  while (*pm != NULL
   12733 		 && ((*pm)->p_type == PT_PHDR
   12734 		     || (*pm)->p_type == PT_INTERP))
   12735 	    pm = &(*pm)->next;
   12736 
   12737 	  m->next = *pm;
   12738 	  *pm = m;
   12739 	}
   12740     }
   12741 
   12742   /* For IRIX 6, we don't have .mdebug sections, nor does anything but
   12743      .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
   12744      PT_MIPS_OPTIONS segment immediately following the program header
   12745      table.  */
   12746   if (NEWABI_P (abfd)
   12747       /* On non-IRIX6 new abi, we'll have already created a segment
   12748 	 for this section, so don't create another.  I'm not sure this
   12749 	 is not also the case for IRIX 6, but I can't test it right
   12750 	 now.  */
   12751       && IRIX_COMPAT (abfd) == ict_irix6)
   12752     {
   12753       for (s = abfd->sections; s; s = s->next)
   12754 	if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
   12755 	  break;
   12756 
   12757       if (s)
   12758 	{
   12759 	  struct elf_segment_map *options_segment;
   12760 
   12761 	  pm = &elf_seg_map (abfd);
   12762 	  while (*pm != NULL
   12763 		 && ((*pm)->p_type == PT_PHDR
   12764 		     || (*pm)->p_type == PT_INTERP))
   12765 	    pm = &(*pm)->next;
   12766 
   12767 	  if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
   12768 	    {
   12769 	      amt = sizeof (struct elf_segment_map);
   12770 	      options_segment = bfd_zalloc (abfd, amt);
   12771 	      options_segment->next = *pm;
   12772 	      options_segment->p_type = PT_MIPS_OPTIONS;
   12773 	      options_segment->p_flags = PF_R;
   12774 	      options_segment->p_flags_valid = true;
   12775 	      options_segment->count = 1;
   12776 	      options_segment->sections[0] = s;
   12777 	      *pm = options_segment;
   12778 	    }
   12779 	}
   12780     }
   12781   else
   12782     {
   12783       if (IRIX_COMPAT (abfd) == ict_irix5)
   12784 	{
   12785 	  /* If there are .dynamic and .mdebug sections, we make a room
   12786 	     for the RTPROC header.  FIXME: Rewrite without section names.  */
   12787 	  if (bfd_get_section_by_name (abfd, ".interp") == NULL
   12788 	      && bfd_get_section_by_name (abfd, ".dynamic") != NULL
   12789 	      && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
   12790 	    {
   12791 	      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12792 		if (m->p_type == PT_MIPS_RTPROC)
   12793 		  break;
   12794 	      if (m == NULL)
   12795 		{
   12796 		  amt = sizeof *m;
   12797 		  m = bfd_zalloc (abfd, amt);
   12798 		  if (m == NULL)
   12799 		    return false;
   12800 
   12801 		  m->p_type = PT_MIPS_RTPROC;
   12802 
   12803 		  s = bfd_get_section_by_name (abfd, ".rtproc");
   12804 		  if (s == NULL)
   12805 		    {
   12806 		      m->count = 0;
   12807 		      m->p_flags = 0;
   12808 		      m->p_flags_valid = 1;
   12809 		    }
   12810 		  else
   12811 		    {
   12812 		      m->count = 1;
   12813 		      m->sections[0] = s;
   12814 		    }
   12815 
   12816 		  /* We want to put it after the DYNAMIC segment.  */
   12817 		  pm = &elf_seg_map (abfd);
   12818 		  while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
   12819 		    pm = &(*pm)->next;
   12820 		  if (*pm != NULL)
   12821 		    pm = &(*pm)->next;
   12822 
   12823 		  m->next = *pm;
   12824 		  *pm = m;
   12825 		}
   12826 	    }
   12827 	}
   12828       /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
   12829 	 .dynstr, .dynsym, and .hash sections, and everything in
   12830 	 between.  */
   12831       for (pm = &elf_seg_map (abfd); *pm != NULL;
   12832 	   pm = &(*pm)->next)
   12833 	if ((*pm)->p_type == PT_DYNAMIC)
   12834 	  break;
   12835       m = *pm;
   12836       /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
   12837 	 glibc's dynamic linker has traditionally derived the number of
   12838 	 tags from the p_filesz field, and sometimes allocates stack
   12839 	 arrays of that size.  An overly-big PT_DYNAMIC segment can
   12840 	 be actively harmful in such cases.  Making PT_DYNAMIC contain
   12841 	 other sections can also make life hard for the prelinker,
   12842 	 which might move one of the other sections to a different
   12843 	 PT_LOAD segment.  */
   12844       if (SGI_COMPAT (abfd)
   12845 	  && m != NULL
   12846 	  && m->count == 1
   12847 	  && strcmp (m->sections[0]->name, ".dynamic") == 0)
   12848 	{
   12849 	  static const char *sec_names[] =
   12850 	  {
   12851 	    ".dynamic", ".dynstr", ".dynsym", ".hash"
   12852 	  };
   12853 	  bfd_vma low, high;
   12854 	  unsigned int i, c;
   12855 	  struct elf_segment_map *n;
   12856 
   12857 	  low = ~(bfd_vma) 0;
   12858 	  high = 0;
   12859 	  for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
   12860 	    {
   12861 	      s = bfd_get_section_by_name (abfd, sec_names[i]);
   12862 	      if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12863 		{
   12864 		  bfd_size_type sz;
   12865 
   12866 		  if (low > s->vma)
   12867 		    low = s->vma;
   12868 		  sz = s->size;
   12869 		  if (high < s->vma + sz)
   12870 		    high = s->vma + sz;
   12871 		}
   12872 	    }
   12873 
   12874 	  c = 0;
   12875 	  for (s = abfd->sections; s != NULL; s = s->next)
   12876 	    if ((s->flags & SEC_LOAD) != 0
   12877 		&& s->vma >= low
   12878 		&& s->vma + s->size <= high)
   12879 	      ++c;
   12880 
   12881 	  amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
   12882 	  n = bfd_zalloc (abfd, amt);
   12883 	  if (n == NULL)
   12884 	    return false;
   12885 	  *n = *m;
   12886 	  n->count = c;
   12887 
   12888 	  i = 0;
   12889 	  for (s = abfd->sections; s != NULL; s = s->next)
   12890 	    {
   12891 	      if ((s->flags & SEC_LOAD) != 0
   12892 		  && s->vma >= low
   12893 		  && s->vma + s->size <= high)
   12894 		{
   12895 		  n->sections[i] = s;
   12896 		  ++i;
   12897 		}
   12898 	    }
   12899 
   12900 	  *pm = n;
   12901 	}
   12902     }
   12903 
   12904   /* Allocate a spare program header in dynamic objects so that tools
   12905      like the prelinker can add an extra PT_LOAD entry.
   12906 
   12907      If the prelinker needs to make room for a new PT_LOAD entry, its
   12908      standard procedure is to move the first (read-only) sections into
   12909      the new (writable) segment.  However, the MIPS ABI requires
   12910      .dynamic to be in a read-only segment, and the section will often
   12911      start within sizeof (ElfNN_Phdr) bytes of the last program header.
   12912 
   12913      Although the prelinker could in principle move .dynamic to a
   12914      writable segment, it seems better to allocate a spare program
   12915      header instead, and avoid the need to move any sections.
   12916      There is a long tradition of allocating spare dynamic tags,
   12917      so allocating a spare program header seems like a natural
   12918      extension.
   12919 
   12920      If INFO is NULL, we may be copying an already prelinked binary
   12921      with objcopy or strip, so do not add this header.  */
   12922   if (info != NULL
   12923       && !SGI_COMPAT (abfd)
   12924       && bfd_get_section_by_name (abfd, ".dynamic"))
   12925     {
   12926       for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
   12927 	if ((*pm)->p_type == PT_NULL)
   12928 	  break;
   12929       if (*pm == NULL)
   12930 	{
   12931 	  m = bfd_zalloc (abfd, sizeof (*m));
   12932 	  if (m == NULL)
   12933 	    return false;
   12934 
   12935 	  m->p_type = PT_NULL;
   12936 	  *pm = m;
   12937 	}
   12938     }
   12939 
   12940   return true;
   12941 }
   12942 
   12943 /* Return the section that should be marked against GC for a given
   12945    relocation.  */
   12946 
   12947 asection *
   12948 _bfd_mips_elf_gc_mark_hook (asection *sec,
   12949 			    struct bfd_link_info *info,
   12950 			    Elf_Internal_Rela *rel,
   12951 			    struct elf_link_hash_entry *h,
   12952 			    Elf_Internal_Sym *sym)
   12953 {
   12954   /* ??? Do mips16 stub sections need to be handled special?  */
   12955 
   12956   if (h != NULL)
   12957     switch (ELF_R_TYPE (sec->owner, rel->r_info))
   12958       {
   12959       case R_MIPS_GNU_VTINHERIT:
   12960       case R_MIPS_GNU_VTENTRY:
   12961 	return NULL;
   12962       }
   12963 
   12964   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   12965 }
   12966 
   12967 /* Prevent .MIPS.abiflags from being discarded with --gc-sections.  */
   12968 
   12969 bool
   12970 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
   12971 				      elf_gc_mark_hook_fn gc_mark_hook)
   12972 {
   12973   bfd *sub;
   12974 
   12975   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
   12976 
   12977   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12978     {
   12979       asection *o;
   12980 
   12981       if (! is_mips_elf (sub))
   12982 	continue;
   12983 
   12984       for (o = sub->sections; o != NULL; o = o->next)
   12985 	if (!o->gc_mark
   12986 	    && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
   12987 	  {
   12988 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   12989 	      return false;
   12990 	  }
   12991     }
   12992 
   12993   return true;
   12994 }
   12995 
   12996 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
   12998    hiding the old indirect symbol.  Process additional relocation
   12999    information.  Also called for weakdefs, in which case we just let
   13000    _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
   13001 
   13002 void
   13003 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
   13004 				    struct elf_link_hash_entry *dir,
   13005 				    struct elf_link_hash_entry *ind)
   13006 {
   13007   struct mips_elf_link_hash_entry *dirmips, *indmips;
   13008 
   13009   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   13010 
   13011   dirmips = (struct mips_elf_link_hash_entry *) dir;
   13012   indmips = (struct mips_elf_link_hash_entry *) ind;
   13013   /* Any absolute non-dynamic relocations against an indirect or weak
   13014      definition will be against the target symbol.  */
   13015   if (indmips->has_static_relocs)
   13016     dirmips->has_static_relocs = true;
   13017 
   13018   if (ind->root.type != bfd_link_hash_indirect)
   13019     return;
   13020 
   13021   dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
   13022   if (indmips->readonly_reloc)
   13023     dirmips->readonly_reloc = true;
   13024   if (indmips->no_fn_stub)
   13025     dirmips->no_fn_stub = true;
   13026   if (indmips->fn_stub)
   13027     {
   13028       dirmips->fn_stub = indmips->fn_stub;
   13029       indmips->fn_stub = NULL;
   13030     }
   13031   if (indmips->need_fn_stub)
   13032     {
   13033       dirmips->need_fn_stub = true;
   13034       indmips->need_fn_stub = false;
   13035     }
   13036   if (indmips->call_stub)
   13037     {
   13038       dirmips->call_stub = indmips->call_stub;
   13039       indmips->call_stub = NULL;
   13040     }
   13041   if (indmips->call_fp_stub)
   13042     {
   13043       dirmips->call_fp_stub = indmips->call_fp_stub;
   13044       indmips->call_fp_stub = NULL;
   13045     }
   13046   if (indmips->global_got_area < dirmips->global_got_area)
   13047     dirmips->global_got_area = indmips->global_got_area;
   13048   if (indmips->global_got_area < GGA_NONE)
   13049     indmips->global_got_area = GGA_NONE;
   13050   if (indmips->has_nonpic_branches)
   13051     dirmips->has_nonpic_branches = true;
   13052 }
   13053 
   13054 /* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
   13055    to hide it.  It has to remain global (it will also be protected) so as to
   13056    be assigned a global GOT entry, which will then remain unchanged at load
   13057    time.  */
   13058 
   13059 void
   13060 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
   13061 			   struct elf_link_hash_entry *entry,
   13062 			   bool force_local)
   13063 {
   13064   struct mips_elf_link_hash_table *htab;
   13065 
   13066   htab = mips_elf_hash_table (info);
   13067   BFD_ASSERT (htab != NULL);
   13068   if (htab->use_absolute_zero
   13069       && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
   13070     return;
   13071 
   13072   _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
   13073 }
   13074 
   13075 #define PDR_SIZE 32
   13077 
   13078 bool
   13079 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
   13080 			    struct bfd_link_info *info)
   13081 {
   13082   asection *o;
   13083   bool ret = false;
   13084   unsigned char *tdata;
   13085   size_t i, skip;
   13086 
   13087   o = bfd_get_section_by_name (abfd, ".pdr");
   13088   if (! o)
   13089     return false;
   13090   if (o->size == 0)
   13091     return false;
   13092   if (o->size % PDR_SIZE != 0)
   13093     return false;
   13094   if (o->output_section != NULL
   13095       && bfd_is_abs_section (o->output_section))
   13096     return false;
   13097 
   13098   tdata = bfd_zmalloc (o->size / PDR_SIZE);
   13099   if (! tdata)
   13100     return false;
   13101 
   13102   cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   13103 					    info->keep_memory);
   13104   if (!cookie->rels)
   13105     {
   13106       free (tdata);
   13107       return false;
   13108     }
   13109 
   13110   cookie->rel = cookie->rels;
   13111   cookie->relend = cookie->rels + o->reloc_count;
   13112 
   13113   for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
   13114     {
   13115       if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
   13116 	{
   13117 	  tdata[i] = 1;
   13118 	  skip ++;
   13119 	}
   13120     }
   13121 
   13122   if (skip != 0)
   13123     {
   13124       mips_elf_section_data (o)->u.tdata = tdata;
   13125       if (o->rawsize == 0)
   13126 	o->rawsize = o->size;
   13127       o->size -= skip * PDR_SIZE;
   13128       ret = true;
   13129     }
   13130   else
   13131     free (tdata);
   13132 
   13133   if (! info->keep_memory)
   13134     free (cookie->rels);
   13135 
   13136   return ret;
   13137 }
   13138 
   13139 bool
   13140 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
   13141 {
   13142   if (strcmp (sec->name, ".pdr") == 0)
   13143     return true;
   13144   return false;
   13145 }
   13146 
   13147 bool
   13148 _bfd_mips_elf_write_section (bfd *output_bfd,
   13149 			     struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   13150 			     asection *sec, bfd_byte *contents)
   13151 {
   13152   bfd_byte *to, *from, *end;
   13153   int i;
   13154 
   13155   if (strcmp (sec->name, ".pdr") != 0)
   13156     return false;
   13157 
   13158   if (mips_elf_section_data (sec)->u.tdata == NULL)
   13159     return false;
   13160 
   13161   to = contents;
   13162   end = contents + sec->size;
   13163   for (from = contents, i = 0;
   13164        from < end;
   13165        from += PDR_SIZE, i++)
   13166     {
   13167       if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
   13168 	continue;
   13169       if (to != from)
   13170 	memcpy (to, from, PDR_SIZE);
   13171       to += PDR_SIZE;
   13172     }
   13173   bfd_set_section_contents (output_bfd, sec->output_section, contents,
   13174 			    sec->output_offset, sec->size);
   13175   return true;
   13176 }
   13177 
   13178 /* microMIPS code retains local labels for linker relaxation.  Omit them
   13180    from output by default for clarity.  */
   13181 
   13182 bool
   13183 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
   13184 {
   13185   return _bfd_elf_is_local_label_name (abfd, sym->name);
   13186 }
   13187 
   13188 bool
   13189 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
   13190 				 asection *section, bfd_vma offset,
   13191 				 const char **filename_ptr,
   13192 				 const char **functionname_ptr,
   13193 				 unsigned int *line_ptr,
   13194 				 unsigned int *discriminator_ptr)
   13195 {
   13196   asection *msec;
   13197 
   13198   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   13199 				     filename_ptr, functionname_ptr,
   13200 				     line_ptr, discriminator_ptr,
   13201 				     dwarf_debug_sections,
   13202 				     &elf_tdata (abfd)->dwarf2_find_line_info)
   13203       == 1)
   13204     return true;
   13205 
   13206   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   13207 				     filename_ptr, functionname_ptr,
   13208 				     line_ptr))
   13209     {
   13210       if (!*functionname_ptr)
   13211 	_bfd_elf_find_function (abfd, symbols, section, offset,
   13212 				*filename_ptr ? NULL : filename_ptr,
   13213 				functionname_ptr);
   13214       return true;
   13215     }
   13216 
   13217   msec = bfd_get_section_by_name (abfd, ".mdebug");
   13218   if (msec != NULL)
   13219     {
   13220       flagword origflags;
   13221       struct mips_elf_find_line *fi;
   13222       const struct ecoff_debug_swap * const swap =
   13223 	get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   13224 
   13225       /* If we are called during a link, mips_elf_final_link may have
   13226 	 cleared the SEC_HAS_CONTENTS field.  We force it back on here
   13227 	 if appropriate (which it normally will be).  */
   13228       origflags = msec->flags;
   13229       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
   13230 	msec->flags |= SEC_HAS_CONTENTS;
   13231 
   13232       fi = mips_elf_tdata (abfd)->find_line_info;
   13233       if (fi == NULL)
   13234 	{
   13235 	  bfd_size_type external_fdr_size;
   13236 	  char *fraw_src;
   13237 	  char *fraw_end;
   13238 	  struct fdr *fdr_ptr;
   13239 	  bfd_size_type amt = sizeof (struct mips_elf_find_line);
   13240 
   13241 	  fi = bfd_zalloc (abfd, amt);
   13242 	  if (fi == NULL)
   13243 	    {
   13244 	      msec->flags = origflags;
   13245 	      return false;
   13246 	    }
   13247 
   13248 	  if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
   13249 	    {
   13250 	      msec->flags = origflags;
   13251 	      return false;
   13252 	    }
   13253 
   13254 	  /* Swap in the FDR information.  */
   13255 	  amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
   13256 	  fi->d.fdr = bfd_alloc (abfd, amt);
   13257 	  if (fi->d.fdr == NULL)
   13258 	    {
   13259 	      _bfd_ecoff_free_ecoff_debug_info (&fi->d);
   13260 	      msec->flags = origflags;
   13261 	      return false;
   13262 	    }
   13263 	  external_fdr_size = swap->external_fdr_size;
   13264 	  fdr_ptr = fi->d.fdr;
   13265 	  fraw_src = (char *) fi->d.external_fdr;
   13266 	  fraw_end = (fraw_src
   13267 		      + fi->d.symbolic_header.ifdMax * external_fdr_size);
   13268 	  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
   13269 	    (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
   13270 
   13271 	  mips_elf_tdata (abfd)->find_line_info = fi;
   13272 	}
   13273 
   13274       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
   13275 				  &fi->i, filename_ptr, functionname_ptr,
   13276 				  line_ptr))
   13277 	{
   13278 	  msec->flags = origflags;
   13279 	  return true;
   13280 	}
   13281 
   13282       msec->flags = origflags;
   13283     }
   13284 
   13285   /* Fall back on the generic ELF find_nearest_line routine.  */
   13286 
   13287   return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
   13288 				     filename_ptr, functionname_ptr,
   13289 				     line_ptr, discriminator_ptr);
   13290 }
   13291 
   13292 bool
   13293 _bfd_mips_elf_find_inliner_info (bfd *abfd,
   13294 				 const char **filename_ptr,
   13295 				 const char **functionname_ptr,
   13296 				 unsigned int *line_ptr)
   13297 {
   13298   bool found;
   13299   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   13300 					 functionname_ptr, line_ptr,
   13301 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   13302   return found;
   13303 }
   13304 
   13305 
   13306 /* When are writing out the .options or .MIPS.options section,
   13308    remember the bytes we are writing out, so that we can install the
   13309    GP value in the section_processing routine.  */
   13310 
   13311 bool
   13312 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
   13313 				    const void *location,
   13314 				    file_ptr offset, bfd_size_type count)
   13315 {
   13316   if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
   13317     {
   13318       bfd_byte *c;
   13319 
   13320       if (elf_section_data (section) == NULL)
   13321 	{
   13322 	  size_t amt = sizeof (struct bfd_elf_section_data);
   13323 	  section->used_by_bfd = bfd_zalloc (abfd, amt);
   13324 	  if (elf_section_data (section) == NULL)
   13325 	    return false;
   13326 	}
   13327       c = mips_elf_section_data (section)->u.tdata;
   13328       if (c == NULL)
   13329 	{
   13330 	  c = bfd_zalloc (abfd, section->size);
   13331 	  if (c == NULL)
   13332 	    return false;
   13333 	  mips_elf_section_data (section)->u.tdata = c;
   13334 	}
   13335 
   13336       memcpy (c + offset, location, count);
   13337     }
   13338 
   13339   return _bfd_elf_set_section_contents (abfd, section, location, offset,
   13340 					count);
   13341 }
   13342 
   13343 /* This is almost identical to bfd_generic_get_... except that some
   13344    MIPS relocations need to be handled specially.  Sigh.  */
   13345 
   13346 bfd_byte *
   13347 _bfd_elf_mips_get_relocated_section_contents
   13348   (bfd *abfd,
   13349    struct bfd_link_info *link_info,
   13350    struct bfd_link_order *link_order,
   13351    bfd_byte *data,
   13352    bool relocatable,
   13353    asymbol **symbols)
   13354 {
   13355   bfd *input_bfd = link_order->u.indirect.section->owner;
   13356   asection *input_section = link_order->u.indirect.section;
   13357   long reloc_size;
   13358   arelent **reloc_vector;
   13359   long reloc_count;
   13360 
   13361   reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
   13362   if (reloc_size < 0)
   13363     return NULL;
   13364 
   13365   /* Read in the section.  */
   13366   bfd_byte *orig_data = data;
   13367   if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
   13368     return NULL;
   13369 
   13370   if (data == NULL)
   13371     return NULL;
   13372 
   13373   if (reloc_size == 0)
   13374     return data;
   13375 
   13376   reloc_vector = (arelent **) bfd_malloc (reloc_size);
   13377   if (reloc_vector == NULL)
   13378     {
   13379       struct mips_elf_obj_tdata *tdata;
   13380       struct mips_hi16 **hip, *hi;
   13381     error_return:
   13382       /* If we are going to return an error, remove entries on
   13383 	 mips_hi16_list that point into this section's data.  Data
   13384 	 will typically be freed on return from this function.  */
   13385       tdata = mips_elf_tdata (abfd);
   13386       hip = &tdata->mips_hi16_list;
   13387       while ((hi = *hip) != NULL)
   13388 	{
   13389 	  if (hi->input_section == input_section)
   13390 	    {
   13391 	      *hip = hi->next;
   13392 	      free (hi);
   13393 	    }
   13394 	  else
   13395 	    hip = &hi->next;
   13396 	}
   13397       if (orig_data == NULL)
   13398 	free (data);
   13399       data = NULL;
   13400       goto out;
   13401     }
   13402 
   13403   reloc_count = bfd_canonicalize_reloc (input_bfd,
   13404 					input_section,
   13405 					reloc_vector,
   13406 					symbols);
   13407   if (reloc_count < 0)
   13408     goto error_return;
   13409 
   13410   if (reloc_count > 0)
   13411     {
   13412       arelent **parent;
   13413       /* for mips */
   13414       int gp_found;
   13415       bfd_vma gp = 0x12345678;	/* initialize just to shut gcc up */
   13416 
   13417       {
   13418 	struct bfd_hash_entry *h;
   13419 	struct bfd_link_hash_entry *lh;
   13420 	/* Skip all this stuff if we aren't mixing formats.  */
   13421 	if (abfd && input_bfd
   13422 	    && abfd->xvec == input_bfd->xvec)
   13423 	  lh = 0;
   13424 	else
   13425 	  {
   13426 	    h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
   13427 	    lh = (struct bfd_link_hash_entry *) h;
   13428 	  }
   13429       lookup:
   13430 	if (lh)
   13431 	  {
   13432 	    switch (lh->type)
   13433 	      {
   13434 	      case bfd_link_hash_undefined:
   13435 	      case bfd_link_hash_undefweak:
   13436 	      case bfd_link_hash_common:
   13437 		gp_found = 0;
   13438 		break;
   13439 	      case bfd_link_hash_defined:
   13440 	      case bfd_link_hash_defweak:
   13441 		gp_found = 1;
   13442 		gp = lh->u.def.value;
   13443 		break;
   13444 	      case bfd_link_hash_indirect:
   13445 	      case bfd_link_hash_warning:
   13446 		lh = lh->u.i.link;
   13447 		/* @@FIXME  ignoring warning for now */
   13448 		goto lookup;
   13449 	      case bfd_link_hash_new:
   13450 	      default:
   13451 		abort ();
   13452 	      }
   13453 	  }
   13454 	else
   13455 	  gp_found = 0;
   13456       }
   13457       /* end mips */
   13458 
   13459       for (parent = reloc_vector; *parent != NULL; parent++)
   13460 	{
   13461 	  char *error_message = NULL;
   13462 	  asymbol *symbol;
   13463 	  bfd_reloc_status_type r;
   13464 
   13465 	  symbol = *(*parent)->sym_ptr_ptr;
   13466 	  /* PR ld/19628: A specially crafted input file
   13467 	     can result in a NULL symbol pointer here.  */
   13468 	  if (symbol == NULL)
   13469 	    {
   13470 	      link_info->callbacks->einfo
   13471 		/* xgettext:c-format */
   13472 		(_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
   13473 		 abfd, input_section, (* parent)->address);
   13474 	      goto error_return;
   13475 	    }
   13476 
   13477 	  /* Zap reloc field when the symbol is from a discarded
   13478 	     section, ignoring any addend.  Do the same when called
   13479 	     from bfd_simple_get_relocated_section_contents for
   13480 	     undefined symbols in debug sections.  This is to keep
   13481 	     debug info reasonably sane, in particular so that
   13482 	     DW_FORM_ref_addr to another file's .debug_info isn't
   13483 	     confused with an offset into the current file's
   13484 	     .debug_info.  */
   13485 	  if ((symbol->section != NULL && discarded_section (symbol->section))
   13486 	      || (symbol->section == bfd_und_section_ptr
   13487 		  && (input_section->flags & SEC_DEBUGGING) != 0
   13488 		  && link_info->input_bfds == link_info->output_bfd))
   13489 	    {
   13490 	      bfd_vma off;
   13491 	      static reloc_howto_type none_howto
   13492 		= HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
   13493 			 "unused", false, 0, 0, false);
   13494 
   13495 	      off = ((*parent)->address
   13496 		     * bfd_octets_per_byte (input_bfd, input_section));
   13497 	      _bfd_clear_contents ((*parent)->howto, input_bfd,
   13498 				   input_section, data, off);
   13499 	      (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
   13500 	      (*parent)->addend = 0;
   13501 	      (*parent)->howto = &none_howto;
   13502 	      r = bfd_reloc_ok;
   13503 	    }
   13504 
   13505 	  /* Specific to MIPS: Deal with relocation types that require
   13506 	     knowing the gp of the output bfd.  */
   13507 
   13508 	  /* If we've managed to find the gp and have a special
   13509 	     function for the relocation then go ahead, else default
   13510 	     to the generic handling.  */
   13511 	  else if (gp_found
   13512 		   && ((*parent)->howto->special_function
   13513 		       == _bfd_mips_elf32_gprel16_reloc))
   13514 	    r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
   13515 					       input_section, relocatable,
   13516 					       data, gp);
   13517 	  else
   13518 	    r = bfd_perform_relocation (input_bfd,
   13519 					*parent,
   13520 					data,
   13521 					input_section,
   13522 					relocatable ? abfd : NULL,
   13523 					&error_message);
   13524 
   13525 	  if (relocatable)
   13526 	    {
   13527 	      asection *os = input_section->output_section;
   13528 
   13529 	      /* A partial link, so keep the relocs.  */
   13530 	      os->orelocation[os->reloc_count] = *parent;
   13531 	      os->reloc_count++;
   13532 	    }
   13533 
   13534 	  if (r != bfd_reloc_ok)
   13535 	    {
   13536 	      switch (r)
   13537 		{
   13538 		case bfd_reloc_undefined:
   13539 		  (*link_info->callbacks->undefined_symbol)
   13540 		    (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
   13541 		     input_bfd, input_section, (*parent)->address, true);
   13542 		  break;
   13543 		case bfd_reloc_dangerous:
   13544 		  BFD_ASSERT (error_message != NULL);
   13545 		  (*link_info->callbacks->reloc_dangerous)
   13546 		    (link_info, error_message,
   13547 		     input_bfd, input_section, (*parent)->address);
   13548 		  break;
   13549 		case bfd_reloc_overflow:
   13550 		  (*link_info->callbacks->reloc_overflow)
   13551 		    (link_info, NULL,
   13552 		     bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
   13553 		     (*parent)->howto->name, (*parent)->addend,
   13554 		     input_bfd, input_section, (*parent)->address);
   13555 		  break;
   13556 		case bfd_reloc_outofrange:
   13557 		  /* PR ld/13730:
   13558 		     This error can result when processing some partially
   13559 		     complete binaries.  Do not abort, but issue an error
   13560 		     message instead.  */
   13561 		  link_info->callbacks->einfo
   13562 		    /* xgettext:c-format */
   13563 		    (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
   13564 		     abfd, input_section, * parent);
   13565 		  goto error_return;
   13566 
   13567 		case bfd_reloc_notsupported:
   13568 		  /* PR ld/17512
   13569 		     This error can result when processing a corrupt binary.
   13570 		     Do not abort.  Issue an error message instead.  */
   13571 		  link_info->callbacks->einfo
   13572 		    /* xgettext:c-format */
   13573 		    (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
   13574 		     abfd, input_section, * parent);
   13575 		  goto error_return;
   13576 
   13577 		default:
   13578 		  /* PR 17512; file: 90c2a92e.
   13579 		     Report unexpected results, without aborting.  */
   13580 		  link_info->callbacks->einfo
   13581 		    /* xgettext:c-format */
   13582 		    (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
   13583 		     abfd, input_section, * parent, r);
   13584 		  break;
   13585 		}
   13586 
   13587 	    }
   13588 	}
   13589     }
   13590 
   13591  out:
   13592   free (reloc_vector);
   13593   return data;
   13594 }
   13595 
   13596 static bool
   13598 mips_elf_relax_delete_bytes (bfd *abfd,
   13599 			     asection *sec, bfd_vma addr, int count)
   13600 {
   13601   Elf_Internal_Shdr *symtab_hdr;
   13602   unsigned int sec_shndx;
   13603   bfd_byte *contents;
   13604   Elf_Internal_Rela *irel, *irelend;
   13605   Elf_Internal_Sym *isym;
   13606   Elf_Internal_Sym *isymend;
   13607   struct elf_link_hash_entry **sym_hashes;
   13608   struct elf_link_hash_entry **end_hashes;
   13609   struct elf_link_hash_entry **start_hashes;
   13610   unsigned int symcount;
   13611 
   13612   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   13613   contents = elf_section_data (sec)->this_hdr.contents;
   13614 
   13615   irel = elf_section_data (sec)->relocs;
   13616   irelend = irel + sec->reloc_count;
   13617 
   13618   /* Actually delete the bytes.  */
   13619   memmove (contents + addr, contents + addr + count,
   13620 	   (size_t) (sec->size - addr - count));
   13621   sec->size -= count;
   13622 
   13623   /* Adjust all the relocs.  */
   13624   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
   13625     {
   13626       /* Get the new reloc address.  */
   13627       if (irel->r_offset > addr)
   13628 	irel->r_offset -= count;
   13629     }
   13630 
   13631   BFD_ASSERT (addr % 2 == 0);
   13632   BFD_ASSERT (count % 2 == 0);
   13633 
   13634   /* Adjust the local symbols defined in this section.  */
   13635   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13636   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
   13637   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
   13638     if (isym->st_shndx == sec_shndx && isym->st_value > addr)
   13639       isym->st_value -= count;
   13640 
   13641   /* Now adjust the global symbols defined in this section.  */
   13642   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   13643 	      - symtab_hdr->sh_info);
   13644   sym_hashes = start_hashes = elf_sym_hashes (abfd);
   13645   end_hashes = sym_hashes + symcount;
   13646 
   13647   for (; sym_hashes < end_hashes; sym_hashes++)
   13648     {
   13649       struct elf_link_hash_entry *sym_hash = *sym_hashes;
   13650 
   13651       if ((sym_hash->root.type == bfd_link_hash_defined
   13652 	   || sym_hash->root.type == bfd_link_hash_defweak)
   13653 	  && sym_hash->root.u.def.section == sec)
   13654 	{
   13655 	  bfd_vma value = sym_hash->root.u.def.value;
   13656 
   13657 	  if (ELF_ST_IS_MICROMIPS (sym_hash->other))
   13658 	    value &= MINUS_TWO;
   13659 	  if (value > addr)
   13660 	    sym_hash->root.u.def.value -= count;
   13661 	}
   13662     }
   13663 
   13664   return true;
   13665 }
   13666 
   13667 
   13668 /* Opcodes needed for microMIPS relaxation as found in
   13669    opcodes/micromips-opc.c.  */
   13670 
   13671 struct opcode_descriptor {
   13672   unsigned long match;
   13673   unsigned long mask;
   13674 };
   13675 
   13676 /* The $ra register aka $31.  */
   13677 
   13678 #define RA 31
   13679 
   13680 /* 32-bit instruction format register fields.  */
   13681 
   13682 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
   13683 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
   13684 
   13685 /* Check if a 5-bit register index can be abbreviated to 3 bits.  */
   13686 
   13687 #define OP16_VALID_REG(r) \
   13688   ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
   13689 
   13690 
   13691 /* 32-bit and 16-bit branches.  */
   13692 
   13693 static const struct opcode_descriptor b_insns_32[] = {
   13694   { /* "b",	"p",		*/ 0x40400000, 0xffff0000 }, /* bgez 0 */
   13695   { /* "b",	"p",		*/ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
   13696   { 0, 0 }  /* End marker for find_match().  */
   13697 };
   13698 
   13699 static const struct opcode_descriptor bc_insn_32 =
   13700   { /* "bc(1|2)(ft)", "N,p",	*/ 0x42800000, 0xfec30000 };
   13701 
   13702 static const struct opcode_descriptor bz_insn_32 =
   13703   { /* "b(g|l)(e|t)z", "s,p",	*/ 0x40000000, 0xff200000 };
   13704 
   13705 static const struct opcode_descriptor bzal_insn_32 =
   13706   { /* "b(ge|lt)zal", "s,p",	*/ 0x40200000, 0xffa00000 };
   13707 
   13708 static const struct opcode_descriptor beq_insn_32 =
   13709   { /* "b(eq|ne)", "s,t,p",	*/ 0x94000000, 0xdc000000 };
   13710 
   13711 static const struct opcode_descriptor b_insn_16 =
   13712   { /* "b",	"mD",		*/ 0xcc00,     0xfc00 };
   13713 
   13714 static const struct opcode_descriptor bz_insn_16 =
   13715   { /* "b(eq|ne)z", "md,mE",	*/ 0x8c00,     0xdc00 };
   13716 
   13717 
   13718 /* 32-bit and 16-bit branch EQ and NE zero.  */
   13719 
   13720 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
   13721    eq and second the ne.  This convention is used when replacing a
   13722    32-bit BEQ/BNE with the 16-bit version.  */
   13723 
   13724 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
   13725 
   13726 static const struct opcode_descriptor bz_rs_insns_32[] = {
   13727   { /* "beqz",	"s,p",		*/ 0x94000000, 0xffe00000 },
   13728   { /* "bnez",	"s,p",		*/ 0xb4000000, 0xffe00000 },
   13729   { 0, 0 }  /* End marker for find_match().  */
   13730 };
   13731 
   13732 static const struct opcode_descriptor bz_rt_insns_32[] = {
   13733   { /* "beqz",	"t,p",		*/ 0x94000000, 0xfc01f000 },
   13734   { /* "bnez",	"t,p",		*/ 0xb4000000, 0xfc01f000 },
   13735   { 0, 0 }  /* End marker for find_match().  */
   13736 };
   13737 
   13738 static const struct opcode_descriptor bzc_insns_32[] = {
   13739   { /* "beqzc",	"s,p",		*/ 0x40e00000, 0xffe00000 },
   13740   { /* "bnezc",	"s,p",		*/ 0x40a00000, 0xffe00000 },
   13741   { 0, 0 }  /* End marker for find_match().  */
   13742 };
   13743 
   13744 static const struct opcode_descriptor bz_insns_16[] = {
   13745   { /* "beqz",	"md,mE",	*/ 0x8c00,     0xfc00 },
   13746   { /* "bnez",	"md,mE",	*/ 0xac00,     0xfc00 },
   13747   { 0, 0 }  /* End marker for find_match().  */
   13748 };
   13749 
   13750 /* Switch between a 5-bit register index and its 3-bit shorthand.  */
   13751 
   13752 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
   13753 #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
   13754 
   13755 
   13756 /* 32-bit instructions with a delay slot.  */
   13757 
   13758 static const struct opcode_descriptor jal_insn_32_bd16 =
   13759   { /* "jals",	"a",		*/ 0x74000000, 0xfc000000 };
   13760 
   13761 static const struct opcode_descriptor jal_insn_32_bd32 =
   13762   { /* "jal",	"a",		*/ 0xf4000000, 0xfc000000 };
   13763 
   13764 static const struct opcode_descriptor jal_x_insn_32_bd32 =
   13765   { /* "jal[x]", "a",		*/ 0xf0000000, 0xf8000000 };
   13766 
   13767 static const struct opcode_descriptor j_insn_32 =
   13768   { /* "j",	"a",		*/ 0xd4000000, 0xfc000000 };
   13769 
   13770 static const struct opcode_descriptor jalr_insn_32 =
   13771   { /* "jalr[.hb]", "t,s",	*/ 0x00000f3c, 0xfc00efff };
   13772 
   13773 /* This table can be compacted, because no opcode replacement is made.  */
   13774 
   13775 static const struct opcode_descriptor ds_insns_32_bd16[] = {
   13776   { /* "jals",	"a",		*/ 0x74000000, 0xfc000000 },
   13777 
   13778   { /* "jalrs[.hb]", "t,s",	*/ 0x00004f3c, 0xfc00efff },
   13779   { /* "b(ge|lt)zals", "s,p",	*/ 0x42200000, 0xffa00000 },
   13780 
   13781   { /* "b(g|l)(e|t)z", "s,p",	*/ 0x40000000, 0xff200000 },
   13782   { /* "b(eq|ne)", "s,t,p",	*/ 0x94000000, 0xdc000000 },
   13783   { /* "j",	"a",		*/ 0xd4000000, 0xfc000000 },
   13784   { 0, 0 }  /* End marker for find_match().  */
   13785 };
   13786 
   13787 /* This table can be compacted, because no opcode replacement is made.  */
   13788 
   13789 static const struct opcode_descriptor ds_insns_32_bd32[] = {
   13790   { /* "jal[x]", "a",		*/ 0xf0000000, 0xf8000000 },
   13791 
   13792   { /* "jalr[.hb]", "t,s",	*/ 0x00000f3c, 0xfc00efff },
   13793   { /* "b(ge|lt)zal", "s,p",	*/ 0x40200000, 0xffa00000 },
   13794   { 0, 0 }  /* End marker for find_match().  */
   13795 };
   13796 
   13797 
   13798 /* 16-bit instructions with a delay slot.  */
   13799 
   13800 static const struct opcode_descriptor jalr_insn_16_bd16 =
   13801   { /* "jalrs",	"my,mj",	*/ 0x45e0,     0xffe0 };
   13802 
   13803 static const struct opcode_descriptor jalr_insn_16_bd32 =
   13804   { /* "jalr",	"my,mj",	*/ 0x45c0,     0xffe0 };
   13805 
   13806 static const struct opcode_descriptor jr_insn_16 =
   13807   { /* "jr",	"mj",		*/ 0x4580,     0xffe0 };
   13808 
   13809 #define JR16_REG(opcode) ((opcode) & 0x1f)
   13810 
   13811 /* This table can be compacted, because no opcode replacement is made.  */
   13812 
   13813 static const struct opcode_descriptor ds_insns_16_bd16[] = {
   13814   { /* "jalrs",	"my,mj",	*/ 0x45e0,     0xffe0 },
   13815 
   13816   { /* "b",	"mD",		*/ 0xcc00,     0xfc00 },
   13817   { /* "b(eq|ne)z", "md,mE",	*/ 0x8c00,     0xdc00 },
   13818   { /* "jr",	"mj",		*/ 0x4580,     0xffe0 },
   13819   { 0, 0 }  /* End marker for find_match().  */
   13820 };
   13821 
   13822 
   13823 /* LUI instruction.  */
   13824 
   13825 static const struct opcode_descriptor lui_insn =
   13826  { /* "lui",	"s,u",		*/ 0x41a00000, 0xffe00000 };
   13827 
   13828 
   13829 /* ADDIU instruction.  */
   13830 
   13831 static const struct opcode_descriptor addiu_insn =
   13832   { /* "addiu",	"t,r,j",	*/ 0x30000000, 0xfc000000 };
   13833 
   13834 static const struct opcode_descriptor addiupc_insn =
   13835   { /* "addiu",	"mb,$pc,mQ",	*/ 0x78000000, 0xfc000000 };
   13836 
   13837 #define ADDIUPC_REG_FIELD(r) \
   13838   (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
   13839 
   13840 
   13841 /* Relaxable instructions in a JAL delay slot: MOVE.  */
   13842 
   13843 /* The 16-bit move has rd in 9:5 and rs in 4:0.  The 32-bit moves
   13844    (ADDU, OR) have rd in 15:11 and rs in 10:16.  */
   13845 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
   13846 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
   13847 
   13848 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
   13849 #define MOVE16_RS_FIELD(r) (((r) & 0x1f)     )
   13850 
   13851 static const struct opcode_descriptor move_insns_32[] = {
   13852   { /* "move",	"d,s",		*/ 0x00000290, 0xffe007ff }, /* or   d,s,$0 */
   13853   { /* "move",	"d,s",		*/ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
   13854   { 0, 0 }  /* End marker for find_match().  */
   13855 };
   13856 
   13857 static const struct opcode_descriptor move_insn_16 =
   13858   { /* "move",	"mp,mj",	*/ 0x0c00,     0xfc00 };
   13859 
   13860 
   13861 /* NOP instructions.  */
   13862 
   13863 static const struct opcode_descriptor nop_insn_32 =
   13864   { /* "nop",	"",		*/ 0x00000000, 0xffffffff };
   13865 
   13866 static const struct opcode_descriptor nop_insn_16 =
   13867   { /* "nop",	"",		*/ 0x0c00,     0xffff };
   13868 
   13869 
   13870 /* Instruction match support.  */
   13871 
   13872 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
   13873 
   13874 static int
   13875 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
   13876 {
   13877   unsigned long indx;
   13878 
   13879   for (indx = 0; insn[indx].mask != 0; indx++)
   13880     if (MATCH (opcode, insn[indx]))
   13881       return indx;
   13882 
   13883   return -1;
   13884 }
   13885 
   13886 
   13887 /* Branch and delay slot decoding support.  */
   13888 
   13889 /* If PTR points to what *might* be a 16-bit branch or jump, then
   13890    return the minimum length of its delay slot, otherwise return 0.
   13891    Non-zero results are not definitive as we might be checking against
   13892    the second half of another instruction.  */
   13893 
   13894 static int
   13895 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
   13896 {
   13897   unsigned long opcode;
   13898   int bdsize;
   13899 
   13900   opcode = bfd_get_16 (abfd, ptr);
   13901   if (MATCH (opcode, jalr_insn_16_bd32) != 0)
   13902     /* 16-bit branch/jump with a 32-bit delay slot.  */
   13903     bdsize = 4;
   13904   else if (MATCH (opcode, jalr_insn_16_bd16) != 0
   13905 	   || find_match (opcode, ds_insns_16_bd16) >= 0)
   13906     /* 16-bit branch/jump with a 16-bit delay slot.  */
   13907     bdsize = 2;
   13908   else
   13909     /* No delay slot.  */
   13910     bdsize = 0;
   13911 
   13912   return bdsize;
   13913 }
   13914 
   13915 /* If PTR points to what *might* be a 32-bit branch or jump, then
   13916    return the minimum length of its delay slot, otherwise return 0.
   13917    Non-zero results are not definitive as we might be checking against
   13918    the second half of another instruction.  */
   13919 
   13920 static int
   13921 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
   13922 {
   13923   unsigned long opcode;
   13924   int bdsize;
   13925 
   13926   opcode = bfd_get_micromips_32 (abfd, ptr);
   13927   if (find_match (opcode, ds_insns_32_bd32) >= 0)
   13928     /* 32-bit branch/jump with a 32-bit delay slot.  */
   13929     bdsize = 4;
   13930   else if (find_match (opcode, ds_insns_32_bd16) >= 0)
   13931     /* 32-bit branch/jump with a 16-bit delay slot.  */
   13932     bdsize = 2;
   13933   else
   13934     /* No delay slot.  */
   13935     bdsize = 0;
   13936 
   13937   return bdsize;
   13938 }
   13939 
   13940 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
   13941    that doesn't fiddle with REG, then return TRUE, otherwise FALSE.  */
   13942 
   13943 static bool
   13944 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
   13945 {
   13946   unsigned long opcode;
   13947 
   13948   opcode = bfd_get_16 (abfd, ptr);
   13949   if (MATCH (opcode, b_insn_16)
   13950 						/* B16  */
   13951       || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
   13952 						/* JR16  */
   13953       || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
   13954 						/* BEQZ16, BNEZ16  */
   13955       || (MATCH (opcode, jalr_insn_16_bd32)
   13956 						/* JALR16  */
   13957 	  && reg != JR16_REG (opcode) && reg != RA))
   13958     return true;
   13959 
   13960   return false;
   13961 }
   13962 
   13963 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
   13964    then return TRUE, otherwise FALSE.  */
   13965 
   13966 static bool
   13967 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
   13968 {
   13969   unsigned long opcode;
   13970 
   13971   opcode = bfd_get_micromips_32 (abfd, ptr);
   13972   if (MATCH (opcode, j_insn_32)
   13973 						/* J  */
   13974       || MATCH (opcode, bc_insn_32)
   13975 						/* BC1F, BC1T, BC2F, BC2T  */
   13976       || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
   13977 						/* JAL, JALX  */
   13978       || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
   13979 						/* BGEZ, BGTZ, BLEZ, BLTZ  */
   13980       || (MATCH (opcode, bzal_insn_32)
   13981 						/* BGEZAL, BLTZAL  */
   13982 	  && reg != OP32_SREG (opcode) && reg != RA)
   13983       || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
   13984 						/* JALR, JALR.HB, BEQ, BNE  */
   13985 	  && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
   13986     return true;
   13987 
   13988   return false;
   13989 }
   13990 
   13991 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
   13992    IRELEND) at OFFSET indicate that there must be a compact branch there,
   13993    then return TRUE, otherwise FALSE.  */
   13994 
   13995 static bool
   13996 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
   13997 		     const Elf_Internal_Rela *internal_relocs,
   13998 		     const Elf_Internal_Rela *irelend)
   13999 {
   14000   const Elf_Internal_Rela *irel;
   14001   unsigned long opcode;
   14002 
   14003   opcode = bfd_get_micromips_32 (abfd, ptr);
   14004   if (find_match (opcode, bzc_insns_32) < 0)
   14005     return false;
   14006 
   14007   for (irel = internal_relocs; irel < irelend; irel++)
   14008     if (irel->r_offset == offset
   14009 	&& ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
   14010       return true;
   14011 
   14012   return false;
   14013 }
   14014 
   14015 /* Bitsize checking.  */
   14016 #define IS_BITSIZE(val, N)						\
   14017   (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1)))		\
   14018     - (1ULL << ((N) - 1))) == (val))
   14019 
   14020 
   14021 bool
   14023 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
   14024 			     struct bfd_link_info *link_info,
   14025 			     bool *again)
   14026 {
   14027   bool insn32 = mips_elf_hash_table (link_info)->insn32;
   14028   Elf_Internal_Shdr *symtab_hdr;
   14029   Elf_Internal_Rela *internal_relocs;
   14030   Elf_Internal_Rela *irel, *irelend;
   14031   bfd_byte *contents = NULL;
   14032   Elf_Internal_Sym *isymbuf = NULL;
   14033 
   14034   /* Assume nothing changes.  */
   14035   *again = false;
   14036 
   14037   /* We don't have to do anything for a relocatable link, if
   14038      this section does not have relocs, or if this is not a
   14039      code section.  */
   14040 
   14041   if (bfd_link_relocatable (link_info)
   14042       || sec->reloc_count == 0
   14043       || (sec->flags & SEC_RELOC) == 0
   14044       || (sec->flags & SEC_HAS_CONTENTS) == 0
   14045       || (sec->flags & SEC_CODE) == 0)
   14046     return true;
   14047 
   14048   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   14049 
   14050   /* Get a copy of the native relocations.  */
   14051   internal_relocs = (_bfd_elf_link_read_relocs
   14052 		     (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
   14053 		      link_info->keep_memory));
   14054   if (internal_relocs == NULL)
   14055     goto error_return;
   14056 
   14057   /* Walk through them looking for relaxing opportunities.  */
   14058   irelend = internal_relocs + sec->reloc_count;
   14059   for (irel = internal_relocs; irel < irelend; irel++)
   14060     {
   14061       unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
   14062       unsigned int r_type = ELF32_R_TYPE (irel->r_info);
   14063       bool target_is_micromips_code_p;
   14064       unsigned long opcode;
   14065       bfd_vma symval;
   14066       bfd_vma pcrval;
   14067       bfd_byte *ptr;
   14068       int fndopc;
   14069 
   14070       /* The number of bytes to delete for relaxation and from where
   14071 	 to delete these bytes starting at irel->r_offset.  */
   14072       int delcnt = 0;
   14073       int deloff = 0;
   14074 
   14075       /* If this isn't something that can be relaxed, then ignore
   14076 	 this reloc.  */
   14077       if (r_type != R_MICROMIPS_HI16
   14078 	  && r_type != R_MICROMIPS_PC16_S1
   14079 	  && r_type != R_MICROMIPS_26_S1)
   14080 	continue;
   14081 
   14082       /* Get the section contents if we haven't done so already.  */
   14083       if (contents == NULL)
   14084 	{
   14085 	  /* Get cached copy if it exists.  */
   14086 	  if (elf_section_data (sec)->this_hdr.contents != NULL)
   14087 	    contents = elf_section_data (sec)->this_hdr.contents;
   14088 	  /* Go get them off disk.  */
   14089 	  else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
   14090 	    goto error_return;
   14091 	}
   14092       ptr = contents + irel->r_offset;
   14093 
   14094       /* Read this BFD's local symbols if we haven't done so already.  */
   14095       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
   14096 	{
   14097 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   14098 	  if (isymbuf == NULL)
   14099 	    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   14100 					    symtab_hdr->sh_info, 0,
   14101 					    NULL, NULL, NULL);
   14102 	  if (isymbuf == NULL)
   14103 	    goto error_return;
   14104 	}
   14105 
   14106       /* Get the value of the symbol referred to by the reloc.  */
   14107       if (r_symndx < symtab_hdr->sh_info)
   14108 	{
   14109 	  /* A local symbol.  */
   14110 	  Elf_Internal_Sym *isym;
   14111 	  asection *sym_sec;
   14112 
   14113 	  isym = isymbuf + r_symndx;
   14114 	  if (isym->st_shndx == SHN_UNDEF)
   14115 	    sym_sec = bfd_und_section_ptr;
   14116 	  else if (isym->st_shndx == SHN_ABS)
   14117 	    sym_sec = bfd_abs_section_ptr;
   14118 	  else if (isym->st_shndx == SHN_COMMON)
   14119 	    sym_sec = bfd_com_section_ptr;
   14120 	  else
   14121 	    sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   14122 	  symval = (isym->st_value
   14123 		    + sym_sec->output_section->vma
   14124 		    + sym_sec->output_offset);
   14125 	  target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
   14126 	}
   14127       else
   14128 	{
   14129 	  unsigned long indx;
   14130 	  struct elf_link_hash_entry *h;
   14131 
   14132 	  /* An external symbol.  */
   14133 	  indx = r_symndx - symtab_hdr->sh_info;
   14134 	  h = elf_sym_hashes (abfd)[indx];
   14135 	  BFD_ASSERT (h != NULL);
   14136 
   14137 	  if (h->root.type != bfd_link_hash_defined
   14138 	      && h->root.type != bfd_link_hash_defweak)
   14139 	    /* This appears to be a reference to an undefined
   14140 	       symbol.  Just ignore it -- it will be caught by the
   14141 	       regular reloc processing.  */
   14142 	    continue;
   14143 
   14144 	  symval = (h->root.u.def.value
   14145 		    + h->root.u.def.section->output_section->vma
   14146 		    + h->root.u.def.section->output_offset);
   14147 	  target_is_micromips_code_p = (!h->needs_plt
   14148 					&& ELF_ST_IS_MICROMIPS (h->other));
   14149 	}
   14150 
   14151 
   14152       /* For simplicity of coding, we are going to modify the
   14153 	 section contents, the section relocs, and the BFD symbol
   14154 	 table.  We must tell the rest of the code not to free up this
   14155 	 information.  It would be possible to instead create a table
   14156 	 of changes which have to be made, as is done in coff-mips.c;
   14157 	 that would be more work, but would require less memory when
   14158 	 the linker is run.  */
   14159 
   14160       /* Only 32-bit instructions relaxed.  */
   14161       if (irel->r_offset + 4 > sec->size)
   14162 	continue;
   14163 
   14164       opcode = bfd_get_micromips_32 (abfd, ptr);
   14165 
   14166       /* This is the pc-relative distance from the instruction the
   14167 	 relocation is applied to, to the symbol referred.  */
   14168       pcrval = (symval
   14169 		- (sec->output_section->vma + sec->output_offset)
   14170 		- irel->r_offset);
   14171 
   14172       /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
   14173 	 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
   14174 	 R_MICROMIPS_PC23_S2.  The R_MICROMIPS_PC23_S2 condition is
   14175 
   14176 	   (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
   14177 
   14178 	 where pcrval has first to be adjusted to apply against the LO16
   14179 	 location (we make the adjustment later on, when we have figured
   14180 	 out the offset).  */
   14181       if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
   14182 	{
   14183 	  bool bzc = false;
   14184 	  unsigned long nextopc;
   14185 	  unsigned long reg;
   14186 	  bfd_vma offset;
   14187 
   14188 	  /* Give up if the previous reloc was a HI16 against this symbol
   14189 	     too.  */
   14190 	  if (irel > internal_relocs
   14191 	      && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
   14192 	      && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
   14193 	    continue;
   14194 
   14195 	  /* Or if the next reloc is not a LO16 against this symbol.  */
   14196 	  if (irel + 1 >= irelend
   14197 	      || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
   14198 	      || ELF32_R_SYM (irel[1].r_info) != r_symndx)
   14199 	    continue;
   14200 
   14201 	  /* Or if the second next reloc is a LO16 against this symbol too.  */
   14202 	  if (irel + 2 >= irelend
   14203 	      && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
   14204 	      && ELF32_R_SYM (irel[2].r_info) == r_symndx)
   14205 	    continue;
   14206 
   14207 	  /* See if the LUI instruction *might* be in a branch delay slot.
   14208 	     We check whether what looks like a 16-bit branch or jump is
   14209 	     actually an immediate argument to a compact branch, and let
   14210 	     it through if so.  */
   14211 	  if (irel->r_offset >= 2
   14212 	      && check_br16_dslot (abfd, ptr - 2)
   14213 	      && !(irel->r_offset >= 4
   14214 		   && (bzc = check_relocated_bzc (abfd,
   14215 						  ptr - 4, irel->r_offset - 4,
   14216 						  internal_relocs, irelend))))
   14217 	    continue;
   14218 	  if (irel->r_offset >= 4
   14219 	      && !bzc
   14220 	      && check_br32_dslot (abfd, ptr - 4))
   14221 	    continue;
   14222 
   14223 	  reg = OP32_SREG (opcode);
   14224 
   14225 	  /* We only relax adjacent instructions or ones separated with
   14226 	     a branch or jump that has a delay slot.  The branch or jump
   14227 	     must not fiddle with the register used to hold the address.
   14228 	     Subtract 4 for the LUI itself.  */
   14229 	  offset = irel[1].r_offset - irel[0].r_offset;
   14230 	  switch (offset - 4)
   14231 	    {
   14232 	    case 0:
   14233 	      break;
   14234 	    case 2:
   14235 	      if (check_br16 (abfd, ptr + 4, reg))
   14236 		break;
   14237 	      continue;
   14238 	    case 4:
   14239 	      if (check_br32 (abfd, ptr + 4, reg))
   14240 		break;
   14241 	      continue;
   14242 	    default:
   14243 	      continue;
   14244 	    }
   14245 
   14246 	  nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
   14247 
   14248 	  /* Give up unless the same register is used with both
   14249 	     relocations.  */
   14250 	  if (OP32_SREG (nextopc) != reg)
   14251 	    continue;
   14252 
   14253 	  /* Now adjust pcrval, subtracting the offset to the LO16 reloc
   14254 	     and rounding up to take masking of the two LSBs into account.  */
   14255 	  pcrval = ((pcrval - offset + 3) | 3) ^ 3;
   14256 
   14257 	  /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16.  */
   14258 	  if (IS_BITSIZE (symval, 16))
   14259 	    {
   14260 	      /* Fix the relocation's type.  */
   14261 	      irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
   14262 
   14263 	      /* Instructions using R_MICROMIPS_LO16 have the base or
   14264 		 source register in bits 20:16.  This register becomes $0
   14265 		 (zero) as the result of the R_MICROMIPS_HI16 being 0.  */
   14266 	      nextopc &= ~0x001f0000;
   14267 	      bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
   14268 			  contents + irel[1].r_offset);
   14269 	    }
   14270 
   14271 	  /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
   14272 	     We add 4 to take LUI deletion into account while checking
   14273 	     the PC-relative distance.  */
   14274 	  else if (symval % 4 == 0
   14275 		   && IS_BITSIZE (pcrval + 4, 25)
   14276 		   && MATCH (nextopc, addiu_insn)
   14277 		   && OP32_TREG (nextopc) == OP32_SREG (nextopc)
   14278 		   && OP16_VALID_REG (OP32_TREG (nextopc)))
   14279 	    {
   14280 	      /* Fix the relocation's type.  */
   14281 	      irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
   14282 
   14283 	      /* Replace ADDIU with the ADDIUPC version.  */
   14284 	      nextopc = (addiupc_insn.match
   14285 			 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
   14286 
   14287 	      bfd_put_micromips_32 (abfd, nextopc,
   14288 				    contents + irel[1].r_offset);
   14289 	    }
   14290 
   14291 	  /* Can't do anything, give up, sigh...  */
   14292 	  else
   14293 	    continue;
   14294 
   14295 	  /* Fix the relocation's type.  */
   14296 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
   14297 
   14298 	  /* Delete the LUI instruction: 4 bytes at irel->r_offset.  */
   14299 	  delcnt = 4;
   14300 	  deloff = 0;
   14301 	}
   14302 
   14303       /* Compact branch relaxation -- due to the multitude of macros
   14304 	 employed by the compiler/assembler, compact branches are not
   14305 	 always generated.  Obviously, this can/will be fixed elsewhere,
   14306 	 but there is no drawback in double checking it here.  */
   14307       else if (r_type == R_MICROMIPS_PC16_S1
   14308 	       && irel->r_offset + 5 < sec->size
   14309 	       && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
   14310 		   || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
   14311 	       && ((!insn32
   14312 		    && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
   14313 					nop_insn_16) ? 2 : 0))
   14314 		   || (irel->r_offset + 7 < sec->size
   14315 		       && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
   14316 								 ptr + 4),
   14317 					   nop_insn_32) ? 4 : 0))))
   14318 	{
   14319 	  unsigned long reg;
   14320 
   14321 	  reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
   14322 
   14323 	  /* Replace BEQZ/BNEZ with the compact version.  */
   14324 	  opcode = (bzc_insns_32[fndopc].match
   14325 		    | BZC32_REG_FIELD (reg)
   14326 		    | (opcode & 0xffff));		/* Addend value.  */
   14327 
   14328 	  bfd_put_micromips_32 (abfd, opcode, ptr);
   14329 
   14330 	  /* Delete the delay slot NOP: two or four bytes from
   14331 	     irel->offset + 4; delcnt has already been set above.  */
   14332 	  deloff = 4;
   14333 	}
   14334 
   14335       /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1.  We need
   14336 	 to check the distance from the next instruction, so subtract 2.  */
   14337       else if (!insn32
   14338 	       && r_type == R_MICROMIPS_PC16_S1
   14339 	       && IS_BITSIZE (pcrval - 2, 11)
   14340 	       && find_match (opcode, b_insns_32) >= 0)
   14341 	{
   14342 	  /* Fix the relocation's type.  */
   14343 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
   14344 
   14345 	  /* Replace the 32-bit opcode with a 16-bit opcode.  */
   14346 	  bfd_put_16 (abfd,
   14347 		      (b_insn_16.match
   14348 		       | (opcode & 0x3ff)),		/* Addend value.  */
   14349 		      ptr);
   14350 
   14351 	  /* Delete 2 bytes from irel->r_offset + 2.  */
   14352 	  delcnt = 2;
   14353 	  deloff = 2;
   14354 	}
   14355 
   14356       /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1.  We need
   14357 	 to check the distance from the next instruction, so subtract 2.  */
   14358       else if (!insn32
   14359 	       && r_type == R_MICROMIPS_PC16_S1
   14360 	       && IS_BITSIZE (pcrval - 2, 8)
   14361 	       && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
   14362 		    && OP16_VALID_REG (OP32_SREG (opcode)))
   14363 		   || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
   14364 		       && OP16_VALID_REG (OP32_TREG (opcode)))))
   14365 	{
   14366 	  unsigned long reg;
   14367 
   14368 	  reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
   14369 
   14370 	  /* Fix the relocation's type.  */
   14371 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
   14372 
   14373 	  /* Replace the 32-bit opcode with a 16-bit opcode.  */
   14374 	  bfd_put_16 (abfd,
   14375 		      (bz_insns_16[fndopc].match
   14376 		       | BZ16_REG_FIELD (reg)
   14377 		       | (opcode & 0x7f)),		/* Addend value.  */
   14378 		      ptr);
   14379 
   14380 	  /* Delete 2 bytes from irel->r_offset + 2.  */
   14381 	  delcnt = 2;
   14382 	  deloff = 2;
   14383 	}
   14384 
   14385       /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets.  */
   14386       else if (!insn32
   14387 	       && r_type == R_MICROMIPS_26_S1
   14388 	       && target_is_micromips_code_p
   14389 	       && irel->r_offset + 7 < sec->size
   14390 	       && MATCH (opcode, jal_insn_32_bd32))
   14391 	{
   14392 	  unsigned long n32opc;
   14393 	  bool relaxed = false;
   14394 
   14395 	  n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
   14396 
   14397 	  if (MATCH (n32opc, nop_insn_32))
   14398 	    {
   14399 	      /* Replace delay slot 32-bit NOP with a 16-bit NOP.  */
   14400 	      bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
   14401 
   14402 	      relaxed = true;
   14403 	    }
   14404 	  else if (find_match (n32opc, move_insns_32) >= 0)
   14405 	    {
   14406 	      /* Replace delay slot 32-bit MOVE with 16-bit MOVE.  */
   14407 	      bfd_put_16 (abfd,
   14408 			  (move_insn_16.match
   14409 			   | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
   14410 			   | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
   14411 			  ptr + 4);
   14412 
   14413 	      relaxed = true;
   14414 	    }
   14415 	  /* Other 32-bit instructions relaxable to 16-bit
   14416 	     instructions will be handled here later.  */
   14417 
   14418 	  if (relaxed)
   14419 	    {
   14420 	      /* JAL with 32-bit delay slot that is changed to a JALS
   14421 		 with 16-bit delay slot.  */
   14422 	      bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
   14423 
   14424 	      /* Delete 2 bytes from irel->r_offset + 6.  */
   14425 	      delcnt = 2;
   14426 	      deloff = 6;
   14427 	    }
   14428 	}
   14429 
   14430       if (delcnt != 0)
   14431 	{
   14432 	  /* Note that we've changed the relocs, section contents, etc.  */
   14433 	  elf_section_data (sec)->relocs = internal_relocs;
   14434 	  elf_section_data (sec)->this_hdr.contents = contents;
   14435 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   14436 
   14437 	  /* Delete bytes depending on the delcnt and deloff.  */
   14438 	  if (!mips_elf_relax_delete_bytes (abfd, sec,
   14439 					    irel->r_offset + deloff, delcnt))
   14440 	    goto error_return;
   14441 
   14442 	  /* That will change things, so we should relax again.
   14443 	     Note that this is not required, and it may be slow.  */
   14444 	  *again = true;
   14445 	}
   14446     }
   14447 
   14448   if (isymbuf != NULL
   14449       && symtab_hdr->contents != (unsigned char *) isymbuf)
   14450     {
   14451       if (! link_info->keep_memory)
   14452 	free (isymbuf);
   14453       else
   14454 	{
   14455 	  /* Cache the symbols for elf_link_input_bfd.  */
   14456 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   14457 	}
   14458     }
   14459 
   14460   if (contents != NULL
   14461       && elf_section_data (sec)->this_hdr.contents != contents)
   14462     {
   14463       if (! link_info->keep_memory)
   14464 	free (contents);
   14465       else
   14466 	{
   14467 	  /* Cache the section contents for elf_link_input_bfd.  */
   14468 	  elf_section_data (sec)->this_hdr.contents = contents;
   14469 	}
   14470     }
   14471 
   14472   if (elf_section_data (sec)->relocs != internal_relocs)
   14473     free (internal_relocs);
   14474 
   14475   return true;
   14476 
   14477  error_return:
   14478   if (symtab_hdr->contents != (unsigned char *) isymbuf)
   14479     free (isymbuf);
   14480   if (elf_section_data (sec)->this_hdr.contents != contents)
   14481     free (contents);
   14482   if (elf_section_data (sec)->relocs != internal_relocs)
   14483     free (internal_relocs);
   14484 
   14485   return false;
   14486 }
   14487 
   14488 /* Create a MIPS ELF linker hash table.  */
   14490 
   14491 struct bfd_link_hash_table *
   14492 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
   14493 {
   14494   struct mips_elf_link_hash_table *ret;
   14495   size_t amt = sizeof (struct mips_elf_link_hash_table);
   14496 
   14497   ret = bfd_zmalloc (amt);
   14498   if (ret == NULL)
   14499     return NULL;
   14500 
   14501   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
   14502 				      mips_elf_link_hash_newfunc,
   14503 				      sizeof (struct mips_elf_link_hash_entry),
   14504 				      MIPS_ELF_DATA))
   14505     {
   14506       free (ret);
   14507       return NULL;
   14508     }
   14509   ret->root.init_plt_refcount.plist = NULL;
   14510   ret->root.init_plt_offset.plist = NULL;
   14511 
   14512   return &ret->root.root;
   14513 }
   14514 
   14515 /* Likewise, but indicate that the target is VxWorks.  */
   14516 
   14517 struct bfd_link_hash_table *
   14518 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
   14519 {
   14520   struct bfd_link_hash_table *ret;
   14521 
   14522   ret = _bfd_mips_elf_link_hash_table_create (abfd);
   14523   if (ret)
   14524     {
   14525       struct mips_elf_link_hash_table *htab;
   14526 
   14527       htab = (struct mips_elf_link_hash_table *) ret;
   14528       htab->use_plts_and_copy_relocs = true;
   14529     }
   14530   return ret;
   14531 }
   14532 
   14533 /* A function that the linker calls if we are allowed to use PLTs
   14534    and copy relocs.  */
   14535 
   14536 void
   14537 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
   14538 {
   14539   mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
   14540 }
   14541 
   14542 /* A function that the linker calls to select between all or only
   14543    32-bit microMIPS instructions, and between making or ignoring
   14544    branch relocation checks for invalid transitions between ISA modes.
   14545    Also record whether we have been configured for a GNU target.  */
   14546 
   14547 void
   14548 _bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
   14549 			    bool ignore_branch_isa,
   14550 			    bool gnu_target)
   14551 {
   14552   mips_elf_hash_table (info)->insn32 = insn32;
   14553   mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
   14554   mips_elf_hash_table (info)->gnu_target = gnu_target;
   14555 }
   14556 
   14557 /* A function that the linker calls to enable use of compact branches in
   14558    linker generated code for MIPSR6.  */
   14559 
   14560 void
   14561 _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
   14562 {
   14563   mips_elf_hash_table (info)->compact_branches = on;
   14564 }
   14565 
   14566 
   14567 /* Structure for saying that BFD machine EXTENSION extends BASE.  */
   14569 
   14570 struct mips_mach_extension
   14571 {
   14572   unsigned long extension, base;
   14573 };
   14574 
   14575 /* An array that maps 64-bit architectures to the corresponding 32-bit
   14576    architectures.  */
   14577 static const struct mips_mach_extension mips_mach_32_64[] =
   14578 {
   14579   { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
   14580   { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
   14581   { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
   14582   { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
   14583   { bfd_mach_mipsisa64,   bfd_mach_mipsisa32 }
   14584 };
   14585 
   14586 /* An array describing how BFD machines relate to one another.  The entries
   14587    are ordered topologically with MIPS I extensions listed last.  */
   14588 
   14589 static const struct mips_mach_extension mips_mach_extensions[] =
   14590 {
   14591   /* MIPS64r2 extensions.  */
   14592   { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
   14593   { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
   14594   { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
   14595   { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
   14596   { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
   14597   { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
   14598   { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
   14599 
   14600   /* MIPS64 extensions.  */
   14601   { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
   14602   { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
   14603   { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
   14604 
   14605   /* MIPS V extensions.  */
   14606   { bfd_mach_mipsisa64, bfd_mach_mips5 },
   14607 
   14608   /* R10000 extensions.  */
   14609   { bfd_mach_mips12000, bfd_mach_mips10000 },
   14610   { bfd_mach_mips14000, bfd_mach_mips10000 },
   14611   { bfd_mach_mips16000, bfd_mach_mips10000 },
   14612 
   14613   /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
   14614      vr5400 ISA, but doesn't include the multimedia stuff.  It seems
   14615      better to allow vr5400 and vr5500 code to be merged anyway, since
   14616      many libraries will just use the core ISA.  Perhaps we could add
   14617      some sort of ASE flag if this ever proves a problem.  */
   14618   { bfd_mach_mips5500, bfd_mach_mips5400 },
   14619   { bfd_mach_mips5400, bfd_mach_mips5000 },
   14620 
   14621   /* MIPS IV extensions.  */
   14622   { bfd_mach_mips5, bfd_mach_mips8000 },
   14623   { bfd_mach_mips10000, bfd_mach_mips8000 },
   14624   { bfd_mach_mips5000, bfd_mach_mips8000 },
   14625   { bfd_mach_mips7000, bfd_mach_mips8000 },
   14626   { bfd_mach_mips9000, bfd_mach_mips8000 },
   14627 
   14628   /* VR4100 extensions.  */
   14629   { bfd_mach_mips4120, bfd_mach_mips4100 },
   14630   { bfd_mach_mips4111, bfd_mach_mips4100 },
   14631 
   14632   /* MIPS III extensions.  */
   14633   { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
   14634   { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
   14635   { bfd_mach_mips8000, bfd_mach_mips4000 },
   14636   { bfd_mach_mips4650, bfd_mach_mips4000 },
   14637   { bfd_mach_mips4600, bfd_mach_mips4000 },
   14638   { bfd_mach_mips4400, bfd_mach_mips4000 },
   14639   { bfd_mach_mips4300, bfd_mach_mips4000 },
   14640   { bfd_mach_mips4100, bfd_mach_mips4000 },
   14641   { bfd_mach_mips5900, bfd_mach_mips4000 },
   14642 
   14643   /* MIPS32r3 extensions.  */
   14644   { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
   14645 
   14646   /* MIPS32r2 extensions.  */
   14647   { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
   14648 
   14649   /* MIPS32 extensions.  */
   14650   { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
   14651 
   14652   /* MIPS II extensions.  */
   14653   { bfd_mach_mips4000, bfd_mach_mips6000 },
   14654   { bfd_mach_mipsisa32, bfd_mach_mips6000 },
   14655   { bfd_mach_mips4010, bfd_mach_mips6000 },
   14656   { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
   14657 
   14658   /* MIPS I extensions.  */
   14659   { bfd_mach_mips6000, bfd_mach_mips3000 },
   14660   { bfd_mach_mips3900, bfd_mach_mips3000 }
   14661 };
   14662 
   14663 /* Return true if bfd machine EXTENSION is the same as BASE, or if
   14664    EXTENSION is the 64-bit equivalent of a 32-bit BASE.  */
   14665 
   14666 static bool
   14667 mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
   14668 {
   14669   size_t i;
   14670 
   14671   if (extension == base)
   14672     return true;
   14673 
   14674   for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
   14675     if (extension == mips_mach_32_64[i].extension)
   14676       return base == mips_mach_32_64[i].base;
   14677 
   14678   return false;
   14679 }
   14680 
   14681 static bool
   14682 mips_mach_extends_p (unsigned long base, unsigned long extension)
   14683 {
   14684   size_t i;
   14685 
   14686   if (mips_mach_extends_32_64 (base, extension))
   14687     return true;
   14688 
   14689   for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
   14690     if (extension == mips_mach_extensions[i].extension)
   14691       {
   14692 	extension = mips_mach_extensions[i].base;
   14693 	if (mips_mach_extends_32_64 (base, extension))
   14694 	  return true;
   14695       }
   14696 
   14697   return false;
   14698 }
   14699 
   14700 /* Return the BFD mach for each .MIPS.abiflags ISA Extension.  */
   14701 
   14702 static unsigned long
   14703 bfd_mips_isa_ext_mach (unsigned int isa_ext)
   14704 {
   14705   switch (isa_ext)
   14706     {
   14707     case AFL_EXT_3900:	      return bfd_mach_mips3900;
   14708     case AFL_EXT_4010:	      return bfd_mach_mips4010;
   14709     case AFL_EXT_4100:	      return bfd_mach_mips4100;
   14710     case AFL_EXT_4111:	      return bfd_mach_mips4111;
   14711     case AFL_EXT_4120:	      return bfd_mach_mips4120;
   14712     case AFL_EXT_4650:	      return bfd_mach_mips4650;
   14713     case AFL_EXT_5400:	      return bfd_mach_mips5400;
   14714     case AFL_EXT_5500:	      return bfd_mach_mips5500;
   14715     case AFL_EXT_5900:	      return bfd_mach_mips5900;
   14716     case AFL_EXT_10000:	      return bfd_mach_mips10000;
   14717     case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
   14718     case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
   14719     case AFL_EXT_SB1:	      return bfd_mach_mips_sb1;
   14720     case AFL_EXT_OCTEON:      return bfd_mach_mips_octeon;
   14721     case AFL_EXT_OCTEONP:     return bfd_mach_mips_octeonp;
   14722     case AFL_EXT_OCTEON2:     return bfd_mach_mips_octeon2;
   14723     case AFL_EXT_XLR:	      return bfd_mach_mips_xlr;
   14724     default:		      return bfd_mach_mips3000;
   14725     }
   14726 }
   14727 
   14728 /* Return the .MIPS.abiflags value representing each ISA Extension.  */
   14729 
   14730 unsigned int
   14731 bfd_mips_isa_ext (bfd *abfd)
   14732 {
   14733   switch (bfd_get_mach (abfd))
   14734     {
   14735     case bfd_mach_mips3900:	    return AFL_EXT_3900;
   14736     case bfd_mach_mips4010:	    return AFL_EXT_4010;
   14737     case bfd_mach_mips4100:	    return AFL_EXT_4100;
   14738     case bfd_mach_mips4111:	    return AFL_EXT_4111;
   14739     case bfd_mach_mips4120:	    return AFL_EXT_4120;
   14740     case bfd_mach_mips4650:	    return AFL_EXT_4650;
   14741     case bfd_mach_mips5400:	    return AFL_EXT_5400;
   14742     case bfd_mach_mips5500:	    return AFL_EXT_5500;
   14743     case bfd_mach_mips5900:	    return AFL_EXT_5900;
   14744     case bfd_mach_mips10000:	    return AFL_EXT_10000;
   14745     case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
   14746     case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
   14747     case bfd_mach_mips_sb1:	    return AFL_EXT_SB1;
   14748     case bfd_mach_mips_octeon:	    return AFL_EXT_OCTEON;
   14749     case bfd_mach_mips_octeonp:	    return AFL_EXT_OCTEONP;
   14750     case bfd_mach_mips_octeon3:	    return AFL_EXT_OCTEON3;
   14751     case bfd_mach_mips_octeon2:	    return AFL_EXT_OCTEON2;
   14752     case bfd_mach_mips_xlr:	    return AFL_EXT_XLR;
   14753     case bfd_mach_mips_interaptiv_mr2:
   14754       return AFL_EXT_INTERAPTIV_MR2;
   14755     default:			    return 0;
   14756     }
   14757 }
   14758 
   14759 /* Encode ISA level and revision as a single value.  */
   14760 #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
   14761 
   14762 /* Decode a single value into level and revision.  */
   14763 #define ISA_LEVEL(LEVREV)  ((LEVREV) >> 3)
   14764 #define ISA_REV(LEVREV)    ((LEVREV) & 0x7)
   14765 
   14766 /* Update the isa_level, isa_rev, isa_ext fields of abiflags.  */
   14767 
   14768 static void
   14769 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
   14770 {
   14771   int new_isa = 0;
   14772   switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
   14773     {
   14774     case EF_MIPS_ARCH_1:    new_isa = LEVEL_REV (1, 0); break;
   14775     case EF_MIPS_ARCH_2:    new_isa = LEVEL_REV (2, 0); break;
   14776     case EF_MIPS_ARCH_3:    new_isa = LEVEL_REV (3, 0); break;
   14777     case EF_MIPS_ARCH_4:    new_isa = LEVEL_REV (4, 0); break;
   14778     case EF_MIPS_ARCH_5:    new_isa = LEVEL_REV (5, 0); break;
   14779     case EF_MIPS_ARCH_32:   new_isa = LEVEL_REV (32, 1); break;
   14780     case EF_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
   14781     case EF_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
   14782     case EF_MIPS_ARCH_64:   new_isa = LEVEL_REV (64, 1); break;
   14783     case EF_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
   14784     case EF_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
   14785     default:
   14786       _bfd_error_handler
   14787 	/* xgettext:c-format */
   14788 	(_("%pB: unknown architecture %s"),
   14789 	 abfd, bfd_printable_name (abfd));
   14790     }
   14791 
   14792   if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
   14793     {
   14794       abiflags->isa_level = ISA_LEVEL (new_isa);
   14795       abiflags->isa_rev = ISA_REV (new_isa);
   14796     }
   14797 
   14798   /* Update the isa_ext if ABFD describes a further extension.  */
   14799   if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
   14800 			   bfd_get_mach (abfd)))
   14801     abiflags->isa_ext = bfd_mips_isa_ext (abfd);
   14802 }
   14803 
   14804 /* Return true if the given ELF header flags describe a 32-bit binary.  */
   14805 
   14806 static bool
   14807 mips_32bit_flags_p (flagword flags)
   14808 {
   14809   return ((flags & EF_MIPS_32BITMODE) != 0
   14810 	  || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32
   14811 	  || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32
   14812 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1
   14813 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2
   14814 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32
   14815 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2
   14816 	  || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6);
   14817 }
   14818 
   14819 /* Infer the content of the ABI flags based on the elf header.  */
   14820 
   14821 static void
   14822 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
   14823 {
   14824   obj_attribute *in_attr;
   14825 
   14826   memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
   14827   update_mips_abiflags_isa (abfd, abiflags);
   14828 
   14829   if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
   14830     abiflags->gpr_size = AFL_REG_32;
   14831   else
   14832     abiflags->gpr_size = AFL_REG_64;
   14833 
   14834   abiflags->cpr1_size = AFL_REG_NONE;
   14835 
   14836   in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
   14837   abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   14838 
   14839   if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
   14840       || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
   14841       || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
   14842 	  && abiflags->gpr_size == AFL_REG_32))
   14843     abiflags->cpr1_size = AFL_REG_32;
   14844   else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
   14845 	   || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
   14846 	   || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
   14847     abiflags->cpr1_size = AFL_REG_64;
   14848 
   14849   abiflags->cpr2_size = AFL_REG_NONE;
   14850 
   14851   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
   14852     abiflags->ases |= AFL_ASE_MDMX;
   14853   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
   14854     abiflags->ases |= AFL_ASE_MIPS16;
   14855   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
   14856     abiflags->ases |= AFL_ASE_MICROMIPS;
   14857 
   14858   if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
   14859       && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
   14860       && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
   14861       && abiflags->isa_level >= 32
   14862       && abiflags->ases != AFL_ASE_LOONGSON_EXT)
   14863     abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
   14864 }
   14865 
   14866 /* We need to use a special link routine to handle the .reginfo and
   14867    the .mdebug sections.  We need to merge all instances of these
   14868    sections together, not write them all out sequentially.  */
   14869 
   14870 bool
   14871 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   14872 {
   14873   asection *o;
   14874   struct bfd_link_order *p;
   14875   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
   14876   asection *rtproc_sec, *abiflags_sec;
   14877   Elf32_RegInfo reginfo;
   14878   struct ecoff_debug_info debug;
   14879   struct mips_htab_traverse_info hti;
   14880   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14881   const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
   14882   HDRR *symhdr = &debug.symbolic_header;
   14883   void *mdebug_handle = NULL;
   14884   asection *s;
   14885   EXTR esym;
   14886   unsigned int i;
   14887   bfd_size_type amt;
   14888   struct mips_elf_link_hash_table *htab;
   14889 
   14890   static const char * const secname[] =
   14891   {
   14892     ".text", ".init", ".fini", ".data",
   14893     ".rodata", ".sdata", ".sbss", ".bss"
   14894   };
   14895   static const int sc[] =
   14896   {
   14897     scText, scInit, scFini, scData,
   14898     scRData, scSData, scSBss, scBss
   14899   };
   14900 
   14901   htab = mips_elf_hash_table (info);
   14902   BFD_ASSERT (htab != NULL);
   14903 
   14904   /* Sort the dynamic symbols so that those with GOT entries come after
   14905      those without.  */
   14906   if (!mips_elf_sort_hash_table (abfd, info))
   14907     return false;
   14908 
   14909   /* Create any scheduled LA25 stubs.  */
   14910   hti.info = info;
   14911   hti.output_bfd = abfd;
   14912   hti.error = false;
   14913   htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
   14914   if (hti.error)
   14915     return false;
   14916 
   14917   /* Get a value for the GP register.  */
   14918   if (elf_gp (abfd) == 0)
   14919     {
   14920       struct bfd_link_hash_entry *h;
   14921 
   14922       h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
   14923       if (h != NULL && h->type == bfd_link_hash_defined)
   14924 	elf_gp (abfd) = (h->u.def.value
   14925 			 + h->u.def.section->output_section->vma
   14926 			 + h->u.def.section->output_offset);
   14927       else if (htab->root.target_os == is_vxworks
   14928 	       && (h = bfd_link_hash_lookup (info->hash,
   14929 					     "_GLOBAL_OFFSET_TABLE_",
   14930 					     false, false, true))
   14931 	       && h->type == bfd_link_hash_defined)
   14932 	elf_gp (abfd) = (h->u.def.section->output_section->vma
   14933 			 + h->u.def.section->output_offset
   14934 			 + h->u.def.value);
   14935       else if (bfd_link_relocatable (info))
   14936 	{
   14937 	  bfd_vma lo = MINUS_ONE;
   14938 
   14939 	  /* Find the GP-relative section with the lowest offset.  */
   14940 	  for (o = abfd->sections; o != NULL; o = o->next)
   14941 	    if (o->vma < lo
   14942 		&& (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
   14943 	      lo = o->vma;
   14944 
   14945 	  /* And calculate GP relative to that.  */
   14946 	  elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
   14947 	}
   14948       else
   14949 	{
   14950 	  /* If the relocate_section function needs to do a reloc
   14951 	     involving the GP value, it should make a reloc_dangerous
   14952 	     callback to warn that GP is not defined.  */
   14953 	}
   14954     }
   14955 
   14956   /* Go through the sections and collect the .reginfo and .mdebug
   14957      information.  */
   14958   abiflags_sec = NULL;
   14959   reginfo_sec = NULL;
   14960   mdebug_sec = NULL;
   14961   gptab_data_sec = NULL;
   14962   gptab_bss_sec = NULL;
   14963   for (o = abfd->sections; o != NULL; o = o->next)
   14964     {
   14965       if (strcmp (o->name, ".MIPS.abiflags") == 0)
   14966 	{
   14967 	  /* We have found the .MIPS.abiflags section in the output file.
   14968 	     Look through all the link_orders comprising it and remove them.
   14969 	     The data is merged in _bfd_mips_elf_merge_private_bfd_data.  */
   14970 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   14971 	    {
   14972 	      asection *input_section;
   14973 
   14974 	      if (p->type != bfd_indirect_link_order)
   14975 		{
   14976 		  if (p->type == bfd_data_link_order)
   14977 		    continue;
   14978 		  abort ();
   14979 		}
   14980 
   14981 	      input_section = p->u.indirect.section;
   14982 
   14983 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   14984 		 elf_link_input_bfd ignores this section.  */
   14985 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   14986 	    }
   14987 
   14988 	  /* Size has been set in _bfd_mips_elf_late_size_sections.  */
   14989 	  BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
   14990 
   14991 	  /* Skip this section later on (I don't think this currently
   14992 	     matters, but someday it might).  */
   14993 	  o->map_head.link_order = NULL;
   14994 
   14995 	  abiflags_sec = o;
   14996 	}
   14997 
   14998       if (strcmp (o->name, ".reginfo") == 0)
   14999 	{
   15000 	  memset (&reginfo, 0, sizeof reginfo);
   15001 
   15002 	  /* We have found the .reginfo section in the output file.
   15003 	     Look through all the link_orders comprising it and merge
   15004 	     the information together.  */
   15005 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   15006 	    {
   15007 	      asection *input_section;
   15008 	      bfd *input_bfd;
   15009 	      Elf32_External_RegInfo ext;
   15010 	      Elf32_RegInfo sub;
   15011 	      bfd_size_type sz;
   15012 
   15013 	      if (p->type != bfd_indirect_link_order)
   15014 		{
   15015 		  if (p->type == bfd_data_link_order)
   15016 		    continue;
   15017 		  abort ();
   15018 		}
   15019 
   15020 	      input_section = p->u.indirect.section;
   15021 	      input_bfd = input_section->owner;
   15022 
   15023 	      sz = (input_section->size < sizeof (ext)
   15024 		    ? input_section->size : sizeof (ext));
   15025 	      memset (&ext, 0, sizeof (ext));
   15026 	      if (! bfd_get_section_contents (input_bfd, input_section,
   15027 					      &ext, 0, sz))
   15028 		return false;
   15029 
   15030 	      bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
   15031 
   15032 	      reginfo.ri_gprmask |= sub.ri_gprmask;
   15033 	      reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
   15034 	      reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
   15035 	      reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
   15036 	      reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
   15037 
   15038 	      /* ri_gp_value is set by the function
   15039 		 `_bfd_mips_elf_section_processing' when the section is
   15040 		 finally written out.  */
   15041 
   15042 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   15043 		 elf_link_input_bfd ignores this section.  */
   15044 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   15045 	    }
   15046 
   15047 	  /* Size has been set in _bfd_mips_elf_late_size_sections.  */
   15048 	  BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
   15049 
   15050 	  /* Skip this section later on (I don't think this currently
   15051 	     matters, but someday it might).  */
   15052 	  o->map_head.link_order = NULL;
   15053 
   15054 	  reginfo_sec = o;
   15055 	}
   15056 
   15057       if (strcmp (o->name, ".mdebug") == 0)
   15058 	{
   15059 	  struct extsym_info einfo;
   15060 	  bfd_vma last;
   15061 
   15062 	  /* We have found the .mdebug section in the output file.
   15063 	     Look through all the link_orders comprising it and merge
   15064 	     the information together.  */
   15065 	  symhdr->magic = swap->sym_magic;
   15066 	  /* FIXME: What should the version stamp be?  */
   15067 	  symhdr->vstamp = 0;
   15068 	  symhdr->ilineMax = 0;
   15069 	  symhdr->cbLine = 0;
   15070 	  symhdr->idnMax = 0;
   15071 	  symhdr->ipdMax = 0;
   15072 	  symhdr->isymMax = 0;
   15073 	  symhdr->ioptMax = 0;
   15074 	  symhdr->iauxMax = 0;
   15075 	  symhdr->issMax = 0;
   15076 	  symhdr->issExtMax = 0;
   15077 	  symhdr->ifdMax = 0;
   15078 	  symhdr->crfd = 0;
   15079 	  symhdr->iextMax = 0;
   15080 
   15081 	  /* We accumulate the debugging information itself in the
   15082 	     debug_info structure.  */
   15083 	  debug.alloc_syments = false;
   15084 	  debug.line = NULL;
   15085 	  debug.external_dnr = NULL;
   15086 	  debug.external_pdr = NULL;
   15087 	  debug.external_sym = NULL;
   15088 	  debug.external_opt = NULL;
   15089 	  debug.external_aux = NULL;
   15090 	  debug.ss = NULL;
   15091 	  debug.ssext = debug.ssext_end = NULL;
   15092 	  debug.external_fdr = NULL;
   15093 	  debug.external_rfd = NULL;
   15094 	  debug.external_ext = debug.external_ext_end = NULL;
   15095 
   15096 	  mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
   15097 	  if (mdebug_handle == NULL)
   15098 	    return false;
   15099 
   15100 	  esym.jmptbl = 0;
   15101 	  esym.cobol_main = 0;
   15102 	  esym.weakext = 0;
   15103 	  esym.reserved = 0;
   15104 	  esym.ifd = ifdNil;
   15105 	  esym.asym.iss = issNil;
   15106 	  esym.asym.st = stLocal;
   15107 	  esym.asym.reserved = 0;
   15108 	  esym.asym.index = indexNil;
   15109 	  last = 0;
   15110 	  for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
   15111 	    {
   15112 	      esym.asym.sc = sc[i];
   15113 	      s = bfd_get_section_by_name (abfd, secname[i]);
   15114 	      if (s != NULL)
   15115 		{
   15116 		  esym.asym.value = s->vma;
   15117 		  last = s->vma + s->size;
   15118 		}
   15119 	      else
   15120 		esym.asym.value = last;
   15121 	      if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
   15122 						 secname[i], &esym))
   15123 		return false;
   15124 	    }
   15125 
   15126 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   15127 	    {
   15128 	      asection *input_section;
   15129 	      bfd *input_bfd;
   15130 	      const struct ecoff_debug_swap *input_swap;
   15131 	      struct ecoff_debug_info input_debug;
   15132 	      char *eraw_src;
   15133 	      char *eraw_end;
   15134 
   15135 	      if (p->type != bfd_indirect_link_order)
   15136 		{
   15137 		  if (p->type == bfd_data_link_order)
   15138 		    continue;
   15139 		  abort ();
   15140 		}
   15141 
   15142 	      input_section = p->u.indirect.section;
   15143 	      input_bfd = input_section->owner;
   15144 
   15145 	      if (!is_mips_elf (input_bfd))
   15146 		{
   15147 		  /* I don't know what a non MIPS ELF bfd would be
   15148 		     doing with a .mdebug section, but I don't really
   15149 		     want to deal with it.  */
   15150 		  continue;
   15151 		}
   15152 
   15153 	      input_swap = (get_elf_backend_data (input_bfd)
   15154 			    ->elf_backend_ecoff_debug_swap);
   15155 
   15156 	      BFD_ASSERT (p->size == input_section->size);
   15157 
   15158 	      /* The ECOFF linking code expects that we have already
   15159 		 read in the debugging information and set up an
   15160 		 ecoff_debug_info structure, so we do that now.  */
   15161 	      if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
   15162 						   &input_debug))
   15163 		return false;
   15164 
   15165 	      if (! (bfd_ecoff_debug_accumulate
   15166 		     (mdebug_handle, abfd, &debug, swap, input_bfd,
   15167 		      &input_debug, input_swap, info)))
   15168 		{
   15169 		  _bfd_ecoff_free_ecoff_debug_info (&input_debug);
   15170 		  return false;
   15171 		}
   15172 
   15173 	      /* Loop through the external symbols.  For each one with
   15174 		 interesting information, try to find the symbol in
   15175 		 the linker global hash table and save the information
   15176 		 for the output external symbols.  */
   15177 	      eraw_src = input_debug.external_ext;
   15178 	      eraw_end = (eraw_src
   15179 			  + (input_debug.symbolic_header.iextMax
   15180 			     * input_swap->external_ext_size));
   15181 	      for (;
   15182 		   eraw_src < eraw_end;
   15183 		   eraw_src += input_swap->external_ext_size)
   15184 		{
   15185 		  EXTR ext;
   15186 		  const char *name;
   15187 		  struct mips_elf_link_hash_entry *h;
   15188 
   15189 		  (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
   15190 		  if (ext.asym.sc == scNil
   15191 		      || ext.asym.sc == scUndefined
   15192 		      || ext.asym.sc == scSUndefined)
   15193 		    continue;
   15194 
   15195 		  name = input_debug.ssext + ext.asym.iss;
   15196 		  h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
   15197 						 name, false, false, true);
   15198 		  if (h == NULL || h->esym.ifd != -2)
   15199 		    continue;
   15200 
   15201 		  if (ext.ifd != -1)
   15202 		    {
   15203 		      BFD_ASSERT (ext.ifd
   15204 				  < input_debug.symbolic_header.ifdMax);
   15205 		      ext.ifd = input_debug.ifdmap[ext.ifd];
   15206 		    }
   15207 
   15208 		  h->esym = ext;
   15209 		}
   15210 
   15211 	      /* Free up the information we just read.  */
   15212 	      _bfd_ecoff_free_ecoff_debug_info (&input_debug);
   15213 
   15214 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   15215 		 elf_link_input_bfd ignores this section.  */
   15216 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   15217 	    }
   15218 
   15219 	  if (SGI_COMPAT (abfd) && bfd_link_pic (info))
   15220 	    {
   15221 	      /* Create .rtproc section.  */
   15222 	      rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
   15223 	      if (rtproc_sec == NULL)
   15224 		{
   15225 		  flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
   15226 				    | SEC_LINKER_CREATED | SEC_READONLY);
   15227 
   15228 		  rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
   15229 								   ".rtproc",
   15230 								   flags);
   15231 		  if (rtproc_sec == NULL
   15232 		      || !bfd_set_section_alignment (rtproc_sec, 4))
   15233 		    return false;
   15234 		}
   15235 
   15236 	      if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
   15237 						     info, rtproc_sec,
   15238 						     &debug))
   15239 		return false;
   15240 	    }
   15241 
   15242 	  /* Build the external symbol information.  */
   15243 	  einfo.abfd = abfd;
   15244 	  einfo.info = info;
   15245 	  einfo.debug = &debug;
   15246 	  einfo.swap = swap;
   15247 	  einfo.failed = false;
   15248 	  mips_elf_link_hash_traverse (mips_elf_hash_table (info),
   15249 				       mips_elf_output_extsym, &einfo);
   15250 	  if (einfo.failed)
   15251 	    return false;
   15252 
   15253 	  /* Set the size of the .mdebug section.  */
   15254 	  o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
   15255 
   15256 	  /* Skip this section later on (I don't think this currently
   15257 	     matters, but someday it might).  */
   15258 	  o->map_head.link_order = NULL;
   15259 
   15260 	  mdebug_sec = o;
   15261 	}
   15262 
   15263       if (startswith (o->name, ".gptab."))
   15264 	{
   15265 	  const char *subname;
   15266 	  unsigned int c;
   15267 	  Elf32_gptab *tab;
   15268 	  Elf32_External_gptab *ext_tab;
   15269 	  unsigned int j;
   15270 
   15271 	  /* The .gptab.sdata and .gptab.sbss sections hold
   15272 	     information describing how the small data area would
   15273 	     change depending upon the -G switch.  These sections
   15274 	     not used in executables files.  */
   15275 	  if (! bfd_link_relocatable (info))
   15276 	    {
   15277 	      for (p = o->map_head.link_order; p != NULL; p = p->next)
   15278 		{
   15279 		  asection *input_section;
   15280 
   15281 		  if (p->type != bfd_indirect_link_order)
   15282 		    {
   15283 		      if (p->type == bfd_data_link_order)
   15284 			continue;
   15285 		      abort ();
   15286 		    }
   15287 
   15288 		  input_section = p->u.indirect.section;
   15289 
   15290 		  /* Hack: reset the SEC_HAS_CONTENTS flag so that
   15291 		     elf_link_input_bfd ignores this section.  */
   15292 		  input_section->flags &= ~SEC_HAS_CONTENTS;
   15293 		}
   15294 
   15295 	      /* Skip this section later on (I don't think this
   15296 		 currently matters, but someday it might).  */
   15297 	      o->map_head.link_order = NULL;
   15298 
   15299 	      /* Really remove the section.  */
   15300 	      bfd_section_list_remove (abfd, o);
   15301 	      --abfd->section_count;
   15302 
   15303 	      continue;
   15304 	    }
   15305 
   15306 	  /* There is one gptab for initialized data, and one for
   15307 	     uninitialized data.  */
   15308 	  if (strcmp (o->name, ".gptab.sdata") == 0)
   15309 	    gptab_data_sec = o;
   15310 	  else if (strcmp (o->name, ".gptab.sbss") == 0)
   15311 	    gptab_bss_sec = o;
   15312 	  else
   15313 	    {
   15314 	      _bfd_error_handler
   15315 		/* xgettext:c-format */
   15316 		(_("%pB: illegal section name `%pA'"), abfd, o);
   15317 	      bfd_set_error (bfd_error_nonrepresentable_section);
   15318 	      return false;
   15319 	    }
   15320 
   15321 	  /* The linker script always combines .gptab.data and
   15322 	     .gptab.sdata into .gptab.sdata, and likewise for
   15323 	     .gptab.bss and .gptab.sbss.  It is possible that there is
   15324 	     no .sdata or .sbss section in the output file, in which
   15325 	     case we must change the name of the output section.  */
   15326 	  subname = o->name + sizeof ".gptab" - 1;
   15327 	  if (bfd_get_section_by_name (abfd, subname) == NULL)
   15328 	    {
   15329 	      if (o == gptab_data_sec)
   15330 		o->name = ".gptab.data";
   15331 	      else
   15332 		o->name = ".gptab.bss";
   15333 	      subname = o->name + sizeof ".gptab" - 1;
   15334 	      BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
   15335 	    }
   15336 
   15337 	  /* Set up the first entry.  */
   15338 	  c = 1;
   15339 	  amt = c * sizeof (Elf32_gptab);
   15340 	  tab = bfd_malloc (amt);
   15341 	  if (tab == NULL)
   15342 	    return false;
   15343 	  tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
   15344 	  tab[0].gt_header.gt_unused = 0;
   15345 
   15346 	  /* Combine the input sections.  */
   15347 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   15348 	    {
   15349 	      asection *input_section;
   15350 	      bfd *input_bfd;
   15351 	      bfd_size_type size;
   15352 	      unsigned long last;
   15353 	      bfd_size_type gpentry;
   15354 
   15355 	      if (p->type != bfd_indirect_link_order)
   15356 		{
   15357 		  if (p->type == bfd_data_link_order)
   15358 		    continue;
   15359 		  abort ();
   15360 		}
   15361 
   15362 	      input_section = p->u.indirect.section;
   15363 	      input_bfd = input_section->owner;
   15364 
   15365 	      /* Combine the gptab entries for this input section one
   15366 		 by one.  We know that the input gptab entries are
   15367 		 sorted by ascending -G value.  */
   15368 	      size = input_section->size;
   15369 	      last = 0;
   15370 	      for (gpentry = sizeof (Elf32_External_gptab);
   15371 		   gpentry < size;
   15372 		   gpentry += sizeof (Elf32_External_gptab))
   15373 		{
   15374 		  Elf32_External_gptab ext_gptab;
   15375 		  Elf32_gptab int_gptab;
   15376 		  unsigned long val;
   15377 		  unsigned long add;
   15378 		  bool exact;
   15379 		  unsigned int look;
   15380 
   15381 		  if (! (bfd_get_section_contents
   15382 			 (input_bfd, input_section, &ext_gptab, gpentry,
   15383 			  sizeof (Elf32_External_gptab))))
   15384 		    {
   15385 		      free (tab);
   15386 		      return false;
   15387 		    }
   15388 
   15389 		  bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
   15390 						&int_gptab);
   15391 		  val = int_gptab.gt_entry.gt_g_value;
   15392 		  add = int_gptab.gt_entry.gt_bytes - last;
   15393 
   15394 		  exact = false;
   15395 		  for (look = 1; look < c; look++)
   15396 		    {
   15397 		      if (tab[look].gt_entry.gt_g_value >= val)
   15398 			tab[look].gt_entry.gt_bytes += add;
   15399 
   15400 		      if (tab[look].gt_entry.gt_g_value == val)
   15401 			exact = true;
   15402 		    }
   15403 
   15404 		  if (! exact)
   15405 		    {
   15406 		      Elf32_gptab *new_tab;
   15407 		      unsigned int max;
   15408 
   15409 		      /* We need a new table entry.  */
   15410 		      amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
   15411 		      new_tab = bfd_realloc (tab, amt);
   15412 		      if (new_tab == NULL)
   15413 			{
   15414 			  free (tab);
   15415 			  return false;
   15416 			}
   15417 		      tab = new_tab;
   15418 		      tab[c].gt_entry.gt_g_value = val;
   15419 		      tab[c].gt_entry.gt_bytes = add;
   15420 
   15421 		      /* Merge in the size for the next smallest -G
   15422 			 value, since that will be implied by this new
   15423 			 value.  */
   15424 		      max = 0;
   15425 		      for (look = 1; look < c; look++)
   15426 			{
   15427 			  if (tab[look].gt_entry.gt_g_value < val
   15428 			      && (max == 0
   15429 				  || (tab[look].gt_entry.gt_g_value
   15430 				      > tab[max].gt_entry.gt_g_value)))
   15431 			    max = look;
   15432 			}
   15433 		      if (max != 0)
   15434 			tab[c].gt_entry.gt_bytes +=
   15435 			  tab[max].gt_entry.gt_bytes;
   15436 
   15437 		      ++c;
   15438 		    }
   15439 
   15440 		  last = int_gptab.gt_entry.gt_bytes;
   15441 		}
   15442 
   15443 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   15444 		 elf_link_input_bfd ignores this section.  */
   15445 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   15446 	    }
   15447 
   15448 	  /* The table must be sorted by -G value.  */
   15449 	  if (c > 2)
   15450 	    qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
   15451 
   15452 	  /* Swap out the table.  */
   15453 	  amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
   15454 	  ext_tab = bfd_alloc (abfd, amt);
   15455 	  if (ext_tab == NULL)
   15456 	    {
   15457 	      free (tab);
   15458 	      return false;
   15459 	    }
   15460 
   15461 	  for (j = 0; j < c; j++)
   15462 	    bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
   15463 	  free (tab);
   15464 
   15465 	  o->size = c * sizeof (Elf32_External_gptab);
   15466 	  o->contents = (bfd_byte *) ext_tab;
   15467 
   15468 	  /* Skip this section later on (I don't think this currently
   15469 	     matters, but someday it might).  */
   15470 	  o->map_head.link_order = NULL;
   15471 	}
   15472     }
   15473 
   15474   /* Invoke the regular ELF backend linker to do all the work.  */
   15475   if (!bfd_elf_final_link (abfd, info))
   15476     return false;
   15477 
   15478   /* Now write out the computed sections.  */
   15479 
   15480   if (abiflags_sec != NULL)
   15481     {
   15482       Elf_External_ABIFlags_v0 ext;
   15483       Elf_Internal_ABIFlags_v0 *abiflags;
   15484 
   15485       abiflags = &mips_elf_tdata (abfd)->abiflags;
   15486 
   15487       /* Set up the abiflags if no valid input sections were found.  */
   15488       if (!mips_elf_tdata (abfd)->abiflags_valid)
   15489 	{
   15490 	  infer_mips_abiflags (abfd, abiflags);
   15491 	  mips_elf_tdata (abfd)->abiflags_valid = true;
   15492 	}
   15493       bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
   15494       if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
   15495 	return false;
   15496     }
   15497 
   15498   if (reginfo_sec != NULL)
   15499     {
   15500       Elf32_External_RegInfo ext;
   15501 
   15502       bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
   15503       if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
   15504 	return false;
   15505     }
   15506 
   15507   if (mdebug_sec != NULL)
   15508     {
   15509       BFD_ASSERT (abfd->output_has_begun);
   15510       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
   15511 					       swap, info,
   15512 					       mdebug_sec->filepos))
   15513 	return false;
   15514 
   15515       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
   15516     }
   15517 
   15518   if (gptab_data_sec != NULL)
   15519     {
   15520       if (! bfd_set_section_contents (abfd, gptab_data_sec,
   15521 				      gptab_data_sec->contents,
   15522 				      0, gptab_data_sec->size))
   15523 	return false;
   15524     }
   15525 
   15526   if (gptab_bss_sec != NULL)
   15527     {
   15528       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
   15529 				      gptab_bss_sec->contents,
   15530 				      0, gptab_bss_sec->size))
   15531 	return false;
   15532     }
   15533 
   15534   if (SGI_COMPAT (abfd))
   15535     {
   15536       rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
   15537       if (rtproc_sec != NULL)
   15538 	{
   15539 	  if (! bfd_set_section_contents (abfd, rtproc_sec,
   15540 					  rtproc_sec->contents,
   15541 					  0, rtproc_sec->size))
   15542 	    return false;
   15543 	}
   15544     }
   15545 
   15546   return true;
   15547 }
   15548 
   15549 /* Merge object file header flags from IBFD into OBFD.  Raise an error
   15551    if there are conflicting settings.  */
   15552 
   15553 static bool
   15554 mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
   15555 {
   15556   bfd *obfd = info->output_bfd;
   15557   struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
   15558   flagword old_flags;
   15559   flagword new_flags;
   15560   bool ok;
   15561 
   15562   new_flags = elf_elfheader (ibfd)->e_flags;
   15563   elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
   15564   old_flags = elf_elfheader (obfd)->e_flags;
   15565 
   15566   /* Check flag compatibility.  */
   15567 
   15568   new_flags &= ~EF_MIPS_NOREORDER;
   15569   old_flags &= ~EF_MIPS_NOREORDER;
   15570 
   15571   /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
   15572      doesn't seem to matter.  */
   15573   new_flags &= ~EF_MIPS_XGOT;
   15574   old_flags &= ~EF_MIPS_XGOT;
   15575 
   15576   /* MIPSpro generates ucode info in n64 objects.  Again, we should
   15577      just be able to ignore this.  */
   15578   new_flags &= ~EF_MIPS_UCODE;
   15579   old_flags &= ~EF_MIPS_UCODE;
   15580 
   15581   /* DSOs should only be linked with CPIC code.  */
   15582   if ((ibfd->flags & DYNAMIC) != 0)
   15583     new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
   15584 
   15585   if (new_flags == old_flags)
   15586     return true;
   15587 
   15588   ok = true;
   15589 
   15590   if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
   15591       != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
   15592     {
   15593       _bfd_error_handler
   15594 	(_("%pB: warning: linking abicalls files with non-abicalls files"),
   15595 	 ibfd);
   15596       ok = true;
   15597     }
   15598 
   15599   if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
   15600     elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
   15601   if (! (new_flags & EF_MIPS_PIC))
   15602     elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
   15603 
   15604   new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
   15605   old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
   15606 
   15607   /* Compare the ISAs.  */
   15608   if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
   15609     {
   15610       _bfd_error_handler
   15611 	(_("%pB: linking 32-bit code with 64-bit code"),
   15612 	 ibfd);
   15613       ok = false;
   15614     }
   15615   else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
   15616     {
   15617       /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
   15618       if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
   15619 	{
   15620 	  /* Copy the architecture info from IBFD to OBFD.  Also copy
   15621 	     the 32-bit flag (if set) so that we continue to recognise
   15622 	     OBFD as a 32-bit binary.  */
   15623 	  bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
   15624 	  elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
   15625 	  elf_elfheader (obfd)->e_flags
   15626 	    |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15627 
   15628 	  /* Update the ABI flags isa_level, isa_rev, isa_ext fields.  */
   15629 	  update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
   15630 
   15631 	  /* Copy across the ABI flags if OBFD doesn't use them
   15632 	     and if that was what caused us to treat IBFD as 32-bit.  */
   15633 	  if ((old_flags & EF_MIPS_ABI) == 0
   15634 	      && mips_32bit_flags_p (new_flags)
   15635 	      && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
   15636 	    elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
   15637 	}
   15638       else
   15639 	{
   15640 	  /* The ISAs aren't compatible.  */
   15641 	  _bfd_error_handler
   15642 	    /* xgettext:c-format */
   15643 	    (_("%pB: linking %s module with previous %s modules"),
   15644 	     ibfd,
   15645 	     bfd_printable_name (ibfd),
   15646 	     bfd_printable_name (obfd));
   15647 	  ok = false;
   15648 	}
   15649     }
   15650 
   15651   new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15652   old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15653 
   15654   /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
   15655      does set EI_CLASS differently from any 32-bit ABI.  */
   15656   if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
   15657       || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   15658 	  != elf_elfheader (obfd)->e_ident[EI_CLASS]))
   15659     {
   15660       /* Only error if both are set (to different values).  */
   15661       if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
   15662 	  || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   15663 	      != elf_elfheader (obfd)->e_ident[EI_CLASS]))
   15664 	{
   15665 	  _bfd_error_handler
   15666 	    /* xgettext:c-format */
   15667 	    (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
   15668 	     ibfd,
   15669 	     elf_mips_abi_name (ibfd),
   15670 	     elf_mips_abi_name (obfd));
   15671 	  ok = false;
   15672 	}
   15673       new_flags &= ~EF_MIPS_ABI;
   15674       old_flags &= ~EF_MIPS_ABI;
   15675     }
   15676 
   15677   /* Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
   15678      and allow arbitrary mixing of the remaining ASEs (retain the union).  */
   15679   if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
   15680     {
   15681       int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
   15682       int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
   15683       int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
   15684       int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
   15685       int micro_mis = old_m16 && new_micro;
   15686       int m16_mis = old_micro && new_m16;
   15687 
   15688       if (m16_mis || micro_mis)
   15689 	{
   15690 	  _bfd_error_handler
   15691 	    /* xgettext:c-format */
   15692 	    (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
   15693 	     ibfd,
   15694 	     m16_mis ? "MIPS16" : "microMIPS",
   15695 	     m16_mis ? "microMIPS" : "MIPS16");
   15696 	  ok = false;
   15697 	}
   15698 
   15699       elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
   15700 
   15701       new_flags &= ~ EF_MIPS_ARCH_ASE;
   15702       old_flags &= ~ EF_MIPS_ARCH_ASE;
   15703     }
   15704 
   15705   /* Compare NaN encodings.  */
   15706   if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
   15707     {
   15708       /* xgettext:c-format */
   15709       _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
   15710 			  ibfd,
   15711 			  (new_flags & EF_MIPS_NAN2008
   15712 			   ? "-mnan=2008" : "-mnan=legacy"),
   15713 			  (old_flags & EF_MIPS_NAN2008
   15714 			   ? "-mnan=2008" : "-mnan=legacy"));
   15715       ok = false;
   15716       new_flags &= ~EF_MIPS_NAN2008;
   15717       old_flags &= ~EF_MIPS_NAN2008;
   15718     }
   15719 
   15720   /* Compare FP64 state.  */
   15721   if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
   15722     {
   15723       /* xgettext:c-format */
   15724       _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
   15725 			  ibfd,
   15726 			  (new_flags & EF_MIPS_FP64
   15727 			   ? "-mfp64" : "-mfp32"),
   15728 			  (old_flags & EF_MIPS_FP64
   15729 			   ? "-mfp64" : "-mfp32"));
   15730       ok = false;
   15731       new_flags &= ~EF_MIPS_FP64;
   15732       old_flags &= ~EF_MIPS_FP64;
   15733     }
   15734 
   15735   /* Warn about any other mismatches */
   15736   if (new_flags != old_flags)
   15737     {
   15738       /* xgettext:c-format */
   15739       _bfd_error_handler
   15740 	(_("%pB: uses different e_flags (%#x) fields than previous modules "
   15741 	   "(%#x)"),
   15742 	 ibfd, new_flags, old_flags);
   15743       ok = false;
   15744     }
   15745 
   15746   return ok;
   15747 }
   15748 
   15749 /* Merge object attributes from IBFD into OBFD.  Raise an error if
   15750    there are conflicting attributes.  */
   15751 static bool
   15752 mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
   15753 {
   15754   bfd *obfd = info->output_bfd;
   15755   obj_attribute *in_attr;
   15756   obj_attribute *out_attr;
   15757   bfd *abi_fp_bfd;
   15758   bfd *abi_msa_bfd;
   15759 
   15760   abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
   15761   in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   15762   if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
   15763     mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   15764 
   15765   abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
   15766   if (!abi_msa_bfd
   15767       && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
   15768     mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
   15769 
   15770   if (!elf_known_obj_attributes_proc (obfd)[0].i)
   15771     {
   15772       /* This is the first object.  Copy the attributes.  */
   15773       _bfd_elf_copy_obj_attributes (ibfd, obfd);
   15774 
   15775       /* Use the Tag_null value to indicate the attributes have been
   15776 	 initialized.  */
   15777       elf_known_obj_attributes_proc (obfd)[0].i = 1;
   15778 
   15779       return true;
   15780     }
   15781 
   15782   /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
   15783      non-conflicting ones.  */
   15784   out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   15785   if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
   15786     {
   15787       int out_fp, in_fp;
   15788 
   15789       out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
   15790       in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   15791       out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
   15792       if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
   15793 	out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
   15794       else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
   15795 	       && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
   15796 		   || in_fp == Val_GNU_MIPS_ABI_FP_64
   15797 		   || in_fp == Val_GNU_MIPS_ABI_FP_64A))
   15798 	{
   15799 	  mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   15800 	  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   15801 	}
   15802       else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
   15803 	       && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
   15804 		   || out_fp == Val_GNU_MIPS_ABI_FP_64
   15805 		   || out_fp == Val_GNU_MIPS_ABI_FP_64A))
   15806 	/* Keep the current setting.  */;
   15807       else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
   15808 	       && in_fp == Val_GNU_MIPS_ABI_FP_64)
   15809 	{
   15810 	  mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   15811 	  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   15812 	}
   15813       else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
   15814 	       && out_fp == Val_GNU_MIPS_ABI_FP_64)
   15815 	/* Keep the current setting.  */;
   15816       else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
   15817 	{
   15818 	  const char *out_string, *in_string;
   15819 
   15820 	  out_string = _bfd_mips_fp_abi_string (out_fp);
   15821 	  in_string = _bfd_mips_fp_abi_string (in_fp);
   15822 	  /* First warn about cases involving unrecognised ABIs.  */
   15823 	  if (!out_string && !in_string)
   15824 	    /* xgettext:c-format */
   15825 	    _bfd_error_handler
   15826 	      (_("warning: %pB uses unknown floating point ABI %d "
   15827 		 "(set by %pB), %pB uses unknown floating point ABI %d"),
   15828 	       obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
   15829 	  else if (!out_string)
   15830 	    _bfd_error_handler
   15831 	      /* xgettext:c-format */
   15832 	      (_("warning: %pB uses unknown floating point ABI %d "
   15833 		 "(set by %pB), %pB uses %s"),
   15834 	       obfd, out_fp, abi_fp_bfd, ibfd, in_string);
   15835 	  else if (!in_string)
   15836 	    _bfd_error_handler
   15837 	      /* xgettext:c-format */
   15838 	      (_("warning: %pB uses %s (set by %pB), "
   15839 		 "%pB uses unknown floating point ABI %d"),
   15840 	       obfd, out_string, abi_fp_bfd, ibfd, in_fp);
   15841 	  else
   15842 	    {
   15843 	      /* If one of the bfds is soft-float, the other must be
   15844 		 hard-float.  The exact choice of hard-float ABI isn't
   15845 		 really relevant to the error message.  */
   15846 	      if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
   15847 		out_string = "-mhard-float";
   15848 	      else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
   15849 		in_string = "-mhard-float";
   15850 	      _bfd_error_handler
   15851 		/* xgettext:c-format */
   15852 		(_("warning: %pB uses %s (set by %pB), %pB uses %s"),
   15853 		 obfd, out_string, abi_fp_bfd, ibfd, in_string);
   15854 	    }
   15855 	}
   15856     }
   15857 
   15858   /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
   15859      non-conflicting ones.  */
   15860   if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
   15861     {
   15862       out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
   15863       if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
   15864 	out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
   15865       else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
   15866 	switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
   15867 	  {
   15868 	  case Val_GNU_MIPS_ABI_MSA_128:
   15869 	    _bfd_error_handler
   15870 	      /* xgettext:c-format */
   15871 	      (_("warning: %pB uses %s (set by %pB), "
   15872 		 "%pB uses unknown MSA ABI %d"),
   15873 	       obfd, "-mmsa", abi_msa_bfd,
   15874 	       ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
   15875 	    break;
   15876 
   15877 	  default:
   15878 	    switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
   15879 	      {
   15880 	      case Val_GNU_MIPS_ABI_MSA_128:
   15881 		_bfd_error_handler
   15882 		  /* xgettext:c-format */
   15883 		  (_("warning: %pB uses unknown MSA ABI %d "
   15884 		     "(set by %pB), %pB uses %s"),
   15885 		     obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
   15886 		   abi_msa_bfd, ibfd, "-mmsa");
   15887 		  break;
   15888 
   15889 	      default:
   15890 		_bfd_error_handler
   15891 		  /* xgettext:c-format */
   15892 		  (_("warning: %pB uses unknown MSA ABI %d "
   15893 		     "(set by %pB), %pB uses unknown MSA ABI %d"),
   15894 		   obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
   15895 		   abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
   15896 		break;
   15897 	      }
   15898 	  }
   15899     }
   15900 
   15901   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   15902   return _bfd_elf_merge_object_attributes (ibfd, info);
   15903 }
   15904 
   15905 /* Merge object ABI flags from IBFD into OBFD.  Raise an error if
   15906    there are conflicting settings.  */
   15907 
   15908 static bool
   15909 mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
   15910 {
   15911   obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   15912   struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
   15913   struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
   15914 
   15915   /* Update the output abiflags fp_abi using the computed fp_abi.  */
   15916   out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
   15917 
   15918 #define max(a, b) ((a) > (b) ? (a) : (b))
   15919   /* Merge abiflags.  */
   15920   out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
   15921 				       in_tdata->abiflags.isa_level);
   15922   out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
   15923 				     in_tdata->abiflags.isa_rev);
   15924   out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
   15925 				      in_tdata->abiflags.gpr_size);
   15926   out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
   15927 				       in_tdata->abiflags.cpr1_size);
   15928   out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
   15929 				       in_tdata->abiflags.cpr2_size);
   15930 #undef max
   15931   out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
   15932   out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
   15933 
   15934   return true;
   15935 }
   15936 
   15937 /* Merge backend specific data from an object file to the output
   15938    object file when linking.  */
   15939 
   15940 bool
   15941 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   15942 {
   15943   bfd *obfd = info->output_bfd;
   15944   struct mips_elf_obj_tdata *out_tdata;
   15945   struct mips_elf_obj_tdata *in_tdata;
   15946   bool null_input_bfd = true;
   15947   asection *sec;
   15948   bool ok;
   15949 
   15950   /* Check if we have the same endianness.  */
   15951   if (! _bfd_generic_verify_endian_match (ibfd, info))
   15952     {
   15953       _bfd_error_handler
   15954 	(_("%pB: endianness incompatible with that of the selected emulation"),
   15955 	 ibfd);
   15956       return false;
   15957     }
   15958 
   15959   if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
   15960     return true;
   15961 
   15962   in_tdata = mips_elf_tdata (ibfd);
   15963   out_tdata = mips_elf_tdata (obfd);
   15964 
   15965   if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
   15966     {
   15967       _bfd_error_handler
   15968 	(_("%pB: ABI is incompatible with that of the selected emulation"),
   15969 	 ibfd);
   15970       return false;
   15971     }
   15972 
   15973   /* Check to see if the input BFD actually contains any sections.  If not,
   15974      then it has no attributes, and its flags may not have been initialized
   15975      either, but it cannot actually cause any incompatibility.  */
   15976   /* FIXME: This excludes any input shared library from consideration.  */
   15977   for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   15978     {
   15979       /* Ignore synthetic sections and empty .text, .data and .bss sections
   15980 	 which are automatically generated by gas.  Also ignore fake
   15981 	 (s)common sections, since merely defining a common symbol does
   15982 	 not affect compatibility.  */
   15983       if ((sec->flags & SEC_IS_COMMON) == 0
   15984 	  && strcmp (sec->name, ".reginfo")
   15985 	  && strcmp (sec->name, ".mdebug")
   15986 	  && (sec->size != 0
   15987 	      || (strcmp (sec->name, ".text")
   15988 		  && strcmp (sec->name, ".data")
   15989 		  && strcmp (sec->name, ".bss"))))
   15990 	{
   15991 	  null_input_bfd = false;
   15992 	  break;
   15993 	}
   15994     }
   15995   if (null_input_bfd)
   15996     return true;
   15997 
   15998   /* Populate abiflags using existing information.  */
   15999   if (in_tdata->abiflags_valid)
   16000     {
   16001       obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   16002       Elf_Internal_ABIFlags_v0 in_abiflags;
   16003       Elf_Internal_ABIFlags_v0 abiflags;
   16004 
   16005       /* Set up the FP ABI attribute from the abiflags if it is not already
   16006 	 set.  */
   16007       if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
   16008 	in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
   16009 
   16010       infer_mips_abiflags (ibfd, &abiflags);
   16011       in_abiflags = in_tdata->abiflags;
   16012 
   16013       /* It is not possible to infer the correct ISA revision
   16014 	 for R3 or R5 so drop down to R2 for the checks.  */
   16015       if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
   16016 	in_abiflags.isa_rev = 2;
   16017 
   16018       if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
   16019 	  < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
   16020 	_bfd_error_handler
   16021 	  (_("%pB: warning: inconsistent ISA between e_flags and "
   16022 	     ".MIPS.abiflags"), ibfd);
   16023       if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
   16024 	  && in_abiflags.fp_abi != abiflags.fp_abi)
   16025 	_bfd_error_handler
   16026 	  (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
   16027 	     ".MIPS.abiflags"), ibfd);
   16028       if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
   16029 	_bfd_error_handler
   16030 	  (_("%pB: warning: inconsistent ASEs between e_flags and "
   16031 	     ".MIPS.abiflags"), ibfd);
   16032       /* The isa_ext is allowed to be an extension of what can be inferred
   16033 	 from e_flags.  */
   16034       if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
   16035 				bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
   16036 	_bfd_error_handler
   16037 	  (_("%pB: warning: inconsistent ISA extensions between e_flags and "
   16038 	     ".MIPS.abiflags"), ibfd);
   16039       if (in_abiflags.flags2 != 0)
   16040 	_bfd_error_handler
   16041 	  (_("%pB: warning: unexpected flag in the flags2 field of "
   16042 	     ".MIPS.abiflags (0x%lx)"), ibfd,
   16043 	   in_abiflags.flags2);
   16044     }
   16045   else
   16046     {
   16047       infer_mips_abiflags (ibfd, &in_tdata->abiflags);
   16048       in_tdata->abiflags_valid = true;
   16049     }
   16050 
   16051   if (!out_tdata->abiflags_valid)
   16052     {
   16053       /* Copy input abiflags if output abiflags are not already valid.  */
   16054       out_tdata->abiflags = in_tdata->abiflags;
   16055       out_tdata->abiflags_valid = true;
   16056     }
   16057 
   16058   if (! elf_flags_init (obfd))
   16059     {
   16060       elf_flags_init (obfd) = true;
   16061       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   16062       elf_elfheader (obfd)->e_ident[EI_CLASS]
   16063 	= elf_elfheader (ibfd)->e_ident[EI_CLASS];
   16064 
   16065       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   16066 	  && (bfd_get_arch_info (obfd)->the_default
   16067 	      || mips_mach_extends_p (bfd_get_mach (obfd),
   16068 				      bfd_get_mach (ibfd))))
   16069 	{
   16070 	  if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
   16071 				   bfd_get_mach (ibfd)))
   16072 	    return false;
   16073 
   16074 	  /* Update the ABI flags isa_level, isa_rev and isa_ext fields.  */
   16075 	  update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
   16076 	}
   16077 
   16078       ok = true;
   16079     }
   16080   else
   16081     ok = mips_elf_merge_obj_e_flags (ibfd, info);
   16082 
   16083   ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
   16084 
   16085   ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
   16086 
   16087   if (!ok)
   16088     {
   16089       bfd_set_error (bfd_error_bad_value);
   16090       return false;
   16091     }
   16092 
   16093   return true;
   16094 }
   16095 
   16096 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
   16097 
   16098 bool
   16099 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
   16100 {
   16101   BFD_ASSERT (!elf_flags_init (abfd)
   16102 	      || elf_elfheader (abfd)->e_flags == flags);
   16103 
   16104   elf_elfheader (abfd)->e_flags = flags;
   16105   elf_flags_init (abfd) = true;
   16106   return true;
   16107 }
   16108 
   16109 char *
   16110 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
   16111 {
   16112   switch (dtag)
   16113     {
   16114     default: return "";
   16115     case DT_MIPS_RLD_VERSION:
   16116       return "MIPS_RLD_VERSION";
   16117     case DT_MIPS_TIME_STAMP:
   16118       return "MIPS_TIME_STAMP";
   16119     case DT_MIPS_ICHECKSUM:
   16120       return "MIPS_ICHECKSUM";
   16121     case DT_MIPS_IVERSION:
   16122       return "MIPS_IVERSION";
   16123     case DT_MIPS_FLAGS:
   16124       return "MIPS_FLAGS";
   16125     case DT_MIPS_BASE_ADDRESS:
   16126       return "MIPS_BASE_ADDRESS";
   16127     case DT_MIPS_MSYM:
   16128       return "MIPS_MSYM";
   16129     case DT_MIPS_CONFLICT:
   16130       return "MIPS_CONFLICT";
   16131     case DT_MIPS_LIBLIST:
   16132       return "MIPS_LIBLIST";
   16133     case DT_MIPS_LOCAL_GOTNO:
   16134       return "MIPS_LOCAL_GOTNO";
   16135     case DT_MIPS_CONFLICTNO:
   16136       return "MIPS_CONFLICTNO";
   16137     case DT_MIPS_LIBLISTNO:
   16138       return "MIPS_LIBLISTNO";
   16139     case DT_MIPS_SYMTABNO:
   16140       return "MIPS_SYMTABNO";
   16141     case DT_MIPS_UNREFEXTNO:
   16142       return "MIPS_UNREFEXTNO";
   16143     case DT_MIPS_GOTSYM:
   16144       return "MIPS_GOTSYM";
   16145     case DT_MIPS_HIPAGENO:
   16146       return "MIPS_HIPAGENO";
   16147     case DT_MIPS_RLD_MAP:
   16148       return "MIPS_RLD_MAP";
   16149     case DT_MIPS_RLD_MAP_REL:
   16150       return "MIPS_RLD_MAP_REL";
   16151     case DT_MIPS_DELTA_CLASS:
   16152       return "MIPS_DELTA_CLASS";
   16153     case DT_MIPS_DELTA_CLASS_NO:
   16154       return "MIPS_DELTA_CLASS_NO";
   16155     case DT_MIPS_DELTA_INSTANCE:
   16156       return "MIPS_DELTA_INSTANCE";
   16157     case DT_MIPS_DELTA_INSTANCE_NO:
   16158       return "MIPS_DELTA_INSTANCE_NO";
   16159     case DT_MIPS_DELTA_RELOC:
   16160       return "MIPS_DELTA_RELOC";
   16161     case DT_MIPS_DELTA_RELOC_NO:
   16162       return "MIPS_DELTA_RELOC_NO";
   16163     case DT_MIPS_DELTA_SYM:
   16164       return "MIPS_DELTA_SYM";
   16165     case DT_MIPS_DELTA_SYM_NO:
   16166       return "MIPS_DELTA_SYM_NO";
   16167     case DT_MIPS_DELTA_CLASSSYM:
   16168       return "MIPS_DELTA_CLASSSYM";
   16169     case DT_MIPS_DELTA_CLASSSYM_NO:
   16170       return "MIPS_DELTA_CLASSSYM_NO";
   16171     case DT_MIPS_CXX_FLAGS:
   16172       return "MIPS_CXX_FLAGS";
   16173     case DT_MIPS_PIXIE_INIT:
   16174       return "MIPS_PIXIE_INIT";
   16175     case DT_MIPS_SYMBOL_LIB:
   16176       return "MIPS_SYMBOL_LIB";
   16177     case DT_MIPS_LOCALPAGE_GOTIDX:
   16178       return "MIPS_LOCALPAGE_GOTIDX";
   16179     case DT_MIPS_LOCAL_GOTIDX:
   16180       return "MIPS_LOCAL_GOTIDX";
   16181     case DT_MIPS_HIDDEN_GOTIDX:
   16182       return "MIPS_HIDDEN_GOTIDX";
   16183     case DT_MIPS_PROTECTED_GOTIDX:
   16184       return "MIPS_PROTECTED_GOT_IDX";
   16185     case DT_MIPS_OPTIONS:
   16186       return "MIPS_OPTIONS";
   16187     case DT_MIPS_INTERFACE:
   16188       return "MIPS_INTERFACE";
   16189     case DT_MIPS_DYNSTR_ALIGN:
   16190       return "DT_MIPS_DYNSTR_ALIGN";
   16191     case DT_MIPS_INTERFACE_SIZE:
   16192       return "DT_MIPS_INTERFACE_SIZE";
   16193     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
   16194       return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
   16195     case DT_MIPS_PERF_SUFFIX:
   16196       return "DT_MIPS_PERF_SUFFIX";
   16197     case DT_MIPS_COMPACT_SIZE:
   16198       return "DT_MIPS_COMPACT_SIZE";
   16199     case DT_MIPS_GP_VALUE:
   16200       return "DT_MIPS_GP_VALUE";
   16201     case DT_MIPS_AUX_DYNAMIC:
   16202       return "DT_MIPS_AUX_DYNAMIC";
   16203     case DT_MIPS_PLTGOT:
   16204       return "DT_MIPS_PLTGOT";
   16205     case DT_MIPS_RWPLT:
   16206       return "DT_MIPS_RWPLT";
   16207     case DT_MIPS_XHASH:
   16208       return "DT_MIPS_XHASH";
   16209     }
   16210 }
   16211 
   16212 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
   16213    not known.  */
   16214 
   16215 const char *
   16216 _bfd_mips_fp_abi_string (int fp)
   16217 {
   16218   switch (fp)
   16219     {
   16220       /* These strings aren't translated because they're simply
   16221 	 option lists.  */
   16222     case Val_GNU_MIPS_ABI_FP_DOUBLE:
   16223       return "-mdouble-float";
   16224 
   16225     case Val_GNU_MIPS_ABI_FP_SINGLE:
   16226       return "-msingle-float";
   16227 
   16228     case Val_GNU_MIPS_ABI_FP_SOFT:
   16229       return "-msoft-float";
   16230 
   16231     case Val_GNU_MIPS_ABI_FP_OLD_64:
   16232       return _("-mips32r2 -mfp64 (12 callee-saved)");
   16233 
   16234     case Val_GNU_MIPS_ABI_FP_XX:
   16235       return "-mfpxx";
   16236 
   16237     case Val_GNU_MIPS_ABI_FP_64:
   16238       return "-mgp32 -mfp64";
   16239 
   16240     case Val_GNU_MIPS_ABI_FP_64A:
   16241       return "-mgp32 -mfp64 -mno-odd-spreg";
   16242 
   16243     default:
   16244       return 0;
   16245     }
   16246 }
   16247 
   16248 static void
   16249 print_mips_ases (FILE *file, unsigned int mask)
   16250 {
   16251   if (mask & AFL_ASE_DSP)
   16252     fputs ("\n\tDSP ASE", file);
   16253   if (mask & AFL_ASE_DSPR2)
   16254     fputs ("\n\tDSP R2 ASE", file);
   16255   if (mask & AFL_ASE_DSPR3)
   16256     fputs ("\n\tDSP R3 ASE", file);
   16257   if (mask & AFL_ASE_EVA)
   16258     fputs ("\n\tEnhanced VA Scheme", file);
   16259   if (mask & AFL_ASE_MCU)
   16260     fputs ("\n\tMCU (MicroController) ASE", file);
   16261   if (mask & AFL_ASE_MDMX)
   16262     fputs ("\n\tMDMX ASE", file);
   16263   if (mask & AFL_ASE_MIPS3D)
   16264     fputs ("\n\tMIPS-3D ASE", file);
   16265   if (mask & AFL_ASE_MT)
   16266     fputs ("\n\tMT ASE", file);
   16267   if (mask & AFL_ASE_SMARTMIPS)
   16268     fputs ("\n\tSmartMIPS ASE", file);
   16269   if (mask & AFL_ASE_VIRT)
   16270     fputs ("\n\tVZ ASE", file);
   16271   if (mask & AFL_ASE_MSA)
   16272     fputs ("\n\tMSA ASE", file);
   16273   if (mask & AFL_ASE_MIPS16)
   16274     fputs ("\n\tMIPS16 ASE", file);
   16275   if (mask & AFL_ASE_MICROMIPS)
   16276     fputs ("\n\tMICROMIPS ASE", file);
   16277   if (mask & AFL_ASE_XPA)
   16278     fputs ("\n\tXPA ASE", file);
   16279   if (mask & AFL_ASE_MIPS16E2)
   16280     fputs ("\n\tMIPS16e2 ASE", file);
   16281   if (mask & AFL_ASE_CRC)
   16282     fputs ("\n\tCRC ASE", file);
   16283   if (mask & AFL_ASE_GINV)
   16284     fputs ("\n\tGINV ASE", file);
   16285   if (mask & AFL_ASE_LOONGSON_MMI)
   16286     fputs ("\n\tLoongson MMI ASE", file);
   16287   if (mask & AFL_ASE_LOONGSON_CAM)
   16288     fputs ("\n\tLoongson CAM ASE", file);
   16289   if (mask & AFL_ASE_LOONGSON_EXT)
   16290     fputs ("\n\tLoongson EXT ASE", file);
   16291   if (mask & AFL_ASE_LOONGSON_EXT2)
   16292     fputs ("\n\tLoongson EXT2 ASE", file);
   16293   if (mask == 0)
   16294     fprintf (file, "\n\t%s", _("None"));
   16295   else if ((mask & ~AFL_ASE_MASK) != 0)
   16296     fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
   16297 }
   16298 
   16299 static void
   16300 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
   16301 {
   16302   switch (isa_ext)
   16303     {
   16304     case 0:
   16305       fputs (_("None"), file);
   16306       break;
   16307     case AFL_EXT_XLR:
   16308       fputs ("RMI XLR", file);
   16309       break;
   16310     case AFL_EXT_OCTEON3:
   16311       fputs ("Cavium Networks Octeon3", file);
   16312       break;
   16313     case AFL_EXT_OCTEON2:
   16314       fputs ("Cavium Networks Octeon2", file);
   16315       break;
   16316     case AFL_EXT_OCTEONP:
   16317       fputs ("Cavium Networks OcteonP", file);
   16318       break;
   16319     case AFL_EXT_OCTEON:
   16320       fputs ("Cavium Networks Octeon", file);
   16321       break;
   16322     case AFL_EXT_5900:
   16323       fputs ("Toshiba R5900", file);
   16324       break;
   16325     case AFL_EXT_4650:
   16326       fputs ("MIPS R4650", file);
   16327       break;
   16328     case AFL_EXT_4010:
   16329       fputs ("LSI R4010", file);
   16330       break;
   16331     case AFL_EXT_4100:
   16332       fputs ("NEC VR4100", file);
   16333       break;
   16334     case AFL_EXT_3900:
   16335       fputs ("Toshiba R3900", file);
   16336       break;
   16337     case AFL_EXT_10000:
   16338       fputs ("MIPS R10000", file);
   16339       break;
   16340     case AFL_EXT_SB1:
   16341       fputs ("Broadcom SB-1", file);
   16342       break;
   16343     case AFL_EXT_4111:
   16344       fputs ("NEC VR4111/VR4181", file);
   16345       break;
   16346     case AFL_EXT_4120:
   16347       fputs ("NEC VR4120", file);
   16348       break;
   16349     case AFL_EXT_5400:
   16350       fputs ("NEC VR5400", file);
   16351       break;
   16352     case AFL_EXT_5500:
   16353       fputs ("NEC VR5500", file);
   16354       break;
   16355     case AFL_EXT_LOONGSON_2E:
   16356       fputs ("ST Microelectronics Loongson 2E", file);
   16357       break;
   16358     case AFL_EXT_LOONGSON_2F:
   16359       fputs ("ST Microelectronics Loongson 2F", file);
   16360       break;
   16361     case AFL_EXT_INTERAPTIV_MR2:
   16362       fputs ("Imagination interAptiv MR2", file);
   16363       break;
   16364     default:
   16365       fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
   16366       break;
   16367     }
   16368 }
   16369 
   16370 static void
   16371 print_mips_fp_abi_value (FILE *file, int val)
   16372 {
   16373   switch (val)
   16374     {
   16375     case Val_GNU_MIPS_ABI_FP_ANY:
   16376       fprintf (file, _("Hard or soft float\n"));
   16377       break;
   16378     case Val_GNU_MIPS_ABI_FP_DOUBLE:
   16379       fprintf (file, _("Hard float (double precision)\n"));
   16380       break;
   16381     case Val_GNU_MIPS_ABI_FP_SINGLE:
   16382       fprintf (file, _("Hard float (single precision)\n"));
   16383       break;
   16384     case Val_GNU_MIPS_ABI_FP_SOFT:
   16385       fprintf (file, _("Soft float\n"));
   16386       break;
   16387     case Val_GNU_MIPS_ABI_FP_OLD_64:
   16388       fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
   16389       break;
   16390     case Val_GNU_MIPS_ABI_FP_XX:
   16391       fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
   16392       break;
   16393     case Val_GNU_MIPS_ABI_FP_64:
   16394       fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
   16395       break;
   16396     case Val_GNU_MIPS_ABI_FP_64A:
   16397       fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
   16398       break;
   16399     default:
   16400       fprintf (file, "??? (%d)\n", val);
   16401       break;
   16402     }
   16403 }
   16404 
   16405 static int
   16406 get_mips_reg_size (int reg_size)
   16407 {
   16408   return (reg_size == AFL_REG_NONE) ? 0
   16409 	 : (reg_size == AFL_REG_32) ? 32
   16410 	 : (reg_size == AFL_REG_64) ? 64
   16411 	 : (reg_size == AFL_REG_128) ? 128
   16412 	 : -1;
   16413 }
   16414 
   16415 bool
   16416 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
   16417 {
   16418   FILE *file = ptr;
   16419 
   16420   BFD_ASSERT (abfd != NULL && ptr != NULL);
   16421 
   16422   /* Print normal ELF private data.  */
   16423   _bfd_elf_print_private_bfd_data (abfd, ptr);
   16424 
   16425   /* xgettext:c-format */
   16426   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
   16427 
   16428   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
   16429     fprintf (file, _(" [abi=O32]"));
   16430   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O64)
   16431     fprintf (file, _(" [abi=O64]"));
   16432   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32)
   16433     fprintf (file, _(" [abi=EABI32]"));
   16434   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
   16435     fprintf (file, _(" [abi=EABI64]"));
   16436   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
   16437     fprintf (file, _(" [abi unknown]"));
   16438   else if (ABI_N32_P (abfd))
   16439     fprintf (file, _(" [abi=N32]"));
   16440   else if (ABI_64_P (abfd))
   16441     fprintf (file, _(" [abi=64]"));
   16442   else
   16443     fprintf (file, _(" [no abi set]"));
   16444 
   16445   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1)
   16446     fprintf (file, " [mips1]");
   16447   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2)
   16448     fprintf (file, " [mips2]");
   16449   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_3)
   16450     fprintf (file, " [mips3]");
   16451   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_4)
   16452     fprintf (file, " [mips4]");
   16453   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_5)
   16454     fprintf (file, " [mips5]");
   16455   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32)
   16456     fprintf (file, " [mips32]");
   16457   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64)
   16458     fprintf (file, " [mips64]");
   16459   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2)
   16460     fprintf (file, " [mips32r2]");
   16461   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R2)
   16462     fprintf (file, " [mips64r2]");
   16463   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6)
   16464     fprintf (file, " [mips32r6]");
   16465   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
   16466     fprintf (file, " [mips64r6]");
   16467   else
   16468     fprintf (file, _(" [unknown ISA]"));
   16469 
   16470   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
   16471     fprintf (file, " [mdmx]");
   16472 
   16473   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
   16474     fprintf (file, " [mips16]");
   16475 
   16476   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
   16477     fprintf (file, " [micromips]");
   16478 
   16479   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
   16480     fprintf (file, " [nan2008]");
   16481 
   16482   if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
   16483     fprintf (file, " [old fp64]");
   16484 
   16485   if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
   16486     fprintf (file, " [32bitmode]");
   16487   else
   16488     fprintf (file, _(" [not 32bitmode]"));
   16489 
   16490   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
   16491     fprintf (file, " [noreorder]");
   16492 
   16493   if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
   16494     fprintf (file, " [PIC]");
   16495 
   16496   if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
   16497     fprintf (file, " [CPIC]");
   16498 
   16499   if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
   16500     fprintf (file, " [XGOT]");
   16501 
   16502   if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
   16503     fprintf (file, " [UCODE]");
   16504 
   16505   fputc ('\n', file);
   16506 
   16507   if (mips_elf_tdata (abfd)->abiflags_valid)
   16508     {
   16509       Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
   16510       fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
   16511       fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
   16512       if (abiflags->isa_rev > 1)
   16513 	fprintf (file, "r%d", abiflags->isa_rev);
   16514       fprintf (file, "\nGPR size: %d",
   16515 	       get_mips_reg_size (abiflags->gpr_size));
   16516       fprintf (file, "\nCPR1 size: %d",
   16517 	       get_mips_reg_size (abiflags->cpr1_size));
   16518       fprintf (file, "\nCPR2 size: %d",
   16519 	       get_mips_reg_size (abiflags->cpr2_size));
   16520       fputs ("\nFP ABI: ", file);
   16521       print_mips_fp_abi_value (file, abiflags->fp_abi);
   16522       fputs ("ISA Extension: ", file);
   16523       print_mips_isa_ext (file, abiflags->isa_ext);
   16524       fputs ("\nASEs:", file);
   16525       print_mips_ases (file, abiflags->ases);
   16526       fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
   16527       fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
   16528       fputc ('\n', file);
   16529     }
   16530 
   16531   return true;
   16532 }
   16533 
   16534 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
   16535 {
   16536   { STRING_COMMA_LEN (".lit4"),	  0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   16537   { STRING_COMMA_LEN (".lit8"),	  0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   16538   { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
   16539   { STRING_COMMA_LEN (".sbss"),	 -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   16540   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   16541   { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
   16542   { STRING_COMMA_LEN (".MIPS.xhash"),  0, SHT_MIPS_XHASH,   SHF_ALLOC },
   16543   { NULL,		      0,  0, 0,		     0 }
   16544 };
   16545 
   16546 /* Merge non visibility st_other attributes.  Ensure that the
   16547    STO_OPTIONAL flag is copied into h->other, even if this is not a
   16548    definiton of the symbol.  */
   16549 void
   16550 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
   16551 				      unsigned int st_other,
   16552 				      bool definition,
   16553 				      bool dynamic ATTRIBUTE_UNUSED)
   16554 {
   16555   if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
   16556     {
   16557       unsigned char other;
   16558 
   16559       other = (definition ? st_other : h->other);
   16560       other &= ~ELF_ST_VISIBILITY (-1);
   16561       h->other = other | ELF_ST_VISIBILITY (h->other);
   16562     }
   16563 
   16564   if (!definition
   16565       && ELF_MIPS_IS_OPTIONAL (st_other))
   16566     h->other |= STO_OPTIONAL;
   16567 }
   16568 
   16569 /* Decide whether an undefined symbol is special and can be ignored.
   16570    This is the case for OPTIONAL symbols on IRIX.  */
   16571 bool
   16572 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
   16573 {
   16574   return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
   16575 }
   16576 
   16577 bool
   16578 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
   16579 {
   16580   return (sym->st_shndx == SHN_COMMON
   16581 	  || sym->st_shndx == SHN_MIPS_ACOMMON
   16582 	  || sym->st_shndx == SHN_MIPS_SCOMMON);
   16583 }
   16584 
   16585 /* Return address for Ith PLT stub in section PLT, for relocation REL
   16586    or (bfd_vma) -1 if it should not be included.  */
   16587 
   16588 bfd_vma
   16589 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
   16590 			   const arelent *rel ATTRIBUTE_UNUSED)
   16591 {
   16592   return (plt->vma
   16593 	  + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
   16594 	  + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
   16595 }
   16596 
   16597 /* Build a table of synthetic symbols to represent the PLT.  As with MIPS16
   16598    and microMIPS PLT slots we may have a many-to-one mapping between .plt
   16599    and .got.plt and also the slots may be of a different size each we walk
   16600    the PLT manually fetching instructions and matching them against known
   16601    patterns.  To make things easier standard MIPS slots, if any, always come
   16602    first.  As we don't create proper ELF symbols we use the UDATA.I member
   16603    of ASYMBOL to carry ISA annotation.  The encoding used is the same as
   16604    with the ST_OTHER member of the ELF symbol.  */
   16605 
   16606 long
   16607 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
   16608 				    long symcount ATTRIBUTE_UNUSED,
   16609 				    asymbol **syms ATTRIBUTE_UNUSED,
   16610 				    long dynsymcount, asymbol **dynsyms,
   16611 				    asymbol **ret)
   16612 {
   16613   static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
   16614   static const char microsuffix[] = "@micromipsplt";
   16615   static const char m16suffix[] = "@mips16plt";
   16616   static const char mipssuffix[] = "@plt";
   16617 
   16618   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   16619   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   16620   bool micromips_p = MICROMIPS_P (abfd);
   16621   Elf_Internal_Shdr *hdr;
   16622   bfd_byte *plt_data;
   16623   bfd_vma plt_offset;
   16624   unsigned int other;
   16625   bfd_vma entry_size;
   16626   bfd_vma plt0_size;
   16627   asection *relplt;
   16628   bfd_vma opcode;
   16629   asection *plt;
   16630   asymbol *send;
   16631   size_t size;
   16632   char *names;
   16633   long counti;
   16634   arelent *p;
   16635   asymbol *s;
   16636   char *nend;
   16637   long count;
   16638   long pi;
   16639   long i;
   16640   long n;
   16641 
   16642   *ret = NULL;
   16643 
   16644   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
   16645     return 0;
   16646 
   16647   relplt = bfd_get_section_by_name (abfd, ".rel.plt");
   16648   if (relplt == NULL)
   16649     return 0;
   16650 
   16651   hdr = &elf_section_data (relplt)->this_hdr;
   16652   if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
   16653     return 0;
   16654 
   16655   plt = bfd_get_section_by_name (abfd, ".plt");
   16656   if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
   16657     return 0;
   16658 
   16659   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   16660   if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
   16661     return -1;
   16662   p = relplt->relocation;
   16663 
   16664   /* Calculating the exact amount of space required for symbols would
   16665      require two passes over the PLT, so just pessimise assuming two
   16666      PLT slots per relocation.  */
   16667   count = NUM_SHDR_ENTRIES (hdr);
   16668   counti = count * bed->s->int_rels_per_ext_rel;
   16669   size = 2 * count * sizeof (asymbol);
   16670   size += count * (sizeof (mipssuffix) +
   16671 		   (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
   16672   for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
   16673     size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
   16674 
   16675   /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too.  */
   16676   size += sizeof (asymbol) + sizeof (pltname);
   16677 
   16678   if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
   16679     return -1;
   16680 
   16681   if (plt->size < 16)
   16682     return -1;
   16683 
   16684   s = *ret = bfd_malloc (size);
   16685   if (s == NULL)
   16686     return -1;
   16687   send = s + 2 * count + 1;
   16688 
   16689   names = (char *) send;
   16690   nend = (char *) s + size;
   16691   n = 0;
   16692 
   16693   opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
   16694   if (opcode == 0x3302fffe)
   16695     {
   16696       if (!micromips_p)
   16697 	return -1;
   16698       plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
   16699       other = STO_MICROMIPS;
   16700     }
   16701   else if (opcode == 0x0398c1d0)
   16702     {
   16703       if (!micromips_p)
   16704 	return -1;
   16705       plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
   16706       other = STO_MICROMIPS;
   16707     }
   16708   else
   16709     {
   16710       plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
   16711       other = 0;
   16712     }
   16713 
   16714   s->the_bfd = abfd;
   16715   s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
   16716   s->section = plt;
   16717   s->value = 0;
   16718   s->name = names;
   16719   s->udata.i = other;
   16720   memcpy (names, pltname, sizeof (pltname));
   16721   names += sizeof (pltname);
   16722   ++s, ++n;
   16723 
   16724   pi = 0;
   16725   for (plt_offset = plt0_size;
   16726        plt_offset + 8 <= plt->size && s < send;
   16727        plt_offset += entry_size)
   16728     {
   16729       bfd_vma gotplt_addr;
   16730       const char *suffix;
   16731       bfd_vma gotplt_hi;
   16732       bfd_vma gotplt_lo;
   16733       size_t suffixlen;
   16734 
   16735       opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
   16736 
   16737       /* Check if the second word matches the expected MIPS16 instruction.  */
   16738       if (opcode == 0x651aeb00)
   16739 	{
   16740 	  if (micromips_p)
   16741 	    return -1;
   16742 	  /* Truncated table???  */
   16743 	  if (plt_offset + 16 > plt->size)
   16744 	    break;
   16745 	  gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
   16746 	  entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
   16747 	  suffixlen = sizeof (m16suffix);
   16748 	  suffix = m16suffix;
   16749 	  other = STO_MIPS16;
   16750 	}
   16751       /* Likewise the expected microMIPS instruction (no insn32 mode).  */
   16752       else if (opcode == 0xff220000)
   16753 	{
   16754 	  if (!micromips_p)
   16755 	    return -1;
   16756 	  gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
   16757 	  gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
   16758 	  gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
   16759 	  gotplt_lo <<= 2;
   16760 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16761 	  gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
   16762 	  entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
   16763 	  suffixlen = sizeof (microsuffix);
   16764 	  suffix = microsuffix;
   16765 	  other = STO_MICROMIPS;
   16766 	}
   16767       /* Likewise the expected microMIPS instruction (insn32 mode).  */
   16768       else if ((opcode & 0xffff0000) == 0xff2f0000)
   16769 	{
   16770 	  gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
   16771 	  gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
   16772 	  gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
   16773 	  gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
   16774 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16775 	  entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
   16776 	  suffixlen = sizeof (microsuffix);
   16777 	  suffix = microsuffix;
   16778 	  other = STO_MICROMIPS;
   16779 	}
   16780       /* Otherwise assume standard MIPS code.  */
   16781       else
   16782 	{
   16783 	  gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
   16784 	  gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
   16785 	  gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
   16786 	  gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
   16787 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16788 	  entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
   16789 	  suffixlen = sizeof (mipssuffix);
   16790 	  suffix = mipssuffix;
   16791 	  other = 0;
   16792 	}
   16793       /* Truncated table???  */
   16794       if (plt_offset + entry_size > plt->size)
   16795 	break;
   16796 
   16797       for (i = 0;
   16798 	   i < count && p[pi].address != gotplt_addr;
   16799 	   i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
   16800 
   16801       if (i < count)
   16802 	{
   16803 	  size_t namelen;
   16804 	  size_t len;
   16805 
   16806 	  *s = **p[pi].sym_ptr_ptr;
   16807 	  /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   16808 	     we are defining a symbol, ensure one of them is set.  */
   16809 	  if ((s->flags & BSF_LOCAL) == 0)
   16810 	    s->flags |= BSF_GLOBAL;
   16811 	  s->flags |= BSF_SYNTHETIC;
   16812 	  s->section = plt;
   16813 	  s->value = plt_offset;
   16814 	  s->name = names;
   16815 	  s->udata.i = other;
   16816 
   16817 	  len = strlen ((*p[pi].sym_ptr_ptr)->name);
   16818 	  namelen = len + suffixlen;
   16819 	  if (names + namelen > nend)
   16820 	    break;
   16821 
   16822 	  memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
   16823 	  names += len;
   16824 	  memcpy (names, suffix, suffixlen);
   16825 	  names += suffixlen;
   16826 
   16827 	  ++s, ++n;
   16828 	  pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
   16829 	}
   16830     }
   16831 
   16832   free (plt_data);
   16833 
   16834   return n;
   16835 }
   16836 
   16837 /* Return the ABI flags associated with ABFD if available.  */
   16838 
   16839 Elf_Internal_ABIFlags_v0 *
   16840 bfd_mips_elf_get_abiflags (bfd *abfd)
   16841 {
   16842   struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
   16843 
   16844   return tdata->abiflags_valid ? &tdata->abiflags : NULL;
   16845 }
   16846 
   16847 /* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
   16848    field.  Taken from `libc-abis.h' generated at GNU libc build time.
   16849    Using a MIPS_ prefix as other libc targets use different values.  */
   16850 enum
   16851 {
   16852   MIPS_LIBC_ABI_DEFAULT = 0,
   16853   MIPS_LIBC_ABI_MIPS_PLT,
   16854   MIPS_LIBC_ABI_UNIQUE,
   16855   MIPS_LIBC_ABI_MIPS_O32_FP64,
   16856   MIPS_LIBC_ABI_ABSOLUTE,
   16857   MIPS_LIBC_ABI_XHASH,
   16858   MIPS_LIBC_ABI_MAX
   16859 };
   16860 
   16861 bool
   16862 _bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
   16863 {
   16864   struct mips_elf_link_hash_table *htab = NULL;
   16865   Elf_Internal_Ehdr *i_ehdrp;
   16866 
   16867   if (!_bfd_elf_init_file_header (abfd, link_info))
   16868     return false;
   16869 
   16870   i_ehdrp = elf_elfheader (abfd);
   16871   if (link_info)
   16872     {
   16873       htab = mips_elf_hash_table (link_info);
   16874       BFD_ASSERT (htab != NULL);
   16875     }
   16876 
   16877   if (htab != NULL
   16878       && htab->use_plts_and_copy_relocs
   16879       && htab->root.target_os != is_vxworks)
   16880     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
   16881 
   16882   if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
   16883       || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
   16884     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
   16885 
   16886   /* Mark that we need support for absolute symbols in the dynamic loader.  */
   16887   if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
   16888     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
   16889 
   16890   /* Mark that we need support for .MIPS.xhash in the dynamic linker,
   16891      if it is the only hash section that will be created.  */
   16892   if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
   16893     i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
   16894   return true;
   16895 }
   16896 
   16897 int
   16898 _bfd_mips_elf_compact_eh_encoding
   16899   (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
   16900 {
   16901   return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
   16902 }
   16903 
   16904 /* Return the opcode for can't unwind.  */
   16905 
   16906 int
   16907 _bfd_mips_elf_cant_unwind_opcode
   16908   (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
   16909 {
   16910   return COMPACT_EH_CANT_UNWIND_OPCODE;
   16911 }
   16912 
   16913 /* Record a position XLAT_LOC in the xlat translation table, associated with
   16914    the hash entry H.  The entry in the translation table will later be
   16915    populated with the real symbol dynindx.  */
   16916 
   16917 void
   16918 _bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
   16919 				   bfd_vma xlat_loc)
   16920 {
   16921   struct mips_elf_link_hash_entry *hmips;
   16922 
   16923   hmips = (struct mips_elf_link_hash_entry *) h;
   16924   hmips->mipsxhash_loc = xlat_loc;
   16925 }
   16926