Home | History | Annotate | Download | only in bfd
      1 /* MIPS-specific support for ELF
      2    Copyright (C) 1993-2016 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 #include "dwarf2.h"
     40 
     41 /* Get the ECOFF swapping routines.  */
     42 #include "coff/sym.h"
     43 #include "coff/symconst.h"
     44 #include "coff/ecoff.h"
     45 #include "coff/mips.h"
     46 
     47 #include "hashtab.h"
     48 
     49 /* Types of TLS GOT entry.  */
     50 enum mips_got_tls_type {
     51   GOT_TLS_NONE,
     52   GOT_TLS_GD,
     53   GOT_TLS_LDM,
     54   GOT_TLS_IE
     55 };
     56 
     57 /* This structure is used to hold information about one GOT entry.
     58    There are four types of entry:
     59 
     60       (1) an absolute address
     61 	    requires: abfd == NULL
     62 	    fields: d.address
     63 
     64       (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
     65 	    requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
     66 	    fields: abfd, symndx, d.addend, tls_type
     67 
     68       (3) a SYMBOL address, where SYMBOL is not local to an input bfd
     69 	    requires: abfd != NULL, symndx == -1
     70 	    fields: d.h, tls_type
     71 
     72       (4) a TLS LDM slot
     73 	    requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
     74 	    fields: none; there's only one of these per GOT.  */
     75 struct mips_got_entry
     76 {
     77   /* One input bfd that needs the GOT entry.  */
     78   bfd *abfd;
     79   /* The index of the symbol, as stored in the relocation r_info, if
     80      we have a local symbol; -1 otherwise.  */
     81   long symndx;
     82   union
     83   {
     84     /* If abfd == NULL, an address that must be stored in the got.  */
     85     bfd_vma address;
     86     /* If abfd != NULL && symndx != -1, the addend of the relocation
     87        that should be added to the symbol value.  */
     88     bfd_vma addend;
     89     /* If abfd != NULL && symndx == -1, the hash table entry
     90        corresponding to a symbol in the GOT.  The symbol's entry
     91        is in the local area if h->global_got_area is GGA_NONE,
     92        otherwise it is in the global area.  */
     93     struct mips_elf_link_hash_entry *h;
     94   } d;
     95 
     96   /* The TLS type of this GOT entry.  An LDM GOT entry will be a local
     97      symbol entry with r_symndx == 0.  */
     98   unsigned char tls_type;
     99 
    100   /* True if we have filled in the GOT contents for a TLS entry,
    101      and created the associated relocations.  */
    102   unsigned char tls_initialized;
    103 
    104   /* The offset from the beginning of the .got section to the entry
    105      corresponding to this symbol+addend.  If it's a global symbol
    106      whose offset is yet to be decided, it's going to be -1.  */
    107   long gotidx;
    108 };
    109 
    110 /* This structure represents a GOT page reference from an input bfd.
    111    Each instance represents a symbol + ADDEND, where the representation
    112    of the symbol depends on whether it is local to the input bfd.
    113    If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
    114    Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
    115 
    116    Page references with SYMNDX >= 0 always become page references
    117    in the output.  Page references with SYMNDX < 0 only become page
    118    references if the symbol binds locally; in other cases, the page
    119    reference decays to a global GOT reference.  */
    120 struct mips_got_page_ref
    121 {
    122   long symndx;
    123   union
    124   {
    125     struct mips_elf_link_hash_entry *h;
    126     bfd *abfd;
    127   } u;
    128   bfd_vma addend;
    129 };
    130 
    131 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
    132    The structures form a non-overlapping list that is sorted by increasing
    133    MIN_ADDEND.  */
    134 struct mips_got_page_range
    135 {
    136   struct mips_got_page_range *next;
    137   bfd_signed_vma min_addend;
    138   bfd_signed_vma max_addend;
    139 };
    140 
    141 /* This structure describes the range of addends that are applied to page
    142    relocations against a given section.  */
    143 struct mips_got_page_entry
    144 {
    145   /* The section that these entries are based on.  */
    146   asection *sec;
    147   /* The ranges for this page entry.  */
    148   struct mips_got_page_range *ranges;
    149   /* The maximum number of page entries needed for RANGES.  */
    150   bfd_vma num_pages;
    151 };
    152 
    153 /* This structure is used to hold .got information when linking.  */
    154 
    155 struct mips_got_info
    156 {
    157   /* The number of global .got entries.  */
    158   unsigned int global_gotno;
    159   /* The number of global .got entries that are in the GGA_RELOC_ONLY area.  */
    160   unsigned int reloc_only_gotno;
    161   /* The number of .got slots used for TLS.  */
    162   unsigned int tls_gotno;
    163   /* The first unused TLS .got entry.  Used only during
    164      mips_elf_initialize_tls_index.  */
    165   unsigned int tls_assigned_gotno;
    166   /* The number of local .got entries, eventually including page entries.  */
    167   unsigned int local_gotno;
    168   /* The maximum number of page entries needed.  */
    169   unsigned int page_gotno;
    170   /* The number of relocations needed for the GOT entries.  */
    171   unsigned int relocs;
    172   /* The first unused local .got entry.  */
    173   unsigned int assigned_low_gotno;
    174   /* The last unused local .got entry.  */
    175   unsigned int assigned_high_gotno;
    176   /* A hash table holding members of the got.  */
    177   struct htab *got_entries;
    178   /* A hash table holding mips_got_page_ref structures.  */
    179   struct htab *got_page_refs;
    180   /* A hash table of mips_got_page_entry structures.  */
    181   struct htab *got_page_entries;
    182   /* In multi-got links, a pointer to the next got (err, rather, most
    183      of the time, it points to the previous got).  */
    184   struct mips_got_info *next;
    185 };
    186 
    187 /* Structure passed when merging bfds' gots.  */
    188 
    189 struct mips_elf_got_per_bfd_arg
    190 {
    191   /* The output bfd.  */
    192   bfd *obfd;
    193   /* The link information.  */
    194   struct bfd_link_info *info;
    195   /* A pointer to the primary got, i.e., the one that's going to get
    196      the implicit relocations from DT_MIPS_LOCAL_GOTNO and
    197      DT_MIPS_GOTSYM.  */
    198   struct mips_got_info *primary;
    199   /* A non-primary got we're trying to merge with other input bfd's
    200      gots.  */
    201   struct mips_got_info *current;
    202   /* The maximum number of got entries that can be addressed with a
    203      16-bit offset.  */
    204   unsigned int max_count;
    205   /* The maximum number of page entries needed by each got.  */
    206   unsigned int max_pages;
    207   /* The total number of global entries which will live in the
    208      primary got and be automatically relocated.  This includes
    209      those not referenced by the primary GOT but included in
    210      the "master" GOT.  */
    211   unsigned int global_count;
    212 };
    213 
    214 /* A structure used to pass information to htab_traverse callbacks
    215    when laying out the GOT.  */
    216 
    217 struct mips_elf_traverse_got_arg
    218 {
    219   struct bfd_link_info *info;
    220   struct mips_got_info *g;
    221   int value;
    222 };
    223 
    224 struct _mips_elf_section_data
    225 {
    226   struct bfd_elf_section_data elf;
    227   union
    228   {
    229     bfd_byte *tdata;
    230   } u;
    231 };
    232 
    233 #define mips_elf_section_data(sec) \
    234   ((struct _mips_elf_section_data *) elf_section_data (sec))
    235 
    236 #define is_mips_elf(bfd)				\
    237   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
    238    && elf_tdata (bfd) != NULL				\
    239    && elf_object_id (bfd) == MIPS_ELF_DATA)
    240 
    241 /* The ABI says that every symbol used by dynamic relocations must have
    242    a global GOT entry.  Among other things, this provides the dynamic
    243    linker with a free, directly-indexed cache.  The GOT can therefore
    244    contain symbols that are not referenced by GOT relocations themselves
    245    (in other words, it may have symbols that are not referenced by things
    246    like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
    247 
    248    GOT relocations are less likely to overflow if we put the associated
    249    GOT entries towards the beginning.  We therefore divide the global
    250    GOT entries into two areas: "normal" and "reloc-only".  Entries in
    251    the first area can be used for both dynamic relocations and GP-relative
    252    accesses, while those in the "reloc-only" area are for dynamic
    253    relocations only.
    254 
    255    These GGA_* ("Global GOT Area") values are organised so that lower
    256    values are more general than higher values.  Also, non-GGA_NONE
    257    values are ordered by the position of the area in the GOT.  */
    258 #define GGA_NORMAL 0
    259 #define GGA_RELOC_ONLY 1
    260 #define GGA_NONE 2
    261 
    262 /* Information about a non-PIC interface to a PIC function.  There are
    263    two ways of creating these interfaces.  The first is to add:
    264 
    265 	lui	$25,%hi(func)
    266 	addiu	$25,$25,%lo(func)
    267 
    268    immediately before a PIC function "func".  The second is to add:
    269 
    270 	lui	$25,%hi(func)
    271 	j	func
    272 	addiu	$25,$25,%lo(func)
    273 
    274    to a separate trampoline section.
    275 
    276    Stubs of the first kind go in a new section immediately before the
    277    target function.  Stubs of the second kind go in a single section
    278    pointed to by the hash table's "strampoline" field.  */
    279 struct mips_elf_la25_stub {
    280   /* The generated section that contains this stub.  */
    281   asection *stub_section;
    282 
    283   /* The offset of the stub from the start of STUB_SECTION.  */
    284   bfd_vma offset;
    285 
    286   /* One symbol for the original function.  Its location is available
    287      in H->root.root.u.def.  */
    288   struct mips_elf_link_hash_entry *h;
    289 };
    290 
    291 /* Macros for populating a mips_elf_la25_stub.  */
    292 
    293 #define LA25_LUI(VAL) (0x3c190000 | (VAL))	/* lui t9,VAL */
    294 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
    295 #define LA25_ADDIU(VAL) (0x27390000 | (VAL))	/* addiu t9,t9,VAL */
    296 #define LA25_LUI_MICROMIPS(VAL)						\
    297   (0x41b90000 | (VAL))				/* lui t9,VAL */
    298 #define LA25_J_MICROMIPS(VAL)						\
    299   (0xd4000000 | (((VAL) >> 1) & 0x3ffffff))	/* j VAL */
    300 #define LA25_ADDIU_MICROMIPS(VAL)					\
    301   (0x33390000 | (VAL))				/* addiu t9,t9,VAL */
    302 
    303 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
    304    the dynamic symbols.  */
    305 
    306 struct mips_elf_hash_sort_data
    307 {
    308   /* The symbol in the global GOT with the lowest dynamic symbol table
    309      index.  */
    310   struct elf_link_hash_entry *low;
    311   /* The least dynamic symbol table index corresponding to a non-TLS
    312      symbol with a GOT entry.  */
    313   long min_got_dynindx;
    314   /* The greatest dynamic symbol table index corresponding to a symbol
    315      with a GOT entry that is not referenced (e.g., a dynamic symbol
    316      with dynamic relocations pointing to it from non-primary GOTs).  */
    317   long max_unref_got_dynindx;
    318   /* The greatest dynamic symbol table index not corresponding to a
    319      symbol without a GOT entry.  */
    320   long max_non_got_dynindx;
    321 };
    322 
    323 /* We make up to two PLT entries if needed, one for standard MIPS code
    324    and one for compressed code, either a MIPS16 or microMIPS one.  We
    325    keep a separate record of traditional lazy-binding stubs, for easier
    326    processing.  */
    327 
    328 struct plt_entry
    329 {
    330   /* Traditional SVR4 stub offset, or -1 if none.  */
    331   bfd_vma stub_offset;
    332 
    333   /* Standard PLT entry offset, or -1 if none.  */
    334   bfd_vma mips_offset;
    335 
    336   /* Compressed PLT entry offset, or -1 if none.  */
    337   bfd_vma comp_offset;
    338 
    339   /* The corresponding .got.plt index, or -1 if none.  */
    340   bfd_vma gotplt_index;
    341 
    342   /* Whether we need a standard PLT entry.  */
    343   unsigned int need_mips : 1;
    344 
    345   /* Whether we need a compressed PLT entry.  */
    346   unsigned int need_comp : 1;
    347 };
    348 
    349 /* The MIPS ELF linker needs additional information for each symbol in
    350    the global hash table.  */
    351 
    352 struct mips_elf_link_hash_entry
    353 {
    354   struct elf_link_hash_entry root;
    355 
    356   /* External symbol information.  */
    357   EXTR esym;
    358 
    359   /* The la25 stub we have created for ths symbol, if any.  */
    360   struct mips_elf_la25_stub *la25_stub;
    361 
    362   /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
    363      this symbol.  */
    364   unsigned int possibly_dynamic_relocs;
    365 
    366   /* If there is a stub that 32 bit functions should use to call this
    367      16 bit function, this points to the section containing the stub.  */
    368   asection *fn_stub;
    369 
    370   /* If there is a stub that 16 bit functions should use to call this
    371      32 bit function, this points to the section containing the stub.  */
    372   asection *call_stub;
    373 
    374   /* This is like the call_stub field, but it is used if the function
    375      being called returns a floating point value.  */
    376   asection *call_fp_stub;
    377 
    378   /* The highest GGA_* value that satisfies all references to this symbol.  */
    379   unsigned int global_got_area : 2;
    380 
    381   /* True if all GOT relocations against this symbol are for calls.  This is
    382      a looser condition than no_fn_stub below, because there may be other
    383      non-call non-GOT relocations against the symbol.  */
    384   unsigned int got_only_for_calls : 1;
    385 
    386   /* True if one of the relocations described by possibly_dynamic_relocs
    387      is against a readonly section.  */
    388   unsigned int readonly_reloc : 1;
    389 
    390   /* True if there is a relocation against this symbol that must be
    391      resolved by the static linker (in other words, if the relocation
    392      cannot possibly be made dynamic).  */
    393   unsigned int has_static_relocs : 1;
    394 
    395   /* True if we must not create a .MIPS.stubs entry for this symbol.
    396      This is set, for example, if there are relocations related to
    397      taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
    398      See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
    399   unsigned int no_fn_stub : 1;
    400 
    401   /* Whether we need the fn_stub; this is true if this symbol appears
    402      in any relocs other than a 16 bit call.  */
    403   unsigned int need_fn_stub : 1;
    404 
    405   /* True if this symbol is referenced by branch relocations from
    406      any non-PIC input file.  This is used to determine whether an
    407      la25 stub is required.  */
    408   unsigned int has_nonpic_branches : 1;
    409 
    410   /* Does this symbol need a traditional MIPS lazy-binding stub
    411      (as opposed to a PLT entry)?  */
    412   unsigned int needs_lazy_stub : 1;
    413 
    414   /* Does this symbol resolve to a PLT entry?  */
    415   unsigned int use_plt_entry : 1;
    416 };
    417 
    418 /* MIPS ELF linker hash table.  */
    419 
    420 struct mips_elf_link_hash_table
    421 {
    422   struct elf_link_hash_table root;
    423 
    424   /* The number of .rtproc entries.  */
    425   bfd_size_type procedure_count;
    426 
    427   /* The size of the .compact_rel section (if SGI_COMPAT).  */
    428   bfd_size_type compact_rel_size;
    429 
    430   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
    431      is set to the address of __rld_obj_head as in IRIX5 and IRIX6.  */
    432   bfd_boolean use_rld_obj_head;
    433 
    434   /* The  __rld_map or __rld_obj_head symbol. */
    435   struct elf_link_hash_entry *rld_symbol;
    436 
    437   /* This is set if we see any mips16 stub sections.  */
    438   bfd_boolean mips16_stubs_seen;
    439 
    440   /* True if we can generate copy relocs and PLTs.  */
    441   bfd_boolean use_plts_and_copy_relocs;
    442 
    443   /* True if we can only use 32-bit microMIPS instructions.  */
    444   bfd_boolean insn32;
    445 
    446   /* True if we're generating code for VxWorks.  */
    447   bfd_boolean is_vxworks;
    448 
    449   /* True if we already reported the small-data section overflow.  */
    450   bfd_boolean small_data_overflow_reported;
    451 
    452   /* Shortcuts to some dynamic sections, or NULL if they are not
    453      being used.  */
    454   asection *srelbss;
    455   asection *sdynbss;
    456   asection *srelplt;
    457   asection *srelplt2;
    458   asection *sgotplt;
    459   asection *splt;
    460   asection *sstubs;
    461   asection *sgot;
    462 
    463   /* The master GOT information.  */
    464   struct mips_got_info *got_info;
    465 
    466   /* The global symbol in the GOT with the lowest index in the dynamic
    467      symbol table.  */
    468   struct elf_link_hash_entry *global_gotsym;
    469 
    470   /* The size of the PLT header in bytes.  */
    471   bfd_vma plt_header_size;
    472 
    473   /* The size of a standard PLT entry in bytes.  */
    474   bfd_vma plt_mips_entry_size;
    475 
    476   /* The size of a compressed PLT entry in bytes.  */
    477   bfd_vma plt_comp_entry_size;
    478 
    479   /* The offset of the next standard PLT entry to create.  */
    480   bfd_vma plt_mips_offset;
    481 
    482   /* The offset of the next compressed PLT entry to create.  */
    483   bfd_vma plt_comp_offset;
    484 
    485   /* The index of the next .got.plt entry to create.  */
    486   bfd_vma plt_got_index;
    487 
    488   /* The number of functions that need a lazy-binding stub.  */
    489   bfd_vma lazy_stub_count;
    490 
    491   /* The size of a function stub entry in bytes.  */
    492   bfd_vma function_stub_size;
    493 
    494   /* The number of reserved entries at the beginning of the GOT.  */
    495   unsigned int reserved_gotno;
    496 
    497   /* The section used for mips_elf_la25_stub trampolines.
    498      See the comment above that structure for details.  */
    499   asection *strampoline;
    500 
    501   /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
    502      pairs.  */
    503   htab_t la25_stubs;
    504 
    505   /* A function FN (NAME, IS, OS) that creates a new input section
    506      called NAME and links it to output section OS.  If IS is nonnull,
    507      the new section should go immediately before it, otherwise it
    508      should go at the (current) beginning of OS.
    509 
    510      The function returns the new section on success, otherwise it
    511      returns null.  */
    512   asection *(*add_stub_section) (const char *, asection *, asection *);
    513 
    514   /* Small local sym cache.  */
    515   struct sym_cache sym_cache;
    516 
    517   /* Is the PLT header compressed?  */
    518   unsigned int plt_header_is_comp : 1;
    519 };
    520 
    521 /* Get the MIPS ELF linker hash table from a link_info structure.  */
    522 
    523 #define mips_elf_hash_table(p) \
    524   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
    525   == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
    526 
    527 /* A structure used to communicate with htab_traverse callbacks.  */
    528 struct mips_htab_traverse_info
    529 {
    530   /* The usual link-wide information.  */
    531   struct bfd_link_info *info;
    532   bfd *output_bfd;
    533 
    534   /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
    535   bfd_boolean error;
    536 };
    537 
    538 /* MIPS ELF private object data.  */
    539 
    540 struct mips_elf_obj_tdata
    541 {
    542   /* Generic ELF private object data.  */
    543   struct elf_obj_tdata root;
    544 
    545   /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output.  */
    546   bfd *abi_fp_bfd;
    547 
    548   /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output.  */
    549   bfd *abi_msa_bfd;
    550 
    551   /* The abiflags for this object.  */
    552   Elf_Internal_ABIFlags_v0 abiflags;
    553   bfd_boolean abiflags_valid;
    554 
    555   /* The GOT requirements of input bfds.  */
    556   struct mips_got_info *got;
    557 
    558   /* Used by _bfd_mips_elf_find_nearest_line.  The structure could be
    559      included directly in this one, but there's no point to wasting
    560      the memory just for the infrequently called find_nearest_line.  */
    561   struct mips_elf_find_line *find_line_info;
    562 
    563   /* An array of stub sections indexed by symbol number.  */
    564   asection **local_stubs;
    565   asection **local_call_stubs;
    566 
    567   /* The Irix 5 support uses two virtual sections, which represent
    568      text/data symbols defined in dynamic objects.  */
    569   asymbol *elf_data_symbol;
    570   asymbol *elf_text_symbol;
    571   asection *elf_data_section;
    572   asection *elf_text_section;
    573 };
    574 
    575 /* Get MIPS ELF private object data from BFD's tdata.  */
    576 
    577 #define mips_elf_tdata(bfd) \
    578   ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
    579 
    580 #define TLS_RELOC_P(r_type) \
    581   (r_type == R_MIPS_TLS_DTPMOD32		\
    582    || r_type == R_MIPS_TLS_DTPMOD64		\
    583    || r_type == R_MIPS_TLS_DTPREL32		\
    584    || r_type == R_MIPS_TLS_DTPREL64		\
    585    || r_type == R_MIPS_TLS_GD			\
    586    || r_type == R_MIPS_TLS_LDM			\
    587    || r_type == R_MIPS_TLS_DTPREL_HI16		\
    588    || r_type == R_MIPS_TLS_DTPREL_LO16		\
    589    || r_type == R_MIPS_TLS_GOTTPREL		\
    590    || r_type == R_MIPS_TLS_TPREL32		\
    591    || r_type == R_MIPS_TLS_TPREL64		\
    592    || r_type == R_MIPS_TLS_TPREL_HI16		\
    593    || r_type == R_MIPS_TLS_TPREL_LO16		\
    594    || r_type == R_MIPS16_TLS_GD			\
    595    || r_type == R_MIPS16_TLS_LDM		\
    596    || r_type == R_MIPS16_TLS_DTPREL_HI16	\
    597    || r_type == R_MIPS16_TLS_DTPREL_LO16	\
    598    || r_type == R_MIPS16_TLS_GOTTPREL		\
    599    || r_type == R_MIPS16_TLS_TPREL_HI16		\
    600    || r_type == R_MIPS16_TLS_TPREL_LO16		\
    601    || r_type == R_MICROMIPS_TLS_GD		\
    602    || r_type == R_MICROMIPS_TLS_LDM		\
    603    || r_type == R_MICROMIPS_TLS_DTPREL_HI16	\
    604    || r_type == R_MICROMIPS_TLS_DTPREL_LO16	\
    605    || r_type == R_MICROMIPS_TLS_GOTTPREL	\
    606    || r_type == R_MICROMIPS_TLS_TPREL_HI16	\
    607    || r_type == R_MICROMIPS_TLS_TPREL_LO16)
    608 
    609 /* Structure used to pass information to mips_elf_output_extsym.  */
    610 
    611 struct extsym_info
    612 {
    613   bfd *abfd;
    614   struct bfd_link_info *info;
    615   struct ecoff_debug_info *debug;
    616   const struct ecoff_debug_swap *swap;
    617   bfd_boolean failed;
    618 };
    619 
    620 /* The names of the runtime procedure table symbols used on IRIX5.  */
    621 
    622 static const char * const mips_elf_dynsym_rtproc_names[] =
    623 {
    624   "_procedure_table",
    625   "_procedure_string_table",
    626   "_procedure_table_size",
    627   NULL
    628 };
    629 
    630 /* These structures are used to generate the .compact_rel section on
    631    IRIX5.  */
    632 
    633 typedef struct
    634 {
    635   unsigned long id1;		/* Always one?  */
    636   unsigned long num;		/* Number of compact relocation entries.  */
    637   unsigned long id2;		/* Always two?  */
    638   unsigned long offset;		/* The file offset of the first relocation.  */
    639   unsigned long reserved0;	/* Zero?  */
    640   unsigned long reserved1;	/* Zero?  */
    641 } Elf32_compact_rel;
    642 
    643 typedef struct
    644 {
    645   bfd_byte id1[4];
    646   bfd_byte num[4];
    647   bfd_byte id2[4];
    648   bfd_byte offset[4];
    649   bfd_byte reserved0[4];
    650   bfd_byte reserved1[4];
    651 } Elf32_External_compact_rel;
    652 
    653 typedef struct
    654 {
    655   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
    656   unsigned int rtype : 4;	/* Relocation types. See below.  */
    657   unsigned int dist2to : 8;
    658   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
    659   unsigned long konst;		/* KONST field. See below.  */
    660   unsigned long vaddr;		/* VADDR to be relocated.  */
    661 } Elf32_crinfo;
    662 
    663 typedef struct
    664 {
    665   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
    666   unsigned int rtype : 4;	/* Relocation types. See below.  */
    667   unsigned int dist2to : 8;
    668   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
    669   unsigned long konst;		/* KONST field. See below.  */
    670 } Elf32_crinfo2;
    671 
    672 typedef struct
    673 {
    674   bfd_byte info[4];
    675   bfd_byte konst[4];
    676   bfd_byte vaddr[4];
    677 } Elf32_External_crinfo;
    678 
    679 typedef struct
    680 {
    681   bfd_byte info[4];
    682   bfd_byte konst[4];
    683 } Elf32_External_crinfo2;
    684 
    685 /* These are the constants used to swap the bitfields in a crinfo.  */
    686 
    687 #define CRINFO_CTYPE (0x1)
    688 #define CRINFO_CTYPE_SH (31)
    689 #define CRINFO_RTYPE (0xf)
    690 #define CRINFO_RTYPE_SH (27)
    691 #define CRINFO_DIST2TO (0xff)
    692 #define CRINFO_DIST2TO_SH (19)
    693 #define CRINFO_RELVADDR (0x7ffff)
    694 #define CRINFO_RELVADDR_SH (0)
    695 
    696 /* A compact relocation info has long (3 words) or short (2 words)
    697    formats.  A short format doesn't have VADDR field and relvaddr
    698    fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
    699 #define CRF_MIPS_LONG			1
    700 #define CRF_MIPS_SHORT			0
    701 
    702 /* There are 4 types of compact relocation at least. The value KONST
    703    has different meaning for each type:
    704 
    705    (type)		(konst)
    706    CT_MIPS_REL32	Address in data
    707    CT_MIPS_WORD		Address in word (XXX)
    708    CT_MIPS_GPHI_LO	GP - vaddr
    709    CT_MIPS_JMPAD	Address to jump
    710    */
    711 
    712 #define CRT_MIPS_REL32			0xa
    713 #define CRT_MIPS_WORD			0xb
    714 #define CRT_MIPS_GPHI_LO		0xc
    715 #define CRT_MIPS_JMPAD			0xd
    716 
    717 #define mips_elf_set_cr_format(x,format)	((x).ctype = (format))
    718 #define mips_elf_set_cr_type(x,type)		((x).rtype = (type))
    719 #define mips_elf_set_cr_dist2to(x,v)		((x).dist2to = (v))
    720 #define mips_elf_set_cr_relvaddr(x,d)		((x).relvaddr = (d)<<2)
    721 
    722 /* The structure of the runtime procedure descriptor created by the
    724    loader for use by the static exception system.  */
    725 
    726 typedef struct runtime_pdr {
    727 	bfd_vma	adr;		/* Memory address of start of procedure.  */
    728 	long	regmask;	/* Save register mask.  */
    729 	long	regoffset;	/* Save register offset.  */
    730 	long	fregmask;	/* Save floating point register mask.  */
    731 	long	fregoffset;	/* Save floating point register offset.  */
    732 	long	frameoffset;	/* Frame size.  */
    733 	short	framereg;	/* Frame pointer register.  */
    734 	short	pcreg;		/* Offset or reg of return pc.  */
    735 	long	irpss;		/* Index into the runtime string table.  */
    736 	long	reserved;
    737 	struct exception_info *exception_info;/* Pointer to exception array.  */
    738 } RPDR, *pRPDR;
    739 #define cbRPDR sizeof (RPDR)
    740 #define rpdNil ((pRPDR) 0)
    741 
    742 static struct mips_got_entry *mips_elf_create_local_got_entry
    744   (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
    745    struct mips_elf_link_hash_entry *, int);
    746 static bfd_boolean mips_elf_sort_hash_table_f
    747   (struct mips_elf_link_hash_entry *, void *);
    748 static bfd_vma mips_elf_high
    749   (bfd_vma);
    750 static bfd_boolean mips_elf_create_dynamic_relocation
    751   (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
    752    struct mips_elf_link_hash_entry *, asection *, bfd_vma,
    753    bfd_vma *, asection *);
    754 static bfd_vma mips_elf_adjust_gp
    755   (bfd *, struct mips_got_info *, bfd *);
    756 
    757 /* This will be used when we sort the dynamic relocation records.  */
    758 static bfd *reldyn_sorting_bfd;
    759 
    760 /* True if ABFD is for CPUs with load interlocking that include
    761    non-MIPS1 CPUs and R3900.  */
    762 #define LOAD_INTERLOCKS_P(abfd) \
    763   (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
    764    || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
    765 
    766 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
    767    This should be safe for all architectures.  We enable this predicate
    768    for RM9000 for now.  */
    769 #define JAL_TO_BAL_P(abfd) \
    770   ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
    771 
    772 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
    773    This should be safe for all architectures.  We enable this predicate for
    774    all CPUs.  */
    775 #define JALR_TO_BAL_P(abfd) 1
    776 
    777 /* True if ABFD is for CPUs that are faster if JR is converted to B.
    778    This should be safe for all architectures.  We enable this predicate for
    779    all CPUs.  */
    780 #define JR_TO_B_P(abfd) 1
    781 
    782 /* True if ABFD is a PIC object.  */
    783 #define PIC_OBJECT_P(abfd) \
    784   ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
    785 
    786 /* Nonzero if ABFD is using the O32 ABI.  */
    787 #define ABI_O32_P(abfd) \
    788   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
    789 
    790 /* Nonzero if ABFD is using the N32 ABI.  */
    791 #define ABI_N32_P(abfd) \
    792   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
    793 
    794 /* Nonzero if ABFD is using the N64 ABI.  */
    795 #define ABI_64_P(abfd) \
    796   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
    797 
    798 /* Nonzero if ABFD is using NewABI conventions.  */
    799 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
    800 
    801 /* Nonzero if ABFD has microMIPS code.  */
    802 #define MICROMIPS_P(abfd) \
    803   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
    804 
    805 /* Nonzero if ABFD is MIPS R6.  */
    806 #define MIPSR6_P(abfd) \
    807   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
    808     || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
    809 
    810 /* The IRIX compatibility level we are striving for.  */
    811 #define IRIX_COMPAT(abfd) \
    812   (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
    813 
    814 /* Whether we are trying to be compatible with IRIX at all.  */
    815 #define SGI_COMPAT(abfd) \
    816   (IRIX_COMPAT (abfd) != ict_none)
    817 
    818 /* The name of the options section.  */
    819 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
    820   (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
    821 
    822 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
    823    Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
    824 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
    825   (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
    826 
    827 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section.  */
    828 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
    829   (strcmp (NAME, ".MIPS.abiflags") == 0)
    830 
    831 /* Whether the section is readonly.  */
    832 #define MIPS_ELF_READONLY_SECTION(sec) \
    833   ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))		\
    834    == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
    835 
    836 /* The name of the stub section.  */
    837 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
    838 
    839 /* The size of an external REL relocation.  */
    840 #define MIPS_ELF_REL_SIZE(abfd) \
    841   (get_elf_backend_data (abfd)->s->sizeof_rel)
    842 
    843 /* The size of an external RELA relocation.  */
    844 #define MIPS_ELF_RELA_SIZE(abfd) \
    845   (get_elf_backend_data (abfd)->s->sizeof_rela)
    846 
    847 /* The size of an external dynamic table entry.  */
    848 #define MIPS_ELF_DYN_SIZE(abfd) \
    849   (get_elf_backend_data (abfd)->s->sizeof_dyn)
    850 
    851 /* The size of a GOT entry.  */
    852 #define MIPS_ELF_GOT_SIZE(abfd) \
    853   (get_elf_backend_data (abfd)->s->arch_size / 8)
    854 
    855 /* The size of the .rld_map section. */
    856 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
    857   (get_elf_backend_data (abfd)->s->arch_size / 8)
    858 
    859 /* The size of a symbol-table entry.  */
    860 #define MIPS_ELF_SYM_SIZE(abfd) \
    861   (get_elf_backend_data (abfd)->s->sizeof_sym)
    862 
    863 /* The default alignment for sections, as a power of two.  */
    864 #define MIPS_ELF_LOG_FILE_ALIGN(abfd)				\
    865   (get_elf_backend_data (abfd)->s->log_file_align)
    866 
    867 /* Get word-sized data.  */
    868 #define MIPS_ELF_GET_WORD(abfd, ptr) \
    869   (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
    870 
    871 /* Put out word-sized data.  */
    872 #define MIPS_ELF_PUT_WORD(abfd, val, ptr)	\
    873   (ABI_64_P (abfd) 				\
    874    ? bfd_put_64 (abfd, val, ptr) 		\
    875    : bfd_put_32 (abfd, val, ptr))
    876 
    877 /* The opcode for word-sized loads (LW or LD).  */
    878 #define MIPS_ELF_LOAD_WORD(abfd) \
    879   (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
    880 
    881 /* Add a dynamic symbol table-entry.  */
    882 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)	\
    883   _bfd_elf_add_dynamic_entry (info, tag, val)
    884 
    885 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)			\
    886   (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
    887 
    888 /* The name of the dynamic relocation section.  */
    889 #define MIPS_ELF_REL_DYN_NAME(INFO) \
    890   (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
    891 
    892 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
    893    from smaller values.  Start with zero, widen, *then* decrement.  */
    894 #define MINUS_ONE	(((bfd_vma)0) - 1)
    895 #define MINUS_TWO	(((bfd_vma)0) - 2)
    896 
    897 /* The value to write into got[1] for SVR4 targets, to identify it is
    898    a GNU object.  The dynamic linker can then use got[1] to store the
    899    module pointer.  */
    900 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
    901   ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
    902 
    903 /* The offset of $gp from the beginning of the .got section.  */
    904 #define ELF_MIPS_GP_OFFSET(INFO) \
    905   (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
    906 
    907 /* The maximum size of the GOT for it to be addressable using 16-bit
    908    offsets from $gp.  */
    909 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
    910 
    911 /* Instructions which appear in a stub.  */
    912 #define STUB_LW(abfd)							\
    913   ((ABI_64_P (abfd)							\
    914     ? 0xdf998010				/* ld t9,0x8010(gp) */	\
    915     : 0x8f998010))              		/* lw t9,0x8010(gp) */
    916 #define STUB_MOVE 0x03e07825			/* or t7,ra,zero */
    917 #define STUB_LUI(VAL) (0x3c180000 + (VAL))	/* lui t8,VAL */
    918 #define STUB_JALR 0x0320f809			/* jalr t9,ra */
    919 #define STUB_ORI(VAL) (0x37180000 + (VAL))	/* ori t8,t8,VAL */
    920 #define STUB_LI16U(VAL) (0x34180000 + (VAL))	/* ori t8,zero,VAL unsigned */
    921 #define STUB_LI16S(abfd, VAL)						\
    922    ((ABI_64_P (abfd)							\
    923     ? (0x64180000 + (VAL))	/* daddiu t8,zero,VAL sign extended */	\
    924     : (0x24180000 + (VAL))))	/* addiu t8,zero,VAL sign extended */
    925 
    926 /* Likewise for the microMIPS ASE.  */
    927 #define STUB_LW_MICROMIPS(abfd)						\
    928   (ABI_64_P (abfd)							\
    929    ? 0xdf3c8010					/* ld t9,0x8010(gp) */	\
    930    : 0xff3c8010)				/* lw t9,0x8010(gp) */
    931 #define STUB_MOVE_MICROMIPS 0x0dff		/* move t7,ra */
    932 #define STUB_MOVE32_MICROMIPS 0x001f7a90	/* or t7,ra,zero */
    933 #define STUB_LUI_MICROMIPS(VAL)						\
    934    (0x41b80000 + (VAL))				/* lui t8,VAL */
    935 #define STUB_JALR_MICROMIPS 0x45d9		/* jalr t9 */
    936 #define STUB_JALR32_MICROMIPS 0x03f90f3c	/* jalr ra,t9 */
    937 #define STUB_ORI_MICROMIPS(VAL)						\
    938   (0x53180000 + (VAL))				/* ori t8,t8,VAL */
    939 #define STUB_LI16U_MICROMIPS(VAL)					\
    940   (0x53000000 + (VAL))				/* ori t8,zero,VAL unsigned */
    941 #define STUB_LI16S_MICROMIPS(abfd, VAL)					\
    942    (ABI_64_P (abfd)							\
    943     ? 0x5f000000 + (VAL)	/* daddiu t8,zero,VAL sign extended */	\
    944     : 0x33000000 + (VAL))	/* addiu t8,zero,VAL sign extended */
    945 
    946 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
    947 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
    948 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
    949 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
    950 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
    951 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
    952 
    953 /* The name of the dynamic interpreter.  This is put in the .interp
    954    section.  */
    955 
    956 #define ELF_DYNAMIC_INTERPRETER(abfd) 		\
    957    (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" 	\
    958     : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" 	\
    959     : "/usr/lib/libc.so.1")
    960 
    961 #ifdef BFD64
    962 #define MNAME(bfd,pre,pos) \
    963   (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
    964 #define ELF_R_SYM(bfd, i)					\
    965   (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
    966 #define ELF_R_TYPE(bfd, i)					\
    967   (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
    968 #define ELF_R_INFO(bfd, s, t)					\
    969   (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
    970 #else
    971 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
    972 #define ELF_R_SYM(bfd, i)					\
    973   (ELF32_R_SYM (i))
    974 #define ELF_R_TYPE(bfd, i)					\
    975   (ELF32_R_TYPE (i))
    976 #define ELF_R_INFO(bfd, s, t)					\
    977   (ELF32_R_INFO (s, t))
    978 #endif
    979 
    980   /* The mips16 compiler uses a couple of special sections to handle
    982      floating point arguments.
    983 
    984      Section names that look like .mips16.fn.FNNAME contain stubs that
    985      copy floating point arguments from the fp regs to the gp regs and
    986      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
    987      call should be redirected to the stub instead.  If no 32 bit
    988      function calls FNNAME, the stub should be discarded.  We need to
    989      consider any reference to the function, not just a call, because
    990      if the address of the function is taken we will need the stub,
    991      since the address might be passed to a 32 bit function.
    992 
    993      Section names that look like .mips16.call.FNNAME contain stubs
    994      that copy floating point arguments from the gp regs to the fp
    995      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
    996      then any 16 bit function that calls FNNAME should be redirected
    997      to the stub instead.  If FNNAME is not a 32 bit function, the
    998      stub should be discarded.
    999 
   1000      .mips16.call.fp.FNNAME sections are similar, but contain stubs
   1001      which call FNNAME and then copy the return value from the fp regs
   1002      to the gp regs.  These stubs store the return value in $18 while
   1003      calling FNNAME; any function which might call one of these stubs
   1004      must arrange to save $18 around the call.  (This case is not
   1005      needed for 32 bit functions that call 16 bit functions, because
   1006      16 bit functions always return floating point values in both
   1007      $f0/$f1 and $2/$3.)
   1008 
   1009      Note that in all cases FNNAME might be defined statically.
   1010      Therefore, FNNAME is not used literally.  Instead, the relocation
   1011      information will indicate which symbol the section is for.
   1012 
   1013      We record any stubs that we find in the symbol table.  */
   1014 
   1015 #define FN_STUB ".mips16.fn."
   1016 #define CALL_STUB ".mips16.call."
   1017 #define CALL_FP_STUB ".mips16.call.fp."
   1018 
   1019 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
   1020 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
   1021 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
   1022 
   1023 /* The format of the first PLT entry in an O32 executable.  */
   1025 static const bfd_vma mips_o32_exec_plt0_entry[] =
   1026 {
   1027   0x3c1c0000,	/* lui $28, %hi(&GOTPLT[0])				*/
   1028   0x8f990000,	/* lw $25, %lo(&GOTPLT[0])($28)				*/
   1029   0x279c0000,	/* addiu $28, $28, %lo(&GOTPLT[0])			*/
   1030   0x031cc023,	/* subu $24, $24, $28					*/
   1031   0x03e07825,	/* or t7, ra, zero					*/
   1032   0x0018c082,	/* srl $24, $24, 2					*/
   1033   0x0320f809,	/* jalr $25						*/
   1034   0x2718fffe	/* subu $24, $24, 2					*/
   1035 };
   1036 
   1037 /* The format of the first PLT entry in an N32 executable.  Different
   1038    because gp ($28) is not available; we use t2 ($14) instead.  */
   1039 static const bfd_vma mips_n32_exec_plt0_entry[] =
   1040 {
   1041   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1042   0x8dd90000,	/* lw $25, %lo(&GOTPLT[0])($14)				*/
   1043   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1044   0x030ec023,	/* subu $24, $24, $14					*/
   1045   0x03e07825,	/* or t7, ra, zero					*/
   1046   0x0018c082,	/* srl $24, $24, 2					*/
   1047   0x0320f809,	/* jalr $25						*/
   1048   0x2718fffe	/* subu $24, $24, 2					*/
   1049 };
   1050 
   1051 /* The format of the first PLT entry in an N64 executable.  Different
   1052    from N32 because of the increased size of GOT entries.  */
   1053 static const bfd_vma mips_n64_exec_plt0_entry[] =
   1054 {
   1055   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
   1056   0xddd90000,	/* ld $25, %lo(&GOTPLT[0])($14)				*/
   1057   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
   1058   0x030ec023,	/* subu $24, $24, $14					*/
   1059   0x03e07825,	/* or t7, ra, zero					*/
   1060   0x0018c0c2,	/* srl $24, $24, 3					*/
   1061   0x0320f809,	/* jalr $25						*/
   1062   0x2718fffe	/* subu $24, $24, 2					*/
   1063 };
   1064 
   1065 /* The format of the microMIPS first PLT entry in an O32 executable.
   1066    We rely on v0 ($2) rather than t8 ($24) to contain the address
   1067    of the GOTPLT entry handled, so this stub may only be used when
   1068    all the subsequent PLT entries are microMIPS code too.
   1069 
   1070    The trailing NOP is for alignment and correct disassembly only.  */
   1071 static const bfd_vma micromips_o32_exec_plt0_entry[] =
   1072 {
   1073   0x7980, 0x0000,	/* addiupc $3, (&GOTPLT[0]) - .			*/
   1074   0xff23, 0x0000,	/* lw $25, 0($3)				*/
   1075   0x0535,		/* subu $2, $2, $3				*/
   1076   0x2525,		/* srl $2, $2, 2				*/
   1077   0x3302, 0xfffe,	/* subu $24, $2, 2				*/
   1078   0x0dff,		/* move $15, $31				*/
   1079   0x45f9,		/* jalrs $25					*/
   1080   0x0f83,		/* move $28, $3					*/
   1081   0x0c00		/* nop						*/
   1082 };
   1083 
   1084 /* The format of the microMIPS first PLT entry in an O32 executable
   1085    in the insn32 mode.  */
   1086 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
   1087 {
   1088   0x41bc, 0x0000,	/* lui $28, %hi(&GOTPLT[0])			*/
   1089   0xff3c, 0x0000,	/* lw $25, %lo(&GOTPLT[0])($28)			*/
   1090   0x339c, 0x0000,	/* addiu $28, $28, %lo(&GOTPLT[0])		*/
   1091   0x0398, 0xc1d0,	/* subu $24, $24, $28				*/
   1092   0x001f, 0x7a90,	/* or $15, $31, zero				*/
   1093   0x0318, 0x1040,	/* srl $24, $24, 2				*/
   1094   0x03f9, 0x0f3c,	/* jalr $25					*/
   1095   0x3318, 0xfffe	/* subu $24, $24, 2				*/
   1096 };
   1097 
   1098 /* The format of subsequent standard PLT entries.  */
   1099 static const bfd_vma mips_exec_plt_entry[] =
   1100 {
   1101   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
   1102   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
   1103   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
   1104   0x03200008	/* jr $25					*/
   1105 };
   1106 
   1107 /* In the following PLT entry the JR and ADDIU instructions will
   1108    be swapped in _bfd_mips_elf_finish_dynamic_symbol because
   1109    LOAD_INTERLOCKS_P will be true for MIPS R6.  */
   1110 static const bfd_vma mipsr6_exec_plt_entry[] =
   1111 {
   1112   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
   1113   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
   1114   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
   1115   0x03200009	/* jr $25					*/
   1116 };
   1117 
   1118 /* The format of subsequent MIPS16 o32 PLT entries.  We use v0 ($2)
   1119    and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
   1120    directly addressable.  */
   1121 static const bfd_vma mips16_o32_exec_plt_entry[] =
   1122 {
   1123   0xb203,		/* lw $2, 12($pc)			*/
   1124   0x9a60,		/* lw $3, 0($2)				*/
   1125   0x651a,		/* move $24, $2				*/
   1126   0xeb00,		/* jr $3				*/
   1127   0x653b,		/* move $25, $3				*/
   1128   0x6500,		/* nop					*/
   1129   0x0000, 0x0000	/* .word (.got.plt entry)		*/
   1130 };
   1131 
   1132 /* The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
   1133    as a temporary because t8 ($24) is not addressable with ADDIUPC.  */
   1134 static const bfd_vma micromips_o32_exec_plt_entry[] =
   1135 {
   1136   0x7900, 0x0000,	/* addiupc $2, (.got.plt entry) - .	*/
   1137   0xff22, 0x0000,	/* lw $25, 0($2)			*/
   1138   0x4599,		/* jr $25				*/
   1139   0x0f02		/* move $24, $2				*/
   1140 };
   1141 
   1142 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode.  */
   1143 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
   1144 {
   1145   0x41af, 0x0000,	/* lui $15, %hi(.got.plt entry)		*/
   1146   0xff2f, 0x0000,	/* lw $25, %lo(.got.plt entry)($15)	*/
   1147   0x0019, 0x0f3c,	/* jr $25				*/
   1148   0x330f, 0x0000	/* addiu $24, $15, %lo(.got.plt entry)	*/
   1149 };
   1150 
   1151 /* The format of the first PLT entry in a VxWorks executable.  */
   1152 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
   1153 {
   1154   0x3c190000,	/* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)		*/
   1155   0x27390000,	/* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_)	*/
   1156   0x8f390008,	/* lw t9, 8(t9)					*/
   1157   0x00000000,	/* nop						*/
   1158   0x03200008,	/* jr t9					*/
   1159   0x00000000	/* nop						*/
   1160 };
   1161 
   1162 /* The format of subsequent PLT entries.  */
   1163 static const bfd_vma mips_vxworks_exec_plt_entry[] =
   1164 {
   1165   0x10000000,	/* b .PLT_resolver			*/
   1166   0x24180000,	/* li t8, <pltindex>			*/
   1167   0x3c190000,	/* lui t9, %hi(<.got.plt slot>)		*/
   1168   0x27390000,	/* addiu t9, t9, %lo(<.got.plt slot>)	*/
   1169   0x8f390000,	/* lw t9, 0(t9)				*/
   1170   0x00000000,	/* nop					*/
   1171   0x03200008,	/* jr t9				*/
   1172   0x00000000	/* nop					*/
   1173 };
   1174 
   1175 /* The format of the first PLT entry in a VxWorks shared object.  */
   1176 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
   1177 {
   1178   0x8f990008,	/* lw t9, 8(gp)		*/
   1179   0x00000000,	/* nop			*/
   1180   0x03200008,	/* jr t9		*/
   1181   0x00000000,	/* nop			*/
   1182   0x00000000,	/* nop			*/
   1183   0x00000000	/* nop			*/
   1184 };
   1185 
   1186 /* The format of subsequent PLT entries.  */
   1187 static const bfd_vma mips_vxworks_shared_plt_entry[] =
   1188 {
   1189   0x10000000,	/* b .PLT_resolver	*/
   1190   0x24180000	/* li t8, <pltindex>	*/
   1191 };
   1192 
   1193 /* microMIPS 32-bit opcode helper installer.  */
   1195 
   1196 static void
   1197 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
   1198 {
   1199   bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
   1200   bfd_put_16 (abfd,  opcode        & 0xffff, ptr + 2);
   1201 }
   1202 
   1203 /* microMIPS 32-bit opcode helper retriever.  */
   1204 
   1205 static bfd_vma
   1206 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
   1207 {
   1208   return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
   1209 }
   1210 
   1211 /* Look up an entry in a MIPS ELF linker hash table.  */
   1213 
   1214 #define mips_elf_link_hash_lookup(table, string, create, copy, follow)	\
   1215   ((struct mips_elf_link_hash_entry *)					\
   1216    elf_link_hash_lookup (&(table)->root, (string), (create),		\
   1217 			 (copy), (follow)))
   1218 
   1219 /* Traverse a MIPS ELF linker hash table.  */
   1220 
   1221 #define mips_elf_link_hash_traverse(table, func, info)			\
   1222   (elf_link_hash_traverse						\
   1223    (&(table)->root,							\
   1224     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),	\
   1225     (info)))
   1226 
   1227 /* Find the base offsets for thread-local storage in this object,
   1228    for GD/LD and IE/LE respectively.  */
   1229 
   1230 #define TP_OFFSET 0x7000
   1231 #define DTP_OFFSET 0x8000
   1232 
   1233 static bfd_vma
   1234 dtprel_base (struct bfd_link_info *info)
   1235 {
   1236   /* If tls_sec is NULL, we should have signalled an error already.  */
   1237   if (elf_hash_table (info)->tls_sec == NULL)
   1238     return 0;
   1239   return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
   1240 }
   1241 
   1242 static bfd_vma
   1243 tprel_base (struct bfd_link_info *info)
   1244 {
   1245   /* If tls_sec is NULL, we should have signalled an error already.  */
   1246   if (elf_hash_table (info)->tls_sec == NULL)
   1247     return 0;
   1248   return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
   1249 }
   1250 
   1251 /* Create an entry in a MIPS ELF linker hash table.  */
   1252 
   1253 static struct bfd_hash_entry *
   1254 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   1255 			    struct bfd_hash_table *table, const char *string)
   1256 {
   1257   struct mips_elf_link_hash_entry *ret =
   1258     (struct mips_elf_link_hash_entry *) entry;
   1259 
   1260   /* Allocate the structure if it has not already been allocated by a
   1261      subclass.  */
   1262   if (ret == NULL)
   1263     ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
   1264   if (ret == NULL)
   1265     return (struct bfd_hash_entry *) ret;
   1266 
   1267   /* Call the allocation method of the superclass.  */
   1268   ret = ((struct mips_elf_link_hash_entry *)
   1269 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
   1270 				     table, string));
   1271   if (ret != NULL)
   1272     {
   1273       /* Set local fields.  */
   1274       memset (&ret->esym, 0, sizeof (EXTR));
   1275       /* We use -2 as a marker to indicate that the information has
   1276 	 not been set.  -1 means there is no associated ifd.  */
   1277       ret->esym.ifd = -2;
   1278       ret->la25_stub = 0;
   1279       ret->possibly_dynamic_relocs = 0;
   1280       ret->fn_stub = NULL;
   1281       ret->call_stub = NULL;
   1282       ret->call_fp_stub = NULL;
   1283       ret->global_got_area = GGA_NONE;
   1284       ret->got_only_for_calls = TRUE;
   1285       ret->readonly_reloc = FALSE;
   1286       ret->has_static_relocs = FALSE;
   1287       ret->no_fn_stub = FALSE;
   1288       ret->need_fn_stub = FALSE;
   1289       ret->has_nonpic_branches = FALSE;
   1290       ret->needs_lazy_stub = FALSE;
   1291       ret->use_plt_entry = FALSE;
   1292     }
   1293 
   1294   return (struct bfd_hash_entry *) ret;
   1295 }
   1296 
   1297 /* Allocate MIPS ELF private object data.  */
   1298 
   1299 bfd_boolean
   1300 _bfd_mips_elf_mkobject (bfd *abfd)
   1301 {
   1302   return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
   1303 				  MIPS_ELF_DATA);
   1304 }
   1305 
   1306 bfd_boolean
   1307 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
   1308 {
   1309   if (!sec->used_by_bfd)
   1310     {
   1311       struct _mips_elf_section_data *sdata;
   1312       bfd_size_type amt = sizeof (*sdata);
   1313 
   1314       sdata = bfd_zalloc (abfd, amt);
   1315       if (sdata == NULL)
   1316 	return FALSE;
   1317       sec->used_by_bfd = sdata;
   1318     }
   1319 
   1320   return _bfd_elf_new_section_hook (abfd, sec);
   1321 }
   1322 
   1323 /* Read ECOFF debugging information from a .mdebug section into a
   1325    ecoff_debug_info structure.  */
   1326 
   1327 bfd_boolean
   1328 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
   1329 			       struct ecoff_debug_info *debug)
   1330 {
   1331   HDRR *symhdr;
   1332   const struct ecoff_debug_swap *swap;
   1333   char *ext_hdr;
   1334 
   1335   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   1336   memset (debug, 0, sizeof (*debug));
   1337 
   1338   ext_hdr = bfd_malloc (swap->external_hdr_size);
   1339   if (ext_hdr == NULL && swap->external_hdr_size != 0)
   1340     goto error_return;
   1341 
   1342   if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
   1343 				  swap->external_hdr_size))
   1344     goto error_return;
   1345 
   1346   symhdr = &debug->symbolic_header;
   1347   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
   1348 
   1349   /* The symbolic header contains absolute file offsets and sizes to
   1350      read.  */
   1351 #define READ(ptr, offset, count, size, type)				\
   1352   if (symhdr->count == 0)						\
   1353     debug->ptr = NULL;							\
   1354   else									\
   1355     {									\
   1356       bfd_size_type amt = (bfd_size_type) size * symhdr->count;		\
   1357       debug->ptr = bfd_malloc (amt);					\
   1358       if (debug->ptr == NULL)						\
   1359 	goto error_return;						\
   1360       if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0		\
   1361 	  || bfd_bread (debug->ptr, amt, abfd) != amt)			\
   1362 	goto error_return;						\
   1363     }
   1364 
   1365   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
   1366   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
   1367   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
   1368   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
   1369   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
   1370   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
   1371 	union aux_ext *);
   1372   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
   1373   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
   1374   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
   1375   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
   1376   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
   1377 #undef READ
   1378 
   1379   debug->fdr = NULL;
   1380 
   1381   return TRUE;
   1382 
   1383  error_return:
   1384   if (ext_hdr != NULL)
   1385     free (ext_hdr);
   1386   if (debug->line != NULL)
   1387     free (debug->line);
   1388   if (debug->external_dnr != NULL)
   1389     free (debug->external_dnr);
   1390   if (debug->external_pdr != NULL)
   1391     free (debug->external_pdr);
   1392   if (debug->external_sym != NULL)
   1393     free (debug->external_sym);
   1394   if (debug->external_opt != NULL)
   1395     free (debug->external_opt);
   1396   if (debug->external_aux != NULL)
   1397     free (debug->external_aux);
   1398   if (debug->ss != NULL)
   1399     free (debug->ss);
   1400   if (debug->ssext != NULL)
   1401     free (debug->ssext);
   1402   if (debug->external_fdr != NULL)
   1403     free (debug->external_fdr);
   1404   if (debug->external_rfd != NULL)
   1405     free (debug->external_rfd);
   1406   if (debug->external_ext != NULL)
   1407     free (debug->external_ext);
   1408   return FALSE;
   1409 }
   1410 
   1411 /* Swap RPDR (runtime procedure table entry) for output.  */
   1413 
   1414 static void
   1415 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
   1416 {
   1417   H_PUT_S32 (abfd, in->adr, ex->p_adr);
   1418   H_PUT_32 (abfd, in->regmask, ex->p_regmask);
   1419   H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
   1420   H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
   1421   H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
   1422   H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
   1423 
   1424   H_PUT_16 (abfd, in->framereg, ex->p_framereg);
   1425   H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
   1426 
   1427   H_PUT_32 (abfd, in->irpss, ex->p_irpss);
   1428 }
   1429 
   1430 /* Create a runtime procedure table from the .mdebug section.  */
   1431 
   1432 static bfd_boolean
   1433 mips_elf_create_procedure_table (void *handle, bfd *abfd,
   1434 				 struct bfd_link_info *info, asection *s,
   1435 				 struct ecoff_debug_info *debug)
   1436 {
   1437   const struct ecoff_debug_swap *swap;
   1438   HDRR *hdr = &debug->symbolic_header;
   1439   RPDR *rpdr, *rp;
   1440   struct rpdr_ext *erp;
   1441   void *rtproc;
   1442   struct pdr_ext *epdr;
   1443   struct sym_ext *esym;
   1444   char *ss, **sv;
   1445   char *str;
   1446   bfd_size_type size;
   1447   bfd_size_type count;
   1448   unsigned long sindex;
   1449   unsigned long i;
   1450   PDR pdr;
   1451   SYMR sym;
   1452   const char *no_name_func = _("static procedure (no name)");
   1453 
   1454   epdr = NULL;
   1455   rpdr = NULL;
   1456   esym = NULL;
   1457   ss = NULL;
   1458   sv = NULL;
   1459 
   1460   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   1461 
   1462   sindex = strlen (no_name_func) + 1;
   1463   count = hdr->ipdMax;
   1464   if (count > 0)
   1465     {
   1466       size = swap->external_pdr_size;
   1467 
   1468       epdr = bfd_malloc (size * count);
   1469       if (epdr == NULL)
   1470 	goto error_return;
   1471 
   1472       if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
   1473 	goto error_return;
   1474 
   1475       size = sizeof (RPDR);
   1476       rp = rpdr = bfd_malloc (size * count);
   1477       if (rpdr == NULL)
   1478 	goto error_return;
   1479 
   1480       size = sizeof (char *);
   1481       sv = bfd_malloc (size * count);
   1482       if (sv == NULL)
   1483 	goto error_return;
   1484 
   1485       count = hdr->isymMax;
   1486       size = swap->external_sym_size;
   1487       esym = bfd_malloc (size * count);
   1488       if (esym == NULL)
   1489 	goto error_return;
   1490 
   1491       if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
   1492 	goto error_return;
   1493 
   1494       count = hdr->issMax;
   1495       ss = bfd_malloc (count);
   1496       if (ss == NULL)
   1497 	goto error_return;
   1498       if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
   1499 	goto error_return;
   1500 
   1501       count = hdr->ipdMax;
   1502       for (i = 0; i < (unsigned long) count; i++, rp++)
   1503 	{
   1504 	  (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
   1505 	  (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
   1506 	  rp->adr = sym.value;
   1507 	  rp->regmask = pdr.regmask;
   1508 	  rp->regoffset = pdr.regoffset;
   1509 	  rp->fregmask = pdr.fregmask;
   1510 	  rp->fregoffset = pdr.fregoffset;
   1511 	  rp->frameoffset = pdr.frameoffset;
   1512 	  rp->framereg = pdr.framereg;
   1513 	  rp->pcreg = pdr.pcreg;
   1514 	  rp->irpss = sindex;
   1515 	  sv[i] = ss + sym.iss;
   1516 	  sindex += strlen (sv[i]) + 1;
   1517 	}
   1518     }
   1519 
   1520   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
   1521   size = BFD_ALIGN (size, 16);
   1522   rtproc = bfd_alloc (abfd, size);
   1523   if (rtproc == NULL)
   1524     {
   1525       mips_elf_hash_table (info)->procedure_count = 0;
   1526       goto error_return;
   1527     }
   1528 
   1529   mips_elf_hash_table (info)->procedure_count = count + 2;
   1530 
   1531   erp = rtproc;
   1532   memset (erp, 0, sizeof (struct rpdr_ext));
   1533   erp++;
   1534   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
   1535   strcpy (str, no_name_func);
   1536   str += strlen (no_name_func) + 1;
   1537   for (i = 0; i < count; i++)
   1538     {
   1539       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
   1540       strcpy (str, sv[i]);
   1541       str += strlen (sv[i]) + 1;
   1542     }
   1543   H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
   1544 
   1545   /* Set the size and contents of .rtproc section.  */
   1546   s->size = size;
   1547   s->contents = rtproc;
   1548 
   1549   /* Skip this section later on (I don't think this currently
   1550      matters, but someday it might).  */
   1551   s->map_head.link_order = NULL;
   1552 
   1553   if (epdr != NULL)
   1554     free (epdr);
   1555   if (rpdr != NULL)
   1556     free (rpdr);
   1557   if (esym != NULL)
   1558     free (esym);
   1559   if (ss != NULL)
   1560     free (ss);
   1561   if (sv != NULL)
   1562     free (sv);
   1563 
   1564   return TRUE;
   1565 
   1566  error_return:
   1567   if (epdr != NULL)
   1568     free (epdr);
   1569   if (rpdr != NULL)
   1570     free (rpdr);
   1571   if (esym != NULL)
   1572     free (esym);
   1573   if (ss != NULL)
   1574     free (ss);
   1575   if (sv != NULL)
   1576     free (sv);
   1577   return FALSE;
   1578 }
   1579 
   1580 /* We're going to create a stub for H.  Create a symbol for the stub's
   1582    value and size, to help make the disassembly easier to read.  */
   1583 
   1584 static bfd_boolean
   1585 mips_elf_create_stub_symbol (struct bfd_link_info *info,
   1586 			     struct mips_elf_link_hash_entry *h,
   1587 			     const char *prefix, asection *s, bfd_vma value,
   1588 			     bfd_vma size)
   1589 {
   1590   struct bfd_link_hash_entry *bh;
   1591   struct elf_link_hash_entry *elfh;
   1592   char *name;
   1593   bfd_boolean res;
   1594 
   1595   if (ELF_ST_IS_MICROMIPS (h->root.other))
   1596     value |= 1;
   1597 
   1598   /* Create a new symbol.  */
   1599   name = concat (prefix, h->root.root.root.string, NULL);
   1600   bh = NULL;
   1601   res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
   1602 					  BSF_LOCAL, s, value, NULL,
   1603 					  TRUE, FALSE, &bh);
   1604   free (name);
   1605   if (! res)
   1606     return FALSE;
   1607 
   1608   /* Make it a local function.  */
   1609   elfh = (struct elf_link_hash_entry *) bh;
   1610   elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   1611   elfh->size = size;
   1612   elfh->forced_local = 1;
   1613   return TRUE;
   1614 }
   1615 
   1616 /* We're about to redefine H.  Create a symbol to represent H's
   1617    current value and size, to help make the disassembly easier
   1618    to read.  */
   1619 
   1620 static bfd_boolean
   1621 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
   1622 			       struct mips_elf_link_hash_entry *h,
   1623 			       const char *prefix)
   1624 {
   1625   struct bfd_link_hash_entry *bh;
   1626   struct elf_link_hash_entry *elfh;
   1627   char *name;
   1628   asection *s;
   1629   bfd_vma value;
   1630   bfd_boolean res;
   1631 
   1632   /* Read the symbol's value.  */
   1633   BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
   1634 	      || h->root.root.type == bfd_link_hash_defweak);
   1635   s = h->root.root.u.def.section;
   1636   value = h->root.root.u.def.value;
   1637 
   1638   /* Create a new symbol.  */
   1639   name = concat (prefix, h->root.root.root.string, NULL);
   1640   bh = NULL;
   1641   res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
   1642 					  BSF_LOCAL, s, value, NULL,
   1643 					  TRUE, FALSE, &bh);
   1644   free (name);
   1645   if (! res)
   1646     return FALSE;
   1647 
   1648   /* Make it local and copy the other attributes from H.  */
   1649   elfh = (struct elf_link_hash_entry *) bh;
   1650   elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
   1651   elfh->other = h->root.other;
   1652   elfh->size = h->root.size;
   1653   elfh->forced_local = 1;
   1654   return TRUE;
   1655 }
   1656 
   1657 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
   1658    function rather than to a hard-float stub.  */
   1659 
   1660 static bfd_boolean
   1661 section_allows_mips16_refs_p (asection *section)
   1662 {
   1663   const char *name;
   1664 
   1665   name = bfd_get_section_name (section->owner, section);
   1666   return (FN_STUB_P (name)
   1667 	  || CALL_STUB_P (name)
   1668 	  || CALL_FP_STUB_P (name)
   1669 	  || strcmp (name, ".pdr") == 0);
   1670 }
   1671 
   1672 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
   1673    stub section of some kind.  Return the R_SYMNDX of the target
   1674    function, or 0 if we can't decide which function that is.  */
   1675 
   1676 static unsigned long
   1677 mips16_stub_symndx (const struct elf_backend_data *bed,
   1678 		    asection *sec ATTRIBUTE_UNUSED,
   1679 		    const Elf_Internal_Rela *relocs,
   1680 		    const Elf_Internal_Rela *relend)
   1681 {
   1682   int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
   1683   const Elf_Internal_Rela *rel;
   1684 
   1685   /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
   1686      one in a compound relocation.  */
   1687   for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
   1688     if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
   1689       return ELF_R_SYM (sec->owner, rel->r_info);
   1690 
   1691   /* Otherwise trust the first relocation, whatever its kind.  This is
   1692      the traditional behavior.  */
   1693   if (relocs < relend)
   1694     return ELF_R_SYM (sec->owner, relocs->r_info);
   1695 
   1696   return 0;
   1697 }
   1698 
   1699 /* Check the mips16 stubs for a particular symbol, and see if we can
   1700    discard them.  */
   1701 
   1702 static void
   1703 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
   1704 			     struct mips_elf_link_hash_entry *h)
   1705 {
   1706   /* Dynamic symbols must use the standard call interface, in case other
   1707      objects try to call them.  */
   1708   if (h->fn_stub != NULL
   1709       && h->root.dynindx != -1)
   1710     {
   1711       mips_elf_create_shadow_symbol (info, h, ".mips16.");
   1712       h->need_fn_stub = TRUE;
   1713     }
   1714 
   1715   if (h->fn_stub != NULL
   1716       && ! h->need_fn_stub)
   1717     {
   1718       /* We don't need the fn_stub; the only references to this symbol
   1719          are 16 bit calls.  Clobber the size to 0 to prevent it from
   1720          being included in the link.  */
   1721       h->fn_stub->size = 0;
   1722       h->fn_stub->flags &= ~SEC_RELOC;
   1723       h->fn_stub->reloc_count = 0;
   1724       h->fn_stub->flags |= SEC_EXCLUDE;
   1725       h->fn_stub->output_section = bfd_abs_section_ptr;
   1726     }
   1727 
   1728   if (h->call_stub != NULL
   1729       && ELF_ST_IS_MIPS16 (h->root.other))
   1730     {
   1731       /* We don't need the call_stub; this is a 16 bit function, so
   1732          calls from other 16 bit functions are OK.  Clobber the size
   1733          to 0 to prevent it from being included in the link.  */
   1734       h->call_stub->size = 0;
   1735       h->call_stub->flags &= ~SEC_RELOC;
   1736       h->call_stub->reloc_count = 0;
   1737       h->call_stub->flags |= SEC_EXCLUDE;
   1738       h->call_stub->output_section = bfd_abs_section_ptr;
   1739     }
   1740 
   1741   if (h->call_fp_stub != NULL
   1742       && ELF_ST_IS_MIPS16 (h->root.other))
   1743     {
   1744       /* We don't need the call_stub; this is a 16 bit function, so
   1745          calls from other 16 bit functions are OK.  Clobber the size
   1746          to 0 to prevent it from being included in the link.  */
   1747       h->call_fp_stub->size = 0;
   1748       h->call_fp_stub->flags &= ~SEC_RELOC;
   1749       h->call_fp_stub->reloc_count = 0;
   1750       h->call_fp_stub->flags |= SEC_EXCLUDE;
   1751       h->call_fp_stub->output_section = bfd_abs_section_ptr;
   1752     }
   1753 }
   1754 
   1755 /* Hashtable callbacks for mips_elf_la25_stubs.  */
   1756 
   1757 static hashval_t
   1758 mips_elf_la25_stub_hash (const void *entry_)
   1759 {
   1760   const struct mips_elf_la25_stub *entry;
   1761 
   1762   entry = (struct mips_elf_la25_stub *) entry_;
   1763   return entry->h->root.root.u.def.section->id
   1764     + entry->h->root.root.u.def.value;
   1765 }
   1766 
   1767 static int
   1768 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
   1769 {
   1770   const struct mips_elf_la25_stub *entry1, *entry2;
   1771 
   1772   entry1 = (struct mips_elf_la25_stub *) entry1_;
   1773   entry2 = (struct mips_elf_la25_stub *) entry2_;
   1774   return ((entry1->h->root.root.u.def.section
   1775 	   == entry2->h->root.root.u.def.section)
   1776 	  && (entry1->h->root.root.u.def.value
   1777 	      == entry2->h->root.root.u.def.value));
   1778 }
   1779 
   1780 /* Called by the linker to set up the la25 stub-creation code.  FN is
   1781    the linker's implementation of add_stub_function.  Return true on
   1782    success.  */
   1783 
   1784 bfd_boolean
   1785 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
   1786 			  asection *(*fn) (const char *, asection *,
   1787 					   asection *))
   1788 {
   1789   struct mips_elf_link_hash_table *htab;
   1790 
   1791   htab = mips_elf_hash_table (info);
   1792   if (htab == NULL)
   1793     return FALSE;
   1794 
   1795   htab->add_stub_section = fn;
   1796   htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
   1797 				      mips_elf_la25_stub_eq, NULL);
   1798   if (htab->la25_stubs == NULL)
   1799     return FALSE;
   1800 
   1801   return TRUE;
   1802 }
   1803 
   1804 /* Return true if H is a locally-defined PIC function, in the sense
   1805    that it or its fn_stub might need $25 to be valid on entry.
   1806    Note that MIPS16 functions set up $gp using PC-relative instructions,
   1807    so they themselves never need $25 to be valid.  Only non-MIPS16
   1808    entry points are of interest here.  */
   1809 
   1810 static bfd_boolean
   1811 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
   1812 {
   1813   return ((h->root.root.type == bfd_link_hash_defined
   1814 	   || h->root.root.type == bfd_link_hash_defweak)
   1815 	  && h->root.def_regular
   1816 	  && !bfd_is_abs_section (h->root.root.u.def.section)
   1817 	  && (!ELF_ST_IS_MIPS16 (h->root.other)
   1818 	      || (h->fn_stub && h->need_fn_stub))
   1819 	  && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
   1820 	      || ELF_ST_IS_MIPS_PIC (h->root.other)));
   1821 }
   1822 
   1823 /* Set *SEC to the input section that contains the target of STUB.
   1824    Return the offset of the target from the start of that section.  */
   1825 
   1826 static bfd_vma
   1827 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
   1828 			  asection **sec)
   1829 {
   1830   if (ELF_ST_IS_MIPS16 (stub->h->root.other))
   1831     {
   1832       BFD_ASSERT (stub->h->need_fn_stub);
   1833       *sec = stub->h->fn_stub;
   1834       return 0;
   1835     }
   1836   else
   1837     {
   1838       *sec = stub->h->root.root.u.def.section;
   1839       return stub->h->root.root.u.def.value;
   1840     }
   1841 }
   1842 
   1843 /* STUB describes an la25 stub that we have decided to implement
   1844    by inserting an LUI/ADDIU pair before the target function.
   1845    Create the section and redirect the function symbol to it.  */
   1846 
   1847 static bfd_boolean
   1848 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
   1849 			 struct bfd_link_info *info)
   1850 {
   1851   struct mips_elf_link_hash_table *htab;
   1852   char *name;
   1853   asection *s, *input_section;
   1854   unsigned int align;
   1855 
   1856   htab = mips_elf_hash_table (info);
   1857   if (htab == NULL)
   1858     return FALSE;
   1859 
   1860   /* Create a unique name for the new section.  */
   1861   name = bfd_malloc (11 + sizeof (".text.stub."));
   1862   if (name == NULL)
   1863     return FALSE;
   1864   sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
   1865 
   1866   /* Create the section.  */
   1867   mips_elf_get_la25_target (stub, &input_section);
   1868   s = htab->add_stub_section (name, input_section,
   1869 			      input_section->output_section);
   1870   if (s == NULL)
   1871     return FALSE;
   1872 
   1873   /* Make sure that any padding goes before the stub.  */
   1874   align = input_section->alignment_power;
   1875   if (!bfd_set_section_alignment (s->owner, s, align))
   1876     return FALSE;
   1877   if (align > 3)
   1878     s->size = (1 << align) - 8;
   1879 
   1880   /* Create a symbol for the stub.  */
   1881   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
   1882   stub->stub_section = s;
   1883   stub->offset = s->size;
   1884 
   1885   /* Allocate room for it.  */
   1886   s->size += 8;
   1887   return TRUE;
   1888 }
   1889 
   1890 /* STUB describes an la25 stub that we have decided to implement
   1891    with a separate trampoline.  Allocate room for it and redirect
   1892    the function symbol to it.  */
   1893 
   1894 static bfd_boolean
   1895 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
   1896 			      struct bfd_link_info *info)
   1897 {
   1898   struct mips_elf_link_hash_table *htab;
   1899   asection *s;
   1900 
   1901   htab = mips_elf_hash_table (info);
   1902   if (htab == NULL)
   1903     return FALSE;
   1904 
   1905   /* Create a trampoline section, if we haven't already.  */
   1906   s = htab->strampoline;
   1907   if (s == NULL)
   1908     {
   1909       asection *input_section = stub->h->root.root.u.def.section;
   1910       s = htab->add_stub_section (".text", NULL,
   1911 				  input_section->output_section);
   1912       if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
   1913 	return FALSE;
   1914       htab->strampoline = s;
   1915     }
   1916 
   1917   /* Create a symbol for the stub.  */
   1918   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
   1919   stub->stub_section = s;
   1920   stub->offset = s->size;
   1921 
   1922   /* Allocate room for it.  */
   1923   s->size += 16;
   1924   return TRUE;
   1925 }
   1926 
   1927 /* H describes a symbol that needs an la25 stub.  Make sure that an
   1928    appropriate stub exists and point H at it.  */
   1929 
   1930 static bfd_boolean
   1931 mips_elf_add_la25_stub (struct bfd_link_info *info,
   1932 			struct mips_elf_link_hash_entry *h)
   1933 {
   1934   struct mips_elf_link_hash_table *htab;
   1935   struct mips_elf_la25_stub search, *stub;
   1936   bfd_boolean use_trampoline_p;
   1937   asection *s;
   1938   bfd_vma value;
   1939   void **slot;
   1940 
   1941   /* Describe the stub we want.  */
   1942   search.stub_section = NULL;
   1943   search.offset = 0;
   1944   search.h = h;
   1945 
   1946   /* See if we've already created an equivalent stub.  */
   1947   htab = mips_elf_hash_table (info);
   1948   if (htab == NULL)
   1949     return FALSE;
   1950 
   1951   slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
   1952   if (slot == NULL)
   1953     return FALSE;
   1954 
   1955   stub = (struct mips_elf_la25_stub *) *slot;
   1956   if (stub != NULL)
   1957     {
   1958       /* We can reuse the existing stub.  */
   1959       h->la25_stub = stub;
   1960       return TRUE;
   1961     }
   1962 
   1963   /* Create a permanent copy of ENTRY and add it to the hash table.  */
   1964   stub = bfd_malloc (sizeof (search));
   1965   if (stub == NULL)
   1966     return FALSE;
   1967   *stub = search;
   1968   *slot = stub;
   1969 
   1970   /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
   1971      of the section and if we would need no more than 2 nops.  */
   1972   value = mips_elf_get_la25_target (stub, &s);
   1973   use_trampoline_p = (value != 0 || s->alignment_power > 4);
   1974 
   1975   h->la25_stub = stub;
   1976   return (use_trampoline_p
   1977 	  ? mips_elf_add_la25_trampoline (stub, info)
   1978 	  : mips_elf_add_la25_intro (stub, info));
   1979 }
   1980 
   1981 /* A mips_elf_link_hash_traverse callback that is called before sizing
   1982    sections.  DATA points to a mips_htab_traverse_info structure.  */
   1983 
   1984 static bfd_boolean
   1985 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
   1986 {
   1987   struct mips_htab_traverse_info *hti;
   1988 
   1989   hti = (struct mips_htab_traverse_info *) data;
   1990   if (!bfd_link_relocatable (hti->info))
   1991     mips_elf_check_mips16_stubs (hti->info, h);
   1992 
   1993   if (mips_elf_local_pic_function_p (h))
   1994     {
   1995       /* PR 12845: If H is in a section that has been garbage
   1996 	 collected it will have its output section set to *ABS*.  */
   1997       if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
   1998 	return TRUE;
   1999 
   2000       /* H is a function that might need $25 to be valid on entry.
   2001 	 If we're creating a non-PIC relocatable object, mark H as
   2002 	 being PIC.  If we're creating a non-relocatable object with
   2003 	 non-PIC branches and jumps to H, make sure that H has an la25
   2004 	 stub.  */
   2005       if (bfd_link_relocatable (hti->info))
   2006 	{
   2007 	  if (!PIC_OBJECT_P (hti->output_bfd))
   2008 	    h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
   2009 	}
   2010       else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
   2011 	{
   2012 	  hti->error = TRUE;
   2013 	  return FALSE;
   2014 	}
   2015     }
   2016   return TRUE;
   2017 }
   2018 
   2019 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
   2021    Most mips16 instructions are 16 bits, but these instructions
   2022    are 32 bits.
   2023 
   2024    The format of these instructions is:
   2025 
   2026    +--------------+--------------------------------+
   2027    |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
   2028    +--------------+--------------------------------+
   2029    |                Immediate  15:0                |
   2030    +-----------------------------------------------+
   2031 
   2032    JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
   2033    Note that the immediate value in the first word is swapped.
   2034 
   2035    When producing a relocatable object file, R_MIPS16_26 is
   2036    handled mostly like R_MIPS_26.  In particular, the addend is
   2037    stored as a straight 26-bit value in a 32-bit instruction.
   2038    (gas makes life simpler for itself by never adjusting a
   2039    R_MIPS16_26 reloc to be against a section, so the addend is
   2040    always zero).  However, the 32 bit instruction is stored as 2
   2041    16-bit values, rather than a single 32-bit value.  In a
   2042    big-endian file, the result is the same; in a little-endian
   2043    file, the two 16-bit halves of the 32 bit value are swapped.
   2044    This is so that a disassembler can recognize the jal
   2045    instruction.
   2046 
   2047    When doing a final link, R_MIPS16_26 is treated as a 32 bit
   2048    instruction stored as two 16-bit values.  The addend A is the
   2049    contents of the targ26 field.  The calculation is the same as
   2050    R_MIPS_26.  When storing the calculated value, reorder the
   2051    immediate value as shown above, and don't forget to store the
   2052    value as two 16-bit values.
   2053 
   2054    To put it in MIPS ABI terms, the relocation field is T-targ26-16,
   2055    defined as
   2056 
   2057    big-endian:
   2058    +--------+----------------------+
   2059    |        |                      |
   2060    |        |    targ26-16         |
   2061    |31    26|25                   0|
   2062    +--------+----------------------+
   2063 
   2064    little-endian:
   2065    +----------+------+-------------+
   2066    |          |      |             |
   2067    |  sub1    |      |     sub2    |
   2068    |0        9|10  15|16         31|
   2069    +----------+--------------------+
   2070    where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
   2071    ((sub1 << 16) | sub2)).
   2072 
   2073    When producing a relocatable object file, the calculation is
   2074    (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
   2075    When producing a fully linked file, the calculation is
   2076    let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
   2077    ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
   2078 
   2079    The table below lists the other MIPS16 instruction relocations.
   2080    Each one is calculated in the same way as the non-MIPS16 relocation
   2081    given on the right, but using the extended MIPS16 layout of 16-bit
   2082    immediate fields:
   2083 
   2084 	R_MIPS16_GPREL		R_MIPS_GPREL16
   2085 	R_MIPS16_GOT16		R_MIPS_GOT16
   2086 	R_MIPS16_CALL16		R_MIPS_CALL16
   2087 	R_MIPS16_HI16		R_MIPS_HI16
   2088 	R_MIPS16_LO16		R_MIPS_LO16
   2089 
   2090    A typical instruction will have a format like this:
   2091 
   2092    +--------------+--------------------------------+
   2093    |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
   2094    +--------------+--------------------------------+
   2095    |    Major     |   rx   |   ry   |   Imm  4:0   |
   2096    +--------------+--------------------------------+
   2097 
   2098    EXTEND is the five bit value 11110.  Major is the instruction
   2099    opcode.
   2100 
   2101    All we need to do here is shuffle the bits appropriately.
   2102    As above, the two 16-bit halves must be swapped on a
   2103    little-endian system.
   2104 
   2105    Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
   2106    relocatable field is shifted by 1 rather than 2 and the same bit
   2107    shuffling is done as with the relocations above.  */
   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     case R_MIPS16_PC16_S1:
   2128       return TRUE;
   2129 
   2130     default:
   2131       return FALSE;
   2132     }
   2133 }
   2134 
   2135 /* Check if a microMIPS reloc.  */
   2136 
   2137 static inline bfd_boolean
   2138 micromips_reloc_p (unsigned int r_type)
   2139 {
   2140   return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
   2141 }
   2142 
   2143 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
   2144    on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1
   2145    and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.  */
   2146 
   2147 static inline bfd_boolean
   2148 micromips_reloc_shuffle_p (unsigned int r_type)
   2149 {
   2150   return (micromips_reloc_p (r_type)
   2151 	  && r_type != R_MICROMIPS_PC7_S1
   2152 	  && r_type != R_MICROMIPS_PC10_S1);
   2153 }
   2154 
   2155 static inline bfd_boolean
   2156 got16_reloc_p (int r_type)
   2157 {
   2158   return (r_type == R_MIPS_GOT16
   2159 	  || r_type == R_MIPS16_GOT16
   2160 	  || r_type == R_MICROMIPS_GOT16);
   2161 }
   2162 
   2163 static inline bfd_boolean
   2164 call16_reloc_p (int r_type)
   2165 {
   2166   return (r_type == R_MIPS_CALL16
   2167 	  || r_type == R_MIPS16_CALL16
   2168 	  || r_type == R_MICROMIPS_CALL16);
   2169 }
   2170 
   2171 static inline bfd_boolean
   2172 got_disp_reloc_p (unsigned int r_type)
   2173 {
   2174   return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
   2175 }
   2176 
   2177 static inline bfd_boolean
   2178 got_page_reloc_p (unsigned int r_type)
   2179 {
   2180   return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
   2181 }
   2182 
   2183 static inline bfd_boolean
   2184 got_lo16_reloc_p (unsigned int r_type)
   2185 {
   2186   return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
   2187 }
   2188 
   2189 static inline bfd_boolean
   2190 call_hi16_reloc_p (unsigned int r_type)
   2191 {
   2192   return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
   2193 }
   2194 
   2195 static inline bfd_boolean
   2196 call_lo16_reloc_p (unsigned int r_type)
   2197 {
   2198   return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
   2199 }
   2200 
   2201 static inline bfd_boolean
   2202 hi16_reloc_p (int r_type)
   2203 {
   2204   return (r_type == R_MIPS_HI16
   2205 	  || r_type == R_MIPS16_HI16
   2206 	  || r_type == R_MICROMIPS_HI16
   2207 	  || r_type == R_MIPS_PCHI16);
   2208 }
   2209 
   2210 static inline bfd_boolean
   2211 lo16_reloc_p (int r_type)
   2212 {
   2213   return (r_type == R_MIPS_LO16
   2214 	  || r_type == R_MIPS16_LO16
   2215 	  || r_type == R_MICROMIPS_LO16
   2216 	  || r_type == R_MIPS_PCLO16);
   2217 }
   2218 
   2219 static inline bfd_boolean
   2220 mips16_call_reloc_p (int r_type)
   2221 {
   2222   return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
   2223 }
   2224 
   2225 static inline bfd_boolean
   2226 jal_reloc_p (int r_type)
   2227 {
   2228   return (r_type == R_MIPS_26
   2229 	  || r_type == R_MIPS16_26
   2230 	  || r_type == R_MICROMIPS_26_S1);
   2231 }
   2232 
   2233 static inline bfd_boolean
   2234 b_reloc_p (int r_type)
   2235 {
   2236   return (r_type == R_MIPS_PC26_S2
   2237 	  || r_type == R_MIPS_PC21_S2
   2238 	  || r_type == R_MIPS_PC16
   2239 	  || r_type == R_MIPS_GNU_REL16_S2
   2240 	  || r_type == R_MIPS16_PC16_S1);
   2241 }
   2242 
   2243 static inline bfd_boolean
   2244 aligned_pcrel_reloc_p (int r_type)
   2245 {
   2246   return (r_type == R_MIPS_PC18_S3
   2247 	  || r_type == R_MIPS_PC19_S2);
   2248 }
   2249 
   2250 static inline bfd_boolean
   2251 mips16_branch_reloc_p (int r_type)
   2252 {
   2253   return (r_type == R_MIPS16_26
   2254 	  || r_type == R_MIPS16_PC16_S1);
   2255 }
   2256 
   2257 static inline bfd_boolean
   2258 micromips_branch_reloc_p (int r_type)
   2259 {
   2260   return (r_type == R_MICROMIPS_26_S1
   2261 	  || r_type == R_MICROMIPS_PC16_S1
   2262 	  || r_type == R_MICROMIPS_PC10_S1
   2263 	  || r_type == R_MICROMIPS_PC7_S1);
   2264 }
   2265 
   2266 static inline bfd_boolean
   2267 tls_gd_reloc_p (unsigned int r_type)
   2268 {
   2269   return (r_type == R_MIPS_TLS_GD
   2270 	  || r_type == R_MIPS16_TLS_GD
   2271 	  || r_type == R_MICROMIPS_TLS_GD);
   2272 }
   2273 
   2274 static inline bfd_boolean
   2275 tls_ldm_reloc_p (unsigned int r_type)
   2276 {
   2277   return (r_type == R_MIPS_TLS_LDM
   2278 	  || r_type == R_MIPS16_TLS_LDM
   2279 	  || r_type == R_MICROMIPS_TLS_LDM);
   2280 }
   2281 
   2282 static inline bfd_boolean
   2283 tls_gottprel_reloc_p (unsigned int r_type)
   2284 {
   2285   return (r_type == R_MIPS_TLS_GOTTPREL
   2286 	  || r_type == R_MIPS16_TLS_GOTTPREL
   2287 	  || r_type == R_MICROMIPS_TLS_GOTTPREL);
   2288 }
   2289 
   2290 void
   2291 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
   2292 			       bfd_boolean jal_shuffle, bfd_byte *data)
   2293 {
   2294   bfd_vma first, second, val;
   2295 
   2296   if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
   2297     return;
   2298 
   2299   /* Pick up the first and second halfwords of the instruction.  */
   2300   first = bfd_get_16 (abfd, data);
   2301   second = bfd_get_16 (abfd, data + 2);
   2302   if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
   2303     val = first << 16 | second;
   2304   else if (r_type != R_MIPS16_26)
   2305     val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
   2306 	   | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
   2307   else
   2308     val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
   2309 	   | ((first & 0x1f) << 21) | second);
   2310   bfd_put_32 (abfd, val, data);
   2311 }
   2312 
   2313 void
   2314 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
   2315 			     bfd_boolean jal_shuffle, bfd_byte *data)
   2316 {
   2317   bfd_vma first, second, val;
   2318 
   2319   if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
   2320     return;
   2321 
   2322   val = bfd_get_32 (abfd, data);
   2323   if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
   2324     {
   2325       second = val & 0xffff;
   2326       first = val >> 16;
   2327     }
   2328   else if (r_type != R_MIPS16_26)
   2329     {
   2330       second = ((val >> 11) & 0xffe0) | (val & 0x1f);
   2331       first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
   2332     }
   2333   else
   2334     {
   2335       second = val & 0xffff;
   2336       first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
   2337 	       | ((val >> 21) & 0x1f);
   2338     }
   2339   bfd_put_16 (abfd, second, data + 2);
   2340   bfd_put_16 (abfd, first, data);
   2341 }
   2342 
   2343 bfd_reloc_status_type
   2344 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
   2345 			       arelent *reloc_entry, asection *input_section,
   2346 			       bfd_boolean relocatable, void *data, bfd_vma gp)
   2347 {
   2348   bfd_vma relocation;
   2349   bfd_signed_vma val;
   2350   bfd_reloc_status_type status;
   2351 
   2352   if (bfd_is_com_section (symbol->section))
   2353     relocation = 0;
   2354   else
   2355     relocation = symbol->value;
   2356 
   2357   relocation += symbol->section->output_section->vma;
   2358   relocation += symbol->section->output_offset;
   2359 
   2360   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2361     return bfd_reloc_outofrange;
   2362 
   2363   /* Set val to the offset into the section or symbol.  */
   2364   val = reloc_entry->addend;
   2365 
   2366   _bfd_mips_elf_sign_extend (val, 16);
   2367 
   2368   /* Adjust val for the final section location and GP value.  If we
   2369      are producing relocatable output, we don't want to do this for
   2370      an external symbol.  */
   2371   if (! relocatable
   2372       || (symbol->flags & BSF_SECTION_SYM) != 0)
   2373     val += relocation - gp;
   2374 
   2375   if (reloc_entry->howto->partial_inplace)
   2376     {
   2377       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
   2378 				       (bfd_byte *) data
   2379 				       + reloc_entry->address);
   2380       if (status != bfd_reloc_ok)
   2381 	return status;
   2382     }
   2383   else
   2384     reloc_entry->addend = val;
   2385 
   2386   if (relocatable)
   2387     reloc_entry->address += input_section->output_offset;
   2388 
   2389   return bfd_reloc_ok;
   2390 }
   2391 
   2392 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
   2393    R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
   2394    that contains the relocation field and DATA points to the start of
   2395    INPUT_SECTION.  */
   2396 
   2397 struct mips_hi16
   2398 {
   2399   struct mips_hi16 *next;
   2400   bfd_byte *data;
   2401   asection *input_section;
   2402   arelent rel;
   2403 };
   2404 
   2405 /* FIXME: This should not be a static variable.  */
   2406 
   2407 static struct mips_hi16 *mips_hi16_list;
   2408 
   2409 /* A howto special_function for REL *HI16 relocations.  We can only
   2410    calculate the correct value once we've seen the partnering
   2411    *LO16 relocation, so just save the information for later.
   2412 
   2413    The ABI requires that the *LO16 immediately follow the *HI16.
   2414    However, as a GNU extension, we permit an arbitrary number of
   2415    *HI16s to be associated with a single *LO16.  This significantly
   2416    simplies the relocation handling in gcc.  */
   2417 
   2418 bfd_reloc_status_type
   2419 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   2420 			  asymbol *symbol ATTRIBUTE_UNUSED, void *data,
   2421 			  asection *input_section, bfd *output_bfd,
   2422 			  char **error_message ATTRIBUTE_UNUSED)
   2423 {
   2424   struct mips_hi16 *n;
   2425 
   2426   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2427     return bfd_reloc_outofrange;
   2428 
   2429   n = bfd_malloc (sizeof *n);
   2430   if (n == NULL)
   2431     return bfd_reloc_outofrange;
   2432 
   2433   n->next = mips_hi16_list;
   2434   n->data = data;
   2435   n->input_section = input_section;
   2436   n->rel = *reloc_entry;
   2437   mips_hi16_list = n;
   2438 
   2439   if (output_bfd != NULL)
   2440     reloc_entry->address += input_section->output_offset;
   2441 
   2442   return bfd_reloc_ok;
   2443 }
   2444 
   2445 /* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
   2446    like any other 16-bit relocation when applied to global symbols, but is
   2447    treated in the same as R_MIPS_HI16 when applied to local symbols.  */
   2448 
   2449 bfd_reloc_status_type
   2450 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2451 			   void *data, asection *input_section,
   2452 			   bfd *output_bfd, char **error_message)
   2453 {
   2454   if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
   2455       || bfd_is_und_section (bfd_get_section (symbol))
   2456       || bfd_is_com_section (bfd_get_section (symbol)))
   2457     /* The relocation is against a global symbol.  */
   2458     return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2459 					input_section, output_bfd,
   2460 					error_message);
   2461 
   2462   return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
   2463 				   input_section, output_bfd, error_message);
   2464 }
   2465 
   2466 /* A howto special_function for REL *LO16 relocations.  The *LO16 itself
   2467    is a straightforward 16 bit inplace relocation, but we must deal with
   2468    any partnering high-part relocations as well.  */
   2469 
   2470 bfd_reloc_status_type
   2471 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2472 			  void *data, asection *input_section,
   2473 			  bfd *output_bfd, char **error_message)
   2474 {
   2475   bfd_vma vallo;
   2476   bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
   2477 
   2478   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2479     return bfd_reloc_outofrange;
   2480 
   2481   _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
   2482 				 location);
   2483   vallo = bfd_get_32 (abfd, location);
   2484   _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
   2485 			       location);
   2486 
   2487   while (mips_hi16_list != NULL)
   2488     {
   2489       bfd_reloc_status_type ret;
   2490       struct mips_hi16 *hi;
   2491 
   2492       hi = mips_hi16_list;
   2493 
   2494       /* R_MIPS*_GOT16 relocations are something of a special case.  We
   2495 	 want to install the addend in the same way as for a R_MIPS*_HI16
   2496 	 relocation (with a rightshift of 16).  However, since GOT16
   2497 	 relocations can also be used with global symbols, their howto
   2498 	 has a rightshift of 0.  */
   2499       if (hi->rel.howto->type == R_MIPS_GOT16)
   2500 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
   2501       else if (hi->rel.howto->type == R_MIPS16_GOT16)
   2502 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
   2503       else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
   2504 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
   2505 
   2506       /* VALLO is a signed 16-bit number.  Bias it by 0x8000 so that any
   2507 	 carry or borrow will induce a change of +1 or -1 in the high part.  */
   2508       hi->rel.addend += (vallo + 0x8000) & 0xffff;
   2509 
   2510       ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
   2511 					 hi->input_section, output_bfd,
   2512 					 error_message);
   2513       if (ret != bfd_reloc_ok)
   2514 	return ret;
   2515 
   2516       mips_hi16_list = hi->next;
   2517       free (hi);
   2518     }
   2519 
   2520   return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2521 				      input_section, output_bfd,
   2522 				      error_message);
   2523 }
   2524 
   2525 /* A generic howto special_function.  This calculates and installs the
   2526    relocation itself, thus avoiding the oft-discussed problems in
   2527    bfd_perform_relocation and bfd_install_relocation.  */
   2528 
   2529 bfd_reloc_status_type
   2530 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   2531 			     asymbol *symbol, void *data ATTRIBUTE_UNUSED,
   2532 			     asection *input_section, bfd *output_bfd,
   2533 			     char **error_message ATTRIBUTE_UNUSED)
   2534 {
   2535   bfd_signed_vma val;
   2536   bfd_reloc_status_type status;
   2537   bfd_boolean relocatable;
   2538 
   2539   relocatable = (output_bfd != NULL);
   2540 
   2541   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2542     return bfd_reloc_outofrange;
   2543 
   2544   /* Build up the field adjustment in VAL.  */
   2545   val = 0;
   2546   if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
   2547     {
   2548       /* Either we're calculating the final field value or we have a
   2549 	 relocation against a section symbol.  Add in the section's
   2550 	 offset or address.  */
   2551       val += symbol->section->output_section->vma;
   2552       val += symbol->section->output_offset;
   2553     }
   2554 
   2555   if (!relocatable)
   2556     {
   2557       /* We're calculating the final field value.  Add in the symbol's value
   2558 	 and, if pc-relative, subtract the address of the field itself.  */
   2559       val += symbol->value;
   2560       if (reloc_entry->howto->pc_relative)
   2561 	{
   2562 	  val -= input_section->output_section->vma;
   2563 	  val -= input_section->output_offset;
   2564 	  val -= reloc_entry->address;
   2565 	}
   2566     }
   2567 
   2568   /* VAL is now the final adjustment.  If we're keeping this relocation
   2569      in the output file, and if the relocation uses a separate addend,
   2570      we just need to add VAL to that addend.  Otherwise we need to add
   2571      VAL to the relocation field itself.  */
   2572   if (relocatable && !reloc_entry->howto->partial_inplace)
   2573     reloc_entry->addend += val;
   2574   else
   2575     {
   2576       bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
   2577 
   2578       /* Add in the separate addend, if any.  */
   2579       val += reloc_entry->addend;
   2580 
   2581       /* Add VAL to the relocation field.  */
   2582       _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
   2583 				     location);
   2584       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
   2585 				       location);
   2586       _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
   2587 				   location);
   2588 
   2589       if (status != bfd_reloc_ok)
   2590 	return status;
   2591     }
   2592 
   2593   if (relocatable)
   2594     reloc_entry->address += input_section->output_offset;
   2595 
   2596   return bfd_reloc_ok;
   2597 }
   2598 
   2599 /* Swap an entry in a .gptab section.  Note that these routines rely
   2601    on the equivalence of the two elements of the union.  */
   2602 
   2603 static void
   2604 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
   2605 			      Elf32_gptab *in)
   2606 {
   2607   in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
   2608   in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
   2609 }
   2610 
   2611 static void
   2612 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
   2613 			       Elf32_External_gptab *ex)
   2614 {
   2615   H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
   2616   H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
   2617 }
   2618 
   2619 static void
   2620 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
   2621 				Elf32_External_compact_rel *ex)
   2622 {
   2623   H_PUT_32 (abfd, in->id1, ex->id1);
   2624   H_PUT_32 (abfd, in->num, ex->num);
   2625   H_PUT_32 (abfd, in->id2, ex->id2);
   2626   H_PUT_32 (abfd, in->offset, ex->offset);
   2627   H_PUT_32 (abfd, in->reserved0, ex->reserved0);
   2628   H_PUT_32 (abfd, in->reserved1, ex->reserved1);
   2629 }
   2630 
   2631 static void
   2632 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
   2633 			   Elf32_External_crinfo *ex)
   2634 {
   2635   unsigned long l;
   2636 
   2637   l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
   2638        | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
   2639        | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
   2640        | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
   2641   H_PUT_32 (abfd, l, ex->info);
   2642   H_PUT_32 (abfd, in->konst, ex->konst);
   2643   H_PUT_32 (abfd, in->vaddr, ex->vaddr);
   2644 }
   2645 
   2646 /* A .reginfo section holds a single Elf32_RegInfo structure.  These
   2648    routines swap this structure in and out.  They are used outside of
   2649    BFD, so they are globally visible.  */
   2650 
   2651 void
   2652 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
   2653 				Elf32_RegInfo *in)
   2654 {
   2655   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
   2656   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
   2657   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
   2658   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
   2659   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
   2660   in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
   2661 }
   2662 
   2663 void
   2664 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
   2665 				 Elf32_External_RegInfo *ex)
   2666 {
   2667   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
   2668   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
   2669   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
   2670   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
   2671   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
   2672   H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
   2673 }
   2674 
   2675 /* In the 64 bit ABI, the .MIPS.options section holds register
   2676    information in an Elf64_Reginfo structure.  These routines swap
   2677    them in and out.  They are globally visible because they are used
   2678    outside of BFD.  These routines are here so that gas can call them
   2679    without worrying about whether the 64 bit ABI has been included.  */
   2680 
   2681 void
   2682 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
   2683 				Elf64_Internal_RegInfo *in)
   2684 {
   2685   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
   2686   in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
   2687   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
   2688   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
   2689   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
   2690   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
   2691   in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
   2692 }
   2693 
   2694 void
   2695 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
   2696 				 Elf64_External_RegInfo *ex)
   2697 {
   2698   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
   2699   H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
   2700   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
   2701   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
   2702   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
   2703   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
   2704   H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
   2705 }
   2706 
   2707 /* Swap in an options header.  */
   2708 
   2709 void
   2710 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
   2711 			      Elf_Internal_Options *in)
   2712 {
   2713   in->kind = H_GET_8 (abfd, ex->kind);
   2714   in->size = H_GET_8 (abfd, ex->size);
   2715   in->section = H_GET_16 (abfd, ex->section);
   2716   in->info = H_GET_32 (abfd, ex->info);
   2717 }
   2718 
   2719 /* Swap out an options header.  */
   2720 
   2721 void
   2722 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
   2723 			       Elf_External_Options *ex)
   2724 {
   2725   H_PUT_8 (abfd, in->kind, ex->kind);
   2726   H_PUT_8 (abfd, in->size, ex->size);
   2727   H_PUT_16 (abfd, in->section, ex->section);
   2728   H_PUT_32 (abfd, in->info, ex->info);
   2729 }
   2730 
   2731 /* Swap in an abiflags structure.  */
   2732 
   2733 void
   2734 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
   2735 				  const Elf_External_ABIFlags_v0 *ex,
   2736 				  Elf_Internal_ABIFlags_v0 *in)
   2737 {
   2738   in->version = H_GET_16 (abfd, ex->version);
   2739   in->isa_level = H_GET_8 (abfd, ex->isa_level);
   2740   in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
   2741   in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
   2742   in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
   2743   in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
   2744   in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
   2745   in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
   2746   in->ases = H_GET_32 (abfd, ex->ases);
   2747   in->flags1 = H_GET_32 (abfd, ex->flags1);
   2748   in->flags2 = H_GET_32 (abfd, ex->flags2);
   2749 }
   2750 
   2751 /* Swap out an abiflags structure.  */
   2752 
   2753 void
   2754 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
   2755 				   const Elf_Internal_ABIFlags_v0 *in,
   2756 				   Elf_External_ABIFlags_v0 *ex)
   2757 {
   2758   H_PUT_16 (abfd, in->version, ex->version);
   2759   H_PUT_8 (abfd, in->isa_level, ex->isa_level);
   2760   H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
   2761   H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
   2762   H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
   2763   H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
   2764   H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
   2765   H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
   2766   H_PUT_32 (abfd, in->ases, ex->ases);
   2767   H_PUT_32 (abfd, in->flags1, ex->flags1);
   2768   H_PUT_32 (abfd, in->flags2, ex->flags2);
   2769 }
   2770 
   2771 /* This function is called via qsort() to sort the dynamic relocation
   2773    entries by increasing r_symndx value.  */
   2774 
   2775 static int
   2776 sort_dynamic_relocs (const void *arg1, const void *arg2)
   2777 {
   2778   Elf_Internal_Rela int_reloc1;
   2779   Elf_Internal_Rela int_reloc2;
   2780   int diff;
   2781 
   2782   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
   2783   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
   2784 
   2785   diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
   2786   if (diff != 0)
   2787     return diff;
   2788 
   2789   if (int_reloc1.r_offset < int_reloc2.r_offset)
   2790     return -1;
   2791   if (int_reloc1.r_offset > int_reloc2.r_offset)
   2792     return 1;
   2793   return 0;
   2794 }
   2795 
   2796 /* Like sort_dynamic_relocs, but used for elf64 relocations.  */
   2797 
   2798 static int
   2799 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
   2800 			const void *arg2 ATTRIBUTE_UNUSED)
   2801 {
   2802 #ifdef BFD64
   2803   Elf_Internal_Rela int_reloc1[3];
   2804   Elf_Internal_Rela int_reloc2[3];
   2805 
   2806   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
   2807     (reldyn_sorting_bfd, arg1, int_reloc1);
   2808   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
   2809     (reldyn_sorting_bfd, arg2, int_reloc2);
   2810 
   2811   if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
   2812     return -1;
   2813   if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
   2814     return 1;
   2815 
   2816   if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
   2817     return -1;
   2818   if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
   2819     return 1;
   2820   return 0;
   2821 #else
   2822   abort ();
   2823 #endif
   2824 }
   2825 
   2826 
   2827 /* This routine is used to write out ECOFF debugging external symbol
   2828    information.  It is called via mips_elf_link_hash_traverse.  The
   2829    ECOFF external symbol information must match the ELF external
   2830    symbol information.  Unfortunately, at this point we don't know
   2831    whether a symbol is required by reloc information, so the two
   2832    tables may wind up being different.  We must sort out the external
   2833    symbol information before we can set the final size of the .mdebug
   2834    section, and we must set the size of the .mdebug section before we
   2835    can relocate any sections, and we can't know which symbols are
   2836    required by relocation until we relocate the sections.
   2837    Fortunately, it is relatively unlikely that any symbol will be
   2838    stripped but required by a reloc.  In particular, it can not happen
   2839    when generating a final executable.  */
   2840 
   2841 static bfd_boolean
   2842 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
   2843 {
   2844   struct extsym_info *einfo = data;
   2845   bfd_boolean strip;
   2846   asection *sec, *output_section;
   2847 
   2848   if (h->root.indx == -2)
   2849     strip = FALSE;
   2850   else if ((h->root.def_dynamic
   2851 	    || h->root.ref_dynamic
   2852 	    || h->root.type == bfd_link_hash_new)
   2853 	   && !h->root.def_regular
   2854 	   && !h->root.ref_regular)
   2855     strip = TRUE;
   2856   else if (einfo->info->strip == strip_all
   2857 	   || (einfo->info->strip == strip_some
   2858 	       && bfd_hash_lookup (einfo->info->keep_hash,
   2859 				   h->root.root.root.string,
   2860 				   FALSE, FALSE) == NULL))
   2861     strip = TRUE;
   2862   else
   2863     strip = FALSE;
   2864 
   2865   if (strip)
   2866     return TRUE;
   2867 
   2868   if (h->esym.ifd == -2)
   2869     {
   2870       h->esym.jmptbl = 0;
   2871       h->esym.cobol_main = 0;
   2872       h->esym.weakext = 0;
   2873       h->esym.reserved = 0;
   2874       h->esym.ifd = ifdNil;
   2875       h->esym.asym.value = 0;
   2876       h->esym.asym.st = stGlobal;
   2877 
   2878       if (h->root.root.type == bfd_link_hash_undefined
   2879 	  || h->root.root.type == bfd_link_hash_undefweak)
   2880 	{
   2881 	  const char *name;
   2882 
   2883 	  /* Use undefined class.  Also, set class and type for some
   2884              special symbols.  */
   2885 	  name = h->root.root.root.string;
   2886 	  if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
   2887 	      || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
   2888 	    {
   2889 	      h->esym.asym.sc = scData;
   2890 	      h->esym.asym.st = stLabel;
   2891 	      h->esym.asym.value = 0;
   2892 	    }
   2893 	  else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
   2894 	    {
   2895 	      h->esym.asym.sc = scAbs;
   2896 	      h->esym.asym.st = stLabel;
   2897 	      h->esym.asym.value =
   2898 		mips_elf_hash_table (einfo->info)->procedure_count;
   2899 	    }
   2900 	  else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
   2901 	    {
   2902 	      h->esym.asym.sc = scAbs;
   2903 	      h->esym.asym.st = stLabel;
   2904 	      h->esym.asym.value = elf_gp (einfo->abfd);
   2905 	    }
   2906 	  else
   2907 	    h->esym.asym.sc = scUndefined;
   2908 	}
   2909       else if (h->root.root.type != bfd_link_hash_defined
   2910 	  && h->root.root.type != bfd_link_hash_defweak)
   2911 	h->esym.asym.sc = scAbs;
   2912       else
   2913 	{
   2914 	  const char *name;
   2915 
   2916 	  sec = h->root.root.u.def.section;
   2917 	  output_section = sec->output_section;
   2918 
   2919 	  /* When making a shared library and symbol h is the one from
   2920 	     the another shared library, OUTPUT_SECTION may be null.  */
   2921 	  if (output_section == NULL)
   2922 	    h->esym.asym.sc = scUndefined;
   2923 	  else
   2924 	    {
   2925 	      name = bfd_section_name (output_section->owner, output_section);
   2926 
   2927 	      if (strcmp (name, ".text") == 0)
   2928 		h->esym.asym.sc = scText;
   2929 	      else if (strcmp (name, ".data") == 0)
   2930 		h->esym.asym.sc = scData;
   2931 	      else if (strcmp (name, ".sdata") == 0)
   2932 		h->esym.asym.sc = scSData;
   2933 	      else if (strcmp (name, ".rodata") == 0
   2934 		       || strcmp (name, ".rdata") == 0)
   2935 		h->esym.asym.sc = scRData;
   2936 	      else if (strcmp (name, ".bss") == 0)
   2937 		h->esym.asym.sc = scBss;
   2938 	      else if (strcmp (name, ".sbss") == 0)
   2939 		h->esym.asym.sc = scSBss;
   2940 	      else if (strcmp (name, ".init") == 0)
   2941 		h->esym.asym.sc = scInit;
   2942 	      else if (strcmp (name, ".fini") == 0)
   2943 		h->esym.asym.sc = scFini;
   2944 	      else
   2945 		h->esym.asym.sc = scAbs;
   2946 	    }
   2947 	}
   2948 
   2949       h->esym.asym.reserved = 0;
   2950       h->esym.asym.index = indexNil;
   2951     }
   2952 
   2953   if (h->root.root.type == bfd_link_hash_common)
   2954     h->esym.asym.value = h->root.root.u.c.size;
   2955   else if (h->root.root.type == bfd_link_hash_defined
   2956 	   || h->root.root.type == bfd_link_hash_defweak)
   2957     {
   2958       if (h->esym.asym.sc == scCommon)
   2959 	h->esym.asym.sc = scBss;
   2960       else if (h->esym.asym.sc == scSCommon)
   2961 	h->esym.asym.sc = scSBss;
   2962 
   2963       sec = h->root.root.u.def.section;
   2964       output_section = sec->output_section;
   2965       if (output_section != NULL)
   2966 	h->esym.asym.value = (h->root.root.u.def.value
   2967 			      + sec->output_offset
   2968 			      + output_section->vma);
   2969       else
   2970 	h->esym.asym.value = 0;
   2971     }
   2972   else
   2973     {
   2974       struct mips_elf_link_hash_entry *hd = h;
   2975 
   2976       while (hd->root.root.type == bfd_link_hash_indirect)
   2977 	hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
   2978 
   2979       if (hd->needs_lazy_stub)
   2980 	{
   2981 	  BFD_ASSERT (hd->root.plt.plist != NULL);
   2982 	  BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
   2983 	  /* Set type and value for a symbol with a function stub.  */
   2984 	  h->esym.asym.st = stProc;
   2985 	  sec = hd->root.root.u.def.section;
   2986 	  if (sec == NULL)
   2987 	    h->esym.asym.value = 0;
   2988 	  else
   2989 	    {
   2990 	      output_section = sec->output_section;
   2991 	      if (output_section != NULL)
   2992 		h->esym.asym.value = (hd->root.plt.plist->stub_offset
   2993 				      + sec->output_offset
   2994 				      + output_section->vma);
   2995 	      else
   2996 		h->esym.asym.value = 0;
   2997 	    }
   2998 	}
   2999     }
   3000 
   3001   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
   3002 				      h->root.root.root.string,
   3003 				      &h->esym))
   3004     {
   3005       einfo->failed = TRUE;
   3006       return FALSE;
   3007     }
   3008 
   3009   return TRUE;
   3010 }
   3011 
   3012 /* A comparison routine used to sort .gptab entries.  */
   3013 
   3014 static int
   3015 gptab_compare (const void *p1, const void *p2)
   3016 {
   3017   const Elf32_gptab *a1 = p1;
   3018   const Elf32_gptab *a2 = p2;
   3019 
   3020   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
   3021 }
   3022 
   3023 /* Functions to manage the got entry hash table.  */
   3025 
   3026 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
   3027    hash number.  */
   3028 
   3029 static INLINE hashval_t
   3030 mips_elf_hash_bfd_vma (bfd_vma addr)
   3031 {
   3032 #ifdef BFD64
   3033   return addr + (addr >> 32);
   3034 #else
   3035   return addr;
   3036 #endif
   3037 }
   3038 
   3039 static hashval_t
   3040 mips_elf_got_entry_hash (const void *entry_)
   3041 {
   3042   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
   3043 
   3044   return (entry->symndx
   3045 	  + ((entry->tls_type == GOT_TLS_LDM) << 18)
   3046 	  + (entry->tls_type == GOT_TLS_LDM ? 0
   3047 	     : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
   3048 	     : entry->symndx >= 0 ? (entry->abfd->id
   3049 				     + mips_elf_hash_bfd_vma (entry->d.addend))
   3050 	     : entry->d.h->root.root.root.hash));
   3051 }
   3052 
   3053 static int
   3054 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
   3055 {
   3056   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
   3057   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
   3058 
   3059   return (e1->symndx == e2->symndx
   3060 	  && e1->tls_type == e2->tls_type
   3061 	  && (e1->tls_type == GOT_TLS_LDM ? TRUE
   3062 	      : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
   3063 	      : e1->symndx >= 0 ? (e1->abfd == e2->abfd
   3064 				   && e1->d.addend == e2->d.addend)
   3065 	      : e2->abfd && e1->d.h == e2->d.h));
   3066 }
   3067 
   3068 static hashval_t
   3069 mips_got_page_ref_hash (const void *ref_)
   3070 {
   3071   const struct mips_got_page_ref *ref;
   3072 
   3073   ref = (const struct mips_got_page_ref *) ref_;
   3074   return ((ref->symndx >= 0
   3075 	   ? (hashval_t) (ref->u.abfd->id + ref->symndx)
   3076 	   : ref->u.h->root.root.root.hash)
   3077 	  + mips_elf_hash_bfd_vma (ref->addend));
   3078 }
   3079 
   3080 static int
   3081 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
   3082 {
   3083   const struct mips_got_page_ref *ref1, *ref2;
   3084 
   3085   ref1 = (const struct mips_got_page_ref *) ref1_;
   3086   ref2 = (const struct mips_got_page_ref *) ref2_;
   3087   return (ref1->symndx == ref2->symndx
   3088 	  && (ref1->symndx < 0
   3089 	      ? ref1->u.h == ref2->u.h
   3090 	      : ref1->u.abfd == ref2->u.abfd)
   3091 	  && ref1->addend == ref2->addend);
   3092 }
   3093 
   3094 static hashval_t
   3095 mips_got_page_entry_hash (const void *entry_)
   3096 {
   3097   const struct mips_got_page_entry *entry;
   3098 
   3099   entry = (const struct mips_got_page_entry *) entry_;
   3100   return entry->sec->id;
   3101 }
   3102 
   3103 static int
   3104 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
   3105 {
   3106   const struct mips_got_page_entry *entry1, *entry2;
   3107 
   3108   entry1 = (const struct mips_got_page_entry *) entry1_;
   3109   entry2 = (const struct mips_got_page_entry *) entry2_;
   3110   return entry1->sec == entry2->sec;
   3111 }
   3112 
   3113 /* Create and return a new mips_got_info structure.  */
   3115 
   3116 static struct mips_got_info *
   3117 mips_elf_create_got_info (bfd *abfd)
   3118 {
   3119   struct mips_got_info *g;
   3120 
   3121   g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
   3122   if (g == NULL)
   3123     return NULL;
   3124 
   3125   g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
   3126 				    mips_elf_got_entry_eq, NULL);
   3127   if (g->got_entries == NULL)
   3128     return NULL;
   3129 
   3130   g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
   3131 				      mips_got_page_ref_eq, NULL);
   3132   if (g->got_page_refs == NULL)
   3133     return NULL;
   3134 
   3135   return g;
   3136 }
   3137 
   3138 /* Return the GOT info for input bfd ABFD, trying to create a new one if
   3139    CREATE_P and if ABFD doesn't already have a GOT.  */
   3140 
   3141 static struct mips_got_info *
   3142 mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
   3143 {
   3144   struct mips_elf_obj_tdata *tdata;
   3145 
   3146   if (!is_mips_elf (abfd))
   3147     return NULL;
   3148 
   3149   tdata = mips_elf_tdata (abfd);
   3150   if (!tdata->got && create_p)
   3151     tdata->got = mips_elf_create_got_info (abfd);
   3152   return tdata->got;
   3153 }
   3154 
   3155 /* Record that ABFD should use output GOT G.  */
   3156 
   3157 static void
   3158 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
   3159 {
   3160   struct mips_elf_obj_tdata *tdata;
   3161 
   3162   BFD_ASSERT (is_mips_elf (abfd));
   3163   tdata = mips_elf_tdata (abfd);
   3164   if (tdata->got)
   3165     {
   3166       /* The GOT structure itself and the hash table entries are
   3167 	 allocated to a bfd, but the hash tables aren't.  */
   3168       htab_delete (tdata->got->got_entries);
   3169       htab_delete (tdata->got->got_page_refs);
   3170       if (tdata->got->got_page_entries)
   3171 	htab_delete (tdata->got->got_page_entries);
   3172     }
   3173   tdata->got = g;
   3174 }
   3175 
   3176 /* Return the dynamic relocation section.  If it doesn't exist, try to
   3177    create a new it if CREATE_P, otherwise return NULL.  Also return NULL
   3178    if creation fails.  */
   3179 
   3180 static asection *
   3181 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
   3182 {
   3183   const char *dname;
   3184   asection *sreloc;
   3185   bfd *dynobj;
   3186 
   3187   dname = MIPS_ELF_REL_DYN_NAME (info);
   3188   dynobj = elf_hash_table (info)->dynobj;
   3189   sreloc = bfd_get_linker_section (dynobj, dname);
   3190   if (sreloc == NULL && create_p)
   3191     {
   3192       sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
   3193 						   (SEC_ALLOC
   3194 						    | SEC_LOAD
   3195 						    | SEC_HAS_CONTENTS
   3196 						    | SEC_IN_MEMORY
   3197 						    | SEC_LINKER_CREATED
   3198 						    | SEC_READONLY));
   3199       if (sreloc == NULL
   3200 	  || ! bfd_set_section_alignment (dynobj, sreloc,
   3201 					  MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
   3202 	return NULL;
   3203     }
   3204   return sreloc;
   3205 }
   3206 
   3207 /* Return the GOT_TLS_* type required by relocation type R_TYPE.  */
   3208 
   3209 static int
   3210 mips_elf_reloc_tls_type (unsigned int r_type)
   3211 {
   3212   if (tls_gd_reloc_p (r_type))
   3213     return GOT_TLS_GD;
   3214 
   3215   if (tls_ldm_reloc_p (r_type))
   3216     return GOT_TLS_LDM;
   3217 
   3218   if (tls_gottprel_reloc_p (r_type))
   3219     return GOT_TLS_IE;
   3220 
   3221   return GOT_TLS_NONE;
   3222 }
   3223 
   3224 /* Return the number of GOT slots needed for GOT TLS type TYPE.  */
   3225 
   3226 static int
   3227 mips_tls_got_entries (unsigned int type)
   3228 {
   3229   switch (type)
   3230     {
   3231     case GOT_TLS_GD:
   3232     case GOT_TLS_LDM:
   3233       return 2;
   3234 
   3235     case GOT_TLS_IE:
   3236       return 1;
   3237 
   3238     case GOT_TLS_NONE:
   3239       return 0;
   3240     }
   3241   abort ();
   3242 }
   3243 
   3244 /* Count the number of relocations needed for a TLS GOT entry, with
   3245    access types from TLS_TYPE, and symbol H (or a local symbol if H
   3246    is NULL).  */
   3247 
   3248 static int
   3249 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
   3250 		     struct elf_link_hash_entry *h)
   3251 {
   3252   int indx = 0;
   3253   bfd_boolean need_relocs = FALSE;
   3254   bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
   3255 
   3256   if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
   3257       && (!bfd_link_pic (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
   3258     indx = h->dynindx;
   3259 
   3260   if ((bfd_link_pic (info) || indx != 0)
   3261       && (h == NULL
   3262 	  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3263 	  || h->root.type != bfd_link_hash_undefweak))
   3264     need_relocs = TRUE;
   3265 
   3266   if (!need_relocs)
   3267     return 0;
   3268 
   3269   switch (tls_type)
   3270     {
   3271     case GOT_TLS_GD:
   3272       return indx != 0 ? 2 : 1;
   3273 
   3274     case GOT_TLS_IE:
   3275       return 1;
   3276 
   3277     case GOT_TLS_LDM:
   3278       return bfd_link_pic (info) ? 1 : 0;
   3279 
   3280     default:
   3281       return 0;
   3282     }
   3283 }
   3284 
   3285 /* Add the number of GOT entries and TLS relocations required by ENTRY
   3286    to G.  */
   3287 
   3288 static void
   3289 mips_elf_count_got_entry (struct bfd_link_info *info,
   3290 			  struct mips_got_info *g,
   3291 			  struct mips_got_entry *entry)
   3292 {
   3293   if (entry->tls_type)
   3294     {
   3295       g->tls_gotno += mips_tls_got_entries (entry->tls_type);
   3296       g->relocs += mips_tls_got_relocs (info, entry->tls_type,
   3297 					entry->symndx < 0
   3298 					? &entry->d.h->root : NULL);
   3299     }
   3300   else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
   3301     g->local_gotno += 1;
   3302   else
   3303     g->global_gotno += 1;
   3304 }
   3305 
   3306 /* Output a simple dynamic relocation into SRELOC.  */
   3307 
   3308 static void
   3309 mips_elf_output_dynamic_relocation (bfd *output_bfd,
   3310 				    asection *sreloc,
   3311 				    unsigned long reloc_index,
   3312 				    unsigned long indx,
   3313 				    int r_type,
   3314 				    bfd_vma offset)
   3315 {
   3316   Elf_Internal_Rela rel[3];
   3317 
   3318   memset (rel, 0, sizeof (rel));
   3319 
   3320   rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
   3321   rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
   3322 
   3323   if (ABI_64_P (output_bfd))
   3324     {
   3325       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
   3326 	(output_bfd, &rel[0],
   3327 	 (sreloc->contents
   3328 	  + reloc_index * sizeof (Elf64_Mips_External_Rel)));
   3329     }
   3330   else
   3331     bfd_elf32_swap_reloc_out
   3332       (output_bfd, &rel[0],
   3333        (sreloc->contents
   3334 	+ reloc_index * sizeof (Elf32_External_Rel)));
   3335 }
   3336 
   3337 /* Initialize a set of TLS GOT entries for one symbol.  */
   3338 
   3339 static void
   3340 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
   3341 			       struct mips_got_entry *entry,
   3342 			       struct mips_elf_link_hash_entry *h,
   3343 			       bfd_vma value)
   3344 {
   3345   struct mips_elf_link_hash_table *htab;
   3346   int indx;
   3347   asection *sreloc, *sgot;
   3348   bfd_vma got_offset, got_offset2;
   3349   bfd_boolean need_relocs = FALSE;
   3350 
   3351   htab = mips_elf_hash_table (info);
   3352   if (htab == NULL)
   3353     return;
   3354 
   3355   sgot = htab->sgot;
   3356 
   3357   indx = 0;
   3358   if (h != NULL)
   3359     {
   3360       bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
   3361 
   3362       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
   3363 					   &h->root)
   3364 	  && (!bfd_link_pic (info)
   3365 	      || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
   3366 	indx = h->root.dynindx;
   3367     }
   3368 
   3369   if (entry->tls_initialized)
   3370     return;
   3371 
   3372   if ((bfd_link_pic (info) || indx != 0)
   3373       && (h == NULL
   3374 	  || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
   3375 	  || h->root.type != bfd_link_hash_undefweak))
   3376     need_relocs = TRUE;
   3377 
   3378   /* MINUS_ONE means the symbol is not defined in this object.  It may not
   3379      be defined at all; assume that the value doesn't matter in that
   3380      case.  Otherwise complain if we would use the value.  */
   3381   BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
   3382 	      || h->root.root.type == bfd_link_hash_undefweak);
   3383 
   3384   /* Emit necessary relocations.  */
   3385   sreloc = mips_elf_rel_dyn_section (info, FALSE);
   3386   got_offset = entry->gotidx;
   3387 
   3388   switch (entry->tls_type)
   3389     {
   3390     case GOT_TLS_GD:
   3391       /* General Dynamic.  */
   3392       got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
   3393 
   3394       if (need_relocs)
   3395 	{
   3396 	  mips_elf_output_dynamic_relocation
   3397 	    (abfd, sreloc, sreloc->reloc_count++, indx,
   3398 	     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
   3399 	     sgot->output_offset + sgot->output_section->vma + got_offset);
   3400 
   3401 	  if (indx)
   3402 	    mips_elf_output_dynamic_relocation
   3403 	      (abfd, sreloc, sreloc->reloc_count++, indx,
   3404 	       ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
   3405 	       sgot->output_offset + sgot->output_section->vma + got_offset2);
   3406 	  else
   3407 	    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
   3408 			       sgot->contents + got_offset2);
   3409 	}
   3410       else
   3411 	{
   3412 	  MIPS_ELF_PUT_WORD (abfd, 1,
   3413 			     sgot->contents + got_offset);
   3414 	  MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
   3415 			     sgot->contents + got_offset2);
   3416 	}
   3417       break;
   3418 
   3419     case GOT_TLS_IE:
   3420       /* Initial Exec model.  */
   3421       if (need_relocs)
   3422 	{
   3423 	  if (indx == 0)
   3424 	    MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
   3425 			       sgot->contents + got_offset);
   3426 	  else
   3427 	    MIPS_ELF_PUT_WORD (abfd, 0,
   3428 			       sgot->contents + got_offset);
   3429 
   3430 	  mips_elf_output_dynamic_relocation
   3431 	    (abfd, sreloc, sreloc->reloc_count++, indx,
   3432 	     ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
   3433 	     sgot->output_offset + sgot->output_section->vma + got_offset);
   3434 	}
   3435       else
   3436 	MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
   3437 			   sgot->contents + got_offset);
   3438       break;
   3439 
   3440     case GOT_TLS_LDM:
   3441       /* The initial offset is zero, and the LD offsets will include the
   3442 	 bias by DTP_OFFSET.  */
   3443       MIPS_ELF_PUT_WORD (abfd, 0,
   3444 			 sgot->contents + got_offset
   3445 			 + MIPS_ELF_GOT_SIZE (abfd));
   3446 
   3447       if (!bfd_link_pic (info))
   3448 	MIPS_ELF_PUT_WORD (abfd, 1,
   3449 			   sgot->contents + got_offset);
   3450       else
   3451 	mips_elf_output_dynamic_relocation
   3452 	  (abfd, sreloc, sreloc->reloc_count++, indx,
   3453 	   ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
   3454 	   sgot->output_offset + sgot->output_section->vma + got_offset);
   3455       break;
   3456 
   3457     default:
   3458       abort ();
   3459     }
   3460 
   3461   entry->tls_initialized = TRUE;
   3462 }
   3463 
   3464 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
   3465    for global symbol H.  .got.plt comes before the GOT, so the offset
   3466    will be negative.  */
   3467 
   3468 static bfd_vma
   3469 mips_elf_gotplt_index (struct bfd_link_info *info,
   3470 		       struct elf_link_hash_entry *h)
   3471 {
   3472   bfd_vma got_address, got_value;
   3473   struct mips_elf_link_hash_table *htab;
   3474 
   3475   htab = mips_elf_hash_table (info);
   3476   BFD_ASSERT (htab != NULL);
   3477 
   3478   BFD_ASSERT (h->plt.plist != NULL);
   3479   BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
   3480 
   3481   /* Calculate the address of the associated .got.plt entry.  */
   3482   got_address = (htab->sgotplt->output_section->vma
   3483 		 + htab->sgotplt->output_offset
   3484 		 + (h->plt.plist->gotplt_index
   3485 		    * MIPS_ELF_GOT_SIZE (info->output_bfd)));
   3486 
   3487   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
   3488   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
   3489 	       + htab->root.hgot->root.u.def.section->output_offset
   3490 	       + htab->root.hgot->root.u.def.value);
   3491 
   3492   return got_address - got_value;
   3493 }
   3494 
   3495 /* Return the GOT offset for address VALUE.   If there is not yet a GOT
   3496    entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
   3497    create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
   3498    offset can be found.  */
   3499 
   3500 static bfd_vma
   3501 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3502 			  bfd_vma value, unsigned long r_symndx,
   3503 			  struct mips_elf_link_hash_entry *h, int r_type)
   3504 {
   3505   struct mips_elf_link_hash_table *htab;
   3506   struct mips_got_entry *entry;
   3507 
   3508   htab = mips_elf_hash_table (info);
   3509   BFD_ASSERT (htab != NULL);
   3510 
   3511   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
   3512 					   r_symndx, h, r_type);
   3513   if (!entry)
   3514     return MINUS_ONE;
   3515 
   3516   if (entry->tls_type)
   3517     mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
   3518   return entry->gotidx;
   3519 }
   3520 
   3521 /* Return the GOT index of global symbol H in the primary GOT.  */
   3522 
   3523 static bfd_vma
   3524 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
   3525 				   struct elf_link_hash_entry *h)
   3526 {
   3527   struct mips_elf_link_hash_table *htab;
   3528   long global_got_dynindx;
   3529   struct mips_got_info *g;
   3530   bfd_vma got_index;
   3531 
   3532   htab = mips_elf_hash_table (info);
   3533   BFD_ASSERT (htab != NULL);
   3534 
   3535   global_got_dynindx = 0;
   3536   if (htab->global_gotsym != NULL)
   3537     global_got_dynindx = htab->global_gotsym->dynindx;
   3538 
   3539   /* Once we determine the global GOT entry with the lowest dynamic
   3540      symbol table index, we must put all dynamic symbols with greater
   3541      indices into the primary GOT.  That makes it easy to calculate the
   3542      GOT offset.  */
   3543   BFD_ASSERT (h->dynindx >= global_got_dynindx);
   3544   g = mips_elf_bfd_got (obfd, FALSE);
   3545   got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
   3546 	       * MIPS_ELF_GOT_SIZE (obfd));
   3547   BFD_ASSERT (got_index < htab->sgot->size);
   3548 
   3549   return got_index;
   3550 }
   3551 
   3552 /* Return the GOT index for the global symbol indicated by H, which is
   3553    referenced by a relocation of type R_TYPE in IBFD.  */
   3554 
   3555 static bfd_vma
   3556 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
   3557 			   struct elf_link_hash_entry *h, int r_type)
   3558 {
   3559   struct mips_elf_link_hash_table *htab;
   3560   struct mips_got_info *g;
   3561   struct mips_got_entry lookup, *entry;
   3562   bfd_vma gotidx;
   3563 
   3564   htab = mips_elf_hash_table (info);
   3565   BFD_ASSERT (htab != NULL);
   3566 
   3567   g = mips_elf_bfd_got (ibfd, FALSE);
   3568   BFD_ASSERT (g);
   3569 
   3570   lookup.tls_type = mips_elf_reloc_tls_type (r_type);
   3571   if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
   3572     return mips_elf_primary_global_got_index (obfd, info, h);
   3573 
   3574   lookup.abfd = ibfd;
   3575   lookup.symndx = -1;
   3576   lookup.d.h = (struct mips_elf_link_hash_entry *) h;
   3577   entry = htab_find (g->got_entries, &lookup);
   3578   BFD_ASSERT (entry);
   3579 
   3580   gotidx = entry->gotidx;
   3581   BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
   3582 
   3583   if (lookup.tls_type)
   3584     {
   3585       bfd_vma value = MINUS_ONE;
   3586 
   3587       if ((h->root.type == bfd_link_hash_defined
   3588 	   || h->root.type == bfd_link_hash_defweak)
   3589 	  && h->root.u.def.section->output_section)
   3590 	value = (h->root.u.def.value
   3591 		 + h->root.u.def.section->output_offset
   3592 		 + h->root.u.def.section->output_section->vma);
   3593 
   3594       mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
   3595     }
   3596   return gotidx;
   3597 }
   3598 
   3599 /* Find a GOT page entry that points to within 32KB of VALUE.  These
   3600    entries are supposed to be placed at small offsets in the GOT, i.e.,
   3601    within 32KB of GP.  Return the index of the GOT entry, or -1 if no
   3602    entry could be created.  If OFFSETP is nonnull, use it to return the
   3603    offset of the GOT entry from VALUE.  */
   3604 
   3605 static bfd_vma
   3606 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3607 		   bfd_vma value, bfd_vma *offsetp)
   3608 {
   3609   bfd_vma page, got_index;
   3610   struct mips_got_entry *entry;
   3611 
   3612   page = (value + 0x8000) & ~(bfd_vma) 0xffff;
   3613   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
   3614 					   NULL, R_MIPS_GOT_PAGE);
   3615 
   3616   if (!entry)
   3617     return MINUS_ONE;
   3618 
   3619   got_index = entry->gotidx;
   3620 
   3621   if (offsetp)
   3622     *offsetp = value - entry->d.address;
   3623 
   3624   return got_index;
   3625 }
   3626 
   3627 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
   3628    EXTERNAL is true if the relocation was originally against a global
   3629    symbol that binds locally.  */
   3630 
   3631 static bfd_vma
   3632 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3633 		      bfd_vma value, bfd_boolean external)
   3634 {
   3635   struct mips_got_entry *entry;
   3636 
   3637   /* GOT16 relocations against local symbols are followed by a LO16
   3638      relocation; those against global symbols are not.  Thus if the
   3639      symbol was originally local, the GOT16 relocation should load the
   3640      equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
   3641   if (! external)
   3642     value = mips_elf_high (value) << 16;
   3643 
   3644   /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
   3645      R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
   3646      same in all cases.  */
   3647   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
   3648 					   NULL, R_MIPS_GOT16);
   3649   if (entry)
   3650     return entry->gotidx;
   3651   else
   3652     return MINUS_ONE;
   3653 }
   3654 
   3655 /* Returns the offset for the entry at the INDEXth position
   3656    in the GOT.  */
   3657 
   3658 static bfd_vma
   3659 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
   3660 				bfd *input_bfd, bfd_vma got_index)
   3661 {
   3662   struct mips_elf_link_hash_table *htab;
   3663   asection *sgot;
   3664   bfd_vma gp;
   3665 
   3666   htab = mips_elf_hash_table (info);
   3667   BFD_ASSERT (htab != NULL);
   3668 
   3669   sgot = htab->sgot;
   3670   gp = _bfd_get_gp_value (output_bfd)
   3671     + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
   3672 
   3673   return sgot->output_section->vma + sgot->output_offset + got_index - gp;
   3674 }
   3675 
   3676 /* Create and return a local GOT entry for VALUE, which was calculated
   3677    from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
   3678    be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
   3679    instead.  */
   3680 
   3681 static struct mips_got_entry *
   3682 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
   3683 				 bfd *ibfd, bfd_vma value,
   3684 				 unsigned long r_symndx,
   3685 				 struct mips_elf_link_hash_entry *h,
   3686 				 int r_type)
   3687 {
   3688   struct mips_got_entry lookup, *entry;
   3689   void **loc;
   3690   struct mips_got_info *g;
   3691   struct mips_elf_link_hash_table *htab;
   3692   bfd_vma gotidx;
   3693 
   3694   htab = mips_elf_hash_table (info);
   3695   BFD_ASSERT (htab != NULL);
   3696 
   3697   g = mips_elf_bfd_got (ibfd, FALSE);
   3698   if (g == NULL)
   3699     {
   3700       g = mips_elf_bfd_got (abfd, FALSE);
   3701       BFD_ASSERT (g != NULL);
   3702     }
   3703 
   3704   /* This function shouldn't be called for symbols that live in the global
   3705      area of the GOT.  */
   3706   BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
   3707 
   3708   lookup.tls_type = mips_elf_reloc_tls_type (r_type);
   3709   if (lookup.tls_type)
   3710     {
   3711       lookup.abfd = ibfd;
   3712       if (tls_ldm_reloc_p (r_type))
   3713 	{
   3714 	  lookup.symndx = 0;
   3715 	  lookup.d.addend = 0;
   3716 	}
   3717       else if (h == NULL)
   3718 	{
   3719 	  lookup.symndx = r_symndx;
   3720 	  lookup.d.addend = 0;
   3721 	}
   3722       else
   3723 	{
   3724 	  lookup.symndx = -1;
   3725 	  lookup.d.h = h;
   3726 	}
   3727 
   3728       entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
   3729       BFD_ASSERT (entry);
   3730 
   3731       gotidx = entry->gotidx;
   3732       BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
   3733 
   3734       return entry;
   3735     }
   3736 
   3737   lookup.abfd = NULL;
   3738   lookup.symndx = -1;
   3739   lookup.d.address = value;
   3740   loc = htab_find_slot (g->got_entries, &lookup, INSERT);
   3741   if (!loc)
   3742     return NULL;
   3743 
   3744   entry = (struct mips_got_entry *) *loc;
   3745   if (entry)
   3746     return entry;
   3747 
   3748   if (g->assigned_low_gotno > g->assigned_high_gotno)
   3749     {
   3750       /* We didn't allocate enough space in the GOT.  */
   3751       (*_bfd_error_handler)
   3752 	(_("not enough GOT space for local GOT entries"));
   3753       bfd_set_error (bfd_error_bad_value);
   3754       return NULL;
   3755     }
   3756 
   3757   entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
   3758   if (!entry)
   3759     return NULL;
   3760 
   3761   if (got16_reloc_p (r_type)
   3762       || call16_reloc_p (r_type)
   3763       || got_page_reloc_p (r_type)
   3764       || got_disp_reloc_p (r_type))
   3765     lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
   3766   else
   3767     lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
   3768 
   3769   *entry = lookup;
   3770   *loc = entry;
   3771 
   3772   MIPS_ELF_PUT_WORD (abfd, value, htab->sgot->contents + entry->gotidx);
   3773 
   3774   /* These GOT entries need a dynamic relocation on VxWorks.  */
   3775   if (htab->is_vxworks)
   3776     {
   3777       Elf_Internal_Rela outrel;
   3778       asection *s;
   3779       bfd_byte *rloc;
   3780       bfd_vma got_address;
   3781 
   3782       s = mips_elf_rel_dyn_section (info, FALSE);
   3783       got_address = (htab->sgot->output_section->vma
   3784 		     + htab->sgot->output_offset
   3785 		     + entry->gotidx);
   3786 
   3787       rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
   3788       outrel.r_offset = got_address;
   3789       outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
   3790       outrel.r_addend = value;
   3791       bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
   3792     }
   3793 
   3794   return entry;
   3795 }
   3796 
   3797 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
   3798    The number might be exact or a worst-case estimate, depending on how
   3799    much information is available to elf_backend_omit_section_dynsym at
   3800    the current linking stage.  */
   3801 
   3802 static bfd_size_type
   3803 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
   3804 {
   3805   bfd_size_type count;
   3806 
   3807   count = 0;
   3808   if (bfd_link_pic (info)
   3809       || elf_hash_table (info)->is_relocatable_executable)
   3810     {
   3811       asection *p;
   3812       const struct elf_backend_data *bed;
   3813 
   3814       bed = get_elf_backend_data (output_bfd);
   3815       for (p = output_bfd->sections; p ; p = p->next)
   3816 	if ((p->flags & SEC_EXCLUDE) == 0
   3817 	    && (p->flags & SEC_ALLOC) != 0
   3818 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
   3819 	  ++count;
   3820     }
   3821   return count;
   3822 }
   3823 
   3824 /* Sort the dynamic symbol table so that symbols that need GOT entries
   3825    appear towards the end.  */
   3826 
   3827 static bfd_boolean
   3828 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
   3829 {
   3830   struct mips_elf_link_hash_table *htab;
   3831   struct mips_elf_hash_sort_data hsd;
   3832   struct mips_got_info *g;
   3833 
   3834   if (elf_hash_table (info)->dynsymcount == 0)
   3835     return TRUE;
   3836 
   3837   htab = mips_elf_hash_table (info);
   3838   BFD_ASSERT (htab != NULL);
   3839 
   3840   g = htab->got_info;
   3841   if (g == NULL)
   3842     return TRUE;
   3843 
   3844   hsd.low = NULL;
   3845   hsd.max_unref_got_dynindx
   3846     = hsd.min_got_dynindx
   3847     = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
   3848   hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
   3849   mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
   3850 				elf_hash_table (info)),
   3851 			       mips_elf_sort_hash_table_f,
   3852 			       &hsd);
   3853 
   3854   /* There should have been enough room in the symbol table to
   3855      accommodate both the GOT and non-GOT symbols.  */
   3856   BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
   3857   BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
   3858 	      == elf_hash_table (info)->dynsymcount);
   3859   BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
   3860 	      == g->global_gotno);
   3861 
   3862   /* Now we know which dynamic symbol has the lowest dynamic symbol
   3863      table index in the GOT.  */
   3864   htab->global_gotsym = hsd.low;
   3865 
   3866   return TRUE;
   3867 }
   3868 
   3869 /* If H needs a GOT entry, assign it the highest available dynamic
   3870    index.  Otherwise, assign it the lowest available dynamic
   3871    index.  */
   3872 
   3873 static bfd_boolean
   3874 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
   3875 {
   3876   struct mips_elf_hash_sort_data *hsd = data;
   3877 
   3878   /* Symbols without dynamic symbol table entries aren't interesting
   3879      at all.  */
   3880   if (h->root.dynindx == -1)
   3881     return TRUE;
   3882 
   3883   switch (h->global_got_area)
   3884     {
   3885     case GGA_NONE:
   3886       h->root.dynindx = hsd->max_non_got_dynindx++;
   3887       break;
   3888 
   3889     case GGA_NORMAL:
   3890       h->root.dynindx = --hsd->min_got_dynindx;
   3891       hsd->low = (struct elf_link_hash_entry *) h;
   3892       break;
   3893 
   3894     case GGA_RELOC_ONLY:
   3895       if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
   3896 	hsd->low = (struct elf_link_hash_entry *) h;
   3897       h->root.dynindx = hsd->max_unref_got_dynindx++;
   3898       break;
   3899     }
   3900 
   3901   return TRUE;
   3902 }
   3903 
   3904 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
   3905    (which is owned by the caller and shouldn't be added to the
   3906    hash table directly).  */
   3907 
   3908 static bfd_boolean
   3909 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
   3910 			   struct mips_got_entry *lookup)
   3911 {
   3912   struct mips_elf_link_hash_table *htab;
   3913   struct mips_got_entry *entry;
   3914   struct mips_got_info *g;
   3915   void **loc, **bfd_loc;
   3916 
   3917   /* Make sure there's a slot for this entry in the master GOT.  */
   3918   htab = mips_elf_hash_table (info);
   3919   g = htab->got_info;
   3920   loc = htab_find_slot (g->got_entries, lookup, INSERT);
   3921   if (!loc)
   3922     return FALSE;
   3923 
   3924   /* Populate the entry if it isn't already.  */
   3925   entry = (struct mips_got_entry *) *loc;
   3926   if (!entry)
   3927     {
   3928       entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
   3929       if (!entry)
   3930 	return FALSE;
   3931 
   3932       lookup->tls_initialized = FALSE;
   3933       lookup->gotidx = -1;
   3934       *entry = *lookup;
   3935       *loc = entry;
   3936     }
   3937 
   3938   /* Reuse the same GOT entry for the BFD's GOT.  */
   3939   g = mips_elf_bfd_got (abfd, TRUE);
   3940   if (!g)
   3941     return FALSE;
   3942 
   3943   bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
   3944   if (!bfd_loc)
   3945     return FALSE;
   3946 
   3947   if (!*bfd_loc)
   3948     *bfd_loc = entry;
   3949   return TRUE;
   3950 }
   3951 
   3952 /* ABFD has a GOT relocation of type R_TYPE against H.  Reserve a GOT
   3953    entry for it.  FOR_CALL is true if the caller is only interested in
   3954    using the GOT entry for calls.  */
   3955 
   3956 static bfd_boolean
   3957 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
   3958 				   bfd *abfd, struct bfd_link_info *info,
   3959 				   bfd_boolean for_call, int r_type)
   3960 {
   3961   struct mips_elf_link_hash_table *htab;
   3962   struct mips_elf_link_hash_entry *hmips;
   3963   struct mips_got_entry entry;
   3964   unsigned char tls_type;
   3965 
   3966   htab = mips_elf_hash_table (info);
   3967   BFD_ASSERT (htab != NULL);
   3968 
   3969   hmips = (struct mips_elf_link_hash_entry *) h;
   3970   if (!for_call)
   3971     hmips->got_only_for_calls = FALSE;
   3972 
   3973   /* A global symbol in the GOT must also be in the dynamic symbol
   3974      table.  */
   3975   if (h->dynindx == -1)
   3976     {
   3977       switch (ELF_ST_VISIBILITY (h->other))
   3978 	{
   3979 	case STV_INTERNAL:
   3980 	case STV_HIDDEN:
   3981 	  _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
   3982 	  break;
   3983 	}
   3984       if (!bfd_elf_link_record_dynamic_symbol (info, h))
   3985 	return FALSE;
   3986     }
   3987 
   3988   tls_type = mips_elf_reloc_tls_type (r_type);
   3989   if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
   3990     hmips->global_got_area = GGA_NORMAL;
   3991 
   3992   entry.abfd = abfd;
   3993   entry.symndx = -1;
   3994   entry.d.h = (struct mips_elf_link_hash_entry *) h;
   3995   entry.tls_type = tls_type;
   3996   return mips_elf_record_got_entry (info, abfd, &entry);
   3997 }
   3998 
   3999 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
   4000    where SYMNDX is a local symbol.  Reserve a GOT entry for it.  */
   4001 
   4002 static bfd_boolean
   4003 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
   4004 				  struct bfd_link_info *info, int r_type)
   4005 {
   4006   struct mips_elf_link_hash_table *htab;
   4007   struct mips_got_info *g;
   4008   struct mips_got_entry entry;
   4009 
   4010   htab = mips_elf_hash_table (info);
   4011   BFD_ASSERT (htab != NULL);
   4012 
   4013   g = htab->got_info;
   4014   BFD_ASSERT (g != NULL);
   4015 
   4016   entry.abfd = abfd;
   4017   entry.symndx = symndx;
   4018   entry.d.addend = addend;
   4019   entry.tls_type = mips_elf_reloc_tls_type (r_type);
   4020   return mips_elf_record_got_entry (info, abfd, &entry);
   4021 }
   4022 
   4023 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
   4024    H is the symbol's hash table entry, or null if SYMNDX is local
   4025    to ABFD.  */
   4026 
   4027 static bfd_boolean
   4028 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
   4029 			      long symndx, struct elf_link_hash_entry *h,
   4030 			      bfd_signed_vma addend)
   4031 {
   4032   struct mips_elf_link_hash_table *htab;
   4033   struct mips_got_info *g1, *g2;
   4034   struct mips_got_page_ref lookup, *entry;
   4035   void **loc, **bfd_loc;
   4036 
   4037   htab = mips_elf_hash_table (info);
   4038   BFD_ASSERT (htab != NULL);
   4039 
   4040   g1 = htab->got_info;
   4041   BFD_ASSERT (g1 != NULL);
   4042 
   4043   if (h)
   4044     {
   4045       lookup.symndx = -1;
   4046       lookup.u.h = (struct mips_elf_link_hash_entry *) h;
   4047     }
   4048   else
   4049     {
   4050       lookup.symndx = symndx;
   4051       lookup.u.abfd = abfd;
   4052     }
   4053   lookup.addend = addend;
   4054   loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
   4055   if (loc == NULL)
   4056     return FALSE;
   4057 
   4058   entry = (struct mips_got_page_ref *) *loc;
   4059   if (!entry)
   4060     {
   4061       entry = bfd_alloc (abfd, sizeof (*entry));
   4062       if (!entry)
   4063 	return FALSE;
   4064 
   4065       *entry = lookup;
   4066       *loc = entry;
   4067     }
   4068 
   4069   /* Add the same entry to the BFD's GOT.  */
   4070   g2 = mips_elf_bfd_got (abfd, TRUE);
   4071   if (!g2)
   4072     return FALSE;
   4073 
   4074   bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
   4075   if (!bfd_loc)
   4076     return FALSE;
   4077 
   4078   if (!*bfd_loc)
   4079     *bfd_loc = entry;
   4080 
   4081   return TRUE;
   4082 }
   4083 
   4084 /* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
   4085 
   4086 static void
   4087 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
   4088 				       unsigned int n)
   4089 {
   4090   asection *s;
   4091   struct mips_elf_link_hash_table *htab;
   4092 
   4093   htab = mips_elf_hash_table (info);
   4094   BFD_ASSERT (htab != NULL);
   4095 
   4096   s = mips_elf_rel_dyn_section (info, FALSE);
   4097   BFD_ASSERT (s != NULL);
   4098 
   4099   if (htab->is_vxworks)
   4100     s->size += n * MIPS_ELF_RELA_SIZE (abfd);
   4101   else
   4102     {
   4103       if (s->size == 0)
   4104 	{
   4105 	  /* Make room for a null element.  */
   4106 	  s->size += MIPS_ELF_REL_SIZE (abfd);
   4107 	  ++s->reloc_count;
   4108 	}
   4109       s->size += n * MIPS_ELF_REL_SIZE (abfd);
   4110     }
   4111 }
   4112 
   4113 /* A htab_traverse callback for GOT entries, with DATA pointing to a
   4115    mips_elf_traverse_got_arg structure.  Count the number of GOT
   4116    entries and TLS relocs.  Set DATA->value to true if we need
   4117    to resolve indirect or warning symbols and then recreate the GOT.  */
   4118 
   4119 static int
   4120 mips_elf_check_recreate_got (void **entryp, void *data)
   4121 {
   4122   struct mips_got_entry *entry;
   4123   struct mips_elf_traverse_got_arg *arg;
   4124 
   4125   entry = (struct mips_got_entry *) *entryp;
   4126   arg = (struct mips_elf_traverse_got_arg *) data;
   4127   if (entry->abfd != NULL && entry->symndx == -1)
   4128     {
   4129       struct mips_elf_link_hash_entry *h;
   4130 
   4131       h = entry->d.h;
   4132       if (h->root.root.type == bfd_link_hash_indirect
   4133 	  || h->root.root.type == bfd_link_hash_warning)
   4134 	{
   4135 	  arg->value = TRUE;
   4136 	  return 0;
   4137 	}
   4138     }
   4139   mips_elf_count_got_entry (arg->info, arg->g, entry);
   4140   return 1;
   4141 }
   4142 
   4143 /* A htab_traverse callback for GOT entries, with DATA pointing to a
   4144    mips_elf_traverse_got_arg structure.  Add all entries to DATA->g,
   4145    converting entries for indirect and warning symbols into entries
   4146    for the target symbol.  Set DATA->g to null on error.  */
   4147 
   4148 static int
   4149 mips_elf_recreate_got (void **entryp, void *data)
   4150 {
   4151   struct mips_got_entry new_entry, *entry;
   4152   struct mips_elf_traverse_got_arg *arg;
   4153   void **slot;
   4154 
   4155   entry = (struct mips_got_entry *) *entryp;
   4156   arg = (struct mips_elf_traverse_got_arg *) data;
   4157   if (entry->abfd != NULL
   4158       && entry->symndx == -1
   4159       && (entry->d.h->root.root.type == bfd_link_hash_indirect
   4160 	  || entry->d.h->root.root.type == bfd_link_hash_warning))
   4161     {
   4162       struct mips_elf_link_hash_entry *h;
   4163 
   4164       new_entry = *entry;
   4165       entry = &new_entry;
   4166       h = entry->d.h;
   4167       do
   4168 	{
   4169 	  BFD_ASSERT (h->global_got_area == GGA_NONE);
   4170 	  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   4171 	}
   4172       while (h->root.root.type == bfd_link_hash_indirect
   4173 	     || h->root.root.type == bfd_link_hash_warning);
   4174       entry->d.h = h;
   4175     }
   4176   slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
   4177   if (slot == NULL)
   4178     {
   4179       arg->g = NULL;
   4180       return 0;
   4181     }
   4182   if (*slot == NULL)
   4183     {
   4184       if (entry == &new_entry)
   4185 	{
   4186 	  entry = bfd_alloc (entry->abfd, sizeof (*entry));
   4187 	  if (!entry)
   4188 	    {
   4189 	      arg->g = NULL;
   4190 	      return 0;
   4191 	    }
   4192 	  *entry = new_entry;
   4193 	}
   4194       *slot = entry;
   4195       mips_elf_count_got_entry (arg->info, arg->g, entry);
   4196     }
   4197   return 1;
   4198 }
   4199 
   4200 /* Return the maximum number of GOT page entries required for RANGE.  */
   4201 
   4202 static bfd_vma
   4203 mips_elf_pages_for_range (const struct mips_got_page_range *range)
   4204 {
   4205   return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
   4206 }
   4207 
   4208 /* Record that G requires a page entry that can reach SEC + ADDEND.  */
   4209 
   4210 static bfd_boolean
   4211 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
   4212 				asection *sec, bfd_signed_vma addend)
   4213 {
   4214   struct mips_got_info *g = arg->g;
   4215   struct mips_got_page_entry lookup, *entry;
   4216   struct mips_got_page_range **range_ptr, *range;
   4217   bfd_vma old_pages, new_pages;
   4218   void **loc;
   4219 
   4220   /* Find the mips_got_page_entry hash table entry for this section.  */
   4221   lookup.sec = sec;
   4222   loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
   4223   if (loc == NULL)
   4224     return FALSE;
   4225 
   4226   /* Create a mips_got_page_entry if this is the first time we've
   4227      seen the section.  */
   4228   entry = (struct mips_got_page_entry *) *loc;
   4229   if (!entry)
   4230     {
   4231       entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
   4232       if (!entry)
   4233 	return FALSE;
   4234 
   4235       entry->sec = sec;
   4236       *loc = entry;
   4237     }
   4238 
   4239   /* Skip over ranges whose maximum extent cannot share a page entry
   4240      with ADDEND.  */
   4241   range_ptr = &entry->ranges;
   4242   while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
   4243     range_ptr = &(*range_ptr)->next;
   4244 
   4245   /* If we scanned to the end of the list, or found a range whose
   4246      minimum extent cannot share a page entry with ADDEND, create
   4247      a new singleton range.  */
   4248   range = *range_ptr;
   4249   if (!range || addend < range->min_addend - 0xffff)
   4250     {
   4251       range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
   4252       if (!range)
   4253 	return FALSE;
   4254 
   4255       range->next = *range_ptr;
   4256       range->min_addend = addend;
   4257       range->max_addend = addend;
   4258 
   4259       *range_ptr = range;
   4260       entry->num_pages++;
   4261       g->page_gotno++;
   4262       return TRUE;
   4263     }
   4264 
   4265   /* Remember how many pages the old range contributed.  */
   4266   old_pages = mips_elf_pages_for_range (range);
   4267 
   4268   /* Update the ranges.  */
   4269   if (addend < range->min_addend)
   4270     range->min_addend = addend;
   4271   else if (addend > range->max_addend)
   4272     {
   4273       if (range->next && addend >= range->next->min_addend - 0xffff)
   4274 	{
   4275 	  old_pages += mips_elf_pages_for_range (range->next);
   4276 	  range->max_addend = range->next->max_addend;
   4277 	  range->next = range->next->next;
   4278 	}
   4279       else
   4280 	range->max_addend = addend;
   4281     }
   4282 
   4283   /* Record any change in the total estimate.  */
   4284   new_pages = mips_elf_pages_for_range (range);
   4285   if (old_pages != new_pages)
   4286     {
   4287       entry->num_pages += new_pages - old_pages;
   4288       g->page_gotno += new_pages - old_pages;
   4289     }
   4290 
   4291   return TRUE;
   4292 }
   4293 
   4294 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
   4295    and for which DATA points to a mips_elf_traverse_got_arg.  Work out
   4296    whether the page reference described by *REFP needs a GOT page entry,
   4297    and record that entry in DATA->g if so.  Set DATA->g to null on failure.  */
   4298 
   4299 static bfd_boolean
   4300 mips_elf_resolve_got_page_ref (void **refp, void *data)
   4301 {
   4302   struct mips_got_page_ref *ref;
   4303   struct mips_elf_traverse_got_arg *arg;
   4304   struct mips_elf_link_hash_table *htab;
   4305   asection *sec;
   4306   bfd_vma addend;
   4307 
   4308   ref = (struct mips_got_page_ref *) *refp;
   4309   arg = (struct mips_elf_traverse_got_arg *) data;
   4310   htab = mips_elf_hash_table (arg->info);
   4311 
   4312   if (ref->symndx < 0)
   4313     {
   4314       struct mips_elf_link_hash_entry *h;
   4315 
   4316       /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries.  */
   4317       h = ref->u.h;
   4318       if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
   4319 	return 1;
   4320 
   4321       /* Ignore undefined symbols; we'll issue an error later if
   4322 	 appropriate.  */
   4323       if (!((h->root.root.type == bfd_link_hash_defined
   4324 	     || h->root.root.type == bfd_link_hash_defweak)
   4325 	    && h->root.root.u.def.section))
   4326 	return 1;
   4327 
   4328       sec = h->root.root.u.def.section;
   4329       addend = h->root.root.u.def.value + ref->addend;
   4330     }
   4331   else
   4332     {
   4333       Elf_Internal_Sym *isym;
   4334 
   4335       /* Read in the symbol.  */
   4336       isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
   4337 				    ref->symndx);
   4338       if (isym == NULL)
   4339 	{
   4340 	  arg->g = NULL;
   4341 	  return 0;
   4342 	}
   4343 
   4344       /* Get the associated input section.  */
   4345       sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
   4346       if (sec == NULL)
   4347 	{
   4348 	  arg->g = NULL;
   4349 	  return 0;
   4350 	}
   4351 
   4352       /* If this is a mergable section, work out the section and offset
   4353 	 of the merged data.  For section symbols, the addend specifies
   4354 	 of the offset _of_ the first byte in the data, otherwise it
   4355 	 specifies the offset _from_ the first byte.  */
   4356       if (sec->flags & SEC_MERGE)
   4357 	{
   4358 	  void *secinfo;
   4359 
   4360 	  secinfo = elf_section_data (sec)->sec_info;
   4361 	  if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   4362 	    addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
   4363 						 isym->st_value + ref->addend);
   4364 	  else
   4365 	    addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
   4366 						 isym->st_value) + ref->addend;
   4367 	}
   4368       else
   4369 	addend = isym->st_value + ref->addend;
   4370     }
   4371   if (!mips_elf_record_got_page_entry (arg, sec, addend))
   4372     {
   4373       arg->g = NULL;
   4374       return 0;
   4375     }
   4376   return 1;
   4377 }
   4378 
   4379 /* If any entries in G->got_entries are for indirect or warning symbols,
   4380    replace them with entries for the target symbol.  Convert g->got_page_refs
   4381    into got_page_entry structures and estimate the number of page entries
   4382    that they require.  */
   4383 
   4384 static bfd_boolean
   4385 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
   4386 				    struct mips_got_info *g)
   4387 {
   4388   struct mips_elf_traverse_got_arg tga;
   4389   struct mips_got_info oldg;
   4390 
   4391   oldg = *g;
   4392 
   4393   tga.info = info;
   4394   tga.g = g;
   4395   tga.value = FALSE;
   4396   htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
   4397   if (tga.value)
   4398     {
   4399       *g = oldg;
   4400       g->got_entries = htab_create (htab_size (oldg.got_entries),
   4401 				    mips_elf_got_entry_hash,
   4402 				    mips_elf_got_entry_eq, NULL);
   4403       if (!g->got_entries)
   4404 	return FALSE;
   4405 
   4406       htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
   4407       if (!tga.g)
   4408 	return FALSE;
   4409 
   4410       htab_delete (oldg.got_entries);
   4411     }
   4412 
   4413   g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
   4414 					 mips_got_page_entry_eq, NULL);
   4415   if (g->got_page_entries == NULL)
   4416     return FALSE;
   4417 
   4418   tga.info = info;
   4419   tga.g = g;
   4420   htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
   4421 
   4422   return TRUE;
   4423 }
   4424 
   4425 /* Return true if a GOT entry for H should live in the local rather than
   4426    global GOT area.  */
   4427 
   4428 static bfd_boolean
   4429 mips_use_local_got_p (struct bfd_link_info *info,
   4430 		      struct mips_elf_link_hash_entry *h)
   4431 {
   4432   /* Symbols that aren't in the dynamic symbol table must live in the
   4433      local GOT.  This includes symbols that are completely undefined
   4434      and which therefore don't bind locally.  We'll report undefined
   4435      symbols later if appropriate.  */
   4436   if (h->root.dynindx == -1)
   4437     return TRUE;
   4438 
   4439   /* Symbols that bind locally can (and in the case of forced-local
   4440      symbols, must) live in the local GOT.  */
   4441   if (h->got_only_for_calls
   4442       ? SYMBOL_CALLS_LOCAL (info, &h->root)
   4443       : SYMBOL_REFERENCES_LOCAL (info, &h->root))
   4444     return TRUE;
   4445 
   4446   /* If this is an executable that must provide a definition of the symbol,
   4447      either though PLTs or copy relocations, then that address should go in
   4448      the local rather than global GOT.  */
   4449   if (bfd_link_executable (info) && h->has_static_relocs)
   4450     return TRUE;
   4451 
   4452   return FALSE;
   4453 }
   4454 
   4455 /* A mips_elf_link_hash_traverse callback for which DATA points to the
   4456    link_info structure.  Decide whether the hash entry needs an entry in
   4457    the global part of the primary GOT, setting global_got_area accordingly.
   4458    Count the number of global symbols that are in the primary GOT only
   4459    because they have relocations against them (reloc_only_gotno).  */
   4460 
   4461 static int
   4462 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
   4463 {
   4464   struct bfd_link_info *info;
   4465   struct mips_elf_link_hash_table *htab;
   4466   struct mips_got_info *g;
   4467 
   4468   info = (struct bfd_link_info *) data;
   4469   htab = mips_elf_hash_table (info);
   4470   g = htab->got_info;
   4471   if (h->global_got_area != GGA_NONE)
   4472     {
   4473       /* Make a final decision about whether the symbol belongs in the
   4474 	 local or global GOT.  */
   4475       if (mips_use_local_got_p (info, h))
   4476 	/* The symbol belongs in the local GOT.  We no longer need this
   4477 	   entry if it was only used for relocations; those relocations
   4478 	   will be against the null or section symbol instead of H.  */
   4479 	h->global_got_area = GGA_NONE;
   4480       else if (htab->is_vxworks
   4481 	       && h->got_only_for_calls
   4482 	       && h->root.plt.plist->mips_offset != MINUS_ONE)
   4483 	/* On VxWorks, calls can refer directly to the .got.plt entry;
   4484 	   they don't need entries in the regular GOT.  .got.plt entries
   4485 	   will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
   4486 	h->global_got_area = GGA_NONE;
   4487       else if (h->global_got_area == GGA_RELOC_ONLY)
   4488 	{
   4489 	  g->reloc_only_gotno++;
   4490 	  g->global_gotno++;
   4491 	}
   4492     }
   4493   return 1;
   4494 }
   4495 
   4496 /* A htab_traverse callback for GOT entries.  Add each one to the GOT
   4498    given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
   4499 
   4500 static int
   4501 mips_elf_add_got_entry (void **entryp, void *data)
   4502 {
   4503   struct mips_got_entry *entry;
   4504   struct mips_elf_traverse_got_arg *arg;
   4505   void **slot;
   4506 
   4507   entry = (struct mips_got_entry *) *entryp;
   4508   arg = (struct mips_elf_traverse_got_arg *) data;
   4509   slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
   4510   if (!slot)
   4511     {
   4512       arg->g = NULL;
   4513       return 0;
   4514     }
   4515   if (!*slot)
   4516     {
   4517       *slot = entry;
   4518       mips_elf_count_got_entry (arg->info, arg->g, entry);
   4519     }
   4520   return 1;
   4521 }
   4522 
   4523 /* A htab_traverse callback for GOT page entries.  Add each one to the GOT
   4524    given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
   4525 
   4526 static int
   4527 mips_elf_add_got_page_entry (void **entryp, void *data)
   4528 {
   4529   struct mips_got_page_entry *entry;
   4530   struct mips_elf_traverse_got_arg *arg;
   4531   void **slot;
   4532 
   4533   entry = (struct mips_got_page_entry *) *entryp;
   4534   arg = (struct mips_elf_traverse_got_arg *) data;
   4535   slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
   4536   if (!slot)
   4537     {
   4538       arg->g = NULL;
   4539       return 0;
   4540     }
   4541   if (!*slot)
   4542     {
   4543       *slot = entry;
   4544       arg->g->page_gotno += entry->num_pages;
   4545     }
   4546   return 1;
   4547 }
   4548 
   4549 /* Consider merging FROM, which is ABFD's GOT, into TO.  Return -1 if
   4550    this would lead to overflow, 1 if they were merged successfully,
   4551    and 0 if a merge failed due to lack of memory.  (These values are chosen
   4552    so that nonnegative return values can be returned by a htab_traverse
   4553    callback.)  */
   4554 
   4555 static int
   4556 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
   4557 			 struct mips_got_info *to,
   4558 			 struct mips_elf_got_per_bfd_arg *arg)
   4559 {
   4560   struct mips_elf_traverse_got_arg tga;
   4561   unsigned int estimate;
   4562 
   4563   /* Work out how many page entries we would need for the combined GOT.  */
   4564   estimate = arg->max_pages;
   4565   if (estimate >= from->page_gotno + to->page_gotno)
   4566     estimate = from->page_gotno + to->page_gotno;
   4567 
   4568   /* And conservatively estimate how many local and TLS entries
   4569      would be needed.  */
   4570   estimate += from->local_gotno + to->local_gotno;
   4571   estimate += from->tls_gotno + to->tls_gotno;
   4572 
   4573   /* If we're merging with the primary got, any TLS relocations will
   4574      come after the full set of global entries.  Otherwise estimate those
   4575      conservatively as well.  */
   4576   if (to == arg->primary && from->tls_gotno + to->tls_gotno)
   4577     estimate += arg->global_count;
   4578   else
   4579     estimate += from->global_gotno + to->global_gotno;
   4580 
   4581   /* Bail out if the combined GOT might be too big.  */
   4582   if (estimate > arg->max_count)
   4583     return -1;
   4584 
   4585   /* Transfer the bfd's got information from FROM to TO.  */
   4586   tga.info = arg->info;
   4587   tga.g = to;
   4588   htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
   4589   if (!tga.g)
   4590     return 0;
   4591 
   4592   htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
   4593   if (!tga.g)
   4594     return 0;
   4595 
   4596   mips_elf_replace_bfd_got (abfd, to);
   4597   return 1;
   4598 }
   4599 
   4600 /* Attempt to merge GOT G, which belongs to ABFD.  Try to use as much
   4601    as possible of the primary got, since it doesn't require explicit
   4602    dynamic relocations, but don't use bfds that would reference global
   4603    symbols out of the addressable range.  Failing the primary got,
   4604    attempt to merge with the current got, or finish the current got
   4605    and then make make the new got current.  */
   4606 
   4607 static bfd_boolean
   4608 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
   4609 		    struct mips_elf_got_per_bfd_arg *arg)
   4610 {
   4611   unsigned int estimate;
   4612   int result;
   4613 
   4614   if (!mips_elf_resolve_final_got_entries (arg->info, g))
   4615     return FALSE;
   4616 
   4617   /* Work out the number of page, local and TLS entries.  */
   4618   estimate = arg->max_pages;
   4619   if (estimate > g->page_gotno)
   4620     estimate = g->page_gotno;
   4621   estimate += g->local_gotno + g->tls_gotno;
   4622 
   4623   /* We place TLS GOT entries after both locals and globals.  The globals
   4624      for the primary GOT may overflow the normal GOT size limit, so be
   4625      sure not to merge a GOT which requires TLS with the primary GOT in that
   4626      case.  This doesn't affect non-primary GOTs.  */
   4627   estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
   4628 
   4629   if (estimate <= arg->max_count)
   4630     {
   4631       /* If we don't have a primary GOT, use it as
   4632 	 a starting point for the primary GOT.  */
   4633       if (!arg->primary)
   4634 	{
   4635 	  arg->primary = g;
   4636 	  return TRUE;
   4637 	}
   4638 
   4639       /* Try merging with the primary GOT.  */
   4640       result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
   4641       if (result >= 0)
   4642 	return result;
   4643     }
   4644 
   4645   /* If we can merge with the last-created got, do it.  */
   4646   if (arg->current)
   4647     {
   4648       result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
   4649       if (result >= 0)
   4650 	return result;
   4651     }
   4652 
   4653   /* Well, we couldn't merge, so create a new GOT.  Don't check if it
   4654      fits; if it turns out that it doesn't, we'll get relocation
   4655      overflows anyway.  */
   4656   g->next = arg->current;
   4657   arg->current = g;
   4658 
   4659   return TRUE;
   4660 }
   4661 
   4662 /* ENTRYP is a hash table entry for a mips_got_entry.  Set its gotidx
   4663    to GOTIDX, duplicating the entry if it has already been assigned
   4664    an index in a different GOT.  */
   4665 
   4666 static bfd_boolean
   4667 mips_elf_set_gotidx (void **entryp, long gotidx)
   4668 {
   4669   struct mips_got_entry *entry;
   4670 
   4671   entry = (struct mips_got_entry *) *entryp;
   4672   if (entry->gotidx > 0)
   4673     {
   4674       struct mips_got_entry *new_entry;
   4675 
   4676       new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
   4677       if (!new_entry)
   4678 	return FALSE;
   4679 
   4680       *new_entry = *entry;
   4681       *entryp = new_entry;
   4682       entry = new_entry;
   4683     }
   4684   entry->gotidx = gotidx;
   4685   return TRUE;
   4686 }
   4687 
   4688 /* Set the TLS GOT index for the GOT entry in ENTRYP.  DATA points to a
   4689    mips_elf_traverse_got_arg in which DATA->value is the size of one
   4690    GOT entry.  Set DATA->g to null on failure.  */
   4691 
   4692 static int
   4693 mips_elf_initialize_tls_index (void **entryp, void *data)
   4694 {
   4695   struct mips_got_entry *entry;
   4696   struct mips_elf_traverse_got_arg *arg;
   4697 
   4698   /* We're only interested in TLS symbols.  */
   4699   entry = (struct mips_got_entry *) *entryp;
   4700   if (entry->tls_type == GOT_TLS_NONE)
   4701     return 1;
   4702 
   4703   arg = (struct mips_elf_traverse_got_arg *) data;
   4704   if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
   4705     {
   4706       arg->g = NULL;
   4707       return 0;
   4708     }
   4709 
   4710   /* Account for the entries we've just allocated.  */
   4711   arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
   4712   return 1;
   4713 }
   4714 
   4715 /* A htab_traverse callback for GOT entries, where DATA points to a
   4716    mips_elf_traverse_got_arg.  Set the global_got_area of each global
   4717    symbol to DATA->value.  */
   4718 
   4719 static int
   4720 mips_elf_set_global_got_area (void **entryp, void *data)
   4721 {
   4722   struct mips_got_entry *entry;
   4723   struct mips_elf_traverse_got_arg *arg;
   4724 
   4725   entry = (struct mips_got_entry *) *entryp;
   4726   arg = (struct mips_elf_traverse_got_arg *) data;
   4727   if (entry->abfd != NULL
   4728       && entry->symndx == -1
   4729       && entry->d.h->global_got_area != GGA_NONE)
   4730     entry->d.h->global_got_area = arg->value;
   4731   return 1;
   4732 }
   4733 
   4734 /* A htab_traverse callback for secondary GOT entries, where DATA points
   4735    to a mips_elf_traverse_got_arg.  Assign GOT indices to global entries
   4736    and record the number of relocations they require.  DATA->value is
   4737    the size of one GOT entry.  Set DATA->g to null on failure.  */
   4738 
   4739 static int
   4740 mips_elf_set_global_gotidx (void **entryp, void *data)
   4741 {
   4742   struct mips_got_entry *entry;
   4743   struct mips_elf_traverse_got_arg *arg;
   4744 
   4745   entry = (struct mips_got_entry *) *entryp;
   4746   arg = (struct mips_elf_traverse_got_arg *) data;
   4747   if (entry->abfd != NULL
   4748       && entry->symndx == -1
   4749       && entry->d.h->global_got_area != GGA_NONE)
   4750     {
   4751       if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
   4752 	{
   4753 	  arg->g = NULL;
   4754 	  return 0;
   4755 	}
   4756       arg->g->assigned_low_gotno += 1;
   4757 
   4758       if (bfd_link_pic (arg->info)
   4759 	  || (elf_hash_table (arg->info)->dynamic_sections_created
   4760 	      && entry->d.h->root.def_dynamic
   4761 	      && !entry->d.h->root.def_regular))
   4762 	arg->g->relocs += 1;
   4763     }
   4764 
   4765   return 1;
   4766 }
   4767 
   4768 /* A htab_traverse callback for GOT entries for which DATA is the
   4769    bfd_link_info.  Forbid any global symbols from having traditional
   4770    lazy-binding stubs.  */
   4771 
   4772 static int
   4773 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
   4774 {
   4775   struct bfd_link_info *info;
   4776   struct mips_elf_link_hash_table *htab;
   4777   struct mips_got_entry *entry;
   4778 
   4779   entry = (struct mips_got_entry *) *entryp;
   4780   info = (struct bfd_link_info *) data;
   4781   htab = mips_elf_hash_table (info);
   4782   BFD_ASSERT (htab != NULL);
   4783 
   4784   if (entry->abfd != NULL
   4785       && entry->symndx == -1
   4786       && entry->d.h->needs_lazy_stub)
   4787     {
   4788       entry->d.h->needs_lazy_stub = FALSE;
   4789       htab->lazy_stub_count--;
   4790     }
   4791 
   4792   return 1;
   4793 }
   4794 
   4795 /* Return the offset of an input bfd IBFD's GOT from the beginning of
   4796    the primary GOT.  */
   4797 static bfd_vma
   4798 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
   4799 {
   4800   if (!g->next)
   4801     return 0;
   4802 
   4803   g = mips_elf_bfd_got (ibfd, FALSE);
   4804   if (! g)
   4805     return 0;
   4806 
   4807   BFD_ASSERT (g->next);
   4808 
   4809   g = g->next;
   4810 
   4811   return (g->local_gotno + g->global_gotno + g->tls_gotno)
   4812     * MIPS_ELF_GOT_SIZE (abfd);
   4813 }
   4814 
   4815 /* Turn a single GOT that is too big for 16-bit addressing into
   4816    a sequence of GOTs, each one 16-bit addressable.  */
   4817 
   4818 static bfd_boolean
   4819 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
   4820 		    asection *got, bfd_size_type pages)
   4821 {
   4822   struct mips_elf_link_hash_table *htab;
   4823   struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
   4824   struct mips_elf_traverse_got_arg tga;
   4825   struct mips_got_info *g, *gg;
   4826   unsigned int assign, needed_relocs;
   4827   bfd *dynobj, *ibfd;
   4828 
   4829   dynobj = elf_hash_table (info)->dynobj;
   4830   htab = mips_elf_hash_table (info);
   4831   BFD_ASSERT (htab != NULL);
   4832 
   4833   g = htab->got_info;
   4834 
   4835   got_per_bfd_arg.obfd = abfd;
   4836   got_per_bfd_arg.info = info;
   4837   got_per_bfd_arg.current = NULL;
   4838   got_per_bfd_arg.primary = NULL;
   4839   got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
   4840 				/ MIPS_ELF_GOT_SIZE (abfd))
   4841 			       - htab->reserved_gotno);
   4842   got_per_bfd_arg.max_pages = pages;
   4843   /* The number of globals that will be included in the primary GOT.
   4844      See the calls to mips_elf_set_global_got_area below for more
   4845      information.  */
   4846   got_per_bfd_arg.global_count = g->global_gotno;
   4847 
   4848   /* Try to merge the GOTs of input bfds together, as long as they
   4849      don't seem to exceed the maximum GOT size, choosing one of them
   4850      to be the primary GOT.  */
   4851   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   4852     {
   4853       gg = mips_elf_bfd_got (ibfd, FALSE);
   4854       if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
   4855 	return FALSE;
   4856     }
   4857 
   4858   /* If we do not find any suitable primary GOT, create an empty one.  */
   4859   if (got_per_bfd_arg.primary == NULL)
   4860     g->next = mips_elf_create_got_info (abfd);
   4861   else
   4862     g->next = got_per_bfd_arg.primary;
   4863   g->next->next = got_per_bfd_arg.current;
   4864 
   4865   /* GG is now the master GOT, and G is the primary GOT.  */
   4866   gg = g;
   4867   g = g->next;
   4868 
   4869   /* Map the output bfd to the primary got.  That's what we're going
   4870      to use for bfds that use GOT16 or GOT_PAGE relocations that we
   4871      didn't mark in check_relocs, and we want a quick way to find it.
   4872      We can't just use gg->next because we're going to reverse the
   4873      list.  */
   4874   mips_elf_replace_bfd_got (abfd, g);
   4875 
   4876   /* Every symbol that is referenced in a dynamic relocation must be
   4877      present in the primary GOT, so arrange for them to appear after
   4878      those that are actually referenced.  */
   4879   gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
   4880   g->global_gotno = gg->global_gotno;
   4881 
   4882   tga.info = info;
   4883   tga.value = GGA_RELOC_ONLY;
   4884   htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
   4885   tga.value = GGA_NORMAL;
   4886   htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
   4887 
   4888   /* Now go through the GOTs assigning them offset ranges.
   4889      [assigned_low_gotno, local_gotno[ will be set to the range of local
   4890      entries in each GOT.  We can then compute the end of a GOT by
   4891      adding local_gotno to global_gotno.  We reverse the list and make
   4892      it circular since then we'll be able to quickly compute the
   4893      beginning of a GOT, by computing the end of its predecessor.  To
   4894      avoid special cases for the primary GOT, while still preserving
   4895      assertions that are valid for both single- and multi-got links,
   4896      we arrange for the main got struct to have the right number of
   4897      global entries, but set its local_gotno such that the initial
   4898      offset of the primary GOT is zero.  Remember that the primary GOT
   4899      will become the last item in the circular linked list, so it
   4900      points back to the master GOT.  */
   4901   gg->local_gotno = -g->global_gotno;
   4902   gg->global_gotno = g->global_gotno;
   4903   gg->tls_gotno = 0;
   4904   assign = 0;
   4905   gg->next = gg;
   4906 
   4907   do
   4908     {
   4909       struct mips_got_info *gn;
   4910 
   4911       assign += htab->reserved_gotno;
   4912       g->assigned_low_gotno = assign;
   4913       g->local_gotno += assign;
   4914       g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
   4915       g->assigned_high_gotno = g->local_gotno - 1;
   4916       assign = g->local_gotno + g->global_gotno + g->tls_gotno;
   4917 
   4918       /* Take g out of the direct list, and push it onto the reversed
   4919 	 list that gg points to.  g->next is guaranteed to be nonnull after
   4920 	 this operation, as required by mips_elf_initialize_tls_index. */
   4921       gn = g->next;
   4922       g->next = gg->next;
   4923       gg->next = g;
   4924 
   4925       /* Set up any TLS entries.  We always place the TLS entries after
   4926 	 all non-TLS entries.  */
   4927       g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
   4928       tga.g = g;
   4929       tga.value = MIPS_ELF_GOT_SIZE (abfd);
   4930       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
   4931       if (!tga.g)
   4932 	return FALSE;
   4933       BFD_ASSERT (g->tls_assigned_gotno == assign);
   4934 
   4935       /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
   4936       g = gn;
   4937 
   4938       /* Forbid global symbols in every non-primary GOT from having
   4939 	 lazy-binding stubs.  */
   4940       if (g)
   4941 	htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
   4942     }
   4943   while (g);
   4944 
   4945   got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
   4946 
   4947   needed_relocs = 0;
   4948   for (g = gg->next; g && g->next != gg; g = g->next)
   4949     {
   4950       unsigned int save_assign;
   4951 
   4952       /* Assign offsets to global GOT entries and count how many
   4953 	 relocations they need.  */
   4954       save_assign = g->assigned_low_gotno;
   4955       g->assigned_low_gotno = g->local_gotno;
   4956       tga.info = info;
   4957       tga.value = MIPS_ELF_GOT_SIZE (abfd);
   4958       tga.g = g;
   4959       htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
   4960       if (!tga.g)
   4961 	return FALSE;
   4962       BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
   4963       g->assigned_low_gotno = save_assign;
   4964 
   4965       if (bfd_link_pic (info))
   4966 	{
   4967 	  g->relocs += g->local_gotno - g->assigned_low_gotno;
   4968 	  BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
   4969 		      + g->next->global_gotno
   4970 		      + g->next->tls_gotno
   4971 		      + htab->reserved_gotno);
   4972 	}
   4973       needed_relocs += g->relocs;
   4974     }
   4975   needed_relocs += g->relocs;
   4976 
   4977   if (needed_relocs)
   4978     mips_elf_allocate_dynamic_relocations (dynobj, info,
   4979 					   needed_relocs);
   4980 
   4981   return TRUE;
   4982 }
   4983 
   4984 
   4985 /* Returns the first relocation of type r_type found, beginning with
   4987    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
   4988 
   4989 static const Elf_Internal_Rela *
   4990 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
   4991 			  const Elf_Internal_Rela *relocation,
   4992 			  const Elf_Internal_Rela *relend)
   4993 {
   4994   unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
   4995 
   4996   while (relocation < relend)
   4997     {
   4998       if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
   4999 	  && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
   5000 	return relocation;
   5001 
   5002       ++relocation;
   5003     }
   5004 
   5005   /* We didn't find it.  */
   5006   return NULL;
   5007 }
   5008 
   5009 /* Return whether an input relocation is against a local symbol.  */
   5010 
   5011 static bfd_boolean
   5012 mips_elf_local_relocation_p (bfd *input_bfd,
   5013 			     const Elf_Internal_Rela *relocation,
   5014 			     asection **local_sections)
   5015 {
   5016   unsigned long r_symndx;
   5017   Elf_Internal_Shdr *symtab_hdr;
   5018   size_t extsymoff;
   5019 
   5020   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
   5021   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   5022   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
   5023 
   5024   if (r_symndx < extsymoff)
   5025     return TRUE;
   5026   if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
   5027     return TRUE;
   5028 
   5029   return FALSE;
   5030 }
   5031 
   5032 /* Sign-extend VALUE, which has the indicated number of BITS.  */
   5034 
   5035 bfd_vma
   5036 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
   5037 {
   5038   if (value & ((bfd_vma) 1 << (bits - 1)))
   5039     /* VALUE is negative.  */
   5040     value |= ((bfd_vma) - 1) << bits;
   5041 
   5042   return value;
   5043 }
   5044 
   5045 /* Return non-zero if the indicated VALUE has overflowed the maximum
   5046    range expressible by a signed number with the indicated number of
   5047    BITS.  */
   5048 
   5049 static bfd_boolean
   5050 mips_elf_overflow_p (bfd_vma value, int bits)
   5051 {
   5052   bfd_signed_vma svalue = (bfd_signed_vma) value;
   5053 
   5054   if (svalue > (1 << (bits - 1)) - 1)
   5055     /* The value is too big.  */
   5056     return TRUE;
   5057   else if (svalue < -(1 << (bits - 1)))
   5058     /* The value is too small.  */
   5059     return TRUE;
   5060 
   5061   /* All is well.  */
   5062   return FALSE;
   5063 }
   5064 
   5065 /* Calculate the %high function.  */
   5066 
   5067 static bfd_vma
   5068 mips_elf_high (bfd_vma value)
   5069 {
   5070   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
   5071 }
   5072 
   5073 /* Calculate the %higher function.  */
   5074 
   5075 static bfd_vma
   5076 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
   5077 {
   5078 #ifdef BFD64
   5079   return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
   5080 #else
   5081   abort ();
   5082   return MINUS_ONE;
   5083 #endif
   5084 }
   5085 
   5086 /* Calculate the %highest function.  */
   5087 
   5088 static bfd_vma
   5089 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
   5090 {
   5091 #ifdef BFD64
   5092   return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
   5093 #else
   5094   abort ();
   5095   return MINUS_ONE;
   5096 #endif
   5097 }
   5098 
   5099 /* Create the .compact_rel section.  */
   5101 
   5102 static bfd_boolean
   5103 mips_elf_create_compact_rel_section
   5104   (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
   5105 {
   5106   flagword flags;
   5107   register asection *s;
   5108 
   5109   if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
   5110     {
   5111       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
   5112 	       | SEC_READONLY);
   5113 
   5114       s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
   5115       if (s == NULL
   5116 	  || ! bfd_set_section_alignment (abfd, s,
   5117 					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   5118 	return FALSE;
   5119 
   5120       s->size = sizeof (Elf32_External_compact_rel);
   5121     }
   5122 
   5123   return TRUE;
   5124 }
   5125 
   5126 /* Create the .got section to hold the global offset table.  */
   5127 
   5128 static bfd_boolean
   5129 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
   5130 {
   5131   flagword flags;
   5132   register asection *s;
   5133   struct elf_link_hash_entry *h;
   5134   struct bfd_link_hash_entry *bh;
   5135   struct mips_elf_link_hash_table *htab;
   5136 
   5137   htab = mips_elf_hash_table (info);
   5138   BFD_ASSERT (htab != NULL);
   5139 
   5140   /* This function may be called more than once.  */
   5141   if (htab->sgot)
   5142     return TRUE;
   5143 
   5144   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   5145 	   | SEC_LINKER_CREATED);
   5146 
   5147   /* We have to use an alignment of 2**4 here because this is hardcoded
   5148      in the function stub generation and in the linker script.  */
   5149   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   5150   if (s == NULL
   5151       || ! bfd_set_section_alignment (abfd, s, 4))
   5152     return FALSE;
   5153   htab->sgot = s;
   5154 
   5155   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
   5156      linker script because we don't want to define the symbol if we
   5157      are not creating a global offset table.  */
   5158   bh = NULL;
   5159   if (! (_bfd_generic_link_add_one_symbol
   5160 	 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
   5161 	  0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
   5162     return FALSE;
   5163 
   5164   h = (struct elf_link_hash_entry *) bh;
   5165   h->non_elf = 0;
   5166   h->def_regular = 1;
   5167   h->type = STT_OBJECT;
   5168   h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
   5169   elf_hash_table (info)->hgot = h;
   5170 
   5171   if (bfd_link_pic (info)
   5172       && ! bfd_elf_link_record_dynamic_symbol (info, h))
   5173     return FALSE;
   5174 
   5175   htab->got_info = mips_elf_create_got_info (abfd);
   5176   mips_elf_section_data (s)->elf.this_hdr.sh_flags
   5177     |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
   5178 
   5179   /* We also need a .got.plt section when generating PLTs.  */
   5180   s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
   5181 					  SEC_ALLOC | SEC_LOAD
   5182 					  | SEC_HAS_CONTENTS
   5183 					  | SEC_IN_MEMORY
   5184 					  | SEC_LINKER_CREATED);
   5185   if (s == NULL)
   5186     return FALSE;
   5187   htab->sgotplt = s;
   5188 
   5189   return TRUE;
   5190 }
   5191 
   5192 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
   5194    __GOTT_INDEX__ symbols.  These symbols are only special for
   5195    shared objects; they are not used in executables.  */
   5196 
   5197 static bfd_boolean
   5198 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
   5199 {
   5200   return (mips_elf_hash_table (info)->is_vxworks
   5201 	  && bfd_link_pic (info)
   5202 	  && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
   5203 	      || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
   5204 }
   5205 
   5206 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
   5207    require an la25 stub.  See also mips_elf_local_pic_function_p,
   5208    which determines whether the destination function ever requires a
   5209    stub.  */
   5210 
   5211 static bfd_boolean
   5212 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
   5213 				     bfd_boolean target_is_16_bit_code_p)
   5214 {
   5215   /* We specifically ignore branches and jumps from EF_PIC objects,
   5216      where the onus is on the compiler or programmer to perform any
   5217      necessary initialization of $25.  Sometimes such initialization
   5218      is unnecessary; for example, -mno-shared functions do not use
   5219      the incoming value of $25, and may therefore be called directly.  */
   5220   if (PIC_OBJECT_P (input_bfd))
   5221     return FALSE;
   5222 
   5223   switch (r_type)
   5224     {
   5225     case R_MIPS_26:
   5226     case R_MIPS_PC16:
   5227     case R_MIPS_PC21_S2:
   5228     case R_MIPS_PC26_S2:
   5229     case R_MICROMIPS_26_S1:
   5230     case R_MICROMIPS_PC7_S1:
   5231     case R_MICROMIPS_PC10_S1:
   5232     case R_MICROMIPS_PC16_S1:
   5233     case R_MICROMIPS_PC23_S2:
   5234       return TRUE;
   5235 
   5236     case R_MIPS16_26:
   5237       return !target_is_16_bit_code_p;
   5238 
   5239     default:
   5240       return FALSE;
   5241     }
   5242 }
   5243 
   5244 /* Calculate the value produced by the RELOCATION (which comes from
   5246    the INPUT_BFD).  The ADDEND is the addend to use for this
   5247    RELOCATION; RELOCATION->R_ADDEND is ignored.
   5248 
   5249    The result of the relocation calculation is stored in VALUEP.
   5250    On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
   5251    is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
   5252 
   5253    This function returns bfd_reloc_continue if the caller need take no
   5254    further action regarding this relocation, bfd_reloc_notsupported if
   5255    something goes dramatically wrong, bfd_reloc_overflow if an
   5256    overflow occurs, and bfd_reloc_ok to indicate success.  */
   5257 
   5258 static bfd_reloc_status_type
   5259 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
   5260 			       asection *input_section,
   5261 			       struct bfd_link_info *info,
   5262 			       const Elf_Internal_Rela *relocation,
   5263 			       bfd_vma addend, reloc_howto_type *howto,
   5264 			       Elf_Internal_Sym *local_syms,
   5265 			       asection **local_sections, bfd_vma *valuep,
   5266 			       const char **namep,
   5267 			       bfd_boolean *cross_mode_jump_p,
   5268 			       bfd_boolean save_addend)
   5269 {
   5270   /* The eventual value we will return.  */
   5271   bfd_vma value;
   5272   /* The address of the symbol against which the relocation is
   5273      occurring.  */
   5274   bfd_vma symbol = 0;
   5275   /* The final GP value to be used for the relocatable, executable, or
   5276      shared object file being produced.  */
   5277   bfd_vma gp;
   5278   /* The place (section offset or address) of the storage unit being
   5279      relocated.  */
   5280   bfd_vma p;
   5281   /* The value of GP used to create the relocatable object.  */
   5282   bfd_vma gp0;
   5283   /* The offset into the global offset table at which the address of
   5284      the relocation entry symbol, adjusted by the addend, resides
   5285      during execution.  */
   5286   bfd_vma g = MINUS_ONE;
   5287   /* The section in which the symbol referenced by the relocation is
   5288      located.  */
   5289   asection *sec = NULL;
   5290   struct mips_elf_link_hash_entry *h = NULL;
   5291   /* TRUE if the symbol referred to by this relocation is a local
   5292      symbol.  */
   5293   bfd_boolean local_p, was_local_p;
   5294   /* TRUE if the symbol referred to by this relocation is a section
   5295      symbol.  */
   5296   bfd_boolean section_p = FALSE;
   5297   /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
   5298   bfd_boolean gp_disp_p = FALSE;
   5299   /* TRUE if the symbol referred to by this relocation is
   5300      "__gnu_local_gp".  */
   5301   bfd_boolean gnu_local_gp_p = FALSE;
   5302   Elf_Internal_Shdr *symtab_hdr;
   5303   size_t extsymoff;
   5304   unsigned long r_symndx;
   5305   int r_type;
   5306   /* TRUE if overflow occurred during the calculation of the
   5307      relocation value.  */
   5308   bfd_boolean overflowed_p;
   5309   /* TRUE if this relocation refers to a MIPS16 function.  */
   5310   bfd_boolean target_is_16_bit_code_p = FALSE;
   5311   bfd_boolean target_is_micromips_code_p = FALSE;
   5312   struct mips_elf_link_hash_table *htab;
   5313   bfd *dynobj;
   5314 
   5315   dynobj = elf_hash_table (info)->dynobj;
   5316   htab = mips_elf_hash_table (info);
   5317   BFD_ASSERT (htab != NULL);
   5318 
   5319   /* Parse the relocation.  */
   5320   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
   5321   r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   5322   p = (input_section->output_section->vma
   5323        + input_section->output_offset
   5324        + relocation->r_offset);
   5325 
   5326   /* Assume that there will be no overflow.  */
   5327   overflowed_p = FALSE;
   5328 
   5329   /* Figure out whether or not the symbol is local, and get the offset
   5330      used in the array of hash table entries.  */
   5331   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   5332   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
   5333 					 local_sections);
   5334   was_local_p = local_p;
   5335   if (! elf_bad_symtab (input_bfd))
   5336     extsymoff = symtab_hdr->sh_info;
   5337   else
   5338     {
   5339       /* The symbol table does not follow the rule that local symbols
   5340 	 must come before globals.  */
   5341       extsymoff = 0;
   5342     }
   5343 
   5344   /* Figure out the value of the symbol.  */
   5345   if (local_p)
   5346     {
   5347       Elf_Internal_Sym *sym;
   5348 
   5349       sym = local_syms + r_symndx;
   5350       sec = local_sections[r_symndx];
   5351 
   5352       section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
   5353 
   5354       symbol = sec->output_section->vma + sec->output_offset;
   5355       if (!section_p || (sec->flags & SEC_MERGE))
   5356 	symbol += sym->st_value;
   5357       if ((sec->flags & SEC_MERGE) && section_p)
   5358 	{
   5359 	  addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
   5360 	  addend -= symbol;
   5361 	  addend += sec->output_section->vma + sec->output_offset;
   5362 	}
   5363 
   5364       /* MIPS16/microMIPS text labels should be treated as odd.  */
   5365       if (ELF_ST_IS_COMPRESSED (sym->st_other))
   5366 	++symbol;
   5367 
   5368       /* Record the name of this symbol, for our caller.  */
   5369       *namep = bfd_elf_string_from_elf_section (input_bfd,
   5370 						symtab_hdr->sh_link,
   5371 						sym->st_name);
   5372       if (*namep == NULL || **namep == '\0')
   5373 	*namep = bfd_section_name (input_bfd, sec);
   5374 
   5375       target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
   5376       target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
   5377     }
   5378   else
   5379     {
   5380       /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
   5381 
   5382       /* For global symbols we look up the symbol in the hash-table.  */
   5383       h = ((struct mips_elf_link_hash_entry *)
   5384 	   elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
   5385       /* Find the real hash-table entry for this symbol.  */
   5386       while (h->root.root.type == bfd_link_hash_indirect
   5387 	     || h->root.root.type == bfd_link_hash_warning)
   5388 	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   5389 
   5390       /* Record the name of this symbol, for our caller.  */
   5391       *namep = h->root.root.root.string;
   5392 
   5393       /* See if this is the special _gp_disp symbol.  Note that such a
   5394 	 symbol must always be a global symbol.  */
   5395       if (strcmp (*namep, "_gp_disp") == 0
   5396 	  && ! NEWABI_P (input_bfd))
   5397 	{
   5398 	  /* Relocations against _gp_disp are permitted only with
   5399 	     R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
   5400 	  if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
   5401 	    return bfd_reloc_notsupported;
   5402 
   5403 	  gp_disp_p = TRUE;
   5404 	}
   5405       /* See if this is the special _gp symbol.  Note that such a
   5406 	 symbol must always be a global symbol.  */
   5407       else if (strcmp (*namep, "__gnu_local_gp") == 0)
   5408 	gnu_local_gp_p = TRUE;
   5409 
   5410 
   5411       /* If this symbol is defined, calculate its address.  Note that
   5412 	 _gp_disp is a magic symbol, always implicitly defined by the
   5413 	 linker, so it's inappropriate to check to see whether or not
   5414 	 its defined.  */
   5415       else if ((h->root.root.type == bfd_link_hash_defined
   5416 		|| h->root.root.type == bfd_link_hash_defweak)
   5417 	       && h->root.root.u.def.section)
   5418 	{
   5419 	  sec = h->root.root.u.def.section;
   5420 	  if (sec->output_section)
   5421 	    symbol = (h->root.root.u.def.value
   5422 		      + sec->output_section->vma
   5423 		      + sec->output_offset);
   5424 	  else
   5425 	    symbol = h->root.root.u.def.value;
   5426 	}
   5427       else if (h->root.root.type == bfd_link_hash_undefweak)
   5428 	/* We allow relocations against undefined weak symbols, giving
   5429 	   it the value zero, so that you can undefined weak functions
   5430 	   and check to see if they exist by looking at their
   5431 	   addresses.  */
   5432 	symbol = 0;
   5433       else if (info->unresolved_syms_in_objects == RM_IGNORE
   5434 	       && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
   5435 	symbol = 0;
   5436       else if (strcmp (*namep, SGI_COMPAT (input_bfd)
   5437 		       ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
   5438 	{
   5439 	  /* If this is a dynamic link, we should have created a
   5440 	     _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
   5441 	     in in _bfd_mips_elf_create_dynamic_sections.
   5442 	     Otherwise, we should define the symbol with a value of 0.
   5443 	     FIXME: It should probably get into the symbol table
   5444 	     somehow as well.  */
   5445 	  BFD_ASSERT (! bfd_link_pic (info));
   5446 	  BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
   5447 	  symbol = 0;
   5448 	}
   5449       else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
   5450 	{
   5451 	  /* This is an optional symbol - an Irix specific extension to the
   5452 	     ELF spec.  Ignore it for now.
   5453 	     XXX - FIXME - there is more to the spec for OPTIONAL symbols
   5454 	     than simply ignoring them, but we do not handle this for now.
   5455 	     For information see the "64-bit ELF Object File Specification"
   5456 	     which is available from here:
   5457 	     http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
   5458 	  symbol = 0;
   5459 	}
   5460       else
   5461 	{
   5462 	  (*info->callbacks->undefined_symbol)
   5463 	    (info, h->root.root.root.string, input_bfd,
   5464 	     input_section, relocation->r_offset,
   5465 	     (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
   5466 	     || ELF_ST_VISIBILITY (h->root.other));
   5467 	  return bfd_reloc_undefined;
   5468 	}
   5469 
   5470       target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
   5471       target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
   5472     }
   5473 
   5474   /* If this is a reference to a 16-bit function with a stub, we need
   5475      to redirect the relocation to the stub unless:
   5476 
   5477      (a) the relocation is for a MIPS16 JAL;
   5478 
   5479      (b) the relocation is for a MIPS16 PIC call, and there are no
   5480 	 non-MIPS16 uses of the GOT slot; or
   5481 
   5482      (c) the section allows direct references to MIPS16 functions.  */
   5483   if (r_type != R_MIPS16_26
   5484       && !bfd_link_relocatable (info)
   5485       && ((h != NULL
   5486 	   && h->fn_stub != NULL
   5487 	   && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
   5488 	  || (local_p
   5489 	      && mips_elf_tdata (input_bfd)->local_stubs != NULL
   5490 	      && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
   5491       && !section_allows_mips16_refs_p (input_section))
   5492     {
   5493       /* This is a 32- or 64-bit call to a 16-bit function.  We should
   5494 	 have already noticed that we were going to need the
   5495 	 stub.  */
   5496       if (local_p)
   5497 	{
   5498 	  sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
   5499 	  value = 0;
   5500 	}
   5501       else
   5502 	{
   5503 	  BFD_ASSERT (h->need_fn_stub);
   5504 	  if (h->la25_stub)
   5505 	    {
   5506 	      /* If a LA25 header for the stub itself exists, point to the
   5507 		 prepended LUI/ADDIU sequence.  */
   5508 	      sec = h->la25_stub->stub_section;
   5509 	      value = h->la25_stub->offset;
   5510 	    }
   5511 	  else
   5512 	    {
   5513 	      sec = h->fn_stub;
   5514 	      value = 0;
   5515 	    }
   5516 	}
   5517 
   5518       symbol = sec->output_section->vma + sec->output_offset + value;
   5519       /* The target is 16-bit, but the stub isn't.  */
   5520       target_is_16_bit_code_p = FALSE;
   5521     }
   5522   /* If this is a MIPS16 call with a stub, that is made through the PLT or
   5523      to a standard MIPS function, we need to redirect the call to the stub.
   5524      Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
   5525      indirect calls should use an indirect stub instead.  */
   5526   else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
   5527 	   && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
   5528 	       || (local_p
   5529 		   && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
   5530 		   && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
   5531 	   && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
   5532     {
   5533       if (local_p)
   5534 	sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
   5535       else
   5536 	{
   5537 	  /* If both call_stub and call_fp_stub are defined, we can figure
   5538 	     out which one to use by checking which one appears in the input
   5539 	     file.  */
   5540 	  if (h->call_stub != NULL && h->call_fp_stub != NULL)
   5541 	    {
   5542 	      asection *o;
   5543 
   5544 	      sec = NULL;
   5545 	      for (o = input_bfd->sections; o != NULL; o = o->next)
   5546 		{
   5547 		  if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
   5548 		    {
   5549 		      sec = h->call_fp_stub;
   5550 		      break;
   5551 		    }
   5552 		}
   5553 	      if (sec == NULL)
   5554 		sec = h->call_stub;
   5555 	    }
   5556 	  else if (h->call_stub != NULL)
   5557 	    sec = h->call_stub;
   5558 	  else
   5559 	    sec = h->call_fp_stub;
   5560   	}
   5561 
   5562       BFD_ASSERT (sec->size > 0);
   5563       symbol = sec->output_section->vma + sec->output_offset;
   5564     }
   5565   /* If this is a direct call to a PIC function, redirect to the
   5566      non-PIC stub.  */
   5567   else if (h != NULL && h->la25_stub
   5568 	   && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
   5569 						   target_is_16_bit_code_p))
   5570     symbol = (h->la25_stub->stub_section->output_section->vma
   5571 	      + h->la25_stub->stub_section->output_offset
   5572 	      + h->la25_stub->offset);
   5573   /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
   5574      entry is used if a standard PLT entry has also been made.  In this
   5575      case the symbol will have been set by mips_elf_set_plt_sym_value
   5576      to point to the standard PLT entry, so redirect to the compressed
   5577      one.  */
   5578   else if ((r_type == R_MIPS16_26 || r_type == R_MICROMIPS_26_S1)
   5579 	   && !bfd_link_relocatable (info)
   5580 	   && h != NULL
   5581 	   && h->use_plt_entry
   5582 	   && h->root.plt.plist->comp_offset != MINUS_ONE
   5583 	   && h->root.plt.plist->mips_offset != MINUS_ONE)
   5584     {
   5585       bfd_boolean micromips_p = MICROMIPS_P (abfd);
   5586 
   5587       sec = htab->splt;
   5588       symbol = (sec->output_section->vma
   5589 		+ sec->output_offset
   5590 		+ htab->plt_header_size
   5591 		+ htab->plt_mips_offset
   5592 		+ h->root.plt.plist->comp_offset
   5593 		+ 1);
   5594 
   5595       target_is_16_bit_code_p = !micromips_p;
   5596       target_is_micromips_code_p = micromips_p;
   5597     }
   5598 
   5599   /* Make sure MIPS16 and microMIPS are not used together.  */
   5600   if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
   5601       || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
   5602    {
   5603       (*_bfd_error_handler)
   5604 	(_("MIPS16 and microMIPS functions cannot call each other"));
   5605       return bfd_reloc_notsupported;
   5606    }
   5607 
   5608   /* Calls from 16-bit code to 32-bit code and vice versa require the
   5609      mode change.  However, we can ignore calls to undefined weak symbols,
   5610      which should never be executed at runtime.  This exception is important
   5611      because the assembly writer may have "known" that any definition of the
   5612      symbol would be 16-bit code, and that direct jumps were therefore
   5613      acceptable.  */
   5614   *cross_mode_jump_p = (!bfd_link_relocatable (info)
   5615 			&& !(h && h->root.root.type == bfd_link_hash_undefweak)
   5616 			&& ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
   5617 			    || (r_type == R_MICROMIPS_26_S1
   5618 				&& !target_is_micromips_code_p)
   5619 			    || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
   5620 				&& (target_is_16_bit_code_p
   5621 				    || target_is_micromips_code_p))));
   5622 
   5623   local_p = (h == NULL || mips_use_local_got_p (info, h));
   5624 
   5625   gp0 = _bfd_get_gp_value (input_bfd);
   5626   gp = _bfd_get_gp_value (abfd);
   5627   if (htab->got_info)
   5628     gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
   5629 
   5630   if (gnu_local_gp_p)
   5631     symbol = gp;
   5632 
   5633   /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
   5634      to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
   5635      corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.  */
   5636   if (got_page_reloc_p (r_type) && !local_p)
   5637     {
   5638       r_type = (micromips_reloc_p (r_type)
   5639 		? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
   5640       addend = 0;
   5641     }
   5642 
   5643   /* If we haven't already determined the GOT offset, and we're going
   5644      to need it, get it now.  */
   5645   switch (r_type)
   5646     {
   5647     case R_MIPS16_CALL16:
   5648     case R_MIPS16_GOT16:
   5649     case R_MIPS_CALL16:
   5650     case R_MIPS_GOT16:
   5651     case R_MIPS_GOT_DISP:
   5652     case R_MIPS_GOT_HI16:
   5653     case R_MIPS_CALL_HI16:
   5654     case R_MIPS_GOT_LO16:
   5655     case R_MIPS_CALL_LO16:
   5656     case R_MICROMIPS_CALL16:
   5657     case R_MICROMIPS_GOT16:
   5658     case R_MICROMIPS_GOT_DISP:
   5659     case R_MICROMIPS_GOT_HI16:
   5660     case R_MICROMIPS_CALL_HI16:
   5661     case R_MICROMIPS_GOT_LO16:
   5662     case R_MICROMIPS_CALL_LO16:
   5663     case R_MIPS_TLS_GD:
   5664     case R_MIPS_TLS_GOTTPREL:
   5665     case R_MIPS_TLS_LDM:
   5666     case R_MIPS16_TLS_GD:
   5667     case R_MIPS16_TLS_GOTTPREL:
   5668     case R_MIPS16_TLS_LDM:
   5669     case R_MICROMIPS_TLS_GD:
   5670     case R_MICROMIPS_TLS_GOTTPREL:
   5671     case R_MICROMIPS_TLS_LDM:
   5672       /* Find the index into the GOT where this value is located.  */
   5673       if (tls_ldm_reloc_p (r_type))
   5674 	{
   5675 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
   5676 					0, 0, NULL, r_type);
   5677 	  if (g == MINUS_ONE)
   5678 	    return bfd_reloc_outofrange;
   5679 	}
   5680       else if (!local_p)
   5681 	{
   5682 	  /* On VxWorks, CALL relocations should refer to the .got.plt
   5683 	     entry, which is initialized to point at the PLT stub.  */
   5684 	  if (htab->is_vxworks
   5685 	      && (call_hi16_reloc_p (r_type)
   5686 		  || call_lo16_reloc_p (r_type)
   5687 		  || call16_reloc_p (r_type)))
   5688 	    {
   5689 	      BFD_ASSERT (addend == 0);
   5690 	      BFD_ASSERT (h->root.needs_plt);
   5691 	      g = mips_elf_gotplt_index (info, &h->root);
   5692 	    }
   5693 	  else
   5694 	    {
   5695 	      BFD_ASSERT (addend == 0);
   5696 	      g = mips_elf_global_got_index (abfd, info, input_bfd,
   5697 					     &h->root, r_type);
   5698 	      if (!TLS_RELOC_P (r_type)
   5699 		  && !elf_hash_table (info)->dynamic_sections_created)
   5700 		/* This is a static link.  We must initialize the GOT entry.  */
   5701 		MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
   5702 	    }
   5703 	}
   5704       else if (!htab->is_vxworks
   5705 	       && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
   5706 	/* The calculation below does not involve "g".  */
   5707 	break;
   5708       else
   5709 	{
   5710 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
   5711 					symbol + addend, r_symndx, h, r_type);
   5712 	  if (g == MINUS_ONE)
   5713 	    return bfd_reloc_outofrange;
   5714 	}
   5715 
   5716       /* Convert GOT indices to actual offsets.  */
   5717       g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
   5718       break;
   5719     }
   5720 
   5721   /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
   5722      symbols are resolved by the loader.  Add them to .rela.dyn.  */
   5723   if (h != NULL && is_gott_symbol (info, &h->root))
   5724     {
   5725       Elf_Internal_Rela outrel;
   5726       bfd_byte *loc;
   5727       asection *s;
   5728 
   5729       s = mips_elf_rel_dyn_section (info, FALSE);
   5730       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
   5731 
   5732       outrel.r_offset = (input_section->output_section->vma
   5733 			 + input_section->output_offset
   5734 			 + relocation->r_offset);
   5735       outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
   5736       outrel.r_addend = addend;
   5737       bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
   5738 
   5739       /* If we've written this relocation for a readonly section,
   5740 	 we need to set DF_TEXTREL again, so that we do not delete the
   5741 	 DT_TEXTREL tag.  */
   5742       if (MIPS_ELF_READONLY_SECTION (input_section))
   5743 	info->flags |= DF_TEXTREL;
   5744 
   5745       *valuep = 0;
   5746       return bfd_reloc_ok;
   5747     }
   5748 
   5749   /* Figure out what kind of relocation is being performed.  */
   5750   switch (r_type)
   5751     {
   5752     case R_MIPS_NONE:
   5753       return bfd_reloc_continue;
   5754 
   5755     case R_MIPS_16:
   5756       if (howto->partial_inplace)
   5757 	addend = _bfd_mips_elf_sign_extend (addend, 16);
   5758       value = symbol + addend;
   5759       overflowed_p = mips_elf_overflow_p (value, 16);
   5760       break;
   5761 
   5762     case R_MIPS_32:
   5763     case R_MIPS_REL32:
   5764     case R_MIPS_64:
   5765       if ((bfd_link_pic (info)
   5766 	   || (htab->root.dynamic_sections_created
   5767 	       && h != NULL
   5768 	       && h->root.def_dynamic
   5769 	       && !h->root.def_regular
   5770 	       && !h->has_static_relocs))
   5771 	  && r_symndx != STN_UNDEF
   5772 	  && (h == NULL
   5773 	      || h->root.root.type != bfd_link_hash_undefweak
   5774 	      || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
   5775 	  && (input_section->flags & SEC_ALLOC) != 0)
   5776 	{
   5777 	  /* If we're creating a shared library, then we can't know
   5778 	     where the symbol will end up.  So, we create a relocation
   5779 	     record in the output, and leave the job up to the dynamic
   5780 	     linker.  We must do the same for executable references to
   5781 	     shared library symbols, unless we've decided to use copy
   5782 	     relocs or PLTs instead.  */
   5783 	  value = addend;
   5784 	  if (!mips_elf_create_dynamic_relocation (abfd,
   5785 						   info,
   5786 						   relocation,
   5787 						   h,
   5788 						   sec,
   5789 						   symbol,
   5790 						   &value,
   5791 						   input_section))
   5792 	    return bfd_reloc_undefined;
   5793 	}
   5794       else
   5795 	{
   5796 	  if (r_type != R_MIPS_REL32)
   5797 	    value = symbol + addend;
   5798 	  else
   5799 	    value = addend;
   5800 	}
   5801       value &= howto->dst_mask;
   5802       break;
   5803 
   5804     case R_MIPS_PC32:
   5805       value = symbol + addend - p;
   5806       value &= howto->dst_mask;
   5807       break;
   5808 
   5809     case R_MIPS16_26:
   5810       /* The calculation for R_MIPS16_26 is just the same as for an
   5811 	 R_MIPS_26.  It's only the storage of the relocated field into
   5812 	 the output file that's different.  That's handled in
   5813 	 mips_elf_perform_relocation.  So, we just fall through to the
   5814 	 R_MIPS_26 case here.  */
   5815     case R_MIPS_26:
   5816     case R_MICROMIPS_26_S1:
   5817       {
   5818 	unsigned int shift;
   5819 
   5820 	/* Shift is 2, unusually, for microMIPS JALX.  */
   5821 	shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
   5822 
   5823 	if (howto->partial_inplace && !section_p)
   5824 	  value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
   5825 	else
   5826 	  value = addend;
   5827 	value += symbol;
   5828 
   5829 	/* Make sure the target of JALX is word-aligned.  Bit 0 must be
   5830 	   the correct ISA mode selector and bit 1 must be 0.  */
   5831 	if (*cross_mode_jump_p && (value & 3) != (r_type == R_MIPS_26))
   5832 	  return bfd_reloc_outofrange;
   5833 
   5834 	value >>= shift;
   5835 	if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   5836 	  overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
   5837 	value &= howto->dst_mask;
   5838       }
   5839       break;
   5840 
   5841     case R_MIPS_TLS_DTPREL_HI16:
   5842     case R_MIPS16_TLS_DTPREL_HI16:
   5843     case R_MICROMIPS_TLS_DTPREL_HI16:
   5844       value = (mips_elf_high (addend + symbol - dtprel_base (info))
   5845 	       & howto->dst_mask);
   5846       break;
   5847 
   5848     case R_MIPS_TLS_DTPREL_LO16:
   5849     case R_MIPS_TLS_DTPREL32:
   5850     case R_MIPS_TLS_DTPREL64:
   5851     case R_MIPS16_TLS_DTPREL_LO16:
   5852     case R_MICROMIPS_TLS_DTPREL_LO16:
   5853       value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
   5854       break;
   5855 
   5856     case R_MIPS_TLS_TPREL_HI16:
   5857     case R_MIPS16_TLS_TPREL_HI16:
   5858     case R_MICROMIPS_TLS_TPREL_HI16:
   5859       value = (mips_elf_high (addend + symbol - tprel_base (info))
   5860 	       & howto->dst_mask);
   5861       break;
   5862 
   5863     case R_MIPS_TLS_TPREL_LO16:
   5864     case R_MIPS_TLS_TPREL32:
   5865     case R_MIPS_TLS_TPREL64:
   5866     case R_MIPS16_TLS_TPREL_LO16:
   5867     case R_MICROMIPS_TLS_TPREL_LO16:
   5868       value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
   5869       break;
   5870 
   5871     case R_MIPS_HI16:
   5872     case R_MIPS16_HI16:
   5873     case R_MICROMIPS_HI16:
   5874       if (!gp_disp_p)
   5875 	{
   5876 	  value = mips_elf_high (addend + symbol);
   5877 	  value &= howto->dst_mask;
   5878 	}
   5879       else
   5880 	{
   5881 	  /* For MIPS16 ABI code we generate this sequence
   5882 	        0: li      $v0,%hi(_gp_disp)
   5883 	        4: addiupc $v1,%lo(_gp_disp)
   5884 	        8: sll     $v0,16
   5885 	       12: addu    $v0,$v1
   5886 	       14: move    $gp,$v0
   5887 	     So the offsets of hi and lo relocs are the same, but the
   5888 	     base $pc is that used by the ADDIUPC instruction at $t9 + 4.
   5889 	     ADDIUPC clears the low two bits of the instruction address,
   5890 	     so the base is ($t9 + 4) & ~3.  */
   5891 	  if (r_type == R_MIPS16_HI16)
   5892 	    value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
   5893 	  /* The microMIPS .cpload sequence uses the same assembly
   5894 	     instructions as the traditional psABI version, but the
   5895 	     incoming $t9 has the low bit set.  */
   5896 	  else if (r_type == R_MICROMIPS_HI16)
   5897 	    value = mips_elf_high (addend + gp - p - 1);
   5898 	  else
   5899 	    value = mips_elf_high (addend + gp - p);
   5900 	  overflowed_p = mips_elf_overflow_p (value, 16);
   5901 	}
   5902       break;
   5903 
   5904     case R_MIPS_LO16:
   5905     case R_MIPS16_LO16:
   5906     case R_MICROMIPS_LO16:
   5907     case R_MICROMIPS_HI0_LO16:
   5908       if (!gp_disp_p)
   5909 	value = (symbol + addend) & howto->dst_mask;
   5910       else
   5911 	{
   5912 	  /* See the comment for R_MIPS16_HI16 above for the reason
   5913 	     for this conditional.  */
   5914 	  if (r_type == R_MIPS16_LO16)
   5915 	    value = addend + gp - (p & ~(bfd_vma) 0x3);
   5916 	  else if (r_type == R_MICROMIPS_LO16
   5917 		   || r_type == R_MICROMIPS_HI0_LO16)
   5918 	    value = addend + gp - p + 3;
   5919 	  else
   5920 	    value = addend + gp - p + 4;
   5921 	  /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
   5922 	     for overflow.  But, on, say, IRIX5, relocations against
   5923 	     _gp_disp are normally generated from the .cpload
   5924 	     pseudo-op.  It generates code that normally looks like
   5925 	     this:
   5926 
   5927 	       lui    $gp,%hi(_gp_disp)
   5928 	       addiu  $gp,$gp,%lo(_gp_disp)
   5929 	       addu   $gp,$gp,$t9
   5930 
   5931 	     Here $t9 holds the address of the function being called,
   5932 	     as required by the MIPS ELF ABI.  The R_MIPS_LO16
   5933 	     relocation can easily overflow in this situation, but the
   5934 	     R_MIPS_HI16 relocation will handle the overflow.
   5935 	     Therefore, we consider this a bug in the MIPS ABI, and do
   5936 	     not check for overflow here.  */
   5937 	}
   5938       break;
   5939 
   5940     case R_MIPS_LITERAL:
   5941     case R_MICROMIPS_LITERAL:
   5942       /* Because we don't merge literal sections, we can handle this
   5943 	 just like R_MIPS_GPREL16.  In the long run, we should merge
   5944 	 shared literals, and then we will need to additional work
   5945 	 here.  */
   5946 
   5947       /* Fall through.  */
   5948 
   5949     case R_MIPS16_GPREL:
   5950       /* The R_MIPS16_GPREL performs the same calculation as
   5951 	 R_MIPS_GPREL16, but stores the relocated bits in a different
   5952 	 order.  We don't need to do anything special here; the
   5953 	 differences are handled in mips_elf_perform_relocation.  */
   5954     case R_MIPS_GPREL16:
   5955     case R_MICROMIPS_GPREL7_S2:
   5956     case R_MICROMIPS_GPREL16:
   5957       /* Only sign-extend the addend if it was extracted from the
   5958 	 instruction.  If the addend was separate, leave it alone,
   5959 	 otherwise we may lose significant bits.  */
   5960       if (howto->partial_inplace)
   5961 	addend = _bfd_mips_elf_sign_extend (addend, 16);
   5962       value = symbol + addend - gp;
   5963       /* If the symbol was local, any earlier relocatable links will
   5964 	 have adjusted its addend with the gp offset, so compensate
   5965 	 for that now.  Don't do it for symbols forced local in this
   5966 	 link, though, since they won't have had the gp offset applied
   5967 	 to them before.  */
   5968       if (was_local_p)
   5969 	value += gp0;
   5970       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   5971 	overflowed_p = mips_elf_overflow_p (value, 16);
   5972       break;
   5973 
   5974     case R_MIPS16_GOT16:
   5975     case R_MIPS16_CALL16:
   5976     case R_MIPS_GOT16:
   5977     case R_MIPS_CALL16:
   5978     case R_MICROMIPS_GOT16:
   5979     case R_MICROMIPS_CALL16:
   5980       /* VxWorks does not have separate local and global semantics for
   5981 	 R_MIPS*_GOT16; every relocation evaluates to "G".  */
   5982       if (!htab->is_vxworks && local_p)
   5983 	{
   5984 	  value = mips_elf_got16_entry (abfd, input_bfd, info,
   5985 					symbol + addend, !was_local_p);
   5986 	  if (value == MINUS_ONE)
   5987 	    return bfd_reloc_outofrange;
   5988 	  value
   5989 	    = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
   5990 	  overflowed_p = mips_elf_overflow_p (value, 16);
   5991 	  break;
   5992 	}
   5993 
   5994       /* Fall through.  */
   5995 
   5996     case R_MIPS_TLS_GD:
   5997     case R_MIPS_TLS_GOTTPREL:
   5998     case R_MIPS_TLS_LDM:
   5999     case R_MIPS_GOT_DISP:
   6000     case R_MIPS16_TLS_GD:
   6001     case R_MIPS16_TLS_GOTTPREL:
   6002     case R_MIPS16_TLS_LDM:
   6003     case R_MICROMIPS_TLS_GD:
   6004     case R_MICROMIPS_TLS_GOTTPREL:
   6005     case R_MICROMIPS_TLS_LDM:
   6006     case R_MICROMIPS_GOT_DISP:
   6007       value = g;
   6008       overflowed_p = mips_elf_overflow_p (value, 16);
   6009       break;
   6010 
   6011     case R_MIPS_GPREL32:
   6012       value = (addend + symbol + gp0 - gp);
   6013       if (!save_addend)
   6014 	value &= howto->dst_mask;
   6015       break;
   6016 
   6017     case R_MIPS_PC16:
   6018     case R_MIPS_GNU_REL16_S2:
   6019       if (howto->partial_inplace)
   6020 	addend = _bfd_mips_elf_sign_extend (addend, 18);
   6021 
   6022       if ((symbol + addend) & 3)
   6023 	return bfd_reloc_outofrange;
   6024 
   6025       value = symbol + addend - p;
   6026       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6027 	overflowed_p = mips_elf_overflow_p (value, 18);
   6028       value >>= howto->rightshift;
   6029       value &= howto->dst_mask;
   6030       break;
   6031 
   6032     case R_MIPS16_PC16_S1:
   6033       if (howto->partial_inplace)
   6034 	addend = _bfd_mips_elf_sign_extend (addend, 17);
   6035 
   6036       if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6037 	  && ((symbol + addend) & 1) == 0)
   6038 	return bfd_reloc_outofrange;
   6039 
   6040       value = symbol + addend - p;
   6041       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6042 	overflowed_p = mips_elf_overflow_p (value, 17);
   6043       value >>= howto->rightshift;
   6044       value &= howto->dst_mask;
   6045       break;
   6046 
   6047     case R_MIPS_PC21_S2:
   6048       if (howto->partial_inplace)
   6049 	addend = _bfd_mips_elf_sign_extend (addend, 23);
   6050 
   6051       if ((symbol + addend) & 3)
   6052 	return bfd_reloc_outofrange;
   6053 
   6054       value = symbol + addend - p;
   6055       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6056 	overflowed_p = mips_elf_overflow_p (value, 23);
   6057       value >>= howto->rightshift;
   6058       value &= howto->dst_mask;
   6059       break;
   6060 
   6061     case R_MIPS_PC26_S2:
   6062       if (howto->partial_inplace)
   6063 	addend = _bfd_mips_elf_sign_extend (addend, 28);
   6064 
   6065       if ((symbol + addend) & 3)
   6066 	return bfd_reloc_outofrange;
   6067 
   6068       value = symbol + addend - p;
   6069       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6070 	overflowed_p = mips_elf_overflow_p (value, 28);
   6071       value >>= howto->rightshift;
   6072       value &= howto->dst_mask;
   6073       break;
   6074 
   6075     case R_MIPS_PC18_S3:
   6076       if (howto->partial_inplace)
   6077 	addend = _bfd_mips_elf_sign_extend (addend, 21);
   6078 
   6079       if ((symbol + addend) & 7)
   6080 	return bfd_reloc_outofrange;
   6081 
   6082       value = symbol + addend - ((p | 7) ^ 7);
   6083       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6084 	overflowed_p = mips_elf_overflow_p (value, 21);
   6085       value >>= howto->rightshift;
   6086       value &= howto->dst_mask;
   6087       break;
   6088 
   6089     case R_MIPS_PC19_S2:
   6090       if (howto->partial_inplace)
   6091 	addend = _bfd_mips_elf_sign_extend (addend, 21);
   6092 
   6093       if ((symbol + addend) & 3)
   6094 	return bfd_reloc_outofrange;
   6095 
   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, 21);
   6099       value >>= howto->rightshift;
   6100       value &= howto->dst_mask;
   6101       break;
   6102 
   6103     case R_MIPS_PCHI16:
   6104       value = mips_elf_high (symbol + addend - p);
   6105       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6106 	overflowed_p = mips_elf_overflow_p (value, 16);
   6107       value &= howto->dst_mask;
   6108       break;
   6109 
   6110     case R_MIPS_PCLO16:
   6111       if (howto->partial_inplace)
   6112 	addend = _bfd_mips_elf_sign_extend (addend, 16);
   6113       value = symbol + addend - p;
   6114       value &= howto->dst_mask;
   6115       break;
   6116 
   6117     case R_MICROMIPS_PC7_S1:
   6118       if (howto->partial_inplace)
   6119 	addend = _bfd_mips_elf_sign_extend (addend, 8);
   6120       value = symbol + addend - p;
   6121       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6122 	overflowed_p = mips_elf_overflow_p (value, 8);
   6123       value >>= howto->rightshift;
   6124       value &= howto->dst_mask;
   6125       break;
   6126 
   6127     case R_MICROMIPS_PC10_S1:
   6128       if (howto->partial_inplace)
   6129 	addend = _bfd_mips_elf_sign_extend (addend, 11);
   6130       value = symbol + addend - p;
   6131       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6132 	overflowed_p = mips_elf_overflow_p (value, 11);
   6133       value >>= howto->rightshift;
   6134       value &= howto->dst_mask;
   6135       break;
   6136 
   6137     case R_MICROMIPS_PC16_S1:
   6138       if (howto->partial_inplace)
   6139 	addend = _bfd_mips_elf_sign_extend (addend, 17);
   6140       value = symbol + addend - p;
   6141       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6142 	overflowed_p = mips_elf_overflow_p (value, 17);
   6143       value >>= howto->rightshift;
   6144       value &= howto->dst_mask;
   6145       break;
   6146 
   6147     case R_MICROMIPS_PC23_S2:
   6148       if (howto->partial_inplace)
   6149 	addend = _bfd_mips_elf_sign_extend (addend, 25);
   6150       value = symbol + addend - ((p | 3) ^ 3);
   6151       if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
   6152 	overflowed_p = mips_elf_overflow_p (value, 25);
   6153       value >>= howto->rightshift;
   6154       value &= howto->dst_mask;
   6155       break;
   6156 
   6157     case R_MIPS_GOT_HI16:
   6158     case R_MIPS_CALL_HI16:
   6159     case R_MICROMIPS_GOT_HI16:
   6160     case R_MICROMIPS_CALL_HI16:
   6161       /* We're allowed to handle these two relocations identically.
   6162 	 The dynamic linker is allowed to handle the CALL relocations
   6163 	 differently by creating a lazy evaluation stub.  */
   6164       value = g;
   6165       value = mips_elf_high (value);
   6166       value &= howto->dst_mask;
   6167       break;
   6168 
   6169     case R_MIPS_GOT_LO16:
   6170     case R_MIPS_CALL_LO16:
   6171     case R_MICROMIPS_GOT_LO16:
   6172     case R_MICROMIPS_CALL_LO16:
   6173       value = g & howto->dst_mask;
   6174       break;
   6175 
   6176     case R_MIPS_GOT_PAGE:
   6177     case R_MICROMIPS_GOT_PAGE:
   6178       value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
   6179       if (value == MINUS_ONE)
   6180 	return bfd_reloc_outofrange;
   6181       value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
   6182       overflowed_p = mips_elf_overflow_p (value, 16);
   6183       break;
   6184 
   6185     case R_MIPS_GOT_OFST:
   6186     case R_MICROMIPS_GOT_OFST:
   6187       if (local_p)
   6188 	mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
   6189       else
   6190 	value = addend;
   6191       overflowed_p = mips_elf_overflow_p (value, 16);
   6192       break;
   6193 
   6194     case R_MIPS_SUB:
   6195     case R_MICROMIPS_SUB:
   6196       value = symbol - addend;
   6197       value &= howto->dst_mask;
   6198       break;
   6199 
   6200     case R_MIPS_HIGHER:
   6201     case R_MICROMIPS_HIGHER:
   6202       value = mips_elf_higher (addend + symbol);
   6203       value &= howto->dst_mask;
   6204       break;
   6205 
   6206     case R_MIPS_HIGHEST:
   6207     case R_MICROMIPS_HIGHEST:
   6208       value = mips_elf_highest (addend + symbol);
   6209       value &= howto->dst_mask;
   6210       break;
   6211 
   6212     case R_MIPS_SCN_DISP:
   6213     case R_MICROMIPS_SCN_DISP:
   6214       value = symbol + addend - sec->output_offset;
   6215       value &= howto->dst_mask;
   6216       break;
   6217 
   6218     case R_MIPS_JALR:
   6219     case R_MICROMIPS_JALR:
   6220       /* This relocation is only a hint.  In some cases, we optimize
   6221 	 it into a bal instruction.  But we don't try to optimize
   6222 	 when the symbol does not resolve locally.  */
   6223       if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
   6224 	return bfd_reloc_continue;
   6225       value = symbol + addend;
   6226       break;
   6227 
   6228     case R_MIPS_PJUMP:
   6229     case R_MIPS_GNU_VTINHERIT:
   6230     case R_MIPS_GNU_VTENTRY:
   6231       /* We don't do anything with these at present.  */
   6232       return bfd_reloc_continue;
   6233 
   6234     default:
   6235       /* An unrecognized relocation type.  */
   6236       return bfd_reloc_notsupported;
   6237     }
   6238 
   6239   /* Store the VALUE for our caller.  */
   6240   *valuep = value;
   6241   return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
   6242 }
   6243 
   6244 /* Obtain the field relocated by RELOCATION.  */
   6245 
   6246 static bfd_vma
   6247 mips_elf_obtain_contents (reloc_howto_type *howto,
   6248 			  const Elf_Internal_Rela *relocation,
   6249 			  bfd *input_bfd, bfd_byte *contents)
   6250 {
   6251   bfd_vma x = 0;
   6252   bfd_byte *location = contents + relocation->r_offset;
   6253   unsigned int size = bfd_get_reloc_size (howto);
   6254 
   6255   /* Obtain the bytes.  */
   6256   if (size != 0)
   6257     x = bfd_get (8 * size, input_bfd, location);
   6258 
   6259   return x;
   6260 }
   6261 
   6262 /* It has been determined that the result of the RELOCATION is the
   6263    VALUE.  Use HOWTO to place VALUE into the output file at the
   6264    appropriate position.  The SECTION is the section to which the
   6265    relocation applies.
   6266    CROSS_MODE_JUMP_P is true if the relocation field
   6267    is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
   6268 
   6269    Returns FALSE if anything goes wrong.  */
   6270 
   6271 static bfd_boolean
   6272 mips_elf_perform_relocation (struct bfd_link_info *info,
   6273 			     reloc_howto_type *howto,
   6274 			     const Elf_Internal_Rela *relocation,
   6275 			     bfd_vma value, bfd *input_bfd,
   6276 			     asection *input_section, bfd_byte *contents,
   6277 			     bfd_boolean cross_mode_jump_p)
   6278 {
   6279   bfd_vma x;
   6280   bfd_byte *location;
   6281   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   6282   unsigned int size;
   6283 
   6284   /* Figure out where the relocation is occurring.  */
   6285   location = contents + relocation->r_offset;
   6286 
   6287   _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
   6288 
   6289   /* Obtain the current value.  */
   6290   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
   6291 
   6292   /* Clear the field we are setting.  */
   6293   x &= ~howto->dst_mask;
   6294 
   6295   /* Set the field.  */
   6296   x |= (value & howto->dst_mask);
   6297 
   6298   /* If required, turn JAL into JALX.  */
   6299   if (cross_mode_jump_p && jal_reloc_p (r_type))
   6300     {
   6301       bfd_boolean ok;
   6302       bfd_vma opcode = x >> 26;
   6303       bfd_vma jalx_opcode;
   6304 
   6305       /* Check to see if the opcode is already JAL or JALX.  */
   6306       if (r_type == R_MIPS16_26)
   6307 	{
   6308 	  ok = ((opcode == 0x6) || (opcode == 0x7));
   6309 	  jalx_opcode = 0x7;
   6310 	}
   6311       else if (r_type == R_MICROMIPS_26_S1)
   6312 	{
   6313 	  ok = ((opcode == 0x3d) || (opcode == 0x3c));
   6314 	  jalx_opcode = 0x3c;
   6315 	}
   6316       else
   6317 	{
   6318 	  ok = ((opcode == 0x3) || (opcode == 0x1d));
   6319 	  jalx_opcode = 0x1d;
   6320 	}
   6321 
   6322       /* If the opcode is not JAL or JALX, there's a problem.  We cannot
   6323          convert J or JALS to JALX.  */
   6324       if (!ok)
   6325 	{
   6326 	  info->callbacks->einfo
   6327 	    (_("%X%H: Unsupported jump between ISA modes; "
   6328 	       "consider recompiling with interlinking enabled\n"),
   6329 	     input_bfd, input_section, relocation->r_offset);
   6330 	  return TRUE;
   6331 	}
   6332 
   6333       /* Make this the JALX opcode.  */
   6334       x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
   6335     }
   6336 
   6337   /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
   6338      range.  */
   6339   if (!bfd_link_relocatable (info)
   6340       && !cross_mode_jump_p
   6341       && ((JAL_TO_BAL_P (input_bfd)
   6342 	   && r_type == R_MIPS_26
   6343 	   && (x >> 26) == 0x3)		/* jal addr */
   6344 	  || (JALR_TO_BAL_P (input_bfd)
   6345 	      && r_type == R_MIPS_JALR
   6346 	      && x == 0x0320f809)	/* jalr t9 */
   6347 	  || (JR_TO_B_P (input_bfd)
   6348 	      && r_type == R_MIPS_JALR
   6349 	      && x == 0x03200008)))	/* jr t9 */
   6350     {
   6351       bfd_vma addr;
   6352       bfd_vma dest;
   6353       bfd_signed_vma off;
   6354 
   6355       addr = (input_section->output_section->vma
   6356 	      + input_section->output_offset
   6357 	      + relocation->r_offset
   6358 	      + 4);
   6359       if (r_type == R_MIPS_26)
   6360 	dest = (value << 2) | ((addr >> 28) << 28);
   6361       else
   6362 	dest = value;
   6363       off = dest - addr;
   6364       if (off <= 0x1ffff && off >= -0x20000)
   6365 	{
   6366 	  if (x == 0x03200008)	/* jr t9 */
   6367 	    x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
   6368 	  else
   6369 	    x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
   6370 	}
   6371     }
   6372 
   6373   /* Put the value into the output.  */
   6374   size = bfd_get_reloc_size (howto);
   6375   if (size != 0)
   6376     bfd_put (8 * size, input_bfd, x, location);
   6377 
   6378   _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
   6379 			       location);
   6380 
   6381   return TRUE;
   6382 }
   6383 
   6384 /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
   6386    is the original relocation, which is now being transformed into a
   6387    dynamic relocation.  The ADDENDP is adjusted if necessary; the
   6388    caller should store the result in place of the original addend.  */
   6389 
   6390 static bfd_boolean
   6391 mips_elf_create_dynamic_relocation (bfd *output_bfd,
   6392 				    struct bfd_link_info *info,
   6393 				    const Elf_Internal_Rela *rel,
   6394 				    struct mips_elf_link_hash_entry *h,
   6395 				    asection *sec, bfd_vma symbol,
   6396 				    bfd_vma *addendp, asection *input_section)
   6397 {
   6398   Elf_Internal_Rela outrel[3];
   6399   asection *sreloc;
   6400   bfd *dynobj;
   6401   int r_type;
   6402   long indx;
   6403   bfd_boolean defined_p;
   6404   struct mips_elf_link_hash_table *htab;
   6405 
   6406   htab = mips_elf_hash_table (info);
   6407   BFD_ASSERT (htab != NULL);
   6408 
   6409   r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   6410   dynobj = elf_hash_table (info)->dynobj;
   6411   sreloc = mips_elf_rel_dyn_section (info, FALSE);
   6412   BFD_ASSERT (sreloc != NULL);
   6413   BFD_ASSERT (sreloc->contents != NULL);
   6414   BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
   6415 	      < sreloc->size);
   6416 
   6417   outrel[0].r_offset =
   6418     _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
   6419   if (ABI_64_P (output_bfd))
   6420     {
   6421       outrel[1].r_offset =
   6422 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
   6423       outrel[2].r_offset =
   6424 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
   6425     }
   6426 
   6427   if (outrel[0].r_offset == MINUS_ONE)
   6428     /* The relocation field has been deleted.  */
   6429     return TRUE;
   6430 
   6431   if (outrel[0].r_offset == MINUS_TWO)
   6432     {
   6433       /* The relocation field has been converted into a relative value of
   6434 	 some sort.  Functions like _bfd_elf_write_section_eh_frame expect
   6435 	 the field to be fully relocated, so add in the symbol's value.  */
   6436       *addendp += symbol;
   6437       return TRUE;
   6438     }
   6439 
   6440   /* We must now calculate the dynamic symbol table index to use
   6441      in the relocation.  */
   6442   if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
   6443     {
   6444       BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
   6445       indx = h->root.dynindx;
   6446       if (SGI_COMPAT (output_bfd))
   6447 	defined_p = h->root.def_regular;
   6448       else
   6449 	/* ??? glibc's ld.so just adds the final GOT entry to the
   6450 	   relocation field.  It therefore treats relocs against
   6451 	   defined symbols in the same way as relocs against
   6452 	   undefined symbols.  */
   6453 	defined_p = FALSE;
   6454     }
   6455   else
   6456     {
   6457       if (sec != NULL && bfd_is_abs_section (sec))
   6458 	indx = 0;
   6459       else if (sec == NULL || sec->owner == NULL)
   6460 	{
   6461 	  bfd_set_error (bfd_error_bad_value);
   6462 	  return FALSE;
   6463 	}
   6464       else
   6465 	{
   6466 	  indx = elf_section_data (sec->output_section)->dynindx;
   6467 	  if (indx == 0)
   6468 	    {
   6469 	      asection *osec = htab->root.text_index_section;
   6470 	      indx = elf_section_data (osec)->dynindx;
   6471 	    }
   6472 	  if (indx == 0)
   6473 	    abort ();
   6474 	}
   6475 
   6476       /* Instead of generating a relocation using the section
   6477 	 symbol, we may as well make it a fully relative
   6478 	 relocation.  We want to avoid generating relocations to
   6479 	 local symbols because we used to generate them
   6480 	 incorrectly, without adding the original symbol value,
   6481 	 which is mandated by the ABI for section symbols.  In
   6482 	 order to give dynamic loaders and applications time to
   6483 	 phase out the incorrect use, we refrain from emitting
   6484 	 section-relative relocations.  It's not like they're
   6485 	 useful, after all.  This should be a bit more efficient
   6486 	 as well.  */
   6487       /* ??? Although this behavior is compatible with glibc's ld.so,
   6488 	 the ABI says that relocations against STN_UNDEF should have
   6489 	 a symbol value of 0.  Irix rld honors this, so relocations
   6490 	 against STN_UNDEF have no effect.  */
   6491       if (!SGI_COMPAT (output_bfd))
   6492 	indx = 0;
   6493       defined_p = TRUE;
   6494     }
   6495 
   6496   /* If the relocation was previously an absolute relocation and
   6497      this symbol will not be referred to by the relocation, we must
   6498      adjust it by the value we give it in the dynamic symbol table.
   6499      Otherwise leave the job up to the dynamic linker.  */
   6500   if (defined_p && r_type != R_MIPS_REL32)
   6501     *addendp += symbol;
   6502 
   6503   if (htab->is_vxworks)
   6504     /* VxWorks uses non-relative relocations for this.  */
   6505     outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
   6506   else
   6507     /* The relocation is always an REL32 relocation because we don't
   6508        know where the shared library will wind up at load-time.  */
   6509     outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
   6510 				   R_MIPS_REL32);
   6511 
   6512   /* For strict adherence to the ABI specification, we should
   6513      generate a R_MIPS_64 relocation record by itself before the
   6514      _REL32/_64 record as well, such that the addend is read in as
   6515      a 64-bit value (REL32 is a 32-bit relocation, after all).
   6516      However, since none of the existing ELF64 MIPS dynamic
   6517      loaders seems to care, we don't waste space with these
   6518      artificial relocations.  If this turns out to not be true,
   6519      mips_elf_allocate_dynamic_relocation() should be tweaked so
   6520      as to make room for a pair of dynamic relocations per
   6521      invocation if ABI_64_P, and here we should generate an
   6522      additional relocation record with R_MIPS_64 by itself for a
   6523      NULL symbol before this relocation record.  */
   6524   outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
   6525 				 ABI_64_P (output_bfd)
   6526 				 ? R_MIPS_64
   6527 				 : R_MIPS_NONE);
   6528   outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
   6529 
   6530   /* Adjust the output offset of the relocation to reference the
   6531      correct location in the output file.  */
   6532   outrel[0].r_offset += (input_section->output_section->vma
   6533 			 + input_section->output_offset);
   6534   outrel[1].r_offset += (input_section->output_section->vma
   6535 			 + input_section->output_offset);
   6536   outrel[2].r_offset += (input_section->output_section->vma
   6537 			 + input_section->output_offset);
   6538 
   6539   /* Put the relocation back out.  We have to use the special
   6540      relocation outputter in the 64-bit case since the 64-bit
   6541      relocation format is non-standard.  */
   6542   if (ABI_64_P (output_bfd))
   6543     {
   6544       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
   6545 	(output_bfd, &outrel[0],
   6546 	 (sreloc->contents
   6547 	  + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
   6548     }
   6549   else if (htab->is_vxworks)
   6550     {
   6551       /* VxWorks uses RELA rather than REL dynamic relocations.  */
   6552       outrel[0].r_addend = *addendp;
   6553       bfd_elf32_swap_reloca_out
   6554 	(output_bfd, &outrel[0],
   6555 	 (sreloc->contents
   6556 	  + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
   6557     }
   6558   else
   6559     bfd_elf32_swap_reloc_out
   6560       (output_bfd, &outrel[0],
   6561        (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
   6562 
   6563   /* We've now added another relocation.  */
   6564   ++sreloc->reloc_count;
   6565 
   6566   /* Make sure the output section is writable.  The dynamic linker
   6567      will be writing to it.  */
   6568   elf_section_data (input_section->output_section)->this_hdr.sh_flags
   6569     |= SHF_WRITE;
   6570 
   6571   /* On IRIX5, make an entry of compact relocation info.  */
   6572   if (IRIX_COMPAT (output_bfd) == ict_irix5)
   6573     {
   6574       asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
   6575       bfd_byte *cr;
   6576 
   6577       if (scpt)
   6578 	{
   6579 	  Elf32_crinfo cptrel;
   6580 
   6581 	  mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
   6582 	  cptrel.vaddr = (rel->r_offset
   6583 			  + input_section->output_section->vma
   6584 			  + input_section->output_offset);
   6585 	  if (r_type == R_MIPS_REL32)
   6586 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
   6587 	  else
   6588 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
   6589 	  mips_elf_set_cr_dist2to (cptrel, 0);
   6590 	  cptrel.konst = *addendp;
   6591 
   6592 	  cr = (scpt->contents
   6593 		+ sizeof (Elf32_External_compact_rel));
   6594 	  mips_elf_set_cr_relvaddr (cptrel, 0);
   6595 	  bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
   6596 				     ((Elf32_External_crinfo *) cr
   6597 				      + scpt->reloc_count));
   6598 	  ++scpt->reloc_count;
   6599 	}
   6600     }
   6601 
   6602   /* If we've written this relocation for a readonly section,
   6603      we need to set DF_TEXTREL again, so that we do not delete the
   6604      DT_TEXTREL tag.  */
   6605   if (MIPS_ELF_READONLY_SECTION (input_section))
   6606     info->flags |= DF_TEXTREL;
   6607 
   6608   return TRUE;
   6609 }
   6610 
   6611 /* Return the MACH for a MIPS e_flags value.  */
   6613 
   6614 unsigned long
   6615 _bfd_elf_mips_mach (flagword flags)
   6616 {
   6617   switch (flags & EF_MIPS_MACH)
   6618     {
   6619     case E_MIPS_MACH_3900:
   6620       return bfd_mach_mips3900;
   6621 
   6622     case E_MIPS_MACH_4010:
   6623       return bfd_mach_mips4010;
   6624 
   6625     case E_MIPS_MACH_4100:
   6626       return bfd_mach_mips4100;
   6627 
   6628     case E_MIPS_MACH_4111:
   6629       return bfd_mach_mips4111;
   6630 
   6631     case E_MIPS_MACH_4120:
   6632       return bfd_mach_mips4120;
   6633 
   6634     case E_MIPS_MACH_4650:
   6635       return bfd_mach_mips4650;
   6636 
   6637     case E_MIPS_MACH_5400:
   6638       return bfd_mach_mips5400;
   6639 
   6640     case E_MIPS_MACH_5500:
   6641       return bfd_mach_mips5500;
   6642 
   6643     case E_MIPS_MACH_5900:
   6644       return bfd_mach_mips5900;
   6645 
   6646     case E_MIPS_MACH_9000:
   6647       return bfd_mach_mips9000;
   6648 
   6649     case E_MIPS_MACH_SB1:
   6650       return bfd_mach_mips_sb1;
   6651 
   6652     case E_MIPS_MACH_LS2E:
   6653       return bfd_mach_mips_loongson_2e;
   6654 
   6655     case E_MIPS_MACH_LS2F:
   6656       return bfd_mach_mips_loongson_2f;
   6657 
   6658     case E_MIPS_MACH_LS3A:
   6659       return bfd_mach_mips_loongson_3a;
   6660 
   6661     case E_MIPS_MACH_OCTEON3:
   6662       return bfd_mach_mips_octeon3;
   6663 
   6664     case E_MIPS_MACH_OCTEON2:
   6665       return bfd_mach_mips_octeon2;
   6666 
   6667     case E_MIPS_MACH_OCTEON:
   6668       return bfd_mach_mips_octeon;
   6669 
   6670     case E_MIPS_MACH_XLR:
   6671       return bfd_mach_mips_xlr;
   6672 
   6673     default:
   6674       switch (flags & EF_MIPS_ARCH)
   6675 	{
   6676 	default:
   6677 	case E_MIPS_ARCH_1:
   6678 	  return bfd_mach_mips3000;
   6679 
   6680 	case E_MIPS_ARCH_2:
   6681 	  return bfd_mach_mips6000;
   6682 
   6683 	case E_MIPS_ARCH_3:
   6684 	  return bfd_mach_mips4000;
   6685 
   6686 	case E_MIPS_ARCH_4:
   6687 	  return bfd_mach_mips8000;
   6688 
   6689 	case E_MIPS_ARCH_5:
   6690 	  return bfd_mach_mips5;
   6691 
   6692 	case E_MIPS_ARCH_32:
   6693 	  return bfd_mach_mipsisa32;
   6694 
   6695 	case E_MIPS_ARCH_64:
   6696 	  return bfd_mach_mipsisa64;
   6697 
   6698 	case E_MIPS_ARCH_32R2:
   6699 	  return bfd_mach_mipsisa32r2;
   6700 
   6701 	case E_MIPS_ARCH_64R2:
   6702 	  return bfd_mach_mipsisa64r2;
   6703 
   6704 	case E_MIPS_ARCH_32R6:
   6705 	  return bfd_mach_mipsisa32r6;
   6706 
   6707 	case E_MIPS_ARCH_64R6:
   6708 	  return bfd_mach_mipsisa64r6;
   6709 	}
   6710     }
   6711 
   6712   return 0;
   6713 }
   6714 
   6715 /* Return printable name for ABI.  */
   6716 
   6717 static INLINE char *
   6718 elf_mips_abi_name (bfd *abfd)
   6719 {
   6720   flagword flags;
   6721 
   6722   flags = elf_elfheader (abfd)->e_flags;
   6723   switch (flags & EF_MIPS_ABI)
   6724     {
   6725     case 0:
   6726       if (ABI_N32_P (abfd))
   6727 	return "N32";
   6728       else if (ABI_64_P (abfd))
   6729 	return "64";
   6730       else
   6731 	return "none";
   6732     case E_MIPS_ABI_O32:
   6733       return "O32";
   6734     case E_MIPS_ABI_O64:
   6735       return "O64";
   6736     case E_MIPS_ABI_EABI32:
   6737       return "EABI32";
   6738     case E_MIPS_ABI_EABI64:
   6739       return "EABI64";
   6740     default:
   6741       return "unknown abi";
   6742     }
   6743 }
   6744 
   6745 /* MIPS ELF uses two common sections.  One is the usual one, and the
   6747    other is for small objects.  All the small objects are kept
   6748    together, and then referenced via the gp pointer, which yields
   6749    faster assembler code.  This is what we use for the small common
   6750    section.  This approach is copied from ecoff.c.  */
   6751 static asection mips_elf_scom_section;
   6752 static asymbol mips_elf_scom_symbol;
   6753 static asymbol *mips_elf_scom_symbol_ptr;
   6754 
   6755 /* MIPS ELF also uses an acommon section, which represents an
   6756    allocated common symbol which may be overridden by a
   6757    definition in a shared library.  */
   6758 static asection mips_elf_acom_section;
   6759 static asymbol mips_elf_acom_symbol;
   6760 static asymbol *mips_elf_acom_symbol_ptr;
   6761 
   6762 /* This is used for both the 32-bit and the 64-bit ABI.  */
   6763 
   6764 void
   6765 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
   6766 {
   6767   elf_symbol_type *elfsym;
   6768 
   6769   /* Handle the special MIPS section numbers that a symbol may use.  */
   6770   elfsym = (elf_symbol_type *) asym;
   6771   switch (elfsym->internal_elf_sym.st_shndx)
   6772     {
   6773     case SHN_MIPS_ACOMMON:
   6774       /* This section is used in a dynamically linked executable file.
   6775 	 It is an allocated common section.  The dynamic linker can
   6776 	 either resolve these symbols to something in a shared
   6777 	 library, or it can just leave them here.  For our purposes,
   6778 	 we can consider these symbols to be in a new section.  */
   6779       if (mips_elf_acom_section.name == NULL)
   6780 	{
   6781 	  /* Initialize the acommon section.  */
   6782 	  mips_elf_acom_section.name = ".acommon";
   6783 	  mips_elf_acom_section.flags = SEC_ALLOC;
   6784 	  mips_elf_acom_section.output_section = &mips_elf_acom_section;
   6785 	  mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
   6786 	  mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
   6787 	  mips_elf_acom_symbol.name = ".acommon";
   6788 	  mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
   6789 	  mips_elf_acom_symbol.section = &mips_elf_acom_section;
   6790 	  mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
   6791 	}
   6792       asym->section = &mips_elf_acom_section;
   6793       break;
   6794 
   6795     case SHN_COMMON:
   6796       /* Common symbols less than the GP size are automatically
   6797 	 treated as SHN_MIPS_SCOMMON symbols on IRIX5.  */
   6798       if (asym->value > elf_gp_size (abfd)
   6799 	  || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
   6800 	  || IRIX_COMPAT (abfd) == ict_irix6)
   6801 	break;
   6802       /* Fall through.  */
   6803     case SHN_MIPS_SCOMMON:
   6804       if (mips_elf_scom_section.name == NULL)
   6805 	{
   6806 	  /* Initialize the small common section.  */
   6807 	  mips_elf_scom_section.name = ".scommon";
   6808 	  mips_elf_scom_section.flags = SEC_IS_COMMON;
   6809 	  mips_elf_scom_section.output_section = &mips_elf_scom_section;
   6810 	  mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
   6811 	  mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
   6812 	  mips_elf_scom_symbol.name = ".scommon";
   6813 	  mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
   6814 	  mips_elf_scom_symbol.section = &mips_elf_scom_section;
   6815 	  mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
   6816 	}
   6817       asym->section = &mips_elf_scom_section;
   6818       asym->value = elfsym->internal_elf_sym.st_size;
   6819       break;
   6820 
   6821     case SHN_MIPS_SUNDEFINED:
   6822       asym->section = bfd_und_section_ptr;
   6823       break;
   6824 
   6825     case SHN_MIPS_TEXT:
   6826       {
   6827 	asection *section = bfd_get_section_by_name (abfd, ".text");
   6828 
   6829 	if (section != NULL)
   6830 	  {
   6831 	    asym->section = section;
   6832 	    /* MIPS_TEXT is a bit special, the address is not an offset
   6833 	       to the base of the .text section.  So substract the section
   6834 	       base address to make it an offset.  */
   6835 	    asym->value -= section->vma;
   6836 	  }
   6837       }
   6838       break;
   6839 
   6840     case SHN_MIPS_DATA:
   6841       {
   6842 	asection *section = bfd_get_section_by_name (abfd, ".data");
   6843 
   6844 	if (section != NULL)
   6845 	  {
   6846 	    asym->section = section;
   6847 	    /* MIPS_DATA is a bit special, the address is not an offset
   6848 	       to the base of the .data section.  So substract the section
   6849 	       base address to make it an offset.  */
   6850 	    asym->value -= section->vma;
   6851 	  }
   6852       }
   6853       break;
   6854     }
   6855 
   6856   /* If this is an odd-valued function symbol, assume it's a MIPS16
   6857      or microMIPS one.  */
   6858   if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
   6859       && (asym->value & 1) != 0)
   6860     {
   6861       asym->value--;
   6862       if (MICROMIPS_P (abfd))
   6863 	elfsym->internal_elf_sym.st_other
   6864 	  = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
   6865       else
   6866 	elfsym->internal_elf_sym.st_other
   6867 	  = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
   6868     }
   6869 }
   6870 
   6871 /* Implement elf_backend_eh_frame_address_size.  This differs from
   6873    the default in the way it handles EABI64.
   6874 
   6875    EABI64 was originally specified as an LP64 ABI, and that is what
   6876    -mabi=eabi normally gives on a 64-bit target.  However, gcc has
   6877    historically accepted the combination of -mabi=eabi and -mlong32,
   6878    and this ILP32 variation has become semi-official over time.
   6879    Both forms use elf32 and have pointer-sized FDE addresses.
   6880 
   6881    If an EABI object was generated by GCC 4.0 or above, it will have
   6882    an empty .gcc_compiled_longXX section, where XX is the size of longs
   6883    in bits.  Unfortunately, ILP32 objects generated by earlier compilers
   6884    have no special marking to distinguish them from LP64 objects.
   6885 
   6886    We don't want users of the official LP64 ABI to be punished for the
   6887    existence of the ILP32 variant, but at the same time, we don't want
   6888    to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
   6889    We therefore take the following approach:
   6890 
   6891       - If ABFD contains a .gcc_compiled_longXX section, use it to
   6892         determine the pointer size.
   6893 
   6894       - Otherwise check the type of the first relocation.  Assume that
   6895         the LP64 ABI is being used if the relocation is of type R_MIPS_64.
   6896 
   6897       - Otherwise punt.
   6898 
   6899    The second check is enough to detect LP64 objects generated by pre-4.0
   6900    compilers because, in the kind of output generated by those compilers,
   6901    the first relocation will be associated with either a CIE personality
   6902    routine or an FDE start address.  Furthermore, the compilers never
   6903    used a special (non-pointer) encoding for this ABI.
   6904 
   6905    Checking the relocation type should also be safe because there is no
   6906    reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
   6907    did so.  */
   6908 
   6909 unsigned int
   6910 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
   6911 {
   6912   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   6913     return 8;
   6914   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
   6915     {
   6916       bfd_boolean long32_p, long64_p;
   6917 
   6918       long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
   6919       long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
   6920       if (long32_p && long64_p)
   6921 	return 0;
   6922       if (long32_p)
   6923 	return 4;
   6924       if (long64_p)
   6925 	return 8;
   6926 
   6927       if (sec->reloc_count > 0
   6928 	  && elf_section_data (sec)->relocs != NULL
   6929 	  && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
   6930 	      == R_MIPS_64))
   6931 	return 8;
   6932 
   6933       return 0;
   6934     }
   6935   return 4;
   6936 }
   6937 
   6938 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
   6940    relocations against two unnamed section symbols to resolve to the
   6941    same address.  For example, if we have code like:
   6942 
   6943 	lw	$4,%got_disp(.data)($gp)
   6944 	lw	$25,%got_disp(.text)($gp)
   6945 	jalr	$25
   6946 
   6947    then the linker will resolve both relocations to .data and the program
   6948    will jump there rather than to .text.
   6949 
   6950    We can work around this problem by giving names to local section symbols.
   6951    This is also what the MIPSpro tools do.  */
   6952 
   6953 bfd_boolean
   6954 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
   6955 {
   6956   return SGI_COMPAT (abfd);
   6957 }
   6958 
   6959 /* Work over a section just before writing it out.  This routine is
   6961    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
   6962    sections that need the SHF_MIPS_GPREL flag by name; there has to be
   6963    a better way.  */
   6964 
   6965 bfd_boolean
   6966 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
   6967 {
   6968   if (hdr->sh_type == SHT_MIPS_REGINFO
   6969       && hdr->sh_size > 0)
   6970     {
   6971       bfd_byte buf[4];
   6972 
   6973       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
   6974       BFD_ASSERT (hdr->contents == NULL);
   6975 
   6976       if (bfd_seek (abfd,
   6977 		    hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
   6978 		    SEEK_SET) != 0)
   6979 	return FALSE;
   6980       H_PUT_32 (abfd, elf_gp (abfd), buf);
   6981       if (bfd_bwrite (buf, 4, abfd) != 4)
   6982 	return FALSE;
   6983     }
   6984 
   6985   if (hdr->sh_type == SHT_MIPS_OPTIONS
   6986       && hdr->bfd_section != NULL
   6987       && mips_elf_section_data (hdr->bfd_section) != NULL
   6988       && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
   6989     {
   6990       bfd_byte *contents, *l, *lend;
   6991 
   6992       /* We stored the section contents in the tdata field in the
   6993 	 set_section_contents routine.  We save the section contents
   6994 	 so that we don't have to read them again.
   6995 	 At this point we know that elf_gp is set, so we can look
   6996 	 through the section contents to see if there is an
   6997 	 ODK_REGINFO structure.  */
   6998 
   6999       contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
   7000       l = contents;
   7001       lend = contents + hdr->sh_size;
   7002       while (l + sizeof (Elf_External_Options) <= lend)
   7003 	{
   7004 	  Elf_Internal_Options intopt;
   7005 
   7006 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
   7007 					&intopt);
   7008 	  if (intopt.size < sizeof (Elf_External_Options))
   7009 	    {
   7010 	      (*_bfd_error_handler)
   7011 		(_("%B: Warning: bad `%s' option size %u smaller than its header"),
   7012 		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
   7013 	      break;
   7014 	    }
   7015 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
   7016 	    {
   7017 	      bfd_byte buf[8];
   7018 
   7019 	      if (bfd_seek (abfd,
   7020 			    (hdr->sh_offset
   7021 			     + (l - contents)
   7022 			     + sizeof (Elf_External_Options)
   7023 			     + (sizeof (Elf64_External_RegInfo) - 8)),
   7024 			     SEEK_SET) != 0)
   7025 		return FALSE;
   7026 	      H_PUT_64 (abfd, elf_gp (abfd), buf);
   7027 	      if (bfd_bwrite (buf, 8, abfd) != 8)
   7028 		return FALSE;
   7029 	    }
   7030 	  else if (intopt.kind == ODK_REGINFO)
   7031 	    {
   7032 	      bfd_byte buf[4];
   7033 
   7034 	      if (bfd_seek (abfd,
   7035 			    (hdr->sh_offset
   7036 			     + (l - contents)
   7037 			     + sizeof (Elf_External_Options)
   7038 			     + (sizeof (Elf32_External_RegInfo) - 4)),
   7039 			    SEEK_SET) != 0)
   7040 		return FALSE;
   7041 	      H_PUT_32 (abfd, elf_gp (abfd), buf);
   7042 	      if (bfd_bwrite (buf, 4, abfd) != 4)
   7043 		return FALSE;
   7044 	    }
   7045 	  l += intopt.size;
   7046 	}
   7047     }
   7048 
   7049   if (hdr->bfd_section != NULL)
   7050     {
   7051       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
   7052 
   7053       /* .sbss is not handled specially here because the GNU/Linux
   7054 	 prelinker can convert .sbss from NOBITS to PROGBITS and
   7055 	 changing it back to NOBITS breaks the binary.  The entry in
   7056 	 _bfd_mips_elf_special_sections will ensure the correct flags
   7057 	 are set on .sbss if BFD creates it without reading it from an
   7058 	 input file, and without special handling here the flags set
   7059 	 on it in an input file will be followed.  */
   7060       if (strcmp (name, ".sdata") == 0
   7061 	  || strcmp (name, ".lit8") == 0
   7062 	  || strcmp (name, ".lit4") == 0)
   7063 	hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
   7064       else if (strcmp (name, ".srdata") == 0)
   7065 	hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
   7066       else if (strcmp (name, ".compact_rel") == 0)
   7067 	hdr->sh_flags = 0;
   7068       else if (strcmp (name, ".rtproc") == 0)
   7069 	{
   7070 	  if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
   7071 	    {
   7072 	      unsigned int adjust;
   7073 
   7074 	      adjust = hdr->sh_size % hdr->sh_addralign;
   7075 	      if (adjust != 0)
   7076 		hdr->sh_size += hdr->sh_addralign - adjust;
   7077 	    }
   7078 	}
   7079     }
   7080 
   7081   return TRUE;
   7082 }
   7083 
   7084 /* Handle a MIPS specific section when reading an object file.  This
   7085    is called when elfcode.h finds a section with an unknown type.
   7086    This routine supports both the 32-bit and 64-bit ELF ABI.
   7087 
   7088    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
   7089    how to.  */
   7090 
   7091 bfd_boolean
   7092 _bfd_mips_elf_section_from_shdr (bfd *abfd,
   7093 				 Elf_Internal_Shdr *hdr,
   7094 				 const char *name,
   7095 				 int shindex)
   7096 {
   7097   flagword flags = 0;
   7098 
   7099   /* There ought to be a place to keep ELF backend specific flags, but
   7100      at the moment there isn't one.  We just keep track of the
   7101      sections by their name, instead.  Fortunately, the ABI gives
   7102      suggested names for all the MIPS specific sections, so we will
   7103      probably get away with this.  */
   7104   switch (hdr->sh_type)
   7105     {
   7106     case SHT_MIPS_LIBLIST:
   7107       if (strcmp (name, ".liblist") != 0)
   7108 	return FALSE;
   7109       break;
   7110     case SHT_MIPS_MSYM:
   7111       if (strcmp (name, ".msym") != 0)
   7112 	return FALSE;
   7113       break;
   7114     case SHT_MIPS_CONFLICT:
   7115       if (strcmp (name, ".conflict") != 0)
   7116 	return FALSE;
   7117       break;
   7118     case SHT_MIPS_GPTAB:
   7119       if (! CONST_STRNEQ (name, ".gptab."))
   7120 	return FALSE;
   7121       break;
   7122     case SHT_MIPS_UCODE:
   7123       if (strcmp (name, ".ucode") != 0)
   7124 	return FALSE;
   7125       break;
   7126     case SHT_MIPS_DEBUG:
   7127       if (strcmp (name, ".mdebug") != 0)
   7128 	return FALSE;
   7129       flags = SEC_DEBUGGING;
   7130       break;
   7131     case SHT_MIPS_REGINFO:
   7132       if (strcmp (name, ".reginfo") != 0
   7133 	  || hdr->sh_size != sizeof (Elf32_External_RegInfo))
   7134 	return FALSE;
   7135       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
   7136       break;
   7137     case SHT_MIPS_IFACE:
   7138       if (strcmp (name, ".MIPS.interfaces") != 0)
   7139 	return FALSE;
   7140       break;
   7141     case SHT_MIPS_CONTENT:
   7142       if (! CONST_STRNEQ (name, ".MIPS.content"))
   7143 	return FALSE;
   7144       break;
   7145     case SHT_MIPS_OPTIONS:
   7146       if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
   7147 	return FALSE;
   7148       break;
   7149     case SHT_MIPS_ABIFLAGS:
   7150       if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
   7151 	return FALSE;
   7152       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
   7153       break;
   7154     case SHT_MIPS_DWARF:
   7155       if (! CONST_STRNEQ (name, ".debug_")
   7156           && ! CONST_STRNEQ (name, ".zdebug_"))
   7157 	return FALSE;
   7158       break;
   7159     case SHT_MIPS_SYMBOL_LIB:
   7160       if (strcmp (name, ".MIPS.symlib") != 0)
   7161 	return FALSE;
   7162       break;
   7163     case SHT_MIPS_EVENTS:
   7164       if (! CONST_STRNEQ (name, ".MIPS.events")
   7165 	  && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
   7166 	return FALSE;
   7167       break;
   7168     default:
   7169       break;
   7170     }
   7171 
   7172   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   7173     return FALSE;
   7174 
   7175   if (flags)
   7176     {
   7177       if (! bfd_set_section_flags (abfd, hdr->bfd_section,
   7178 				   (bfd_get_section_flags (abfd,
   7179 							   hdr->bfd_section)
   7180 				    | flags)))
   7181 	return FALSE;
   7182     }
   7183 
   7184   if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
   7185     {
   7186       Elf_External_ABIFlags_v0 ext;
   7187 
   7188       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
   7189 				      &ext, 0, sizeof ext))
   7190 	return FALSE;
   7191       bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
   7192 					&mips_elf_tdata (abfd)->abiflags);
   7193       if (mips_elf_tdata (abfd)->abiflags.version != 0)
   7194 	return FALSE;
   7195       mips_elf_tdata (abfd)->abiflags_valid = TRUE;
   7196     }
   7197 
   7198   /* FIXME: We should record sh_info for a .gptab section.  */
   7199 
   7200   /* For a .reginfo section, set the gp value in the tdata information
   7201      from the contents of this section.  We need the gp value while
   7202      processing relocs, so we just get it now.  The .reginfo section
   7203      is not used in the 64-bit MIPS ELF ABI.  */
   7204   if (hdr->sh_type == SHT_MIPS_REGINFO)
   7205     {
   7206       Elf32_External_RegInfo ext;
   7207       Elf32_RegInfo s;
   7208 
   7209       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
   7210 				      &ext, 0, sizeof ext))
   7211 	return FALSE;
   7212       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
   7213       elf_gp (abfd) = s.ri_gp_value;
   7214     }
   7215 
   7216   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
   7217      set the gp value based on what we find.  We may see both
   7218      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
   7219      they should agree.  */
   7220   if (hdr->sh_type == SHT_MIPS_OPTIONS)
   7221     {
   7222       bfd_byte *contents, *l, *lend;
   7223 
   7224       contents = bfd_malloc (hdr->sh_size);
   7225       if (contents == NULL)
   7226 	return FALSE;
   7227       if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
   7228 				      0, hdr->sh_size))
   7229 	{
   7230 	  free (contents);
   7231 	  return FALSE;
   7232 	}
   7233       l = contents;
   7234       lend = contents + hdr->sh_size;
   7235       while (l + sizeof (Elf_External_Options) <= lend)
   7236 	{
   7237 	  Elf_Internal_Options intopt;
   7238 
   7239 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
   7240 					&intopt);
   7241 	  if (intopt.size < sizeof (Elf_External_Options))
   7242 	    {
   7243 	      (*_bfd_error_handler)
   7244 		(_("%B: Warning: bad `%s' option size %u smaller than its header"),
   7245 		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
   7246 	      break;
   7247 	    }
   7248 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
   7249 	    {
   7250 	      Elf64_Internal_RegInfo intreg;
   7251 
   7252 	      bfd_mips_elf64_swap_reginfo_in
   7253 		(abfd,
   7254 		 ((Elf64_External_RegInfo *)
   7255 		  (l + sizeof (Elf_External_Options))),
   7256 		 &intreg);
   7257 	      elf_gp (abfd) = intreg.ri_gp_value;
   7258 	    }
   7259 	  else if (intopt.kind == ODK_REGINFO)
   7260 	    {
   7261 	      Elf32_RegInfo intreg;
   7262 
   7263 	      bfd_mips_elf32_swap_reginfo_in
   7264 		(abfd,
   7265 		 ((Elf32_External_RegInfo *)
   7266 		  (l + sizeof (Elf_External_Options))),
   7267 		 &intreg);
   7268 	      elf_gp (abfd) = intreg.ri_gp_value;
   7269 	    }
   7270 	  l += intopt.size;
   7271 	}
   7272       free (contents);
   7273     }
   7274 
   7275   return TRUE;
   7276 }
   7277 
   7278 /* Set the correct type for a MIPS ELF section.  We do this by the
   7279    section name, which is a hack, but ought to work.  This routine is
   7280    used by both the 32-bit and the 64-bit ABI.  */
   7281 
   7282 bfd_boolean
   7283 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
   7284 {
   7285   const char *name = bfd_get_section_name (abfd, sec);
   7286 
   7287   if (strcmp (name, ".liblist") == 0)
   7288     {
   7289       hdr->sh_type = SHT_MIPS_LIBLIST;
   7290       hdr->sh_info = sec->size / sizeof (Elf32_Lib);
   7291       /* The sh_link field is set in final_write_processing.  */
   7292     }
   7293   else if (strcmp (name, ".conflict") == 0)
   7294     hdr->sh_type = SHT_MIPS_CONFLICT;
   7295   else if (CONST_STRNEQ (name, ".gptab."))
   7296     {
   7297       hdr->sh_type = SHT_MIPS_GPTAB;
   7298       hdr->sh_entsize = sizeof (Elf32_External_gptab);
   7299       /* The sh_info field is set in final_write_processing.  */
   7300     }
   7301   else if (strcmp (name, ".ucode") == 0)
   7302     hdr->sh_type = SHT_MIPS_UCODE;
   7303   else if (strcmp (name, ".mdebug") == 0)
   7304     {
   7305       hdr->sh_type = SHT_MIPS_DEBUG;
   7306       /* In a shared object on IRIX 5.3, the .mdebug section has an
   7307          entsize of 0.  FIXME: Does this matter?  */
   7308       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
   7309 	hdr->sh_entsize = 0;
   7310       else
   7311 	hdr->sh_entsize = 1;
   7312     }
   7313   else if (strcmp (name, ".reginfo") == 0)
   7314     {
   7315       hdr->sh_type = SHT_MIPS_REGINFO;
   7316       /* In a shared object on IRIX 5.3, the .reginfo section has an
   7317          entsize of 0x18.  FIXME: Does this matter?  */
   7318       if (SGI_COMPAT (abfd))
   7319 	{
   7320 	  if ((abfd->flags & DYNAMIC) != 0)
   7321 	    hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
   7322 	  else
   7323 	    hdr->sh_entsize = 1;
   7324 	}
   7325       else
   7326 	hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
   7327     }
   7328   else if (SGI_COMPAT (abfd)
   7329 	   && (strcmp (name, ".hash") == 0
   7330 	       || strcmp (name, ".dynamic") == 0
   7331 	       || strcmp (name, ".dynstr") == 0))
   7332     {
   7333       if (SGI_COMPAT (abfd))
   7334 	hdr->sh_entsize = 0;
   7335 #if 0
   7336       /* This isn't how the IRIX6 linker behaves.  */
   7337       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
   7338 #endif
   7339     }
   7340   else if (strcmp (name, ".got") == 0
   7341 	   || strcmp (name, ".srdata") == 0
   7342 	   || strcmp (name, ".sdata") == 0
   7343 	   || strcmp (name, ".sbss") == 0
   7344 	   || strcmp (name, ".lit4") == 0
   7345 	   || strcmp (name, ".lit8") == 0)
   7346     hdr->sh_flags |= SHF_MIPS_GPREL;
   7347   else if (strcmp (name, ".MIPS.interfaces") == 0)
   7348     {
   7349       hdr->sh_type = SHT_MIPS_IFACE;
   7350       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7351     }
   7352   else if (CONST_STRNEQ (name, ".MIPS.content"))
   7353     {
   7354       hdr->sh_type = SHT_MIPS_CONTENT;
   7355       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7356       /* The sh_info field is set in final_write_processing.  */
   7357     }
   7358   else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
   7359     {
   7360       hdr->sh_type = SHT_MIPS_OPTIONS;
   7361       hdr->sh_entsize = 1;
   7362       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7363     }
   7364   else if (CONST_STRNEQ (name, ".MIPS.abiflags"))
   7365     {
   7366       hdr->sh_type = SHT_MIPS_ABIFLAGS;
   7367       hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
   7368     }
   7369   else if (CONST_STRNEQ (name, ".debug_")
   7370            || CONST_STRNEQ (name, ".zdebug_"))
   7371     {
   7372       hdr->sh_type = SHT_MIPS_DWARF;
   7373 
   7374       /* Irix facilities such as libexc expect a single .debug_frame
   7375 	 per executable, the system ones have NOSTRIP set and the linker
   7376 	 doesn't merge sections with different flags so ...  */
   7377       if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
   7378 	hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7379     }
   7380   else if (strcmp (name, ".MIPS.symlib") == 0)
   7381     {
   7382       hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
   7383       /* The sh_link and sh_info fields are set in
   7384          final_write_processing.  */
   7385     }
   7386   else if (CONST_STRNEQ (name, ".MIPS.events")
   7387 	   || CONST_STRNEQ (name, ".MIPS.post_rel"))
   7388     {
   7389       hdr->sh_type = SHT_MIPS_EVENTS;
   7390       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   7391       /* The sh_link field is set in final_write_processing.  */
   7392     }
   7393   else if (strcmp (name, ".msym") == 0)
   7394     {
   7395       hdr->sh_type = SHT_MIPS_MSYM;
   7396       hdr->sh_flags |= SHF_ALLOC;
   7397       hdr->sh_entsize = 8;
   7398     }
   7399 
   7400   /* The generic elf_fake_sections will set up REL_HDR using the default
   7401    kind of relocations.  We used to set up a second header for the
   7402    non-default kind of relocations here, but only NewABI would use
   7403    these, and the IRIX ld doesn't like resulting empty RELA sections.
   7404    Thus we create those header only on demand now.  */
   7405 
   7406   return TRUE;
   7407 }
   7408 
   7409 /* Given a BFD section, try to locate the corresponding ELF section
   7410    index.  This is used by both the 32-bit and the 64-bit ABI.
   7411    Actually, it's not clear to me that the 64-bit ABI supports these,
   7412    but for non-PIC objects we will certainly want support for at least
   7413    the .scommon section.  */
   7414 
   7415 bfd_boolean
   7416 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
   7417 					asection *sec, int *retval)
   7418 {
   7419   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
   7420     {
   7421       *retval = SHN_MIPS_SCOMMON;
   7422       return TRUE;
   7423     }
   7424   if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
   7425     {
   7426       *retval = SHN_MIPS_ACOMMON;
   7427       return TRUE;
   7428     }
   7429   return FALSE;
   7430 }
   7431 
   7432 /* Hook called by the linker routine which adds symbols from an object
   7434    file.  We must handle the special MIPS section numbers here.  */
   7435 
   7436 bfd_boolean
   7437 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
   7438 			       Elf_Internal_Sym *sym, const char **namep,
   7439 			       flagword *flagsp ATTRIBUTE_UNUSED,
   7440 			       asection **secp, bfd_vma *valp)
   7441 {
   7442   if (SGI_COMPAT (abfd)
   7443       && (abfd->flags & DYNAMIC) != 0
   7444       && strcmp (*namep, "_rld_new_interface") == 0)
   7445     {
   7446       /* Skip IRIX5 rld entry name.  */
   7447       *namep = NULL;
   7448       return TRUE;
   7449     }
   7450 
   7451   /* Shared objects may have a dynamic symbol '_gp_disp' defined as
   7452      a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
   7453      by setting a DT_NEEDED for the shared object.  Since _gp_disp is
   7454      a magic symbol resolved by the linker, we ignore this bogus definition
   7455      of _gp_disp.  New ABI objects do not suffer from this problem so this
   7456      is not done for them. */
   7457   if (!NEWABI_P(abfd)
   7458       && (sym->st_shndx == SHN_ABS)
   7459       && (strcmp (*namep, "_gp_disp") == 0))
   7460     {
   7461       *namep = NULL;
   7462       return TRUE;
   7463     }
   7464 
   7465   switch (sym->st_shndx)
   7466     {
   7467     case SHN_COMMON:
   7468       /* Common symbols less than the GP size are automatically
   7469 	 treated as SHN_MIPS_SCOMMON symbols.  */
   7470       if (sym->st_size > elf_gp_size (abfd)
   7471 	  || ELF_ST_TYPE (sym->st_info) == STT_TLS
   7472 	  || IRIX_COMPAT (abfd) == ict_irix6)
   7473 	break;
   7474       /* Fall through.  */
   7475     case SHN_MIPS_SCOMMON:
   7476       *secp = bfd_make_section_old_way (abfd, ".scommon");
   7477       (*secp)->flags |= SEC_IS_COMMON;
   7478       *valp = sym->st_size;
   7479       break;
   7480 
   7481     case SHN_MIPS_TEXT:
   7482       /* This section is used in a shared object.  */
   7483       if (mips_elf_tdata (abfd)->elf_text_section == NULL)
   7484 	{
   7485 	  asymbol *elf_text_symbol;
   7486 	  asection *elf_text_section;
   7487 	  bfd_size_type amt = sizeof (asection);
   7488 
   7489 	  elf_text_section = bfd_zalloc (abfd, amt);
   7490 	  if (elf_text_section == NULL)
   7491 	    return FALSE;
   7492 
   7493 	  amt = sizeof (asymbol);
   7494 	  elf_text_symbol = bfd_zalloc (abfd, amt);
   7495 	  if (elf_text_symbol == NULL)
   7496 	    return FALSE;
   7497 
   7498 	  /* Initialize the section.  */
   7499 
   7500 	  mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
   7501 	  mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
   7502 
   7503 	  elf_text_section->symbol = elf_text_symbol;
   7504 	  elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
   7505 
   7506 	  elf_text_section->name = ".text";
   7507 	  elf_text_section->flags = SEC_NO_FLAGS;
   7508 	  elf_text_section->output_section = NULL;
   7509 	  elf_text_section->owner = abfd;
   7510 	  elf_text_symbol->name = ".text";
   7511 	  elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
   7512 	  elf_text_symbol->section = elf_text_section;
   7513 	}
   7514       /* This code used to do *secp = bfd_und_section_ptr if
   7515          bfd_link_pic (info).  I don't know why, and that doesn't make sense,
   7516          so I took it out.  */
   7517       *secp = mips_elf_tdata (abfd)->elf_text_section;
   7518       break;
   7519 
   7520     case SHN_MIPS_ACOMMON:
   7521       /* Fall through. XXX Can we treat this as allocated data?  */
   7522     case SHN_MIPS_DATA:
   7523       /* This section is used in a shared object.  */
   7524       if (mips_elf_tdata (abfd)->elf_data_section == NULL)
   7525 	{
   7526 	  asymbol *elf_data_symbol;
   7527 	  asection *elf_data_section;
   7528 	  bfd_size_type amt = sizeof (asection);
   7529 
   7530 	  elf_data_section = bfd_zalloc (abfd, amt);
   7531 	  if (elf_data_section == NULL)
   7532 	    return FALSE;
   7533 
   7534 	  amt = sizeof (asymbol);
   7535 	  elf_data_symbol = bfd_zalloc (abfd, amt);
   7536 	  if (elf_data_symbol == NULL)
   7537 	    return FALSE;
   7538 
   7539 	  /* Initialize the section.  */
   7540 
   7541 	  mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
   7542 	  mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
   7543 
   7544 	  elf_data_section->symbol = elf_data_symbol;
   7545 	  elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
   7546 
   7547 	  elf_data_section->name = ".data";
   7548 	  elf_data_section->flags = SEC_NO_FLAGS;
   7549 	  elf_data_section->output_section = NULL;
   7550 	  elf_data_section->owner = abfd;
   7551 	  elf_data_symbol->name = ".data";
   7552 	  elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
   7553 	  elf_data_symbol->section = elf_data_section;
   7554 	}
   7555       /* This code used to do *secp = bfd_und_section_ptr if
   7556          bfd_link_pic (info).  I don't know why, and that doesn't make sense,
   7557          so I took it out.  */
   7558       *secp = mips_elf_tdata (abfd)->elf_data_section;
   7559       break;
   7560 
   7561     case SHN_MIPS_SUNDEFINED:
   7562       *secp = bfd_und_section_ptr;
   7563       break;
   7564     }
   7565 
   7566   if (SGI_COMPAT (abfd)
   7567       && ! bfd_link_pic (info)
   7568       && info->output_bfd->xvec == abfd->xvec
   7569       && strcmp (*namep, "__rld_obj_head") == 0)
   7570     {
   7571       struct elf_link_hash_entry *h;
   7572       struct bfd_link_hash_entry *bh;
   7573 
   7574       /* Mark __rld_obj_head as dynamic.  */
   7575       bh = NULL;
   7576       if (! (_bfd_generic_link_add_one_symbol
   7577 	     (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
   7578 	      get_elf_backend_data (abfd)->collect, &bh)))
   7579 	return FALSE;
   7580 
   7581       h = (struct elf_link_hash_entry *) bh;
   7582       h->non_elf = 0;
   7583       h->def_regular = 1;
   7584       h->type = STT_OBJECT;
   7585 
   7586       if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7587 	return FALSE;
   7588 
   7589       mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
   7590       mips_elf_hash_table (info)->rld_symbol = h;
   7591     }
   7592 
   7593   /* If this is a mips16 text symbol, add 1 to the value to make it
   7594      odd.  This will cause something like .word SYM to come up with
   7595      the right value when it is loaded into the PC.  */
   7596   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   7597     ++*valp;
   7598 
   7599   return TRUE;
   7600 }
   7601 
   7602 /* This hook function is called before the linker writes out a global
   7603    symbol.  We mark symbols as small common if appropriate.  This is
   7604    also where we undo the increment of the value for a mips16 symbol.  */
   7605 
   7606 int
   7607 _bfd_mips_elf_link_output_symbol_hook
   7608   (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   7609    const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
   7610    asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
   7611 {
   7612   /* If we see a common symbol, which implies a relocatable link, then
   7613      if a symbol was small common in an input file, mark it as small
   7614      common in the output file.  */
   7615   if (sym->st_shndx == SHN_COMMON
   7616       && strcmp (input_sec->name, ".scommon") == 0)
   7617     sym->st_shndx = SHN_MIPS_SCOMMON;
   7618 
   7619   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   7620     sym->st_value &= ~1;
   7621 
   7622   return 1;
   7623 }
   7624 
   7625 /* Functions for the dynamic linker.  */
   7627 
   7628 /* Create dynamic sections when linking against a dynamic object.  */
   7629 
   7630 bfd_boolean
   7631 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   7632 {
   7633   struct elf_link_hash_entry *h;
   7634   struct bfd_link_hash_entry *bh;
   7635   flagword flags;
   7636   register asection *s;
   7637   const char * const *namep;
   7638   struct mips_elf_link_hash_table *htab;
   7639 
   7640   htab = mips_elf_hash_table (info);
   7641   BFD_ASSERT (htab != NULL);
   7642 
   7643   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   7644 	   | SEC_LINKER_CREATED | SEC_READONLY);
   7645 
   7646   /* The psABI requires a read-only .dynamic section, but the VxWorks
   7647      EABI doesn't.  */
   7648   if (!htab->is_vxworks)
   7649     {
   7650       s = bfd_get_linker_section (abfd, ".dynamic");
   7651       if (s != NULL)
   7652 	{
   7653 	  if (! bfd_set_section_flags (abfd, s, flags))
   7654 	    return FALSE;
   7655 	}
   7656     }
   7657 
   7658   /* We need to create .got section.  */
   7659   if (!mips_elf_create_got_section (abfd, info))
   7660     return FALSE;
   7661 
   7662   if (! mips_elf_rel_dyn_section (info, TRUE))
   7663     return FALSE;
   7664 
   7665   /* Create .stub section.  */
   7666   s = bfd_make_section_anyway_with_flags (abfd,
   7667 					  MIPS_ELF_STUB_SECTION_NAME (abfd),
   7668 					  flags | SEC_CODE);
   7669   if (s == NULL
   7670       || ! bfd_set_section_alignment (abfd, s,
   7671 				      MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   7672     return FALSE;
   7673   htab->sstubs = s;
   7674 
   7675   if (!mips_elf_hash_table (info)->use_rld_obj_head
   7676       && bfd_link_executable (info)
   7677       && bfd_get_linker_section (abfd, ".rld_map") == NULL)
   7678     {
   7679       s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
   7680 					      flags &~ (flagword) SEC_READONLY);
   7681       if (s == NULL
   7682 	  || ! bfd_set_section_alignment (abfd, s,
   7683 					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   7684 	return FALSE;
   7685     }
   7686 
   7687   /* On IRIX5, we adjust add some additional symbols and change the
   7688      alignments of several sections.  There is no ABI documentation
   7689      indicating that this is necessary on IRIX6, nor any evidence that
   7690      the linker takes such action.  */
   7691   if (IRIX_COMPAT (abfd) == ict_irix5)
   7692     {
   7693       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
   7694 	{
   7695 	  bh = NULL;
   7696 	  if (! (_bfd_generic_link_add_one_symbol
   7697 		 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
   7698 		  NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
   7699 	    return FALSE;
   7700 
   7701 	  h = (struct elf_link_hash_entry *) bh;
   7702 	  h->non_elf = 0;
   7703 	  h->def_regular = 1;
   7704 	  h->type = STT_SECTION;
   7705 
   7706 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7707 	    return FALSE;
   7708 	}
   7709 
   7710       /* We need to create a .compact_rel section.  */
   7711       if (SGI_COMPAT (abfd))
   7712 	{
   7713 	  if (!mips_elf_create_compact_rel_section (abfd, info))
   7714 	    return FALSE;
   7715 	}
   7716 
   7717       /* Change alignments of some sections.  */
   7718       s = bfd_get_linker_section (abfd, ".hash");
   7719       if (s != NULL)
   7720 	(void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7721 
   7722       s = bfd_get_linker_section (abfd, ".dynsym");
   7723       if (s != NULL)
   7724 	(void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7725 
   7726       s = bfd_get_linker_section (abfd, ".dynstr");
   7727       if (s != NULL)
   7728 	(void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7729 
   7730       /* ??? */
   7731       s = bfd_get_section_by_name (abfd, ".reginfo");
   7732       if (s != NULL)
   7733 	(void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7734 
   7735       s = bfd_get_linker_section (abfd, ".dynamic");
   7736       if (s != NULL)
   7737 	(void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7738     }
   7739 
   7740   if (bfd_link_executable (info))
   7741     {
   7742       const char *name;
   7743 
   7744       name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
   7745       bh = NULL;
   7746       if (!(_bfd_generic_link_add_one_symbol
   7747 	    (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
   7748 	     NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
   7749 	return FALSE;
   7750 
   7751       h = (struct elf_link_hash_entry *) bh;
   7752       h->non_elf = 0;
   7753       h->def_regular = 1;
   7754       h->type = STT_SECTION;
   7755 
   7756       if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7757 	return FALSE;
   7758 
   7759       if (! mips_elf_hash_table (info)->use_rld_obj_head)
   7760 	{
   7761 	  /* __rld_map is a four byte word located in the .data section
   7762 	     and is filled in by the rtld to contain a pointer to
   7763 	     the _r_debug structure. Its symbol value will be set in
   7764 	     _bfd_mips_elf_finish_dynamic_symbol.  */
   7765 	  s = bfd_get_linker_section (abfd, ".rld_map");
   7766 	  BFD_ASSERT (s != NULL);
   7767 
   7768 	  name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
   7769 	  bh = NULL;
   7770 	  if (!(_bfd_generic_link_add_one_symbol
   7771 		(info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
   7772 		 get_elf_backend_data (abfd)->collect, &bh)))
   7773 	    return FALSE;
   7774 
   7775 	  h = (struct elf_link_hash_entry *) bh;
   7776 	  h->non_elf = 0;
   7777 	  h->def_regular = 1;
   7778 	  h->type = STT_OBJECT;
   7779 
   7780 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7781 	    return FALSE;
   7782 	  mips_elf_hash_table (info)->rld_symbol = h;
   7783 	}
   7784     }
   7785 
   7786   /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
   7787      Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol.  */
   7788   if (!_bfd_elf_create_dynamic_sections (abfd, info))
   7789     return FALSE;
   7790 
   7791   /* Cache the sections created above.  */
   7792   htab->splt = bfd_get_linker_section (abfd, ".plt");
   7793   htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
   7794   if (htab->is_vxworks)
   7795     {
   7796       htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
   7797       htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
   7798     }
   7799   else
   7800     htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
   7801   if (!htab->sdynbss
   7802       || (htab->is_vxworks && !htab->srelbss && !bfd_link_pic (info))
   7803       || !htab->srelplt
   7804       || !htab->splt)
   7805     abort ();
   7806 
   7807   /* Do the usual VxWorks handling.  */
   7808   if (htab->is_vxworks
   7809       && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
   7810     return FALSE;
   7811 
   7812   return TRUE;
   7813 }
   7814 
   7815 /* Return true if relocation REL against section SEC is a REL rather than
   7817    RELA relocation.  RELOCS is the first relocation in the section and
   7818    ABFD is the bfd that contains SEC.  */
   7819 
   7820 static bfd_boolean
   7821 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
   7822 			   const Elf_Internal_Rela *relocs,
   7823 			   const Elf_Internal_Rela *rel)
   7824 {
   7825   Elf_Internal_Shdr *rel_hdr;
   7826   const struct elf_backend_data *bed;
   7827 
   7828   /* To determine which flavor of relocation this is, we depend on the
   7829      fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
   7830   rel_hdr = elf_section_data (sec)->rel.hdr;
   7831   if (rel_hdr == NULL)
   7832     return FALSE;
   7833   bed = get_elf_backend_data (abfd);
   7834   return ((size_t) (rel - relocs)
   7835 	  < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
   7836 }
   7837 
   7838 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
   7839    HOWTO is the relocation's howto and CONTENTS points to the contents
   7840    of the section that REL is against.  */
   7841 
   7842 static bfd_vma
   7843 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
   7844 			  reloc_howto_type *howto, bfd_byte *contents)
   7845 {
   7846   bfd_byte *location;
   7847   unsigned int r_type;
   7848   bfd_vma addend;
   7849   bfd_vma bytes;
   7850 
   7851   r_type = ELF_R_TYPE (abfd, rel->r_info);
   7852   location = contents + rel->r_offset;
   7853 
   7854   /* Get the addend, which is stored in the input file.  */
   7855   _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
   7856   bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
   7857   _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
   7858 
   7859   addend = bytes & howto->src_mask;
   7860 
   7861   /* Shift is 2, unusually, for microMIPS JALX.  Adjust the addend
   7862      accordingly.  */
   7863   if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
   7864     addend <<= 1;
   7865 
   7866   return addend;
   7867 }
   7868 
   7869 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
   7870    and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
   7871    and update *ADDEND with the final addend.  Return true on success
   7872    or false if the LO16 could not be found.  RELEND is the exclusive
   7873    upper bound on the relocations for REL's section.  */
   7874 
   7875 static bfd_boolean
   7876 mips_elf_add_lo16_rel_addend (bfd *abfd,
   7877 			      const Elf_Internal_Rela *rel,
   7878 			      const Elf_Internal_Rela *relend,
   7879 			      bfd_byte *contents, bfd_vma *addend)
   7880 {
   7881   unsigned int r_type, lo16_type;
   7882   const Elf_Internal_Rela *lo16_relocation;
   7883   reloc_howto_type *lo16_howto;
   7884   bfd_vma l;
   7885 
   7886   r_type = ELF_R_TYPE (abfd, rel->r_info);
   7887   if (mips16_reloc_p (r_type))
   7888     lo16_type = R_MIPS16_LO16;
   7889   else if (micromips_reloc_p (r_type))
   7890     lo16_type = R_MICROMIPS_LO16;
   7891   else if (r_type == R_MIPS_PCHI16)
   7892     lo16_type = R_MIPS_PCLO16;
   7893   else
   7894     lo16_type = R_MIPS_LO16;
   7895 
   7896   /* The combined value is the sum of the HI16 addend, left-shifted by
   7897      sixteen bits, and the LO16 addend, sign extended.  (Usually, the
   7898      code does a `lui' of the HI16 value, and then an `addiu' of the
   7899      LO16 value.)
   7900 
   7901      Scan ahead to find a matching LO16 relocation.
   7902 
   7903      According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
   7904      be immediately following.  However, for the IRIX6 ABI, the next
   7905      relocation may be a composed relocation consisting of several
   7906      relocations for the same address.  In that case, the R_MIPS_LO16
   7907      relocation may occur as one of these.  We permit a similar
   7908      extension in general, as that is useful for GCC.
   7909 
   7910      In some cases GCC dead code elimination removes the LO16 but keeps
   7911      the corresponding HI16.  This is strictly speaking a violation of
   7912      the ABI but not immediately harmful.  */
   7913   lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
   7914   if (lo16_relocation == NULL)
   7915     return FALSE;
   7916 
   7917   /* Obtain the addend kept there.  */
   7918   lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
   7919   l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
   7920 
   7921   l <<= lo16_howto->rightshift;
   7922   l = _bfd_mips_elf_sign_extend (l, 16);
   7923 
   7924   *addend <<= 16;
   7925   *addend += l;
   7926   return TRUE;
   7927 }
   7928 
   7929 /* Try to read the contents of section SEC in bfd ABFD.  Return true and
   7930    store the contents in *CONTENTS on success.  Assume that *CONTENTS
   7931    already holds the contents if it is nonull on entry.  */
   7932 
   7933 static bfd_boolean
   7934 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
   7935 {
   7936   if (*contents)
   7937     return TRUE;
   7938 
   7939   /* Get cached copy if it exists.  */
   7940   if (elf_section_data (sec)->this_hdr.contents != NULL)
   7941     {
   7942       *contents = elf_section_data (sec)->this_hdr.contents;
   7943       return TRUE;
   7944     }
   7945 
   7946   return bfd_malloc_and_get_section (abfd, sec, contents);
   7947 }
   7948 
   7949 /* Make a new PLT record to keep internal data.  */
   7950 
   7951 static struct plt_entry *
   7952 mips_elf_make_plt_record (bfd *abfd)
   7953 {
   7954   struct plt_entry *entry;
   7955 
   7956   entry = bfd_zalloc (abfd, sizeof (*entry));
   7957   if (entry == NULL)
   7958     return NULL;
   7959 
   7960   entry->stub_offset = MINUS_ONE;
   7961   entry->mips_offset = MINUS_ONE;
   7962   entry->comp_offset = MINUS_ONE;
   7963   entry->gotplt_index = MINUS_ONE;
   7964   return entry;
   7965 }
   7966 
   7967 /* Look through the relocs for a section during the first phase, and
   7968    allocate space in the global offset table and record the need for
   7969    standard MIPS and compressed procedure linkage table entries.  */
   7970 
   7971 bfd_boolean
   7972 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   7973 			    asection *sec, const Elf_Internal_Rela *relocs)
   7974 {
   7975   const char *name;
   7976   bfd *dynobj;
   7977   Elf_Internal_Shdr *symtab_hdr;
   7978   struct elf_link_hash_entry **sym_hashes;
   7979   size_t extsymoff;
   7980   const Elf_Internal_Rela *rel;
   7981   const Elf_Internal_Rela *rel_end;
   7982   asection *sreloc;
   7983   const struct elf_backend_data *bed;
   7984   struct mips_elf_link_hash_table *htab;
   7985   bfd_byte *contents;
   7986   bfd_vma addend;
   7987   reloc_howto_type *howto;
   7988 
   7989   if (bfd_link_relocatable (info))
   7990     return TRUE;
   7991 
   7992   htab = mips_elf_hash_table (info);
   7993   BFD_ASSERT (htab != NULL);
   7994 
   7995   dynobj = elf_hash_table (info)->dynobj;
   7996   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   7997   sym_hashes = elf_sym_hashes (abfd);
   7998   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
   7999 
   8000   bed = get_elf_backend_data (abfd);
   8001   rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
   8002 
   8003   /* Check for the mips16 stub sections.  */
   8004 
   8005   name = bfd_get_section_name (abfd, sec);
   8006   if (FN_STUB_P (name))
   8007     {
   8008       unsigned long r_symndx;
   8009 
   8010       /* Look at the relocation information to figure out which symbol
   8011          this is for.  */
   8012 
   8013       r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
   8014       if (r_symndx == 0)
   8015 	{
   8016 	  (*_bfd_error_handler)
   8017 	    (_("%B: Warning: cannot determine the target function for"
   8018 	       " stub section `%s'"),
   8019 	     abfd, name);
   8020 	  bfd_set_error (bfd_error_bad_value);
   8021 	  return FALSE;
   8022 	}
   8023 
   8024       if (r_symndx < extsymoff
   8025 	  || sym_hashes[r_symndx - extsymoff] == NULL)
   8026 	{
   8027 	  asection *o;
   8028 
   8029 	  /* This stub is for a local symbol.  This stub will only be
   8030              needed if there is some relocation in this BFD, other
   8031              than a 16 bit function call, which refers to this symbol.  */
   8032 	  for (o = abfd->sections; o != NULL; o = o->next)
   8033 	    {
   8034 	      Elf_Internal_Rela *sec_relocs;
   8035 	      const Elf_Internal_Rela *r, *rend;
   8036 
   8037 	      /* We can ignore stub sections when looking for relocs.  */
   8038 	      if ((o->flags & SEC_RELOC) == 0
   8039 		  || o->reloc_count == 0
   8040 		  || section_allows_mips16_refs_p (o))
   8041 		continue;
   8042 
   8043 	      sec_relocs
   8044 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   8045 					     info->keep_memory);
   8046 	      if (sec_relocs == NULL)
   8047 		return FALSE;
   8048 
   8049 	      rend = sec_relocs + o->reloc_count;
   8050 	      for (r = sec_relocs; r < rend; r++)
   8051 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
   8052 		    && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
   8053 		  break;
   8054 
   8055 	      if (elf_section_data (o)->relocs != sec_relocs)
   8056 		free (sec_relocs);
   8057 
   8058 	      if (r < rend)
   8059 		break;
   8060 	    }
   8061 
   8062 	  if (o == NULL)
   8063 	    {
   8064 	      /* There is no non-call reloc for this stub, so we do
   8065                  not need it.  Since this function is called before
   8066                  the linker maps input sections to output sections, we
   8067                  can easily discard it by setting the SEC_EXCLUDE
   8068                  flag.  */
   8069 	      sec->flags |= SEC_EXCLUDE;
   8070 	      return TRUE;
   8071 	    }
   8072 
   8073 	  /* Record this stub in an array of local symbol stubs for
   8074              this BFD.  */
   8075 	  if (mips_elf_tdata (abfd)->local_stubs == NULL)
   8076 	    {
   8077 	      unsigned long symcount;
   8078 	      asection **n;
   8079 	      bfd_size_type amt;
   8080 
   8081 	      if (elf_bad_symtab (abfd))
   8082 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
   8083 	      else
   8084 		symcount = symtab_hdr->sh_info;
   8085 	      amt = symcount * sizeof (asection *);
   8086 	      n = bfd_zalloc (abfd, amt);
   8087 	      if (n == NULL)
   8088 		return FALSE;
   8089 	      mips_elf_tdata (abfd)->local_stubs = n;
   8090 	    }
   8091 
   8092 	  sec->flags |= SEC_KEEP;
   8093 	  mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
   8094 
   8095 	  /* We don't need to set mips16_stubs_seen in this case.
   8096              That flag is used to see whether we need to look through
   8097              the global symbol table for stubs.  We don't need to set
   8098              it here, because we just have a local stub.  */
   8099 	}
   8100       else
   8101 	{
   8102 	  struct mips_elf_link_hash_entry *h;
   8103 
   8104 	  h = ((struct mips_elf_link_hash_entry *)
   8105 	       sym_hashes[r_symndx - extsymoff]);
   8106 
   8107 	  while (h->root.root.type == bfd_link_hash_indirect
   8108 		 || h->root.root.type == bfd_link_hash_warning)
   8109 	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   8110 
   8111 	  /* H is the symbol this stub is for.  */
   8112 
   8113 	  /* If we already have an appropriate stub for this function, we
   8114 	     don't need another one, so we can discard this one.  Since
   8115 	     this function is called before the linker maps input sections
   8116 	     to output sections, we can easily discard it by setting the
   8117 	     SEC_EXCLUDE flag.  */
   8118 	  if (h->fn_stub != NULL)
   8119 	    {
   8120 	      sec->flags |= SEC_EXCLUDE;
   8121 	      return TRUE;
   8122 	    }
   8123 
   8124 	  sec->flags |= SEC_KEEP;
   8125 	  h->fn_stub = sec;
   8126 	  mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
   8127 	}
   8128     }
   8129   else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
   8130     {
   8131       unsigned long r_symndx;
   8132       struct mips_elf_link_hash_entry *h;
   8133       asection **loc;
   8134 
   8135       /* Look at the relocation information to figure out which symbol
   8136          this is for.  */
   8137 
   8138       r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
   8139       if (r_symndx == 0)
   8140 	{
   8141 	  (*_bfd_error_handler)
   8142 	    (_("%B: Warning: cannot determine the target function for"
   8143 	       " stub section `%s'"),
   8144 	     abfd, name);
   8145 	  bfd_set_error (bfd_error_bad_value);
   8146 	  return FALSE;
   8147 	}
   8148 
   8149       if (r_symndx < extsymoff
   8150 	  || sym_hashes[r_symndx - extsymoff] == NULL)
   8151 	{
   8152 	  asection *o;
   8153 
   8154 	  /* This stub is for a local symbol.  This stub will only be
   8155              needed if there is some relocation (R_MIPS16_26) in this BFD
   8156              that refers to this symbol.  */
   8157 	  for (o = abfd->sections; o != NULL; o = o->next)
   8158 	    {
   8159 	      Elf_Internal_Rela *sec_relocs;
   8160 	      const Elf_Internal_Rela *r, *rend;
   8161 
   8162 	      /* We can ignore stub sections when looking for relocs.  */
   8163 	      if ((o->flags & SEC_RELOC) == 0
   8164 		  || o->reloc_count == 0
   8165 		  || section_allows_mips16_refs_p (o))
   8166 		continue;
   8167 
   8168 	      sec_relocs
   8169 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   8170 					     info->keep_memory);
   8171 	      if (sec_relocs == NULL)
   8172 		return FALSE;
   8173 
   8174 	      rend = sec_relocs + o->reloc_count;
   8175 	      for (r = sec_relocs; r < rend; r++)
   8176 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
   8177 		    && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
   8178 		    break;
   8179 
   8180 	      if (elf_section_data (o)->relocs != sec_relocs)
   8181 		free (sec_relocs);
   8182 
   8183 	      if (r < rend)
   8184 		break;
   8185 	    }
   8186 
   8187 	  if (o == NULL)
   8188 	    {
   8189 	      /* There is no non-call reloc for this stub, so we do
   8190                  not need it.  Since this function is called before
   8191                  the linker maps input sections to output sections, we
   8192                  can easily discard it by setting the SEC_EXCLUDE
   8193                  flag.  */
   8194 	      sec->flags |= SEC_EXCLUDE;
   8195 	      return TRUE;
   8196 	    }
   8197 
   8198 	  /* Record this stub in an array of local symbol call_stubs for
   8199              this BFD.  */
   8200 	  if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
   8201 	    {
   8202 	      unsigned long symcount;
   8203 	      asection **n;
   8204 	      bfd_size_type amt;
   8205 
   8206 	      if (elf_bad_symtab (abfd))
   8207 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
   8208 	      else
   8209 		symcount = symtab_hdr->sh_info;
   8210 	      amt = symcount * sizeof (asection *);
   8211 	      n = bfd_zalloc (abfd, amt);
   8212 	      if (n == NULL)
   8213 		return FALSE;
   8214 	      mips_elf_tdata (abfd)->local_call_stubs = n;
   8215 	    }
   8216 
   8217 	  sec->flags |= SEC_KEEP;
   8218 	  mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
   8219 
   8220 	  /* We don't need to set mips16_stubs_seen in this case.
   8221              That flag is used to see whether we need to look through
   8222              the global symbol table for stubs.  We don't need to set
   8223              it here, because we just have a local stub.  */
   8224 	}
   8225       else
   8226 	{
   8227 	  h = ((struct mips_elf_link_hash_entry *)
   8228 	       sym_hashes[r_symndx - extsymoff]);
   8229 
   8230 	  /* H is the symbol this stub is for.  */
   8231 
   8232 	  if (CALL_FP_STUB_P (name))
   8233 	    loc = &h->call_fp_stub;
   8234 	  else
   8235 	    loc = &h->call_stub;
   8236 
   8237 	  /* If we already have an appropriate stub for this function, we
   8238 	     don't need another one, so we can discard this one.  Since
   8239 	     this function is called before the linker maps input sections
   8240 	     to output sections, we can easily discard it by setting the
   8241 	     SEC_EXCLUDE flag.  */
   8242 	  if (*loc != NULL)
   8243 	    {
   8244 	      sec->flags |= SEC_EXCLUDE;
   8245 	      return TRUE;
   8246 	    }
   8247 
   8248 	  sec->flags |= SEC_KEEP;
   8249 	  *loc = sec;
   8250 	  mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
   8251 	}
   8252     }
   8253 
   8254   sreloc = NULL;
   8255   contents = NULL;
   8256   for (rel = relocs; rel < rel_end; ++rel)
   8257     {
   8258       unsigned long r_symndx;
   8259       unsigned int r_type;
   8260       struct elf_link_hash_entry *h;
   8261       bfd_boolean can_make_dynamic_p;
   8262       bfd_boolean call_reloc_p;
   8263       bfd_boolean constrain_symbol_p;
   8264 
   8265       r_symndx = ELF_R_SYM (abfd, rel->r_info);
   8266       r_type = ELF_R_TYPE (abfd, rel->r_info);
   8267 
   8268       if (r_symndx < extsymoff)
   8269 	h = NULL;
   8270       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
   8271 	{
   8272 	  (*_bfd_error_handler)
   8273 	    (_("%B: Malformed reloc detected for section %s"),
   8274 	     abfd, name);
   8275 	  bfd_set_error (bfd_error_bad_value);
   8276 	  return FALSE;
   8277 	}
   8278       else
   8279 	{
   8280 	  h = sym_hashes[r_symndx - extsymoff];
   8281 	  if (h != NULL)
   8282 	    {
   8283 	      while (h->root.type == bfd_link_hash_indirect
   8284 		     || h->root.type == bfd_link_hash_warning)
   8285 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8286 
   8287 	      /* PR15323, ref flags aren't set for references in the
   8288 		 same object.  */
   8289 	      h->root.non_ir_ref = 1;
   8290 	    }
   8291 	}
   8292 
   8293       /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
   8294 	 relocation into a dynamic one.  */
   8295       can_make_dynamic_p = FALSE;
   8296 
   8297       /* Set CALL_RELOC_P to true if the relocation is for a call,
   8298 	 and if pointer equality therefore doesn't matter.  */
   8299       call_reloc_p = FALSE;
   8300 
   8301       /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
   8302 	 into account when deciding how to define the symbol.
   8303 	 Relocations in nonallocatable sections such as .pdr and
   8304 	 .debug* should have no effect.  */
   8305       constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
   8306 
   8307       switch (r_type)
   8308 	{
   8309 	case R_MIPS_CALL16:
   8310 	case R_MIPS_CALL_HI16:
   8311 	case R_MIPS_CALL_LO16:
   8312 	case R_MIPS16_CALL16:
   8313 	case R_MICROMIPS_CALL16:
   8314 	case R_MICROMIPS_CALL_HI16:
   8315 	case R_MICROMIPS_CALL_LO16:
   8316 	  call_reloc_p = TRUE;
   8317 	  /* Fall through.  */
   8318 
   8319 	case R_MIPS_GOT16:
   8320 	case R_MIPS_GOT_HI16:
   8321 	case R_MIPS_GOT_LO16:
   8322 	case R_MIPS_GOT_PAGE:
   8323 	case R_MIPS_GOT_OFST:
   8324 	case R_MIPS_GOT_DISP:
   8325 	case R_MIPS_TLS_GOTTPREL:
   8326 	case R_MIPS_TLS_GD:
   8327 	case R_MIPS_TLS_LDM:
   8328 	case R_MIPS16_GOT16:
   8329 	case R_MIPS16_TLS_GOTTPREL:
   8330 	case R_MIPS16_TLS_GD:
   8331 	case R_MIPS16_TLS_LDM:
   8332 	case R_MICROMIPS_GOT16:
   8333 	case R_MICROMIPS_GOT_HI16:
   8334 	case R_MICROMIPS_GOT_LO16:
   8335 	case R_MICROMIPS_GOT_PAGE:
   8336 	case R_MICROMIPS_GOT_OFST:
   8337 	case R_MICROMIPS_GOT_DISP:
   8338 	case R_MICROMIPS_TLS_GOTTPREL:
   8339 	case R_MICROMIPS_TLS_GD:
   8340 	case R_MICROMIPS_TLS_LDM:
   8341 	  if (dynobj == NULL)
   8342 	    elf_hash_table (info)->dynobj = dynobj = abfd;
   8343 	  if (!mips_elf_create_got_section (dynobj, info))
   8344 	    return FALSE;
   8345 	  if (htab->is_vxworks && !bfd_link_pic (info))
   8346 	    {
   8347 	      (*_bfd_error_handler)
   8348 		(_("%B: GOT reloc at 0x%lx not expected in executables"),
   8349 		 abfd, (unsigned long) rel->r_offset);
   8350 	      bfd_set_error (bfd_error_bad_value);
   8351 	      return FALSE;
   8352 	    }
   8353 	  can_make_dynamic_p = TRUE;
   8354 	  break;
   8355 
   8356 	case R_MIPS_NONE:
   8357 	case R_MIPS_JALR:
   8358 	case R_MICROMIPS_JALR:
   8359 	  /* These relocations have empty fields and are purely there to
   8360 	     provide link information.  The symbol value doesn't matter.  */
   8361 	  constrain_symbol_p = FALSE;
   8362 	  break;
   8363 
   8364 	case R_MIPS_GPREL16:
   8365 	case R_MIPS_GPREL32:
   8366 	case R_MIPS16_GPREL:
   8367 	case R_MICROMIPS_GPREL16:
   8368 	  /* GP-relative relocations always resolve to a definition in a
   8369 	     regular input file, ignoring the one-definition rule.  This is
   8370 	     important for the GP setup sequence in NewABI code, which
   8371 	     always resolves to a local function even if other relocations
   8372 	     against the symbol wouldn't.  */
   8373 	  constrain_symbol_p = FALSE;
   8374 	  break;
   8375 
   8376 	case R_MIPS_32:
   8377 	case R_MIPS_REL32:
   8378 	case R_MIPS_64:
   8379 	  /* In VxWorks executables, references to external symbols
   8380 	     must be handled using copy relocs or PLT entries; it is not
   8381 	     possible to convert this relocation into a dynamic one.
   8382 
   8383 	     For executables that use PLTs and copy-relocs, we have a
   8384 	     choice between converting the relocation into a dynamic
   8385 	     one or using copy relocations or PLT entries.  It is
   8386 	     usually better to do the former, unless the relocation is
   8387 	     against a read-only section.  */
   8388 	  if ((bfd_link_pic (info)
   8389 	       || (h != NULL
   8390 		   && !htab->is_vxworks
   8391 		   && strcmp (h->root.root.string, "__gnu_local_gp") != 0
   8392 		   && !(!info->nocopyreloc
   8393 			&& !PIC_OBJECT_P (abfd)
   8394 			&& MIPS_ELF_READONLY_SECTION (sec))))
   8395 	      && (sec->flags & SEC_ALLOC) != 0)
   8396 	    {
   8397 	      can_make_dynamic_p = TRUE;
   8398 	      if (dynobj == NULL)
   8399 		elf_hash_table (info)->dynobj = dynobj = abfd;
   8400 	    }
   8401 	  break;
   8402 
   8403 	case R_MIPS_26:
   8404 	case R_MIPS_PC16:
   8405 	case R_MIPS_PC21_S2:
   8406 	case R_MIPS_PC26_S2:
   8407 	case R_MIPS16_26:
   8408 	case R_MIPS16_PC16_S1:
   8409 	case R_MICROMIPS_26_S1:
   8410 	case R_MICROMIPS_PC7_S1:
   8411 	case R_MICROMIPS_PC10_S1:
   8412 	case R_MICROMIPS_PC16_S1:
   8413 	case R_MICROMIPS_PC23_S2:
   8414 	  call_reloc_p = TRUE;
   8415 	  break;
   8416 	}
   8417 
   8418       if (h)
   8419 	{
   8420 	  if (constrain_symbol_p)
   8421 	    {
   8422 	      if (!can_make_dynamic_p)
   8423 		((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
   8424 
   8425 	      if (!call_reloc_p)
   8426 		h->pointer_equality_needed = 1;
   8427 
   8428 	      /* We must not create a stub for a symbol that has
   8429 		 relocations related to taking the function's address.
   8430 		 This doesn't apply to VxWorks, where CALL relocs refer
   8431 		 to a .got.plt entry instead of a normal .got entry.  */
   8432 	      if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
   8433 		((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
   8434 	    }
   8435 
   8436 	  /* Relocations against the special VxWorks __GOTT_BASE__ and
   8437 	     __GOTT_INDEX__ symbols must be left to the loader.  Allocate
   8438 	     room for them in .rela.dyn.  */
   8439 	  if (is_gott_symbol (info, h))
   8440 	    {
   8441 	      if (sreloc == NULL)
   8442 		{
   8443 		  sreloc = mips_elf_rel_dyn_section (info, TRUE);
   8444 		  if (sreloc == NULL)
   8445 		    return FALSE;
   8446 		}
   8447 	      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   8448 	      if (MIPS_ELF_READONLY_SECTION (sec))
   8449 		/* We tell the dynamic linker that there are
   8450 		   relocations against the text segment.  */
   8451 		info->flags |= DF_TEXTREL;
   8452 	    }
   8453 	}
   8454       else if (call_lo16_reloc_p (r_type)
   8455 	       || got_lo16_reloc_p (r_type)
   8456 	       || got_disp_reloc_p (r_type)
   8457 	       || (got16_reloc_p (r_type) && htab->is_vxworks))
   8458 	{
   8459 	  /* We may need a local GOT entry for this relocation.  We
   8460 	     don't count R_MIPS_GOT_PAGE because we can estimate the
   8461 	     maximum number of pages needed by looking at the size of
   8462 	     the segment.  Similar comments apply to R_MIPS*_GOT16 and
   8463 	     R_MIPS*_CALL16, except on VxWorks, where GOT relocations
   8464 	     always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
   8465 	     R_MIPS_CALL_HI16 because these are always followed by an
   8466 	     R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
   8467 	  if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
   8468 						 rel->r_addend, info, r_type))
   8469 	    return FALSE;
   8470 	}
   8471 
   8472       if (h != NULL
   8473 	  && mips_elf_relocation_needs_la25_stub (abfd, r_type,
   8474 						  ELF_ST_IS_MIPS16 (h->other)))
   8475 	((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
   8476 
   8477       switch (r_type)
   8478 	{
   8479 	case R_MIPS_CALL16:
   8480 	case R_MIPS16_CALL16:
   8481 	case R_MICROMIPS_CALL16:
   8482 	  if (h == NULL)
   8483 	    {
   8484 	      (*_bfd_error_handler)
   8485 		(_("%B: CALL16 reloc at 0x%lx not against global symbol"),
   8486 		 abfd, (unsigned long) rel->r_offset);
   8487 	      bfd_set_error (bfd_error_bad_value);
   8488 	      return FALSE;
   8489 	    }
   8490 	  /* Fall through.  */
   8491 
   8492 	case R_MIPS_CALL_HI16:
   8493 	case R_MIPS_CALL_LO16:
   8494 	case R_MICROMIPS_CALL_HI16:
   8495 	case R_MICROMIPS_CALL_LO16:
   8496 	  if (h != NULL)
   8497 	    {
   8498 	      /* Make sure there is room in the regular GOT to hold the
   8499 		 function's address.  We may eliminate it in favour of
   8500 		 a .got.plt entry later; see mips_elf_count_got_symbols.  */
   8501 	      if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
   8502 						      r_type))
   8503 		return FALSE;
   8504 
   8505 	      /* We need a stub, not a plt entry for the undefined
   8506 		 function.  But we record it as if it needs plt.  See
   8507 		 _bfd_elf_adjust_dynamic_symbol.  */
   8508 	      h->needs_plt = 1;
   8509 	      h->type = STT_FUNC;
   8510 	    }
   8511 	  break;
   8512 
   8513 	case R_MIPS_GOT_PAGE:
   8514 	case R_MICROMIPS_GOT_PAGE:
   8515 	case R_MIPS16_GOT16:
   8516 	case R_MIPS_GOT16:
   8517 	case R_MIPS_GOT_HI16:
   8518 	case R_MIPS_GOT_LO16:
   8519 	case R_MICROMIPS_GOT16:
   8520 	case R_MICROMIPS_GOT_HI16:
   8521 	case R_MICROMIPS_GOT_LO16:
   8522 	  if (!h || got_page_reloc_p (r_type))
   8523 	    {
   8524 	      /* This relocation needs (or may need, if h != NULL) a
   8525 		 page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
   8526 		 know for sure until we know whether the symbol is
   8527 		 preemptible.  */
   8528 	      if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
   8529 		{
   8530 		  if (!mips_elf_get_section_contents (abfd, sec, &contents))
   8531 		    return FALSE;
   8532 		  howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
   8533 		  addend = mips_elf_read_rel_addend (abfd, rel,
   8534 						     howto, contents);
   8535 		  if (got16_reloc_p (r_type))
   8536 		    mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
   8537 						  contents, &addend);
   8538 		  else
   8539 		    addend <<= howto->rightshift;
   8540 		}
   8541 	      else
   8542 		addend = rel->r_addend;
   8543 	      if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
   8544 						 h, addend))
   8545 		return FALSE;
   8546 
   8547 	      if (h)
   8548 		{
   8549 		  struct mips_elf_link_hash_entry *hmips =
   8550 		    (struct mips_elf_link_hash_entry *) h;
   8551 
   8552 		  /* This symbol is definitely not overridable.  */
   8553 		  if (hmips->root.def_regular
   8554 		      && ! (bfd_link_pic (info) && ! info->symbolic
   8555 			    && ! hmips->root.forced_local))
   8556 		    h = NULL;
   8557 		}
   8558 	    }
   8559 	  /* If this is a global, overridable symbol, GOT_PAGE will
   8560 	     decay to GOT_DISP, so we'll need a GOT entry for it.  */
   8561 	  /* Fall through.  */
   8562 
   8563 	case R_MIPS_GOT_DISP:
   8564 	case R_MICROMIPS_GOT_DISP:
   8565 	  if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
   8566 						       FALSE, r_type))
   8567 	    return FALSE;
   8568 	  break;
   8569 
   8570 	case R_MIPS_TLS_GOTTPREL:
   8571 	case R_MIPS16_TLS_GOTTPREL:
   8572 	case R_MICROMIPS_TLS_GOTTPREL:
   8573 	  if (bfd_link_pic (info))
   8574 	    info->flags |= DF_STATIC_TLS;
   8575 	  /* Fall through */
   8576 
   8577 	case R_MIPS_TLS_LDM:
   8578 	case R_MIPS16_TLS_LDM:
   8579 	case R_MICROMIPS_TLS_LDM:
   8580 	  if (tls_ldm_reloc_p (r_type))
   8581 	    {
   8582 	      r_symndx = STN_UNDEF;
   8583 	      h = NULL;
   8584 	    }
   8585 	  /* Fall through */
   8586 
   8587 	case R_MIPS_TLS_GD:
   8588 	case R_MIPS16_TLS_GD:
   8589 	case R_MICROMIPS_TLS_GD:
   8590 	  /* This symbol requires a global offset table entry, or two
   8591 	     for TLS GD relocations.  */
   8592 	  if (h != NULL)
   8593 	    {
   8594 	      if (!mips_elf_record_global_got_symbol (h, abfd, info,
   8595 						      FALSE, r_type))
   8596 		return FALSE;
   8597 	    }
   8598 	  else
   8599 	    {
   8600 	      if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
   8601 						     rel->r_addend,
   8602 						     info, r_type))
   8603 		return FALSE;
   8604 	    }
   8605 	  break;
   8606 
   8607 	case R_MIPS_32:
   8608 	case R_MIPS_REL32:
   8609 	case R_MIPS_64:
   8610 	  /* In VxWorks executables, references to external symbols
   8611 	     are handled using copy relocs or PLT stubs, so there's
   8612 	     no need to add a .rela.dyn entry for this relocation.  */
   8613 	  if (can_make_dynamic_p)
   8614 	    {
   8615 	      if (sreloc == NULL)
   8616 		{
   8617 		  sreloc = mips_elf_rel_dyn_section (info, TRUE);
   8618 		  if (sreloc == NULL)
   8619 		    return FALSE;
   8620 		}
   8621 	      if (bfd_link_pic (info) && h == NULL)
   8622 		{
   8623 		  /* When creating a shared object, we must copy these
   8624 		     reloc types into the output file as R_MIPS_REL32
   8625 		     relocs.  Make room for this reloc in .rel(a).dyn.  */
   8626 		  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   8627 		  if (MIPS_ELF_READONLY_SECTION (sec))
   8628 		    /* We tell the dynamic linker that there are
   8629 		       relocations against the text segment.  */
   8630 		    info->flags |= DF_TEXTREL;
   8631 		}
   8632 	      else
   8633 		{
   8634 		  struct mips_elf_link_hash_entry *hmips;
   8635 
   8636 		  /* For a shared object, we must copy this relocation
   8637 		     unless the symbol turns out to be undefined and
   8638 		     weak with non-default visibility, in which case
   8639 		     it will be left as zero.
   8640 
   8641 		     We could elide R_MIPS_REL32 for locally binding symbols
   8642 		     in shared libraries, but do not yet do so.
   8643 
   8644 		     For an executable, we only need to copy this
   8645 		     reloc if the symbol is defined in a dynamic
   8646 		     object.  */
   8647 		  hmips = (struct mips_elf_link_hash_entry *) h;
   8648 		  ++hmips->possibly_dynamic_relocs;
   8649 		  if (MIPS_ELF_READONLY_SECTION (sec))
   8650 		    /* We need it to tell the dynamic linker if there
   8651 		       are relocations against the text segment.  */
   8652 		    hmips->readonly_reloc = TRUE;
   8653 		}
   8654 	    }
   8655 
   8656 	  if (SGI_COMPAT (abfd))
   8657 	    mips_elf_hash_table (info)->compact_rel_size +=
   8658 	      sizeof (Elf32_External_crinfo);
   8659 	  break;
   8660 
   8661 	case R_MIPS_26:
   8662 	case R_MIPS_GPREL16:
   8663 	case R_MIPS_LITERAL:
   8664 	case R_MIPS_GPREL32:
   8665 	case R_MICROMIPS_26_S1:
   8666 	case R_MICROMIPS_GPREL16:
   8667 	case R_MICROMIPS_LITERAL:
   8668 	case R_MICROMIPS_GPREL7_S2:
   8669 	  if (SGI_COMPAT (abfd))
   8670 	    mips_elf_hash_table (info)->compact_rel_size +=
   8671 	      sizeof (Elf32_External_crinfo);
   8672 	  break;
   8673 
   8674 	  /* This relocation describes the C++ object vtable hierarchy.
   8675 	     Reconstruct it for later use during GC.  */
   8676 	case R_MIPS_GNU_VTINHERIT:
   8677 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   8678 	    return FALSE;
   8679 	  break;
   8680 
   8681 	  /* This relocation describes which C++ vtable entries are actually
   8682 	     used.  Record for later use during GC.  */
   8683 	case R_MIPS_GNU_VTENTRY:
   8684 	  BFD_ASSERT (h != NULL);
   8685 	  if (h != NULL
   8686 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
   8687 	    return FALSE;
   8688 	  break;
   8689 
   8690 	default:
   8691 	  break;
   8692 	}
   8693 
   8694       /* Record the need for a PLT entry.  At this point we don't know
   8695          yet if we are going to create a PLT in the first place, but
   8696          we only record whether the relocation requires a standard MIPS
   8697          or a compressed code entry anyway.  If we don't make a PLT after
   8698          all, then we'll just ignore these arrangements.  Likewise if
   8699          a PLT entry is not created because the symbol is satisfied
   8700          locally.  */
   8701       if (h != NULL
   8702 	  && jal_reloc_p (r_type)
   8703 	  && !SYMBOL_CALLS_LOCAL (info, h))
   8704 	{
   8705 	  if (h->plt.plist == NULL)
   8706 	    h->plt.plist = mips_elf_make_plt_record (abfd);
   8707 	  if (h->plt.plist == NULL)
   8708 	    return FALSE;
   8709 
   8710 	  if (r_type == R_MIPS_26)
   8711 	    h->plt.plist->need_mips = TRUE;
   8712 	  else
   8713 	    h->plt.plist->need_comp = TRUE;
   8714 	}
   8715 
   8716       /* See if this reloc would need to refer to a MIPS16 hard-float stub,
   8717 	 if there is one.  We only need to handle global symbols here;
   8718 	 we decide whether to keep or delete stubs for local symbols
   8719 	 when processing the stub's relocations.  */
   8720       if (h != NULL
   8721 	  && !mips16_call_reloc_p (r_type)
   8722 	  && !section_allows_mips16_refs_p (sec))
   8723 	{
   8724 	  struct mips_elf_link_hash_entry *mh;
   8725 
   8726 	  mh = (struct mips_elf_link_hash_entry *) h;
   8727 	  mh->need_fn_stub = TRUE;
   8728 	}
   8729 
   8730       /* Refuse some position-dependent relocations when creating a
   8731 	 shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
   8732 	 not PIC, but we can create dynamic relocations and the result
   8733 	 will be fine.  Also do not refuse R_MIPS_LO16, which can be
   8734 	 combined with R_MIPS_GOT16.  */
   8735       if (bfd_link_pic (info))
   8736 	{
   8737 	  switch (r_type)
   8738 	    {
   8739 	    case R_MIPS16_HI16:
   8740 	    case R_MIPS_HI16:
   8741 	    case R_MIPS_HIGHER:
   8742 	    case R_MIPS_HIGHEST:
   8743 	    case R_MICROMIPS_HI16:
   8744 	    case R_MICROMIPS_HIGHER:
   8745 	    case R_MICROMIPS_HIGHEST:
   8746 	      /* Don't refuse a high part relocation if it's against
   8747 		 no symbol (e.g. part of a compound relocation).  */
   8748 	      if (r_symndx == STN_UNDEF)
   8749 		break;
   8750 
   8751 	      /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
   8752 		 and has a special meaning.  */
   8753 	      if (!NEWABI_P (abfd) && h != NULL
   8754 		  && strcmp (h->root.root.string, "_gp_disp") == 0)
   8755 		break;
   8756 
   8757 	      /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
   8758 	      if (is_gott_symbol (info, h))
   8759 		break;
   8760 
   8761 	      /* FALLTHROUGH */
   8762 
   8763 	    case R_MIPS16_26:
   8764 	    case R_MIPS_26:
   8765 	    case R_MICROMIPS_26_S1:
   8766 	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
   8767 	      (*_bfd_error_handler)
   8768 		(_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
   8769 		 abfd, howto->name,
   8770 		 (h) ? h->root.root.string : "a local symbol");
   8771 	      bfd_set_error (bfd_error_bad_value);
   8772 	      return FALSE;
   8773 	    default:
   8774 	      break;
   8775 	    }
   8776 	}
   8777     }
   8778 
   8779   return TRUE;
   8780 }
   8781 
   8782 bfd_boolean
   8784 _bfd_mips_relax_section (bfd *abfd, asection *sec,
   8785 			 struct bfd_link_info *link_info,
   8786 			 bfd_boolean *again)
   8787 {
   8788   Elf_Internal_Rela *internal_relocs;
   8789   Elf_Internal_Rela *irel, *irelend;
   8790   Elf_Internal_Shdr *symtab_hdr;
   8791   bfd_byte *contents = NULL;
   8792   size_t extsymoff;
   8793   bfd_boolean changed_contents = FALSE;
   8794   bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
   8795   Elf_Internal_Sym *isymbuf = NULL;
   8796 
   8797   /* We are not currently changing any sizes, so only one pass.  */
   8798   *again = FALSE;
   8799 
   8800   if (bfd_link_relocatable (link_info))
   8801     return TRUE;
   8802 
   8803   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   8804 					       link_info->keep_memory);
   8805   if (internal_relocs == NULL)
   8806     return TRUE;
   8807 
   8808   irelend = internal_relocs + sec->reloc_count
   8809     * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
   8810   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8811   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
   8812 
   8813   for (irel = internal_relocs; irel < irelend; irel++)
   8814     {
   8815       bfd_vma symval;
   8816       bfd_signed_vma sym_offset;
   8817       unsigned int r_type;
   8818       unsigned long r_symndx;
   8819       asection *sym_sec;
   8820       unsigned long instruction;
   8821 
   8822       /* Turn jalr into bgezal, and jr into beq, if they're marked
   8823 	 with a JALR relocation, that indicate where they jump to.
   8824 	 This saves some pipeline bubbles.  */
   8825       r_type = ELF_R_TYPE (abfd, irel->r_info);
   8826       if (r_type != R_MIPS_JALR)
   8827 	continue;
   8828 
   8829       r_symndx = ELF_R_SYM (abfd, irel->r_info);
   8830       /* Compute the address of the jump target.  */
   8831       if (r_symndx >= extsymoff)
   8832 	{
   8833 	  struct mips_elf_link_hash_entry *h
   8834 	    = ((struct mips_elf_link_hash_entry *)
   8835 	       elf_sym_hashes (abfd) [r_symndx - extsymoff]);
   8836 
   8837 	  while (h->root.root.type == bfd_link_hash_indirect
   8838 		 || h->root.root.type == bfd_link_hash_warning)
   8839 	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   8840 
   8841 	  /* If a symbol is undefined, or if it may be overridden,
   8842 	     skip it.  */
   8843 	  if (! ((h->root.root.type == bfd_link_hash_defined
   8844 		  || h->root.root.type == bfd_link_hash_defweak)
   8845 		 && h->root.root.u.def.section)
   8846 	      || (bfd_link_pic (link_info) && ! link_info->symbolic
   8847 		  && !h->root.forced_local))
   8848 	    continue;
   8849 
   8850 	  sym_sec = h->root.root.u.def.section;
   8851 	  if (sym_sec->output_section)
   8852 	    symval = (h->root.root.u.def.value
   8853 		      + sym_sec->output_section->vma
   8854 		      + sym_sec->output_offset);
   8855 	  else
   8856 	    symval = h->root.root.u.def.value;
   8857 	}
   8858       else
   8859 	{
   8860 	  Elf_Internal_Sym *isym;
   8861 
   8862 	  /* Read this BFD's symbols if we haven't done so already.  */
   8863 	  if (isymbuf == NULL && symtab_hdr->sh_info != 0)
   8864 	    {
   8865 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   8866 	      if (isymbuf == NULL)
   8867 		isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   8868 						symtab_hdr->sh_info, 0,
   8869 						NULL, NULL, NULL);
   8870 	      if (isymbuf == NULL)
   8871 		goto relax_return;
   8872 	    }
   8873 
   8874 	  isym = isymbuf + r_symndx;
   8875 	  if (isym->st_shndx == SHN_UNDEF)
   8876 	    continue;
   8877 	  else if (isym->st_shndx == SHN_ABS)
   8878 	    sym_sec = bfd_abs_section_ptr;
   8879 	  else if (isym->st_shndx == SHN_COMMON)
   8880 	    sym_sec = bfd_com_section_ptr;
   8881 	  else
   8882 	    sym_sec
   8883 	      = bfd_section_from_elf_index (abfd, isym->st_shndx);
   8884 	  symval = isym->st_value
   8885 	    + sym_sec->output_section->vma
   8886 	    + sym_sec->output_offset;
   8887 	}
   8888 
   8889       /* Compute branch offset, from delay slot of the jump to the
   8890 	 branch target.  */
   8891       sym_offset = (symval + irel->r_addend)
   8892 	- (sec_start + irel->r_offset + 4);
   8893 
   8894       /* Branch offset must be properly aligned.  */
   8895       if ((sym_offset & 3) != 0)
   8896 	continue;
   8897 
   8898       sym_offset >>= 2;
   8899 
   8900       /* Check that it's in range.  */
   8901       if (sym_offset < -0x8000 || sym_offset >= 0x8000)
   8902 	continue;
   8903 
   8904       /* Get the section contents if we haven't done so already.  */
   8905       if (!mips_elf_get_section_contents (abfd, sec, &contents))
   8906 	goto relax_return;
   8907 
   8908       instruction = bfd_get_32 (abfd, contents + irel->r_offset);
   8909 
   8910       /* If it was jalr <reg>, turn it into bgezal $zero, <target>.  */
   8911       if ((instruction & 0xfc1fffff) == 0x0000f809)
   8912 	instruction = 0x04110000;
   8913       /* If it was jr <reg>, turn it into b <target>.  */
   8914       else if ((instruction & 0xfc1fffff) == 0x00000008)
   8915 	instruction = 0x10000000;
   8916       else
   8917 	continue;
   8918 
   8919       instruction |= (sym_offset & 0xffff);
   8920       bfd_put_32 (abfd, instruction, contents + irel->r_offset);
   8921       changed_contents = TRUE;
   8922     }
   8923 
   8924   if (contents != NULL
   8925       && elf_section_data (sec)->this_hdr.contents != contents)
   8926     {
   8927       if (!changed_contents && !link_info->keep_memory)
   8928         free (contents);
   8929       else
   8930         {
   8931           /* Cache the section contents for elf_link_input_bfd.  */
   8932           elf_section_data (sec)->this_hdr.contents = contents;
   8933         }
   8934     }
   8935   return TRUE;
   8936 
   8937  relax_return:
   8938   if (contents != NULL
   8939       && elf_section_data (sec)->this_hdr.contents != contents)
   8940     free (contents);
   8941   return FALSE;
   8942 }
   8943 
   8944 /* Allocate space for global sym dynamic relocs.  */
   8946 
   8947 static bfd_boolean
   8948 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   8949 {
   8950   struct bfd_link_info *info = inf;
   8951   bfd *dynobj;
   8952   struct mips_elf_link_hash_entry *hmips;
   8953   struct mips_elf_link_hash_table *htab;
   8954 
   8955   htab = mips_elf_hash_table (info);
   8956   BFD_ASSERT (htab != NULL);
   8957 
   8958   dynobj = elf_hash_table (info)->dynobj;
   8959   hmips = (struct mips_elf_link_hash_entry *) h;
   8960 
   8961   /* VxWorks executables are handled elsewhere; we only need to
   8962      allocate relocations in shared objects.  */
   8963   if (htab->is_vxworks && !bfd_link_pic (info))
   8964     return TRUE;
   8965 
   8966   /* Ignore indirect symbols.  All relocations against such symbols
   8967      will be redirected to the target symbol.  */
   8968   if (h->root.type == bfd_link_hash_indirect)
   8969     return TRUE;
   8970 
   8971   /* If this symbol is defined in a dynamic object, or we are creating
   8972      a shared library, we will need to copy any R_MIPS_32 or
   8973      R_MIPS_REL32 relocs against it into the output file.  */
   8974   if (! bfd_link_relocatable (info)
   8975       && hmips->possibly_dynamic_relocs != 0
   8976       && (h->root.type == bfd_link_hash_defweak
   8977 	  || (!h->def_regular && !ELF_COMMON_DEF_P (h))
   8978 	  || bfd_link_pic (info)))
   8979     {
   8980       bfd_boolean do_copy = TRUE;
   8981 
   8982       if (h->root.type == bfd_link_hash_undefweak)
   8983 	{
   8984 	  /* Do not copy relocations for undefined weak symbols with
   8985 	     non-default visibility.  */
   8986 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   8987 	    do_copy = FALSE;
   8988 
   8989 	  /* Make sure undefined weak symbols are output as a dynamic
   8990 	     symbol in PIEs.  */
   8991 	  else if (h->dynindx == -1 && !h->forced_local)
   8992 	    {
   8993 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8994 		return FALSE;
   8995 	    }
   8996 	}
   8997 
   8998       if (do_copy)
   8999 	{
   9000 	  /* Even though we don't directly need a GOT entry for this symbol,
   9001 	     the SVR4 psABI requires it to have a dynamic symbol table
   9002 	     index greater that DT_MIPS_GOTSYM if there are dynamic
   9003 	     relocations against it.
   9004 
   9005 	     VxWorks does not enforce the same mapping between the GOT
   9006 	     and the symbol table, so the same requirement does not
   9007 	     apply there.  */
   9008 	  if (!htab->is_vxworks)
   9009 	    {
   9010 	      if (hmips->global_got_area > GGA_RELOC_ONLY)
   9011 		hmips->global_got_area = GGA_RELOC_ONLY;
   9012 	      hmips->got_only_for_calls = FALSE;
   9013 	    }
   9014 
   9015 	  mips_elf_allocate_dynamic_relocations
   9016 	    (dynobj, info, hmips->possibly_dynamic_relocs);
   9017 	  if (hmips->readonly_reloc)
   9018 	    /* We tell the dynamic linker that there are relocations
   9019 	       against the text segment.  */
   9020 	    info->flags |= DF_TEXTREL;
   9021 	}
   9022     }
   9023 
   9024   return TRUE;
   9025 }
   9026 
   9027 /* Adjust a symbol defined by a dynamic object and referenced by a
   9028    regular object.  The current definition is in some section of the
   9029    dynamic object, but we're not including those sections.  We have to
   9030    change the definition to something the rest of the link can
   9031    understand.  */
   9032 
   9033 bfd_boolean
   9034 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   9035 				     struct elf_link_hash_entry *h)
   9036 {
   9037   bfd *dynobj;
   9038   struct mips_elf_link_hash_entry *hmips;
   9039   struct mips_elf_link_hash_table *htab;
   9040 
   9041   htab = mips_elf_hash_table (info);
   9042   BFD_ASSERT (htab != NULL);
   9043 
   9044   dynobj = elf_hash_table (info)->dynobj;
   9045   hmips = (struct mips_elf_link_hash_entry *) h;
   9046 
   9047   /* Make sure we know what is going on here.  */
   9048   BFD_ASSERT (dynobj != NULL
   9049 	      && (h->needs_plt
   9050 		  || h->u.weakdef != NULL
   9051 		  || (h->def_dynamic
   9052 		      && h->ref_regular
   9053 		      && !h->def_regular)));
   9054 
   9055   hmips = (struct mips_elf_link_hash_entry *) h;
   9056 
   9057   /* If there are call relocations against an externally-defined symbol,
   9058      see whether we can create a MIPS lazy-binding stub for it.  We can
   9059      only do this if all references to the function are through call
   9060      relocations, and in that case, the traditional lazy-binding stubs
   9061      are much more efficient than PLT entries.
   9062 
   9063      Traditional stubs are only available on SVR4 psABI-based systems;
   9064      VxWorks always uses PLTs instead.  */
   9065   if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
   9066     {
   9067       if (! elf_hash_table (info)->dynamic_sections_created)
   9068 	return TRUE;
   9069 
   9070       /* If this symbol is not defined in a regular file, then set
   9071 	 the symbol to the stub location.  This is required to make
   9072 	 function pointers compare as equal between the normal
   9073 	 executable and the shared library.  */
   9074       if (!h->def_regular)
   9075 	{
   9076 	  hmips->needs_lazy_stub = TRUE;
   9077 	  htab->lazy_stub_count++;
   9078 	  return TRUE;
   9079 	}
   9080     }
   9081   /* As above, VxWorks requires PLT entries for externally-defined
   9082      functions that are only accessed through call relocations.
   9083 
   9084      Both VxWorks and non-VxWorks targets also need PLT entries if there
   9085      are static-only relocations against an externally-defined function.
   9086      This can technically occur for shared libraries if there are
   9087      branches to the symbol, although it is unlikely that this will be
   9088      used in practice due to the short ranges involved.  It can occur
   9089      for any relative or absolute relocation in executables; in that
   9090      case, the PLT entry becomes the function's canonical address.  */
   9091   else if (((h->needs_plt && !hmips->no_fn_stub)
   9092 	    || (h->type == STT_FUNC && hmips->has_static_relocs))
   9093 	   && htab->use_plts_and_copy_relocs
   9094 	   && !SYMBOL_CALLS_LOCAL (info, h)
   9095 	   && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   9096 		&& h->root.type == bfd_link_hash_undefweak))
   9097     {
   9098       bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
   9099       bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
   9100 
   9101       /* If this is the first symbol to need a PLT entry, then make some
   9102          basic setup.  Also work out PLT entry sizes.  We'll need them
   9103          for PLT offset calculations.  */
   9104       if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
   9105 	{
   9106 	  BFD_ASSERT (htab->sgotplt->size == 0);
   9107 	  BFD_ASSERT (htab->plt_got_index == 0);
   9108 
   9109 	  /* If we're using the PLT additions to the psABI, each PLT
   9110 	     entry is 16 bytes and the PLT0 entry is 32 bytes.
   9111 	     Encourage better cache usage by aligning.  We do this
   9112 	     lazily to avoid pessimizing traditional objects.  */
   9113 	  if (!htab->is_vxworks
   9114 	      && !bfd_set_section_alignment (dynobj, htab->splt, 5))
   9115 	    return FALSE;
   9116 
   9117 	  /* Make sure that .got.plt is word-aligned.  We do this lazily
   9118 	     for the same reason as above.  */
   9119 	  if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
   9120 					  MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
   9121 	    return FALSE;
   9122 
   9123 	  /* On non-VxWorks targets, the first two entries in .got.plt
   9124 	     are reserved.  */
   9125 	  if (!htab->is_vxworks)
   9126 	    htab->plt_got_index
   9127 	      += (get_elf_backend_data (dynobj)->got_header_size
   9128 		  / MIPS_ELF_GOT_SIZE (dynobj));
   9129 
   9130 	  /* On VxWorks, also allocate room for the header's
   9131 	     .rela.plt.unloaded entries.  */
   9132 	  if (htab->is_vxworks && !bfd_link_pic (info))
   9133 	    htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
   9134 
   9135 	  /* Now work out the sizes of individual PLT entries.  */
   9136 	  if (htab->is_vxworks && bfd_link_pic (info))
   9137 	    htab->plt_mips_entry_size
   9138 	      = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
   9139 	  else if (htab->is_vxworks)
   9140 	    htab->plt_mips_entry_size
   9141 	      = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
   9142 	  else if (newabi_p)
   9143 	    htab->plt_mips_entry_size
   9144 	      = 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9145 	  else if (!micromips_p)
   9146 	    {
   9147 	      htab->plt_mips_entry_size
   9148 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9149 	      htab->plt_comp_entry_size
   9150 		= 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
   9151 	    }
   9152 	  else if (htab->insn32)
   9153 	    {
   9154 	      htab->plt_mips_entry_size
   9155 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9156 	      htab->plt_comp_entry_size
   9157 		= 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
   9158 	    }
   9159 	  else
   9160 	    {
   9161 	      htab->plt_mips_entry_size
   9162 		= 4 * ARRAY_SIZE (mips_exec_plt_entry);
   9163 	      htab->plt_comp_entry_size
   9164 		= 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
   9165 	    }
   9166 	}
   9167 
   9168       if (h->plt.plist == NULL)
   9169 	h->plt.plist = mips_elf_make_plt_record (dynobj);
   9170       if (h->plt.plist == NULL)
   9171 	return FALSE;
   9172 
   9173       /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
   9174          n32 or n64, so always use a standard entry there.
   9175 
   9176          If the symbol has a MIPS16 call stub and gets a PLT entry, then
   9177          all MIPS16 calls will go via that stub, and there is no benefit
   9178          to having a MIPS16 entry.  And in the case of call_stub a
   9179          standard entry actually has to be used as the stub ends with a J
   9180          instruction.  */
   9181       if (newabi_p
   9182 	  || htab->is_vxworks
   9183 	  || hmips->call_stub
   9184 	  || hmips->call_fp_stub)
   9185 	{
   9186 	  h->plt.plist->need_mips = TRUE;
   9187 	  h->plt.plist->need_comp = FALSE;
   9188 	}
   9189 
   9190       /* Otherwise, if there are no direct calls to the function, we
   9191          have a free choice of whether to use standard or compressed
   9192          entries.  Prefer microMIPS entries if the object is known to
   9193          contain microMIPS code, so that it becomes possible to create
   9194          pure microMIPS binaries.  Prefer standard entries otherwise,
   9195          because MIPS16 ones are no smaller and are usually slower.  */
   9196       if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
   9197 	{
   9198 	  if (micromips_p)
   9199 	    h->plt.plist->need_comp = TRUE;
   9200 	  else
   9201 	    h->plt.plist->need_mips = TRUE;
   9202 	}
   9203 
   9204       if (h->plt.plist->need_mips)
   9205 	{
   9206 	  h->plt.plist->mips_offset = htab->plt_mips_offset;
   9207 	  htab->plt_mips_offset += htab->plt_mips_entry_size;
   9208 	}
   9209       if (h->plt.plist->need_comp)
   9210 	{
   9211 	  h->plt.plist->comp_offset = htab->plt_comp_offset;
   9212 	  htab->plt_comp_offset += htab->plt_comp_entry_size;
   9213 	}
   9214 
   9215       /* Reserve the corresponding .got.plt entry now too.  */
   9216       h->plt.plist->gotplt_index = htab->plt_got_index++;
   9217 
   9218       /* If the output file has no definition of the symbol, set the
   9219 	 symbol's value to the address of the stub.  */
   9220       if (!bfd_link_pic (info) && !h->def_regular)
   9221 	hmips->use_plt_entry = TRUE;
   9222 
   9223       /* Make room for the R_MIPS_JUMP_SLOT relocation.  */
   9224       htab->srelplt->size += (htab->is_vxworks
   9225 			      ? MIPS_ELF_RELA_SIZE (dynobj)
   9226 			      : MIPS_ELF_REL_SIZE (dynobj));
   9227 
   9228       /* Make room for the .rela.plt.unloaded relocations.  */
   9229       if (htab->is_vxworks && !bfd_link_pic (info))
   9230 	htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
   9231 
   9232       /* All relocations against this symbol that could have been made
   9233 	 dynamic will now refer to the PLT entry instead.  */
   9234       hmips->possibly_dynamic_relocs = 0;
   9235 
   9236       return TRUE;
   9237     }
   9238 
   9239   /* If this is a weak symbol, and there is a real definition, the
   9240      processor independent code will have arranged for us to see the
   9241      real definition first, and we can just use the same value.  */
   9242   if (h->u.weakdef != NULL)
   9243     {
   9244       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   9245 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   9246       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   9247       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   9248       return TRUE;
   9249     }
   9250 
   9251   /* Otherwise, there is nothing further to do for symbols defined
   9252      in regular objects.  */
   9253   if (h->def_regular)
   9254     return TRUE;
   9255 
   9256   /* There's also nothing more to do if we'll convert all relocations
   9257      against this symbol into dynamic relocations.  */
   9258   if (!hmips->has_static_relocs)
   9259     return TRUE;
   9260 
   9261   /* We're now relying on copy relocations.  Complain if we have
   9262      some that we can't convert.  */
   9263   if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
   9264     {
   9265       (*_bfd_error_handler) (_("non-dynamic relocations refer to "
   9266 			       "dynamic symbol %s"),
   9267 			     h->root.root.string);
   9268       bfd_set_error (bfd_error_bad_value);
   9269       return FALSE;
   9270     }
   9271 
   9272   /* We must allocate the symbol in our .dynbss section, which will
   9273      become part of the .bss section of the executable.  There will be
   9274      an entry for this symbol in the .dynsym section.  The dynamic
   9275      object will contain position independent code, so all references
   9276      from the dynamic object to this symbol will go through the global
   9277      offset table.  The dynamic linker will use the .dynsym entry to
   9278      determine the address it must put in the global offset table, so
   9279      both the dynamic object and the regular object will refer to the
   9280      same memory location for the variable.  */
   9281 
   9282   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
   9283     {
   9284       if (htab->is_vxworks)
   9285 	htab->srelbss->size += sizeof (Elf32_External_Rela);
   9286       else
   9287 	mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   9288       h->needs_copy = 1;
   9289     }
   9290 
   9291   /* All relocations against this symbol that could have been made
   9292      dynamic will now refer to the local copy instead.  */
   9293   hmips->possibly_dynamic_relocs = 0;
   9294 
   9295   return _bfd_elf_adjust_dynamic_copy (info, h, htab->sdynbss);
   9296 }
   9297 
   9298 /* This function is called after all the input files have been read,
   9300    and the input sections have been assigned to output sections.  We
   9301    check for any mips16 stub sections that we can discard.  */
   9302 
   9303 bfd_boolean
   9304 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
   9305 				    struct bfd_link_info *info)
   9306 {
   9307   asection *sect;
   9308   struct mips_elf_link_hash_table *htab;
   9309   struct mips_htab_traverse_info hti;
   9310 
   9311   htab = mips_elf_hash_table (info);
   9312   BFD_ASSERT (htab != NULL);
   9313 
   9314   /* The .reginfo section has a fixed size.  */
   9315   sect = bfd_get_section_by_name (output_bfd, ".reginfo");
   9316   if (sect != NULL)
   9317     bfd_set_section_size (output_bfd, sect, sizeof (Elf32_External_RegInfo));
   9318 
   9319   /* The .MIPS.abiflags section has a fixed size.  */
   9320   sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
   9321   if (sect != NULL)
   9322     bfd_set_section_size (output_bfd, sect, sizeof (Elf_External_ABIFlags_v0));
   9323 
   9324   hti.info = info;
   9325   hti.output_bfd = output_bfd;
   9326   hti.error = FALSE;
   9327   mips_elf_link_hash_traverse (mips_elf_hash_table (info),
   9328 			       mips_elf_check_symbols, &hti);
   9329   if (hti.error)
   9330     return FALSE;
   9331 
   9332   return TRUE;
   9333 }
   9334 
   9335 /* If the link uses a GOT, lay it out and work out its size.  */
   9336 
   9337 static bfd_boolean
   9338 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
   9339 {
   9340   bfd *dynobj;
   9341   asection *s;
   9342   struct mips_got_info *g;
   9343   bfd_size_type loadable_size = 0;
   9344   bfd_size_type page_gotno;
   9345   bfd *ibfd;
   9346   struct mips_elf_traverse_got_arg tga;
   9347   struct mips_elf_link_hash_table *htab;
   9348 
   9349   htab = mips_elf_hash_table (info);
   9350   BFD_ASSERT (htab != NULL);
   9351 
   9352   s = htab->sgot;
   9353   if (s == NULL)
   9354     return TRUE;
   9355 
   9356   dynobj = elf_hash_table (info)->dynobj;
   9357   g = htab->got_info;
   9358 
   9359   /* Allocate room for the reserved entries.  VxWorks always reserves
   9360      3 entries; other objects only reserve 2 entries.  */
   9361   BFD_ASSERT (g->assigned_low_gotno == 0);
   9362   if (htab->is_vxworks)
   9363     htab->reserved_gotno = 3;
   9364   else
   9365     htab->reserved_gotno = 2;
   9366   g->local_gotno += htab->reserved_gotno;
   9367   g->assigned_low_gotno = htab->reserved_gotno;
   9368 
   9369   /* Decide which symbols need to go in the global part of the GOT and
   9370      count the number of reloc-only GOT symbols.  */
   9371   mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
   9372 
   9373   if (!mips_elf_resolve_final_got_entries (info, g))
   9374     return FALSE;
   9375 
   9376   /* Calculate the total loadable size of the output.  That
   9377      will give us the maximum number of GOT_PAGE entries
   9378      required.  */
   9379   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   9380     {
   9381       asection *subsection;
   9382 
   9383       for (subsection = ibfd->sections;
   9384 	   subsection;
   9385 	   subsection = subsection->next)
   9386 	{
   9387 	  if ((subsection->flags & SEC_ALLOC) == 0)
   9388 	    continue;
   9389 	  loadable_size += ((subsection->size + 0xf)
   9390 			    &~ (bfd_size_type) 0xf);
   9391 	}
   9392     }
   9393 
   9394   if (htab->is_vxworks)
   9395     /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
   9396        relocations against local symbols evaluate to "G", and the EABI does
   9397        not include R_MIPS_GOT_PAGE.  */
   9398     page_gotno = 0;
   9399   else
   9400     /* Assume there are two loadable segments consisting of contiguous
   9401        sections.  Is 5 enough?  */
   9402     page_gotno = (loadable_size >> 16) + 5;
   9403 
   9404   /* Choose the smaller of the two page estimates; both are intended to be
   9405      conservative.  */
   9406   if (page_gotno > g->page_gotno)
   9407     page_gotno = g->page_gotno;
   9408 
   9409   g->local_gotno += page_gotno;
   9410   g->assigned_high_gotno = g->local_gotno - 1;
   9411 
   9412   s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9413   s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9414   s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   9415 
   9416   /* VxWorks does not support multiple GOTs.  It initializes $gp to
   9417      __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
   9418      dynamic loader.  */
   9419   if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
   9420     {
   9421       if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
   9422 	return FALSE;
   9423     }
   9424   else
   9425     {
   9426       /* Record that all bfds use G.  This also has the effect of freeing
   9427 	 the per-bfd GOTs, which we no longer need.  */
   9428       for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   9429 	if (mips_elf_bfd_got (ibfd, FALSE))
   9430 	  mips_elf_replace_bfd_got (ibfd, g);
   9431       mips_elf_replace_bfd_got (output_bfd, g);
   9432 
   9433       /* Set up TLS entries.  */
   9434       g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
   9435       tga.info = info;
   9436       tga.g = g;
   9437       tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
   9438       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
   9439       if (!tga.g)
   9440 	return FALSE;
   9441       BFD_ASSERT (g->tls_assigned_gotno
   9442 		  == g->global_gotno + g->local_gotno + g->tls_gotno);
   9443 
   9444       /* Each VxWorks GOT entry needs an explicit relocation.  */
   9445       if (htab->is_vxworks && bfd_link_pic (info))
   9446 	g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
   9447 
   9448       /* Allocate room for the TLS relocations.  */
   9449       if (g->relocs)
   9450 	mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
   9451     }
   9452 
   9453   return TRUE;
   9454 }
   9455 
   9456 /* Estimate the size of the .MIPS.stubs section.  */
   9457 
   9458 static void
   9459 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
   9460 {
   9461   struct mips_elf_link_hash_table *htab;
   9462   bfd_size_type dynsymcount;
   9463 
   9464   htab = mips_elf_hash_table (info);
   9465   BFD_ASSERT (htab != NULL);
   9466 
   9467   if (htab->lazy_stub_count == 0)
   9468     return;
   9469 
   9470   /* IRIX rld assumes that a function stub isn't at the end of the .text
   9471      section, so add a dummy entry to the end.  */
   9472   htab->lazy_stub_count++;
   9473 
   9474   /* Get a worst-case estimate of the number of dynamic symbols needed.
   9475      At this point, dynsymcount does not account for section symbols
   9476      and count_section_dynsyms may overestimate the number that will
   9477      be needed.  */
   9478   dynsymcount = (elf_hash_table (info)->dynsymcount
   9479 		 + count_section_dynsyms (output_bfd, info));
   9480 
   9481   /* Determine the size of one stub entry.  There's no disadvantage
   9482      from using microMIPS code here, so for the sake of pure-microMIPS
   9483      binaries we prefer it whenever there's any microMIPS code in
   9484      output produced at all.  This has a benefit of stubs being
   9485      shorter by 4 bytes each too, unless in the insn32 mode.  */
   9486   if (!MICROMIPS_P (output_bfd))
   9487     htab->function_stub_size = (dynsymcount > 0x10000
   9488 				? MIPS_FUNCTION_STUB_BIG_SIZE
   9489 				: MIPS_FUNCTION_STUB_NORMAL_SIZE);
   9490   else if (htab->insn32)
   9491     htab->function_stub_size = (dynsymcount > 0x10000
   9492 				? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
   9493 				: MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
   9494   else
   9495     htab->function_stub_size = (dynsymcount > 0x10000
   9496 				? MICROMIPS_FUNCTION_STUB_BIG_SIZE
   9497 				: MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
   9498 
   9499   htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
   9500 }
   9501 
   9502 /* A mips_elf_link_hash_traverse callback for which DATA points to a
   9503    mips_htab_traverse_info.  If H needs a traditional MIPS lazy-binding
   9504    stub, allocate an entry in the stubs section.  */
   9505 
   9506 static bfd_boolean
   9507 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
   9508 {
   9509   struct mips_htab_traverse_info *hti = data;
   9510   struct mips_elf_link_hash_table *htab;
   9511   struct bfd_link_info *info;
   9512   bfd *output_bfd;
   9513 
   9514   info = hti->info;
   9515   output_bfd = hti->output_bfd;
   9516   htab = mips_elf_hash_table (info);
   9517   BFD_ASSERT (htab != NULL);
   9518 
   9519   if (h->needs_lazy_stub)
   9520     {
   9521       bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
   9522       unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   9523       bfd_vma isa_bit = micromips_p;
   9524 
   9525       BFD_ASSERT (htab->root.dynobj != NULL);
   9526       if (h->root.plt.plist == NULL)
   9527 	h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
   9528       if (h->root.plt.plist == NULL)
   9529 	{
   9530 	  hti->error = TRUE;
   9531 	  return FALSE;
   9532 	}
   9533       h->root.root.u.def.section = htab->sstubs;
   9534       h->root.root.u.def.value = htab->sstubs->size + isa_bit;
   9535       h->root.plt.plist->stub_offset = htab->sstubs->size;
   9536       h->root.other = other;
   9537       htab->sstubs->size += htab->function_stub_size;
   9538     }
   9539   return TRUE;
   9540 }
   9541 
   9542 /* Allocate offsets in the stubs section to each symbol that needs one.
   9543    Set the final size of the .MIPS.stub section.  */
   9544 
   9545 static bfd_boolean
   9546 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
   9547 {
   9548   bfd *output_bfd = info->output_bfd;
   9549   bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
   9550   unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   9551   bfd_vma isa_bit = micromips_p;
   9552   struct mips_elf_link_hash_table *htab;
   9553   struct mips_htab_traverse_info hti;
   9554   struct elf_link_hash_entry *h;
   9555   bfd *dynobj;
   9556 
   9557   htab = mips_elf_hash_table (info);
   9558   BFD_ASSERT (htab != NULL);
   9559 
   9560   if (htab->lazy_stub_count == 0)
   9561     return TRUE;
   9562 
   9563   htab->sstubs->size = 0;
   9564   hti.info = info;
   9565   hti.output_bfd = output_bfd;
   9566   hti.error = FALSE;
   9567   mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
   9568   if (hti.error)
   9569     return FALSE;
   9570   htab->sstubs->size += htab->function_stub_size;
   9571   BFD_ASSERT (htab->sstubs->size
   9572 	      == htab->lazy_stub_count * htab->function_stub_size);
   9573 
   9574   dynobj = elf_hash_table (info)->dynobj;
   9575   BFD_ASSERT (dynobj != NULL);
   9576   h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
   9577   if (h == NULL)
   9578     return FALSE;
   9579   h->root.u.def.value = isa_bit;
   9580   h->other = other;
   9581   h->type = STT_FUNC;
   9582 
   9583   return TRUE;
   9584 }
   9585 
   9586 /* A mips_elf_link_hash_traverse callback for which DATA points to a
   9587    bfd_link_info.  If H uses the address of a PLT entry as the value
   9588    of the symbol, then set the entry in the symbol table now.  Prefer
   9589    a standard MIPS PLT entry.  */
   9590 
   9591 static bfd_boolean
   9592 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
   9593 {
   9594   struct bfd_link_info *info = data;
   9595   bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
   9596   struct mips_elf_link_hash_table *htab;
   9597   unsigned int other;
   9598   bfd_vma isa_bit;
   9599   bfd_vma val;
   9600 
   9601   htab = mips_elf_hash_table (info);
   9602   BFD_ASSERT (htab != NULL);
   9603 
   9604   if (h->use_plt_entry)
   9605     {
   9606       BFD_ASSERT (h->root.plt.plist != NULL);
   9607       BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
   9608 		  || h->root.plt.plist->comp_offset != MINUS_ONE);
   9609 
   9610       val = htab->plt_header_size;
   9611       if (h->root.plt.plist->mips_offset != MINUS_ONE)
   9612 	{
   9613 	  isa_bit = 0;
   9614 	  val += h->root.plt.plist->mips_offset;
   9615 	  other = 0;
   9616 	}
   9617       else
   9618 	{
   9619 	  isa_bit = 1;
   9620 	  val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
   9621 	  other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
   9622 	}
   9623       val += isa_bit;
   9624       /* For VxWorks, point at the PLT load stub rather than the lazy
   9625          resolution stub; this stub will become the canonical function
   9626          address.  */
   9627       if (htab->is_vxworks)
   9628 	val += 8;
   9629 
   9630       h->root.root.u.def.section = htab->splt;
   9631       h->root.root.u.def.value = val;
   9632       h->root.other = other;
   9633     }
   9634 
   9635   return TRUE;
   9636 }
   9637 
   9638 /* Set the sizes of the dynamic sections.  */
   9639 
   9640 bfd_boolean
   9641 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
   9642 				     struct bfd_link_info *info)
   9643 {
   9644   bfd *dynobj;
   9645   asection *s, *sreldyn;
   9646   bfd_boolean reltext;
   9647   struct mips_elf_link_hash_table *htab;
   9648 
   9649   htab = mips_elf_hash_table (info);
   9650   BFD_ASSERT (htab != NULL);
   9651   dynobj = elf_hash_table (info)->dynobj;
   9652   BFD_ASSERT (dynobj != NULL);
   9653 
   9654   if (elf_hash_table (info)->dynamic_sections_created)
   9655     {
   9656       /* Set the contents of the .interp section to the interpreter.  */
   9657       if (bfd_link_executable (info) && !info->nointerp)
   9658 	{
   9659 	  s = bfd_get_linker_section (dynobj, ".interp");
   9660 	  BFD_ASSERT (s != NULL);
   9661 	  s->size
   9662 	    = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
   9663 	  s->contents
   9664 	    = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
   9665 	}
   9666 
   9667       /* Figure out the size of the PLT header if we know that we
   9668          are using it.  For the sake of cache alignment always use
   9669          a standard header whenever any standard entries are present
   9670          even if microMIPS entries are present as well.  This also
   9671          lets the microMIPS header rely on the value of $v0 only set
   9672          by microMIPS entries, for a small size reduction.
   9673 
   9674          Set symbol table entry values for symbols that use the
   9675          address of their PLT entry now that we can calculate it.
   9676 
   9677          Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
   9678          haven't already in _bfd_elf_create_dynamic_sections.  */
   9679       if (htab->splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
   9680 	{
   9681 	  bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
   9682 				     && !htab->plt_mips_offset);
   9683 	  unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   9684 	  bfd_vma isa_bit = micromips_p;
   9685 	  struct elf_link_hash_entry *h;
   9686 	  bfd_vma size;
   9687 
   9688 	  BFD_ASSERT (htab->use_plts_and_copy_relocs);
   9689 	  BFD_ASSERT (htab->sgotplt->size == 0);
   9690 	  BFD_ASSERT (htab->splt->size == 0);
   9691 
   9692 	  if (htab->is_vxworks && bfd_link_pic (info))
   9693 	    size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
   9694 	  else if (htab->is_vxworks)
   9695 	    size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
   9696 	  else if (ABI_64_P (output_bfd))
   9697 	    size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
   9698 	  else if (ABI_N32_P (output_bfd))
   9699 	    size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
   9700 	  else if (!micromips_p)
   9701 	    size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
   9702 	  else if (htab->insn32)
   9703 	    size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
   9704 	  else
   9705 	    size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
   9706 
   9707 	  htab->plt_header_is_comp = micromips_p;
   9708 	  htab->plt_header_size = size;
   9709 	  htab->splt->size = (size
   9710 			      + htab->plt_mips_offset
   9711 			      + htab->plt_comp_offset);
   9712 	  htab->sgotplt->size = (htab->plt_got_index
   9713 				 * MIPS_ELF_GOT_SIZE (dynobj));
   9714 
   9715 	  mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
   9716 
   9717 	  if (htab->root.hplt == NULL)
   9718 	    {
   9719 	      h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
   9720 					       "_PROCEDURE_LINKAGE_TABLE_");
   9721 	      htab->root.hplt = h;
   9722 	      if (h == NULL)
   9723 		return FALSE;
   9724 	    }
   9725 
   9726 	  h = htab->root.hplt;
   9727 	  h->root.u.def.value = isa_bit;
   9728 	  h->other = other;
   9729 	  h->type = STT_FUNC;
   9730 	}
   9731     }
   9732 
   9733   /* Allocate space for global sym dynamic relocs.  */
   9734   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
   9735 
   9736   mips_elf_estimate_stub_size (output_bfd, info);
   9737 
   9738   if (!mips_elf_lay_out_got (output_bfd, info))
   9739     return FALSE;
   9740 
   9741   mips_elf_lay_out_lazy_stubs (info);
   9742 
   9743   /* The check_relocs and adjust_dynamic_symbol entry points have
   9744      determined the sizes of the various dynamic sections.  Allocate
   9745      memory for them.  */
   9746   reltext = FALSE;
   9747   for (s = dynobj->sections; s != NULL; s = s->next)
   9748     {
   9749       const char *name;
   9750 
   9751       /* It's OK to base decisions on the section name, because none
   9752 	 of the dynobj section names depend upon the input files.  */
   9753       name = bfd_get_section_name (dynobj, s);
   9754 
   9755       if ((s->flags & SEC_LINKER_CREATED) == 0)
   9756 	continue;
   9757 
   9758       if (CONST_STRNEQ (name, ".rel"))
   9759 	{
   9760 	  if (s->size != 0)
   9761 	    {
   9762 	      const char *outname;
   9763 	      asection *target;
   9764 
   9765 	      /* If this relocation section applies to a read only
   9766                  section, then we probably need a DT_TEXTREL entry.
   9767                  If the relocation section is .rel(a).dyn, we always
   9768                  assert a DT_TEXTREL entry rather than testing whether
   9769                  there exists a relocation to a read only section or
   9770                  not.  */
   9771 	      outname = bfd_get_section_name (output_bfd,
   9772 					      s->output_section);
   9773 	      target = bfd_get_section_by_name (output_bfd, outname + 4);
   9774 	      if ((target != NULL
   9775 		   && (target->flags & SEC_READONLY) != 0
   9776 		   && (target->flags & SEC_ALLOC) != 0)
   9777 		  || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
   9778 		reltext = TRUE;
   9779 
   9780 	      /* We use the reloc_count field as a counter if we need
   9781 		 to copy relocs into the output file.  */
   9782 	      if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
   9783 		s->reloc_count = 0;
   9784 
   9785 	      /* If combreloc is enabled, elf_link_sort_relocs() will
   9786 		 sort relocations, but in a different way than we do,
   9787 		 and before we're done creating relocations.  Also, it
   9788 		 will move them around between input sections'
   9789 		 relocation's contents, so our sorting would be
   9790 		 broken, so don't let it run.  */
   9791 	      info->combreloc = 0;
   9792 	    }
   9793 	}
   9794       else if (bfd_link_executable (info)
   9795 	       && ! mips_elf_hash_table (info)->use_rld_obj_head
   9796 	       && CONST_STRNEQ (name, ".rld_map"))
   9797 	{
   9798 	  /* We add a room for __rld_map.  It will be filled in by the
   9799 	     rtld to contain a pointer to the _r_debug structure.  */
   9800 	  s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
   9801 	}
   9802       else if (SGI_COMPAT (output_bfd)
   9803 	       && CONST_STRNEQ (name, ".compact_rel"))
   9804 	s->size += mips_elf_hash_table (info)->compact_rel_size;
   9805       else if (s == htab->splt)
   9806 	{
   9807 	  /* If the last PLT entry has a branch delay slot, allocate
   9808 	     room for an extra nop to fill the delay slot.  This is
   9809 	     for CPUs without load interlocking.  */
   9810 	  if (! LOAD_INTERLOCKS_P (output_bfd)
   9811 	      && ! htab->is_vxworks && s->size > 0)
   9812 	    s->size += 4;
   9813 	}
   9814       else if (! CONST_STRNEQ (name, ".init")
   9815 	       && s != htab->sgot
   9816 	       && s != htab->sgotplt
   9817 	       && s != htab->sstubs
   9818 	       && s != htab->sdynbss)
   9819 	{
   9820 	  /* It's not one of our sections, so don't allocate space.  */
   9821 	  continue;
   9822 	}
   9823 
   9824       if (s->size == 0)
   9825 	{
   9826 	  s->flags |= SEC_EXCLUDE;
   9827 	  continue;
   9828 	}
   9829 
   9830       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   9831 	continue;
   9832 
   9833       /* Allocate memory for the section contents.  */
   9834       s->contents = bfd_zalloc (dynobj, s->size);
   9835       if (s->contents == NULL)
   9836 	{
   9837 	  bfd_set_error (bfd_error_no_memory);
   9838 	  return FALSE;
   9839 	}
   9840     }
   9841 
   9842   if (elf_hash_table (info)->dynamic_sections_created)
   9843     {
   9844       /* Add some entries to the .dynamic section.  We fill in the
   9845 	 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
   9846 	 must add the entries now so that we get the correct size for
   9847 	 the .dynamic section.  */
   9848 
   9849       /* SGI object has the equivalence of DT_DEBUG in the
   9850 	 DT_MIPS_RLD_MAP entry.  This must come first because glibc
   9851 	 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
   9852 	 may only look at the first one they see.  */
   9853       if (!bfd_link_pic (info)
   9854 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
   9855 	return FALSE;
   9856 
   9857       if (bfd_link_executable (info)
   9858 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
   9859 	return FALSE;
   9860 
   9861       /* The DT_DEBUG entry may be filled in by the dynamic linker and
   9862 	 used by the debugger.  */
   9863       if (bfd_link_executable (info)
   9864 	  && !SGI_COMPAT (output_bfd)
   9865 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
   9866 	return FALSE;
   9867 
   9868       if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
   9869 	info->flags |= DF_TEXTREL;
   9870 
   9871       if ((info->flags & DF_TEXTREL) != 0)
   9872 	{
   9873 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
   9874 	    return FALSE;
   9875 
   9876 	  /* Clear the DF_TEXTREL flag.  It will be set again if we
   9877 	     write out an actual text relocation; we may not, because
   9878 	     at this point we do not know whether e.g. any .eh_frame
   9879 	     absolute relocations have been converted to PC-relative.  */
   9880 	  info->flags &= ~DF_TEXTREL;
   9881 	}
   9882 
   9883       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
   9884 	return FALSE;
   9885 
   9886       sreldyn = mips_elf_rel_dyn_section (info, FALSE);
   9887       if (htab->is_vxworks)
   9888 	{
   9889 	  /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
   9890 	     use any of the DT_MIPS_* tags.  */
   9891 	  if (sreldyn && sreldyn->size > 0)
   9892 	    {
   9893 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
   9894 		return FALSE;
   9895 
   9896 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
   9897 		return FALSE;
   9898 
   9899 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
   9900 		return FALSE;
   9901 	    }
   9902 	}
   9903       else
   9904 	{
   9905 	  if (sreldyn && sreldyn->size > 0)
   9906 	    {
   9907 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
   9908 		return FALSE;
   9909 
   9910 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
   9911 		return FALSE;
   9912 
   9913 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
   9914 		return FALSE;
   9915 	    }
   9916 
   9917 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
   9918 	    return FALSE;
   9919 
   9920 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
   9921 	    return FALSE;
   9922 
   9923 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
   9924 	    return FALSE;
   9925 
   9926 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
   9927 	    return FALSE;
   9928 
   9929 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
   9930 	    return FALSE;
   9931 
   9932 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
   9933 	    return FALSE;
   9934 
   9935 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
   9936 	    return FALSE;
   9937 
   9938 	  if (IRIX_COMPAT (dynobj) == ict_irix5
   9939 	      && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
   9940 	    return FALSE;
   9941 
   9942 	  if (IRIX_COMPAT (dynobj) == ict_irix6
   9943 	      && (bfd_get_section_by_name
   9944 		  (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
   9945 	      && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
   9946 	    return FALSE;
   9947 	}
   9948       if (htab->splt->size > 0)
   9949 	{
   9950 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
   9951 	    return FALSE;
   9952 
   9953 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
   9954 	    return FALSE;
   9955 
   9956 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
   9957 	    return FALSE;
   9958 
   9959 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
   9960 	    return FALSE;
   9961 	}
   9962       if (htab->is_vxworks
   9963 	  && !elf_vxworks_add_dynamic_entries (output_bfd, info))
   9964 	return FALSE;
   9965     }
   9966 
   9967   return TRUE;
   9968 }
   9969 
   9970 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
   9972    Adjust its R_ADDEND field so that it is correct for the output file.
   9973    LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
   9974    and sections respectively; both use symbol indexes.  */
   9975 
   9976 static void
   9977 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
   9978 			bfd *input_bfd, Elf_Internal_Sym *local_syms,
   9979 			asection **local_sections, Elf_Internal_Rela *rel)
   9980 {
   9981   unsigned int r_type, r_symndx;
   9982   Elf_Internal_Sym *sym;
   9983   asection *sec;
   9984 
   9985   if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
   9986     {
   9987       r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   9988       if (gprel16_reloc_p (r_type)
   9989 	  || r_type == R_MIPS_GPREL32
   9990 	  || literal_reloc_p (r_type))
   9991 	{
   9992 	  rel->r_addend += _bfd_get_gp_value (input_bfd);
   9993 	  rel->r_addend -= _bfd_get_gp_value (output_bfd);
   9994 	}
   9995 
   9996       r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
   9997       sym = local_syms + r_symndx;
   9998 
   9999       /* Adjust REL's addend to account for section merging.  */
   10000       if (!bfd_link_relocatable (info))
   10001 	{
   10002 	  sec = local_sections[r_symndx];
   10003 	  _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   10004 	}
   10005 
   10006       /* This would normally be done by the rela_normal code in elflink.c.  */
   10007       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   10008 	rel->r_addend += local_sections[r_symndx]->output_offset;
   10009     }
   10010 }
   10011 
   10012 /* Handle relocations against symbols from removed linkonce sections,
   10013    or sections discarded by a linker script.  We use this wrapper around
   10014    RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
   10015    on 64-bit ELF targets.  In this case for any relocation handled, which
   10016    always be the first in a triplet, the remaining two have to be processed
   10017    together with the first, even if they are R_MIPS_NONE.  It is the symbol
   10018    index referred by the first reloc that applies to all the three and the
   10019    remaining two never refer to an object symbol.  And it is the final
   10020    relocation (the last non-null one) that determines the output field of
   10021    the whole relocation so retrieve the corresponding howto structure for
   10022    the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
   10023 
   10024    Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
   10025    and therefore requires to be pasted in a loop.  It also defines a block
   10026    and does not protect any of its arguments, hence the extra brackets.  */
   10027 
   10028 static void
   10029 mips_reloc_against_discarded_section (bfd *output_bfd,
   10030 				      struct bfd_link_info *info,
   10031 				      bfd *input_bfd, asection *input_section,
   10032 				      Elf_Internal_Rela **rel,
   10033 				      const Elf_Internal_Rela **relend,
   10034 				      bfd_boolean rel_reloc,
   10035 				      reloc_howto_type *howto,
   10036 				      bfd_byte *contents)
   10037 {
   10038   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   10039   int count = bed->s->int_rels_per_ext_rel;
   10040   unsigned int r_type;
   10041   int i;
   10042 
   10043   for (i = count - 1; i > 0; i--)
   10044     {
   10045       r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
   10046       if (r_type != R_MIPS_NONE)
   10047 	{
   10048 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
   10049 	  break;
   10050 	}
   10051     }
   10052   do
   10053     {
   10054        RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   10055 					(*rel), count, (*relend),
   10056 					howto, i, contents);
   10057     }
   10058   while (0);
   10059 }
   10060 
   10061 /* Relocate a MIPS ELF section.  */
   10062 
   10063 bfd_boolean
   10064 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
   10065 				bfd *input_bfd, asection *input_section,
   10066 				bfd_byte *contents, Elf_Internal_Rela *relocs,
   10067 				Elf_Internal_Sym *local_syms,
   10068 				asection **local_sections)
   10069 {
   10070   Elf_Internal_Rela *rel;
   10071   const Elf_Internal_Rela *relend;
   10072   bfd_vma addend = 0;
   10073   bfd_boolean use_saved_addend_p = FALSE;
   10074   const struct elf_backend_data *bed;
   10075 
   10076   bed = get_elf_backend_data (output_bfd);
   10077   relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
   10078   for (rel = relocs; rel < relend; ++rel)
   10079     {
   10080       const char *name;
   10081       bfd_vma value = 0;
   10082       reloc_howto_type *howto;
   10083       bfd_boolean cross_mode_jump_p = FALSE;
   10084       /* TRUE if the relocation is a RELA relocation, rather than a
   10085          REL relocation.  */
   10086       bfd_boolean rela_relocation_p = TRUE;
   10087       unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   10088       const char *msg;
   10089       unsigned long r_symndx;
   10090       asection *sec;
   10091       Elf_Internal_Shdr *symtab_hdr;
   10092       struct elf_link_hash_entry *h;
   10093       bfd_boolean rel_reloc;
   10094 
   10095       rel_reloc = (NEWABI_P (input_bfd)
   10096 		   && mips_elf_rel_relocation_p (input_bfd, input_section,
   10097 						 relocs, rel));
   10098       /* Find the relocation howto for this relocation.  */
   10099       howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
   10100 
   10101       r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
   10102       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   10103       if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
   10104 	{
   10105 	  sec = local_sections[r_symndx];
   10106 	  h = NULL;
   10107 	}
   10108       else
   10109 	{
   10110 	  unsigned long extsymoff;
   10111 
   10112 	  extsymoff = 0;
   10113 	  if (!elf_bad_symtab (input_bfd))
   10114 	    extsymoff = symtab_hdr->sh_info;
   10115 	  h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
   10116 	  while (h->root.type == bfd_link_hash_indirect
   10117 		 || h->root.type == bfd_link_hash_warning)
   10118 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10119 
   10120 	  sec = NULL;
   10121 	  if (h->root.type == bfd_link_hash_defined
   10122 	      || h->root.type == bfd_link_hash_defweak)
   10123 	    sec = h->root.u.def.section;
   10124 	}
   10125 
   10126       if (sec != NULL && discarded_section (sec))
   10127 	{
   10128 	  mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
   10129 						input_section, &rel, &relend,
   10130 						rel_reloc, howto, contents);
   10131 	  continue;
   10132 	}
   10133 
   10134       if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
   10135 	{
   10136 	  /* Some 32-bit code uses R_MIPS_64.  In particular, people use
   10137 	     64-bit code, but make sure all their addresses are in the
   10138 	     lowermost or uppermost 32-bit section of the 64-bit address
   10139 	     space.  Thus, when they use an R_MIPS_64 they mean what is
   10140 	     usually meant by R_MIPS_32, with the exception that the
   10141 	     stored value is sign-extended to 64 bits.  */
   10142 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
   10143 
   10144 	  /* On big-endian systems, we need to lie about the position
   10145 	     of the reloc.  */
   10146 	  if (bfd_big_endian (input_bfd))
   10147 	    rel->r_offset += 4;
   10148 	}
   10149 
   10150       if (!use_saved_addend_p)
   10151 	{
   10152 	  /* If these relocations were originally of the REL variety,
   10153 	     we must pull the addend out of the field that will be
   10154 	     relocated.  Otherwise, we simply use the contents of the
   10155 	     RELA relocation.  */
   10156 	  if (mips_elf_rel_relocation_p (input_bfd, input_section,
   10157 					 relocs, rel))
   10158 	    {
   10159 	      rela_relocation_p = FALSE;
   10160 	      addend = mips_elf_read_rel_addend (input_bfd, rel,
   10161 						 howto, contents);
   10162 	      if (hi16_reloc_p (r_type)
   10163 		  || (got16_reloc_p (r_type)
   10164 		      && mips_elf_local_relocation_p (input_bfd, rel,
   10165 						      local_sections)))
   10166 		{
   10167 		  if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
   10168 						     contents, &addend))
   10169 		    {
   10170 		      if (h)
   10171 			name = h->root.root.string;
   10172 		      else
   10173 			name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   10174 						 local_syms + r_symndx,
   10175 						 sec);
   10176 		      (*_bfd_error_handler)
   10177 			(_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
   10178 			 input_bfd, input_section, name, howto->name,
   10179 			 rel->r_offset);
   10180 		    }
   10181 		}
   10182 	      else
   10183 		addend <<= howto->rightshift;
   10184 	    }
   10185 	  else
   10186 	    addend = rel->r_addend;
   10187 	  mips_elf_adjust_addend (output_bfd, info, input_bfd,
   10188 				  local_syms, local_sections, rel);
   10189 	}
   10190 
   10191       if (bfd_link_relocatable (info))
   10192 	{
   10193 	  if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
   10194 	      && bfd_big_endian (input_bfd))
   10195 	    rel->r_offset -= 4;
   10196 
   10197 	  if (!rela_relocation_p && rel->r_addend)
   10198 	    {
   10199 	      addend += rel->r_addend;
   10200 	      if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
   10201 		addend = mips_elf_high (addend);
   10202 	      else if (r_type == R_MIPS_HIGHER)
   10203 		addend = mips_elf_higher (addend);
   10204 	      else if (r_type == R_MIPS_HIGHEST)
   10205 		addend = mips_elf_highest (addend);
   10206 	      else
   10207 		addend >>= howto->rightshift;
   10208 
   10209 	      /* We use the source mask, rather than the destination
   10210 		 mask because the place to which we are writing will be
   10211 		 source of the addend in the final link.  */
   10212 	      addend &= howto->src_mask;
   10213 
   10214 	      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
   10215 		/* See the comment above about using R_MIPS_64 in the 32-bit
   10216 		   ABI.  Here, we need to update the addend.  It would be
   10217 		   possible to get away with just using the R_MIPS_32 reloc
   10218 		   but for endianness.  */
   10219 		{
   10220 		  bfd_vma sign_bits;
   10221 		  bfd_vma low_bits;
   10222 		  bfd_vma high_bits;
   10223 
   10224 		  if (addend & ((bfd_vma) 1 << 31))
   10225 #ifdef BFD64
   10226 		    sign_bits = ((bfd_vma) 1 << 32) - 1;
   10227 #else
   10228 		    sign_bits = -1;
   10229 #endif
   10230 		  else
   10231 		    sign_bits = 0;
   10232 
   10233 		  /* If we don't know that we have a 64-bit type,
   10234 		     do two separate stores.  */
   10235 		  if (bfd_big_endian (input_bfd))
   10236 		    {
   10237 		      /* Store the sign-bits (which are most significant)
   10238 			 first.  */
   10239 		      low_bits = sign_bits;
   10240 		      high_bits = addend;
   10241 		    }
   10242 		  else
   10243 		    {
   10244 		      low_bits = addend;
   10245 		      high_bits = sign_bits;
   10246 		    }
   10247 		  bfd_put_32 (input_bfd, low_bits,
   10248 			      contents + rel->r_offset);
   10249 		  bfd_put_32 (input_bfd, high_bits,
   10250 			      contents + rel->r_offset + 4);
   10251 		  continue;
   10252 		}
   10253 
   10254 	      if (! mips_elf_perform_relocation (info, howto, rel, addend,
   10255 						 input_bfd, input_section,
   10256 						 contents, FALSE))
   10257 		return FALSE;
   10258 	    }
   10259 
   10260 	  /* Go on to the next relocation.  */
   10261 	  continue;
   10262 	}
   10263 
   10264       /* In the N32 and 64-bit ABIs there may be multiple consecutive
   10265 	 relocations for the same offset.  In that case we are
   10266 	 supposed to treat the output of each relocation as the addend
   10267 	 for the next.  */
   10268       if (rel + 1 < relend
   10269 	  && rel->r_offset == rel[1].r_offset
   10270 	  && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
   10271 	use_saved_addend_p = TRUE;
   10272       else
   10273 	use_saved_addend_p = FALSE;
   10274 
   10275       /* Figure out what value we are supposed to relocate.  */
   10276       switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
   10277 					     input_section, info, rel,
   10278 					     addend, howto, local_syms,
   10279 					     local_sections, &value,
   10280 					     &name, &cross_mode_jump_p,
   10281 					     use_saved_addend_p))
   10282 	{
   10283 	case bfd_reloc_continue:
   10284 	  /* There's nothing to do.  */
   10285 	  continue;
   10286 
   10287 	case bfd_reloc_undefined:
   10288 	  /* mips_elf_calculate_relocation already called the
   10289 	     undefined_symbol callback.  There's no real point in
   10290 	     trying to perform the relocation at this point, so we
   10291 	     just skip ahead to the next relocation.  */
   10292 	  continue;
   10293 
   10294 	case bfd_reloc_notsupported:
   10295 	  msg = _("internal error: unsupported relocation error");
   10296 	  info->callbacks->warning
   10297 	    (info, msg, name, input_bfd, input_section, rel->r_offset);
   10298 	  return FALSE;
   10299 
   10300 	case bfd_reloc_overflow:
   10301 	  if (use_saved_addend_p)
   10302 	    /* Ignore overflow until we reach the last relocation for
   10303 	       a given location.  */
   10304 	    ;
   10305 	  else
   10306 	    {
   10307 	      struct mips_elf_link_hash_table *htab;
   10308 
   10309 	      htab = mips_elf_hash_table (info);
   10310 	      BFD_ASSERT (htab != NULL);
   10311 	      BFD_ASSERT (name != NULL);
   10312 	      if (!htab->small_data_overflow_reported
   10313 		  && (gprel16_reloc_p (howto->type)
   10314 		      || literal_reloc_p (howto->type)))
   10315 		{
   10316 		  msg = _("small-data section exceeds 64KB;"
   10317 			  " lower small-data size limit (see option -G)");
   10318 
   10319 		  htab->small_data_overflow_reported = TRUE;
   10320 		  (*info->callbacks->einfo) ("%P: %s\n", msg);
   10321 		}
   10322 	      (*info->callbacks->reloc_overflow)
   10323 		(info, NULL, name, howto->name, (bfd_vma) 0,
   10324 		 input_bfd, input_section, rel->r_offset);
   10325 	    }
   10326 	  break;
   10327 
   10328 	case bfd_reloc_ok:
   10329 	  break;
   10330 
   10331 	case bfd_reloc_outofrange:
   10332 	  msg = NULL;
   10333 	  if (jal_reloc_p (howto->type))
   10334 	    msg = _("JALX to a non-word-aligned address");
   10335 	  else if (b_reloc_p (howto->type))
   10336 	    msg = _("Branch to a non-instruction-aligned address");
   10337 	  else if (aligned_pcrel_reloc_p (howto->type))
   10338 	    msg = _("PC-relative load from unaligned address");
   10339 	  if (msg)
   10340 	    {
   10341 	      info->callbacks->einfo
   10342 		("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
   10343 	      break;
   10344 	    }
   10345 	  /* Fall through.  */
   10346 
   10347 	default:
   10348 	  abort ();
   10349 	  break;
   10350 	}
   10351 
   10352       /* If we've got another relocation for the address, keep going
   10353 	 until we reach the last one.  */
   10354       if (use_saved_addend_p)
   10355 	{
   10356 	  addend = value;
   10357 	  continue;
   10358 	}
   10359 
   10360       if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
   10361 	/* See the comment above about using R_MIPS_64 in the 32-bit
   10362 	   ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
   10363 	   that calculated the right value.  Now, however, we
   10364 	   sign-extend the 32-bit result to 64-bits, and store it as a
   10365 	   64-bit value.  We are especially generous here in that we
   10366 	   go to extreme lengths to support this usage on systems with
   10367 	   only a 32-bit VMA.  */
   10368 	{
   10369 	  bfd_vma sign_bits;
   10370 	  bfd_vma low_bits;
   10371 	  bfd_vma high_bits;
   10372 
   10373 	  if (value & ((bfd_vma) 1 << 31))
   10374 #ifdef BFD64
   10375 	    sign_bits = ((bfd_vma) 1 << 32) - 1;
   10376 #else
   10377 	    sign_bits = -1;
   10378 #endif
   10379 	  else
   10380 	    sign_bits = 0;
   10381 
   10382 	  /* If we don't know that we have a 64-bit type,
   10383 	     do two separate stores.  */
   10384 	  if (bfd_big_endian (input_bfd))
   10385 	    {
   10386 	      /* Undo what we did above.  */
   10387 	      rel->r_offset -= 4;
   10388 	      /* Store the sign-bits (which are most significant)
   10389 		 first.  */
   10390 	      low_bits = sign_bits;
   10391 	      high_bits = value;
   10392 	    }
   10393 	  else
   10394 	    {
   10395 	      low_bits = value;
   10396 	      high_bits = sign_bits;
   10397 	    }
   10398 	  bfd_put_32 (input_bfd, low_bits,
   10399 		      contents + rel->r_offset);
   10400 	  bfd_put_32 (input_bfd, high_bits,
   10401 		      contents + rel->r_offset + 4);
   10402 	  continue;
   10403 	}
   10404 
   10405       /* Actually perform the relocation.  */
   10406       if (! mips_elf_perform_relocation (info, howto, rel, value,
   10407 					 input_bfd, input_section,
   10408 					 contents, cross_mode_jump_p))
   10409 	return FALSE;
   10410     }
   10411 
   10412   return TRUE;
   10413 }
   10414 
   10415 /* A function that iterates over each entry in la25_stubs and fills
   10417    in the code for each one.  DATA points to a mips_htab_traverse_info.  */
   10418 
   10419 static int
   10420 mips_elf_create_la25_stub (void **slot, void *data)
   10421 {
   10422   struct mips_htab_traverse_info *hti;
   10423   struct mips_elf_link_hash_table *htab;
   10424   struct mips_elf_la25_stub *stub;
   10425   asection *s;
   10426   bfd_byte *loc;
   10427   bfd_vma offset, target, target_high, target_low;
   10428 
   10429   stub = (struct mips_elf_la25_stub *) *slot;
   10430   hti = (struct mips_htab_traverse_info *) data;
   10431   htab = mips_elf_hash_table (hti->info);
   10432   BFD_ASSERT (htab != NULL);
   10433 
   10434   /* Create the section contents, if we haven't already.  */
   10435   s = stub->stub_section;
   10436   loc = s->contents;
   10437   if (loc == NULL)
   10438     {
   10439       loc = bfd_malloc (s->size);
   10440       if (loc == NULL)
   10441 	{
   10442 	  hti->error = TRUE;
   10443 	  return FALSE;
   10444 	}
   10445       s->contents = loc;
   10446     }
   10447 
   10448   /* Work out where in the section this stub should go.  */
   10449   offset = stub->offset;
   10450 
   10451   /* Work out the target address.  */
   10452   target = mips_elf_get_la25_target (stub, &s);
   10453   target += s->output_section->vma + s->output_offset;
   10454 
   10455   target_high = ((target + 0x8000) >> 16) & 0xffff;
   10456   target_low = (target & 0xffff);
   10457 
   10458   if (stub->stub_section != htab->strampoline)
   10459     {
   10460       /* This is a simple LUI/ADDIU stub.  Zero out the beginning
   10461 	 of the section and write the two instructions at the end.  */
   10462       memset (loc, 0, offset);
   10463       loc += offset;
   10464       if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
   10465 	{
   10466 	  bfd_put_micromips_32 (hti->output_bfd,
   10467 				LA25_LUI_MICROMIPS (target_high),
   10468 				loc);
   10469 	  bfd_put_micromips_32 (hti->output_bfd,
   10470 				LA25_ADDIU_MICROMIPS (target_low),
   10471 				loc + 4);
   10472 	}
   10473       else
   10474 	{
   10475 	  bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
   10476 	  bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
   10477 	}
   10478     }
   10479   else
   10480     {
   10481       /* This is trampoline.  */
   10482       loc += offset;
   10483       if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
   10484 	{
   10485 	  bfd_put_micromips_32 (hti->output_bfd,
   10486 				LA25_LUI_MICROMIPS (target_high), loc);
   10487 	  bfd_put_micromips_32 (hti->output_bfd,
   10488 				LA25_J_MICROMIPS (target), loc + 4);
   10489 	  bfd_put_micromips_32 (hti->output_bfd,
   10490 				LA25_ADDIU_MICROMIPS (target_low), loc + 8);
   10491 	  bfd_put_32 (hti->output_bfd, 0, loc + 12);
   10492 	}
   10493       else
   10494 	{
   10495 	  bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
   10496 	  bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
   10497 	  bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
   10498 	  bfd_put_32 (hti->output_bfd, 0, loc + 12);
   10499 	}
   10500     }
   10501   return TRUE;
   10502 }
   10503 
   10504 /* If NAME is one of the special IRIX6 symbols defined by the linker,
   10505    adjust it appropriately now.  */
   10506 
   10507 static void
   10508 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
   10509 				      const char *name, Elf_Internal_Sym *sym)
   10510 {
   10511   /* The linker script takes care of providing names and values for
   10512      these, but we must place them into the right sections.  */
   10513   static const char* const text_section_symbols[] = {
   10514     "_ftext",
   10515     "_etext",
   10516     "__dso_displacement",
   10517     "__elf_header",
   10518     "__program_header_table",
   10519     NULL
   10520   };
   10521 
   10522   static const char* const data_section_symbols[] = {
   10523     "_fdata",
   10524     "_edata",
   10525     "_end",
   10526     "_fbss",
   10527     NULL
   10528   };
   10529 
   10530   const char* const *p;
   10531   int i;
   10532 
   10533   for (i = 0; i < 2; ++i)
   10534     for (p = (i == 0) ? text_section_symbols : data_section_symbols;
   10535 	 *p;
   10536 	 ++p)
   10537       if (strcmp (*p, name) == 0)
   10538 	{
   10539 	  /* All of these symbols are given type STT_SECTION by the
   10540 	     IRIX6 linker.  */
   10541 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   10542 	  sym->st_other = STO_PROTECTED;
   10543 
   10544 	  /* The IRIX linker puts these symbols in special sections.  */
   10545 	  if (i == 0)
   10546 	    sym->st_shndx = SHN_MIPS_TEXT;
   10547 	  else
   10548 	    sym->st_shndx = SHN_MIPS_DATA;
   10549 
   10550 	  break;
   10551 	}
   10552 }
   10553 
   10554 /* Finish up dynamic symbol handling.  We set the contents of various
   10555    dynamic sections here.  */
   10556 
   10557 bfd_boolean
   10558 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
   10559 				     struct bfd_link_info *info,
   10560 				     struct elf_link_hash_entry *h,
   10561 				     Elf_Internal_Sym *sym)
   10562 {
   10563   bfd *dynobj;
   10564   asection *sgot;
   10565   struct mips_got_info *g, *gg;
   10566   const char *name;
   10567   int idx;
   10568   struct mips_elf_link_hash_table *htab;
   10569   struct mips_elf_link_hash_entry *hmips;
   10570 
   10571   htab = mips_elf_hash_table (info);
   10572   BFD_ASSERT (htab != NULL);
   10573   dynobj = elf_hash_table (info)->dynobj;
   10574   hmips = (struct mips_elf_link_hash_entry *) h;
   10575 
   10576   BFD_ASSERT (!htab->is_vxworks);
   10577 
   10578   if (h->plt.plist != NULL
   10579       && (h->plt.plist->mips_offset != MINUS_ONE
   10580 	  || h->plt.plist->comp_offset != MINUS_ONE))
   10581     {
   10582       /* We've decided to create a PLT entry for this symbol.  */
   10583       bfd_byte *loc;
   10584       bfd_vma header_address, got_address;
   10585       bfd_vma got_address_high, got_address_low, load;
   10586       bfd_vma got_index;
   10587       bfd_vma isa_bit;
   10588 
   10589       got_index = h->plt.plist->gotplt_index;
   10590 
   10591       BFD_ASSERT (htab->use_plts_and_copy_relocs);
   10592       BFD_ASSERT (h->dynindx != -1);
   10593       BFD_ASSERT (htab->splt != NULL);
   10594       BFD_ASSERT (got_index != MINUS_ONE);
   10595       BFD_ASSERT (!h->def_regular);
   10596 
   10597       /* Calculate the address of the PLT header.  */
   10598       isa_bit = htab->plt_header_is_comp;
   10599       header_address = (htab->splt->output_section->vma
   10600 			+ htab->splt->output_offset + isa_bit);
   10601 
   10602       /* Calculate the address of the .got.plt entry.  */
   10603       got_address = (htab->sgotplt->output_section->vma
   10604 		     + htab->sgotplt->output_offset
   10605 		     + got_index * MIPS_ELF_GOT_SIZE (dynobj));
   10606 
   10607       got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
   10608       got_address_low = got_address & 0xffff;
   10609 
   10610       /* Initially point the .got.plt entry at the PLT header.  */
   10611       loc = (htab->sgotplt->contents + got_index * MIPS_ELF_GOT_SIZE (dynobj));
   10612       if (ABI_64_P (output_bfd))
   10613 	bfd_put_64 (output_bfd, header_address, loc);
   10614       else
   10615 	bfd_put_32 (output_bfd, header_address, loc);
   10616 
   10617       /* Now handle the PLT itself.  First the standard entry (the order
   10618          does not matter, we just have to pick one).  */
   10619       if (h->plt.plist->mips_offset != MINUS_ONE)
   10620 	{
   10621 	  const bfd_vma *plt_entry;
   10622 	  bfd_vma plt_offset;
   10623 
   10624 	  plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
   10625 
   10626 	  BFD_ASSERT (plt_offset <= htab->splt->size);
   10627 
   10628 	  /* Find out where the .plt entry should go.  */
   10629 	  loc = htab->splt->contents + plt_offset;
   10630 
   10631 	  /* Pick the load opcode.  */
   10632 	  load = MIPS_ELF_LOAD_WORD (output_bfd);
   10633 
   10634 	  /* Fill in the PLT entry itself.  */
   10635 
   10636 	  if (MIPSR6_P (output_bfd))
   10637 	    plt_entry = mipsr6_exec_plt_entry;
   10638 	  else
   10639 	    plt_entry = mips_exec_plt_entry;
   10640 	  bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
   10641 	  bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
   10642 		      loc + 4);
   10643 
   10644 	  if (! LOAD_INTERLOCKS_P (output_bfd))
   10645 	    {
   10646 	      bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
   10647 	      bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   10648 	    }
   10649 	  else
   10650 	    {
   10651 	      bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
   10652 	      bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
   10653 			  loc + 12);
   10654 	    }
   10655 	}
   10656 
   10657       /* Now the compressed entry.  They come after any standard ones.  */
   10658       if (h->plt.plist->comp_offset != MINUS_ONE)
   10659 	{
   10660 	  bfd_vma plt_offset;
   10661 
   10662 	  plt_offset = (htab->plt_header_size + htab->plt_mips_offset
   10663 			+ h->plt.plist->comp_offset);
   10664 
   10665 	  BFD_ASSERT (plt_offset <= htab->splt->size);
   10666 
   10667 	  /* Find out where the .plt entry should go.  */
   10668 	  loc = htab->splt->contents + plt_offset;
   10669 
   10670 	  /* Fill in the PLT entry itself.  */
   10671 	  if (!MICROMIPS_P (output_bfd))
   10672 	    {
   10673 	      const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
   10674 
   10675 	      bfd_put_16 (output_bfd, plt_entry[0], loc);
   10676 	      bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
   10677 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   10678 	      bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
   10679 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   10680 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   10681 	      bfd_put_32 (output_bfd, got_address, loc + 12);
   10682 	    }
   10683 	  else if (htab->insn32)
   10684 	    {
   10685 	      const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
   10686 
   10687 	      bfd_put_16 (output_bfd, plt_entry[0], loc);
   10688 	      bfd_put_16 (output_bfd, got_address_high, loc + 2);
   10689 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   10690 	      bfd_put_16 (output_bfd, got_address_low, loc + 6);
   10691 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   10692 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   10693 	      bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
   10694 	      bfd_put_16 (output_bfd, got_address_low, loc + 14);
   10695 	    }
   10696 	  else
   10697 	    {
   10698 	      const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
   10699 	      bfd_signed_vma gotpc_offset;
   10700 	      bfd_vma loc_address;
   10701 
   10702 	      BFD_ASSERT (got_address % 4 == 0);
   10703 
   10704 	      loc_address = (htab->splt->output_section->vma
   10705 			     + htab->splt->output_offset + plt_offset);
   10706 	      gotpc_offset = got_address - ((loc_address | 3) ^ 3);
   10707 
   10708 	      /* ADDIUPC has a span of +/-16MB, check we're in range.  */
   10709 	      if (gotpc_offset + 0x1000000 >= 0x2000000)
   10710 		{
   10711 		  (*_bfd_error_handler)
   10712 		    (_("%B: `%A' offset of %ld from `%A' "
   10713 		       "beyond the range of ADDIUPC"),
   10714 		     output_bfd,
   10715 		     htab->sgotplt->output_section,
   10716 		     htab->splt->output_section,
   10717 		     (long) gotpc_offset);
   10718 		  bfd_set_error (bfd_error_no_error);
   10719 		  return FALSE;
   10720 		}
   10721 	      bfd_put_16 (output_bfd,
   10722 			  plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
   10723 	      bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
   10724 	      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   10725 	      bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
   10726 	      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   10727 	      bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
   10728 	    }
   10729 	}
   10730 
   10731       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
   10732       mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
   10733 					  got_index - 2, h->dynindx,
   10734 					  R_MIPS_JUMP_SLOT, got_address);
   10735 
   10736       /* We distinguish between PLT entries and lazy-binding stubs by
   10737 	 giving the former an st_other value of STO_MIPS_PLT.  Set the
   10738 	 flag and leave the value if there are any relocations in the
   10739 	 binary where pointer equality matters.  */
   10740       sym->st_shndx = SHN_UNDEF;
   10741       if (h->pointer_equality_needed)
   10742 	sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
   10743       else
   10744 	{
   10745 	  sym->st_value = 0;
   10746 	  sym->st_other = 0;
   10747 	}
   10748     }
   10749 
   10750   if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
   10751     {
   10752       /* We've decided to create a lazy-binding stub.  */
   10753       bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
   10754       unsigned int other = micromips_p ? STO_MICROMIPS : 0;
   10755       bfd_vma stub_size = htab->function_stub_size;
   10756       bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
   10757       bfd_vma isa_bit = micromips_p;
   10758       bfd_vma stub_big_size;
   10759 
   10760       if (!micromips_p)
   10761 	stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
   10762       else if (htab->insn32)
   10763 	stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
   10764       else
   10765 	stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
   10766 
   10767       /* This symbol has a stub.  Set it up.  */
   10768 
   10769       BFD_ASSERT (h->dynindx != -1);
   10770 
   10771       BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
   10772 
   10773       /* Values up to 2^31 - 1 are allowed.  Larger values would cause
   10774 	 sign extension at runtime in the stub, resulting in a negative
   10775 	 index value.  */
   10776       if (h->dynindx & ~0x7fffffff)
   10777 	return FALSE;
   10778 
   10779       /* Fill the stub.  */
   10780       if (micromips_p)
   10781 	{
   10782 	  idx = 0;
   10783 	  bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
   10784 				stub + idx);
   10785 	  idx += 4;
   10786 	  if (htab->insn32)
   10787 	    {
   10788 	      bfd_put_micromips_32 (output_bfd,
   10789 				    STUB_MOVE32_MICROMIPS, stub + idx);
   10790 	      idx += 4;
   10791 	    }
   10792 	  else
   10793 	    {
   10794 	      bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
   10795 	      idx += 2;
   10796 	    }
   10797 	  if (stub_size == stub_big_size)
   10798 	    {
   10799 	      long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
   10800 
   10801 	      bfd_put_micromips_32 (output_bfd,
   10802 				    STUB_LUI_MICROMIPS (dynindx_hi),
   10803 				    stub + idx);
   10804 	      idx += 4;
   10805 	    }
   10806 	  if (htab->insn32)
   10807 	    {
   10808 	      bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
   10809 				    stub + idx);
   10810 	      idx += 4;
   10811 	    }
   10812 	  else
   10813 	    {
   10814 	      bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
   10815 	      idx += 2;
   10816 	    }
   10817 
   10818 	  /* If a large stub is not required and sign extension is not a
   10819 	     problem, then use legacy code in the stub.  */
   10820 	  if (stub_size == stub_big_size)
   10821 	    bfd_put_micromips_32 (output_bfd,
   10822 				  STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
   10823 				  stub + idx);
   10824 	  else if (h->dynindx & ~0x7fff)
   10825 	    bfd_put_micromips_32 (output_bfd,
   10826 				  STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
   10827 				  stub + idx);
   10828 	  else
   10829 	    bfd_put_micromips_32 (output_bfd,
   10830 				  STUB_LI16S_MICROMIPS (output_bfd,
   10831 							h->dynindx),
   10832 				  stub + idx);
   10833 	}
   10834       else
   10835 	{
   10836 	  idx = 0;
   10837 	  bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
   10838 	  idx += 4;
   10839 	  bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
   10840 	  idx += 4;
   10841 	  if (stub_size == stub_big_size)
   10842 	    {
   10843 	      bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
   10844 			  stub + idx);
   10845 	      idx += 4;
   10846 	    }
   10847 	  bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
   10848 	  idx += 4;
   10849 
   10850 	  /* If a large stub is not required and sign extension is not a
   10851 	     problem, then use legacy code in the stub.  */
   10852 	  if (stub_size == stub_big_size)
   10853 	    bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
   10854 			stub + idx);
   10855 	  else if (h->dynindx & ~0x7fff)
   10856 	    bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
   10857 			stub + idx);
   10858 	  else
   10859 	    bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
   10860 			stub + idx);
   10861 	}
   10862 
   10863       BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
   10864       memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
   10865 	      stub, stub_size);
   10866 
   10867       /* Mark the symbol as undefined.  stub_offset != -1 occurs
   10868 	 only for the referenced symbol.  */
   10869       sym->st_shndx = SHN_UNDEF;
   10870 
   10871       /* The run-time linker uses the st_value field of the symbol
   10872 	 to reset the global offset table entry for this external
   10873 	 to its stub address when unlinking a shared object.  */
   10874       sym->st_value = (htab->sstubs->output_section->vma
   10875 		       + htab->sstubs->output_offset
   10876 		       + h->plt.plist->stub_offset
   10877 		       + isa_bit);
   10878       sym->st_other = other;
   10879     }
   10880 
   10881   /* If we have a MIPS16 function with a stub, the dynamic symbol must
   10882      refer to the stub, since only the stub uses the standard calling
   10883      conventions.  */
   10884   if (h->dynindx != -1 && hmips->fn_stub != NULL)
   10885     {
   10886       BFD_ASSERT (hmips->need_fn_stub);
   10887       sym->st_value = (hmips->fn_stub->output_section->vma
   10888 		       + hmips->fn_stub->output_offset);
   10889       sym->st_size = hmips->fn_stub->size;
   10890       sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
   10891     }
   10892 
   10893   BFD_ASSERT (h->dynindx != -1
   10894 	      || h->forced_local);
   10895 
   10896   sgot = htab->sgot;
   10897   g = htab->got_info;
   10898   BFD_ASSERT (g != NULL);
   10899 
   10900   /* Run through the global symbol table, creating GOT entries for all
   10901      the symbols that need them.  */
   10902   if (hmips->global_got_area != GGA_NONE)
   10903     {
   10904       bfd_vma offset;
   10905       bfd_vma value;
   10906 
   10907       value = sym->st_value;
   10908       offset = mips_elf_primary_global_got_index (output_bfd, info, h);
   10909       MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
   10910     }
   10911 
   10912   if (hmips->global_got_area != GGA_NONE && g->next)
   10913     {
   10914       struct mips_got_entry e, *p;
   10915       bfd_vma entry;
   10916       bfd_vma offset;
   10917 
   10918       gg = g;
   10919 
   10920       e.abfd = output_bfd;
   10921       e.symndx = -1;
   10922       e.d.h = hmips;
   10923       e.tls_type = GOT_TLS_NONE;
   10924 
   10925       for (g = g->next; g->next != gg; g = g->next)
   10926 	{
   10927 	  if (g->got_entries
   10928 	      && (p = (struct mips_got_entry *) htab_find (g->got_entries,
   10929 							   &e)))
   10930 	    {
   10931 	      offset = p->gotidx;
   10932 	      BFD_ASSERT (offset > 0 && offset < htab->sgot->size);
   10933 	      if (bfd_link_pic (info)
   10934 		  || (elf_hash_table (info)->dynamic_sections_created
   10935 		      && p->d.h != NULL
   10936 		      && p->d.h->root.def_dynamic
   10937 		      && !p->d.h->root.def_regular))
   10938 		{
   10939 		  /* Create an R_MIPS_REL32 relocation for this entry.  Due to
   10940 		     the various compatibility problems, it's easier to mock
   10941 		     up an R_MIPS_32 or R_MIPS_64 relocation and leave
   10942 		     mips_elf_create_dynamic_relocation to calculate the
   10943 		     appropriate addend.  */
   10944 		  Elf_Internal_Rela rel[3];
   10945 
   10946 		  memset (rel, 0, sizeof (rel));
   10947 		  if (ABI_64_P (output_bfd))
   10948 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
   10949 		  else
   10950 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
   10951 		  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
   10952 
   10953 		  entry = 0;
   10954 		  if (! (mips_elf_create_dynamic_relocation
   10955 			 (output_bfd, info, rel,
   10956 			  e.d.h, NULL, sym->st_value, &entry, sgot)))
   10957 		    return FALSE;
   10958 		}
   10959 	      else
   10960 		entry = sym->st_value;
   10961 	      MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
   10962 	    }
   10963 	}
   10964     }
   10965 
   10966   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
   10967   name = h->root.root.string;
   10968   if (h == elf_hash_table (info)->hdynamic
   10969       || h == elf_hash_table (info)->hgot)
   10970     sym->st_shndx = SHN_ABS;
   10971   else if (strcmp (name, "_DYNAMIC_LINK") == 0
   10972 	   || strcmp (name, "_DYNAMIC_LINKING") == 0)
   10973     {
   10974       sym->st_shndx = SHN_ABS;
   10975       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   10976       sym->st_value = 1;
   10977     }
   10978   else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
   10979     {
   10980       sym->st_shndx = SHN_ABS;
   10981       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   10982       sym->st_value = elf_gp (output_bfd);
   10983     }
   10984   else if (SGI_COMPAT (output_bfd))
   10985     {
   10986       if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
   10987 	  || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
   10988 	{
   10989 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   10990 	  sym->st_other = STO_PROTECTED;
   10991 	  sym->st_value = 0;
   10992 	  sym->st_shndx = SHN_MIPS_DATA;
   10993 	}
   10994       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
   10995 	{
   10996 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   10997 	  sym->st_other = STO_PROTECTED;
   10998 	  sym->st_value = mips_elf_hash_table (info)->procedure_count;
   10999 	  sym->st_shndx = SHN_ABS;
   11000 	}
   11001       else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
   11002 	{
   11003 	  if (h->type == STT_FUNC)
   11004 	    sym->st_shndx = SHN_MIPS_TEXT;
   11005 	  else if (h->type == STT_OBJECT)
   11006 	    sym->st_shndx = SHN_MIPS_DATA;
   11007 	}
   11008     }
   11009 
   11010   /* Emit a copy reloc, if needed.  */
   11011   if (h->needs_copy)
   11012     {
   11013       asection *s;
   11014       bfd_vma symval;
   11015 
   11016       BFD_ASSERT (h->dynindx != -1);
   11017       BFD_ASSERT (htab->use_plts_and_copy_relocs);
   11018 
   11019       s = mips_elf_rel_dyn_section (info, FALSE);
   11020       symval = (h->root.u.def.section->output_section->vma
   11021 		+ h->root.u.def.section->output_offset
   11022 		+ h->root.u.def.value);
   11023       mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
   11024 					  h->dynindx, R_MIPS_COPY, symval);
   11025     }
   11026 
   11027   /* Handle the IRIX6-specific symbols.  */
   11028   if (IRIX_COMPAT (output_bfd) == ict_irix6)
   11029     mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
   11030 
   11031   /* Keep dynamic compressed symbols odd.  This allows the dynamic linker
   11032      to treat compressed symbols like any other.  */
   11033   if (ELF_ST_IS_MIPS16 (sym->st_other))
   11034     {
   11035       BFD_ASSERT (sym->st_value & 1);
   11036       sym->st_other -= STO_MIPS16;
   11037     }
   11038   else if (ELF_ST_IS_MICROMIPS (sym->st_other))
   11039     {
   11040       BFD_ASSERT (sym->st_value & 1);
   11041       sym->st_other -= STO_MICROMIPS;
   11042     }
   11043 
   11044   return TRUE;
   11045 }
   11046 
   11047 /* Likewise, for VxWorks.  */
   11048 
   11049 bfd_boolean
   11050 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
   11051 					 struct bfd_link_info *info,
   11052 					 struct elf_link_hash_entry *h,
   11053 					 Elf_Internal_Sym *sym)
   11054 {
   11055   bfd *dynobj;
   11056   asection *sgot;
   11057   struct mips_got_info *g;
   11058   struct mips_elf_link_hash_table *htab;
   11059   struct mips_elf_link_hash_entry *hmips;
   11060 
   11061   htab = mips_elf_hash_table (info);
   11062   BFD_ASSERT (htab != NULL);
   11063   dynobj = elf_hash_table (info)->dynobj;
   11064   hmips = (struct mips_elf_link_hash_entry *) h;
   11065 
   11066   if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
   11067     {
   11068       bfd_byte *loc;
   11069       bfd_vma plt_address, got_address, got_offset, branch_offset;
   11070       Elf_Internal_Rela rel;
   11071       static const bfd_vma *plt_entry;
   11072       bfd_vma gotplt_index;
   11073       bfd_vma plt_offset;
   11074 
   11075       plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
   11076       gotplt_index = h->plt.plist->gotplt_index;
   11077 
   11078       BFD_ASSERT (h->dynindx != -1);
   11079       BFD_ASSERT (htab->splt != NULL);
   11080       BFD_ASSERT (gotplt_index != MINUS_ONE);
   11081       BFD_ASSERT (plt_offset <= htab->splt->size);
   11082 
   11083       /* Calculate the address of the .plt entry.  */
   11084       plt_address = (htab->splt->output_section->vma
   11085 		     + htab->splt->output_offset
   11086 		     + plt_offset);
   11087 
   11088       /* Calculate the address of the .got.plt entry.  */
   11089       got_address = (htab->sgotplt->output_section->vma
   11090 		     + htab->sgotplt->output_offset
   11091 		     + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
   11092 
   11093       /* Calculate the offset of the .got.plt entry from
   11094 	 _GLOBAL_OFFSET_TABLE_.  */
   11095       got_offset = mips_elf_gotplt_index (info, h);
   11096 
   11097       /* Calculate the offset for the branch at the start of the PLT
   11098 	 entry.  The branch jumps to the beginning of .plt.  */
   11099       branch_offset = -(plt_offset / 4 + 1) & 0xffff;
   11100 
   11101       /* Fill in the initial value of the .got.plt entry.  */
   11102       bfd_put_32 (output_bfd, plt_address,
   11103 		  (htab->sgotplt->contents
   11104 		   + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
   11105 
   11106       /* Find out where the .plt entry should go.  */
   11107       loc = htab->splt->contents + plt_offset;
   11108 
   11109       if (bfd_link_pic (info))
   11110 	{
   11111 	  plt_entry = mips_vxworks_shared_plt_entry;
   11112 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
   11113 	  bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
   11114 	}
   11115       else
   11116 	{
   11117 	  bfd_vma got_address_high, got_address_low;
   11118 
   11119 	  plt_entry = mips_vxworks_exec_plt_entry;
   11120 	  got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
   11121 	  got_address_low = got_address & 0xffff;
   11122 
   11123 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
   11124 	  bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
   11125 	  bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
   11126 	  bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
   11127 	  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11128 	  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11129 	  bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
   11130 	  bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
   11131 
   11132 	  loc = (htab->srelplt2->contents
   11133 		 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
   11134 
   11135 	  /* Emit a relocation for the .got.plt entry.  */
   11136 	  rel.r_offset = got_address;
   11137 	  rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
   11138 	  rel.r_addend = plt_offset;
   11139 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11140 
   11141 	  /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
   11142 	  loc += sizeof (Elf32_External_Rela);
   11143 	  rel.r_offset = plt_address + 8;
   11144 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11145 	  rel.r_addend = got_offset;
   11146 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11147 
   11148 	  /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
   11149 	  loc += sizeof (Elf32_External_Rela);
   11150 	  rel.r_offset += 4;
   11151 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11152 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11153 	}
   11154 
   11155       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
   11156       loc = (htab->srelplt->contents
   11157 	     + gotplt_index * sizeof (Elf32_External_Rela));
   11158       rel.r_offset = got_address;
   11159       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
   11160       rel.r_addend = 0;
   11161       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11162 
   11163       if (!h->def_regular)
   11164 	sym->st_shndx = SHN_UNDEF;
   11165     }
   11166 
   11167   BFD_ASSERT (h->dynindx != -1 || h->forced_local);
   11168 
   11169   sgot = htab->sgot;
   11170   g = htab->got_info;
   11171   BFD_ASSERT (g != NULL);
   11172 
   11173   /* See if this symbol has an entry in the GOT.  */
   11174   if (hmips->global_got_area != GGA_NONE)
   11175     {
   11176       bfd_vma offset;
   11177       Elf_Internal_Rela outrel;
   11178       bfd_byte *loc;
   11179       asection *s;
   11180 
   11181       /* Install the symbol value in the GOT.   */
   11182       offset = mips_elf_primary_global_got_index (output_bfd, info, h);
   11183       MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
   11184 
   11185       /* Add a dynamic relocation for it.  */
   11186       s = mips_elf_rel_dyn_section (info, FALSE);
   11187       loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
   11188       outrel.r_offset = (sgot->output_section->vma
   11189 			 + sgot->output_offset
   11190 			 + offset);
   11191       outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
   11192       outrel.r_addend = 0;
   11193       bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
   11194     }
   11195 
   11196   /* Emit a copy reloc, if needed.  */
   11197   if (h->needs_copy)
   11198     {
   11199       Elf_Internal_Rela rel;
   11200 
   11201       BFD_ASSERT (h->dynindx != -1);
   11202 
   11203       rel.r_offset = (h->root.u.def.section->output_section->vma
   11204 		      + h->root.u.def.section->output_offset
   11205 		      + h->root.u.def.value);
   11206       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
   11207       rel.r_addend = 0;
   11208       bfd_elf32_swap_reloca_out (output_bfd, &rel,
   11209 				 htab->srelbss->contents
   11210 				 + (htab->srelbss->reloc_count
   11211 				    * sizeof (Elf32_External_Rela)));
   11212       ++htab->srelbss->reloc_count;
   11213     }
   11214 
   11215   /* If this is a mips16/microMIPS symbol, force the value to be even.  */
   11216   if (ELF_ST_IS_COMPRESSED (sym->st_other))
   11217     sym->st_value &= ~1;
   11218 
   11219   return TRUE;
   11220 }
   11221 
   11222 /* Write out a plt0 entry to the beginning of .plt.  */
   11223 
   11224 static bfd_boolean
   11225 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
   11226 {
   11227   bfd_byte *loc;
   11228   bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
   11229   static const bfd_vma *plt_entry;
   11230   struct mips_elf_link_hash_table *htab;
   11231 
   11232   htab = mips_elf_hash_table (info);
   11233   BFD_ASSERT (htab != NULL);
   11234 
   11235   if (ABI_64_P (output_bfd))
   11236     plt_entry = mips_n64_exec_plt0_entry;
   11237   else if (ABI_N32_P (output_bfd))
   11238     plt_entry = mips_n32_exec_plt0_entry;
   11239   else if (!htab->plt_header_is_comp)
   11240     plt_entry = mips_o32_exec_plt0_entry;
   11241   else if (htab->insn32)
   11242     plt_entry = micromips_insn32_o32_exec_plt0_entry;
   11243   else
   11244     plt_entry = micromips_o32_exec_plt0_entry;
   11245 
   11246   /* Calculate the value of .got.plt.  */
   11247   gotplt_value = (htab->sgotplt->output_section->vma
   11248 		  + htab->sgotplt->output_offset);
   11249   gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
   11250   gotplt_value_low = gotplt_value & 0xffff;
   11251 
   11252   /* The PLT sequence is not safe for N64 if .got.plt's address can
   11253      not be loaded in two instructions.  */
   11254   BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
   11255 	      || ~(gotplt_value | 0x7fffffff) == 0);
   11256 
   11257   /* Install the PLT header.  */
   11258   loc = htab->splt->contents;
   11259   if (plt_entry == micromips_o32_exec_plt0_entry)
   11260     {
   11261       bfd_vma gotpc_offset;
   11262       bfd_vma loc_address;
   11263       size_t i;
   11264 
   11265       BFD_ASSERT (gotplt_value % 4 == 0);
   11266 
   11267       loc_address = (htab->splt->output_section->vma
   11268 		     + htab->splt->output_offset);
   11269       gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
   11270 
   11271       /* ADDIUPC has a span of +/-16MB, check we're in range.  */
   11272       if (gotpc_offset + 0x1000000 >= 0x2000000)
   11273 	{
   11274 	  (*_bfd_error_handler)
   11275 	    (_("%B: `%A' offset of %ld from `%A' beyond the range of ADDIUPC"),
   11276 	     output_bfd,
   11277 	     htab->sgotplt->output_section,
   11278 	     htab->splt->output_section,
   11279 	     (long) gotpc_offset);
   11280 	  bfd_set_error (bfd_error_no_error);
   11281 	  return FALSE;
   11282 	}
   11283       bfd_put_16 (output_bfd,
   11284 		  plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
   11285       bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
   11286       for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
   11287 	bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
   11288     }
   11289   else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
   11290     {
   11291       size_t i;
   11292 
   11293       bfd_put_16 (output_bfd, plt_entry[0], loc);
   11294       bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
   11295       bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
   11296       bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
   11297       bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
   11298       bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
   11299       for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
   11300 	bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
   11301     }
   11302   else
   11303     {
   11304       bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
   11305       bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
   11306       bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
   11307       bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   11308       bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11309       bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11310       bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
   11311       bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
   11312     }
   11313 
   11314   return TRUE;
   11315 }
   11316 
   11317 /* Install the PLT header for a VxWorks executable and finalize the
   11318    contents of .rela.plt.unloaded.  */
   11319 
   11320 static void
   11321 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
   11322 {
   11323   Elf_Internal_Rela rela;
   11324   bfd_byte *loc;
   11325   bfd_vma got_value, got_value_high, got_value_low, plt_address;
   11326   static const bfd_vma *plt_entry;
   11327   struct mips_elf_link_hash_table *htab;
   11328 
   11329   htab = mips_elf_hash_table (info);
   11330   BFD_ASSERT (htab != NULL);
   11331 
   11332   plt_entry = mips_vxworks_exec_plt0_entry;
   11333 
   11334   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
   11335   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
   11336 	       + htab->root.hgot->root.u.def.section->output_offset
   11337 	       + htab->root.hgot->root.u.def.value);
   11338 
   11339   got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
   11340   got_value_low = got_value & 0xffff;
   11341 
   11342   /* Calculate the address of the PLT header.  */
   11343   plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
   11344 
   11345   /* Install the PLT header.  */
   11346   loc = htab->splt->contents;
   11347   bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
   11348   bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
   11349   bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
   11350   bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   11351   bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   11352   bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   11353 
   11354   /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
   11355   loc = htab->srelplt2->contents;
   11356   rela.r_offset = plt_address;
   11357   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11358   rela.r_addend = 0;
   11359   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   11360   loc += sizeof (Elf32_External_Rela);
   11361 
   11362   /* Output the relocation for the following addiu of
   11363      %lo(_GLOBAL_OFFSET_TABLE_).  */
   11364   rela.r_offset += 4;
   11365   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11366   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   11367   loc += sizeof (Elf32_External_Rela);
   11368 
   11369   /* Fix up the remaining relocations.  They may have the wrong
   11370      symbol index for _G_O_T_ or _P_L_T_ depending on the order
   11371      in which symbols were output.  */
   11372   while (loc < htab->srelplt2->contents + htab->srelplt2->size)
   11373     {
   11374       Elf_Internal_Rela rel;
   11375 
   11376       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11377       rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
   11378       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11379       loc += sizeof (Elf32_External_Rela);
   11380 
   11381       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11382       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   11383       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11384       loc += sizeof (Elf32_External_Rela);
   11385 
   11386       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   11387       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   11388       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   11389       loc += sizeof (Elf32_External_Rela);
   11390     }
   11391 }
   11392 
   11393 /* Install the PLT header for a VxWorks shared library.  */
   11394 
   11395 static void
   11396 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
   11397 {
   11398   unsigned int i;
   11399   struct mips_elf_link_hash_table *htab;
   11400 
   11401   htab = mips_elf_hash_table (info);
   11402   BFD_ASSERT (htab != NULL);
   11403 
   11404   /* We just need to copy the entry byte-by-byte.  */
   11405   for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
   11406     bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
   11407 		htab->splt->contents + i * 4);
   11408 }
   11409 
   11410 /* Finish up the dynamic sections.  */
   11411 
   11412 bfd_boolean
   11413 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
   11414 				       struct bfd_link_info *info)
   11415 {
   11416   bfd *dynobj;
   11417   asection *sdyn;
   11418   asection *sgot;
   11419   struct mips_got_info *gg, *g;
   11420   struct mips_elf_link_hash_table *htab;
   11421 
   11422   htab = mips_elf_hash_table (info);
   11423   BFD_ASSERT (htab != NULL);
   11424 
   11425   dynobj = elf_hash_table (info)->dynobj;
   11426 
   11427   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   11428 
   11429   sgot = htab->sgot;
   11430   gg = htab->got_info;
   11431 
   11432   if (elf_hash_table (info)->dynamic_sections_created)
   11433     {
   11434       bfd_byte *b;
   11435       int dyn_to_skip = 0, dyn_skipped = 0;
   11436 
   11437       BFD_ASSERT (sdyn != NULL);
   11438       BFD_ASSERT (gg != NULL);
   11439 
   11440       g = mips_elf_bfd_got (output_bfd, FALSE);
   11441       BFD_ASSERT (g != NULL);
   11442 
   11443       for (b = sdyn->contents;
   11444 	   b < sdyn->contents + sdyn->size;
   11445 	   b += MIPS_ELF_DYN_SIZE (dynobj))
   11446 	{
   11447 	  Elf_Internal_Dyn dyn;
   11448 	  const char *name;
   11449 	  size_t elemsize;
   11450 	  asection *s;
   11451 	  bfd_boolean swap_out_p;
   11452 
   11453 	  /* Read in the current dynamic entry.  */
   11454 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
   11455 
   11456 	  /* Assume that we're going to modify it and write it out.  */
   11457 	  swap_out_p = TRUE;
   11458 
   11459 	  switch (dyn.d_tag)
   11460 	    {
   11461 	    case DT_RELENT:
   11462 	      dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
   11463 	      break;
   11464 
   11465 	    case DT_RELAENT:
   11466 	      BFD_ASSERT (htab->is_vxworks);
   11467 	      dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
   11468 	      break;
   11469 
   11470 	    case DT_STRSZ:
   11471 	      /* Rewrite DT_STRSZ.  */
   11472 	      dyn.d_un.d_val =
   11473 		_bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   11474 	      break;
   11475 
   11476 	    case DT_PLTGOT:
   11477 	      s = htab->sgot;
   11478 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   11479 	      break;
   11480 
   11481 	    case DT_MIPS_PLTGOT:
   11482 	      s = htab->sgotplt;
   11483 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   11484 	      break;
   11485 
   11486 	    case DT_MIPS_RLD_VERSION:
   11487 	      dyn.d_un.d_val = 1; /* XXX */
   11488 	      break;
   11489 
   11490 	    case DT_MIPS_FLAGS:
   11491 	      dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
   11492 	      break;
   11493 
   11494 	    case DT_MIPS_TIME_STAMP:
   11495 	      {
   11496 		time_t t;
   11497 		time (&t);
   11498 		dyn.d_un.d_val = t;
   11499 	      }
   11500 	      break;
   11501 
   11502 	    case DT_MIPS_ICHECKSUM:
   11503 	      /* XXX FIXME: */
   11504 	      swap_out_p = FALSE;
   11505 	      break;
   11506 
   11507 	    case DT_MIPS_IVERSION:
   11508 	      /* XXX FIXME: */
   11509 	      swap_out_p = FALSE;
   11510 	      break;
   11511 
   11512 	    case DT_MIPS_BASE_ADDRESS:
   11513 	      s = output_bfd->sections;
   11514 	      BFD_ASSERT (s != NULL);
   11515 	      dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
   11516 	      break;
   11517 
   11518 	    case DT_MIPS_LOCAL_GOTNO:
   11519 	      dyn.d_un.d_val = g->local_gotno;
   11520 	      break;
   11521 
   11522 	    case DT_MIPS_UNREFEXTNO:
   11523 	      /* The index into the dynamic symbol table which is the
   11524 		 entry of the first external symbol that is not
   11525 		 referenced within the same object.  */
   11526 	      dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
   11527 	      break;
   11528 
   11529 	    case DT_MIPS_GOTSYM:
   11530 	      if (htab->global_gotsym)
   11531 		{
   11532 		  dyn.d_un.d_val = htab->global_gotsym->dynindx;
   11533 		  break;
   11534 		}
   11535 	      /* In case if we don't have global got symbols we default
   11536 		 to setting DT_MIPS_GOTSYM to the same value as
   11537 		 DT_MIPS_SYMTABNO, so we just fall through.  */
   11538 
   11539 	    case DT_MIPS_SYMTABNO:
   11540 	      name = ".dynsym";
   11541 	      elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
   11542 	      s = bfd_get_linker_section (dynobj, name);
   11543 
   11544 	      if (s != NULL)
   11545 		dyn.d_un.d_val = s->size / elemsize;
   11546 	      else
   11547 		dyn.d_un.d_val = 0;
   11548 	      break;
   11549 
   11550 	    case DT_MIPS_HIPAGENO:
   11551 	      dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
   11552 	      break;
   11553 
   11554 	    case DT_MIPS_RLD_MAP:
   11555 	      {
   11556 		struct elf_link_hash_entry *h;
   11557 		h = mips_elf_hash_table (info)->rld_symbol;
   11558 		if (!h)
   11559 		  {
   11560 		    dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   11561 		    swap_out_p = FALSE;
   11562 		    break;
   11563 		  }
   11564 		s = h->root.u.def.section;
   11565 
   11566 		/* The MIPS_RLD_MAP tag stores the absolute address of the
   11567 		   debug pointer.  */
   11568 		dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
   11569 				  + h->root.u.def.value);
   11570 	      }
   11571 	      break;
   11572 
   11573 	    case DT_MIPS_RLD_MAP_REL:
   11574 	      {
   11575 		struct elf_link_hash_entry *h;
   11576 		bfd_vma dt_addr, rld_addr;
   11577 		h = mips_elf_hash_table (info)->rld_symbol;
   11578 		if (!h)
   11579 		  {
   11580 		    dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   11581 		    swap_out_p = FALSE;
   11582 		    break;
   11583 		  }
   11584 		s = h->root.u.def.section;
   11585 
   11586 		/* The MIPS_RLD_MAP_REL tag stores the offset to the debug
   11587 		   pointer, relative to the address of the tag.  */
   11588 		dt_addr = (sdyn->output_section->vma + sdyn->output_offset
   11589 			   + (b - sdyn->contents));
   11590 		rld_addr = (s->output_section->vma + s->output_offset
   11591 			    + h->root.u.def.value);
   11592 		dyn.d_un.d_ptr = rld_addr - dt_addr;
   11593 	      }
   11594 	      break;
   11595 
   11596 	    case DT_MIPS_OPTIONS:
   11597 	      s = (bfd_get_section_by_name
   11598 		   (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
   11599 	      dyn.d_un.d_ptr = s->vma;
   11600 	      break;
   11601 
   11602 	    case DT_RELASZ:
   11603 	      BFD_ASSERT (htab->is_vxworks);
   11604 	      /* The count does not include the JUMP_SLOT relocations.  */
   11605 	      if (htab->srelplt)
   11606 		dyn.d_un.d_val -= htab->srelplt->size;
   11607 	      break;
   11608 
   11609 	    case DT_PLTREL:
   11610 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   11611 	      if (htab->is_vxworks)
   11612 		dyn.d_un.d_val = DT_RELA;
   11613 	      else
   11614 		dyn.d_un.d_val = DT_REL;
   11615 	      break;
   11616 
   11617 	    case DT_PLTRELSZ:
   11618 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   11619 	      dyn.d_un.d_val = htab->srelplt->size;
   11620 	      break;
   11621 
   11622 	    case DT_JMPREL:
   11623 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   11624 	      dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
   11625 				+ htab->srelplt->output_offset);
   11626 	      break;
   11627 
   11628 	    case DT_TEXTREL:
   11629 	      /* If we didn't need any text relocations after all, delete
   11630 		 the dynamic tag.  */
   11631 	      if (!(info->flags & DF_TEXTREL))
   11632 		{
   11633 		  dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   11634 		  swap_out_p = FALSE;
   11635 		}
   11636 	      break;
   11637 
   11638 	    case DT_FLAGS:
   11639 	      /* If we didn't need any text relocations after all, clear
   11640 		 DF_TEXTREL from DT_FLAGS.  */
   11641 	      if (!(info->flags & DF_TEXTREL))
   11642 		dyn.d_un.d_val &= ~DF_TEXTREL;
   11643 	      else
   11644 		swap_out_p = FALSE;
   11645 	      break;
   11646 
   11647 	    default:
   11648 	      swap_out_p = FALSE;
   11649 	      if (htab->is_vxworks
   11650 		  && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
   11651 		swap_out_p = TRUE;
   11652 	      break;
   11653 	    }
   11654 
   11655 	  if (swap_out_p || dyn_skipped)
   11656 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
   11657 	      (dynobj, &dyn, b - dyn_skipped);
   11658 
   11659 	  if (dyn_to_skip)
   11660 	    {
   11661 	      dyn_skipped += dyn_to_skip;
   11662 	      dyn_to_skip = 0;
   11663 	    }
   11664 	}
   11665 
   11666       /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
   11667       if (dyn_skipped > 0)
   11668 	memset (b - dyn_skipped, 0, dyn_skipped);
   11669     }
   11670 
   11671   if (sgot != NULL && sgot->size > 0
   11672       && !bfd_is_abs_section (sgot->output_section))
   11673     {
   11674       if (htab->is_vxworks)
   11675 	{
   11676 	  /* The first entry of the global offset table points to the
   11677 	     ".dynamic" section.  The second is initialized by the
   11678 	     loader and contains the shared library identifier.
   11679 	     The third is also initialized by the loader and points
   11680 	     to the lazy resolution stub.  */
   11681 	  MIPS_ELF_PUT_WORD (output_bfd,
   11682 			     sdyn->output_offset + sdyn->output_section->vma,
   11683 			     sgot->contents);
   11684 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
   11685 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
   11686 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
   11687 			     sgot->contents
   11688 			     + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
   11689 	}
   11690       else
   11691 	{
   11692 	  /* The first entry of the global offset table will be filled at
   11693 	     runtime. The second entry will be used by some runtime loaders.
   11694 	     This isn't the case of IRIX rld.  */
   11695 	  MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
   11696 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
   11697 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
   11698 	}
   11699 
   11700       elf_section_data (sgot->output_section)->this_hdr.sh_entsize
   11701 	 = MIPS_ELF_GOT_SIZE (output_bfd);
   11702     }
   11703 
   11704   /* Generate dynamic relocations for the non-primary gots.  */
   11705   if (gg != NULL && gg->next)
   11706     {
   11707       Elf_Internal_Rela rel[3];
   11708       bfd_vma addend = 0;
   11709 
   11710       memset (rel, 0, sizeof (rel));
   11711       rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
   11712 
   11713       for (g = gg->next; g->next != gg; g = g->next)
   11714 	{
   11715 	  bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
   11716 	    + g->next->tls_gotno;
   11717 
   11718 	  MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
   11719 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
   11720 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
   11721 			     sgot->contents
   11722 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
   11723 
   11724 	  if (! bfd_link_pic (info))
   11725 	    continue;
   11726 
   11727 	  for (; got_index < g->local_gotno; got_index++)
   11728 	    {
   11729 	      if (got_index >= g->assigned_low_gotno
   11730 		  && got_index <= g->assigned_high_gotno)
   11731 		continue;
   11732 
   11733 	      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
   11734 		= got_index * MIPS_ELF_GOT_SIZE (output_bfd);
   11735 	      if (!(mips_elf_create_dynamic_relocation
   11736 		    (output_bfd, info, rel, NULL,
   11737 		     bfd_abs_section_ptr,
   11738 		     0, &addend, sgot)))
   11739 		return FALSE;
   11740 	      BFD_ASSERT (addend == 0);
   11741 	    }
   11742 	}
   11743     }
   11744 
   11745   /* The generation of dynamic relocations for the non-primary gots
   11746      adds more dynamic relocations.  We cannot count them until
   11747      here.  */
   11748 
   11749   if (elf_hash_table (info)->dynamic_sections_created)
   11750     {
   11751       bfd_byte *b;
   11752       bfd_boolean swap_out_p;
   11753 
   11754       BFD_ASSERT (sdyn != NULL);
   11755 
   11756       for (b = sdyn->contents;
   11757 	   b < sdyn->contents + sdyn->size;
   11758 	   b += MIPS_ELF_DYN_SIZE (dynobj))
   11759 	{
   11760 	  Elf_Internal_Dyn dyn;
   11761 	  asection *s;
   11762 
   11763 	  /* Read in the current dynamic entry.  */
   11764 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
   11765 
   11766 	  /* Assume that we're going to modify it and write it out.  */
   11767 	  swap_out_p = TRUE;
   11768 
   11769 	  switch (dyn.d_tag)
   11770 	    {
   11771 	    case DT_RELSZ:
   11772 	      /* Reduce DT_RELSZ to account for any relocations we
   11773 		 decided not to make.  This is for the n64 irix rld,
   11774 		 which doesn't seem to apply any relocations if there
   11775 		 are trailing null entries.  */
   11776 	      s = mips_elf_rel_dyn_section (info, FALSE);
   11777 	      dyn.d_un.d_val = (s->reloc_count
   11778 				* (ABI_64_P (output_bfd)
   11779 				   ? sizeof (Elf64_Mips_External_Rel)
   11780 				   : sizeof (Elf32_External_Rel)));
   11781 	      /* Adjust the section size too.  Tools like the prelinker
   11782 		 can reasonably expect the values to the same.  */
   11783 	      elf_section_data (s->output_section)->this_hdr.sh_size
   11784 		= dyn.d_un.d_val;
   11785 	      break;
   11786 
   11787 	    default:
   11788 	      swap_out_p = FALSE;
   11789 	      break;
   11790 	    }
   11791 
   11792 	  if (swap_out_p)
   11793 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
   11794 	      (dynobj, &dyn, b);
   11795 	}
   11796     }
   11797 
   11798   {
   11799     asection *s;
   11800     Elf32_compact_rel cpt;
   11801 
   11802     if (SGI_COMPAT (output_bfd))
   11803       {
   11804 	/* Write .compact_rel section out.  */
   11805 	s = bfd_get_linker_section (dynobj, ".compact_rel");
   11806 	if (s != NULL)
   11807 	  {
   11808 	    cpt.id1 = 1;
   11809 	    cpt.num = s->reloc_count;
   11810 	    cpt.id2 = 2;
   11811 	    cpt.offset = (s->output_section->filepos
   11812 			  + sizeof (Elf32_External_compact_rel));
   11813 	    cpt.reserved0 = 0;
   11814 	    cpt.reserved1 = 0;
   11815 	    bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
   11816 					    ((Elf32_External_compact_rel *)
   11817 					     s->contents));
   11818 
   11819 	    /* Clean up a dummy stub function entry in .text.  */
   11820 	    if (htab->sstubs != NULL)
   11821 	      {
   11822 		file_ptr dummy_offset;
   11823 
   11824 		BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
   11825 		dummy_offset = htab->sstubs->size - htab->function_stub_size;
   11826 		memset (htab->sstubs->contents + dummy_offset, 0,
   11827 			htab->function_stub_size);
   11828 	      }
   11829 	  }
   11830       }
   11831 
   11832     /* The psABI says that the dynamic relocations must be sorted in
   11833        increasing order of r_symndx.  The VxWorks EABI doesn't require
   11834        this, and because the code below handles REL rather than RELA
   11835        relocations, using it for VxWorks would be outright harmful.  */
   11836     if (!htab->is_vxworks)
   11837       {
   11838 	s = mips_elf_rel_dyn_section (info, FALSE);
   11839 	if (s != NULL
   11840 	    && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
   11841 	  {
   11842 	    reldyn_sorting_bfd = output_bfd;
   11843 
   11844 	    if (ABI_64_P (output_bfd))
   11845 	      qsort ((Elf64_External_Rel *) s->contents + 1,
   11846 		     s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
   11847 		     sort_dynamic_relocs_64);
   11848 	    else
   11849 	      qsort ((Elf32_External_Rel *) s->contents + 1,
   11850 		     s->reloc_count - 1, sizeof (Elf32_External_Rel),
   11851 		     sort_dynamic_relocs);
   11852 	  }
   11853       }
   11854   }
   11855 
   11856   if (htab->splt && htab->splt->size > 0)
   11857     {
   11858       if (htab->is_vxworks)
   11859 	{
   11860 	  if (bfd_link_pic (info))
   11861 	    mips_vxworks_finish_shared_plt (output_bfd, info);
   11862 	  else
   11863 	    mips_vxworks_finish_exec_plt (output_bfd, info);
   11864 	}
   11865       else
   11866 	{
   11867 	  BFD_ASSERT (!bfd_link_pic (info));
   11868 	  if (!mips_finish_exec_plt (output_bfd, info))
   11869 	    return FALSE;
   11870 	}
   11871     }
   11872   return TRUE;
   11873 }
   11874 
   11875 
   11876 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
   11877 
   11878 static void
   11879 mips_set_isa_flags (bfd *abfd)
   11880 {
   11881   flagword val;
   11882 
   11883   switch (bfd_get_mach (abfd))
   11884     {
   11885     default:
   11886     case bfd_mach_mips3000:
   11887       val = E_MIPS_ARCH_1;
   11888       break;
   11889 
   11890     case bfd_mach_mips3900:
   11891       val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
   11892       break;
   11893 
   11894     case bfd_mach_mips6000:
   11895       val = E_MIPS_ARCH_2;
   11896       break;
   11897 
   11898     case bfd_mach_mips4000:
   11899     case bfd_mach_mips4300:
   11900     case bfd_mach_mips4400:
   11901     case bfd_mach_mips4600:
   11902       val = E_MIPS_ARCH_3;
   11903       break;
   11904 
   11905     case bfd_mach_mips4010:
   11906       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
   11907       break;
   11908 
   11909     case bfd_mach_mips4100:
   11910       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
   11911       break;
   11912 
   11913     case bfd_mach_mips4111:
   11914       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
   11915       break;
   11916 
   11917     case bfd_mach_mips4120:
   11918       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
   11919       break;
   11920 
   11921     case bfd_mach_mips4650:
   11922       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
   11923       break;
   11924 
   11925     case bfd_mach_mips5400:
   11926       val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
   11927       break;
   11928 
   11929     case bfd_mach_mips5500:
   11930       val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
   11931       break;
   11932 
   11933     case bfd_mach_mips5900:
   11934       val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
   11935       break;
   11936 
   11937     case bfd_mach_mips9000:
   11938       val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
   11939       break;
   11940 
   11941     case bfd_mach_mips5000:
   11942     case bfd_mach_mips7000:
   11943     case bfd_mach_mips8000:
   11944     case bfd_mach_mips10000:
   11945     case bfd_mach_mips12000:
   11946     case bfd_mach_mips14000:
   11947     case bfd_mach_mips16000:
   11948       val = E_MIPS_ARCH_4;
   11949       break;
   11950 
   11951     case bfd_mach_mips5:
   11952       val = E_MIPS_ARCH_5;
   11953       break;
   11954 
   11955     case bfd_mach_mips_loongson_2e:
   11956       val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
   11957       break;
   11958 
   11959     case bfd_mach_mips_loongson_2f:
   11960       val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
   11961       break;
   11962 
   11963     case bfd_mach_mips_sb1:
   11964       val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
   11965       break;
   11966 
   11967     case bfd_mach_mips_loongson_3a:
   11968       val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_LS3A;
   11969       break;
   11970 
   11971     case bfd_mach_mips_octeon:
   11972     case bfd_mach_mips_octeonp:
   11973       val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
   11974       break;
   11975 
   11976     case bfd_mach_mips_octeon3:
   11977       val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
   11978       break;
   11979 
   11980     case bfd_mach_mips_xlr:
   11981       val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
   11982       break;
   11983 
   11984     case bfd_mach_mips_octeon2:
   11985       val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
   11986       break;
   11987 
   11988     case bfd_mach_mipsisa32:
   11989       val = E_MIPS_ARCH_32;
   11990       break;
   11991 
   11992     case bfd_mach_mipsisa64:
   11993       val = E_MIPS_ARCH_64;
   11994       break;
   11995 
   11996     case bfd_mach_mipsisa32r2:
   11997     case bfd_mach_mipsisa32r3:
   11998     case bfd_mach_mipsisa32r5:
   11999       val = E_MIPS_ARCH_32R2;
   12000       break;
   12001 
   12002     case bfd_mach_mipsisa64r2:
   12003     case bfd_mach_mipsisa64r3:
   12004     case bfd_mach_mipsisa64r5:
   12005       val = E_MIPS_ARCH_64R2;
   12006       break;
   12007 
   12008     case bfd_mach_mipsisa32r6:
   12009       val = E_MIPS_ARCH_32R6;
   12010       break;
   12011 
   12012     case bfd_mach_mipsisa64r6:
   12013       val = E_MIPS_ARCH_64R6;
   12014       break;
   12015     }
   12016   elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
   12017   elf_elfheader (abfd)->e_flags |= val;
   12018 
   12019 }
   12020 
   12021 
   12022 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
   12023    Don't do so for code sections.  We want to keep ordering of HI16/LO16
   12024    as is.  On the other hand, elf-eh-frame.c processing requires .eh_frame
   12025    relocs to be sorted.  */
   12026 
   12027 bfd_boolean
   12028 _bfd_mips_elf_sort_relocs_p (asection *sec)
   12029 {
   12030   return (sec->flags & SEC_CODE) == 0;
   12031 }
   12032 
   12033 
   12034 /* The final processing done just before writing out a MIPS ELF object
   12035    file.  This gets the MIPS architecture right based on the machine
   12036    number.  This is used by both the 32-bit and the 64-bit ABI.  */
   12037 
   12038 void
   12039 _bfd_mips_elf_final_write_processing (bfd *abfd,
   12040 				      bfd_boolean linker ATTRIBUTE_UNUSED)
   12041 {
   12042   unsigned int i;
   12043   Elf_Internal_Shdr **hdrpp;
   12044   const char *name;
   12045   asection *sec;
   12046 
   12047   /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
   12048      is nonzero.  This is for compatibility with old objects, which used
   12049      a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
   12050   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
   12051     mips_set_isa_flags (abfd);
   12052 
   12053   /* Set the sh_info field for .gptab sections and other appropriate
   12054      info for each special section.  */
   12055   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
   12056        i < elf_numsections (abfd);
   12057        i++, hdrpp++)
   12058     {
   12059       switch ((*hdrpp)->sh_type)
   12060 	{
   12061 	case SHT_MIPS_MSYM:
   12062 	case SHT_MIPS_LIBLIST:
   12063 	  sec = bfd_get_section_by_name (abfd, ".dynstr");
   12064 	  if (sec != NULL)
   12065 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12066 	  break;
   12067 
   12068 	case SHT_MIPS_GPTAB:
   12069 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12070 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
   12071 	  BFD_ASSERT (name != NULL
   12072 		      && CONST_STRNEQ (name, ".gptab."));
   12073 	  sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
   12074 	  BFD_ASSERT (sec != NULL);
   12075 	  (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
   12076 	  break;
   12077 
   12078 	case SHT_MIPS_CONTENT:
   12079 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12080 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
   12081 	  BFD_ASSERT (name != NULL
   12082 		      && CONST_STRNEQ (name, ".MIPS.content"));
   12083 	  sec = bfd_get_section_by_name (abfd,
   12084 					 name + sizeof ".MIPS.content" - 1);
   12085 	  BFD_ASSERT (sec != NULL);
   12086 	  (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12087 	  break;
   12088 
   12089 	case SHT_MIPS_SYMBOL_LIB:
   12090 	  sec = bfd_get_section_by_name (abfd, ".dynsym");
   12091 	  if (sec != NULL)
   12092 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12093 	  sec = bfd_get_section_by_name (abfd, ".liblist");
   12094 	  if (sec != NULL)
   12095 	    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
   12096 	  break;
   12097 
   12098 	case SHT_MIPS_EVENTS:
   12099 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   12100 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
   12101 	  BFD_ASSERT (name != NULL);
   12102 	  if (CONST_STRNEQ (name, ".MIPS.events"))
   12103 	    sec = bfd_get_section_by_name (abfd,
   12104 					   name + sizeof ".MIPS.events" - 1);
   12105 	  else
   12106 	    {
   12107 	      BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
   12108 	      sec = bfd_get_section_by_name (abfd,
   12109 					     (name
   12110 					      + sizeof ".MIPS.post_rel" - 1));
   12111 	    }
   12112 	  BFD_ASSERT (sec != NULL);
   12113 	  (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   12114 	  break;
   12115 
   12116 	}
   12117     }
   12118 }
   12119 
   12120 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
   12122    segments.  */
   12123 
   12124 int
   12125 _bfd_mips_elf_additional_program_headers (bfd *abfd,
   12126 					  struct bfd_link_info *info ATTRIBUTE_UNUSED)
   12127 {
   12128   asection *s;
   12129   int ret = 0;
   12130 
   12131   /* See if we need a PT_MIPS_REGINFO segment.  */
   12132   s = bfd_get_section_by_name (abfd, ".reginfo");
   12133   if (s && (s->flags & SEC_LOAD))
   12134     ++ret;
   12135 
   12136   /* See if we need a PT_MIPS_ABIFLAGS segment.  */
   12137   if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
   12138     ++ret;
   12139 
   12140   /* See if we need a PT_MIPS_OPTIONS segment.  */
   12141   if (IRIX_COMPAT (abfd) == ict_irix6
   12142       && bfd_get_section_by_name (abfd,
   12143 				  MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
   12144     ++ret;
   12145 
   12146   /* See if we need a PT_MIPS_RTPROC segment.  */
   12147   if (IRIX_COMPAT (abfd) == ict_irix5
   12148       && bfd_get_section_by_name (abfd, ".dynamic")
   12149       && bfd_get_section_by_name (abfd, ".mdebug"))
   12150     ++ret;
   12151 
   12152   /* Allocate a PT_NULL header in dynamic objects.  See
   12153      _bfd_mips_elf_modify_segment_map for details.  */
   12154   if (!SGI_COMPAT (abfd)
   12155       && bfd_get_section_by_name (abfd, ".dynamic"))
   12156     ++ret;
   12157 
   12158   return ret;
   12159 }
   12160 
   12161 /* Modify the segment map for an IRIX5 executable.  */
   12162 
   12163 bfd_boolean
   12164 _bfd_mips_elf_modify_segment_map (bfd *abfd,
   12165 				  struct bfd_link_info *info)
   12166 {
   12167   asection *s;
   12168   struct elf_segment_map *m, **pm;
   12169   bfd_size_type amt;
   12170 
   12171   /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
   12172      segment.  */
   12173   s = bfd_get_section_by_name (abfd, ".reginfo");
   12174   if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12175     {
   12176       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12177 	if (m->p_type == PT_MIPS_REGINFO)
   12178 	  break;
   12179       if (m == NULL)
   12180 	{
   12181 	  amt = sizeof *m;
   12182 	  m = bfd_zalloc (abfd, amt);
   12183 	  if (m == NULL)
   12184 	    return FALSE;
   12185 
   12186 	  m->p_type = PT_MIPS_REGINFO;
   12187 	  m->count = 1;
   12188 	  m->sections[0] = s;
   12189 
   12190 	  /* We want to put it after the PHDR and INTERP segments.  */
   12191 	  pm = &elf_seg_map (abfd);
   12192 	  while (*pm != NULL
   12193 		 && ((*pm)->p_type == PT_PHDR
   12194 		     || (*pm)->p_type == PT_INTERP))
   12195 	    pm = &(*pm)->next;
   12196 
   12197 	  m->next = *pm;
   12198 	  *pm = m;
   12199 	}
   12200     }
   12201 
   12202   /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
   12203      segment.  */
   12204   s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
   12205   if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12206     {
   12207       for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12208 	if (m->p_type == PT_MIPS_ABIFLAGS)
   12209 	  break;
   12210       if (m == NULL)
   12211 	{
   12212 	  amt = sizeof *m;
   12213 	  m = bfd_zalloc (abfd, amt);
   12214 	  if (m == NULL)
   12215 	    return FALSE;
   12216 
   12217 	  m->p_type = PT_MIPS_ABIFLAGS;
   12218 	  m->count = 1;
   12219 	  m->sections[0] = s;
   12220 
   12221 	  /* We want to put it after the PHDR and INTERP segments.  */
   12222 	  pm = &elf_seg_map (abfd);
   12223 	  while (*pm != NULL
   12224 		 && ((*pm)->p_type == PT_PHDR
   12225 		     || (*pm)->p_type == PT_INTERP))
   12226 	    pm = &(*pm)->next;
   12227 
   12228 	  m->next = *pm;
   12229 	  *pm = m;
   12230 	}
   12231     }
   12232 
   12233   /* For IRIX 6, we don't have .mdebug sections, nor does anything but
   12234      .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
   12235      PT_MIPS_OPTIONS segment immediately following the program header
   12236      table.  */
   12237   if (NEWABI_P (abfd)
   12238       /* On non-IRIX6 new abi, we'll have already created a segment
   12239 	 for this section, so don't create another.  I'm not sure this
   12240 	 is not also the case for IRIX 6, but I can't test it right
   12241 	 now.  */
   12242       && IRIX_COMPAT (abfd) == ict_irix6)
   12243     {
   12244       for (s = abfd->sections; s; s = s->next)
   12245 	if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
   12246 	  break;
   12247 
   12248       if (s)
   12249 	{
   12250 	  struct elf_segment_map *options_segment;
   12251 
   12252 	  pm = &elf_seg_map (abfd);
   12253 	  while (*pm != NULL
   12254 		 && ((*pm)->p_type == PT_PHDR
   12255 		     || (*pm)->p_type == PT_INTERP))
   12256 	    pm = &(*pm)->next;
   12257 
   12258 	  if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
   12259 	    {
   12260 	      amt = sizeof (struct elf_segment_map);
   12261 	      options_segment = bfd_zalloc (abfd, amt);
   12262 	      options_segment->next = *pm;
   12263 	      options_segment->p_type = PT_MIPS_OPTIONS;
   12264 	      options_segment->p_flags = PF_R;
   12265 	      options_segment->p_flags_valid = TRUE;
   12266 	      options_segment->count = 1;
   12267 	      options_segment->sections[0] = s;
   12268 	      *pm = options_segment;
   12269 	    }
   12270 	}
   12271     }
   12272   else
   12273     {
   12274       if (IRIX_COMPAT (abfd) == ict_irix5)
   12275 	{
   12276 	  /* If there are .dynamic and .mdebug sections, we make a room
   12277 	     for the RTPROC header.  FIXME: Rewrite without section names.  */
   12278 	  if (bfd_get_section_by_name (abfd, ".interp") == NULL
   12279 	      && bfd_get_section_by_name (abfd, ".dynamic") != NULL
   12280 	      && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
   12281 	    {
   12282 	      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   12283 		if (m->p_type == PT_MIPS_RTPROC)
   12284 		  break;
   12285 	      if (m == NULL)
   12286 		{
   12287 		  amt = sizeof *m;
   12288 		  m = bfd_zalloc (abfd, amt);
   12289 		  if (m == NULL)
   12290 		    return FALSE;
   12291 
   12292 		  m->p_type = PT_MIPS_RTPROC;
   12293 
   12294 		  s = bfd_get_section_by_name (abfd, ".rtproc");
   12295 		  if (s == NULL)
   12296 		    {
   12297 		      m->count = 0;
   12298 		      m->p_flags = 0;
   12299 		      m->p_flags_valid = 1;
   12300 		    }
   12301 		  else
   12302 		    {
   12303 		      m->count = 1;
   12304 		      m->sections[0] = s;
   12305 		    }
   12306 
   12307 		  /* We want to put it after the DYNAMIC segment.  */
   12308 		  pm = &elf_seg_map (abfd);
   12309 		  while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
   12310 		    pm = &(*pm)->next;
   12311 		  if (*pm != NULL)
   12312 		    pm = &(*pm)->next;
   12313 
   12314 		  m->next = *pm;
   12315 		  *pm = m;
   12316 		}
   12317 	    }
   12318 	}
   12319       /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
   12320 	 .dynstr, .dynsym, and .hash sections, and everything in
   12321 	 between.  */
   12322       for (pm = &elf_seg_map (abfd); *pm != NULL;
   12323 	   pm = &(*pm)->next)
   12324 	if ((*pm)->p_type == PT_DYNAMIC)
   12325 	  break;
   12326       m = *pm;
   12327       /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
   12328 	 glibc's dynamic linker has traditionally derived the number of
   12329 	 tags from the p_filesz field, and sometimes allocates stack
   12330 	 arrays of that size.  An overly-big PT_DYNAMIC segment can
   12331 	 be actively harmful in such cases.  Making PT_DYNAMIC contain
   12332 	 other sections can also make life hard for the prelinker,
   12333 	 which might move one of the other sections to a different
   12334 	 PT_LOAD segment.  */
   12335       if (SGI_COMPAT (abfd)
   12336 	  && m != NULL
   12337 	  && m->count == 1
   12338 	  && strcmp (m->sections[0]->name, ".dynamic") == 0)
   12339 	{
   12340 	  static const char *sec_names[] =
   12341 	  {
   12342 	    ".dynamic", ".dynstr", ".dynsym", ".hash"
   12343 	  };
   12344 	  bfd_vma low, high;
   12345 	  unsigned int i, c;
   12346 	  struct elf_segment_map *n;
   12347 
   12348 	  low = ~(bfd_vma) 0;
   12349 	  high = 0;
   12350 	  for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
   12351 	    {
   12352 	      s = bfd_get_section_by_name (abfd, sec_names[i]);
   12353 	      if (s != NULL && (s->flags & SEC_LOAD) != 0)
   12354 		{
   12355 		  bfd_size_type sz;
   12356 
   12357 		  if (low > s->vma)
   12358 		    low = s->vma;
   12359 		  sz = s->size;
   12360 		  if (high < s->vma + sz)
   12361 		    high = s->vma + sz;
   12362 		}
   12363 	    }
   12364 
   12365 	  c = 0;
   12366 	  for (s = abfd->sections; s != NULL; s = s->next)
   12367 	    if ((s->flags & SEC_LOAD) != 0
   12368 		&& s->vma >= low
   12369 		&& s->vma + s->size <= high)
   12370 	      ++c;
   12371 
   12372 	  amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
   12373 	  n = bfd_zalloc (abfd, amt);
   12374 	  if (n == NULL)
   12375 	    return FALSE;
   12376 	  *n = *m;
   12377 	  n->count = c;
   12378 
   12379 	  i = 0;
   12380 	  for (s = abfd->sections; s != NULL; s = s->next)
   12381 	    {
   12382 	      if ((s->flags & SEC_LOAD) != 0
   12383 		  && s->vma >= low
   12384 		  && s->vma + s->size <= high)
   12385 		{
   12386 		  n->sections[i] = s;
   12387 		  ++i;
   12388 		}
   12389 	    }
   12390 
   12391 	  *pm = n;
   12392 	}
   12393     }
   12394 
   12395   /* Allocate a spare program header in dynamic objects so that tools
   12396      like the prelinker can add an extra PT_LOAD entry.
   12397 
   12398      If the prelinker needs to make room for a new PT_LOAD entry, its
   12399      standard procedure is to move the first (read-only) sections into
   12400      the new (writable) segment.  However, the MIPS ABI requires
   12401      .dynamic to be in a read-only segment, and the section will often
   12402      start within sizeof (ElfNN_Phdr) bytes of the last program header.
   12403 
   12404      Although the prelinker could in principle move .dynamic to a
   12405      writable segment, it seems better to allocate a spare program
   12406      header instead, and avoid the need to move any sections.
   12407      There is a long tradition of allocating spare dynamic tags,
   12408      so allocating a spare program header seems like a natural
   12409      extension.
   12410 
   12411      If INFO is NULL, we may be copying an already prelinked binary
   12412      with objcopy or strip, so do not add this header.  */
   12413   if (info != NULL
   12414       && !SGI_COMPAT (abfd)
   12415       && bfd_get_section_by_name (abfd, ".dynamic"))
   12416     {
   12417       for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
   12418 	if ((*pm)->p_type == PT_NULL)
   12419 	  break;
   12420       if (*pm == NULL)
   12421 	{
   12422 	  m = bfd_zalloc (abfd, sizeof (*m));
   12423 	  if (m == NULL)
   12424 	    return FALSE;
   12425 
   12426 	  m->p_type = PT_NULL;
   12427 	  *pm = m;
   12428 	}
   12429     }
   12430 
   12431   return TRUE;
   12432 }
   12433 
   12434 /* Return the section that should be marked against GC for a given
   12436    relocation.  */
   12437 
   12438 asection *
   12439 _bfd_mips_elf_gc_mark_hook (asection *sec,
   12440 			    struct bfd_link_info *info,
   12441 			    Elf_Internal_Rela *rel,
   12442 			    struct elf_link_hash_entry *h,
   12443 			    Elf_Internal_Sym *sym)
   12444 {
   12445   /* ??? Do mips16 stub sections need to be handled special?  */
   12446 
   12447   if (h != NULL)
   12448     switch (ELF_R_TYPE (sec->owner, rel->r_info))
   12449       {
   12450       case R_MIPS_GNU_VTINHERIT:
   12451       case R_MIPS_GNU_VTENTRY:
   12452 	return NULL;
   12453       }
   12454 
   12455   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   12456 }
   12457 
   12458 /* Update the got entry reference counts for the section being removed.  */
   12459 
   12460 bfd_boolean
   12461 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
   12462 			     struct bfd_link_info *info ATTRIBUTE_UNUSED,
   12463 			     asection *sec ATTRIBUTE_UNUSED,
   12464 			     const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
   12465 {
   12466 #if 0
   12467   Elf_Internal_Shdr *symtab_hdr;
   12468   struct elf_link_hash_entry **sym_hashes;
   12469   bfd_signed_vma *local_got_refcounts;
   12470   const Elf_Internal_Rela *rel, *relend;
   12471   unsigned long r_symndx;
   12472   struct elf_link_hash_entry *h;
   12473 
   12474   if (bfd_link_relocatable (info))
   12475     return TRUE;
   12476 
   12477   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12478   sym_hashes = elf_sym_hashes (abfd);
   12479   local_got_refcounts = elf_local_got_refcounts (abfd);
   12480 
   12481   relend = relocs + sec->reloc_count;
   12482   for (rel = relocs; rel < relend; rel++)
   12483     switch (ELF_R_TYPE (abfd, rel->r_info))
   12484       {
   12485       case R_MIPS16_GOT16:
   12486       case R_MIPS16_CALL16:
   12487       case R_MIPS_GOT16:
   12488       case R_MIPS_CALL16:
   12489       case R_MIPS_CALL_HI16:
   12490       case R_MIPS_CALL_LO16:
   12491       case R_MIPS_GOT_HI16:
   12492       case R_MIPS_GOT_LO16:
   12493       case R_MIPS_GOT_DISP:
   12494       case R_MIPS_GOT_PAGE:
   12495       case R_MIPS_GOT_OFST:
   12496       case R_MICROMIPS_GOT16:
   12497       case R_MICROMIPS_CALL16:
   12498       case R_MICROMIPS_CALL_HI16:
   12499       case R_MICROMIPS_CALL_LO16:
   12500       case R_MICROMIPS_GOT_HI16:
   12501       case R_MICROMIPS_GOT_LO16:
   12502       case R_MICROMIPS_GOT_DISP:
   12503       case R_MICROMIPS_GOT_PAGE:
   12504       case R_MICROMIPS_GOT_OFST:
   12505 	/* ??? It would seem that the existing MIPS code does no sort
   12506 	   of reference counting or whatnot on its GOT and PLT entries,
   12507 	   so it is not possible to garbage collect them at this time.  */
   12508 	break;
   12509 
   12510       default:
   12511 	break;
   12512       }
   12513 #endif
   12514 
   12515   return TRUE;
   12516 }
   12517 
   12518 /* Prevent .MIPS.abiflags from being discarded with --gc-sections.  */
   12519 
   12520 bfd_boolean
   12521 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
   12522 				      elf_gc_mark_hook_fn gc_mark_hook)
   12523 {
   12524   bfd *sub;
   12525 
   12526   _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
   12527 
   12528   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   12529     {
   12530       asection *o;
   12531 
   12532       if (! is_mips_elf (sub))
   12533 	continue;
   12534 
   12535       for (o = sub->sections; o != NULL; o = o->next)
   12536 	if (!o->gc_mark
   12537 	    && MIPS_ELF_ABIFLAGS_SECTION_NAME_P
   12538 		 (bfd_get_section_name (sub, o)))
   12539 	  {
   12540 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
   12541 	      return FALSE;
   12542 	  }
   12543     }
   12544 
   12545   return TRUE;
   12546 }
   12547 
   12548 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
   12550    hiding the old indirect symbol.  Process additional relocation
   12551    information.  Also called for weakdefs, in which case we just let
   12552    _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
   12553 
   12554 void
   12555 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
   12556 				    struct elf_link_hash_entry *dir,
   12557 				    struct elf_link_hash_entry *ind)
   12558 {
   12559   struct mips_elf_link_hash_entry *dirmips, *indmips;
   12560 
   12561   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   12562 
   12563   dirmips = (struct mips_elf_link_hash_entry *) dir;
   12564   indmips = (struct mips_elf_link_hash_entry *) ind;
   12565   /* Any absolute non-dynamic relocations against an indirect or weak
   12566      definition will be against the target symbol.  */
   12567   if (indmips->has_static_relocs)
   12568     dirmips->has_static_relocs = TRUE;
   12569 
   12570   if (ind->root.type != bfd_link_hash_indirect)
   12571     return;
   12572 
   12573   dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
   12574   if (indmips->readonly_reloc)
   12575     dirmips->readonly_reloc = TRUE;
   12576   if (indmips->no_fn_stub)
   12577     dirmips->no_fn_stub = TRUE;
   12578   if (indmips->fn_stub)
   12579     {
   12580       dirmips->fn_stub = indmips->fn_stub;
   12581       indmips->fn_stub = NULL;
   12582     }
   12583   if (indmips->need_fn_stub)
   12584     {
   12585       dirmips->need_fn_stub = TRUE;
   12586       indmips->need_fn_stub = FALSE;
   12587     }
   12588   if (indmips->call_stub)
   12589     {
   12590       dirmips->call_stub = indmips->call_stub;
   12591       indmips->call_stub = NULL;
   12592     }
   12593   if (indmips->call_fp_stub)
   12594     {
   12595       dirmips->call_fp_stub = indmips->call_fp_stub;
   12596       indmips->call_fp_stub = NULL;
   12597     }
   12598   if (indmips->global_got_area < dirmips->global_got_area)
   12599     dirmips->global_got_area = indmips->global_got_area;
   12600   if (indmips->global_got_area < GGA_NONE)
   12601     indmips->global_got_area = GGA_NONE;
   12602   if (indmips->has_nonpic_branches)
   12603     dirmips->has_nonpic_branches = TRUE;
   12604 }
   12605 
   12606 #define PDR_SIZE 32
   12608 
   12609 bfd_boolean
   12610 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
   12611 			    struct bfd_link_info *info)
   12612 {
   12613   asection *o;
   12614   bfd_boolean ret = FALSE;
   12615   unsigned char *tdata;
   12616   size_t i, skip;
   12617 
   12618   o = bfd_get_section_by_name (abfd, ".pdr");
   12619   if (! o)
   12620     return FALSE;
   12621   if (o->size == 0)
   12622     return FALSE;
   12623   if (o->size % PDR_SIZE != 0)
   12624     return FALSE;
   12625   if (o->output_section != NULL
   12626       && bfd_is_abs_section (o->output_section))
   12627     return FALSE;
   12628 
   12629   tdata = bfd_zmalloc (o->size / PDR_SIZE);
   12630   if (! tdata)
   12631     return FALSE;
   12632 
   12633   cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   12634 					    info->keep_memory);
   12635   if (!cookie->rels)
   12636     {
   12637       free (tdata);
   12638       return FALSE;
   12639     }
   12640 
   12641   cookie->rel = cookie->rels;
   12642   cookie->relend = cookie->rels + o->reloc_count;
   12643 
   12644   for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
   12645     {
   12646       if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
   12647 	{
   12648 	  tdata[i] = 1;
   12649 	  skip ++;
   12650 	}
   12651     }
   12652 
   12653   if (skip != 0)
   12654     {
   12655       mips_elf_section_data (o)->u.tdata = tdata;
   12656       if (o->rawsize == 0)
   12657 	o->rawsize = o->size;
   12658       o->size -= skip * PDR_SIZE;
   12659       ret = TRUE;
   12660     }
   12661   else
   12662     free (tdata);
   12663 
   12664   if (! info->keep_memory)
   12665     free (cookie->rels);
   12666 
   12667   return ret;
   12668 }
   12669 
   12670 bfd_boolean
   12671 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
   12672 {
   12673   if (strcmp (sec->name, ".pdr") == 0)
   12674     return TRUE;
   12675   return FALSE;
   12676 }
   12677 
   12678 bfd_boolean
   12679 _bfd_mips_elf_write_section (bfd *output_bfd,
   12680 			     struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   12681                              asection *sec, bfd_byte *contents)
   12682 {
   12683   bfd_byte *to, *from, *end;
   12684   int i;
   12685 
   12686   if (strcmp (sec->name, ".pdr") != 0)
   12687     return FALSE;
   12688 
   12689   if (mips_elf_section_data (sec)->u.tdata == NULL)
   12690     return FALSE;
   12691 
   12692   to = contents;
   12693   end = contents + sec->size;
   12694   for (from = contents, i = 0;
   12695        from < end;
   12696        from += PDR_SIZE, i++)
   12697     {
   12698       if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
   12699 	continue;
   12700       if (to != from)
   12701 	memcpy (to, from, PDR_SIZE);
   12702       to += PDR_SIZE;
   12703     }
   12704   bfd_set_section_contents (output_bfd, sec->output_section, contents,
   12705 			    sec->output_offset, sec->size);
   12706   return TRUE;
   12707 }
   12708 
   12709 /* microMIPS code retains local labels for linker relaxation.  Omit them
   12711    from output by default for clarity.  */
   12712 
   12713 bfd_boolean
   12714 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
   12715 {
   12716   return _bfd_elf_is_local_label_name (abfd, sym->name);
   12717 }
   12718 
   12719 /* MIPS ELF uses a special find_nearest_line routine in order the
   12720    handle the ECOFF debugging information.  */
   12721 
   12722 struct mips_elf_find_line
   12723 {
   12724   struct ecoff_debug_info d;
   12725   struct ecoff_find_line i;
   12726 };
   12727 
   12728 bfd_boolean
   12729 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
   12730 				 asection *section, bfd_vma offset,
   12731 				 const char **filename_ptr,
   12732 				 const char **functionname_ptr,
   12733 				 unsigned int *line_ptr,
   12734 				 unsigned int *discriminator_ptr)
   12735 {
   12736   asection *msec;
   12737 
   12738   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   12739 				     filename_ptr, functionname_ptr,
   12740 				     line_ptr, discriminator_ptr,
   12741 				     dwarf_debug_sections,
   12742 				     ABI_64_P (abfd) ? 8 : 0,
   12743 				     &elf_tdata (abfd)->dwarf2_find_line_info))
   12744     return TRUE;
   12745 
   12746   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   12747 				     filename_ptr, functionname_ptr,
   12748 				     line_ptr))
   12749     return TRUE;
   12750 
   12751   msec = bfd_get_section_by_name (abfd, ".mdebug");
   12752   if (msec != NULL)
   12753     {
   12754       flagword origflags;
   12755       struct mips_elf_find_line *fi;
   12756       const struct ecoff_debug_swap * const swap =
   12757 	get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   12758 
   12759       /* If we are called during a link, mips_elf_final_link may have
   12760 	 cleared the SEC_HAS_CONTENTS field.  We force it back on here
   12761 	 if appropriate (which it normally will be).  */
   12762       origflags = msec->flags;
   12763       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
   12764 	msec->flags |= SEC_HAS_CONTENTS;
   12765 
   12766       fi = mips_elf_tdata (abfd)->find_line_info;
   12767       if (fi == NULL)
   12768 	{
   12769 	  bfd_size_type external_fdr_size;
   12770 	  char *fraw_src;
   12771 	  char *fraw_end;
   12772 	  struct fdr *fdr_ptr;
   12773 	  bfd_size_type amt = sizeof (struct mips_elf_find_line);
   12774 
   12775 	  fi = bfd_zalloc (abfd, amt);
   12776 	  if (fi == NULL)
   12777 	    {
   12778 	      msec->flags = origflags;
   12779 	      return FALSE;
   12780 	    }
   12781 
   12782 	  if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
   12783 	    {
   12784 	      msec->flags = origflags;
   12785 	      return FALSE;
   12786 	    }
   12787 
   12788 	  /* Swap in the FDR information.  */
   12789 	  amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
   12790 	  fi->d.fdr = bfd_alloc (abfd, amt);
   12791 	  if (fi->d.fdr == NULL)
   12792 	    {
   12793 	      msec->flags = origflags;
   12794 	      return FALSE;
   12795 	    }
   12796 	  external_fdr_size = swap->external_fdr_size;
   12797 	  fdr_ptr = fi->d.fdr;
   12798 	  fraw_src = (char *) fi->d.external_fdr;
   12799 	  fraw_end = (fraw_src
   12800 		      + fi->d.symbolic_header.ifdMax * external_fdr_size);
   12801 	  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
   12802 	    (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
   12803 
   12804 	  mips_elf_tdata (abfd)->find_line_info = fi;
   12805 
   12806 	  /* Note that we don't bother to ever free this information.
   12807              find_nearest_line is either called all the time, as in
   12808              objdump -l, so the information should be saved, or it is
   12809              rarely called, as in ld error messages, so the memory
   12810              wasted is unimportant.  Still, it would probably be a
   12811              good idea for free_cached_info to throw it away.  */
   12812 	}
   12813 
   12814       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
   12815 				  &fi->i, filename_ptr, functionname_ptr,
   12816 				  line_ptr))
   12817 	{
   12818 	  msec->flags = origflags;
   12819 	  return TRUE;
   12820 	}
   12821 
   12822       msec->flags = origflags;
   12823     }
   12824 
   12825   /* Fall back on the generic ELF find_nearest_line routine.  */
   12826 
   12827   return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
   12828 				     filename_ptr, functionname_ptr,
   12829 				     line_ptr, discriminator_ptr);
   12830 }
   12831 
   12832 bfd_boolean
   12833 _bfd_mips_elf_find_inliner_info (bfd *abfd,
   12834 				 const char **filename_ptr,
   12835 				 const char **functionname_ptr,
   12836 				 unsigned int *line_ptr)
   12837 {
   12838   bfd_boolean found;
   12839   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   12840 					 functionname_ptr, line_ptr,
   12841 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   12842   return found;
   12843 }
   12844 
   12845 
   12846 /* When are writing out the .options or .MIPS.options section,
   12848    remember the bytes we are writing out, so that we can install the
   12849    GP value in the section_processing routine.  */
   12850 
   12851 bfd_boolean
   12852 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
   12853 				    const void *location,
   12854 				    file_ptr offset, bfd_size_type count)
   12855 {
   12856   if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
   12857     {
   12858       bfd_byte *c;
   12859 
   12860       if (elf_section_data (section) == NULL)
   12861 	{
   12862 	  bfd_size_type amt = sizeof (struct bfd_elf_section_data);
   12863 	  section->used_by_bfd = bfd_zalloc (abfd, amt);
   12864 	  if (elf_section_data (section) == NULL)
   12865 	    return FALSE;
   12866 	}
   12867       c = mips_elf_section_data (section)->u.tdata;
   12868       if (c == NULL)
   12869 	{
   12870 	  c = bfd_zalloc (abfd, section->size);
   12871 	  if (c == NULL)
   12872 	    return FALSE;
   12873 	  mips_elf_section_data (section)->u.tdata = c;
   12874 	}
   12875 
   12876       memcpy (c + offset, location, count);
   12877     }
   12878 
   12879   return _bfd_elf_set_section_contents (abfd, section, location, offset,
   12880 					count);
   12881 }
   12882 
   12883 /* This is almost identical to bfd_generic_get_... except that some
   12884    MIPS relocations need to be handled specially.  Sigh.  */
   12885 
   12886 bfd_byte *
   12887 _bfd_elf_mips_get_relocated_section_contents
   12888   (bfd *abfd,
   12889    struct bfd_link_info *link_info,
   12890    struct bfd_link_order *link_order,
   12891    bfd_byte *data,
   12892    bfd_boolean relocatable,
   12893    asymbol **symbols)
   12894 {
   12895   /* Get enough memory to hold the stuff */
   12896   bfd *input_bfd = link_order->u.indirect.section->owner;
   12897   asection *input_section = link_order->u.indirect.section;
   12898   bfd_size_type sz;
   12899 
   12900   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
   12901   arelent **reloc_vector = NULL;
   12902   long reloc_count;
   12903 
   12904   if (reloc_size < 0)
   12905     goto error_return;
   12906 
   12907   reloc_vector = bfd_malloc (reloc_size);
   12908   if (reloc_vector == NULL && reloc_size != 0)
   12909     goto error_return;
   12910 
   12911   /* read in the section */
   12912   sz = input_section->rawsize ? input_section->rawsize : input_section->size;
   12913   if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
   12914     goto error_return;
   12915 
   12916   reloc_count = bfd_canonicalize_reloc (input_bfd,
   12917 					input_section,
   12918 					reloc_vector,
   12919 					symbols);
   12920   if (reloc_count < 0)
   12921     goto error_return;
   12922 
   12923   if (reloc_count > 0)
   12924     {
   12925       arelent **parent;
   12926       /* for mips */
   12927       int gp_found;
   12928       bfd_vma gp = 0x12345678;	/* initialize just to shut gcc up */
   12929 
   12930       {
   12931 	struct bfd_hash_entry *h;
   12932 	struct bfd_link_hash_entry *lh;
   12933 	/* Skip all this stuff if we aren't mixing formats.  */
   12934 	if (abfd && input_bfd
   12935 	    && abfd->xvec == input_bfd->xvec)
   12936 	  lh = 0;
   12937 	else
   12938 	  {
   12939 	    h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
   12940 	    lh = (struct bfd_link_hash_entry *) h;
   12941 	  }
   12942       lookup:
   12943 	if (lh)
   12944 	  {
   12945 	    switch (lh->type)
   12946 	      {
   12947 	      case bfd_link_hash_undefined:
   12948 	      case bfd_link_hash_undefweak:
   12949 	      case bfd_link_hash_common:
   12950 		gp_found = 0;
   12951 		break;
   12952 	      case bfd_link_hash_defined:
   12953 	      case bfd_link_hash_defweak:
   12954 		gp_found = 1;
   12955 		gp = lh->u.def.value;
   12956 		break;
   12957 	      case bfd_link_hash_indirect:
   12958 	      case bfd_link_hash_warning:
   12959 		lh = lh->u.i.link;
   12960 		/* @@FIXME  ignoring warning for now */
   12961 		goto lookup;
   12962 	      case bfd_link_hash_new:
   12963 	      default:
   12964 		abort ();
   12965 	      }
   12966 	  }
   12967 	else
   12968 	  gp_found = 0;
   12969       }
   12970       /* end mips */
   12971       for (parent = reloc_vector; *parent != NULL; parent++)
   12972 	{
   12973 	  char *error_message = NULL;
   12974 	  bfd_reloc_status_type r;
   12975 
   12976 	  /* Specific to MIPS: Deal with relocation types that require
   12977 	     knowing the gp of the output bfd.  */
   12978 	  asymbol *sym = *(*parent)->sym_ptr_ptr;
   12979 
   12980 	  /* If we've managed to find the gp and have a special
   12981 	     function for the relocation then go ahead, else default
   12982 	     to the generic handling.  */
   12983 	  if (gp_found
   12984 	      && (*parent)->howto->special_function
   12985 	      == _bfd_mips_elf32_gprel16_reloc)
   12986 	    r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
   12987 					       input_section, relocatable,
   12988 					       data, gp);
   12989 	  else
   12990 	    r = bfd_perform_relocation (input_bfd, *parent, data,
   12991 					input_section,
   12992 					relocatable ? abfd : NULL,
   12993 					&error_message);
   12994 
   12995 	  if (relocatable)
   12996 	    {
   12997 	      asection *os = input_section->output_section;
   12998 
   12999 	      /* A partial link, so keep the relocs */
   13000 	      os->orelocation[os->reloc_count] = *parent;
   13001 	      os->reloc_count++;
   13002 	    }
   13003 
   13004 	  if (r != bfd_reloc_ok)
   13005 	    {
   13006 	      switch (r)
   13007 		{
   13008 		case bfd_reloc_undefined:
   13009 		  (*link_info->callbacks->undefined_symbol)
   13010 		    (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
   13011 		     input_bfd, input_section, (*parent)->address, TRUE);
   13012 		  break;
   13013 		case bfd_reloc_dangerous:
   13014 		  BFD_ASSERT (error_message != NULL);
   13015 		  (*link_info->callbacks->reloc_dangerous)
   13016 		    (link_info, error_message,
   13017 		     input_bfd, input_section, (*parent)->address);
   13018 		  break;
   13019 		case bfd_reloc_overflow:
   13020 		  (*link_info->callbacks->reloc_overflow)
   13021 		    (link_info, NULL,
   13022 		     bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
   13023 		     (*parent)->howto->name, (*parent)->addend,
   13024 		     input_bfd, input_section, (*parent)->address);
   13025 		  break;
   13026 		case bfd_reloc_outofrange:
   13027 		default:
   13028 		  abort ();
   13029 		  break;
   13030 		}
   13031 
   13032 	    }
   13033 	}
   13034     }
   13035   if (reloc_vector != NULL)
   13036     free (reloc_vector);
   13037   return data;
   13038 
   13039 error_return:
   13040   if (reloc_vector != NULL)
   13041     free (reloc_vector);
   13042   return NULL;
   13043 }
   13044 
   13045 static bfd_boolean
   13047 mips_elf_relax_delete_bytes (bfd *abfd,
   13048 			     asection *sec, bfd_vma addr, int count)
   13049 {
   13050   Elf_Internal_Shdr *symtab_hdr;
   13051   unsigned int sec_shndx;
   13052   bfd_byte *contents;
   13053   Elf_Internal_Rela *irel, *irelend;
   13054   Elf_Internal_Sym *isym;
   13055   Elf_Internal_Sym *isymend;
   13056   struct elf_link_hash_entry **sym_hashes;
   13057   struct elf_link_hash_entry **end_hashes;
   13058   struct elf_link_hash_entry **start_hashes;
   13059   unsigned int symcount;
   13060 
   13061   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   13062   contents = elf_section_data (sec)->this_hdr.contents;
   13063 
   13064   irel = elf_section_data (sec)->relocs;
   13065   irelend = irel + sec->reloc_count;
   13066 
   13067   /* Actually delete the bytes.  */
   13068   memmove (contents + addr, contents + addr + count,
   13069 	   (size_t) (sec->size - addr - count));
   13070   sec->size -= count;
   13071 
   13072   /* Adjust all the relocs.  */
   13073   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
   13074     {
   13075       /* Get the new reloc address.  */
   13076       if (irel->r_offset > addr)
   13077 	irel->r_offset -= count;
   13078     }
   13079 
   13080   BFD_ASSERT (addr % 2 == 0);
   13081   BFD_ASSERT (count % 2 == 0);
   13082 
   13083   /* Adjust the local symbols defined in this section.  */
   13084   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13085   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
   13086   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
   13087     if (isym->st_shndx == sec_shndx && isym->st_value > addr)
   13088       isym->st_value -= count;
   13089 
   13090   /* Now adjust the global symbols defined in this section.  */
   13091   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   13092 	      - symtab_hdr->sh_info);
   13093   sym_hashes = start_hashes = elf_sym_hashes (abfd);
   13094   end_hashes = sym_hashes + symcount;
   13095 
   13096   for (; sym_hashes < end_hashes; sym_hashes++)
   13097     {
   13098       struct elf_link_hash_entry *sym_hash = *sym_hashes;
   13099 
   13100       if ((sym_hash->root.type == bfd_link_hash_defined
   13101 	   || sym_hash->root.type == bfd_link_hash_defweak)
   13102 	  && sym_hash->root.u.def.section == sec)
   13103 	{
   13104 	  bfd_vma value = sym_hash->root.u.def.value;
   13105 
   13106 	  if (ELF_ST_IS_MICROMIPS (sym_hash->other))
   13107 	    value &= MINUS_TWO;
   13108 	  if (value > addr)
   13109 	    sym_hash->root.u.def.value -= count;
   13110 	}
   13111     }
   13112 
   13113   return TRUE;
   13114 }
   13115 
   13116 
   13117 /* Opcodes needed for microMIPS relaxation as found in
   13118    opcodes/micromips-opc.c.  */
   13119 
   13120 struct opcode_descriptor {
   13121   unsigned long match;
   13122   unsigned long mask;
   13123 };
   13124 
   13125 /* The $ra register aka $31.  */
   13126 
   13127 #define RA 31
   13128 
   13129 /* 32-bit instruction format register fields.  */
   13130 
   13131 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
   13132 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
   13133 
   13134 /* Check if a 5-bit register index can be abbreviated to 3 bits.  */
   13135 
   13136 #define OP16_VALID_REG(r) \
   13137   ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
   13138 
   13139 
   13140 /* 32-bit and 16-bit branches.  */
   13141 
   13142 static const struct opcode_descriptor b_insns_32[] = {
   13143   { /* "b",	"p",		*/ 0x40400000, 0xffff0000 }, /* bgez 0 */
   13144   { /* "b",	"p",		*/ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
   13145   { 0, 0 }  /* End marker for find_match().  */
   13146 };
   13147 
   13148 static const struct opcode_descriptor bc_insn_32 =
   13149   { /* "bc(1|2)(ft)", "N,p",	*/ 0x42800000, 0xfec30000 };
   13150 
   13151 static const struct opcode_descriptor bz_insn_32 =
   13152   { /* "b(g|l)(e|t)z", "s,p",	*/ 0x40000000, 0xff200000 };
   13153 
   13154 static const struct opcode_descriptor bzal_insn_32 =
   13155   { /* "b(ge|lt)zal", "s,p",	*/ 0x40200000, 0xffa00000 };
   13156 
   13157 static const struct opcode_descriptor beq_insn_32 =
   13158   { /* "b(eq|ne)", "s,t,p",	*/ 0x94000000, 0xdc000000 };
   13159 
   13160 static const struct opcode_descriptor b_insn_16 =
   13161   { /* "b",	"mD",		*/ 0xcc00,     0xfc00 };
   13162 
   13163 static const struct opcode_descriptor bz_insn_16 =
   13164   { /* "b(eq|ne)z", "md,mE",	*/ 0x8c00,     0xdc00 };
   13165 
   13166 
   13167 /* 32-bit and 16-bit branch EQ and NE zero.  */
   13168 
   13169 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
   13170    eq and second the ne.  This convention is used when replacing a
   13171    32-bit BEQ/BNE with the 16-bit version.  */
   13172 
   13173 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
   13174 
   13175 static const struct opcode_descriptor bz_rs_insns_32[] = {
   13176   { /* "beqz",	"s,p",		*/ 0x94000000, 0xffe00000 },
   13177   { /* "bnez",	"s,p",		*/ 0xb4000000, 0xffe00000 },
   13178   { 0, 0 }  /* End marker for find_match().  */
   13179 };
   13180 
   13181 static const struct opcode_descriptor bz_rt_insns_32[] = {
   13182   { /* "beqz",	"t,p",		*/ 0x94000000, 0xfc01f000 },
   13183   { /* "bnez",	"t,p",		*/ 0xb4000000, 0xfc01f000 },
   13184   { 0, 0 }  /* End marker for find_match().  */
   13185 };
   13186 
   13187 static const struct opcode_descriptor bzc_insns_32[] = {
   13188   { /* "beqzc",	"s,p",		*/ 0x40e00000, 0xffe00000 },
   13189   { /* "bnezc",	"s,p",		*/ 0x40a00000, 0xffe00000 },
   13190   { 0, 0 }  /* End marker for find_match().  */
   13191 };
   13192 
   13193 static const struct opcode_descriptor bz_insns_16[] = {
   13194   { /* "beqz",	"md,mE",	*/ 0x8c00,     0xfc00 },
   13195   { /* "bnez",	"md,mE",	*/ 0xac00,     0xfc00 },
   13196   { 0, 0 }  /* End marker for find_match().  */
   13197 };
   13198 
   13199 /* Switch between a 5-bit register index and its 3-bit shorthand.  */
   13200 
   13201 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
   13202 #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
   13203 
   13204 
   13205 /* 32-bit instructions with a delay slot.  */
   13206 
   13207 static const struct opcode_descriptor jal_insn_32_bd16 =
   13208   { /* "jals",	"a",		*/ 0x74000000, 0xfc000000 };
   13209 
   13210 static const struct opcode_descriptor jal_insn_32_bd32 =
   13211   { /* "jal",	"a",		*/ 0xf4000000, 0xfc000000 };
   13212 
   13213 static const struct opcode_descriptor jal_x_insn_32_bd32 =
   13214   { /* "jal[x]", "a",		*/ 0xf0000000, 0xf8000000 };
   13215 
   13216 static const struct opcode_descriptor j_insn_32 =
   13217   { /* "j",	"a",		*/ 0xd4000000, 0xfc000000 };
   13218 
   13219 static const struct opcode_descriptor jalr_insn_32 =
   13220   { /* "jalr[.hb]", "t,s",	*/ 0x00000f3c, 0xfc00efff };
   13221 
   13222 /* This table can be compacted, because no opcode replacement is made.  */
   13223 
   13224 static const struct opcode_descriptor ds_insns_32_bd16[] = {
   13225   { /* "jals",	"a",		*/ 0x74000000, 0xfc000000 },
   13226 
   13227   { /* "jalrs[.hb]", "t,s",	*/ 0x00004f3c, 0xfc00efff },
   13228   { /* "b(ge|lt)zals", "s,p",	*/ 0x42200000, 0xffa00000 },
   13229 
   13230   { /* "b(g|l)(e|t)z", "s,p",	*/ 0x40000000, 0xff200000 },
   13231   { /* "b(eq|ne)", "s,t,p",	*/ 0x94000000, 0xdc000000 },
   13232   { /* "j",	"a",		*/ 0xd4000000, 0xfc000000 },
   13233   { 0, 0 }  /* End marker for find_match().  */
   13234 };
   13235 
   13236 /* This table can be compacted, because no opcode replacement is made.  */
   13237 
   13238 static const struct opcode_descriptor ds_insns_32_bd32[] = {
   13239   { /* "jal[x]", "a",		*/ 0xf0000000, 0xf8000000 },
   13240 
   13241   { /* "jalr[.hb]", "t,s",	*/ 0x00000f3c, 0xfc00efff },
   13242   { /* "b(ge|lt)zal", "s,p",	*/ 0x40200000, 0xffa00000 },
   13243   { 0, 0 }  /* End marker for find_match().  */
   13244 };
   13245 
   13246 
   13247 /* 16-bit instructions with a delay slot.  */
   13248 
   13249 static const struct opcode_descriptor jalr_insn_16_bd16 =
   13250   { /* "jalrs",	"my,mj",	*/ 0x45e0,     0xffe0 };
   13251 
   13252 static const struct opcode_descriptor jalr_insn_16_bd32 =
   13253   { /* "jalr",	"my,mj",	*/ 0x45c0,     0xffe0 };
   13254 
   13255 static const struct opcode_descriptor jr_insn_16 =
   13256   { /* "jr",	"mj",		*/ 0x4580,     0xffe0 };
   13257 
   13258 #define JR16_REG(opcode) ((opcode) & 0x1f)
   13259 
   13260 /* This table can be compacted, because no opcode replacement is made.  */
   13261 
   13262 static const struct opcode_descriptor ds_insns_16_bd16[] = {
   13263   { /* "jalrs",	"my,mj",	*/ 0x45e0,     0xffe0 },
   13264 
   13265   { /* "b",	"mD",		*/ 0xcc00,     0xfc00 },
   13266   { /* "b(eq|ne)z", "md,mE",	*/ 0x8c00,     0xdc00 },
   13267   { /* "jr",	"mj",		*/ 0x4580,     0xffe0 },
   13268   { 0, 0 }  /* End marker for find_match().  */
   13269 };
   13270 
   13271 
   13272 /* LUI instruction.  */
   13273 
   13274 static const struct opcode_descriptor lui_insn =
   13275  { /* "lui",	"s,u",		*/ 0x41a00000, 0xffe00000 };
   13276 
   13277 
   13278 /* ADDIU instruction.  */
   13279 
   13280 static const struct opcode_descriptor addiu_insn =
   13281   { /* "addiu",	"t,r,j",	*/ 0x30000000, 0xfc000000 };
   13282 
   13283 static const struct opcode_descriptor addiupc_insn =
   13284   { /* "addiu",	"mb,$pc,mQ",	*/ 0x78000000, 0xfc000000 };
   13285 
   13286 #define ADDIUPC_REG_FIELD(r) \
   13287   (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
   13288 
   13289 
   13290 /* Relaxable instructions in a JAL delay slot: MOVE.  */
   13291 
   13292 /* The 16-bit move has rd in 9:5 and rs in 4:0.  The 32-bit moves
   13293    (ADDU, OR) have rd in 15:11 and rs in 10:16.  */
   13294 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
   13295 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
   13296 
   13297 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
   13298 #define MOVE16_RS_FIELD(r) (((r) & 0x1f)     )
   13299 
   13300 static const struct opcode_descriptor move_insns_32[] = {
   13301   { /* "move",	"d,s",		*/ 0x00000290, 0xffe007ff }, /* or   d,s,$0 */
   13302   { /* "move",	"d,s",		*/ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
   13303   { 0, 0 }  /* End marker for find_match().  */
   13304 };
   13305 
   13306 static const struct opcode_descriptor move_insn_16 =
   13307   { /* "move",	"mp,mj",	*/ 0x0c00,     0xfc00 };
   13308 
   13309 
   13310 /* NOP instructions.  */
   13311 
   13312 static const struct opcode_descriptor nop_insn_32 =
   13313   { /* "nop",	"",		*/ 0x00000000, 0xffffffff };
   13314 
   13315 static const struct opcode_descriptor nop_insn_16 =
   13316   { /* "nop",	"",		*/ 0x0c00,     0xffff };
   13317 
   13318 
   13319 /* Instruction match support.  */
   13320 
   13321 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
   13322 
   13323 static int
   13324 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
   13325 {
   13326   unsigned long indx;
   13327 
   13328   for (indx = 0; insn[indx].mask != 0; indx++)
   13329     if (MATCH (opcode, insn[indx]))
   13330       return indx;
   13331 
   13332   return -1;
   13333 }
   13334 
   13335 
   13336 /* Branch and delay slot decoding support.  */
   13337 
   13338 /* If PTR points to what *might* be a 16-bit branch or jump, then
   13339    return the minimum length of its delay slot, otherwise return 0.
   13340    Non-zero results are not definitive as we might be checking against
   13341    the second half of another instruction.  */
   13342 
   13343 static int
   13344 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
   13345 {
   13346   unsigned long opcode;
   13347   int bdsize;
   13348 
   13349   opcode = bfd_get_16 (abfd, ptr);
   13350   if (MATCH (opcode, jalr_insn_16_bd32) != 0)
   13351     /* 16-bit branch/jump with a 32-bit delay slot.  */
   13352     bdsize = 4;
   13353   else if (MATCH (opcode, jalr_insn_16_bd16) != 0
   13354 	   || find_match (opcode, ds_insns_16_bd16) >= 0)
   13355     /* 16-bit branch/jump with a 16-bit delay slot.  */
   13356     bdsize = 2;
   13357   else
   13358     /* No delay slot.  */
   13359     bdsize = 0;
   13360 
   13361   return bdsize;
   13362 }
   13363 
   13364 /* If PTR points to what *might* be a 32-bit branch or jump, then
   13365    return the minimum length of its delay slot, otherwise return 0.
   13366    Non-zero results are not definitive as we might be checking against
   13367    the second half of another instruction.  */
   13368 
   13369 static int
   13370 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
   13371 {
   13372   unsigned long opcode;
   13373   int bdsize;
   13374 
   13375   opcode = bfd_get_micromips_32 (abfd, ptr);
   13376   if (find_match (opcode, ds_insns_32_bd32) >= 0)
   13377     /* 32-bit branch/jump with a 32-bit delay slot.  */
   13378     bdsize = 4;
   13379   else if (find_match (opcode, ds_insns_32_bd16) >= 0)
   13380     /* 32-bit branch/jump with a 16-bit delay slot.  */
   13381     bdsize = 2;
   13382   else
   13383     /* No delay slot.  */
   13384     bdsize = 0;
   13385 
   13386   return bdsize;
   13387 }
   13388 
   13389 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
   13390    that doesn't fiddle with REG, then return TRUE, otherwise FALSE.  */
   13391 
   13392 static bfd_boolean
   13393 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
   13394 {
   13395   unsigned long opcode;
   13396 
   13397   opcode = bfd_get_16 (abfd, ptr);
   13398   if (MATCH (opcode, b_insn_16)
   13399 						/* B16  */
   13400       || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
   13401 						/* JR16  */
   13402       || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
   13403 						/* BEQZ16, BNEZ16  */
   13404       || (MATCH (opcode, jalr_insn_16_bd32)
   13405 						/* JALR16  */
   13406 	  && reg != JR16_REG (opcode) && reg != RA))
   13407     return TRUE;
   13408 
   13409   return FALSE;
   13410 }
   13411 
   13412 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
   13413    then return TRUE, otherwise FALSE.  */
   13414 
   13415 static bfd_boolean
   13416 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
   13417 {
   13418   unsigned long opcode;
   13419 
   13420   opcode = bfd_get_micromips_32 (abfd, ptr);
   13421   if (MATCH (opcode, j_insn_32)
   13422 						/* J  */
   13423       || MATCH (opcode, bc_insn_32)
   13424 						/* BC1F, BC1T, BC2F, BC2T  */
   13425       || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
   13426 						/* JAL, JALX  */
   13427       || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
   13428 						/* BGEZ, BGTZ, BLEZ, BLTZ  */
   13429       || (MATCH (opcode, bzal_insn_32)
   13430 						/* BGEZAL, BLTZAL  */
   13431 	  && reg != OP32_SREG (opcode) && reg != RA)
   13432       || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
   13433 						/* JALR, JALR.HB, BEQ, BNE  */
   13434 	  && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
   13435     return TRUE;
   13436 
   13437   return FALSE;
   13438 }
   13439 
   13440 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
   13441    IRELEND) at OFFSET indicate that there must be a compact branch there,
   13442    then return TRUE, otherwise FALSE.  */
   13443 
   13444 static bfd_boolean
   13445 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
   13446 		     const Elf_Internal_Rela *internal_relocs,
   13447 		     const Elf_Internal_Rela *irelend)
   13448 {
   13449   const Elf_Internal_Rela *irel;
   13450   unsigned long opcode;
   13451 
   13452   opcode = bfd_get_micromips_32 (abfd, ptr);
   13453   if (find_match (opcode, bzc_insns_32) < 0)
   13454     return FALSE;
   13455 
   13456   for (irel = internal_relocs; irel < irelend; irel++)
   13457     if (irel->r_offset == offset
   13458 	&& ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
   13459       return TRUE;
   13460 
   13461   return FALSE;
   13462 }
   13463 
   13464 /* Bitsize checking.  */
   13465 #define IS_BITSIZE(val, N)						\
   13466   (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1)))		\
   13467     - (1ULL << ((N) - 1))) == (val))
   13468 
   13469 
   13470 bfd_boolean
   13472 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
   13473 			     struct bfd_link_info *link_info,
   13474 			     bfd_boolean *again)
   13475 {
   13476   bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
   13477   Elf_Internal_Shdr *symtab_hdr;
   13478   Elf_Internal_Rela *internal_relocs;
   13479   Elf_Internal_Rela *irel, *irelend;
   13480   bfd_byte *contents = NULL;
   13481   Elf_Internal_Sym *isymbuf = NULL;
   13482 
   13483   /* Assume nothing changes.  */
   13484   *again = FALSE;
   13485 
   13486   /* We don't have to do anything for a relocatable link, if
   13487      this section does not have relocs, or if this is not a
   13488      code section.  */
   13489 
   13490   if (bfd_link_relocatable (link_info)
   13491       || (sec->flags & SEC_RELOC) == 0
   13492       || sec->reloc_count == 0
   13493       || (sec->flags & SEC_CODE) == 0)
   13494     return TRUE;
   13495 
   13496   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13497 
   13498   /* Get a copy of the native relocations.  */
   13499   internal_relocs = (_bfd_elf_link_read_relocs
   13500 		     (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
   13501 		      link_info->keep_memory));
   13502   if (internal_relocs == NULL)
   13503     goto error_return;
   13504 
   13505   /* Walk through them looking for relaxing opportunities.  */
   13506   irelend = internal_relocs + sec->reloc_count;
   13507   for (irel = internal_relocs; irel < irelend; irel++)
   13508     {
   13509       unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
   13510       unsigned int r_type = ELF32_R_TYPE (irel->r_info);
   13511       bfd_boolean target_is_micromips_code_p;
   13512       unsigned long opcode;
   13513       bfd_vma symval;
   13514       bfd_vma pcrval;
   13515       bfd_byte *ptr;
   13516       int fndopc;
   13517 
   13518       /* The number of bytes to delete for relaxation and from where
   13519          to delete these bytes starting at irel->r_offset.  */
   13520       int delcnt = 0;
   13521       int deloff = 0;
   13522 
   13523       /* If this isn't something that can be relaxed, then ignore
   13524          this reloc.  */
   13525       if (r_type != R_MICROMIPS_HI16
   13526 	  && r_type != R_MICROMIPS_PC16_S1
   13527 	  && r_type != R_MICROMIPS_26_S1)
   13528 	continue;
   13529 
   13530       /* Get the section contents if we haven't done so already.  */
   13531       if (contents == NULL)
   13532 	{
   13533 	  /* Get cached copy if it exists.  */
   13534 	  if (elf_section_data (sec)->this_hdr.contents != NULL)
   13535 	    contents = elf_section_data (sec)->this_hdr.contents;
   13536 	  /* Go get them off disk.  */
   13537 	  else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
   13538 	    goto error_return;
   13539 	}
   13540       ptr = contents + irel->r_offset;
   13541 
   13542       /* Read this BFD's local symbols if we haven't done so already.  */
   13543       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
   13544 	{
   13545 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   13546 	  if (isymbuf == NULL)
   13547 	    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   13548 					    symtab_hdr->sh_info, 0,
   13549 					    NULL, NULL, NULL);
   13550 	  if (isymbuf == NULL)
   13551 	    goto error_return;
   13552 	}
   13553 
   13554       /* Get the value of the symbol referred to by the reloc.  */
   13555       if (r_symndx < symtab_hdr->sh_info)
   13556 	{
   13557 	  /* A local symbol.  */
   13558 	  Elf_Internal_Sym *isym;
   13559 	  asection *sym_sec;
   13560 
   13561 	  isym = isymbuf + r_symndx;
   13562 	  if (isym->st_shndx == SHN_UNDEF)
   13563 	    sym_sec = bfd_und_section_ptr;
   13564 	  else if (isym->st_shndx == SHN_ABS)
   13565 	    sym_sec = bfd_abs_section_ptr;
   13566 	  else if (isym->st_shndx == SHN_COMMON)
   13567 	    sym_sec = bfd_com_section_ptr;
   13568 	  else
   13569 	    sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   13570 	  symval = (isym->st_value
   13571 		    + sym_sec->output_section->vma
   13572 		    + sym_sec->output_offset);
   13573 	  target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
   13574 	}
   13575       else
   13576 	{
   13577 	  unsigned long indx;
   13578 	  struct elf_link_hash_entry *h;
   13579 
   13580 	  /* An external symbol.  */
   13581 	  indx = r_symndx - symtab_hdr->sh_info;
   13582 	  h = elf_sym_hashes (abfd)[indx];
   13583 	  BFD_ASSERT (h != NULL);
   13584 
   13585 	  if (h->root.type != bfd_link_hash_defined
   13586 	      && h->root.type != bfd_link_hash_defweak)
   13587 	    /* This appears to be a reference to an undefined
   13588 	       symbol.  Just ignore it -- it will be caught by the
   13589 	       regular reloc processing.  */
   13590 	    continue;
   13591 
   13592 	  symval = (h->root.u.def.value
   13593 		    + h->root.u.def.section->output_section->vma
   13594 		    + h->root.u.def.section->output_offset);
   13595 	  target_is_micromips_code_p = (!h->needs_plt
   13596 					&& ELF_ST_IS_MICROMIPS (h->other));
   13597 	}
   13598 
   13599 
   13600       /* For simplicity of coding, we are going to modify the
   13601          section contents, the section relocs, and the BFD symbol
   13602          table.  We must tell the rest of the code not to free up this
   13603          information.  It would be possible to instead create a table
   13604          of changes which have to be made, as is done in coff-mips.c;
   13605          that would be more work, but would require less memory when
   13606          the linker is run.  */
   13607 
   13608       /* Only 32-bit instructions relaxed.  */
   13609       if (irel->r_offset + 4 > sec->size)
   13610 	continue;
   13611 
   13612       opcode = bfd_get_micromips_32 (abfd, ptr);
   13613 
   13614       /* This is the pc-relative distance from the instruction the
   13615          relocation is applied to, to the symbol referred.  */
   13616       pcrval = (symval
   13617 		- (sec->output_section->vma + sec->output_offset)
   13618 		- irel->r_offset);
   13619 
   13620       /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
   13621          of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
   13622          R_MICROMIPS_PC23_S2.  The R_MICROMIPS_PC23_S2 condition is
   13623 
   13624            (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
   13625 
   13626          where pcrval has first to be adjusted to apply against the LO16
   13627          location (we make the adjustment later on, when we have figured
   13628          out the offset).  */
   13629       if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
   13630 	{
   13631 	  bfd_boolean bzc = FALSE;
   13632 	  unsigned long nextopc;
   13633 	  unsigned long reg;
   13634 	  bfd_vma offset;
   13635 
   13636 	  /* Give up if the previous reloc was a HI16 against this symbol
   13637 	     too.  */
   13638 	  if (irel > internal_relocs
   13639 	      && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
   13640 	      && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
   13641 	    continue;
   13642 
   13643 	  /* Or if the next reloc is not a LO16 against this symbol.  */
   13644 	  if (irel + 1 >= irelend
   13645 	      || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
   13646 	      || ELF32_R_SYM (irel[1].r_info) != r_symndx)
   13647 	    continue;
   13648 
   13649 	  /* Or if the second next reloc is a LO16 against this symbol too.  */
   13650 	  if (irel + 2 >= irelend
   13651 	      && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
   13652 	      && ELF32_R_SYM (irel[2].r_info) == r_symndx)
   13653 	    continue;
   13654 
   13655 	  /* See if the LUI instruction *might* be in a branch delay slot.
   13656 	     We check whether what looks like a 16-bit branch or jump is
   13657 	     actually an immediate argument to a compact branch, and let
   13658 	     it through if so.  */
   13659 	  if (irel->r_offset >= 2
   13660 	      && check_br16_dslot (abfd, ptr - 2)
   13661 	      && !(irel->r_offset >= 4
   13662 		   && (bzc = check_relocated_bzc (abfd,
   13663 						  ptr - 4, irel->r_offset - 4,
   13664 						  internal_relocs, irelend))))
   13665 	    continue;
   13666 	  if (irel->r_offset >= 4
   13667 	      && !bzc
   13668 	      && check_br32_dslot (abfd, ptr - 4))
   13669 	    continue;
   13670 
   13671 	  reg = OP32_SREG (opcode);
   13672 
   13673 	  /* We only relax adjacent instructions or ones separated with
   13674 	     a branch or jump that has a delay slot.  The branch or jump
   13675 	     must not fiddle with the register used to hold the address.
   13676 	     Subtract 4 for the LUI itself.  */
   13677 	  offset = irel[1].r_offset - irel[0].r_offset;
   13678 	  switch (offset - 4)
   13679 	    {
   13680 	    case 0:
   13681 	      break;
   13682 	    case 2:
   13683 	      if (check_br16 (abfd, ptr + 4, reg))
   13684 		break;
   13685 	      continue;
   13686 	    case 4:
   13687 	      if (check_br32 (abfd, ptr + 4, reg))
   13688 		break;
   13689 	      continue;
   13690 	    default:
   13691 	      continue;
   13692 	    }
   13693 
   13694 	  nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
   13695 
   13696 	  /* Give up unless the same register is used with both
   13697 	     relocations.  */
   13698 	  if (OP32_SREG (nextopc) != reg)
   13699 	    continue;
   13700 
   13701 	  /* Now adjust pcrval, subtracting the offset to the LO16 reloc
   13702 	     and rounding up to take masking of the two LSBs into account.  */
   13703 	  pcrval = ((pcrval - offset + 3) | 3) ^ 3;
   13704 
   13705 	  /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16.  */
   13706 	  if (IS_BITSIZE (symval, 16))
   13707 	    {
   13708 	      /* Fix the relocation's type.  */
   13709 	      irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
   13710 
   13711 	      /* Instructions using R_MICROMIPS_LO16 have the base or
   13712 	         source register in bits 20:16.  This register becomes $0
   13713 	         (zero) as the result of the R_MICROMIPS_HI16 being 0.  */
   13714 	      nextopc &= ~0x001f0000;
   13715 	      bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
   13716 			  contents + irel[1].r_offset);
   13717 	    }
   13718 
   13719 	  /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
   13720 	     We add 4 to take LUI deletion into account while checking
   13721 	     the PC-relative distance.  */
   13722 	  else if (symval % 4 == 0
   13723 		   && IS_BITSIZE (pcrval + 4, 25)
   13724 		   && MATCH (nextopc, addiu_insn)
   13725 		   && OP32_TREG (nextopc) == OP32_SREG (nextopc)
   13726 		   && OP16_VALID_REG (OP32_TREG (nextopc)))
   13727 	    {
   13728 	      /* Fix the relocation's type.  */
   13729 	      irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
   13730 
   13731 	      /* Replace ADDIU with the ADDIUPC version.  */
   13732 	      nextopc = (addiupc_insn.match
   13733 			 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
   13734 
   13735 	      bfd_put_micromips_32 (abfd, nextopc,
   13736 				    contents + irel[1].r_offset);
   13737 	    }
   13738 
   13739 	  /* Can't do anything, give up, sigh...  */
   13740 	  else
   13741 	    continue;
   13742 
   13743 	  /* Fix the relocation's type.  */
   13744 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
   13745 
   13746 	  /* Delete the LUI instruction: 4 bytes at irel->r_offset.  */
   13747 	  delcnt = 4;
   13748 	  deloff = 0;
   13749 	}
   13750 
   13751       /* Compact branch relaxation -- due to the multitude of macros
   13752          employed by the compiler/assembler, compact branches are not
   13753          always generated.  Obviously, this can/will be fixed elsewhere,
   13754          but there is no drawback in double checking it here.  */
   13755       else if (r_type == R_MICROMIPS_PC16_S1
   13756 	       && irel->r_offset + 5 < sec->size
   13757 	       && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
   13758 		   || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
   13759 	       && ((!insn32
   13760 		    && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
   13761 					nop_insn_16) ? 2 : 0))
   13762 		   || (irel->r_offset + 7 < sec->size
   13763 		       && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
   13764 								 ptr + 4),
   13765 					   nop_insn_32) ? 4 : 0))))
   13766 	{
   13767 	  unsigned long reg;
   13768 
   13769 	  reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
   13770 
   13771 	  /* Replace BEQZ/BNEZ with the compact version.  */
   13772 	  opcode = (bzc_insns_32[fndopc].match
   13773 		    | BZC32_REG_FIELD (reg)
   13774 		    | (opcode & 0xffff));		/* Addend value.  */
   13775 
   13776 	  bfd_put_micromips_32 (abfd, opcode, ptr);
   13777 
   13778 	  /* Delete the delay slot NOP: two or four bytes from
   13779 	     irel->offset + 4; delcnt has already been set above.  */
   13780 	  deloff = 4;
   13781 	}
   13782 
   13783       /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1.  We need
   13784          to check the distance from the next instruction, so subtract 2.  */
   13785       else if (!insn32
   13786 	       && r_type == R_MICROMIPS_PC16_S1
   13787 	       && IS_BITSIZE (pcrval - 2, 11)
   13788 	       && find_match (opcode, b_insns_32) >= 0)
   13789 	{
   13790 	  /* Fix the relocation's type.  */
   13791 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
   13792 
   13793 	  /* Replace the 32-bit opcode with a 16-bit opcode.  */
   13794 	  bfd_put_16 (abfd,
   13795 		      (b_insn_16.match
   13796 		       | (opcode & 0x3ff)),		/* Addend value.  */
   13797 		      ptr);
   13798 
   13799 	  /* Delete 2 bytes from irel->r_offset + 2.  */
   13800 	  delcnt = 2;
   13801 	  deloff = 2;
   13802 	}
   13803 
   13804       /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1.  We need
   13805          to check the distance from the next instruction, so subtract 2.  */
   13806       else if (!insn32
   13807 	       && r_type == R_MICROMIPS_PC16_S1
   13808 	       && IS_BITSIZE (pcrval - 2, 8)
   13809 	       && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
   13810 		    && OP16_VALID_REG (OP32_SREG (opcode)))
   13811 		   || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
   13812 		       && OP16_VALID_REG (OP32_TREG (opcode)))))
   13813 	{
   13814 	  unsigned long reg;
   13815 
   13816 	  reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
   13817 
   13818 	  /* Fix the relocation's type.  */
   13819 	  irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
   13820 
   13821 	  /* Replace the 32-bit opcode with a 16-bit opcode.  */
   13822 	  bfd_put_16 (abfd,
   13823 		      (bz_insns_16[fndopc].match
   13824 		       | BZ16_REG_FIELD (reg)
   13825 		       | (opcode & 0x7f)),		/* Addend value.  */
   13826 		      ptr);
   13827 
   13828 	  /* Delete 2 bytes from irel->r_offset + 2.  */
   13829 	  delcnt = 2;
   13830 	  deloff = 2;
   13831 	}
   13832 
   13833       /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets.  */
   13834       else if (!insn32
   13835 	       && r_type == R_MICROMIPS_26_S1
   13836 	       && target_is_micromips_code_p
   13837 	       && irel->r_offset + 7 < sec->size
   13838 	       && MATCH (opcode, jal_insn_32_bd32))
   13839 	{
   13840 	  unsigned long n32opc;
   13841 	  bfd_boolean relaxed = FALSE;
   13842 
   13843 	  n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
   13844 
   13845 	  if (MATCH (n32opc, nop_insn_32))
   13846 	    {
   13847 	      /* Replace delay slot 32-bit NOP with a 16-bit NOP.  */
   13848 	      bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
   13849 
   13850 	      relaxed = TRUE;
   13851 	    }
   13852 	  else if (find_match (n32opc, move_insns_32) >= 0)
   13853 	    {
   13854 	      /* Replace delay slot 32-bit MOVE with 16-bit MOVE.  */
   13855 	      bfd_put_16 (abfd,
   13856 			  (move_insn_16.match
   13857 			   | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
   13858 			   | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
   13859 			  ptr + 4);
   13860 
   13861 	      relaxed = TRUE;
   13862 	    }
   13863 	  /* Other 32-bit instructions relaxable to 16-bit
   13864 	     instructions will be handled here later.  */
   13865 
   13866 	  if (relaxed)
   13867 	    {
   13868 	      /* JAL with 32-bit delay slot that is changed to a JALS
   13869 	         with 16-bit delay slot.  */
   13870 	      bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
   13871 
   13872 	      /* Delete 2 bytes from irel->r_offset + 6.  */
   13873 	      delcnt = 2;
   13874 	      deloff = 6;
   13875 	    }
   13876 	}
   13877 
   13878       if (delcnt != 0)
   13879 	{
   13880 	  /* Note that we've changed the relocs, section contents, etc.  */
   13881 	  elf_section_data (sec)->relocs = internal_relocs;
   13882 	  elf_section_data (sec)->this_hdr.contents = contents;
   13883 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   13884 
   13885 	  /* Delete bytes depending on the delcnt and deloff.  */
   13886 	  if (!mips_elf_relax_delete_bytes (abfd, sec,
   13887 					    irel->r_offset + deloff, delcnt))
   13888 	    goto error_return;
   13889 
   13890 	  /* That will change things, so we should relax again.
   13891 	     Note that this is not required, and it may be slow.  */
   13892 	  *again = TRUE;
   13893 	}
   13894     }
   13895 
   13896   if (isymbuf != NULL
   13897       && symtab_hdr->contents != (unsigned char *) isymbuf)
   13898     {
   13899       if (! link_info->keep_memory)
   13900 	free (isymbuf);
   13901       else
   13902 	{
   13903 	  /* Cache the symbols for elf_link_input_bfd.  */
   13904 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   13905 	}
   13906     }
   13907 
   13908   if (contents != NULL
   13909       && elf_section_data (sec)->this_hdr.contents != contents)
   13910     {
   13911       if (! link_info->keep_memory)
   13912 	free (contents);
   13913       else
   13914 	{
   13915 	  /* Cache the section contents for elf_link_input_bfd.  */
   13916 	  elf_section_data (sec)->this_hdr.contents = contents;
   13917 	}
   13918     }
   13919 
   13920   if (internal_relocs != NULL
   13921       && elf_section_data (sec)->relocs != internal_relocs)
   13922     free (internal_relocs);
   13923 
   13924   return TRUE;
   13925 
   13926  error_return:
   13927   if (isymbuf != NULL
   13928       && symtab_hdr->contents != (unsigned char *) isymbuf)
   13929     free (isymbuf);
   13930   if (contents != NULL
   13931       && elf_section_data (sec)->this_hdr.contents != contents)
   13932     free (contents);
   13933   if (internal_relocs != NULL
   13934       && elf_section_data (sec)->relocs != internal_relocs)
   13935     free (internal_relocs);
   13936 
   13937   return FALSE;
   13938 }
   13939 
   13940 /* Create a MIPS ELF linker hash table.  */
   13942 
   13943 struct bfd_link_hash_table *
   13944 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
   13945 {
   13946   struct mips_elf_link_hash_table *ret;
   13947   bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
   13948 
   13949   ret = bfd_zmalloc (amt);
   13950   if (ret == NULL)
   13951     return NULL;
   13952 
   13953   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
   13954 				      mips_elf_link_hash_newfunc,
   13955 				      sizeof (struct mips_elf_link_hash_entry),
   13956 				      MIPS_ELF_DATA))
   13957     {
   13958       free (ret);
   13959       return NULL;
   13960     }
   13961   ret->root.init_plt_refcount.plist = NULL;
   13962   ret->root.init_plt_offset.plist = NULL;
   13963 
   13964   return &ret->root.root;
   13965 }
   13966 
   13967 /* Likewise, but indicate that the target is VxWorks.  */
   13968 
   13969 struct bfd_link_hash_table *
   13970 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
   13971 {
   13972   struct bfd_link_hash_table *ret;
   13973 
   13974   ret = _bfd_mips_elf_link_hash_table_create (abfd);
   13975   if (ret)
   13976     {
   13977       struct mips_elf_link_hash_table *htab;
   13978 
   13979       htab = (struct mips_elf_link_hash_table *) ret;
   13980       htab->use_plts_and_copy_relocs = TRUE;
   13981       htab->is_vxworks = TRUE;
   13982     }
   13983   return ret;
   13984 }
   13985 
   13986 /* A function that the linker calls if we are allowed to use PLTs
   13987    and copy relocs.  */
   13988 
   13989 void
   13990 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
   13991 {
   13992   mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
   13993 }
   13994 
   13995 /* A function that the linker calls to select between all or only
   13996    32-bit microMIPS instructions.  */
   13997 
   13998 void
   13999 _bfd_mips_elf_insn32 (struct bfd_link_info *info, bfd_boolean on)
   14000 {
   14001   mips_elf_hash_table (info)->insn32 = on;
   14002 }
   14003 
   14004 /* Structure for saying that BFD machine EXTENSION extends BASE.  */
   14006 
   14007 struct mips_mach_extension
   14008 {
   14009   unsigned long extension, base;
   14010 };
   14011 
   14012 
   14013 /* An array describing how BFD machines relate to one another.  The entries
   14014    are ordered topologically with MIPS I extensions listed last.  */
   14015 
   14016 static const struct mips_mach_extension mips_mach_extensions[] =
   14017 {
   14018   /* MIPS64r2 extensions.  */
   14019   { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
   14020   { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
   14021   { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
   14022   { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
   14023   { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64r2 },
   14024 
   14025   /* MIPS64 extensions.  */
   14026   { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
   14027   { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
   14028   { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
   14029 
   14030   /* MIPS V extensions.  */
   14031   { bfd_mach_mipsisa64, bfd_mach_mips5 },
   14032 
   14033   /* R10000 extensions.  */
   14034   { bfd_mach_mips12000, bfd_mach_mips10000 },
   14035   { bfd_mach_mips14000, bfd_mach_mips10000 },
   14036   { bfd_mach_mips16000, bfd_mach_mips10000 },
   14037 
   14038   /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
   14039      vr5400 ISA, but doesn't include the multimedia stuff.  It seems
   14040      better to allow vr5400 and vr5500 code to be merged anyway, since
   14041      many libraries will just use the core ISA.  Perhaps we could add
   14042      some sort of ASE flag if this ever proves a problem.  */
   14043   { bfd_mach_mips5500, bfd_mach_mips5400 },
   14044   { bfd_mach_mips5400, bfd_mach_mips5000 },
   14045 
   14046   /* MIPS IV extensions.  */
   14047   { bfd_mach_mips5, bfd_mach_mips8000 },
   14048   { bfd_mach_mips10000, bfd_mach_mips8000 },
   14049   { bfd_mach_mips5000, bfd_mach_mips8000 },
   14050   { bfd_mach_mips7000, bfd_mach_mips8000 },
   14051   { bfd_mach_mips9000, bfd_mach_mips8000 },
   14052 
   14053   /* VR4100 extensions.  */
   14054   { bfd_mach_mips4120, bfd_mach_mips4100 },
   14055   { bfd_mach_mips4111, bfd_mach_mips4100 },
   14056 
   14057   /* MIPS III extensions.  */
   14058   { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
   14059   { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
   14060   { bfd_mach_mips8000, bfd_mach_mips4000 },
   14061   { bfd_mach_mips4650, bfd_mach_mips4000 },
   14062   { bfd_mach_mips4600, bfd_mach_mips4000 },
   14063   { bfd_mach_mips4400, bfd_mach_mips4000 },
   14064   { bfd_mach_mips4300, bfd_mach_mips4000 },
   14065   { bfd_mach_mips4100, bfd_mach_mips4000 },
   14066   { bfd_mach_mips4010, bfd_mach_mips4000 },
   14067   { bfd_mach_mips5900, bfd_mach_mips4000 },
   14068 
   14069   /* MIPS32 extensions.  */
   14070   { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
   14071 
   14072   /* MIPS II extensions.  */
   14073   { bfd_mach_mips4000, bfd_mach_mips6000 },
   14074   { bfd_mach_mipsisa32, bfd_mach_mips6000 },
   14075 
   14076   /* MIPS I extensions.  */
   14077   { bfd_mach_mips6000, bfd_mach_mips3000 },
   14078   { bfd_mach_mips3900, bfd_mach_mips3000 }
   14079 };
   14080 
   14081 /* Return true if bfd machine EXTENSION is an extension of machine BASE.  */
   14082 
   14083 static bfd_boolean
   14084 mips_mach_extends_p (unsigned long base, unsigned long extension)
   14085 {
   14086   size_t i;
   14087 
   14088   if (extension == base)
   14089     return TRUE;
   14090 
   14091   if (base == bfd_mach_mipsisa32
   14092       && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
   14093     return TRUE;
   14094 
   14095   if (base == bfd_mach_mipsisa32r2
   14096       && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
   14097     return TRUE;
   14098 
   14099   for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
   14100     if (extension == mips_mach_extensions[i].extension)
   14101       {
   14102 	extension = mips_mach_extensions[i].base;
   14103 	if (extension == base)
   14104 	  return TRUE;
   14105       }
   14106 
   14107   return FALSE;
   14108 }
   14109 
   14110 /* Return the BFD mach for each .MIPS.abiflags ISA Extension.  */
   14111 
   14112 static unsigned long
   14113 bfd_mips_isa_ext_mach (unsigned int isa_ext)
   14114 {
   14115   switch (isa_ext)
   14116     {
   14117     case AFL_EXT_3900:        return bfd_mach_mips3900;
   14118     case AFL_EXT_4010:        return bfd_mach_mips4010;
   14119     case AFL_EXT_4100:        return bfd_mach_mips4100;
   14120     case AFL_EXT_4111:        return bfd_mach_mips4111;
   14121     case AFL_EXT_4120:        return bfd_mach_mips4120;
   14122     case AFL_EXT_4650:        return bfd_mach_mips4650;
   14123     case AFL_EXT_5400:        return bfd_mach_mips5400;
   14124     case AFL_EXT_5500:        return bfd_mach_mips5500;
   14125     case AFL_EXT_5900:        return bfd_mach_mips5900;
   14126     case AFL_EXT_10000:       return bfd_mach_mips10000;
   14127     case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
   14128     case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
   14129     case AFL_EXT_LOONGSON_3A: return bfd_mach_mips_loongson_3a;
   14130     case AFL_EXT_SB1:         return bfd_mach_mips_sb1;
   14131     case AFL_EXT_OCTEON:      return bfd_mach_mips_octeon;
   14132     case AFL_EXT_OCTEONP:     return bfd_mach_mips_octeonp;
   14133     case AFL_EXT_OCTEON2:     return bfd_mach_mips_octeon2;
   14134     case AFL_EXT_XLR:         return bfd_mach_mips_xlr;
   14135     default:                  return bfd_mach_mips3000;
   14136     }
   14137 }
   14138 
   14139 /* Return the .MIPS.abiflags value representing each ISA Extension.  */
   14140 
   14141 unsigned int
   14142 bfd_mips_isa_ext (bfd *abfd)
   14143 {
   14144   switch (bfd_get_mach (abfd))
   14145     {
   14146     case bfd_mach_mips3900:         return AFL_EXT_3900;
   14147     case bfd_mach_mips4010:         return AFL_EXT_4010;
   14148     case bfd_mach_mips4100:         return AFL_EXT_4100;
   14149     case bfd_mach_mips4111:         return AFL_EXT_4111;
   14150     case bfd_mach_mips4120:         return AFL_EXT_4120;
   14151     case bfd_mach_mips4650:         return AFL_EXT_4650;
   14152     case bfd_mach_mips5400:         return AFL_EXT_5400;
   14153     case bfd_mach_mips5500:         return AFL_EXT_5500;
   14154     case bfd_mach_mips5900:         return AFL_EXT_5900;
   14155     case bfd_mach_mips10000:        return AFL_EXT_10000;
   14156     case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
   14157     case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
   14158     case bfd_mach_mips_loongson_3a: return AFL_EXT_LOONGSON_3A;
   14159     case bfd_mach_mips_sb1:         return AFL_EXT_SB1;
   14160     case bfd_mach_mips_octeon:      return AFL_EXT_OCTEON;
   14161     case bfd_mach_mips_octeonp:     return AFL_EXT_OCTEONP;
   14162     case bfd_mach_mips_octeon3:     return AFL_EXT_OCTEON3;
   14163     case bfd_mach_mips_octeon2:     return AFL_EXT_OCTEON2;
   14164     case bfd_mach_mips_xlr:         return AFL_EXT_XLR;
   14165     default:                        return 0;
   14166     }
   14167 }
   14168 
   14169 /* Encode ISA level and revision as a single value.  */
   14170 #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
   14171 
   14172 /* Decode a single value into level and revision.  */
   14173 #define ISA_LEVEL(LEVREV)  ((LEVREV) >> 3)
   14174 #define ISA_REV(LEVREV)    ((LEVREV) & 0x7)
   14175 
   14176 /* Update the isa_level, isa_rev, isa_ext fields of abiflags.  */
   14177 
   14178 static void
   14179 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
   14180 {
   14181   int new_isa = 0;
   14182   switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
   14183     {
   14184     case E_MIPS_ARCH_1:    new_isa = LEVEL_REV (1, 0); break;
   14185     case E_MIPS_ARCH_2:    new_isa = LEVEL_REV (2, 0); break;
   14186     case E_MIPS_ARCH_3:    new_isa = LEVEL_REV (3, 0); break;
   14187     case E_MIPS_ARCH_4:    new_isa = LEVEL_REV (4, 0); break;
   14188     case E_MIPS_ARCH_5:    new_isa = LEVEL_REV (5, 0); break;
   14189     case E_MIPS_ARCH_32:   new_isa = LEVEL_REV (32, 1); break;
   14190     case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
   14191     case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
   14192     case E_MIPS_ARCH_64:   new_isa = LEVEL_REV (64, 1); break;
   14193     case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
   14194     case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
   14195     default:
   14196       (*_bfd_error_handler)
   14197 	(_("%B: Unknown architecture %s"),
   14198 	 abfd, bfd_printable_name (abfd));
   14199     }
   14200 
   14201   if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
   14202     {
   14203       abiflags->isa_level = ISA_LEVEL (new_isa);
   14204       abiflags->isa_rev = ISA_REV (new_isa);
   14205     }
   14206 
   14207   /* Update the isa_ext if ABFD describes a further extension.  */
   14208   if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
   14209 			   bfd_get_mach (abfd)))
   14210     abiflags->isa_ext = bfd_mips_isa_ext (abfd);
   14211 }
   14212 
   14213 /* Return true if the given ELF header flags describe a 32-bit binary.  */
   14214 
   14215 static bfd_boolean
   14216 mips_32bit_flags_p (flagword flags)
   14217 {
   14218   return ((flags & EF_MIPS_32BITMODE) != 0
   14219 	  || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
   14220 	  || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
   14221 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
   14222 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
   14223 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
   14224 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
   14225 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
   14226 }
   14227 
   14228 /* Infer the content of the ABI flags based on the elf header.  */
   14229 
   14230 static void
   14231 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
   14232 {
   14233   obj_attribute *in_attr;
   14234 
   14235   memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
   14236   update_mips_abiflags_isa (abfd, abiflags);
   14237 
   14238   if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
   14239     abiflags->gpr_size = AFL_REG_32;
   14240   else
   14241     abiflags->gpr_size = AFL_REG_64;
   14242 
   14243   abiflags->cpr1_size = AFL_REG_NONE;
   14244 
   14245   in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
   14246   abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   14247 
   14248   if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
   14249       || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
   14250       || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
   14251 	  && abiflags->gpr_size == AFL_REG_32))
   14252     abiflags->cpr1_size = AFL_REG_32;
   14253   else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
   14254 	   || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
   14255 	   || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
   14256     abiflags->cpr1_size = AFL_REG_64;
   14257 
   14258   abiflags->cpr2_size = AFL_REG_NONE;
   14259 
   14260   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
   14261     abiflags->ases |= AFL_ASE_MDMX;
   14262   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
   14263     abiflags->ases |= AFL_ASE_MIPS16;
   14264   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
   14265     abiflags->ases |= AFL_ASE_MICROMIPS;
   14266 
   14267   if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
   14268       && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
   14269       && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
   14270       && abiflags->isa_level >= 32
   14271       && abiflags->isa_ext != AFL_EXT_LOONGSON_3A)
   14272     abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
   14273 }
   14274 
   14275 /* We need to use a special link routine to handle the .reginfo and
   14276    the .mdebug sections.  We need to merge all instances of these
   14277    sections together, not write them all out sequentially.  */
   14278 
   14279 bfd_boolean
   14280 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   14281 {
   14282   asection *o;
   14283   struct bfd_link_order *p;
   14284   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
   14285   asection *rtproc_sec, *abiflags_sec;
   14286   Elf32_RegInfo reginfo;
   14287   struct ecoff_debug_info debug;
   14288   struct mips_htab_traverse_info hti;
   14289   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14290   const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
   14291   HDRR *symhdr = &debug.symbolic_header;
   14292   void *mdebug_handle = NULL;
   14293   asection *s;
   14294   EXTR esym;
   14295   unsigned int i;
   14296   bfd_size_type amt;
   14297   struct mips_elf_link_hash_table *htab;
   14298 
   14299   static const char * const secname[] =
   14300   {
   14301     ".text", ".init", ".fini", ".data",
   14302     ".rodata", ".sdata", ".sbss", ".bss"
   14303   };
   14304   static const int sc[] =
   14305   {
   14306     scText, scInit, scFini, scData,
   14307     scRData, scSData, scSBss, scBss
   14308   };
   14309 
   14310   /* Sort the dynamic symbols so that those with GOT entries come after
   14311      those without.  */
   14312   htab = mips_elf_hash_table (info);
   14313   BFD_ASSERT (htab != NULL);
   14314 
   14315   if (!mips_elf_sort_hash_table (abfd, info))
   14316     return FALSE;
   14317 
   14318   /* Create any scheduled LA25 stubs.  */
   14319   hti.info = info;
   14320   hti.output_bfd = abfd;
   14321   hti.error = FALSE;
   14322   htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
   14323   if (hti.error)
   14324     return FALSE;
   14325 
   14326   /* Get a value for the GP register.  */
   14327   if (elf_gp (abfd) == 0)
   14328     {
   14329       struct bfd_link_hash_entry *h;
   14330 
   14331       h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
   14332       if (h != NULL && h->type == bfd_link_hash_defined)
   14333 	elf_gp (abfd) = (h->u.def.value
   14334 			 + h->u.def.section->output_section->vma
   14335 			 + h->u.def.section->output_offset);
   14336       else if (htab->is_vxworks
   14337 	       && (h = bfd_link_hash_lookup (info->hash,
   14338 					     "_GLOBAL_OFFSET_TABLE_",
   14339 					     FALSE, FALSE, TRUE))
   14340 	       && h->type == bfd_link_hash_defined)
   14341 	elf_gp (abfd) = (h->u.def.section->output_section->vma
   14342 			 + h->u.def.section->output_offset
   14343 			 + h->u.def.value);
   14344       else if (bfd_link_relocatable (info))
   14345 	{
   14346 	  bfd_vma lo = MINUS_ONE;
   14347 
   14348 	  /* Find the GP-relative section with the lowest offset.  */
   14349 	  for (o = abfd->sections; o != NULL; o = o->next)
   14350 	    if (o->vma < lo
   14351 		&& (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
   14352 	      lo = o->vma;
   14353 
   14354 	  /* And calculate GP relative to that.  */
   14355 	  elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
   14356 	}
   14357       else
   14358 	{
   14359 	  /* If the relocate_section function needs to do a reloc
   14360 	     involving the GP value, it should make a reloc_dangerous
   14361 	     callback to warn that GP is not defined.  */
   14362 	}
   14363     }
   14364 
   14365   /* Go through the sections and collect the .reginfo and .mdebug
   14366      information.  */
   14367   abiflags_sec = NULL;
   14368   reginfo_sec = NULL;
   14369   mdebug_sec = NULL;
   14370   gptab_data_sec = NULL;
   14371   gptab_bss_sec = NULL;
   14372   for (o = abfd->sections; o != NULL; o = o->next)
   14373     {
   14374       if (strcmp (o->name, ".MIPS.abiflags") == 0)
   14375 	{
   14376 	  /* We have found the .MIPS.abiflags section in the output file.
   14377 	     Look through all the link_orders comprising it and remove them.
   14378 	     The data is merged in _bfd_mips_elf_merge_private_bfd_data.  */
   14379 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   14380 	    {
   14381 	      asection *input_section;
   14382 
   14383 	      if (p->type != bfd_indirect_link_order)
   14384 		{
   14385 		  if (p->type == bfd_data_link_order)
   14386 		    continue;
   14387 		  abort ();
   14388 		}
   14389 
   14390 	      input_section = p->u.indirect.section;
   14391 
   14392 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   14393 		 elf_link_input_bfd ignores this section.  */
   14394 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   14395 	    }
   14396 
   14397 	  /* Size has been set in _bfd_mips_elf_always_size_sections.  */
   14398 	  BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
   14399 
   14400 	  /* Skip this section later on (I don't think this currently
   14401 	     matters, but someday it might).  */
   14402 	  o->map_head.link_order = NULL;
   14403 
   14404 	  abiflags_sec = o;
   14405 	}
   14406 
   14407       if (strcmp (o->name, ".reginfo") == 0)
   14408 	{
   14409 	  memset (&reginfo, 0, sizeof reginfo);
   14410 
   14411 	  /* We have found the .reginfo section in the output file.
   14412 	     Look through all the link_orders comprising it and merge
   14413 	     the information together.  */
   14414 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   14415 	    {
   14416 	      asection *input_section;
   14417 	      bfd *input_bfd;
   14418 	      Elf32_External_RegInfo ext;
   14419 	      Elf32_RegInfo sub;
   14420 
   14421 	      if (p->type != bfd_indirect_link_order)
   14422 		{
   14423 		  if (p->type == bfd_data_link_order)
   14424 		    continue;
   14425 		  abort ();
   14426 		}
   14427 
   14428 	      input_section = p->u.indirect.section;
   14429 	      input_bfd = input_section->owner;
   14430 
   14431 	      if (! bfd_get_section_contents (input_bfd, input_section,
   14432 					      &ext, 0, sizeof ext))
   14433 		return FALSE;
   14434 
   14435 	      bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
   14436 
   14437 	      reginfo.ri_gprmask |= sub.ri_gprmask;
   14438 	      reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
   14439 	      reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
   14440 	      reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
   14441 	      reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
   14442 
   14443 	      /* ri_gp_value is set by the function
   14444 		 mips_elf32_section_processing when the section is
   14445 		 finally written out.  */
   14446 
   14447 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   14448 		 elf_link_input_bfd ignores this section.  */
   14449 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   14450 	    }
   14451 
   14452 	  /* Size has been set in _bfd_mips_elf_always_size_sections.  */
   14453 	  BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
   14454 
   14455 	  /* Skip this section later on (I don't think this currently
   14456 	     matters, but someday it might).  */
   14457 	  o->map_head.link_order = NULL;
   14458 
   14459 	  reginfo_sec = o;
   14460 	}
   14461 
   14462       if (strcmp (o->name, ".mdebug") == 0)
   14463 	{
   14464 	  struct extsym_info einfo;
   14465 	  bfd_vma last;
   14466 
   14467 	  /* We have found the .mdebug section in the output file.
   14468 	     Look through all the link_orders comprising it and merge
   14469 	     the information together.  */
   14470 	  symhdr->magic = swap->sym_magic;
   14471 	  /* FIXME: What should the version stamp be?  */
   14472 	  symhdr->vstamp = 0;
   14473 	  symhdr->ilineMax = 0;
   14474 	  symhdr->cbLine = 0;
   14475 	  symhdr->idnMax = 0;
   14476 	  symhdr->ipdMax = 0;
   14477 	  symhdr->isymMax = 0;
   14478 	  symhdr->ioptMax = 0;
   14479 	  symhdr->iauxMax = 0;
   14480 	  symhdr->issMax = 0;
   14481 	  symhdr->issExtMax = 0;
   14482 	  symhdr->ifdMax = 0;
   14483 	  symhdr->crfd = 0;
   14484 	  symhdr->iextMax = 0;
   14485 
   14486 	  /* We accumulate the debugging information itself in the
   14487 	     debug_info structure.  */
   14488 	  debug.line = NULL;
   14489 	  debug.external_dnr = NULL;
   14490 	  debug.external_pdr = NULL;
   14491 	  debug.external_sym = NULL;
   14492 	  debug.external_opt = NULL;
   14493 	  debug.external_aux = NULL;
   14494 	  debug.ss = NULL;
   14495 	  debug.ssext = debug.ssext_end = NULL;
   14496 	  debug.external_fdr = NULL;
   14497 	  debug.external_rfd = NULL;
   14498 	  debug.external_ext = debug.external_ext_end = NULL;
   14499 
   14500 	  mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
   14501 	  if (mdebug_handle == NULL)
   14502 	    return FALSE;
   14503 
   14504 	  esym.jmptbl = 0;
   14505 	  esym.cobol_main = 0;
   14506 	  esym.weakext = 0;
   14507 	  esym.reserved = 0;
   14508 	  esym.ifd = ifdNil;
   14509 	  esym.asym.iss = issNil;
   14510 	  esym.asym.st = stLocal;
   14511 	  esym.asym.reserved = 0;
   14512 	  esym.asym.index = indexNil;
   14513 	  last = 0;
   14514 	  for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
   14515 	    {
   14516 	      esym.asym.sc = sc[i];
   14517 	      s = bfd_get_section_by_name (abfd, secname[i]);
   14518 	      if (s != NULL)
   14519 		{
   14520 		  esym.asym.value = s->vma;
   14521 		  last = s->vma + s->size;
   14522 		}
   14523 	      else
   14524 		esym.asym.value = last;
   14525 	      if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
   14526 						 secname[i], &esym))
   14527 		return FALSE;
   14528 	    }
   14529 
   14530 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   14531 	    {
   14532 	      asection *input_section;
   14533 	      bfd *input_bfd;
   14534 	      const struct ecoff_debug_swap *input_swap;
   14535 	      struct ecoff_debug_info input_debug;
   14536 	      char *eraw_src;
   14537 	      char *eraw_end;
   14538 
   14539 	      if (p->type != bfd_indirect_link_order)
   14540 		{
   14541 		  if (p->type == bfd_data_link_order)
   14542 		    continue;
   14543 		  abort ();
   14544 		}
   14545 
   14546 	      input_section = p->u.indirect.section;
   14547 	      input_bfd = input_section->owner;
   14548 
   14549 	      if (!is_mips_elf (input_bfd))
   14550 		{
   14551 		  /* I don't know what a non MIPS ELF bfd would be
   14552 		     doing with a .mdebug section, but I don't really
   14553 		     want to deal with it.  */
   14554 		  continue;
   14555 		}
   14556 
   14557 	      input_swap = (get_elf_backend_data (input_bfd)
   14558 			    ->elf_backend_ecoff_debug_swap);
   14559 
   14560 	      BFD_ASSERT (p->size == input_section->size);
   14561 
   14562 	      /* The ECOFF linking code expects that we have already
   14563 		 read in the debugging information and set up an
   14564 		 ecoff_debug_info structure, so we do that now.  */
   14565 	      if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
   14566 						   &input_debug))
   14567 		return FALSE;
   14568 
   14569 	      if (! (bfd_ecoff_debug_accumulate
   14570 		     (mdebug_handle, abfd, &debug, swap, input_bfd,
   14571 		      &input_debug, input_swap, info)))
   14572 		return FALSE;
   14573 
   14574 	      /* Loop through the external symbols.  For each one with
   14575 		 interesting information, try to find the symbol in
   14576 		 the linker global hash table and save the information
   14577 		 for the output external symbols.  */
   14578 	      eraw_src = input_debug.external_ext;
   14579 	      eraw_end = (eraw_src
   14580 			  + (input_debug.symbolic_header.iextMax
   14581 			     * input_swap->external_ext_size));
   14582 	      for (;
   14583 		   eraw_src < eraw_end;
   14584 		   eraw_src += input_swap->external_ext_size)
   14585 		{
   14586 		  EXTR ext;
   14587 		  const char *name;
   14588 		  struct mips_elf_link_hash_entry *h;
   14589 
   14590 		  (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
   14591 		  if (ext.asym.sc == scNil
   14592 		      || ext.asym.sc == scUndefined
   14593 		      || ext.asym.sc == scSUndefined)
   14594 		    continue;
   14595 
   14596 		  name = input_debug.ssext + ext.asym.iss;
   14597 		  h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
   14598 						 name, FALSE, FALSE, TRUE);
   14599 		  if (h == NULL || h->esym.ifd != -2)
   14600 		    continue;
   14601 
   14602 		  if (ext.ifd != -1)
   14603 		    {
   14604 		      BFD_ASSERT (ext.ifd
   14605 				  < input_debug.symbolic_header.ifdMax);
   14606 		      ext.ifd = input_debug.ifdmap[ext.ifd];
   14607 		    }
   14608 
   14609 		  h->esym = ext;
   14610 		}
   14611 
   14612 	      /* Free up the information we just read.  */
   14613 	      free (input_debug.line);
   14614 	      free (input_debug.external_dnr);
   14615 	      free (input_debug.external_pdr);
   14616 	      free (input_debug.external_sym);
   14617 	      free (input_debug.external_opt);
   14618 	      free (input_debug.external_aux);
   14619 	      free (input_debug.ss);
   14620 	      free (input_debug.ssext);
   14621 	      free (input_debug.external_fdr);
   14622 	      free (input_debug.external_rfd);
   14623 	      free (input_debug.external_ext);
   14624 
   14625 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   14626 		 elf_link_input_bfd ignores this section.  */
   14627 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   14628 	    }
   14629 
   14630 	  if (SGI_COMPAT (abfd) && bfd_link_pic (info))
   14631 	    {
   14632 	      /* Create .rtproc section.  */
   14633 	      rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
   14634 	      if (rtproc_sec == NULL)
   14635 		{
   14636 		  flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
   14637 				    | SEC_LINKER_CREATED | SEC_READONLY);
   14638 
   14639 		  rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
   14640 								   ".rtproc",
   14641 								   flags);
   14642 		  if (rtproc_sec == NULL
   14643 		      || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
   14644 		    return FALSE;
   14645 		}
   14646 
   14647 	      if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
   14648 						     info, rtproc_sec,
   14649 						     &debug))
   14650 		return FALSE;
   14651 	    }
   14652 
   14653 	  /* Build the external symbol information.  */
   14654 	  einfo.abfd = abfd;
   14655 	  einfo.info = info;
   14656 	  einfo.debug = &debug;
   14657 	  einfo.swap = swap;
   14658 	  einfo.failed = FALSE;
   14659 	  mips_elf_link_hash_traverse (mips_elf_hash_table (info),
   14660 				       mips_elf_output_extsym, &einfo);
   14661 	  if (einfo.failed)
   14662 	    return FALSE;
   14663 
   14664 	  /* Set the size of the .mdebug section.  */
   14665 	  o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
   14666 
   14667 	  /* Skip this section later on (I don't think this currently
   14668 	     matters, but someday it might).  */
   14669 	  o->map_head.link_order = NULL;
   14670 
   14671 	  mdebug_sec = o;
   14672 	}
   14673 
   14674       if (CONST_STRNEQ (o->name, ".gptab."))
   14675 	{
   14676 	  const char *subname;
   14677 	  unsigned int c;
   14678 	  Elf32_gptab *tab;
   14679 	  Elf32_External_gptab *ext_tab;
   14680 	  unsigned int j;
   14681 
   14682 	  /* The .gptab.sdata and .gptab.sbss sections hold
   14683 	     information describing how the small data area would
   14684 	     change depending upon the -G switch.  These sections
   14685 	     not used in executables files.  */
   14686 	  if (! bfd_link_relocatable (info))
   14687 	    {
   14688 	      for (p = o->map_head.link_order; p != NULL; p = p->next)
   14689 		{
   14690 		  asection *input_section;
   14691 
   14692 		  if (p->type != bfd_indirect_link_order)
   14693 		    {
   14694 		      if (p->type == bfd_data_link_order)
   14695 			continue;
   14696 		      abort ();
   14697 		    }
   14698 
   14699 		  input_section = p->u.indirect.section;
   14700 
   14701 		  /* Hack: reset the SEC_HAS_CONTENTS flag so that
   14702 		     elf_link_input_bfd ignores this section.  */
   14703 		  input_section->flags &= ~SEC_HAS_CONTENTS;
   14704 		}
   14705 
   14706 	      /* Skip this section later on (I don't think this
   14707 		 currently matters, but someday it might).  */
   14708 	      o->map_head.link_order = NULL;
   14709 
   14710 	      /* Really remove the section.  */
   14711 	      bfd_section_list_remove (abfd, o);
   14712 	      --abfd->section_count;
   14713 
   14714 	      continue;
   14715 	    }
   14716 
   14717 	  /* There is one gptab for initialized data, and one for
   14718 	     uninitialized data.  */
   14719 	  if (strcmp (o->name, ".gptab.sdata") == 0)
   14720 	    gptab_data_sec = o;
   14721 	  else if (strcmp (o->name, ".gptab.sbss") == 0)
   14722 	    gptab_bss_sec = o;
   14723 	  else
   14724 	    {
   14725 	      (*_bfd_error_handler)
   14726 		(_("%s: illegal section name `%s'"),
   14727 		 bfd_get_filename (abfd), o->name);
   14728 	      bfd_set_error (bfd_error_nonrepresentable_section);
   14729 	      return FALSE;
   14730 	    }
   14731 
   14732 	  /* The linker script always combines .gptab.data and
   14733 	     .gptab.sdata into .gptab.sdata, and likewise for
   14734 	     .gptab.bss and .gptab.sbss.  It is possible that there is
   14735 	     no .sdata or .sbss section in the output file, in which
   14736 	     case we must change the name of the output section.  */
   14737 	  subname = o->name + sizeof ".gptab" - 1;
   14738 	  if (bfd_get_section_by_name (abfd, subname) == NULL)
   14739 	    {
   14740 	      if (o == gptab_data_sec)
   14741 		o->name = ".gptab.data";
   14742 	      else
   14743 		o->name = ".gptab.bss";
   14744 	      subname = o->name + sizeof ".gptab" - 1;
   14745 	      BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
   14746 	    }
   14747 
   14748 	  /* Set up the first entry.  */
   14749 	  c = 1;
   14750 	  amt = c * sizeof (Elf32_gptab);
   14751 	  tab = bfd_malloc (amt);
   14752 	  if (tab == NULL)
   14753 	    return FALSE;
   14754 	  tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
   14755 	  tab[0].gt_header.gt_unused = 0;
   14756 
   14757 	  /* Combine the input sections.  */
   14758 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   14759 	    {
   14760 	      asection *input_section;
   14761 	      bfd *input_bfd;
   14762 	      bfd_size_type size;
   14763 	      unsigned long last;
   14764 	      bfd_size_type gpentry;
   14765 
   14766 	      if (p->type != bfd_indirect_link_order)
   14767 		{
   14768 		  if (p->type == bfd_data_link_order)
   14769 		    continue;
   14770 		  abort ();
   14771 		}
   14772 
   14773 	      input_section = p->u.indirect.section;
   14774 	      input_bfd = input_section->owner;
   14775 
   14776 	      /* Combine the gptab entries for this input section one
   14777 		 by one.  We know that the input gptab entries are
   14778 		 sorted by ascending -G value.  */
   14779 	      size = input_section->size;
   14780 	      last = 0;
   14781 	      for (gpentry = sizeof (Elf32_External_gptab);
   14782 		   gpentry < size;
   14783 		   gpentry += sizeof (Elf32_External_gptab))
   14784 		{
   14785 		  Elf32_External_gptab ext_gptab;
   14786 		  Elf32_gptab int_gptab;
   14787 		  unsigned long val;
   14788 		  unsigned long add;
   14789 		  bfd_boolean exact;
   14790 		  unsigned int look;
   14791 
   14792 		  if (! (bfd_get_section_contents
   14793 			 (input_bfd, input_section, &ext_gptab, gpentry,
   14794 			  sizeof (Elf32_External_gptab))))
   14795 		    {
   14796 		      free (tab);
   14797 		      return FALSE;
   14798 		    }
   14799 
   14800 		  bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
   14801 						&int_gptab);
   14802 		  val = int_gptab.gt_entry.gt_g_value;
   14803 		  add = int_gptab.gt_entry.gt_bytes - last;
   14804 
   14805 		  exact = FALSE;
   14806 		  for (look = 1; look < c; look++)
   14807 		    {
   14808 		      if (tab[look].gt_entry.gt_g_value >= val)
   14809 			tab[look].gt_entry.gt_bytes += add;
   14810 
   14811 		      if (tab[look].gt_entry.gt_g_value == val)
   14812 			exact = TRUE;
   14813 		    }
   14814 
   14815 		  if (! exact)
   14816 		    {
   14817 		      Elf32_gptab *new_tab;
   14818 		      unsigned int max;
   14819 
   14820 		      /* We need a new table entry.  */
   14821 		      amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
   14822 		      new_tab = bfd_realloc (tab, amt);
   14823 		      if (new_tab == NULL)
   14824 			{
   14825 			  free (tab);
   14826 			  return FALSE;
   14827 			}
   14828 		      tab = new_tab;
   14829 		      tab[c].gt_entry.gt_g_value = val;
   14830 		      tab[c].gt_entry.gt_bytes = add;
   14831 
   14832 		      /* Merge in the size for the next smallest -G
   14833 			 value, since that will be implied by this new
   14834 			 value.  */
   14835 		      max = 0;
   14836 		      for (look = 1; look < c; look++)
   14837 			{
   14838 			  if (tab[look].gt_entry.gt_g_value < val
   14839 			      && (max == 0
   14840 				  || (tab[look].gt_entry.gt_g_value
   14841 				      > tab[max].gt_entry.gt_g_value)))
   14842 			    max = look;
   14843 			}
   14844 		      if (max != 0)
   14845 			tab[c].gt_entry.gt_bytes +=
   14846 			  tab[max].gt_entry.gt_bytes;
   14847 
   14848 		      ++c;
   14849 		    }
   14850 
   14851 		  last = int_gptab.gt_entry.gt_bytes;
   14852 		}
   14853 
   14854 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   14855 		 elf_link_input_bfd ignores this section.  */
   14856 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   14857 	    }
   14858 
   14859 	  /* The table must be sorted by -G value.  */
   14860 	  if (c > 2)
   14861 	    qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
   14862 
   14863 	  /* Swap out the table.  */
   14864 	  amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
   14865 	  ext_tab = bfd_alloc (abfd, amt);
   14866 	  if (ext_tab == NULL)
   14867 	    {
   14868 	      free (tab);
   14869 	      return FALSE;
   14870 	    }
   14871 
   14872 	  for (j = 0; j < c; j++)
   14873 	    bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
   14874 	  free (tab);
   14875 
   14876 	  o->size = c * sizeof (Elf32_External_gptab);
   14877 	  o->contents = (bfd_byte *) ext_tab;
   14878 
   14879 	  /* Skip this section later on (I don't think this currently
   14880 	     matters, but someday it might).  */
   14881 	  o->map_head.link_order = NULL;
   14882 	}
   14883     }
   14884 
   14885   /* Invoke the regular ELF backend linker to do all the work.  */
   14886   if (!bfd_elf_final_link (abfd, info))
   14887     return FALSE;
   14888 
   14889   /* Now write out the computed sections.  */
   14890 
   14891   if (abiflags_sec != NULL)
   14892     {
   14893       Elf_External_ABIFlags_v0 ext;
   14894       Elf_Internal_ABIFlags_v0 *abiflags;
   14895 
   14896       abiflags = &mips_elf_tdata (abfd)->abiflags;
   14897 
   14898       /* Set up the abiflags if no valid input sections were found.  */
   14899       if (!mips_elf_tdata (abfd)->abiflags_valid)
   14900 	{
   14901 	  infer_mips_abiflags (abfd, abiflags);
   14902 	  mips_elf_tdata (abfd)->abiflags_valid = TRUE;
   14903 	}
   14904       bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
   14905       if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
   14906 	return FALSE;
   14907     }
   14908 
   14909   if (reginfo_sec != NULL)
   14910     {
   14911       Elf32_External_RegInfo ext;
   14912 
   14913       bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
   14914       if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
   14915 	return FALSE;
   14916     }
   14917 
   14918   if (mdebug_sec != NULL)
   14919     {
   14920       BFD_ASSERT (abfd->output_has_begun);
   14921       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
   14922 					       swap, info,
   14923 					       mdebug_sec->filepos))
   14924 	return FALSE;
   14925 
   14926       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
   14927     }
   14928 
   14929   if (gptab_data_sec != NULL)
   14930     {
   14931       if (! bfd_set_section_contents (abfd, gptab_data_sec,
   14932 				      gptab_data_sec->contents,
   14933 				      0, gptab_data_sec->size))
   14934 	return FALSE;
   14935     }
   14936 
   14937   if (gptab_bss_sec != NULL)
   14938     {
   14939       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
   14940 				      gptab_bss_sec->contents,
   14941 				      0, gptab_bss_sec->size))
   14942 	return FALSE;
   14943     }
   14944 
   14945   if (SGI_COMPAT (abfd))
   14946     {
   14947       rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
   14948       if (rtproc_sec != NULL)
   14949 	{
   14950 	  if (! bfd_set_section_contents (abfd, rtproc_sec,
   14951 					  rtproc_sec->contents,
   14952 					  0, rtproc_sec->size))
   14953 	    return FALSE;
   14954 	}
   14955     }
   14956 
   14957   return TRUE;
   14958 }
   14959 
   14960 /* Merge object file header flags from IBFD into OBFD.  Raise an error
   14962    if there are conflicting settings.  */
   14963 
   14964 static bfd_boolean
   14965 mips_elf_merge_obj_e_flags (bfd *ibfd, bfd *obfd)
   14966 {
   14967   struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
   14968   flagword old_flags;
   14969   flagword new_flags;
   14970   bfd_boolean ok;
   14971 
   14972   new_flags = elf_elfheader (ibfd)->e_flags;
   14973   elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
   14974   old_flags = elf_elfheader (obfd)->e_flags;
   14975 
   14976   /* Check flag compatibility.  */
   14977 
   14978   new_flags &= ~EF_MIPS_NOREORDER;
   14979   old_flags &= ~EF_MIPS_NOREORDER;
   14980 
   14981   /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
   14982      doesn't seem to matter.  */
   14983   new_flags &= ~EF_MIPS_XGOT;
   14984   old_flags &= ~EF_MIPS_XGOT;
   14985 
   14986   /* MIPSpro generates ucode info in n64 objects.  Again, we should
   14987      just be able to ignore this.  */
   14988   new_flags &= ~EF_MIPS_UCODE;
   14989   old_flags &= ~EF_MIPS_UCODE;
   14990 
   14991   /* DSOs should only be linked with CPIC code.  */
   14992   if ((ibfd->flags & DYNAMIC) != 0)
   14993     new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
   14994 
   14995   if (new_flags == old_flags)
   14996     return TRUE;
   14997 
   14998   ok = TRUE;
   14999 
   15000   if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
   15001       != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
   15002     {
   15003       (*_bfd_error_handler)
   15004 	(_("%B: warning: linking abicalls files with non-abicalls files"),
   15005 	 ibfd);
   15006       ok = TRUE;
   15007     }
   15008 
   15009   if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
   15010     elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
   15011   if (! (new_flags & EF_MIPS_PIC))
   15012     elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
   15013 
   15014   new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
   15015   old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
   15016 
   15017   /* Compare the ISAs.  */
   15018   if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
   15019     {
   15020       (*_bfd_error_handler)
   15021 	(_("%B: linking 32-bit code with 64-bit code"),
   15022 	 ibfd);
   15023       ok = FALSE;
   15024     }
   15025   else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
   15026     {
   15027       /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
   15028       if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
   15029 	{
   15030 	  /* Copy the architecture info from IBFD to OBFD.  Also copy
   15031 	     the 32-bit flag (if set) so that we continue to recognise
   15032 	     OBFD as a 32-bit binary.  */
   15033 	  bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
   15034 	  elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
   15035 	  elf_elfheader (obfd)->e_flags
   15036 	    |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15037 
   15038 	  /* Update the ABI flags isa_level, isa_rev, isa_ext fields.  */
   15039 	  update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
   15040 
   15041 	  /* Copy across the ABI flags if OBFD doesn't use them
   15042 	     and if that was what caused us to treat IBFD as 32-bit.  */
   15043 	  if ((old_flags & EF_MIPS_ABI) == 0
   15044 	      && mips_32bit_flags_p (new_flags)
   15045 	      && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
   15046 	    elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
   15047 	}
   15048       else
   15049 	{
   15050 	  /* The ISAs aren't compatible.  */
   15051 	  (*_bfd_error_handler)
   15052 	    (_("%B: linking %s module with previous %s modules"),
   15053 	     ibfd,
   15054 	     bfd_printable_name (ibfd),
   15055 	     bfd_printable_name (obfd));
   15056 	  ok = FALSE;
   15057 	}
   15058     }
   15059 
   15060   new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15061   old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   15062 
   15063   /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
   15064      does set EI_CLASS differently from any 32-bit ABI.  */
   15065   if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
   15066       || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   15067 	  != elf_elfheader (obfd)->e_ident[EI_CLASS]))
   15068     {
   15069       /* Only error if both are set (to different values).  */
   15070       if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
   15071 	  || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   15072 	      != elf_elfheader (obfd)->e_ident[EI_CLASS]))
   15073 	{
   15074 	  (*_bfd_error_handler)
   15075 	    (_("%B: ABI mismatch: linking %s module with previous %s modules"),
   15076 	     ibfd,
   15077 	     elf_mips_abi_name (ibfd),
   15078 	     elf_mips_abi_name (obfd));
   15079 	  ok = FALSE;
   15080 	}
   15081       new_flags &= ~EF_MIPS_ABI;
   15082       old_flags &= ~EF_MIPS_ABI;
   15083     }
   15084 
   15085   /* Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
   15086      and allow arbitrary mixing of the remaining ASEs (retain the union).  */
   15087   if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
   15088     {
   15089       int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
   15090       int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
   15091       int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
   15092       int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
   15093       int micro_mis = old_m16 && new_micro;
   15094       int m16_mis = old_micro && new_m16;
   15095 
   15096       if (m16_mis || micro_mis)
   15097 	{
   15098 	  (*_bfd_error_handler)
   15099 	    (_("%B: ASE mismatch: linking %s module with previous %s modules"),
   15100 	     ibfd,
   15101 	     m16_mis ? "MIPS16" : "microMIPS",
   15102 	     m16_mis ? "microMIPS" : "MIPS16");
   15103 	  ok = FALSE;
   15104 	}
   15105 
   15106       elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
   15107 
   15108       new_flags &= ~ EF_MIPS_ARCH_ASE;
   15109       old_flags &= ~ EF_MIPS_ARCH_ASE;
   15110     }
   15111 
   15112   /* Compare NaN encodings.  */
   15113   if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
   15114     {
   15115       _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
   15116 			  ibfd,
   15117 			  (new_flags & EF_MIPS_NAN2008
   15118 			   ? "-mnan=2008" : "-mnan=legacy"),
   15119 			  (old_flags & EF_MIPS_NAN2008
   15120 			   ? "-mnan=2008" : "-mnan=legacy"));
   15121       ok = FALSE;
   15122       new_flags &= ~EF_MIPS_NAN2008;
   15123       old_flags &= ~EF_MIPS_NAN2008;
   15124     }
   15125 
   15126   /* Compare FP64 state.  */
   15127   if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
   15128     {
   15129       _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
   15130 			  ibfd,
   15131 			  (new_flags & EF_MIPS_FP64
   15132 			   ? "-mfp64" : "-mfp32"),
   15133 			  (old_flags & EF_MIPS_FP64
   15134 			   ? "-mfp64" : "-mfp32"));
   15135       ok = FALSE;
   15136       new_flags &= ~EF_MIPS_FP64;
   15137       old_flags &= ~EF_MIPS_FP64;
   15138     }
   15139 
   15140   /* Warn about any other mismatches */
   15141   if (new_flags != old_flags)
   15142     {
   15143       (*_bfd_error_handler)
   15144 	(_("%B: uses different e_flags (0x%lx) fields than previous modules "
   15145 	   "(0x%lx)"),
   15146 	 ibfd, (unsigned long) new_flags,
   15147 	 (unsigned long) old_flags);
   15148       ok = FALSE;
   15149     }
   15150 
   15151   return ok;
   15152 }
   15153 
   15154 /* Merge object attributes from IBFD into OBFD.  Raise an error if
   15155    there are conflicting attributes.  */
   15156 static bfd_boolean
   15157 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
   15158 {
   15159   obj_attribute *in_attr;
   15160   obj_attribute *out_attr;
   15161   bfd *abi_fp_bfd;
   15162   bfd *abi_msa_bfd;
   15163 
   15164   abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
   15165   in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   15166   if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
   15167     mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   15168 
   15169   abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
   15170   if (!abi_msa_bfd
   15171       && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
   15172     mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
   15173 
   15174   if (!elf_known_obj_attributes_proc (obfd)[0].i)
   15175     {
   15176       /* This is the first object.  Copy the attributes.  */
   15177       _bfd_elf_copy_obj_attributes (ibfd, obfd);
   15178 
   15179       /* Use the Tag_null value to indicate the attributes have been
   15180 	 initialized.  */
   15181       elf_known_obj_attributes_proc (obfd)[0].i = 1;
   15182 
   15183       return TRUE;
   15184     }
   15185 
   15186   /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
   15187      non-conflicting ones.  */
   15188   out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   15189   if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
   15190     {
   15191       int out_fp, in_fp;
   15192 
   15193       out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
   15194       in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   15195       out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
   15196       if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
   15197 	out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
   15198       else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
   15199 	       && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
   15200 		   || in_fp == Val_GNU_MIPS_ABI_FP_64
   15201 		   || in_fp == Val_GNU_MIPS_ABI_FP_64A))
   15202 	{
   15203 	  mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   15204 	  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   15205 	}
   15206       else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
   15207 	       && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
   15208 		   || out_fp == Val_GNU_MIPS_ABI_FP_64
   15209 		   || out_fp == Val_GNU_MIPS_ABI_FP_64A))
   15210 	/* Keep the current setting.  */;
   15211       else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
   15212 	       && in_fp == Val_GNU_MIPS_ABI_FP_64)
   15213 	{
   15214 	  mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
   15215 	  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   15216 	}
   15217       else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
   15218 	       && out_fp == Val_GNU_MIPS_ABI_FP_64)
   15219 	/* Keep the current setting.  */;
   15220       else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
   15221 	{
   15222 	  const char *out_string, *in_string;
   15223 
   15224 	  out_string = _bfd_mips_fp_abi_string (out_fp);
   15225 	  in_string = _bfd_mips_fp_abi_string (in_fp);
   15226 	  /* First warn about cases involving unrecognised ABIs.  */
   15227 	  if (!out_string && !in_string)
   15228 	    _bfd_error_handler
   15229 	      (_("Warning: %B uses unknown floating point ABI %d "
   15230 		 "(set by %B), %B uses unknown floating point ABI %d"),
   15231 	       obfd, abi_fp_bfd, ibfd, out_fp, in_fp);
   15232 	  else if (!out_string)
   15233 	    _bfd_error_handler
   15234 	      (_("Warning: %B uses unknown floating point ABI %d "
   15235 		 "(set by %B), %B uses %s"),
   15236 	       obfd, abi_fp_bfd, ibfd, out_fp, in_string);
   15237 	  else if (!in_string)
   15238 	    _bfd_error_handler
   15239 	      (_("Warning: %B uses %s (set by %B), "
   15240 		 "%B uses unknown floating point ABI %d"),
   15241 	       obfd, abi_fp_bfd, ibfd, out_string, in_fp);
   15242 	  else
   15243 	    {
   15244 	      /* If one of the bfds is soft-float, the other must be
   15245 		 hard-float.  The exact choice of hard-float ABI isn't
   15246 		 really relevant to the error message.  */
   15247 	      if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
   15248 		out_string = "-mhard-float";
   15249 	      else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
   15250 		in_string = "-mhard-float";
   15251 	      _bfd_error_handler
   15252 		(_("Warning: %B uses %s (set by %B), %B uses %s"),
   15253 		 obfd, abi_fp_bfd, ibfd, out_string, in_string);
   15254 	    }
   15255 	}
   15256     }
   15257 
   15258   /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
   15259      non-conflicting ones.  */
   15260   if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
   15261     {
   15262       out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
   15263       if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
   15264 	out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
   15265       else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
   15266 	switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
   15267 	  {
   15268 	  case Val_GNU_MIPS_ABI_MSA_128:
   15269 	    _bfd_error_handler
   15270 	      (_("Warning: %B uses %s (set by %B), "
   15271 		 "%B uses unknown MSA ABI %d"),
   15272 	       obfd, abi_msa_bfd, ibfd,
   15273 	       "-mmsa", in_attr[Tag_GNU_MIPS_ABI_MSA].i);
   15274 	    break;
   15275 
   15276 	  default:
   15277 	    switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
   15278 	      {
   15279 	      case Val_GNU_MIPS_ABI_MSA_128:
   15280 		_bfd_error_handler
   15281 		  (_("Warning: %B uses unknown MSA ABI %d "
   15282 		     "(set by %B), %B uses %s"),
   15283 		     obfd, abi_msa_bfd, ibfd,
   15284 		     out_attr[Tag_GNU_MIPS_ABI_MSA].i, "-mmsa");
   15285 		  break;
   15286 
   15287 	      default:
   15288 		_bfd_error_handler
   15289 		  (_("Warning: %B uses unknown MSA ABI %d "
   15290 		     "(set by %B), %B uses unknown MSA ABI %d"),
   15291 		   obfd, abi_msa_bfd, ibfd,
   15292 		   out_attr[Tag_GNU_MIPS_ABI_MSA].i,
   15293 		   in_attr[Tag_GNU_MIPS_ABI_MSA].i);
   15294 		break;
   15295 	      }
   15296 	  }
   15297     }
   15298 
   15299   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   15300   return _bfd_elf_merge_object_attributes (ibfd, obfd);
   15301 }
   15302 
   15303 /* Merge object ABI flags from IBFD into OBFD.  Raise an error if
   15304    there are conflicting settings.  */
   15305 
   15306 static bfd_boolean
   15307 mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
   15308 {
   15309   obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   15310   struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
   15311   struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
   15312 
   15313   /* Update the output abiflags fp_abi using the computed fp_abi.  */
   15314   out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
   15315 
   15316 #define max(a, b) ((a) > (b) ? (a) : (b))
   15317   /* Merge abiflags.  */
   15318   out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
   15319 				       in_tdata->abiflags.isa_level);
   15320   out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
   15321 				     in_tdata->abiflags.isa_rev);
   15322   out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
   15323 				      in_tdata->abiflags.gpr_size);
   15324   out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
   15325 				       in_tdata->abiflags.cpr1_size);
   15326   out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
   15327 				       in_tdata->abiflags.cpr2_size);
   15328 #undef max
   15329   out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
   15330   out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
   15331 
   15332   return TRUE;
   15333 }
   15334 
   15335 /* Merge backend specific data from an object file to the output
   15336    object file when linking.  */
   15337 
   15338 bfd_boolean
   15339 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   15340 {
   15341   struct mips_elf_obj_tdata *out_tdata;
   15342   struct mips_elf_obj_tdata *in_tdata;
   15343   bfd_boolean null_input_bfd = TRUE;
   15344   asection *sec;
   15345   bfd_boolean ok;
   15346 
   15347   /* Check if we have the same endianness.  */
   15348   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
   15349     {
   15350       (*_bfd_error_handler)
   15351 	(_("%B: endianness incompatible with that of the selected emulation"),
   15352 	 ibfd);
   15353       return FALSE;
   15354     }
   15355 
   15356   if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
   15357     return TRUE;
   15358 
   15359   in_tdata = mips_elf_tdata (ibfd);
   15360   out_tdata = mips_elf_tdata (obfd);
   15361 
   15362   if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
   15363     {
   15364       (*_bfd_error_handler)
   15365 	(_("%B: ABI is incompatible with that of the selected emulation"),
   15366 	 ibfd);
   15367       return FALSE;
   15368     }
   15369 
   15370   /* Check to see if the input BFD actually contains any sections.  If not,
   15371      then it has no attributes, and its flags may not have been initialized
   15372      either, but it cannot actually cause any incompatibility.  */
   15373   for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   15374     {
   15375       /* Ignore synthetic sections and empty .text, .data and .bss sections
   15376 	 which are automatically generated by gas.  Also ignore fake
   15377 	 (s)common sections, since merely defining a common symbol does
   15378 	 not affect compatibility.  */
   15379       if ((sec->flags & SEC_IS_COMMON) == 0
   15380 	  && strcmp (sec->name, ".reginfo")
   15381 	  && strcmp (sec->name, ".mdebug")
   15382 	  && (sec->size != 0
   15383 	      || (strcmp (sec->name, ".text")
   15384 		  && strcmp (sec->name, ".data")
   15385 		  && strcmp (sec->name, ".bss"))))
   15386 	{
   15387 	  null_input_bfd = FALSE;
   15388 	  break;
   15389 	}
   15390     }
   15391   if (null_input_bfd)
   15392     return TRUE;
   15393 
   15394   /* Populate abiflags using existing information.  */
   15395   if (in_tdata->abiflags_valid)
   15396     {
   15397       obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   15398       Elf_Internal_ABIFlags_v0 in_abiflags;
   15399       Elf_Internal_ABIFlags_v0 abiflags;
   15400 
   15401       /* Set up the FP ABI attribute from the abiflags if it is not already
   15402          set.  */
   15403       if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
   15404         in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
   15405 
   15406       infer_mips_abiflags (ibfd, &abiflags);
   15407       in_abiflags = in_tdata->abiflags;
   15408 
   15409       /* It is not possible to infer the correct ISA revision
   15410          for R3 or R5 so drop down to R2 for the checks.  */
   15411       if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
   15412 	in_abiflags.isa_rev = 2;
   15413 
   15414       if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
   15415 	  < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
   15416 	(*_bfd_error_handler)
   15417 	  (_("%B: warning: Inconsistent ISA between e_flags and "
   15418 	     ".MIPS.abiflags"), ibfd);
   15419       if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
   15420 	  && in_abiflags.fp_abi != abiflags.fp_abi)
   15421 	(*_bfd_error_handler)
   15422 	  (_("%B: warning: Inconsistent FP ABI between .gnu.attributes and "
   15423 	     ".MIPS.abiflags"), ibfd);
   15424       if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
   15425 	(*_bfd_error_handler)
   15426 	  (_("%B: warning: Inconsistent ASEs between e_flags and "
   15427 	     ".MIPS.abiflags"), ibfd);
   15428       /* The isa_ext is allowed to be an extension of what can be inferred
   15429 	 from e_flags.  */
   15430       if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
   15431 				bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
   15432 	(*_bfd_error_handler)
   15433 	  (_("%B: warning: Inconsistent ISA extensions between e_flags and "
   15434 	     ".MIPS.abiflags"), ibfd);
   15435       if (in_abiflags.flags2 != 0)
   15436 	(*_bfd_error_handler)
   15437 	  (_("%B: warning: Unexpected flag in the flags2 field of "
   15438 	     ".MIPS.abiflags (0x%lx)"), ibfd,
   15439 	   (unsigned long) in_abiflags.flags2);
   15440     }
   15441   else
   15442     {
   15443       infer_mips_abiflags (ibfd, &in_tdata->abiflags);
   15444       in_tdata->abiflags_valid = TRUE;
   15445     }
   15446 
   15447   if (!out_tdata->abiflags_valid)
   15448     {
   15449       /* Copy input abiflags if output abiflags are not already valid.  */
   15450       out_tdata->abiflags = in_tdata->abiflags;
   15451       out_tdata->abiflags_valid = TRUE;
   15452     }
   15453 
   15454   if (! elf_flags_init (obfd))
   15455     {
   15456       elf_flags_init (obfd) = TRUE;
   15457       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   15458       elf_elfheader (obfd)->e_ident[EI_CLASS]
   15459 	= elf_elfheader (ibfd)->e_ident[EI_CLASS];
   15460 
   15461       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   15462 	  && (bfd_get_arch_info (obfd)->the_default
   15463 	      || mips_mach_extends_p (bfd_get_mach (obfd),
   15464 				      bfd_get_mach (ibfd))))
   15465 	{
   15466 	  if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
   15467 				   bfd_get_mach (ibfd)))
   15468 	    return FALSE;
   15469 
   15470 	  /* Update the ABI flags isa_level, isa_rev and isa_ext fields.  */
   15471 	  update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
   15472 	}
   15473 
   15474       ok = TRUE;
   15475     }
   15476   else
   15477     ok = mips_elf_merge_obj_e_flags (ibfd, obfd);
   15478 
   15479   ok = mips_elf_merge_obj_attributes (ibfd, obfd) && ok;
   15480 
   15481   ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
   15482 
   15483   if (!ok)
   15484     {
   15485       bfd_set_error (bfd_error_bad_value);
   15486       return FALSE;
   15487     }
   15488 
   15489   return TRUE;
   15490 }
   15491 
   15492 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
   15493 
   15494 bfd_boolean
   15495 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
   15496 {
   15497   BFD_ASSERT (!elf_flags_init (abfd)
   15498 	      || elf_elfheader (abfd)->e_flags == flags);
   15499 
   15500   elf_elfheader (abfd)->e_flags = flags;
   15501   elf_flags_init (abfd) = TRUE;
   15502   return TRUE;
   15503 }
   15504 
   15505 char *
   15506 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
   15507 {
   15508   switch (dtag)
   15509     {
   15510     default: return "";
   15511     case DT_MIPS_RLD_VERSION:
   15512       return "MIPS_RLD_VERSION";
   15513     case DT_MIPS_TIME_STAMP:
   15514       return "MIPS_TIME_STAMP";
   15515     case DT_MIPS_ICHECKSUM:
   15516       return "MIPS_ICHECKSUM";
   15517     case DT_MIPS_IVERSION:
   15518       return "MIPS_IVERSION";
   15519     case DT_MIPS_FLAGS:
   15520       return "MIPS_FLAGS";
   15521     case DT_MIPS_BASE_ADDRESS:
   15522       return "MIPS_BASE_ADDRESS";
   15523     case DT_MIPS_MSYM:
   15524       return "MIPS_MSYM";
   15525     case DT_MIPS_CONFLICT:
   15526       return "MIPS_CONFLICT";
   15527     case DT_MIPS_LIBLIST:
   15528       return "MIPS_LIBLIST";
   15529     case DT_MIPS_LOCAL_GOTNO:
   15530       return "MIPS_LOCAL_GOTNO";
   15531     case DT_MIPS_CONFLICTNO:
   15532       return "MIPS_CONFLICTNO";
   15533     case DT_MIPS_LIBLISTNO:
   15534       return "MIPS_LIBLISTNO";
   15535     case DT_MIPS_SYMTABNO:
   15536       return "MIPS_SYMTABNO";
   15537     case DT_MIPS_UNREFEXTNO:
   15538       return "MIPS_UNREFEXTNO";
   15539     case DT_MIPS_GOTSYM:
   15540       return "MIPS_GOTSYM";
   15541     case DT_MIPS_HIPAGENO:
   15542       return "MIPS_HIPAGENO";
   15543     case DT_MIPS_RLD_MAP:
   15544       return "MIPS_RLD_MAP";
   15545     case DT_MIPS_RLD_MAP_REL:
   15546       return "MIPS_RLD_MAP_REL";
   15547     case DT_MIPS_DELTA_CLASS:
   15548       return "MIPS_DELTA_CLASS";
   15549     case DT_MIPS_DELTA_CLASS_NO:
   15550       return "MIPS_DELTA_CLASS_NO";
   15551     case DT_MIPS_DELTA_INSTANCE:
   15552       return "MIPS_DELTA_INSTANCE";
   15553     case DT_MIPS_DELTA_INSTANCE_NO:
   15554       return "MIPS_DELTA_INSTANCE_NO";
   15555     case DT_MIPS_DELTA_RELOC:
   15556       return "MIPS_DELTA_RELOC";
   15557     case DT_MIPS_DELTA_RELOC_NO:
   15558       return "MIPS_DELTA_RELOC_NO";
   15559     case DT_MIPS_DELTA_SYM:
   15560       return "MIPS_DELTA_SYM";
   15561     case DT_MIPS_DELTA_SYM_NO:
   15562       return "MIPS_DELTA_SYM_NO";
   15563     case DT_MIPS_DELTA_CLASSSYM:
   15564       return "MIPS_DELTA_CLASSSYM";
   15565     case DT_MIPS_DELTA_CLASSSYM_NO:
   15566       return "MIPS_DELTA_CLASSSYM_NO";
   15567     case DT_MIPS_CXX_FLAGS:
   15568       return "MIPS_CXX_FLAGS";
   15569     case DT_MIPS_PIXIE_INIT:
   15570       return "MIPS_PIXIE_INIT";
   15571     case DT_MIPS_SYMBOL_LIB:
   15572       return "MIPS_SYMBOL_LIB";
   15573     case DT_MIPS_LOCALPAGE_GOTIDX:
   15574       return "MIPS_LOCALPAGE_GOTIDX";
   15575     case DT_MIPS_LOCAL_GOTIDX:
   15576       return "MIPS_LOCAL_GOTIDX";
   15577     case DT_MIPS_HIDDEN_GOTIDX:
   15578       return "MIPS_HIDDEN_GOTIDX";
   15579     case DT_MIPS_PROTECTED_GOTIDX:
   15580       return "MIPS_PROTECTED_GOT_IDX";
   15581     case DT_MIPS_OPTIONS:
   15582       return "MIPS_OPTIONS";
   15583     case DT_MIPS_INTERFACE:
   15584       return "MIPS_INTERFACE";
   15585     case DT_MIPS_DYNSTR_ALIGN:
   15586       return "DT_MIPS_DYNSTR_ALIGN";
   15587     case DT_MIPS_INTERFACE_SIZE:
   15588       return "DT_MIPS_INTERFACE_SIZE";
   15589     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
   15590       return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
   15591     case DT_MIPS_PERF_SUFFIX:
   15592       return "DT_MIPS_PERF_SUFFIX";
   15593     case DT_MIPS_COMPACT_SIZE:
   15594       return "DT_MIPS_COMPACT_SIZE";
   15595     case DT_MIPS_GP_VALUE:
   15596       return "DT_MIPS_GP_VALUE";
   15597     case DT_MIPS_AUX_DYNAMIC:
   15598       return "DT_MIPS_AUX_DYNAMIC";
   15599     case DT_MIPS_PLTGOT:
   15600       return "DT_MIPS_PLTGOT";
   15601     case DT_MIPS_RWPLT:
   15602       return "DT_MIPS_RWPLT";
   15603     }
   15604 }
   15605 
   15606 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
   15607    not known.  */
   15608 
   15609 const char *
   15610 _bfd_mips_fp_abi_string (int fp)
   15611 {
   15612   switch (fp)
   15613     {
   15614       /* These strings aren't translated because they're simply
   15615 	 option lists.  */
   15616     case Val_GNU_MIPS_ABI_FP_DOUBLE:
   15617       return "-mdouble-float";
   15618 
   15619     case Val_GNU_MIPS_ABI_FP_SINGLE:
   15620       return "-msingle-float";
   15621 
   15622     case Val_GNU_MIPS_ABI_FP_SOFT:
   15623       return "-msoft-float";
   15624 
   15625     case Val_GNU_MIPS_ABI_FP_OLD_64:
   15626       return _("-mips32r2 -mfp64 (12 callee-saved)");
   15627 
   15628     case Val_GNU_MIPS_ABI_FP_XX:
   15629       return "-mfpxx";
   15630 
   15631     case Val_GNU_MIPS_ABI_FP_64:
   15632       return "-mgp32 -mfp64";
   15633 
   15634     case Val_GNU_MIPS_ABI_FP_64A:
   15635       return "-mgp32 -mfp64 -mno-odd-spreg";
   15636 
   15637     default:
   15638       return 0;
   15639     }
   15640 }
   15641 
   15642 static void
   15643 print_mips_ases (FILE *file, unsigned int mask)
   15644 {
   15645   if (mask & AFL_ASE_DSP)
   15646     fputs ("\n\tDSP ASE", file);
   15647   if (mask & AFL_ASE_DSPR2)
   15648     fputs ("\n\tDSP R2 ASE", file);
   15649   if (mask & AFL_ASE_DSPR3)
   15650     fputs ("\n\tDSP R3 ASE", file);
   15651   if (mask & AFL_ASE_EVA)
   15652     fputs ("\n\tEnhanced VA Scheme", file);
   15653   if (mask & AFL_ASE_MCU)
   15654     fputs ("\n\tMCU (MicroController) ASE", file);
   15655   if (mask & AFL_ASE_MDMX)
   15656     fputs ("\n\tMDMX ASE", file);
   15657   if (mask & AFL_ASE_MIPS3D)
   15658     fputs ("\n\tMIPS-3D ASE", file);
   15659   if (mask & AFL_ASE_MT)
   15660     fputs ("\n\tMT ASE", file);
   15661   if (mask & AFL_ASE_SMARTMIPS)
   15662     fputs ("\n\tSmartMIPS ASE", file);
   15663   if (mask & AFL_ASE_VIRT)
   15664     fputs ("\n\tVZ ASE", file);
   15665   if (mask & AFL_ASE_MSA)
   15666     fputs ("\n\tMSA ASE", file);
   15667   if (mask & AFL_ASE_MIPS16)
   15668     fputs ("\n\tMIPS16 ASE", file);
   15669   if (mask & AFL_ASE_MICROMIPS)
   15670     fputs ("\n\tMICROMIPS ASE", file);
   15671   if (mask & AFL_ASE_XPA)
   15672     fputs ("\n\tXPA ASE", file);
   15673   if (mask == 0)
   15674     fprintf (file, "\n\t%s", _("None"));
   15675   else if ((mask & ~AFL_ASE_MASK) != 0)
   15676     fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
   15677 }
   15678 
   15679 static void
   15680 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
   15681 {
   15682   switch (isa_ext)
   15683     {
   15684     case 0:
   15685       fputs (_("None"), file);
   15686       break;
   15687     case AFL_EXT_XLR:
   15688       fputs ("RMI XLR", file);
   15689       break;
   15690     case AFL_EXT_OCTEON3:
   15691       fputs ("Cavium Networks Octeon3", file);
   15692       break;
   15693     case AFL_EXT_OCTEON2:
   15694       fputs ("Cavium Networks Octeon2", file);
   15695       break;
   15696     case AFL_EXT_OCTEONP:
   15697       fputs ("Cavium Networks OcteonP", file);
   15698       break;
   15699     case AFL_EXT_LOONGSON_3A:
   15700       fputs ("Loongson 3A", file);
   15701       break;
   15702     case AFL_EXT_OCTEON:
   15703       fputs ("Cavium Networks Octeon", file);
   15704       break;
   15705     case AFL_EXT_5900:
   15706       fputs ("Toshiba R5900", file);
   15707       break;
   15708     case AFL_EXT_4650:
   15709       fputs ("MIPS R4650", file);
   15710       break;
   15711     case AFL_EXT_4010:
   15712       fputs ("LSI R4010", file);
   15713       break;
   15714     case AFL_EXT_4100:
   15715       fputs ("NEC VR4100", file);
   15716       break;
   15717     case AFL_EXT_3900:
   15718       fputs ("Toshiba R3900", file);
   15719       break;
   15720     case AFL_EXT_10000:
   15721       fputs ("MIPS R10000", file);
   15722       break;
   15723     case AFL_EXT_SB1:
   15724       fputs ("Broadcom SB-1", file);
   15725       break;
   15726     case AFL_EXT_4111:
   15727       fputs ("NEC VR4111/VR4181", file);
   15728       break;
   15729     case AFL_EXT_4120:
   15730       fputs ("NEC VR4120", file);
   15731       break;
   15732     case AFL_EXT_5400:
   15733       fputs ("NEC VR5400", file);
   15734       break;
   15735     case AFL_EXT_5500:
   15736       fputs ("NEC VR5500", file);
   15737       break;
   15738     case AFL_EXT_LOONGSON_2E:
   15739       fputs ("ST Microelectronics Loongson 2E", file);
   15740       break;
   15741     case AFL_EXT_LOONGSON_2F:
   15742       fputs ("ST Microelectronics Loongson 2F", file);
   15743       break;
   15744     default:
   15745       fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
   15746       break;
   15747     }
   15748 }
   15749 
   15750 static void
   15751 print_mips_fp_abi_value (FILE *file, int val)
   15752 {
   15753   switch (val)
   15754     {
   15755     case Val_GNU_MIPS_ABI_FP_ANY:
   15756       fprintf (file, _("Hard or soft float\n"));
   15757       break;
   15758     case Val_GNU_MIPS_ABI_FP_DOUBLE:
   15759       fprintf (file, _("Hard float (double precision)\n"));
   15760       break;
   15761     case Val_GNU_MIPS_ABI_FP_SINGLE:
   15762       fprintf (file, _("Hard float (single precision)\n"));
   15763       break;
   15764     case Val_GNU_MIPS_ABI_FP_SOFT:
   15765       fprintf (file, _("Soft float\n"));
   15766       break;
   15767     case Val_GNU_MIPS_ABI_FP_OLD_64:
   15768       fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
   15769       break;
   15770     case Val_GNU_MIPS_ABI_FP_XX:
   15771       fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
   15772       break;
   15773     case Val_GNU_MIPS_ABI_FP_64:
   15774       fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
   15775       break;
   15776     case Val_GNU_MIPS_ABI_FP_64A:
   15777       fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
   15778       break;
   15779     default:
   15780       fprintf (file, "??? (%d)\n", val);
   15781       break;
   15782     }
   15783 }
   15784 
   15785 static int
   15786 get_mips_reg_size (int reg_size)
   15787 {
   15788   return (reg_size == AFL_REG_NONE) ? 0
   15789 	 : (reg_size == AFL_REG_32) ? 32
   15790 	 : (reg_size == AFL_REG_64) ? 64
   15791 	 : (reg_size == AFL_REG_128) ? 128
   15792 	 : -1;
   15793 }
   15794 
   15795 bfd_boolean
   15796 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
   15797 {
   15798   FILE *file = ptr;
   15799 
   15800   BFD_ASSERT (abfd != NULL && ptr != NULL);
   15801 
   15802   /* Print normal ELF private data.  */
   15803   _bfd_elf_print_private_bfd_data (abfd, ptr);
   15804 
   15805   /* xgettext:c-format */
   15806   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
   15807 
   15808   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
   15809     fprintf (file, _(" [abi=O32]"));
   15810   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
   15811     fprintf (file, _(" [abi=O64]"));
   15812   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
   15813     fprintf (file, _(" [abi=EABI32]"));
   15814   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
   15815     fprintf (file, _(" [abi=EABI64]"));
   15816   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
   15817     fprintf (file, _(" [abi unknown]"));
   15818   else if (ABI_N32_P (abfd))
   15819     fprintf (file, _(" [abi=N32]"));
   15820   else if (ABI_64_P (abfd))
   15821     fprintf (file, _(" [abi=64]"));
   15822   else
   15823     fprintf (file, _(" [no abi set]"));
   15824 
   15825   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
   15826     fprintf (file, " [mips1]");
   15827   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
   15828     fprintf (file, " [mips2]");
   15829   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
   15830     fprintf (file, " [mips3]");
   15831   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
   15832     fprintf (file, " [mips4]");
   15833   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
   15834     fprintf (file, " [mips5]");
   15835   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
   15836     fprintf (file, " [mips32]");
   15837   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
   15838     fprintf (file, " [mips64]");
   15839   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
   15840     fprintf (file, " [mips32r2]");
   15841   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
   15842     fprintf (file, " [mips64r2]");
   15843   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
   15844     fprintf (file, " [mips32r6]");
   15845   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
   15846     fprintf (file, " [mips64r6]");
   15847   else
   15848     fprintf (file, _(" [unknown ISA]"));
   15849 
   15850   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
   15851     fprintf (file, " [mdmx]");
   15852 
   15853   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
   15854     fprintf (file, " [mips16]");
   15855 
   15856   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
   15857     fprintf (file, " [micromips]");
   15858 
   15859   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
   15860     fprintf (file, " [nan2008]");
   15861 
   15862   if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
   15863     fprintf (file, " [old fp64]");
   15864 
   15865   if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
   15866     fprintf (file, " [32bitmode]");
   15867   else
   15868     fprintf (file, _(" [not 32bitmode]"));
   15869 
   15870   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
   15871     fprintf (file, " [noreorder]");
   15872 
   15873   if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
   15874     fprintf (file, " [PIC]");
   15875 
   15876   if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
   15877     fprintf (file, " [CPIC]");
   15878 
   15879   if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
   15880     fprintf (file, " [XGOT]");
   15881 
   15882   if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
   15883     fprintf (file, " [UCODE]");
   15884 
   15885   fputc ('\n', file);
   15886 
   15887   if (mips_elf_tdata (abfd)->abiflags_valid)
   15888     {
   15889       Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
   15890       fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
   15891       fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
   15892       if (abiflags->isa_rev > 1)
   15893 	fprintf (file, "r%d", abiflags->isa_rev);
   15894       fprintf (file, "\nGPR size: %d",
   15895 	       get_mips_reg_size (abiflags->gpr_size));
   15896       fprintf (file, "\nCPR1 size: %d",
   15897 	       get_mips_reg_size (abiflags->cpr1_size));
   15898       fprintf (file, "\nCPR2 size: %d",
   15899 	       get_mips_reg_size (abiflags->cpr2_size));
   15900       fputs ("\nFP ABI: ", file);
   15901       print_mips_fp_abi_value (file, abiflags->fp_abi);
   15902       fputs ("ISA Extension: ", file);
   15903       print_mips_isa_ext (file, abiflags->isa_ext);
   15904       fputs ("\nASEs:", file);
   15905       print_mips_ases (file, abiflags->ases);
   15906       fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
   15907       fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
   15908       fputc ('\n', file);
   15909     }
   15910 
   15911   return TRUE;
   15912 }
   15913 
   15914 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
   15915 {
   15916   { STRING_COMMA_LEN (".lit4"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   15917   { STRING_COMMA_LEN (".lit8"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   15918   { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
   15919   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   15920   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   15921   { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
   15922   { NULL,                     0,  0, 0,              0 }
   15923 };
   15924 
   15925 /* Merge non visibility st_other attributes.  Ensure that the
   15926    STO_OPTIONAL flag is copied into h->other, even if this is not a
   15927    definiton of the symbol.  */
   15928 void
   15929 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
   15930 				      const Elf_Internal_Sym *isym,
   15931 				      bfd_boolean definition,
   15932 				      bfd_boolean dynamic ATTRIBUTE_UNUSED)
   15933 {
   15934   if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
   15935     {
   15936       unsigned char other;
   15937 
   15938       other = (definition ? isym->st_other : h->other);
   15939       other &= ~ELF_ST_VISIBILITY (-1);
   15940       h->other = other | ELF_ST_VISIBILITY (h->other);
   15941     }
   15942 
   15943   if (!definition
   15944       && ELF_MIPS_IS_OPTIONAL (isym->st_other))
   15945     h->other |= STO_OPTIONAL;
   15946 }
   15947 
   15948 /* Decide whether an undefined symbol is special and can be ignored.
   15949    This is the case for OPTIONAL symbols on IRIX.  */
   15950 bfd_boolean
   15951 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
   15952 {
   15953   return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
   15954 }
   15955 
   15956 bfd_boolean
   15957 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
   15958 {
   15959   return (sym->st_shndx == SHN_COMMON
   15960 	  || sym->st_shndx == SHN_MIPS_ACOMMON
   15961 	  || sym->st_shndx == SHN_MIPS_SCOMMON);
   15962 }
   15963 
   15964 /* Return address for Ith PLT stub in section PLT, for relocation REL
   15965    or (bfd_vma) -1 if it should not be included.  */
   15966 
   15967 bfd_vma
   15968 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
   15969 			   const arelent *rel ATTRIBUTE_UNUSED)
   15970 {
   15971   return (plt->vma
   15972 	  + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
   15973 	  + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
   15974 }
   15975 
   15976 /* Build a table of synthetic symbols to represent the PLT.  As with MIPS16
   15977    and microMIPS PLT slots we may have a many-to-one mapping between .plt
   15978    and .got.plt and also the slots may be of a different size each we walk
   15979    the PLT manually fetching instructions and matching them against known
   15980    patterns.  To make things easier standard MIPS slots, if any, always come
   15981    first.  As we don't create proper ELF symbols we use the UDATA.I member
   15982    of ASYMBOL to carry ISA annotation.  The encoding used is the same as
   15983    with the ST_OTHER member of the ELF symbol.  */
   15984 
   15985 long
   15986 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
   15987 				    long symcount ATTRIBUTE_UNUSED,
   15988 				    asymbol **syms ATTRIBUTE_UNUSED,
   15989 				    long dynsymcount, asymbol **dynsyms,
   15990 				    asymbol **ret)
   15991 {
   15992   static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
   15993   static const char microsuffix[] = "@micromipsplt";
   15994   static const char m16suffix[] = "@mips16plt";
   15995   static const char mipssuffix[] = "@plt";
   15996 
   15997   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   15998   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   15999   bfd_boolean micromips_p = MICROMIPS_P (abfd);
   16000   Elf_Internal_Shdr *hdr;
   16001   bfd_byte *plt_data;
   16002   bfd_vma plt_offset;
   16003   unsigned int other;
   16004   bfd_vma entry_size;
   16005   bfd_vma plt0_size;
   16006   asection *relplt;
   16007   bfd_vma opcode;
   16008   asection *plt;
   16009   asymbol *send;
   16010   size_t size;
   16011   char *names;
   16012   long counti;
   16013   arelent *p;
   16014   asymbol *s;
   16015   char *nend;
   16016   long count;
   16017   long pi;
   16018   long i;
   16019   long n;
   16020 
   16021   *ret = NULL;
   16022 
   16023   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
   16024     return 0;
   16025 
   16026   relplt = bfd_get_section_by_name (abfd, ".rel.plt");
   16027   if (relplt == NULL)
   16028     return 0;
   16029 
   16030   hdr = &elf_section_data (relplt)->this_hdr;
   16031   if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
   16032     return 0;
   16033 
   16034   plt = bfd_get_section_by_name (abfd, ".plt");
   16035   if (plt == NULL)
   16036     return 0;
   16037 
   16038   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   16039   if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
   16040     return -1;
   16041   p = relplt->relocation;
   16042 
   16043   /* Calculating the exact amount of space required for symbols would
   16044      require two passes over the PLT, so just pessimise assuming two
   16045      PLT slots per relocation.  */
   16046   count = relplt->size / hdr->sh_entsize;
   16047   counti = count * bed->s->int_rels_per_ext_rel;
   16048   size = 2 * count * sizeof (asymbol);
   16049   size += count * (sizeof (mipssuffix) +
   16050 		   (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
   16051   for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
   16052     size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
   16053 
   16054   /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too.  */
   16055   size += sizeof (asymbol) + sizeof (pltname);
   16056 
   16057   if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
   16058     return -1;
   16059 
   16060   if (plt->size < 16)
   16061     return -1;
   16062 
   16063   s = *ret = bfd_malloc (size);
   16064   if (s == NULL)
   16065     return -1;
   16066   send = s + 2 * count + 1;
   16067 
   16068   names = (char *) send;
   16069   nend = (char *) s + size;
   16070   n = 0;
   16071 
   16072   opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
   16073   if (opcode == 0x3302fffe)
   16074     {
   16075       if (!micromips_p)
   16076 	return -1;
   16077       plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
   16078       other = STO_MICROMIPS;
   16079     }
   16080   else if (opcode == 0x0398c1d0)
   16081     {
   16082       if (!micromips_p)
   16083 	return -1;
   16084       plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
   16085       other = STO_MICROMIPS;
   16086     }
   16087   else
   16088     {
   16089       plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
   16090       other = 0;
   16091     }
   16092 
   16093   s->the_bfd = abfd;
   16094   s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
   16095   s->section = plt;
   16096   s->value = 0;
   16097   s->name = names;
   16098   s->udata.i = other;
   16099   memcpy (names, pltname, sizeof (pltname));
   16100   names += sizeof (pltname);
   16101   ++s, ++n;
   16102 
   16103   pi = 0;
   16104   for (plt_offset = plt0_size;
   16105        plt_offset + 8 <= plt->size && s < send;
   16106        plt_offset += entry_size)
   16107     {
   16108       bfd_vma gotplt_addr;
   16109       const char *suffix;
   16110       bfd_vma gotplt_hi;
   16111       bfd_vma gotplt_lo;
   16112       size_t suffixlen;
   16113 
   16114       opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
   16115 
   16116       /* Check if the second word matches the expected MIPS16 instruction.  */
   16117       if (opcode == 0x651aeb00)
   16118 	{
   16119 	  if (micromips_p)
   16120 	    return -1;
   16121 	  /* Truncated table???  */
   16122 	  if (plt_offset + 16 > plt->size)
   16123 	    break;
   16124 	  gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
   16125 	  entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
   16126 	  suffixlen = sizeof (m16suffix);
   16127 	  suffix = m16suffix;
   16128 	  other = STO_MIPS16;
   16129 	}
   16130       /* Likewise the expected microMIPS instruction (no insn32 mode).  */
   16131       else if (opcode == 0xff220000)
   16132 	{
   16133 	  if (!micromips_p)
   16134 	    return -1;
   16135 	  gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
   16136 	  gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
   16137 	  gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
   16138 	  gotplt_lo <<= 2;
   16139 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16140 	  gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
   16141 	  entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
   16142 	  suffixlen = sizeof (microsuffix);
   16143 	  suffix = microsuffix;
   16144 	  other = STO_MICROMIPS;
   16145 	}
   16146       /* Likewise the expected microMIPS instruction (insn32 mode).  */
   16147       else if ((opcode & 0xffff0000) == 0xff2f0000)
   16148 	{
   16149 	  gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
   16150 	  gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
   16151 	  gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
   16152 	  gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
   16153 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16154 	  entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
   16155 	  suffixlen = sizeof (microsuffix);
   16156 	  suffix = microsuffix;
   16157 	  other = STO_MICROMIPS;
   16158 	}
   16159       /* Otherwise assume standard MIPS code.  */
   16160       else
   16161 	{
   16162 	  gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
   16163 	  gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
   16164 	  gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
   16165 	  gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
   16166 	  gotplt_addr = gotplt_hi + gotplt_lo;
   16167 	  entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
   16168 	  suffixlen = sizeof (mipssuffix);
   16169 	  suffix = mipssuffix;
   16170 	  other = 0;
   16171 	}
   16172       /* Truncated table???  */
   16173       if (plt_offset + entry_size > plt->size)
   16174 	break;
   16175 
   16176       for (i = 0;
   16177 	   i < count && p[pi].address != gotplt_addr;
   16178 	   i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
   16179 
   16180       if (i < count)
   16181 	{
   16182 	  size_t namelen;
   16183 	  size_t len;
   16184 
   16185 	  *s = **p[pi].sym_ptr_ptr;
   16186 	  /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   16187 	     we are defining a symbol, ensure one of them is set.  */
   16188 	  if ((s->flags & BSF_LOCAL) == 0)
   16189 	    s->flags |= BSF_GLOBAL;
   16190 	  s->flags |= BSF_SYNTHETIC;
   16191 	  s->section = plt;
   16192 	  s->value = plt_offset;
   16193 	  s->name = names;
   16194 	  s->udata.i = other;
   16195 
   16196 	  len = strlen ((*p[pi].sym_ptr_ptr)->name);
   16197 	  namelen = len + suffixlen;
   16198 	  if (names + namelen > nend)
   16199 	    break;
   16200 
   16201 	  memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
   16202 	  names += len;
   16203 	  memcpy (names, suffix, suffixlen);
   16204 	  names += suffixlen;
   16205 
   16206 	  ++s, ++n;
   16207 	  pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
   16208 	}
   16209     }
   16210 
   16211   free (plt_data);
   16212 
   16213   return n;
   16214 }
   16215 
   16216 void
   16217 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
   16218 {
   16219   struct mips_elf_link_hash_table *htab;
   16220   Elf_Internal_Ehdr *i_ehdrp;
   16221 
   16222   i_ehdrp = elf_elfheader (abfd);
   16223   if (link_info)
   16224     {
   16225       htab = mips_elf_hash_table (link_info);
   16226       BFD_ASSERT (htab != NULL);
   16227 
   16228       if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
   16229 	i_ehdrp->e_ident[EI_ABIVERSION] = 1;
   16230     }
   16231 
   16232   _bfd_elf_post_process_headers (abfd, link_info);
   16233 
   16234   if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
   16235       || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
   16236     i_ehdrp->e_ident[EI_ABIVERSION] = 3;
   16237 }
   16238 
   16239 int
   16240 _bfd_mips_elf_compact_eh_encoding (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
   16241 {
   16242   return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
   16243 }
   16244 
   16245 /* Return the opcode for can't unwind.  */
   16246 
   16247 int
   16248 _bfd_mips_elf_cant_unwind_opcode (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
   16249 {
   16250   return COMPACT_EH_CANT_UNWIND_OPCODE;
   16251 }
   16252