Home | History | Annotate | Download | only in bfd
      1 /* MIPS-specific support for ELF
      2    Copyright (C) 1993-2014 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 "elfxx-mips.h"
     37 #include "elf/mips.h"
     38 #include "elf-vxworks.h"
     39 
     40 /* Get the ECOFF swapping routines.  */
     41 #include "coff/sym.h"
     42 #include "coff/symconst.h"
     43 #include "coff/ecoff.h"
     44 #include "coff/mips.h"
     45 
     46 #include "hashtab.h"
     47 
     48 /* Types of TLS GOT entry.  */
     49 enum mips_got_tls_type {
     50   GOT_TLS_NONE,
     51   GOT_TLS_GD,
     52   GOT_TLS_LDM,
     53   GOT_TLS_IE
     54 };
     55 
     56 /* This structure is used to hold information about one GOT entry.
     57    There are four types of entry:
     58 
     59       (1) an absolute address
     60 	    requires: abfd == NULL
     61 	    fields: d.address
     62 
     63       (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
     64 	    requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
     65 	    fields: abfd, symndx, d.addend, tls_type
     66 
     67       (3) a SYMBOL address, where SYMBOL is not local to an input bfd
     68 	    requires: abfd != NULL, symndx == -1
     69 	    fields: d.h, tls_type
     70 
     71       (4) a TLS LDM slot
     72 	    requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
     73 	    fields: none; there's only one of these per GOT.  */
     74 struct mips_got_entry
     75 {
     76   /* One input bfd that needs the GOT entry.  */
     77   bfd *abfd;
     78   /* The index of the symbol, as stored in the relocation r_info, if
     79      we have a local symbol; -1 otherwise.  */
     80   long symndx;
     81   union
     82   {
     83     /* If abfd == NULL, an address that must be stored in the got.  */
     84     bfd_vma address;
     85     /* If abfd != NULL && symndx != -1, the addend of the relocation
     86        that should be added to the symbol value.  */
     87     bfd_vma addend;
     88     /* If abfd != NULL && symndx == -1, the hash table entry
     89        corresponding to a symbol in the GOT.  The symbol's entry
     90        is in the local area if h->global_got_area is GGA_NONE,
     91        otherwise it is in the global area.  */
     92     struct mips_elf_link_hash_entry *h;
     93   } d;
     94 
     95   /* The TLS type of this GOT entry.  An LDM GOT entry will be a local
     96      symbol entry with r_symndx == 0.  */
     97   unsigned char tls_type;
     98 
     99   /* True if we have filled in the GOT contents for a TLS entry,
    100      and created the associated relocations.  */
    101   unsigned char tls_initialized;
    102 
    103   /* The offset from the beginning of the .got section to the entry
    104      corresponding to this symbol+addend.  If it's a global symbol
    105      whose offset is yet to be decided, it's going to be -1.  */
    106   long gotidx;
    107 };
    108 
    109 /* This structure represents a GOT page reference from an input bfd.
    110    Each instance represents a symbol + ADDEND, where the representation
    111    of the symbol depends on whether it is local to the input bfd.
    112    If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
    113    Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
    114 
    115    Page references with SYMNDX >= 0 always become page references
    116    in the output.  Page references with SYMNDX < 0 only become page
    117    references if the symbol binds locally; in other cases, the page
    118    reference decays to a global GOT reference.  */
    119 struct mips_got_page_ref
    120 {
    121   long symndx;
    122   union
    123   {
    124     struct mips_elf_link_hash_entry *h;
    125     bfd *abfd;
    126   } u;
    127   bfd_vma addend;
    128 };
    129 
    130 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
    131    The structures form a non-overlapping list that is sorted by increasing
    132    MIN_ADDEND.  */
    133 struct mips_got_page_range
    134 {
    135   struct mips_got_page_range *next;
    136   bfd_signed_vma min_addend;
    137   bfd_signed_vma max_addend;
    138 };
    139 
    140 /* This structure describes the range of addends that are applied to page
    141    relocations against a given section.  */
    142 struct mips_got_page_entry
    143 {
    144   /* The section that these entries are based on.  */
    145   asection *sec;
    146   /* The ranges for this page entry.  */
    147   struct mips_got_page_range *ranges;
    148   /* The maximum number of page entries needed for RANGES.  */
    149   bfd_vma num_pages;
    150 };
    151 
    152 /* This structure is used to hold .got information when linking.  */
    153 
    154 struct mips_got_info
    155 {
    156   /* The number of global .got entries.  */
    157   unsigned int global_gotno;
    158   /* The number of global .got entries that are in the GGA_RELOC_ONLY area.  */
    159   unsigned int reloc_only_gotno;
    160   /* The number of .got slots used for TLS.  */
    161   unsigned int tls_gotno;
    162   /* The first unused TLS .got entry.  Used only during
    163      mips_elf_initialize_tls_index.  */
    164   unsigned int tls_assigned_gotno;
    165   /* The number of local .got entries, eventually including page entries.  */
    166   unsigned int local_gotno;
    167   /* The maximum number of page entries needed.  */
    168   unsigned int page_gotno;
    169   /* The number of relocations needed for the GOT entries.  */
    170   unsigned int relocs;
    171   /* The first unused local .got entry.  */
    172   unsigned int assigned_low_gotno;
    173   /* The last unused local .got entry.  */
    174   unsigned int assigned_high_gotno;
    175   /* A hash table holding members of the got.  */
    176   struct htab *got_entries;
    177   /* A hash table holding mips_got_page_ref structures.  */
    178   struct htab *got_page_refs;
    179   /* A hash table of mips_got_page_entry structures.  */
    180   struct htab *got_page_entries;
    181   /* In multi-got links, a pointer to the next got (err, rather, most
    182      of the time, it points to the previous got).  */
    183   struct mips_got_info *next;
    184 };
    185 
    186 /* Structure passed when merging bfds' gots.  */
    187 
    188 struct mips_elf_got_per_bfd_arg
    189 {
    190   /* The output bfd.  */
    191   bfd *obfd;
    192   /* The link information.  */
    193   struct bfd_link_info *info;
    194   /* A pointer to the primary got, i.e., the one that's going to get
    195      the implicit relocations from DT_MIPS_LOCAL_GOTNO and
    196      DT_MIPS_GOTSYM.  */
    197   struct mips_got_info *primary;
    198   /* A non-primary got we're trying to merge with other input bfd's
    199      gots.  */
    200   struct mips_got_info *current;
    201   /* The maximum number of got entries that can be addressed with a
    202      16-bit offset.  */
    203   unsigned int max_count;
    204   /* The maximum number of page entries needed by each got.  */
    205   unsigned int max_pages;
    206   /* The total number of global entries which will live in the
    207      primary got and be automatically relocated.  This includes
    208      those not referenced by the primary GOT but included in
    209      the "master" GOT.  */
    210   unsigned int global_count;
    211 };
    212 
    213 /* A structure used to pass information to htab_traverse callbacks
    214    when laying out the GOT.  */
    215 
    216 struct mips_elf_traverse_got_arg
    217 {
    218   struct bfd_link_info *info;
    219   struct mips_got_info *g;
    220   int value;
    221 };
    222 
    223 struct _mips_elf_section_data
    224 {
    225   struct bfd_elf_section_data elf;
    226   union
    227   {
    228     bfd_byte *tdata;
    229   } u;
    230 };
    231 
    232 #define mips_elf_section_data(sec) \
    233   ((struct _mips_elf_section_data *) elf_section_data (sec))
    234 
    235 #define is_mips_elf(bfd)				\
    236   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
    237    && elf_tdata (bfd) != NULL				\
    238    && elf_object_id (bfd) == MIPS_ELF_DATA)
    239 
    240 /* The ABI says that every symbol used by dynamic relocations must have
    241    a global GOT entry.  Among other things, this provides the dynamic
    242    linker with a free, directly-indexed cache.  The GOT can therefore
    243    contain symbols that are not referenced by GOT relocations themselves
    244    (in other words, it may have symbols that are not referenced by things
    245    like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
    246 
    247    GOT relocations are less likely to overflow if we put the associated
    248    GOT entries towards the beginning.  We therefore divide the global
    249    GOT entries into two areas: "normal" and "reloc-only".  Entries in
    250    the first area can be used for both dynamic relocations and GP-relative
    251    accesses, while those in the "reloc-only" area are for dynamic
    252    relocations only.
    253 
    254    These GGA_* ("Global GOT Area") values are organised so that lower
    255    values are more general than higher values.  Also, non-GGA_NONE
    256    values are ordered by the position of the area in the GOT.  */
    257 #define GGA_NORMAL 0
    258 #define GGA_RELOC_ONLY 1
    259 #define GGA_NONE 2
    260 
    261 /* Information about a non-PIC interface to a PIC function.  There are
    262    two ways of creating these interfaces.  The first is to add:
    263 
    264 	lui	$25,%hi(func)
    265 	addiu	$25,$25,%lo(func)
    266 
    267    immediately before a PIC function "func".  The second is to add:
    268 
    269 	lui	$25,%hi(func)
    270 	j	func
    271 	addiu	$25,$25,%lo(func)
    272 
    273    to a separate trampoline section.
    274 
    275    Stubs of the first kind go in a new section immediately before the
    276    target function.  Stubs of the second kind go in a single section
    277    pointed to by the hash table's "strampoline" field.  */
    278 struct mips_elf_la25_stub {
    279   /* The generated section that contains this stub.  */
    280   asection *stub_section;
    281 
    282   /* The offset of the stub from the start of STUB_SECTION.  */
    283   bfd_vma offset;
    284 
    285   /* One symbol for the original function.  Its location is available
    286      in H->root.root.u.def.  */
    287   struct mips_elf_link_hash_entry *h;
    288 };
    289 
    290 /* Macros for populating a mips_elf_la25_stub.  */
    291 
    292 #define LA25_LUI(VAL) (0x3c190000 | (VAL))	/* lui t9,VAL */
    293 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
    294 #define LA25_ADDIU(VAL) (0x27390000 | (VAL))	/* addiu t9,t9,VAL */
    295 #define LA25_LUI_MICROMIPS(VAL)						\
    296   (0x41b90000 | (VAL))				/* lui t9,VAL */
    297 #define LA25_J_MICROMIPS(VAL)						\
    298   (0xd4000000 | (((VAL) >> 1) & 0x3ffffff))	/* j VAL */
    299 #define LA25_ADDIU_MICROMIPS(VAL)					\
    300   (0x33390000 | (VAL))				/* addiu t9,t9,VAL */
    301 
    302 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
    303    the dynamic symbols.  */
    304 
    305 struct mips_elf_hash_sort_data
    306 {
    307   /* The symbol in the global GOT with the lowest dynamic symbol table
    308      index.  */
    309   struct elf_link_hash_entry *low;
    310   /* The least dynamic symbol table index corresponding to a non-TLS
    311      symbol with a GOT entry.  */
    312   long min_got_dynindx;
    313   /* The greatest dynamic symbol table index corresponding to a symbol
    314      with a GOT entry that is not referenced (e.g., a dynamic symbol
    315      with dynamic relocations pointing to it from non-primary GOTs).  */
    316   long max_unref_got_dynindx;
    317   /* The greatest dynamic symbol table index not corresponding to a
    318      symbol without a GOT entry.  */
    319   long max_non_got_dynindx;
    320 };
    321 
    322 /* We make up to two PLT entries if needed, one for standard MIPS code
    323    and one for compressed code, either a MIPS16 or microMIPS one.  We
    324    keep a separate record of traditional lazy-binding stubs, for easier
    325    processing.  */
    326 
    327 struct plt_entry
    328 {
    329   /* Traditional SVR4 stub offset, or -1 if none.  */
    330   bfd_vma stub_offset;
    331 
    332   /* Standard PLT entry offset, or -1 if none.  */
    333   bfd_vma mips_offset;
    334 
    335   /* Compressed PLT entry offset, or -1 if none.  */
    336   bfd_vma comp_offset;
    337 
    338   /* The corresponding .got.plt index, or -1 if none.  */
    339   bfd_vma gotplt_index;
    340 
    341   /* Whether we need a standard PLT entry.  */
    342   unsigned int need_mips : 1;
    343 
    344   /* Whether we need a compressed PLT entry.  */
    345   unsigned int need_comp : 1;
    346 };
    347 
    348 /* The MIPS ELF linker needs additional information for each symbol in
    349    the global hash table.  */
    350 
    351 struct mips_elf_link_hash_entry
    352 {
    353   struct elf_link_hash_entry root;
    354 
    355   /* External symbol information.  */
    356   EXTR esym;
    357 
    358   /* The la25 stub we have created for ths symbol, if any.  */
    359   struct mips_elf_la25_stub *la25_stub;
    360 
    361   /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
    362      this symbol.  */
    363   unsigned int possibly_dynamic_relocs;
    364 
    365   /* If there is a stub that 32 bit functions should use to call this
    366      16 bit function, this points to the section containing the stub.  */
    367   asection *fn_stub;
    368 
    369   /* If there is a stub that 16 bit functions should use to call this
    370      32 bit function, this points to the section containing the stub.  */
    371   asection *call_stub;
    372 
    373   /* This is like the call_stub field, but it is used if the function
    374      being called returns a floating point value.  */
    375   asection *call_fp_stub;
    376 
    377   /* The highest GGA_* value that satisfies all references to this symbol.  */
    378   unsigned int global_got_area : 2;
    379 
    380   /* True if all GOT relocations against this symbol are for calls.  This is
    381      a looser condition than no_fn_stub below, because there may be other
    382      non-call non-GOT relocations against the symbol.  */
    383   unsigned int got_only_for_calls : 1;
    384 
    385   /* True if one of the relocations described by possibly_dynamic_relocs
    386      is against a readonly section.  */
    387   unsigned int readonly_reloc : 1;
    388 
    389   /* True if there is a relocation against this symbol that must be
    390      resolved by the static linker (in other words, if the relocation
    391      cannot possibly be made dynamic).  */
    392   unsigned int has_static_relocs : 1;
    393 
    394   /* True if we must not create a .MIPS.stubs entry for this symbol.
    395      This is set, for example, if there are relocations related to
    396      taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
    397      See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
    398   unsigned int no_fn_stub : 1;
    399 
    400   /* Whether we need the fn_stub; this is true if this symbol appears
    401      in any relocs other than a 16 bit call.  */
    402   unsigned int need_fn_stub : 1;
    403 
    404   /* True if this symbol is referenced by branch relocations from
    405      any non-PIC input file.  This is used to determine whether an
    406      la25 stub is required.  */
    407   unsigned int has_nonpic_branches : 1;
    408 
    409   /* Does this symbol need a traditional MIPS lazy-binding stub
    410      (as opposed to a PLT entry)?  */
    411   unsigned int needs_lazy_stub : 1;
    412 
    413   /* Does this symbol resolve to a PLT entry?  */
    414   unsigned int use_plt_entry : 1;
    415 };
    416 
    417 /* MIPS ELF linker hash table.  */
    418 
    419 struct mips_elf_link_hash_table
    420 {
    421   struct elf_link_hash_table root;
    422 
    423   /* The number of .rtproc entries.  */
    424   bfd_size_type procedure_count;
    425 
    426   /* The size of the .compact_rel section (if SGI_COMPAT).  */
    427   bfd_size_type compact_rel_size;
    428 
    429   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
    430      is set to the address of __rld_obj_head as in IRIX5 and IRIX6.  */
    431   bfd_boolean use_rld_obj_head;
    432 
    433   /* The  __rld_map or __rld_obj_head symbol. */
    434   struct elf_link_hash_entry *rld_symbol;
    435 
    436   /* This is set if we see any mips16 stub sections.  */
    437   bfd_boolean mips16_stubs_seen;
    438 
    439   /* True if we can generate copy relocs and PLTs.  */
    440   bfd_boolean use_plts_and_copy_relocs;
    441 
    442   /* True if we can only use 32-bit microMIPS instructions.  */
    443   bfd_boolean insn32;
    444 
    445   /* True if we are targetting R6 compact branches.  */
    446   bfd_boolean compact_branches;
    447 
    448   /* True if we're generating code for VxWorks.  */
    449   bfd_boolean is_vxworks;
    450 
    451   /* True if we already reported the small-data section overflow.  */
    452   bfd_boolean small_data_overflow_reported;
    453 
    454   /* Shortcuts to some dynamic sections, or NULL if they are not
    455      being used.  */
    456   asection *srelbss;
    457   asection *sdynbss;
    458   asection *srelplt;
    459   asection *srelplt2;
    460   asection *sgotplt;
    461   asection *splt;
    462   asection *sstubs;
    463   asection *sgot;
    464 
    465   /* The master GOT information.  */
    466   struct mips_got_info *got_info;
    467 
    468   /* The global symbol in the GOT with the lowest index in the dynamic
    469      symbol table.  */
    470   struct elf_link_hash_entry *global_gotsym;
    471 
    472   /* The size of the PLT header in bytes.  */
    473   bfd_vma plt_header_size;
    474 
    475   /* The size of a standard PLT entry in bytes.  */
    476   bfd_vma plt_mips_entry_size;
    477 
    478   /* The size of a compressed PLT entry in bytes.  */
    479   bfd_vma plt_comp_entry_size;
    480 
    481   /* The offset of the next standard PLT entry to create.  */
    482   bfd_vma plt_mips_offset;
    483 
    484   /* The offset of the next compressed PLT entry to create.  */
    485   bfd_vma plt_comp_offset;
    486 
    487   /* The index of the next .got.plt entry to create.  */
    488   bfd_vma plt_got_index;
    489 
    490   /* The number of functions that need a lazy-binding stub.  */
    491   bfd_vma lazy_stub_count;
    492 
    493   /* The size of a function stub entry in bytes.  */
    494   bfd_vma function_stub_size;
    495 
    496   /* The number of reserved entries at the beginning of the GOT.  */
    497   unsigned int reserved_gotno;
    498 
    499   /* The section used for mips_elf_la25_stub trampolines.
    500      See the comment above that structure for details.  */
    501   asection *strampoline;
    502 
    503   /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
    504      pairs.  */
    505   htab_t la25_stubs;
    506 
    507   /* A function FN (NAME, IS, OS) that creates a new input section
    508      called NAME and links it to output section OS.  If IS is nonnull,
    509      the new section should go immediately before it, otherwise it
    510      should go at the (current) beginning of OS.
    511 
    512      The function returns the new section on success, otherwise it
    513      returns null.  */
    514   asection *(*add_stub_section) (const char *, asection *, asection *);
    515 
    516   /* Small local sym cache.  */
    517   struct sym_cache sym_cache;
    518 
    519   /* Is the PLT header compressed?  */
    520   unsigned int plt_header_is_comp : 1;
    521 };
    522 
    523 /* Get the MIPS ELF linker hash table from a link_info structure.  */
    524 
    525 #define mips_elf_hash_table(p) \
    526   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
    527   == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
    528 
    529 /* A structure used to communicate with htab_traverse callbacks.  */
    530 struct mips_htab_traverse_info
    531 {
    532   /* The usual link-wide information.  */
    533   struct bfd_link_info *info;
    534   bfd *output_bfd;
    535 
    536   /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
    537   bfd_boolean error;
    538 };
    539 
    540 /* MIPS ELF private object data.  */
    541 
    542 struct mips_elf_obj_tdata
    543 {
    544   /* Generic ELF private object data.  */
    545   struct elf_obj_tdata root;
    546 
    547   /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output.  */
    548   bfd *abi_fp_bfd;
    549 
    550   /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output.  */
    551   bfd *abi_msa_bfd;
    552 
    553   /* The abiflags for this object.  */
    554   Elf_Internal_ABIFlags_v0 abiflags;
    555   bfd_boolean abiflags_valid;
    556 
    557   /* The GOT requirements of input bfds.  */
    558   struct mips_got_info *got;
    559 
    560   /* Used by _bfd_mips_elf_find_nearest_line.  The structure could be
    561      included directly in this one, but there's no point to wasting
    562      the memory just for the infrequently called find_nearest_line.  */
    563   struct mips_elf_find_line *find_line_info;
    564 
    565   /* An array of stub sections indexed by symbol number.  */
    566   asection **local_stubs;
    567   asection **local_call_stubs;
    568 
    569   /* The Irix 5 support uses two virtual sections, which represent
    570      text/data symbols defined in dynamic objects.  */
    571   asymbol *elf_data_symbol;
    572   asymbol *elf_text_symbol;
    573   asection *elf_data_section;
    574   asection *elf_text_section;
    575 };
    576 
    577 /* Get MIPS ELF private object data from BFD's tdata.  */
    578 
    579 #define mips_elf_tdata(bfd) \
    580   ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
    581 
    582 #define TLS_RELOC_P(r_type) \
    583   (r_type == R_MIPS_TLS_DTPMOD32		\
    584    || r_type == R_MIPS_TLS_DTPMOD64		\
    585    || r_type == R_MIPS_TLS_DTPREL32		\
    586    || r_type == R_MIPS_TLS_DTPREL64		\
    587    || r_type == R_MIPS_TLS_GD			\
    588    || r_type == R_MIPS_TLS_LDM			\
    589    || r_type == R_MIPS_TLS_DTPREL_HI16		\
    590    || r_type == R_MIPS_TLS_DTPREL_LO16		\
    591    || r_type == R_MIPS_TLS_GOTTPREL		\
    592    || r_type == R_MIPS_TLS_TPREL32		\
    593    || r_type == R_MIPS_TLS_TPREL64		\
    594    || r_type == R_MIPS_TLS_TPREL_HI16		\
    595    || r_type == R_MIPS_TLS_TPREL_LO16		\
    596    || r_type == R_MIPS16_TLS_GD			\
    597    || r_type == R_MIPS16_TLS_LDM		\
    598    || r_type == R_MIPS16_TLS_DTPREL_HI16	\
    599    || r_type == R_MIPS16_TLS_DTPREL_LO16	\
    600    || r_type == R_MIPS16_TLS_GOTTPREL		\
    601    || r_type == R_MIPS16_TLS_TPREL_HI16		\
    602    || r_type == R_MIPS16_TLS_TPREL_LO16		\
    603    || r_type == R_MICROMIPS_TLS_GD		\
    604    || r_type == R_MICROMIPS_TLS_LDM		\
    605    || r_type == R_MICROMIPS_TLS_DTPREL_HI16	\
    606    || r_type == R_MICROMIPS_TLS_DTPREL_LO16	\
    607    || r_type == R_MICROMIPS_TLS_GOTTPREL	\
    608    || r_type == R_MICROMIPS_TLS_TPREL_HI16	\
    609    || r_type == R_MICROMIPS_TLS_TPREL_LO16)
    610 
    611 /* Structure used to pass information to mips_elf_output_extsym.  */
    612 
    613 struct extsym_info
    614 {
    615   bfd *abfd;
    616   struct bfd_link_info *info;
    617   struct ecoff_debug_info *debug;
    618   const struct ecoff_debug_swap *swap;
    619   bfd_boolean failed;
    620 };
    621 
    622 /* The names of the runtime procedure table symbols used on IRIX5.  */
    623 
    624 static const char * const mips_elf_dynsym_rtproc_names[] =
    625 {
    626   "_procedure_table",
    627   "_procedure_string_table",
    628   "_procedure_table_size",
    629   NULL
    630 };
    631 
    632 /* These structures are used to generate the .compact_rel section on
    633    IRIX5.  */
    634 
    635 typedef struct
    636 {
    637   unsigned long id1;		/* Always one?  */
    638   unsigned long num;		/* Number of compact relocation entries.  */
    639   unsigned long id2;		/* Always two?  */
    640   unsigned long offset;		/* The file offset of the first relocation.  */
    641   unsigned long reserved0;	/* Zero?  */
    642   unsigned long reserved1;	/* Zero?  */
    643 } Elf32_compact_rel;
    644 
    645 typedef struct
    646 {
    647   bfd_byte id1[4];
    648   bfd_byte num[4];
    649   bfd_byte id2[4];
    650   bfd_byte offset[4];
    651   bfd_byte reserved0[4];
    652   bfd_byte reserved1[4];
    653 } Elf32_External_compact_rel;
    654 
    655 typedef struct
    656 {
    657   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
    658   unsigned int rtype : 4;	/* Relocation types. See below.  */
    659   unsigned int dist2to : 8;
    660   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
    661   unsigned long konst;		/* KONST field. See below.  */
    662   unsigned long vaddr;		/* VADDR to be relocated.  */
    663 } Elf32_crinfo;
    664 
    665 typedef struct
    666 {
    667   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
    668   unsigned int rtype : 4;	/* Relocation types. See below.  */
    669   unsigned int dist2to : 8;
    670   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
    671   unsigned long konst;		/* KONST field. See below.  */
    672 } Elf32_crinfo2;
    673 
    674 typedef struct
    675 {
    676   bfd_byte info[4];
    677   bfd_byte konst[4];
    678   bfd_byte vaddr[4];
    679 } Elf32_External_crinfo;
    680 
    681 typedef struct
    682 {
    683   bfd_byte info[4];
    684   bfd_byte konst[4];
    685 } Elf32_External_crinfo2;
    686 
    687 /* These are the constants used to swap the bitfields in a crinfo.  */
    688 
    689 #define CRINFO_CTYPE (0x1)
    690 #define CRINFO_CTYPE_SH (31)
    691 #define CRINFO_RTYPE (0xf)
    692 #define CRINFO_RTYPE_SH (27)
    693 #define CRINFO_DIST2TO (0xff)
    694 #define CRINFO_DIST2TO_SH (19)
    695 #define CRINFO_RELVADDR (0x7ffff)
    696 #define CRINFO_RELVADDR_SH (0)
    697 
    698 /* A compact relocation info has long (3 words) or short (2 words)
    699    formats.  A short format doesn't have VADDR field and relvaddr
    700    fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
    701 #define CRF_MIPS_LONG			1
    702 #define CRF_MIPS_SHORT			0
    703 
    704 /* There are 4 types of compact relocation at least. The value KONST
    705    has different meaning for each type:
    706 
    707    (type)		(konst)
    708    CT_MIPS_REL32	Address in data
    709    CT_MIPS_WORD		Address in word (XXX)
    710    CT_MIPS_GPHI_LO	GP - vaddr
    711    CT_MIPS_JMPAD	Address to jump
    712    */
    713 
    714 #define CRT_MIPS_REL32			0xa
    715 #define CRT_MIPS_WORD			0xb
    716 #define CRT_MIPS_GPHI_LO		0xc
    717 #define CRT_MIPS_JMPAD			0xd
    718 
    719 #define mips_elf_set_cr_format(x,format)	((x).ctype = (format))
    720 #define mips_elf_set_cr_type(x,type)		((x).rtype = (type))
    721 #define mips_elf_set_cr_dist2to(x,v)		((x).dist2to = (v))
    722 #define mips_elf_set_cr_relvaddr(x,d)		((x).relvaddr = (d)<<2)
    723 
    724 /* The structure of the runtime procedure descriptor created by the
    726    loader for use by the static exception system.  */
    727 
    728 typedef struct runtime_pdr {
    729 	bfd_vma	adr;		/* Memory address of start of procedure.  */
    730 	long	regmask;	/* Save register mask.  */
    731 	long	regoffset;	/* Save register offset.  */
    732 	long	fregmask;	/* Save floating point register mask.  */
    733 	long	fregoffset;	/* Save floating point register offset.  */
    734 	long	frameoffset;	/* Frame size.  */
    735 	short	framereg;	/* Frame pointer register.  */
    736 	short	pcreg;		/* Offset or reg of return pc.  */
    737 	long	irpss;		/* Index into the runtime string table.  */
    738 	long	reserved;
    739 	struct exception_info *exception_info;/* Pointer to exception array.  */
    740 } RPDR, *pRPDR;
    741 #define cbRPDR sizeof (RPDR)
    742 #define rpdNil ((pRPDR) 0)
    743 
    744 static struct mips_got_entry *mips_elf_create_local_got_entry
    746   (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
    747    struct mips_elf_link_hash_entry *, int);
    748 static bfd_boolean mips_elf_sort_hash_table_f
    749   (struct mips_elf_link_hash_entry *, void *);
    750 static bfd_vma mips_elf_high
    751   (bfd_vma);
    752 static bfd_boolean mips_elf_create_dynamic_relocation
    753   (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
    754    struct mips_elf_link_hash_entry *, asection *, bfd_vma,
    755    bfd_vma *, asection *);
    756 static bfd_vma mips_elf_adjust_gp
    757   (bfd *, struct mips_got_info *, bfd *);
    758 
    759 /* This will be used when we sort the dynamic relocation records.  */
    760 static bfd *reldyn_sorting_bfd;
    761 
    762 /* True if ABFD is for CPUs with load interlocking that include
    763    non-MIPS1 CPUs and R3900.  */
    764 #define LOAD_INTERLOCKS_P(abfd) \
    765   (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
    766    || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
    767 
    768 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
    769    This should be safe for all architectures.  We enable this predicate
    770    for RM9000 for now.  */
    771 #define JAL_TO_BAL_P(abfd) \
    772   ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
    773 
    774 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
    775    This should be safe for all architectures.  We enable this predicate for
    776    all CPUs.  */
    777 #define JALR_TO_BAL_P(abfd) 1
    778 
    779 /* True if ABFD is for CPUs that are faster if JR is converted to B.
    780    This should be safe for all architectures.  We enable this predicate for
    781    all CPUs.  */
    782 #define JR_TO_B_P(abfd) 1
    783 
    784 /* True if ABFD is a PIC object.  */
    785 #define PIC_OBJECT_P(abfd) \
    786   ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
    787 
    788 /* Nonzero if ABFD is using the O32 ABI.  */
    789 #define ABI_O32_P(abfd) \
    790   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
    791 
    792 /* Nonzero if ABFD is using the N32 ABI.  */
    793 #define ABI_N32_P(abfd) \
    794   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
    795 
    796 /* Nonzero if ABFD is using the N64 ABI.  */
    797 #define ABI_64_P(abfd) \
    798   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
    799 
    800 /* Nonzero if ABFD is using NewABI conventions.  */
    801 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
    802 
    803 /* Nonzero if ABFD has microMIPS code.  */
    804 #define MICROMIPS_P(abfd) \
    805   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
    806 
    807 /* Nonzero if ABFD is MIPS R6.  */
    808 #define MIPSR6_P(abfd) \
    809   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
    810     || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
    811 
    812 /* The IRIX compatibility level we are striving for.  */
    813 #define IRIX_COMPAT(abfd) \
    814   (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
    815 
    816 /* Whether we are trying to be compatible with IRIX at all.  */
    817 #define SGI_COMPAT(abfd) \
    818   (IRIX_COMPAT (abfd) != ict_none)
    819 
    820 /* The name of the options section.  */
    821 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
    822   (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
    823 
    824 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
    825    Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
    826 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
    827   (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
    828 
    829 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section.  */
    830 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
    831   (strcmp (NAME, ".MIPS.abiflags") == 0)
    832 
    833 /* Whether the section is readonly.  */
    834 #define MIPS_ELF_READONLY_SECTION(sec) \
    835   ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))		\
    836    == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
    837 
    838 /* The name of the stub section.  */
    839 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
    840 
    841 /* The size of an external REL relocation.  */
    842 #define MIPS_ELF_REL_SIZE(abfd) \
    843   (get_elf_backend_data (abfd)->s->sizeof_rel)
    844 
    845 /* The size of an external RELA relocation.  */
    846 #define MIPS_ELF_RELA_SIZE(abfd) \
    847   (get_elf_backend_data (abfd)->s->sizeof_rela)
    848 
    849 /* The size of an external dynamic table entry.  */
    850 #define MIPS_ELF_DYN_SIZE(abfd) \
    851   (get_elf_backend_data (abfd)->s->sizeof_dyn)
    852 
    853 /* The size of a GOT entry.  */
    854 #define MIPS_ELF_GOT_SIZE(abfd) \
    855   (get_elf_backend_data (abfd)->s->arch_size / 8)
    856 
    857 /* The size of the .rld_map section. */
    858 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
    859   (get_elf_backend_data (abfd)->s->arch_size / 8)
    860 
    861 /* The size of a symbol-table entry.  */
    862 #define MIPS_ELF_SYM_SIZE(abfd) \
    863   (get_elf_backend_data (abfd)->s->sizeof_sym)
    864 
    865 /* The default alignment for sections, as a power of two.  */
    866 #define MIPS_ELF_LOG_FILE_ALIGN(abfd)				\
    867   (get_elf_backend_data (abfd)->s->log_file_align)
    868 
    869 /* Get word-sized data.  */
    870 #define MIPS_ELF_GET_WORD(abfd, ptr) \
    871   (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
    872 
    873 /* Put out word-sized data.  */
    874 #define MIPS_ELF_PUT_WORD(abfd, val, ptr)	\
    875   (ABI_64_P (abfd) 				\
    876    ? bfd_put_64 (abfd, val, ptr) 		\
    877    : bfd_put_32 (abfd, val, ptr))
    878 
    879 /* The opcode for word-sized loads (LW or LD).  */
    880 #define MIPS_ELF_LOAD_WORD(abfd) \
    881   (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
    882 
    883 /* Add a dynamic symbol table-entry.  */
    884 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)	\
    885   _bfd_elf_add_dynamic_entry (info, tag, val)
    886 
    887 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)			\
    888   (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
    889 
    890 /* The name of the dynamic relocation section.  */
    891 #define MIPS_ELF_REL_DYN_NAME(INFO) \
    892   (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
    893 
    894 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
    895    from smaller values.  Start with zero, widen, *then* decrement.  */
    896 #define MINUS_ONE	(((bfd_vma)0) - 1)
    897 #define MINUS_TWO	(((bfd_vma)0) - 2)
    898 
    899 /* The value to write into got[1] for SVR4 targets, to identify it is
    900    a GNU object.  The dynamic linker can then use got[1] to store the
    901    module pointer.  */
    902 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
    903   ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
    904 
    905 /* The offset of $gp from the beginning of the .got section.  */
    906 #define ELF_MIPS_GP_OFFSET(INFO) \
    907   (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
    908 
    909 /* The maximum size of the GOT for it to be addressable using 16-bit
    910    offsets from $gp.  */
    911 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
    912 
    913 /* Instructions which appear in a stub.  */
    914 #define STUB_LW(abfd)							\
    915   ((ABI_64_P (abfd)							\
    916     ? 0xdf998010				/* ld t9,0x8010(gp) */	\
    917     : 0x8f998010))              		/* lw t9,0x8010(gp) */
    918 #define STUB_MOVE(abfd)							\
    919    ((ABI_64_P (abfd)							\
    920      ? 0x03e0782d				/* daddu t7,ra */	\
    921      : 0x03e07821))				/* addu t7,ra */
    922 #define STUB_LUI(VAL) (0x3c180000 + (VAL))	/* lui t8,VAL */
    923 #define STUB_JALR 0x0320f809			/* jalr t9,ra */
    924 #define STUB_ORI(VAL) (0x37180000 + (VAL))	/* ori t8,t8,VAL */
    925 #define STUB_LI16U(VAL) (0x34180000 + (VAL))	/* ori t8,zero,VAL unsigned */
    926 #define STUB_LI16S(abfd, VAL)						\
    927    ((ABI_64_P (abfd)							\
    928     ? (0x64180000 + (VAL))	/* daddiu t8,zero,VAL sign extended */	\
    929     : (0x24180000 + (VAL))))	/* addiu t8,zero,VAL sign extended */
    930 
    931 /* Likewise for the microMIPS ASE.  */
    932 #define STUB_LW_MICROMIPS(abfd)						\
    933   (ABI_64_P (abfd)							\
    934    ? 0xdf3c8010					/* ld t9,0x8010(gp) */	\
    935    : 0xff3c8010)				/* lw t9,0x8010(gp) */
    936 #define STUB_MOVE_MICROMIPS 0x0dff		/* move t7,ra */
    937 #define STUB_MOVE32_MICROMIPS(abfd)					\
    938    (ABI_64_P (abfd)							\
    939     ? 0x581f7950				/* daddu t7,ra,zero */	\
    940     : 0x001f7950)				/* addu t7,ra,zero */
    941 #define STUB_LUI_MICROMIPS(VAL)						\
    942    (0x41b80000 + (VAL))				/* lui t8,VAL */
    943 #define STUB_JALR_MICROMIPS 0x45d9		/* jalr t9 */
    944 #define STUB_JALR32_MICROMIPS 0x03f90f3c	/* jalr ra,t9 */
    945 #define STUB_ORI_MICROMIPS(VAL)						\
    946   (0x53180000 + (VAL))				/* ori t8,t8,VAL */
    947 #define STUB_LI16U_MICROMIPS(VAL)					\
    948   (0x53000000 + (VAL))				/* ori t8,zero,VAL unsigned */
    949 #define STUB_LI16S_MICROMIPS(abfd, VAL)					\
    950    (ABI_64_P (abfd)							\
    951     ? 0x5f000000 + (VAL)	/* daddiu t8,zero,VAL sign extended */	\
    952     : 0x33000000 + (VAL))	/* addiu t8,zero,VAL sign extended */
    953 
    954 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
    955 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
    956 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
    957 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
    958 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
    959 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
    960 
    961 /* The name of the dynamic interpreter.  This is put in the .interp
    962    section.  */
    963 
    964 #define ELF_DYNAMIC_INTERPRETER(abfd) 		\
    965    (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" 	\
    966     : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" 	\
    967     : "/usr/lib/libc.so.1")
    968 
    969 #ifdef BFD64
    970 #define MNAME(bfd,pre,pos) \
    971   (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
    972 #define ELF_R_SYM(bfd, i)					\
    973   (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
    974 #define ELF_R_TYPE(bfd, i)					\
    975   (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
    976 #define ELF_R_INFO(bfd, s, t)					\
    977   (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
    978 #else
    979 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
    980 #define ELF_R_SYM(bfd, i)					\
    981   (ELF32_R_SYM (i))
    982 #define ELF_R_TYPE(bfd, i)					\
    983   (ELF32_R_TYPE (i))
    984 #define ELF_R_INFO(bfd, s, t)					\
    985   (ELF32_R_INFO (s, t))
    986 #endif
    987 
    988   /* The mips16 compiler uses a couple of special sections to handle
    990      floating point arguments.
    991 
    992      Section names that look like .mips16.fn.FNNAME contain stubs that
    993      copy floating point arguments from the fp regs to the gp regs and
    994      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
    995      call should be redirected to the stub instead.  If no 32 bit
    996      function calls FNNAME, the stub should be discarded.  We need to
    997      consider any reference to the function, not just a call, because
    998      if the address of the function is taken we will need the stub,
    999      since the address might be passed to a 32 bit function.
   1000 
   1001      Section names that look like .mips16.call.FNNAME contain stubs
   1002      that copy floating point arguments from the gp regs to the fp
   1003      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
   1004      then any 16 bit function that calls FNNAME should be redirected
   1005      to the stub instead.  If FNNAME is not a 32 bit function, the
   1006      stub should be discarded.
   1007 
   1008      .mips16.call.fp.FNNAME sections are similar, but contain stubs
   1009      which call FNNAME and then copy the return value from the fp regs
   1010      to the gp regs.  These stubs store the return value in $18 while
   1011      calling FNNAME; any function which might call one of these stubs
   1012      must arrange to save $18 around the call.  (This case is not
   1013      needed for 32 bit functions that call 16 bit functions, because
   1014      16 bit functions always return floating point values in both
   1015      $f0/$f1 and $2/$3.)
   1016 
   1017      Note that in all cases FNNAME might be defined statically.
   1018      Therefore, FNNAME is not used literally.  Instead, the relocation
   1019      information will indicate which symbol the section is for.
   1020 
   1021      We record any stubs that we find in the symbol table.  */
   1022 
   1023 #define FN_STUB ".mips16.fn."
   1024 #define CALL_STUB ".mips16.call."
   1025 #define CALL_FP_STUB ".mips16.call.fp."
   1026 
   1027 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
   1028 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
   1029 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
   1030 
   1031 /* The format of the first PLT entry in an O32 executable.  */
   1033 static const bfd_vma mips_o32_exec_plt0_entry[] =
   1034 {
   1035   0x3c1c0000,	/* lui $28, %hi(&GOTPLT[0])				*/
   1036   0x8f990000,	/* lw $25, %lo(&GOTPLT[0])($28)				*/
   1037   0x279c0000,	/* addiu $28, $28, %lo(&GOTPLT[0])			*/
   1038   0x031cc023,	/* subu $24, $24, $28					*/
   1039   0x03e07821,	/* move $15, $31	# 32-bit move (addu)		*/
   1040   0x0018c082,	/* srl $24, $24, 2					*/
   1041   0x0320f809,	/* jalr $25						*/
   1042   0x2718fffe	/* subu $24, $24, 2					*/
   1043 };
   1044 
   1045 /* The format of the first PLT entry in an N32 executable.  Different
   1046    because gp ($28) is not available; we use t2 ($14) instead.  */
   1047 static const bfd_vma mips_n32_exec_plt0_entry[] =
   1048 {
   1049   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1050   0x8dd90000,	/* lw $25, %lo(&GOTPLT[0])($14)				*/
   1051   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1052   0x030ec023,	/* subu $24, $24, $14					*/
   1053   0x03e07821,	/* move $15, $31	# 32-bit move (addu)		*/
   1054   0x0018c082,	/* srl $24, $24, 2					*/
   1055   0x0320f809,	/* jalr $25						*/
   1056   0x2718fffe	/* subu $24, $24, 2					*/
   1057 };
   1058 
   1059 /* The format of the first PLT entry in an N64 executable.  Different
   1060    from N32 because of the increased size of GOT entries.  */
   1061 static const bfd_vma mips_n64_exec_plt0_entry[] =
   1062 {
   1063   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1064   0xddd90000,	/* ld $25, %lo(&GOTPLT[0])($14)				*/
   1065   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1066   0x030ec023,	/* subu $24, $24, $14					*/
   1067   0x03e0782d,	/* move $15, $31	# 64-bit move (daddu)		*/
   1068   0x0018c0c2,	/* srl $24, $24, 3					*/
   1069   0x0320f809,	/* jalr $25						*/
   1070   0x2718fffe	/* subu $24, $24, 2					*/
   1071 };
   1072 
   1073 /* The format of the microMIPS first PLT entry in an O32 executable.
   1074    We rely on v0 ($2) rather than t8 ($24) to contain the address
   1075    of the GOTPLT entry handled, so this stub may only be used when
   1076    all the subsequent PLT entries are microMIPS code too.
   1077 
   1078    The trailing NOP is for alignment and correct disassembly only.  */
   1079 static const bfd_vma micromips_o32_exec_plt0_entry[] =
   1080 {
   1081   0x7980, 0x0000,	/* addiupc $3, (&GOTPLT[0]) - .			*/
   1082   0xff23, 0x0000,	/* lw $25, 0($3)				*/
   1083   0x0535,		/* subu $2, $2, $3				*/
   1084   0x2525,		/* srl $2, $2, 2				*/
   1085   0x3302, 0xfffe,	/* subu $24, $2, 2				*/
   1086   0x0dff,		/* move $15, $31				*/
   1087   0x45f9,		/* jalrs $25					*/
   1088   0x0f83,		/* move $28, $3					*/
   1089   0x0c00		/* nop						*/
   1090 };
   1091 
   1092 /* The format of the microMIPS first PLT entry in an O32 executable
   1093    in the insn32 mode.  */
   1094 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
   1095 {
   1096   0x41bc, 0x0000,	/* lui $28, %hi(&GOTPLT[0])			*/
   1097   0xff3c, 0x0000,	/* lw $25, %lo(&GOTPLT[0])($28)			*/
   1098   0x339c, 0x0000,	/* addiu $28, $28, %lo(&GOTPLT[0])		*/
   1099   0x0398, 0xc1d0,	/* subu $24, $24, $28				*/
   1100   0x001f, 0x7950,	/* move $15, $31				*/
   1101   0x0318, 0x1040,	/* srl $24, $24, 2				*/
   1102   0x03f9, 0x0f3c,	/* jalr $25					*/
   1103   0x3318, 0xfffe	/* subu $24, $24, 2				*/
   1104 };
   1105 
   1106 /* The format of subsequent standard PLT entries.  */
   1107 static const bfd_vma mips_exec_plt_entry[] =
   1108 {
   1109   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
   1110   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
   1111   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
   1112   0x03200008	/* jr $25					*/
   1113 };
   1114 
   1115 static const bfd_vma mipsr6_exec_plt_entry[] =
   1116 {
   1117   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
   1118   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
   1119   0x03200009,	/* jr $25					*/
   1120   0x25f80000	/* addiu $24, $15, %lo(.got.plt entry)		*/
   1121 };
   1122 
   1123 static const bfd_vma mipsr6_exec_plt_entry_compact[] =
   1124 {
   1125   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
   1126   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
   1127   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
   1128   0xd8190000	/* jic $25, 0					*/
   1129 };
   1130 
   1131 /* The format of subsequent MIPS16 o32 PLT entries.  We use v0 ($2)
   1132    and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
   1133    directly addressable.  */
   1134 static const bfd_vma mips16_o32_exec_plt_entry[] =
   1135 {
   1136   0xb203,		/* lw $2, 12($pc)			*/
   1137   0x9a60,		/* lw $3, 0($2)				*/
   1138   0x651a,		/* move $24, $2				*/
   1139   0xeb00,		/* jr $3				*/
   1140   0x653b,		/* move $25, $3				*/
   1141   0x6500,		/* nop					*/
   1142   0x0000, 0x0000	/* .word (.got.plt entry)		*/
   1143 };
   1144 
   1145 /* The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
   1146    as a temporary because t8 ($24) is not addressable with ADDIUPC.  */
   1147 static const bfd_vma micromips_o32_exec_plt_entry[] =
   1148 {
   1149   0x7900, 0x0000,	/* addiupc $2, (.got.plt entry) - .	*/
   1150   0xff22, 0x0000,	/* lw $25, 0($2)			*/
   1151   0x4599,		/* jr $25				*/
   1152   0x0f02		/* move $24, $2				*/
   1153 };
   1154 
   1155 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode.  */
   1156 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
   1157 {
   1158   0x41af, 0x0000,	/* lui $15, %hi(.got.plt entry)		*/
   1159   0xff2f, 0x0000,	/* lw $25, %lo(.got.plt entry)($15)	*/
   1160   0x0019, 0x0f3c,	/* jr $25				*/
   1161   0x330f, 0x0000	/* addiu $24, $15, %lo(.got.plt entry)	*/
   1162 };
   1163 
   1164 /* The format of the first PLT entry in a VxWorks executable.  */
   1165 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
   1166 {
   1167   0x3c190000,	/* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)		*/
   1168   0x27390000,	/* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_)	*/
   1169   0x8f390008,	/* lw t9, 8(t9)					*/
   1170   0x00000000,	/* nop						*/
   1171   0x03200008,	/* jr t9					*/
   1172   0x00000000	/* nop						*/
   1173 };
   1174 
   1175 /* The format of subsequent PLT entries.  */
   1176 static const bfd_vma mips_vxworks_exec_plt_entry[] =
   1177 {
   1178   0x10000000,	/* b .PLT_resolver			*/
   1179   0x24180000,	/* li t8, <pltindex>			*/
   1180   0x3c190000,	/* lui t9, %hi(<.got.plt slot>)		*/
   1181   0x27390000,	/* addiu t9, t9, %lo(<.got.plt slot>)	*/
   1182   0x8f390000,	/* lw t9, 0(t9)				*/
   1183   0x00000000,	/* nop					*/
   1184   0x03200008,	/* jr t9				*/
   1185   0x00000000	/* nop					*/
   1186 };
   1187 
   1188 /* The format of the first PLT entry in a VxWorks shared object.  */
   1189 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
   1190 {
   1191   0x8f990008,	/* lw t9, 8(gp)		*/
   1192   0x00000000,	/* nop			*/
   1193   0x03200008,	/* jr t9		*/
   1194   0x00000000,	/* nop			*/
   1195   0x00000000,	/* nop			*/
   1196   0x00000000	/* nop			*/
   1197 };
   1198 
   1199 /* The format of subsequent PLT entries.  */
   1200 static const bfd_vma mips_vxworks_shared_plt_entry[] =
   1201 {
   1202   0x10000000,	/* b .PLT_resolver	*/
   1203   0x24180000	/* li t8, <pltindex>	*/
   1204 };
   1205 
   1206 /* microMIPS 32-bit opcode helper installer.  */
   1208 
   1209 static void
   1210 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
   1211 {
   1212   bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
   1213   bfd_put_16 (abfd,  opcode        & 0xffff, ptr + 2);
   1214 }
   1215 
   1216 /* microMIPS 32-bit opcode helper retriever.  */
   1217 
   1218 static bfd_vma
   1219 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
   1220 {
   1221   return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
   1222 }
   1223 
   1224 /* Look up an entry in a MIPS ELF linker hash table.  */
   1226 
   1227 #define mips_elf_link_hash_lookup(table, string, create, copy, follow)	\
   1228   ((struct mips_elf_link_hash_entry *)					\
   1229    elf_link_hash_lookup (&(table)->root, (string), (create),		\
   1230 			 (copy), (follow)))
   1231 
   1232 /* Traverse a MIPS ELF linker hash table.  */
   1233 
   1234 #define mips_elf_link_hash_traverse(table, func, info)			\
   1235   (elf_link_hash_traverse						\
   1236    (&(table)->root,							\
   1237     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),	\
   1238     (info)))
   1239 
   1240 /* Find the base offsets for thread-local storage in this object,
   1241    for GD/LD and IE/LE respectively.  */
   1242 
   1243 #define TP_OFFSET 0x7000
   1244 #define DTP_OFFSET 0x8000
   1245 
   1246 static bfd_vma
   1247 dtprel_base (struct bfd_link_info *info)
   1248 {
   1249   /* If tls_sec is NULL, we should have signalled an error already.  */
   1250   if (elf_hash_table (info)->tls_sec == NULL)
   1251     return 0;
   1252   return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
   1253 }
   1254 
   1255 static bfd_vma
   1256 tprel_base (struct bfd_link_info *info)
   1257 {
   1258   /* If tls_sec is NULL, we should have signalled an error already.  */
   1259   if (elf_hash_table (info)->tls_sec == NULL)
   1260     return 0;
   1261   return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
   1262 }
   1263 
   1264 /* Create an entry in a MIPS ELF linker hash table.  */
   1265 
   1266 static struct bfd_hash_entry *
   1267 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   1268 			    struct bfd_hash_table *table, const char *string)
   1269 {
   1270   struct mips_elf_link_hash_entry *ret =
   1271     (struct mips_elf_link_hash_entry *) entry;
   1272 
   1273   /* Allocate the structure if it has not already been allocated by a
   1274      subclass.  */
   1275   if (ret == NULL)
   1276     ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
   1277   if (ret == NULL)
   1278     return (struct bfd_hash_entry *) ret;
   1279 
   1280   /* Call the allocation method of the superclass.  */
   1281   ret = ((struct mips_elf_link_hash_entry *)
   1282 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
   1283 				     table, string));
   1284   if (ret != NULL)
   1285     {
   1286       /* Set local fields.  */
   1287       memset (&ret->esym, 0, sizeof (EXTR));
   1288       /* We use -2 as a marker to indicate that the information has
   1289 	 not been set.  -1 means there is no associated ifd.  */
   1290       ret->esym.ifd = -2;
   1291       ret->la25_stub = 0;
   1292       ret->possibly_dynamic_relocs = 0;
   1293       ret->fn_stub = NULL;
   1294       ret->call_stub = NULL;
   1295       ret->call_fp_stub = NULL;
   1296       ret->global_got_area = GGA_NONE;
   1297       ret->got_only_for_calls = TRUE;
   1298       ret->readonly_reloc = FALSE;
   1299       ret->has_static_relocs = FALSE;
   1300       ret->no_fn_stub = FALSE;
   1301       ret->need_fn_stub = FALSE;
   1302       ret->has_nonpic_branches = FALSE;
   1303       ret->needs_lazy_stub = FALSE;
   1304       ret->use_plt_entry = FALSE;
   1305     }
   1306 
   1307   return (struct bfd_hash_entry *) ret;
   1308 }
   1309 
   1310 /* Allocate MIPS ELF private object data.  */
   1311 
   1312 bfd_boolean
   1313 _bfd_mips_elf_mkobject (bfd *abfd)
   1314 {
   1315   return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
   1316 				  MIPS_ELF_DATA);
   1317 }
   1318 
   1319 bfd_boolean
   1320 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
   1321 {
   1322   if (!sec->used_by_bfd)
   1323     {
   1324       struct _mips_elf_section_data *sdata;
   1325       bfd_size_type amt = sizeof (*sdata);
   1326 
   1327       sdata = bfd_zalloc (abfd, amt);
   1328       if (sdata == NULL)
   1329 	return FALSE;
   1330       sec->used_by_bfd = sdata;
   1331     }
   1332 
   1333   return _bfd_elf_new_section_hook (abfd, sec);
   1334 }
   1335 
   1336 /* Read ECOFF debugging information from a .mdebug section into a
   1338    ecoff_debug_info structure.  */
   1339 
   1340 bfd_boolean
   1341 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
   1342 			       struct ecoff_debug_info *debug)
   1343 {
   1344   HDRR *symhdr;
   1345   const struct ecoff_debug_swap *swap;
   1346   char *ext_hdr;
   1347 
   1348   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   1349   memset (debug, 0, sizeof (*debug));
   1350 
   1351   ext_hdr = bfd_malloc (swap->external_hdr_size);
   1352   if (ext_hdr == NULL && swap->external_hdr_size != 0)
   1353     goto error_return;
   1354 
   1355   if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
   1356 				  swap->external_hdr_size))
   1357     goto error_return;
   1358 
   1359   symhdr = &debug->symbolic_header;
   1360   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
   1361 
   1362   /* The symbolic header contains absolute file offsets and sizes to
   1363      read.  */
   1364 #define READ(ptr, offset, count, size, type)				\
   1365   if (symhdr->count == 0)						\
   1366     debug->ptr = NULL;							\
   1367   else									\
   1368     {									\
   1369       bfd_size_type amt = (bfd_size_type) size * symhdr->count;		\
   1370       debug->ptr = bfd_malloc (amt);					\
   1371       if (debug->ptr == NULL)						\
   1372 	goto error_return;						\
   1373       if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0		\
   1374 	  || bfd_bread (debug->ptr, amt, abfd) != amt)			\
   1375 	goto error_return;						\
   1376     }
   1377 
   1378   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
   1379   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
   1380   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
   1381   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
   1382   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
   1383   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
   1384 	union aux_ext *);
   1385   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
   1386   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
   1387   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
   1388   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
   1389   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
   1390 #undef READ
   1391 
   1392   debug->fdr = NULL;
   1393 
   1394   return TRUE;
   1395 
   1396  error_return:
   1397   if (ext_hdr != NULL)
   1398     free (ext_hdr);
   1399   if (debug->line != NULL)
   1400     free (debug->line);
   1401   if (debug->external_dnr != NULL)
   1402     free (debug->external_dnr);
   1403   if (debug->external_pdr != NULL)
   1404     free (debug->external_pdr);
   1405   if (debug->external_sym != NULL)
   1406     free (debug->external_sym);
   1407   if (debug->external_opt != NULL)
   1408     free (debug->external_opt);
   1409   if (debug->external_aux != NULL)
   1410     free (debug->external_aux);
   1411   if (debug->ss != NULL)
   1412     free (debug->ss);
   1413   if (debug->ssext != NULL)
   1414     free (debug->ssext);
   1415   if (debug->external_fdr != NULL)
   1416     free (debug->external_fdr);
   1417   if (debug->external_rfd != NULL)
   1418     free (debug->external_rfd);
   1419   if (debug->external_ext != NULL)
   1420     free (debug->external_ext);
   1421   return FALSE;
   1422 }
   1423 
   1424 /* Swap RPDR (runtime procedure table entry) for output.  */
   1426 
   1427 static void
   1428 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
   1429 {
   1430   H_PUT_S32 (abfd, in->adr, ex->p_adr);
   1431   H_PUT_32 (abfd, in->regmask, ex->p_regmask);
   1432   H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
   1433   H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
   1434   H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
   1435   H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
   1436 
   1437   H_PUT_16 (abfd, in->framereg, ex->p_framereg);
   1438   H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
   1439 
   1440   H_PUT_32 (abfd, in->irpss, ex->p_irpss);
   1441 }
   1442 
   1443 /* Create a runtime procedure table from the .mdebug section.  */
   1444 
   1445 static bfd_boolean
   1446 mips_elf_create_procedure_table (void *handle, bfd *abfd,
   1447 				 struct bfd_link_info *info, asection *s,
   1448 				 struct ecoff_debug_info *debug)
   1449 {
   1450   const struct ecoff_debug_swap *swap;
   1451   HDRR *hdr = &debug->symbolic_header;
   1452   RPDR *rpdr, *rp;
   1453   struct rpdr_ext *erp;
   1454   void *rtproc;
   1455   struct pdr_ext *epdr;
   1456   struct sym_ext *esym;
   1457   char *ss, **sv;
   1458   char *str;
   1459   bfd_size_type size;
   1460   bfd_size_type count;
   1461   unsigned long sindex;
   1462   unsigned long i;
   1463   PDR pdr;
   1464   SYMR sym;
   1465   const char *no_name_func = _("static procedure (no name)");
   1466 
   1467   epdr = NULL;
   1468   rpdr = NULL;
   1469   esym = NULL;
   1470   ss = NULL;
   1471   sv = NULL;
   1472 
   1473   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   1474 
   1475   sindex = strlen (no_name_func) + 1;
   1476   count = hdr->ipdMax;
   1477   if (count > 0)
   1478     {
   1479       size = swap->external_pdr_size;
   1480 
   1481       epdr = bfd_malloc (size * count);
   1482       if (epdr == NULL)
   1483 	goto error_return;
   1484 
   1485       if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
   1486 	goto error_return;
   1487 
   1488       size = sizeof (RPDR);
   1489       rp = rpdr = bfd_malloc (size * count);
   1490       if (rpdr == NULL)
   1491 	goto error_return;
   1492 
   1493       size = sizeof (char *);
   1494       sv = bfd_malloc (size * count);
   1495       if (sv == NULL)
   1496 	goto error_return;
   1497 
   1498       count = hdr->isymMax;
   1499       size = swap->external_sym_size;
   1500       esym = bfd_malloc (size * count);
   1501       if (esym == NULL)
   1502 	goto error_return;
   1503 
   1504       if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
   1505 	goto error_return;
   1506 
   1507       count = hdr->issMax;
   1508       ss = bfd_malloc (count);
   1509       if (ss == NULL)
   1510 	goto error_return;
   1511       if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
   1512 	goto error_return;
   1513 
   1514       count = hdr->ipdMax;
   1515       for (i = 0; i < (unsigned long) count; i++, rp++)
   1516 	{
   1517 	  (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
   1518 	  (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
   1519 	  rp->adr = sym.value;
   1520 	  rp->regmask = pdr.regmask;
   1521 	  rp->regoffset = pdr.regoffset;
   1522 	  rp->fregmask = pdr.fregmask;
   1523 	  rp->fregoffset = pdr.fregoffset;
   1524 	  rp->frameoffset = pdr.frameoffset;
   1525 	  rp->framereg = pdr.framereg;
   1526 	  rp->pcreg = pdr.pcreg;
   1527 	  rp->irpss = sindex;
   1528 	  sv[i] = ss + sym.iss;
   1529 	  sindex += strlen (sv[i]) + 1;
   1530 	}
   1531     }
   1532 
   1533   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
   1534   size = BFD_ALIGN (size, 16);
   1535   rtproc = bfd_alloc (abfd, size);
   1536   if (rtproc == NULL)
   1537     {
   1538       mips_elf_hash_table (info)->procedure_count = 0;
   1539       goto error_return;
   1540     }
   1541 
   1542   mips_elf_hash_table (info)->procedure_count = count + 2;
   1543 
   1544   erp = rtproc;
   1545   memset (erp, 0, sizeof (struct rpdr_ext));
   1546   erp++;
   1547   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
   1548   strcpy (str, no_name_func);
   1549   str += strlen (no_name_func) + 1;
   1550   for (i = 0; i < count; i++)
   1551     {
   1552       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
   1553       strcpy (str, sv[i]);
   1554       str += strlen (sv[i]) + 1;
   1555     }
   1556   H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
   1557 
   1558   /* Set the size and contents of .rtproc section.  */
   1559   s->size = size;
   1560   s->contents = rtproc;
   1561 
   1562   /* Skip this section later on (I don't think this currently
   1563      matters, but someday it might).  */
   1564   s->map_head.link_order = NULL;
   1565 
   1566   if (epdr != NULL)
   1567     free (epdr);
   1568   if (rpdr != NULL)
   1569     free (rpdr);
   1570   if (esym != NULL)
   1571     free (esym);
   1572   if (ss != NULL)
   1573     free (ss);
   1574   if (sv != NULL)
   1575     free (sv);
   1576 
   1577   return TRUE;
   1578 
   1579  error_return:
   1580   if (epdr != NULL)
   1581     free (epdr);
   1582   if (rpdr != NULL)
   1583     free (rpdr);
   1584   if (esym != NULL)
   1585     free (esym);
   1586   if (ss != NULL)
   1587     free (ss);
   1588   if (sv != NULL)
   1589     free (sv);
   1590   return FALSE;
   1591 }
   1592 
   1593 /* We're going to create a stub for H.  Create a symbol for the stub's
   1595    value and size, to help make the disassembly easier to read.  */
   1596 
   1597 static bfd_boolean
   1598 mips_elf_create_stub_symbol (struct bfd_link_info *info,
   1599 			     struct mips_elf_link_hash_entry *h,
   1600 			     const char *prefix, asection *s, bfd_vma value,
   1601 			     bfd_vma size)
   1602 {
   1603   struct bfd_link_hash_entry *bh;
   1604   struct elf_link_hash_entry *elfh;
   1605   const char *name;
   1606 
   1607   if (ELF_ST_IS_MICROMIPS (h->root.other))
   1608     value |= 1;
   1609 
   1610   /* Create a new symbol.  */
   1611   name = ACONCAT ((prefix, h->root.root.root.string, NULL));
   1612   bh = NULL;
   1613   if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
   1614 					 BSF_LOCAL, s, value, NULL,
   1615 					 TRUE, FALSE, &bh))
   1616     return FALSE;
   1617 
   1618   /* Make it a local function.  */
   1619   elfh = (struct elf_link_hash_entry *) bh;
   1620   elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   1621   elfh->size = size;
   1622   elfh->forced_local = 1;
   1623   return TRUE;
   1624 }
   1625 
   1626 /* We're about to redefine H.  Create a symbol to represent H's
   1627    current value and size, to help make the disassembly easier
   1628    to read.  */
   1629 
   1630 static bfd_boolean
   1631 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
   1632 			       struct mips_elf_link_hash_entry *h,
   1633 			       const char *prefix)
   1634 {
   1635   struct bfd_link_hash_entry *bh;
   1636   struct elf_link_hash_entry *elfh;
   1637   const char *name;
   1638   asection *s;
   1639   bfd_vma value;
   1640 
   1641   /* Read the symbol's value.  */
   1642   BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
   1643 	      || h->root.root.type == bfd_link_hash_defweak);
   1644   s = h->root.root.u.def.section;
   1645   value = h->root.root.u.def.value;
   1646 
   1647   /* Create a new symbol.  */
   1648   name = ACONCAT ((prefix, h->root.root.root.string, NULL));
   1649   bh = NULL;
   1650   if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
   1651 					 BSF_LOCAL, s, value, NULL,
   1652 					 TRUE, FALSE, &bh))
   1653     return FALSE;
   1654 
   1655   /* Make it local and copy the other attributes from H.  */
   1656   elfh = (struct elf_link_hash_entry *) bh;
   1657   elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
   1658   elfh->other = h->root.other;
   1659   elfh->size = h->root.size;
   1660   elfh->forced_local = 1;
   1661   return TRUE;
   1662 }
   1663 
   1664 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
   1665    function rather than to a hard-float stub.  */
   1666 
   1667 static bfd_boolean
   1668 section_allows_mips16_refs_p (asection *section)
   1669 {
   1670   const char *name;
   1671 
   1672   name = bfd_get_section_name (section->owner, section);
   1673   return (FN_STUB_P (name)
   1674 	  || CALL_STUB_P (name)
   1675 	  || CALL_FP_STUB_P (name)
   1676 	  || strcmp (name, ".pdr") == 0);
   1677 }
   1678 
   1679 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
   1680    stub section of some kind.  Return the R_SYMNDX of the target
   1681    function, or 0 if we can't decide which function that is.  */
   1682 
   1683 static unsigned long
   1684 mips16_stub_symndx (const struct elf_backend_data *bed,
   1685 		    asection *sec ATTRIBUTE_UNUSED,
   1686 		    const Elf_Internal_Rela *relocs,
   1687 		    const Elf_Internal_Rela *relend)
   1688 {
   1689   int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
   1690   const Elf_Internal_Rela *rel;
   1691 
   1692   /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
   1693      one in a compound relocation.  */
   1694   for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
   1695     if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
   1696       return ELF_R_SYM (sec->owner, rel->r_info);
   1697 
   1698   /* Otherwise trust the first relocation, whatever its kind.  This is
   1699      the traditional behavior.  */
   1700   if (relocs < relend)
   1701     return ELF_R_SYM (sec->owner, relocs->r_info);
   1702 
   1703   return 0;
   1704 }
   1705 
   1706 /* Check the mips16 stubs for a particular symbol, and see if we can
   1707    discard them.  */
   1708 
   1709 static void
   1710 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
   1711 			     struct mips_elf_link_hash_entry *h)
   1712 {
   1713   /* Dynamic symbols must use the standard call interface, in case other
   1714      objects try to call them.  */
   1715   if (h->fn_stub != NULL
   1716       && h->root.dynindx != -1)
   1717     {
   1718       mips_elf_create_shadow_symbol (info, h, ".mips16.");
   1719       h->need_fn_stub = TRUE;
   1720     }
   1721 
   1722   if (h->fn_stub != NULL
   1723       && ! h->need_fn_stub)
   1724     {
   1725       /* We don't need the fn_stub; the only references to this symbol
   1726          are 16 bit calls.  Clobber the size to 0 to prevent it from
   1727          being included in the link.  */
   1728       h->fn_stub->size = 0;
   1729       h->fn_stub->flags &= ~SEC_RELOC;
   1730       h->fn_stub->reloc_count = 0;
   1731       h->fn_stub->flags |= SEC_EXCLUDE;
   1732     }
   1733 
   1734   if (h->call_stub != NULL
   1735       && ELF_ST_IS_MIPS16 (h->root.other))
   1736     {
   1737       /* We don't need the call_stub; this is a 16 bit function, so
   1738          calls from other 16 bit functions are OK.  Clobber the size
   1739          to 0 to prevent it from being included in the link.  */
   1740       h->call_stub->size = 0;
   1741       h->call_stub->flags &= ~SEC_RELOC;
   1742       h->call_stub->reloc_count = 0;
   1743       h->call_stub->flags |= SEC_EXCLUDE;
   1744     }
   1745 
   1746   if (h->call_fp_stub != NULL
   1747       && ELF_ST_IS_MIPS16 (h->root.other))
   1748     {
   1749       /* We don't need the call_stub; this is a 16 bit function, so
   1750          calls from other 16 bit functions are OK.  Clobber the size
   1751          to 0 to prevent it from being included in the link.  */
   1752       h->call_fp_stub->size = 0;
   1753       h->call_fp_stub->flags &= ~SEC_RELOC;
   1754       h->call_fp_stub->reloc_count = 0;
   1755       h->call_fp_stub->flags |= SEC_EXCLUDE;
   1756     }
   1757 }
   1758 
   1759 /* Hashtable callbacks for mips_elf_la25_stubs.  */
   1760 
   1761 static hashval_t
   1762 mips_elf_la25_stub_hash (const void *entry_)
   1763 {
   1764   const struct mips_elf_la25_stub *entry;
   1765 
   1766   entry = (struct mips_elf_la25_stub *) entry_;
   1767   return entry->h->root.root.u.def.section->id
   1768     + entry->h->root.root.u.def.value;
   1769 }
   1770 
   1771 static int
   1772 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
   1773 {
   1774   const struct mips_elf_la25_stub *entry1, *entry2;
   1775 
   1776   entry1 = (struct mips_elf_la25_stub *) entry1_;
   1777   entry2 = (struct mips_elf_la25_stub *) entry2_;
   1778   return ((entry1->h->root.root.u.def.section
   1779 	   == entry2->h->root.root.u.def.section)
   1780 	  && (entry1->h->root.root.u.def.value
   1781 	      == entry2->h->root.root.u.def.value));
   1782 }
   1783 
   1784 /* Called by the linker to set up the la25 stub-creation code.  FN is
   1785    the linker's implementation of add_stub_function.  Return true on
   1786    success.  */
   1787 
   1788 bfd_boolean
   1789 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
   1790 			  asection *(*fn) (const char *, asection *,
   1791 					   asection *))
   1792 {
   1793   struct mips_elf_link_hash_table *htab;
   1794 
   1795   htab = mips_elf_hash_table (info);
   1796   if (htab == NULL)
   1797     return FALSE;
   1798 
   1799   htab->add_stub_section = fn;
   1800   htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
   1801 				      mips_elf_la25_stub_eq, NULL);
   1802   if (htab->la25_stubs == NULL)
   1803     return FALSE;
   1804 
   1805   return TRUE;
   1806 }
   1807 
   1808 /* Return true if H is a locally-defined PIC function, in the sense
   1809    that it or its fn_stub might need $25 to be valid on entry.
   1810    Note that MIPS16 functions set up $gp using PC-relative instructions,
   1811    so they themselves never need $25 to be valid.  Only non-MIPS16
   1812    entry points are of interest here.  */
   1813 
   1814 static bfd_boolean
   1815 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
   1816 {
   1817   return ((h->root.root.type == bfd_link_hash_defined
   1818 	   || h->root.root.type == bfd_link_hash_defweak)
   1819 	  && h->root.def_regular
   1820 	  && !bfd_is_abs_section (h->root.root.u.def.section)
   1821 	  && (!ELF_ST_IS_MIPS16 (h->root.other)
   1822 	      || (h->fn_stub && h->need_fn_stub))
   1823 	  && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
   1824 	      || ELF_ST_IS_MIPS_PIC (h->root.other)));
   1825 }
   1826 
   1827 /* Set *SEC to the input section that contains the target of STUB.
   1828    Return the offset of the target from the start of that section.  */
   1829 
   1830 static bfd_vma
   1831 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
   1832 			  asection **sec)
   1833 {
   1834   if (ELF_ST_IS_MIPS16 (stub->h->root.other))
   1835     {
   1836       BFD_ASSERT (stub->h->need_fn_stub);
   1837       *sec = stub->h->fn_stub;
   1838       return 0;
   1839     }
   1840   else
   1841     {
   1842       *sec = stub->h->root.root.u.def.section;
   1843       return stub->h->root.root.u.def.value;
   1844     }
   1845 }
   1846 
   1847 /* STUB describes an la25 stub that we have decided to implement
   1848    by inserting an LUI/ADDIU pair before the target function.
   1849    Create the section and redirect the function symbol to it.  */
   1850 
   1851 static bfd_boolean
   1852 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
   1853 			 struct bfd_link_info *info)
   1854 {
   1855   struct mips_elf_link_hash_table *htab;
   1856   char *name;
   1857   asection *s, *input_section;
   1858   unsigned int align;
   1859 
   1860   htab = mips_elf_hash_table (info);
   1861   if (htab == NULL)
   1862     return FALSE;
   1863 
   1864   /* Create a unique name for the new section.  */
   1865   name = bfd_malloc (11 + sizeof (".text.stub."));
   1866   if (name == NULL)
   1867     return FALSE;
   1868   sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
   1869 
   1870   /* Create the section.  */
   1871   mips_elf_get_la25_target (stub, &input_section);
   1872   s = htab->add_stub_section (name, input_section,
   1873 			      input_section->output_section);
   1874   if (s == NULL)
   1875     return FALSE;
   1876 
   1877   /* Make sure that any padding goes before the stub.  */
   1878   align = input_section->alignment_power;
   1879   if (!bfd_set_section_alignment (s->owner, s, align))
   1880     return FALSE;
   1881   if (align > 3)
   1882     s->size = (1 << align) - 8;
   1883 
   1884   /* Create a symbol for the stub.  */
   1885   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
   1886   stub->stub_section = s;
   1887   stub->offset = s->size;
   1888 
   1889   /* Allocate room for it.  */
   1890   s->size += 8;
   1891   return TRUE;
   1892 }
   1893 
   1894 /* STUB describes an la25 stub that we have decided to implement
   1895    with a separate trampoline.  Allocate room for it and redirect
   1896    the function symbol to it.  */
   1897 
   1898 static bfd_boolean
   1899 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
   1900 			      struct bfd_link_info *info)
   1901 {
   1902   struct mips_elf_link_hash_table *htab;
   1903   asection *s;
   1904 
   1905   htab = mips_elf_hash_table (info);
   1906   if (htab == NULL)
   1907     return FALSE;
   1908 
   1909   /* Create a trampoline section, if we haven't already.  */
   1910   s = htab->strampoline;
   1911   if (s == NULL)
   1912     {
   1913       asection *input_section = stub->h->root.root.u.def.section;
   1914       s = htab->add_stub_section (".text", NULL,
   1915 				  input_section->output_section);
   1916       if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
   1917 	return FALSE;
   1918       htab->strampoline = s;
   1919     }
   1920 
   1921   /* Create a symbol for the stub.  */
   1922   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
   1923   stub->stub_section = s;
   1924   stub->offset = s->size;
   1925 
   1926   /* Allocate room for it.  */
   1927   s->size += 16;
   1928   return TRUE;
   1929 }
   1930 
   1931 /* H describes a symbol that needs an la25 stub.  Make sure that an
   1932    appropriate stub exists and point H at it.  */
   1933 
   1934 static bfd_boolean
   1935 mips_elf_add_la25_stub (struct bfd_link_info *info,
   1936 			struct mips_elf_link_hash_entry *h)
   1937 {
   1938   struct mips_elf_link_hash_table *htab;
   1939   struct mips_elf_la25_stub search, *stub;
   1940   bfd_boolean use_trampoline_p;
   1941   asection *s;
   1942   bfd_vma value;
   1943   void **slot;
   1944 
   1945   /* Describe the stub we want.  */
   1946   search.stub_section = NULL;
   1947   search.offset = 0;
   1948   search.h = h;
   1949 
   1950   /* See if we've already created an equivalent stub.  */
   1951   htab = mips_elf_hash_table (info);
   1952   if (htab == NULL)
   1953     return FALSE;
   1954 
   1955   slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
   1956   if (slot == NULL)
   1957     return FALSE;
   1958 
   1959   stub = (struct mips_elf_la25_stub *) *slot;
   1960   if (stub != NULL)
   1961     {
   1962       /* We can reuse the existing stub.  */
   1963       h->la25_stub = stub;
   1964       return TRUE;
   1965     }
   1966 
   1967   /* Create a permanent copy of ENTRY and add it to the hash table.  */
   1968   stub = bfd_malloc (sizeof (search));
   1969   if (stub == NULL)
   1970     return FALSE;
   1971   *stub = search;
   1972   *slot = stub;
   1973 
   1974   /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
   1975      of the section and if we would need no more than 2 nops.  */
   1976   value = mips_elf_get_la25_target (stub, &s);
   1977   use_trampoline_p = (value != 0 || s->alignment_power > 4);
   1978 
   1979   h->la25_stub = stub;
   1980   return (use_trampoline_p
   1981 	  ? mips_elf_add_la25_trampoline (stub, info)
   1982 	  : mips_elf_add_la25_intro (stub, info));
   1983 }
   1984 
   1985 /* A mips_elf_link_hash_traverse callback that is called before sizing
   1986    sections.  DATA points to a mips_htab_traverse_info structure.  */
   1987 
   1988 static bfd_boolean
   1989 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
   1990 {
   1991   struct mips_htab_traverse_info *hti;
   1992 
   1993   hti = (struct mips_htab_traverse_info *) data;
   1994   if (!hti->info->relocatable)
   1995     mips_elf_check_mips16_stubs (hti->info, h);
   1996 
   1997   if (mips_elf_local_pic_function_p (h))
   1998     {
   1999       /* PR 12845: If H is in a section that has been garbage
   2000 	 collected it will have its output section set to *ABS*.  */
   2001       if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
   2002 	return TRUE;
   2003 
   2004       /* H is a function that might need $25 to be valid on entry.
   2005 	 If we're creating a non-PIC relocatable object, mark H as
   2006 	 being PIC.  If we're creating a non-relocatable object with
   2007 	 non-PIC branches and jumps to H, make sure that H has an la25
   2008 	 stub.  */
   2009       if (hti->info->relocatable)
   2010 	{
   2011 	  if (!PIC_OBJECT_P (hti->output_bfd))
   2012 	    h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
   2013 	}
   2014       else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
   2015 	{
   2016 	  hti->error = TRUE;
   2017 	  return FALSE;
   2018 	}
   2019     }
   2020   return TRUE;
   2021 }
   2022 
   2023 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
   2025    Most mips16 instructions are 16 bits, but these instructions
   2026    are 32 bits.
   2027 
   2028    The format of these instructions is:
   2029 
   2030    +--------------+--------------------------------+
   2031    |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
   2032    +--------------+--------------------------------+
   2033    |                Immediate  15:0                |
   2034    +-----------------------------------------------+
   2035 
   2036    JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
   2037    Note that the immediate value in the first word is swapped.
   2038 
   2039    When producing a relocatable object file, R_MIPS16_26 is
   2040    handled mostly like R_MIPS_26.  In particular, the addend is
   2041    stored as a straight 26-bit value in a 32-bit instruction.
   2042    (gas makes life simpler for itself by never adjusting a
   2043    R_MIPS16_26 reloc to be against a section, so the addend is
   2044    always zero).  However, the 32 bit instruction is stored as 2
   2045    16-bit values, rather than a single 32-bit value.  In a
   2046    big-endian file, the result is the same; in a little-endian
   2047    file, the two 16-bit halves of the 32 bit value are swapped.
   2048    This is so that a disassembler can recognize the jal
   2049    instruction.
   2050 
   2051    When doing a final link, R_MIPS16_26 is treated as a 32 bit
   2052    instruction stored as two 16-bit values.  The addend A is the
   2053    contents of the targ26 field.  The calculation is the same as
   2054    R_MIPS_26.  When storing the calculated value, reorder the
   2055    immediate value as shown above, and don't forget to store the
   2056    value as two 16-bit values.
   2057 
   2058    To put it in MIPS ABI terms, the relocation field is T-targ26-16,
   2059    defined as
   2060 
   2061    big-endian:
   2062    +--------+----------------------+
   2063    |        |                      |
   2064    |        |    targ26-16         |
   2065    |31    26|25                   0|
   2066    +--------+----------------------+
   2067 
   2068    little-endian:
   2069    +----------+------+-------------+
   2070    |          |      |             |
   2071    |  sub1    |      |     sub2    |
   2072    |0        9|10  15|16         31|
   2073    +----------+--------------------+
   2074    where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
   2075    ((sub1 << 16) | sub2)).
   2076 
   2077    When producing a relocatable object file, the calculation is
   2078    (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
   2079    When producing a fully linked file, the calculation is
   2080    let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
   2081    ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
   2082 
   2083    The table below lists the other MIPS16 instruction relocations.
   2084    Each one is calculated in the same way as the non-MIPS16 relocation
   2085    given on the right, but using the extended MIPS16 layout of 16-bit
   2086    immediate fields:
   2087 
   2088 	R_MIPS16_GPREL		R_MIPS_GPREL16
   2089 	R_MIPS16_GOT16		R_MIPS_GOT16
   2090 	R_MIPS16_CALL16		R_MIPS_CALL16
   2091 	R_MIPS16_HI16		R_MIPS_HI16
   2092 	R_MIPS16_LO16		R_MIPS_LO16
   2093 
   2094    A typical instruction will have a format like this:
   2095 
   2096    +--------------+--------------------------------+
   2097    |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
   2098    +--------------+--------------------------------+
   2099    |    Major     |   rx   |   ry   |   Imm  4:0   |
   2100    +--------------+--------------------------------+
   2101 
   2102    EXTEND is the five bit value 11110.  Major is the instruction
   2103    opcode.
   2104 
   2105    All we need to do here is shuffle the bits appropriately.
   2106    As above, the two 16-bit halves must be swapped on a
   2107    little-endian system.  */
   2108 
   2109 static inline bfd_boolean
   2110 mips16_reloc_p (int r_type)
   2111 {
   2112   switch (r_type)
   2113     {
   2114     case R_MIPS16_26:
   2115     case R_MIPS16_GPREL:
   2116     case R_MIPS16_GOT16:
   2117     case R_MIPS16_CALL16:
   2118     case R_MIPS16_HI16:
   2119     case R_MIPS16_LO16:
   2120     case R_MIPS16_TLS_GD:
   2121     case R_MIPS16_TLS_LDM:
   2122     case R_MIPS16_TLS_DTPREL_HI16:
   2123     case R_MIPS16_TLS_DTPREL_LO16:
   2124     case R_MIPS16_TLS_GOTTPREL:
   2125     case R_MIPS16_TLS_TPREL_HI16:
   2126     case R_MIPS16_TLS_TPREL_LO16:
   2127       return TRUE;
   2128 
   2129     default:
   2130       return FALSE;
   2131     }
   2132 }
   2133 
   2134 /* Check if a microMIPS reloc.  */
   2135 
   2136 static inline bfd_boolean
   2137 micromips_reloc_p (unsigned int r_type)
   2138 {
   2139   return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
   2140 }
   2141 
   2142 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
   2143    on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1
   2144    and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.  */
   2145 
   2146 static inline bfd_boolean
   2147 micromips_reloc_shuffle_p (unsigned int r_type)
   2148 {
   2149   return (micromips_reloc_p (r_type)
   2150 	  && r_type != R_MICROMIPS_PC7_S1
   2151 	  && r_type != R_MICROMIPS_PC10_S1);
   2152 }
   2153 
   2154 static inline bfd_boolean
   2155 got16_reloc_p (int r_type)
   2156 {
   2157   return (r_type == R_MIPS_GOT16
   2158 	  || r_type == R_MIPS16_GOT16
   2159 	  || r_type == R_MICROMIPS_GOT16);
   2160 }
   2161 
   2162 static inline bfd_boolean
   2163 call16_reloc_p (int r_type)
   2164 {
   2165   return (r_type == R_MIPS_CALL16
   2166 	  || r_type == R_MIPS16_CALL16
   2167 	  || r_type == R_MICROMIPS_CALL16);
   2168 }
   2169 
   2170 static inline bfd_boolean
   2171 got_disp_reloc_p (unsigned int r_type)
   2172 {
   2173   return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
   2174 }
   2175 
   2176 static inline bfd_boolean
   2177 got_page_reloc_p (unsigned int r_type)
   2178 {
   2179   return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
   2180 }
   2181 
   2182 static inline bfd_boolean
   2183 got_ofst_reloc_p (unsigned int r_type)
   2184 {
   2185   return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
   2186 }
   2187 
   2188 static inline bfd_boolean
   2189 got_hi16_reloc_p (unsigned int r_type)
   2190 {
   2191   return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
   2192 }
   2193 
   2194 static inline bfd_boolean
   2195 got_lo16_reloc_p (unsigned int r_type)
   2196 {
   2197   return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
   2198 }
   2199 
   2200 static inline bfd_boolean
   2201 call_hi16_reloc_p (unsigned int r_type)
   2202 {
   2203   return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
   2204 }
   2205 
   2206 static inline bfd_boolean
   2207 call_lo16_reloc_p (unsigned int r_type)
   2208 {
   2209   return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
   2210 }
   2211 
   2212 static inline bfd_boolean
   2213 hi16_reloc_p (int r_type)
   2214 {
   2215   return (r_type == R_MIPS_HI16
   2216 	  || r_type == R_MIPS16_HI16
   2217 	  || r_type == R_MICROMIPS_HI16
   2218 	  || r_type == R_MIPS_PCHI16);
   2219 }
   2220 
   2221 static inline bfd_boolean
   2222 lo16_reloc_p (int r_type)
   2223 {
   2224   return (r_type == R_MIPS_LO16
   2225 	  || r_type == R_MIPS16_LO16
   2226 	  || r_type == R_MICROMIPS_LO16
   2227 	  || r_type == R_MIPS_PCLO16);
   2228 }
   2229 
   2230 static inline bfd_boolean
   2231 mips16_call_reloc_p (int r_type)
   2232 {
   2233   return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
   2234 }
   2235 
   2236 static inline bfd_boolean
   2237 jal_reloc_p (int r_type)
   2238 {
   2239   return (r_type == R_MIPS_26
   2240 	  || r_type == R_MIPS16_26
   2241 	  || r_type == R_MICROMIPS_26_S1);
   2242 }
   2243 
   2244 static inline bfd_boolean
   2245 aligned_pcrel_reloc_p (int r_type)
   2246 {
   2247   return (r_type == R_MIPS_PC18_S3
   2248 	  || r_type == R_MIPS_PC19_S2);
   2249 }
   2250 
   2251 static inline bfd_boolean
   2252 micromips_branch_reloc_p (int r_type)
   2253 {
   2254   return (r_type == R_MICROMIPS_26_S1
   2255 	  || r_type == R_MICROMIPS_PC16_S1
   2256 	  || r_type == R_MICROMIPS_PC10_S1
   2257 	  || r_type == R_MICROMIPS_PC7_S1);
   2258 }
   2259 
   2260 static inline bfd_boolean
   2261 tls_gd_reloc_p (unsigned int r_type)
   2262 {
   2263   return (r_type == R_MIPS_TLS_GD
   2264 	  || r_type == R_MIPS16_TLS_GD
   2265 	  || r_type == R_MICROMIPS_TLS_GD);
   2266 }
   2267 
   2268 static inline bfd_boolean
   2269 tls_ldm_reloc_p (unsigned int r_type)
   2270 {
   2271   return (r_type == R_MIPS_TLS_LDM
   2272 	  || r_type == R_MIPS16_TLS_LDM
   2273 	  || r_type == R_MICROMIPS_TLS_LDM);
   2274 }
   2275 
   2276 static inline bfd_boolean
   2277 tls_gottprel_reloc_p (unsigned int r_type)
   2278 {
   2279   return (r_type == R_MIPS_TLS_GOTTPREL
   2280 	  || r_type == R_MIPS16_TLS_GOTTPREL
   2281 	  || r_type == R_MICROMIPS_TLS_GOTTPREL);
   2282 }
   2283 
   2284 void
   2285 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
   2286 			       bfd_boolean jal_shuffle, bfd_byte *data)
   2287 {
   2288   bfd_vma first, second, val;
   2289 
   2290   if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
   2291     return;
   2292 
   2293   /* Pick up the first and second halfwords of the instruction.  */
   2294   first = bfd_get_16 (abfd, data);
   2295   second = bfd_get_16 (abfd, data + 2);
   2296   if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
   2297     val = first << 16 | second;
   2298   else if (r_type != R_MIPS16_26)
   2299     val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
   2300 	   | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
   2301   else
   2302     val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
   2303 	   | ((first & 0x1f) << 21) | second);
   2304   bfd_put_32 (abfd, val, data);
   2305 }
   2306 
   2307 void
   2308 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
   2309 			     bfd_boolean jal_shuffle, bfd_byte *data)
   2310 {
   2311   bfd_vma first, second, val;
   2312 
   2313   if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
   2314     return;
   2315 
   2316   val = bfd_get_32 (abfd, data);
   2317   if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
   2318     {
   2319       second = val & 0xffff;
   2320       first = val >> 16;
   2321     }
   2322   else if (r_type != R_MIPS16_26)
   2323     {
   2324       second = ((val >> 11) & 0xffe0) | (val & 0x1f);
   2325       first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
   2326     }
   2327   else
   2328     {
   2329       second = val & 0xffff;
   2330       first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
   2331 	       | ((val >> 21) & 0x1f);
   2332     }
   2333   bfd_put_16 (abfd, second, data + 2);
   2334   bfd_put_16 (abfd, first, data);
   2335 }
   2336 
   2337 bfd_reloc_status_type
   2338 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
   2339 			       arelent *reloc_entry, asection *input_section,
   2340 			       bfd_boolean relocatable, void *data, bfd_vma gp)
   2341 {
   2342   bfd_vma relocation;
   2343   bfd_signed_vma val;
   2344   bfd_reloc_status_type status;
   2345 
   2346   if (bfd_is_com_section (symbol->section))
   2347     relocation = 0;
   2348   else
   2349     relocation = symbol->value;
   2350 
   2351   relocation += symbol->section->output_section->vma;
   2352   relocation += symbol->section->output_offset;
   2353 
   2354   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2355     return bfd_reloc_outofrange;
   2356 
   2357   /* Set val to the offset into the section or symbol.  */
   2358   val = reloc_entry->addend;
   2359 
   2360   _bfd_mips_elf_sign_extend (val, 16);
   2361 
   2362   /* Adjust val for the final section location and GP value.  If we
   2363      are producing relocatable output, we don't want to do this for
   2364      an external symbol.  */
   2365   if (! relocatable
   2366       || (symbol->flags & BSF_SECTION_SYM) != 0)
   2367     val += relocation - gp;
   2368 
   2369   if (reloc_entry->howto->partial_inplace)
   2370     {
   2371       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
   2372 				       (bfd_byte *) data
   2373 				       + reloc_entry->address);
   2374       if (status != bfd_reloc_ok)
   2375 	return status;
   2376     }
   2377   else
   2378     reloc_entry->addend = val;
   2379 
   2380   if (relocatable)
   2381     reloc_entry->address += input_section->output_offset;
   2382 
   2383   return bfd_reloc_ok;
   2384 }
   2385 
   2386 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
   2387    R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
   2388    that contains the relocation field and DATA points to the start of
   2389    INPUT_SECTION.  */
   2390 
   2391 struct mips_hi16
   2392 {
   2393   struct mips_hi16 *next;
   2394   bfd_byte *data;
   2395   asection *input_section;
   2396   arelent rel;
   2397 };
   2398 
   2399 /* FIXME: This should not be a static variable.  */
   2400 
   2401 static struct mips_hi16 *mips_hi16_list;
   2402 
   2403 /* A howto special_function for REL *HI16 relocations.  We can only
   2404    calculate the correct value once we've seen the partnering
   2405    *LO16 relocation, so just save the information for later.
   2406 
   2407    The ABI requires that the *LO16 immediately follow the *HI16.
   2408    However, as a GNU extension, we permit an arbitrary number of
   2409    *HI16s to be associated with a single *LO16.  This significantly
   2410    simplies the relocation handling in gcc.  */
   2411 
   2412 bfd_reloc_status_type
   2413 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   2414 			  asymbol *symbol ATTRIBUTE_UNUSED, void *data,
   2415 			  asection *input_section, bfd *output_bfd,
   2416 			  char **error_message ATTRIBUTE_UNUSED)
   2417 {
   2418   struct mips_hi16 *n;
   2419 
   2420   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2421     return bfd_reloc_outofrange;
   2422 
   2423   n = bfd_malloc (sizeof *n);
   2424   if (n == NULL)
   2425     return bfd_reloc_outofrange;
   2426 
   2427   n->next = mips_hi16_list;
   2428   n->data = data;
   2429   n->input_section = input_section;
   2430   n->rel = *reloc_entry;
   2431   mips_hi16_list = n;
   2432 
   2433   if (output_bfd != NULL)
   2434     reloc_entry->address += input_section->output_offset;
   2435 
   2436   return bfd_reloc_ok;
   2437 }
   2438 
   2439 /* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
   2440    like any other 16-bit relocation when applied to global symbols, but is
   2441    treated in the same as R_MIPS_HI16 when applied to local symbols.  */
   2442 
   2443 bfd_reloc_status_type
   2444 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2445 			   void *data, asection *input_section,
   2446 			   bfd *output_bfd, char **error_message)
   2447 {
   2448   if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
   2449       || bfd_is_und_section (bfd_get_section (symbol))
   2450       || bfd_is_com_section (bfd_get_section (symbol)))
   2451     /* The relocation is against a global symbol.  */
   2452     return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2453 					input_section, output_bfd,
   2454 					error_message);
   2455 
   2456   return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
   2457 				   input_section, output_bfd, error_message);
   2458 }
   2459 
   2460 /* A howto special_function for REL *LO16 relocations.  The *LO16 itself
   2461    is a straightforward 16 bit inplace relocation, but we must deal with
   2462    any partnering high-part relocations as well.  */
   2463 
   2464 bfd_reloc_status_type
   2465 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2466 			  void *data, asection *input_section,
   2467 			  bfd *output_bfd, char **error_message)
   2468 {
   2469   bfd_vma vallo;
   2470   bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
   2471 
   2472   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2473     return bfd_reloc_outofrange;
   2474 
   2475   _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
   2476 				 location);
   2477   vallo = bfd_get_32 (abfd, location);
   2478   _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
   2479 			       location);
   2480 
   2481   while (mips_hi16_list != NULL)
   2482     {
   2483       bfd_reloc_status_type ret;
   2484       struct mips_hi16 *hi;
   2485 
   2486       hi = mips_hi16_list;
   2487 
   2488       /* R_MIPS*_GOT16 relocations are something of a special case.  We
   2489 	 want to install the addend in the same way as for a R_MIPS*_HI16
   2490 	 relocation (with a rightshift of 16).  However, since GOT16
   2491 	 relocations can also be used with global symbols, their howto
   2492 	 has a rightshift of 0.  */
   2493       if (hi->rel.howto->type == R_MIPS_GOT16)
   2494 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
   2495       else if (hi->rel.howto->type == R_MIPS16_GOT16)
   2496 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
   2497       else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
   2498 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
   2499 
   2500       /* VALLO is a signed 16-bit number.  Bias it by 0x8000 so that any
   2501 	 carry or borrow will induce a change of +1 or -1 in the high part.  */
   2502       hi->rel.addend += (vallo + 0x8000) & 0xffff;
   2503 
   2504       ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
   2505 					 hi->input_section, output_bfd,
   2506 					 error_message);
   2507       if (ret != bfd_reloc_ok)
   2508 	return ret;
   2509 
   2510       mips_hi16_list = hi->next;
   2511       free (hi);
   2512     }
   2513 
   2514   return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2515 				      input_section, output_bfd,
   2516 				      error_message);
   2517 }
   2518 
   2519 /* A generic howto special_function.  This calculates and installs the
   2520    relocation itself, thus avoiding the oft-discussed problems in
   2521    bfd_perform_relocation and bfd_install_relocation.  */
   2522 
   2523 bfd_reloc_status_type
   2524 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   2525 			     asymbol *symbol, void *data ATTRIBUTE_UNUSED,
   2526 			     asection *input_section, bfd *output_bfd,
   2527 			     char **error_message ATTRIBUTE_UNUSED)
   2528 {
   2529   bfd_signed_vma val;
   2530   bfd_reloc_status_type status;
   2531   bfd_boolean relocatable;
   2532 
   2533   relocatable = (output_bfd != NULL);
   2534 
   2535   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2536     return bfd_reloc_outofrange;
   2537 
   2538   /* Build up the field adjustment in VAL.  */
   2539   val = 0;
   2540   if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
   2541     {
   2542       /* Either we're calculating the final field value or we have a
   2543 	 relocation against a section symbol.  Add in the section's
   2544 	 offset or address.  */
   2545       val += symbol->section->output_section->vma;
   2546       val += symbol->section->output_offset;
   2547     }
   2548 
   2549   if (!relocatable)
   2550     {
   2551       /* We're calculating the final field value.  Add in the symbol's value
   2552 	 and, if pc-relative, subtract the address of the field itself.  */
   2553       val += symbol->value;
   2554       if (reloc_entry->howto->pc_relative)
   2555 	{
   2556 	  val -= input_section->output_section->vma;
   2557 	  val -= input_section->output_offset;
   2558 	  val -= reloc_entry->address;
   2559 	}
   2560     }
   2561 
   2562   /* VAL is now the final adjustment.  If we're keeping this relocation
   2563      in the output file, and if the relocation uses a separate addend,
   2564      we just need to add VAL to that addend.  Otherwise we need to add
   2565      VAL to the relocation field itself.  */
   2566   if (relocatable && !reloc_entry->howto->partial_inplace)
   2567     reloc_entry->addend += val;
   2568   else
   2569     {
   2570       bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
   2571 
   2572       /* Add in the separate addend, if any.  */
   2573       val += reloc_entry->addend;
   2574 
   2575       /* Add VAL to the relocation field.  */
   2576       _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
   2577 				     location);
   2578       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
   2579 				       location);
   2580       _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
   2581 				   location);
   2582 
   2583       if (status != bfd_reloc_ok)
   2584 	return status;
   2585     }
   2586 
   2587   if (relocatable)
   2588     reloc_entry->address += input_section->output_offset;
   2589 
   2590   return bfd_reloc_ok;
   2591 }
   2592 
   2593 /* Swap an entry in a .gptab section.  Note that these routines rely
   2595    on the equivalence of the two elements of the union.  */
   2596 
   2597 static void
   2598 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
   2599 			      Elf32_gptab *in)
   2600 {
   2601   in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
   2602   in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
   2603 }
   2604 
   2605 static void
   2606 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
   2607 			       Elf32_External_gptab *ex)
   2608 {
   2609   H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
   2610   H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
   2611 }
   2612 
   2613 static void
   2614 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
   2615 				Elf32_External_compact_rel *ex)
   2616 {
   2617   H_PUT_32 (abfd, in->id1, ex->id1);
   2618   H_PUT_32 (abfd, in->num, ex->num);
   2619   H_PUT_32 (abfd, in->id2, ex->id2);
   2620   H_PUT_32 (abfd, in->offset, ex->offset);
   2621   H_PUT_32 (abfd, in->reserved0, ex->reserved0);
   2622   H_PUT_32 (abfd, in->reserved1, ex->reserved1);
   2623 }
   2624 
   2625 static void
   2626 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
   2627 			   Elf32_External_crinfo *ex)
   2628 {
   2629   unsigned long l;
   2630 
   2631   l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
   2632        | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
   2633        | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
   2634        | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
   2635   H_PUT_32 (abfd, l, ex->info);
   2636   H_PUT_32 (abfd, in->konst, ex->konst);
   2637   H_PUT_32 (abfd, in->vaddr, ex->vaddr);
   2638 }
   2639 
   2640 /* A .reginfo section holds a single Elf32_RegInfo structure.  These
   2642    routines swap this structure in and out.  They are used outside of
   2643    BFD, so they are globally visible.  */
   2644 
   2645 void
   2646 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
   2647 				Elf32_RegInfo *in)
   2648 {
   2649   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
   2650   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
   2651   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
   2652   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
   2653   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
   2654   in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
   2655 }
   2656 
   2657 void
   2658 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
   2659 				 Elf32_External_RegInfo *ex)
   2660 {
   2661   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
   2662   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
   2663   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
   2664   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
   2665   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
   2666   H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
   2667 }
   2668 
   2669 /* In the 64 bit ABI, the .MIPS.options section holds register
   2670    information in an Elf64_Reginfo structure.  These routines swap
   2671    them in and out.  They are globally visible because they are used
   2672    outside of BFD.  These routines are here so that gas can call them
   2673    without worrying about whether the 64 bit ABI has been included.  */
   2674 
   2675 void
   2676 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
   2677 				Elf64_Internal_RegInfo *in)
   2678 {
   2679   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
   2680   in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
   2681   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
   2682   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
   2683   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
   2684   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
   2685   in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
   2686 }
   2687 
   2688 void
   2689 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
   2690 				 Elf64_External_RegInfo *ex)
   2691 {
   2692   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
   2693   H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
   2694   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
   2695   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
   2696   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
   2697   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
   2698   H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
   2699 }
   2700 
   2701 /* Swap in an options header.  */
   2702 
   2703 void
   2704 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
   2705 			      Elf_Internal_Options *in)
   2706 {
   2707   in->kind = H_GET_8 (abfd, ex->kind);
   2708   in->size = H_GET_8 (abfd, ex->size);
   2709   in->section = H_GET_16 (abfd, ex->section);
   2710   in->info = H_GET_32 (abfd, ex->info);
   2711 }
   2712 
   2713 /* Swap out an options header.  */
   2714 
   2715 void
   2716 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
   2717 			       Elf_External_Options *ex)
   2718 {
   2719   H_PUT_8 (abfd, in->kind, ex->kind);
   2720   H_PUT_8 (abfd, in->size, ex->size);
   2721   H_PUT_16 (abfd, in->section, ex->section);
   2722   H_PUT_32 (abfd, in->info, ex->info);
   2723 }
   2724 
   2725 /* Swap in an abiflags structure.  */
   2726 
   2727 void
   2728 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
   2729 				  const Elf_External_ABIFlags_v0 *ex,
   2730 				  Elf_Internal_ABIFlags_v0 *in)
   2731 {
   2732   in->version = H_GET_16 (abfd, ex->version);
   2733   in->isa_level = H_GET_8 (abfd, ex->isa_level);
   2734   in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
   2735   in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
   2736   in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
   2737   in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
   2738   in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
   2739   in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
   2740   in->ases = H_GET_32 (abfd, ex->ases);
   2741   in->flags1 = H_GET_32 (abfd, ex->flags1);
   2742   in->flags2 = H_GET_32 (abfd, ex->flags2);
   2743 }
   2744 
   2745 /* Swap out an abiflags structure.  */
   2746 
   2747 void
   2748 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
   2749 				   const Elf_Internal_ABIFlags_v0 *in,
   2750 				   Elf_External_ABIFlags_v0 *ex)
   2751 {
   2752   H_PUT_16 (abfd, in->version, ex->version);
   2753   H_PUT_8 (abfd, in->isa_level, ex->isa_level);
   2754   H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
   2755   H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
   2756   H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
   2757   H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
   2758   H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
   2759   H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
   2760   H_PUT_32 (abfd, in->ases, ex->ases);
   2761   H_PUT_32 (abfd, in->flags1, ex->flags1);
   2762   H_PUT_32 (abfd, in->flags2, ex->flags2);
   2763 }
   2764 
   2765 /* This function is called via qsort() to sort the dynamic relocation
   2767    entries by increasing r_symndx value.  */
   2768 
   2769 static int
   2770 sort_dynamic_relocs (const void *arg1, const void *arg2)
   2771 {
   2772   Elf_Internal_Rela int_reloc1;
   2773   Elf_Internal_Rela int_reloc2;
   2774   int diff;
   2775 
   2776   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
   2777   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
   2778 
   2779   diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
   2780   if (diff != 0)
   2781     return diff;
   2782 
   2783   if (int_reloc1.r_offset < int_reloc2.r_offset)
   2784     return -1;
   2785   if (int_reloc1.r_offset > int_reloc2.r_offset)
   2786     return 1;
   2787   return 0;
   2788 }
   2789 
   2790 /* Like sort_dynamic_relocs, but used for elf64 relocations.  */
   2791 
   2792 static int
   2793 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
   2794 			const void *arg2 ATTRIBUTE_UNUSED)
   2795 {
   2796 #ifdef BFD64
   2797   Elf_Internal_Rela int_reloc1[3];
   2798   Elf_Internal_Rela int_reloc2[3];
   2799 
   2800   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
   2801     (reldyn_sorting_bfd, arg1, int_reloc1);
   2802   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
   2803     (reldyn_sorting_bfd, arg2, int_reloc2);
   2804 
   2805   if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
   2806     return -1;
   2807   if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
   2808     return 1;
   2809 
   2810   if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
   2811     return -1;
   2812   if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
   2813     return 1;
   2814   return 0;
   2815 #else
   2816   abort ();
   2817 #endif
   2818 }
   2819 
   2820 
   2821 /* This routine is used to write out ECOFF debugging external symbol
   2822    information.  It is called via mips_elf_link_hash_traverse.  The
   2823    ECOFF external symbol information must match the ELF external
   2824    symbol information.  Unfortunately, at this point we don't know
   2825    whether a symbol is required by reloc information, so the two
   2826    tables may wind up being different.  We must sort out the external
   2827    symbol information before we can set the final size of the .mdebug
   2828    section, and we must set the size of the .mdebug section before we
   2829    can relocate any sections, and we can't know which symbols are
   2830    required by relocation until we relocate the sections.
   2831    Fortunately, it is relatively unlikely that any symbol will be
   2832    stripped but required by a reloc.  In particular, it can not happen
   2833    when generating a final executable.  */
   2834 
   2835 static bfd_boolean
   2836 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
   2837 {
   2838   struct extsym_info *einfo = data;
   2839   bfd_boolean strip;
   2840   asection *sec, *output_section;
   2841 
   2842   if (h->root.indx == -2)
   2843     strip = FALSE;
   2844   else if ((h->root.def_dynamic
   2845 	    || h->root.ref_dynamic
   2846 	    || h->root.type == bfd_link_hash_new)
   2847 	   && !h->root.def_regular
   2848 	   && !h->root.ref_regular)
   2849     strip = TRUE;
   2850   else if (einfo->info->strip == strip_all
   2851 	   || (einfo->info->strip == strip_some
   2852 	       && bfd_hash_lookup (einfo->info->keep_hash,
   2853 				   h->root.root.root.string,
   2854 				   FALSE, FALSE) == NULL))
   2855     strip = TRUE;
   2856   else
   2857     strip = FALSE;
   2858 
   2859   if (strip)
   2860     return TRUE;
   2861 
   2862   if (h->esym.ifd == -2)
   2863     {
   2864       h->esym.jmptbl = 0;
   2865       h->esym.cobol_main = 0;
   2866       h->esym.weakext = 0;
   2867       h->esym.reserved = 0;
   2868       h->esym.ifd = ifdNil;
   2869       h->esym.asym.value = 0;
   2870       h->esym.asym.st = stGlobal;
   2871 
   2872       if (h->root.root.type == bfd_link_hash_undefined
   2873 	  || h->root.root.type == bfd_link_hash_undefweak)
   2874 	{
   2875 	  const char *name;
   2876 
   2877 	  /* Use undefined class.  Also, set class and type for some
   2878              special symbols.  */
   2879 	  name = h->root.root.root.string;
   2880 	  if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
   2881 	      || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
   2882 	    {
   2883 	      h->esym.asym.sc = scData;
   2884 	      h->esym.asym.st = stLabel;
   2885 	      h->esym.asym.value = 0;
   2886 	    }
   2887 	  else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
   2888 	    {
   2889 	      h->esym.asym.sc = scAbs;
   2890 	      h->esym.asym.st = stLabel;
   2891 	      h->esym.asym.value =
   2892 		mips_elf_hash_table (einfo->info)->procedure_count;
   2893 	    }
   2894 	  else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
   2895 	    {
   2896 	      h->esym.asym.sc = scAbs;
   2897 	      h->esym.asym.st = stLabel;
   2898 	      h->esym.asym.value = elf_gp (einfo->abfd);
   2899 	    }
   2900 	  else
   2901 	    h->esym.asym.sc = scUndefined;
   2902 	}
   2903       else if (h->root.root.type != bfd_link_hash_defined
   2904 	  && h->root.root.type != bfd_link_hash_defweak)
   2905 	h->esym.asym.sc = scAbs;
   2906       else
   2907 	{
   2908 	  const char *name;
   2909 
   2910 	  sec = h->root.root.u.def.section;
   2911 	  output_section = sec->output_section;
   2912 
   2913 	  /* When making a shared library and symbol h is the one from
   2914 	     the another shared library, OUTPUT_SECTION may be null.  */
   2915 	  if (output_section == NULL)
   2916 	    h->esym.asym.sc = scUndefined;
   2917 	  else
   2918 	    {
   2919 	      name = bfd_section_name (output_section->owner, output_section);
   2920 
   2921 	      if (strcmp (name, ".text") == 0)
   2922 		h->esym.asym.sc = scText;
   2923 	      else if (strcmp (name, ".data") == 0)
   2924 		h->esym.asym.sc = scData;
   2925 	      else if (strcmp (name, ".sdata") == 0)
   2926 		h->esym.asym.sc = scSData;
   2927 	      else if (strcmp (name, ".rodata") == 0
   2928 		       || strcmp (name, ".rdata") == 0)
   2929 		h->esym.asym.sc = scRData;
   2930 	      else if (strcmp (name, ".bss") == 0)
   2931 		h->esym.asym.sc = scBss;
   2932 	      else if (strcmp (name, ".sbss") == 0)
   2933 		h->esym.asym.sc = scSBss;
   2934 	      else if (strcmp (name, ".init") == 0)
   2935 		h->esym.asym.sc = scInit;
   2936 	      else if (strcmp (name, ".fini") == 0)
   2937 		h->esym.asym.sc = scFini;
   2938 	      else
   2939 		h->esym.asym.sc = scAbs;
   2940 	    }
   2941 	}
   2942 
   2943       h->esym.asym.reserved = 0;
   2944       h->esym.asym.index = indexNil;
   2945     }
   2946 
   2947   if (h->root.root.type == bfd_link_hash_common)
   2948     h->esym.asym.value = h->root.root.u.c.size;
   2949   else if (h->root.root.type == bfd_link_hash_defined
   2950 	   || h->root.root.type == bfd_link_hash_defweak)
   2951     {
   2952       if (h->esym.asym.sc == scCommon)
   2953 	h->esym.asym.sc = scBss;
   2954       else if (h->esym.asym.sc == scSCommon)
   2955 	h->esym.asym.sc = scSBss;
   2956 
   2957       sec = h->root.root.u.def.section;
   2958       output_section = sec->output_section;
   2959       if (output_section != NULL)
   2960 	h->esym.asym.value = (h->root.root.u.def.value
   2961 			      + sec->output_offset
   2962 			      + output_section->vma);
   2963       else
   2964 	h->esym.asym.value = 0;
   2965     }
   2966   else
   2967     {
   2968       struct mips_elf_link_hash_entry *hd = h;
   2969 
   2970       while (hd->root.root.type == bfd_link_hash_indirect)
   2971 	hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
   2972 
   2973       if (hd->needs_lazy_stub)
   2974 	{
   2975 	  BFD_ASSERT (hd->root.plt.plist != NULL);
   2976 	  BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
   2977 	  /* Set type and value for a symbol with a function stub.  */
   2978 	  h->esym.asym.st = stProc;
   2979 	  sec = hd->root.root.u.def.section;
   2980 	  if (sec == NULL)
   2981 	    h->esym.asym.value = 0;
   2982 	  else
   2983 	    {
   2984 	      output_section = sec->output_section;
   2985 	      if (output_section != NULL)
   2986 		h->esym.asym.value = (hd->root.plt.plist->stub_offset
   2987 				      + sec->output_offset
   2988 				      + output_section->vma);
   2989 	      else
   2990 		h->esym.asym.value = 0;
   2991 	    }
   2992 	}
   2993     }
   2994 
   2995   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
   2996 				      h->root.root.root.string,
   2997 				      &h->esym))
   2998     {
   2999       einfo->failed = TRUE;
   3000       return FALSE;
   3001     }
   3002 
   3003   return TRUE;
   3004 }
   3005 
   3006 /* A comparison routine used to sort .gptab entries.  */
   3007 
   3008 static int
   3009 gptab_compare (const void *p1, const void *p2)
   3010 {
   3011   const Elf32_gptab *a1 = p1;
   3012   const Elf32_gptab *a2 = p2;
   3013 
   3014   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
   3015 }
   3016 
   3017 /* Functions to manage the got entry hash table.  */
   3019 
   3020 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
   3021    hash number.  */
   3022 
   3023 static INLINE hashval_t
   3024 mips_elf_hash_bfd_vma (bfd_vma addr)
   3025 {
   3026 #ifdef BFD64
   3027   return addr + (addr >> 32);
   3028 #else
   3029   return addr;
   3030 #endif
   3031 }
   3032 
   3033 static hashval_t
   3034 mips_elf_got_entry_hash (const void *entry_)
   3035 {
   3036   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
   3037 
   3038   return (entry->symndx
   3039 	  + ((entry->tls_type == GOT_TLS_LDM) << 18)
   3040 	  + (entry->tls_type == GOT_TLS_LDM ? 0
   3041 	     : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
   3042 	     : entry->symndx >= 0 ? (entry->abfd->id
   3043 				     + mips_elf_hash_bfd_vma (entry->d.addend))
   3044 	     : entry->d.h->root.root.root.hash));
   3045 }
   3046 
   3047 static int
   3048 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
   3049 {
   3050   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
   3051   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
   3052 
   3053   return (e1->symndx == e2->symndx
   3054 	  && e1->tls_type == e2->tls_type
   3055 	  && (e1->tls_type == GOT_TLS_LDM ? TRUE
   3056 	      : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
   3057 	      : e1->symndx >= 0 ? (e1->abfd == e2->abfd
   3058 				   && e1->d.addend == e2->d.addend)
   3059 	      : e2->abfd && e1->d.h == e2->d.h));
   3060 }
   3061 
   3062 static hashval_t
   3063 mips_got_page_ref_hash (const void *ref_)
   3064 {
   3065   const struct mips_got_page_ref *ref;
   3066 
   3067   ref = (const struct mips_got_page_ref *) ref_;
   3068   return ((ref->symndx >= 0
   3069 	   ? (hashval_t) (ref->u.abfd->id + ref->symndx)
   3070 	   : ref->u.h->root.root.root.hash)
   3071 	  + mips_elf_hash_bfd_vma (ref->addend));
   3072 }
   3073 
   3074 static int
   3075 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
   3076 {
   3077   const struct mips_got_page_ref *ref1, *ref2;
   3078 
   3079   ref1 = (const struct mips_got_page_ref *) ref1_;
   3080   ref2 = (const struct mips_got_page_ref *) ref2_;
   3081   return (ref1->symndx == ref2->symndx
   3082 	  && (ref1->symndx < 0
   3083 	      ? ref1->u.h == ref2->u.h
   3084 	      : ref1->u.abfd == ref2->u.abfd)
   3085 	  && ref1->addend == ref2->addend);
   3086 }
   3087 
   3088 static hashval_t
   3089 mips_got_page_entry_hash (const void *entry_)
   3090 {
   3091   const struct mips_got_page_entry *entry;
   3092 
   3093   entry = (const struct mips_got_page_entry *) entry_;
   3094   return entry->sec->id;
   3095 }
   3096 
   3097 static int
   3098 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
   3099 {
   3100   const struct mips_got_page_entry *entry1, *entry2;
   3101 
   3102   entry1 = (const struct mips_got_page_entry *) entry1_;
   3103   entry2 = (const struct mips_got_page_entry *) entry2_;
   3104   return entry1->sec == entry2->sec;
   3105 }
   3106 
   3107 /* Create and return a new mips_got_info structure.  */
   3109 
   3110 static struct mips_got_info *
   3111 mips_elf_create_got_info (bfd *abfd)
   3112 {
   3113   struct mips_got_info *g;
   3114 
   3115   g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
   3116   if (g == NULL)
   3117     return NULL;
   3118 
   3119   g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
   3120 				    mips_elf_got_entry_eq, NULL);
   3121   if (g->got_entries == NULL)
   3122     return NULL;
   3123 
   3124   g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
   3125 				      mips_got_page_ref_eq, NULL);
   3126   if (g->got_page_refs == NULL)
   3127     return NULL;
   3128 
   3129   return g;
   3130 }
   3131 
   3132 /* Return the GOT info for input bfd ABFD, trying to create a new one if
   3133    CREATE_P and if ABFD doesn't already have a GOT.  */
   3134 
   3135 static struct mips_got_info *
   3136 mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
   3137 {
   3138   struct mips_elf_obj_tdata *tdata;
   3139 
   3140   if (!is_mips_elf (abfd))
   3141     return NULL;
   3142 
   3143   tdata = mips_elf_tdata (abfd);
   3144   if (!tdata->got && create_p)
   3145     tdata->got = mips_elf_create_got_info (abfd);
   3146   return tdata->got;
   3147 }
   3148 
   3149 /* Record that ABFD should use output GOT G.  */
   3150 
   3151 static void
   3152 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
   3153 {
   3154   struct mips_elf_obj_tdata *tdata;
   3155 
   3156   BFD_ASSERT (is_mips_elf (abfd));
   3157   tdata = mips_elf_tdata (abfd);
   3158   if (tdata->got)
   3159     {
   3160       /* The GOT structure itself and the hash table entries are
   3161 	 allocated to a bfd, but the hash tables aren't.  */
   3162       htab_delete (tdata->got->got_entries);
   3163       htab_delete (tdata->got->got_page_refs);
   3164       if (tdata->got->got_page_entries)
   3165 	htab_delete (tdata->got->got_page_entries);
   3166     }
   3167   tdata->got = g;
   3168 }
   3169 
   3170 /* Return the dynamic relocation section.  If it doesn't exist, try to
   3171    create a new it if CREATE_P, otherwise return NULL.  Also return NULL
   3172    if creation fails.  */
   3173 
   3174 static asection *
   3175 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
   3176 {
   3177   const char *dname;
   3178   asection *sreloc;
   3179   bfd *dynobj;
   3180 
   3181   dname = MIPS_ELF_REL_DYN_NAME (info);
   3182   dynobj = elf_hash_table (info)->dynobj;
   3183   sreloc = bfd_get_linker_section (dynobj, dname);
   3184   if (sreloc == NULL && create_p)
   3185     {
   3186       sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
   3187 						   (SEC_ALLOC
   3188 						    | SEC_LOAD
   3189 						    | SEC_HAS_CONTENTS
   3190 						    | SEC_IN_MEMORY
   3191 						    | SEC_LINKER_CREATED
   3192 						    | SEC_READONLY));
   3193       if (sreloc == NULL
   3194 	  || ! bfd_set_section_alignment (dynobj, sreloc,
   3195 					  MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
   3196 	return NULL;
   3197     }
   3198   return sreloc;
   3199 }
   3200 
   3201 /* Return the GOT_TLS_* type required by relocation type R_TYPE.  */
   3202 
   3203 static int
   3204 mips_elf_reloc_tls_type (unsigned int r_type)
   3205 {
   3206   if (tls_gd_reloc_p (r_type))
   3207     return GOT_TLS_GD;
   3208 
   3209   if (tls_ldm_reloc_p (r_type))
   3210     return GOT_TLS_LDM;
   3211 
   3212   if (tls_gottprel_reloc_p (r_type))
   3213     return GOT_TLS_IE;
   3214 
   3215   return GOT_TLS_NONE;
   3216 }
   3217 
   3218 /* Return the number of GOT slots needed for GOT TLS type TYPE.  */
   3219 
   3220 static int
   3221 mips_tls_got_entries (unsigned int type)
   3222 {
   3223   switch (type)
   3224     {
   3225     case GOT_TLS_GD:
   3226     case GOT_TLS_LDM:
   3227       return 2;
   3228 
   3229     case GOT_TLS_IE:
   3230       return 1;
   3231 
   3232     case GOT_TLS_NONE:
   3233       return 0;
   3234     }
   3235   abort ();
   3236 }
   3237 
   3238 /* Count the number of relocations needed for a TLS GOT entry, with
   3239    access types from TLS_TYPE, and symbol H (or a local symbol if H
   3240    is NULL).  */
   3241 
   3242 static int
   3243 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
   3244 		     struct elf_link_hash_entry *h)
   3245 {
   3246   int indx = 0;
   3247   bfd_boolean need_relocs = FALSE;
   3248   bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
   3249 
   3250   if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
   3251       && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
   3252     indx = h->dynindx;
   3253 
   3254   if ((info->shared || indx != 0)
   3255       && (h == NULL
   3256 	  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3257 	  || h->root.type != bfd_link_hash_undefweak))
   3258     need_relocs = TRUE;
   3259 
   3260   if (!need_relocs)
   3261     return 0;
   3262 
   3263   switch (tls_type)
   3264     {
   3265     case GOT_TLS_GD:
   3266       return indx != 0 ? 2 : 1;
   3267 
   3268     case GOT_TLS_IE:
   3269       return 1;
   3270 
   3271     case GOT_TLS_LDM:
   3272       return info->shared ? 1 : 0;
   3273 
   3274     default:
   3275       return 0;
   3276     }
   3277 }
   3278 
   3279 /* Add the number of GOT entries and TLS relocations required by ENTRY
   3280    to G.  */
   3281 
   3282 static void
   3283 mips_elf_count_got_entry (struct bfd_link_info *info,
   3284 			  struct mips_got_info *g,
   3285 			  struct mips_got_entry *entry)
   3286 {
   3287   if (entry->tls_type)
   3288     {
   3289       g->tls_gotno += mips_tls_got_entries (entry->tls_type);
   3290       g->relocs += mips_tls_got_relocs (info, entry->tls_type,
   3291 					entry->symndx < 0
   3292 					? &entry->d.h->root : NULL);
   3293     }
   3294   else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
   3295     g->local_gotno += 1;
   3296   else
   3297     g->global_gotno += 1;
   3298 }
   3299 
   3300 /* Output a simple dynamic relocation into SRELOC.  */
   3301 
   3302 static void
   3303 mips_elf_output_dynamic_relocation (bfd *output_bfd,
   3304 				    asection *sreloc,
   3305 				    unsigned long reloc_index,
   3306 				    unsigned long indx,
   3307 				    int r_type,
   3308 				    bfd_vma offset)
   3309 {
   3310   Elf_Internal_Rela rel[3];
   3311 
   3312   memset (rel, 0, sizeof (rel));
   3313 
   3314   rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
   3315   rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
   3316 
   3317   if (ABI_64_P (output_bfd))
   3318     {
   3319       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
   3320 	(output_bfd, &rel[0],
   3321 	 (sreloc->contents
   3322 	  + reloc_index * sizeof (Elf64_Mips_External_Rel)));
   3323     }
   3324   else
   3325     bfd_elf32_swap_reloc_out
   3326       (output_bfd, &rel[0],
   3327        (sreloc->contents
   3328 	+ reloc_index * sizeof (Elf32_External_Rel)));
   3329 }
   3330 
   3331 /* Initialize a set of TLS GOT entries for one symbol.  */
   3332 
   3333 static void
   3334 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
   3335 			       struct mips_got_entry *entry,
   3336 			       struct mips_elf_link_hash_entry *h,
   3337 			       bfd_vma value)
   3338 {
   3339   struct mips_elf_link_hash_table *htab;
   3340   int indx;
   3341   asection *sreloc, *sgot;
   3342   bfd_vma got_offset, got_offset2;
   3343   bfd_boolean need_relocs = FALSE;
   3344 
   3345   htab = mips_elf_hash_table (info);
   3346   if (htab == NULL)
   3347     return;
   3348 
   3349   sgot = htab->sgot;
   3350 
   3351   indx = 0;
   3352   if (h != NULL)
   3353     {
   3354       bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
   3355 
   3356       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
   3357 	  && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
   3358 	indx = h->root.dynindx;
   3359     }
   3360 
   3361   if (entry->tls_initialized)
   3362     return;
   3363 
   3364   if ((info->shared || indx != 0)
   3365       && (h == NULL
   3366 	  || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
   3367 	  || h->root.type != bfd_link_hash_undefweak))
   3368     need_relocs = TRUE;
   3369 
   3370   /* MINUS_ONE means the symbol is not defined in this object.  It may not
   3371      be defined at all; assume that the value doesn't matter in that
   3372      case.  Otherwise complain if we would use the value.  */
   3373   BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
   3374 	      || h->root.root.type == bfd_link_hash_undefweak);
   3375 
   3376   /* Emit necessary relocations.  */
   3377   sreloc = mips_elf_rel_dyn_section (info, FALSE);
   3378   got_offset = entry->gotidx;
   3379 
   3380   switch (entry->tls_type)
   3381     {
   3382     case GOT_TLS_GD:
   3383       /* General Dynamic.  */
   3384       got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
   3385 
   3386       if (need_relocs)
   3387 	{
   3388 	  mips_elf_output_dynamic_relocation
   3389 	    (abfd, sreloc, sreloc->reloc_count++, indx,
   3390 	     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
   3391 	     sgot->output_offset + sgot->output_section->vma + got_offset);
   3392 
   3393 	  if (indx)
   3394 	    mips_elf_output_dynamic_relocation
   3395 	      (abfd, sreloc, sreloc->reloc_count++, indx,
   3396 	       ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
   3397 	       sgot->output_offset + sgot->output_section->vma + got_offset2);
   3398 	  else
   3399 	    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
   3400 			       sgot->contents + got_offset2);
   3401 	}
   3402       else
   3403 	{
   3404 	  MIPS_ELF_PUT_WORD (abfd, 1,
   3405 			     sgot->contents + got_offset);
   3406 	  MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
   3407 			     sgot->contents + got_offset2);
   3408 	}
   3409       break;
   3410 
   3411     case GOT_TLS_IE:
   3412       /* Initial Exec model.  */
   3413       if (need_relocs)
   3414 	{
   3415 	  if (indx == 0)
   3416 	    MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
   3417 			       sgot->contents + got_offset);
   3418 	  else
   3419 	    MIPS_ELF_PUT_WORD (abfd, 0,
   3420 			       sgot->contents + got_offset);
   3421 
   3422 	  mips_elf_output_dynamic_relocation
   3423 	    (abfd, sreloc, sreloc->reloc_count++, indx,
   3424 	     ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
   3425 	     sgot->output_offset + sgot->output_section->vma + got_offset);
   3426 	}
   3427       else
   3428 	MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
   3429 			   sgot->contents + got_offset);
   3430       break;
   3431 
   3432     case GOT_TLS_LDM:
   3433       /* The initial offset is zero, and the LD offsets will include the
   3434 	 bias by DTP_OFFSET.  */
   3435       MIPS_ELF_PUT_WORD (abfd, 0,
   3436 			 sgot->contents + got_offset
   3437 			 + MIPS_ELF_GOT_SIZE (abfd));
   3438 
   3439       if (!info->shared)
   3440 	MIPS_ELF_PUT_WORD (abfd, 1,
   3441 			   sgot->contents + got_offset);
   3442       else
   3443 	mips_elf_output_dynamic_relocation
   3444 	  (abfd, sreloc, sreloc->reloc_count++, indx,
   3445 	   ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
   3446 	   sgot->output_offset + sgot->output_section->vma + got_offset);
   3447       break;
   3448 
   3449     default:
   3450       abort ();
   3451     }
   3452 
   3453   entry->tls_initialized = TRUE;
   3454 }
   3455 
   3456 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
   3457    for global symbol H.  .got.plt comes before the GOT, so the offset
   3458    will be negative.  */
   3459 
   3460 static bfd_vma
   3461 mips_elf_gotplt_index (struct bfd_link_info *info,
   3462 		       struct elf_link_hash_entry *h)
   3463 {
   3464   bfd_vma got_address, got_value;
   3465   struct mips_elf_link_hash_table *htab;
   3466 
   3467   htab = mips_elf_hash_table (info);
   3468   BFD_ASSERT (htab != NULL);
   3469 
   3470   BFD_ASSERT (h->plt.plist != NULL);
   3471   BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
   3472 
   3473   /* Calculate the address of the associated .got.plt entry.  */
   3474   got_address = (htab->sgotplt->output_section->vma
   3475 		 + htab->sgotplt->output_offset
   3476 		 + (h->plt.plist->gotplt_index
   3477 		    * MIPS_ELF_GOT_SIZE (info->output_bfd)));
   3478 
   3479   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
   3480   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
   3481 	       + htab->root.hgot->root.u.def.section->output_offset
   3482 	       + htab->root.hgot->root.u.def.value);
   3483 
   3484   return got_address - got_value;
   3485 }
   3486 
   3487 /* Return the GOT offset for address VALUE.   If there is not yet a GOT
   3488    entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
   3489    create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
   3490    offset can be found.  */
   3491 
   3492 static bfd_vma
   3493 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3494 			  bfd_vma value, unsigned long r_symndx,
   3495 			  struct mips_elf_link_hash_entry *h, int r_type)
   3496 {
   3497   struct mips_elf_link_hash_table *htab;
   3498   struct mips_got_entry *entry;
   3499 
   3500   htab = mips_elf_hash_table (info);
   3501   BFD_ASSERT (htab != NULL);
   3502 
   3503   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
   3504 					   r_symndx, h, r_type);
   3505   if (!entry)
   3506     return MINUS_ONE;
   3507 
   3508   if (entry->tls_type)
   3509     mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
   3510   return entry->gotidx;
   3511 }
   3512 
   3513 /* Return the GOT index of global symbol H in the primary GOT.  */
   3514 
   3515 static bfd_vma
   3516 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
   3517 				   struct elf_link_hash_entry *h)
   3518 {
   3519   struct mips_elf_link_hash_table *htab;
   3520   long global_got_dynindx;
   3521   struct mips_got_info *g;
   3522   bfd_vma got_index;
   3523 
   3524   htab = mips_elf_hash_table (info);
   3525   BFD_ASSERT (htab != NULL);
   3526 
   3527   global_got_dynindx = 0;
   3528   if (htab->global_gotsym != NULL)
   3529     global_got_dynindx = htab->global_gotsym->dynindx;
   3530 
   3531   /* Once we determine the global GOT entry with the lowest dynamic
   3532      symbol table index, we must put all dynamic symbols with greater
   3533      indices into the primary GOT.  That makes it easy to calculate the
   3534      GOT offset.  */
   3535   BFD_ASSERT (h->dynindx >= global_got_dynindx);
   3536   g = mips_elf_bfd_got (obfd, FALSE);
   3537   got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
   3538 	       * MIPS_ELF_GOT_SIZE (obfd));
   3539   BFD_ASSERT (got_index < htab->sgot->size);
   3540 
   3541   return got_index;
   3542 }
   3543 
   3544 /* Return the GOT index for the global symbol indicated by H, which is
   3545    referenced by a relocation of type R_TYPE in IBFD.  */
   3546 
   3547 static bfd_vma
   3548 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
   3549 			   struct elf_link_hash_entry *h, int r_type)
   3550 {
   3551   struct mips_elf_link_hash_table *htab;
   3552   struct mips_got_info *g;
   3553   struct mips_got_entry lookup, *entry;
   3554   bfd_vma gotidx;
   3555 
   3556   htab = mips_elf_hash_table (info);
   3557   BFD_ASSERT (htab != NULL);
   3558 
   3559   g = mips_elf_bfd_got (ibfd, FALSE);
   3560   BFD_ASSERT (g);
   3561 
   3562   lookup.tls_type = mips_elf_reloc_tls_type (r_type);
   3563   if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
   3564     return mips_elf_primary_global_got_index (obfd, info, h);
   3565 
   3566   lookup.abfd = ibfd;
   3567   lookup.symndx = -1;
   3568   lookup.d.h = (struct mips_elf_link_hash_entry *) h;
   3569   entry = htab_find (g->got_entries, &lookup);
   3570   BFD_ASSERT (entry);
   3571 
   3572   gotidx = entry->gotidx;
   3573   BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
   3574 
   3575   if (lookup.tls_type)
   3576     {
   3577       bfd_vma value = MINUS_ONE;
   3578 
   3579       if ((h->root.type == bfd_link_hash_defined
   3580 	   || h->root.type == bfd_link_hash_defweak)
   3581 	  && h->root.u.def.section->output_section)
   3582 	value = (h->root.u.def.value
   3583 		 + h->root.u.def.section->output_offset
   3584 		 + h->root.u.def.section->output_section->vma);
   3585 
   3586       mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
   3587     }
   3588   return gotidx;
   3589 }
   3590 
   3591 /* Find a GOT page entry that points to within 32KB of VALUE.  These
   3592    entries are supposed to be placed at small offsets in the GOT, i.e.,
   3593    within 32KB of GP.  Return the index of the GOT entry, or -1 if no
   3594    entry could be created.  If OFFSETP is nonnull, use it to return the
   3595    offset of the GOT entry from VALUE.  */
   3596 
   3597 static bfd_vma
   3598 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3599 		   bfd_vma value, bfd_vma *offsetp)
   3600 {
   3601   bfd_vma page, got_index;
   3602   struct mips_got_entry *entry;
   3603 
   3604   page = (value + 0x8000) & ~(bfd_vma) 0xffff;
   3605   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
   3606 					   NULL, R_MIPS_GOT_PAGE);
   3607 
   3608   if (!entry)
   3609     return MINUS_ONE;
   3610 
   3611   got_index = entry->gotidx;
   3612 
   3613   if (offsetp)
   3614     *offsetp = value - entry->d.address;
   3615 
   3616   return got_index;
   3617 }
   3618 
   3619 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
   3620    EXTERNAL is true if the relocation was originally against a global
   3621    symbol that binds locally.  */
   3622 
   3623 static bfd_vma
   3624 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3625 		      bfd_vma value, bfd_boolean external)
   3626 {
   3627   struct mips_got_entry *entry;
   3628 
   3629   /* GOT16 relocations against local symbols are followed by a LO16
   3630      relocation; those against global symbols are not.  Thus if the
   3631      symbol was originally local, the GOT16 relocation should load the
   3632      equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
   3633   if (! external)
   3634     value = mips_elf_high (value) << 16;
   3635 
   3636   /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
   3637      R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
   3638      same in all cases.  */
   3639   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
   3640 					   NULL, R_MIPS_GOT16);
   3641   if (entry)
   3642     return entry->gotidx;
   3643   else
   3644     return MINUS_ONE;
   3645 }
   3646 
   3647 /* Returns the offset for the entry at the INDEXth position
   3648    in the GOT.  */
   3649 
   3650 static bfd_vma
   3651 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
   3652 				bfd *input_bfd, bfd_vma got_index)
   3653 {
   3654   struct mips_elf_link_hash_table *htab;
   3655   asection *sgot;
   3656   bfd_vma gp;
   3657 
   3658   htab = mips_elf_hash_table (info);
   3659   BFD_ASSERT (htab != NULL);
   3660 
   3661   sgot = htab->sgot;
   3662   gp = _bfd_get_gp_value (output_bfd)
   3663     + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
   3664 
   3665   return sgot->output_section->vma + sgot->output_offset + got_index - gp;
   3666 }
   3667 
   3668 /* Create and return a local GOT entry for VALUE, which was calculated
   3669    from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
   3670    be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
   3671    instead.  */
   3672 
   3673 static struct mips_got_entry *
   3674 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
   3675 				 bfd *ibfd, bfd_vma value,
   3676 				 unsigned long r_symndx,
   3677 				 struct mips_elf_link_hash_entry *h,
   3678 				 int r_type)
   3679 {
   3680   struct mips_got_entry lookup, *entry;
   3681   void **loc;
   3682   struct mips_got_info *g;
   3683   struct mips_elf_link_hash_table *htab;
   3684   bfd_vma gotidx;
   3685 
   3686   htab = mips_elf_hash_table (info);
   3687   BFD_ASSERT (htab != NULL);
   3688 
   3689   g = mips_elf_bfd_got (ibfd, FALSE);
   3690   if (g == NULL)
   3691     {
   3692       g = mips_elf_bfd_got (abfd, FALSE);
   3693       BFD_ASSERT (g != NULL);
   3694     }
   3695 
   3696   /* This function shouldn't be called for symbols that live in the global
   3697      area of the GOT.  */
   3698   BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
   3699 
   3700   lookup.tls_type = mips_elf_reloc_tls_type (r_type);
   3701   if (lookup.tls_type)
   3702     {
   3703       lookup.abfd = ibfd;
   3704       if (tls_ldm_reloc_p (r_type))
   3705 	{
   3706 	  lookup.symndx = 0;
   3707 	  lookup.d.addend = 0;
   3708 	}
   3709       else if (h == NULL)
   3710 	{
   3711 	  lookup.symndx = r_symndx;
   3712 	  lookup.d.addend = 0;
   3713 	}
   3714       else
   3715 	{
   3716 	  lookup.symndx = -1;
   3717 	  lookup.d.h = h;
   3718 	}
   3719 
   3720       entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
   3721       BFD_ASSERT (entry);
   3722 
   3723       gotidx = entry->gotidx;
   3724       BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
   3725 
   3726       return entry;
   3727     }
   3728 
   3729   lookup.abfd = NULL;
   3730   lookup.symndx = -1;
   3731   lookup.d.address = value;
   3732   loc = htab_find_slot (g->got_entries, &lookup, INSERT);
   3733   if (!loc)
   3734     return NULL;
   3735 
   3736   entry = (struct mips_got_entry *) *loc;
   3737   if (entry)
   3738     return entry;
   3739 
   3740   if (g->assigned_low_gotno > g->assigned_high_gotno)
   3741     {
   3742       /* We didn't allocate enough space in the GOT.  */
   3743       (*_bfd_error_handler)
   3744 	(_("not enough GOT space for local GOT entries"));
   3745       bfd_set_error (bfd_error_bad_value);
   3746       return NULL;
   3747     }
   3748 
   3749   entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
   3750   if (!entry)
   3751     return NULL;
   3752 
   3753   if (got16_reloc_p (r_type)
   3754       || call16_reloc_p (r_type)
   3755       || got_page_reloc_p (r_type)
   3756       || got_disp_reloc_p (r_type))
   3757     lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
   3758   else
   3759     lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
   3760 
   3761   *entry = lookup;
   3762   *loc = entry;
   3763 
   3764   MIPS_ELF_PUT_WORD (abfd, value, htab->sgot->contents + entry->gotidx);
   3765 
   3766   /* These GOT entries need a dynamic relocation on VxWorks.  */
   3767   if (htab->is_vxworks)
   3768     {
   3769       Elf_Internal_Rela outrel;
   3770       asection *s;
   3771       bfd_byte *rloc;
   3772       bfd_vma got_address;
   3773 
   3774       s = mips_elf_rel_dyn_section (info, FALSE);
   3775       got_address = (htab->sgot->output_section->vma
   3776 		     + htab->sgot->output_offset
   3777 		     + entry->gotidx);
   3778 
   3779       rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
   3780       outrel.r_offset = got_address;
   3781       outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
   3782       outrel.r_addend = value;
   3783       bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
   3784     }
   3785 
   3786   return entry;
   3787 }
   3788 
   3789 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
   3790    The number might be exact or a worst-case estimate, depending on how
   3791    much information is available to elf_backend_omit_section_dynsym at
   3792    the current linking stage.  */
   3793 
   3794 static bfd_size_type
   3795 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
   3796 {
   3797   bfd_size_type count;
   3798 
   3799   count = 0;
   3800   if (info->shared || elf_hash_table (info)->is_relocatable_executable)
   3801     {
   3802       asection *p;
   3803       const struct elf_backend_data *bed;
   3804 
   3805       bed = get_elf_backend_data (output_bfd);
   3806       for (p = output_bfd->sections; p ; p = p->next)
   3807 	if ((p->flags & SEC_EXCLUDE) == 0
   3808 	    && (p->flags & SEC_ALLOC) != 0
   3809 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
   3810 	  ++count;
   3811     }
   3812   return count;
   3813 }
   3814 
   3815 /* Sort the dynamic symbol table so that symbols that need GOT entries
   3816    appear towards the end.  */
   3817 
   3818 static bfd_boolean
   3819 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
   3820 {
   3821   struct mips_elf_link_hash_table *htab;
   3822   struct mips_elf_hash_sort_data hsd;
   3823   struct mips_got_info *g;
   3824 
   3825   if (elf_hash_table (info)->dynsymcount == 0)
   3826     return TRUE;
   3827 
   3828   htab = mips_elf_hash_table (info);
   3829   BFD_ASSERT (htab != NULL);
   3830 
   3831   g = htab->got_info;
   3832   if (g == NULL)
   3833     return TRUE;
   3834 
   3835   hsd.low = NULL;
   3836   hsd.max_unref_got_dynindx
   3837     = hsd.min_got_dynindx
   3838     = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
   3839   hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
   3840   mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
   3841 				elf_hash_table (info)),
   3842 			       mips_elf_sort_hash_table_f,
   3843 			       &hsd);
   3844 
   3845   /* There should have been enough room in the symbol table to
   3846      accommodate both the GOT and non-GOT symbols.  */
   3847   BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
   3848   BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
   3849 	      == elf_hash_table (info)->dynsymcount);
   3850   BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
   3851 	      == g->global_gotno);
   3852 
   3853   /* Now we know which dynamic symbol has the lowest dynamic symbol
   3854      table index in the GOT.  */
   3855   htab->global_gotsym = hsd.low;
   3856 
   3857   return TRUE;
   3858 }
   3859 
   3860 /* If H needs a GOT entry, assign it the highest available dynamic
   3861    index.  Otherwise, assign it the lowest available dynamic
   3862    index.  */
   3863 
   3864 static bfd_boolean
   3865 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
   3866 {
   3867   struct mips_elf_hash_sort_data *hsd = data;
   3868 
   3869   /* Symbols without dynamic symbol table entries aren't interesting
   3870      at all.  */
   3871   if (h->root.dynindx == -1)
   3872     return TRUE;
   3873 
   3874   switch (h->global_got_area)
   3875     {
   3876     case GGA_NONE:
   3877       h->root.dynindx = hsd->max_non_got_dynindx++;
   3878       break;
   3879 
   3880     case GGA_NORMAL:
   3881       h->root.dynindx = --hsd->min_got_dynindx;
   3882       hsd->low = (struct elf_link_hash_entry *) h;
   3883       break;
   3884 
   3885     case GGA_RELOC_ONLY:
   3886       if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
   3887 	hsd->low = (struct elf_link_hash_entry *) h;
   3888       h->root.dynindx = hsd->max_unref_got_dynindx++;
   3889       break;
   3890     }
   3891 
   3892   return TRUE;
   3893 }
   3894 
   3895 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
   3896    (which is owned by the caller and shouldn't be added to the
   3897    hash table directly).  */
   3898 
   3899 static bfd_boolean
   3900 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
   3901 			   struct mips_got_entry *lookup)
   3902 {
   3903   struct mips_elf_link_hash_table *htab;
   3904   struct mips_got_entry *entry;
   3905   struct mips_got_info *g;
   3906   void **loc, **bfd_loc;
   3907 
   3908   /* Make sure there's a slot for this entry in the master GOT.  */
   3909   htab = mips_elf_hash_table (info);
   3910   g = htab->got_info;
   3911   loc = htab_find_slot (g->got_entries, lookup, INSERT);
   3912   if (!loc)
   3913     return FALSE;
   3914 
   3915   /* Populate the entry if it isn't already.  */
   3916   entry = (struct mips_got_entry *) *loc;
   3917   if (!entry)
   3918     {
   3919       entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
   3920       if (!entry)
   3921 	return FALSE;
   3922 
   3923       lookup->tls_initialized = FALSE;
   3924       lookup->gotidx = -1;
   3925       *entry = *lookup;
   3926       *loc = entry;
   3927     }
   3928 
   3929   /* Reuse the same GOT entry for the BFD's GOT.  */
   3930   g = mips_elf_bfd_got (abfd, TRUE);
   3931   if (!g)
   3932     return FALSE;
   3933 
   3934   bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
   3935   if (!bfd_loc)
   3936     return FALSE;
   3937 
   3938   if (!*bfd_loc)
   3939     *bfd_loc = entry;
   3940   return TRUE;
   3941 }
   3942 
   3943 /* ABFD has a GOT relocation of type R_TYPE against H.  Reserve a GOT
   3944    entry for it.  FOR_CALL is true if the caller is only interested in
   3945    using the GOT entry for calls.  */
   3946 
   3947 static bfd_boolean
   3948 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
   3949 				   bfd *abfd, struct bfd_link_info *info,
   3950 				   bfd_boolean for_call, int r_type)
   3951 {
   3952   struct mips_elf_link_hash_table *htab;
   3953   struct mips_elf_link_hash_entry *hmips;
   3954   struct mips_got_entry entry;
   3955   unsigned char tls_type;
   3956 
   3957   htab = mips_elf_hash_table (info);
   3958   BFD_ASSERT (htab != NULL);
   3959 
   3960   hmips = (struct mips_elf_link_hash_entry *) h;
   3961   if (!for_call)
   3962     hmips->got_only_for_calls = FALSE;
   3963 
   3964   /* A global symbol in the GOT must also be in the dynamic symbol
   3965      table.  */
   3966   if (h->dynindx == -1)
   3967     {
   3968       switch (ELF_ST_VISIBILITY (h->other))
   3969 	{
   3970 	case STV_INTERNAL:
   3971 	case STV_HIDDEN:
   3972 	  _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
   3973 	  break;
   3974 	}
   3975       if (!bfd_elf_link_record_dynamic_symbol (info, h))
   3976 	return FALSE;
   3977     }
   3978 
   3979   tls_type = mips_elf_reloc_tls_type (r_type);
   3980   if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
   3981     hmips->global_got_area = GGA_NORMAL;
   3982 
   3983   entry.abfd = abfd;
   3984   entry.symndx = -1;
   3985   entry.d.h = (struct mips_elf_link_hash_entry *) h;
   3986   entry.tls_type = tls_type;
   3987   return mips_elf_record_got_entry (info, abfd, &entry);
   3988 }
   3989 
   3990 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
   3991    where SYMNDX is a local symbol.  Reserve a GOT entry for it.  */
   3992 
   3993 static bfd_boolean
   3994 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
   3995 				  struct bfd_link_info *info, int r_type)
   3996 {
   3997   struct mips_elf_link_hash_table *htab;
   3998   struct mips_got_info *g;
   3999   struct mips_got_entry entry;
   4000 
   4001   htab = mips_elf_hash_table (info);
   4002   BFD_ASSERT (htab != NULL);
   4003 
   4004   g = htab->got_info;
   4005   BFD_ASSERT (g != NULL);
   4006 
   4007   entry.abfd = abfd;
   4008   entry.symndx = symndx;
   4009   entry.d.addend = addend;
   4010   entry.tls_type = mips_elf_reloc_tls_type (r_type);
   4011   return mips_elf_record_got_entry (info, abfd, &entry);
   4012 }
   4013 
   4014 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
   4015    H is the symbol's hash table entry, or null if SYMNDX is local
   4016    to ABFD.  */
   4017 
   4018 static bfd_boolean
   4019 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
   4020 			      long symndx, struct elf_link_hash_entry *h,
   4021 			      bfd_signed_vma addend)
   4022 {
   4023   struct mips_elf_link_hash_table *htab;
   4024   struct mips_got_info *g1, *g2;
   4025   struct mips_got_page_ref lookup, *entry;
   4026   void **loc, **bfd_loc;
   4027 
   4028   htab = mips_elf_hash_table (info);
   4029   BFD_ASSERT (htab != NULL);
   4030 
   4031   g1 = htab->got_info;
   4032   BFD_ASSERT (g1 != NULL);
   4033 
   4034   if (h)
   4035     {
   4036       lookup.symndx = -1;
   4037       lookup.u.h = (struct mips_elf_link_hash_entry *) h;
   4038     }
   4039   else
   4040     {
   4041       lookup.symndx = symndx;
   4042       lookup.u.abfd = abfd;
   4043     }
   4044   lookup.addend = addend;
   4045   loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
   4046   if (loc == NULL)
   4047     return FALSE;
   4048 
   4049   entry = (struct mips_got_page_ref *) *loc;
   4050   if (!entry)
   4051     {
   4052       entry = bfd_alloc (abfd, sizeof (*entry));
   4053       if (!entry)
   4054 	return FALSE;
   4055 
   4056       *entry = lookup;
   4057       *loc = entry;
   4058     }
   4059 
   4060   /* Add the same entry to the BFD's GOT.  */
   4061   g2 = mips_elf_bfd_got (abfd, TRUE);
   4062   if (!g2)
   4063     return FALSE;
   4064 
   4065   bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
   4066   if (!bfd_loc)
   4067     return FALSE;
   4068 
   4069   if (!*bfd_loc)
   4070     *bfd_loc = entry;
   4071 
   4072   return TRUE;
   4073 }
   4074 
   4075 /* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
   4076 
   4077 static void
   4078 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
   4079 				       unsigned int n)
   4080 {
   4081   asection *s;
   4082   struct mips_elf_link_hash_table *htab;
   4083 
   4084   htab = mips_elf_hash_table (info);
   4085   BFD_ASSERT (htab != NULL);
   4086 
   4087   s = mips_elf_rel_dyn_section (info, FALSE);
   4088   BFD_ASSERT (s != NULL);
   4089 
   4090   if (htab->is_vxworks)
   4091     s->size += n * MIPS_ELF_RELA_SIZE (abfd);
   4092   else
   4093     {
   4094       if (s->size == 0)
   4095 	{
   4096 	  /* Make room for a null element.  */
   4097 	  s->size += MIPS_ELF_REL_SIZE (abfd);
   4098 	  ++s->reloc_count;
   4099 	}
   4100       s->size += n * MIPS_ELF_REL_SIZE (abfd);
   4101     }
   4102 }
   4103 
   4104 /* A htab_traverse callback for GOT entries, with DATA pointing to a
   4106    mips_elf_traverse_got_arg structure.  Count the number of GOT
   4107    entries and TLS relocs.  Set DATA->value to true if we need
   4108    to resolve indirect or warning symbols and then recreate the GOT.  */
   4109 
   4110 static int
   4111 mips_elf_check_recreate_got (void **entryp, void *data)
   4112 {
   4113   struct mips_got_entry *entry;
   4114   struct mips_elf_traverse_got_arg *arg;
   4115 
   4116   entry = (struct mips_got_entry *) *entryp;
   4117   arg = (struct mips_elf_traverse_got_arg *) data;
   4118   if (entry->abfd != NULL && entry->symndx == -1)
   4119     {
   4120       struct mips_elf_link_hash_entry *h;
   4121 
   4122       h = entry->d.h;
   4123       if (h->root.root.type == bfd_link_hash_indirect
   4124 	  || h->root.root.type == bfd_link_hash_warning)
   4125 	{
   4126 	  arg->value = TRUE;
   4127 	  return 0;
   4128 	}
   4129     }
   4130   mips_elf_count_got_entry (arg->info, arg->g, entry);
   4131   return 1;
   4132 }
   4133 
   4134 /* A htab_traverse callback for GOT entries, with DATA pointing to a
   4135    mips_elf_traverse_got_arg structure.  Add all entries to DATA->g,
   4136    converting entries for indirect and warning symbols into entries
   4137    for the target symbol.  Set DATA->g to null on error.  */
   4138 
   4139 static int
   4140 mips_elf_recreate_got (void **entryp, void *data)
   4141 {
   4142   struct mips_got_entry new_entry, *entry;
   4143   struct mips_elf_traverse_got_arg *arg;
   4144   void **slot;
   4145 
   4146   entry = (struct mips_got_entry *) *entryp;
   4147   arg = (struct mips_elf_traverse_got_arg *) data;
   4148   if (entry->abfd != NULL
   4149       && entry->symndx == -1
   4150       && (entry->d.h->root.root.type == bfd_link_hash_indirect
   4151 	  || entry->d.h->root.root.type == bfd_link_hash_warning))
   4152     {
   4153       struct mips_elf_link_hash_entry *h;
   4154 
   4155       new_entry = *entry;
   4156       entry = &new_entry;
   4157       h = entry->d.h;
   4158       do
   4159 	{
   4160 	  BFD_ASSERT (h->global_got_area == GGA_NONE);
   4161 	  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   4162 	}
   4163       while (h->root.root.type == bfd_link_hash_indirect
   4164 	     || h->root.root.type == bfd_link_hash_warning);
   4165       entry->d.h = h;
   4166     }
   4167   slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
   4168   if (slot == NULL)
   4169     {
   4170       arg->g = NULL;
   4171       return 0;
   4172     }
   4173   if (*slot == NULL)
   4174     {
   4175       if (entry == &new_entry)
   4176 	{
   4177 	  entry = bfd_alloc (entry->abfd, sizeof (*entry));
   4178 	  if (!entry)
   4179 	    {
   4180 	      arg->g = NULL;
   4181 	      return 0;
   4182 	    }
   4183 	  *entry = new_entry;
   4184 	}
   4185       *slot = entry;
   4186       mips_elf_count_got_entry (arg->info, arg->g, entry);
   4187     }
   4188   return 1;
   4189 }
   4190 
   4191 /* Return the maximum number of GOT page entries required for RANGE.  */
   4192 
   4193 static bfd_vma
   4194 mips_elf_pages_for_range (const struct mips_got_page_range *range)
   4195 {
   4196   return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
   4197 }
   4198 
   4199 /* Record that G requires a page entry that can reach SEC + ADDEND.  */
   4200 
   4201 static bfd_boolean
   4202 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
   4203 				asection *sec, bfd_signed_vma addend)
   4204 {
   4205   struct mips_got_info *g = arg->g;
   4206   struct mips_got_page_entry lookup, *entry;
   4207   struct mips_got_page_range **range_ptr, *range;
   4208   bfd_vma old_pages, new_pages;
   4209   void **loc;
   4210 
   4211   /* Find the mips_got_page_entry hash table entry for this section.  */
   4212   lookup.sec = sec;
   4213   loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
   4214   if (loc == NULL)
   4215     return FALSE;
   4216 
   4217   /* Create a mips_got_page_entry if this is the first time we've
   4218      seen the section.  */
   4219   entry = (struct mips_got_page_entry *) *loc;
   4220   if (!entry)
   4221     {
   4222       entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
   4223       if (!entry)
   4224 	return FALSE;
   4225 
   4226       entry->sec = sec;
   4227       *loc = entry;
   4228     }
   4229 
   4230   /* Skip over ranges whose maximum extent cannot share a page entry
   4231      with ADDEND.  */
   4232   range_ptr = &entry->ranges;
   4233   while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
   4234     range_ptr = &(*range_ptr)->next;
   4235 
   4236   /* If we scanned to the end of the list, or found a range whose
   4237      minimum extent cannot share a page entry with ADDEND, create
   4238      a new singleton range.  */
   4239   range = *range_ptr;
   4240   if (!range || addend < range->min_addend - 0xffff)
   4241     {
   4242       range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
   4243       if (!range)
   4244 	return FALSE;
   4245 
   4246       range->next = *range_ptr;
   4247       range->min_addend = addend;
   4248       range->max_addend = addend;
   4249 
   4250       *range_ptr = range;
   4251       entry->num_pages++;
   4252       g->page_gotno++;
   4253       return TRUE;
   4254     }
   4255 
   4256   /* Remember how many pages the old range contributed.  */
   4257   old_pages = mips_elf_pages_for_range (range);
   4258 
   4259   /* Update the ranges.  */
   4260   if (addend < range->min_addend)
   4261     range->min_addend = addend;
   4262   else if (addend > range->max_addend)
   4263     {
   4264       if (range->next && addend >= range->next->min_addend - 0xffff)
   4265 	{
   4266 	  old_pages += mips_elf_pages_for_range (range->next);
   4267 	  range->max_addend = range->next->max_addend;
   4268 	  range->next = range->next->next;
   4269 	}
   4270       else
   4271 	range->max_addend = addend;
   4272     }
   4273 
   4274   /* Record any change in the total estimate.  */
   4275   new_pages = mips_elf_pages_for_range (range);
   4276   if (old_pages != new_pages)
   4277     {
   4278       entry->num_pages += new_pages - old_pages;
   4279       g->page_gotno += new_pages - old_pages;
   4280     }
   4281 
   4282   return TRUE;
   4283 }
   4284 
   4285 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
   4286    and for which DATA points to a mips_elf_traverse_got_arg.  Work out
   4287    whether the page reference described by *REFP needs a GOT page entry,
   4288    and record that entry in DATA->g if so.  Set DATA->g to null on failure.  */
   4289 
   4290 static bfd_boolean
   4291 mips_elf_resolve_got_page_ref (void **refp, void *data)
   4292 {
   4293   struct mips_got_page_ref *ref;
   4294   struct mips_elf_traverse_got_arg *arg;
   4295   struct mips_elf_link_hash_table *htab;
   4296   asection *sec;
   4297   bfd_vma addend;
   4298 
   4299   ref = (struct mips_got_page_ref *) *refp;
   4300   arg = (struct mips_elf_traverse_got_arg *) data;
   4301   htab = mips_elf_hash_table (arg->info);
   4302 
   4303   if (ref->symndx < 0)
   4304     {
   4305       struct mips_elf_link_hash_entry *h;
   4306 
   4307       /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries.  */
   4308       h = ref->u.h;
   4309       if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
   4310 	return 1;
   4311 
   4312       /* Ignore undefined symbols; we'll issue an error later if
   4313 	 appropriate.  */
   4314       if (!((h->root.root.type == bfd_link_hash_defined
   4315 	     || h->root.root.type == bfd_link_hash_defweak)
   4316 	    && h->root.root.u.def.section))
   4317 	return 1;
   4318 
   4319       sec = h->root.root.u.def.section;
   4320       addend = h->root.root.u.def.value + ref->addend;
   4321     }
   4322   else
   4323     {
   4324       Elf_Internal_Sym *isym;
   4325 
   4326       /* Read in the symbol.  */
   4327       isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
   4328 				    ref->symndx);
   4329       if (isym == NULL)
   4330 	{
   4331 	  arg->g = NULL;
   4332 	  return 0;
   4333 	}
   4334 
   4335       /* Get the associated input section.  */
   4336       sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
   4337       if (sec == NULL)
   4338 	{
   4339 	  arg->g = NULL;
   4340 	  return 0;
   4341 	}
   4342 
   4343       /* If this is a mergable section, work out the section and offset
   4344 	 of the merged data.  For section symbols, the addend specifies
   4345 	 of the offset _of_ the first byte in the data, otherwise it
   4346 	 specifies the offset _from_ the first byte.  */
   4347       if (sec->flags & SEC_MERGE)
   4348 	{
   4349 	  void *secinfo;
   4350 
   4351 	  secinfo = elf_section_data (sec)->sec_info;
   4352 	  if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   4353 	    addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
   4354 						 isym->st_value + ref->addend);
   4355 	  else
   4356 	    addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
   4357 						 isym->st_value) + ref->addend;
   4358 	}
   4359       else
   4360 	addend = isym->st_value + ref->addend;
   4361     }
   4362   if (!mips_elf_record_got_page_entry (arg, sec, addend))
   4363     {
   4364       arg->g = NULL;
   4365       return 0;
   4366     }
   4367   return 1;
   4368 }
   4369 
   4370 /* If any entries in G->got_entries are for indirect or warning symbols,
   4371    replace them with entries for the target symbol.  Convert g->got_page_refs
   4372    into got_page_entry structures and estimate the number of page entries
   4373    that they require.  */
   4374 
   4375 static bfd_boolean
   4376 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
   4377 				    struct mips_got_info *g)
   4378 {
   4379   struct mips_elf_traverse_got_arg tga;
   4380   struct mips_got_info oldg;
   4381 
   4382   oldg = *g;
   4383 
   4384   tga.info = info;
   4385   tga.g = g;
   4386   tga.value = FALSE;
   4387   htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
   4388   if (tga.value)
   4389     {
   4390       *g = oldg;
   4391       g->got_entries = htab_create (htab_size (oldg.got_entries),
   4392 				    mips_elf_got_entry_hash,
   4393 				    mips_elf_got_entry_eq, NULL);
   4394       if (!g->got_entries)
   4395 	return FALSE;
   4396 
   4397       htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
   4398       if (!tga.g)
   4399 	return FALSE;
   4400 
   4401       htab_delete (oldg.got_entries);
   4402     }
   4403 
   4404   g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
   4405 					 mips_got_page_entry_eq, NULL);
   4406   if (g->got_page_entries == NULL)
   4407     return FALSE;
   4408 
   4409   tga.info = info;
   4410   tga.g = g;
   4411   htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
   4412 
   4413   return TRUE;
   4414 }
   4415 
   4416 /* Return true if a GOT entry for H should live in the local rather than
   4417    global GOT area.  */
   4418 
   4419 static bfd_boolean
   4420 mips_use_local_got_p (struct bfd_link_info *info,
   4421 		      struct mips_elf_link_hash_entry *h)
   4422 {
   4423   /* Symbols that aren't in the dynamic symbol table must live in the
   4424      local GOT.  This includes symbols that are completely undefined
   4425      and which therefore don't bind locally.  We'll report undefined
   4426      symbols later if appropriate.  */
   4427   if (h->root.dynindx == -1)
   4428     return TRUE;
   4429 
   4430   /* Symbols that bind locally can (and in the case of forced-local
   4431      symbols, must) live in the local GOT.  */
   4432   if (h->got_only_for_calls
   4433       ? SYMBOL_CALLS_LOCAL (info, &h->root)
   4434       : SYMBOL_REFERENCES_LOCAL (info, &h->root))
   4435     return TRUE;
   4436 
   4437   /* If this is an executable that must provide a definition of the symbol,
   4438      either though PLTs or copy relocations, then that address should go in
   4439      the local rather than global GOT.  */
   4440   if (info->executable && h->has_static_relocs)
   4441     return TRUE;
   4442 
   4443   return FALSE;
   4444 }
   4445 
   4446 /* A mips_elf_link_hash_traverse callback for which DATA points to the
   4447    link_info structure.  Decide whether the hash entry needs an entry in
   4448    the global part of the primary GOT, setting global_got_area accordingly.
   4449    Count the number of global symbols that are in the primary GOT only
   4450    because they have relocations against them (reloc_only_gotno).  */
   4451 
   4452 static int
   4453 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
   4454 {
   4455   struct bfd_link_info *info;
   4456   struct mips_elf_link_hash_table *htab;
   4457   struct mips_got_info *g;
   4458 
   4459   info = (struct bfd_link_info *) data;
   4460   htab = mips_elf_hash_table (info);
   4461   g = htab->got_info;
   4462   if (h->global_got_area != GGA_NONE)
   4463     {
   4464       /* Make a final decision about whether the symbol belongs in the
   4465 	 local or global GOT.  */
   4466       if (mips_use_local_got_p (info, h))
   4467 	/* The symbol belongs in the local GOT.  We no longer need this
   4468 	   entry if it was only used for relocations; those relocations
   4469 	   will be against the null or section symbol instead of H.  */
   4470 	h->global_got_area = GGA_NONE;
   4471       else if (htab->is_vxworks
   4472 	       && h->got_only_for_calls
   4473 	       && h->root.plt.plist->mips_offset != MINUS_ONE)
   4474 	/* On VxWorks, calls can refer directly to the .got.plt entry;
   4475 	   they don't need entries in the regular GOT.  .got.plt entries
   4476 	   will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
   4477 	h->global_got_area = GGA_NONE;
   4478       else if (h->global_got_area == GGA_RELOC_ONLY)
   4479 	{
   4480 	  g->reloc_only_gotno++;
   4481 	  g->global_gotno++;
   4482 	}
   4483     }
   4484   return 1;
   4485 }
   4486 
   4487 /* A htab_traverse callback for GOT entries.  Add each one to the GOT
   4489    given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
   4490 
   4491 static int
   4492 mips_elf_add_got_entry (void **entryp, void *data)
   4493 {
   4494   struct mips_got_entry *entry;
   4495   struct mips_elf_traverse_got_arg *arg;
   4496   void **slot;
   4497 
   4498   entry = (struct mips_got_entry *) *entryp;
   4499   arg = (struct mips_elf_traverse_got_arg *) data;
   4500   slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
   4501   if (!slot)
   4502     {
   4503       arg->g = NULL;
   4504       return 0;
   4505     }
   4506   if (!*slot)
   4507     {
   4508       *slot = entry;
   4509       mips_elf_count_got_entry (arg->info, arg->g, entry);
   4510     }
   4511   return 1;
   4512 }
   4513 
   4514 /* A htab_traverse callback for GOT page entries.  Add each one to the GOT
   4515    given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
   4516 
   4517 static int
   4518 mips_elf_add_got_page_entry (void **entryp, void *data)
   4519 {
   4520   struct mips_got_page_entry *entry;
   4521   struct mips_elf_traverse_got_arg *arg;
   4522   void **slot;
   4523 
   4524   entry = (struct mips_got_page_entry *) *entryp;
   4525   arg = (struct mips_elf_traverse_got_arg *) data;
   4526   slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
   4527   if (!slot)
   4528     {
   4529       arg->g = NULL;
   4530       return 0;
   4531     }
   4532   if (!*slot)
   4533     {
   4534       *slot = entry;
   4535       arg->g->page_gotno += entry->num_pages;
   4536     }
   4537   return 1;
   4538 }
   4539 
   4540 /* Consider merging FROM, which is ABFD's GOT, into TO.  Return -1 if
   4541    this would lead to overflow, 1 if they were merged successfully,
   4542    and 0 if a merge failed due to lack of memory.  (These values are chosen
   4543    so that nonnegative return values can be returned by a htab_traverse
   4544    callback.)  */
   4545 
   4546 static int
   4547 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
   4548 			 struct mips_got_info *to,
   4549 			 struct mips_elf_got_per_bfd_arg *arg)
   4550 {
   4551   struct mips_elf_traverse_got_arg tga;
   4552   unsigned int estimate;
   4553 
   4554   /* Work out how many page entries we would need for the combined GOT.  */
   4555   estimate = arg->max_pages;
   4556   if (estimate >= from->page_gotno + to->page_gotno)
   4557     estimate = from->page_gotno + to->page_gotno;
   4558 
   4559   /* And conservatively estimate how many local and TLS entries
   4560      would be needed.  */
   4561   estimate += from->local_gotno + to->local_gotno;
   4562   estimate += from->tls_gotno + to->tls_gotno;
   4563 
   4564   /* If we're merging with the primary got, any TLS relocations will
   4565      come after the full set of global entries.  Otherwise estimate those
   4566      conservatively as well.  */
   4567   if (to == arg->primary && from->tls_gotno + to->tls_gotno)
   4568     estimate += arg->global_count;
   4569   else
   4570     estimate += from->global_gotno + to->global_gotno;
   4571 
   4572   /* Bail out if the combined GOT might be too big.  */
   4573   if (estimate > arg->max_count)
   4574     return -1;
   4575 
   4576   /* Transfer the bfd's got information from FROM to TO.  */
   4577   tga.info = arg->info;
   4578   tga.g = to;
   4579   htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
   4580   if (!tga.g)
   4581     return 0;
   4582 
   4583   htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
   4584   if (!tga.g)
   4585     return 0;
   4586 
   4587   mips_elf_replace_bfd_got (abfd, to);
   4588   return 1;
   4589 }
   4590 
   4591 /* Attempt to merge GOT G, which belongs to ABFD.  Try to use as much
   4592    as possible of the primary got, since it doesn't require explicit
   4593    dynamic relocations, but don't use bfds that would reference global
   4594    symbols out of the addressable range.  Failing the primary got,
   4595    attempt to merge with the current got, or finish the current got
   4596    and then make make the new got current.  */
   4597 
   4598 static bfd_boolean
   4599 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
   4600 		    struct mips_elf_got_per_bfd_arg *arg)
   4601 {
   4602   unsigned int estimate;
   4603   int result;
   4604 
   4605   if (!mips_elf_resolve_final_got_entries (arg->info, g))
   4606     return FALSE;
   4607 
   4608   /* Work out the number of page, local and TLS entries.  */
   4609   estimate = arg->max_pages;
   4610   if (estimate > g->page_gotno)
   4611     estimate = g->page_gotno;
   4612   estimate += g->local_gotno + g->tls_gotno;
   4613 
   4614   /* We place TLS GOT entries after both locals and globals.  The globals
   4615      for the primary GOT may overflow the normal GOT size limit, so be
   4616      sure not to merge a GOT which requires TLS with the primary GOT in that
   4617      case.  This doesn't affect non-primary GOTs.  */
   4618   estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
   4619 
   4620   if (estimate <= arg->max_count)
   4621     {
   4622       /* If we don't have a primary GOT, use it as
   4623 	 a starting point for the primary GOT.  */
   4624       if (!arg->primary)
   4625 	{
   4626 	  arg->primary = g;
   4627 	  return TRUE;
   4628 	}
   4629 
   4630       /* Try merging with the primary GOT.  */
   4631       result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
   4632       if (result >= 0)
   4633 	return result;
   4634     }
   4635 
   4636   /* If we can merge with the last-created got, do it.  */
   4637   if (arg->current)
   4638     {
   4639       result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
   4640       if (result >= 0)
   4641 	return result;
   4642     }
   4643 
   4644   /* Well, we couldn't merge, so create a new GOT.  Don't check if it
   4645      fits; if it turns out that it doesn't, we'll get relocation
   4646      overflows anyway.  */
   4647   g->next = arg->current;
   4648   arg->current = g;
   4649 
   4650   return TRUE;
   4651 }
   4652 
   4653 /* ENTRYP is a hash table entry for a mips_got_entry.  Set its gotidx
   4654    to GOTIDX, duplicating the entry if it has already been assigned
   4655    an index in a different GOT.  */
   4656 
   4657 static bfd_boolean
   4658 mips_elf_set_gotidx (void **entryp, long gotidx)
   4659 {
   4660   struct mips_got_entry *entry;
   4661 
   4662   entry = (struct mips_got_entry *) *entryp;
   4663   if (entry->gotidx > 0)
   4664     {
   4665       struct mips_got_entry *new_entry;
   4666 
   4667       new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
   4668       if (!new_entry)
   4669 	return FALSE;
   4670 
   4671       *new_entry = *entry;
   4672       *entryp = new_entry;
   4673       entry = new_entry;
   4674     }
   4675   entry->gotidx = gotidx;
   4676   return TRUE;
   4677 }
   4678 
   4679 /* Set the TLS GOT index for the GOT entry in ENTRYP.  DATA points to a
   4680    mips_elf_traverse_got_arg in which DATA->value is the size of one
   4681    GOT entry.  Set DATA->g to null on failure.  */
   4682 
   4683 static int
   4684 mips_elf_initialize_tls_index (void **entryp, void *data)
   4685 {
   4686   struct mips_got_entry *entry;
   4687   struct mips_elf_traverse_got_arg *arg;
   4688 
   4689   /* We're only interested in TLS symbols.  */
   4690   entry = (struct mips_got_entry *) *entryp;
   4691   if (entry->tls_type == GOT_TLS_NONE)
   4692     return 1;
   4693 
   4694   arg = (struct mips_elf_traverse_got_arg *) data;
   4695   if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
   4696     {
   4697       arg->g = NULL;
   4698       return 0;
   4699     }
   4700 
   4701   /* Account for the entries we've just allocated.  */
   4702   arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
   4703   return 1;
   4704 }
   4705 
   4706 /* A htab_traverse callback for GOT entries, where DATA points to a
   4707    mips_elf_traverse_got_arg.  Set the global_got_area of each global
   4708    symbol to DATA->value.  */
   4709 
   4710 static int
   4711 mips_elf_set_global_got_area (void **entryp, void *data)
   4712 {
   4713   struct mips_got_entry *entry;
   4714   struct mips_elf_traverse_got_arg *arg;
   4715 
   4716   entry = (struct mips_got_entry *) *entryp;
   4717   arg = (struct mips_elf_traverse_got_arg *) data;
   4718   if (entry->abfd != NULL
   4719       && entry->symndx == -1
   4720       && entry->d.h->global_got_area != GGA_NONE)
   4721     entry->d.h->global_got_area = arg->value;
   4722   return 1;
   4723 }
   4724 
   4725 /* A htab_traverse callback for secondary GOT entries, where DATA points
   4726    to a mips_elf_traverse_got_arg.  Assign GOT indices to global entries
   4727    and record the number of relocations they require.  DATA->value is
   4728    the size of one GOT entry.  Set DATA->g to null on failure.  */
   4729 
   4730 static int
   4731 mips_elf_set_global_gotidx (void **entryp, void *data)
   4732 {
   4733   struct mips_got_entry *entry;
   4734   struct mips_elf_traverse_got_arg *arg;
   4735 
   4736   entry = (struct mips_got_entry *) *entryp;
   4737   arg = (struct mips_elf_traverse_got_arg *) data;
   4738   if (entry->abfd != NULL
   4739       && entry->symndx == -1
   4740       && entry->d.h->global_got_area != GGA_NONE)
   4741     {
   4742       if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
   4743 	{
   4744 	  arg->g = NULL;
   4745 	  return 0;
   4746 	}
   4747       arg->g->assigned_low_gotno += 1;
   4748 
   4749       if (arg->info->shared
   4750 	  || (elf_hash_table (arg->info)->dynamic_sections_created
   4751 	      && entry->d.h->root.def_dynamic
   4752 	      && !entry->d.h->root.def_regular))
   4753 	arg->g->relocs += 1;
   4754     }
   4755 
   4756   return 1;
   4757 }
   4758 
   4759 /* A htab_traverse callback for GOT entries for which DATA is the
   4760    bfd_link_info.  Forbid any global symbols from having traditional
   4761    lazy-binding stubs.  */
   4762 
   4763 static int
   4764 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
   4765 {
   4766   struct bfd_link_info *info;
   4767   struct mips_elf_link_hash_table *htab;
   4768   struct mips_got_entry *entry;
   4769 
   4770   entry = (struct mips_got_entry *) *entryp;
   4771   info = (struct bfd_link_info *) data;
   4772   htab = mips_elf_hash_table (info);
   4773   BFD_ASSERT (htab != NULL);
   4774 
   4775   if (entry->abfd != NULL
   4776       && entry->symndx == -1
   4777       && entry->d.h->needs_lazy_stub)
   4778     {
   4779       entry->d.h->needs_lazy_stub = FALSE;
   4780       htab->lazy_stub_count--;
   4781     }
   4782 
   4783   return 1;
   4784 }
   4785 
   4786 /* Return the offset of an input bfd IBFD's GOT from the beginning of
   4787    the primary GOT.  */
   4788 static bfd_vma
   4789 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
   4790 {
   4791   if (!g->next)
   4792     return 0;
   4793 
   4794   g = mips_elf_bfd_got (ibfd, FALSE);
   4795   if (! g)
   4796     return 0;
   4797 
   4798   BFD_ASSERT (g->next);
   4799 
   4800   g = g->next;
   4801 
   4802   return (g->local_gotno + g->global_gotno + g->tls_gotno)
   4803     * MIPS_ELF_GOT_SIZE (abfd);
   4804 }
   4805 
   4806 /* Turn a single GOT that is too big for 16-bit addressing into
   4807    a sequence of GOTs, each one 16-bit addressable.  */
   4808 
   4809 static bfd_boolean
   4810 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
   4811 		    asection *got, bfd_size_type pages)
   4812 {
   4813   struct mips_elf_link_hash_table *htab;
   4814   struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
   4815   struct mips_elf_traverse_got_arg tga;
   4816   struct mips_got_info *g, *gg;
   4817   unsigned int assign, needed_relocs;
   4818   bfd *dynobj, *ibfd;
   4819 
   4820   dynobj = elf_hash_table (info)->dynobj;
   4821   htab = mips_elf_hash_table (info);
   4822   BFD_ASSERT (htab != NULL);
   4823 
   4824   g = htab->got_info;
   4825 
   4826   got_per_bfd_arg.obfd = abfd;
   4827   got_per_bfd_arg.info = info;
   4828   got_per_bfd_arg.current = NULL;
   4829   got_per_bfd_arg.primary = NULL;
   4830   got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
   4831 				/ MIPS_ELF_GOT_SIZE (abfd))
   4832 			       - htab->reserved_gotno);
   4833   got_per_bfd_arg.max_pages = pages;
   4834   /* The number of globals that will be included in the primary GOT.
   4835      See the calls to mips_elf_set_global_got_area below for more
   4836      information.  */
   4837   got_per_bfd_arg.global_count = g->global_gotno;
   4838 
   4839   /* Try to merge the GOTs of input bfds together, as long as they
   4840      don't seem to exceed the maximum GOT size, choosing one of them
   4841      to be the primary GOT.  */
   4842   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   4843     {
   4844       gg = mips_elf_bfd_got (ibfd, FALSE);
   4845       if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
   4846 	return FALSE;
   4847     }
   4848 
   4849   /* If we do not find any suitable primary GOT, create an empty one.  */
   4850   if (got_per_bfd_arg.primary == NULL)
   4851     g->next = mips_elf_create_got_info (abfd);
   4852   else
   4853     g->next = got_per_bfd_arg.primary;
   4854   g->next->next = got_per_bfd_arg.current;
   4855 
   4856   /* GG is now the master GOT, and G is the primary GOT.  */
   4857   gg = g;
   4858   g = g->next;
   4859 
   4860   /* Map the output bfd to the primary got.  That's what we're going
   4861      to use for bfds that use GOT16 or GOT_PAGE relocations that we
   4862      didn't mark in check_relocs, and we want a quick way to find it.
   4863      We can't just use gg->next because we're going to reverse the
   4864      list.  */
   4865   mips_elf_replace_bfd_got (abfd, g);
   4866 
   4867   /* Every symbol that is referenced in a dynamic relocation must be
   4868      present in the primary GOT, so arrange for them to appear after
   4869      those that are actually referenced.  */
   4870   gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
   4871   g->global_gotno = gg->global_gotno;
   4872 
   4873   tga.info = info;
   4874   tga.value = GGA_RELOC_ONLY;
   4875   htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
   4876   tga.value = GGA_NORMAL;
   4877   htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
   4878 
   4879   /* Now go through the GOTs assigning them offset ranges.
   4880      [assigned_low_gotno, local_gotno[ will be set to the range of local
   4881      entries in each GOT.  We can then compute the end of a GOT by
   4882      adding local_gotno to global_gotno.  We reverse the list and make
   4883      it circular since then we'll be able to quickly compute the
   4884      beginning of a GOT, by computing the end of its predecessor.  To
   4885      avoid special cases for the primary GOT, while still preserving
   4886      assertions that are valid for both single- and multi-got links,
   4887      we arrange for the main got struct to have the right number of
   4888      global entries, but set its local_gotno such that the initial
   4889      offset of the primary GOT is zero.  Remember that the primary GOT
   4890      will become the last item in the circular linked list, so it
   4891      points back to the master GOT.  */
   4892   gg->local_gotno = -g->global_gotno;
   4893   gg->global_gotno = g->global_gotno;
   4894   gg->tls_gotno = 0;
   4895   assign = 0;
   4896   gg->next = gg;
   4897 
   4898   do
   4899     {
   4900       struct mips_got_info *gn;
   4901 
   4902       assign += htab->reserved_gotno;
   4903       g->assigned_low_gotno = assign;
   4904       g->local_gotno += assign;
   4905       g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
   4906       g->assigned_high_gotno = g->local_gotno - 1;
   4907       assign = g->local_gotno + g->global_gotno + g->tls_gotno;
   4908 
   4909       /* Take g out of the direct list, and push it onto the reversed
   4910 	 list that gg points to.  g->next is guaranteed to be nonnull after
   4911 	 this operation, as required by mips_elf_initialize_tls_index. */
   4912       gn = g->next;
   4913       g->next = gg->next;
   4914       gg->next = g;
   4915 
   4916       /* Set up any TLS entries.  We always place the TLS entries after
   4917 	 all non-TLS entries.  */
   4918       g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
   4919       tga.g = g;
   4920       tga.value = MIPS_ELF_GOT_SIZE (abfd);
   4921       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
   4922       if (!tga.g)
   4923 	return FALSE;
   4924       BFD_ASSERT (g->tls_assigned_gotno == assign);
   4925 
   4926       /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
   4927       g = gn;
   4928 
   4929       /* Forbid global symbols in every non-primary GOT from having
   4930 	 lazy-binding stubs.  */
   4931       if (g)
   4932 	htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
   4933     }
   4934   while (g);
   4935 
   4936   got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
   4937 
   4938   needed_relocs = 0;
   4939   for (g = gg->next; g && g->next != gg; g = g->next)
   4940     {
   4941       unsigned int save_assign;
   4942 
   4943       /* Assign offsets to global GOT entries and count how many
   4944 	 relocations they need.  */
   4945       save_assign = g->assigned_low_gotno;
   4946       g->assigned_low_gotno = g->local_gotno;
   4947       tga.info = info;
   4948       tga.value = MIPS_ELF_GOT_SIZE (abfd);
   4949       tga.g = g;
   4950       htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
   4951       if (!tga.g)
   4952 	return FALSE;
   4953       BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
   4954       g->assigned_low_gotno = save_assign;
   4955 
   4956       if (info->shared)
   4957 	{
   4958 	  g->relocs += g->local_gotno - g->assigned_low_gotno;
   4959 	  BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
   4960 		      + g->next->global_gotno
   4961 		      + g->next->tls_gotno
   4962 		      + htab->reserved_gotno);
   4963 	}
   4964       needed_relocs += g->relocs;
   4965     }
   4966   needed_relocs += g->relocs;
   4967 
   4968   if (needed_relocs)
   4969     mips_elf_allocate_dynamic_relocations (dynobj, info,
   4970 					   needed_relocs);
   4971 
   4972   return TRUE;
   4973 }
   4974 
   4975 
   4976 /* Returns the first relocation of type r_type found, beginning with
   4978    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
   4979 
   4980 static const Elf_Internal_Rela *
   4981 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
   4982 			  const Elf_Internal_Rela *relocation,
   4983 			  const Elf_Internal_Rela *relend)
   4984 {
   4985   unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
   4986 
   4987   while (relocation < relend)
   4988     {
   4989       if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
   4990 	  && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
   4991 	return relocation;
   4992 
   4993       ++relocation;
   4994     }
   4995 
   4996   /* We didn't find it.  */
   4997   return NULL;
   4998 }
   4999 
   5000 /* Return whether an input relocation is against a local symbol.  */
   5001 
   5002 static bfd_boolean
   5003 mips_elf_local_relocation_p (bfd *input_bfd,
   5004 			     const Elf_Internal_Rela *relocation,
   5005 			     asection **local_sections)
   5006 {
   5007   unsigned long r_symndx;
   5008   Elf_Internal_Shdr *symtab_hdr;
   5009   size_t extsymoff;
   5010 
   5011   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
   5012   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   5013   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
   5014 
   5015   if (r_symndx < extsymoff)
   5016     return TRUE;
   5017   if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
   5018     return TRUE;
   5019 
   5020   return FALSE;
   5021 }
   5022 
   5023 /* Sign-extend VALUE, which has the indicated number of BITS.  */
   5025 
   5026 bfd_vma
   5027 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
   5028 {
   5029   if (value & ((bfd_vma) 1 << (bits - 1)))
   5030     /* VALUE is negative.  */
   5031     value |= ((bfd_vma) - 1) << bits;
   5032 
   5033   return value;
   5034 }
   5035 
   5036 /* Return non-zero if the indicated VALUE has overflowed the maximum
   5037    range expressible by a signed number with the indicated number of
   5038    BITS.  */
   5039 
   5040 static bfd_boolean
   5041 mips_elf_overflow_p (bfd_vma value, int bits)
   5042 {
   5043   bfd_signed_vma svalue = (bfd_signed_vma) value;
   5044 
   5045   if (svalue > (1 << (bits - 1)) - 1)
   5046     /* The value is too big.  */
   5047     return TRUE;
   5048   else if (svalue < -(1 << (bits - 1)))
   5049     /* The value is too small.  */
   5050     return TRUE;
   5051 
   5052   /* All is well.  */
   5053   return FALSE;
   5054 }
   5055 
   5056 /* Calculate the %high function.  */
   5057 
   5058 static bfd_vma
   5059 mips_elf_high (bfd_vma value)
   5060 {
   5061   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
   5062 }
   5063 
   5064 /* Calculate the %higher function.  */
   5065 
   5066 static bfd_vma
   5067 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
   5068 {
   5069 #ifdef BFD64
   5070   return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
   5071 #else
   5072   abort ();
   5073   return MINUS_ONE;
   5074 #endif
   5075 }
   5076 
   5077 /* Calculate the %highest function.  */
   5078 
   5079 static bfd_vma
   5080 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
   5081 {
   5082 #ifdef BFD64
   5083   return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
   5084 #else
   5085   abort ();
   5086   return MINUS_ONE;
   5087 #endif
   5088 }
   5089 
   5090 /* Create the .compact_rel section.  */
   5092 
   5093 static bfd_boolean
   5094 mips_elf_create_compact_rel_section
   5095   (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
   5096 {
   5097   flagword flags;
   5098   register asection *s;
   5099 
   5100   if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
   5101     {
   5102       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
   5103 	       | SEC_READONLY);
   5104 
   5105       s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
   5106       if (s == NULL
   5107 	  || ! bfd_set_section_alignment (abfd, s,
   5108 					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   5109 	return FALSE;
   5110 
   5111       s->size = sizeof (Elf32_External_compact_rel);
   5112     }
   5113 
   5114   return TRUE;
   5115 }
   5116 
   5117 /* Create the .got section to hold the global offset table.  */
   5118 
   5119 static bfd_boolean
   5120 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
   5121 {
   5122   flagword flags;
   5123   register asection *s;
   5124   struct elf_link_hash_entry *h;
   5125   struct bfd_link_hash_entry *bh;
   5126   struct mips_elf_link_hash_table *htab;
   5127 
   5128   htab = mips_elf_hash_table (info);
   5129   BFD_ASSERT (htab != NULL);
   5130 
   5131   /* This function may be called more than once.  */
   5132   if (htab->sgot)
   5133     return TRUE;
   5134 
   5135   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   5136 	   | SEC_LINKER_CREATED);
   5137 
   5138   /* We have to use an alignment of 2**4 here because this is hardcoded
   5139      in the function stub generation and in the linker script.  */
   5140   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   5141   if (s == NULL
   5142       || ! bfd_set_section_alignment (abfd, s, 4))
   5143     return FALSE;
   5144   htab->sgot = s;
   5145 
   5146   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
   5147      linker script because we don't want to define the symbol if we
   5148      are not creating a global offset table.  */
   5149   bh = NULL;
   5150   if (! (_bfd_generic_link_add_one_symbol
   5151 	 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
   5152 	  0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
   5153     return FALSE;
   5154 
   5155   h = (struct elf_link_hash_entry *) bh;
   5156   h->non_elf = 0;
   5157   h->def_regular = 1;
   5158   h->type = STT_OBJECT;
   5159   h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
   5160   elf_hash_table (info)->hgot = h;
   5161 
   5162   if (info->shared
   5163       && ! bfd_elf_link_record_dynamic_symbol (info, h))
   5164     return FALSE;
   5165 
   5166   htab->got_info = mips_elf_create_got_info (abfd);
   5167   mips_elf_section_data (s)->elf.this_hdr.sh_flags
   5168     |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
   5169 
   5170   /* We also need a .got.plt section when generating PLTs.  */
   5171   s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
   5172 					  SEC_ALLOC | SEC_LOAD
   5173 					  | SEC_HAS_CONTENTS
   5174 					  | SEC_IN_MEMORY
   5175 					  | SEC_LINKER_CREATED);
   5176   if (s == NULL)
   5177     return FALSE;
   5178   htab->sgotplt = s;
   5179 
   5180   return TRUE;
   5181 }
   5182 
   5183 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
   5185    __GOTT_INDEX__ symbols.  These symbols are only special for
   5186    shared objects; they are not used in executables.  */
   5187 
   5188 static bfd_boolean
   5189 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
   5190 {
   5191   return (mips_elf_hash_table (info)->is_vxworks
   5192 	  && info->shared
   5193 	  && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
   5194 	      || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
   5195 }
   5196 
   5197 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
   5198    require an la25 stub.  See also mips_elf_local_pic_function_p,
   5199    which determines whether the destination function ever requires a
   5200    stub.  */
   5201 
   5202 static bfd_boolean
   5203 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
   5204 				     bfd_boolean target_is_16_bit_code_p)
   5205 {
   5206   /* We specifically ignore branches and jumps from EF_PIC objects,
   5207      where the onus is on the compiler or programmer to perform any
   5208      necessary initialization of $25.  Sometimes such initialization
   5209      is unnecessary; for example, -mno-shared functions do not use
   5210      the incoming value of $25, and may therefore be called directly.  */
   5211   if (PIC_OBJECT_P (input_bfd))
   5212     return FALSE;
   5213 
   5214   switch (r_type)
   5215     {
   5216     case R_MIPS_26:
   5217     case R_MIPS_PC16:
   5218     case R_MIPS_PC21_S2:
   5219     case R_MIPS_PC26_S2:
   5220     case R_MICROMIPS_26_S1:
   5221     case R_MICROMIPS_PC7_S1:
   5222     case R_MICROMIPS_PC10_S1:
   5223     case R_MICROMIPS_PC16_S1:
   5224     case R_MICROMIPS_PC23_S2:
   5225       return TRUE;
   5226 
   5227     case R_MIPS16_26:
   5228       return !target_is_16_bit_code_p;
   5229 
   5230     default:
   5231       return FALSE;
   5232     }
   5233 }
   5234 
   5235 /* Calculate the value produced by the RELOCATION (which comes from
   5237    the INPUT_BFD).  The ADDEND is the addend to use for this
   5238    RELOCATION; RELOCATION->R_ADDEND is ignored.
   5239 
   5240    The result of the relocation calculation is stored in VALUEP.
   5241    On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
   5242    is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
   5243 
   5244    This function returns bfd_reloc_continue if the caller need take no
   5245    further action regarding this relocation, bfd_reloc_notsupported if
   5246    something goes dramatically wrong, bfd_reloc_overflow if an
   5247    overflow occurs, and bfd_reloc_ok to indicate success.  */
   5248 
   5249 static bfd_reloc_status_type
   5250 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
   5251 			       asection *input_section,
   5252 			       struct bfd_link_info *info,
   5253 			       const Elf_Internal_Rela *relocation,
   5254 			       bfd_vma addend, reloc_howto_type *howto,
   5255 			       Elf_Internal_Sym *local_syms,
   5256 			       asection **local_sections, bfd_vma *valuep,
   5257 			       const char **namep,
   5258 			       bfd_boolean *cross_mode_jump_p,
   5259 			       bfd_boolean save_addend)
   5260 {
   5261   /* The eventual value we will return.  */
   5262   bfd_vma value;
   5263   /* The address of the symbol against which the relocation is
   5264      occurring.  */
   5265   bfd_vma symbol = 0;
   5266   /* The final GP value to be used for the relocatable, executable, or
   5267      shared object file being produced.  */
   5268   bfd_vma gp;
   5269   /* The place (section offset or address) of the storage unit being
   5270      relocated.  */
   5271   bfd_vma p;
   5272   /* The value of GP used to create the relocatable object.  */
   5273   bfd_vma gp0;
   5274   /* The offset into the global offset table at which the address of
   5275      the relocation entry symbol, adjusted by the addend, resides
   5276      during execution.  */
   5277   bfd_vma g = MINUS_ONE;
   5278   /* The section in which the symbol referenced by the relocation is
   5279      located.  */
   5280   asection *sec = NULL;
   5281   struct mips_elf_link_hash_entry *h = NULL;
   5282   /* TRUE if the symbol referred to by this relocation is a local
   5283      symbol.  */
   5284   bfd_boolean local_p, was_local_p;
   5285   /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
   5286   bfd_boolean gp_disp_p = FALSE;
   5287   /* TRUE if the symbol referred to by this relocation is
   5288      "__gnu_local_gp".  */
   5289   bfd_boolean gnu_local_gp_p = FALSE;
   5290   Elf_Internal_Shdr *symtab_hdr;
   5291   size_t extsymoff;
   5292   unsigned long r_symndx;
   5293   int r_type;
   5294   /* TRUE if overflow occurred during the calculation of the
   5295      relocation value.  */
   5296   bfd_boolean overflowed_p;
   5297   /* TRUE if this relocation refers to a MIPS16 function.  */
   5298   bfd_boolean target_is_16_bit_code_p = FALSE;
   5299   bfd_boolean target_is_micromips_code_p = FALSE;
   5300   struct mips_elf_link_hash_table *htab;
   5301   bfd *dynobj;
   5302 
   5303   dynobj = elf_hash_table (info)->dynobj;
   5304   htab = mips_elf_hash_table (info);
   5305   BFD_ASSERT (htab != NULL);
   5306 
   5307   /* Parse the relocation.  */
   5308   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
   5309   r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   5310   p = (input_section->output_section->vma
   5311        + input_section->output_offset
   5312        + relocation->r_offset);
   5313 
   5314   /* Assume that there will be no overflow.  */
   5315   overflowed_p = FALSE;
   5316 
   5317   /* Figure out whether or not the symbol is local, and get the offset
   5318      used in the array of hash table entries.  */
   5319   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   5320   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
   5321 					 local_sections);
   5322   was_local_p = local_p;
   5323   if (! elf_bad_symtab (input_bfd))
   5324     extsymoff = symtab_hdr->sh_info;
   5325   else
   5326     {
   5327       /* The symbol table does not follow the rule that local symbols
   5328 	 must come before globals.  */
   5329       extsymoff = 0;
   5330     }
   5331 
   5332   /* Figure out the value of the symbol.  */
   5333   if (local_p)
   5334     {
   5335       Elf_Internal_Sym *sym;
   5336 
   5337       sym = local_syms + r_symndx;
   5338       sec = local_sections[r_symndx];
   5339 
   5340       symbol = sec->output_section->vma + sec->output_offset;
   5341       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
   5342 	  || (sec->flags & SEC_MERGE))
   5343 	symbol += sym->st_value;
   5344       if ((sec->flags & SEC_MERGE)
   5345 	  && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   5346 	{
   5347 	  addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
   5348 	  addend -= symbol;
   5349 	  addend += sec->output_section->vma + sec->output_offset;
   5350 	}
   5351 
   5352       /* MIPS16/microMIPS text labels should be treated as odd.  */
   5353       if (ELF_ST_IS_COMPRESSED (sym->st_other))
   5354 	++symbol;
   5355 
   5356       /* Record the name of this symbol, for our caller.  */
   5357       *namep = bfd_elf_string_from_elf_section (input_bfd,
   5358 						symtab_hdr->sh_link,
   5359 						sym->st_name);
   5360       if (*namep == '\0')
   5361 	*namep = bfd_section_name (input_bfd, sec);
   5362 
   5363       target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
   5364       target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
   5365     }
   5366   else
   5367     {
   5368       /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
   5369 
   5370       /* For global symbols we look up the symbol in the hash-table.  */
   5371       h = ((struct mips_elf_link_hash_entry *)
   5372 	   elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
   5373       /* Find the real hash-table entry for this symbol.  */
   5374       while (h->root.root.type == bfd_link_hash_indirect
   5375 	     || h->root.root.type == bfd_link_hash_warning)
   5376 	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   5377 
   5378       /* Record the name of this symbol, for our caller.  */
   5379       *namep = h->root.root.root.string;
   5380 
   5381       /* See if this is the special _gp_disp symbol.  Note that such a
   5382 	 symbol must always be a global symbol.  */
   5383       if (strcmp (*namep, "_gp_disp") == 0
   5384 	  && ! NEWABI_P (input_bfd))
   5385 	{
   5386 	  /* Relocations against _gp_disp are permitted only with
   5387 	     R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
   5388 	  if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
   5389 	    return bfd_reloc_notsupported;
   5390 
   5391 	  gp_disp_p = TRUE;
   5392 	}
   5393       /* See if this is the special _gp symbol.  Note that such a
   5394 	 symbol must always be a global symbol.  */
   5395       else if (strcmp (*namep, "__gnu_local_gp") == 0)
   5396 	gnu_local_gp_p = TRUE;
   5397 
   5398 
   5399       /* If this symbol is defined, calculate its address.  Note that
   5400 	 _gp_disp is a magic symbol, always implicitly defined by the
   5401 	 linker, so it's inappropriate to check to see whether or not
   5402 	 its defined.  */
   5403       else if ((h->root.root.type == bfd_link_hash_defined
   5404 		|| h->root.root.type == bfd_link_hash_defweak)
   5405 	       && h->root.root.u.def.section)
   5406 	{
   5407 	  sec = h->root.root.u.def.section;
   5408 	  if (sec->output_section)
   5409 	    symbol = (h->root.root.u.def.value
   5410 		      + sec->output_section->vma
   5411 		      + sec->output_offset);
   5412 	  else
   5413 	    symbol = h->root.root.u.def.value;
   5414 	}
   5415       else if (h->root.root.type == bfd_link_hash_undefweak)
   5416 	/* We allow relocations against undefined weak symbols, giving
   5417 	   it the value zero, so that you can undefined weak functions
   5418 	   and check to see if they exist by looking at their
   5419 	   addresses.  */
   5420 	symbol = 0;
   5421       else if (info->unresolved_syms_in_objects == RM_IGNORE
   5422 	       && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
   5423 	symbol = 0;
   5424       else if (strcmp (*namep, SGI_COMPAT (input_bfd)
   5425 		       ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
   5426 	{
   5427 	  /* If this is a dynamic link, we should have created a
   5428 	     _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
   5429 	     in in _bfd_mips_elf_create_dynamic_sections.
   5430 	     Otherwise, we should define the symbol with a value of 0.
   5431 	     FIXME: It should probably get into the symbol table
   5432 	     somehow as well.  */
   5433 	  BFD_ASSERT (! info->shared);
   5434 	  BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
   5435 	  symbol = 0;
   5436 	}
   5437       else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
   5438 	{
   5439 	  /* This is an optional symbol - an Irix specific extension to the
   5440 	     ELF spec.  Ignore it for now.
   5441 	     XXX - FIXME - there is more to the spec for OPTIONAL symbols
   5442 	     than simply ignoring them, but we do not handle this for now.
   5443 	     For information see the "64-bit ELF Object File Specification"
   5444 	     which is available from here:
   5445 	     http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
   5446 	  symbol = 0;
   5447 	}
   5448       else if ((*info->callbacks->undefined_symbol)
   5449 	       (info, h->root.root.root.string, input_bfd,
   5450 		input_section, relocation->r_offset,
   5451 		(info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
   5452 		 || ELF_ST_VISIBILITY (h->root.other)))
   5453 	{
   5454 	  return bfd_reloc_undefined;
   5455 	}
   5456       else
   5457 	{
   5458 	  return bfd_reloc_notsupported;
   5459 	}
   5460 
   5461       target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
   5462       target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
   5463     }
   5464 
   5465   /* If this is a reference to a 16-bit function with a stub, we need
   5466      to redirect the relocation to the stub unless:
   5467 
   5468      (a) the relocation is for a MIPS16 JAL;
   5469 
   5470      (b) the relocation is for a MIPS16 PIC call, and there are no
   5471 	 non-MIPS16 uses of the GOT slot; or
   5472 
   5473      (c) the section allows direct references to MIPS16 functions.  */
   5474   if (r_type != R_MIPS16_26
   5475       && !info->relocatable
   5476       && ((h != NULL
   5477 	   && h->fn_stub != NULL
   5478 	   && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
   5479 	  || (local_p
   5480 	      && mips_elf_tdata (input_bfd)->local_stubs != NULL
   5481 	      && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
   5482       && !section_allows_mips16_refs_p (input_section))
   5483     {
   5484       /* This is a 32- or 64-bit call to a 16-bit function.  We should
   5485 	 have already noticed that we were going to need the
   5486 	 stub.  */
   5487       if (local_p)
   5488 	{
   5489 	  sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
   5490 	  value = 0;
   5491 	}
   5492       else
   5493 	{
   5494 	  BFD_ASSERT (h->need_fn_stub);
   5495 	  if (h->la25_stub)
   5496 	    {
   5497 	      /* If a LA25 header for the stub itself exists, point to the
   5498 		 prepended LUI/ADDIU sequence.  */
   5499 	      sec = h->la25_stub->stub_section;
   5500 	      value = h->la25_stub->offset;
   5501 	    }
   5502 	  else
   5503 	    {
   5504 	      sec = h->fn_stub;
   5505 	      value = 0;
   5506 	    }
   5507 	}
   5508 
   5509       symbol = sec->output_section->vma + sec->output_offset + value;
   5510       /* The target is 16-bit, but the stub isn't.  */
   5511       target_is_16_bit_code_p = FALSE;
   5512     }
   5513   /* If this is a MIPS16 call with a stub, that is made through the PLT or
   5514      to a standard MIPS function, we need to redirect the call to the stub.
   5515      Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
   5516      indirect calls should use an indirect stub instead.  */
   5517   else if (r_type == R_MIPS16_26 && !info->relocatable
   5518 	   && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
   5519 	       || (local_p
   5520 		   && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
   5521 		   && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
   5522 	   && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
   5523     {
   5524       if (local_p)
   5525 	sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
   5526       else
   5527 	{
   5528 	  /* If both call_stub and call_fp_stub are defined, we can figure
   5529 	     out which one to use by checking which one appears in the input
   5530 	     file.  */
   5531 	  if (h->call_stub != NULL && h->call_fp_stub != NULL)
   5532 	    {
   5533 	      asection *o;
   5534 
   5535 	      sec = NULL;
   5536 	      for (o = input_bfd->sections; o != NULL; o = o->next)
   5537 		{
   5538 		  if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
   5539 		    {
   5540 		      sec = h->call_fp_stub;
   5541 		      break;
   5542 		    }
   5543 		}
   5544 	      if (sec == NULL)
   5545 		sec = h->call_stub;
   5546 	    }
   5547 	  else if (h->call_stub != NULL)
   5548 	    sec = h->call_stub;
   5549 	  else
   5550 	    sec = h->call_fp_stub;
   5551   	}
   5552 
   5553       BFD_ASSERT (sec->size > 0);
   5554       symbol = sec->output_section->vma + sec->output_offset;
   5555     }
   5556   /* If this is a direct call to a PIC function, redirect to the
   5557      non-PIC stub.  */
   5558   else if (h != NULL && h->la25_stub
   5559 	   && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
   5560 						   target_is_16_bit_code_p))
   5561     symbol = (h->la25_stub->stub_section->output_section->vma
   5562 	      + h->la25_stub->stub_section->output_offset
   5563 	      + h->la25_stub->offset);
   5564   /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
   5565      entry is used if a standard PLT entry has also been made.  In this
   5566      case the symbol will have been set by mips_elf_set_plt_sym_value
   5567      to point to the standard PLT entry, so redirect to the compressed
   5568      one.  */
   5569   else if ((r_type == R_MIPS16_26 || r_type == R_MICROMIPS_26_S1)
   5570 	   && !info->relocatable
   5571 	   && h != NULL
   5572 	   && h->use_plt_entry
   5573 	   && h->root.plt.plist->comp_offset != MINUS_ONE
   5574 	   && h->root.plt.plist->mips_offset != MINUS_ONE)
   5575     {
   5576       bfd_boolean micromips_p = MICROMIPS_P (abfd);
   5577 
   5578       sec = htab->splt;
   5579       symbol = (sec->output_section->vma
   5580 		+ sec->output_offset
   5581 		+ htab->plt_header_size
   5582 		+ htab->plt_mips_offset
   5583 		+ h->root.plt.plist->comp_offset
   5584 		+ 1);
   5585 
   5586       target_is_16_bit_code_p = !micromips_p;
   5587       target_is_micromips_code_p = micromips_p;
   5588     }
   5589 
   5590   /* Make sure MIPS16 and microMIPS are not used together.  */
   5591   if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
   5592       || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
   5593    {
   5594       (*_bfd_error_handler)
   5595 	(_("MIPS16 and microMIPS functions cannot call each other"));
   5596       return bfd_reloc_notsupported;
   5597    }
   5598 
   5599   /* Calls from 16-bit code to 32-bit code and vice versa require the
   5600      mode change.  However, we can ignore calls to undefined weak symbols,
   5601      which should never be executed at runtime.  This exception is important
   5602      because the assembly writer may have "known" that any definition of the
   5603      symbol would be 16-bit code, and that direct jumps were therefore
   5604      acceptable.  */
   5605   *cross_mode_jump_p = (!info->relocatable
   5606 			&& !(h && h->root.root.type == bfd_link_hash_undefweak)
   5607 			&& ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
   5608 			    || (r_type == R_MICROMIPS_26_S1
   5609 				&& !target_is_micromips_code_p)
   5610 			    || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
   5611 				&& (target_is_16_bit_code_p
   5612 				    || target_is_micromips_code_p))));
   5613 
   5614   local_p = (h == NULL || mips_use_local_got_p (info, h));
   5615 
   5616   gp0 = _bfd_get_gp_value (input_bfd);
   5617   gp = _bfd_get_gp_value (abfd);
   5618   if (htab->got_info)
   5619     gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
   5620 
   5621   if (gnu_local_gp_p)
   5622     symbol = gp;
   5623 
   5624   /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
   5625      to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
   5626      corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.  */
   5627   if (got_page_reloc_p (r_type) && !local_p)
   5628     {
   5629       r_type = (micromips_reloc_p (r_type)
   5630 		? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
   5631       addend = 0;
   5632     }
   5633 
   5634   /* If we haven't already determined the GOT offset, and we're going
   5635      to need it, get it now.  */
   5636   switch (r_type)
   5637     {
   5638     case R_MIPS16_CALL16:
   5639     case R_MIPS16_GOT16:
   5640     case R_MIPS_CALL16:
   5641     case R_MIPS_GOT16:
   5642     case R_MIPS_GOT_DISP:
   5643     case R_MIPS_GOT_HI16:
   5644     case R_MIPS_CALL_HI16:
   5645     case R_MIPS_GOT_LO16:
   5646     case R_MIPS_CALL_LO16:
   5647     case R_MICROMIPS_CALL16:
   5648     case R_MICROMIPS_GOT16:
   5649     case R_MICROMIPS_GOT_DISP:
   5650     case R_MICROMIPS_GOT_HI16:
   5651     case R_MICROMIPS_CALL_HI16:
   5652     case R_MICROMIPS_GOT_LO16:
   5653     case R_MICROMIPS_CALL_LO16:
   5654     case R_MIPS_TLS_GD:
   5655     case R_MIPS_TLS_GOTTPREL:
   5656     case R_MIPS_TLS_LDM:
   5657     case R_MIPS16_TLS_GD:
   5658     case R_MIPS16_TLS_GOTTPREL:
   5659     case R_MIPS16_TLS_LDM:
   5660     case R_MICROMIPS_TLS_GD:
   5661     case R_MICROMIPS_TLS_GOTTPREL:
   5662     case R_MICROMIPS_TLS_LDM:
   5663       /* Find the index into the GOT where this value is located.  */
   5664       if (tls_ldm_reloc_p (r_type))
   5665 	{
   5666 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
   5667 					0, 0, NULL, r_type);
   5668 	  if (g == MINUS_ONE)
   5669 	    return bfd_reloc_outofrange;
   5670 	}
   5671       else if (!local_p)
   5672 	{
   5673 	  /* On VxWorks, CALL relocations should refer to the .got.plt
   5674 	     entry, which is initialized to point at the PLT stub.  */
   5675 	  if (htab->is_vxworks
   5676 	      && (call_hi16_reloc_p (r_type)
   5677 		  || call_lo16_reloc_p (r_type)
   5678 		  || call16_reloc_p (r_type)))
   5679 	    {
   5680 	      BFD_ASSERT (addend == 0);
   5681 	      BFD_ASSERT (h->root.needs_plt);
   5682 	      g = mips_elf_gotplt_index (info, &h->root);
   5683 	    }
   5684 	  else
   5685 	    {
   5686 	      BFD_ASSERT (addend == 0);
   5687 	      g = mips_elf_global_got_index (abfd, info, input_bfd,
   5688 					     &h->root, r_type);
   5689 	      if (!TLS_RELOC_P (r_type)
   5690 		  && !elf_hash_table (info)->dynamic_sections_created)
   5691 		/* This is a static link.  We must initialize the GOT entry.  */
   5692 		MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
   5693 	    }
   5694 	}
   5695       else if (!htab->is_vxworks
   5696 	       && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
   5697 	/* The calculation below does not involve "g".  */
   5698 	break;
   5699       else
   5700 	{
   5701 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
   5702 					symbol + addend, r_symndx, h, r_type);
   5703 	  if (g == MINUS_ONE)
   5704 	    return bfd_reloc_outofrange;
   5705 	}
   5706 
   5707       /* Convert GOT indices to actual offsets.  */
   5708       g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
   5709       break;
   5710     }
   5711 
   5712   /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
   5713      symbols are resolved by the loader.  Add them to .rela.dyn.  */
   5714   if (h != NULL && is_gott_symbol (info, &h->root))
   5715     {
   5716       Elf_Internal_Rela outrel;
   5717       bfd_byte *loc;
   5718       asection *s;
   5719 
   5720       s = mips_elf_rel_dyn_section (info, FALSE);
   5721       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
   5722 
   5723       outrel.r_offset = (input_section->output_section->vma
   5724 			 + input_section->output_offset
   5725 			 + relocation->r_offset);
   5726       outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
   5727       outrel.r_addend = addend;
   5728       bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
   5729 
   5730       /* If we've written this relocation for a readonly section,
   5731 	 we need to set DF_TEXTREL again, so that we do not delete the
   5732 	 DT_TEXTREL tag.  */
   5733       if (MIPS_ELF_READONLY_SECTION (input_section))
   5734 	info->flags |= DF_TEXTREL;
   5735 
   5736       *valuep = 0;
   5737       return bfd_reloc_ok;
   5738     }
   5739 
   5740   /* Figure out what kind of relocation is being performed.  */
   5741   switch (r_type)
   5742     {
   5743     case R_MIPS_NONE:
   5744       return bfd_reloc_continue;
   5745 
   5746     case R_MIPS_16:
   5747       if (howto->partial_inplace)
   5748 	addend = _bfd_mips_elf_sign_extend (addend, 16);
   5749       value = symbol + addend;
   5750       overflowed_p = mips_elf_overflow_p (value, 16);
   5751       break;
   5752 
   5753     case R_MIPS_32:
   5754     case R_MIPS_REL32:
   5755     case R_MIPS_64:
   5756       if ((info->shared
   5757 	   || (htab->root.dynamic_sections_created
   5758 	       && h != NULL
   5759 	       && h->root.def_dynamic
   5760 	       && !h->root.def_regular
   5761 	       && !h->has_static_relocs))
   5762 	  && r_symndx != STN_UNDEF
   5763 	  && (h == NULL
   5764 	      || h->root.root.type != bfd_link_hash_undefweak
   5765 	      || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
   5766 	  && (input_section->flags & SEC_ALLOC) != 0)
   5767 	{
   5768 	  /* If we're creating a shared library, then we can't know
   5769 	     where the symbol will end up.  So, we create a relocation
   5770 	     record in the output, and leave the job up to the dynamic
   5771 	     linker.  We must do the same for executable references to
   5772 	     shared library symbols, unless we've decided to use copy
   5773 	     relocs or PLTs instead.  */
   5774 	  value = addend;
   5775 	  if (!mips_elf_create_dynamic_relocation (abfd,
   5776 						   info,
   5777 						   relocation,
   5778 						   h,
   5779 						   sec,
   5780 						   symbol,
   5781 						   &value,
   5782 						   input_section))
   5783 	    return bfd_reloc_undefined;
   5784 	}
   5785       else
   5786 	{
   5787 	  if (r_type != R_MIPS_REL32)
   5788 	    value = symbol + addend;
   5789 	  else
   5790 	    value = addend;
   5791 	}
   5792       value &= howto->dst_mask;
   5793       break;
   5794 
   5795     case R_MIPS_PC32:
   5796       value = symbol + addend - p;
   5797       value &= howto->dst_mask;
   5798       break;
   5799 
   5800     case R_MIPS16_26:
   5801       /* The calculation for R_MIPS16_26 is just the same as for an
   5802 	 R_MIPS_26.  It's only the storage of the relocated field into
   5803 	 the output file that's different.  That's handled in
   5804 	 mips_elf_perform_relocation.  So, we just fall through to the
   5805 	 R_MIPS_26 case here.  */
   5806     case R_MIPS_26:
   5807     case R_MICROMIPS_26_S1:
   5808       {
   5809 	unsigned int shift;
   5810 
   5811 	/* Make sure the target of JALX is word-aligned.  Bit 0 must be
   5812 	   the correct ISA mode selector and bit 1 must be 0.  */
   5813 	if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
   5814 	  return bfd_reloc_outofrange;
   5815 
   5816 	/* Shift is 2, unusually, for microMIPS JALX.  */
   5817 	shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
   5818 
   5819 	if (was_local_p)
   5820 	  value = addend | ((p + 4) & (0xfc000000 << shift));
   5821 	else if (howto->partial_inplace)
   5822 	  value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
   5823 	else
   5824 	  value = addend;
   5825 	value = (value + symbol) >> shift;
   5826 	if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
   5827 	  overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
   5828 	value &= howto->dst_mask;
   5829       }
   5830       break;
   5831 
   5832     case R_MIPS_TLS_DTPREL_HI16:
   5833     case R_MIPS16_TLS_DTPREL_HI16:
   5834     case R_MICROMIPS_TLS_DTPREL_HI16:
   5835       value = (mips_elf_high (addend + symbol - dtprel_base (info))
   5836 	       & howto->dst_mask);
   5837       break;
   5838 
   5839     case R_MIPS_TLS_DTPREL_LO16:
   5840     case R_MIPS_TLS_DTPREL32:
   5841     case R_MIPS_TLS_DTPREL64:
   5842     case R_MIPS16_TLS_DTPREL_LO16:
   5843     case R_MICROMIPS_TLS_DTPREL_LO16:
   5844       value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
   5845       break;
   5846 
   5847     case R_MIPS_TLS_TPREL_HI16:
   5848     case R_MIPS16_TLS_TPREL_HI16:
   5849     case R_MICROMIPS_TLS_TPREL_HI16:
   5850       value = (mips_elf_high (addend + symbol - tprel_base (info))
   5851 	       & howto->dst_mask);
   5852       break;
   5853 
   5854     case R_MIPS_TLS_TPREL_LO16:
   5855     case R_MIPS_TLS_TPREL32:
   5856     case R_MIPS_TLS_TPREL64:
   5857     case R_MIPS16_TLS_TPREL_LO16:
   5858     case R_MICROMIPS_TLS_TPREL_LO16:
   5859       value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
   5860       break;
   5861 
   5862     case R_MIPS_HI16:
   5863     case R_MIPS16_HI16:
   5864     case R_MICROMIPS_HI16:
   5865       if (!gp_disp_p)
   5866 	{
   5867 	  value = mips_elf_high (addend + symbol);
   5868 	  value &= howto->dst_mask;
   5869 	}
   5870       else
   5871 	{
   5872 	  /* For MIPS16 ABI code we generate this sequence
   5873 	        0: li      $v0,%hi(_gp_disp)
   5874 	        4: addiupc $v1,%lo(_gp_disp)
   5875 	        8: sll     $v0,16
   5876 	       12: addu    $v0,$v1
   5877 	       14: move    $gp,$v0
   5878 	     So the offsets of hi and lo relocs are the same, but the
   5879 	     base $pc is that used by the ADDIUPC instruction at $t9 + 4.
   5880 	     ADDIUPC clears the low two bits of the instruction address,
   5881 	     so the base is ($t9 + 4) & ~3.  */
   5882 	  if (r_type == R_MIPS16_HI16)
   5883 	    value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
   5884 	  /* The microMIPS .cpload sequence uses the same assembly
   5885 	     instructions as the traditional psABI version, but the
   5886 	     incoming $t9 has the low bit set.  */
   5887 	  else if (r_type == R_MICROMIPS_HI16)
   5888 	    value = mips_elf_high (addend + gp - p - 1);
   5889 	  else
   5890 	    value = mips_elf_high (addend + gp - p);
   5891 	  overflowed_p = mips_elf_overflow_p (value, 16);
   5892 	}
   5893       break;
   5894 
   5895     case R_MIPS_LO16:
   5896     case R_MIPS16_LO16:
   5897     case R_MICROMIPS_LO16:
   5898     case R_MICROMIPS_HI0_LO16:
   5899       if (!gp_disp_p)
   5900 	value = (symbol + addend) & howto->dst_mask;
   5901       else
   5902 	{
   5903 	  /* See the comment for R_MIPS16_HI16 above for the reason
   5904 	     for this conditional.  */
   5905 	  if (r_type == R_MIPS16_LO16)
   5906 	    value = addend + gp - (p & ~(bfd_vma) 0x3);
   5907 	  else if (r_type == R_MICROMIPS_LO16
   5908 		   || r_type == R_MICROMIPS_HI0_LO16)
   5909 	    value = addend + gp - p + 3;
   5910 	  else
   5911 	    value = addend + gp - p + 4;
   5912 	  /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
   5913 	     for overflow.  But, on, say, IRIX5, relocations against
   5914 	     _gp_disp are normally generated from the .cpload
   5915 	     pseudo-op.  It generates code that normally looks like
   5916 	     this:
   5917 
   5918 	       lui    $gp,%hi(_gp_disp)
   5919 	       addiu  $gp,$gp,%lo(_gp_disp)
   5920 	       addu   $gp,$gp,$t9
   5921 
   5922 	     Here $t9 holds the address of the function being called,
   5923 	     as required by the MIPS ELF ABI.  The R_MIPS_LO16
   5924 	     relocation can easily overflow in this situation, but the
   5925 	     R_MIPS_HI16 relocation will handle the overflow.
   5926 	     Therefore, we consider this a bug in the MIPS ABI, and do
   5927 	     not check for overflow here.  */
   5928 	}
   5929       break;
   5930 
   5931     case R_MIPS_LITERAL:
   5932     case R_MICROMIPS_LITERAL:
   5933       /* Because we don't merge literal sections, we can handle this
   5934 	 just like R_MIPS_GPREL16.  In the long run, we should merge
   5935 	 shared literals, and then we will need to additional work
   5936 	 here.  */
   5937 
   5938       /* Fall through.  */
   5939 
   5940     case R_MIPS16_GPREL:
   5941       /* The R_MIPS16_GPREL performs the same calculation as
   5942 	 R_MIPS_GPREL16, but stores the relocated bits in a different
   5943 	 order.  We don't need to do anything special here; the
   5944 	 differences are handled in mips_elf_perform_relocation.  */
   5945     case R_MIPS_GPREL16:
   5946     case R_MICROMIPS_GPREL7_S2:
   5947     case R_MICROMIPS_GPREL16:
   5948       /* Only sign-extend the addend if it was extracted from the
   5949 	 instruction.  If the addend was separate, leave it alone,
   5950 	 otherwise we may lose significant bits.  */
   5951       if (howto->partial_inplace)
   5952 	addend = _bfd_mips_elf_sign_extend (addend, 16);
   5953       value = symbol + addend - gp;
   5954       /* If the symbol was local, any earlier relocatable links will
   5955 	 have adjusted its addend with the gp offset, so compensate
   5956 	 for that now.  Don't do it for symbols forced local in this
   5957 	 link, though, since they won't have had the gp offset applied
   5958 	 to them before.  */
   5959       if (was_local_p)
   5960 	value += gp0;
   5961       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   5962 	overflowed_p = mips_elf_overflow_p (value, 16);
   5963       break;
   5964 
   5965     case R_MIPS16_GOT16:
   5966     case R_MIPS16_CALL16:
   5967     case R_MIPS_GOT16:
   5968     case R_MIPS_CALL16:
   5969     case R_MICROMIPS_GOT16:
   5970     case R_MICROMIPS_CALL16:
   5971       /* VxWorks does not have separate local and global semantics for
   5972 	 R_MIPS*_GOT16; every relocation evaluates to "G".  */
   5973       if (!htab->is_vxworks && local_p)
   5974 	{
   5975 	  value = mips_elf_got16_entry (abfd, input_bfd, info,
   5976 					symbol + addend, !was_local_p);
   5977 	  if (value == MINUS_ONE)
   5978 	    return bfd_reloc_outofrange;
   5979 	  value
   5980 	    = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
   5981 	  overflowed_p = mips_elf_overflow_p (value, 16);
   5982 	  break;
   5983 	}
   5984 
   5985       /* Fall through.  */
   5986 
   5987     case R_MIPS_TLS_GD:
   5988     case R_MIPS_TLS_GOTTPREL:
   5989     case R_MIPS_TLS_LDM:
   5990     case R_MIPS_GOT_DISP:
   5991     case R_MIPS16_TLS_GD:
   5992     case R_MIPS16_TLS_GOTTPREL:
   5993     case R_MIPS16_TLS_LDM:
   5994     case R_MICROMIPS_TLS_GD:
   5995     case R_MICROMIPS_TLS_GOTTPREL:
   5996     case R_MICROMIPS_TLS_LDM:
   5997     case R_MICROMIPS_GOT_DISP:
   5998       value = g;
   5999       overflowed_p = mips_elf_overflow_p (value, 16);
   6000       break;
   6001 
   6002     case R_MIPS_GPREL32:
   6003       value = (addend + symbol + gp0 - gp);
   6004       if (!save_addend)
   6005 	value &= howto->dst_mask;
   6006       break;
   6007 
   6008     case R_MIPS_PC16:
   6009     case R_MIPS_GNU_REL16_S2:
   6010       if (howto->partial_inplace)
   6011 	addend = _bfd_mips_elf_sign_extend (addend, 18);
   6012 
   6013       if ((symbol + addend) & 3)
   6014 	return bfd_reloc_outofrange;
   6015 
   6016       value = symbol + addend - p;
   6017       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6018 	overflowed_p = mips_elf_overflow_p (value, 18);
   6019       value >>= howto->rightshift;
   6020       value &= howto->dst_mask;
   6021       break;
   6022 
   6023     case R_MIPS_PC21_S2:
   6024       if (howto->partial_inplace)
   6025 	addend = _bfd_mips_elf_sign_extend (addend, 23);
   6026 
   6027       if ((symbol + addend) & 3)
   6028 	return bfd_reloc_outofrange;
   6029 
   6030       value = symbol + addend - p;
   6031       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6032 	overflowed_p = mips_elf_overflow_p (value, 23);
   6033       value >>= howto->rightshift;
   6034       value &= howto->dst_mask;
   6035       break;
   6036 
   6037     case R_MIPS_PC26_S2:
   6038       if (howto->partial_inplace)
   6039 	addend = _bfd_mips_elf_sign_extend (addend, 28);
   6040 
   6041       if ((symbol + addend) & 3)
   6042 	return bfd_reloc_outofrange;
   6043 
   6044       value = symbol + addend - p;
   6045       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6046 	overflowed_p = mips_elf_overflow_p (value, 28);
   6047       value >>= howto->rightshift;
   6048       value &= howto->dst_mask;
   6049       break;
   6050 
   6051     case R_MIPS_PC18_S3:
   6052       if (howto->partial_inplace)
   6053 	addend = _bfd_mips_elf_sign_extend (addend, 21);
   6054 
   6055       if ((symbol + addend) & 7)
   6056 	return bfd_reloc_outofrange;
   6057 
   6058       value = symbol + addend - ((p | 7) ^ 7);
   6059       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6060 	overflowed_p = mips_elf_overflow_p (value, 21);
   6061       value >>= howto->rightshift;
   6062       value &= howto->dst_mask;
   6063       break;
   6064 
   6065     case R_MIPS_PC19_S2:
   6066       if (howto->partial_inplace)
   6067 	addend = _bfd_mips_elf_sign_extend (addend, 21);
   6068 
   6069       if ((symbol + addend) & 3)
   6070 	return bfd_reloc_outofrange;
   6071 
   6072       value = symbol + addend - p;
   6073       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6074 	overflowed_p = mips_elf_overflow_p (value, 21);
   6075       value >>= howto->rightshift;
   6076       value &= howto->dst_mask;
   6077       break;
   6078 
   6079     case R_MIPS_PCHI16:
   6080       value = mips_elf_high (symbol + addend - p);
   6081       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6082 	overflowed_p = mips_elf_overflow_p (value, 16);
   6083       value &= howto->dst_mask;
   6084       break;
   6085 
   6086     case R_MIPS_PCLO16:
   6087       if (howto->partial_inplace)
   6088 	addend = _bfd_mips_elf_sign_extend (addend, 16);
   6089       value = symbol + addend - p;
   6090       value &= howto->dst_mask;
   6091       break;
   6092 
   6093     case R_MICROMIPS_PC7_S1:
   6094       if (howto->partial_inplace)
   6095 	addend = _bfd_mips_elf_sign_extend (addend, 8);
   6096       value = symbol + addend - p;
   6097       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6098 	overflowed_p = mips_elf_overflow_p (value, 8);
   6099       value >>= howto->rightshift;
   6100       value &= howto->dst_mask;
   6101       break;
   6102 
   6103     case R_MICROMIPS_PC10_S1:
   6104       if (howto->partial_inplace)
   6105 	addend = _bfd_mips_elf_sign_extend (addend, 11);
   6106       value = symbol + addend - p;
   6107       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6108 	overflowed_p = mips_elf_overflow_p (value, 11);
   6109       value >>= howto->rightshift;
   6110       value &= howto->dst_mask;
   6111       break;
   6112 
   6113     case R_MICROMIPS_PC16_S1:
   6114       if (howto->partial_inplace)
   6115 	addend = _bfd_mips_elf_sign_extend (addend, 17);
   6116       value = symbol + addend - p;
   6117       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6118 	overflowed_p = mips_elf_overflow_p (value, 17);
   6119       value >>= howto->rightshift;
   6120       value &= howto->dst_mask;
   6121       break;
   6122 
   6123     case R_MICROMIPS_PC23_S2:
   6124       if (howto->partial_inplace)
   6125 	addend = _bfd_mips_elf_sign_extend (addend, 25);
   6126       value = symbol + addend - ((p | 3) ^ 3);
   6127       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6128 	overflowed_p = mips_elf_overflow_p (value, 25);
   6129       value >>= howto->rightshift;
   6130       value &= howto->dst_mask;
   6131       break;
   6132 
   6133     case R_MIPS_GOT_HI16:
   6134     case R_MIPS_CALL_HI16:
   6135     case R_MICROMIPS_GOT_HI16:
   6136     case R_MICROMIPS_CALL_HI16:
   6137       /* We're allowed to handle these two relocations identically.
   6138 	 The dynamic linker is allowed to handle the CALL relocations
   6139 	 differently by creating a lazy evaluation stub.  */
   6140       value = g;
   6141       value = mips_elf_high (value);
   6142       value &= howto->dst_mask;
   6143       break;
   6144 
   6145     case R_MIPS_GOT_LO16:
   6146     case R_MIPS_CALL_LO16:
   6147     case R_MICROMIPS_GOT_LO16:
   6148     case R_MICROMIPS_CALL_LO16:
   6149       value = g & howto->dst_mask;
   6150       break;
   6151 
   6152     case R_MIPS_GOT_PAGE:
   6153     case R_MICROMIPS_GOT_PAGE:
   6154       value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
   6155       if (value == MINUS_ONE)
   6156 	return bfd_reloc_outofrange;
   6157       value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
   6158       overflowed_p = mips_elf_overflow_p (value, 16);
   6159       break;
   6160 
   6161     case R_MIPS_GOT_OFST:
   6162     case R_MICROMIPS_GOT_OFST:
   6163       if (local_p)
   6164 	mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
   6165       else
   6166 	value = addend;
   6167       overflowed_p = mips_elf_overflow_p (value, 16);
   6168       break;
   6169 
   6170     case R_MIPS_SUB:
   6171     case R_MICROMIPS_SUB:
   6172       value = symbol - addend;
   6173       value &= howto->dst_mask;
   6174       break;
   6175 
   6176     case R_MIPS_HIGHER:
   6177     case R_MICROMIPS_HIGHER:
   6178       value = mips_elf_higher (addend + symbol);
   6179       value &= howto->dst_mask;
   6180       break;
   6181 
   6182     case R_MIPS_HIGHEST:
   6183     case R_MICROMIPS_HIGHEST:
   6184       value = mips_elf_highest (addend + symbol);
   6185       value &= howto->dst_mask;
   6186       break;
   6187 
   6188     case R_MIPS_SCN_DISP:
   6189     case R_MICROMIPS_SCN_DISP:
   6190       value = symbol + addend - sec->output_offset;
   6191       value &= howto->dst_mask;
   6192       break;
   6193 
   6194     case R_MIPS_JALR:
   6195     case R_MICROMIPS_JALR:
   6196       /* This relocation is only a hint.  In some cases, we optimize
   6197 	 it into a bal instruction.  But we don't try to optimize
   6198 	 when the symbol does not resolve locally.  */
   6199       if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
   6200 	return bfd_reloc_continue;
   6201       value = symbol + addend;
   6202       break;
   6203 
   6204     case R_MIPS_PJUMP:
   6205     case R_MIPS_GNU_VTINHERIT:
   6206     case R_MIPS_GNU_VTENTRY:
   6207       /* We don't do anything with these at present.  */
   6208       return bfd_reloc_continue;
   6209 
   6210     default:
   6211       /* An unrecognized relocation type.  */
   6212       return bfd_reloc_notsupported;
   6213     }
   6214 
   6215   /* Store the VALUE for our caller.  */
   6216   *valuep = value;
   6217   return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
   6218 }
   6219 
   6220 /* Obtain the field relocated by RELOCATION.  */
   6221 
   6222 static bfd_vma
   6223 mips_elf_obtain_contents (reloc_howto_type *howto,
   6224 			  const Elf_Internal_Rela *relocation,
   6225 			  bfd *input_bfd, bfd_byte *contents)
   6226 {
   6227   bfd_vma x;
   6228   bfd_byte *location = contents + relocation->r_offset;
   6229 
   6230   /* Obtain the bytes.  */
   6231   x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
   6232 
   6233   return x;
   6234 }
   6235 
   6236 /* It has been determined that the result of the RELOCATION is the
   6237    VALUE.  Use HOWTO to place VALUE into the output file at the
   6238    appropriate position.  The SECTION is the section to which the
   6239    relocation applies.
   6240    CROSS_MODE_JUMP_P is true if the relocation field
   6241    is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
   6242 
   6243    Returns FALSE if anything goes wrong.  */
   6244 
   6245 static bfd_boolean
   6246 mips_elf_perform_relocation (struct bfd_link_info *info,
   6247 			     reloc_howto_type *howto,
   6248 			     const Elf_Internal_Rela *relocation,
   6249 			     bfd_vma value, bfd *input_bfd,
   6250 			     asection *input_section, bfd_byte *contents,
   6251 			     bfd_boolean cross_mode_jump_p)
   6252 {
   6253   bfd_vma x;
   6254   bfd_byte *location;
   6255   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   6256 
   6257   /* Figure out where the relocation is occurring.  */
   6258   location = contents + relocation->r_offset;
   6259 
   6260   _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
   6261 
   6262   /* Obtain the current value.  */
   6263   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
   6264 
   6265   /* Clear the field we are setting.  */
   6266   x &= ~howto->dst_mask;
   6267 
   6268   /* Set the field.  */
   6269   x |= (value & howto->dst_mask);
   6270 
   6271   /* If required, turn JAL into JALX.  */
   6272   if (cross_mode_jump_p && jal_reloc_p (r_type))
   6273     {
   6274       bfd_boolean ok;
   6275       bfd_vma opcode = x >> 26;
   6276       bfd_vma jalx_opcode;
   6277 
   6278       /* Check to see if the opcode is already JAL or JALX.  */
   6279       if (r_type == R_MIPS16_26)
   6280 	{
   6281 	  ok = ((opcode == 0x6) || (opcode == 0x7));
   6282 	  jalx_opcode = 0x7;
   6283 	}
   6284       else if (r_type == R_MICROMIPS_26_S1)
   6285 	{
   6286 	  ok = ((opcode == 0x3d) || (opcode == 0x3c));
   6287 	  jalx_opcode = 0x3c;
   6288 	}
   6289       else
   6290 	{
   6291 	  ok = ((opcode == 0x3) || (opcode == 0x1d));
   6292 	  jalx_opcode = 0x1d;
   6293 	}
   6294 
   6295       /* If the opcode is not JAL or JALX, there's a problem.  We cannot
   6296          convert J or JALS to JALX.  */
   6297       if (!ok)
   6298 	{
   6299 	  (*_bfd_error_handler)
   6300 	    (_("%B: %A+0x%lx: Unsupported jump between ISA modes; consider recompiling with interlinking enabled."),
   6301 	     input_bfd,
   6302 	     input_section,
   6303 	     (unsigned long) relocation->r_offset);
   6304 	  bfd_set_error (bfd_error_bad_value);
   6305 	  return FALSE;
   6306 	}
   6307 
   6308       /* Make this the JALX opcode.  */
   6309       x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
   6310     }
   6311 
   6312   /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
   6313      range.  */
   6314   if (!info->relocatable
   6315       && !cross_mode_jump_p
   6316       && ((JAL_TO_BAL_P (input_bfd)
   6317 	   && r_type == R_MIPS_26
   6318 	   && (x >> 26) == 0x3)		/* jal addr */
   6319 	  || (JALR_TO_BAL_P (input_bfd)
   6320 	      && r_type == R_MIPS_JALR
   6321 	      && x == 0x0320f809)	/* jalr t9 */
   6322 	  || (JR_TO_B_P (input_bfd)
   6323 	      && r_type == R_MIPS_JALR
   6324 	      && x == 0x03200008)))	/* jr t9 */
   6325     {
   6326       bfd_vma addr;
   6327       bfd_vma dest;
   6328       bfd_signed_vma off;
   6329 
   6330       addr = (input_section->output_section->vma
   6331 	      + input_section->output_offset
   6332 	      + relocation->r_offset
   6333 	      + 4);
   6334       if (r_type == R_MIPS_26)
   6335 	dest = (value << 2) | ((addr >> 28) << 28);
   6336       else
   6337 	dest = value;
   6338       off = dest - addr;
   6339       if (off <= 0x1ffff && off >= -0x20000)
   6340 	{
   6341 	  if (x == 0x03200008)	/* jr t9 */
   6342 	    x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
   6343 	  else
   6344 	    x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
   6345 	}
   6346     }
   6347 
   6348   /* Put the value into the output.  */
   6349   bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
   6350 
   6351   _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
   6352 			       location);
   6353 
   6354   return TRUE;
   6355 }
   6356 
   6357 /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
   6359    is the original relocation, which is now being transformed into a
   6360    dynamic relocation.  The ADDENDP is adjusted if necessary; the
   6361    caller should store the result in place of the original addend.  */
   6362 
   6363 static bfd_boolean
   6364 mips_elf_create_dynamic_relocation (bfd *output_bfd,
   6365 				    struct bfd_link_info *info,
   6366 				    const Elf_Internal_Rela *rel,
   6367 				    struct mips_elf_link_hash_entry *h,
   6368 				    asection *sec, bfd_vma symbol,
   6369 				    bfd_vma *addendp, asection *input_section)
   6370 {
   6371   Elf_Internal_Rela outrel[3];
   6372   asection *sreloc;
   6373   bfd *dynobj;
   6374   int r_type;
   6375   long indx;
   6376   bfd_boolean defined_p;
   6377   struct mips_elf_link_hash_table *htab;
   6378 
   6379   htab = mips_elf_hash_table (info);
   6380   BFD_ASSERT (htab != NULL);
   6381 
   6382   r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   6383   dynobj = elf_hash_table (info)->dynobj;
   6384   sreloc = mips_elf_rel_dyn_section (info, FALSE);
   6385   BFD_ASSERT (sreloc != NULL);
   6386   BFD_ASSERT (sreloc->contents != NULL);
   6387   BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
   6388 	      < sreloc->size);
   6389 
   6390   outrel[0].r_offset =
   6391     _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
   6392   if (ABI_64_P (output_bfd))
   6393     {
   6394       outrel[1].r_offset =
   6395 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
   6396       outrel[2].r_offset =
   6397 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
   6398     }
   6399 
   6400   if (outrel[0].r_offset == MINUS_ONE)
   6401     /* The relocation field has been deleted.  */
   6402     return TRUE;
   6403 
   6404   if (outrel[0].r_offset == MINUS_TWO)
   6405     {
   6406       /* The relocation field has been converted into a relative value of
   6407 	 some sort.  Functions like _bfd_elf_write_section_eh_frame expect
   6408 	 the field to be fully relocated, so add in the symbol's value.  */
   6409       *addendp += symbol;
   6410       return TRUE;
   6411     }
   6412 
   6413   /* We must now calculate the dynamic symbol table index to use
   6414      in the relocation.  */
   6415   if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
   6416     {
   6417       BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
   6418       indx = h->root.dynindx;
   6419       if (SGI_COMPAT (output_bfd))
   6420 	defined_p = h->root.def_regular;
   6421       else
   6422 	/* ??? glibc's ld.so just adds the final GOT entry to the
   6423 	   relocation field.  It therefore treats relocs against
   6424 	   defined symbols in the same way as relocs against
   6425 	   undefined symbols.  */
   6426 	defined_p = FALSE;
   6427     }
   6428   else
   6429     {
   6430       if (sec != NULL && bfd_is_abs_section (sec))
   6431 	indx = 0;
   6432       else if (sec == NULL || sec->owner == NULL)
   6433 	{
   6434 	  bfd_set_error (bfd_error_bad_value);
   6435 	  return FALSE;
   6436 	}
   6437       else
   6438 	{
   6439 	  indx = elf_section_data (sec->output_section)->dynindx;
   6440 	  if (indx == 0)
   6441 	    {
   6442 	      asection *osec = htab->root.text_index_section;
   6443 	      indx = elf_section_data (osec)->dynindx;
   6444 	    }
   6445 	  if (indx == 0)
   6446 	    abort ();
   6447 	}
   6448 
   6449       /* Instead of generating a relocation using the section
   6450 	 symbol, we may as well make it a fully relative
   6451 	 relocation.  We want to avoid generating relocations to
   6452 	 local symbols because we used to generate them
   6453 	 incorrectly, without adding the original symbol value,
   6454 	 which is mandated by the ABI for section symbols.  In
   6455 	 order to give dynamic loaders and applications time to
   6456 	 phase out the incorrect use, we refrain from emitting
   6457 	 section-relative relocations.  It's not like they're
   6458 	 useful, after all.  This should be a bit more efficient
   6459 	 as well.  */
   6460       /* ??? Although this behavior is compatible with glibc's ld.so,
   6461 	 the ABI says that relocations against STN_UNDEF should have
   6462 	 a symbol value of 0.  Irix rld honors this, so relocations
   6463 	 against STN_UNDEF have no effect.  */
   6464       if (!SGI_COMPAT (output_bfd))
   6465 	indx = 0;
   6466       defined_p = TRUE;
   6467     }
   6468 
   6469   /* If the relocation was previously an absolute relocation and
   6470      this symbol will not be referred to by the relocation, we must
   6471      adjust it by the value we give it in the dynamic symbol table.
   6472      Otherwise leave the job up to the dynamic linker.  */
   6473   if (defined_p && r_type != R_MIPS_REL32)
   6474     *addendp += symbol;
   6475 
   6476   if (htab->is_vxworks)
   6477     /* VxWorks uses non-relative relocations for this.  */
   6478     outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
   6479   else
   6480     /* The relocation is always an REL32 relocation because we don't
   6481        know where the shared library will wind up at load-time.  */
   6482     outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
   6483 				   R_MIPS_REL32);
   6484 
   6485   /* For strict adherence to the ABI specification, we should
   6486      generate a R_MIPS_64 relocation record by itself before the
   6487      _REL32/_64 record as well, such that the addend is read in as
   6488      a 64-bit value (REL32 is a 32-bit relocation, after all).
   6489      However, since none of the existing ELF64 MIPS dynamic
   6490      loaders seems to care, we don't waste space with these
   6491      artificial relocations.  If this turns out to not be true,
   6492      mips_elf_allocate_dynamic_relocation() should be tweaked so
   6493      as to make room for a pair of dynamic relocations per
   6494      invocation if ABI_64_P, and here we should generate an
   6495      additional relocation record with R_MIPS_64 by itself for a
   6496      NULL symbol before this relocation record.  */
   6497   outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
   6498 				 ABI_64_P (output_bfd)
   6499 				 ? R_MIPS_64
   6500 				 : R_MIPS_NONE);
   6501   outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
   6502 
   6503   /* Adjust the output offset of the relocation to reference the
   6504      correct location in the output file.  */
   6505   outrel[0].r_offset += (input_section->output_section->vma
   6506 			 + input_section->output_offset);
   6507   outrel[1].r_offset += (input_section->output_section->vma
   6508 			 + input_section->output_offset);
   6509   outrel[2].r_offset += (input_section->output_section->vma
   6510 			 + input_section->output_offset);
   6511 
   6512   /* Put the relocation back out.  We have to use the special
   6513      relocation outputter in the 64-bit case since the 64-bit
   6514      relocation format is non-standard.  */
   6515   if (ABI_64_P (output_bfd))
   6516     {
   6517       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
   6518 	(output_bfd, &outrel[0],
   6519 	 (sreloc->contents
   6520 	  + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
   6521     }
   6522   else if (htab->is_vxworks)
   6523     {
   6524       /* VxWorks uses RELA rather than REL dynamic relocations.  */
   6525       outrel[0].r_addend = *addendp;
   6526       bfd_elf32_swap_reloca_out
   6527 	(output_bfd, &outrel[0],
   6528 	 (sreloc->contents
   6529 	  + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
   6530     }
   6531   else
   6532     bfd_elf32_swap_reloc_out
   6533       (output_bfd, &outrel[0],
   6534        (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
   6535 
   6536   /* We've now added another relocation.  */
   6537   ++sreloc->reloc_count;
   6538 
   6539   /* Make sure the output section is writable.  The dynamic linker
   6540      will be writing to it.  */
   6541   elf_section_data (input_section->output_section)->this_hdr.sh_flags
   6542     |= SHF_WRITE;
   6543 
   6544   /* On IRIX5, make an entry of compact relocation info.  */
   6545   if (IRIX_COMPAT (output_bfd) == ict_irix5)
   6546     {
   6547       asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
   6548       bfd_byte *cr;
   6549 
   6550       if (scpt)
   6551 	{
   6552 	  Elf32_crinfo cptrel;
   6553 
   6554 	  mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
   6555 	  cptrel.vaddr = (rel->r_offset
   6556 			  + input_section->output_section->vma
   6557 			  + input_section->output_offset);
   6558 	  if (r_type == R_MIPS_REL32)
   6559 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
   6560 	  else
   6561 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
   6562 	  mips_elf_set_cr_dist2to (cptrel, 0);
   6563 	  cptrel.konst = *addendp;
   6564 
   6565 	  cr = (scpt->contents
   6566 		+ sizeof (Elf32_External_compact_rel));
   6567 	  mips_elf_set_cr_relvaddr (cptrel, 0);
   6568 	  bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
   6569 				     ((Elf32_External_crinfo *) cr
   6570 				      + scpt->reloc_count));
   6571 	  ++scpt->reloc_count;
   6572 	}
   6573     }
   6574 
   6575   /* If we've written this relocation for a readonly section,
   6576      we need to set DF_TEXTREL again, so that we do not delete the
   6577      DT_TEXTREL tag.  */
   6578   if (MIPS_ELF_READONLY_SECTION (input_section))
   6579     info->flags |= DF_TEXTREL;
   6580 
   6581   return TRUE;
   6582 }
   6583 
   6584 /* Return the MACH for a MIPS e_flags value.  */
   6586 
   6587 unsigned long
   6588 _bfd_elf_mips_mach (flagword flags)
   6589 {
   6590   switch (flags & EF_MIPS_MACH)
   6591     {
   6592     case E_MIPS_MACH_3900:
   6593       return bfd_mach_mips3900;
   6594 
   6595     case E_MIPS_MACH_4010:
   6596       return bfd_mach_mips4010;
   6597 
   6598     case E_MIPS_MACH_4100:
   6599       return bfd_mach_mips4100;
   6600 
   6601     case E_MIPS_MACH_4111:
   6602       return bfd_mach_mips4111;
   6603 
   6604     case E_MIPS_MACH_4120:
   6605       return bfd_mach_mips4120;
   6606 
   6607     case E_MIPS_MACH_4650:
   6608       return bfd_mach_mips4650;
   6609 
   6610     case E_MIPS_MACH_5400:
   6611       return bfd_mach_mips5400;
   6612 
   6613     case E_MIPS_MACH_5500:
   6614       return bfd_mach_mips5500;
   6615 
   6616     case E_MIPS_MACH_5900:
   6617       return bfd_mach_mips5900;
   6618 
   6619     case E_MIPS_MACH_9000:
   6620       return bfd_mach_mips9000;
   6621 
   6622     case E_MIPS_MACH_SB1:
   6623       return bfd_mach_mips_sb1;
   6624 
   6625     case E_MIPS_MACH_LS2E:
   6626       return bfd_mach_mips_loongson_2e;
   6627 
   6628     case E_MIPS_MACH_LS2F:
   6629       return bfd_mach_mips_loongson_2f;
   6630 
   6631     case E_MIPS_MACH_LS3A:
   6632       return bfd_mach_mips_loongson_3a;
   6633 
   6634     case E_MIPS_MACH_OCTEON3:
   6635       return bfd_mach_mips_octeon3;
   6636 
   6637     case E_MIPS_MACH_OCTEON2:
   6638       return bfd_mach_mips_octeon2;
   6639 
   6640     case E_MIPS_MACH_OCTEON:
   6641       return bfd_mach_mips_octeon;
   6642 
   6643     case E_MIPS_MACH_XLR:
   6644       return bfd_mach_mips_xlr;
   6645 
   6646     default:
   6647       switch (flags & EF_MIPS_ARCH)
   6648 	{
   6649 	default:
   6650 	case E_MIPS_ARCH_1:
   6651 	  return bfd_mach_mips3000;
   6652 
   6653 	case E_MIPS_ARCH_2:
   6654 	  return bfd_mach_mips6000;
   6655 
   6656 	case E_MIPS_ARCH_3:
   6657 	  return bfd_mach_mips4000;
   6658 
   6659 	case E_MIPS_ARCH_4:
   6660 	  return bfd_mach_mips8000;
   6661 
   6662 	case E_MIPS_ARCH_5:
   6663 	  return bfd_mach_mips5;
   6664 
   6665 	case E_MIPS_ARCH_32:
   6666 	  return bfd_mach_mipsisa32;
   6667 
   6668 	case E_MIPS_ARCH_64:
   6669 	  return bfd_mach_mipsisa64;
   6670 
   6671 	case E_MIPS_ARCH_32R2:
   6672 	  return bfd_mach_mipsisa32r2;
   6673 
   6674 	case E_MIPS_ARCH_64R2:
   6675 	  return bfd_mach_mipsisa64r2;
   6676 
   6677 	case E_MIPS_ARCH_32R6:
   6678 	  return bfd_mach_mipsisa32r6;
   6679 
   6680 	case E_MIPS_ARCH_64R6:
   6681 	  return bfd_mach_mipsisa64r6;
   6682 	}
   6683     }
   6684 
   6685   return 0;
   6686 }
   6687 
   6688 /* Return printable name for ABI.  */
   6689 
   6690 static INLINE char *
   6691 elf_mips_abi_name (bfd *abfd)
   6692 {
   6693   flagword flags;
   6694 
   6695   flags = elf_elfheader (abfd)->e_flags;
   6696   switch (flags & EF_MIPS_ABI)
   6697     {
   6698     case 0:
   6699       if (ABI_N32_P (abfd))
   6700 	return "N32";
   6701       else if (ABI_64_P (abfd))
   6702 	return "64";
   6703       else
   6704 	return "none";
   6705     case E_MIPS_ABI_O32:
   6706       return "O32";
   6707     case E_MIPS_ABI_O64:
   6708       return "O64";
   6709     case E_MIPS_ABI_EABI32:
   6710       return "EABI32";
   6711     case E_MIPS_ABI_EABI64:
   6712       return "EABI64";
   6713     default:
   6714       return "unknown abi";
   6715     }
   6716 }
   6717 
   6718 /* MIPS ELF uses two common sections.  One is the usual one, and the
   6720    other is for small objects.  All the small objects are kept
   6721    together, and then referenced via the gp pointer, which yields
   6722    faster assembler code.  This is what we use for the small common
   6723    section.  This approach is copied from ecoff.c.  */
   6724 static asection mips_elf_scom_section;
   6725 static asymbol mips_elf_scom_symbol;
   6726 static asymbol *mips_elf_scom_symbol_ptr;
   6727 
   6728 /* MIPS ELF also uses an acommon section, which represents an
   6729    allocated common symbol which may be overridden by a
   6730    definition in a shared library.  */
   6731 static asection mips_elf_acom_section;
   6732 static asymbol mips_elf_acom_symbol;
   6733 static asymbol *mips_elf_acom_symbol_ptr;
   6734 
   6735 /* This is used for both the 32-bit and the 64-bit ABI.  */
   6736 
   6737 void
   6738 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
   6739 {
   6740   elf_symbol_type *elfsym;
   6741 
   6742   /* Handle the special MIPS section numbers that a symbol may use.  */
   6743   elfsym = (elf_symbol_type *) asym;
   6744   switch (elfsym->internal_elf_sym.st_shndx)
   6745     {
   6746     case SHN_MIPS_ACOMMON:
   6747       /* This section is used in a dynamically linked executable file.
   6748 	 It is an allocated common section.  The dynamic linker can
   6749 	 either resolve these symbols to something in a shared
   6750 	 library, or it can just leave them here.  For our purposes,
   6751 	 we can consider these symbols to be in a new section.  */
   6752       if (mips_elf_acom_section.name == NULL)
   6753 	{
   6754 	  /* Initialize the acommon section.  */
   6755 	  mips_elf_acom_section.name = ".acommon";
   6756 	  mips_elf_acom_section.flags = SEC_ALLOC;
   6757 	  mips_elf_acom_section.output_section = &mips_elf_acom_section;
   6758 	  mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
   6759 	  mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
   6760 	  mips_elf_acom_symbol.name = ".acommon";
   6761 	  mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
   6762 	  mips_elf_acom_symbol.section = &mips_elf_acom_section;
   6763 	  mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
   6764 	}
   6765       asym->section = &mips_elf_acom_section;
   6766       break;
   6767 
   6768     case SHN_COMMON:
   6769       /* Common symbols less than the GP size are automatically
   6770 	 treated as SHN_MIPS_SCOMMON symbols on IRIX5.  */
   6771       if (asym->value > elf_gp_size (abfd)
   6772 	  || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
   6773 	  || IRIX_COMPAT (abfd) == ict_irix6)
   6774 	break;
   6775       /* Fall through.  */
   6776     case SHN_MIPS_SCOMMON:
   6777       if (mips_elf_scom_section.name == NULL)
   6778 	{
   6779 	  /* Initialize the small common section.  */
   6780 	  mips_elf_scom_section.name = ".scommon";
   6781 	  mips_elf_scom_section.flags = SEC_IS_COMMON;
   6782 	  mips_elf_scom_section.output_section = &mips_elf_scom_section;
   6783 	  mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
   6784 	  mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
   6785 	  mips_elf_scom_symbol.name = ".scommon";
   6786 	  mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
   6787 	  mips_elf_scom_symbol.section = &mips_elf_scom_section;
   6788 	  mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
   6789 	}
   6790       asym->section = &mips_elf_scom_section;
   6791       asym->value = elfsym->internal_elf_sym.st_size;
   6792       break;
   6793 
   6794     case SHN_MIPS_SUNDEFINED:
   6795       asym->section = bfd_und_section_ptr;
   6796       break;
   6797 
   6798     case SHN_MIPS_TEXT:
   6799       {
   6800 	asection *section = bfd_get_section_by_name (abfd, ".text");
   6801 
   6802 	if (section != NULL)
   6803 	  {
   6804 	    asym->section = section;
   6805 	    /* MIPS_TEXT is a bit special, the address is not an offset
   6806 	       to the base of the .text section.  So substract the section
   6807 	       base address to make it an offset.  */
   6808 	    asym->value -= section->vma;
   6809 	  }
   6810       }
   6811       break;
   6812 
   6813     case SHN_MIPS_DATA:
   6814       {
   6815 	asection *section = bfd_get_section_by_name (abfd, ".data");
   6816 
   6817 	if (section != NULL)
   6818 	  {
   6819 	    asym->section = section;
   6820 	    /* MIPS_DATA is a bit special, the address is not an offset
   6821 	       to the base of the .data section.  So substract the section
   6822 	       base address to make it an offset.  */
   6823 	    asym->value -= section->vma;
   6824 	  }
   6825       }
   6826       break;
   6827     }
   6828 
   6829   /* If this is an odd-valued function symbol, assume it's a MIPS16
   6830      or microMIPS one.  */
   6831   if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
   6832       && (asym->value & 1) != 0)
   6833     {
   6834       asym->value--;
   6835       if (MICROMIPS_P (abfd))
   6836 	elfsym->internal_elf_sym.st_other
   6837 	  = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
   6838       else
   6839 	elfsym->internal_elf_sym.st_other
   6840 	  = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
   6841     }
   6842 }
   6843 
   6844 /* Implement elf_backend_eh_frame_address_size.  This differs from
   6846    the default in the way it handles EABI64.
   6847 
   6848    EABI64 was originally specified as an LP64 ABI, and that is what
   6849    -mabi=eabi normally gives on a 64-bit target.  However, gcc has
   6850    historically accepted the combination of -mabi=eabi and -mlong32,
   6851    and this ILP32 variation has become semi-official over time.
   6852    Both forms use elf32 and have pointer-sized FDE addresses.
   6853 
   6854    If an EABI object was generated by GCC 4.0 or above, it will have
   6855    an empty .gcc_compiled_longXX section, where XX is the size of longs
   6856    in bits.  Unfortunately, ILP32 objects generated by earlier compilers
   6857    have no special marking to distinguish them from LP64 objects.
   6858 
   6859    We don't want users of the official LP64 ABI to be punished for the
   6860    existence of the ILP32 variant, but at the same time, we don't want
   6861    to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
   6862    We therefore take the following approach:
   6863 
   6864       - If ABFD contains a .gcc_compiled_longXX section, use it to
   6865         determine the pointer size.
   6866 
   6867       - Otherwise check the type of the first relocation.  Assume that
   6868         the LP64 ABI is being used if the relocation is of type R_MIPS_64.
   6869 
   6870       - Otherwise punt.
   6871 
   6872    The second check is enough to detect LP64 objects generated by pre-4.0
   6873    compilers because, in the kind of output generated by those compilers,
   6874    the first relocation will be associated with either a CIE personality
   6875    routine or an FDE start address.  Furthermore, the compilers never
   6876    used a special (non-pointer) encoding for this ABI.
   6877 
   6878    Checking the relocation type should also be safe because there is no
   6879    reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
   6880    did so.  */
   6881 
   6882 unsigned int
   6883 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
   6884 {
   6885   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   6886     return 8;
   6887   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
   6888     {
   6889       bfd_boolean long32_p, long64_p;
   6890 
   6891       long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
   6892       long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
   6893       if (long32_p && long64_p)
   6894 	return 0;
   6895       if (long32_p)
   6896 	return 4;
   6897       if (long64_p)
   6898 	return 8;
   6899 
   6900       if (sec->reloc_count > 0
   6901 	  && elf_section_data (sec)->relocs != NULL
   6902 	  && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
   6903 	      == R_MIPS_64))
   6904 	return 8;
   6905 
   6906       return 0;
   6907     }
   6908   return 4;
   6909 }
   6910 
   6911 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
   6913    relocations against two unnamed section symbols to resolve to the
   6914    same address.  For example, if we have code like:
   6915 
   6916 	lw	$4,%got_disp(.data)($gp)
   6917 	lw	$25,%got_disp(.text)($gp)
   6918 	jalr	$25
   6919 
   6920    then the linker will resolve both relocations to .data and the program
   6921    will jump there rather than to .text.
   6922 
   6923    We can work around this problem by giving names to local section symbols.
   6924    This is also what the MIPSpro tools do.  */
   6925 
   6926 bfd_boolean
   6927 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
   6928 {
   6929   return SGI_COMPAT (abfd);
   6930 }
   6931 
   6932 /* Work over a section just before writing it out.  This routine is
   6934    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
   6935    sections that need the SHF_MIPS_GPREL flag by name; there has to be
   6936    a better way.  */
   6937 
   6938 bfd_boolean
   6939 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
   6940 {
   6941   if (hdr->sh_type == SHT_MIPS_REGINFO
   6942       && hdr->sh_size > 0)
   6943     {
   6944       bfd_byte buf[4];
   6945 
   6946       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
   6947       BFD_ASSERT (hdr->contents == NULL);
   6948 
   6949       if (bfd_seek (abfd,
   6950 		    hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
   6951 		    SEEK_SET) != 0)
   6952 	return FALSE;
   6953       H_PUT_32 (abfd, elf_gp (abfd), buf);
   6954       if (bfd_bwrite (buf, 4, abfd) != 4)
   6955 	return FALSE;
   6956     }
   6957 
   6958   if (hdr->sh_type == SHT_MIPS_OPTIONS
   6959       && hdr->bfd_section != NULL
   6960       && mips_elf_section_data (hdr->bfd_section) != NULL
   6961       && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
   6962     {
   6963       bfd_byte *contents, *l, *lend;
   6964 
   6965       /* We stored the section contents in the tdata field in the
   6966 	 set_section_contents routine.  We save the section contents
   6967 	 so that we don't have to read them again.
   6968 	 At this point we know that elf_gp is set, so we can look
   6969 	 through the section contents to see if there is an
   6970 	 ODK_REGINFO structure.  */
   6971 
   6972       contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
   6973       l = contents;
   6974       lend = contents + hdr->sh_size;
   6975       while (l + sizeof (Elf_External_Options) <= lend)
   6976 	{
   6977 	  Elf_Internal_Options intopt;
   6978 
   6979 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
   6980 					&intopt);
   6981 	  if (intopt.size < sizeof (Elf_External_Options))
   6982 	    {
   6983 	      (*_bfd_error_handler)
   6984 		(_("%B: Warning: bad `%s' option size %u smaller than its header"),
   6985 		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
   6986 	      break;
   6987 	    }
   6988 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
   6989 	    {
   6990 	      bfd_byte buf[8];
   6991 
   6992 	      if (bfd_seek (abfd,
   6993 			    (hdr->sh_offset
   6994 			     + (l - contents)
   6995 			     + sizeof (Elf_External_Options)
   6996 			     + (sizeof (Elf64_External_RegInfo) - 8)),
   6997 			     SEEK_SET) != 0)
   6998 		return FALSE;
   6999 	      H_PUT_64 (abfd, elf_gp (abfd), buf);
   7000 	      if (bfd_bwrite (buf, 8, abfd) != 8)
   7001 		return FALSE;
   7002 	    }
   7003 	  else if (intopt.kind == ODK_REGINFO)
   7004 	    {
   7005 	      bfd_byte buf[4];
   7006 
   7007 	      if (bfd_seek (abfd,
   7008 			    (hdr->sh_offset
   7009 			     + (l - contents)
   7010 			     + sizeof (Elf_External_Options)
   7011 			     + (sizeof (Elf32_External_RegInfo) - 4)),
   7012 			    SEEK_SET) != 0)
   7013 		return FALSE;
   7014 	      H_PUT_32 (abfd, elf_gp (abfd), buf);
   7015 	      if (bfd_bwrite (buf, 4, abfd) != 4)
   7016 		return FALSE;
   7017 	    }
   7018 	  l += intopt.size;
   7019 	}
   7020     }
   7021 
   7022   if (hdr->bfd_section != NULL)
   7023     {
   7024       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
   7025 
   7026       /* .sbss is not handled specially here because the GNU/Linux
   7027 	 prelinker can convert .sbss from NOBITS to PROGBITS and
   7028 	 changing it back to NOBITS breaks the binary.  The entry in
   7029 	 _bfd_mips_elf_special_sections will ensure the correct flags
   7030 	 are set on .sbss if BFD creates it without reading it from an
   7031 	 input file, and without special handling here the flags set
   7032 	 on it in an input file will be followed.  */
   7033       if (strcmp (name, ".sdata") == 0
   7034 	  || strcmp (name, ".lit8") == 0
   7035 	  || strcmp (name, ".lit4") == 0)
   7036 	hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
   7037       else if (strcmp (name, ".srdata") == 0)
   7038 	hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
   7039       else if (strcmp (name, ".compact_rel") == 0)
   7040 	hdr->sh_flags = 0;
   7041       else if (strcmp (name, ".rtproc") == 0)
   7042 	{
   7043 	  if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
   7044 	    {
   7045 	      unsigned int adjust;
   7046 
   7047 	      adjust = hdr->sh_size % hdr->sh_addralign;
   7048 	      if (adjust != 0)
   7049 		hdr->sh_size += hdr->sh_addralign - adjust;
   7050 	    }
   7051 	}
   7052     }
   7053 
   7054   return TRUE;
   7055 }
   7056 
   7057 /* Handle a MIPS specific section when reading an object file.  This
   7058    is called when elfcode.h finds a section with an unknown type.
   7059    This routine supports both the 32-bit and 64-bit ELF ABI.
   7060 
   7061    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
   7062    how to.  */
   7063 
   7064 bfd_boolean
   7065 _bfd_mips_elf_section_from_shdr (bfd *abfd,
   7066 				 Elf_Internal_Shdr *hdr,
   7067 				 const char *name,
   7068 				 int shindex)
   7069 {
   7070   flagword flags = 0;
   7071 
   7072   /* There ought to be a place to keep ELF backend specific flags, but
   7073      at the moment there isn't one.  We just keep track of the
   7074      sections by their name, instead.  Fortunately, the ABI gives
   7075      suggested names for all the MIPS specific sections, so we will
   7076      probably get away with this.  */
   7077   switch (hdr->sh_type)
   7078     {
   7079     case SHT_MIPS_LIBLIST:
   7080       if (strcmp (name, ".liblist") != 0)
   7081 	return FALSE;
   7082       break;
   7083     case SHT_MIPS_MSYM:
   7084       if (strcmp (name, ".msym") != 0)
   7085 	return FALSE;
   7086       break;
   7087     case SHT_MIPS_CONFLICT:
   7088       if (strcmp (name, ".conflict") != 0)
   7089 	return FALSE;
   7090       break;
   7091     case SHT_MIPS_GPTAB:
   7092       if (! CONST_STRNEQ (name, ".gptab."))
   7093 	return FALSE;
   7094       break;
   7095     case SHT_MIPS_UCODE:
   7096       if (strcmp (name, ".ucode") != 0)
   7097 	return FALSE;
   7098       break;
   7099     case SHT_MIPS_DEBUG:
   7100       if (strcmp (name, ".mdebug") != 0)
   7101 	return FALSE;
   7102       flags = SEC_DEBUGGING;
   7103       break;
   7104     case SHT_MIPS_REGINFO:
   7105       if (strcmp (name, ".reginfo") != 0
   7106 	  || hdr->sh_size != sizeof (Elf32_External_RegInfo))
   7107 	return FALSE;
   7108       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
   7109       break;
   7110     case SHT_MIPS_IFACE:
   7111       if (strcmp (name, ".MIPS.interfaces") != 0)
   7112 	return FALSE;
   7113       break;
   7114     case SHT_MIPS_CONTENT:
   7115       if (! CONST_STRNEQ (name, ".MIPS.content"))
   7116 	return FALSE;
   7117       break;
   7118     case SHT_MIPS_OPTIONS:
   7119       if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
   7120 	return FALSE;
   7121       break;
   7122     case SHT_MIPS_ABIFLAGS:
   7123       if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
   7124 	return FALSE;
   7125       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
   7126       break;
   7127     case SHT_MIPS_DWARF:
   7128       if (! CONST_STRNEQ (name, ".debug_")
   7129           && ! CONST_STRNEQ (name, ".zdebug_"))
   7130 	return FALSE;
   7131       break;
   7132     case SHT_MIPS_SYMBOL_LIB:
   7133       if (strcmp (name, ".MIPS.symlib") != 0)
   7134 	return FALSE;
   7135       break;
   7136     case SHT_MIPS_EVENTS:
   7137       if (! CONST_STRNEQ (name, ".MIPS.events")
   7138 	  && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
   7139 	return FALSE;
   7140       break;
   7141     default:
   7142       break;
   7143     }
   7144 
   7145   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   7146     return FALSE;
   7147 
   7148   if (flags)
   7149     {
   7150       if (! bfd_set_section_flags (abfd, hdr->bfd_section,
   7151 				   (bfd_get_section_flags (abfd,
   7152 							   hdr->bfd_section)
   7153 				    | flags)))
   7154 	return FALSE;
   7155     }
   7156 
   7157   if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
   7158     {
   7159       Elf_External_ABIFlags_v0 ext;
   7160 
   7161       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
   7162 				      &ext, 0, sizeof ext))
   7163 	return FALSE;
   7164       bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
   7165 					&mips_elf_tdata (abfd)->abiflags);
   7166       if (mips_elf_tdata (abfd)->abiflags.version != 0)
   7167 	return FALSE;
   7168       mips_elf_tdata (abfd)->abiflags_valid = TRUE;
   7169     }
   7170 
   7171   /* FIXME: We should record sh_info for a .gptab section.  */
   7172 
   7173   /* For a .reginfo section, set the gp value in the tdata information
   7174      from the contents of this section.  We need the gp value while
   7175      processing relocs, so we just get it now.  The .reginfo section
   7176      is not used in the 64-bit MIPS ELF ABI.  */
   7177   if (hdr->sh_type == SHT_MIPS_REGINFO)
   7178     {
   7179       Elf32_External_RegInfo ext;
   7180       Elf32_RegInfo s;
   7181 
   7182       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
   7183 				      &ext, 0, sizeof ext))
   7184 	return FALSE;
   7185       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
   7186       elf_gp (abfd) = s.ri_gp_value;
   7187     }
   7188 
   7189   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
   7190      set the gp value based on what we find.  We may see both
   7191      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
   7192      they should agree.  */
   7193   if (hdr->sh_type == SHT_MIPS_OPTIONS)
   7194     {
   7195       bfd_byte *contents, *l, *lend;
   7196 
   7197       contents = bfd_malloc (hdr->sh_size);
   7198       if (contents == NULL)
   7199 	return FALSE;
   7200       if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
   7201 				      0, hdr->sh_size))
   7202 	{
   7203 	  free (contents);
   7204 	  return FALSE;
   7205 	}
   7206       l = contents;
   7207       lend = contents + hdr->sh_size;
   7208       while (l + sizeof (Elf_External_Options) <= lend)
   7209 	{
   7210 	  Elf_Internal_Options intopt;
   7211 
   7212 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
   7213 					&intopt);
   7214 	  if (intopt.size < sizeof (Elf_External_Options))
   7215 	    {
   7216 	      (*_bfd_error_handler)
   7217 		(_("%B: Warning: bad `%s' option size %u smaller than its header"),
   7218 		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
   7219 	      break;
   7220 	    }
   7221 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
   7222 	    {
   7223 	      Elf64_Internal_RegInfo intreg;
   7224 
   7225 	      bfd_mips_elf64_swap_reginfo_in
   7226 		(abfd,
   7227 		 ((Elf64_External_RegInfo *)
   7228 		  (l + sizeof (Elf_External_Options))),
   7229 		 &intreg);
   7230 	      elf_gp (abfd) = intreg.ri_gp_value;
   7231 	    }
   7232 	  else if (intopt.kind == ODK_REGINFO)
   7233 	    {
   7234 	      Elf32_RegInfo intreg;
   7235 
   7236 	      bfd_mips_elf32_swap_reginfo_in
   7237 		(abfd,
   7238 		 ((Elf32_External_RegInfo *)
   7239 		  (l + sizeof (Elf_External_Options))),
   7240 		 &intreg);
   7241 	      elf_gp (abfd) = intreg.ri_gp_value;
   7242 	    }
   7243 	  l += intopt.size;
   7244 	}
   7245       free (contents);
   7246     }
   7247 
   7248   return TRUE;
   7249 }
   7250 
   7251 /* Set the correct type for a MIPS ELF section.  We do this by the
   7252    section name, which is a hack, but ought to work.  This routine is
   7253    used by both the 32-bit and the 64-bit ABI.  */
   7254 
   7255 bfd_boolean
   7256 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
   7257 {
   7258   const char *name = bfd_get_section_name (abfd, sec);
   7259 
   7260   if (strcmp (name, ".liblist") == 0)
   7261     {
   7262       hdr->sh_type = SHT_MIPS_LIBLIST;
   7263       hdr->sh_info = sec->size / sizeof (Elf32_Lib);
   7264       /* The sh_link field is set in final_write_processing.  */
   7265     }
   7266   else if (strcmp (name, ".conflict") == 0)
   7267     hdr->sh_type = SHT_MIPS_CONFLICT;
   7268   else if (CONST_STRNEQ (name, ".gptab."))
   7269     {
   7270       hdr->sh_type = SHT_MIPS_GPTAB;
   7271       hdr->sh_entsize = sizeof (Elf32_External_gptab);
   7272       /* The sh_info field is set in final_write_processing.  */
   7273     }
   7274   else if (strcmp (name, ".ucode") == 0)
   7275     hdr->sh_type = SHT_MIPS_UCODE;
   7276   else if (strcmp (name, ".mdebug") == 0)
   7277     {
   7278       hdr->sh_type = SHT_MIPS_DEBUG;
   7279       /* In a shared object on IRIX 5.3, the .mdebug section has an
   7280          entsize of 0.  FIXME: Does this matter?  */
   7281       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
   7282 	hdr->sh_entsize = 0;
   7283       else
   7284 	hdr->sh_entsize = 1;
   7285     }
   7286   else if (strcmp (name, ".reginfo") == 0)
   7287     {
   7288       hdr->sh_type = SHT_MIPS_REGINFO;
   7289       /* In a shared object on IRIX 5.3, the .reginfo section has an
   7290          entsize of 0x18.  FIXME: Does this matter?  */
   7291       if (SGI_COMPAT (abfd))
   7292 	{
   7293 	  if ((abfd->flags & DYNAMIC) != 0)
   7294 	    hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
   7295 	  else
   7296 	    hdr->sh_entsize = 1;
   7297 	}
   7298       else
   7299 	hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
   7300     }
   7301   else if (SGI_COMPAT (abfd)
   7302 	   && (strcmp (name, ".hash") == 0
   7303 	       || strcmp (name, ".dynamic") == 0
   7304 	       || strcmp (name, ".dynstr") == 0))
   7305     {
   7306       if (SGI_COMPAT (abfd))
   7307 	hdr->sh_entsize = 0;
   7308 #if 0
   7309       /* This isn't how the IRIX6 linker behaves.  */
   7310       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
   7311 #endif
   7312     }
   7313   else if (strcmp (name, ".got") == 0
   7314 	   || strcmp (name, ".srdata") == 0
   7315 	   || strcmp (name, ".sdata") == 0
   7316 	   || strcmp (name, ".sbss") == 0
   7317 	   || strcmp (name, ".lit4") == 0
   7318 	   || strcmp (name, ".lit8") == 0)
   7319     hdr->sh_flags |= SHF_MIPS_GPREL;
   7320   else if (strcmp (name, ".MIPS.interfaces") == 0)
   7321     {
   7322       hdr->sh_type = SHT_MIPS_IFACE;
   7323       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7324     }
   7325   else if (CONST_STRNEQ (name, ".MIPS.content"))
   7326     {
   7327       hdr->sh_type = SHT_MIPS_CONTENT;
   7328       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7329       /* The sh_info field is set in final_write_processing.  */
   7330     }
   7331   else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
   7332     {
   7333       hdr->sh_type = SHT_MIPS_OPTIONS;
   7334       hdr->sh_entsize = 1;
   7335       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7336     }
   7337   else if (CONST_STRNEQ (name, ".MIPS.abiflags"))
   7338     {
   7339       hdr->sh_type = SHT_MIPS_ABIFLAGS;
   7340       hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
   7341     }
   7342   else if (CONST_STRNEQ (name, ".debug_")
   7343            || CONST_STRNEQ (name, ".zdebug_"))
   7344     {
   7345       hdr->sh_type = SHT_MIPS_DWARF;
   7346 
   7347       /* Irix facilities such as libexc expect a single .debug_frame
   7348 	 per executable, the system ones have NOSTRIP set and the linker
   7349 	 doesn't merge sections with different flags so ...  */
   7350       if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
   7351 	hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7352     }
   7353   else if (strcmp (name, ".MIPS.symlib") == 0)
   7354     {
   7355       hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
   7356       /* The sh_link and sh_info fields are set in
   7357          final_write_processing.  */
   7358     }
   7359   else if (CONST_STRNEQ (name, ".MIPS.events")
   7360 	   || CONST_STRNEQ (name, ".MIPS.post_rel"))
   7361     {
   7362       hdr->sh_type = SHT_MIPS_EVENTS;
   7363       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7364       /* The sh_link field is set in final_write_processing.  */
   7365     }
   7366   else if (strcmp (name, ".msym") == 0)
   7367     {
   7368       hdr->sh_type = SHT_MIPS_MSYM;
   7369       hdr->sh_flags |= SHF_ALLOC;
   7370       hdr->sh_entsize = 8;
   7371     }
   7372 
   7373   /* The generic elf_fake_sections will set up REL_HDR using the default
   7374    kind of relocations.  We used to set up a second header for the
   7375    non-default kind of relocations here, but only NewABI would use
   7376    these, and the IRIX ld doesn't like resulting empty RELA sections.
   7377    Thus we create those header only on demand now.  */
   7378 
   7379   return TRUE;
   7380 }
   7381 
   7382 /* Given a BFD section, try to locate the corresponding ELF section
   7383    index.  This is used by both the 32-bit and the 64-bit ABI.
   7384    Actually, it's not clear to me that the 64-bit ABI supports these,
   7385    but for non-PIC objects we will certainly want support for at least
   7386    the .scommon section.  */
   7387 
   7388 bfd_boolean
   7389 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
   7390 					asection *sec, int *retval)
   7391 {
   7392   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
   7393     {
   7394       *retval = SHN_MIPS_SCOMMON;
   7395       return TRUE;
   7396     }
   7397   if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
   7398     {
   7399       *retval = SHN_MIPS_ACOMMON;
   7400       return TRUE;
   7401     }
   7402   return FALSE;
   7403 }
   7404 
   7405 /* Hook called by the linker routine which adds symbols from an object
   7407    file.  We must handle the special MIPS section numbers here.  */
   7408 
   7409 bfd_boolean
   7410 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
   7411 			       Elf_Internal_Sym *sym, const char **namep,
   7412 			       flagword *flagsp ATTRIBUTE_UNUSED,
   7413 			       asection **secp, bfd_vma *valp)
   7414 {
   7415   if (SGI_COMPAT (abfd)
   7416       && (abfd->flags & DYNAMIC) != 0
   7417       && strcmp (*namep, "_rld_new_interface") == 0)
   7418     {
   7419       /* Skip IRIX5 rld entry name.  */
   7420       *namep = NULL;
   7421       return TRUE;
   7422     }
   7423 
   7424   /* Shared objects may have a dynamic symbol '_gp_disp' defined as
   7425      a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
   7426      by setting a DT_NEEDED for the shared object.  Since _gp_disp is
   7427      a magic symbol resolved by the linker, we ignore this bogus definition
   7428      of _gp_disp.  New ABI objects do not suffer from this problem so this
   7429      is not done for them. */
   7430   if (!NEWABI_P(abfd)
   7431       && (sym->st_shndx == SHN_ABS)
   7432       && (strcmp (*namep, "_gp_disp") == 0))
   7433     {
   7434       *namep = NULL;
   7435       return TRUE;
   7436     }
   7437 
   7438   switch (sym->st_shndx)
   7439     {
   7440     case SHN_COMMON:
   7441       /* Common symbols less than the GP size are automatically
   7442 	 treated as SHN_MIPS_SCOMMON symbols.  */
   7443       if (sym->st_size > elf_gp_size (abfd)
   7444 	  || ELF_ST_TYPE (sym->st_info) == STT_TLS
   7445 	  || IRIX_COMPAT (abfd) == ict_irix6)
   7446 	break;
   7447       /* Fall through.  */
   7448     case SHN_MIPS_SCOMMON:
   7449       *secp = bfd_make_section_old_way (abfd, ".scommon");
   7450       (*secp)->flags |= SEC_IS_COMMON;
   7451       *valp = sym->st_size;
   7452       break;
   7453 
   7454     case SHN_MIPS_TEXT:
   7455       /* This section is used in a shared object.  */
   7456       if (mips_elf_tdata (abfd)->elf_text_section == NULL)
   7457 	{
   7458 	  asymbol *elf_text_symbol;
   7459 	  asection *elf_text_section;
   7460 	  bfd_size_type amt = sizeof (asection);
   7461 
   7462 	  elf_text_section = bfd_zalloc (abfd, amt);
   7463 	  if (elf_text_section == NULL)
   7464 	    return FALSE;
   7465 
   7466 	  amt = sizeof (asymbol);
   7467 	  elf_text_symbol = bfd_zalloc (abfd, amt);
   7468 	  if (elf_text_symbol == NULL)
   7469 	    return FALSE;
   7470 
   7471 	  /* Initialize the section.  */
   7472 
   7473 	  mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
   7474 	  mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
   7475 
   7476 	  elf_text_section->symbol = elf_text_symbol;
   7477 	  elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
   7478 
   7479 	  elf_text_section->name = ".text";
   7480 	  elf_text_section->flags = SEC_NO_FLAGS;
   7481 	  elf_text_section->output_section = NULL;
   7482 	  elf_text_section->owner = abfd;
   7483 	  elf_text_symbol->name = ".text";
   7484 	  elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
   7485 	  elf_text_symbol->section = elf_text_section;
   7486 	}
   7487       /* This code used to do *secp = bfd_und_section_ptr if
   7488          info->shared.  I don't know why, and that doesn't make sense,
   7489          so I took it out.  */
   7490       *secp = mips_elf_tdata (abfd)->elf_text_section;
   7491       break;
   7492 
   7493     case SHN_MIPS_ACOMMON:
   7494       /* Fall through. XXX Can we treat this as allocated data?  */
   7495     case SHN_MIPS_DATA:
   7496       /* This section is used in a shared object.  */
   7497       if (mips_elf_tdata (abfd)->elf_data_section == NULL)
   7498 	{
   7499 	  asymbol *elf_data_symbol;
   7500 	  asection *elf_data_section;
   7501 	  bfd_size_type amt = sizeof (asection);
   7502 
   7503 	  elf_data_section = bfd_zalloc (abfd, amt);
   7504 	  if (elf_data_section == NULL)
   7505 	    return FALSE;
   7506 
   7507 	  amt = sizeof (asymbol);
   7508 	  elf_data_symbol = bfd_zalloc (abfd, amt);
   7509 	  if (elf_data_symbol == NULL)
   7510 	    return FALSE;
   7511 
   7512 	  /* Initialize the section.  */
   7513 
   7514 	  mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
   7515 	  mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
   7516 
   7517 	  elf_data_section->symbol = elf_data_symbol;
   7518 	  elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
   7519 
   7520 	  elf_data_section->name = ".data";
   7521 	  elf_data_section->flags = SEC_NO_FLAGS;
   7522 	  elf_data_section->output_section = NULL;
   7523 	  elf_data_section->owner = abfd;
   7524 	  elf_data_symbol->name = ".data";
   7525 	  elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
   7526 	  elf_data_symbol->section = elf_data_section;
   7527 	}
   7528       /* This code used to do *secp = bfd_und_section_ptr if
   7529          info->shared.  I don't know why, and that doesn't make sense,
   7530          so I took it out.  */
   7531       *secp = mips_elf_tdata (abfd)->elf_data_section;
   7532       break;
   7533 
   7534     case SHN_MIPS_SUNDEFINED:
   7535       *secp = bfd_und_section_ptr;
   7536       break;
   7537     }
   7538 
   7539   if (SGI_COMPAT (abfd)
   7540       && ! info->shared
   7541       && info->output_bfd->xvec == abfd->xvec
   7542       && strcmp (*namep, "__rld_obj_head") == 0)
   7543     {
   7544       struct elf_link_hash_entry *h;
   7545       struct bfd_link_hash_entry *bh;
   7546 
   7547       /* Mark __rld_obj_head as dynamic.  */
   7548       bh = NULL;
   7549       if (! (_bfd_generic_link_add_one_symbol
   7550 	     (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
   7551 	      get_elf_backend_data (abfd)->collect, &bh)))
   7552 	return FALSE;
   7553 
   7554       h = (struct elf_link_hash_entry *) bh;
   7555       h->non_elf = 0;
   7556       h->def_regular = 1;
   7557       h->type = STT_OBJECT;
   7558 
   7559       if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7560 	return FALSE;
   7561 
   7562       mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
   7563       mips_elf_hash_table (info)->rld_symbol = h;
   7564     }
   7565 
   7566   /* If this is a mips16 text symbol, add 1 to the value to make it
   7567      odd.  This will cause something like .word SYM to come up with
   7568      the right value when it is loaded into the PC.  */
   7569   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   7570     ++*valp;
   7571 
   7572   return TRUE;
   7573 }
   7574 
   7575 /* This hook function is called before the linker writes out a global
   7576    symbol.  We mark symbols as small common if appropriate.  This is
   7577    also where we undo the increment of the value for a mips16 symbol.  */
   7578 
   7579 int
   7580 _bfd_mips_elf_link_output_symbol_hook
   7581   (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   7582    const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
   7583    asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
   7584 {
   7585   /* If we see a common symbol, which implies a relocatable link, then
   7586      if a symbol was small common in an input file, mark it as small
   7587      common in the output file.  */
   7588   if (sym->st_shndx == SHN_COMMON
   7589       && strcmp (input_sec->name, ".scommon") == 0)
   7590     sym->st_shndx = SHN_MIPS_SCOMMON;
   7591 
   7592   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   7593     sym->st_value &= ~1;
   7594 
   7595   return 1;
   7596 }
   7597 
   7598 /* Functions for the dynamic linker.  */
   7600 
   7601 /* Create dynamic sections when linking against a dynamic object.  */
   7602 
   7603 bfd_boolean
   7604 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   7605 {
   7606   struct elf_link_hash_entry *h;
   7607   struct bfd_link_hash_entry *bh;
   7608   flagword flags;
   7609   register asection *s;
   7610   const char * const *namep;
   7611   struct mips_elf_link_hash_table *htab;
   7612 
   7613   htab = mips_elf_hash_table (info);
   7614   BFD_ASSERT (htab != NULL);
   7615 
   7616   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   7617 	   | SEC_LINKER_CREATED | SEC_READONLY);
   7618 
   7619   /* The psABI requires a read-only .dynamic section, but the VxWorks
   7620      EABI doesn't.  */
   7621   if (!htab->is_vxworks)
   7622     {
   7623       s = bfd_get_linker_section (abfd, ".dynamic");
   7624       if (s != NULL)
   7625 	{
   7626 	  if (! bfd_set_section_flags (abfd, s, flags))
   7627 	    return FALSE;
   7628 	}
   7629     }
   7630 
   7631   /* We need to create .got section.  */
   7632   if (!mips_elf_create_got_section (abfd, info))
   7633     return FALSE;
   7634 
   7635   if (! mips_elf_rel_dyn_section (info, TRUE))
   7636     return FALSE;
   7637 
   7638   /* Create .stub section.  */
   7639   s = bfd_make_section_anyway_with_flags (abfd,
   7640 					  MIPS_ELF_STUB_SECTION_NAME (abfd),
   7641 					  flags | SEC_CODE);
   7642   if (s == NULL
   7643       || ! bfd_set_section_alignment (abfd, s,
   7644 				      MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   7645     return FALSE;
   7646   htab->sstubs = s;
   7647 
   7648   if (!mips_elf_hash_table (info)->use_rld_obj_head
   7649       && info->executable
   7650       && bfd_get_linker_section (abfd, ".rld_map") == NULL)
   7651     {
   7652       s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
   7653 					      flags &~ (flagword) SEC_READONLY);
   7654       if (s == NULL
   7655 	  || ! bfd_set_section_alignment (abfd, s,
   7656 					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   7657 	return FALSE;
   7658     }
   7659 
   7660   /* On IRIX5, we adjust add some additional symbols and change the
   7661      alignments of several sections.  There is no ABI documentation
   7662      indicating that this is necessary on IRIX6, nor any evidence that
   7663      the linker takes such action.  */
   7664   if (IRIX_COMPAT (abfd) == ict_irix5)
   7665     {
   7666       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
   7667 	{
   7668 	  bh = NULL;
   7669 	  if (! (_bfd_generic_link_add_one_symbol
   7670 		 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
   7671 		  NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
   7672 	    return FALSE;
   7673 
   7674 	  h = (struct elf_link_hash_entry *) bh;
   7675 	  h->non_elf = 0;
   7676 	  h->def_regular = 1;
   7677 	  h->type = STT_SECTION;
   7678 
   7679 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7680 	    return FALSE;
   7681 	}
   7682 
   7683       /* We need to create a .compact_rel section.  */
   7684       if (SGI_COMPAT (abfd))
   7685 	{
   7686 	  if (!mips_elf_create_compact_rel_section (abfd, info))
   7687 	    return FALSE;
   7688 	}
   7689 
   7690       /* Change alignments of some sections.  */
   7691       s = bfd_get_linker_section (abfd, ".hash");
   7692       if (s != NULL)
   7693 	(void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7694 
   7695       s = bfd_get_linker_section (abfd, ".dynsym");
   7696       if (s != NULL)
   7697 	(void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7698 
   7699       s = bfd_get_linker_section (abfd, ".dynstr");
   7700       if (s != NULL)
   7701 	(void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7702 
   7703       /* ??? */
   7704       s = bfd_get_section_by_name (abfd, ".reginfo");
   7705       if (s != NULL)
   7706 	(void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7707 
   7708       s = bfd_get_linker_section (abfd, ".dynamic");
   7709       if (s != NULL)
   7710 	(void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7711     }
   7712 
   7713   if (info->executable)
   7714     {
   7715       const char *name;
   7716 
   7717       name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
   7718       bh = NULL;
   7719       if (!(_bfd_generic_link_add_one_symbol
   7720 	    (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
   7721 	     NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
   7722 	return FALSE;
   7723 
   7724       h = (struct elf_link_hash_entry *) bh;
   7725       h->non_elf = 0;
   7726       h->def_regular = 1;
   7727       h->type = STT_SECTION;
   7728 
   7729       if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7730 	return FALSE;
   7731 
   7732       if (! mips_elf_hash_table (info)->use_rld_obj_head)
   7733 	{
   7734 	  /* __rld_map is a four byte word located in the .data section
   7735 	     and is filled in by the rtld to contain a pointer to
   7736 	     the _r_debug structure. Its symbol value will be set in
   7737 	     _bfd_mips_elf_finish_dynamic_symbol.  */
   7738 	  s = bfd_get_linker_section (abfd, ".rld_map");
   7739 	  BFD_ASSERT (s != NULL);
   7740 
   7741 	  name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
   7742 	  bh = NULL;
   7743 	  if (!(_bfd_generic_link_add_one_symbol
   7744 		(info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
   7745 		 get_elf_backend_data (abfd)->collect, &bh)))
   7746 	    return FALSE;
   7747 
   7748 	  h = (struct elf_link_hash_entry *) bh;
   7749 	  h->non_elf = 0;
   7750 	  h->def_regular = 1;
   7751 	  h->type = STT_OBJECT;
   7752 
   7753 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7754 	    return FALSE;
   7755 	  mips_elf_hash_table (info)->rld_symbol = h;
   7756 	}
   7757     }
   7758 
   7759   /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
   7760      Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol.  */
   7761   if (!_bfd_elf_create_dynamic_sections (abfd, info))
   7762     return FALSE;
   7763 
   7764   /* Cache the sections created above.  */
   7765   htab->splt = bfd_get_linker_section (abfd, ".plt");
   7766   htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
   7767   if (htab->is_vxworks)
   7768     {
   7769       htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
   7770       htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
   7771     }
   7772   else
   7773     htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
   7774   if (!htab->sdynbss
   7775       || (htab->is_vxworks && !htab->srelbss && !info->shared)
   7776       || !htab->srelplt
   7777       || !htab->splt)
   7778     abort ();
   7779 
   7780   /* Do the usual VxWorks handling.  */
   7781   if (htab->is_vxworks
   7782       && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
   7783     return FALSE;
   7784 
   7785   return TRUE;
   7786 }
   7787 
   7788 /* Return true if relocation REL against section SEC is a REL rather than
   7790    RELA relocation.  RELOCS is the first relocation in the section and
   7791    ABFD is the bfd that contains SEC.  */
   7792 
   7793 static bfd_boolean
   7794 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
   7795 			   const Elf_Internal_Rela *relocs,
   7796 			   const Elf_Internal_Rela *rel)
   7797 {
   7798   Elf_Internal_Shdr *rel_hdr;
   7799   const struct elf_backend_data *bed;
   7800 
   7801   /* To determine which flavor of relocation this is, we depend on the
   7802      fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
   7803   rel_hdr = elf_section_data (sec)->rel.hdr;
   7804   if (rel_hdr == NULL)
   7805     return FALSE;
   7806   bed = get_elf_backend_data (abfd);
   7807   return ((size_t) (rel - relocs)
   7808 	  < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
   7809 }
   7810 
   7811 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
   7812    HOWTO is the relocation's howto and CONTENTS points to the contents
   7813    of the section that REL is against.  */
   7814 
   7815 static bfd_vma
   7816 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
   7817 			  reloc_howto_type *howto, bfd_byte *contents)
   7818 {
   7819   bfd_byte *location;
   7820   unsigned int r_type;
   7821   bfd_vma addend;
   7822 
   7823   r_type = ELF_R_TYPE (abfd, rel->r_info);
   7824   location = contents + rel->r_offset;
   7825 
   7826   /* Get the addend, which is stored in the input file.  */
   7827   _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
   7828   addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
   7829   _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
   7830 
   7831   return addend & howto->src_mask;
   7832 }
   7833 
   7834 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
   7835    and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
   7836    and update *ADDEND with the final addend.  Return true on success
   7837    or false if the LO16 could not be found.  RELEND is the exclusive
   7838    upper bound on the relocations for REL's section.  */
   7839 
   7840 static bfd_boolean
   7841 mips_elf_add_lo16_rel_addend (bfd *abfd,
   7842 			      const Elf_Internal_Rela *rel,
   7843 			      const Elf_Internal_Rela *relend,
   7844 			      bfd_byte *contents, bfd_vma *addend)
   7845 {
   7846   unsigned int r_type, lo16_type;
   7847   const Elf_Internal_Rela *lo16_relocation;
   7848   reloc_howto_type *lo16_howto;
   7849   bfd_vma l;
   7850 
   7851   r_type = ELF_R_TYPE (abfd, rel->r_info);
   7852   if (mips16_reloc_p (r_type))
   7853     lo16_type = R_MIPS16_LO16;
   7854   else if (micromips_reloc_p (r_type))
   7855     lo16_type = R_MICROMIPS_LO16;
   7856   else if (r_type == R_MIPS_PCHI16)
   7857     lo16_type = R_MIPS_PCLO16;
   7858   else
   7859     lo16_type = R_MIPS_LO16;
   7860 
   7861   /* The combined value is the sum of the HI16 addend, left-shifted by
   7862      sixteen bits, and the LO16 addend, sign extended.  (Usually, the
   7863      code does a `lui' of the HI16 value, and then an `addiu' of the
   7864      LO16 value.)
   7865 
   7866      Scan ahead to find a matching LO16 relocation.
   7867 
   7868      According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
   7869      be immediately following.  However, for the IRIX6 ABI, the next
   7870      relocation may be a composed relocation consisting of several
   7871      relocations for the same address.  In that case, the R_MIPS_LO16
   7872      relocation may occur as one of these.  We permit a similar
   7873      extension in general, as that is useful for GCC.
   7874 
   7875      In some cases GCC dead code elimination removes the LO16 but keeps
   7876      the corresponding HI16.  This is strictly speaking a violation of
   7877      the ABI but not immediately harmful.  */
   7878   lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
   7879   if (lo16_relocation == NULL)
   7880     return FALSE;
   7881 
   7882   /* Obtain the addend kept there.  */
   7883   lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
   7884   l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
   7885 
   7886   l <<= lo16_howto->rightshift;
   7887   l = _bfd_mips_elf_sign_extend (l, 16);
   7888 
   7889   *addend <<= 16;
   7890   *addend += l;
   7891   return TRUE;
   7892 }
   7893 
   7894 /* Try to read the contents of section SEC in bfd ABFD.  Return true and
   7895    store the contents in *CONTENTS on success.  Assume that *CONTENTS
   7896    already holds the contents if it is nonull on entry.  */
   7897 
   7898 static bfd_boolean
   7899 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
   7900 {
   7901   if (*contents)
   7902     return TRUE;
   7903 
   7904   /* Get cached copy if it exists.  */
   7905   if (elf_section_data (sec)->this_hdr.contents != NULL)
   7906     {
   7907       *contents = elf_section_data (sec)->this_hdr.contents;
   7908       return TRUE;
   7909     }
   7910 
   7911   return bfd_malloc_and_get_section (abfd, sec, contents);
   7912 }
   7913 
   7914 /* Make a new PLT record to keep internal data.  */
   7915 
   7916 static struct plt_entry *
   7917 mips_elf_make_plt_record (bfd *abfd)
   7918 {
   7919   struct plt_entry *entry;
   7920 
   7921   entry = bfd_zalloc (abfd, sizeof (*entry));
   7922   if (entry == NULL)
   7923     return NULL;
   7924 
   7925   entry->stub_offset = MINUS_ONE;
   7926   entry->mips_offset = MINUS_ONE;
   7927   entry->comp_offset = MINUS_ONE;
   7928   entry->gotplt_index = MINUS_ONE;
   7929   return entry;
   7930 }
   7931 
   7932 /* Look through the relocs for a section during the first phase, and
   7933    allocate space in the global offset table and record the need for
   7934    standard MIPS and compressed procedure linkage table entries.  */
   7935 
   7936 bfd_boolean
   7937 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   7938 			    asection *sec, const Elf_Internal_Rela *relocs)
   7939 {
   7940   const char *name;
   7941   bfd *dynobj;
   7942   Elf_Internal_Shdr *symtab_hdr;
   7943   struct elf_link_hash_entry **sym_hashes;
   7944   size_t extsymoff;
   7945   const Elf_Internal_Rela *rel;
   7946   const Elf_Internal_Rela *rel_end;
   7947   asection *sreloc;
   7948   const struct elf_backend_data *bed;
   7949   struct mips_elf_link_hash_table *htab;
   7950   bfd_byte *contents;
   7951   bfd_vma addend;
   7952   reloc_howto_type *howto;
   7953 
   7954   if (info->relocatable)
   7955     return TRUE;
   7956 
   7957   htab = mips_elf_hash_table (info);
   7958   BFD_ASSERT (htab != NULL);
   7959 
   7960   dynobj = elf_hash_table (info)->dynobj;
   7961   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   7962   sym_hashes = elf_sym_hashes (abfd);
   7963   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
   7964 
   7965   bed = get_elf_backend_data (abfd);
   7966   rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
   7967 
   7968   /* Check for the mips16 stub sections.  */
   7969 
   7970   name = bfd_get_section_name (abfd, sec);
   7971   if (FN_STUB_P (name))
   7972     {
   7973       unsigned long r_symndx;
   7974 
   7975       /* Look at the relocation information to figure out which symbol
   7976          this is for.  */
   7977 
   7978       r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
   7979       if (r_symndx == 0)
   7980 	{
   7981 	  (*_bfd_error_handler)
   7982 	    (_("%B: Warning: cannot determine the target function for"
   7983 	       " stub section `%s'"),
   7984 	     abfd, name);
   7985 	  bfd_set_error (bfd_error_bad_value);
   7986 	  return FALSE;
   7987 	}
   7988 
   7989       if (r_symndx < extsymoff
   7990 	  || sym_hashes[r_symndx - extsymoff] == NULL)
   7991 	{
   7992 	  asection *o;
   7993 
   7994 	  /* This stub is for a local symbol.  This stub will only be
   7995              needed if there is some relocation in this BFD, other
   7996              than a 16 bit function call, which refers to this symbol.  */
   7997 	  for (o = abfd->sections; o != NULL; o = o->next)
   7998 	    {
   7999 	      Elf_Internal_Rela *sec_relocs;
   8000 	      const Elf_Internal_Rela *r, *rend;
   8001 
   8002 	      /* We can ignore stub sections when looking for relocs.  */
   8003 	      if ((o->flags & SEC_RELOC) == 0
   8004 		  || o->reloc_count == 0
   8005 		  || section_allows_mips16_refs_p (o))
   8006 		continue;
   8007 
   8008 	      sec_relocs
   8009 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   8010 					     info->keep_memory);
   8011 	      if (sec_relocs == NULL)
   8012 		return FALSE;
   8013 
   8014 	      rend = sec_relocs + o->reloc_count;
   8015 	      for (r = sec_relocs; r < rend; r++)
   8016 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
   8017 		    && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
   8018 		  break;
   8019 
   8020 	      if (elf_section_data (o)->relocs != sec_relocs)
   8021 		free (sec_relocs);
   8022 
   8023 	      if (r < rend)
   8024 		break;
   8025 	    }
   8026 
   8027 	  if (o == NULL)
   8028 	    {
   8029 	      /* There is no non-call reloc for this stub, so we do
   8030                  not need it.  Since this function is called before
   8031                  the linker maps input sections to output sections, we
   8032                  can easily discard it by setting the SEC_EXCLUDE
   8033                  flag.  */
   8034 	      sec->flags |= SEC_EXCLUDE;
   8035 	      return TRUE;
   8036 	    }
   8037 
   8038 	  /* Record this stub in an array of local symbol stubs for
   8039              this BFD.  */
   8040 	  if (mips_elf_tdata (abfd)->local_stubs == NULL)
   8041 	    {
   8042 	      unsigned long symcount;
   8043 	      asection **n;
   8044 	      bfd_size_type amt;
   8045 
   8046 	      if (elf_bad_symtab (abfd))
   8047 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
   8048 	      else
   8049 		symcount = symtab_hdr->sh_info;
   8050 	      amt = symcount * sizeof (asection *);
   8051 	      n = bfd_zalloc (abfd, amt);
   8052 	      if (n == NULL)
   8053 		return FALSE;
   8054 	      mips_elf_tdata (abfd)->local_stubs = n;
   8055 	    }
   8056 
   8057 	  sec->flags |= SEC_KEEP;
   8058 	  mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
   8059 
   8060 	  /* We don't need to set mips16_stubs_seen in this case.
   8061              That flag is used to see whether we need to look through
   8062              the global symbol table for stubs.  We don't need to set
   8063              it here, because we just have a local stub.  */
   8064 	}
   8065       else
   8066 	{
   8067 	  struct mips_elf_link_hash_entry *h;
   8068 
   8069 	  h = ((struct mips_elf_link_hash_entry *)
   8070 	       sym_hashes[r_symndx - extsymoff]);
   8071 
   8072 	  while (h->root.root.type == bfd_link_hash_indirect
   8073 		 || h->root.root.type == bfd_link_hash_warning)
   8074 	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   8075 
   8076 	  /* H is the symbol this stub is for.  */
   8077 
   8078 	  /* If we already have an appropriate stub for this function, we
   8079 	     don't need another one, so we can discard this one.  Since
   8080 	     this function is called before the linker maps input sections
   8081 	     to output sections, we can easily discard it by setting the
   8082 	     SEC_EXCLUDE flag.  */
   8083 	  if (h->fn_stub != NULL)
   8084 	    {
   8085 	      sec->flags |= SEC_EXCLUDE;
   8086 	      return TRUE;
   8087 	    }
   8088 
   8089 	  sec->flags |= SEC_KEEP;
   8090 	  h->fn_stub = sec;
   8091 	  mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
   8092 	}
   8093     }
   8094   else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
   8095     {
   8096       unsigned long r_symndx;
   8097       struct mips_elf_link_hash_entry *h;
   8098       asection **loc;
   8099 
   8100       /* Look at the relocation information to figure out which symbol
   8101          this is for.  */
   8102 
   8103       r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
   8104       if (r_symndx == 0)
   8105 	{
   8106 	  (*_bfd_error_handler)
   8107 	    (_("%B: Warning: cannot determine the target function for"
   8108 	       " stub section `%s'"),
   8109 	     abfd, name);
   8110 	  bfd_set_error (bfd_error_bad_value);
   8111 	  return FALSE;
   8112 	}
   8113 
   8114       if (r_symndx < extsymoff
   8115 	  || sym_hashes[r_symndx - extsymoff] == NULL)
   8116 	{
   8117 	  asection *o;
   8118 
   8119 	  /* This stub is for a local symbol.  This stub will only be
   8120              needed if there is some relocation (R_MIPS16_26) in this BFD
   8121              that refers to this symbol.  */
   8122 	  for (o = abfd->sections; o != NULL; o = o->next)
   8123 	    {
   8124 	      Elf_Internal_Rela *sec_relocs;
   8125 	      const Elf_Internal_Rela *r, *rend;
   8126 
   8127 	      /* We can ignore stub sections when looking for relocs.  */
   8128 	      if ((o->flags & SEC_RELOC) == 0
   8129 		  || o->reloc_count == 0
   8130 		  || section_allows_mips16_refs_p (o))
   8131 		continue;
   8132 
   8133 	      sec_relocs
   8134 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   8135 					     info->keep_memory);
   8136 	      if (sec_relocs == NULL)
   8137 		return FALSE;
   8138 
   8139 	      rend = sec_relocs + o->reloc_count;
   8140 	      for (r = sec_relocs; r < rend; r++)
   8141 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
   8142 		    && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
   8143 		    break;
   8144 
   8145 	      if (elf_section_data (o)->relocs != sec_relocs)
   8146 		free (sec_relocs);
   8147 
   8148 	      if (r < rend)
   8149 		break;
   8150 	    }
   8151 
   8152 	  if (o == NULL)
   8153 	    {
   8154 	      /* There is no non-call reloc for this stub, so we do
   8155                  not need it.  Since this function is called before
   8156                  the linker maps input sections to output sections, we
   8157                  can easily discard it by setting the SEC_EXCLUDE
   8158                  flag.  */
   8159 	      sec->flags |= SEC_EXCLUDE;
   8160 	      return TRUE;
   8161 	    }
   8162 
   8163 	  /* Record this stub in an array of local symbol call_stubs for
   8164              this BFD.  */
   8165 	  if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
   8166 	    {
   8167 	      unsigned long symcount;
   8168 	      asection **n;
   8169 	      bfd_size_type amt;
   8170 
   8171 	      if (elf_bad_symtab (abfd))
   8172 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
   8173 	      else
   8174 		symcount = symtab_hdr->sh_info;
   8175 	      amt = symcount * sizeof (asection *);
   8176 	      n = bfd_zalloc (abfd, amt);
   8177 	      if (n == NULL)
   8178 		return FALSE;
   8179 	      mips_elf_tdata (abfd)->local_call_stubs = n;
   8180 	    }
   8181 
   8182 	  sec->flags |= SEC_KEEP;
   8183 	  mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
   8184 
   8185 	  /* We don't need to set mips16_stubs_seen in this case.
   8186              That flag is used to see whether we need to look through
   8187              the global symbol table for stubs.  We don't need to set
   8188              it here, because we just have a local stub.  */
   8189 	}
   8190       else
   8191 	{
   8192 	  h = ((struct mips_elf_link_hash_entry *)
   8193 	       sym_hashes[r_symndx - extsymoff]);
   8194 
   8195 	  /* H is the symbol this stub is for.  */
   8196 
   8197 	  if (CALL_FP_STUB_P (name))
   8198 	    loc = &h->call_fp_stub;
   8199 	  else
   8200 	    loc = &h->call_stub;
   8201 
   8202 	  /* If we already have an appropriate stub for this function, we
   8203 	     don't need another one, so we can discard this one.  Since
   8204 	     this function is called before the linker maps input sections
   8205 	     to output sections, we can easily discard it by setting the
   8206 	     SEC_EXCLUDE flag.  */
   8207 	  if (*loc != NULL)
   8208 	    {
   8209 	      sec->flags |= SEC_EXCLUDE;
   8210 	      return TRUE;
   8211 	    }
   8212 
   8213 	  sec->flags |= SEC_KEEP;
   8214 	  *loc = sec;
   8215 	  mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
   8216 	}
   8217     }
   8218 
   8219   sreloc = NULL;
   8220   contents = NULL;
   8221   for (rel = relocs; rel < rel_end; ++rel)
   8222     {
   8223       unsigned long r_symndx;
   8224       unsigned int r_type;
   8225       struct elf_link_hash_entry *h;
   8226       bfd_boolean can_make_dynamic_p;
   8227       bfd_boolean call_reloc_p;
   8228       bfd_boolean constrain_symbol_p;
   8229 
   8230       r_symndx = ELF_R_SYM (abfd, rel->r_info);
   8231       r_type = ELF_R_TYPE (abfd, rel->r_info);
   8232 
   8233       if (r_symndx < extsymoff)
   8234 	h = NULL;
   8235       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
   8236 	{
   8237 	  (*_bfd_error_handler)
   8238 	    (_("%B: Malformed reloc detected for section %s"),
   8239 	     abfd, name);
   8240 	  bfd_set_error (bfd_error_bad_value);
   8241 	  return FALSE;
   8242 	}
   8243       else
   8244 	{
   8245 	  h = sym_hashes[r_symndx - extsymoff];
   8246 	  if (h != NULL)
   8247 	    {
   8248 	      while (h->root.type == bfd_link_hash_indirect
   8249 		     || h->root.type == bfd_link_hash_warning)
   8250 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8251 
   8252 	      /* PR15323, ref flags aren't set for references in the
   8253 		 same object.  */
   8254 	      h->root.non_ir_ref = 1;
   8255 	    }
   8256 	}
   8257 
   8258       /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
   8259 	 relocation into a dynamic one.  */
   8260       can_make_dynamic_p = FALSE;
   8261 
   8262       /* Set CALL_RELOC_P to true if the relocation is for a call,
   8263 	 and if pointer equality therefore doesn't matter.  */
   8264       call_reloc_p = FALSE;
   8265 
   8266       /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
   8267 	 into account when deciding how to define the symbol.
   8268 	 Relocations in nonallocatable sections such as .pdr and
   8269 	 .debug* should have no effect.  */
   8270       constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
   8271 
   8272       switch (r_type)
   8273 	{
   8274 	case R_MIPS_CALL16:
   8275 	case R_MIPS_CALL_HI16:
   8276 	case R_MIPS_CALL_LO16:
   8277 	case R_MIPS16_CALL16:
   8278 	case R_MICROMIPS_CALL16:
   8279 	case R_MICROMIPS_CALL_HI16:
   8280 	case R_MICROMIPS_CALL_LO16:
   8281 	  call_reloc_p = TRUE;
   8282 	  /* Fall through.  */
   8283 
   8284 	case R_MIPS_GOT16:
   8285 	case R_MIPS_GOT_HI16:
   8286 	case R_MIPS_GOT_LO16:
   8287 	case R_MIPS_GOT_PAGE:
   8288 	case R_MIPS_GOT_OFST:
   8289 	case R_MIPS_GOT_DISP:
   8290 	case R_MIPS_TLS_GOTTPREL:
   8291 	case R_MIPS_TLS_GD:
   8292 	case R_MIPS_TLS_LDM:
   8293 	case R_MIPS16_GOT16:
   8294 	case R_MIPS16_TLS_GOTTPREL:
   8295 	case R_MIPS16_TLS_GD:
   8296 	case R_MIPS16_TLS_LDM:
   8297 	case R_MICROMIPS_GOT16:
   8298 	case R_MICROMIPS_GOT_HI16:
   8299 	case R_MICROMIPS_GOT_LO16:
   8300 	case R_MICROMIPS_GOT_PAGE:
   8301 	case R_MICROMIPS_GOT_OFST:
   8302 	case R_MICROMIPS_GOT_DISP:
   8303 	case R_MICROMIPS_TLS_GOTTPREL:
   8304 	case R_MICROMIPS_TLS_GD:
   8305 	case R_MICROMIPS_TLS_LDM:
   8306 	  if (dynobj == NULL)
   8307 	    elf_hash_table (info)->dynobj = dynobj = abfd;
   8308 	  if (!mips_elf_create_got_section (dynobj, info))
   8309 	    return FALSE;
   8310 	  if (htab->is_vxworks && !info->shared)
   8311 	    {
   8312 	      (*_bfd_error_handler)
   8313 		(_("%B: GOT reloc at 0x%lx not expected in executables"),
   8314 		 abfd, (unsigned long) rel->r_offset);
   8315 	      bfd_set_error (bfd_error_bad_value);
   8316 	      return FALSE;
   8317 	    }
   8318 	  can_make_dynamic_p = TRUE;
   8319 	  break;
   8320 
   8321 	case R_MIPS_NONE:
   8322 	case R_MIPS_JALR:
   8323 	case R_MICROMIPS_JALR:
   8324 	  /* These relocations have empty fields and are purely there to
   8325 	     provide link information.  The symbol value doesn't matter.  */
   8326 	  constrain_symbol_p = FALSE;
   8327 	  break;
   8328 
   8329 	case R_MIPS_GPREL16:
   8330 	case R_MIPS_GPREL32:
   8331 	case R_MIPS16_GPREL:
   8332 	case R_MICROMIPS_GPREL16:
   8333 	  /* GP-relative relocations always resolve to a definition in a
   8334 	     regular input file, ignoring the one-definition rule.  This is
   8335 	     important for the GP setup sequence in NewABI code, which
   8336 	     always resolves to a local function even if other relocations
   8337 	     against the symbol wouldn't.  */
   8338 	  constrain_symbol_p = FALSE;
   8339 	  break;
   8340 
   8341 	case R_MIPS_32:
   8342 	case R_MIPS_REL32:
   8343 	case R_MIPS_64:
   8344 	  /* In VxWorks executables, references to external symbols
   8345 	     must be handled using copy relocs or PLT entries; it is not
   8346 	     possible to convert this relocation into a dynamic one.
   8347 
   8348 	     For executables that use PLTs and copy-relocs, we have a
   8349 	     choice between converting the relocation into a dynamic
   8350 	     one or using copy relocations or PLT entries.  It is
   8351 	     usually better to do the former, unless the relocation is
   8352 	     against a read-only section.  */
   8353 	  if ((info->shared
   8354 	       || (h != NULL
   8355 		   && !htab->is_vxworks
   8356 		   && strcmp (h->root.root.string, "__gnu_local_gp") != 0
   8357 		   && !(!info->nocopyreloc
   8358 			&& !PIC_OBJECT_P (abfd)
   8359 			&& MIPS_ELF_READONLY_SECTION (sec))))
   8360 	      && (sec->flags & SEC_ALLOC) != 0)
   8361 	    {
   8362 	      can_make_dynamic_p = TRUE;
   8363 	      if (dynobj == NULL)
   8364 		elf_hash_table (info)->dynobj = dynobj = abfd;
   8365 	    }
   8366 	  break;
   8367 
   8368 	case R_MIPS_26:
   8369 	case R_MIPS_PC16:
   8370 	case R_MIPS_PC21_S2:
   8371 	case R_MIPS_PC26_S2:
   8372 	case R_MIPS16_26:
   8373 	case R_MICROMIPS_26_S1:
   8374 	case R_MICROMIPS_PC7_S1:
   8375 	case R_MICROMIPS_PC10_S1:
   8376 	case R_MICROMIPS_PC16_S1:
   8377 	case R_MICROMIPS_PC23_S2:
   8378 	  call_reloc_p = TRUE;
   8379 	  break;
   8380 	}
   8381 
   8382       if (h)
   8383 	{
   8384 	  if (constrain_symbol_p)
   8385 	    {
   8386 	      if (!can_make_dynamic_p)
   8387 		((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
   8388 
   8389 	      if (!call_reloc_p)
   8390 		h->pointer_equality_needed = 1;
   8391 
   8392 	      /* We must not create a stub for a symbol that has
   8393 		 relocations related to taking the function's address.
   8394 		 This doesn't apply to VxWorks, where CALL relocs refer
   8395 		 to a .got.plt entry instead of a normal .got entry.  */
   8396 	      if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
   8397 		((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
   8398 	    }
   8399 
   8400 	  /* Relocations against the special VxWorks __GOTT_BASE__ and
   8401 	     __GOTT_INDEX__ symbols must be left to the loader.  Allocate
   8402 	     room for them in .rela.dyn.  */
   8403 	  if (is_gott_symbol (info, h))
   8404 	    {
   8405 	      if (sreloc == NULL)
   8406 		{
   8407 		  sreloc = mips_elf_rel_dyn_section (info, TRUE);
   8408 		  if (sreloc == NULL)
   8409 		    return FALSE;
   8410 		}
   8411 	      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   8412 	      if (MIPS_ELF_READONLY_SECTION (sec))
   8413 		/* We tell the dynamic linker that there are
   8414 		   relocations against the text segment.  */
   8415 		info->flags |= DF_TEXTREL;
   8416 	    }
   8417 	}
   8418       else if (call_lo16_reloc_p (r_type)
   8419 	       || got_lo16_reloc_p (r_type)
   8420 	       || got_disp_reloc_p (r_type)
   8421 	       || (got16_reloc_p (r_type) && htab->is_vxworks))
   8422 	{
   8423 	  /* We may need a local GOT entry for this relocation.  We
   8424 	     don't count R_MIPS_GOT_PAGE because we can estimate the
   8425 	     maximum number of pages needed by looking at the size of
   8426 	     the segment.  Similar comments apply to R_MIPS*_GOT16 and
   8427 	     R_MIPS*_CALL16, except on VxWorks, where GOT relocations
   8428 	     always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
   8429 	     R_MIPS_CALL_HI16 because these are always followed by an
   8430 	     R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
   8431 	  if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
   8432 						 rel->r_addend, info, r_type))
   8433 	    return FALSE;
   8434 	}
   8435 
   8436       if (h != NULL
   8437 	  && mips_elf_relocation_needs_la25_stub (abfd, r_type,
   8438 						  ELF_ST_IS_MIPS16 (h->other)))
   8439 	((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
   8440 
   8441       switch (r_type)
   8442 	{
   8443 	case R_MIPS_CALL16:
   8444 	case R_MIPS16_CALL16:
   8445 	case R_MICROMIPS_CALL16:
   8446 	  if (h == NULL)
   8447 	    {
   8448 	      (*_bfd_error_handler)
   8449 		(_("%B: CALL16 reloc at 0x%lx not against global symbol"),
   8450 		 abfd, (unsigned long) rel->r_offset);
   8451 	      bfd_set_error (bfd_error_bad_value);
   8452 	      return FALSE;
   8453 	    }
   8454 	  /* Fall through.  */
   8455 
   8456 	case R_MIPS_CALL_HI16:
   8457 	case R_MIPS_CALL_LO16:
   8458 	case R_MICROMIPS_CALL_HI16:
   8459 	case R_MICROMIPS_CALL_LO16:
   8460 	  if (h != NULL)
   8461 	    {
   8462 	      /* Make sure there is room in the regular GOT to hold the
   8463 		 function's address.  We may eliminate it in favour of
   8464 		 a .got.plt entry later; see mips_elf_count_got_symbols.  */
   8465 	      if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
   8466 						      r_type))
   8467 		return FALSE;
   8468 
   8469 	      /* We need a stub, not a plt entry for the undefined
   8470 		 function.  But we record it as if it needs plt.  See
   8471 		 _bfd_elf_adjust_dynamic_symbol.  */
   8472 	      h->needs_plt = 1;
   8473 	      h->type = STT_FUNC;
   8474 	    }
   8475 	  break;
   8476 
   8477 	case R_MIPS_GOT_PAGE:
   8478 	case R_MICROMIPS_GOT_PAGE:
   8479 	case R_MIPS16_GOT16:
   8480 	case R_MIPS_GOT16:
   8481 	case R_MIPS_GOT_HI16:
   8482 	case R_MIPS_GOT_LO16:
   8483 	case R_MICROMIPS_GOT16:
   8484 	case R_MICROMIPS_GOT_HI16:
   8485 	case R_MICROMIPS_GOT_LO16:
   8486 	  if (!h || got_page_reloc_p (r_type))
   8487 	    {
   8488 	      /* This relocation needs (or may need, if h != NULL) a
   8489 		 page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
   8490 		 know for sure until we know whether the symbol is
   8491 		 preemptible.  */
   8492 	      if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
   8493 		{
   8494 		  if (!mips_elf_get_section_contents (abfd, sec, &contents))
   8495 		    return FALSE;
   8496 		  howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
   8497 		  addend = mips_elf_read_rel_addend (abfd, rel,
   8498 						     howto, contents);
   8499 		  if (got16_reloc_p (r_type))
   8500 		    mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
   8501 						  contents, &addend);
   8502 		  else
   8503 		    addend <<= howto->rightshift;
   8504 		}
   8505 	      else
   8506 		addend = rel->r_addend;
   8507 	      if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
   8508 						 h, addend))
   8509 		return FALSE;
   8510 
   8511 	      if (h)
   8512 		{
   8513 		  struct mips_elf_link_hash_entry *hmips =
   8514 		    (struct mips_elf_link_hash_entry *) h;
   8515 
   8516 		  /* This symbol is definitely not overridable.  */
   8517 		  if (hmips->root.def_regular
   8518 		      && ! (info->shared && ! info->symbolic
   8519 			    && ! hmips->root.forced_local))
   8520 		    h = NULL;
   8521 		}
   8522 	    }
   8523 	  /* If this is a global, overridable symbol, GOT_PAGE will
   8524 	     decay to GOT_DISP, so we'll need a GOT entry for it.  */
   8525 	  /* Fall through.  */
   8526 
   8527 	case R_MIPS_GOT_DISP:
   8528 	case R_MICROMIPS_GOT_DISP:
   8529 	  if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
   8530 						       FALSE, r_type))
   8531 	    return FALSE;
   8532 	  break;
   8533 
   8534 	case R_MIPS_TLS_GOTTPREL:
   8535 	case R_MIPS16_TLS_GOTTPREL:
   8536 	case R_MICROMIPS_TLS_GOTTPREL:
   8537 	  if (info->shared)
   8538 	    info->flags |= DF_STATIC_TLS;
   8539 	  /* Fall through */
   8540 
   8541 	case R_MIPS_TLS_LDM:
   8542 	case R_MIPS16_TLS_LDM:
   8543 	case R_MICROMIPS_TLS_LDM:
   8544 	  if (tls_ldm_reloc_p (r_type))
   8545 	    {
   8546 	      r_symndx = STN_UNDEF;
   8547 	      h = NULL;
   8548 	    }
   8549 	  /* Fall through */
   8550 
   8551 	case R_MIPS_TLS_GD:
   8552 	case R_MIPS16_TLS_GD:
   8553 	case R_MICROMIPS_TLS_GD:
   8554 	  /* This symbol requires a global offset table entry, or two
   8555 	     for TLS GD relocations.  */
   8556 	  if (h != NULL)
   8557 	    {
   8558 	      if (!mips_elf_record_global_got_symbol (h, abfd, info,
   8559 						      FALSE, r_type))
   8560 		return FALSE;
   8561 	    }
   8562 	  else
   8563 	    {
   8564 	      if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
   8565 						     rel->r_addend,
   8566 						     info, r_type))
   8567 		return FALSE;
   8568 	    }
   8569 	  break;
   8570 
   8571 	case R_MIPS_32:
   8572 	case R_MIPS_REL32:
   8573 	case R_MIPS_64:
   8574 	  /* In VxWorks executables, references to external symbols
   8575 	     are handled using copy relocs or PLT stubs, so there's
   8576 	     no need to add a .rela.dyn entry for this relocation.  */
   8577 	  if (can_make_dynamic_p)
   8578 	    {
   8579 	      if (sreloc == NULL)
   8580 		{
   8581 		  sreloc = mips_elf_rel_dyn_section (info, TRUE);
   8582 		  if (sreloc == NULL)
   8583 		    return FALSE;
   8584 		}
   8585 	      if (info->shared && h == NULL)
   8586 		{
   8587 		  /* When creating a shared object, we must copy these
   8588 		     reloc types into the output file as R_MIPS_REL32
   8589 		     relocs.  Make room for this reloc in .rel(a).dyn.  */
   8590 		  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   8591 		  if (MIPS_ELF_READONLY_SECTION (sec))
   8592 		    /* We tell the dynamic linker that there are
   8593 		       relocations against the text segment.  */
   8594 		    info->flags |= DF_TEXTREL;
   8595 		}
   8596 	      else
   8597 		{
   8598 		  struct mips_elf_link_hash_entry *hmips;
   8599 
   8600 		  /* For a shared object, we must copy this relocation
   8601 		     unless the symbol turns out to be undefined and
   8602 		     weak with non-default visibility, in which case
   8603 		     it will be left as zero.
   8604 
   8605 		     We could elide R_MIPS_REL32 for locally binding symbols
   8606 		     in shared libraries, but do not yet do so.
   8607 
   8608 		     For an executable, we only need to copy this
   8609 		     reloc if the symbol is defined in a dynamic
   8610 		     object.  */
   8611 		  hmips = (struct mips_elf_link_hash_entry *) h;
   8612 		  ++hmips->possibly_dynamic_relocs;
   8613 		  if (MIPS_ELF_READONLY_SECTION (sec))
   8614 		    /* We need it to tell the dynamic linker if there
   8615 		       are relocations against the text segment.  */
   8616 		    hmips->readonly_reloc = TRUE;
   8617 		}
   8618 	    }
   8619 
   8620 	  if (SGI_COMPAT (abfd))
   8621 	    mips_elf_hash_table (info)->compact_rel_size +=
   8622 	      sizeof (Elf32_External_crinfo);
   8623 	  break;
   8624 
   8625 	case R_MIPS_26:
   8626 	case R_MIPS_GPREL16:
   8627 	case R_MIPS_LITERAL:
   8628 	case R_MIPS_GPREL32:
   8629 	case R_MICROMIPS_26_S1:
   8630 	case R_MICROMIPS_GPREL16:
   8631 	case R_MICROMIPS_LITERAL:
   8632 	case R_MICROMIPS_GPREL7_S2:
   8633 	  if (SGI_COMPAT (abfd))
   8634 	    mips_elf_hash_table (info)->compact_rel_size +=
   8635 	      sizeof (Elf32_External_crinfo);
   8636 	  break;
   8637 
   8638 	  /* This relocation describes the C++ object vtable hierarchy.
   8639 	     Reconstruct it for later use during GC.  */
   8640 	case R_MIPS_GNU_VTINHERIT:
   8641 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   8642 	    return FALSE;
   8643 	  break;
   8644 
   8645 	  /* This relocation describes which C++ vtable entries are actually
   8646 	     used.  Record for later use during GC.  */
   8647 	case R_MIPS_GNU_VTENTRY:
   8648 	  BFD_ASSERT (h != NULL);
   8649 	  if (h != NULL
   8650 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
   8651 	    return FALSE;
   8652 	  break;
   8653 
   8654 	default:
   8655 	  break;
   8656 	}
   8657 
   8658       /* Record the need for a PLT entry.  At this point we don't know
   8659          yet if we are going to create a PLT in the first place, but
   8660          we only record whether the relocation requires a standard MIPS
   8661          or a compressed code entry anyway.  If we don't make a PLT after
   8662          all, then we'll just ignore these arrangements.  Likewise if
   8663          a PLT entry is not created because the symbol is satisfied
   8664          locally.  */
   8665       if (h != NULL
   8666 	  && jal_reloc_p (r_type)
   8667 	  && !SYMBOL_CALLS_LOCAL (info, h))
   8668 	{
   8669 	  if (h->plt.plist == NULL)
   8670 	    h->plt.plist = mips_elf_make_plt_record (abfd);
   8671 	  if (h->plt.plist == NULL)
   8672 	    return FALSE;
   8673 
   8674 	  if (r_type == R_MIPS_26)
   8675 	    h->plt.plist->need_mips = TRUE;
   8676 	  else
   8677 	    h->plt.plist->need_comp = TRUE;
   8678 	}
   8679 
   8680       /* See if this reloc would need to refer to a MIPS16 hard-float stub,
   8681 	 if there is one.  We only need to handle global symbols here;
   8682 	 we decide whether to keep or delete stubs for local symbols
   8683 	 when processing the stub's relocations.  */
   8684       if (h != NULL
   8685 	  && !mips16_call_reloc_p (r_type)
   8686 	  && !section_allows_mips16_refs_p (sec))
   8687 	{
   8688 	  struct mips_elf_link_hash_entry *mh;
   8689 
   8690 	  mh = (struct mips_elf_link_hash_entry *) h;
   8691 	  mh->need_fn_stub = TRUE;
   8692 	}
   8693 
   8694       /* Refuse some position-dependent relocations when creating a
   8695 	 shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
   8696 	 not PIC, but we can create dynamic relocations and the result
   8697 	 will be fine.  Also do not refuse R_MIPS_LO16, which can be
   8698 	 combined with R_MIPS_GOT16.  */
   8699       if (info->shared)
   8700 	{
   8701 	  switch (r_type)
   8702 	    {
   8703 	    case R_MIPS16_HI16:
   8704 	    case R_MIPS_HI16:
   8705 	    case R_MIPS_HIGHER:
   8706 	    case R_MIPS_HIGHEST:
   8707 	    case R_MICROMIPS_HI16:
   8708 	    case R_MICROMIPS_HIGHER:
   8709 	    case R_MICROMIPS_HIGHEST:
   8710 	      /* Don't refuse a high part relocation if it's against
   8711 		 no symbol (e.g. part of a compound relocation).  */
   8712 	      if (r_symndx == STN_UNDEF)
   8713 		break;
   8714 
   8715 	      /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
   8716 		 and has a special meaning.  */
   8717 	      if (!NEWABI_P (abfd) && h != NULL
   8718 		  && strcmp (h->root.root.string, "_gp_disp") == 0)
   8719 		break;
   8720 
   8721 	      /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
   8722 	      if (is_gott_symbol (info, h))
   8723 		break;
   8724 
   8725 	      /* FALLTHROUGH */
   8726 
   8727 	    case R_MIPS16_26:
   8728 	    case R_MIPS_26:
   8729 	    case R_MICROMIPS_26_S1:
   8730 	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
   8731 	      (*_bfd_error_handler)
   8732 		(_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
   8733 		 abfd, howto->name,
   8734 		 (h) ? h->root.root.string : "a local symbol");
   8735 	      bfd_set_error (bfd_error_bad_value);
   8736 	      return FALSE;
   8737 	    default:
   8738 	      break;
   8739 	    }
   8740 	}
   8741     }
   8742 
   8743   return TRUE;
   8744 }
   8745 
   8746 bfd_boolean
   8748 _bfd_mips_relax_section (bfd *abfd, asection *sec,
   8749 			 struct bfd_link_info *link_info,
   8750 			 bfd_boolean *again)
   8751 {
   8752   Elf_Internal_Rela *internal_relocs;
   8753   Elf_Internal_Rela *irel, *irelend;
   8754   Elf_Internal_Shdr *symtab_hdr;
   8755   bfd_byte *contents = NULL;
   8756   size_t extsymoff;
   8757   bfd_boolean changed_contents = FALSE;
   8758   bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
   8759   Elf_Internal_Sym *isymbuf = NULL;
   8760 
   8761   /* We are not currently changing any sizes, so only one pass.  */
   8762   *again = FALSE;
   8763 
   8764   if (link_info->relocatable)
   8765     return TRUE;
   8766 
   8767   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   8768 					       link_info->keep_memory);
   8769   if (internal_relocs == NULL)
   8770     return TRUE;
   8771 
   8772   irelend = internal_relocs + sec->reloc_count
   8773     * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
   8774   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8775   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
   8776 
   8777   for (irel = internal_relocs; irel < irelend; irel++)
   8778     {
   8779       bfd_vma symval;
   8780       bfd_signed_vma sym_offset;
   8781       unsigned int r_type;
   8782       unsigned long r_symndx;
   8783       asection *sym_sec;
   8784       unsigned long instruction;
   8785 
   8786       /* Turn jalr into bgezal, and jr into beq, if they're marked
   8787 	 with a JALR relocation, that indicate where they jump to.
   8788 	 This saves some pipeline bubbles.  */
   8789       r_type = ELF_R_TYPE (abfd, irel->r_info);
   8790       if (r_type != R_MIPS_JALR)
   8791 	continue;
   8792 
   8793       r_symndx = ELF_R_SYM (abfd, irel->r_info);
   8794       /* Compute the address of the jump target.  */
   8795       if (r_symndx >= extsymoff)
   8796 	{
   8797 	  struct mips_elf_link_hash_entry *h
   8798 	    = ((struct mips_elf_link_hash_entry *)
   8799 	       elf_sym_hashes (abfd) [r_symndx - extsymoff]);
   8800 
   8801 	  while (h->root.root.type == bfd_link_hash_indirect
   8802 		 || h->root.root.type == bfd_link_hash_warning)
   8803 	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   8804 
   8805 	  /* If a symbol is undefined, or if it may be overridden,
   8806 	     skip it.  */
   8807 	  if (! ((h->root.root.type == bfd_link_hash_defined
   8808 		  || h->root.root.type == bfd_link_hash_defweak)
   8809 		 && h->root.root.u.def.section)
   8810 	      || (link_info->shared && ! link_info->symbolic
   8811 		  && !h->root.forced_local))
   8812 	    continue;
   8813 
   8814 	  sym_sec = h->root.root.u.def.section;
   8815 	  if (sym_sec->output_section)
   8816 	    symval = (h->root.root.u.def.value
   8817 		      + sym_sec->output_section->vma
   8818 		      + sym_sec->output_offset);
   8819 	  else
   8820 	    symval = h->root.root.u.def.value;
   8821 	}
   8822       else
   8823 	{
   8824 	  Elf_Internal_Sym *isym;
   8825 
   8826 	  /* Read this BFD's symbols if we haven't done so already.  */
   8827 	  if (isymbuf == NULL && symtab_hdr->sh_info != 0)
   8828 	    {
   8829 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   8830 	      if (isymbuf == NULL)
   8831 		isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   8832 						symtab_hdr->sh_info, 0,
   8833 						NULL, NULL, NULL);
   8834 	      if (isymbuf == NULL)
   8835 		goto relax_return;
   8836 	    }
   8837 
   8838 	  isym = isymbuf + r_symndx;
   8839 	  if (isym->st_shndx == SHN_UNDEF)
   8840 	    continue;
   8841 	  else if (isym->st_shndx == SHN_ABS)
   8842 	    sym_sec = bfd_abs_section_ptr;
   8843 	  else if (isym->st_shndx == SHN_COMMON)
   8844 	    sym_sec = bfd_com_section_ptr;
   8845 	  else
   8846 	    sym_sec
   8847 	      = bfd_section_from_elf_index (abfd, isym->st_shndx);
   8848 	  symval = isym->st_value
   8849 	    + sym_sec->output_section->vma
   8850 	    + sym_sec->output_offset;
   8851 	}
   8852 
   8853       /* Compute branch offset, from delay slot of the jump to the
   8854 	 branch target.  */
   8855       sym_offset = (symval + irel->r_addend)
   8856 	- (sec_start + irel->r_offset + 4);
   8857 
   8858       /* Branch offset must be properly aligned.  */
   8859       if ((sym_offset & 3) != 0)
   8860 	continue;
   8861 
   8862       sym_offset >>= 2;
   8863 
   8864       /* Check that it's in range.  */
   8865       if (sym_offset < -0x8000 || sym_offset >= 0x8000)
   8866 	continue;
   8867 
   8868       /* Get the section contents if we haven't done so already.  */
   8869       if (!mips_elf_get_section_contents (abfd, sec, &contents))
   8870 	goto relax_return;
   8871 
   8872       instruction = bfd_get_32 (abfd, contents + irel->r_offset);
   8873 
   8874       /* If it was jalr <reg>, turn it into bgezal $zero, <target>.  */
   8875       if ((instruction & 0xfc1fffff) == 0x0000f809)
   8876 	instruction = 0x04110000;
   8877       /* If it was jr <reg>, turn it into b <target>.  */
   8878       else if ((instruction & 0xfc1fffff) == 0x00000008)
   8879 	instruction = 0x10000000;
   8880       else
   8881 	continue;
   8882 
   8883       instruction |= (sym_offset & 0xffff);
   8884       bfd_put_32 (abfd, instruction, contents + irel->r_offset);
   8885       changed_contents = TRUE;
   8886     }
   8887 
   8888   if (contents != NULL
   8889       && elf_section_data (sec)->this_hdr.contents != contents)
   8890     {
   8891       if (!changed_contents && !link_info->keep_memory)
   8892         free (contents);
   8893       else
   8894         {
   8895           /* Cache the section contents for elf_link_input_bfd.  */
   8896           elf_section_data (sec)->this_hdr.contents = contents;
   8897         }
   8898     }
   8899   return TRUE;
   8900 
   8901  relax_return:
   8902   if (contents != NULL
   8903       && elf_section_data (sec)->this_hdr.contents != contents)
   8904     free (contents);
   8905   return FALSE;
   8906 }
   8907 
   8908 /* Allocate space for global sym dynamic relocs.  */
   8910 
   8911 static bfd_boolean
   8912 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   8913 {
   8914   struct bfd_link_info *info = inf;
   8915   bfd *dynobj;
   8916   struct mips_elf_link_hash_entry *hmips;
   8917   struct mips_elf_link_hash_table *htab;
   8918 
   8919   htab = mips_elf_hash_table (info);
   8920   BFD_ASSERT (htab != NULL);
   8921 
   8922   dynobj = elf_hash_table (info)->dynobj;
   8923   hmips = (struct mips_elf_link_hash_entry *) h;
   8924 
   8925   /* VxWorks executables are handled elsewhere; we only need to
   8926      allocate relocations in shared objects.  */
   8927   if (htab->is_vxworks && !info->shared)
   8928     return TRUE;
   8929 
   8930   /* Ignore indirect symbols.  All relocations against such symbols
   8931      will be redirected to the target symbol.  */
   8932   if (h->root.type == bfd_link_hash_indirect)
   8933     return TRUE;
   8934 
   8935   /* If this symbol is defined in a dynamic object, or we are creating
   8936      a shared library, we will need to copy any R_MIPS_32 or
   8937      R_MIPS_REL32 relocs against it into the output file.  */
   8938   if (! info->relocatable
   8939       && hmips->possibly_dynamic_relocs != 0
   8940       && (h->root.type == bfd_link_hash_defweak
   8941 	  || (!h->def_regular && !ELF_COMMON_DEF_P (h))
   8942 	  || info->shared))
   8943     {
   8944       bfd_boolean do_copy = TRUE;
   8945 
   8946       if (h->root.type == bfd_link_hash_undefweak)
   8947 	{
   8948 	  /* Do not copy relocations for undefined weak symbols with
   8949 	     non-default visibility.  */
   8950 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   8951 	    do_copy = FALSE;
   8952 
   8953 	  /* Make sure undefined weak symbols are output as a dynamic
   8954 	     symbol in PIEs.  */
   8955 	  else if (h->dynindx == -1 && !h->forced_local)
   8956 	    {
   8957 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8958 		return FALSE;
   8959 	    }
   8960 	}
   8961 
   8962       if (do_copy)
   8963 	{
   8964 	  /* Even though we don't directly need a GOT entry for this symbol,
   8965 	     the SVR4 psABI requires it to have a dynamic symbol table
   8966 	     index greater that DT_MIPS_GOTSYM if there are dynamic
   8967 	     relocations against it.
   8968 
   8969 	     VxWorks does not enforce the same mapping between the GOT
   8970 	     and the symbol table, so the same requirement does not
   8971 	     apply there.  */
   8972 	  if (!htab->is_vxworks)
   8973 	    {
   8974 	      if (hmips->global_got_area > GGA_RELOC_ONLY)
   8975 		hmips->global_got_area = GGA_RELOC_ONLY;
   8976 	      hmips->got_only_for_calls = FALSE;
   8977 	    }
   8978 
   8979 	  mips_elf_allocate_dynamic_relocations
   8980 	    (dynobj, info, hmips->possibly_dynamic_relocs);
   8981 	  if (hmips->readonly_reloc)
   8982 	    /* We tell the dynamic linker that there are relocations
   8983 	       against the text segment.  */
   8984 	    info->flags |= DF_TEXTREL;
   8985 	}
   8986     }
   8987 
   8988   return TRUE;
   8989 }
   8990 
   8991 /* Adjust a symbol defined by a dynamic object and referenced by a
   8992    regular object.  The current definition is in some section of the
   8993    dynamic object, but we're not including those sections.  We have to
   8994    change the definition to something the rest of the link can
   8995    understand.  */
   8996 
   8997 bfd_boolean
   8998 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   8999 				     struct elf_link_hash_entry *h)
   9000 {
   9001   bfd *dynobj;
   9002   struct mips_elf_link_hash_entry *hmips;
   9003   struct mips_elf_link_hash_table *htab;
   9004 
   9005   htab = mips_elf_hash_table (info);
   9006   BFD_ASSERT (htab != NULL);
   9007 
   9008   dynobj = elf_hash_table (info)->dynobj;
   9009   hmips = (struct mips_elf_link_hash_entry *) h;
   9010 
   9011   /* Make sure we know what is going on here.  */
   9012   BFD_ASSERT (dynobj != NULL
   9013 	      && (h->needs_plt
   9014 		  || h->u.weakdef != NULL
   9015 		  || (h->def_dynamic
   9016 		      && h->ref_regular
   9017 		      && !h->def_regular)));
   9018 
   9019   hmips = (struct mips_elf_link_hash_entry *) h;
   9020 
   9021   /* If there are call relocations against an externally-defined symbol,
   9022      see whether we can create a MIPS lazy-binding stub for it.  We can
   9023      only do this if all references to the function are through call
   9024      relocations, and in that case, the traditional lazy-binding stubs
   9025      are much more efficient than PLT entries.
   9026 
   9027      Traditional stubs are only available on SVR4 psABI-based systems;
   9028      VxWorks always uses PLTs instead.  */
   9029   if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
   9030     {
   9031       if (! elf_hash_table (info)->dynamic_sections_created)
   9032 	return TRUE;
   9033 
   9034       /* If this symbol is not defined in a regular file, then set
   9035 	 the symbol to the stub location.  This is required to make
   9036 	 function pointers compare as equal between the normal
   9037 	 executable and the shared library.  */
   9038       if (!h->def_regular)
   9039 	{
   9040 	  hmips->needs_lazy_stub = TRUE;
   9041 	  htab->lazy_stub_count++;
   9042 	  return TRUE;
   9043 	}
   9044     }
   9045   /* As above, VxWorks requires PLT entries for externally-defined
   9046      functions that are only accessed through call relocations.
   9047 
   9048      Both VxWorks and non-VxWorks targets also need PLT entries if there
   9049      are static-only relocations against an externally-defined function.
   9050      This can technically occur for shared libraries if there are
   9051      branches to the symbol, although it is unlikely that this will be
   9052      used in practice due to the short ranges involved.  It can occur
   9053      for any relative or absolute relocation in executables; in that
   9054      case, the PLT entry becomes the function's canonical address.  */
   9055   else if (((h->needs_plt && !hmips->no_fn_stub)
   9056 	    || (h->type == STT_FUNC && hmips->has_static_relocs))
   9057 	   && htab->use_plts_and_copy_relocs
   9058 	   && !SYMBOL_CALLS_LOCAL (info, h)
   9059 	   && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   9060 		&& h->root.type == bfd_link_hash_undefweak))
   9061     {
   9062       bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
   9063       bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
   9064 
   9065       /* If this is the first symbol to need a PLT entry, then make some
   9066          basic setup.  Also work out PLT entry sizes.  We'll need them
   9067          for PLT offset calculations.  */
   9068       if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
   9069 	{
   9070 	  BFD_ASSERT (htab->sgotplt->size == 0);
   9071 	  BFD_ASSERT (htab->plt_got_index == 0);
   9072 
   9073 	  /* If we're using the PLT additions to the psABI, each PLT
   9074 	     entry is 16 bytes and the PLT0 entry is 32 bytes.
   9075 	     Encourage better cache usage by aligning.  We do this
   9076 	     lazily to avoid pessimizing traditional objects.  */
   9077 	  if (!htab->is_vxworks
   9078 	      && !bfd_set_section_alignment (dynobj, htab->splt, 5))
   9079 	    return FALSE;
   9080 
   9081 	  /* Make sure that .got.plt is word-aligned.  We do this lazily
   9082 	     for the same reason as above.  */
   9083 	  if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
   9084 					  MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
   9085 	    return FALSE;
   9086 
   9087 	  /* On non-VxWorks targets, the first two entries in .got.plt
   9088 	     are reserved.  */
   9089 	  if (!htab->is_vxworks)
   9090 	    htab->plt_got_index
   9091 	      += (get_elf_backend_data (dynobj)->got_header_size
   9092 		  / MIPS_ELF_GOT_SIZE (dynobj));
   9093 
   9094 	  /* On VxWorks, also allocate room for the header's
   9095 	     .rela.plt.unloaded entries.  */
   9096 	  if (htab->is_vxworks && !info->shared)
   9097 	    htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
   9098 
   9099 	  /* Now work out the sizes of individual PLT entries.  */
   9100 	  if (htab->is_vxworks && info->shared)
   9101 	    htab->plt_mips_entry_size
   9102 	      = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
   9103 	  else if (htab->is_vxworks)
   9104 	    htab->plt_mips_entry_size
   9105 	      = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
   9106 	  else if (newabi_p)
   9107 	    htab->plt_mips_entry_size
   9108 	      = 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9109 	  else if (!micromips_p)
   9110 	    {
   9111 	      htab->plt_mips_entry_size
   9112 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9113 	      htab->plt_comp_entry_size
   9114 		= 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
   9115 	    }
   9116 	  else if (htab->insn32)
   9117 	    {
   9118 	      htab->plt_mips_entry_size
   9119 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9120 	      htab->plt_comp_entry_size
   9121 		= 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
   9122 	    }
   9123 	  else
   9124 	    {
   9125 	      htab->plt_mips_entry_size
   9126 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9127 	      htab->plt_comp_entry_size
   9128 		= 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
   9129 	    }
   9130 	}
   9131 
   9132       if (h->plt.plist == NULL)
   9133 	h->plt.plist = mips_elf_make_plt_record (dynobj);
   9134       if (h->plt.plist == NULL)
   9135 	return FALSE;
   9136 
   9137       /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
   9138          n32 or n64, so always use a standard entry there.
   9139 
   9140          If the symbol has a MIPS16 call stub and gets a PLT entry, then
   9141          all MIPS16 calls will go via that stub, and there is no benefit
   9142          to having a MIPS16 entry.  And in the case of call_stub a
   9143          standard entry actually has to be used as the stub ends with a J
   9144          instruction.  */
   9145       if (newabi_p
   9146 	  || htab->is_vxworks
   9147 	  || hmips->call_stub
   9148 	  || hmips->call_fp_stub)
   9149 	{
   9150 	  h->plt.plist->need_mips = TRUE;
   9151 	  h->plt.plist->need_comp = FALSE;
   9152 	}
   9153 
   9154       /* Otherwise, if there are no direct calls to the function, we
   9155          have a free choice of whether to use standard or compressed
   9156          entries.  Prefer microMIPS entries if the object is known to
   9157          contain microMIPS code, so that it becomes possible to create
   9158          pure microMIPS binaries.  Prefer standard entries otherwise,
   9159          because MIPS16 ones are no smaller and are usually slower.  */
   9160       if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
   9161 	{
   9162 	  if (micromips_p)
   9163 	    h->plt.plist->need_comp = TRUE;
   9164 	  else
   9165 	    h->plt.plist->need_mips = TRUE;
   9166 	}
   9167 
   9168       if (h->plt.plist->need_mips)
   9169 	{
   9170 	  h->plt.plist->mips_offset = htab->plt_mips_offset;
   9171 	  htab->plt_mips_offset += htab->plt_mips_entry_size;
   9172 	}
   9173       if (h->plt.plist->need_comp)
   9174 	{
   9175 	  h->plt.plist->comp_offset = htab->plt_comp_offset;
   9176 	  htab->plt_comp_offset += htab->plt_comp_entry_size;
   9177 	}
   9178 
   9179       /* Reserve the corresponding .got.plt entry now too.  */
   9180       h->plt.plist->gotplt_index = htab->plt_got_index++;
   9181 
   9182       /* If the output file has no definition of the symbol, set the
   9183 	 symbol's value to the address of the stub.  */
   9184       if (!info->shared && !h->def_regular)
   9185 	hmips->use_plt_entry = TRUE;
   9186 
   9187       /* Make room for the R_MIPS_JUMP_SLOT relocation.  */
   9188       htab->srelplt->size += (htab->is_vxworks
   9189 			      ? MIPS_ELF_RELA_SIZE (dynobj)
   9190 			      : MIPS_ELF_REL_SIZE (dynobj));
   9191 
   9192       /* Make room for the .rela.plt.unloaded relocations.  */
   9193       if (htab->is_vxworks && !info->shared)
   9194 	htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
   9195 
   9196       /* All relocations against this symbol that could have been made
   9197 	 dynamic will now refer to the PLT entry instead.  */
   9198       hmips->possibly_dynamic_relocs = 0;
   9199 
   9200       return TRUE;
   9201     }
   9202 
   9203   /* If this is a weak symbol, and there is a real definition, the
   9204      processor independent code will have arranged for us to see the
   9205      real definition first, and we can just use the same value.  */
   9206   if (h->u.weakdef != NULL)
   9207     {
   9208       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   9209 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   9210       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   9211       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   9212       return TRUE;
   9213     }
   9214 
   9215   /* Otherwise, there is nothing further to do for symbols defined
   9216      in regular objects.  */
   9217   if (h->def_regular)
   9218     return TRUE;
   9219 
   9220   /* There's also nothing more to do if we'll convert all relocations
   9221      against this symbol into dynamic relocations.  */
   9222   if (!hmips->has_static_relocs)
   9223     return TRUE;
   9224 
   9225   /* We're now relying on copy relocations.  Complain if we have
   9226      some that we can't convert.  */
   9227   if (!htab->use_plts_and_copy_relocs || info->shared)
   9228     {
   9229       (*_bfd_error_handler) (_("non-dynamic relocations refer to "
   9230 			       "dynamic symbol %s"),
   9231 			     h->root.root.string);
   9232       bfd_set_error (bfd_error_bad_value);
   9233       return FALSE;
   9234     }
   9235 
   9236   /* We must allocate the symbol in our .dynbss section, which will
   9237      become part of the .bss section of the executable.  There will be
   9238      an entry for this symbol in the .dynsym section.  The dynamic
   9239      object will contain position independent code, so all references
   9240      from the dynamic object to this symbol will go through the global
   9241      offset table.  The dynamic linker will use the .dynsym entry to
   9242      determine the address it must put in the global offset table, so
   9243      both the dynamic object and the regular object will refer to the
   9244      same memory location for the variable.  */
   9245 
   9246   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
   9247     {
   9248       if (htab->is_vxworks)
   9249 	htab->srelbss->size += sizeof (Elf32_External_Rela);
   9250       else
   9251 	mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   9252       h->needs_copy = 1;
   9253     }
   9254 
   9255   /* All relocations against this symbol that could have been made
   9256      dynamic will now refer to the local copy instead.  */
   9257   hmips->possibly_dynamic_relocs = 0;
   9258 
   9259   return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
   9260 }
   9261 
   9262 /* This function is called after all the input files have been read,
   9264    and the input sections have been assigned to output sections.  We
   9265    check for any mips16 stub sections that we can discard.  */
   9266 
   9267 bfd_boolean
   9268 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
   9269 				    struct bfd_link_info *info)
   9270 {
   9271   asection *sect;
   9272   struct mips_elf_link_hash_table *htab;
   9273   struct mips_htab_traverse_info hti;
   9274 
   9275   htab = mips_elf_hash_table (info);
   9276   BFD_ASSERT (htab != NULL);
   9277 
   9278   /* The .reginfo section has a fixed size.  */
   9279   sect = bfd_get_section_by_name (output_bfd, ".reginfo");
   9280   if (sect != NULL)
   9281     bfd_set_section_size (output_bfd, sect, sizeof (Elf32_External_RegInfo));
   9282 
   9283   /* The .MIPS.abiflags section has a fixed size.  */
   9284   sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
   9285   if (sect != NULL)
   9286     bfd_set_section_size (output_bfd, sect, sizeof (Elf_External_ABIFlags_v0));
   9287 
   9288   hti.info = info;
   9289   hti.output_bfd = output_bfd;
   9290   hti.error = FALSE;
   9291   mips_elf_link_hash_traverse (mips_elf_hash_table (info),
   9292 			       mips_elf_check_symbols, &hti);
   9293   if (hti.error)
   9294     return FALSE;
   9295 
   9296   return TRUE;
   9297 }
   9298 
   9299 /* If the link uses a GOT, lay it out and work out its size.  */
   9300 
   9301 static bfd_boolean
   9302 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
   9303 {
   9304   bfd *dynobj;
   9305   asection *s;
   9306   struct mips_got_info *g;
   9307   bfd_size_type loadable_size = 0;
   9308   bfd_size_type page_gotno;
   9309   bfd *ibfd;
   9310   struct mips_elf_traverse_got_arg tga;
   9311   struct mips_elf_link_hash_table *htab;
   9312 
   9313   htab = mips_elf_hash_table (info);
   9314   BFD_ASSERT (htab != NULL);
   9315 
   9316   s = htab->sgot;
   9317   if (s == NULL)
   9318     return TRUE;
   9319 
   9320   dynobj = elf_hash_table (info)->dynobj;
   9321   g = htab->got_info;
   9322 
   9323   /* Allocate room for the reserved entries.  VxWorks always reserves
   9324      3 entries; other objects only reserve 2 entries.  */
   9325   BFD_ASSERT (g->assigned_low_gotno == 0);
   9326   if (htab->is_vxworks)
   9327     htab->reserved_gotno = 3;
   9328   else
   9329     htab->reserved_gotno = 2;
   9330   g->local_gotno += htab->reserved_gotno;
   9331   g->assigned_low_gotno = htab->reserved_gotno;
   9332 
   9333   /* Decide which symbols need to go in the global part of the GOT and
   9334      count the number of reloc-only GOT symbols.  */
   9335   mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
   9336 
   9337   if (!mips_elf_resolve_final_got_entries (info, g))
   9338     return FALSE;
   9339 
   9340   /* Calculate the total loadable size of the output.  That
   9341      will give us the maximum number of GOT_PAGE entries
   9342      required.  */
   9343   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   9344     {
   9345       asection *subsection;
   9346 
   9347       for (subsection = ibfd->sections;
   9348 	   subsection;
   9349 	   subsection = subsection->next)
   9350 	{
   9351 	  if ((subsection->flags & SEC_ALLOC) == 0)
   9352 	    continue;
   9353 	  loadable_size += ((subsection->size + 0xf)
   9354 			    &~ (bfd_size_type) 0xf);
   9355 	}
   9356     }
   9357 
   9358   if (htab->is_vxworks)
   9359     /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
   9360        relocations against local symbols evaluate to "G", and the EABI does
   9361        not include R_MIPS_GOT_PAGE.  */
   9362     page_gotno = 0;
   9363   else
   9364     /* Assume there are two loadable segments consisting of contiguous
   9365        sections.  Is 5 enough?  */
   9366     page_gotno = (loadable_size >> 16) + 5;
   9367 
   9368   /* Choose the smaller of the two page estimates; both are intended to be
   9369      conservative.  */
   9370   if (page_gotno > g->page_gotno)
   9371     page_gotno = g->page_gotno;
   9372 
   9373   g->local_gotno += page_gotno;
   9374   g->assigned_high_gotno = g->local_gotno - 1;
   9375 
   9376   s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9377   s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9378   s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9379 
   9380   /* VxWorks does not support multiple GOTs.  It initializes $gp to
   9381      __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
   9382      dynamic loader.  */
   9383   if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
   9384     {
   9385       if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
   9386 	return FALSE;
   9387     }
   9388   else
   9389     {
   9390       /* Record that all bfds use G.  This also has the effect of freeing
   9391 	 the per-bfd GOTs, which we no longer need.  */
   9392       for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   9393 	if (mips_elf_bfd_got (ibfd, FALSE))
   9394 	  mips_elf_replace_bfd_got (ibfd, g);
   9395       mips_elf_replace_bfd_got (output_bfd, g);
   9396 
   9397       /* Set up TLS entries.  */
   9398       g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
   9399       tga.info = info;
   9400       tga.g = g;
   9401       tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
   9402       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
   9403       if (!tga.g)
   9404 	return FALSE;
   9405       BFD_ASSERT (g->tls_assigned_gotno
   9406 		  == g->global_gotno + g->local_gotno + g->tls_gotno);
   9407 
   9408       /* Each VxWorks GOT entry needs an explicit relocation.  */
   9409       if (htab->is_vxworks && info->shared)
   9410 	g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
   9411 
   9412       /* Allocate room for the TLS relocations.  */
   9413       if (g->relocs)
   9414 	mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
   9415     }
   9416 
   9417   return TRUE;
   9418 }
   9419 
   9420 /* Estimate the size of the .MIPS.stubs section.  */
   9421 
   9422 static void
   9423 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
   9424 {
   9425   struct mips_elf_link_hash_table *htab;
   9426   bfd_size_type dynsymcount;
   9427 
   9428   htab = mips_elf_hash_table (info);
   9429   BFD_ASSERT (htab != NULL);
   9430 
   9431   if (htab->lazy_stub_count == 0)
   9432     return;
   9433 
   9434   /* IRIX rld assumes that a function stub isn't at the end of the .text
   9435      section, so add a dummy entry to the end.  */
   9436   htab->lazy_stub_count++;
   9437 
   9438   /* Get a worst-case estimate of the number of dynamic symbols needed.
   9439      At this point, dynsymcount does not account for section symbols
   9440      and count_section_dynsyms may overestimate the number that will
   9441      be needed.  */
   9442   dynsymcount = (elf_hash_table (info)->dynsymcount
   9443 		 + count_section_dynsyms (output_bfd, info));
   9444 
   9445   /* Determine the size of one stub entry.  There's no disadvantage
   9446      from using microMIPS code here, so for the sake of pure-microMIPS
   9447      binaries we prefer it whenever there's any microMIPS code in
   9448      output produced at all.  This has a benefit of stubs being
   9449      shorter by 4 bytes each too, unless in the insn32 mode.  */
   9450   if (!MICROMIPS_P (output_bfd))
   9451     htab->function_stub_size = (dynsymcount > 0x10000
   9452 				? MIPS_FUNCTION_STUB_BIG_SIZE
   9453 				: MIPS_FUNCTION_STUB_NORMAL_SIZE);
   9454   else if (htab->insn32)
   9455     htab->function_stub_size = (dynsymcount > 0x10000
   9456 				? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
   9457 				: MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
   9458   else
   9459     htab->function_stub_size = (dynsymcount > 0x10000
   9460 				? MICROMIPS_FUNCTION_STUB_BIG_SIZE
   9461 				: MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
   9462 
   9463   htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
   9464 }
   9465 
   9466 /* A mips_elf_link_hash_traverse callback for which DATA points to a
   9467    mips_htab_traverse_info.  If H needs a traditional MIPS lazy-binding
   9468    stub, allocate an entry in the stubs section.  */
   9469 
   9470 static bfd_boolean
   9471 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
   9472 {
   9473   struct mips_htab_traverse_info *hti = data;
   9474   struct mips_elf_link_hash_table *htab;
   9475   struct bfd_link_info *info;
   9476   bfd *output_bfd;
   9477 
   9478   info = hti->info;
   9479   output_bfd = hti->output_bfd;
   9480   htab = mips_elf_hash_table (info);
   9481   BFD_ASSERT (htab != NULL);
   9482 
   9483   if (h->needs_lazy_stub)
   9484     {
   9485       bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
   9486       unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   9487       bfd_vma isa_bit = micromips_p;
   9488 
   9489       BFD_ASSERT (htab->root.dynobj != NULL);
   9490       if (h->root.plt.plist == NULL)
   9491 	h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
   9492       if (h->root.plt.plist == NULL)
   9493 	{
   9494 	  hti->error = TRUE;
   9495 	  return FALSE;
   9496 	}
   9497       h->root.root.u.def.section = htab->sstubs;
   9498       h->root.root.u.def.value = htab->sstubs->size + isa_bit;
   9499       h->root.plt.plist->stub_offset = htab->sstubs->size;
   9500       h->root.other = other;
   9501       htab->sstubs->size += htab->function_stub_size;
   9502     }
   9503   return TRUE;
   9504 }
   9505 
   9506 /* Allocate offsets in the stubs section to each symbol that needs one.
   9507    Set the final size of the .MIPS.stub section.  */
   9508 
   9509 static bfd_boolean
   9510 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
   9511 {
   9512   bfd *output_bfd = info->output_bfd;
   9513   bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
   9514   unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   9515   bfd_vma isa_bit = micromips_p;
   9516   struct mips_elf_link_hash_table *htab;
   9517   struct mips_htab_traverse_info hti;
   9518   struct elf_link_hash_entry *h;
   9519   bfd *dynobj;
   9520 
   9521   htab = mips_elf_hash_table (info);
   9522   BFD_ASSERT (htab != NULL);
   9523 
   9524   if (htab->lazy_stub_count == 0)
   9525     return TRUE;
   9526 
   9527   htab->sstubs->size = 0;
   9528   hti.info = info;
   9529   hti.output_bfd = output_bfd;
   9530   hti.error = FALSE;
   9531   mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
   9532   if (hti.error)
   9533     return FALSE;
   9534   htab->sstubs->size += htab->function_stub_size;
   9535   BFD_ASSERT (htab->sstubs->size
   9536 	      == htab->lazy_stub_count * htab->function_stub_size);
   9537 
   9538   dynobj = elf_hash_table (info)->dynobj;
   9539   BFD_ASSERT (dynobj != NULL);
   9540   h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
   9541   if (h == NULL)
   9542     return FALSE;
   9543   h->root.u.def.value = isa_bit;
   9544   h->other = other;
   9545   h->type = STT_FUNC;
   9546 
   9547   return TRUE;
   9548 }
   9549 
   9550 /* A mips_elf_link_hash_traverse callback for which DATA points to a
   9551    bfd_link_info.  If H uses the address of a PLT entry as the value
   9552    of the symbol, then set the entry in the symbol table now.  Prefer
   9553    a standard MIPS PLT entry.  */
   9554 
   9555 static bfd_boolean
   9556 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
   9557 {
   9558   struct bfd_link_info *info = data;
   9559   bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
   9560   struct mips_elf_link_hash_table *htab;
   9561   unsigned int other;
   9562   bfd_vma isa_bit;
   9563   bfd_vma val;
   9564 
   9565   htab = mips_elf_hash_table (info);
   9566   BFD_ASSERT (htab != NULL);
   9567 
   9568   if (h->use_plt_entry)
   9569     {
   9570       BFD_ASSERT (h->root.plt.plist != NULL);
   9571       BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
   9572 		  || h->root.plt.plist->comp_offset != MINUS_ONE);
   9573 
   9574       val = htab->plt_header_size;
   9575       if (h->root.plt.plist->mips_offset != MINUS_ONE)
   9576 	{
   9577 	  isa_bit = 0;
   9578 	  val += h->root.plt.plist->mips_offset;
   9579 	  other = 0;
   9580 	}
   9581       else
   9582 	{
   9583 	  isa_bit = 1;
   9584 	  val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
   9585 	  other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
   9586 	}
   9587       val += isa_bit;
   9588       /* For VxWorks, point at the PLT load stub rather than the lazy
   9589          resolution stub; this stub will become the canonical function
   9590          address.  */
   9591       if (htab->is_vxworks)
   9592 	val += 8;
   9593 
   9594       h->root.root.u.def.section = htab->splt;
   9595       h->root.root.u.def.value = val;
   9596       h->root.other = other;
   9597     }
   9598 
   9599   return TRUE;
   9600 }
   9601 
   9602 /* Set the sizes of the dynamic sections.  */
   9603 
   9604 bfd_boolean
   9605 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
   9606 				     struct bfd_link_info *info)
   9607 {
   9608   bfd *dynobj;
   9609   asection *s, *sreldyn;
   9610   bfd_boolean reltext;
   9611   struct mips_elf_link_hash_table *htab;
   9612 
   9613   htab = mips_elf_hash_table (info);
   9614   BFD_ASSERT (htab != NULL);
   9615   dynobj = elf_hash_table (info)->dynobj;
   9616   BFD_ASSERT (dynobj != NULL);
   9617 
   9618   if (elf_hash_table (info)->dynamic_sections_created)
   9619     {
   9620       /* Set the contents of the .interp section to the interpreter.  */
   9621       if (info->executable)
   9622 	{
   9623 	  s = bfd_get_linker_section (dynobj, ".interp");
   9624 	  BFD_ASSERT (s != NULL);
   9625 	  s->size
   9626 	    = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
   9627 	  s->contents
   9628 	    = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
   9629 	}
   9630 
   9631       /* Figure out the size of the PLT header if we know that we
   9632          are using it.  For the sake of cache alignment always use
   9633          a standard header whenever any standard entries are present
   9634          even if microMIPS entries are present as well.  This also
   9635          lets the microMIPS header rely on the value of $v0 only set
   9636          by microMIPS entries, for a small size reduction.
   9637 
   9638          Set symbol table entry values for symbols that use the
   9639          address of their PLT entry now that we can calculate it.
   9640 
   9641          Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
   9642          haven't already in _bfd_elf_create_dynamic_sections.  */
   9643       if (htab->splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
   9644 	{
   9645 	  bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
   9646 				     && !htab->plt_mips_offset);
   9647 	  unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   9648 	  bfd_vma isa_bit = micromips_p;
   9649 	  struct elf_link_hash_entry *h;
   9650 	  bfd_vma size;
   9651 
   9652 	  BFD_ASSERT (htab->use_plts_and_copy_relocs);
   9653 	  BFD_ASSERT (htab->sgotplt->size == 0);
   9654 	  BFD_ASSERT (htab->splt->size == 0);
   9655 
   9656 	  if (htab->is_vxworks && info->shared)
   9657 	    size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
   9658 	  else if (htab->is_vxworks)
   9659 	    size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
   9660 	  else if (ABI_64_P (output_bfd))
   9661 	    size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
   9662 	  else if (ABI_N32_P (output_bfd))
   9663 	    size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
   9664 	  else if (!micromips_p)
   9665 	    size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
   9666 	  else if (htab->insn32)
   9667 	    size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
   9668 	  else
   9669 	    size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
   9670 
   9671 	  htab->plt_header_is_comp = micromips_p;
   9672 	  htab->plt_header_size = size;
   9673 	  htab->splt->size = (size
   9674 			      + htab->plt_mips_offset
   9675 			      + htab->plt_comp_offset);
   9676 	  htab->sgotplt->size = (htab->plt_got_index
   9677 				 * MIPS_ELF_GOT_SIZE (dynobj));
   9678 
   9679 	  mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
   9680 
   9681 	  if (htab->root.hplt == NULL)
   9682 	    {
   9683 	      h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
   9684 					       "_PROCEDURE_LINKAGE_TABLE_");
   9685 	      htab->root.hplt = h;
   9686 	      if (h == NULL)
   9687 		return FALSE;
   9688 	    }
   9689 
   9690 	  h = htab->root.hplt;
   9691 	  h->root.u.def.value = isa_bit;
   9692 	  h->other = other;
   9693 	  h->type = STT_FUNC;
   9694 	}
   9695     }
   9696 
   9697   /* Allocate space for global sym dynamic relocs.  */
   9698   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
   9699 
   9700   mips_elf_estimate_stub_size (output_bfd, info);
   9701 
   9702   if (!mips_elf_lay_out_got (output_bfd, info))
   9703     return FALSE;
   9704 
   9705   mips_elf_lay_out_lazy_stubs (info);
   9706 
   9707   /* The check_relocs and adjust_dynamic_symbol entry points have
   9708      determined the sizes of the various dynamic sections.  Allocate
   9709      memory for them.  */
   9710   reltext = FALSE;
   9711   for (s = dynobj->sections; s != NULL; s = s->next)
   9712     {
   9713       const char *name;
   9714 
   9715       /* It's OK to base decisions on the section name, because none
   9716 	 of the dynobj section names depend upon the input files.  */
   9717       name = bfd_get_section_name (dynobj, s);
   9718 
   9719       if ((s->flags & SEC_LINKER_CREATED) == 0)
   9720 	continue;
   9721 
   9722       if (CONST_STRNEQ (name, ".rel"))
   9723 	{
   9724 	  if (s->size != 0)
   9725 	    {
   9726 	      const char *outname;
   9727 	      asection *target;
   9728 
   9729 	      /* If this relocation section applies to a read only
   9730                  section, then we probably need a DT_TEXTREL entry.
   9731                  If the relocation section is .rel(a).dyn, we always
   9732                  assert a DT_TEXTREL entry rather than testing whether
   9733                  there exists a relocation to a read only section or
   9734                  not.  */
   9735 	      outname = bfd_get_section_name (output_bfd,
   9736 					      s->output_section);
   9737 	      target = bfd_get_section_by_name (output_bfd, outname + 4);
   9738 	      if ((target != NULL
   9739 		   && (target->flags & SEC_READONLY) != 0
   9740 		   && (target->flags & SEC_ALLOC) != 0)
   9741 		  || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
   9742 		reltext = TRUE;
   9743 
   9744 	      /* We use the reloc_count field as a counter if we need
   9745 		 to copy relocs into the output file.  */
   9746 	      if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
   9747 		s->reloc_count = 0;
   9748 
   9749 	      /* If combreloc is enabled, elf_link_sort_relocs() will
   9750 		 sort relocations, but in a different way than we do,
   9751 		 and before we're done creating relocations.  Also, it
   9752 		 will move them around between input sections'
   9753 		 relocation's contents, so our sorting would be
   9754 		 broken, so don't let it run.  */
   9755 	      info->combreloc = 0;
   9756 	    }
   9757 	}
   9758       else if (info->executable
   9759 	       && ! mips_elf_hash_table (info)->use_rld_obj_head
   9760 	       && CONST_STRNEQ (name, ".rld_map"))
   9761 	{
   9762 	  /* We add a room for __rld_map.  It will be filled in by the
   9763 	     rtld to contain a pointer to the _r_debug structure.  */
   9764 	  s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
   9765 	}
   9766       else if (SGI_COMPAT (output_bfd)
   9767 	       && CONST_STRNEQ (name, ".compact_rel"))
   9768 	s->size += mips_elf_hash_table (info)->compact_rel_size;
   9769       else if (s == htab->splt)
   9770 	{
   9771 	  /* If the last PLT entry has a branch delay slot, allocate
   9772 	     room for an extra nop to fill the delay slot.  This is
   9773 	     for CPUs without load interlocking.  */
   9774 	  if (! LOAD_INTERLOCKS_P (output_bfd)
   9775 	      && ! htab->is_vxworks && s->size > 0)
   9776 	    s->size += 4;
   9777 	}
   9778       else if (! CONST_STRNEQ (name, ".init")
   9779 	       && s != htab->sgot
   9780 	       && s != htab->sgotplt
   9781 	       && s != htab->sstubs
   9782 	       && s != htab->sdynbss)
   9783 	{
   9784 	  /* It's not one of our sections, so don't allocate space.  */
   9785 	  continue;
   9786 	}
   9787 
   9788       if (s->size == 0)
   9789 	{
   9790 	  s->flags |= SEC_EXCLUDE;
   9791 	  continue;
   9792 	}
   9793 
   9794       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   9795 	continue;
   9796 
   9797       /* Allocate memory for the section contents.  */
   9798       s->contents = bfd_zalloc (dynobj, s->size);
   9799       if (s->contents == NULL)
   9800 	{
   9801 	  bfd_set_error (bfd_error_no_memory);
   9802 	  return FALSE;
   9803 	}
   9804     }
   9805 
   9806   if (elf_hash_table (info)->dynamic_sections_created)
   9807     {
   9808       /* Add some entries to the .dynamic section.  We fill in the
   9809 	 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
   9810 	 must add the entries now so that we get the correct size for
   9811 	 the .dynamic section.  */
   9812 
   9813       /* SGI object has the equivalence of DT_DEBUG in the
   9814 	 DT_MIPS_RLD_MAP entry.  This must come first because glibc
   9815 	 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
   9816 	 may only look at the first one they see.  */
   9817       if (!info->shared
   9818 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
   9819 	return FALSE;
   9820 
   9821       if (info->executable
   9822 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP2, 0))
   9823 	return FALSE;
   9824 
   9825       /* The DT_DEBUG entry may be filled in by the dynamic linker and
   9826 	 used by the debugger.  */
   9827       if (info->executable
   9828 	  && !SGI_COMPAT (output_bfd)
   9829 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
   9830 	return FALSE;
   9831 
   9832       if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
   9833 	info->flags |= DF_TEXTREL;
   9834 
   9835       if ((info->flags & DF_TEXTREL) != 0)
   9836 	{
   9837 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
   9838 	    return FALSE;
   9839 
   9840 	  /* Clear the DF_TEXTREL flag.  It will be set again if we
   9841 	     write out an actual text relocation; we may not, because
   9842 	     at this point we do not know whether e.g. any .eh_frame
   9843 	     absolute relocations have been converted to PC-relative.  */
   9844 	  info->flags &= ~DF_TEXTREL;
   9845 	}
   9846 
   9847       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
   9848 	return FALSE;
   9849 
   9850       sreldyn = mips_elf_rel_dyn_section (info, FALSE);
   9851       if (htab->is_vxworks)
   9852 	{
   9853 	  /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
   9854 	     use any of the DT_MIPS_* tags.  */
   9855 	  if (sreldyn && sreldyn->size > 0)
   9856 	    {
   9857 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
   9858 		return FALSE;
   9859 
   9860 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
   9861 		return FALSE;
   9862 
   9863 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
   9864 		return FALSE;
   9865 	    }
   9866 	}
   9867       else
   9868 	{
   9869 	  if (sreldyn && sreldyn->size > 0)
   9870 	    {
   9871 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
   9872 		return FALSE;
   9873 
   9874 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
   9875 		return FALSE;
   9876 
   9877 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
   9878 		return FALSE;
   9879 	    }
   9880 
   9881 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
   9882 	    return FALSE;
   9883 
   9884 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
   9885 	    return FALSE;
   9886 
   9887 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
   9888 	    return FALSE;
   9889 
   9890 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
   9891 	    return FALSE;
   9892 
   9893 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
   9894 	    return FALSE;
   9895 
   9896 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
   9897 	    return FALSE;
   9898 
   9899 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
   9900 	    return FALSE;
   9901 
   9902 	  if (IRIX_COMPAT (dynobj) == ict_irix5
   9903 	      && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
   9904 	    return FALSE;
   9905 
   9906 	  if (IRIX_COMPAT (dynobj) == ict_irix6
   9907 	      && (bfd_get_section_by_name
   9908 		  (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
   9909 	      && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
   9910 	    return FALSE;
   9911 	}
   9912       if (htab->splt->size > 0)
   9913 	{
   9914 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
   9915 	    return FALSE;
   9916 
   9917 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
   9918 	    return FALSE;
   9919 
   9920 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
   9921 	    return FALSE;
   9922 
   9923 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
   9924 	    return FALSE;
   9925 	}
   9926       if (htab->is_vxworks
   9927 	  && !elf_vxworks_add_dynamic_entries (output_bfd, info))
   9928 	return FALSE;
   9929     }
   9930 
   9931   return TRUE;
   9932 }
   9933 
   9934 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
   9936    Adjust its R_ADDEND field so that it is correct for the output file.
   9937    LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
   9938    and sections respectively; both use symbol indexes.  */
   9939 
   9940 static void
   9941 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
   9942 			bfd *input_bfd, Elf_Internal_Sym *local_syms,
   9943 			asection **local_sections, Elf_Internal_Rela *rel)
   9944 {
   9945   unsigned int r_type, r_symndx;
   9946   Elf_Internal_Sym *sym;
   9947   asection *sec;
   9948 
   9949   if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
   9950     {
   9951       r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   9952       if (gprel16_reloc_p (r_type)
   9953 	  || r_type == R_MIPS_GPREL32
   9954 	  || literal_reloc_p (r_type))
   9955 	{
   9956 	  rel->r_addend += _bfd_get_gp_value (input_bfd);
   9957 	  rel->r_addend -= _bfd_get_gp_value (output_bfd);
   9958 	}
   9959 
   9960       r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
   9961       sym = local_syms + r_symndx;
   9962 
   9963       /* Adjust REL's addend to account for section merging.  */
   9964       if (!info->relocatable)
   9965 	{
   9966 	  sec = local_sections[r_symndx];
   9967 	  _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   9968 	}
   9969 
   9970       /* This would normally be done by the rela_normal code in elflink.c.  */
   9971       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   9972 	rel->r_addend += local_sections[r_symndx]->output_offset;
   9973     }
   9974 }
   9975 
   9976 /* Handle relocations against symbols from removed linkonce sections,
   9977    or sections discarded by a linker script.  We use this wrapper around
   9978    RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
   9979    on 64-bit ELF targets.  In this case for any relocation handled, which
   9980    always be the first in a triplet, the remaining two have to be processed
   9981    together with the first, even if they are R_MIPS_NONE.  It is the symbol
   9982    index referred by the first reloc that applies to all the three and the
   9983    remaining two never refer to an object symbol.  And it is the final
   9984    relocation (the last non-null one) that determines the output field of
   9985    the whole relocation so retrieve the corresponding howto structure for
   9986    the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
   9987 
   9988    Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
   9989    and therefore requires to be pasted in a loop.  It also defines a block
   9990    and does not protect any of its arguments, hence the extra brackets.  */
   9991 
   9992 static void
   9993 mips_reloc_against_discarded_section (bfd *output_bfd,
   9994 				      struct bfd_link_info *info,
   9995 				      bfd *input_bfd, asection *input_section,
   9996 				      Elf_Internal_Rela **rel,
   9997 				      const Elf_Internal_Rela **relend,
   9998 				      bfd_boolean rel_reloc,
   9999 				      reloc_howto_type *howto,
   10000 				      bfd_byte *contents)
   10001 {
   10002   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   10003   int count = bed->s->int_rels_per_ext_rel;
   10004   unsigned int r_type;
   10005   int i;
   10006 
   10007   for (i = count - 1; i > 0; i--)
   10008     {
   10009       r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
   10010       if (r_type != R_MIPS_NONE)
   10011 	{
   10012 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
   10013 	  break;
   10014 	}
   10015     }
   10016   do
   10017     {
   10018        RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   10019 					(*rel), count, (*relend),
   10020 					howto, i, contents);
   10021     }
   10022   while (0);
   10023 }
   10024 
   10025 /* Relocate a MIPS ELF section.  */
   10026 
   10027 bfd_boolean
   10028 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
   10029 				bfd *input_bfd, asection *input_section,
   10030 				bfd_byte *contents, Elf_Internal_Rela *relocs,
   10031 				Elf_Internal_Sym *local_syms,
   10032 				asection **local_sections)
   10033 {
   10034   Elf_Internal_Rela *rel;
   10035   const Elf_Internal_Rela *relend;
   10036   bfd_vma addend = 0;
   10037   bfd_boolean use_saved_addend_p = FALSE;
   10038   const struct elf_backend_data *bed;
   10039 
   10040   bed = get_elf_backend_data (output_bfd);
   10041   relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
   10042   for (rel = relocs; rel < relend; ++rel)
   10043     {
   10044       const char *name;
   10045       bfd_vma value = 0;
   10046       reloc_howto_type *howto;
   10047       bfd_boolean cross_mode_jump_p = FALSE;
   10048       /* TRUE if the relocation is a RELA relocation, rather than a
   10049          REL relocation.  */
   10050       bfd_boolean rela_relocation_p = TRUE;
   10051       unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   10052       const char *msg;
   10053       unsigned long r_symndx;
   10054       asection *sec;
   10055       Elf_Internal_Shdr *symtab_hdr;
   10056       struct elf_link_hash_entry *h;
   10057       bfd_boolean rel_reloc;
   10058 
   10059       rel_reloc = (NEWABI_P (input_bfd)
   10060 		   && mips_elf_rel_relocation_p (input_bfd, input_section,
   10061 						 relocs, rel));
   10062       /* Find the relocation howto for this relocation.  */
   10063       howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
   10064 
   10065       r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
   10066       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   10067       if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
   10068 	{
   10069 	  sec = local_sections[r_symndx];
   10070 	  h = NULL;
   10071 	}
   10072       else
   10073 	{
   10074 	  unsigned long extsymoff;
   10075 
   10076 	  extsymoff = 0;
   10077 	  if (!elf_bad_symtab (input_bfd))
   10078 	    extsymoff = symtab_hdr->sh_info;
   10079 	  h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
   10080 	  while (h->root.type == bfd_link_hash_indirect
   10081 		 || h->root.type == bfd_link_hash_warning)
   10082 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10083 
   10084 	  sec = NULL;
   10085 	  if (h->root.type == bfd_link_hash_defined
   10086 	      || h->root.type == bfd_link_hash_defweak)
   10087 	    sec = h->root.u.def.section;
   10088 	}
   10089 
   10090       if (sec != NULL && discarded_section (sec))
   10091 	{
   10092 	  mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
   10093 						input_section, &rel, &relend,
   10094 						rel_reloc, howto, contents);
   10095 	  continue;
   10096 	}
   10097 
   10098       if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
   10099 	{
   10100 	  /* Some 32-bit code uses R_MIPS_64.  In particular, people use
   10101 	     64-bit code, but make sure all their addresses are in the
   10102 	     lowermost or uppermost 32-bit section of the 64-bit address
   10103 	     space.  Thus, when they use an R_MIPS_64 they mean what is
   10104 	     usually meant by R_MIPS_32, with the exception that the
   10105 	     stored value is sign-extended to 64 bits.  */
   10106 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
   10107 
   10108 	  /* On big-endian systems, we need to lie about the position
   10109 	     of the reloc.  */
   10110 	  if (bfd_big_endian (input_bfd))
   10111 	    rel->r_offset += 4;
   10112 	}
   10113 
   10114       if (!use_saved_addend_p)
   10115 	{
   10116 	  /* If these relocations were originally of the REL variety,
   10117 	     we must pull the addend out of the field that will be
   10118 	     relocated.  Otherwise, we simply use the contents of the
   10119 	     RELA relocation.  */
   10120 	  if (mips_elf_rel_relocation_p (input_bfd, input_section,
   10121 					 relocs, rel))
   10122 	    {
   10123 	      rela_relocation_p = FALSE;
   10124 	      addend = mips_elf_read_rel_addend (input_bfd, rel,
   10125 						 howto, contents);
   10126 	      if (hi16_reloc_p (r_type)
   10127 		  || (got16_reloc_p (r_type)
   10128 		      && mips_elf_local_relocation_p (input_bfd, rel,
   10129 						      local_sections)))
   10130 		{
   10131 		  if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
   10132 						     contents, &addend))
   10133 		    {
   10134 		      if (h)
   10135 			name = h->root.root.string;
   10136 		      else
   10137 			name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   10138 						 local_syms + r_symndx,
   10139 						 sec);
   10140 		      (*_bfd_error_handler)
   10141 			(_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
   10142 			 input_bfd, input_section, name, howto->name,
   10143 			 rel->r_offset);
   10144 		    }
   10145 		}
   10146 	      else
   10147 		addend <<= howto->rightshift;
   10148 	    }
   10149 	  else
   10150 	    addend = rel->r_addend;
   10151 	  mips_elf_adjust_addend (output_bfd, info, input_bfd,
   10152 				  local_syms, local_sections, rel);
   10153 	}
   10154 
   10155       if (info->relocatable)
   10156 	{
   10157 	  if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
   10158 	      && bfd_big_endian (input_bfd))
   10159 	    rel->r_offset -= 4;
   10160 
   10161 	  if (!rela_relocation_p && rel->r_addend)
   10162 	    {
   10163 	      addend += rel->r_addend;
   10164 	      if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
   10165 		addend = mips_elf_high (addend);
   10166 	      else if (r_type == R_MIPS_HIGHER)
   10167 		addend = mips_elf_higher (addend);
   10168 	      else if (r_type == R_MIPS_HIGHEST)
   10169 		addend = mips_elf_highest (addend);
   10170 	      else
   10171 		addend >>= howto->rightshift;
   10172 
   10173 	      /* We use the source mask, rather than the destination
   10174 		 mask because the place to which we are writing will be
   10175 		 source of the addend in the final link.  */
   10176 	      addend &= howto->src_mask;
   10177 
   10178 	      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
   10179 		/* See the comment above about using R_MIPS_64 in the 32-bit
   10180 		   ABI.  Here, we need to update the addend.  It would be
   10181 		   possible to get away with just using the R_MIPS_32 reloc
   10182 		   but for endianness.  */
   10183 		{
   10184 		  bfd_vma sign_bits;
   10185 		  bfd_vma low_bits;
   10186 		  bfd_vma high_bits;
   10187 
   10188 		  if (addend & ((bfd_vma) 1 << 31))
   10189 #ifdef BFD64
   10190 		    sign_bits = ((bfd_vma) 1 << 32) - 1;
   10191 #else
   10192 		    sign_bits = -1;
   10193 #endif
   10194 		  else
   10195 		    sign_bits = 0;
   10196 
   10197 		  /* If we don't know that we have a 64-bit type,
   10198 		     do two separate stores.  */
   10199 		  if (bfd_big_endian (input_bfd))
   10200 		    {
   10201 		      /* Store the sign-bits (which are most significant)
   10202 			 first.  */
   10203 		      low_bits = sign_bits;
   10204 		      high_bits = addend;
   10205 		    }
   10206 		  else
   10207 		    {
   10208 		      low_bits = addend;
   10209 		      high_bits = sign_bits;
   10210 		    }
   10211 		  bfd_put_32 (input_bfd, low_bits,
   10212 			      contents + rel->r_offset);
   10213 		  bfd_put_32 (input_bfd, high_bits,
   10214 			      contents + rel->r_offset + 4);
   10215 		  continue;
   10216 		}
   10217 
   10218 	      if (! mips_elf_perform_relocation (info, howto, rel, addend,
   10219 						 input_bfd, input_section,
   10220 						 contents, FALSE))
   10221 		return FALSE;
   10222 	    }
   10223 
   10224 	  /* Go on to the next relocation.  */
   10225 	  continue;
   10226 	}
   10227 
   10228       /* In the N32 and 64-bit ABIs there may be multiple consecutive
   10229 	 relocations for the same offset.  In that case we are
   10230 	 supposed to treat the output of each relocation as the addend
   10231 	 for the next.  */
   10232       if (rel + 1 < relend
   10233 	  && rel->r_offset == rel[1].r_offset
   10234 	  && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
   10235 	use_saved_addend_p = TRUE;
   10236       else
   10237 	use_saved_addend_p = FALSE;
   10238 
   10239       /* Figure out what value we are supposed to relocate.  */
   10240       switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
   10241 					     input_section, info, rel,
   10242 					     addend, howto, local_syms,
   10243 					     local_sections, &value,
   10244 					     &name, &cross_mode_jump_p,
   10245 					     use_saved_addend_p))
   10246 	{
   10247 	case bfd_reloc_continue:
   10248 	  /* There's nothing to do.  */
   10249 	  continue;
   10250 
   10251 	case bfd_reloc_undefined:
   10252 	  /* mips_elf_calculate_relocation already called the
   10253 	     undefined_symbol callback.  There's no real point in
   10254 	     trying to perform the relocation at this point, so we
   10255 	     just skip ahead to the next relocation.  */
   10256 	  continue;
   10257 
   10258 	case bfd_reloc_notsupported:
   10259 	  msg = _("internal error: unsupported relocation error");
   10260 	  info->callbacks->warning
   10261 	    (info, msg, name, input_bfd, input_section, rel->r_offset);
   10262 	  return FALSE;
   10263 
   10264 	case bfd_reloc_overflow:
   10265 	  if (use_saved_addend_p)
   10266 	    /* Ignore overflow until we reach the last relocation for
   10267 	       a given location.  */
   10268 	    ;
   10269 	  else
   10270 	    {
   10271 	      struct mips_elf_link_hash_table *htab;
   10272 
   10273 	      htab = mips_elf_hash_table (info);
   10274 	      BFD_ASSERT (htab != NULL);
   10275 	      BFD_ASSERT (name != NULL);
   10276 	      if (!htab->small_data_overflow_reported
   10277 		  && (gprel16_reloc_p (howto->type)
   10278 		      || literal_reloc_p (howto->type)))
   10279 		{
   10280 		  msg = _("small-data section exceeds 64KB;"
   10281 			  " lower small-data size limit (see option -G)");
   10282 
   10283 		  htab->small_data_overflow_reported = TRUE;
   10284 		  (*info->callbacks->einfo) ("%P: %s\n", msg);
   10285 		}
   10286 	      if (! ((*info->callbacks->reloc_overflow)
   10287 		     (info, NULL, name, howto->name, (bfd_vma) 0,
   10288 		      input_bfd, input_section, rel->r_offset)))
   10289 		return FALSE;
   10290 	    }
   10291 	  break;
   10292 
   10293 	case bfd_reloc_ok:
   10294 	  break;
   10295 
   10296 	case bfd_reloc_outofrange:
   10297 	  if (jal_reloc_p (howto->type))
   10298 	    {
   10299 	      msg = _("JALX to a non-word-aligned address");
   10300 	      info->callbacks->warning
   10301 		(info, msg, name, input_bfd, input_section, rel->r_offset);
   10302 	      return FALSE;
   10303 	    }
   10304 	  if (aligned_pcrel_reloc_p (howto->type))
   10305 	    {
   10306 	      msg = _("PC-relative load from unaligned address");
   10307 	      info->callbacks->warning
   10308 		(info, msg, name, input_bfd, input_section, rel->r_offset);
   10309 	      return FALSE;
   10310 	    }
   10311 	  /* Fall through.  */
   10312 
   10313 	default:
   10314 	  abort ();
   10315 	  break;
   10316 	}
   10317 
   10318       /* If we've got another relocation for the address, keep going
   10319 	 until we reach the last one.  */
   10320       if (use_saved_addend_p)
   10321 	{
   10322 	  addend = value;
   10323 	  continue;
   10324 	}
   10325 
   10326       if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
   10327 	/* See the comment above about using R_MIPS_64 in the 32-bit
   10328 	   ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
   10329 	   that calculated the right value.  Now, however, we
   10330 	   sign-extend the 32-bit result to 64-bits, and store it as a
   10331 	   64-bit value.  We are especially generous here in that we
   10332 	   go to extreme lengths to support this usage on systems with
   10333 	   only a 32-bit VMA.  */
   10334 	{
   10335 	  bfd_vma sign_bits;
   10336 	  bfd_vma low_bits;
   10337 	  bfd_vma high_bits;
   10338 
   10339 	  if (value & ((bfd_vma) 1 << 31))
   10340 #ifdef BFD64
   10341 	    sign_bits = ((bfd_vma) 1 << 32) - 1;
   10342 #else
   10343 	    sign_bits = -1;
   10344 #endif
   10345 	  else
   10346 	    sign_bits = 0;
   10347 
   10348 	  /* If we don't know that we have a 64-bit type,
   10349 	     do two separate stores.  */
   10350 	  if (bfd_big_endian (input_bfd))
   10351 	    {
   10352 	      /* Undo what we did above.  */
   10353 	      rel->r_offset -= 4;
   10354 	      /* Store the sign-bits (which are most significant)
   10355 		 first.  */
   10356 	      low_bits = sign_bits;
   10357 	      high_bits = value;
   10358 	    }
   10359 	  else
   10360 	    {
   10361 	      low_bits = value;
   10362 	      high_bits = sign_bits;
   10363 	    }
   10364 	  bfd_put_32 (input_bfd, low_bits,
   10365 		      contents + rel->r_offset);
   10366 	  bfd_put_32 (input_bfd, high_bits,
   10367 		      contents + rel->r_offset + 4);
   10368 	  continue;
   10369 	}
   10370 
   10371       /* Actually perform the relocation.  */
   10372       if (! mips_elf_perform_relocation (info, howto, rel, value,
   10373 					 input_bfd, input_section,
   10374 					 contents, cross_mode_jump_p))
   10375 	return FALSE;
   10376     }
   10377 
   10378   return TRUE;
   10379 }
   10380 
   10381 /* A function that iterates over each entry in la25_stubs and fills
   10383    in the code for each one.  DATA points to a mips_htab_traverse_info.  */
   10384 
   10385 static int
   10386 mips_elf_create_la25_stub (void **slot, void *data)
   10387 {
   10388   struct mips_htab_traverse_info *hti;
   10389   struct mips_elf_link_hash_table *htab;
   10390   struct mips_elf_la25_stub *stub;
   10391   asection *s;
   10392   bfd_byte *loc;
   10393   bfd_vma offset, target, target_high, target_low;
   10394 
   10395   stub = (struct mips_elf_la25_stub *) *slot;
   10396   hti = (struct mips_htab_traverse_info *) data;
   10397   htab = mips_elf_hash_table (hti->info);
   10398   BFD_ASSERT (htab != NULL);
   10399 
   10400   /* Create the section contents, if we haven't already.  */
   10401   s = stub->stub_section;
   10402   loc = s->contents;
   10403   if (loc == NULL)
   10404     {
   10405       loc = bfd_malloc (s->size);
   10406       if (loc == NULL)
   10407 	{
   10408 	  hti->error = TRUE;
   10409 	  return FALSE;
   10410 	}
   10411       s->contents = loc;
   10412     }
   10413 
   10414   /* Work out where in the section this stub should go.  */
   10415   offset = stub->offset;
   10416 
   10417   /* Work out the target address.  */
   10418   target = mips_elf_get_la25_target (stub, &s);
   10419   target += s->output_section->vma + s->output_offset;
   10420 
   10421   target_high = ((target + 0x8000) >> 16) & 0xffff;
   10422   target_low = (target & 0xffff);
   10423 
   10424   if (stub->stub_section != htab->strampoline)
   10425     {
   10426       /* This is a simple LUI/ADDIU stub.  Zero out the beginning
   10427 	 of the section and write the two instructions at the end.  */
   10428       memset (loc, 0, offset);
   10429       loc += offset;
   10430       if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
   10431 	{
   10432 	  bfd_put_micromips_32 (hti->output_bfd,
   10433 				LA25_LUI_MICROMIPS (target_high),
   10434 				loc);
   10435 	  bfd_put_micromips_32 (hti->output_bfd,
   10436 				LA25_ADDIU_MICROMIPS (target_low),
   10437 				loc + 4);
   10438 	}
   10439       else
   10440 	{
   10441 	  bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
   10442 	  bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
   10443 	}
   10444     }
   10445   else
   10446     {
   10447       /* This is trampoline.  */
   10448       loc += offset;
   10449       if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
   10450 	{
   10451 	  bfd_put_micromips_32 (hti->output_bfd,
   10452 				LA25_LUI_MICROMIPS (target_high), loc);
   10453 	  bfd_put_micromips_32 (hti->output_bfd,
   10454 				LA25_J_MICROMIPS (target), loc + 4);
   10455 	  bfd_put_micromips_32 (hti->output_bfd,
   10456 				LA25_ADDIU_MICROMIPS (target_low), loc + 8);
   10457 	  bfd_put_32 (hti->output_bfd, 0, loc + 12);
   10458 	}
   10459       else
   10460 	{
   10461 	  bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
   10462 	  bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
   10463 	  bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
   10464 	  bfd_put_32 (hti->output_bfd, 0, loc + 12);
   10465 	}
   10466     }
   10467   return TRUE;
   10468 }
   10469 
   10470 /* If NAME is one of the special IRIX6 symbols defined by the linker,
   10471    adjust it appropriately now.  */
   10472 
   10473 static void
   10474 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
   10475 				      const char *name, Elf_Internal_Sym *sym)
   10476 {
   10477   /* The linker script takes care of providing names and values for
   10478      these, but we must place them into the right sections.  */
   10479   static const char* const text_section_symbols[] = {
   10480     "_ftext",
   10481     "_etext",
   10482     "__dso_displacement",
   10483     "__elf_header",
   10484     "__program_header_table",
   10485     NULL
   10486   };
   10487 
   10488   static const char* const data_section_symbols[] = {
   10489     "_fdata",
   10490     "_edata",
   10491     "_end",
   10492     "_fbss",
   10493     NULL
   10494   };
   10495 
   10496   const char* const *p;
   10497   int i;
   10498 
   10499   for (i = 0; i < 2; ++i)
   10500     for (p = (i == 0) ? text_section_symbols : data_section_symbols;
   10501 	 *p;
   10502 	 ++p)
   10503       if (strcmp (*p, name) == 0)
   10504 	{
   10505 	  /* All of these symbols are given type STT_SECTION by the
   10506 	     IRIX6 linker.  */
   10507 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   10508 	  sym->st_other = STO_PROTECTED;
   10509 
   10510 	  /* The IRIX linker puts these symbols in special sections.  */
   10511 	  if (i == 0)
   10512 	    sym->st_shndx = SHN_MIPS_TEXT;
   10513 	  else
   10514 	    sym->st_shndx = SHN_MIPS_DATA;
   10515 
   10516 	  break;
   10517 	}
   10518 }
   10519 
   10520 /* Finish up dynamic symbol handling.  We set the contents of various
   10521    dynamic sections here.  */
   10522 
   10523 bfd_boolean
   10524 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
   10525 				     struct bfd_link_info *info,
   10526 				     struct elf_link_hash_entry *h,
   10527 				     Elf_Internal_Sym *sym)
   10528 {
   10529   bfd *dynobj;
   10530   asection *sgot;
   10531   struct mips_got_info *g, *gg;
   10532   const char *name;
   10533   int idx;
   10534   struct mips_elf_link_hash_table *htab;
   10535   struct mips_elf_link_hash_entry *hmips;
   10536 
   10537   htab = mips_elf_hash_table (info);
   10538   BFD_ASSERT (htab != NULL);
   10539   dynobj = elf_hash_table (info)->dynobj;
   10540   hmips = (struct mips_elf_link_hash_entry *) h;
   10541 
   10542   BFD_ASSERT (!htab->is_vxworks);
   10543 
   10544   if (h->plt.plist != NULL
   10545       && (h->plt.plist->mips_offset != MINUS_ONE
   10546 	  || h->plt.plist->comp_offset != MINUS_ONE))
   10547     {
   10548       /* We've decided to create a PLT entry for this symbol.  */
   10549       bfd_byte *loc;
   10550       bfd_vma header_address, got_address;
   10551       bfd_vma got_address_high, got_address_low, load;
   10552       bfd_vma got_index;
   10553       bfd_vma isa_bit;
   10554 
   10555       got_index = h->plt.plist->gotplt_index;
   10556 
   10557       BFD_ASSERT (htab->use_plts_and_copy_relocs);
   10558       BFD_ASSERT (h->dynindx != -1);
   10559       BFD_ASSERT (htab->splt != NULL);
   10560       BFD_ASSERT (got_index != MINUS_ONE);
   10561       BFD_ASSERT (!h->def_regular);
   10562 
   10563       /* Calculate the address of the PLT header.  */
   10564       isa_bit = htab->plt_header_is_comp;
   10565       header_address = (htab->splt->output_section->vma
   10566 			+ htab->splt->output_offset + isa_bit);
   10567 
   10568       /* Calculate the address of the .got.plt entry.  */
   10569       got_address = (htab->sgotplt->output_section->vma
   10570 		     + htab->sgotplt->output_offset
   10571 		     + got_index * MIPS_ELF_GOT_SIZE (dynobj));
   10572 
   10573       got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
   10574       got_address_low = got_address & 0xffff;
   10575 
   10576       /* Initially point the .got.plt entry at the PLT header.  */
   10577       loc = (htab->sgotplt->contents + got_index * MIPS_ELF_GOT_SIZE (dynobj));
   10578       if (ABI_64_P (output_bfd))
   10579 	bfd_put_64 (output_bfd, header_address, loc);
   10580       else
   10581 	bfd_put_32 (output_bfd, header_address, loc);
   10582 
   10583       /* Now handle the PLT itself.  First the standard entry (the order
   10584          does not matter, we just have to pick one).  */
   10585       if (h->plt.plist->mips_offset != MINUS_ONE)
   10586 	{
   10587 	  const bfd_vma *plt_entry;
   10588 	  bfd_vma plt_offset;
   10589 
   10590 	  plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
   10591 
   10592 	  BFD_ASSERT (plt_offset <= htab->splt->size);
   10593 
   10594 	  /* Find out where the .plt entry should go.  */
   10595 	  loc = htab->splt->contents + plt_offset;
   10596 
   10597 	  /* Pick the load opcode.  */
   10598 	  load = MIPS_ELF_LOAD_WORD (output_bfd);
   10599 
   10600 	  /* Fill in the PLT entry itself.  */
   10601 
   10602 	  if (MIPSR6_P (output_bfd)
   10603 	      && mips_elf_hash_table (info)->compact_branches)
   10604 	    plt_entry = mipsr6_exec_plt_entry_compact;
   10605 	  else if (MIPSR6_P (output_bfd))
   10606 	    plt_entry = mipsr6_exec_plt_entry;
   10607 	  else
   10608 	    plt_entry = mips_exec_plt_entry;
   10609 	  bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
   10610 	  bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
   10611 		      loc + 4);
   10612 
   10613 	  if (! LOAD_INTERLOCKS_P (output_bfd) || MIPSR6_P (output_bfd))
   10614 	    {
   10615 	      bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
   10616 	      bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   10617 	    }
   10618 	  else
   10619 	    {
   10620 	      bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
   10621 	      bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
   10622 			  loc + 12);
   10623 	    }
   10624 	}
   10625 
   10626       /* Now the compressed entry.  They come after any standard ones.  */
   10627       if (h->plt.plist->comp_offset != MINUS_ONE)
   10628 	{
   10629 	  bfd_vma plt_offset;
   10630 
   10631 	  plt_offset = (htab->plt_header_size + htab->plt_mips_offset
   10632 			+ h->plt.plist->comp_offset);
   10633 
   10634 	  BFD_ASSERT (plt_offset <= htab->splt->size);
   10635 
   10636 	  /* Find out where the .plt entry should go.  */
   10637 	  loc = htab->splt->contents + plt_offset;
   10638 
   10639 	  /* Fill in the PLT entry itself.  */
   10640 	  if (!MICROMIPS_P (output_bfd))
   10641 	    {
   10642 	      const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
   10643 
   10644 	      bfd_put_16 (output_bfd, plt_entry[0], loc);
   10645 	      bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
   10646 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   10647 	      bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
   10648 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   10649 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   10650 	      bfd_put_32 (output_bfd, got_address, loc + 12);
   10651 	    }
   10652 	  else if (htab->insn32)
   10653 	    {
   10654 	      const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
   10655 
   10656 	      bfd_put_16 (output_bfd, plt_entry[0], loc);
   10657 	      bfd_put_16 (output_bfd, got_address_high, loc + 2);
   10658 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   10659 	      bfd_put_16 (output_bfd, got_address_low, loc + 6);
   10660 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   10661 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   10662 	      bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
   10663 	      bfd_put_16 (output_bfd, got_address_low, loc + 14);
   10664 	    }
   10665 	  else
   10666 	    {
   10667 	      const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
   10668 	      bfd_signed_vma gotpc_offset;
   10669 	      bfd_vma loc_address;
   10670 
   10671 	      BFD_ASSERT (got_address % 4 == 0);
   10672 
   10673 	      loc_address = (htab->splt->output_section->vma
   10674 			     + htab->splt->output_offset + plt_offset);
   10675 	      gotpc_offset = got_address - ((loc_address | 3) ^ 3);
   10676 
   10677 	      /* ADDIUPC has a span of +/-16MB, check we're in range.  */
   10678 	      if (gotpc_offset + 0x1000000 >= 0x2000000)
   10679 		{
   10680 		  (*_bfd_error_handler)
   10681 		    (_("%B: `%A' offset of %ld from `%A' "
   10682 		       "beyond the range of ADDIUPC"),
   10683 		     output_bfd,
   10684 		     htab->sgotplt->output_section,
   10685 		     htab->splt->output_section,
   10686 		     (long) gotpc_offset);
   10687 		  bfd_set_error (bfd_error_no_error);
   10688 		  return FALSE;
   10689 		}
   10690 	      bfd_put_16 (output_bfd,
   10691 			  plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
   10692 	      bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
   10693 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   10694 	      bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
   10695 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   10696 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   10697 	    }
   10698 	}
   10699 
   10700       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
   10701       mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
   10702 					  got_index - 2, h->dynindx,
   10703 					  R_MIPS_JUMP_SLOT, got_address);
   10704 
   10705       /* We distinguish between PLT entries and lazy-binding stubs by
   10706 	 giving the former an st_other value of STO_MIPS_PLT.  Set the
   10707 	 flag and leave the value if there are any relocations in the
   10708 	 binary where pointer equality matters.  */
   10709       sym->st_shndx = SHN_UNDEF;
   10710       if (h->pointer_equality_needed)
   10711 	sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
   10712       else
   10713 	{
   10714 	  sym->st_value = 0;
   10715 	  sym->st_other = 0;
   10716 	}
   10717     }
   10718 
   10719   if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
   10720     {
   10721       /* We've decided to create a lazy-binding stub.  */
   10722       bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
   10723       unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   10724       bfd_vma stub_size = htab->function_stub_size;
   10725       bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
   10726       bfd_vma isa_bit = micromips_p;
   10727       bfd_vma stub_big_size;
   10728 
   10729       if (!micromips_p)
   10730 	stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
   10731       else if (htab->insn32)
   10732 	stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
   10733       else
   10734 	stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
   10735 
   10736       /* This symbol has a stub.  Set it up.  */
   10737 
   10738       BFD_ASSERT (h->dynindx != -1);
   10739 
   10740       BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
   10741 
   10742       /* Values up to 2^31 - 1 are allowed.  Larger values would cause
   10743 	 sign extension at runtime in the stub, resulting in a negative
   10744 	 index value.  */
   10745       if (h->dynindx & ~0x7fffffff)
   10746 	return FALSE;
   10747 
   10748       /* Fill the stub.  */
   10749       if (micromips_p)
   10750 	{
   10751 	  idx = 0;
   10752 	  bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
   10753 				stub + idx);
   10754 	  idx += 4;
   10755 	  if (htab->insn32)
   10756 	    {
   10757 	      bfd_put_micromips_32 (output_bfd,
   10758 				    STUB_MOVE32_MICROMIPS (output_bfd),
   10759 				    stub + idx);
   10760 	      idx += 4;
   10761 	    }
   10762 	  else
   10763 	    {
   10764 	      bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
   10765 	      idx += 2;
   10766 	    }
   10767 	  if (stub_size == stub_big_size)
   10768 	    {
   10769 	      long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
   10770 
   10771 	      bfd_put_micromips_32 (output_bfd,
   10772 				    STUB_LUI_MICROMIPS (dynindx_hi),
   10773 				    stub + idx);
   10774 	      idx += 4;
   10775 	    }
   10776 	  if (htab->insn32)
   10777 	    {
   10778 	      bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
   10779 				    stub + idx);
   10780 	      idx += 4;
   10781 	    }
   10782 	  else
   10783 	    {
   10784 	      bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
   10785 	      idx += 2;
   10786 	    }
   10787 
   10788 	  /* If a large stub is not required and sign extension is not a
   10789 	     problem, then use legacy code in the stub.  */
   10790 	  if (stub_size == stub_big_size)
   10791 	    bfd_put_micromips_32 (output_bfd,
   10792 				  STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
   10793 				  stub + idx);
   10794 	  else if (h->dynindx & ~0x7fff)
   10795 	    bfd_put_micromips_32 (output_bfd,
   10796 				  STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
   10797 				  stub + idx);
   10798 	  else
   10799 	    bfd_put_micromips_32 (output_bfd,
   10800 				  STUB_LI16S_MICROMIPS (output_bfd,
   10801 							h->dynindx),
   10802 				  stub + idx);
   10803 	}
   10804       else
   10805 	{
   10806 	  idx = 0;
   10807 	  bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
   10808 	  idx += 4;
   10809 	  bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
   10810 	  idx += 4;
   10811 	  if (stub_size == stub_big_size)
   10812 	    {
   10813 	      bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
   10814 			  stub + idx);
   10815 	      idx += 4;
   10816 	    }
   10817 	  bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
   10818 	  idx += 4;
   10819 
   10820 	  /* If a large stub is not required and sign extension is not a
   10821 	     problem, then use legacy code in the stub.  */
   10822 	  if (stub_size == stub_big_size)
   10823 	    bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
   10824 			stub + idx);
   10825 	  else if (h->dynindx & ~0x7fff)
   10826 	    bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
   10827 			stub + idx);
   10828 	  else
   10829 	    bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
   10830 			stub + idx);
   10831 	}
   10832 
   10833       BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
   10834       memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
   10835 	      stub, stub_size);
   10836 
   10837       /* Mark the symbol as undefined.  stub_offset != -1 occurs
   10838 	 only for the referenced symbol.  */
   10839       sym->st_shndx = SHN_UNDEF;
   10840 
   10841       /* The run-time linker uses the st_value field of the symbol
   10842 	 to reset the global offset table entry for this external
   10843 	 to its stub address when unlinking a shared object.  */
   10844       sym->st_value = (htab->sstubs->output_section->vma
   10845 		       + htab->sstubs->output_offset
   10846 		       + h->plt.plist->stub_offset
   10847 		       + isa_bit);
   10848       sym->st_other = other;
   10849     }
   10850 
   10851   /* If we have a MIPS16 function with a stub, the dynamic symbol must
   10852      refer to the stub, since only the stub uses the standard calling
   10853      conventions.  */
   10854   if (h->dynindx != -1 && hmips->fn_stub != NULL)
   10855     {
   10856       BFD_ASSERT (hmips->need_fn_stub);
   10857       sym->st_value = (hmips->fn_stub->output_section->vma
   10858 		       + hmips->fn_stub->output_offset);
   10859       sym->st_size = hmips->fn_stub->size;
   10860       sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
   10861     }
   10862 
   10863   BFD_ASSERT (h->dynindx != -1
   10864 	      || h->forced_local);
   10865 
   10866   sgot = htab->sgot;
   10867   g = htab->got_info;
   10868   BFD_ASSERT (g != NULL);
   10869 
   10870   /* Run through the global symbol table, creating GOT entries for all
   10871      the symbols that need them.  */
   10872   if (hmips->global_got_area != GGA_NONE)
   10873     {
   10874       bfd_vma offset;
   10875       bfd_vma value;
   10876 
   10877       value = sym->st_value;
   10878       offset = mips_elf_primary_global_got_index (output_bfd, info, h);
   10879       MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
   10880     }
   10881 
   10882   if (hmips->global_got_area != GGA_NONE && g->next)
   10883     {
   10884       struct mips_got_entry e, *p;
   10885       bfd_vma entry;
   10886       bfd_vma offset;
   10887 
   10888       gg = g;
   10889 
   10890       e.abfd = output_bfd;
   10891       e.symndx = -1;
   10892       e.d.h = hmips;
   10893       e.tls_type = GOT_TLS_NONE;
   10894 
   10895       for (g = g->next; g->next != gg; g = g->next)
   10896 	{
   10897 	  if (g->got_entries
   10898 	      && (p = (struct mips_got_entry *) htab_find (g->got_entries,
   10899 							   &e)))
   10900 	    {
   10901 	      offset = p->gotidx;
   10902 	      BFD_ASSERT (offset > 0 && offset < htab->sgot->size);
   10903 	      if (info->shared
   10904 		  || (elf_hash_table (info)->dynamic_sections_created
   10905 		      && p->d.h != NULL
   10906 		      && p->d.h->root.def_dynamic
   10907 		      && !p->d.h->root.def_regular))
   10908 		{
   10909 		  /* Create an R_MIPS_REL32 relocation for this entry.  Due to
   10910 		     the various compatibility problems, it's easier to mock
   10911 		     up an R_MIPS_32 or R_MIPS_64 relocation and leave
   10912 		     mips_elf_create_dynamic_relocation to calculate the
   10913 		     appropriate addend.  */
   10914 		  Elf_Internal_Rela rel[3];
   10915 
   10916 		  memset (rel, 0, sizeof (rel));
   10917 		  if (ABI_64_P (output_bfd))
   10918 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
   10919 		  else
   10920 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
   10921 		  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
   10922 
   10923 		  entry = 0;
   10924 		  if (! (mips_elf_create_dynamic_relocation
   10925 			 (output_bfd, info, rel,
   10926 			  e.d.h, NULL, sym->st_value, &entry, sgot)))
   10927 		    return FALSE;
   10928 		}
   10929 	      else
   10930 		entry = sym->st_value;
   10931 	      MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
   10932 	    }
   10933 	}
   10934     }
   10935 
   10936   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
   10937   name = h->root.root.string;
   10938   if (h == elf_hash_table (info)->hdynamic
   10939       || h == elf_hash_table (info)->hgot)
   10940     sym->st_shndx = SHN_ABS;
   10941   else if (strcmp (name, "_DYNAMIC_LINK") == 0
   10942 	   || strcmp (name, "_DYNAMIC_LINKING") == 0)
   10943     {
   10944       sym->st_shndx = SHN_ABS;
   10945       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   10946       sym->st_value = 1;
   10947     }
   10948   else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
   10949     {
   10950       sym->st_shndx = SHN_ABS;
   10951       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   10952       sym->st_value = elf_gp (output_bfd);
   10953     }
   10954   else if (SGI_COMPAT (output_bfd))
   10955     {
   10956       if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
   10957 	  || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
   10958 	{
   10959 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   10960 	  sym->st_other = STO_PROTECTED;
   10961 	  sym->st_value = 0;
   10962 	  sym->st_shndx = SHN_MIPS_DATA;
   10963 	}
   10964       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
   10965 	{
   10966 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   10967 	  sym->st_other = STO_PROTECTED;
   10968 	  sym->st_value = mips_elf_hash_table (info)->procedure_count;
   10969 	  sym->st_shndx = SHN_ABS;
   10970 	}
   10971       else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
   10972 	{
   10973 	  if (h->type == STT_FUNC)
   10974 	    sym->st_shndx = SHN_MIPS_TEXT;
   10975 	  else if (h->type == STT_OBJECT)
   10976 	    sym->st_shndx = SHN_MIPS_DATA;
   10977 	}
   10978     }
   10979 
   10980   /* Emit a copy reloc, if needed.  */
   10981   if (h->needs_copy)
   10982     {
   10983       asection *s;
   10984       bfd_vma symval;
   10985 
   10986       BFD_ASSERT (h->dynindx != -1);
   10987       BFD_ASSERT (htab->use_plts_and_copy_relocs);
   10988 
   10989       s = mips_elf_rel_dyn_section (info, FALSE);
   10990       symval = (h->root.u.def.section->output_section->vma
   10991 		+ h->root.u.def.section->output_offset
   10992 		+ h->root.u.def.value);
   10993       mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
   10994 					  h->dynindx, R_MIPS_COPY, symval);
   10995     }
   10996 
   10997   /* Handle the IRIX6-specific symbols.  */
   10998   if (IRIX_COMPAT (output_bfd) == ict_irix6)
   10999     mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
   11000 
   11001   /* Keep dynamic compressed symbols odd.  This allows the dynamic linker
   11002      to treat compressed symbols like any other.  */
   11003   if (ELF_ST_IS_MIPS16 (sym->st_other))
   11004     {
   11005       BFD_ASSERT (sym->st_value & 1);
   11006       sym->st_other -= STO_MIPS16;
   11007     }
   11008   else if (ELF_ST_IS_MICROMIPS (sym->st_other))
   11009     {
   11010       BFD_ASSERT (sym->st_value & 1);
   11011       sym->st_other -= STO_MICROMIPS;
   11012     }
   11013 
   11014   return TRUE;
   11015 }
   11016 
   11017 /* Likewise, for VxWorks.  */
   11018 
   11019 bfd_boolean
   11020 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
   11021 					 struct bfd_link_info *info,
   11022 					 struct elf_link_hash_entry *h,
   11023 					 Elf_Internal_Sym *sym)
   11024 {
   11025   bfd *dynobj;
   11026   asection *sgot;
   11027   struct mips_got_info *g;
   11028   struct mips_elf_link_hash_table *htab;
   11029   struct mips_elf_link_hash_entry *hmips;
   11030 
   11031   htab = mips_elf_hash_table (info);
   11032   BFD_ASSERT (htab != NULL);
   11033   dynobj = elf_hash_table (info)->dynobj;
   11034   hmips = (struct mips_elf_link_hash_entry *) h;
   11035 
   11036   if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
   11037     {
   11038       bfd_byte *loc;
   11039       bfd_vma plt_address, got_address, got_offset, branch_offset;
   11040       Elf_Internal_Rela rel;
   11041       static const bfd_vma *plt_entry;
   11042       bfd_vma gotplt_index;
   11043       bfd_vma plt_offset;
   11044 
   11045       plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
   11046       gotplt_index = h->plt.plist->gotplt_index;
   11047 
   11048       BFD_ASSERT (h->dynindx != -1);
   11049       BFD_ASSERT (htab->splt != NULL);
   11050       BFD_ASSERT (gotplt_index != MINUS_ONE);
   11051       BFD_ASSERT (plt_offset <= htab->splt->size);
   11052 
   11053       /* Calculate the address of the .plt entry.  */
   11054       plt_address = (htab->splt->output_section->vma
   11055 		     + htab->splt->output_offset
   11056 		     + plt_offset);
   11057 
   11058       /* Calculate the address of the .got.plt entry.  */
   11059       got_address = (htab->sgotplt->output_section->vma
   11060 		     + htab->sgotplt->output_offset
   11061 		     + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
   11062 
   11063       /* Calculate the offset of the .got.plt entry from
   11064 	 _GLOBAL_OFFSET_TABLE_.  */
   11065       got_offset = mips_elf_gotplt_index (info, h);
   11066 
   11067       /* Calculate the offset for the branch at the start of the PLT
   11068 	 entry.  The branch jumps to the beginning of .plt.  */
   11069       branch_offset = -(plt_offset / 4 + 1) & 0xffff;
   11070 
   11071       /* Fill in the initial value of the .got.plt entry.  */
   11072       bfd_put_32 (output_bfd, plt_address,
   11073 		  (htab->sgotplt->contents
   11074 		   + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
   11075 
   11076       /* Find out where the .plt entry should go.  */
   11077       loc = htab->splt->contents + plt_offset;
   11078 
   11079       if (info->shared)
   11080 	{
   11081 	  plt_entry = mips_vxworks_shared_plt_entry;
   11082 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
   11083 	  bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
   11084 	}
   11085       else
   11086 	{
   11087 	  bfd_vma got_address_high, got_address_low;
   11088 
   11089 	  plt_entry = mips_vxworks_exec_plt_entry;
   11090 	  got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
   11091 	  got_address_low = got_address & 0xffff;
   11092 
   11093 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
   11094 	  bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
   11095 	  bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
   11096 	  bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
   11097 	  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11098 	  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11099 	  bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
   11100 	  bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
   11101 
   11102 	  loc = (htab->srelplt2->contents
   11103 		 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
   11104 
   11105 	  /* Emit a relocation for the .got.plt entry.  */
   11106 	  rel.r_offset = got_address;
   11107 	  rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
   11108 	  rel.r_addend = plt_offset;
   11109 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11110 
   11111 	  /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
   11112 	  loc += sizeof (Elf32_External_Rela);
   11113 	  rel.r_offset = plt_address + 8;
   11114 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11115 	  rel.r_addend = got_offset;
   11116 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11117 
   11118 	  /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
   11119 	  loc += sizeof (Elf32_External_Rela);
   11120 	  rel.r_offset += 4;
   11121 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11122 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11123 	}
   11124 
   11125       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
   11126       loc = (htab->srelplt->contents
   11127 	     + gotplt_index * sizeof (Elf32_External_Rela));
   11128       rel.r_offset = got_address;
   11129       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
   11130       rel.r_addend = 0;
   11131       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11132 
   11133       if (!h->def_regular)
   11134 	sym->st_shndx = SHN_UNDEF;
   11135     }
   11136 
   11137   BFD_ASSERT (h->dynindx != -1 || h->forced_local);
   11138 
   11139   sgot = htab->sgot;
   11140   g = htab->got_info;
   11141   BFD_ASSERT (g != NULL);
   11142 
   11143   /* See if this symbol has an entry in the GOT.  */
   11144   if (hmips->global_got_area != GGA_NONE)
   11145     {
   11146       bfd_vma offset;
   11147       Elf_Internal_Rela outrel;
   11148       bfd_byte *loc;
   11149       asection *s;
   11150 
   11151       /* Install the symbol value in the GOT.   */
   11152       offset = mips_elf_primary_global_got_index (output_bfd, info, h);
   11153       MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
   11154 
   11155       /* Add a dynamic relocation for it.  */
   11156       s = mips_elf_rel_dyn_section (info, FALSE);
   11157       loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
   11158       outrel.r_offset = (sgot->output_section->vma
   11159 			 + sgot->output_offset
   11160 			 + offset);
   11161       outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
   11162       outrel.r_addend = 0;
   11163       bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
   11164     }
   11165 
   11166   /* Emit a copy reloc, if needed.  */
   11167   if (h->needs_copy)
   11168     {
   11169       Elf_Internal_Rela rel;
   11170 
   11171       BFD_ASSERT (h->dynindx != -1);
   11172 
   11173       rel.r_offset = (h->root.u.def.section->output_section->vma
   11174 		      + h->root.u.def.section->output_offset
   11175 		      + h->root.u.def.value);
   11176       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
   11177       rel.r_addend = 0;
   11178       bfd_elf32_swap_reloca_out (output_bfd, &rel,
   11179 				 htab->srelbss->contents
   11180 				 + (htab->srelbss->reloc_count
   11181 				    * sizeof (Elf32_External_Rela)));
   11182       ++htab->srelbss->reloc_count;
   11183     }
   11184 
   11185   /* If this is a mips16/microMIPS symbol, force the value to be even.  */
   11186   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   11187     sym->st_value &= ~1;
   11188 
   11189   return TRUE;
   11190 }
   11191 
   11192 /* Write out a plt0 entry to the beginning of .plt.  */
   11193 
   11194 static bfd_boolean
   11195 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
   11196 {
   11197   bfd_byte *loc;
   11198   bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
   11199   static const bfd_vma *plt_entry;
   11200   struct mips_elf_link_hash_table *htab;
   11201 
   11202   htab = mips_elf_hash_table (info);
   11203   BFD_ASSERT (htab != NULL);
   11204 
   11205   if (ABI_64_P (output_bfd))
   11206     plt_entry = mips_n64_exec_plt0_entry;
   11207   else if (ABI_N32_P (output_bfd))
   11208     plt_entry = mips_n32_exec_plt0_entry;
   11209   else if (!htab->plt_header_is_comp)
   11210     plt_entry = mips_o32_exec_plt0_entry;
   11211   else if (htab->insn32)
   11212     plt_entry = micromips_insn32_o32_exec_plt0_entry;
   11213   else
   11214     plt_entry = micromips_o32_exec_plt0_entry;
   11215 
   11216   /* Calculate the value of .got.plt.  */
   11217   gotplt_value = (htab->sgotplt->output_section->vma
   11218 		  + htab->sgotplt->output_offset);
   11219   gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
   11220   gotplt_value_low = gotplt_value & 0xffff;
   11221 
   11222   /* The PLT sequence is not safe for N64 if .got.plt's address can
   11223      not be loaded in two instructions.  */
   11224   BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
   11225 	      || ~(gotplt_value | 0x7fffffff) == 0);
   11226 
   11227   /* Install the PLT header.  */
   11228   loc = htab->splt->contents;
   11229   if (plt_entry == micromips_o32_exec_plt0_entry)
   11230     {
   11231       bfd_vma gotpc_offset;
   11232       bfd_vma loc_address;
   11233       size_t i;
   11234 
   11235       BFD_ASSERT (gotplt_value % 4 == 0);
   11236 
   11237       loc_address = (htab->splt->output_section->vma
   11238 		     + htab->splt->output_offset);
   11239       gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
   11240 
   11241       /* ADDIUPC has a span of +/-16MB, check we're in range.  */
   11242       if (gotpc_offset + 0x1000000 >= 0x2000000)
   11243 	{
   11244 	  (*_bfd_error_handler)
   11245 	    (_("%B: `%A' offset of %ld from `%A' beyond the range of ADDIUPC"),
   11246 	     output_bfd,
   11247 	     htab->sgotplt->output_section,
   11248 	     htab->splt->output_section,
   11249 	     (long) gotpc_offset);
   11250 	  bfd_set_error (bfd_error_no_error);
   11251 	  return FALSE;
   11252 	}
   11253       bfd_put_16 (output_bfd,
   11254 		  plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
   11255       bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
   11256       for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
   11257 	bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
   11258     }
   11259   else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
   11260     {
   11261       size_t i;
   11262 
   11263       bfd_put_16 (output_bfd, plt_entry[0], loc);
   11264       bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
   11265       bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   11266       bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
   11267       bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   11268       bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
   11269       for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
   11270 	bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
   11271     }
   11272   else
   11273     {
   11274       bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
   11275       bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
   11276       bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
   11277       bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   11278       bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11279       bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11280       bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
   11281       bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
   11282     }
   11283 
   11284   return TRUE;
   11285 }
   11286 
   11287 /* Install the PLT header for a VxWorks executable and finalize the
   11288    contents of .rela.plt.unloaded.  */
   11289 
   11290 static void
   11291 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
   11292 {
   11293   Elf_Internal_Rela rela;
   11294   bfd_byte *loc;
   11295   bfd_vma got_value, got_value_high, got_value_low, plt_address;
   11296   static const bfd_vma *plt_entry;
   11297   struct mips_elf_link_hash_table *htab;
   11298 
   11299   htab = mips_elf_hash_table (info);
   11300   BFD_ASSERT (htab != NULL);
   11301 
   11302   plt_entry = mips_vxworks_exec_plt0_entry;
   11303 
   11304   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
   11305   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
   11306 	       + htab->root.hgot->root.u.def.section->output_offset
   11307 	       + htab->root.hgot->root.u.def.value);
   11308 
   11309   got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
   11310   got_value_low = got_value & 0xffff;
   11311 
   11312   /* Calculate the address of the PLT header.  */
   11313   plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
   11314 
   11315   /* Install the PLT header.  */
   11316   loc = htab->splt->contents;
   11317   bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
   11318   bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
   11319   bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
   11320   bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   11321   bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11322   bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11323 
   11324   /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
   11325   loc = htab->srelplt2->contents;
   11326   rela.r_offset = plt_address;
   11327   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11328   rela.r_addend = 0;
   11329   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   11330   loc += sizeof (Elf32_External_Rela);
   11331 
   11332   /* Output the relocation for the following addiu of
   11333      %lo(_GLOBAL_OFFSET_TABLE_).  */
   11334   rela.r_offset += 4;
   11335   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11336   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   11337   loc += sizeof (Elf32_External_Rela);
   11338 
   11339   /* Fix up the remaining relocations.  They may have the wrong
   11340      symbol index for _G_O_T_ or _P_L_T_ depending on the order
   11341      in which symbols were output.  */
   11342   while (loc < htab->srelplt2->contents + htab->srelplt2->size)
   11343     {
   11344       Elf_Internal_Rela rel;
   11345 
   11346       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11347       rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
   11348       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11349       loc += sizeof (Elf32_External_Rela);
   11350 
   11351       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11352       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11353       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11354       loc += sizeof (Elf32_External_Rela);
   11355 
   11356       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11357       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11358       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11359       loc += sizeof (Elf32_External_Rela);
   11360     }
   11361 }
   11362 
   11363 /* Install the PLT header for a VxWorks shared library.  */
   11364 
   11365 static void
   11366 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
   11367 {
   11368   unsigned int i;
   11369   struct mips_elf_link_hash_table *htab;
   11370 
   11371   htab = mips_elf_hash_table (info);
   11372   BFD_ASSERT (htab != NULL);
   11373 
   11374   /* We just need to copy the entry byte-by-byte.  */
   11375   for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
   11376     bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
   11377 		htab->splt->contents + i * 4);
   11378 }
   11379 
   11380 /* Finish up the dynamic sections.  */
   11381 
   11382 bfd_boolean
   11383 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
   11384 				       struct bfd_link_info *info)
   11385 {
   11386   bfd *dynobj;
   11387   asection *sdyn;
   11388   asection *sgot;
   11389   struct mips_got_info *gg, *g;
   11390   struct mips_elf_link_hash_table *htab;
   11391 
   11392   htab = mips_elf_hash_table (info);
   11393   BFD_ASSERT (htab != NULL);
   11394 
   11395   dynobj = elf_hash_table (info)->dynobj;
   11396 
   11397   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   11398 
   11399   sgot = htab->sgot;
   11400   gg = htab->got_info;
   11401 
   11402   if (elf_hash_table (info)->dynamic_sections_created)
   11403     {
   11404       bfd_byte *b;
   11405       int dyn_to_skip = 0, dyn_skipped = 0;
   11406 
   11407       BFD_ASSERT (sdyn != NULL);
   11408       BFD_ASSERT (gg != NULL);
   11409 
   11410       g = mips_elf_bfd_got (output_bfd, FALSE);
   11411       BFD_ASSERT (g != NULL);
   11412 
   11413       for (b = sdyn->contents;
   11414 	   b < sdyn->contents + sdyn->size;
   11415 	   b += MIPS_ELF_DYN_SIZE (dynobj))
   11416 	{
   11417 	  Elf_Internal_Dyn dyn;
   11418 	  const char *name;
   11419 	  size_t elemsize;
   11420 	  asection *s;
   11421 	  bfd_boolean swap_out_p;
   11422 
   11423 	  /* Read in the current dynamic entry.  */
   11424 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
   11425 
   11426 	  /* Assume that we're going to modify it and write it out.  */
   11427 	  swap_out_p = TRUE;
   11428 
   11429 	  switch (dyn.d_tag)
   11430 	    {
   11431 	    case DT_RELENT:
   11432 	      dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
   11433 	      break;
   11434 
   11435 	    case DT_RELAENT:
   11436 	      BFD_ASSERT (htab->is_vxworks);
   11437 	      dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
   11438 	      break;
   11439 
   11440 	    case DT_STRSZ:
   11441 	      /* Rewrite DT_STRSZ.  */
   11442 	      dyn.d_un.d_val =
   11443 		_bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   11444 	      break;
   11445 
   11446 	    case DT_PLTGOT:
   11447 	      s = htab->sgot;
   11448 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   11449 	      break;
   11450 
   11451 	    case DT_MIPS_PLTGOT:
   11452 	      s = htab->sgotplt;
   11453 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   11454 	      break;
   11455 
   11456 	    case DT_MIPS_RLD_VERSION:
   11457 	      dyn.d_un.d_val = 1; /* XXX */
   11458 	      break;
   11459 
   11460 	    case DT_MIPS_FLAGS:
   11461 	      dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
   11462 	      break;
   11463 
   11464 	    case DT_MIPS_TIME_STAMP:
   11465 	      {
   11466 		time_t t;
   11467 		time (&t);
   11468 		dyn.d_un.d_val = t;
   11469 	      }
   11470 	      break;
   11471 
   11472 	    case DT_MIPS_ICHECKSUM:
   11473 	      /* XXX FIXME: */
   11474 	      swap_out_p = FALSE;
   11475 	      break;
   11476 
   11477 	    case DT_MIPS_IVERSION:
   11478 	      /* XXX FIXME: */
   11479 	      swap_out_p = FALSE;
   11480 	      break;
   11481 
   11482 	    case DT_MIPS_BASE_ADDRESS:
   11483 	      s = output_bfd->sections;
   11484 	      BFD_ASSERT (s != NULL);
   11485 	      dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
   11486 	      break;
   11487 
   11488 	    case DT_MIPS_LOCAL_GOTNO:
   11489 	      dyn.d_un.d_val = g->local_gotno;
   11490 	      break;
   11491 
   11492 	    case DT_MIPS_UNREFEXTNO:
   11493 	      /* The index into the dynamic symbol table which is the
   11494 		 entry of the first external symbol that is not
   11495 		 referenced within the same object.  */
   11496 	      dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
   11497 	      break;
   11498 
   11499 	    case DT_MIPS_GOTSYM:
   11500 	      if (htab->global_gotsym)
   11501 		{
   11502 		  dyn.d_un.d_val = htab->global_gotsym->dynindx;
   11503 		  break;
   11504 		}
   11505 	      /* In case if we don't have global got symbols we default
   11506 		 to setting DT_MIPS_GOTSYM to the same value as
   11507 		 DT_MIPS_SYMTABNO, so we just fall through.  */
   11508 
   11509 	    case DT_MIPS_SYMTABNO:
   11510 	      name = ".dynsym";
   11511 	      elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
   11512 	      s = bfd_get_section_by_name (output_bfd, name);
   11513 
   11514 	      if (s != NULL)
   11515 		dyn.d_un.d_val = s->size / elemsize;
   11516 	      else
   11517 		dyn.d_un.d_val = 0;
   11518 	      break;
   11519 
   11520 	    case DT_MIPS_HIPAGENO:
   11521 	      dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
   11522 	      break;
   11523 
   11524 	    case DT_MIPS_RLD_MAP:
   11525 	      {
   11526 		struct elf_link_hash_entry *h;
   11527 		h = mips_elf_hash_table (info)->rld_symbol;
   11528 		if (!h)
   11529 		  {
   11530 		    dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   11531 		    swap_out_p = FALSE;
   11532 		    break;
   11533 		  }
   11534 		s = h->root.u.def.section;
   11535 		dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
   11536 				  + h->root.u.def.value);
   11537 	      }
   11538 	      break;
   11539 
   11540 	    case DT_MIPS_RLD_MAP2:
   11541 	      {
   11542 		struct elf_link_hash_entry *h;
   11543 		bfd_vma dt_addr, rld_addr;
   11544 		h = mips_elf_hash_table (info)->rld_symbol;
   11545 		if (!h)
   11546 		  {
   11547 		    dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   11548 		    swap_out_p = FALSE;
   11549 		    break;
   11550 		  }
   11551 		s = h->root.u.def.section;
   11552 
   11553 		dt_addr = (sdyn->output_section->vma + sdyn->output_offset
   11554 			   + (b - sdyn->contents));
   11555 		rld_addr = (s->output_section->vma + s->output_offset
   11556 			    + h->root.u.def.value);
   11557 		dyn.d_un.d_ptr = rld_addr - dt_addr;
   11558 	      }
   11559 	      break;
   11560 
   11561 	    case DT_MIPS_OPTIONS:
   11562 	      s = (bfd_get_section_by_name
   11563 		   (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
   11564 	      dyn.d_un.d_ptr = s->vma;
   11565 	      break;
   11566 
   11567 	    case DT_RELASZ:
   11568 	      BFD_ASSERT (htab->is_vxworks);
   11569 	      /* The count does not include the JUMP_SLOT relocations.  */
   11570 	      if (htab->srelplt)
   11571 		dyn.d_un.d_val -= htab->srelplt->size;
   11572 	      break;
   11573 
   11574 	    case DT_PLTREL:
   11575 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   11576 	      if (htab->is_vxworks)
   11577 		dyn.d_un.d_val = DT_RELA;
   11578 	      else
   11579 		dyn.d_un.d_val = DT_REL;
   11580 	      break;
   11581 
   11582 	    case DT_PLTRELSZ:
   11583 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   11584 	      dyn.d_un.d_val = htab->srelplt->size;
   11585 	      break;
   11586 
   11587 	    case DT_JMPREL:
   11588 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   11589 	      dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
   11590 				+ htab->srelplt->output_offset);
   11591 	      break;
   11592 
   11593 	    case DT_TEXTREL:
   11594 	      /* If we didn't need any text relocations after all, delete
   11595 		 the dynamic tag.  */
   11596 	      if (!(info->flags & DF_TEXTREL))
   11597 		{
   11598 		  dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   11599 		  swap_out_p = FALSE;
   11600 		}
   11601 	      break;
   11602 
   11603 	    case DT_FLAGS:
   11604 	      /* If we didn't need any text relocations after all, clear
   11605 		 DF_TEXTREL from DT_FLAGS.  */
   11606 	      if (!(info->flags & DF_TEXTREL))
   11607 		dyn.d_un.d_val &= ~DF_TEXTREL;
   11608 	      else
   11609 		swap_out_p = FALSE;
   11610 	      break;
   11611 
   11612 	    default:
   11613 	      swap_out_p = FALSE;
   11614 	      if (htab->is_vxworks
   11615 		  && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
   11616 		swap_out_p = TRUE;
   11617 	      break;
   11618 	    }
   11619 
   11620 	  if (swap_out_p || dyn_skipped)
   11621 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
   11622 	      (dynobj, &dyn, b - dyn_skipped);
   11623 
   11624 	  if (dyn_to_skip)
   11625 	    {
   11626 	      dyn_skipped += dyn_to_skip;
   11627 	      dyn_to_skip = 0;
   11628 	    }
   11629 	}
   11630 
   11631       /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
   11632       if (dyn_skipped > 0)
   11633 	memset (b - dyn_skipped, 0, dyn_skipped);
   11634     }
   11635 
   11636   if (sgot != NULL && sgot->size > 0
   11637       && !bfd_is_abs_section (sgot->output_section))
   11638     {
   11639       if (htab->is_vxworks)
   11640 	{
   11641 	  /* The first entry of the global offset table points to the
   11642 	     ".dynamic" section.  The second is initialized by the
   11643 	     loader and contains the shared library identifier.
   11644 	     The third is also initialized by the loader and points
   11645 	     to the lazy resolution stub.  */
   11646 	  MIPS_ELF_PUT_WORD (output_bfd,
   11647 			     sdyn->output_offset + sdyn->output_section->vma,
   11648 			     sgot->contents);
   11649 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
   11650 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
   11651 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
   11652 			     sgot->contents
   11653 			     + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
   11654 	}
   11655       else
   11656 	{
   11657 	  /* The first entry of the global offset table will be filled at
   11658 	     runtime. The second entry will be used by some runtime loaders.
   11659 	     This isn't the case of IRIX rld.  */
   11660 	  MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
   11661 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
   11662 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
   11663 	}
   11664 
   11665       elf_section_data (sgot->output_section)->this_hdr.sh_entsize
   11666 	 = MIPS_ELF_GOT_SIZE (output_bfd);
   11667     }
   11668 
   11669   /* Generate dynamic relocations for the non-primary gots.  */
   11670   if (gg != NULL && gg->next)
   11671     {
   11672       Elf_Internal_Rela rel[3];
   11673       bfd_vma addend = 0;
   11674 
   11675       memset (rel, 0, sizeof (rel));
   11676       rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
   11677 
   11678       for (g = gg->next; g->next != gg; g = g->next)
   11679 	{
   11680 	  bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
   11681 	    + g->next->tls_gotno;
   11682 
   11683 	  MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
   11684 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
   11685 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
   11686 			     sgot->contents
   11687 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
   11688 
   11689 	  if (! info->shared)
   11690 	    continue;
   11691 
   11692 	  for (; got_index < g->local_gotno; got_index++)
   11693 	    {
   11694 	      if (got_index >= g->assigned_low_gotno
   11695 		  && got_index <= g->assigned_high_gotno)
   11696 		continue;
   11697 
   11698 	      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
   11699 		= got_index * MIPS_ELF_GOT_SIZE (output_bfd);
   11700 	      if (!(mips_elf_create_dynamic_relocation
   11701 		    (output_bfd, info, rel, NULL,
   11702 		     bfd_abs_section_ptr,
   11703 		     0, &addend, sgot)))
   11704 		return FALSE;
   11705 	      BFD_ASSERT (addend == 0);
   11706 	    }
   11707 	}
   11708     }
   11709 
   11710   /* The generation of dynamic relocations for the non-primary gots
   11711      adds more dynamic relocations.  We cannot count them until
   11712      here.  */
   11713 
   11714   if (elf_hash_table (info)->dynamic_sections_created)
   11715     {
   11716       bfd_byte *b;
   11717       bfd_boolean swap_out_p;
   11718 
   11719       BFD_ASSERT (sdyn != NULL);
   11720 
   11721       for (b = sdyn->contents;
   11722 	   b < sdyn->contents + sdyn->size;
   11723 	   b += MIPS_ELF_DYN_SIZE (dynobj))
   11724 	{
   11725 	  Elf_Internal_Dyn dyn;
   11726 	  asection *s;
   11727 
   11728 	  /* Read in the current dynamic entry.  */
   11729 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
   11730 
   11731 	  /* Assume that we're going to modify it and write it out.  */
   11732 	  swap_out_p = TRUE;
   11733 
   11734 	  switch (dyn.d_tag)
   11735 	    {
   11736 	    case DT_RELSZ:
   11737 	      /* Reduce DT_RELSZ to account for any relocations we
   11738 		 decided not to make.  This is for the n64 irix rld,
   11739 		 which doesn't seem to apply any relocations if there
   11740 		 are trailing null entries.  */
   11741 	      s = mips_elf_rel_dyn_section (info, FALSE);
   11742 	      dyn.d_un.d_val = (s->reloc_count
   11743 				* (ABI_64_P (output_bfd)
   11744 				   ? sizeof (Elf64_Mips_External_Rel)
   11745 				   : sizeof (Elf32_External_Rel)));
   11746 	      /* Adjust the section size too.  Tools like the prelinker
   11747 		 can reasonably expect the values to the same.  */
   11748 	      elf_section_data (s->output_section)->this_hdr.sh_size
   11749 		= dyn.d_un.d_val;
   11750 	      break;
   11751 
   11752 	    default:
   11753 	      swap_out_p = FALSE;
   11754 	      break;
   11755 	    }
   11756 
   11757 	  if (swap_out_p)
   11758 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
   11759 	      (dynobj, &dyn, b);
   11760 	}
   11761     }
   11762 
   11763   {
   11764     asection *s;
   11765     Elf32_compact_rel cpt;
   11766 
   11767     if (SGI_COMPAT (output_bfd))
   11768       {
   11769 	/* Write .compact_rel section out.  */
   11770 	s = bfd_get_linker_section (dynobj, ".compact_rel");
   11771 	if (s != NULL)
   11772 	  {
   11773 	    cpt.id1 = 1;
   11774 	    cpt.num = s->reloc_count;
   11775 	    cpt.id2 = 2;
   11776 	    cpt.offset = (s->output_section->filepos
   11777 			  + sizeof (Elf32_External_compact_rel));
   11778 	    cpt.reserved0 = 0;
   11779 	    cpt.reserved1 = 0;
   11780 	    bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
   11781 					    ((Elf32_External_compact_rel *)
   11782 					     s->contents));
   11783 
   11784 	    /* Clean up a dummy stub function entry in .text.  */
   11785 	    if (htab->sstubs != NULL)
   11786 	      {
   11787 		file_ptr dummy_offset;
   11788 
   11789 		BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
   11790 		dummy_offset = htab->sstubs->size - htab->function_stub_size;
   11791 		memset (htab->sstubs->contents + dummy_offset, 0,
   11792 			htab->function_stub_size);
   11793 	      }
   11794 	  }
   11795       }
   11796 
   11797     /* The psABI says that the dynamic relocations must be sorted in
   11798        increasing order of r_symndx.  The VxWorks EABI doesn't require
   11799        this, and because the code below handles REL rather than RELA
   11800        relocations, using it for VxWorks would be outright harmful.  */
   11801     if (!htab->is_vxworks)
   11802       {
   11803 	s = mips_elf_rel_dyn_section (info, FALSE);
   11804 	if (s != NULL
   11805 	    && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
   11806 	  {
   11807 	    reldyn_sorting_bfd = output_bfd;
   11808 
   11809 	    if (ABI_64_P (output_bfd))
   11810 	      qsort ((Elf64_External_Rel *) s->contents + 1,
   11811 		     s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
   11812 		     sort_dynamic_relocs_64);
   11813 	    else
   11814 	      qsort ((Elf32_External_Rel *) s->contents + 1,
   11815 		     s->reloc_count - 1, sizeof (Elf32_External_Rel),
   11816 		     sort_dynamic_relocs);
   11817 	  }
   11818       }
   11819   }
   11820 
   11821   if (htab->splt && htab->splt->size > 0)
   11822     {
   11823       if (htab->is_vxworks)
   11824 	{
   11825 	  if (info->shared)
   11826 	    mips_vxworks_finish_shared_plt (output_bfd, info);
   11827 	  else
   11828 	    mips_vxworks_finish_exec_plt (output_bfd, info);
   11829 	}
   11830       else
   11831 	{
   11832 	  BFD_ASSERT (!info->shared);
   11833 	  if (!mips_finish_exec_plt (output_bfd, info))
   11834 	    return FALSE;
   11835 	}
   11836     }
   11837   return TRUE;
   11838 }
   11839 
   11840 
   11841 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
   11842 
   11843 static void
   11844 mips_set_isa_flags (bfd *abfd)
   11845 {
   11846   flagword val;
   11847 
   11848   switch (bfd_get_mach (abfd))
   11849     {
   11850     default:
   11851     case bfd_mach_mips3000:
   11852       val = E_MIPS_ARCH_1;
   11853       break;
   11854 
   11855     case bfd_mach_mips3900:
   11856       val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
   11857       break;
   11858 
   11859     case bfd_mach_mips6000:
   11860       val = E_MIPS_ARCH_2;
   11861       break;
   11862 
   11863     case bfd_mach_mips4000:
   11864     case bfd_mach_mips4300:
   11865     case bfd_mach_mips4400:
   11866     case bfd_mach_mips4600:
   11867       val = E_MIPS_ARCH_3;
   11868       break;
   11869 
   11870     case bfd_mach_mips4010:
   11871       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
   11872       break;
   11873 
   11874     case bfd_mach_mips4100:
   11875       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
   11876       break;
   11877 
   11878     case bfd_mach_mips4111:
   11879       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
   11880       break;
   11881 
   11882     case bfd_mach_mips4120:
   11883       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
   11884       break;
   11885 
   11886     case bfd_mach_mips4650:
   11887       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
   11888       break;
   11889 
   11890     case bfd_mach_mips5400:
   11891       val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
   11892       break;
   11893 
   11894     case bfd_mach_mips5500:
   11895       val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
   11896       break;
   11897 
   11898     case bfd_mach_mips5900:
   11899       val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
   11900       break;
   11901 
   11902     case bfd_mach_mips9000:
   11903       val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
   11904       break;
   11905 
   11906     case bfd_mach_mips5000:
   11907     case bfd_mach_mips7000:
   11908     case bfd_mach_mips8000:
   11909     case bfd_mach_mips10000:
   11910     case bfd_mach_mips12000:
   11911     case bfd_mach_mips14000:
   11912     case bfd_mach_mips16000:
   11913       val = E_MIPS_ARCH_4;
   11914       break;
   11915 
   11916     case bfd_mach_mips5:
   11917       val = E_MIPS_ARCH_5;
   11918       break;
   11919 
   11920     case bfd_mach_mips_loongson_2e:
   11921       val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
   11922       break;
   11923 
   11924     case bfd_mach_mips_loongson_2f:
   11925       val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
   11926       break;
   11927 
   11928     case bfd_mach_mips_sb1:
   11929       val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
   11930       break;
   11931 
   11932     case bfd_mach_mips_loongson_3a:
   11933       val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_LS3A;
   11934       break;
   11935 
   11936     case bfd_mach_mips_octeon:
   11937     case bfd_mach_mips_octeonp:
   11938       val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
   11939       break;
   11940 
   11941     case bfd_mach_mips_octeon3:
   11942       val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
   11943       break;
   11944 
   11945     case bfd_mach_mips_xlr:
   11946       val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
   11947       break;
   11948 
   11949     case bfd_mach_mips_octeon2:
   11950       val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
   11951       break;
   11952 
   11953     case bfd_mach_mipsisa32:
   11954       val = E_MIPS_ARCH_32;
   11955       break;
   11956 
   11957     case bfd_mach_mipsisa64:
   11958       val = E_MIPS_ARCH_64;
   11959       break;
   11960 
   11961     case bfd_mach_mipsisa32r2:
   11962     case bfd_mach_mipsisa32r3:
   11963     case bfd_mach_mipsisa32r5:
   11964       val = E_MIPS_ARCH_32R2;
   11965       break;
   11966 
   11967     case bfd_mach_mipsisa64r2:
   11968     case bfd_mach_mipsisa64r3:
   11969     case bfd_mach_mipsisa64r5:
   11970       val = E_MIPS_ARCH_64R2;
   11971       break;
   11972 
   11973     case bfd_mach_mipsisa32r6:
   11974       val = E_MIPS_ARCH_32R6;
   11975       break;
   11976 
   11977     case bfd_mach_mipsisa64r6:
   11978       val = E_MIPS_ARCH_64R6;
   11979       break;
   11980     }
   11981   elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
   11982   elf_elfheader (abfd)->e_flags |= val;
   11983 
   11984 }
   11985 
   11986 
   11987 /* The final processing done just before writing out a MIPS ELF object
   11988    file.  This gets the MIPS architecture right based on the machine
   11989    number.  This is used by both the 32-bit and the 64-bit ABI.  */
   11990 
   11991 void
   11992 _bfd_mips_elf_final_write_processing (bfd *abfd,
   11993 				      bfd_boolean linker ATTRIBUTE_UNUSED)
   11994 {
   11995   unsigned int i;
   11996   Elf_Internal_Shdr **hdrpp;
   11997   const char *name;
   11998   asection *sec;
   11999 
   12000   /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
   12001      is nonzero.  This is for compatibility with old objects, which used
   12002      a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
   12003   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
   12004     mips_set_isa_flags (abfd);
   12005 
   12006   /* Set the sh_info field for .gptab sections and other appropriate
   12007      info for each special section.  */
   12008   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
   12009        i < elf_numsections (abfd);
   12010        i++, hdrpp++)
   12011     {
   12012       switch ((*hdrpp)->sh_type)
   12013 	{
   12014 	case SHT_MIPS_MSYM:
   12015 	case SHT_MIPS_LIBLIST:
   12016 	  sec = bfd_get_section_by_name (abfd, ".dynstr");
   12017 	  if (sec != NULL)
   12018 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12019 	  break;
   12020 
   12021 	case SHT_MIPS_GPTAB:
   12022 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12023 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
   12024 	  BFD_ASSERT (name != NULL
   12025 		      && CONST_STRNEQ (name, ".gptab."));
   12026 	  sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
   12027 	  BFD_ASSERT (sec != NULL);
   12028 	  (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
   12029 	  break;
   12030 
   12031 	case SHT_MIPS_CONTENT:
   12032 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12033 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
   12034 	  BFD_ASSERT (name != NULL
   12035 		      && CONST_STRNEQ (name, ".MIPS.content"));
   12036 	  sec = bfd_get_section_by_name (abfd,
   12037 					 name + sizeof ".MIPS.content" - 1);
   12038 	  BFD_ASSERT (sec != NULL);
   12039 	  (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12040 	  break;
   12041 
   12042 	case SHT_MIPS_SYMBOL_LIB:
   12043 	  sec = bfd_get_section_by_name (abfd, ".dynsym");
   12044 	  if (sec != NULL)
   12045 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12046 	  sec = bfd_get_section_by_name (abfd, ".liblist");
   12047 	  if (sec != NULL)
   12048 	    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
   12049 	  break;
   12050 
   12051 	case SHT_MIPS_EVENTS:
   12052 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12053 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
   12054 	  BFD_ASSERT (name != NULL);
   12055 	  if (CONST_STRNEQ (name, ".MIPS.events"))
   12056 	    sec = bfd_get_section_by_name (abfd,
   12057 					   name + sizeof ".MIPS.events" - 1);
   12058 	  else
   12059 	    {
   12060 	      BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
   12061 	      sec = bfd_get_section_by_name (abfd,
   12062 					     (name
   12063 					      + sizeof ".MIPS.post_rel" - 1));
   12064 	    }
   12065 	  BFD_ASSERT (sec != NULL);
   12066 	  (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12067 	  break;
   12068 
   12069 	}
   12070     }
   12071 }
   12072 
   12073 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
   12075    segments.  */
   12076 
   12077 int
   12078 _bfd_mips_elf_additional_program_headers (bfd *abfd,
   12079 					  struct bfd_link_info *info ATTRIBUTE_UNUSED)
   12080 {
   12081   asection *s;
   12082   int ret = 0;
   12083 
   12084   /* See if we need a PT_MIPS_REGINFO segment.  */
   12085   s = bfd_get_section_by_name (abfd, ".reginfo");
   12086   if (s && (s->flags & SEC_LOAD))
   12087     ++ret;
   12088 
   12089   /* See if we need a PT_MIPS_ABIFLAGS segment.  */
   12090   if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
   12091     ++ret;
   12092 
   12093   /* See if we need a PT_MIPS_OPTIONS segment.  */
   12094   if (IRIX_COMPAT (abfd) == ict_irix6
   12095       && bfd_get_section_by_name (abfd,
   12096 				  MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
   12097     ++ret;
   12098 
   12099   /* See if we need a PT_MIPS_RTPROC segment.  */
   12100   if (IRIX_COMPAT (abfd) == ict_irix5
   12101       && bfd_get_section_by_name (abfd, ".dynamic")
   12102       && bfd_get_section_by_name (abfd, ".mdebug"))
   12103     ++ret;
   12104 
   12105   /* Allocate a PT_NULL header in dynamic objects.  See
   12106      _bfd_mips_elf_modify_segment_map for details.  */
   12107   if (!SGI_COMPAT (abfd)
   12108       && bfd_get_section_by_name (abfd, ".dynamic"))
   12109     ++ret;
   12110 
   12111   return ret;
   12112 }
   12113 
   12114 /* Modify the segment map for an IRIX5 executable.  */
   12115 
   12116 bfd_boolean
   12117 _bfd_mips_elf_modify_segment_map (bfd *abfd,
   12118 				  struct bfd_link_info *info)
   12119 {
   12120   asection *s;
   12121   struct elf_segment_map *m, **pm;
   12122   bfd_size_type amt;
   12123 
   12124   /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
   12125      segment.  */
   12126   s = bfd_get_section_by_name (abfd, ".reginfo");
   12127   if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12128     {
   12129       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12130 	if (m->p_type == PT_MIPS_REGINFO)
   12131 	  break;
   12132       if (m == NULL)
   12133 	{
   12134 	  amt = sizeof *m;
   12135 	  m = bfd_zalloc (abfd, amt);
   12136 	  if (m == NULL)
   12137 	    return FALSE;
   12138 
   12139 	  m->p_type = PT_MIPS_REGINFO;
   12140 	  m->count = 1;
   12141 	  m->sections[0] = s;
   12142 
   12143 	  /* We want to put it after the PHDR and INTERP segments.  */
   12144 	  pm = &elf_seg_map (abfd);
   12145 	  while (*pm != NULL
   12146 		 && ((*pm)->p_type == PT_PHDR
   12147 		     || (*pm)->p_type == PT_INTERP))
   12148 	    pm = &(*pm)->next;
   12149 
   12150 	  m->next = *pm;
   12151 	  *pm = m;
   12152 	}
   12153     }
   12154 
   12155   /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
   12156      segment.  */
   12157   s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
   12158   if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12159     {
   12160       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12161 	if (m->p_type == PT_MIPS_ABIFLAGS)
   12162 	  break;
   12163       if (m == NULL)
   12164 	{
   12165 	  amt = sizeof *m;
   12166 	  m = bfd_zalloc (abfd, amt);
   12167 	  if (m == NULL)
   12168 	    return FALSE;
   12169 
   12170 	  m->p_type = PT_MIPS_ABIFLAGS;
   12171 	  m->count = 1;
   12172 	  m->sections[0] = s;
   12173 
   12174 	  /* We want to put it after the PHDR and INTERP segments.  */
   12175 	  pm = &elf_seg_map (abfd);
   12176 	  while (*pm != NULL
   12177 		 && ((*pm)->p_type == PT_PHDR
   12178 		     || (*pm)->p_type == PT_INTERP))
   12179 	    pm = &(*pm)->next;
   12180 
   12181 	  m->next = *pm;
   12182 	  *pm = m;
   12183 	}
   12184     }
   12185 
   12186   /* For IRIX 6, we don't have .mdebug sections, nor does anything but
   12187      .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
   12188      PT_MIPS_OPTIONS segment immediately following the program header
   12189      table.  */
   12190   if (NEWABI_P (abfd)
   12191       /* On non-IRIX6 new abi, we'll have already created a segment
   12192 	 for this section, so don't create another.  I'm not sure this
   12193 	 is not also the case for IRIX 6, but I can't test it right
   12194 	 now.  */
   12195       && IRIX_COMPAT (abfd) == ict_irix6)
   12196     {
   12197       for (s = abfd->sections; s; s = s->next)
   12198 	if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
   12199 	  break;
   12200 
   12201       if (s)
   12202 	{
   12203 	  struct elf_segment_map *options_segment;
   12204 
   12205 	  pm = &elf_seg_map (abfd);
   12206 	  while (*pm != NULL
   12207 		 && ((*pm)->p_type == PT_PHDR
   12208 		     || (*pm)->p_type == PT_INTERP))
   12209 	    pm = &(*pm)->next;
   12210 
   12211 	  if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
   12212 	    {
   12213 	      amt = sizeof (struct elf_segment_map);
   12214 	      options_segment = bfd_zalloc (abfd, amt);
   12215 	      options_segment->next = *pm;
   12216 	      options_segment->p_type = PT_MIPS_OPTIONS;
   12217 	      options_segment->p_flags = PF_R;
   12218 	      options_segment->p_flags_valid = TRUE;
   12219 	      options_segment->count = 1;
   12220 	      options_segment->sections[0] = s;
   12221 	      *pm = options_segment;
   12222 	    }
   12223 	}
   12224     }
   12225   else
   12226     {
   12227       if (IRIX_COMPAT (abfd) == ict_irix5)
   12228 	{
   12229 	  /* If there are .dynamic and .mdebug sections, we make a room
   12230 	     for the RTPROC header.  FIXME: Rewrite without section names.  */
   12231 	  if (bfd_get_section_by_name (abfd, ".interp") == NULL
   12232 	      && bfd_get_section_by_name (abfd, ".dynamic") != NULL
   12233 	      && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
   12234 	    {
   12235 	      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12236 		if (m->p_type == PT_MIPS_RTPROC)
   12237 		  break;
   12238 	      if (m == NULL)
   12239 		{
   12240 		  amt = sizeof *m;
   12241 		  m = bfd_zalloc (abfd, amt);
   12242 		  if (m == NULL)
   12243 		    return FALSE;
   12244 
   12245 		  m->p_type = PT_MIPS_RTPROC;
   12246 
   12247 		  s = bfd_get_section_by_name (abfd, ".rtproc");
   12248 		  if (s == NULL)
   12249 		    {
   12250 		      m->count = 0;
   12251 		      m->p_flags = 0;
   12252 		      m->p_flags_valid = 1;
   12253 		    }
   12254 		  else
   12255 		    {
   12256 		      m->count = 1;
   12257 		      m->sections[0] = s;
   12258 		    }
   12259 
   12260 		  /* We want to put it after the DYNAMIC segment.  */
   12261 		  pm = &elf_seg_map (abfd);
   12262 		  while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
   12263 		    pm = &(*pm)->next;
   12264 		  if (*pm != NULL)
   12265 		    pm = &(*pm)->next;
   12266 
   12267 		  m->next = *pm;
   12268 		  *pm = m;
   12269 		}
   12270 	    }
   12271 	}
   12272       /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
   12273 	 .dynstr, .dynsym, and .hash sections, and everything in
   12274 	 between.  */
   12275       for (pm = &elf_seg_map (abfd); *pm != NULL;
   12276 	   pm = &(*pm)->next)
   12277 	if ((*pm)->p_type == PT_DYNAMIC)
   12278 	  break;
   12279       m = *pm;
   12280       /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
   12281 	 glibc's dynamic linker has traditionally derived the number of
   12282 	 tags from the p_filesz field, and sometimes allocates stack
   12283 	 arrays of that size.  An overly-big PT_DYNAMIC segment can
   12284 	 be actively harmful in such cases.  Making PT_DYNAMIC contain
   12285 	 other sections can also make life hard for the prelinker,
   12286 	 which might move one of the other sections to a different
   12287 	 PT_LOAD segment.  */
   12288       if (SGI_COMPAT (abfd)
   12289 	  && m != NULL
   12290 	  && m->count == 1
   12291 	  && strcmp (m->sections[0]->name, ".dynamic") == 0)
   12292 	{
   12293 	  static const char *sec_names[] =
   12294 	  {
   12295 	    ".dynamic", ".dynstr", ".dynsym", ".hash"
   12296 	  };
   12297 	  bfd_vma low, high;
   12298 	  unsigned int i, c;
   12299 	  struct elf_segment_map *n;
   12300 
   12301 	  low = ~(bfd_vma) 0;
   12302 	  high = 0;
   12303 	  for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
   12304 	    {
   12305 	      s = bfd_get_section_by_name (abfd, sec_names[i]);
   12306 	      if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12307 		{
   12308 		  bfd_size_type sz;
   12309 
   12310 		  if (low > s->vma)
   12311 		    low = s->vma;
   12312 		  sz = s->size;
   12313 		  if (high < s->vma + sz)
   12314 		    high = s->vma + sz;
   12315 		}
   12316 	    }
   12317 
   12318 	  c = 0;
   12319 	  for (s = abfd->sections; s != NULL; s = s->next)
   12320 	    if ((s->flags & SEC_LOAD) != 0
   12321 		&& s->vma >= low
   12322 		&& s->vma + s->size <= high)
   12323 	      ++c;
   12324 
   12325 	  amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
   12326 	  n = bfd_zalloc (abfd, amt);
   12327 	  if (n == NULL)
   12328 	    return FALSE;
   12329 	  *n = *m;
   12330 	  n->count = c;
   12331 
   12332 	  i = 0;
   12333 	  for (s = abfd->sections; s != NULL; s = s->next)
   12334 	    {
   12335 	      if ((s->flags & SEC_LOAD) != 0
   12336 		  && s->vma >= low
   12337 		  && s->vma + s->size <= high)
   12338 		{
   12339 		  n->sections[i] = s;
   12340 		  ++i;
   12341 		}
   12342 	    }
   12343 
   12344 	  *pm = n;
   12345 	}
   12346     }
   12347 
   12348   /* Allocate a spare program header in dynamic objects so that tools
   12349      like the prelinker can add an extra PT_LOAD entry.
   12350 
   12351      If the prelinker needs to make room for a new PT_LOAD entry, its
   12352      standard procedure is to move the first (read-only) sections into
   12353      the new (writable) segment.  However, the MIPS ABI requires
   12354      .dynamic to be in a read-only segment, and the section will often
   12355      start within sizeof (ElfNN_Phdr) bytes of the last program header.
   12356 
   12357      Although the prelinker could in principle move .dynamic to a
   12358      writable segment, it seems better to allocate a spare program
   12359      header instead, and avoid the need to move any sections.
   12360      There is a long tradition of allocating spare dynamic tags,
   12361      so allocating a spare program header seems like a natural
   12362      extension.
   12363 
   12364      If INFO is NULL, we may be copying an already prelinked binary
   12365      with objcopy or strip, so do not add this header.  */
   12366   if (info != NULL
   12367       && !SGI_COMPAT (abfd)
   12368       && bfd_get_section_by_name (abfd, ".dynamic"))
   12369     {
   12370       for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
   12371 	if ((*pm)->p_type == PT_NULL)
   12372 	  break;
   12373       if (*pm == NULL)
   12374 	{
   12375 	  m = bfd_zalloc (abfd, sizeof (*m));
   12376 	  if (m == NULL)
   12377 	    return FALSE;
   12378 
   12379 	  m->p_type = PT_NULL;
   12380 	  *pm = m;
   12381 	}
   12382     }
   12383 
   12384   return TRUE;
   12385 }
   12386 
   12387 /* Return the section that should be marked against GC for a given
   12389    relocation.  */
   12390 
   12391 asection *
   12392 _bfd_mips_elf_gc_mark_hook (asection *sec,
   12393 			    struct bfd_link_info *info,
   12394 			    Elf_Internal_Rela *rel,
   12395 			    struct elf_link_hash_entry *h,
   12396 			    Elf_Internal_Sym *sym)
   12397 {
   12398   /* ??? Do mips16 stub sections need to be handled special?  */
   12399 
   12400   if (h != NULL)
   12401     switch (ELF_R_TYPE (sec->owner, rel->r_info))
   12402       {
   12403       case R_MIPS_GNU_VTINHERIT:
   12404       case R_MIPS_GNU_VTENTRY:
   12405 	return NULL;
   12406       }
   12407 
   12408   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   12409 }
   12410 
   12411 /* Update the got entry reference counts for the section being removed.  */
   12412 
   12413 bfd_boolean
   12414 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
   12415 			     struct bfd_link_info *info ATTRIBUTE_UNUSED,
   12416 			     asection *sec ATTRIBUTE_UNUSED,
   12417 			     const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
   12418 {
   12419 #if 0
   12420   Elf_Internal_Shdr *symtab_hdr;
   12421   struct elf_link_hash_entry **sym_hashes;
   12422   bfd_signed_vma *local_got_refcounts;
   12423   const Elf_Internal_Rela *rel, *relend;
   12424   unsigned long r_symndx;
   12425   struct elf_link_hash_entry *h;
   12426 
   12427   if (info->relocatable)
   12428     return TRUE;
   12429 
   12430   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12431   sym_hashes = elf_sym_hashes (abfd);
   12432   local_got_refcounts = elf_local_got_refcounts (abfd);
   12433 
   12434   relend = relocs + sec->reloc_count;
   12435   for (rel = relocs; rel < relend; rel++)
   12436     switch (ELF_R_TYPE (abfd, rel->r_info))
   12437       {
   12438       case R_MIPS16_GOT16:
   12439       case R_MIPS16_CALL16:
   12440       case R_MIPS_GOT16:
   12441       case R_MIPS_CALL16:
   12442       case R_MIPS_CALL_HI16:
   12443       case R_MIPS_CALL_LO16:
   12444       case R_MIPS_GOT_HI16:
   12445       case R_MIPS_GOT_LO16:
   12446       case R_MIPS_GOT_DISP:
   12447       case R_MIPS_GOT_PAGE:
   12448       case R_MIPS_GOT_OFST:
   12449       case R_MICROMIPS_GOT16:
   12450       case R_MICROMIPS_CALL16:
   12451       case R_MICROMIPS_CALL_HI16:
   12452       case R_MICROMIPS_CALL_LO16:
   12453       case R_MICROMIPS_GOT_HI16:
   12454       case R_MICROMIPS_GOT_LO16:
   12455       case R_MICROMIPS_GOT_DISP:
   12456       case R_MICROMIPS_GOT_PAGE:
   12457       case R_MICROMIPS_GOT_OFST:
   12458 	/* ??? It would seem that the existing MIPS code does no sort
   12459 	   of reference counting or whatnot on its GOT and PLT entries,
   12460 	   so it is not possible to garbage collect them at this time.  */
   12461 	break;
   12462 
   12463       default:
   12464 	break;
   12465       }
   12466 #endif
   12467 
   12468   return TRUE;
   12469 }
   12470 
   12471 /* Prevent .MIPS.abiflags from being discarded with --gc-sections.  */
   12472 
   12473 bfd_boolean
   12474 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
   12475 				      elf_gc_mark_hook_fn gc_mark_hook)
   12476 {
   12477   bfd *sub;
   12478 
   12479   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
   12480 
   12481   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12482     {
   12483       asection *o;
   12484 
   12485       if (! is_mips_elf (sub))
   12486 	continue;
   12487 
   12488       for (o = sub->sections; o != NULL; o = o->next)
   12489 	if (!o->gc_mark
   12490 	    && MIPS_ELF_ABIFLAGS_SECTION_NAME_P
   12491 		 (bfd_get_section_name (sub, o)))
   12492 	  {
   12493 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   12494 	      return FALSE;
   12495 	  }
   12496     }
   12497 
   12498   return TRUE;
   12499 }
   12500 
   12501 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
   12503    hiding the old indirect symbol.  Process additional relocation
   12504    information.  Also called for weakdefs, in which case we just let
   12505    _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
   12506 
   12507 void
   12508 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
   12509 				    struct elf_link_hash_entry *dir,
   12510 				    struct elf_link_hash_entry *ind)
   12511 {
   12512   struct mips_elf_link_hash_entry *dirmips, *indmips;
   12513 
   12514   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   12515 
   12516   dirmips = (struct mips_elf_link_hash_entry *) dir;
   12517   indmips = (struct mips_elf_link_hash_entry *) ind;
   12518   /* Any absolute non-dynamic relocations against an indirect or weak
   12519      definition will be against the target symbol.  */
   12520   if (indmips->has_static_relocs)
   12521     dirmips->has_static_relocs = TRUE;
   12522 
   12523   if (ind->root.type != bfd_link_hash_indirect)
   12524     return;
   12525 
   12526   dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
   12527   if (indmips->readonly_reloc)
   12528     dirmips->readonly_reloc = TRUE;
   12529   if (indmips->no_fn_stub)
   12530     dirmips->no_fn_stub = TRUE;
   12531   if (indmips->fn_stub)
   12532     {
   12533       dirmips->fn_stub = indmips->fn_stub;
   12534       indmips->fn_stub = NULL;
   12535     }
   12536   if (indmips->need_fn_stub)
   12537     {
   12538       dirmips->need_fn_stub = TRUE;
   12539       indmips->need_fn_stub = FALSE;
   12540     }
   12541   if (indmips->call_stub)
   12542     {
   12543       dirmips->call_stub = indmips->call_stub;
   12544       indmips->call_stub = NULL;
   12545     }
   12546   if (indmips->call_fp_stub)
   12547     {
   12548       dirmips->call_fp_stub = indmips->call_fp_stub;
   12549       indmips->call_fp_stub = NULL;
   12550     }
   12551   if (indmips->global_got_area < dirmips->global_got_area)
   12552     dirmips->global_got_area = indmips->global_got_area;
   12553   if (indmips->global_got_area < GGA_NONE)
   12554     indmips->global_got_area = GGA_NONE;
   12555   if (indmips->has_nonpic_branches)
   12556     dirmips->has_nonpic_branches = TRUE;
   12557 }
   12558 
   12559 #define PDR_SIZE 32
   12561 
   12562 bfd_boolean
   12563 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
   12564 			    struct bfd_link_info *info)
   12565 {
   12566   asection *o;
   12567   bfd_boolean ret = FALSE;
   12568   unsigned char *tdata;
   12569   size_t i, skip;
   12570 
   12571   o = bfd_get_section_by_name (abfd, ".pdr");
   12572   if (! o)
   12573     return FALSE;
   12574   if (o->size == 0)
   12575     return FALSE;
   12576   if (o->size % PDR_SIZE != 0)
   12577     return FALSE;
   12578   if (o->output_section != NULL
   12579       && bfd_is_abs_section (o->output_section))
   12580     return FALSE;
   12581 
   12582   tdata = bfd_zmalloc (o->size / PDR_SIZE);
   12583   if (! tdata)
   12584     return FALSE;
   12585 
   12586   cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   12587 					    info->keep_memory);
   12588   if (!cookie->rels)
   12589     {
   12590       free (tdata);
   12591       return FALSE;
   12592     }
   12593 
   12594   cookie->rel = cookie->rels;
   12595   cookie->relend = cookie->rels + o->reloc_count;
   12596 
   12597   for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
   12598     {
   12599       if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
   12600 	{
   12601 	  tdata[i] = 1;
   12602 	  skip ++;
   12603 	}
   12604     }
   12605 
   12606   if (skip != 0)
   12607     {
   12608       mips_elf_section_data (o)->u.tdata = tdata;
   12609       if (o->rawsize == 0)
   12610 	o->rawsize = o->size;
   12611       o->size -= skip * PDR_SIZE;
   12612       ret = TRUE;
   12613     }
   12614   else
   12615     free (tdata);
   12616 
   12617   if (! info->keep_memory)
   12618     free (cookie->rels);
   12619 
   12620   return ret;
   12621 }
   12622 
   12623 bfd_boolean
   12624 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
   12625 {
   12626   if (strcmp (sec->name, ".pdr") == 0)
   12627     return TRUE;
   12628   return FALSE;
   12629 }
   12630 
   12631 bfd_boolean
   12632 _bfd_mips_elf_write_section (bfd *output_bfd,
   12633 			     struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   12634                              asection *sec, bfd_byte *contents)
   12635 {
   12636   bfd_byte *to, *from, *end;
   12637   int i;
   12638 
   12639   if (strcmp (sec->name, ".pdr") != 0)
   12640     return FALSE;
   12641 
   12642   if (mips_elf_section_data (sec)->u.tdata == NULL)
   12643     return FALSE;
   12644 
   12645   to = contents;
   12646   end = contents + sec->size;
   12647   for (from = contents, i = 0;
   12648        from < end;
   12649        from += PDR_SIZE, i++)
   12650     {
   12651       if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
   12652 	continue;
   12653       if (to != from)
   12654 	memcpy (to, from, PDR_SIZE);
   12655       to += PDR_SIZE;
   12656     }
   12657   bfd_set_section_contents (output_bfd, sec->output_section, contents,
   12658 			    sec->output_offset, sec->size);
   12659   return TRUE;
   12660 }
   12661 
   12662 /* microMIPS code retains local labels for linker relaxation.  Omit them
   12664    from output by default for clarity.  */
   12665 
   12666 bfd_boolean
   12667 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
   12668 {
   12669   return _bfd_elf_is_local_label_name (abfd, sym->name);
   12670 }
   12671 
   12672 /* MIPS ELF uses a special find_nearest_line routine in order the
   12673    handle the ECOFF debugging information.  */
   12674 
   12675 struct mips_elf_find_line
   12676 {
   12677   struct ecoff_debug_info d;
   12678   struct ecoff_find_line i;
   12679 };
   12680 
   12681 bfd_boolean
   12682 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
   12683 				 asection *section, bfd_vma offset,
   12684 				 const char **filename_ptr,
   12685 				 const char **functionname_ptr,
   12686 				 unsigned int *line_ptr,
   12687 				 unsigned int *discriminator_ptr)
   12688 {
   12689   asection *msec;
   12690 
   12691   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   12692 				     filename_ptr, functionname_ptr,
   12693 				     line_ptr, discriminator_ptr,
   12694 				     dwarf_debug_sections,
   12695 				     ABI_64_P (abfd) ? 8 : 0,
   12696 				     &elf_tdata (abfd)->dwarf2_find_line_info))
   12697     return TRUE;
   12698 
   12699   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   12700 				     filename_ptr, functionname_ptr,
   12701 				     line_ptr))
   12702     return TRUE;
   12703 
   12704   msec = bfd_get_section_by_name (abfd, ".mdebug");
   12705   if (msec != NULL)
   12706     {
   12707       flagword origflags;
   12708       struct mips_elf_find_line *fi;
   12709       const struct ecoff_debug_swap * const swap =
   12710 	get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   12711 
   12712       /* If we are called during a link, mips_elf_final_link may have
   12713 	 cleared the SEC_HAS_CONTENTS field.  We force it back on here
   12714 	 if appropriate (which it normally will be).  */
   12715       origflags = msec->flags;
   12716       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
   12717 	msec->flags |= SEC_HAS_CONTENTS;
   12718 
   12719       fi = mips_elf_tdata (abfd)->find_line_info;
   12720       if (fi == NULL)
   12721 	{
   12722 	  bfd_size_type external_fdr_size;
   12723 	  char *fraw_src;
   12724 	  char *fraw_end;
   12725 	  struct fdr *fdr_ptr;
   12726 	  bfd_size_type amt = sizeof (struct mips_elf_find_line);
   12727 
   12728 	  fi = bfd_zalloc (abfd, amt);
   12729 	  if (fi == NULL)
   12730 	    {
   12731 	      msec->flags = origflags;
   12732 	      return FALSE;
   12733 	    }
   12734 
   12735 	  if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
   12736 	    {
   12737 	      msec->flags = origflags;
   12738 	      return FALSE;
   12739 	    }
   12740 
   12741 	  /* Swap in the FDR information.  */
   12742 	  amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
   12743 	  fi->d.fdr = bfd_alloc (abfd, amt);
   12744 	  if (fi->d.fdr == NULL)
   12745 	    {
   12746 	      msec->flags = origflags;
   12747 	      return FALSE;
   12748 	    }
   12749 	  external_fdr_size = swap->external_fdr_size;
   12750 	  fdr_ptr = fi->d.fdr;
   12751 	  fraw_src = (char *) fi->d.external_fdr;
   12752 	  fraw_end = (fraw_src
   12753 		      + fi->d.symbolic_header.ifdMax * external_fdr_size);
   12754 	  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
   12755 	    (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
   12756 
   12757 	  mips_elf_tdata (abfd)->find_line_info = fi;
   12758 
   12759 	  /* Note that we don't bother to ever free this information.
   12760              find_nearest_line is either called all the time, as in
   12761              objdump -l, so the information should be saved, or it is
   12762              rarely called, as in ld error messages, so the memory
   12763              wasted is unimportant.  Still, it would probably be a
   12764              good idea for free_cached_info to throw it away.  */
   12765 	}
   12766 
   12767       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
   12768 				  &fi->i, filename_ptr, functionname_ptr,
   12769 				  line_ptr))
   12770 	{
   12771 	  msec->flags = origflags;
   12772 	  return TRUE;
   12773 	}
   12774 
   12775       msec->flags = origflags;
   12776     }
   12777 
   12778   /* Fall back on the generic ELF find_nearest_line routine.  */
   12779 
   12780   return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
   12781 				     filename_ptr, functionname_ptr,
   12782 				     line_ptr, discriminator_ptr);
   12783 }
   12784 
   12785 bfd_boolean
   12786 _bfd_mips_elf_find_inliner_info (bfd *abfd,
   12787 				 const char **filename_ptr,
   12788 				 const char **functionname_ptr,
   12789 				 unsigned int *line_ptr)
   12790 {
   12791   bfd_boolean found;
   12792   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   12793 					 functionname_ptr, line_ptr,
   12794 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   12795   return found;
   12796 }
   12797 
   12798 
   12799 /* When are writing out the .options or .MIPS.options section,
   12801    remember the bytes we are writing out, so that we can install the
   12802    GP value in the section_processing routine.  */
   12803 
   12804 bfd_boolean
   12805 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
   12806 				    const void *location,
   12807 				    file_ptr offset, bfd_size_type count)
   12808 {
   12809   if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
   12810     {
   12811       bfd_byte *c;
   12812 
   12813       if (elf_section_data (section) == NULL)
   12814 	{
   12815 	  bfd_size_type amt = sizeof (struct bfd_elf_section_data);
   12816 	  section->used_by_bfd = bfd_zalloc (abfd, amt);
   12817 	  if (elf_section_data (section) == NULL)
   12818 	    return FALSE;
   12819 	}
   12820       c = mips_elf_section_data (section)->u.tdata;
   12821       if (c == NULL)
   12822 	{
   12823 	  c = bfd_zalloc (abfd, section->size);
   12824 	  if (c == NULL)
   12825 	    return FALSE;
   12826 	  mips_elf_section_data (section)->u.tdata = c;
   12827 	}
   12828 
   12829       memcpy (c + offset, location, count);
   12830     }
   12831 
   12832   return _bfd_elf_set_section_contents (abfd, section, location, offset,
   12833 					count);
   12834 }
   12835 
   12836 /* This is almost identical to bfd_generic_get_... except that some
   12837    MIPS relocations need to be handled specially.  Sigh.  */
   12838 
   12839 bfd_byte *
   12840 _bfd_elf_mips_get_relocated_section_contents
   12841   (bfd *abfd,
   12842    struct bfd_link_info *link_info,
   12843    struct bfd_link_order *link_order,
   12844    bfd_byte *data,
   12845    bfd_boolean relocatable,
   12846    asymbol **symbols)
   12847 {
   12848   /* Get enough memory to hold the stuff */
   12849   bfd *input_bfd = link_order->u.indirect.section->owner;
   12850   asection *input_section = link_order->u.indirect.section;
   12851   bfd_size_type sz;
   12852 
   12853   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
   12854   arelent **reloc_vector = NULL;
   12855   long reloc_count;
   12856 
   12857   if (reloc_size < 0)
   12858     goto error_return;
   12859 
   12860   reloc_vector = bfd_malloc (reloc_size);
   12861   if (reloc_vector == NULL && reloc_size != 0)
   12862     goto error_return;
   12863 
   12864   /* read in the section */
   12865   sz = input_section->rawsize ? input_section->rawsize : input_section->size;
   12866   if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
   12867     goto error_return;
   12868 
   12869   reloc_count = bfd_canonicalize_reloc (input_bfd,
   12870 					input_section,
   12871 					reloc_vector,
   12872 					symbols);
   12873   if (reloc_count < 0)
   12874     goto error_return;
   12875 
   12876   if (reloc_count > 0)
   12877     {
   12878       arelent **parent;
   12879       /* for mips */
   12880       int gp_found;
   12881       bfd_vma gp = 0x12345678;	/* initialize just to shut gcc up */
   12882 
   12883       {
   12884 	struct bfd_hash_entry *h;
   12885 	struct bfd_link_hash_entry *lh;
   12886 	/* Skip all this stuff if we aren't mixing formats.  */
   12887 	if (abfd && input_bfd
   12888 	    && abfd->xvec == input_bfd->xvec)
   12889 	  lh = 0;
   12890 	else
   12891 	  {
   12892 	    h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
   12893 	    lh = (struct bfd_link_hash_entry *) h;
   12894 	  }
   12895       lookup:
   12896 	if (lh)
   12897 	  {
   12898 	    switch (lh->type)
   12899 	      {
   12900 	      case bfd_link_hash_undefined:
   12901 	      case bfd_link_hash_undefweak:
   12902 	      case bfd_link_hash_common:
   12903 		gp_found = 0;
   12904 		break;
   12905 	      case bfd_link_hash_defined:
   12906 	      case bfd_link_hash_defweak:
   12907 		gp_found = 1;
   12908 		gp = lh->u.def.value;
   12909 		break;
   12910 	      case bfd_link_hash_indirect:
   12911 	      case bfd_link_hash_warning:
   12912 		lh = lh->u.i.link;
   12913 		/* @@FIXME  ignoring warning for now */
   12914 		goto lookup;
   12915 	      case bfd_link_hash_new:
   12916 	      default:
   12917 		abort ();
   12918 	      }
   12919 	  }
   12920 	else
   12921 	  gp_found = 0;
   12922       }
   12923       /* end mips */
   12924       for (parent = reloc_vector; *parent != NULL; parent++)
   12925 	{
   12926 	  char *error_message = NULL;
   12927 	  bfd_reloc_status_type r;
   12928 
   12929 	  /* Specific to MIPS: Deal with relocation types that require
   12930 	     knowing the gp of the output bfd.  */
   12931 	  asymbol *sym = *(*parent)->sym_ptr_ptr;
   12932 
   12933 	  /* If we've managed to find the gp and have a special
   12934 	     function for the relocation then go ahead, else default
   12935 	     to the generic handling.  */
   12936 	  if (gp_found
   12937 	      && (*parent)->howto->special_function
   12938 	      == _bfd_mips_elf32_gprel16_reloc)
   12939 	    r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
   12940 					       input_section, relocatable,
   12941 					       data, gp);
   12942 	  else
   12943 	    r = bfd_perform_relocation (input_bfd, *parent, data,
   12944 					input_section,
   12945 					relocatable ? abfd : NULL,
   12946 					&error_message);
   12947 
   12948 	  if (relocatable)
   12949 	    {
   12950 	      asection *os = input_section->output_section;
   12951 
   12952 	      /* A partial link, so keep the relocs */
   12953 	      os->orelocation[os->reloc_count] = *parent;
   12954 	      os->reloc_count++;
   12955 	    }
   12956 
   12957 	  if (r != bfd_reloc_ok)
   12958 	    {
   12959 	      switch (r)
   12960 		{
   12961 		case bfd_reloc_undefined:
   12962 		  if (!((*link_info->callbacks->undefined_symbol)
   12963 			(link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
   12964 			 input_bfd, input_section, (*parent)->address, TRUE)))
   12965 		    goto error_return;
   12966 		  break;
   12967 		case bfd_reloc_dangerous:
   12968 		  BFD_ASSERT (error_message != NULL);
   12969 		  if (!((*link_info->callbacks->reloc_dangerous)
   12970 			(link_info, error_message, input_bfd, input_section,
   12971 			 (*parent)->address)))
   12972 		    goto error_return;
   12973 		  break;
   12974 		case bfd_reloc_overflow:
   12975 		  if (!((*link_info->callbacks->reloc_overflow)
   12976 			(link_info, NULL,
   12977 			 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
   12978 			 (*parent)->howto->name, (*parent)->addend,
   12979 			 input_bfd, input_section, (*parent)->address)))
   12980 		    goto error_return;
   12981 		  break;
   12982 		case bfd_reloc_outofrange:
   12983 		default:
   12984 		  abort ();
   12985 		  break;
   12986 		}
   12987 
   12988 	    }
   12989 	}
   12990     }
   12991   if (reloc_vector != NULL)
   12992     free (reloc_vector);
   12993   return data;
   12994 
   12995 error_return:
   12996   if (reloc_vector != NULL)
   12997     free (reloc_vector);
   12998   return NULL;
   12999 }
   13000 
   13001 static bfd_boolean
   13003 mips_elf_relax_delete_bytes (bfd *abfd,
   13004 			     asection *sec, bfd_vma addr, int count)
   13005 {
   13006   Elf_Internal_Shdr *symtab_hdr;
   13007   unsigned int sec_shndx;
   13008   bfd_byte *contents;
   13009   Elf_Internal_Rela *irel, *irelend;
   13010   Elf_Internal_Sym *isym;
   13011   Elf_Internal_Sym *isymend;
   13012   struct elf_link_hash_entry **sym_hashes;
   13013   struct elf_link_hash_entry **end_hashes;
   13014   struct elf_link_hash_entry **start_hashes;
   13015   unsigned int symcount;
   13016 
   13017   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   13018   contents = elf_section_data (sec)->this_hdr.contents;
   13019 
   13020   irel = elf_section_data (sec)->relocs;
   13021   irelend = irel + sec->reloc_count;
   13022 
   13023   /* Actually delete the bytes.  */
   13024   memmove (contents + addr, contents + addr + count,
   13025 	   (size_t) (sec->size - addr - count));
   13026   sec->size -= count;
   13027 
   13028   /* Adjust all the relocs.  */
   13029   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
   13030     {
   13031       /* Get the new reloc address.  */
   13032       if (irel->r_offset > addr)
   13033 	irel->r_offset -= count;
   13034     }
   13035 
   13036   BFD_ASSERT (addr % 2 == 0);
   13037   BFD_ASSERT (count % 2 == 0);
   13038 
   13039   /* Adjust the local symbols defined in this section.  */
   13040   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13041   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
   13042   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
   13043     if (isym->st_shndx == sec_shndx && isym->st_value > addr)
   13044       isym->st_value -= count;
   13045 
   13046   /* Now adjust the global symbols defined in this section.  */
   13047   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   13048 	      - symtab_hdr->sh_info);
   13049   sym_hashes = start_hashes = elf_sym_hashes (abfd);
   13050   end_hashes = sym_hashes + symcount;
   13051 
   13052   for (; sym_hashes < end_hashes; sym_hashes++)
   13053     {
   13054       struct elf_link_hash_entry *sym_hash = *sym_hashes;
   13055 
   13056       if ((sym_hash->root.type == bfd_link_hash_defined
   13057 	   || sym_hash->root.type == bfd_link_hash_defweak)
   13058 	  && sym_hash->root.u.def.section == sec)
   13059 	{
   13060 	  bfd_vma value = sym_hash->root.u.def.value;
   13061 
   13062 	  if (ELF_ST_IS_MICROMIPS (sym_hash->other))
   13063 	    value &= MINUS_TWO;
   13064 	  if (value > addr)
   13065 	    sym_hash->root.u.def.value -= count;
   13066 	}
   13067     }
   13068 
   13069   return TRUE;
   13070 }
   13071 
   13072 
   13073 /* Opcodes needed for microMIPS relaxation as found in
   13074    opcodes/micromips-opc.c.  */
   13075 
   13076 struct opcode_descriptor {
   13077   unsigned long match;
   13078   unsigned long mask;
   13079 };
   13080 
   13081 /* The $ra register aka $31.  */
   13082 
   13083 #define RA 31
   13084 
   13085 /* 32-bit instruction format register fields.  */
   13086 
   13087 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
   13088 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
   13089 
   13090 /* Check if a 5-bit register index can be abbreviated to 3 bits.  */
   13091 
   13092 #define OP16_VALID_REG(r) \
   13093   ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
   13094 
   13095 
   13096 /* 32-bit and 16-bit branches.  */
   13097 
   13098 static const struct opcode_descriptor b_insns_32[] = {
   13099   { /* "b",	"p",		*/ 0x40400000, 0xffff0000 }, /* bgez 0 */
   13100   { /* "b",	"p",		*/ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
   13101   { 0, 0 }  /* End marker for find_match().  */
   13102 };
   13103 
   13104 static const struct opcode_descriptor bc_insn_32 =
   13105   { /* "bc(1|2)(ft)", "N,p",	*/ 0x42800000, 0xfec30000 };
   13106 
   13107 static const struct opcode_descriptor bz_insn_32 =
   13108   { /* "b(g|l)(e|t)z", "s,p",	*/ 0x40000000, 0xff200000 };
   13109 
   13110 static const struct opcode_descriptor bzal_insn_32 =
   13111   { /* "b(ge|lt)zal", "s,p",	*/ 0x40200000, 0xffa00000 };
   13112 
   13113 static const struct opcode_descriptor beq_insn_32 =
   13114   { /* "b(eq|ne)", "s,t,p",	*/ 0x94000000, 0xdc000000 };
   13115 
   13116 static const struct opcode_descriptor b_insn_16 =
   13117   { /* "b",	"mD",		*/ 0xcc00,     0xfc00 };
   13118 
   13119 static const struct opcode_descriptor bz_insn_16 =
   13120   { /* "b(eq|ne)z", "md,mE",	*/ 0x8c00,     0xdc00 };
   13121 
   13122 
   13123 /* 32-bit and 16-bit branch EQ and NE zero.  */
   13124 
   13125 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
   13126    eq and second the ne.  This convention is used when replacing a
   13127    32-bit BEQ/BNE with the 16-bit version.  */
   13128 
   13129 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
   13130 
   13131 static const struct opcode_descriptor bz_rs_insns_32[] = {
   13132   { /* "beqz",	"s,p",		*/ 0x94000000, 0xffe00000 },
   13133   { /* "bnez",	"s,p",		*/ 0xb4000000, 0xffe00000 },
   13134   { 0, 0 }  /* End marker for find_match().  */
   13135 };
   13136 
   13137 static const struct opcode_descriptor bz_rt_insns_32[] = {
   13138   { /* "beqz",	"t,p",		*/ 0x94000000, 0xfc01f000 },
   13139   { /* "bnez",	"t,p",		*/ 0xb4000000, 0xfc01f000 },
   13140   { 0, 0 }  /* End marker for find_match().  */
   13141 };
   13142 
   13143 static const struct opcode_descriptor bzc_insns_32[] = {
   13144   { /* "beqzc",	"s,p",		*/ 0x40e00000, 0xffe00000 },
   13145   { /* "bnezc",	"s,p",		*/ 0x40a00000, 0xffe00000 },
   13146   { 0, 0 }  /* End marker for find_match().  */
   13147 };
   13148 
   13149 static const struct opcode_descriptor bz_insns_16[] = {
   13150   { /* "beqz",	"md,mE",	*/ 0x8c00,     0xfc00 },
   13151   { /* "bnez",	"md,mE",	*/ 0xac00,     0xfc00 },
   13152   { 0, 0 }  /* End marker for find_match().  */
   13153 };
   13154 
   13155 /* Switch between a 5-bit register index and its 3-bit shorthand.  */
   13156 
   13157 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
   13158 #define BZ16_REG_FIELD(r) \
   13159   (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
   13160 
   13161 
   13162 /* 32-bit instructions with a delay slot.  */
   13163 
   13164 static const struct opcode_descriptor jal_insn_32_bd16 =
   13165   { /* "jals",	"a",		*/ 0x74000000, 0xfc000000 };
   13166 
   13167 static const struct opcode_descriptor jal_insn_32_bd32 =
   13168   { /* "jal",	"a",		*/ 0xf4000000, 0xfc000000 };
   13169 
   13170 static const struct opcode_descriptor jal_x_insn_32_bd32 =
   13171   { /* "jal[x]", "a",		*/ 0xf0000000, 0xf8000000 };
   13172 
   13173 static const struct opcode_descriptor j_insn_32 =
   13174   { /* "j",	"a",		*/ 0xd4000000, 0xfc000000 };
   13175 
   13176 static const struct opcode_descriptor jalr_insn_32 =
   13177   { /* "jalr[.hb]", "t,s",	*/ 0x00000f3c, 0xfc00efff };
   13178 
   13179 /* This table can be compacted, because no opcode replacement is made.  */
   13180 
   13181 static const struct opcode_descriptor ds_insns_32_bd16[] = {
   13182   { /* "jals",	"a",		*/ 0x74000000, 0xfc000000 },
   13183 
   13184   { /* "jalrs[.hb]", "t,s",	*/ 0x00004f3c, 0xfc00efff },
   13185   { /* "b(ge|lt)zals", "s,p",	*/ 0x42200000, 0xffa00000 },
   13186 
   13187   { /* "b(g|l)(e|t)z", "s,p",	*/ 0x40000000, 0xff200000 },
   13188   { /* "b(eq|ne)", "s,t,p",	*/ 0x94000000, 0xdc000000 },
   13189   { /* "j",	"a",		*/ 0xd4000000, 0xfc000000 },
   13190   { 0, 0 }  /* End marker for find_match().  */
   13191 };
   13192 
   13193 /* This table can be compacted, because no opcode replacement is made.  */
   13194 
   13195 static const struct opcode_descriptor ds_insns_32_bd32[] = {
   13196   { /* "jal[x]", "a",		*/ 0xf0000000, 0xf8000000 },
   13197 
   13198   { /* "jalr[.hb]", "t,s",	*/ 0x00000f3c, 0xfc00efff },
   13199   { /* "b(ge|lt)zal", "s,p",	*/ 0x40200000, 0xffa00000 },
   13200   { 0, 0 }  /* End marker for find_match().  */
   13201 };
   13202 
   13203 
   13204 /* 16-bit instructions with a delay slot.  */
   13205 
   13206 static const struct opcode_descriptor jalr_insn_16_bd16 =
   13207   { /* "jalrs",	"my,mj",	*/ 0x45e0,     0xffe0 };
   13208 
   13209 static const struct opcode_descriptor jalr_insn_16_bd32 =
   13210   { /* "jalr",	"my,mj",	*/ 0x45c0,     0xffe0 };
   13211 
   13212 static const struct opcode_descriptor jr_insn_16 =
   13213   { /* "jr",	"mj",		*/ 0x4580,     0xffe0 };
   13214 
   13215 #define JR16_REG(opcode) ((opcode) & 0x1f)
   13216 
   13217 /* This table can be compacted, because no opcode replacement is made.  */
   13218 
   13219 static const struct opcode_descriptor ds_insns_16_bd16[] = {
   13220   { /* "jalrs",	"my,mj",	*/ 0x45e0,     0xffe0 },
   13221 
   13222   { /* "b",	"mD",		*/ 0xcc00,     0xfc00 },
   13223   { /* "b(eq|ne)z", "md,mE",	*/ 0x8c00,     0xdc00 },
   13224   { /* "jr",	"mj",		*/ 0x4580,     0xffe0 },
   13225   { 0, 0 }  /* End marker for find_match().  */
   13226 };
   13227 
   13228 
   13229 /* LUI instruction.  */
   13230 
   13231 static const struct opcode_descriptor lui_insn =
   13232  { /* "lui",	"s,u",		*/ 0x41a00000, 0xffe00000 };
   13233 
   13234 
   13235 /* ADDIU instruction.  */
   13236 
   13237 static const struct opcode_descriptor addiu_insn =
   13238   { /* "addiu",	"t,r,j",	*/ 0x30000000, 0xfc000000 };
   13239 
   13240 static const struct opcode_descriptor addiupc_insn =
   13241   { /* "addiu",	"mb,$pc,mQ",	*/ 0x78000000, 0xfc000000 };
   13242 
   13243 #define ADDIUPC_REG_FIELD(r) \
   13244   (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
   13245 
   13246 
   13247 /* Relaxable instructions in a JAL delay slot: MOVE.  */
   13248 
   13249 /* The 16-bit move has rd in 9:5 and rs in 4:0.  The 32-bit moves
   13250    (ADDU, OR) have rd in 15:11 and rs in 10:16.  */
   13251 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
   13252 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
   13253 
   13254 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
   13255 #define MOVE16_RS_FIELD(r) (((r) & 0x1f)     )
   13256 
   13257 static const struct opcode_descriptor move_insns_32[] = {
   13258   { /* "move",	"d,s",		*/ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
   13259   { /* "move",	"d,s",		*/ 0x00000290, 0xffe007ff }, /* or   d,s,$0 */
   13260   { 0, 0 }  /* End marker for find_match().  */
   13261 };
   13262 
   13263 static const struct opcode_descriptor move_insn_16 =
   13264   { /* "move",	"mp,mj",	*/ 0x0c00,     0xfc00 };
   13265 
   13266 
   13267 /* NOP instructions.  */
   13268 
   13269 static const struct opcode_descriptor nop_insn_32 =
   13270   { /* "nop",	"",		*/ 0x00000000, 0xffffffff };
   13271 
   13272 static const struct opcode_descriptor nop_insn_16 =
   13273   { /* "nop",	"",		*/ 0x0c00,     0xffff };
   13274 
   13275 
   13276 /* Instruction match support.  */
   13277 
   13278 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
   13279 
   13280 static int
   13281 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
   13282 {
   13283   unsigned long indx;
   13284 
   13285   for (indx = 0; insn[indx].mask != 0; indx++)
   13286     if (MATCH (opcode, insn[indx]))
   13287       return indx;
   13288 
   13289   return -1;
   13290 }
   13291 
   13292 
   13293 /* Branch and delay slot decoding support.  */
   13294 
   13295 /* If PTR points to what *might* be a 16-bit branch or jump, then
   13296    return the minimum length of its delay slot, otherwise return 0.
   13297    Non-zero results are not definitive as we might be checking against
   13298    the second half of another instruction.  */
   13299 
   13300 static int
   13301 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
   13302 {
   13303   unsigned long opcode;
   13304   int bdsize;
   13305 
   13306   opcode = bfd_get_16 (abfd, ptr);
   13307   if (MATCH (opcode, jalr_insn_16_bd32) != 0)
   13308     /* 16-bit branch/jump with a 32-bit delay slot.  */
   13309     bdsize = 4;
   13310   else if (MATCH (opcode, jalr_insn_16_bd16) != 0
   13311 	   || find_match (opcode, ds_insns_16_bd16) >= 0)
   13312     /* 16-bit branch/jump with a 16-bit delay slot.  */
   13313     bdsize = 2;
   13314   else
   13315     /* No delay slot.  */
   13316     bdsize = 0;
   13317 
   13318   return bdsize;
   13319 }
   13320 
   13321 /* If PTR points to what *might* be a 32-bit branch or jump, then
   13322    return the minimum length of its delay slot, otherwise return 0.
   13323    Non-zero results are not definitive as we might be checking against
   13324    the second half of another instruction.  */
   13325 
   13326 static int
   13327 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
   13328 {
   13329   unsigned long opcode;
   13330   int bdsize;
   13331 
   13332   opcode = bfd_get_micromips_32 (abfd, ptr);
   13333   if (find_match (opcode, ds_insns_32_bd32) >= 0)
   13334     /* 32-bit branch/jump with a 32-bit delay slot.  */
   13335     bdsize = 4;
   13336   else if (find_match (opcode, ds_insns_32_bd16) >= 0)
   13337     /* 32-bit branch/jump with a 16-bit delay slot.  */
   13338     bdsize = 2;
   13339   else
   13340     /* No delay slot.  */
   13341     bdsize = 0;
   13342 
   13343   return bdsize;
   13344 }
   13345 
   13346 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
   13347    that doesn't fiddle with REG, then return TRUE, otherwise FALSE.  */
   13348 
   13349 static bfd_boolean
   13350 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
   13351 {
   13352   unsigned long opcode;
   13353 
   13354   opcode = bfd_get_16 (abfd, ptr);
   13355   if (MATCH (opcode, b_insn_16)
   13356 						/* B16  */
   13357       || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
   13358 						/* JR16  */
   13359       || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
   13360 						/* BEQZ16, BNEZ16  */
   13361       || (MATCH (opcode, jalr_insn_16_bd32)
   13362 						/* JALR16  */
   13363 	  && reg != JR16_REG (opcode) && reg != RA))
   13364     return TRUE;
   13365 
   13366   return FALSE;
   13367 }
   13368 
   13369 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
   13370    then return TRUE, otherwise FALSE.  */
   13371 
   13372 static bfd_boolean
   13373 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
   13374 {
   13375   unsigned long opcode;
   13376 
   13377   opcode = bfd_get_micromips_32 (abfd, ptr);
   13378   if (MATCH (opcode, j_insn_32)
   13379 						/* J  */
   13380       || MATCH (opcode, bc_insn_32)
   13381 						/* BC1F, BC1T, BC2F, BC2T  */
   13382       || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
   13383 						/* JAL, JALX  */
   13384       || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
   13385 						/* BGEZ, BGTZ, BLEZ, BLTZ  */
   13386       || (MATCH (opcode, bzal_insn_32)
   13387 						/* BGEZAL, BLTZAL  */
   13388 	  && reg != OP32_SREG (opcode) && reg != RA)
   13389       || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
   13390 						/* JALR, JALR.HB, BEQ, BNE  */
   13391 	  && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
   13392     return TRUE;
   13393 
   13394   return FALSE;
   13395 }
   13396 
   13397 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
   13398    IRELEND) at OFFSET indicate that there must be a compact branch there,
   13399    then return TRUE, otherwise FALSE.  */
   13400 
   13401 static bfd_boolean
   13402 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
   13403 		     const Elf_Internal_Rela *internal_relocs,
   13404 		     const Elf_Internal_Rela *irelend)
   13405 {
   13406   const Elf_Internal_Rela *irel;
   13407   unsigned long opcode;
   13408 
   13409   opcode = bfd_get_micromips_32 (abfd, ptr);
   13410   if (find_match (opcode, bzc_insns_32) < 0)
   13411     return FALSE;
   13412 
   13413   for (irel = internal_relocs; irel < irelend; irel++)
   13414     if (irel->r_offset == offset
   13415 	&& ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
   13416       return TRUE;
   13417 
   13418   return FALSE;
   13419 }
   13420 
   13421 /* Bitsize checking.  */
   13422 #define IS_BITSIZE(val, N)						\
   13423   (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1)))		\
   13424     - (1ULL << ((N) - 1))) == (val))
   13425 
   13426 
   13427 bfd_boolean
   13429 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
   13430 			     struct bfd_link_info *link_info,
   13431 			     bfd_boolean *again)
   13432 {
   13433   bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
   13434   Elf_Internal_Shdr *symtab_hdr;
   13435   Elf_Internal_Rela *internal_relocs;
   13436   Elf_Internal_Rela *irel, *irelend;
   13437   bfd_byte *contents = NULL;
   13438   Elf_Internal_Sym *isymbuf = NULL;
   13439 
   13440   /* Assume nothing changes.  */
   13441   *again = FALSE;
   13442 
   13443   /* We don't have to do anything for a relocatable link, if
   13444      this section does not have relocs, or if this is not a
   13445      code section.  */
   13446 
   13447   if (link_info->relocatable
   13448       || (sec->flags & SEC_RELOC) == 0
   13449       || sec->reloc_count == 0
   13450       || (sec->flags & SEC_CODE) == 0)
   13451     return TRUE;
   13452 
   13453   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13454 
   13455   /* Get a copy of the native relocations.  */
   13456   internal_relocs = (_bfd_elf_link_read_relocs
   13457 		     (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
   13458 		      link_info->keep_memory));
   13459   if (internal_relocs == NULL)
   13460     goto error_return;
   13461 
   13462   /* Walk through them looking for relaxing opportunities.  */
   13463   irelend = internal_relocs + sec->reloc_count;
   13464   for (irel = internal_relocs; irel < irelend; irel++)
   13465     {
   13466       unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
   13467       unsigned int r_type = ELF32_R_TYPE (irel->r_info);
   13468       bfd_boolean target_is_micromips_code_p;
   13469       unsigned long opcode;
   13470       bfd_vma symval;
   13471       bfd_vma pcrval;
   13472       bfd_byte *ptr;
   13473       int fndopc;
   13474 
   13475       /* The number of bytes to delete for relaxation and from where
   13476          to delete these bytes starting at irel->r_offset.  */
   13477       int delcnt = 0;
   13478       int deloff = 0;
   13479 
   13480       /* If this isn't something that can be relaxed, then ignore
   13481          this reloc.  */
   13482       if (r_type != R_MICROMIPS_HI16
   13483 	  && r_type != R_MICROMIPS_PC16_S1
   13484 	  && r_type != R_MICROMIPS_26_S1)
   13485 	continue;
   13486 
   13487       /* Get the section contents if we haven't done so already.  */
   13488       if (contents == NULL)
   13489 	{
   13490 	  /* Get cached copy if it exists.  */
   13491 	  if (elf_section_data (sec)->this_hdr.contents != NULL)
   13492 	    contents = elf_section_data (sec)->this_hdr.contents;
   13493 	  /* Go get them off disk.  */
   13494 	  else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
   13495 	    goto error_return;
   13496 	}
   13497       ptr = contents + irel->r_offset;
   13498 
   13499       /* Read this BFD's local symbols if we haven't done so already.  */
   13500       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
   13501 	{
   13502 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   13503 	  if (isymbuf == NULL)
   13504 	    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   13505 					    symtab_hdr->sh_info, 0,
   13506 					    NULL, NULL, NULL);
   13507 	  if (isymbuf == NULL)
   13508 	    goto error_return;
   13509 	}
   13510 
   13511       /* Get the value of the symbol referred to by the reloc.  */
   13512       if (r_symndx < symtab_hdr->sh_info)
   13513 	{
   13514 	  /* A local symbol.  */
   13515 	  Elf_Internal_Sym *isym;
   13516 	  asection *sym_sec;
   13517 
   13518 	  isym = isymbuf + r_symndx;
   13519 	  if (isym->st_shndx == SHN_UNDEF)
   13520 	    sym_sec = bfd_und_section_ptr;
   13521 	  else if (isym->st_shndx == SHN_ABS)
   13522 	    sym_sec = bfd_abs_section_ptr;
   13523 	  else if (isym->st_shndx == SHN_COMMON)
   13524 	    sym_sec = bfd_com_section_ptr;
   13525 	  else
   13526 	    sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   13527 	  symval = (isym->st_value
   13528 		    + sym_sec->output_section->vma
   13529 		    + sym_sec->output_offset);
   13530 	  target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
   13531 	}
   13532       else
   13533 	{
   13534 	  unsigned long indx;
   13535 	  struct elf_link_hash_entry *h;
   13536 
   13537 	  /* An external symbol.  */
   13538 	  indx = r_symndx - symtab_hdr->sh_info;
   13539 	  h = elf_sym_hashes (abfd)[indx];
   13540 	  BFD_ASSERT (h != NULL);
   13541 
   13542 	  if (h->root.type != bfd_link_hash_defined
   13543 	      && h->root.type != bfd_link_hash_defweak)
   13544 	    /* This appears to be a reference to an undefined
   13545 	       symbol.  Just ignore it -- it will be caught by the
   13546 	       regular reloc processing.  */
   13547 	    continue;
   13548 
   13549 	  symval = (h->root.u.def.value
   13550 		    + h->root.u.def.section->output_section->vma
   13551 		    + h->root.u.def.section->output_offset);
   13552 	  target_is_micromips_code_p = (!h->needs_plt
   13553 					&& ELF_ST_IS_MICROMIPS (h->other));
   13554 	}
   13555 
   13556 
   13557       /* For simplicity of coding, we are going to modify the
   13558          section contents, the section relocs, and the BFD symbol
   13559          table.  We must tell the rest of the code not to free up this
   13560          information.  It would be possible to instead create a table
   13561          of changes which have to be made, as is done in coff-mips.c;
   13562          that would be more work, but would require less memory when
   13563          the linker is run.  */
   13564 
   13565       /* Only 32-bit instructions relaxed.  */
   13566       if (irel->r_offset + 4 > sec->size)
   13567 	continue;
   13568 
   13569       opcode = bfd_get_micromips_32 (abfd, ptr);
   13570 
   13571       /* This is the pc-relative distance from the instruction the
   13572          relocation is applied to, to the symbol referred.  */
   13573       pcrval = (symval
   13574 		- (sec->output_section->vma + sec->output_offset)
   13575 		- irel->r_offset);
   13576 
   13577       /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
   13578          of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
   13579          R_MICROMIPS_PC23_S2.  The R_MICROMIPS_PC23_S2 condition is
   13580 
   13581            (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
   13582 
   13583          where pcrval has first to be adjusted to apply against the LO16
   13584          location (we make the adjustment later on, when we have figured
   13585          out the offset).  */
   13586       if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
   13587 	{
   13588 	  bfd_boolean bzc = FALSE;
   13589 	  unsigned long nextopc;
   13590 	  unsigned long reg;
   13591 	  bfd_vma offset;
   13592 
   13593 	  /* Give up if the previous reloc was a HI16 against this symbol
   13594 	     too.  */
   13595 	  if (irel > internal_relocs
   13596 	      && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
   13597 	      && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
   13598 	    continue;
   13599 
   13600 	  /* Or if the next reloc is not a LO16 against this symbol.  */
   13601 	  if (irel + 1 >= irelend
   13602 	      || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
   13603 	      || ELF32_R_SYM (irel[1].r_info) != r_symndx)
   13604 	    continue;
   13605 
   13606 	  /* Or if the second next reloc is a LO16 against this symbol too.  */
   13607 	  if (irel + 2 >= irelend
   13608 	      && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
   13609 	      && ELF32_R_SYM (irel[2].r_info) == r_symndx)
   13610 	    continue;
   13611 
   13612 	  /* See if the LUI instruction *might* be in a branch delay slot.
   13613 	     We check whether what looks like a 16-bit branch or jump is
   13614 	     actually an immediate argument to a compact branch, and let
   13615 	     it through if so.  */
   13616 	  if (irel->r_offset >= 2
   13617 	      && check_br16_dslot (abfd, ptr - 2)
   13618 	      && !(irel->r_offset >= 4
   13619 		   && (bzc = check_relocated_bzc (abfd,
   13620 						  ptr - 4, irel->r_offset - 4,
   13621 						  internal_relocs, irelend))))
   13622 	    continue;
   13623 	  if (irel->r_offset >= 4
   13624 	      && !bzc
   13625 	      && check_br32_dslot (abfd, ptr - 4))
   13626 	    continue;
   13627 
   13628 	  reg = OP32_SREG (opcode);
   13629 
   13630 	  /* We only relax adjacent instructions or ones separated with
   13631 	     a branch or jump that has a delay slot.  The branch or jump
   13632 	     must not fiddle with the register used to hold the address.
   13633 	     Subtract 4 for the LUI itself.  */
   13634 	  offset = irel[1].r_offset - irel[0].r_offset;
   13635 	  switch (offset - 4)
   13636 	    {
   13637 	    case 0:
   13638 	      break;
   13639 	    case 2:
   13640 	      if (check_br16 (abfd, ptr + 4, reg))
   13641 		break;
   13642 	      continue;
   13643 	    case 4:
   13644 	      if (check_br32 (abfd, ptr + 4, reg))
   13645 		break;
   13646 	      continue;
   13647 	    default:
   13648 	      continue;
   13649 	    }
   13650 
   13651 	  nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
   13652 
   13653 	  /* Give up unless the same register is used with both
   13654 	     relocations.  */
   13655 	  if (OP32_SREG (nextopc) != reg)
   13656 	    continue;
   13657 
   13658 	  /* Now adjust pcrval, subtracting the offset to the LO16 reloc
   13659 	     and rounding up to take masking of the two LSBs into account.  */
   13660 	  pcrval = ((pcrval - offset + 3) | 3) ^ 3;
   13661 
   13662 	  /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16.  */
   13663 	  if (IS_BITSIZE (symval, 16))
   13664 	    {
   13665 	      /* Fix the relocation's type.  */
   13666 	      irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
   13667 
   13668 	      /* Instructions using R_MICROMIPS_LO16 have the base or
   13669 	         source register in bits 20:16.  This register becomes $0
   13670 	         (zero) as the result of the R_MICROMIPS_HI16 being 0.  */
   13671 	      nextopc &= ~0x001f0000;
   13672 	      bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
   13673 			  contents + irel[1].r_offset);
   13674 	    }
   13675 
   13676 	  /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
   13677 	     We add 4 to take LUI deletion into account while checking
   13678 	     the PC-relative distance.  */
   13679 	  else if (symval % 4 == 0
   13680 		   && IS_BITSIZE (pcrval + 4, 25)
   13681 		   && MATCH (nextopc, addiu_insn)
   13682 		   && OP32_TREG (nextopc) == OP32_SREG (nextopc)
   13683 		   && OP16_VALID_REG (OP32_TREG (nextopc)))
   13684 	    {
   13685 	      /* Fix the relocation's type.  */
   13686 	      irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
   13687 
   13688 	      /* Replace ADDIU with the ADDIUPC version.  */
   13689 	      nextopc = (addiupc_insn.match
   13690 			 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
   13691 
   13692 	      bfd_put_micromips_32 (abfd, nextopc,
   13693 				    contents + irel[1].r_offset);
   13694 	    }
   13695 
   13696 	  /* Can't do anything, give up, sigh...  */
   13697 	  else
   13698 	    continue;
   13699 
   13700 	  /* Fix the relocation's type.  */
   13701 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
   13702 
   13703 	  /* Delete the LUI instruction: 4 bytes at irel->r_offset.  */
   13704 	  delcnt = 4;
   13705 	  deloff = 0;
   13706 	}
   13707 
   13708       /* Compact branch relaxation -- due to the multitude of macros
   13709          employed by the compiler/assembler, compact branches are not
   13710          always generated.  Obviously, this can/will be fixed elsewhere,
   13711          but there is no drawback in double checking it here.  */
   13712       else if (r_type == R_MICROMIPS_PC16_S1
   13713 	       && irel->r_offset + 5 < sec->size
   13714 	       && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
   13715 		   || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
   13716 	       && ((!insn32
   13717 		    && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
   13718 					nop_insn_16) ? 2 : 0))
   13719 		   || (irel->r_offset + 7 < sec->size
   13720 		       && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
   13721 								 ptr + 4),
   13722 					   nop_insn_32) ? 4 : 0))))
   13723 	{
   13724 	  unsigned long reg;
   13725 
   13726 	  reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
   13727 
   13728 	  /* Replace BEQZ/BNEZ with the compact version.  */
   13729 	  opcode = (bzc_insns_32[fndopc].match
   13730 		    | BZC32_REG_FIELD (reg)
   13731 		    | (opcode & 0xffff));		/* Addend value.  */
   13732 
   13733 	  bfd_put_micromips_32 (abfd, opcode, ptr);
   13734 
   13735 	  /* Delete the delay slot NOP: two or four bytes from
   13736 	     irel->offset + 4; delcnt has already been set above.  */
   13737 	  deloff = 4;
   13738 	}
   13739 
   13740       /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1.  We need
   13741          to check the distance from the next instruction, so subtract 2.  */
   13742       else if (!insn32
   13743 	       && r_type == R_MICROMIPS_PC16_S1
   13744 	       && IS_BITSIZE (pcrval - 2, 11)
   13745 	       && find_match (opcode, b_insns_32) >= 0)
   13746 	{
   13747 	  /* Fix the relocation's type.  */
   13748 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
   13749 
   13750 	  /* Replace the 32-bit opcode with a 16-bit opcode.  */
   13751 	  bfd_put_16 (abfd,
   13752 		      (b_insn_16.match
   13753 		       | (opcode & 0x3ff)),		/* Addend value.  */
   13754 		      ptr);
   13755 
   13756 	  /* Delete 2 bytes from irel->r_offset + 2.  */
   13757 	  delcnt = 2;
   13758 	  deloff = 2;
   13759 	}
   13760 
   13761       /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1.  We need
   13762          to check the distance from the next instruction, so subtract 2.  */
   13763       else if (!insn32
   13764 	       && r_type == R_MICROMIPS_PC16_S1
   13765 	       && IS_BITSIZE (pcrval - 2, 8)
   13766 	       && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
   13767 		    && OP16_VALID_REG (OP32_SREG (opcode)))
   13768 		   || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
   13769 		       && OP16_VALID_REG (OP32_TREG (opcode)))))
   13770 	{
   13771 	  unsigned long reg;
   13772 
   13773 	  reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
   13774 
   13775 	  /* Fix the relocation's type.  */
   13776 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
   13777 
   13778 	  /* Replace the 32-bit opcode with a 16-bit opcode.  */
   13779 	  bfd_put_16 (abfd,
   13780 		      (bz_insns_16[fndopc].match
   13781 		       | BZ16_REG_FIELD (reg)
   13782 		       | (opcode & 0x7f)),		/* Addend value.  */
   13783 		      ptr);
   13784 
   13785 	  /* Delete 2 bytes from irel->r_offset + 2.  */
   13786 	  delcnt = 2;
   13787 	  deloff = 2;
   13788 	}
   13789 
   13790       /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets.  */
   13791       else if (!insn32
   13792 	       && r_type == R_MICROMIPS_26_S1
   13793 	       && target_is_micromips_code_p
   13794 	       && irel->r_offset + 7 < sec->size
   13795 	       && MATCH (opcode, jal_insn_32_bd32))
   13796 	{
   13797 	  unsigned long n32opc;
   13798 	  bfd_boolean relaxed = FALSE;
   13799 
   13800 	  n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
   13801 
   13802 	  if (MATCH (n32opc, nop_insn_32))
   13803 	    {
   13804 	      /* Replace delay slot 32-bit NOP with a 16-bit NOP.  */
   13805 	      bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
   13806 
   13807 	      relaxed = TRUE;
   13808 	    }
   13809 	  else if (find_match (n32opc, move_insns_32) >= 0)
   13810 	    {
   13811 	      /* Replace delay slot 32-bit MOVE with 16-bit MOVE.  */
   13812 	      bfd_put_16 (abfd,
   13813 			  (move_insn_16.match
   13814 			   | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
   13815 			   | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
   13816 			  ptr + 4);
   13817 
   13818 	      relaxed = TRUE;
   13819 	    }
   13820 	  /* Other 32-bit instructions relaxable to 16-bit
   13821 	     instructions will be handled here later.  */
   13822 
   13823 	  if (relaxed)
   13824 	    {
   13825 	      /* JAL with 32-bit delay slot that is changed to a JALS
   13826 	         with 16-bit delay slot.  */
   13827 	      bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
   13828 
   13829 	      /* Delete 2 bytes from irel->r_offset + 6.  */
   13830 	      delcnt = 2;
   13831 	      deloff = 6;
   13832 	    }
   13833 	}
   13834 
   13835       if (delcnt != 0)
   13836 	{
   13837 	  /* Note that we've changed the relocs, section contents, etc.  */
   13838 	  elf_section_data (sec)->relocs = internal_relocs;
   13839 	  elf_section_data (sec)->this_hdr.contents = contents;
   13840 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   13841 
   13842 	  /* Delete bytes depending on the delcnt and deloff.  */
   13843 	  if (!mips_elf_relax_delete_bytes (abfd, sec,
   13844 					    irel->r_offset + deloff, delcnt))
   13845 	    goto error_return;
   13846 
   13847 	  /* That will change things, so we should relax again.
   13848 	     Note that this is not required, and it may be slow.  */
   13849 	  *again = TRUE;
   13850 	}
   13851     }
   13852 
   13853   if (isymbuf != NULL
   13854       && symtab_hdr->contents != (unsigned char *) isymbuf)
   13855     {
   13856       if (! link_info->keep_memory)
   13857 	free (isymbuf);
   13858       else
   13859 	{
   13860 	  /* Cache the symbols for elf_link_input_bfd.  */
   13861 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   13862 	}
   13863     }
   13864 
   13865   if (contents != NULL
   13866       && elf_section_data (sec)->this_hdr.contents != contents)
   13867     {
   13868       if (! link_info->keep_memory)
   13869 	free (contents);
   13870       else
   13871 	{
   13872 	  /* Cache the section contents for elf_link_input_bfd.  */
   13873 	  elf_section_data (sec)->this_hdr.contents = contents;
   13874 	}
   13875     }
   13876 
   13877   if (internal_relocs != NULL
   13878       && elf_section_data (sec)->relocs != internal_relocs)
   13879     free (internal_relocs);
   13880 
   13881   return TRUE;
   13882 
   13883  error_return:
   13884   if (isymbuf != NULL
   13885       && symtab_hdr->contents != (unsigned char *) isymbuf)
   13886     free (isymbuf);
   13887   if (contents != NULL
   13888       && elf_section_data (sec)->this_hdr.contents != contents)
   13889     free (contents);
   13890   if (internal_relocs != NULL
   13891       && elf_section_data (sec)->relocs != internal_relocs)
   13892     free (internal_relocs);
   13893 
   13894   return FALSE;
   13895 }
   13896 
   13897 /* Create a MIPS ELF linker hash table.  */
   13899 
   13900 struct bfd_link_hash_table *
   13901 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
   13902 {
   13903   struct mips_elf_link_hash_table *ret;
   13904   bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
   13905 
   13906   ret = bfd_zmalloc (amt);
   13907   if (ret == NULL)
   13908     return NULL;
   13909 
   13910   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
   13911 				      mips_elf_link_hash_newfunc,
   13912 				      sizeof (struct mips_elf_link_hash_entry),
   13913 				      MIPS_ELF_DATA))
   13914     {
   13915       free (ret);
   13916       return NULL;
   13917     }
   13918   ret->root.init_plt_refcount.plist = NULL;
   13919   ret->root.init_plt_offset.plist = NULL;
   13920 
   13921   return &ret->root.root;
   13922 }
   13923 
   13924 /* Likewise, but indicate that the target is VxWorks.  */
   13925 
   13926 struct bfd_link_hash_table *
   13927 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
   13928 {
   13929   struct bfd_link_hash_table *ret;
   13930 
   13931   ret = _bfd_mips_elf_link_hash_table_create (abfd);
   13932   if (ret)
   13933     {
   13934       struct mips_elf_link_hash_table *htab;
   13935 
   13936       htab = (struct mips_elf_link_hash_table *) ret;
   13937       htab->use_plts_and_copy_relocs = TRUE;
   13938       htab->is_vxworks = TRUE;
   13939     }
   13940   return ret;
   13941 }
   13942 
   13943 /* A function that the linker calls if we are allowed to use PLTs
   13944    and copy relocs.  */
   13945 
   13946 void
   13947 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
   13948 {
   13949   mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
   13950 }
   13951 
   13952 /* A function that the linker calls to select between all or only
   13953    32-bit microMIPS instructions.  */
   13954 
   13955 void
   13956 _bfd_mips_elf_insn32 (struct bfd_link_info *info, bfd_boolean on)
   13957 {
   13958   mips_elf_hash_table (info)->insn32 = on;
   13959 }
   13960 
   13961 void
   13962 _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bfd_boolean on)
   13963 {
   13964   mips_elf_hash_table (info)->compact_branches = on;
   13965 }
   13966 
   13967 
   13968 /* Return the .MIPS.abiflags value representing each ISA Extension.  */
   13970 
   13971 unsigned int
   13972 bfd_mips_isa_ext (bfd *abfd)
   13973 {
   13974   switch (bfd_get_mach (abfd))
   13975     {
   13976     case bfd_mach_mips3900:
   13977       return AFL_EXT_3900;
   13978     case bfd_mach_mips4010:
   13979       return AFL_EXT_4010;
   13980     case bfd_mach_mips4100:
   13981       return AFL_EXT_4100;
   13982     case bfd_mach_mips4111:
   13983       return AFL_EXT_4111;
   13984     case bfd_mach_mips4120:
   13985       return AFL_EXT_4120;
   13986     case bfd_mach_mips4650:
   13987       return AFL_EXT_4650;
   13988     case bfd_mach_mips5400:
   13989       return AFL_EXT_5400;
   13990     case bfd_mach_mips5500:
   13991       return AFL_EXT_5500;
   13992     case bfd_mach_mips5900:
   13993       return AFL_EXT_5900;
   13994     case bfd_mach_mips10000:
   13995       return AFL_EXT_10000;
   13996     case bfd_mach_mips_loongson_2e:
   13997       return AFL_EXT_LOONGSON_2E;
   13998     case bfd_mach_mips_loongson_2f:
   13999       return AFL_EXT_LOONGSON_2F;
   14000     case bfd_mach_mips_loongson_3a:
   14001       return AFL_EXT_LOONGSON_3A;
   14002     case bfd_mach_mips_sb1:
   14003       return AFL_EXT_SB1;
   14004     case bfd_mach_mips_octeon:
   14005       return AFL_EXT_OCTEON;
   14006     case bfd_mach_mips_octeonp:
   14007       return AFL_EXT_OCTEONP;
   14008     case bfd_mach_mips_octeon3:
   14009       return AFL_EXT_OCTEON3;
   14010     case bfd_mach_mips_octeon2:
   14011       return AFL_EXT_OCTEON2;
   14012     case bfd_mach_mips_xlr:
   14013       return AFL_EXT_XLR;
   14014     }
   14015   return 0;
   14016 }
   14017 
   14018 /* Update the isa_level, isa_rev, isa_ext fields of abiflags.  */
   14019 
   14020 static void
   14021 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
   14022 {
   14023   switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
   14024     {
   14025     case E_MIPS_ARCH_1:
   14026       abiflags->isa_level = 1;
   14027       abiflags->isa_rev = 0;
   14028       break;
   14029     case E_MIPS_ARCH_2:
   14030       abiflags->isa_level = 2;
   14031       abiflags->isa_rev = 0;
   14032       break;
   14033     case E_MIPS_ARCH_3:
   14034       abiflags->isa_level = 3;
   14035       abiflags->isa_rev = 0;
   14036       break;
   14037     case E_MIPS_ARCH_4:
   14038       abiflags->isa_level = 4;
   14039       abiflags->isa_rev = 0;
   14040       break;
   14041     case E_MIPS_ARCH_5:
   14042       abiflags->isa_level = 5;
   14043       abiflags->isa_rev = 0;
   14044       break;
   14045     case E_MIPS_ARCH_32:
   14046       abiflags->isa_level = 32;
   14047       abiflags->isa_rev = 1;
   14048       break;
   14049     case E_MIPS_ARCH_32R2:
   14050       abiflags->isa_level = 32;
   14051       /* Handle MIPS32r3 and MIPS32r5 which do not have a header flag.  */
   14052       if (abiflags->isa_rev < 2)
   14053 	abiflags->isa_rev = 2;
   14054       break;
   14055     case E_MIPS_ARCH_32R6:
   14056       abiflags->isa_level = 32;
   14057       abiflags->isa_rev = 6;
   14058       break;
   14059     case E_MIPS_ARCH_64:
   14060       abiflags->isa_level = 64;
   14061       abiflags->isa_rev = 1;
   14062       break;
   14063     case E_MIPS_ARCH_64R2:
   14064       /* Handle MIPS64r3 and MIPS64r5 which do not have a header flag.  */
   14065       abiflags->isa_level = 64;
   14066       if (abiflags->isa_rev < 2)
   14067 	abiflags->isa_rev = 2;
   14068       break;
   14069     case E_MIPS_ARCH_64R6:
   14070       abiflags->isa_level = 64;
   14071       abiflags->isa_rev = 6;
   14072       break;
   14073     default:
   14074       (*_bfd_error_handler)
   14075 	(_("%B: Unknown architecture %s"),
   14076 	 abfd, bfd_printable_name (abfd));
   14077     }
   14078 
   14079   abiflags->isa_ext = bfd_mips_isa_ext (abfd);
   14080 }
   14081 
   14082 /* Return true if the given ELF header flags describe a 32-bit binary.  */
   14083 
   14084 static bfd_boolean
   14085 mips_32bit_flags_p (flagword flags)
   14086 {
   14087   return ((flags & EF_MIPS_32BITMODE) != 0
   14088 	  || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
   14089 	  || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
   14090 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
   14091 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
   14092 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
   14093 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
   14094 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
   14095 }
   14096 
   14097 /* Infer the content of the ABI flags based on the elf header.  */
   14098 
   14099 static void
   14100 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
   14101 {
   14102   obj_attribute *in_attr;
   14103 
   14104   memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
   14105   update_mips_abiflags_isa (abfd, abiflags);
   14106 
   14107   if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
   14108     abiflags->gpr_size = AFL_REG_32;
   14109   else
   14110     abiflags->gpr_size = AFL_REG_64;
   14111 
   14112   abiflags->cpr1_size = AFL_REG_NONE;
   14113 
   14114   in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
   14115   abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   14116 
   14117   if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
   14118       || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
   14119       || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
   14120 	  && abiflags->gpr_size == AFL_REG_32))
   14121     abiflags->cpr1_size = AFL_REG_32;
   14122   else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
   14123 	   || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
   14124 	   || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
   14125     abiflags->cpr1_size = AFL_REG_64;
   14126 
   14127   abiflags->cpr2_size = AFL_REG_NONE;
   14128 
   14129   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
   14130     abiflags->ases |= AFL_ASE_MDMX;
   14131   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
   14132     abiflags->ases |= AFL_ASE_MIPS16;
   14133   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
   14134     abiflags->ases |= AFL_ASE_MICROMIPS;
   14135 
   14136   if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
   14137       && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
   14138       && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
   14139       && abiflags->isa_level >= 32
   14140       && abiflags->isa_ext != AFL_EXT_LOONGSON_3A)
   14141     abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
   14142 }
   14143 
   14144 /* We need to use a special link routine to handle the .reginfo and
   14145    the .mdebug sections.  We need to merge all instances of these
   14146    sections together, not write them all out sequentially.  */
   14147 
   14148 bfd_boolean
   14149 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   14150 {
   14151   asection *o;
   14152   struct bfd_link_order *p;
   14153   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
   14154   asection *rtproc_sec, *abiflags_sec;
   14155   Elf32_RegInfo reginfo;
   14156   struct ecoff_debug_info debug;
   14157   struct mips_htab_traverse_info hti;
   14158   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14159   const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
   14160   HDRR *symhdr = &debug.symbolic_header;
   14161   void *mdebug_handle = NULL;
   14162   asection *s;
   14163   EXTR esym;
   14164   unsigned int i;
   14165   bfd_size_type amt;
   14166   struct mips_elf_link_hash_table *htab;
   14167 
   14168   static const char * const secname[] =
   14169   {
   14170     ".text", ".init", ".fini", ".data",
   14171     ".rodata", ".sdata", ".sbss", ".bss"
   14172   };
   14173   static const int sc[] =
   14174   {
   14175     scText, scInit, scFini, scData,
   14176     scRData, scSData, scSBss, scBss
   14177   };
   14178 
   14179   /* Sort the dynamic symbols so that those with GOT entries come after
   14180      those without.  */
   14181   htab = mips_elf_hash_table (info);
   14182   BFD_ASSERT (htab != NULL);
   14183 
   14184   if (!mips_elf_sort_hash_table (abfd, info))
   14185     return FALSE;
   14186 
   14187   /* Create any scheduled LA25 stubs.  */
   14188   hti.info = info;
   14189   hti.output_bfd = abfd;
   14190   hti.error = FALSE;
   14191   htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
   14192   if (hti.error)
   14193     return FALSE;
   14194 
   14195   /* Get a value for the GP register.  */
   14196   if (elf_gp (abfd) == 0)
   14197     {
   14198       struct bfd_link_hash_entry *h;
   14199 
   14200       h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
   14201       if (h != NULL && h->type == bfd_link_hash_defined)
   14202 	elf_gp (abfd) = (h->u.def.value
   14203 			 + h->u.def.section->output_section->vma
   14204 			 + h->u.def.section->output_offset);
   14205       else if (htab->is_vxworks
   14206 	       && (h = bfd_link_hash_lookup (info->hash,
   14207 					     "_GLOBAL_OFFSET_TABLE_",
   14208 					     FALSE, FALSE, TRUE))
   14209 	       && h->type == bfd_link_hash_defined)
   14210 	elf_gp (abfd) = (h->u.def.section->output_section->vma
   14211 			 + h->u.def.section->output_offset
   14212 			 + h->u.def.value);
   14213       else if (info->relocatable)
   14214 	{
   14215 	  bfd_vma lo = MINUS_ONE;
   14216 
   14217 	  /* Find the GP-relative section with the lowest offset.  */
   14218 	  for (o = abfd->sections; o != NULL; o = o->next)
   14219 	    if (o->vma < lo
   14220 		&& (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
   14221 	      lo = o->vma;
   14222 
   14223 	  /* And calculate GP relative to that.  */
   14224 	  elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
   14225 	}
   14226       else
   14227 	{
   14228 	  /* If the relocate_section function needs to do a reloc
   14229 	     involving the GP value, it should make a reloc_dangerous
   14230 	     callback to warn that GP is not defined.  */
   14231 	}
   14232     }
   14233 
   14234   /* Go through the sections and collect the .reginfo and .mdebug
   14235      information.  */
   14236   abiflags_sec = NULL;
   14237   reginfo_sec = NULL;
   14238   mdebug_sec = NULL;
   14239   gptab_data_sec = NULL;
   14240   gptab_bss_sec = NULL;
   14241   for (o = abfd->sections; o != NULL; o = o->next)
   14242     {
   14243       if (strcmp (o->name, ".MIPS.abiflags") == 0)
   14244 	{
   14245 	  /* We have found the .MIPS.abiflags section in the output file.
   14246 	     Look through all the link_orders comprising it and remove them.
   14247 	     The data is merged in _bfd_mips_elf_merge_private_bfd_data.  */
   14248 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   14249 	    {
   14250 	      asection *input_section;
   14251 
   14252 	      if (p->type != bfd_indirect_link_order)
   14253 		{
   14254 		  if (p->type == bfd_data_link_order)
   14255 		    continue;
   14256 		  abort ();
   14257 		}
   14258 
   14259 	      input_section = p->u.indirect.section;
   14260 
   14261 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   14262 		 elf_link_input_bfd ignores this section.  */
   14263 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   14264 	    }
   14265 
   14266 	  /* Size has been set in _bfd_mips_elf_always_size_sections.  */
   14267 	  BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
   14268 
   14269 	  /* Skip this section later on (I don't think this currently
   14270 	     matters, but someday it might).  */
   14271 	  o->map_head.link_order = NULL;
   14272 
   14273 	  abiflags_sec = o;
   14274 	}
   14275 
   14276       if (strcmp (o->name, ".reginfo") == 0)
   14277 	{
   14278 	  memset (&reginfo, 0, sizeof reginfo);
   14279 
   14280 	  /* We have found the .reginfo section in the output file.
   14281 	     Look through all the link_orders comprising it and merge
   14282 	     the information together.  */
   14283 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   14284 	    {
   14285 	      asection *input_section;
   14286 	      bfd *input_bfd;
   14287 	      Elf32_External_RegInfo ext;
   14288 	      Elf32_RegInfo sub;
   14289 
   14290 	      if (p->type != bfd_indirect_link_order)
   14291 		{
   14292 		  if (p->type == bfd_data_link_order)
   14293 		    continue;
   14294 		  abort ();
   14295 		}
   14296 
   14297 	      input_section = p->u.indirect.section;
   14298 	      input_bfd = input_section->owner;
   14299 
   14300 	      if (! bfd_get_section_contents (input_bfd, input_section,
   14301 					      &ext, 0, sizeof ext))
   14302 		return FALSE;
   14303 
   14304 	      bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
   14305 
   14306 	      reginfo.ri_gprmask |= sub.ri_gprmask;
   14307 	      reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
   14308 	      reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
   14309 	      reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
   14310 	      reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
   14311 
   14312 	      /* ri_gp_value is set by the function
   14313 		 mips_elf32_section_processing when the section is
   14314 		 finally written out.  */
   14315 
   14316 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   14317 		 elf_link_input_bfd ignores this section.  */
   14318 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   14319 	    }
   14320 
   14321 	  /* Size has been set in _bfd_mips_elf_always_size_sections.  */
   14322 	  BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
   14323 
   14324 	  /* Skip this section later on (I don't think this currently
   14325 	     matters, but someday it might).  */
   14326 	  o->map_head.link_order = NULL;
   14327 
   14328 	  reginfo_sec = o;
   14329 	}
   14330 
   14331       if (strcmp (o->name, ".mdebug") == 0)
   14332 	{
   14333 	  struct extsym_info einfo;
   14334 	  bfd_vma last;
   14335 
   14336 	  /* We have found the .mdebug section in the output file.
   14337 	     Look through all the link_orders comprising it and merge
   14338 	     the information together.  */
   14339 	  symhdr->magic = swap->sym_magic;
   14340 	  /* FIXME: What should the version stamp be?  */
   14341 	  symhdr->vstamp = 0;
   14342 	  symhdr->ilineMax = 0;
   14343 	  symhdr->cbLine = 0;
   14344 	  symhdr->idnMax = 0;
   14345 	  symhdr->ipdMax = 0;
   14346 	  symhdr->isymMax = 0;
   14347 	  symhdr->ioptMax = 0;
   14348 	  symhdr->iauxMax = 0;
   14349 	  symhdr->issMax = 0;
   14350 	  symhdr->issExtMax = 0;
   14351 	  symhdr->ifdMax = 0;
   14352 	  symhdr->crfd = 0;
   14353 	  symhdr->iextMax = 0;
   14354 
   14355 	  /* We accumulate the debugging information itself in the
   14356 	     debug_info structure.  */
   14357 	  debug.line = NULL;
   14358 	  debug.external_dnr = NULL;
   14359 	  debug.external_pdr = NULL;
   14360 	  debug.external_sym = NULL;
   14361 	  debug.external_opt = NULL;
   14362 	  debug.external_aux = NULL;
   14363 	  debug.ss = NULL;
   14364 	  debug.ssext = debug.ssext_end = NULL;
   14365 	  debug.external_fdr = NULL;
   14366 	  debug.external_rfd = NULL;
   14367 	  debug.external_ext = debug.external_ext_end = NULL;
   14368 
   14369 	  mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
   14370 	  if (mdebug_handle == NULL)
   14371 	    return FALSE;
   14372 
   14373 	  esym.jmptbl = 0;
   14374 	  esym.cobol_main = 0;
   14375 	  esym.weakext = 0;
   14376 	  esym.reserved = 0;
   14377 	  esym.ifd = ifdNil;
   14378 	  esym.asym.iss = issNil;
   14379 	  esym.asym.st = stLocal;
   14380 	  esym.asym.reserved = 0;
   14381 	  esym.asym.index = indexNil;
   14382 	  last = 0;
   14383 	  for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
   14384 	    {
   14385 	      esym.asym.sc = sc[i];
   14386 	      s = bfd_get_section_by_name (abfd, secname[i]);
   14387 	      if (s != NULL)
   14388 		{
   14389 		  esym.asym.value = s->vma;
   14390 		  last = s->vma + s->size;
   14391 		}
   14392 	      else
   14393 		esym.asym.value = last;
   14394 	      if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
   14395 						 secname[i], &esym))
   14396 		return FALSE;
   14397 	    }
   14398 
   14399 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   14400 	    {
   14401 	      asection *input_section;
   14402 	      bfd *input_bfd;
   14403 	      const struct ecoff_debug_swap *input_swap;
   14404 	      struct ecoff_debug_info input_debug;
   14405 	      char *eraw_src;
   14406 	      char *eraw_end;
   14407 
   14408 	      if (p->type != bfd_indirect_link_order)
   14409 		{
   14410 		  if (p->type == bfd_data_link_order)
   14411 		    continue;
   14412 		  abort ();
   14413 		}
   14414 
   14415 	      input_section = p->u.indirect.section;
   14416 	      input_bfd = input_section->owner;
   14417 
   14418 	      if (!is_mips_elf (input_bfd))
   14419 		{
   14420 		  /* I don't know what a non MIPS ELF bfd would be
   14421 		     doing with a .mdebug section, but I don't really
   14422 		     want to deal with it.  */
   14423 		  continue;
   14424 		}
   14425 
   14426 	      input_swap = (get_elf_backend_data (input_bfd)
   14427 			    ->elf_backend_ecoff_debug_swap);
   14428 
   14429 	      BFD_ASSERT (p->size == input_section->size);
   14430 
   14431 	      /* The ECOFF linking code expects that we have already
   14432 		 read in the debugging information and set up an
   14433 		 ecoff_debug_info structure, so we do that now.  */
   14434 	      if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
   14435 						   &input_debug))
   14436 		return FALSE;
   14437 
   14438 	      if (! (bfd_ecoff_debug_accumulate
   14439 		     (mdebug_handle, abfd, &debug, swap, input_bfd,
   14440 		      &input_debug, input_swap, info)))
   14441 		return FALSE;
   14442 
   14443 	      /* Loop through the external symbols.  For each one with
   14444 		 interesting information, try to find the symbol in
   14445 		 the linker global hash table and save the information
   14446 		 for the output external symbols.  */
   14447 	      eraw_src = input_debug.external_ext;
   14448 	      eraw_end = (eraw_src
   14449 			  + (input_debug.symbolic_header.iextMax
   14450 			     * input_swap->external_ext_size));
   14451 	      for (;
   14452 		   eraw_src < eraw_end;
   14453 		   eraw_src += input_swap->external_ext_size)
   14454 		{
   14455 		  EXTR ext;
   14456 		  const char *name;
   14457 		  struct mips_elf_link_hash_entry *h;
   14458 
   14459 		  (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
   14460 		  if (ext.asym.sc == scNil
   14461 		      || ext.asym.sc == scUndefined
   14462 		      || ext.asym.sc == scSUndefined)
   14463 		    continue;
   14464 
   14465 		  name = input_debug.ssext + ext.asym.iss;
   14466 		  h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
   14467 						 name, FALSE, FALSE, TRUE);
   14468 		  if (h == NULL || h->esym.ifd != -2)
   14469 		    continue;
   14470 
   14471 		  if (ext.ifd != -1)
   14472 		    {
   14473 		      BFD_ASSERT (ext.ifd
   14474 				  < input_debug.symbolic_header.ifdMax);
   14475 		      ext.ifd = input_debug.ifdmap[ext.ifd];
   14476 		    }
   14477 
   14478 		  h->esym = ext;
   14479 		}
   14480 
   14481 	      /* Free up the information we just read.  */
   14482 	      free (input_debug.line);
   14483 	      free (input_debug.external_dnr);
   14484 	      free (input_debug.external_pdr);
   14485 	      free (input_debug.external_sym);
   14486 	      free (input_debug.external_opt);
   14487 	      free (input_debug.external_aux);
   14488 	      free (input_debug.ss);
   14489 	      free (input_debug.ssext);
   14490 	      free (input_debug.external_fdr);
   14491 	      free (input_debug.external_rfd);
   14492 	      free (input_debug.external_ext);
   14493 
   14494 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   14495 		 elf_link_input_bfd ignores this section.  */
   14496 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   14497 	    }
   14498 
   14499 	  if (SGI_COMPAT (abfd) && info->shared)
   14500 	    {
   14501 	      /* Create .rtproc section.  */
   14502 	      rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
   14503 	      if (rtproc_sec == NULL)
   14504 		{
   14505 		  flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
   14506 				    | SEC_LINKER_CREATED | SEC_READONLY);
   14507 
   14508 		  rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
   14509 								   ".rtproc",
   14510 								   flags);
   14511 		  if (rtproc_sec == NULL
   14512 		      || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
   14513 		    return FALSE;
   14514 		}
   14515 
   14516 	      if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
   14517 						     info, rtproc_sec,
   14518 						     &debug))
   14519 		return FALSE;
   14520 	    }
   14521 
   14522 	  /* Build the external symbol information.  */
   14523 	  einfo.abfd = abfd;
   14524 	  einfo.info = info;
   14525 	  einfo.debug = &debug;
   14526 	  einfo.swap = swap;
   14527 	  einfo.failed = FALSE;
   14528 	  mips_elf_link_hash_traverse (mips_elf_hash_table (info),
   14529 				       mips_elf_output_extsym, &einfo);
   14530 	  if (einfo.failed)
   14531 	    return FALSE;
   14532 
   14533 	  /* Set the size of the .mdebug section.  */
   14534 	  o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
   14535 
   14536 	  /* Skip this section later on (I don't think this currently
   14537 	     matters, but someday it might).  */
   14538 	  o->map_head.link_order = NULL;
   14539 
   14540 	  mdebug_sec = o;
   14541 	}
   14542 
   14543       if (CONST_STRNEQ (o->name, ".gptab."))
   14544 	{
   14545 	  const char *subname;
   14546 	  unsigned int c;
   14547 	  Elf32_gptab *tab;
   14548 	  Elf32_External_gptab *ext_tab;
   14549 	  unsigned int j;
   14550 
   14551 	  /* The .gptab.sdata and .gptab.sbss sections hold
   14552 	     information describing how the small data area would
   14553 	     change depending upon the -G switch.  These sections
   14554 	     not used in executables files.  */
   14555 	  if (! info->relocatable)
   14556 	    {
   14557 	      for (p = o->map_head.link_order; p != NULL; p = p->next)
   14558 		{
   14559 		  asection *input_section;
   14560 
   14561 		  if (p->type != bfd_indirect_link_order)
   14562 		    {
   14563 		      if (p->type == bfd_data_link_order)
   14564 			continue;
   14565 		      abort ();
   14566 		    }
   14567 
   14568 		  input_section = p->u.indirect.section;
   14569 
   14570 		  /* Hack: reset the SEC_HAS_CONTENTS flag so that
   14571 		     elf_link_input_bfd ignores this section.  */
   14572 		  input_section->flags &= ~SEC_HAS_CONTENTS;
   14573 		}
   14574 
   14575 	      /* Skip this section later on (I don't think this
   14576 		 currently matters, but someday it might).  */
   14577 	      o->map_head.link_order = NULL;
   14578 
   14579 	      /* Really remove the section.  */
   14580 	      bfd_section_list_remove (abfd, o);
   14581 	      --abfd->section_count;
   14582 
   14583 	      continue;
   14584 	    }
   14585 
   14586 	  /* There is one gptab for initialized data, and one for
   14587 	     uninitialized data.  */
   14588 	  if (strcmp (o->name, ".gptab.sdata") == 0)
   14589 	    gptab_data_sec = o;
   14590 	  else if (strcmp (o->name, ".gptab.sbss") == 0)
   14591 	    gptab_bss_sec = o;
   14592 	  else
   14593 	    {
   14594 	      (*_bfd_error_handler)
   14595 		(_("%s: illegal section name `%s'"),
   14596 		 bfd_get_filename (abfd), o->name);
   14597 	      bfd_set_error (bfd_error_nonrepresentable_section);
   14598 	      return FALSE;
   14599 	    }
   14600 
   14601 	  /* The linker script always combines .gptab.data and
   14602 	     .gptab.sdata into .gptab.sdata, and likewise for
   14603 	     .gptab.bss and .gptab.sbss.  It is possible that there is
   14604 	     no .sdata or .sbss section in the output file, in which
   14605 	     case we must change the name of the output section.  */
   14606 	  subname = o->name + sizeof ".gptab" - 1;
   14607 	  if (bfd_get_section_by_name (abfd, subname) == NULL)
   14608 	    {
   14609 	      if (o == gptab_data_sec)
   14610 		o->name = ".gptab.data";
   14611 	      else
   14612 		o->name = ".gptab.bss";
   14613 	      subname = o->name + sizeof ".gptab" - 1;
   14614 	      BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
   14615 	    }
   14616 
   14617 	  /* Set up the first entry.  */
   14618 	  c = 1;
   14619 	  amt = c * sizeof (Elf32_gptab);
   14620 	  tab = bfd_malloc (amt);
   14621 	  if (tab == NULL)
   14622 	    return FALSE;
   14623 	  tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
   14624 	  tab[0].gt_header.gt_unused = 0;
   14625 
   14626 	  /* Combine the input sections.  */
   14627 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   14628 	    {
   14629 	      asection *input_section;
   14630 	      bfd *input_bfd;
   14631 	      bfd_size_type size;
   14632 	      unsigned long last;
   14633 	      bfd_size_type gpentry;
   14634 
   14635 	      if (p->type != bfd_indirect_link_order)
   14636 		{
   14637 		  if (p->type == bfd_data_link_order)
   14638 		    continue;
   14639 		  abort ();
   14640 		}
   14641 
   14642 	      input_section = p->u.indirect.section;
   14643 	      input_bfd = input_section->owner;
   14644 
   14645 	      /* Combine the gptab entries for this input section one
   14646 		 by one.  We know that the input gptab entries are
   14647 		 sorted by ascending -G value.  */
   14648 	      size = input_section->size;
   14649 	      last = 0;
   14650 	      for (gpentry = sizeof (Elf32_External_gptab);
   14651 		   gpentry < size;
   14652 		   gpentry += sizeof (Elf32_External_gptab))
   14653 		{
   14654 		  Elf32_External_gptab ext_gptab;
   14655 		  Elf32_gptab int_gptab;
   14656 		  unsigned long val;
   14657 		  unsigned long add;
   14658 		  bfd_boolean exact;
   14659 		  unsigned int look;
   14660 
   14661 		  if (! (bfd_get_section_contents
   14662 			 (input_bfd, input_section, &ext_gptab, gpentry,
   14663 			  sizeof (Elf32_External_gptab))))
   14664 		    {
   14665 		      free (tab);
   14666 		      return FALSE;
   14667 		    }
   14668 
   14669 		  bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
   14670 						&int_gptab);
   14671 		  val = int_gptab.gt_entry.gt_g_value;
   14672 		  add = int_gptab.gt_entry.gt_bytes - last;
   14673 
   14674 		  exact = FALSE;
   14675 		  for (look = 1; look < c; look++)
   14676 		    {
   14677 		      if (tab[look].gt_entry.gt_g_value >= val)
   14678 			tab[look].gt_entry.gt_bytes += add;
   14679 
   14680 		      if (tab[look].gt_entry.gt_g_value == val)
   14681 			exact = TRUE;
   14682 		    }
   14683 
   14684 		  if (! exact)
   14685 		    {
   14686 		      Elf32_gptab *new_tab;
   14687 		      unsigned int max;
   14688 
   14689 		      /* We need a new table entry.  */
   14690 		      amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
   14691 		      new_tab = bfd_realloc (tab, amt);
   14692 		      if (new_tab == NULL)
   14693 			{
   14694 			  free (tab);
   14695 			  return FALSE;
   14696 			}
   14697 		      tab = new_tab;
   14698 		      tab[c].gt_entry.gt_g_value = val;
   14699 		      tab[c].gt_entry.gt_bytes = add;
   14700 
   14701 		      /* Merge in the size for the next smallest -G
   14702 			 value, since that will be implied by this new
   14703 			 value.  */
   14704 		      max = 0;
   14705 		      for (look = 1; look < c; look++)
   14706 			{
   14707 			  if (tab[look].gt_entry.gt_g_value < val
   14708 			      && (max == 0
   14709 				  || (tab[look].gt_entry.gt_g_value
   14710 				      > tab[max].gt_entry.gt_g_value)))
   14711 			    max = look;
   14712 			}
   14713 		      if (max != 0)
   14714 			tab[c].gt_entry.gt_bytes +=
   14715 			  tab[max].gt_entry.gt_bytes;
   14716 
   14717 		      ++c;
   14718 		    }
   14719 
   14720 		  last = int_gptab.gt_entry.gt_bytes;
   14721 		}
   14722 
   14723 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   14724 		 elf_link_input_bfd ignores this section.  */
   14725 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   14726 	    }
   14727 
   14728 	  /* The table must be sorted by -G value.  */
   14729 	  if (c > 2)
   14730 	    qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
   14731 
   14732 	  /* Swap out the table.  */
   14733 	  amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
   14734 	  ext_tab = bfd_alloc (abfd, amt);
   14735 	  if (ext_tab == NULL)
   14736 	    {
   14737 	      free (tab);
   14738 	      return FALSE;
   14739 	    }
   14740 
   14741 	  for (j = 0; j < c; j++)
   14742 	    bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
   14743 	  free (tab);
   14744 
   14745 	  o->size = c * sizeof (Elf32_External_gptab);
   14746 	  o->contents = (bfd_byte *) ext_tab;
   14747 
   14748 	  /* Skip this section later on (I don't think this currently
   14749 	     matters, but someday it might).  */
   14750 	  o->map_head.link_order = NULL;
   14751 	}
   14752     }
   14753 
   14754   /* Invoke the regular ELF backend linker to do all the work.  */
   14755   if (!bfd_elf_final_link (abfd, info))
   14756     return FALSE;
   14757 
   14758   /* Now write out the computed sections.  */
   14759 
   14760   if (abiflags_sec != NULL)
   14761     {
   14762       Elf_External_ABIFlags_v0 ext;
   14763       Elf_Internal_ABIFlags_v0 *abiflags;
   14764 
   14765       abiflags = &mips_elf_tdata (abfd)->abiflags;
   14766 
   14767       /* Set up the abiflags if no valid input sections were found.  */
   14768       if (!mips_elf_tdata (abfd)->abiflags_valid)
   14769 	{
   14770 	  infer_mips_abiflags (abfd, abiflags);
   14771 	  mips_elf_tdata (abfd)->abiflags_valid = TRUE;
   14772 	}
   14773       bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
   14774       if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
   14775 	return FALSE;
   14776     }
   14777 
   14778   if (reginfo_sec != NULL)
   14779     {
   14780       Elf32_External_RegInfo ext;
   14781 
   14782       bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
   14783       if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
   14784 	return FALSE;
   14785     }
   14786 
   14787   if (mdebug_sec != NULL)
   14788     {
   14789       BFD_ASSERT (abfd->output_has_begun);
   14790       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
   14791 					       swap, info,
   14792 					       mdebug_sec->filepos))
   14793 	return FALSE;
   14794 
   14795       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
   14796     }
   14797 
   14798   if (gptab_data_sec != NULL)
   14799     {
   14800       if (! bfd_set_section_contents (abfd, gptab_data_sec,
   14801 				      gptab_data_sec->contents,
   14802 				      0, gptab_data_sec->size))
   14803 	return FALSE;
   14804     }
   14805 
   14806   if (gptab_bss_sec != NULL)
   14807     {
   14808       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
   14809 				      gptab_bss_sec->contents,
   14810 				      0, gptab_bss_sec->size))
   14811 	return FALSE;
   14812     }
   14813 
   14814   if (SGI_COMPAT (abfd))
   14815     {
   14816       rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
   14817       if (rtproc_sec != NULL)
   14818 	{
   14819 	  if (! bfd_set_section_contents (abfd, rtproc_sec,
   14820 					  rtproc_sec->contents,
   14821 					  0, rtproc_sec->size))
   14822 	    return FALSE;
   14823 	}
   14824     }
   14825 
   14826   return TRUE;
   14827 }
   14828 
   14829 /* Structure for saying that BFD machine EXTENSION extends BASE.  */
   14831 
   14832 struct mips_mach_extension
   14833 {
   14834   unsigned long extension, base;
   14835 };
   14836 
   14837 
   14838 /* An array describing how BFD machines relate to one another.  The entries
   14839    are ordered topologically with MIPS I extensions listed last.  */
   14840 
   14841 static const struct mips_mach_extension mips_mach_extensions[] =
   14842 {
   14843   /* MIPS64r2 extensions.  */
   14844   { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
   14845   { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
   14846   { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
   14847   { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
   14848   { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64r2 },
   14849 
   14850   /* MIPS64 extensions.  */
   14851   { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
   14852   { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
   14853   { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
   14854 
   14855   /* MIPS V extensions.  */
   14856   { bfd_mach_mipsisa64, bfd_mach_mips5 },
   14857 
   14858   /* R10000 extensions.  */
   14859   { bfd_mach_mips12000, bfd_mach_mips10000 },
   14860   { bfd_mach_mips14000, bfd_mach_mips10000 },
   14861   { bfd_mach_mips16000, bfd_mach_mips10000 },
   14862 
   14863   /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
   14864      vr5400 ISA, but doesn't include the multimedia stuff.  It seems
   14865      better to allow vr5400 and vr5500 code to be merged anyway, since
   14866      many libraries will just use the core ISA.  Perhaps we could add
   14867      some sort of ASE flag if this ever proves a problem.  */
   14868   { bfd_mach_mips5500, bfd_mach_mips5400 },
   14869   { bfd_mach_mips5400, bfd_mach_mips5000 },
   14870 
   14871   /* MIPS IV extensions.  */
   14872   { bfd_mach_mips5, bfd_mach_mips8000 },
   14873   { bfd_mach_mips10000, bfd_mach_mips8000 },
   14874   { bfd_mach_mips5000, bfd_mach_mips8000 },
   14875   { bfd_mach_mips7000, bfd_mach_mips8000 },
   14876   { bfd_mach_mips9000, bfd_mach_mips8000 },
   14877 
   14878   /* VR4100 extensions.  */
   14879   { bfd_mach_mips4120, bfd_mach_mips4100 },
   14880   { bfd_mach_mips4111, bfd_mach_mips4100 },
   14881 
   14882   /* MIPS III extensions.  */
   14883   { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
   14884   { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
   14885   { bfd_mach_mips8000, bfd_mach_mips4000 },
   14886   { bfd_mach_mips4650, bfd_mach_mips4000 },
   14887   { bfd_mach_mips4600, bfd_mach_mips4000 },
   14888   { bfd_mach_mips4400, bfd_mach_mips4000 },
   14889   { bfd_mach_mips4300, bfd_mach_mips4000 },
   14890   { bfd_mach_mips4100, bfd_mach_mips4000 },
   14891   { bfd_mach_mips4010, bfd_mach_mips4000 },
   14892   { bfd_mach_mips5900, bfd_mach_mips4000 },
   14893 
   14894   /* MIPS32 extensions.  */
   14895   { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
   14896 
   14897   /* MIPS II extensions.  */
   14898   { bfd_mach_mips4000, bfd_mach_mips6000 },
   14899   { bfd_mach_mipsisa32, bfd_mach_mips6000 },
   14900 
   14901   /* MIPS I extensions.  */
   14902   { bfd_mach_mips6000, bfd_mach_mips3000 },
   14903   { bfd_mach_mips3900, bfd_mach_mips3000 }
   14904 };
   14905 
   14906 
   14907 /* Return true if bfd machine EXTENSION is an extension of machine BASE.  */
   14908 
   14909 static bfd_boolean
   14910 mips_mach_extends_p (unsigned long base, unsigned long extension)
   14911 {
   14912   size_t i;
   14913 
   14914   if (extension == base)
   14915     return TRUE;
   14916 
   14917   if (base == bfd_mach_mipsisa32
   14918       && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
   14919     return TRUE;
   14920 
   14921   if (base == bfd_mach_mipsisa32r2
   14922       && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
   14923     return TRUE;
   14924 
   14925   for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
   14926     if (extension == mips_mach_extensions[i].extension)
   14927       {
   14928 	extension = mips_mach_extensions[i].base;
   14929 	if (extension == base)
   14930 	  return TRUE;
   14931       }
   14932 
   14933   return FALSE;
   14934 }
   14935 
   14936 
   14937 /* Merge object attributes from IBFD into OBFD.  Raise an error if
   14938    there are conflicting attributes.  */
   14939 static bfd_boolean
   14940 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
   14941 {
   14942   obj_attribute *in_attr;
   14943   obj_attribute *out_attr;
   14944   bfd *abi_fp_bfd;
   14945   bfd *abi_msa_bfd;
   14946 
   14947   abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
   14948   in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   14949   if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
   14950     mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   14951 
   14952   abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
   14953   if (!abi_msa_bfd
   14954       && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
   14955     mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
   14956 
   14957   if (!elf_known_obj_attributes_proc (obfd)[0].i)
   14958     {
   14959       /* This is the first object.  Copy the attributes.  */
   14960       _bfd_elf_copy_obj_attributes (ibfd, obfd);
   14961 
   14962       /* Use the Tag_null value to indicate the attributes have been
   14963 	 initialized.  */
   14964       elf_known_obj_attributes_proc (obfd)[0].i = 1;
   14965 
   14966       return TRUE;
   14967     }
   14968 
   14969   /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
   14970      non-conflicting ones.  */
   14971   out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   14972   if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
   14973     {
   14974       int out_fp, in_fp;
   14975 
   14976       out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
   14977       in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   14978       out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
   14979       if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
   14980 	out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
   14981       else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
   14982 	       && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
   14983 		   || in_fp == Val_GNU_MIPS_ABI_FP_64
   14984 		   || in_fp == Val_GNU_MIPS_ABI_FP_64A))
   14985 	{
   14986 	  mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   14987 	  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   14988 	}
   14989       else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
   14990 	       && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
   14991 		   || out_fp == Val_GNU_MIPS_ABI_FP_64
   14992 		   || out_fp == Val_GNU_MIPS_ABI_FP_64A))
   14993 	/* Keep the current setting.  */;
   14994       else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
   14995 	       && in_fp == Val_GNU_MIPS_ABI_FP_64)
   14996 	{
   14997 	  mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   14998 	  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   14999 	}
   15000       else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
   15001 	       && out_fp == Val_GNU_MIPS_ABI_FP_64)
   15002 	/* Keep the current setting.  */;
   15003       else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
   15004 	{
   15005 	  const char *out_string, *in_string;
   15006 
   15007 	  out_string = _bfd_mips_fp_abi_string (out_fp);
   15008 	  in_string = _bfd_mips_fp_abi_string (in_fp);
   15009 	  /* First warn about cases involving unrecognised ABIs.  */
   15010 	  if (!out_string && !in_string)
   15011 	    _bfd_error_handler
   15012 	      (_("Warning: %B uses unknown floating point ABI %d "
   15013 		 "(set by %B), %B uses unknown floating point ABI %d"),
   15014 	       obfd, abi_fp_bfd, ibfd, out_fp, in_fp);
   15015 	  else if (!out_string)
   15016 	    _bfd_error_handler
   15017 	      (_("Warning: %B uses unknown floating point ABI %d "
   15018 		 "(set by %B), %B uses %s"),
   15019 	       obfd, abi_fp_bfd, ibfd, out_fp, in_string);
   15020 	  else if (!in_string)
   15021 	    _bfd_error_handler
   15022 	      (_("Warning: %B uses %s (set by %B), "
   15023 		 "%B uses unknown floating point ABI %d"),
   15024 	       obfd, abi_fp_bfd, ibfd, out_string, in_fp);
   15025 	  else
   15026 	    {
   15027 	      /* If one of the bfds is soft-float, the other must be
   15028 		 hard-float.  The exact choice of hard-float ABI isn't
   15029 		 really relevant to the error message.  */
   15030 	      if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
   15031 		out_string = "-mhard-float";
   15032 	      else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
   15033 		in_string = "-mhard-float";
   15034 	      _bfd_error_handler
   15035 		(_("Warning: %B uses %s (set by %B), %B uses %s"),
   15036 		 obfd, abi_fp_bfd, ibfd, out_string, in_string);
   15037 	    }
   15038 	}
   15039     }
   15040 
   15041   /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
   15042      non-conflicting ones.  */
   15043   if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
   15044     {
   15045       out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
   15046       if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
   15047 	out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
   15048       else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
   15049 	switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
   15050 	  {
   15051 	  case Val_GNU_MIPS_ABI_MSA_128:
   15052 	    _bfd_error_handler
   15053 	      (_("Warning: %B uses %s (set by %B), "
   15054 		 "%B uses unknown MSA ABI %d"),
   15055 	       obfd, abi_msa_bfd, ibfd,
   15056 	       "-mmsa", in_attr[Tag_GNU_MIPS_ABI_MSA].i);
   15057 	    break;
   15058 
   15059 	  default:
   15060 	    switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
   15061 	      {
   15062 	      case Val_GNU_MIPS_ABI_MSA_128:
   15063 		_bfd_error_handler
   15064 		  (_("Warning: %B uses unknown MSA ABI %d "
   15065 		     "(set by %B), %B uses %s"),
   15066 		     obfd, abi_msa_bfd, ibfd,
   15067 		     out_attr[Tag_GNU_MIPS_ABI_MSA].i, "-mmsa");
   15068 		  break;
   15069 
   15070 	      default:
   15071 		_bfd_error_handler
   15072 		  (_("Warning: %B uses unknown MSA ABI %d "
   15073 		     "(set by %B), %B uses unknown MSA ABI %d"),
   15074 		   obfd, abi_msa_bfd, ibfd,
   15075 		   out_attr[Tag_GNU_MIPS_ABI_MSA].i,
   15076 		   in_attr[Tag_GNU_MIPS_ABI_MSA].i);
   15077 		break;
   15078 	      }
   15079 	  }
   15080     }
   15081 
   15082   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   15083   _bfd_elf_merge_object_attributes (ibfd, obfd);
   15084 
   15085   return TRUE;
   15086 }
   15087 
   15088 /* Merge backend specific data from an object file to the output
   15089    object file when linking.  */
   15090 
   15091 bfd_boolean
   15092 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   15093 {
   15094   flagword old_flags;
   15095   flagword new_flags;
   15096   bfd_boolean ok;
   15097   bfd_boolean null_input_bfd = TRUE;
   15098   asection *sec;
   15099   obj_attribute *out_attr;
   15100 
   15101   /* Check if we have the same endianness.  */
   15102   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
   15103     {
   15104       (*_bfd_error_handler)
   15105 	(_("%B: endianness incompatible with that of the selected emulation"),
   15106 	 ibfd);
   15107       return FALSE;
   15108     }
   15109 
   15110   if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
   15111     return TRUE;
   15112 
   15113   if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
   15114     {
   15115       (*_bfd_error_handler)
   15116 	(_("%B: ABI is incompatible with that of the selected emulation"),
   15117 	 ibfd);
   15118       return FALSE;
   15119     }
   15120 
   15121   /* Set up the FP ABI attribute from the abiflags if it is not already
   15122      set.  */
   15123   if (mips_elf_tdata (ibfd)->abiflags_valid)
   15124     {
   15125       obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   15126       if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
   15127         in_attr[Tag_GNU_MIPS_ABI_FP].i =
   15128 	  mips_elf_tdata (ibfd)->abiflags.fp_abi;
   15129     }
   15130 
   15131   if (!mips_elf_merge_obj_attributes (ibfd, obfd))
   15132     return FALSE;
   15133 
   15134   /* Check to see if the input BFD actually contains any sections.
   15135      If not, its flags may not have been initialised either, but it cannot
   15136      actually cause any incompatibility.  */
   15137   for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   15138     {
   15139       /* Ignore synthetic sections and empty .text, .data and .bss sections
   15140 	 which are automatically generated by gas.  Also ignore fake
   15141 	 (s)common sections, since merely defining a common symbol does
   15142 	 not affect compatibility.  */
   15143       if ((sec->flags & SEC_IS_COMMON) == 0
   15144 	  && strcmp (sec->name, ".reginfo")
   15145 	  && strcmp (sec->name, ".mdebug")
   15146 	  && (sec->size != 0
   15147 	      || (strcmp (sec->name, ".text")
   15148 		  && strcmp (sec->name, ".data")
   15149 		  && strcmp (sec->name, ".bss"))))
   15150 	{
   15151 	  null_input_bfd = FALSE;
   15152 	  break;
   15153 	}
   15154     }
   15155   if (null_input_bfd)
   15156     return TRUE;
   15157 
   15158   /* Populate abiflags using existing information.  */
   15159   if (!mips_elf_tdata (ibfd)->abiflags_valid)
   15160     {
   15161       infer_mips_abiflags (ibfd, &mips_elf_tdata (ibfd)->abiflags);
   15162       mips_elf_tdata (ibfd)->abiflags_valid = TRUE;
   15163     }
   15164   else
   15165     {
   15166       Elf_Internal_ABIFlags_v0 abiflags;
   15167       Elf_Internal_ABIFlags_v0 in_abiflags;
   15168       infer_mips_abiflags (ibfd, &abiflags);
   15169       in_abiflags = mips_elf_tdata (ibfd)->abiflags;
   15170 
   15171       /* It is not possible to infer the correct ISA revision
   15172          for R3 or R5 so drop down to R2 for the checks.  */
   15173       if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
   15174 	in_abiflags.isa_rev = 2;
   15175 
   15176       if (in_abiflags.isa_level != abiflags.isa_level
   15177 	  || in_abiflags.isa_rev != abiflags.isa_rev
   15178 	  || in_abiflags.isa_ext != abiflags.isa_ext)
   15179 	(*_bfd_error_handler)
   15180 	  (_("%B: warning: Inconsistent ISA between e_flags and "
   15181 	     ".MIPS.abiflags"), ibfd);
   15182       if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
   15183 	  && in_abiflags.fp_abi != abiflags.fp_abi)
   15184 	(*_bfd_error_handler)
   15185 	  (_("%B: warning: Inconsistent FP ABI between e_flags and "
   15186 	     ".MIPS.abiflags"), ibfd);
   15187       if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
   15188 	(*_bfd_error_handler)
   15189 	  (_("%B: warning: Inconsistent ASEs between e_flags and "
   15190 	     ".MIPS.abiflags"), ibfd);
   15191       if (in_abiflags.isa_ext != abiflags.isa_ext)
   15192 	(*_bfd_error_handler)
   15193 	  (_("%B: warning: Inconsistent ISA extensions between e_flags and "
   15194 	     ".MIPS.abiflags"), ibfd);
   15195       if (in_abiflags.flags2 != 0)
   15196 	(*_bfd_error_handler)
   15197 	  (_("%B: warning: Unexpected flag in the flags2 field of "
   15198 	     ".MIPS.abiflags (0x%lx)"), ibfd,
   15199 	   (unsigned long) in_abiflags.flags2);
   15200     }
   15201 
   15202   if (!mips_elf_tdata (obfd)->abiflags_valid)
   15203     {
   15204       /* Copy input abiflags if output abiflags are not already valid.  */
   15205       mips_elf_tdata (obfd)->abiflags = mips_elf_tdata (ibfd)->abiflags;
   15206       mips_elf_tdata (obfd)->abiflags_valid = TRUE;
   15207     }
   15208 
   15209   if (! elf_flags_init (obfd))
   15210     {
   15211       elf_flags_init (obfd) = TRUE;
   15212       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   15213       elf_elfheader (obfd)->e_ident[EI_CLASS]
   15214 	= elf_elfheader (ibfd)->e_ident[EI_CLASS];
   15215 
   15216       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   15217 	  && (bfd_get_arch_info (obfd)->the_default
   15218 	      || mips_mach_extends_p (bfd_get_mach (obfd),
   15219 				      bfd_get_mach (ibfd))))
   15220 	{
   15221 	  if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
   15222 				   bfd_get_mach (ibfd)))
   15223 	    return FALSE;
   15224 
   15225 	  /* Update the ABI flags isa_level, isa_rev and isa_ext fields.  */
   15226 	  update_mips_abiflags_isa (obfd, &mips_elf_tdata (obfd)->abiflags);
   15227 	}
   15228 
   15229       return TRUE;
   15230     }
   15231 
   15232   /* Update the output abiflags fp_abi using the computed fp_abi.  */
   15233   out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   15234   mips_elf_tdata (obfd)->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
   15235 
   15236 #define max(a,b) ((a) > (b) ? (a) : (b))
   15237   /* Merge abiflags.  */
   15238   mips_elf_tdata (obfd)->abiflags.isa_rev
   15239     = max (mips_elf_tdata (obfd)->abiflags.isa_rev,
   15240 	   mips_elf_tdata (ibfd)->abiflags.isa_rev);
   15241   mips_elf_tdata (obfd)->abiflags.gpr_size
   15242     = max (mips_elf_tdata (obfd)->abiflags.gpr_size,
   15243 	   mips_elf_tdata (ibfd)->abiflags.gpr_size);
   15244   mips_elf_tdata (obfd)->abiflags.cpr1_size
   15245     = max (mips_elf_tdata (obfd)->abiflags.cpr1_size,
   15246 	   mips_elf_tdata (ibfd)->abiflags.cpr1_size);
   15247   mips_elf_tdata (obfd)->abiflags.cpr2_size
   15248     = max (mips_elf_tdata (obfd)->abiflags.cpr2_size,
   15249 	   mips_elf_tdata (ibfd)->abiflags.cpr2_size);
   15250 #undef max
   15251   mips_elf_tdata (obfd)->abiflags.ases
   15252     |= mips_elf_tdata (ibfd)->abiflags.ases;
   15253   mips_elf_tdata (obfd)->abiflags.flags1
   15254     |= mips_elf_tdata (ibfd)->abiflags.flags1;
   15255 
   15256   new_flags = elf_elfheader (ibfd)->e_flags;
   15257   elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
   15258   old_flags = elf_elfheader (obfd)->e_flags;
   15259 
   15260   /* Check flag compatibility.  */
   15261 
   15262   new_flags &= ~EF_MIPS_NOREORDER;
   15263   old_flags &= ~EF_MIPS_NOREORDER;
   15264 
   15265   /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
   15266      doesn't seem to matter.  */
   15267   new_flags &= ~EF_MIPS_XGOT;
   15268   old_flags &= ~EF_MIPS_XGOT;
   15269 
   15270   /* MIPSpro generates ucode info in n64 objects.  Again, we should
   15271      just be able to ignore this.  */
   15272   new_flags &= ~EF_MIPS_UCODE;
   15273   old_flags &= ~EF_MIPS_UCODE;
   15274 
   15275   /* DSOs should only be linked with CPIC code.  */
   15276   if ((ibfd->flags & DYNAMIC) != 0)
   15277     new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
   15278 
   15279   if (new_flags == old_flags)
   15280     return TRUE;
   15281 
   15282   ok = TRUE;
   15283 
   15284   if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
   15285       != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
   15286     {
   15287       (*_bfd_error_handler)
   15288 	(_("%B: warning: linking abicalls files with non-abicalls files"),
   15289 	 ibfd);
   15290       ok = TRUE;
   15291     }
   15292 
   15293   if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
   15294     elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
   15295   if (! (new_flags & EF_MIPS_PIC))
   15296     elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
   15297 
   15298   new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
   15299   old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
   15300 
   15301   /* Compare the ISAs.  */
   15302   if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
   15303     {
   15304       (*_bfd_error_handler)
   15305 	(_("%B: linking 32-bit code with 64-bit code"),
   15306 	 ibfd);
   15307       ok = FALSE;
   15308     }
   15309   else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
   15310     {
   15311       /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
   15312       if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
   15313 	{
   15314 	  /* Copy the architecture info from IBFD to OBFD.  Also copy
   15315 	     the 32-bit flag (if set) so that we continue to recognise
   15316 	     OBFD as a 32-bit binary.  */
   15317 	  bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
   15318 	  elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
   15319 	  elf_elfheader (obfd)->e_flags
   15320 	    |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15321 
   15322 	  /* Update the ABI flags isa_level, isa_rev, isa_ext fields.  */
   15323 	  update_mips_abiflags_isa (obfd, &mips_elf_tdata (obfd)->abiflags);
   15324 
   15325 	  /* Copy across the ABI flags if OBFD doesn't use them
   15326 	     and if that was what caused us to treat IBFD as 32-bit.  */
   15327 	  if ((old_flags & EF_MIPS_ABI) == 0
   15328 	      && mips_32bit_flags_p (new_flags)
   15329 	      && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
   15330 	    elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
   15331 	}
   15332       else
   15333 	{
   15334 	  /* The ISAs aren't compatible.  */
   15335 	  (*_bfd_error_handler)
   15336 	    (_("%B: linking %s module with previous %s modules"),
   15337 	     ibfd,
   15338 	     bfd_printable_name (ibfd),
   15339 	     bfd_printable_name (obfd));
   15340 	  ok = FALSE;
   15341 	}
   15342     }
   15343 
   15344   new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15345   old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15346 
   15347   /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
   15348      does set EI_CLASS differently from any 32-bit ABI.  */
   15349   if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
   15350       || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   15351 	  != elf_elfheader (obfd)->e_ident[EI_CLASS]))
   15352     {
   15353       /* Only error if both are set (to different values).  */
   15354       if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
   15355 	  || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   15356 	      != elf_elfheader (obfd)->e_ident[EI_CLASS]))
   15357 	{
   15358 	  (*_bfd_error_handler)
   15359 	    (_("%B: ABI mismatch: linking %s module with previous %s modules"),
   15360 	     ibfd,
   15361 	     elf_mips_abi_name (ibfd),
   15362 	     elf_mips_abi_name (obfd));
   15363 	  ok = FALSE;
   15364 	}
   15365       new_flags &= ~EF_MIPS_ABI;
   15366       old_flags &= ~EF_MIPS_ABI;
   15367     }
   15368 
   15369   /* Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
   15370      and allow arbitrary mixing of the remaining ASEs (retain the union).  */
   15371   if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
   15372     {
   15373       int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
   15374       int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
   15375       int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
   15376       int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
   15377       int micro_mis = old_m16 && new_micro;
   15378       int m16_mis = old_micro && new_m16;
   15379 
   15380       if (m16_mis || micro_mis)
   15381 	{
   15382 	  (*_bfd_error_handler)
   15383 	    (_("%B: ASE mismatch: linking %s module with previous %s modules"),
   15384 	     ibfd,
   15385 	     m16_mis ? "MIPS16" : "microMIPS",
   15386 	     m16_mis ? "microMIPS" : "MIPS16");
   15387 	  ok = FALSE;
   15388 	}
   15389 
   15390       elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
   15391 
   15392       new_flags &= ~ EF_MIPS_ARCH_ASE;
   15393       old_flags &= ~ EF_MIPS_ARCH_ASE;
   15394     }
   15395 
   15396   /* Compare NaN encodings.  */
   15397   if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
   15398     {
   15399       _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
   15400 			  ibfd,
   15401 			  (new_flags & EF_MIPS_NAN2008
   15402 			   ? "-mnan=2008" : "-mnan=legacy"),
   15403 			  (old_flags & EF_MIPS_NAN2008
   15404 			   ? "-mnan=2008" : "-mnan=legacy"));
   15405       ok = FALSE;
   15406       new_flags &= ~EF_MIPS_NAN2008;
   15407       old_flags &= ~EF_MIPS_NAN2008;
   15408     }
   15409 
   15410   /* Compare FP64 state.  */
   15411   if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
   15412     {
   15413       _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
   15414 			  ibfd,
   15415 			  (new_flags & EF_MIPS_FP64
   15416 			   ? "-mfp64" : "-mfp32"),
   15417 			  (old_flags & EF_MIPS_FP64
   15418 			   ? "-mfp64" : "-mfp32"));
   15419       ok = FALSE;
   15420       new_flags &= ~EF_MIPS_FP64;
   15421       old_flags &= ~EF_MIPS_FP64;
   15422     }
   15423 
   15424   /* Warn about any other mismatches */
   15425   if (new_flags != old_flags)
   15426     {
   15427       (*_bfd_error_handler)
   15428 	(_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
   15429 	 ibfd, (unsigned long) new_flags,
   15430 	 (unsigned long) old_flags);
   15431       ok = FALSE;
   15432     }
   15433 
   15434   if (! ok)
   15435     {
   15436       bfd_set_error (bfd_error_bad_value);
   15437       return FALSE;
   15438     }
   15439 
   15440   return TRUE;
   15441 }
   15442 
   15443 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
   15444 
   15445 bfd_boolean
   15446 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
   15447 {
   15448   BFD_ASSERT (!elf_flags_init (abfd)
   15449 	      || elf_elfheader (abfd)->e_flags == flags);
   15450 
   15451   elf_elfheader (abfd)->e_flags = flags;
   15452   elf_flags_init (abfd) = TRUE;
   15453   return TRUE;
   15454 }
   15455 
   15456 char *
   15457 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
   15458 {
   15459   switch (dtag)
   15460     {
   15461     default: return "";
   15462     case DT_MIPS_RLD_VERSION:
   15463       return "MIPS_RLD_VERSION";
   15464     case DT_MIPS_TIME_STAMP:
   15465       return "MIPS_TIME_STAMP";
   15466     case DT_MIPS_ICHECKSUM:
   15467       return "MIPS_ICHECKSUM";
   15468     case DT_MIPS_IVERSION:
   15469       return "MIPS_IVERSION";
   15470     case DT_MIPS_FLAGS:
   15471       return "MIPS_FLAGS";
   15472     case DT_MIPS_BASE_ADDRESS:
   15473       return "MIPS_BASE_ADDRESS";
   15474     case DT_MIPS_MSYM:
   15475       return "MIPS_MSYM";
   15476     case DT_MIPS_CONFLICT:
   15477       return "MIPS_CONFLICT";
   15478     case DT_MIPS_LIBLIST:
   15479       return "MIPS_LIBLIST";
   15480     case DT_MIPS_LOCAL_GOTNO:
   15481       return "MIPS_LOCAL_GOTNO";
   15482     case DT_MIPS_CONFLICTNO:
   15483       return "MIPS_CONFLICTNO";
   15484     case DT_MIPS_LIBLISTNO:
   15485       return "MIPS_LIBLISTNO";
   15486     case DT_MIPS_SYMTABNO:
   15487       return "MIPS_SYMTABNO";
   15488     case DT_MIPS_UNREFEXTNO:
   15489       return "MIPS_UNREFEXTNO";
   15490     case DT_MIPS_GOTSYM:
   15491       return "MIPS_GOTSYM";
   15492     case DT_MIPS_HIPAGENO:
   15493       return "MIPS_HIPAGENO";
   15494     case DT_MIPS_RLD_MAP:
   15495       return "MIPS_RLD_MAP";
   15496     case DT_MIPS_RLD_MAP2:
   15497       return "MIPS_RLD_MAP2";
   15498     case DT_MIPS_DELTA_CLASS:
   15499       return "MIPS_DELTA_CLASS";
   15500     case DT_MIPS_DELTA_CLASS_NO:
   15501       return "MIPS_DELTA_CLASS_NO";
   15502     case DT_MIPS_DELTA_INSTANCE:
   15503       return "MIPS_DELTA_INSTANCE";
   15504     case DT_MIPS_DELTA_INSTANCE_NO:
   15505       return "MIPS_DELTA_INSTANCE_NO";
   15506     case DT_MIPS_DELTA_RELOC:
   15507       return "MIPS_DELTA_RELOC";
   15508     case DT_MIPS_DELTA_RELOC_NO:
   15509       return "MIPS_DELTA_RELOC_NO";
   15510     case DT_MIPS_DELTA_SYM:
   15511       return "MIPS_DELTA_SYM";
   15512     case DT_MIPS_DELTA_SYM_NO:
   15513       return "MIPS_DELTA_SYM_NO";
   15514     case DT_MIPS_DELTA_CLASSSYM:
   15515       return "MIPS_DELTA_CLASSSYM";
   15516     case DT_MIPS_DELTA_CLASSSYM_NO:
   15517       return "MIPS_DELTA_CLASSSYM_NO";
   15518     case DT_MIPS_CXX_FLAGS:
   15519       return "MIPS_CXX_FLAGS";
   15520     case DT_MIPS_PIXIE_INIT:
   15521       return "MIPS_PIXIE_INIT";
   15522     case DT_MIPS_SYMBOL_LIB:
   15523       return "MIPS_SYMBOL_LIB";
   15524     case DT_MIPS_LOCALPAGE_GOTIDX:
   15525       return "MIPS_LOCALPAGE_GOTIDX";
   15526     case DT_MIPS_LOCAL_GOTIDX:
   15527       return "MIPS_LOCAL_GOTIDX";
   15528     case DT_MIPS_HIDDEN_GOTIDX:
   15529       return "MIPS_HIDDEN_GOTIDX";
   15530     case DT_MIPS_PROTECTED_GOTIDX:
   15531       return "MIPS_PROTECTED_GOT_IDX";
   15532     case DT_MIPS_OPTIONS:
   15533       return "MIPS_OPTIONS";
   15534     case DT_MIPS_INTERFACE:
   15535       return "MIPS_INTERFACE";
   15536     case DT_MIPS_DYNSTR_ALIGN:
   15537       return "DT_MIPS_DYNSTR_ALIGN";
   15538     case DT_MIPS_INTERFACE_SIZE:
   15539       return "DT_MIPS_INTERFACE_SIZE";
   15540     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
   15541       return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
   15542     case DT_MIPS_PERF_SUFFIX:
   15543       return "DT_MIPS_PERF_SUFFIX";
   15544     case DT_MIPS_COMPACT_SIZE:
   15545       return "DT_MIPS_COMPACT_SIZE";
   15546     case DT_MIPS_GP_VALUE:
   15547       return "DT_MIPS_GP_VALUE";
   15548     case DT_MIPS_AUX_DYNAMIC:
   15549       return "DT_MIPS_AUX_DYNAMIC";
   15550     case DT_MIPS_PLTGOT:
   15551       return "DT_MIPS_PLTGOT";
   15552     case DT_MIPS_RWPLT:
   15553       return "DT_MIPS_RWPLT";
   15554     }
   15555 }
   15556 
   15557 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
   15558    not known.  */
   15559 
   15560 const char *
   15561 _bfd_mips_fp_abi_string (int fp)
   15562 {
   15563   switch (fp)
   15564     {
   15565       /* These strings aren't translated because they're simply
   15566 	 option lists.  */
   15567     case Val_GNU_MIPS_ABI_FP_DOUBLE:
   15568       return "-mdouble-float";
   15569 
   15570     case Val_GNU_MIPS_ABI_FP_SINGLE:
   15571       return "-msingle-float";
   15572 
   15573     case Val_GNU_MIPS_ABI_FP_SOFT:
   15574       return "-msoft-float";
   15575 
   15576     case Val_GNU_MIPS_ABI_FP_OLD_64:
   15577       return _("-mips32r2 -mfp64 (12 callee-saved)");
   15578 
   15579     case Val_GNU_MIPS_ABI_FP_XX:
   15580       return "-mfpxx";
   15581 
   15582     case Val_GNU_MIPS_ABI_FP_64:
   15583       return "-mgp32 -mfp64";
   15584 
   15585     case Val_GNU_MIPS_ABI_FP_64A:
   15586       return "-mgp32 -mfp64 -mno-odd-spreg";
   15587 
   15588     default:
   15589       return 0;
   15590     }
   15591 }
   15592 
   15593 static void
   15594 print_mips_ases (FILE *file, unsigned int mask)
   15595 {
   15596   if (mask & AFL_ASE_DSP)
   15597     fputs ("\n\tDSP ASE", file);
   15598   if (mask & AFL_ASE_DSPR2)
   15599     fputs ("\n\tDSP R2 ASE", file);
   15600   if (mask & AFL_ASE_DSPR6)
   15601     fputs ("\n\tDSP R6 ASE", file);
   15602   if (mask & AFL_ASE_EVA)
   15603     fputs ("\n\tEnhanced VA Scheme", file);
   15604   if (mask & AFL_ASE_MCU)
   15605     fputs ("\n\tMCU (MicroController) ASE", file);
   15606   if (mask & AFL_ASE_MDMX)
   15607     fputs ("\n\tMDMX ASE", file);
   15608   if (mask & AFL_ASE_MIPS3D)
   15609     fputs ("\n\tMIPS-3D ASE", file);
   15610   if (mask & AFL_ASE_MT)
   15611     fputs ("\n\tMT ASE", file);
   15612   if (mask & AFL_ASE_SMARTMIPS)
   15613     fputs ("\n\tSmartMIPS ASE", file);
   15614   if (mask & AFL_ASE_VIRT)
   15615     fputs ("\n\tVZ ASE", file);
   15616   if (mask & AFL_ASE_MSA)
   15617     fputs ("\n\tMSA ASE", file);
   15618   if (mask & AFL_ASE_MIPS16)
   15619     fputs ("\n\tMIPS16 ASE", file);
   15620   if (mask & AFL_ASE_MICROMIPS)
   15621     fputs ("\n\tMICROMIPS ASE", file);
   15622   if (mask & AFL_ASE_XPA)
   15623     fputs ("\n\tXPA ASE", file);
   15624   if (mask == 0)
   15625     fprintf (file, "\n\t%s", _("None"));
   15626   else if ((mask & ~AFL_ASE_MASK) != 0)
   15627     fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
   15628 }
   15629 
   15630 static void
   15631 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
   15632 {
   15633   switch (isa_ext)
   15634     {
   15635     case 0:
   15636       fputs (_("None"), file);
   15637       break;
   15638     case AFL_EXT_XLR:
   15639       fputs ("RMI XLR", file);
   15640       break;
   15641     case AFL_EXT_OCTEON3:
   15642       fputs ("Cavium Networks Octeon3", file);
   15643       break;
   15644     case AFL_EXT_OCTEON2:
   15645       fputs ("Cavium Networks Octeon2", file);
   15646       break;
   15647     case AFL_EXT_OCTEONP:
   15648       fputs ("Cavium Networks OcteonP", file);
   15649       break;
   15650     case AFL_EXT_LOONGSON_3A:
   15651       fputs ("Loongson 3A", file);
   15652       break;
   15653     case AFL_EXT_OCTEON:
   15654       fputs ("Cavium Networks Octeon", file);
   15655       break;
   15656     case AFL_EXT_5900:
   15657       fputs ("Toshiba R5900", file);
   15658       break;
   15659     case AFL_EXT_4650:
   15660       fputs ("MIPS R4650", file);
   15661       break;
   15662     case AFL_EXT_4010:
   15663       fputs ("LSI R4010", file);
   15664       break;
   15665     case AFL_EXT_4100:
   15666       fputs ("NEC VR4100", file);
   15667       break;
   15668     case AFL_EXT_3900:
   15669       fputs ("Toshiba R3900", file);
   15670       break;
   15671     case AFL_EXT_10000:
   15672       fputs ("MIPS R10000", file);
   15673       break;
   15674     case AFL_EXT_SB1:
   15675       fputs ("Broadcom SB-1", file);
   15676       break;
   15677     case AFL_EXT_4111:
   15678       fputs ("NEC VR4111/VR4181", file);
   15679       break;
   15680     case AFL_EXT_4120:
   15681       fputs ("NEC VR4120", file);
   15682       break;
   15683     case AFL_EXT_5400:
   15684       fputs ("NEC VR5400", file);
   15685       break;
   15686     case AFL_EXT_5500:
   15687       fputs ("NEC VR5500", file);
   15688       break;
   15689     case AFL_EXT_LOONGSON_2E:
   15690       fputs ("ST Microelectronics Loongson 2E", file);
   15691       break;
   15692     case AFL_EXT_LOONGSON_2F:
   15693       fputs ("ST Microelectronics Loongson 2F", file);
   15694       break;
   15695     default:
   15696       fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
   15697       break;
   15698     }
   15699 }
   15700 
   15701 static void
   15702 print_mips_fp_abi_value (FILE *file, int val)
   15703 {
   15704   switch (val)
   15705     {
   15706     case Val_GNU_MIPS_ABI_FP_ANY:
   15707       fprintf (file, _("Hard or soft float\n"));
   15708       break;
   15709     case Val_GNU_MIPS_ABI_FP_DOUBLE:
   15710       fprintf (file, _("Hard float (double precision)\n"));
   15711       break;
   15712     case Val_GNU_MIPS_ABI_FP_SINGLE:
   15713       fprintf (file, _("Hard float (single precision)\n"));
   15714       break;
   15715     case Val_GNU_MIPS_ABI_FP_SOFT:
   15716       fprintf (file, _("Soft float\n"));
   15717       break;
   15718     case Val_GNU_MIPS_ABI_FP_OLD_64:
   15719       fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
   15720       break;
   15721     case Val_GNU_MIPS_ABI_FP_XX:
   15722       fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
   15723       break;
   15724     case Val_GNU_MIPS_ABI_FP_64:
   15725       fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
   15726       break;
   15727     case Val_GNU_MIPS_ABI_FP_64A:
   15728       fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
   15729       break;
   15730     default:
   15731       fprintf (file, "??? (%d)\n", val);
   15732       break;
   15733     }
   15734 }
   15735 
   15736 static int
   15737 get_mips_reg_size (int reg_size)
   15738 {
   15739   return (reg_size == AFL_REG_NONE) ? 0
   15740 	 : (reg_size == AFL_REG_32) ? 32
   15741 	 : (reg_size == AFL_REG_64) ? 64
   15742 	 : (reg_size == AFL_REG_128) ? 128
   15743 	 : -1;
   15744 }
   15745 
   15746 bfd_boolean
   15747 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
   15748 {
   15749   FILE *file = ptr;
   15750 
   15751   BFD_ASSERT (abfd != NULL && ptr != NULL);
   15752 
   15753   /* Print normal ELF private data.  */
   15754   _bfd_elf_print_private_bfd_data (abfd, ptr);
   15755 
   15756   /* xgettext:c-format */
   15757   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
   15758 
   15759   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
   15760     fprintf (file, _(" [abi=O32]"));
   15761   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
   15762     fprintf (file, _(" [abi=O64]"));
   15763   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
   15764     fprintf (file, _(" [abi=EABI32]"));
   15765   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
   15766     fprintf (file, _(" [abi=EABI64]"));
   15767   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
   15768     fprintf (file, _(" [abi unknown]"));
   15769   else if (ABI_N32_P (abfd))
   15770     fprintf (file, _(" [abi=N32]"));
   15771   else if (ABI_64_P (abfd))
   15772     fprintf (file, _(" [abi=64]"));
   15773   else
   15774     fprintf (file, _(" [no abi set]"));
   15775 
   15776   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
   15777     fprintf (file, " [mips1]");
   15778   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
   15779     fprintf (file, " [mips2]");
   15780   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
   15781     fprintf (file, " [mips3]");
   15782   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
   15783     fprintf (file, " [mips4]");
   15784   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
   15785     fprintf (file, " [mips5]");
   15786   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
   15787     fprintf (file, " [mips32]");
   15788   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
   15789     fprintf (file, " [mips64]");
   15790   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
   15791     fprintf (file, " [mips32r2]");
   15792   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
   15793     fprintf (file, " [mips64r2]");
   15794   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
   15795     fprintf (file, " [mips32r6]");
   15796   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
   15797     fprintf (file, " [mips64r6]");
   15798   else
   15799     fprintf (file, _(" [unknown ISA]"));
   15800 
   15801   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
   15802     fprintf (file, " [mdmx]");
   15803 
   15804   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
   15805     fprintf (file, " [mips16]");
   15806 
   15807   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
   15808     fprintf (file, " [micromips]");
   15809 
   15810   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
   15811     fprintf (file, " [nan2008]");
   15812 
   15813   if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
   15814     fprintf (file, " [old fp64]");
   15815 
   15816   if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
   15817     fprintf (file, " [32bitmode]");
   15818   else
   15819     fprintf (file, _(" [not 32bitmode]"));
   15820 
   15821   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
   15822     fprintf (file, " [noreorder]");
   15823 
   15824   if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
   15825     fprintf (file, " [PIC]");
   15826 
   15827   if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
   15828     fprintf (file, " [CPIC]");
   15829 
   15830   if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
   15831     fprintf (file, " [XGOT]");
   15832 
   15833   if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
   15834     fprintf (file, " [UCODE]");
   15835 
   15836   fputc ('\n', file);
   15837 
   15838   if (mips_elf_tdata (abfd)->abiflags_valid)
   15839     {
   15840       Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
   15841       fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
   15842       fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
   15843       if (abiflags->isa_rev > 1)
   15844 	fprintf (file, "r%d", abiflags->isa_rev);
   15845       fprintf (file, "\nGPR size: %d",
   15846 	       get_mips_reg_size (abiflags->gpr_size));
   15847       fprintf (file, "\nCPR1 size: %d",
   15848 	       get_mips_reg_size (abiflags->cpr1_size));
   15849       fprintf (file, "\nCPR2 size: %d",
   15850 	       get_mips_reg_size (abiflags->cpr2_size));
   15851       fputs ("\nFP ABI: ", file);
   15852       print_mips_fp_abi_value (file, abiflags->fp_abi);
   15853       fputs ("ISA Extension: ", file);
   15854       print_mips_isa_ext (file, abiflags->isa_ext);
   15855       fputs ("\nASEs:", file);
   15856       print_mips_ases (file, abiflags->ases);
   15857       fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
   15858       fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
   15859       fputc ('\n', file);
   15860     }
   15861 
   15862   return TRUE;
   15863 }
   15864 
   15865 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
   15866 {
   15867   { STRING_COMMA_LEN (".lit4"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   15868   { STRING_COMMA_LEN (".lit8"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   15869   { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
   15870   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   15871   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   15872   { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
   15873   { NULL,                     0,  0, 0,              0 }
   15874 };
   15875 
   15876 /* Merge non visibility st_other attributes.  Ensure that the
   15877    STO_OPTIONAL flag is copied into h->other, even if this is not a
   15878    definiton of the symbol.  */
   15879 void
   15880 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
   15881 				      const Elf_Internal_Sym *isym,
   15882 				      bfd_boolean definition,
   15883 				      bfd_boolean dynamic ATTRIBUTE_UNUSED)
   15884 {
   15885   if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
   15886     {
   15887       unsigned char other;
   15888 
   15889       other = (definition ? isym->st_other : h->other);
   15890       other &= ~ELF_ST_VISIBILITY (-1);
   15891       h->other = other | ELF_ST_VISIBILITY (h->other);
   15892     }
   15893 
   15894   if (!definition
   15895       && ELF_MIPS_IS_OPTIONAL (isym->st_other))
   15896     h->other |= STO_OPTIONAL;
   15897 }
   15898 
   15899 /* Decide whether an undefined symbol is special and can be ignored.
   15900    This is the case for OPTIONAL symbols on IRIX.  */
   15901 bfd_boolean
   15902 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
   15903 {
   15904   return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
   15905 }
   15906 
   15907 bfd_boolean
   15908 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
   15909 {
   15910   return (sym->st_shndx == SHN_COMMON
   15911 	  || sym->st_shndx == SHN_MIPS_ACOMMON
   15912 	  || sym->st_shndx == SHN_MIPS_SCOMMON);
   15913 }
   15914 
   15915 /* Return address for Ith PLT stub in section PLT, for relocation REL
   15916    or (bfd_vma) -1 if it should not be included.  */
   15917 
   15918 bfd_vma
   15919 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
   15920 			   const arelent *rel ATTRIBUTE_UNUSED)
   15921 {
   15922   return (plt->vma
   15923 	  + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
   15924 	  + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
   15925 }
   15926 
   15927 /* Build a table of synthetic symbols to represent the PLT.  As with MIPS16
   15928    and microMIPS PLT slots we may have a many-to-one mapping between .plt
   15929    and .got.plt and also the slots may be of a different size each we walk
   15930    the PLT manually fetching instructions and matching them against known
   15931    patterns.  To make things easier standard MIPS slots, if any, always come
   15932    first.  As we don't create proper ELF symbols we use the UDATA.I member
   15933    of ASYMBOL to carry ISA annotation.  The encoding used is the same as
   15934    with the ST_OTHER member of the ELF symbol.  */
   15935 
   15936 long
   15937 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
   15938 				    long symcount ATTRIBUTE_UNUSED,
   15939 				    asymbol **syms ATTRIBUTE_UNUSED,
   15940 				    long dynsymcount, asymbol **dynsyms,
   15941 				    asymbol **ret)
   15942 {
   15943   static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
   15944   static const char microsuffix[] = "@micromipsplt";
   15945   static const char m16suffix[] = "@mips16plt";
   15946   static const char mipssuffix[] = "@plt";
   15947 
   15948   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   15949   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   15950   bfd_boolean micromips_p = MICROMIPS_P (abfd);
   15951   Elf_Internal_Shdr *hdr;
   15952   bfd_byte *plt_data;
   15953   bfd_vma plt_offset;
   15954   unsigned int other;
   15955   bfd_vma entry_size;
   15956   bfd_vma plt0_size;
   15957   asection *relplt;
   15958   bfd_vma opcode;
   15959   asection *plt;
   15960   asymbol *send;
   15961   size_t size;
   15962   char *names;
   15963   long counti;
   15964   arelent *p;
   15965   asymbol *s;
   15966   char *nend;
   15967   long count;
   15968   long pi;
   15969   long i;
   15970   long n;
   15971 
   15972   *ret = NULL;
   15973 
   15974   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
   15975     return 0;
   15976 
   15977   relplt = bfd_get_section_by_name (abfd, ".rel.plt");
   15978   if (relplt == NULL)
   15979     return 0;
   15980 
   15981   hdr = &elf_section_data (relplt)->this_hdr;
   15982   if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
   15983     return 0;
   15984 
   15985   plt = bfd_get_section_by_name (abfd, ".plt");
   15986   if (plt == NULL)
   15987     return 0;
   15988 
   15989   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   15990   if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
   15991     return -1;
   15992   p = relplt->relocation;
   15993 
   15994   /* Calculating the exact amount of space required for symbols would
   15995      require two passes over the PLT, so just pessimise assuming two
   15996      PLT slots per relocation.  */
   15997   count = relplt->size / hdr->sh_entsize;
   15998   counti = count * bed->s->int_rels_per_ext_rel;
   15999   size = 2 * count * sizeof (asymbol);
   16000   size += count * (sizeof (mipssuffix) +
   16001 		   (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
   16002   for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
   16003     size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
   16004 
   16005   /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too.  */
   16006   size += sizeof (asymbol) + sizeof (pltname);
   16007 
   16008   if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
   16009     return -1;
   16010 
   16011   if (plt->size < 16)
   16012     return -1;
   16013 
   16014   s = *ret = bfd_malloc (size);
   16015   if (s == NULL)
   16016     return -1;
   16017   send = s + 2 * count + 1;
   16018 
   16019   names = (char *) send;
   16020   nend = (char *) s + size;
   16021   n = 0;
   16022 
   16023   opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
   16024   if (opcode == 0x3302fffe)
   16025     {
   16026       if (!micromips_p)
   16027 	return -1;
   16028       plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
   16029       other = STO_MICROMIPS;
   16030     }
   16031   else if (opcode == 0x0398c1d0)
   16032     {
   16033       if (!micromips_p)
   16034 	return -1;
   16035       plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
   16036       other = STO_MICROMIPS;
   16037     }
   16038   else
   16039     {
   16040       plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
   16041       other = 0;
   16042     }
   16043 
   16044   s->the_bfd = abfd;
   16045   s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
   16046   s->section = plt;
   16047   s->value = 0;
   16048   s->name = names;
   16049   s->udata.i = other;
   16050   memcpy (names, pltname, sizeof (pltname));
   16051   names += sizeof (pltname);
   16052   ++s, ++n;
   16053 
   16054   pi = 0;
   16055   for (plt_offset = plt0_size;
   16056        plt_offset + 8 <= plt->size && s < send;
   16057        plt_offset += entry_size)
   16058     {
   16059       bfd_vma gotplt_addr;
   16060       const char *suffix;
   16061       bfd_vma gotplt_hi;
   16062       bfd_vma gotplt_lo;
   16063       size_t suffixlen;
   16064 
   16065       opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
   16066 
   16067       /* Check if the second word matches the expected MIPS16 instruction.  */
   16068       if (opcode == 0x651aeb00)
   16069 	{
   16070 	  if (micromips_p)
   16071 	    return -1;
   16072 	  /* Truncated table???  */
   16073 	  if (plt_offset + 16 > plt->size)
   16074 	    break;
   16075 	  gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
   16076 	  entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
   16077 	  suffixlen = sizeof (m16suffix);
   16078 	  suffix = m16suffix;
   16079 	  other = STO_MIPS16;
   16080 	}
   16081       /* Likewise the expected microMIPS instruction (no insn32 mode).  */
   16082       else if (opcode == 0xff220000)
   16083 	{
   16084 	  if (!micromips_p)
   16085 	    return -1;
   16086 	  gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
   16087 	  gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
   16088 	  gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
   16089 	  gotplt_lo <<= 2;
   16090 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16091 	  gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
   16092 	  entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
   16093 	  suffixlen = sizeof (microsuffix);
   16094 	  suffix = microsuffix;
   16095 	  other = STO_MICROMIPS;
   16096 	}
   16097       /* Likewise the expected microMIPS instruction (insn32 mode).  */
   16098       else if ((opcode & 0xffff0000) == 0xff2f0000)
   16099 	{
   16100 	  gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
   16101 	  gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
   16102 	  gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
   16103 	  gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
   16104 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16105 	  entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
   16106 	  suffixlen = sizeof (microsuffix);
   16107 	  suffix = microsuffix;
   16108 	  other = STO_MICROMIPS;
   16109 	}
   16110       /* Otherwise assume standard MIPS code.  */
   16111       else
   16112 	{
   16113 	  gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
   16114 	  gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
   16115 	  gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
   16116 	  gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
   16117 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16118 	  entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
   16119 	  suffixlen = sizeof (mipssuffix);
   16120 	  suffix = mipssuffix;
   16121 	  other = 0;
   16122 	}
   16123       /* Truncated table???  */
   16124       if (plt_offset + entry_size > plt->size)
   16125 	break;
   16126 
   16127       for (i = 0;
   16128 	   i < count && p[pi].address != gotplt_addr;
   16129 	   i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
   16130 
   16131       if (i < count)
   16132 	{
   16133 	  size_t namelen;
   16134 	  size_t len;
   16135 
   16136 	  *s = **p[pi].sym_ptr_ptr;
   16137 	  /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   16138 	     we are defining a symbol, ensure one of them is set.  */
   16139 	  if ((s->flags & BSF_LOCAL) == 0)
   16140 	    s->flags |= BSF_GLOBAL;
   16141 	  s->flags |= BSF_SYNTHETIC;
   16142 	  s->section = plt;
   16143 	  s->value = plt_offset;
   16144 	  s->name = names;
   16145 	  s->udata.i = other;
   16146 
   16147 	  len = strlen ((*p[pi].sym_ptr_ptr)->name);
   16148 	  namelen = len + suffixlen;
   16149 	  if (names + namelen > nend)
   16150 	    break;
   16151 
   16152 	  memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
   16153 	  names += len;
   16154 	  memcpy (names, suffix, suffixlen);
   16155 	  names += suffixlen;
   16156 
   16157 	  ++s, ++n;
   16158 	  pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
   16159 	}
   16160     }
   16161 
   16162   free (plt_data);
   16163 
   16164   return n;
   16165 }
   16166 
   16167 void
   16168 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
   16169 {
   16170   struct mips_elf_link_hash_table *htab;
   16171   Elf_Internal_Ehdr *i_ehdrp;
   16172 
   16173   i_ehdrp = elf_elfheader (abfd);
   16174   if (link_info)
   16175     {
   16176       htab = mips_elf_hash_table (link_info);
   16177       BFD_ASSERT (htab != NULL);
   16178 
   16179       if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
   16180 	i_ehdrp->e_ident[EI_ABIVERSION] = 1;
   16181     }
   16182 
   16183   _bfd_elf_post_process_headers (abfd, link_info);
   16184 
   16185   if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
   16186       || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
   16187     i_ehdrp->e_ident[EI_ABIVERSION] = 3;
   16188 }
   16189