Back to index

cell-binutils  2.17cvs20070401
elfxx-mips.c
Go to the documentation of this file.
00001 /* MIPS-specific support for ELF
00002    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
00003    2003, 2004, 2005, 2006 Free Software Foundation, Inc.
00004 
00005    Most of the information added by Ian Lance Taylor, Cygnus Support,
00006    <ian@cygnus.com>.
00007    N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
00008    <mark@codesourcery.com>
00009    Traditional MIPS targets support added by Koundinya.K, Dansk Data
00010    Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
00011 
00012    This file is part of BFD, the Binary File Descriptor library.
00013 
00014    This program is free software; you can redistribute it and/or modify
00015    it under the terms of the GNU General Public License as published by
00016    the Free Software Foundation; either version 2 of the License, or
00017    (at your option) any later version.
00018 
00019    This program is distributed in the hope that it will be useful,
00020    but WITHOUT ANY WARRANTY; without even the implied warranty of
00021    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022    GNU General Public License for more details.
00023 
00024    You should have received a copy of the GNU General Public License
00025    along with this program; if not, write to the Free Software
00026    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00027 
00028 /* This file handles functionality common to the different MIPS ABI's.  */
00029 
00030 #include "bfd.h"
00031 #include "sysdep.h"
00032 #include "libbfd.h"
00033 #include "libiberty.h"
00034 #include "elf-bfd.h"
00035 #include "elfxx-mips.h"
00036 #include "elf/mips.h"
00037 #include "elf-vxworks.h"
00038 
00039 /* Get the ECOFF swapping routines.  */
00040 #include "coff/sym.h"
00041 #include "coff/symconst.h"
00042 #include "coff/ecoff.h"
00043 #include "coff/mips.h"
00044 
00045 #include "hashtab.h"
00046 
00047 /* This structure is used to hold information about one GOT entry.
00048    There are three types of entry:
00049 
00050       (1) absolute addresses
00051            (abfd == NULL)
00052       (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
00053            (abfd != NULL, symndx >= 0)
00054       (3) global and forced-local symbols
00055            (abfd != NULL, symndx == -1)
00056 
00057    Type (3) entries are treated differently for different types of GOT.
00058    In the "master" GOT -- i.e.  the one that describes every GOT
00059    reference needed in the link -- the mips_got_entry is keyed on both
00060    the symbol and the input bfd that references it.  If it turns out
00061    that we need multiple GOTs, we can then use this information to
00062    create separate GOTs for each input bfd.
00063 
00064    However, we want each of these separate GOTs to have at most one
00065    entry for a given symbol, so their type (3) entries are keyed only
00066    on the symbol.  The input bfd given by the "abfd" field is somewhat
00067    arbitrary in this case.
00068 
00069    This means that when there are multiple GOTs, each GOT has a unique
00070    mips_got_entry for every symbol within it.  We can therefore use the
00071    mips_got_entry fields (tls_type and gotidx) to track the symbol's
00072    GOT index.
00073 
00074    However, if it turns out that we need only a single GOT, we continue
00075    to use the master GOT to describe it.  There may therefore be several
00076    mips_got_entries for the same symbol, each with a different input bfd.
00077    We want to make sure that each symbol gets a unique GOT entry, so when
00078    there's a single GOT, we use the symbol's hash entry, not the
00079    mips_got_entry fields, to track a symbol's GOT index.  */
00080 struct mips_got_entry
00081 {
00082   /* The input bfd in which the symbol is defined.  */
00083   bfd *abfd;
00084   /* The index of the symbol, as stored in the relocation r_info, if
00085      we have a local symbol; -1 otherwise.  */
00086   long symndx;
00087   union
00088   {
00089     /* If abfd == NULL, an address that must be stored in the got.  */
00090     bfd_vma address;
00091     /* If abfd != NULL && symndx != -1, the addend of the relocation
00092        that should be added to the symbol value.  */
00093     bfd_vma addend;
00094     /* If abfd != NULL && symndx == -1, the hash table entry
00095        corresponding to a global symbol in the got (or, local, if
00096        h->forced_local).  */
00097     struct mips_elf_link_hash_entry *h;
00098   } d;
00099 
00100   /* The TLS types included in this GOT entry (specifically, GD and
00101      IE).  The GD and IE flags can be added as we encounter new
00102      relocations.  LDM can also be set; it will always be alone, not
00103      combined with any GD or IE flags.  An LDM GOT entry will be
00104      a local symbol entry with r_symndx == 0.  */
00105   unsigned char tls_type;
00106 
00107   /* The offset from the beginning of the .got section to the entry
00108      corresponding to this symbol+addend.  If it's a global symbol
00109      whose offset is yet to be decided, it's going to be -1.  */
00110   long gotidx;
00111 };
00112 
00113 /* This structure is used to hold .got information when linking.  */
00114 
00115 struct mips_got_info
00116 {
00117   /* The global symbol in the GOT with the lowest index in the dynamic
00118      symbol table.  */
00119   struct elf_link_hash_entry *global_gotsym;
00120   /* The number of global .got entries.  */
00121   unsigned int global_gotno;
00122   /* The number of .got slots used for TLS.  */
00123   unsigned int tls_gotno;
00124   /* The first unused TLS .got entry.  Used only during
00125      mips_elf_initialize_tls_index.  */
00126   unsigned int tls_assigned_gotno;
00127   /* The number of local .got entries.  */
00128   unsigned int local_gotno;
00129   /* The number of local .got entries we have used.  */
00130   unsigned int assigned_gotno;
00131   /* A hash table holding members of the got.  */
00132   struct htab *got_entries;
00133   /* A hash table mapping input bfds to other mips_got_info.  NULL
00134      unless multi-got was necessary.  */
00135   struct htab *bfd2got;
00136   /* In multi-got links, a pointer to the next got (err, rather, most
00137      of the time, it points to the previous got).  */
00138   struct mips_got_info *next;
00139   /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
00140      for none, or MINUS_TWO for not yet assigned.  This is needed
00141      because a single-GOT link may have multiple hash table entries
00142      for the LDM.  It does not get initialized in multi-GOT mode.  */
00143   bfd_vma tls_ldm_offset;
00144 };
00145 
00146 /* Map an input bfd to a got in a multi-got link.  */
00147 
00148 struct mips_elf_bfd2got_hash {
00149   bfd *bfd;
00150   struct mips_got_info *g;
00151 };
00152 
00153 /* Structure passed when traversing the bfd2got hash table, used to
00154    create and merge bfd's gots.  */
00155 
00156 struct mips_elf_got_per_bfd_arg
00157 {
00158   /* A hashtable that maps bfds to gots.  */
00159   htab_t bfd2got;
00160   /* The output bfd.  */
00161   bfd *obfd;
00162   /* The link information.  */
00163   struct bfd_link_info *info;
00164   /* A pointer to the primary got, i.e., the one that's going to get
00165      the implicit relocations from DT_MIPS_LOCAL_GOTNO and
00166      DT_MIPS_GOTSYM.  */
00167   struct mips_got_info *primary;
00168   /* A non-primary got we're trying to merge with other input bfd's
00169      gots.  */
00170   struct mips_got_info *current;
00171   /* The maximum number of got entries that can be addressed with a
00172      16-bit offset.  */
00173   unsigned int max_count;
00174   /* The number of local and global entries in the primary got.  */
00175   unsigned int primary_count;
00176   /* The number of local and global entries in the current got.  */
00177   unsigned int current_count;
00178   /* The total number of global entries which will live in the
00179      primary got and be automatically relocated.  This includes
00180      those not referenced by the primary GOT but included in
00181      the "master" GOT.  */
00182   unsigned int global_count;
00183 };
00184 
00185 /* Another structure used to pass arguments for got entries traversal.  */
00186 
00187 struct mips_elf_set_global_got_offset_arg
00188 {
00189   struct mips_got_info *g;
00190   int value;
00191   unsigned int needed_relocs;
00192   struct bfd_link_info *info;
00193 };
00194 
00195 /* A structure used to count TLS relocations or GOT entries, for GOT
00196    entry or ELF symbol table traversal.  */
00197 
00198 struct mips_elf_count_tls_arg
00199 {
00200   struct bfd_link_info *info;
00201   unsigned int needed;
00202 };
00203 
00204 struct _mips_elf_section_data
00205 {
00206   struct bfd_elf_section_data elf;
00207   union
00208   {
00209     struct mips_got_info *got_info;
00210     bfd_byte *tdata;
00211   } u;
00212 };
00213 
00214 #define mips_elf_section_data(sec) \
00215   ((struct _mips_elf_section_data *) elf_section_data (sec))
00216 
00217 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
00218    the dynamic symbols.  */
00219 
00220 struct mips_elf_hash_sort_data
00221 {
00222   /* The symbol in the global GOT with the lowest dynamic symbol table
00223      index.  */
00224   struct elf_link_hash_entry *low;
00225   /* The least dynamic symbol table index corresponding to a non-TLS
00226      symbol with a GOT entry.  */
00227   long min_got_dynindx;
00228   /* The greatest dynamic symbol table index corresponding to a symbol
00229      with a GOT entry that is not referenced (e.g., a dynamic symbol
00230      with dynamic relocations pointing to it from non-primary GOTs).  */
00231   long max_unref_got_dynindx;
00232   /* The greatest dynamic symbol table index not corresponding to a
00233      symbol without a GOT entry.  */
00234   long max_non_got_dynindx;
00235 };
00236 
00237 /* The MIPS ELF linker needs additional information for each symbol in
00238    the global hash table.  */
00239 
00240 struct mips_elf_link_hash_entry
00241 {
00242   struct elf_link_hash_entry root;
00243 
00244   /* External symbol information.  */
00245   EXTR esym;
00246 
00247   /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
00248      this symbol.  */
00249   unsigned int possibly_dynamic_relocs;
00250 
00251   /* If the R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 reloc is against
00252      a readonly section.  */
00253   bfd_boolean readonly_reloc;
00254 
00255   /* We must not create a stub for a symbol that has relocations
00256      related to taking the function's address, i.e. any but
00257      R_MIPS_CALL*16 ones -- see "MIPS ABI Supplement, 3rd Edition",
00258      p. 4-20.  */
00259   bfd_boolean no_fn_stub;
00260 
00261   /* If there is a stub that 32 bit functions should use to call this
00262      16 bit function, this points to the section containing the stub.  */
00263   asection *fn_stub;
00264 
00265   /* Whether we need the fn_stub; this is set if this symbol appears
00266      in any relocs other than a 16 bit call.  */
00267   bfd_boolean need_fn_stub;
00268 
00269   /* If there is a stub that 16 bit functions should use to call this
00270      32 bit function, this points to the section containing the stub.  */
00271   asection *call_stub;
00272 
00273   /* This is like the call_stub field, but it is used if the function
00274      being called returns a floating point value.  */
00275   asection *call_fp_stub;
00276 
00277   /* Are we forced local?  This will only be set if we have converted
00278      the initial global GOT entry to a local GOT entry.  */
00279   bfd_boolean forced_local;
00280 
00281   /* Are we referenced by some kind of relocation?  */
00282   bfd_boolean is_relocation_target;
00283 
00284   /* Are we referenced by branch relocations?  */
00285   bfd_boolean is_branch_target;
00286 
00287 #define GOT_NORMAL   0
00288 #define GOT_TLS_GD   1
00289 #define GOT_TLS_LDM  2
00290 #define GOT_TLS_IE   4
00291 #define GOT_TLS_OFFSET_DONE    0x40
00292 #define GOT_TLS_DONE    0x80
00293   unsigned char tls_type;
00294   /* This is only used in single-GOT mode; in multi-GOT mode there
00295      is one mips_got_entry per GOT entry, so the offset is stored
00296      there.  In single-GOT mode there may be many mips_got_entry
00297      structures all referring to the same GOT slot.  It might be
00298      possible to use root.got.offset instead, but that field is
00299      overloaded already.  */
00300   bfd_vma tls_got_offset;
00301 };
00302 
00303 /* MIPS ELF linker hash table.  */
00304 
00305 struct mips_elf_link_hash_table
00306 {
00307   struct elf_link_hash_table root;
00308 #if 0
00309   /* We no longer use this.  */
00310   /* String section indices for the dynamic section symbols.  */
00311   bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
00312 #endif
00313   /* The number of .rtproc entries.  */
00314   bfd_size_type procedure_count;
00315   /* The size of the .compact_rel section (if SGI_COMPAT).  */
00316   bfd_size_type compact_rel_size;
00317   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
00318      entry is set to the address of __rld_obj_head as in IRIX5.  */
00319   bfd_boolean use_rld_obj_head;
00320   /* This is the value of the __rld_map or __rld_obj_head symbol.  */
00321   bfd_vma rld_value;
00322   /* This is set if we see any mips16 stub sections.  */
00323   bfd_boolean mips16_stubs_seen;
00324   /* True if we're generating code for VxWorks.  */
00325   bfd_boolean is_vxworks;
00326   /* Shortcuts to some dynamic sections, or NULL if they are not
00327      being used.  */
00328   asection *srelbss;
00329   asection *sdynbss;
00330   asection *srelplt;
00331   asection *srelplt2;
00332   asection *sgotplt;
00333   asection *splt;
00334   /* The size of the PLT header in bytes (VxWorks only).  */
00335   bfd_vma plt_header_size;
00336   /* The size of a PLT entry in bytes (VxWorks only).  */
00337   bfd_vma plt_entry_size;
00338   /* The size of a function stub entry in bytes.  */
00339   bfd_vma function_stub_size;
00340 };
00341 
00342 #define TLS_RELOC_P(r_type) \
00343   (r_type == R_MIPS_TLS_DTPMOD32          \
00344    || r_type == R_MIPS_TLS_DTPMOD64              \
00345    || r_type == R_MIPS_TLS_DTPREL32              \
00346    || r_type == R_MIPS_TLS_DTPREL64              \
00347    || r_type == R_MIPS_TLS_GD                    \
00348    || r_type == R_MIPS_TLS_LDM                   \
00349    || r_type == R_MIPS_TLS_DTPREL_HI16           \
00350    || r_type == R_MIPS_TLS_DTPREL_LO16           \
00351    || r_type == R_MIPS_TLS_GOTTPREL              \
00352    || r_type == R_MIPS_TLS_TPREL32        \
00353    || r_type == R_MIPS_TLS_TPREL64        \
00354    || r_type == R_MIPS_TLS_TPREL_HI16            \
00355    || r_type == R_MIPS_TLS_TPREL_LO16)
00356 
00357 /* Structure used to pass information to mips_elf_output_extsym.  */
00358 
00359 struct extsym_info
00360 {
00361   bfd *abfd;
00362   struct bfd_link_info *info;
00363   struct ecoff_debug_info *debug;
00364   const struct ecoff_debug_swap *swap;
00365   bfd_boolean failed;
00366 };
00367 
00368 /* The names of the runtime procedure table symbols used on IRIX5.  */
00369 
00370 static const char * const mips_elf_dynsym_rtproc_names[] =
00371 {
00372   "_procedure_table",
00373   "_procedure_string_table",
00374   "_procedure_table_size",
00375   NULL
00376 };
00377 
00378 /* These structures are used to generate the .compact_rel section on
00379    IRIX5.  */
00380 
00381 typedef struct
00382 {
00383   unsigned long id1;        /* Always one?  */
00384   unsigned long num;        /* Number of compact relocation entries.  */
00385   unsigned long id2;        /* Always two?  */
00386   unsigned long offset;            /* The file offset of the first relocation.  */
00387   unsigned long reserved0;  /* Zero?  */
00388   unsigned long reserved1;  /* Zero?  */
00389 } Elf32_compact_rel;
00390 
00391 typedef struct
00392 {
00393   bfd_byte id1[4];
00394   bfd_byte num[4];
00395   bfd_byte id2[4];
00396   bfd_byte offset[4];
00397   bfd_byte reserved0[4];
00398   bfd_byte reserved1[4];
00399 } Elf32_External_compact_rel;
00400 
00401 typedef struct
00402 {
00403   unsigned int ctype : 1;   /* 1: long 0: short format. See below.  */
00404   unsigned int rtype : 4;   /* Relocation types. See below.  */
00405   unsigned int dist2to : 8;
00406   unsigned int relvaddr : 19;      /* (VADDR - vaddr of the previous entry)/ 4 */
00407   unsigned long konst;             /* KONST field. See below.  */
00408   unsigned long vaddr;             /* VADDR to be relocated.  */
00409 } Elf32_crinfo;
00410 
00411 typedef struct
00412 {
00413   unsigned int ctype : 1;   /* 1: long 0: short format. See below.  */
00414   unsigned int rtype : 4;   /* Relocation types. See below.  */
00415   unsigned int dist2to : 8;
00416   unsigned int relvaddr : 19;      /* (VADDR - vaddr of the previous entry)/ 4 */
00417   unsigned long konst;             /* KONST field. See below.  */
00418 } Elf32_crinfo2;
00419 
00420 typedef struct
00421 {
00422   bfd_byte info[4];
00423   bfd_byte konst[4];
00424   bfd_byte vaddr[4];
00425 } Elf32_External_crinfo;
00426 
00427 typedef struct
00428 {
00429   bfd_byte info[4];
00430   bfd_byte konst[4];
00431 } Elf32_External_crinfo2;
00432 
00433 /* These are the constants used to swap the bitfields in a crinfo.  */
00434 
00435 #define CRINFO_CTYPE (0x1)
00436 #define CRINFO_CTYPE_SH (31)
00437 #define CRINFO_RTYPE (0xf)
00438 #define CRINFO_RTYPE_SH (27)
00439 #define CRINFO_DIST2TO (0xff)
00440 #define CRINFO_DIST2TO_SH (19)
00441 #define CRINFO_RELVADDR (0x7ffff)
00442 #define CRINFO_RELVADDR_SH (0)
00443 
00444 /* A compact relocation info has long (3 words) or short (2 words)
00445    formats.  A short format doesn't have VADDR field and relvaddr
00446    fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
00447 #define CRF_MIPS_LONG                     1
00448 #define CRF_MIPS_SHORT                    0
00449 
00450 /* There are 4 types of compact relocation at least. The value KONST
00451    has different meaning for each type:
00452 
00453    (type)            (konst)
00454    CT_MIPS_REL32     Address in data
00455    CT_MIPS_WORD             Address in word (XXX)
00456    CT_MIPS_GPHI_LO   GP - vaddr
00457    CT_MIPS_JMPAD     Address to jump
00458    */
00459 
00460 #define CRT_MIPS_REL32                    0xa
00461 #define CRT_MIPS_WORD                     0xb
00462 #define CRT_MIPS_GPHI_LO           0xc
00463 #define CRT_MIPS_JMPAD                    0xd
00464 
00465 #define mips_elf_set_cr_format(x,format)  ((x).ctype = (format))
00466 #define mips_elf_set_cr_type(x,type)             ((x).rtype = (type))
00467 #define mips_elf_set_cr_dist2to(x,v)             ((x).dist2to = (v))
00468 #define mips_elf_set_cr_relvaddr(x,d)            ((x).relvaddr = (d)<<2)
00469 
00470 /* The structure of the runtime procedure descriptor created by the
00471    loader for use by the static exception system.  */
00472 
00473 typedef struct runtime_pdr {
00474        bfd_vma       adr;          /* Memory address of start of procedure.  */
00475        long   regmask;      /* Save register mask.  */
00476        long   regoffset;    /* Save register offset.  */
00477        long   fregmask;     /* Save floating point register mask.  */
00478        long   fregoffset;   /* Save floating point register offset.  */
00479        long   frameoffset;  /* Frame size.  */
00480        short  framereg;     /* Frame pointer register.  */
00481        short  pcreg;        /* Offset or reg of return pc.  */
00482        long   irpss;        /* Index into the runtime string table.  */
00483        long   reserved;
00484        struct exception_info *exception_info;/* Pointer to exception array.  */
00485 } RPDR, *pRPDR;
00486 #define cbRPDR sizeof (RPDR)
00487 #define rpdNil ((pRPDR) 0)
00488 
00489 static struct mips_got_entry *mips_elf_create_local_got_entry
00490   (bfd *, struct bfd_link_info *, bfd *, struct mips_got_info *, asection *,
00491    asection *, bfd_vma, unsigned long, struct mips_elf_link_hash_entry *, int);
00492 static bfd_boolean mips_elf_sort_hash_table_f
00493   (struct mips_elf_link_hash_entry *, void *);
00494 static bfd_vma mips_elf_high
00495   (bfd_vma);
00496 static bfd_boolean mips16_stub_section_p
00497   (bfd *, asection *);
00498 static bfd_boolean mips_elf_create_dynamic_relocation
00499   (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
00500    struct mips_elf_link_hash_entry *, asection *, bfd_vma,
00501    bfd_vma *, asection *);
00502 static hashval_t mips_elf_got_entry_hash
00503   (const void *);
00504 static bfd_vma mips_elf_adjust_gp
00505   (bfd *, struct mips_got_info *, bfd *);
00506 static struct mips_got_info *mips_elf_got_for_ibfd
00507   (struct mips_got_info *, bfd *);
00508 
00509 /* This will be used when we sort the dynamic relocation records.  */
00510 static bfd *reldyn_sorting_bfd;
00511 
00512 /* Nonzero if ABFD is using the N32 ABI.  */
00513 #define ABI_N32_P(abfd) \
00514   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
00515 
00516 /* Nonzero if ABFD is using the N64 ABI.  */
00517 #define ABI_64_P(abfd) \
00518   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
00519 
00520 /* Nonzero if ABFD is using NewABI conventions.  */
00521 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
00522 
00523 /* The IRIX compatibility level we are striving for.  */
00524 #define IRIX_COMPAT(abfd) \
00525   (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
00526 
00527 /* Whether we are trying to be compatible with IRIX at all.  */
00528 #define SGI_COMPAT(abfd) \
00529   (IRIX_COMPAT (abfd) != ict_none)
00530 
00531 /* The name of the options section.  */
00532 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
00533   (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
00534 
00535 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
00536    Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
00537 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
00538   (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
00539 
00540 /* Whether the section is readonly.  */
00541 #define MIPS_ELF_READONLY_SECTION(sec) \
00542   ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))        \
00543    == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
00544 
00545 /* The name of the stub section.  */
00546 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
00547 
00548 /* The size of an external REL relocation.  */
00549 #define MIPS_ELF_REL_SIZE(abfd) \
00550   (get_elf_backend_data (abfd)->s->sizeof_rel)
00551 
00552 /* The size of an external RELA relocation.  */
00553 #define MIPS_ELF_RELA_SIZE(abfd) \
00554   (get_elf_backend_data (abfd)->s->sizeof_rela)
00555 
00556 /* The size of an external dynamic table entry.  */
00557 #define MIPS_ELF_DYN_SIZE(abfd) \
00558   (get_elf_backend_data (abfd)->s->sizeof_dyn)
00559 
00560 /* The size of a GOT entry.  */
00561 #define MIPS_ELF_GOT_SIZE(abfd) \
00562   (get_elf_backend_data (abfd)->s->arch_size / 8)
00563 
00564 /* The size of a symbol-table entry.  */
00565 #define MIPS_ELF_SYM_SIZE(abfd) \
00566   (get_elf_backend_data (abfd)->s->sizeof_sym)
00567 
00568 /* The default alignment for sections, as a power of two.  */
00569 #define MIPS_ELF_LOG_FILE_ALIGN(abfd)                          \
00570   (get_elf_backend_data (abfd)->s->log_file_align)
00571 
00572 /* Get word-sized data.  */
00573 #define MIPS_ELF_GET_WORD(abfd, ptr) \
00574   (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
00575 
00576 /* Put out word-sized data.  */
00577 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
00578   (ABI_64_P (abfd)                        \
00579    ? bfd_put_64 (abfd, val, ptr)          \
00580    : bfd_put_32 (abfd, val, ptr))
00581 
00582 /* Add a dynamic symbol table-entry.  */
00583 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)      \
00584   _bfd_elf_add_dynamic_entry (info, tag, val)
00585 
00586 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)                    \
00587   (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
00588 
00589 /* Determine whether the internal relocation of index REL_IDX is REL
00590    (zero) or RELA (non-zero).  The assumption is that, if there are
00591    two relocation sections for this section, one of them is REL and
00592    the other is RELA.  If the index of the relocation we're testing is
00593    in range for the first relocation section, check that the external
00594    relocation size is that for RELA.  It is also assumed that, if
00595    rel_idx is not in range for the first section, and this first
00596    section contains REL relocs, then the relocation is in the second
00597    section, that is RELA.  */
00598 #define MIPS_RELOC_RELA_P(abfd, sec, rel_idx)                         \
00599   ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr)               \
00600     * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel            \
00601     > (bfd_vma)(rel_idx))                                      \
00602    == (elf_section_data (sec)->rel_hdr.sh_entsize                     \
00603        == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela)             \
00604           : sizeof (Elf32_External_Rela))))
00605 
00606 /* The name of the dynamic relocation section.  */
00607 #define MIPS_ELF_REL_DYN_NAME(INFO) \
00608   (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
00609 
00610 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
00611    from smaller values.  Start with zero, widen, *then* decrement.  */
00612 #define MINUS_ONE    (((bfd_vma)0) - 1)
00613 #define MINUS_TWO    (((bfd_vma)0) - 2)
00614 
00615 /* The number of local .got entries we reserve.  */
00616 #define MIPS_RESERVED_GOTNO(INFO) \
00617   (mips_elf_hash_table (INFO)->is_vxworks ? 3 : 2)
00618 
00619 /* The offset of $gp from the beginning of the .got section.  */
00620 #define ELF_MIPS_GP_OFFSET(INFO) \
00621   (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
00622 
00623 /* The maximum size of the GOT for it to be addressable using 16-bit
00624    offsets from $gp.  */
00625 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
00626 
00627 /* Instructions which appear in a stub.  */
00628 #define STUB_LW(abfd)                                                 \
00629   ((ABI_64_P (abfd)                                            \
00630     ? 0xdf998010                          /* ld t9,0x8010(gp) */      \
00631     : 0x8f998010))                        /* lw t9,0x8010(gp) */
00632 #define STUB_MOVE(abfd)                                               \
00633    ((ABI_64_P (abfd)                                           \
00634      ? 0x03e0782d                         /* daddu t7,ra */    \
00635      : 0x03e07821))                       /* addu t7,ra */
00636 #define STUB_LUI(VAL) (0x3c180000 + (VAL))       /* lui t8,VAL */
00637 #define STUB_JALR 0x0320f809                     /* jalr t9,ra */
00638 #define STUB_ORI(VAL) (0x37180000 + (VAL))       /* ori t8,t8,VAL */
00639 #define STUB_LI16U(VAL) (0x34180000 + (VAL))     /* ori t8,zero,VAL unsigned */
00640 #define STUB_LI16S(abfd, VAL)                                         \
00641    ((ABI_64_P (abfd)                                           \
00642     ? (0x64180000 + (VAL))  /* daddiu t8,zero,VAL sign extended */    \
00643     : (0x24180000 + (VAL))))       /* addiu t8,zero,VAL sign extended */
00644 
00645 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
00646 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
00647 
00648 /* The name of the dynamic interpreter.  This is put in the .interp
00649    section.  */
00650 
00651 #define ELF_DYNAMIC_INTERPRETER(abfd)            \
00652    (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1"    \
00653     : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1"   \
00654     : "/usr/lib/libc.so.1")
00655 
00656 #ifdef BFD64
00657 #define MNAME(bfd,pre,pos) \
00658   (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
00659 #define ELF_R_SYM(bfd, i)                               \
00660   (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
00661 #define ELF_R_TYPE(bfd, i)                              \
00662   (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
00663 #define ELF_R_INFO(bfd, s, t)                                  \
00664   (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
00665 #else
00666 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
00667 #define ELF_R_SYM(bfd, i)                               \
00668   (ELF32_R_SYM (i))
00669 #define ELF_R_TYPE(bfd, i)                              \
00670   (ELF32_R_TYPE (i))
00671 #define ELF_R_INFO(bfd, s, t)                                  \
00672   (ELF32_R_INFO (s, t))
00673 #endif
00674 
00675   /* The mips16 compiler uses a couple of special sections to handle
00676      floating point arguments.
00677 
00678      Section names that look like .mips16.fn.FNNAME contain stubs that
00679      copy floating point arguments from the fp regs to the gp regs and
00680      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
00681      call should be redirected to the stub instead.  If no 32 bit
00682      function calls FNNAME, the stub should be discarded.  We need to
00683      consider any reference to the function, not just a call, because
00684      if the address of the function is taken we will need the stub,
00685      since the address might be passed to a 32 bit function.
00686 
00687      Section names that look like .mips16.call.FNNAME contain stubs
00688      that copy floating point arguments from the gp regs to the fp
00689      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
00690      then any 16 bit function that calls FNNAME should be redirected
00691      to the stub instead.  If FNNAME is not a 32 bit function, the
00692      stub should be discarded.
00693 
00694      .mips16.call.fp.FNNAME sections are similar, but contain stubs
00695      which call FNNAME and then copy the return value from the fp regs
00696      to the gp regs.  These stubs store the return value in $18 while
00697      calling FNNAME; any function which might call one of these stubs
00698      must arrange to save $18 around the call.  (This case is not
00699      needed for 32 bit functions that call 16 bit functions, because
00700      16 bit functions always return floating point values in both
00701      $f0/$f1 and $2/$3.)
00702 
00703      Note that in all cases FNNAME might be defined statically.
00704      Therefore, FNNAME is not used literally.  Instead, the relocation
00705      information will indicate which symbol the section is for.
00706 
00707      We record any stubs that we find in the symbol table.  */
00708 
00709 #define FN_STUB ".mips16.fn."
00710 #define CALL_STUB ".mips16.call."
00711 #define CALL_FP_STUB ".mips16.call.fp."
00712 
00713 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
00714 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
00715 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
00716 
00717 /* The format of the first PLT entry in a VxWorks executable.  */
00718 static const bfd_vma mips_vxworks_exec_plt0_entry[] = {
00719   0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)            */
00720   0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_)      */
00721   0x8f390008, /* lw t9, 8(t9)                                  */
00722   0x00000000, /* nop                                    */
00723   0x03200008, /* jr t9                                  */
00724   0x00000000  /* nop                                    */
00725 };
00726 
00727 /* The format of subsequent PLT entries.  */
00728 static const bfd_vma mips_vxworks_exec_plt_entry[] = {
00729   0x10000000, /* b .PLT_resolver                 */
00730   0x24180000, /* li t8, <pltindex>               */
00731   0x3c190000, /* lui t9, %hi(<.got.plt slot>)           */
00732   0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>)     */
00733   0x8f390000, /* lw t9, 0(t9)                           */
00734   0x00000000, /* nop                             */
00735   0x03200008, /* jr t9                           */
00736   0x00000000  /* nop                             */
00737 };
00738 
00739 /* The format of the first PLT entry in a VxWorks shared object.  */
00740 static const bfd_vma mips_vxworks_shared_plt0_entry[] = {
00741   0x8f990008, /* lw t9, 8(gp)             */
00742   0x00000000, /* nop               */
00743   0x03200008, /* jr t9             */
00744   0x00000000, /* nop               */
00745   0x00000000, /* nop               */
00746   0x00000000  /* nop               */
00747 };
00748 
00749 /* The format of subsequent PLT entries.  */
00750 static const bfd_vma mips_vxworks_shared_plt_entry[] = {
00751   0x10000000, /* b .PLT_resolver   */
00752   0x24180000  /* li t8, <pltindex> */
00753 };
00754 
00755 /* Look up an entry in a MIPS ELF linker hash table.  */
00756 
00757 #define mips_elf_link_hash_lookup(table, string, create, copy, follow)       \
00758   ((struct mips_elf_link_hash_entry *)                                \
00759    elf_link_hash_lookup (&(table)->root, (string), (create),          \
00760                       (copy), (follow)))
00761 
00762 /* Traverse a MIPS ELF linker hash table.  */
00763 
00764 #define mips_elf_link_hash_traverse(table, func, info)                \
00765   (elf_link_hash_traverse                                      \
00766    (&(table)->root,                                            \
00767     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),  \
00768     (info)))
00769 
00770 /* Get the MIPS ELF linker hash table from a link_info structure.  */
00771 
00772 #define mips_elf_hash_table(p) \
00773   ((struct mips_elf_link_hash_table *) ((p)->hash))
00774 
00775 /* Find the base offsets for thread-local storage in this object,
00776    for GD/LD and IE/LE respectively.  */
00777 
00778 #define TP_OFFSET 0x7000
00779 #define DTP_OFFSET 0x8000
00780 
00781 static bfd_vma
00782 dtprel_base (struct bfd_link_info *info)
00783 {
00784   /* If tls_sec is NULL, we should have signalled an error already.  */
00785   if (elf_hash_table (info)->tls_sec == NULL)
00786     return 0;
00787   return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
00788 }
00789 
00790 static bfd_vma
00791 tprel_base (struct bfd_link_info *info)
00792 {
00793   /* If tls_sec is NULL, we should have signalled an error already.  */
00794   if (elf_hash_table (info)->tls_sec == NULL)
00795     return 0;
00796   return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
00797 }
00798 
00799 /* Create an entry in a MIPS ELF linker hash table.  */
00800 
00801 static struct bfd_hash_entry *
00802 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
00803                          struct bfd_hash_table *table, const char *string)
00804 {
00805   struct mips_elf_link_hash_entry *ret =
00806     (struct mips_elf_link_hash_entry *) entry;
00807 
00808   /* Allocate the structure if it has not already been allocated by a
00809      subclass.  */
00810   if (ret == NULL)
00811     ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
00812   if (ret == NULL)
00813     return (struct bfd_hash_entry *) ret;
00814 
00815   /* Call the allocation method of the superclass.  */
00816   ret = ((struct mips_elf_link_hash_entry *)
00817         _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
00818                                  table, string));
00819   if (ret != NULL)
00820     {
00821       /* Set local fields.  */
00822       memset (&ret->esym, 0, sizeof (EXTR));
00823       /* We use -2 as a marker to indicate that the information has
00824         not been set.  -1 means there is no associated ifd.  */
00825       ret->esym.ifd = -2;
00826       ret->possibly_dynamic_relocs = 0;
00827       ret->readonly_reloc = FALSE;
00828       ret->no_fn_stub = FALSE;
00829       ret->fn_stub = NULL;
00830       ret->need_fn_stub = FALSE;
00831       ret->call_stub = NULL;
00832       ret->call_fp_stub = NULL;
00833       ret->forced_local = FALSE;
00834       ret->is_branch_target = FALSE;
00835       ret->is_relocation_target = FALSE;
00836       ret->tls_type = GOT_NORMAL;
00837     }
00838 
00839   return (struct bfd_hash_entry *) ret;
00840 }
00841 
00842 bfd_boolean
00843 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
00844 {
00845   if (!sec->used_by_bfd)
00846     {
00847       struct _mips_elf_section_data *sdata;
00848       bfd_size_type amt = sizeof (*sdata);
00849 
00850       sdata = bfd_zalloc (abfd, amt);
00851       if (sdata == NULL)
00852        return FALSE;
00853       sec->used_by_bfd = sdata;
00854     }
00855 
00856   return _bfd_elf_new_section_hook (abfd, sec);
00857 }
00858 
00859 /* Read ECOFF debugging information from a .mdebug section into a
00860    ecoff_debug_info structure.  */
00861 
00862 bfd_boolean
00863 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
00864                             struct ecoff_debug_info *debug)
00865 {
00866   HDRR *symhdr;
00867   const struct ecoff_debug_swap *swap;
00868   char *ext_hdr;
00869 
00870   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
00871   memset (debug, 0, sizeof (*debug));
00872 
00873   ext_hdr = bfd_malloc (swap->external_hdr_size);
00874   if (ext_hdr == NULL && swap->external_hdr_size != 0)
00875     goto error_return;
00876 
00877   if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
00878                               swap->external_hdr_size))
00879     goto error_return;
00880 
00881   symhdr = &debug->symbolic_header;
00882   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
00883 
00884   /* The symbolic header contains absolute file offsets and sizes to
00885      read.  */
00886 #define READ(ptr, offset, count, size, type)                          \
00887   if (symhdr->count == 0)                                      \
00888     debug->ptr = NULL;                                                \
00889   else                                                         \
00890     {                                                          \
00891       bfd_size_type amt = (bfd_size_type) size * symhdr->count;              \
00892       debug->ptr = bfd_malloc (amt);                                  \
00893       if (debug->ptr == NULL)                                         \
00894        goto error_return;                                      \
00895       if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0              \
00896          || bfd_bread (debug->ptr, amt, abfd) != amt)                 \
00897        goto error_return;                                      \
00898     }
00899 
00900   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
00901   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
00902   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
00903   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
00904   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
00905   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
00906        union aux_ext *);
00907   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
00908   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
00909   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
00910   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
00911   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
00912 #undef READ
00913 
00914   debug->fdr = NULL;
00915 
00916   return TRUE;
00917 
00918  error_return:
00919   if (ext_hdr != NULL)
00920     free (ext_hdr);
00921   if (debug->line != NULL)
00922     free (debug->line);
00923   if (debug->external_dnr != NULL)
00924     free (debug->external_dnr);
00925   if (debug->external_pdr != NULL)
00926     free (debug->external_pdr);
00927   if (debug->external_sym != NULL)
00928     free (debug->external_sym);
00929   if (debug->external_opt != NULL)
00930     free (debug->external_opt);
00931   if (debug->external_aux != NULL)
00932     free (debug->external_aux);
00933   if (debug->ss != NULL)
00934     free (debug->ss);
00935   if (debug->ssext != NULL)
00936     free (debug->ssext);
00937   if (debug->external_fdr != NULL)
00938     free (debug->external_fdr);
00939   if (debug->external_rfd != NULL)
00940     free (debug->external_rfd);
00941   if (debug->external_ext != NULL)
00942     free (debug->external_ext);
00943   return FALSE;
00944 }
00945 
00946 /* Swap RPDR (runtime procedure table entry) for output.  */
00947 
00948 static void
00949 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
00950 {
00951   H_PUT_S32 (abfd, in->adr, ex->p_adr);
00952   H_PUT_32 (abfd, in->regmask, ex->p_regmask);
00953   H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
00954   H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
00955   H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
00956   H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
00957 
00958   H_PUT_16 (abfd, in->framereg, ex->p_framereg);
00959   H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
00960 
00961   H_PUT_32 (abfd, in->irpss, ex->p_irpss);
00962 }
00963 
00964 /* Create a runtime procedure table from the .mdebug section.  */
00965 
00966 static bfd_boolean
00967 mips_elf_create_procedure_table (void *handle, bfd *abfd,
00968                              struct bfd_link_info *info, asection *s,
00969                              struct ecoff_debug_info *debug)
00970 {
00971   const struct ecoff_debug_swap *swap;
00972   HDRR *hdr = &debug->symbolic_header;
00973   RPDR *rpdr, *rp;
00974   struct rpdr_ext *erp;
00975   void *rtproc;
00976   struct pdr_ext *epdr;
00977   struct sym_ext *esym;
00978   char *ss, **sv;
00979   char *str;
00980   bfd_size_type size;
00981   bfd_size_type count;
00982   unsigned long sindex;
00983   unsigned long i;
00984   PDR pdr;
00985   SYMR sym;
00986   const char *no_name_func = _("static procedure (no name)");
00987 
00988   epdr = NULL;
00989   rpdr = NULL;
00990   esym = NULL;
00991   ss = NULL;
00992   sv = NULL;
00993 
00994   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
00995 
00996   sindex = strlen (no_name_func) + 1;
00997   count = hdr->ipdMax;
00998   if (count > 0)
00999     {
01000       size = swap->external_pdr_size;
01001 
01002       epdr = bfd_malloc (size * count);
01003       if (epdr == NULL)
01004        goto error_return;
01005 
01006       if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
01007        goto error_return;
01008 
01009       size = sizeof (RPDR);
01010       rp = rpdr = bfd_malloc (size * count);
01011       if (rpdr == NULL)
01012        goto error_return;
01013 
01014       size = sizeof (char *);
01015       sv = bfd_malloc (size * count);
01016       if (sv == NULL)
01017        goto error_return;
01018 
01019       count = hdr->isymMax;
01020       size = swap->external_sym_size;
01021       esym = bfd_malloc (size * count);
01022       if (esym == NULL)
01023        goto error_return;
01024 
01025       if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
01026        goto error_return;
01027 
01028       count = hdr->issMax;
01029       ss = bfd_malloc (count);
01030       if (ss == NULL)
01031        goto error_return;
01032       if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
01033        goto error_return;
01034 
01035       count = hdr->ipdMax;
01036       for (i = 0; i < (unsigned long) count; i++, rp++)
01037        {
01038          (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
01039          (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
01040          rp->adr = sym.value;
01041          rp->regmask = pdr.regmask;
01042          rp->regoffset = pdr.regoffset;
01043          rp->fregmask = pdr.fregmask;
01044          rp->fregoffset = pdr.fregoffset;
01045          rp->frameoffset = pdr.frameoffset;
01046          rp->framereg = pdr.framereg;
01047          rp->pcreg = pdr.pcreg;
01048          rp->irpss = sindex;
01049          sv[i] = ss + sym.iss;
01050          sindex += strlen (sv[i]) + 1;
01051        }
01052     }
01053 
01054   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
01055   size = BFD_ALIGN (size, 16);
01056   rtproc = bfd_alloc (abfd, size);
01057   if (rtproc == NULL)
01058     {
01059       mips_elf_hash_table (info)->procedure_count = 0;
01060       goto error_return;
01061     }
01062 
01063   mips_elf_hash_table (info)->procedure_count = count + 2;
01064 
01065   erp = rtproc;
01066   memset (erp, 0, sizeof (struct rpdr_ext));
01067   erp++;
01068   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
01069   strcpy (str, no_name_func);
01070   str += strlen (no_name_func) + 1;
01071   for (i = 0; i < count; i++)
01072     {
01073       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
01074       strcpy (str, sv[i]);
01075       str += strlen (sv[i]) + 1;
01076     }
01077   H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
01078 
01079   /* Set the size and contents of .rtproc section.  */
01080   s->size = size;
01081   s->contents = rtproc;
01082 
01083   /* Skip this section later on (I don't think this currently
01084      matters, but someday it might).  */
01085   s->map_head.link_order = NULL;
01086 
01087   if (epdr != NULL)
01088     free (epdr);
01089   if (rpdr != NULL)
01090     free (rpdr);
01091   if (esym != NULL)
01092     free (esym);
01093   if (ss != NULL)
01094     free (ss);
01095   if (sv != NULL)
01096     free (sv);
01097 
01098   return TRUE;
01099 
01100  error_return:
01101   if (epdr != NULL)
01102     free (epdr);
01103   if (rpdr != NULL)
01104     free (rpdr);
01105   if (esym != NULL)
01106     free (esym);
01107   if (ss != NULL)
01108     free (ss);
01109   if (sv != NULL)
01110     free (sv);
01111   return FALSE;
01112 }
01113 
01114 /* Check the mips16 stubs for a particular symbol, and see if we can
01115    discard them.  */
01116 
01117 static bfd_boolean
01118 mips_elf_check_mips16_stubs (struct mips_elf_link_hash_entry *h,
01119                           void *data ATTRIBUTE_UNUSED)
01120 {
01121   if (h->root.root.type == bfd_link_hash_warning)
01122     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
01123 
01124   if (h->fn_stub != NULL
01125       && ! h->need_fn_stub)
01126     {
01127       /* We don't need the fn_stub; the only references to this symbol
01128          are 16 bit calls.  Clobber the size to 0 to prevent it from
01129          being included in the link.  */
01130       h->fn_stub->size = 0;
01131       h->fn_stub->flags &= ~SEC_RELOC;
01132       h->fn_stub->reloc_count = 0;
01133       h->fn_stub->flags |= SEC_EXCLUDE;
01134     }
01135 
01136   if (h->call_stub != NULL
01137       && h->root.other == STO_MIPS16)
01138     {
01139       /* We don't need the call_stub; this is a 16 bit function, so
01140          calls from other 16 bit functions are OK.  Clobber the size
01141          to 0 to prevent it from being included in the link.  */
01142       h->call_stub->size = 0;
01143       h->call_stub->flags &= ~SEC_RELOC;
01144       h->call_stub->reloc_count = 0;
01145       h->call_stub->flags |= SEC_EXCLUDE;
01146     }
01147 
01148   if (h->call_fp_stub != NULL
01149       && h->root.other == STO_MIPS16)
01150     {
01151       /* We don't need the call_stub; this is a 16 bit function, so
01152          calls from other 16 bit functions are OK.  Clobber the size
01153          to 0 to prevent it from being included in the link.  */
01154       h->call_fp_stub->size = 0;
01155       h->call_fp_stub->flags &= ~SEC_RELOC;
01156       h->call_fp_stub->reloc_count = 0;
01157       h->call_fp_stub->flags |= SEC_EXCLUDE;
01158     }
01159 
01160   return TRUE;
01161 }
01162 
01163 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
01164    Most mips16 instructions are 16 bits, but these instructions
01165    are 32 bits.
01166 
01167    The format of these instructions is:
01168 
01169    +--------------+--------------------------------+
01170    |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
01171    +--------------+--------------------------------+
01172    |                Immediate  15:0                |
01173    +-----------------------------------------------+
01174 
01175    JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
01176    Note that the immediate value in the first word is swapped.
01177 
01178    When producing a relocatable object file, R_MIPS16_26 is
01179    handled mostly like R_MIPS_26.  In particular, the addend is
01180    stored as a straight 26-bit value in a 32-bit instruction.
01181    (gas makes life simpler for itself by never adjusting a
01182    R_MIPS16_26 reloc to be against a section, so the addend is
01183    always zero).  However, the 32 bit instruction is stored as 2
01184    16-bit values, rather than a single 32-bit value.  In a
01185    big-endian file, the result is the same; in a little-endian
01186    file, the two 16-bit halves of the 32 bit value are swapped.
01187    This is so that a disassembler can recognize the jal
01188    instruction.
01189 
01190    When doing a final link, R_MIPS16_26 is treated as a 32 bit
01191    instruction stored as two 16-bit values.  The addend A is the
01192    contents of the targ26 field.  The calculation is the same as
01193    R_MIPS_26.  When storing the calculated value, reorder the
01194    immediate value as shown above, and don't forget to store the
01195    value as two 16-bit values.
01196 
01197    To put it in MIPS ABI terms, the relocation field is T-targ26-16,
01198    defined as
01199 
01200    big-endian:
01201    +--------+----------------------+
01202    |        |                      |
01203    |        |    targ26-16         |
01204    |31    26|25                   0|
01205    +--------+----------------------+
01206 
01207    little-endian:
01208    +----------+------+-------------+
01209    |          |      |             |
01210    |  sub1    |      |     sub2    |
01211    |0        9|10  15|16         31|
01212    +----------+--------------------+
01213    where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
01214    ((sub1 << 16) | sub2)).
01215 
01216    When producing a relocatable object file, the calculation is
01217    (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
01218    When producing a fully linked file, the calculation is
01219    let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
01220    ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
01221 
01222    R_MIPS16_GPREL is used for GP-relative addressing in mips16
01223    mode.  A typical instruction will have a format like this:
01224 
01225    +--------------+--------------------------------+
01226    |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
01227    +--------------+--------------------------------+
01228    |    Major     |   rx   |   ry   |   Imm  4:0   |
01229    +--------------+--------------------------------+
01230 
01231    EXTEND is the five bit value 11110.  Major is the instruction
01232    opcode.
01233 
01234    This is handled exactly like R_MIPS_GPREL16, except that the
01235    addend is retrieved and stored as shown in this diagram; that
01236    is, the Imm fields above replace the V-rel16 field.
01237 
01238    All we need to do here is shuffle the bits appropriately.  As
01239    above, the two 16-bit halves must be swapped on a
01240    little-endian system.
01241 
01242    R_MIPS16_HI16 and R_MIPS16_LO16 are used in mips16 mode to
01243    access data when neither GP-relative nor PC-relative addressing
01244    can be used.  They are handled like R_MIPS_HI16 and R_MIPS_LO16,
01245    except that the addend is retrieved and stored as shown above
01246    for R_MIPS16_GPREL.
01247   */
01248 void
01249 _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
01250                              bfd_boolean jal_shuffle, bfd_byte *data)
01251 {
01252   bfd_vma extend, insn, val;
01253 
01254   if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
01255       && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
01256     return;
01257 
01258   /* Pick up the mips16 extend instruction and the real instruction.  */
01259   extend = bfd_get_16 (abfd, data);
01260   insn = bfd_get_16 (abfd, data + 2);
01261   if (r_type == R_MIPS16_26)
01262     {
01263       if (jal_shuffle)
01264        val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
01265              | ((extend & 0x1f) << 21) | insn;
01266       else
01267        val = extend << 16 | insn;
01268     }
01269   else
01270     val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
01271          | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
01272   bfd_put_32 (abfd, val, data);
01273 }
01274 
01275 void
01276 _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
01277                             bfd_boolean jal_shuffle, bfd_byte *data)
01278 {
01279   bfd_vma extend, insn, val;
01280 
01281   if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
01282       && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
01283     return;
01284 
01285   val = bfd_get_32 (abfd, data);
01286   if (r_type == R_MIPS16_26)
01287     {
01288       if (jal_shuffle)
01289        {
01290          insn = val & 0xffff;
01291          extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
01292                  | ((val >> 21) & 0x1f);
01293        }
01294       else
01295        {
01296          insn = val & 0xffff;
01297          extend = val >> 16;
01298        }
01299     }
01300   else
01301     {
01302       insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
01303       extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
01304     }
01305   bfd_put_16 (abfd, insn, data + 2);
01306   bfd_put_16 (abfd, extend, data);
01307 }
01308 
01309 bfd_reloc_status_type
01310 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
01311                             arelent *reloc_entry, asection *input_section,
01312                             bfd_boolean relocatable, void *data, bfd_vma gp)
01313 {
01314   bfd_vma relocation;
01315   bfd_signed_vma val;
01316   bfd_reloc_status_type status;
01317 
01318   if (bfd_is_com_section (symbol->section))
01319     relocation = 0;
01320   else
01321     relocation = symbol->value;
01322 
01323   relocation += symbol->section->output_section->vma;
01324   relocation += symbol->section->output_offset;
01325 
01326   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
01327     return bfd_reloc_outofrange;
01328 
01329   /* Set val to the offset into the section or symbol.  */
01330   val = reloc_entry->addend;
01331 
01332   _bfd_mips_elf_sign_extend (val, 16);
01333 
01334   /* Adjust val for the final section location and GP value.  If we
01335      are producing relocatable output, we don't want to do this for
01336      an external symbol.  */
01337   if (! relocatable
01338       || (symbol->flags & BSF_SECTION_SYM) != 0)
01339     val += relocation - gp;
01340 
01341   if (reloc_entry->howto->partial_inplace)
01342     {
01343       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
01344                                    (bfd_byte *) data
01345                                    + reloc_entry->address);
01346       if (status != bfd_reloc_ok)
01347        return status;
01348     }
01349   else
01350     reloc_entry->addend = val;
01351 
01352   if (relocatable)
01353     reloc_entry->address += input_section->output_offset;
01354 
01355   return bfd_reloc_ok;
01356 }
01357 
01358 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
01359    R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
01360    that contains the relocation field and DATA points to the start of
01361    INPUT_SECTION.  */
01362 
01363 struct mips_hi16
01364 {
01365   struct mips_hi16 *next;
01366   bfd_byte *data;
01367   asection *input_section;
01368   arelent rel;
01369 };
01370 
01371 /* FIXME: This should not be a static variable.  */
01372 
01373 static struct mips_hi16 *mips_hi16_list;
01374 
01375 /* A howto special_function for REL *HI16 relocations.  We can only
01376    calculate the correct value once we've seen the partnering
01377    *LO16 relocation, so just save the information for later.
01378 
01379    The ABI requires that the *LO16 immediately follow the *HI16.
01380    However, as a GNU extension, we permit an arbitrary number of
01381    *HI16s to be associated with a single *LO16.  This significantly
01382    simplies the relocation handling in gcc.  */
01383 
01384 bfd_reloc_status_type
01385 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
01386                        asymbol *symbol ATTRIBUTE_UNUSED, void *data,
01387                        asection *input_section, bfd *output_bfd,
01388                        char **error_message ATTRIBUTE_UNUSED)
01389 {
01390   struct mips_hi16 *n;
01391 
01392   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
01393     return bfd_reloc_outofrange;
01394 
01395   n = bfd_malloc (sizeof *n);
01396   if (n == NULL)
01397     return bfd_reloc_outofrange;
01398 
01399   n->next = mips_hi16_list;
01400   n->data = data;
01401   n->input_section = input_section;
01402   n->rel = *reloc_entry;
01403   mips_hi16_list = n;
01404 
01405   if (output_bfd != NULL)
01406     reloc_entry->address += input_section->output_offset;
01407 
01408   return bfd_reloc_ok;
01409 }
01410 
01411 /* A howto special_function for REL R_MIPS_GOT16 relocations.  This is just
01412    like any other 16-bit relocation when applied to global symbols, but is
01413    treated in the same as R_MIPS_HI16 when applied to local symbols.  */
01414 
01415 bfd_reloc_status_type
01416 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
01417                         void *data, asection *input_section,
01418                         bfd *output_bfd, char **error_message)
01419 {
01420   if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
01421       || bfd_is_und_section (bfd_get_section (symbol))
01422       || bfd_is_com_section (bfd_get_section (symbol)))
01423     /* The relocation is against a global symbol.  */
01424     return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
01425                                    input_section, output_bfd,
01426                                    error_message);
01427 
01428   return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
01429                                input_section, output_bfd, error_message);
01430 }
01431 
01432 /* A howto special_function for REL *LO16 relocations.  The *LO16 itself
01433    is a straightforward 16 bit inplace relocation, but we must deal with
01434    any partnering high-part relocations as well.  */
01435 
01436 bfd_reloc_status_type
01437 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
01438                        void *data, asection *input_section,
01439                        bfd *output_bfd, char **error_message)
01440 {
01441   bfd_vma vallo;
01442   bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
01443 
01444   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
01445     return bfd_reloc_outofrange;
01446 
01447   _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
01448                                location);
01449   vallo = bfd_get_32 (abfd, location);
01450   _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
01451                              location);
01452 
01453   while (mips_hi16_list != NULL)
01454     {
01455       bfd_reloc_status_type ret;
01456       struct mips_hi16 *hi;
01457 
01458       hi = mips_hi16_list;
01459 
01460       /* R_MIPS_GOT16 relocations are something of a special case.  We
01461         want to install the addend in the same way as for a R_MIPS_HI16
01462         relocation (with a rightshift of 16).  However, since GOT16
01463         relocations can also be used with global symbols, their howto
01464         has a rightshift of 0.  */
01465       if (hi->rel.howto->type == R_MIPS_GOT16)
01466        hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
01467 
01468       /* VALLO is a signed 16-bit number.  Bias it by 0x8000 so that any
01469         carry or borrow will induce a change of +1 or -1 in the high part.  */
01470       hi->rel.addend += (vallo + 0x8000) & 0xffff;
01471 
01472       ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
01473                                     hi->input_section, output_bfd,
01474                                     error_message);
01475       if (ret != bfd_reloc_ok)
01476        return ret;
01477 
01478       mips_hi16_list = hi->next;
01479       free (hi);
01480     }
01481 
01482   return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
01483                                   input_section, output_bfd,
01484                                   error_message);
01485 }
01486 
01487 /* A generic howto special_function.  This calculates and installs the
01488    relocation itself, thus avoiding the oft-discussed problems in
01489    bfd_perform_relocation and bfd_install_relocation.  */
01490 
01491 bfd_reloc_status_type
01492 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
01493                           asymbol *symbol, void *data ATTRIBUTE_UNUSED,
01494                           asection *input_section, bfd *output_bfd,
01495                           char **error_message ATTRIBUTE_UNUSED)
01496 {
01497   bfd_signed_vma val;
01498   bfd_reloc_status_type status;
01499   bfd_boolean relocatable;
01500 
01501   relocatable = (output_bfd != NULL);
01502 
01503   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
01504     return bfd_reloc_outofrange;
01505 
01506   /* Build up the field adjustment in VAL.  */
01507   val = 0;
01508   if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
01509     {
01510       /* Either we're calculating the final field value or we have a
01511         relocation against a section symbol.  Add in the section's
01512         offset or address.  */
01513       val += symbol->section->output_section->vma;
01514       val += symbol->section->output_offset;
01515     }
01516 
01517   if (!relocatable)
01518     {
01519       /* We're calculating the final field value.  Add in the symbol's value
01520         and, if pc-relative, subtract the address of the field itself.  */
01521       val += symbol->value;
01522       if (reloc_entry->howto->pc_relative)
01523        {
01524          val -= input_section->output_section->vma;
01525          val -= input_section->output_offset;
01526          val -= reloc_entry->address;
01527        }
01528     }
01529 
01530   /* VAL is now the final adjustment.  If we're keeping this relocation
01531      in the output file, and if the relocation uses a separate addend,
01532      we just need to add VAL to that addend.  Otherwise we need to add
01533      VAL to the relocation field itself.  */
01534   if (relocatable && !reloc_entry->howto->partial_inplace)
01535     reloc_entry->addend += val;
01536   else
01537     {
01538       bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
01539 
01540       /* Add in the separate addend, if any.  */
01541       val += reloc_entry->addend;
01542 
01543       /* Add VAL to the relocation field.  */
01544       _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
01545                                    location);
01546       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
01547                                    location);
01548       _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
01549                                  location);
01550 
01551       if (status != bfd_reloc_ok)
01552        return status;
01553     }
01554 
01555   if (relocatable)
01556     reloc_entry->address += input_section->output_offset;
01557 
01558   return bfd_reloc_ok;
01559 }
01560 
01561 /* Swap an entry in a .gptab section.  Note that these routines rely
01562    on the equivalence of the two elements of the union.  */
01563 
01564 static void
01565 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
01566                            Elf32_gptab *in)
01567 {
01568   in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
01569   in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
01570 }
01571 
01572 static void
01573 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
01574                             Elf32_External_gptab *ex)
01575 {
01576   H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
01577   H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
01578 }
01579 
01580 static void
01581 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
01582                             Elf32_External_compact_rel *ex)
01583 {
01584   H_PUT_32 (abfd, in->id1, ex->id1);
01585   H_PUT_32 (abfd, in->num, ex->num);
01586   H_PUT_32 (abfd, in->id2, ex->id2);
01587   H_PUT_32 (abfd, in->offset, ex->offset);
01588   H_PUT_32 (abfd, in->reserved0, ex->reserved0);
01589   H_PUT_32 (abfd, in->reserved1, ex->reserved1);
01590 }
01591 
01592 static void
01593 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
01594                         Elf32_External_crinfo *ex)
01595 {
01596   unsigned long l;
01597 
01598   l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
01599        | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
01600        | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
01601        | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
01602   H_PUT_32 (abfd, l, ex->info);
01603   H_PUT_32 (abfd, in->konst, ex->konst);
01604   H_PUT_32 (abfd, in->vaddr, ex->vaddr);
01605 }
01606 
01607 /* A .reginfo section holds a single Elf32_RegInfo structure.  These
01608    routines swap this structure in and out.  They are used outside of
01609    BFD, so they are globally visible.  */
01610 
01611 void
01612 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
01613                             Elf32_RegInfo *in)
01614 {
01615   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
01616   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
01617   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
01618   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
01619   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
01620   in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
01621 }
01622 
01623 void
01624 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
01625                              Elf32_External_RegInfo *ex)
01626 {
01627   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
01628   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
01629   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
01630   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
01631   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
01632   H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
01633 }
01634 
01635 /* In the 64 bit ABI, the .MIPS.options section holds register
01636    information in an Elf64_Reginfo structure.  These routines swap
01637    them in and out.  They are globally visible because they are used
01638    outside of BFD.  These routines are here so that gas can call them
01639    without worrying about whether the 64 bit ABI has been included.  */
01640 
01641 void
01642 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
01643                             Elf64_Internal_RegInfo *in)
01644 {
01645   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
01646   in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
01647   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
01648   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
01649   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
01650   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
01651   in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
01652 }
01653 
01654 void
01655 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
01656                              Elf64_External_RegInfo *ex)
01657 {
01658   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
01659   H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
01660   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
01661   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
01662   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
01663   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
01664   H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
01665 }
01666 
01667 /* Swap in an options header.  */
01668 
01669 void
01670 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
01671                            Elf_Internal_Options *in)
01672 {
01673   in->kind = H_GET_8 (abfd, ex->kind);
01674   in->size = H_GET_8 (abfd, ex->size);
01675   in->section = H_GET_16 (abfd, ex->section);
01676   in->info = H_GET_32 (abfd, ex->info);
01677 }
01678 
01679 /* Swap out an options header.  */
01680 
01681 void
01682 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
01683                             Elf_External_Options *ex)
01684 {
01685   H_PUT_8 (abfd, in->kind, ex->kind);
01686   H_PUT_8 (abfd, in->size, ex->size);
01687   H_PUT_16 (abfd, in->section, ex->section);
01688   H_PUT_32 (abfd, in->info, ex->info);
01689 }
01690 
01691 /* This function is called via qsort() to sort the dynamic relocation
01692    entries by increasing r_symndx value.  */
01693 
01694 static int
01695 sort_dynamic_relocs (const void *arg1, const void *arg2)
01696 {
01697   Elf_Internal_Rela int_reloc1;
01698   Elf_Internal_Rela int_reloc2;
01699   int diff;
01700 
01701   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
01702   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
01703 
01704   diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
01705   if (diff != 0)
01706     return diff;
01707 
01708   if (int_reloc1.r_offset < int_reloc2.r_offset)
01709     return -1;
01710   if (int_reloc1.r_offset > int_reloc2.r_offset)
01711     return 1;
01712   return 0;
01713 }
01714 
01715 /* Like sort_dynamic_relocs, but used for elf64 relocations.  */
01716 
01717 static int
01718 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
01719                      const void *arg2 ATTRIBUTE_UNUSED)
01720 {
01721 #ifdef BFD64
01722   Elf_Internal_Rela int_reloc1[3];
01723   Elf_Internal_Rela int_reloc2[3];
01724 
01725   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
01726     (reldyn_sorting_bfd, arg1, int_reloc1);
01727   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
01728     (reldyn_sorting_bfd, arg2, int_reloc2);
01729 
01730   if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
01731     return -1;
01732   if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
01733     return 1;
01734 
01735   if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
01736     return -1;
01737   if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
01738     return 1;
01739   return 0;
01740 #else
01741   abort ();
01742 #endif
01743 }
01744 
01745 
01746 /* This routine is used to write out ECOFF debugging external symbol
01747    information.  It is called via mips_elf_link_hash_traverse.  The
01748    ECOFF external symbol information must match the ELF external
01749    symbol information.  Unfortunately, at this point we don't know
01750    whether a symbol is required by reloc information, so the two
01751    tables may wind up being different.  We must sort out the external
01752    symbol information before we can set the final size of the .mdebug
01753    section, and we must set the size of the .mdebug section before we
01754    can relocate any sections, and we can't know which symbols are
01755    required by relocation until we relocate the sections.
01756    Fortunately, it is relatively unlikely that any symbol will be
01757    stripped but required by a reloc.  In particular, it can not happen
01758    when generating a final executable.  */
01759 
01760 static bfd_boolean
01761 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
01762 {
01763   struct extsym_info *einfo = data;
01764   bfd_boolean strip;
01765   asection *sec, *output_section;
01766 
01767   if (h->root.root.type == bfd_link_hash_warning)
01768     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
01769 
01770   if (h->root.indx == -2)
01771     strip = FALSE;
01772   else if ((h->root.def_dynamic
01773            || h->root.ref_dynamic
01774            || h->root.type == bfd_link_hash_new)
01775           && !h->root.def_regular
01776           && !h->root.ref_regular)
01777     strip = TRUE;
01778   else if (einfo->info->strip == strip_all
01779           || (einfo->info->strip == strip_some
01780               && bfd_hash_lookup (einfo->info->keep_hash,
01781                                h->root.root.root.string,
01782                                FALSE, FALSE) == NULL))
01783     strip = TRUE;
01784   else
01785     strip = FALSE;
01786 
01787   if (strip)
01788     return TRUE;
01789 
01790   if (h->esym.ifd == -2)
01791     {
01792       h->esym.jmptbl = 0;
01793       h->esym.cobol_main = 0;
01794       h->esym.weakext = 0;
01795       h->esym.reserved = 0;
01796       h->esym.ifd = ifdNil;
01797       h->esym.asym.value = 0;
01798       h->esym.asym.st = stGlobal;
01799 
01800       if (h->root.root.type == bfd_link_hash_undefined
01801          || h->root.root.type == bfd_link_hash_undefweak)
01802        {
01803          const char *name;
01804 
01805          /* Use undefined class.  Also, set class and type for some
01806              special symbols.  */
01807          name = h->root.root.root.string;
01808          if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
01809              || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
01810            {
01811              h->esym.asym.sc = scData;
01812              h->esym.asym.st = stLabel;
01813              h->esym.asym.value = 0;
01814            }
01815          else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
01816            {
01817              h->esym.asym.sc = scAbs;
01818              h->esym.asym.st = stLabel;
01819              h->esym.asym.value =
01820               mips_elf_hash_table (einfo->info)->procedure_count;
01821            }
01822          else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
01823            {
01824              h->esym.asym.sc = scAbs;
01825              h->esym.asym.st = stLabel;
01826              h->esym.asym.value = elf_gp (einfo->abfd);
01827            }
01828          else
01829            h->esym.asym.sc = scUndefined;
01830        }
01831       else if (h->root.root.type != bfd_link_hash_defined
01832          && h->root.root.type != bfd_link_hash_defweak)
01833        h->esym.asym.sc = scAbs;
01834       else
01835        {
01836          const char *name;
01837 
01838          sec = h->root.root.u.def.section;
01839          output_section = sec->output_section;
01840 
01841          /* When making a shared library and symbol h is the one from
01842             the another shared library, OUTPUT_SECTION may be null.  */
01843          if (output_section == NULL)
01844            h->esym.asym.sc = scUndefined;
01845          else
01846            {
01847              name = bfd_section_name (output_section->owner, output_section);
01848 
01849              if (strcmp (name, ".text") == 0)
01850               h->esym.asym.sc = scText;
01851              else if (strcmp (name, ".data") == 0)
01852               h->esym.asym.sc = scData;
01853              else if (strcmp (name, ".sdata") == 0)
01854               h->esym.asym.sc = scSData;
01855              else if (strcmp (name, ".rodata") == 0
01856                      || strcmp (name, ".rdata") == 0)
01857               h->esym.asym.sc = scRData;
01858              else if (strcmp (name, ".bss") == 0)
01859               h->esym.asym.sc = scBss;
01860              else if (strcmp (name, ".sbss") == 0)
01861               h->esym.asym.sc = scSBss;
01862              else if (strcmp (name, ".init") == 0)
01863               h->esym.asym.sc = scInit;
01864              else if (strcmp (name, ".fini") == 0)
01865               h->esym.asym.sc = scFini;
01866              else
01867               h->esym.asym.sc = scAbs;
01868            }
01869        }
01870 
01871       h->esym.asym.reserved = 0;
01872       h->esym.asym.index = indexNil;
01873     }
01874 
01875   if (h->root.root.type == bfd_link_hash_common)
01876     h->esym.asym.value = h->root.root.u.c.size;
01877   else if (h->root.root.type == bfd_link_hash_defined
01878           || h->root.root.type == bfd_link_hash_defweak)
01879     {
01880       if (h->esym.asym.sc == scCommon)
01881        h->esym.asym.sc = scBss;
01882       else if (h->esym.asym.sc == scSCommon)
01883        h->esym.asym.sc = scSBss;
01884 
01885       sec = h->root.root.u.def.section;
01886       output_section = sec->output_section;
01887       if (output_section != NULL)
01888        h->esym.asym.value = (h->root.root.u.def.value
01889                            + sec->output_offset
01890                            + output_section->vma);
01891       else
01892        h->esym.asym.value = 0;
01893     }
01894   else if (h->root.needs_plt)
01895     {
01896       struct mips_elf_link_hash_entry *hd = h;
01897       bfd_boolean no_fn_stub = h->no_fn_stub;
01898 
01899       while (hd->root.root.type == bfd_link_hash_indirect)
01900        {
01901          hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
01902          no_fn_stub = no_fn_stub || hd->no_fn_stub;
01903        }
01904 
01905       if (!no_fn_stub)
01906        {
01907          /* Set type and value for a symbol with a function stub.  */
01908          h->esym.asym.st = stProc;
01909          sec = hd->root.root.u.def.section;
01910          if (sec == NULL)
01911            h->esym.asym.value = 0;
01912          else
01913            {
01914              output_section = sec->output_section;
01915              if (output_section != NULL)
01916               h->esym.asym.value = (hd->root.plt.offset
01917                                   + sec->output_offset
01918                                   + output_section->vma);
01919              else
01920               h->esym.asym.value = 0;
01921            }
01922        }
01923     }
01924 
01925   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
01926                                   h->root.root.root.string,
01927                                   &h->esym))
01928     {
01929       einfo->failed = TRUE;
01930       return FALSE;
01931     }
01932 
01933   return TRUE;
01934 }
01935 
01936 /* A comparison routine used to sort .gptab entries.  */
01937 
01938 static int
01939 gptab_compare (const void *p1, const void *p2)
01940 {
01941   const Elf32_gptab *a1 = p1;
01942   const Elf32_gptab *a2 = p2;
01943 
01944   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
01945 }
01946 
01947 /* Functions to manage the got entry hash table.  */
01948 
01949 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
01950    hash number.  */
01951 
01952 static INLINE hashval_t
01953 mips_elf_hash_bfd_vma (bfd_vma addr)
01954 {
01955 #ifdef BFD64
01956   return addr + (addr >> 32);
01957 #else
01958   return addr;
01959 #endif
01960 }
01961 
01962 /* got_entries only match if they're identical, except for gotidx, so
01963    use all fields to compute the hash, and compare the appropriate
01964    union members.  */
01965 
01966 static hashval_t
01967 mips_elf_got_entry_hash (const void *entry_)
01968 {
01969   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
01970 
01971   return entry->symndx
01972     + ((entry->tls_type & GOT_TLS_LDM) << 17)
01973     + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
01974        : entry->abfd->id
01975          + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
01976            : entry->d.h->root.root.root.hash));
01977 }
01978 
01979 static int
01980 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
01981 {
01982   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
01983   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
01984 
01985   /* An LDM entry can only match another LDM entry.  */
01986   if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
01987     return 0;
01988 
01989   return e1->abfd == e2->abfd && e1->symndx == e2->symndx
01990     && (! e1->abfd ? e1->d.address == e2->d.address
01991        : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
01992        : e1->d.h == e2->d.h);
01993 }
01994 
01995 /* multi_got_entries are still a match in the case of global objects,
01996    even if the input bfd in which they're referenced differs, so the
01997    hash computation and compare functions are adjusted
01998    accordingly.  */
01999 
02000 static hashval_t
02001 mips_elf_multi_got_entry_hash (const void *entry_)
02002 {
02003   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
02004 
02005   return entry->symndx
02006     + (! entry->abfd
02007        ? mips_elf_hash_bfd_vma (entry->d.address)
02008        : entry->symndx >= 0
02009        ? ((entry->tls_type & GOT_TLS_LDM)
02010          ? (GOT_TLS_LDM << 17)
02011          : (entry->abfd->id
02012             + mips_elf_hash_bfd_vma (entry->d.addend)))
02013        : entry->d.h->root.root.root.hash);
02014 }
02015 
02016 static int
02017 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
02018 {
02019   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
02020   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
02021 
02022   /* Any two LDM entries match.  */
02023   if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
02024     return 1;
02025 
02026   /* Nothing else matches an LDM entry.  */
02027   if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
02028     return 0;
02029 
02030   return e1->symndx == e2->symndx
02031     && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
02032        : e1->abfd == NULL || e2->abfd == NULL
02033        ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
02034        : e1->d.h == e2->d.h);
02035 }
02036 
02037 /* Return the dynamic relocation section.  If it doesn't exist, try to
02038    create a new it if CREATE_P, otherwise return NULL.  Also return NULL
02039    if creation fails.  */
02040 
02041 static asection *
02042 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
02043 {
02044   const char *dname;
02045   asection *sreloc;
02046   bfd *dynobj;
02047 
02048   dname = MIPS_ELF_REL_DYN_NAME (info);
02049   dynobj = elf_hash_table (info)->dynobj;
02050   sreloc = bfd_get_section_by_name (dynobj, dname);
02051   if (sreloc == NULL && create_p)
02052     {
02053       sreloc = bfd_make_section_with_flags (dynobj, dname,
02054                                        (SEC_ALLOC
02055                                         | SEC_LOAD
02056                                         | SEC_HAS_CONTENTS
02057                                         | SEC_IN_MEMORY
02058                                         | SEC_LINKER_CREATED
02059                                         | SEC_READONLY));
02060       if (sreloc == NULL
02061          || ! bfd_set_section_alignment (dynobj, sreloc,
02062                                      MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
02063        return NULL;
02064     }
02065   return sreloc;
02066 }
02067 
02068 /* Returns the GOT section for ABFD.  */
02069 
02070 static asection *
02071 mips_elf_got_section (bfd *abfd, bfd_boolean maybe_excluded)
02072 {
02073   asection *sgot = bfd_get_section_by_name (abfd, ".got");
02074   if (sgot == NULL
02075       || (! maybe_excluded && (sgot->flags & SEC_EXCLUDE) != 0))
02076     return NULL;
02077   return sgot;
02078 }
02079 
02080 /* Returns the GOT information associated with the link indicated by
02081    INFO.  If SGOTP is non-NULL, it is filled in with the GOT
02082    section.  */
02083 
02084 static struct mips_got_info *
02085 mips_elf_got_info (bfd *abfd, asection **sgotp)
02086 {
02087   asection *sgot;
02088   struct mips_got_info *g;
02089 
02090   sgot = mips_elf_got_section (abfd, TRUE);
02091   BFD_ASSERT (sgot != NULL);
02092   BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
02093   g = mips_elf_section_data (sgot)->u.got_info;
02094   BFD_ASSERT (g != NULL);
02095 
02096   if (sgotp)
02097     *sgotp = (sgot->flags & SEC_EXCLUDE) == 0 ? sgot : NULL;
02098 
02099   return g;
02100 }
02101 
02102 /* Count the number of relocations needed for a TLS GOT entry, with
02103    access types from TLS_TYPE, and symbol H (or a local symbol if H
02104    is NULL).  */
02105 
02106 static int
02107 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
02108                    struct elf_link_hash_entry *h)
02109 {
02110   int indx = 0;
02111   int ret = 0;
02112   bfd_boolean need_relocs = FALSE;
02113   bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
02114 
02115   if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
02116       && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
02117     indx = h->dynindx;
02118 
02119   if ((info->shared || indx != 0)
02120       && (h == NULL
02121          || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
02122          || h->root.type != bfd_link_hash_undefweak))
02123     need_relocs = TRUE;
02124 
02125   if (!need_relocs)
02126     return FALSE;
02127 
02128   if (tls_type & GOT_TLS_GD)
02129     {
02130       ret++;
02131       if (indx != 0)
02132        ret++;
02133     }
02134 
02135   if (tls_type & GOT_TLS_IE)
02136     ret++;
02137 
02138   if ((tls_type & GOT_TLS_LDM) && info->shared)
02139     ret++;
02140 
02141   return ret;
02142 }
02143 
02144 /* Count the number of TLS relocations required for the GOT entry in
02145    ARG1, if it describes a local symbol.  */
02146 
02147 static int
02148 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
02149 {
02150   struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
02151   struct mips_elf_count_tls_arg *arg = arg2;
02152 
02153   if (entry->abfd != NULL && entry->symndx != -1)
02154     arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
02155 
02156   return 1;
02157 }
02158 
02159 /* Count the number of TLS GOT entries required for the global (or
02160    forced-local) symbol in ARG1.  */
02161 
02162 static int
02163 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
02164 {
02165   struct mips_elf_link_hash_entry *hm
02166     = (struct mips_elf_link_hash_entry *) arg1;
02167   struct mips_elf_count_tls_arg *arg = arg2;
02168 
02169   if (hm->tls_type & GOT_TLS_GD)
02170     arg->needed += 2;
02171   if (hm->tls_type & GOT_TLS_IE)
02172     arg->needed += 1;
02173 
02174   return 1;
02175 }
02176 
02177 /* Count the number of TLS relocations required for the global (or
02178    forced-local) symbol in ARG1.  */
02179 
02180 static int
02181 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
02182 {
02183   struct mips_elf_link_hash_entry *hm
02184     = (struct mips_elf_link_hash_entry *) arg1;
02185   struct mips_elf_count_tls_arg *arg = arg2;
02186 
02187   arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
02188 
02189   return 1;
02190 }
02191 
02192 /* Output a simple dynamic relocation into SRELOC.  */
02193 
02194 static void
02195 mips_elf_output_dynamic_relocation (bfd *output_bfd,
02196                                 asection *sreloc,
02197                                 unsigned long indx,
02198                                 int r_type,
02199                                 bfd_vma offset)
02200 {
02201   Elf_Internal_Rela rel[3];
02202 
02203   memset (rel, 0, sizeof (rel));
02204 
02205   rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
02206   rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
02207 
02208   if (ABI_64_P (output_bfd))
02209     {
02210       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
02211        (output_bfd, &rel[0],
02212         (sreloc->contents
02213          + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
02214     }
02215   else
02216     bfd_elf32_swap_reloc_out
02217       (output_bfd, &rel[0],
02218        (sreloc->contents
02219        + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
02220   ++sreloc->reloc_count;
02221 }
02222 
02223 /* Initialize a set of TLS GOT entries for one symbol.  */
02224 
02225 static void
02226 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
02227                             unsigned char *tls_type_p,
02228                             struct bfd_link_info *info,
02229                             struct mips_elf_link_hash_entry *h,
02230                             bfd_vma value)
02231 {
02232   int indx;
02233   asection *sreloc, *sgot;
02234   bfd_vma offset, offset2;
02235   bfd *dynobj;
02236   bfd_boolean need_relocs = FALSE;
02237 
02238   dynobj = elf_hash_table (info)->dynobj;
02239   sgot = mips_elf_got_section (dynobj, FALSE);
02240 
02241   indx = 0;
02242   if (h != NULL)
02243     {
02244       bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
02245 
02246       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
02247          && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
02248        indx = h->root.dynindx;
02249     }
02250 
02251   if (*tls_type_p & GOT_TLS_DONE)
02252     return;
02253 
02254   if ((info->shared || indx != 0)
02255       && (h == NULL
02256          || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
02257          || h->root.type != bfd_link_hash_undefweak))
02258     need_relocs = TRUE;
02259 
02260   /* MINUS_ONE means the symbol is not defined in this object.  It may not
02261      be defined at all; assume that the value doesn't matter in that
02262      case.  Otherwise complain if we would use the value.  */
02263   BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
02264              || h->root.root.type == bfd_link_hash_undefweak);
02265 
02266   /* Emit necessary relocations.  */
02267   sreloc = mips_elf_rel_dyn_section (info, FALSE);
02268 
02269   /* General Dynamic.  */
02270   if (*tls_type_p & GOT_TLS_GD)
02271     {
02272       offset = got_offset;
02273       offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
02274 
02275       if (need_relocs)
02276        {
02277          mips_elf_output_dynamic_relocation
02278            (abfd, sreloc, indx,
02279             ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
02280             sgot->output_offset + sgot->output_section->vma + offset);
02281 
02282          if (indx)
02283            mips_elf_output_dynamic_relocation
02284              (abfd, sreloc, indx,
02285               ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
02286               sgot->output_offset + sgot->output_section->vma + offset2);
02287          else
02288            MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
02289                             sgot->contents + offset2);
02290        }
02291       else
02292        {
02293          MIPS_ELF_PUT_WORD (abfd, 1,
02294                           sgot->contents + offset);
02295          MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
02296                           sgot->contents + offset2);
02297        }
02298 
02299       got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
02300     }
02301 
02302   /* Initial Exec model.  */
02303   if (*tls_type_p & GOT_TLS_IE)
02304     {
02305       offset = got_offset;
02306 
02307       if (need_relocs)
02308        {
02309          if (indx == 0)
02310            MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
02311                             sgot->contents + offset);
02312          else
02313            MIPS_ELF_PUT_WORD (abfd, 0,
02314                             sgot->contents + offset);
02315 
02316          mips_elf_output_dynamic_relocation
02317            (abfd, sreloc, indx,
02318             ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
02319             sgot->output_offset + sgot->output_section->vma + offset);
02320        }
02321       else
02322        MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
02323                         sgot->contents + offset);
02324     }
02325 
02326   if (*tls_type_p & GOT_TLS_LDM)
02327     {
02328       /* The initial offset is zero, and the LD offsets will include the
02329         bias by DTP_OFFSET.  */
02330       MIPS_ELF_PUT_WORD (abfd, 0,
02331                       sgot->contents + got_offset
02332                       + MIPS_ELF_GOT_SIZE (abfd));
02333 
02334       if (!info->shared)
02335        MIPS_ELF_PUT_WORD (abfd, 1,
02336                         sgot->contents + got_offset);
02337       else
02338        mips_elf_output_dynamic_relocation
02339          (abfd, sreloc, indx,
02340           ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
02341           sgot->output_offset + sgot->output_section->vma + got_offset);
02342     }
02343 
02344   *tls_type_p |= GOT_TLS_DONE;
02345 }
02346 
02347 /* Return the GOT index to use for a relocation of type R_TYPE against
02348    a symbol accessed using TLS_TYPE models.  The GOT entries for this
02349    symbol in this GOT start at GOT_INDEX.  This function initializes the
02350    GOT entries and corresponding relocations.  */
02351 
02352 static bfd_vma
02353 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
02354                   int r_type, struct bfd_link_info *info,
02355                   struct mips_elf_link_hash_entry *h, bfd_vma symbol)
02356 {
02357   BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
02358              || r_type == R_MIPS_TLS_LDM);
02359 
02360   mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
02361 
02362   if (r_type == R_MIPS_TLS_GOTTPREL)
02363     {
02364       BFD_ASSERT (*tls_type & GOT_TLS_IE);
02365       if (*tls_type & GOT_TLS_GD)
02366        return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
02367       else
02368        return got_index;
02369     }
02370 
02371   if (r_type == R_MIPS_TLS_GD)
02372     {
02373       BFD_ASSERT (*tls_type & GOT_TLS_GD);
02374       return got_index;
02375     }
02376 
02377   if (r_type == R_MIPS_TLS_LDM)
02378     {
02379       BFD_ASSERT (*tls_type & GOT_TLS_LDM);
02380       return got_index;
02381     }
02382 
02383   return got_index;
02384 }
02385 
02386 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
02387    for global symbol H.  .got.plt comes before the GOT, so the offset
02388    will be negative.  */
02389 
02390 static bfd_vma
02391 mips_elf_gotplt_index (struct bfd_link_info *info,
02392                      struct elf_link_hash_entry *h)
02393 {
02394   bfd_vma plt_index, got_address, got_value;
02395   struct mips_elf_link_hash_table *htab;
02396 
02397   htab = mips_elf_hash_table (info);
02398   BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
02399 
02400   /* Calculate the index of the symbol's PLT entry.  */
02401   plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
02402 
02403   /* Calculate the address of the associated .got.plt entry.  */
02404   got_address = (htab->sgotplt->output_section->vma
02405                + htab->sgotplt->output_offset
02406                + plt_index * 4);
02407 
02408   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
02409   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
02410               + htab->root.hgot->root.u.def.section->output_offset
02411               + htab->root.hgot->root.u.def.value);
02412 
02413   return got_address - got_value;
02414 }
02415 
02416 /* Return the GOT offset for address VALUE, which was derived from
02417    a symbol belonging to INPUT_SECTION.   If there is not yet a GOT
02418    entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
02419    create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
02420    offset can be found.  */
02421 
02422 static bfd_vma
02423 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
02424                        asection *input_section, bfd_vma value,
02425                        unsigned long r_symndx,
02426                        struct mips_elf_link_hash_entry *h, int r_type)
02427 {
02428   asection *sgot;
02429   struct mips_got_info *g;
02430   struct mips_got_entry *entry;
02431 
02432   g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
02433 
02434   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
02435                                       input_section, value,
02436                                       r_symndx, h, r_type);
02437   if (!entry)
02438     return MINUS_ONE;
02439 
02440   if (TLS_RELOC_P (r_type))
02441     {
02442       if (entry->symndx == -1 && g->next == NULL)
02443        /* A type (3) entry in the single-GOT case.  We use the symbol's
02444           hash table entry to track the index.  */
02445        return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
02446                                r_type, info, h, value);
02447       else
02448        return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
02449                                r_type, info, h, value);
02450     }
02451   else
02452     return entry->gotidx;
02453 }
02454 
02455 /* Returns the GOT index for the global symbol indicated by H.  */
02456 
02457 static bfd_vma
02458 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
02459                         int r_type, struct bfd_link_info *info)
02460 {
02461   bfd_vma index;
02462   asection *sgot;
02463   struct mips_got_info *g, *gg;
02464   long global_got_dynindx = 0;
02465 
02466   gg = g = mips_elf_got_info (abfd, &sgot);
02467   if (g->bfd2got && ibfd)
02468     {
02469       struct mips_got_entry e, *p;
02470 
02471       BFD_ASSERT (h->dynindx >= 0);
02472 
02473       g = mips_elf_got_for_ibfd (g, ibfd);
02474       if (g->next != gg || TLS_RELOC_P (r_type))
02475        {
02476          e.abfd = ibfd;
02477          e.symndx = -1;
02478          e.d.h = (struct mips_elf_link_hash_entry *)h;
02479          e.tls_type = 0;
02480 
02481          p = htab_find (g->got_entries, &e);
02482 
02483          BFD_ASSERT (p->gotidx > 0);
02484 
02485          if (TLS_RELOC_P (r_type))
02486            {
02487              bfd_vma value = MINUS_ONE;
02488              if ((h->root.type == bfd_link_hash_defined
02489                  || h->root.type == bfd_link_hash_defweak)
02490                 && h->root.u.def.section->output_section)
02491               value = (h->root.u.def.value
02492                       + h->root.u.def.section->output_offset
02493                       + h->root.u.def.section->output_section->vma);
02494 
02495              return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
02496                                     info, e.d.h, value);
02497            }
02498          else
02499            return p->gotidx;
02500        }
02501     }
02502 
02503   if (gg->global_gotsym != NULL)
02504     global_got_dynindx = gg->global_gotsym->dynindx;
02505 
02506   if (TLS_RELOC_P (r_type))
02507     {
02508       struct mips_elf_link_hash_entry *hm
02509        = (struct mips_elf_link_hash_entry *) h;
02510       bfd_vma value = MINUS_ONE;
02511 
02512       if ((h->root.type == bfd_link_hash_defined
02513           || h->root.type == bfd_link_hash_defweak)
02514          && h->root.u.def.section->output_section)
02515        value = (h->root.u.def.value
02516                + h->root.u.def.section->output_offset
02517                + h->root.u.def.section->output_section->vma);
02518 
02519       index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
02520                               r_type, info, hm, value);
02521     }
02522   else
02523     {
02524       /* Once we determine the global GOT entry with the lowest dynamic
02525         symbol table index, we must put all dynamic symbols with greater
02526         indices into the GOT.  That makes it easy to calculate the GOT
02527         offset.  */
02528       BFD_ASSERT (h->dynindx >= global_got_dynindx);
02529       index = ((h->dynindx - global_got_dynindx + g->local_gotno)
02530               * MIPS_ELF_GOT_SIZE (abfd));
02531     }
02532   BFD_ASSERT (index < sgot->size);
02533 
02534   return index;
02535 }
02536 
02537 /* Find a GOT page entry that points to within 32KB of VALUE, which was
02538    calculated from a symbol belonging to INPUT_SECTION.  These entries
02539    are supposed to be placed at small offsets in the GOT, i.e., within
02540    32KB of GP.  Return the index of the GOT entry, or -1 if no entry
02541    could be created.  If OFFSETP is nonnull, use it to return the
02542    offset of the GOT entry from VALUE.  */
02543 
02544 static bfd_vma
02545 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
02546                  asection *input_section, bfd_vma value, bfd_vma *offsetp)
02547 {
02548   asection *sgot;
02549   struct mips_got_info *g;
02550   bfd_vma page, index;
02551   struct mips_got_entry *entry;
02552 
02553   g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
02554 
02555   page = (value + 0x8000) & ~(bfd_vma) 0xffff;
02556   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
02557                                       input_section, page, 0,
02558                                       NULL, R_MIPS_GOT_PAGE);
02559 
02560   if (!entry)
02561     return MINUS_ONE;
02562 
02563   index = entry->gotidx;
02564 
02565   if (offsetp)
02566     *offsetp = value - entry->d.address;
02567 
02568   return index;
02569 }
02570 
02571 /* Find a local GOT entry for an R_MIPS_GOT16 relocation against VALUE,
02572    which was calculated from a symbol belonging to INPUT_SECTION.
02573    EXTERNAL is true if the relocation was against a global symbol
02574    that has been forced local.  */
02575 
02576 static bfd_vma
02577 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
02578                     asection *input_section, bfd_vma value,
02579                     bfd_boolean external)
02580 {
02581   asection *sgot;
02582   struct mips_got_info *g;
02583   struct mips_got_entry *entry;
02584 
02585   /* GOT16 relocations against local symbols are followed by a LO16
02586      relocation; those against global symbols are not.  Thus if the
02587      symbol was originally local, the GOT16 relocation should load the
02588      equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
02589   if (! external)
02590     value = mips_elf_high (value) << 16;
02591 
02592   g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
02593 
02594   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
02595                                       input_section, value, 0,
02596                                       NULL, R_MIPS_GOT16);
02597   if (entry)
02598     return entry->gotidx;
02599   else
02600     return MINUS_ONE;
02601 }
02602 
02603 /* Returns the offset for the entry at the INDEXth position
02604    in the GOT.  */
02605 
02606 static bfd_vma
02607 mips_elf_got_offset_from_index (bfd *dynobj, bfd *output_bfd,
02608                             bfd *input_bfd, bfd_vma index)
02609 {
02610   asection *sgot;
02611   bfd_vma gp;
02612   struct mips_got_info *g;
02613 
02614   g = mips_elf_got_info (dynobj, &sgot);
02615   gp = _bfd_get_gp_value (output_bfd)
02616     + mips_elf_adjust_gp (output_bfd, g, input_bfd);
02617 
02618   return sgot->output_section->vma + sgot->output_offset + index - gp;
02619 }
02620 
02621 /* Create and return a local GOT entry for VALUE, which was calculated
02622    from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
02623    be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
02624    instead.  */
02625 
02626 static struct mips_got_entry *
02627 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
02628                              bfd *ibfd, struct mips_got_info *gg,
02629                              asection *sgot, asection *input_section,
02630                              bfd_vma value, unsigned long r_symndx,
02631                              struct mips_elf_link_hash_entry *h,
02632                              int r_type)
02633 {
02634   struct mips_got_entry entry, **loc;
02635   struct mips_got_info *g;
02636   struct mips_elf_link_hash_table *htab;
02637 
02638   htab = mips_elf_hash_table (info);
02639 
02640   entry.abfd = NULL;
02641   entry.symndx = -1;
02642   entry.d.address = value;
02643   entry.tls_type = 0;
02644 
02645   g = mips_elf_got_for_ibfd (gg, ibfd);
02646   if (g == NULL)
02647     {
02648       g = mips_elf_got_for_ibfd (gg, abfd);
02649       BFD_ASSERT (g != NULL);
02650     }
02651 
02652   /* We might have a symbol, H, if it has been forced local.  Use the
02653      global entry then.  It doesn't matter whether an entry is local
02654      or global for TLS, since the dynamic linker does not
02655      automatically relocate TLS GOT entries.  */
02656   BFD_ASSERT (h == NULL || h->root.forced_local);
02657   if (TLS_RELOC_P (r_type))
02658     {
02659       struct mips_got_entry *p;
02660 
02661       entry.abfd = ibfd;
02662       if (r_type == R_MIPS_TLS_LDM)
02663        {
02664          entry.tls_type = GOT_TLS_LDM;
02665          entry.symndx = 0;
02666          entry.d.addend = 0;
02667        }
02668       else if (h == NULL)
02669        {
02670          entry.symndx = r_symndx;
02671          entry.d.addend = 0;
02672        }
02673       else
02674        entry.d.h = h;
02675 
02676       p = (struct mips_got_entry *)
02677        htab_find (g->got_entries, &entry);
02678 
02679       BFD_ASSERT (p);
02680       return p;
02681     }
02682 
02683   loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
02684                                              INSERT);
02685   if (*loc)
02686     return *loc;
02687 
02688   entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
02689   entry.tls_type = 0;
02690 
02691   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
02692 
02693   if (! *loc)
02694     return NULL;
02695 
02696   memcpy (*loc, &entry, sizeof entry);
02697 
02698   if (g->assigned_gotno >= g->local_gotno)
02699     {
02700       (*loc)->gotidx = -1;
02701       /* We didn't allocate enough space in the GOT.  */
02702       (*_bfd_error_handler)
02703        (_("not enough GOT space for local GOT entries"));
02704       bfd_set_error (bfd_error_bad_value);
02705       return NULL;
02706     }
02707 
02708   MIPS_ELF_PUT_WORD (abfd, value,
02709                    (sgot->contents + entry.gotidx));
02710 
02711   /* These GOT entries need a dynamic relocation on VxWorks.  Because
02712      the offset between segments is not fixed, the relocation must be
02713      against a symbol in the same segment as the original symbol.
02714      The easiest way to do this is to take INPUT_SECTION's output
02715      section and emit a relocation against its section symbol.  */
02716   if (htab->is_vxworks)
02717     {
02718       Elf_Internal_Rela outrel;
02719       asection *s, *output_section;
02720       bfd_byte *loc;
02721       bfd_vma got_address;
02722       int dynindx;
02723 
02724       s = mips_elf_rel_dyn_section (info, FALSE);
02725       output_section = input_section->output_section;
02726       dynindx = elf_section_data (output_section)->dynindx;
02727       got_address = (sgot->output_section->vma
02728                    + sgot->output_offset
02729                    + entry.gotidx);
02730 
02731       loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
02732       outrel.r_offset = got_address;
02733       outrel.r_info = ELF32_R_INFO (dynindx, R_MIPS_32);
02734       outrel.r_addend = value - output_section->vma;
02735       bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
02736     }
02737 
02738   return *loc;
02739 }
02740 
02741 /* Sort the dynamic symbol table so that symbols that need GOT entries
02742    appear towards the end.  This reduces the amount of GOT space
02743    required.  MAX_LOCAL is used to set the number of local symbols
02744    known to be in the dynamic symbol table.  During
02745    _bfd_mips_elf_size_dynamic_sections, this value is 1.  Afterward, the
02746    section symbols are added and the count is higher.  */
02747 
02748 static bfd_boolean
02749 mips_elf_sort_hash_table (struct bfd_link_info *info, unsigned long max_local)
02750 {
02751   struct mips_elf_hash_sort_data hsd;
02752   struct mips_got_info *g;
02753   bfd *dynobj;
02754 
02755   dynobj = elf_hash_table (info)->dynobj;
02756 
02757   g = mips_elf_got_info (dynobj, NULL);
02758 
02759   hsd.low = NULL;
02760   hsd.max_unref_got_dynindx =
02761   hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
02762     /* In the multi-got case, assigned_gotno of the master got_info
02763        indicate the number of entries that aren't referenced in the
02764        primary GOT, but that must have entries because there are
02765        dynamic relocations that reference it.  Since they aren't
02766        referenced, we move them to the end of the GOT, so that they
02767        don't prevent other entries that are referenced from getting
02768        too large offsets.  */
02769     - (g->next ? g->assigned_gotno : 0);
02770   hsd.max_non_got_dynindx = max_local;
02771   mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
02772                             elf_hash_table (info)),
02773                             mips_elf_sort_hash_table_f,
02774                             &hsd);
02775 
02776   /* There should have been enough room in the symbol table to
02777      accommodate both the GOT and non-GOT symbols.  */
02778   BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
02779   BFD_ASSERT ((unsigned long)hsd.max_unref_got_dynindx
02780              <= elf_hash_table (info)->dynsymcount);
02781 
02782   /* Now we know which dynamic symbol has the lowest dynamic symbol
02783      table index in the GOT.  */
02784   g->global_gotsym = hsd.low;
02785 
02786   return TRUE;
02787 }
02788 
02789 /* If H needs a GOT entry, assign it the highest available dynamic
02790    index.  Otherwise, assign it the lowest available dynamic
02791    index.  */
02792 
02793 static bfd_boolean
02794 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
02795 {
02796   struct mips_elf_hash_sort_data *hsd = data;
02797 
02798   if (h->root.root.type == bfd_link_hash_warning)
02799     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
02800 
02801   /* Symbols without dynamic symbol table entries aren't interesting
02802      at all.  */
02803   if (h->root.dynindx == -1)
02804     return TRUE;
02805 
02806   /* Global symbols that need GOT entries that are not explicitly
02807      referenced are marked with got offset 2.  Those that are
02808      referenced get a 1, and those that don't need GOT entries get
02809      -1.  */
02810   if (h->root.got.offset == 2)
02811     {
02812       BFD_ASSERT (h->tls_type == GOT_NORMAL);
02813 
02814       if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
02815        hsd->low = (struct elf_link_hash_entry *) h;
02816       h->root.dynindx = hsd->max_unref_got_dynindx++;
02817     }
02818   else if (h->root.got.offset != 1)
02819     h->root.dynindx = hsd->max_non_got_dynindx++;
02820   else
02821     {
02822       BFD_ASSERT (h->tls_type == GOT_NORMAL);
02823 
02824       h->root.dynindx = --hsd->min_got_dynindx;
02825       hsd->low = (struct elf_link_hash_entry *) h;
02826     }
02827 
02828   return TRUE;
02829 }
02830 
02831 /* If H is a symbol that needs a global GOT entry, but has a dynamic
02832    symbol table index lower than any we've seen to date, record it for
02833    posterity.  */
02834 
02835 static bfd_boolean
02836 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
02837                                bfd *abfd, struct bfd_link_info *info,
02838                                struct mips_got_info *g,
02839                                unsigned char tls_flag)
02840 {
02841   struct mips_got_entry entry, **loc;
02842 
02843   /* A global symbol in the GOT must also be in the dynamic symbol
02844      table.  */
02845   if (h->dynindx == -1)
02846     {
02847       switch (ELF_ST_VISIBILITY (h->other))
02848        {
02849        case STV_INTERNAL:
02850        case STV_HIDDEN:
02851          _bfd_mips_elf_hide_symbol (info, h, TRUE);
02852          break;
02853        }
02854       if (!bfd_elf_link_record_dynamic_symbol (info, h))
02855        return FALSE;
02856     }
02857 
02858   /* Make sure we have a GOT to put this entry into.  */
02859   BFD_ASSERT (g != NULL);
02860 
02861   entry.abfd = abfd;
02862   entry.symndx = -1;
02863   entry.d.h = (struct mips_elf_link_hash_entry *) h;
02864   entry.tls_type = 0;
02865 
02866   loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
02867                                              INSERT);
02868 
02869   /* If we've already marked this entry as needing GOT space, we don't
02870      need to do it again.  */
02871   if (*loc)
02872     {
02873       (*loc)->tls_type |= tls_flag;
02874       return TRUE;
02875     }
02876 
02877   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
02878 
02879   if (! *loc)
02880     return FALSE;
02881 
02882   entry.gotidx = -1;
02883   entry.tls_type = tls_flag;
02884 
02885   memcpy (*loc, &entry, sizeof entry);
02886 
02887   if (h->got.offset != MINUS_ONE)
02888     return TRUE;
02889 
02890   /* By setting this to a value other than -1, we are indicating that
02891      there needs to be a GOT entry for H.  Avoid using zero, as the
02892      generic ELF copy_indirect_symbol tests for <= 0.  */
02893   if (tls_flag == 0)
02894     h->got.offset = 1;
02895 
02896   return TRUE;
02897 }
02898 
02899 /* Reserve space in G for a GOT entry containing the value of symbol
02900    SYMNDX in input bfd ABDF, plus ADDEND.  */
02901 
02902 static bfd_boolean
02903 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
02904                               struct mips_got_info *g,
02905                               unsigned char tls_flag)
02906 {
02907   struct mips_got_entry entry, **loc;
02908 
02909   entry.abfd = abfd;
02910   entry.symndx = symndx;
02911   entry.d.addend = addend;
02912   entry.tls_type = tls_flag;
02913   loc = (struct mips_got_entry **)
02914     htab_find_slot (g->got_entries, &entry, INSERT);
02915 
02916   if (*loc)
02917     {
02918       if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
02919        {
02920          g->tls_gotno += 2;
02921          (*loc)->tls_type |= tls_flag;
02922        }
02923       else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
02924        {
02925          g->tls_gotno += 1;
02926          (*loc)->tls_type |= tls_flag;
02927        }
02928       return TRUE;
02929     }
02930 
02931   if (tls_flag != 0)
02932     {
02933       entry.gotidx = -1;
02934       entry.tls_type = tls_flag;
02935       if (tls_flag == GOT_TLS_IE)
02936        g->tls_gotno += 1;
02937       else if (tls_flag == GOT_TLS_GD)
02938        g->tls_gotno += 2;
02939       else if (g->tls_ldm_offset == MINUS_ONE)
02940        {
02941          g->tls_ldm_offset = MINUS_TWO;
02942          g->tls_gotno += 2;
02943        }
02944     }
02945   else
02946     {
02947       entry.gotidx = g->local_gotno++;
02948       entry.tls_type = 0;
02949     }
02950 
02951   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
02952 
02953   if (! *loc)
02954     return FALSE;
02955 
02956   memcpy (*loc, &entry, sizeof entry);
02957 
02958   return TRUE;
02959 }
02960 
02961 /* Compute the hash value of the bfd in a bfd2got hash entry.  */
02962 
02963 static hashval_t
02964 mips_elf_bfd2got_entry_hash (const void *entry_)
02965 {
02966   const struct mips_elf_bfd2got_hash *entry
02967     = (struct mips_elf_bfd2got_hash *)entry_;
02968 
02969   return entry->bfd->id;
02970 }
02971 
02972 /* Check whether two hash entries have the same bfd.  */
02973 
02974 static int
02975 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
02976 {
02977   const struct mips_elf_bfd2got_hash *e1
02978     = (const struct mips_elf_bfd2got_hash *)entry1;
02979   const struct mips_elf_bfd2got_hash *e2
02980     = (const struct mips_elf_bfd2got_hash *)entry2;
02981 
02982   return e1->bfd == e2->bfd;
02983 }
02984 
02985 /* In a multi-got link, determine the GOT to be used for IBFD.  G must
02986    be the master GOT data.  */
02987 
02988 static struct mips_got_info *
02989 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
02990 {
02991   struct mips_elf_bfd2got_hash e, *p;
02992 
02993   if (! g->bfd2got)
02994     return g;
02995 
02996   e.bfd = ibfd;
02997   p = htab_find (g->bfd2got, &e);
02998   return p ? p->g : NULL;
02999 }
03000 
03001 /* Create one separate got for each bfd that has entries in the global
03002    got, such that we can tell how many local and global entries each
03003    bfd requires.  */
03004 
03005 static int
03006 mips_elf_make_got_per_bfd (void **entryp, void *p)
03007 {
03008   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
03009   struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
03010   htab_t bfd2got = arg->bfd2got;
03011   struct mips_got_info *g;
03012   struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
03013   void **bfdgotp;
03014 
03015   /* Find the got_info for this GOT entry's input bfd.  Create one if
03016      none exists.  */
03017   bfdgot_entry.bfd = entry->abfd;
03018   bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
03019   bfdgot = (struct mips_elf_bfd2got_hash *)*bfdgotp;
03020 
03021   if (bfdgot != NULL)
03022     g = bfdgot->g;
03023   else
03024     {
03025       bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
03026        (arg->obfd, sizeof (struct mips_elf_bfd2got_hash));
03027 
03028       if (bfdgot == NULL)
03029        {
03030          arg->obfd = 0;
03031          return 0;
03032        }
03033 
03034       *bfdgotp = bfdgot;
03035 
03036       bfdgot->bfd = entry->abfd;
03037       bfdgot->g = g = (struct mips_got_info *)
03038        bfd_alloc (arg->obfd, sizeof (struct mips_got_info));
03039       if (g == NULL)
03040        {
03041          arg->obfd = 0;
03042          return 0;
03043        }
03044 
03045       g->global_gotsym = NULL;
03046       g->global_gotno = 0;
03047       g->local_gotno = 0;
03048       g->assigned_gotno = -1;
03049       g->tls_gotno = 0;
03050       g->tls_assigned_gotno = 0;
03051       g->tls_ldm_offset = MINUS_ONE;
03052       g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
03053                                    mips_elf_multi_got_entry_eq, NULL);
03054       if (g->got_entries == NULL)
03055        {
03056          arg->obfd = 0;
03057          return 0;
03058        }
03059 
03060       g->bfd2got = NULL;
03061       g->next = NULL;
03062     }
03063 
03064   /* Insert the GOT entry in the bfd's got entry hash table.  */
03065   entryp = htab_find_slot (g->got_entries, entry, INSERT);
03066   if (*entryp != NULL)
03067     return 1;
03068 
03069   *entryp = entry;
03070 
03071   if (entry->tls_type)
03072     {
03073       if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
03074        g->tls_gotno += 2;
03075       if (entry->tls_type & GOT_TLS_IE)
03076        g->tls_gotno += 1;
03077     }
03078   else if (entry->symndx >= 0 || entry->d.h->forced_local)
03079     ++g->local_gotno;
03080   else
03081     ++g->global_gotno;
03082 
03083   return 1;
03084 }
03085 
03086 /* Attempt to merge gots of different input bfds.  Try to use as much
03087    as possible of the primary got, since it doesn't require explicit
03088    dynamic relocations, but don't use bfds that would reference global
03089    symbols out of the addressable range.  Failing the primary got,
03090    attempt to merge with the current got, or finish the current got
03091    and then make make the new got current.  */
03092 
03093 static int
03094 mips_elf_merge_gots (void **bfd2got_, void *p)
03095 {
03096   struct mips_elf_bfd2got_hash *bfd2got
03097     = (struct mips_elf_bfd2got_hash *)*bfd2got_;
03098   struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
03099   unsigned int lcount = bfd2got->g->local_gotno;
03100   unsigned int gcount = bfd2got->g->global_gotno;
03101   unsigned int tcount = bfd2got->g->tls_gotno;
03102   unsigned int maxcnt = arg->max_count;
03103   bfd_boolean too_many_for_tls = FALSE;
03104 
03105   /* We place TLS GOT entries after both locals and globals.  The globals
03106      for the primary GOT may overflow the normal GOT size limit, so be
03107      sure not to merge a GOT which requires TLS with the primary GOT in that
03108      case.  This doesn't affect non-primary GOTs.  */
03109   if (tcount > 0)
03110     {
03111       unsigned int primary_total = lcount + tcount + arg->global_count;
03112       if (primary_total > maxcnt)
03113        too_many_for_tls = TRUE;
03114     }
03115 
03116   /* If we don't have a primary GOT and this is not too big, use it as
03117      a starting point for the primary GOT.  */
03118   if (! arg->primary && lcount + gcount + tcount <= maxcnt
03119       && ! too_many_for_tls)
03120     {
03121       arg->primary = bfd2got->g;
03122       arg->primary_count = lcount + gcount;
03123     }
03124   /* If it looks like we can merge this bfd's entries with those of
03125      the primary, merge them.  The heuristics is conservative, but we
03126      don't have to squeeze it too hard.  */
03127   else if (arg->primary && ! too_many_for_tls
03128           && (arg->primary_count + lcount + gcount + tcount) <= maxcnt)
03129     {
03130       struct mips_got_info *g = bfd2got->g;
03131       int old_lcount = arg->primary->local_gotno;
03132       int old_gcount = arg->primary->global_gotno;
03133       int old_tcount = arg->primary->tls_gotno;
03134 
03135       bfd2got->g = arg->primary;
03136 
03137       htab_traverse (g->got_entries,
03138                    mips_elf_make_got_per_bfd,
03139                    arg);
03140       if (arg->obfd == NULL)
03141        return 0;
03142 
03143       htab_delete (g->got_entries);
03144       /* We don't have to worry about releasing memory of the actual
03145         got entries, since they're all in the master got_entries hash
03146         table anyway.  */
03147 
03148       BFD_ASSERT (old_lcount + lcount >= arg->primary->local_gotno);
03149       BFD_ASSERT (old_gcount + gcount >= arg->primary->global_gotno);
03150       BFD_ASSERT (old_tcount + tcount >= arg->primary->tls_gotno);
03151 
03152       arg->primary_count = arg->primary->local_gotno
03153        + arg->primary->global_gotno + arg->primary->tls_gotno;
03154     }
03155   /* If we can merge with the last-created got, do it.  */
03156   else if (arg->current
03157           && arg->current_count + lcount + gcount + tcount <= maxcnt)
03158     {
03159       struct mips_got_info *g = bfd2got->g;
03160       int old_lcount = arg->current->local_gotno;
03161       int old_gcount = arg->current->global_gotno;
03162       int old_tcount = arg->current->tls_gotno;
03163 
03164       bfd2got->g = arg->current;
03165 
03166       htab_traverse (g->got_entries,
03167                    mips_elf_make_got_per_bfd,
03168                    arg);
03169       if (arg->obfd == NULL)
03170        return 0;
03171 
03172       htab_delete (g->got_entries);
03173 
03174       BFD_ASSERT (old_lcount + lcount >= arg->current->local_gotno);
03175       BFD_ASSERT (old_gcount + gcount >= arg->current->global_gotno);
03176       BFD_ASSERT (old_tcount + tcount >= arg->current->tls_gotno);
03177 
03178       arg->current_count = arg->current->local_gotno
03179        + arg->current->global_gotno + arg->current->tls_gotno;
03180     }
03181   /* Well, we couldn't merge, so create a new GOT.  Don't check if it
03182      fits; if it turns out that it doesn't, we'll get relocation
03183      overflows anyway.  */
03184   else
03185     {
03186       bfd2got->g->next = arg->current;
03187       arg->current = bfd2got->g;
03188 
03189       arg->current_count = lcount + gcount + 2 * tcount;
03190     }
03191 
03192   return 1;
03193 }
03194 
03195 /* Set the TLS GOT index for the GOT entry in ENTRYP.  ENTRYP's NEXT field
03196    is null iff there is just a single GOT.  */
03197 
03198 static int
03199 mips_elf_initialize_tls_index (void **entryp, void *p)
03200 {
03201   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
03202   struct mips_got_info *g = p;
03203   bfd_vma next_index;
03204 
03205   /* We're only interested in TLS symbols.  */
03206   if (entry->tls_type == 0)
03207     return 1;
03208 
03209   next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
03210 
03211   if (entry->symndx == -1 && g->next == NULL)
03212     {
03213       /* A type (3) got entry in the single-GOT case.  We use the symbol's
03214         hash table entry to track its index.  */
03215       if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
03216        return 1;
03217       entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
03218       entry->d.h->tls_got_offset = next_index;
03219     }
03220   else
03221     {
03222       if (entry->tls_type & GOT_TLS_LDM)
03223        {
03224          /* There are separate mips_got_entry objects for each input bfd
03225             that requires an LDM entry.  Make sure that all LDM entries in
03226             a GOT resolve to the same index.  */
03227          if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
03228            {
03229              entry->gotidx = g->tls_ldm_offset;
03230              return 1;
03231            }
03232          g->tls_ldm_offset = next_index;
03233        }
03234       entry->gotidx = next_index;
03235     }
03236 
03237   /* Account for the entries we've just allocated.  */
03238   if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
03239     g->tls_assigned_gotno += 2;
03240   if (entry->tls_type & GOT_TLS_IE)
03241     g->tls_assigned_gotno += 1;
03242 
03243   return 1;
03244 }
03245 
03246 /* If passed a NULL mips_got_info in the argument, set the marker used
03247    to tell whether a global symbol needs a got entry (in the primary
03248    got) to the given VALUE.
03249 
03250    If passed a pointer G to a mips_got_info in the argument (it must
03251    not be the primary GOT), compute the offset from the beginning of
03252    the (primary) GOT section to the entry in G corresponding to the
03253    global symbol.  G's assigned_gotno must contain the index of the
03254    first available global GOT entry in G.  VALUE must contain the size
03255    of a GOT entry in bytes.  For each global GOT entry that requires a
03256    dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
03257    marked as not eligible for lazy resolution through a function
03258    stub.  */
03259 static int
03260 mips_elf_set_global_got_offset (void **entryp, void *p)
03261 {
03262   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
03263   struct mips_elf_set_global_got_offset_arg *arg
03264     = (struct mips_elf_set_global_got_offset_arg *)p;
03265   struct mips_got_info *g = arg->g;
03266 
03267   if (g && entry->tls_type != GOT_NORMAL)
03268     arg->needed_relocs +=
03269       mips_tls_got_relocs (arg->info, entry->tls_type,
03270                         entry->symndx == -1 ? &entry->d.h->root : NULL);
03271 
03272   if (entry->abfd != NULL && entry->symndx == -1
03273       && entry->d.h->root.dynindx != -1
03274       && entry->d.h->tls_type == GOT_NORMAL)
03275     {
03276       if (g)
03277        {
03278          BFD_ASSERT (g->global_gotsym == NULL);
03279 
03280          entry->gotidx = arg->value * (long) g->assigned_gotno++;
03281          if (arg->info->shared
03282              || (elf_hash_table (arg->info)->dynamic_sections_created
03283                 && entry->d.h->root.def_dynamic
03284                 && !entry->d.h->root.def_regular))
03285            ++arg->needed_relocs;
03286        }
03287       else
03288        entry->d.h->root.got.offset = arg->value;
03289     }
03290 
03291   return 1;
03292 }
03293 
03294 /* Mark any global symbols referenced in the GOT we are iterating over
03295    as inelligible for lazy resolution stubs.  */
03296 static int
03297 mips_elf_set_no_stub (void **entryp, void *p ATTRIBUTE_UNUSED)
03298 {
03299   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
03300 
03301   if (entry->abfd != NULL
03302       && entry->symndx == -1
03303       && entry->d.h->root.dynindx != -1)
03304     entry->d.h->no_fn_stub = TRUE;
03305 
03306   return 1;
03307 }
03308 
03309 /* Follow indirect and warning hash entries so that each got entry
03310    points to the final symbol definition.  P must point to a pointer
03311    to the hash table we're traversing.  Since this traversal may
03312    modify the hash table, we set this pointer to NULL to indicate
03313    we've made a potentially-destructive change to the hash table, so
03314    the traversal must be restarted.  */
03315 static int
03316 mips_elf_resolve_final_got_entry (void **entryp, void *p)
03317 {
03318   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
03319   htab_t got_entries = *(htab_t *)p;
03320 
03321   if (entry->abfd != NULL && entry->symndx == -1)
03322     {
03323       struct mips_elf_link_hash_entry *h = entry->d.h;
03324 
03325       while (h->root.root.type == bfd_link_hash_indirect
03326             || h->root.root.type == bfd_link_hash_warning)
03327        h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
03328 
03329       if (entry->d.h == h)
03330        return 1;
03331 
03332       entry->d.h = h;
03333 
03334       /* If we can't find this entry with the new bfd hash, re-insert
03335         it, and get the traversal restarted.  */
03336       if (! htab_find (got_entries, entry))
03337        {
03338          htab_clear_slot (got_entries, entryp);
03339          entryp = htab_find_slot (got_entries, entry, INSERT);
03340          if (! *entryp)
03341            *entryp = entry;
03342          /* Abort the traversal, since the whole table may have
03343             moved, and leave it up to the parent to restart the
03344             process.  */
03345          *(htab_t *)p = NULL;
03346          return 0;
03347        }
03348       /* We might want to decrement the global_gotno count, but it's
03349         either too early or too late for that at this point.  */
03350     }
03351 
03352   return 1;
03353 }
03354 
03355 /* Turn indirect got entries in a got_entries table into their final
03356    locations.  */
03357 static void
03358 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
03359 {
03360   htab_t got_entries;
03361 
03362   do
03363     {
03364       got_entries = g->got_entries;
03365 
03366       htab_traverse (got_entries,
03367                    mips_elf_resolve_final_got_entry,
03368                    &got_entries);
03369     }
03370   while (got_entries == NULL);
03371 }
03372 
03373 /* Return the offset of an input bfd IBFD's GOT from the beginning of
03374    the primary GOT.  */
03375 static bfd_vma
03376 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
03377 {
03378   if (g->bfd2got == NULL)
03379     return 0;
03380 
03381   g = mips_elf_got_for_ibfd (g, ibfd);
03382   if (! g)
03383     return 0;
03384 
03385   BFD_ASSERT (g->next);
03386 
03387   g = g->next;
03388 
03389   return (g->local_gotno + g->global_gotno + g->tls_gotno)
03390     * MIPS_ELF_GOT_SIZE (abfd);
03391 }
03392 
03393 /* Turn a single GOT that is too big for 16-bit addressing into
03394    a sequence of GOTs, each one 16-bit addressable.  */
03395 
03396 static bfd_boolean
03397 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
03398                   struct mips_got_info *g, asection *got,
03399                   bfd_size_type pages)
03400 {
03401   struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
03402   struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
03403   struct mips_got_info *gg;
03404   unsigned int assign;
03405 
03406   g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
03407                             mips_elf_bfd2got_entry_eq, NULL);
03408   if (g->bfd2got == NULL)
03409     return FALSE;
03410 
03411   got_per_bfd_arg.bfd2got = g->bfd2got;
03412   got_per_bfd_arg.obfd = abfd;
03413   got_per_bfd_arg.info = info;
03414 
03415   /* Count how many GOT entries each input bfd requires, creating a
03416      map from bfd to got info while at that.  */
03417   htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
03418   if (got_per_bfd_arg.obfd == NULL)
03419     return FALSE;
03420 
03421   got_per_bfd_arg.current = NULL;
03422   got_per_bfd_arg.primary = NULL;
03423   /* Taking out PAGES entries is a worst-case estimate.  We could
03424      compute the maximum number of pages that each separate input bfd
03425      uses, but it's probably not worth it.  */
03426   got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
03427                             / MIPS_ELF_GOT_SIZE (abfd))
03428                             - MIPS_RESERVED_GOTNO (info) - pages);
03429   /* The number of globals that will be included in the primary GOT.
03430      See the calls to mips_elf_set_global_got_offset below for more
03431      information.  */
03432   got_per_bfd_arg.global_count = g->global_gotno;
03433 
03434   /* Try to merge the GOTs of input bfds together, as long as they
03435      don't seem to exceed the maximum GOT size, choosing one of them
03436      to be the primary GOT.  */
03437   htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
03438   if (got_per_bfd_arg.obfd == NULL)
03439     return FALSE;
03440 
03441   /* If we do not find any suitable primary GOT, create an empty one.  */
03442   if (got_per_bfd_arg.primary == NULL)
03443     {
03444       g->next = (struct mips_got_info *)
03445        bfd_alloc (abfd, sizeof (struct mips_got_info));
03446       if (g->next == NULL)
03447        return FALSE;
03448 
03449       g->next->global_gotsym = NULL;
03450       g->next->global_gotno = 0;
03451       g->next->local_gotno = 0;
03452       g->next->tls_gotno = 0;
03453       g->next->assigned_gotno = 0;
03454       g->next->tls_assigned_gotno = 0;
03455       g->next->tls_ldm_offset = MINUS_ONE;
03456       g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
03457                                          mips_elf_multi_got_entry_eq,
03458                                          NULL);
03459       if (g->next->got_entries == NULL)
03460        return FALSE;
03461       g->next->bfd2got = NULL;
03462     }
03463   else
03464     g->next = got_per_bfd_arg.primary;
03465   g->next->next = got_per_bfd_arg.current;
03466 
03467   /* GG is now the master GOT, and G is the primary GOT.  */
03468   gg = g;
03469   g = g->next;
03470 
03471   /* Map the output bfd to the primary got.  That's what we're going
03472      to use for bfds that use GOT16 or GOT_PAGE relocations that we
03473      didn't mark in check_relocs, and we want a quick way to find it.
03474      We can't just use gg->next because we're going to reverse the
03475      list.  */
03476   {
03477     struct mips_elf_bfd2got_hash *bfdgot;
03478     void **bfdgotp;
03479 
03480     bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
03481       (abfd, sizeof (struct mips_elf_bfd2got_hash));
03482 
03483     if (bfdgot == NULL)
03484       return FALSE;
03485 
03486     bfdgot->bfd = abfd;
03487     bfdgot->g = g;
03488     bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
03489 
03490     BFD_ASSERT (*bfdgotp == NULL);
03491     *bfdgotp = bfdgot;
03492   }
03493 
03494   /* The IRIX dynamic linker requires every symbol that is referenced
03495      in a dynamic relocation to be present in the primary GOT, so
03496      arrange for them to appear after those that are actually
03497      referenced.
03498 
03499      GNU/Linux could very well do without it, but it would slow down
03500      the dynamic linker, since it would have to resolve every dynamic
03501      symbol referenced in other GOTs more than once, without help from
03502      the cache.  Also, knowing that every external symbol has a GOT
03503      helps speed up the resolution of local symbols too, so GNU/Linux
03504      follows IRIX's practice.
03505 
03506      The number 2 is used by mips_elf_sort_hash_table_f to count
03507      global GOT symbols that are unreferenced in the primary GOT, with
03508      an initial dynamic index computed from gg->assigned_gotno, where
03509      the number of unreferenced global entries in the primary GOT is
03510      preserved.  */
03511   if (1)
03512     {
03513       gg->assigned_gotno = gg->global_gotno - g->global_gotno;
03514       g->global_gotno = gg->global_gotno;
03515       set_got_offset_arg.value = 2;
03516     }
03517   else
03518     {
03519       /* This could be used for dynamic linkers that don't optimize
03520         symbol resolution while applying relocations so as to use
03521         primary GOT entries or assuming the symbol is locally-defined.
03522         With this code, we assign lower dynamic indices to global
03523         symbols that are not referenced in the primary GOT, so that
03524         their entries can be omitted.  */
03525       gg->assigned_gotno = 0;
03526       set_got_offset_arg.value = -1;
03527     }
03528 
03529   /* Reorder dynamic symbols as described above (which behavior
03530      depends on the setting of VALUE).  */
03531   set_got_offset_arg.g = NULL;
03532   htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
03533                &set_got_offset_arg);
03534   set_got_offset_arg.value = 1;
03535   htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
03536                &set_got_offset_arg);
03537   if (! mips_elf_sort_hash_table (info, 1))
03538     return FALSE;
03539 
03540   /* Now go through the GOTs assigning them offset ranges.
03541      [assigned_gotno, local_gotno[ will be set to the range of local
03542      entries in each GOT.  We can then compute the end of a GOT by
03543      adding local_gotno to global_gotno.  We reverse the list and make
03544      it circular since then we'll be able to quickly compute the
03545      beginning of a GOT, by computing the end of its predecessor.  To
03546      avoid special cases for the primary GOT, while still preserving
03547      assertions that are valid for both single- and multi-got links,
03548      we arrange for the main got struct to have the right number of
03549      global entries, but set its local_gotno such that the initial
03550      offset of the primary GOT is zero.  Remember that the primary GOT
03551      will become the last item in the circular linked list, so it
03552      points back to the master GOT.  */
03553   gg->local_gotno = -g->global_gotno;
03554   gg->global_gotno = g->global_gotno;
03555   gg->tls_gotno = 0;
03556   assign = 0;
03557   gg->next = gg;
03558 
03559   do
03560     {
03561       struct mips_got_info *gn;
03562 
03563       assign += MIPS_RESERVED_GOTNO (info);
03564       g->assigned_gotno = assign;
03565       g->local_gotno += assign + pages;
03566       assign = g->local_gotno + g->global_gotno + g->tls_gotno;
03567 
03568       /* Take g out of the direct list, and push it onto the reversed
03569         list that gg points to.  g->next is guaranteed to be nonnull after
03570         this operation, as required by mips_elf_initialize_tls_index. */
03571       gn = g->next;
03572       g->next = gg->next;
03573       gg->next = g;
03574 
03575       /* Set up any TLS entries.  We always place the TLS entries after
03576         all non-TLS entries.  */
03577       g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
03578       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
03579 
03580       /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
03581       g = gn;
03582 
03583       /* Mark global symbols in every non-primary GOT as ineligible for
03584         stubs.  */
03585       if (g)
03586        htab_traverse (g->got_entries, mips_elf_set_no_stub, NULL);
03587     }
03588   while (g);
03589 
03590   got->size = (gg->next->local_gotno
03591                   + gg->next->global_gotno
03592                   + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
03593 
03594   return TRUE;
03595 }
03596 
03597 
03598 /* Returns the first relocation of type r_type found, beginning with
03599    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
03600 
03601 static const Elf_Internal_Rela *
03602 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
03603                        const Elf_Internal_Rela *relocation,
03604                        const Elf_Internal_Rela *relend)
03605 {
03606   unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
03607 
03608   while (relocation < relend)
03609     {
03610       if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
03611          && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
03612        return relocation;
03613 
03614       ++relocation;
03615     }
03616 
03617   /* We didn't find it.  */
03618   return NULL;
03619 }
03620 
03621 /* Return whether a relocation is against a local symbol.  */
03622 
03623 static bfd_boolean
03624 mips_elf_local_relocation_p (bfd *input_bfd,
03625                           const Elf_Internal_Rela *relocation,
03626                           asection **local_sections,
03627                           bfd_boolean check_forced)
03628 {
03629   unsigned long r_symndx;
03630   Elf_Internal_Shdr *symtab_hdr;
03631   struct mips_elf_link_hash_entry *h;
03632   size_t extsymoff;
03633 
03634   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
03635   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
03636   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
03637 
03638   if (r_symndx < extsymoff)
03639     return TRUE;
03640   if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
03641     return TRUE;
03642 
03643   if (check_forced)
03644     {
03645       /* Look up the hash table to check whether the symbol
03646         was forced local.  */
03647       h = (struct mips_elf_link_hash_entry *)
03648        elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
03649       /* Find the real hash-table entry for this symbol.  */
03650       while (h->root.root.type == bfd_link_hash_indirect
03651             || h->root.root.type == bfd_link_hash_warning)
03652        h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
03653       if (h->root.forced_local)
03654        return TRUE;
03655     }
03656 
03657   return FALSE;
03658 }
03659 
03660 /* Sign-extend VALUE, which has the indicated number of BITS.  */
03661 
03662 bfd_vma
03663 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
03664 {
03665   if (value & ((bfd_vma) 1 << (bits - 1)))
03666     /* VALUE is negative.  */
03667     value |= ((bfd_vma) - 1) << bits;
03668 
03669   return value;
03670 }
03671 
03672 /* Return non-zero if the indicated VALUE has overflowed the maximum
03673    range expressible by a signed number with the indicated number of
03674    BITS.  */
03675 
03676 static bfd_boolean
03677 mips_elf_overflow_p (bfd_vma value, int bits)
03678 {
03679   bfd_signed_vma svalue = (bfd_signed_vma) value;
03680 
03681   if (svalue > (1 << (bits - 1)) - 1)
03682     /* The value is too big.  */
03683     return TRUE;
03684   else if (svalue < -(1 << (bits - 1)))
03685     /* The value is too small.  */
03686     return TRUE;
03687 
03688   /* All is well.  */
03689   return FALSE;
03690 }
03691 
03692 /* Calculate the %high function.  */
03693 
03694 static bfd_vma
03695 mips_elf_high (bfd_vma value)
03696 {
03697   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
03698 }
03699 
03700 /* Calculate the %higher function.  */
03701 
03702 static bfd_vma
03703 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
03704 {
03705 #ifdef BFD64
03706   return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
03707 #else
03708   abort ();
03709   return MINUS_ONE;
03710 #endif
03711 }
03712 
03713 /* Calculate the %highest function.  */
03714 
03715 static bfd_vma
03716 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
03717 {
03718 #ifdef BFD64
03719   return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
03720 #else
03721   abort ();
03722   return MINUS_ONE;
03723 #endif
03724 }
03725 
03726 /* Create the .compact_rel section.  */
03727 
03728 static bfd_boolean
03729 mips_elf_create_compact_rel_section
03730   (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
03731 {
03732   flagword flags;
03733   register asection *s;
03734 
03735   if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
03736     {
03737       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
03738               | SEC_READONLY);
03739 
03740       s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
03741       if (s == NULL
03742          || ! bfd_set_section_alignment (abfd, s,
03743                                      MIPS_ELF_LOG_FILE_ALIGN (abfd)))
03744        return FALSE;
03745 
03746       s->size = sizeof (Elf32_External_compact_rel);
03747     }
03748 
03749   return TRUE;
03750 }
03751 
03752 /* Create the .got section to hold the global offset table.  */
03753 
03754 static bfd_boolean
03755 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info,
03756                           bfd_boolean maybe_exclude)
03757 {
03758   flagword flags;
03759   register asection *s;
03760   struct elf_link_hash_entry *h;
03761   struct bfd_link_hash_entry *bh;
03762   struct mips_got_info *g;
03763   bfd_size_type amt;
03764   struct mips_elf_link_hash_table *htab;
03765 
03766   htab = mips_elf_hash_table (info);
03767 
03768   /* This function may be called more than once.  */
03769   s = mips_elf_got_section (abfd, TRUE);
03770   if (s)
03771     {
03772       if (! maybe_exclude)
03773        s->flags &= ~SEC_EXCLUDE;
03774       return TRUE;
03775     }
03776 
03777   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
03778           | SEC_LINKER_CREATED);
03779 
03780   if (maybe_exclude)
03781     flags |= SEC_EXCLUDE;
03782 
03783   /* We have to use an alignment of 2**4 here because this is hardcoded
03784      in the function stub generation and in the linker script.  */
03785   s = bfd_make_section_with_flags (abfd, ".got", flags);
03786   if (s == NULL
03787       || ! bfd_set_section_alignment (abfd, s, 4))
03788     return FALSE;
03789 
03790   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
03791      linker script because we don't want to define the symbol if we
03792      are not creating a global offset table.  */
03793   bh = NULL;
03794   if (! (_bfd_generic_link_add_one_symbol
03795         (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
03796          0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
03797     return FALSE;
03798 
03799   h = (struct elf_link_hash_entry *) bh;
03800   h->non_elf = 0;
03801   h->def_regular = 1;
03802   h->type = STT_OBJECT;
03803   elf_hash_table (info)->hgot = h;
03804 
03805   if (info->shared
03806       && ! bfd_elf_link_record_dynamic_symbol (info, h))
03807     return FALSE;
03808 
03809   amt = sizeof (struct mips_got_info);
03810   g = bfd_alloc (abfd, amt);
03811   if (g == NULL)
03812     return FALSE;
03813   g->global_gotsym = NULL;
03814   g->global_gotno = 0;
03815   g->tls_gotno = 0;
03816   g->local_gotno = MIPS_RESERVED_GOTNO (info);
03817   g->assigned_gotno = MIPS_RESERVED_GOTNO (info);
03818   g->bfd2got = NULL;
03819   g->next = NULL;
03820   g->tls_ldm_offset = MINUS_ONE;
03821   g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
03822                                 mips_elf_got_entry_eq, NULL);
03823   if (g->got_entries == NULL)
03824     return FALSE;
03825   mips_elf_section_data (s)->u.got_info = g;
03826   mips_elf_section_data (s)->elf.this_hdr.sh_flags
03827     |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
03828 
03829   /* VxWorks also needs a .got.plt section.  */
03830   if (htab->is_vxworks)
03831     {
03832       s = bfd_make_section_with_flags (abfd, ".got.plt",
03833                                    SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
03834                                    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
03835       if (s == NULL || !bfd_set_section_alignment (abfd, s, 4))
03836        return FALSE;
03837 
03838       htab->sgotplt = s;
03839     }
03840   return TRUE;
03841 }
03842 
03843 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
03844    __GOTT_INDEX__ symbols.  These symbols are only special for
03845    shared objects; they are not used in executables.  */
03846 
03847 static bfd_boolean
03848 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
03849 {
03850   return (mips_elf_hash_table (info)->is_vxworks
03851          && info->shared
03852          && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
03853              || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
03854 }
03855 
03856 /* Calculate the value produced by the RELOCATION (which comes from
03857    the INPUT_BFD).  The ADDEND is the addend to use for this
03858    RELOCATION; RELOCATION->R_ADDEND is ignored.
03859 
03860    The result of the relocation calculation is stored in VALUEP.
03861    REQUIRE_JALXP indicates whether or not the opcode used with this
03862    relocation must be JALX.
03863 
03864    This function returns bfd_reloc_continue if the caller need take no
03865    further action regarding this relocation, bfd_reloc_notsupported if
03866    something goes dramatically wrong, bfd_reloc_overflow if an
03867    overflow occurs, and bfd_reloc_ok to indicate success.  */
03868 
03869 static bfd_reloc_status_type
03870 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
03871                             asection *input_section,
03872                             struct bfd_link_info *info,
03873                             const Elf_Internal_Rela *relocation,
03874                             bfd_vma addend, reloc_howto_type *howto,
03875                             Elf_Internal_Sym *local_syms,
03876                             asection **local_sections, bfd_vma *valuep,
03877                             const char **namep, bfd_boolean *require_jalxp,
03878                             bfd_boolean save_addend)
03879 {
03880   /* The eventual value we will return.  */
03881   bfd_vma value;
03882   /* The address of the symbol against which the relocation is
03883      occurring.  */
03884   bfd_vma symbol = 0;
03885   /* The final GP value to be used for the relocatable, executable, or
03886      shared object file being produced.  */
03887   bfd_vma gp = MINUS_ONE;
03888   /* The place (section offset or address) of the storage unit being
03889      relocated.  */
03890   bfd_vma p;
03891   /* The value of GP used to create the relocatable object.  */
03892   bfd_vma gp0 = MINUS_ONE;
03893   /* The offset into the global offset table at which the address of
03894      the relocation entry symbol, adjusted by the addend, resides
03895      during execution.  */
03896   bfd_vma g = MINUS_ONE;
03897   /* The section in which the symbol referenced by the relocation is
03898      located.  */
03899   asection *sec = NULL;
03900   struct mips_elf_link_hash_entry *h = NULL;
03901   /* TRUE if the symbol referred to by this relocation is a local
03902      symbol.  */
03903   bfd_boolean local_p, was_local_p;
03904   /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
03905   bfd_boolean gp_disp_p = FALSE;
03906   /* TRUE if the symbol referred to by this relocation is
03907      "__gnu_local_gp".  */
03908   bfd_boolean gnu_local_gp_p = FALSE;
03909   Elf_Internal_Shdr *symtab_hdr;
03910   size_t extsymoff;
03911   unsigned long r_symndx;
03912   int r_type;
03913   /* TRUE if overflow occurred during the calculation of the
03914      relocation value.  */
03915   bfd_boolean overflowed_p;
03916   /* TRUE if this relocation refers to a MIPS16 function.  */
03917   bfd_boolean target_is_16_bit_code_p = FALSE;
03918   struct mips_elf_link_hash_table *htab;
03919   bfd *dynobj;
03920 
03921   dynobj = elf_hash_table (info)->dynobj;
03922   htab = mips_elf_hash_table (info);
03923 
03924   /* Parse the relocation.  */
03925   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
03926   r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
03927   p = (input_section->output_section->vma
03928        + input_section->output_offset
03929        + relocation->r_offset);
03930 
03931   /* Assume that there will be no overflow.  */
03932   overflowed_p = FALSE;
03933 
03934   /* Figure out whether or not the symbol is local, and get the offset
03935      used in the array of hash table entries.  */
03936   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
03937   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
03938                                     local_sections, FALSE);
03939   was_local_p = local_p;
03940   if (! elf_bad_symtab (input_bfd))
03941     extsymoff = symtab_hdr->sh_info;
03942   else
03943     {
03944       /* The symbol table does not follow the rule that local symbols
03945         must come before globals.  */
03946       extsymoff = 0;
03947     }
03948 
03949   /* Figure out the value of the symbol.  */
03950   if (local_p)
03951     {
03952       Elf_Internal_Sym *sym;
03953 
03954       sym = local_syms + r_symndx;
03955       sec = local_sections[r_symndx];
03956 
03957       symbol = sec->output_section->vma + sec->output_offset;
03958       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
03959          || (sec->flags & SEC_MERGE))
03960        symbol += sym->st_value;
03961       if ((sec->flags & SEC_MERGE)
03962          && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
03963        {
03964          addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
03965          addend -= symbol;
03966          addend += sec->output_section->vma + sec->output_offset;
03967        }
03968 
03969       /* MIPS16 text labels should be treated as odd.  */
03970       if (sym->st_other == STO_MIPS16)
03971        ++symbol;
03972 
03973       /* Record the name of this symbol, for our caller.  */
03974       *namep = bfd_elf_string_from_elf_section (input_bfd,
03975                                           symtab_hdr->sh_link,
03976                                           sym->st_name);
03977       if (*namep == '\0')
03978        *namep = bfd_section_name (input_bfd, sec);
03979 
03980       target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
03981     }
03982   else
03983     {
03984       /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
03985 
03986       /* For global symbols we look up the symbol in the hash-table.  */
03987       h = ((struct mips_elf_link_hash_entry *)
03988           elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
03989       /* Find the real hash-table entry for this symbol.  */
03990       while (h->root.root.type == bfd_link_hash_indirect
03991             || h->root.root.type == bfd_link_hash_warning)
03992        h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
03993 
03994       /* Record the name of this symbol, for our caller.  */
03995       *namep = h->root.root.root.string;
03996 
03997       /* See if this is the special _gp_disp symbol.  Note that such a
03998         symbol must always be a global symbol.  */
03999       if (strcmp (*namep, "_gp_disp") == 0
04000          && ! NEWABI_P (input_bfd))
04001        {
04002          /* Relocations against _gp_disp are permitted only with
04003             R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
04004          if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16
04005              && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
04006            return bfd_reloc_notsupported;
04007 
04008          gp_disp_p = TRUE;
04009        }
04010       /* See if this is the special _gp symbol.  Note that such a
04011         symbol must always be a global symbol.  */
04012       else if (strcmp (*namep, "__gnu_local_gp") == 0)
04013        gnu_local_gp_p = TRUE;
04014 
04015 
04016       /* If this symbol is defined, calculate its address.  Note that
04017         _gp_disp is a magic symbol, always implicitly defined by the
04018         linker, so it's inappropriate to check to see whether or not
04019         its defined.  */
04020       else if ((h->root.root.type == bfd_link_hash_defined
04021               || h->root.root.type == bfd_link_hash_defweak)
04022               && h->root.root.u.def.section)
04023        {
04024          sec = h->root.root.u.def.section;
04025          if (sec->output_section)
04026            symbol = (h->root.root.u.def.value
04027                     + sec->output_section->vma
04028                     + sec->output_offset);
04029          else
04030            symbol = h->root.root.u.def.value;
04031        }
04032       else if (h->root.root.type == bfd_link_hash_undefweak)
04033        /* We allow relocations against undefined weak symbols, giving
04034           it the value zero, so that you can undefined weak functions
04035           and check to see if they exist by looking at their
04036           addresses.  */
04037        symbol = 0;
04038       else if (info->unresolved_syms_in_objects == RM_IGNORE
04039               && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
04040        symbol = 0;
04041       else if (strcmp (*namep, SGI_COMPAT (input_bfd)
04042                      ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
04043        {
04044          /* If this is a dynamic link, we should have created a
04045             _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
04046             in in _bfd_mips_elf_create_dynamic_sections.
04047             Otherwise, we should define the symbol with a value of 0.
04048             FIXME: It should probably get into the symbol table
04049             somehow as well.  */
04050          BFD_ASSERT (! info->shared);
04051          BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
04052          symbol = 0;
04053        }
04054       else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
04055        {
04056          /* This is an optional symbol - an Irix specific extension to the
04057             ELF spec.  Ignore it for now.
04058             XXX - FIXME - there is more to the spec for OPTIONAL symbols
04059             than simply ignoring them, but we do not handle this for now.
04060             For information see the "64-bit ELF Object File Specification"
04061             which is available from here:
04062             http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
04063          symbol = 0;
04064        }
04065       else
04066        {
04067          if (! ((*info->callbacks->undefined_symbol)
04068                (info, h->root.root.root.string, input_bfd,
04069                 input_section, relocation->r_offset,
04070                 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
04071                  || ELF_ST_VISIBILITY (h->root.other))))
04072            return bfd_reloc_undefined;
04073          symbol = 0;
04074        }
04075 
04076       target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
04077     }
04078 
04079   /* If this is a 32- or 64-bit call to a 16-bit function with a stub, we
04080      need to redirect the call to the stub, unless we're already *in*
04081      a stub.  */
04082   if (r_type != R_MIPS16_26 && !info->relocatable
04083       && ((h != NULL && h->fn_stub != NULL)
04084          || (local_p
04085              && elf_tdata (input_bfd)->local_stubs != NULL
04086              && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
04087       && !mips16_stub_section_p (input_bfd, input_section))
04088     {
04089       /* This is a 32- or 64-bit call to a 16-bit function.  We should
04090         have already noticed that we were going to need the
04091         stub.  */
04092       if (local_p)
04093        sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
04094       else
04095        {
04096          BFD_ASSERT (h->need_fn_stub);
04097          sec = h->fn_stub;
04098        }
04099 
04100       symbol = sec->output_section->vma + sec->output_offset;
04101       /* The target is 16-bit, but the stub isn't.  */
04102       target_is_16_bit_code_p = FALSE;
04103     }
04104   /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
04105      need to redirect the call to the stub.  */
04106   else if (r_type == R_MIPS16_26 && !info->relocatable
04107           && h != NULL
04108           && ((h->call_stub != NULL || h->call_fp_stub != NULL)
04109               || (local_p
04110                  && elf_tdata (input_bfd)->local_call_stubs != NULL
04111                  && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
04112           && !target_is_16_bit_code_p)
04113     {
04114       if (local_p)
04115        sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
04116       else
04117        {
04118          /* If both call_stub and call_fp_stub are defined, we can figure
04119             out which one to use by checking which one appears in the input
04120             file.  */
04121          if (h->call_stub != NULL && h->call_fp_stub != NULL)
04122            {
04123              asection *o;
04124              
04125              sec = NULL;
04126              for (o = input_bfd->sections; o != NULL; o = o->next)
04127               {
04128                 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
04129                   {
04130                     sec = h->call_fp_stub;
04131                     break;
04132                   }
04133               }
04134              if (sec == NULL)
04135               sec = h->call_stub;
04136            }
04137          else if (h->call_stub != NULL)
04138            sec = h->call_stub;
04139          else
04140            sec = h->call_fp_stub;
04141        }
04142 
04143       BFD_ASSERT (sec->size > 0);
04144       symbol = sec->output_section->vma + sec->output_offset;
04145     }
04146 
04147   /* Calls from 16-bit code to 32-bit code and vice versa require the
04148      special jalx instruction.  */
04149   *require_jalxp = (!info->relocatable
04150                     && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
04151                         || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
04152 
04153   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
04154                                     local_sections, TRUE);
04155 
04156   /* If we haven't already determined the GOT offset, or the GP value,
04157      and we're going to need it, get it now.  */
04158   switch (r_type)
04159     {
04160     case R_MIPS_GOT_PAGE:
04161     case R_MIPS_GOT_OFST:
04162       /* We need to decay to GOT_DISP/addend if the symbol doesn't
04163         bind locally.  */
04164       local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
04165       if (local_p || r_type == R_MIPS_GOT_OFST)
04166        break;
04167       /* Fall through.  */
04168 
04169     case R_MIPS_CALL16:
04170     case R_MIPS_GOT16:
04171     case R_MIPS_GOT_DISP:
04172     case R_MIPS_GOT_HI16:
04173     case R_MIPS_CALL_HI16:
04174     case R_MIPS_GOT_LO16:
04175     case R_MIPS_CALL_LO16:
04176     case R_MIPS_TLS_GD:
04177     case R_MIPS_TLS_GOTTPREL:
04178     case R_MIPS_TLS_LDM:
04179       /* Find the index into the GOT where this value is located.  */
04180       if (r_type == R_MIPS_TLS_LDM)
04181        {
04182          g = mips_elf_local_got_index (abfd, input_bfd, info,
04183                                    sec, 0, 0, NULL, r_type);
04184          if (g == MINUS_ONE)
04185            return bfd_reloc_outofrange;
04186        }
04187       else if (!local_p)
04188        {
04189          /* On VxWorks, CALL relocations should refer to the .got.plt
04190             entry, which is initialized to point at the PLT stub.  */
04191          if (htab->is_vxworks
04192              && (r_type == R_MIPS_CALL_HI16
04193                 || r_type == R_MIPS_CALL_LO16
04194                 || r_type == R_MIPS_CALL16))
04195            {
04196              BFD_ASSERT (addend == 0);
04197              BFD_ASSERT (h->root.needs_plt);
04198              g = mips_elf_gotplt_index (info, &h->root);
04199            }
04200          else
04201            {
04202              /* GOT_PAGE may take a non-zero addend, that is ignored in a
04203                GOT_PAGE relocation that decays to GOT_DISP because the
04204                symbol turns out to be global.  The addend is then added
04205                as GOT_OFST.  */
04206              BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
04207              g = mips_elf_global_got_index (dynobj, input_bfd,
04208                                         &h->root, r_type, info);
04209              if (h->tls_type == GOT_NORMAL
04210                 && (! elf_hash_table(info)->dynamic_sections_created
04211                     || (info->shared
04212                        && (info->symbolic || h->root.forced_local)
04213                        && h->root.def_regular)))
04214               {
04215                 /* This is a static link or a -Bsymbolic link.  The
04216                    symbol is defined locally, or was forced to be local.
04217                    We must initialize this entry in the GOT.  */
04218                 asection *sgot = mips_elf_got_section (dynobj, FALSE);
04219                 MIPS_ELF_PUT_WORD (dynobj, symbol, sgot->contents + g);
04220               }
04221            }
04222        }
04223       else if (!htab->is_vxworks
04224               && (r_type == R_MIPS_CALL16 || (r_type == R_MIPS_GOT16)))
04225        /* The calculation below does not involve "g".  */
04226        break;
04227       else
04228        {
04229          g = mips_elf_local_got_index (abfd, input_bfd, info, sec,
04230                                    symbol + addend, r_symndx, h, r_type);
04231          if (g == MINUS_ONE)
04232            return bfd_reloc_outofrange;
04233        }
04234 
04235       /* Convert GOT indices to actual offsets.  */
04236       g = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, g);
04237       break;
04238 
04239     case R_MIPS_HI16:
04240     case R_MIPS_LO16:
04241     case R_MIPS_GPREL16:
04242     case R_MIPS_GPREL32:
04243     case R_MIPS_LITERAL:
04244     case R_MIPS16_HI16:
04245     case R_MIPS16_LO16:
04246     case R_MIPS16_GPREL:
04247       gp0 = _bfd_get_gp_value (input_bfd);
04248       gp = _bfd_get_gp_value (abfd);
04249       if (dynobj)
04250        gp += mips_elf_adjust_gp (abfd, mips_elf_got_info (dynobj, NULL),
04251                               input_bfd);
04252       break;
04253 
04254     default:
04255       break;
04256     }
04257 
04258   if (gnu_local_gp_p)
04259     symbol = gp;
04260 
04261   /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
04262      symbols are resolved by the loader.  Add them to .rela.dyn.  */
04263   if (h != NULL && is_gott_symbol (info, &h->root))
04264     {
04265       Elf_Internal_Rela outrel;
04266       bfd_byte *loc;
04267       asection *s;
04268 
04269       s = mips_elf_rel_dyn_section (info, FALSE);
04270       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
04271 
04272       outrel.r_offset = (input_section->output_section->vma
04273                       + input_section->output_offset
04274                       + relocation->r_offset);
04275       outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
04276       outrel.r_addend = addend;
04277       bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
04278       *valuep = 0;
04279       return bfd_reloc_ok;
04280     }
04281 
04282   /* Figure out what kind of relocation is being performed.  */
04283   switch (r_type)
04284     {
04285     case R_MIPS_NONE:
04286       return bfd_reloc_continue;
04287 
04288     case R_MIPS_16:
04289       value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
04290       overflowed_p = mips_elf_overflow_p (value, 16);
04291       break;
04292 
04293     case R_MIPS_32:
04294     case R_MIPS_REL32:
04295     case R_MIPS_64:
04296       if ((info->shared
04297           || (!htab->is_vxworks
04298               && htab->root.dynamic_sections_created
04299               && h != NULL
04300               && h->root.def_dynamic
04301               && !h->root.def_regular))
04302          && r_symndx != 0
04303          && (input_section->flags & SEC_ALLOC) != 0)
04304        {
04305          /* If we're creating a shared library, or this relocation is
04306             against a symbol in a shared library, then we can't know
04307             where the symbol will end up.  So, we create a relocation
04308             record in the output, and leave the job up to the dynamic
04309             linker.
04310 
04311             In VxWorks executables, references to external symbols
04312             are handled using copy relocs or PLT stubs, so there's
04313             no need to add a dynamic relocation here.  */
04314          value = addend;
04315          if (!mips_elf_create_dynamic_relocation (abfd,
04316                                              info,
04317                                              relocation,
04318                                              h,
04319                                              sec,
04320                                              symbol,
04321                                              &value,
04322                                              input_section))
04323            return bfd_reloc_undefined;
04324        }
04325       else
04326        {
04327          if (r_type != R_MIPS_REL32)
04328            value = symbol + addend;
04329          else
04330            value = addend;
04331        }
04332       value &= howto->dst_mask;
04333       break;
04334 
04335     case R_MIPS_PC32:
04336       value = symbol + addend - p;
04337       value &= howto->dst_mask;
04338       break;
04339 
04340     case R_MIPS16_26:
04341       /* The calculation for R_MIPS16_26 is just the same as for an
04342         R_MIPS_26.  It's only the storage of the relocated field into
04343         the output file that's different.  That's handled in
04344         mips_elf_perform_relocation.  So, we just fall through to the
04345         R_MIPS_26 case here.  */
04346     case R_MIPS_26:
04347       if (local_p)
04348        value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
04349       else
04350        {
04351          value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
04352          if (h->root.root.type != bfd_link_hash_undefweak)
04353            overflowed_p = (value >> 26) != ((p + 4) >> 28);
04354        }
04355       value &= howto->dst_mask;
04356       break;
04357 
04358     case R_MIPS_TLS_DTPREL_HI16:
04359       value = (mips_elf_high (addend + symbol - dtprel_base (info))
04360               & howto->dst_mask);
04361       break;
04362 
04363     case R_MIPS_TLS_DTPREL_LO16:
04364       value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
04365       break;
04366 
04367     case R_MIPS_TLS_TPREL_HI16:
04368       value = (mips_elf_high (addend + symbol - tprel_base (info))
04369               & howto->dst_mask);
04370       break;
04371 
04372     case R_MIPS_TLS_TPREL_LO16:
04373       value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
04374       break;
04375 
04376     case R_MIPS_HI16:
04377     case R_MIPS16_HI16:
04378       if (!gp_disp_p)
04379        {
04380          value = mips_elf_high (addend + symbol);
04381          value &= howto->dst_mask;
04382        }
04383       else
04384        {
04385          /* For MIPS16 ABI code we generate this sequence
04386                0: li      $v0,%hi(_gp_disp)
04387                4: addiupc $v1,%lo(_gp_disp)
04388                8: sll     $v0,16
04389               12: addu    $v0,$v1
04390               14: move    $gp,$v0
04391             So the offsets of hi and lo relocs are the same, but the
04392             $pc is four higher than $t9 would be, so reduce
04393             both reloc addends by 4. */
04394          if (r_type == R_MIPS16_HI16)
04395            value = mips_elf_high (addend + gp - p - 4);
04396          else
04397            value = mips_elf_high (addend + gp - p);
04398          overflowed_p = mips_elf_overflow_p (value, 16);
04399        }
04400       break;
04401 
04402     case R_MIPS_LO16:
04403     case R_MIPS16_LO16:
04404       if (!gp_disp_p)
04405        value = (symbol + addend) & howto->dst_mask;
04406       else
04407        {
04408          /* See the comment for R_MIPS16_HI16 above for the reason
04409             for this conditional.  */
04410          if (r_type == R_MIPS16_LO16)
04411            value = addend + gp - p;
04412          else
04413            value = addend + gp - p + 4;
04414          /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
04415             for overflow.  But, on, say, IRIX5, relocations against
04416             _gp_disp are normally generated from the .cpload
04417             pseudo-op.  It generates code that normally looks like
04418             this:
04419 
04420               lui    $gp,%hi(_gp_disp)
04421               addiu  $gp,$gp,%lo(_gp_disp)
04422               addu   $gp,$gp,$t9
04423 
04424             Here $t9 holds the address of the function being called,
04425             as required by the MIPS ELF ABI.  The R_MIPS_LO16
04426             relocation can easily overflow in this situation, but the
04427             R_MIPS_HI16 relocation will handle the overflow.
04428             Therefore, we consider this a bug in the MIPS ABI, and do
04429             not check for overflow here.  */
04430        }
04431       break;
04432 
04433     case R_MIPS_LITERAL:
04434       /* Because we don't merge literal sections, we can handle this
04435         just like R_MIPS_GPREL16.  In the long run, we should merge
04436         shared literals, and then we will need to additional work
04437         here.  */
04438 
04439       /* Fall through.  */
04440 
04441     case R_MIPS16_GPREL:
04442       /* The R_MIPS16_GPREL performs the same calculation as
04443         R_MIPS_GPREL16, but stores the relocated bits in a different
04444         order.  We don't need to do anything special here; the
04445         differences are handled in mips_elf_perform_relocation.  */
04446     case R_MIPS_GPREL16:
04447       /* Only sign-extend the addend if it was extracted from the
04448         instruction.  If the addend was separate, leave it alone,
04449         otherwise we may lose significant bits.  */
04450       if (howto->partial_inplace)
04451        addend = _bfd_mips_elf_sign_extend (addend, 16);
04452       value = symbol + addend - gp;
04453       /* If the symbol was local, any earlier relocatable links will
04454         have adjusted its addend with the gp offset, so compensate
04455         for that now.  Don't do it for symbols forced local in this
04456         link, though, since they won't have had the gp offset applied
04457         to them before.  */
04458       if (was_local_p)
04459        value += gp0;
04460       overflowed_p = mips_elf_overflow_p (value, 16);
04461       break;
04462 
04463     case R_MIPS_GOT16:
04464     case R_MIPS_CALL16:
04465       /* VxWorks does not have separate local and global semantics for
04466         R_MIPS_GOT16; every relocation evaluates to "G".  */
04467       if (!htab->is_vxworks && local_p)
04468        {
04469          bfd_boolean forced;
04470 
04471          forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
04472                                             local_sections, FALSE);
04473          value = mips_elf_got16_entry (abfd, input_bfd, info, sec,
04474                                    symbol + addend, forced);
04475          if (value == MINUS_ONE)
04476            return bfd_reloc_outofrange;
04477          value
04478            = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, value);
04479          overflowed_p = mips_elf_overflow_p (value, 16);
04480          break;
04481        }
04482 
04483       /* Fall through.  */
04484 
04485     case R_MIPS_TLS_GD:
04486     case R_MIPS_TLS_GOTTPREL:
04487     case R_MIPS_TLS_LDM:
04488     case R_MIPS_GOT_DISP:
04489     got_disp:
04490       value = g;
04491       overflowed_p = mips_elf_overflow_p (value, 16);
04492       break;
04493 
04494     case R_MIPS_GPREL32:
04495       value = (addend + symbol + gp0 - gp);
04496       if (!save_addend)
04497        value &= howto->dst_mask;
04498       break;
04499 
04500     case R_MIPS_PC16:
04501     case R_MIPS_GNU_REL16_S2:
04502       value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
04503       overflowed_p = mips_elf_overflow_p (value, 18);
04504       value >>= howto->rightshift;
04505       value &= howto->dst_mask;
04506       break;
04507 
04508     case R_MIPS_GOT_HI16:
04509     case R_MIPS_CALL_HI16:
04510       /* We're allowed to handle these two relocations identically.
04511         The dynamic linker is allowed to handle the CALL relocations
04512         differently by creating a lazy evaluation stub.  */
04513       value = g;
04514       value = mips_elf_high (value);
04515       value &= howto->dst_mask;
04516       break;
04517 
04518     case R_MIPS_GOT_LO16:
04519     case R_MIPS_CALL_LO16:
04520       value = g & howto->dst_mask;
04521       break;
04522 
04523     case R_MIPS_GOT_PAGE:
04524       /* GOT_PAGE relocations that reference non-local symbols decay
04525         to GOT_DISP.  The corresponding GOT_OFST relocation decays to
04526         0.  */
04527       if (! local_p)
04528        goto got_disp;
04529       value = mips_elf_got_page (abfd, input_bfd, info, sec,
04530                              symbol + addend, NULL);
04531       if (value == MINUS_ONE)
04532        return bfd_reloc_outofrange;
04533       value = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, value);
04534       overflowed_p = mips_elf_overflow_p (value, 16);
04535       break;
04536 
04537     case R_MIPS_GOT_OFST:
04538       if (local_p)
04539        mips_elf_got_page (abfd, input_bfd, info, sec,
04540                         symbol + addend, &value);
04541       else
04542        value = addend;
04543       overflowed_p = mips_elf_overflow_p (value, 16);
04544       break;
04545 
04546     case R_MIPS_SUB:
04547       value = symbol - addend;
04548       value &= howto->dst_mask;
04549       break;
04550 
04551     case R_MIPS_HIGHER:
04552       value = mips_elf_higher (addend + symbol);
04553       value &= howto->dst_mask;
04554       break;
04555 
04556     case R_MIPS_HIGHEST:
04557       value = mips_elf_highest (addend + symbol);
04558       value &= howto->dst_mask;
04559       break;
04560 
04561     case R_MIPS_SCN_DISP:
04562       value = symbol + addend - sec->output_offset;
04563       value &= howto->dst_mask;
04564       break;
04565 
04566     case R_MIPS_JALR:
04567       /* This relocation is only a hint.  In some cases, we optimize
04568         it into a bal instruction.  But we don't try to optimize
04569         branches to the PLT; that will wind up wasting time.  */
04570       if (h != NULL && h->root.plt.offset != (bfd_vma) -1)
04571        return bfd_reloc_continue;
04572       value = symbol + addend;
04573       break;
04574 
04575     case R_MIPS_PJUMP:
04576     case R_MIPS_GNU_VTINHERIT:
04577     case R_MIPS_GNU_VTENTRY:
04578       /* We don't do anything with these at present.  */
04579       return bfd_reloc_continue;
04580 
04581     default:
04582       /* An unrecognized relocation type.  */
04583       return bfd_reloc_notsupported;
04584     }
04585 
04586   /* Store the VALUE for our caller.  */
04587   *valuep = value;
04588   return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
04589 }
04590 
04591 /* Obtain the field relocated by RELOCATION.  */
04592 
04593 static bfd_vma
04594 mips_elf_obtain_contents (reloc_howto_type *howto,
04595                        const Elf_Internal_Rela *relocation,
04596                        bfd *input_bfd, bfd_byte *contents)
04597 {
04598   bfd_vma x;
04599   bfd_byte *location = contents + relocation->r_offset;
04600 
04601   /* Obtain the bytes.  */
04602   x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
04603 
04604   return x;
04605 }
04606 
04607 /* It has been determined that the result of the RELOCATION is the
04608    VALUE.  Use HOWTO to place VALUE into the output file at the
04609    appropriate position.  The SECTION is the section to which the
04610    relocation applies.  If REQUIRE_JALX is TRUE, then the opcode used
04611    for the relocation must be either JAL or JALX, and it is
04612    unconditionally converted to JALX.
04613 
04614    Returns FALSE if anything goes wrong.  */
04615 
04616 static bfd_boolean
04617 mips_elf_perform_relocation (struct bfd_link_info *info,
04618                           reloc_howto_type *howto,
04619                           const Elf_Internal_Rela *relocation,
04620                           bfd_vma value, bfd *input_bfd,
04621                           asection *input_section, bfd_byte *contents,
04622                           bfd_boolean require_jalx)
04623 {
04624   bfd_vma x;
04625   bfd_byte *location;
04626   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
04627 
04628   /* Figure out where the relocation is occurring.  */
04629   location = contents + relocation->r_offset;
04630 
04631   _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
04632 
04633   /* Obtain the current value.  */
04634   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
04635 
04636   /* Clear the field we are setting.  */
04637   x &= ~howto->dst_mask;
04638 
04639   /* Set the field.  */
04640   x |= (value & howto->dst_mask);
04641 
04642   /* If required, turn JAL into JALX.  */
04643   if (require_jalx)
04644     {
04645       bfd_boolean ok;
04646       bfd_vma opcode = x >> 26;
04647       bfd_vma jalx_opcode;
04648 
04649       /* Check to see if the opcode is already JAL or JALX.  */
04650       if (r_type == R_MIPS16_26)
04651        {
04652          ok = ((opcode == 0x6) || (opcode == 0x7));
04653          jalx_opcode = 0x7;
04654        }
04655       else
04656        {
04657          ok = ((opcode == 0x3) || (opcode == 0x1d));
04658          jalx_opcode = 0x1d;
04659        }
04660 
04661       /* If the opcode is not JAL or JALX, there's a problem.  */
04662       if (!ok)
04663        {
04664          (*_bfd_error_handler)
04665            (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
04666             input_bfd,
04667             input_section,
04668             (unsigned long) relocation->r_offset);
04669          bfd_set_error (bfd_error_bad_value);
04670          return FALSE;
04671        }
04672 
04673       /* Make this the JALX opcode.  */
04674       x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
04675     }
04676 
04677   /* On the RM9000, bal is faster than jal, because bal uses branch
04678      prediction hardware.  If we are linking for the RM9000, and we
04679      see jal, and bal fits, use it instead.  Note that this
04680      transformation should be safe for all architectures.  */
04681   if (bfd_get_mach (input_bfd) == bfd_mach_mips9000
04682       && !info->relocatable
04683       && !require_jalx
04684       && ((r_type == R_MIPS_26 && (x >> 26) == 0x3)         /* jal addr */
04685          || (r_type == R_MIPS_JALR && x == 0x0320f809)))   /* jalr t9 */
04686     {
04687       bfd_vma addr;
04688       bfd_vma dest;
04689       bfd_signed_vma off;
04690 
04691       addr = (input_section->output_section->vma
04692              + input_section->output_offset
04693              + relocation->r_offset
04694              + 4);
04695       if (r_type == R_MIPS_26)
04696        dest = (value << 2) | ((addr >> 28) << 28);
04697       else
04698        dest = value;
04699       off = dest - addr;
04700       if (off <= 0x1ffff && off >= -0x20000)
04701        x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
04702     }
04703 
04704   /* Put the value into the output.  */
04705   bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
04706 
04707   _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
04708                             location);
04709 
04710   return TRUE;
04711 }
04712 
04713 /* Returns TRUE if SECTION is a MIPS16 stub section.  */
04714 
04715 static bfd_boolean
04716 mips16_stub_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *section)
04717 {
04718   const char *name = bfd_get_section_name (abfd, section);
04719 
04720   return FN_STUB_P (name) || CALL_STUB_P (name) || CALL_FP_STUB_P (name);
04721 }
04722 
04723 /* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
04724 
04725 static void
04726 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
04727                                    unsigned int n)
04728 {
04729   asection *s;
04730   struct mips_elf_link_hash_table *htab;
04731 
04732   htab = mips_elf_hash_table (info);
04733   s = mips_elf_rel_dyn_section (info, FALSE);
04734   BFD_ASSERT (s != NULL);
04735 
04736   if (htab->is_vxworks)
04737     s->size += n * MIPS_ELF_RELA_SIZE (abfd);
04738   else
04739     {
04740       if (s->size == 0)
04741        {
04742          /* Make room for a null element.  */
04743          s->size += MIPS_ELF_REL_SIZE (abfd);
04744          ++s->reloc_count;
04745        }
04746       s->size += n * MIPS_ELF_REL_SIZE (abfd);
04747     }
04748 }
04749 
04750 /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
04751    is the original relocation, which is now being transformed into a
04752    dynamic relocation.  The ADDENDP is adjusted if necessary; the
04753    caller should store the result in place of the original addend.  */
04754 
04755 static bfd_boolean
04756 mips_elf_create_dynamic_relocation (bfd *output_bfd,
04757                                 struct bfd_link_info *info,
04758                                 const Elf_Internal_Rela *rel,
04759                                 struct mips_elf_link_hash_entry *h,
04760                                 asection *sec, bfd_vma symbol,
04761                                 bfd_vma *addendp, asection *input_section)
04762 {
04763   Elf_Internal_Rela outrel[3];
04764   asection *sreloc;
04765   bfd *dynobj;
04766   int r_type;
04767   long indx;
04768   bfd_boolean defined_p;
04769   struct mips_elf_link_hash_table *htab;
04770 
04771   htab = mips_elf_hash_table (info);
04772   r_type = ELF_R_TYPE (output_bfd, rel->r_info);
04773   dynobj = elf_hash_table (info)->dynobj;
04774   sreloc = mips_elf_rel_dyn_section (info, FALSE);
04775   BFD_ASSERT (sreloc != NULL);
04776   BFD_ASSERT (sreloc->contents != NULL);
04777   BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
04778              < sreloc->size);
04779 
04780   outrel[0].r_offset =
04781     _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
04782   outrel[1].r_offset =
04783     _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
04784   outrel[2].r_offset =
04785     _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
04786 
04787   if (outrel[0].r_offset == MINUS_ONE)
04788     /* The relocation field has been deleted.  */
04789     return TRUE;
04790 
04791   if (outrel[0].r_offset == MINUS_TWO)
04792     {
04793       /* The relocation field has been converted into a relative value of
04794         some sort.  Functions like _bfd_elf_write_section_eh_frame expect
04795         the field to be fully relocated, so add in the symbol's value.  */
04796       *addendp += symbol;
04797       return TRUE;
04798     }
04799 
04800   /* We must now calculate the dynamic symbol table index to use
04801      in the relocation.  */
04802   if (h != NULL
04803       && (!h->root.def_regular
04804          || (info->shared && !info->symbolic && !h->root.forced_local)))
04805     {
04806       indx = h->root.dynindx;
04807       if (SGI_COMPAT (output_bfd))
04808        defined_p = h->root.def_regular;
04809       else
04810        /* ??? glibc's ld.so just adds the final GOT entry to the
04811           relocation field.  It therefore treats relocs against
04812           defined symbols in the same way as relocs against
04813           undefined symbols.  */
04814        defined_p = FALSE;
04815     }
04816   else
04817     {
04818       if (sec != NULL && bfd_is_abs_section (sec))
04819        indx = 0;
04820       else if (sec == NULL || sec->owner == NULL)
04821        {
04822          bfd_set_error (bfd_error_bad_value);
04823          return FALSE;
04824        }
04825       else
04826        {
04827          indx = elf_section_data (sec->output_section)->dynindx;
04828          if (indx == 0)
04829            {
04830              asection *osec = htab->root.text_index_section;
04831              indx = elf_section_data (osec)->dynindx;
04832            }
04833          if (indx == 0)
04834            abort ();
04835        }
04836 
04837       /* Instead of generating a relocation using the section
04838         symbol, we may as well make it a fully relative
04839         relocation.  We want to avoid generating relocations to
04840         local symbols because we used to generate them
04841         incorrectly, without adding the original symbol value,
04842         which is mandated by the ABI for section symbols.  In
04843         order to give dynamic loaders and applications time to
04844         phase out the incorrect use, we refrain from emitting
04845         section-relative relocations.  It's not like they're
04846         useful, after all.  This should be a bit more efficient
04847         as well.  */
04848       /* ??? Although this behavior is compatible with glibc's ld.so,
04849         the ABI says that relocations against STN_UNDEF should have
04850         a symbol value of 0.  Irix rld honors this, so relocations
04851         against STN_UNDEF have no effect.  */
04852       if (!SGI_COMPAT (output_bfd))
04853        indx = 0;
04854       defined_p = TRUE;
04855     }
04856 
04857   /* If the relocation was previously an absolute relocation and
04858      this symbol will not be referred to by the relocation, we must
04859      adjust it by the value we give it in the dynamic symbol table.
04860      Otherwise leave the job up to the dynamic linker.  */
04861   if (defined_p && r_type != R_MIPS_REL32)
04862     *addendp += symbol;
04863 
04864   if (htab->is_vxworks)
04865     /* VxWorks uses non-relative relocations for this.  */
04866     outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
04867   else
04868     /* The relocation is always an REL32 relocation because we don't
04869        know where the shared library will wind up at load-time.  */
04870     outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
04871                                R_MIPS_REL32);
04872 
04873   /* For strict adherence to the ABI specification, we should
04874      generate a R_MIPS_64 relocation record by itself before the
04875      _REL32/_64 record as well, such that the addend is read in as
04876      a 64-bit value (REL32 is a 32-bit relocation, after all).
04877      However, since none of the existing ELF64 MIPS dynamic
04878      loaders seems to care, we don't waste space with these
04879      artificial relocations.  If this turns out to not be true,
04880      mips_elf_allocate_dynamic_relocation() should be tweaked so
04881      as to make room for a pair of dynamic relocations per
04882      invocation if ABI_64_P, and here we should generate an
04883      additional relocation record with R_MIPS_64 by itself for a
04884      NULL symbol before this relocation record.  */
04885   outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
04886                              ABI_64_P (output_bfd)
04887                              ? R_MIPS_64
04888                              : R_MIPS_NONE);
04889   outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
04890 
04891   /* Adjust the output offset of the relocation to reference the
04892      correct location in the output file.  */
04893   outrel[0].r_offset += (input_section->output_section->vma
04894                       + input_section->output_offset);
04895   outrel[1].r_offset += (input_section->output_section->vma
04896                       + input_section->output_offset);
04897   outrel[2].r_offset += (input_section->output_section->vma
04898                       + input_section->output_offset);
04899 
04900   /* Put the relocation back out.  We have to use the special
04901      relocation outputter in the 64-bit case since the 64-bit
04902      relocation format is non-standard.  */
04903   if (ABI_64_P (output_bfd))
04904     {
04905       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
04906        (output_bfd, &outrel[0],
04907         (sreloc->contents
04908          + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
04909     }
04910   else if (htab->is_vxworks)
04911     {
04912       /* VxWorks uses RELA rather than REL dynamic relocations.  */
04913       outrel[0].r_addend = *addendp;
04914       bfd_elf32_swap_reloca_out
04915        (output_bfd, &outrel[0],
04916         (sreloc->contents
04917          + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
04918     }
04919   else
04920     bfd_elf32_swap_reloc_out
04921       (output_bfd, &outrel[0],
04922        (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
04923 
04924   /* We've now added another relocation.  */
04925   ++sreloc->reloc_count;
04926 
04927   /* Make sure the output section is writable.  The dynamic linker
04928      will be writing to it.  */
04929   elf_section_data (input_section->output_section)->this_hdr.sh_flags
04930     |= SHF_WRITE;
04931 
04932   /* On IRIX5, make an entry of compact relocation info.  */
04933   if (IRIX_COMPAT (output_bfd) == ict_irix5)
04934     {
04935       asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
04936       bfd_byte *cr;
04937 
04938       if (scpt)
04939        {
04940          Elf32_crinfo cptrel;
04941 
04942          mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
04943          cptrel.vaddr = (rel->r_offset
04944                        + input_section->output_section->vma
04945                        + input_section->output_offset);
04946          if (r_type == R_MIPS_REL32)
04947            mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
04948          else
04949            mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
04950          mips_elf_set_cr_dist2to (cptrel, 0);
04951          cptrel.konst = *addendp;
04952 
04953          cr = (scpt->contents
04954               + sizeof (Elf32_External_compact_rel));
04955          mips_elf_set_cr_relvaddr (cptrel, 0);
04956          bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
04957                                  ((Elf32_External_crinfo *) cr
04958                                   + scpt->reloc_count));
04959          ++scpt->reloc_count;
04960        }
04961     }
04962 
04963   /* If we've written this relocation for a readonly section,
04964      we need to set DF_TEXTREL again, so that we do not delete the
04965      DT_TEXTREL tag.  */
04966   if (MIPS_ELF_READONLY_SECTION (input_section))
04967     info->flags |= DF_TEXTREL;
04968 
04969   return TRUE;
04970 }
04971 
04972 /* Return the MACH for a MIPS e_flags value.  */
04973 
04974 unsigned long
04975 _bfd_elf_mips_mach (flagword flags)
04976 {
04977   switch (flags & EF_MIPS_MACH)
04978     {
04979     case E_MIPS_MACH_3900:
04980       return bfd_mach_mips3900;
04981 
04982     case E_MIPS_MACH_4010:
04983       return bfd_mach_mips4010;
04984 
04985     case E_MIPS_MACH_4100:
04986       return bfd_mach_mips4100;
04987 
04988     case E_MIPS_MACH_4111:
04989       return bfd_mach_mips4111;
04990 
04991     case E_MIPS_MACH_4120:
04992       return bfd_mach_mips4120;
04993 
04994     case E_MIPS_MACH_4650:
04995       return bfd_mach_mips4650;
04996 
04997     case E_MIPS_MACH_5400:
04998       return bfd_mach_mips5400;
04999 
05000     case E_MIPS_MACH_5500:
05001       return bfd_mach_mips5500;
05002 
05003     case E_MIPS_MACH_9000:
05004       return bfd_mach_mips9000;
05005 
05006     case E_MIPS_MACH_SB1:
05007       return bfd_mach_mips_sb1;
05008 
05009     default:
05010       switch (flags & EF_MIPS_ARCH)
05011        {
05012        default:
05013        case E_MIPS_ARCH_1:
05014          return bfd_mach_mips3000;
05015 
05016        case E_MIPS_ARCH_2:
05017          return bfd_mach_mips6000;
05018 
05019        case E_MIPS_ARCH_3:
05020          return bfd_mach_mips4000;
05021 
05022        case E_MIPS_ARCH_4:
05023          return bfd_mach_mips8000;
05024 
05025        case E_MIPS_ARCH_5:
05026          return bfd_mach_mips5;
05027 
05028        case E_MIPS_ARCH_32:
05029          return bfd_mach_mipsisa32;
05030 
05031        case E_MIPS_ARCH_64:
05032          return bfd_mach_mipsisa64;
05033 
05034        case E_MIPS_ARCH_32R2:
05035          return bfd_mach_mipsisa32r2;
05036 
05037        case E_MIPS_ARCH_64R2:
05038          return bfd_mach_mipsisa64r2;
05039        }
05040     }
05041 
05042   return 0;
05043 }
05044 
05045 /* Return printable name for ABI.  */
05046 
05047 static INLINE char *
05048 elf_mips_abi_name (bfd *abfd)
05049 {
05050   flagword flags;
05051 
05052   flags = elf_elfheader (abfd)->e_flags;
05053   switch (flags & EF_MIPS_ABI)
05054     {
05055     case 0:
05056       if (ABI_N32_P (abfd))
05057        return "N32";
05058       else if (ABI_64_P (abfd))
05059        return "64";
05060       else
05061        return "none";
05062     case E_MIPS_ABI_O32:
05063       return "O32";
05064     case E_MIPS_ABI_O64:
05065       return "O64";
05066     case E_MIPS_ABI_EABI32:
05067       return "EABI32";
05068     case E_MIPS_ABI_EABI64:
05069       return "EABI64";
05070     default:
05071       return "unknown abi";
05072     }
05073 }
05074 
05075 /* MIPS ELF uses two common sections.  One is the usual one, and the
05076    other is for small objects.  All the small objects are kept
05077    together, and then referenced via the gp pointer, which yields
05078    faster assembler code.  This is what we use for the small common
05079    section.  This approach is copied from ecoff.c.  */
05080 static asection mips_elf_scom_section;
05081 static asymbol mips_elf_scom_symbol;
05082 static asymbol *mips_elf_scom_symbol_ptr;
05083 
05084 /* MIPS ELF also uses an acommon section, which represents an
05085    allocated common symbol which may be overridden by a
05086    definition in a shared library.  */
05087 static asection mips_elf_acom_section;
05088 static asymbol mips_elf_acom_symbol;
05089 static asymbol *mips_elf_acom_symbol_ptr;
05090 
05091 /* Handle the special MIPS section numbers that a symbol may use.
05092    This is used for both the 32-bit and the 64-bit ABI.  */
05093 
05094 void
05095 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
05096 {
05097   elf_symbol_type *elfsym;
05098 
05099   elfsym = (elf_symbol_type *) asym;
05100   switch (elfsym->internal_elf_sym.st_shndx)
05101     {
05102     case SHN_MIPS_ACOMMON:
05103       /* This section is used in a dynamically linked executable file.
05104         It is an allocated common section.  The dynamic linker can
05105         either resolve these symbols to something in a shared
05106         library, or it can just leave them here.  For our purposes,
05107         we can consider these symbols to be in a new section.  */
05108       if (mips_elf_acom_section.name == NULL)
05109        {
05110          /* Initialize the acommon section.  */
05111          mips_elf_acom_section.name = ".acommon";
05112          mips_elf_acom_section.flags = SEC_ALLOC;
05113          mips_elf_acom_section.output_section = &mips_elf_acom_section;
05114          mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
05115          mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
05116          mips_elf_acom_symbol.name = ".acommon";
05117          mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
05118          mips_elf_acom_symbol.section = &mips_elf_acom_section;
05119          mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
05120        }
05121       asym->section = &mips_elf_acom_section;
05122       break;
05123 
05124     case SHN_COMMON:
05125       /* Common symbols less than the GP size are automatically
05126         treated as SHN_MIPS_SCOMMON symbols on IRIX5.  */
05127       if (asym->value > elf_gp_size (abfd)
05128          || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
05129          || IRIX_COMPAT (abfd) == ict_irix6)
05130        break;
05131       /* Fall through.  */
05132     case SHN_MIPS_SCOMMON:
05133       if (mips_elf_scom_section.name == NULL)
05134        {
05135          /* Initialize the small common section.  */
05136          mips_elf_scom_section.name = ".scommon";
05137          mips_elf_scom_section.flags = SEC_IS_COMMON;
05138          mips_elf_scom_section.output_section = &mips_elf_scom_section;
05139          mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
05140          mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
05141          mips_elf_scom_symbol.name = ".scommon";
05142          mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
05143          mips_elf_scom_symbol.section = &mips_elf_scom_section;
05144          mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
05145        }
05146       asym->section = &mips_elf_scom_section;
05147       asym->value = elfsym->internal_elf_sym.st_size;
05148       break;
05149 
05150     case SHN_MIPS_SUNDEFINED:
05151       asym->section = bfd_und_section_ptr;
05152       break;
05153 
05154     case SHN_MIPS_TEXT:
05155       {
05156        asection *section = bfd_get_section_by_name (abfd, ".text");
05157 
05158        BFD_ASSERT (SGI_COMPAT (abfd));
05159        if (section != NULL)
05160          {
05161            asym->section = section;
05162            /* MIPS_TEXT is a bit special, the address is not an offset
05163               to the base of the .text section.  So substract the section
05164               base address to make it an offset.  */
05165            asym->value -= section->vma;
05166          }
05167       }
05168       break;
05169 
05170     case SHN_MIPS_DATA:
05171       {
05172        asection *section = bfd_get_section_by_name (abfd, ".data");
05173 
05174        BFD_ASSERT (SGI_COMPAT (abfd));
05175        if (section != NULL)
05176          {
05177            asym->section = section;
05178            /* MIPS_DATA is a bit special, the address is not an offset
05179               to the base of the .data section.  So substract the section
05180               base address to make it an offset.  */
05181            asym->value -= section->vma;
05182          }
05183       }
05184       break;
05185     }
05186 }
05187 
05188 /* Implement elf_backend_eh_frame_address_size.  This differs from
05189    the default in the way it handles EABI64.
05190 
05191    EABI64 was originally specified as an LP64 ABI, and that is what
05192    -mabi=eabi normally gives on a 64-bit target.  However, gcc has
05193    historically accepted the combination of -mabi=eabi and -mlong32,
05194    and this ILP32 variation has become semi-official over time.
05195    Both forms use elf32 and have pointer-sized FDE addresses.
05196 
05197    If an EABI object was generated by GCC 4.0 or above, it will have
05198    an empty .gcc_compiled_longXX section, where XX is the size of longs
05199    in bits.  Unfortunately, ILP32 objects generated by earlier compilers
05200    have no special marking to distinguish them from LP64 objects.
05201 
05202    We don't want users of the official LP64 ABI to be punished for the
05203    existence of the ILP32 variant, but at the same time, we don't want
05204    to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
05205    We therefore take the following approach:
05206 
05207       - If ABFD contains a .gcc_compiled_longXX section, use it to
05208         determine the pointer size.
05209 
05210       - Otherwise check the type of the first relocation.  Assume that
05211         the LP64 ABI is being used if the relocation is of type R_MIPS_64.
05212 
05213       - Otherwise punt.
05214 
05215    The second check is enough to detect LP64 objects generated by pre-4.0
05216    compilers because, in the kind of output generated by those compilers,
05217    the first relocation will be associated with either a CIE personality
05218    routine or an FDE start address.  Furthermore, the compilers never
05219    used a special (non-pointer) encoding for this ABI.
05220 
05221    Checking the relocation type should also be safe because there is no
05222    reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
05223    did so.  */
05224 
05225 unsigned int
05226 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
05227 {
05228   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
05229     return 8;
05230   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
05231     {
05232       bfd_boolean long32_p, long64_p;
05233 
05234       long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
05235       long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
05236       if (long32_p && long64_p)
05237        return 0;
05238       if (long32_p)
05239        return 4;
05240       if (long64_p)
05241        return 8;
05242 
05243       if (sec->reloc_count > 0
05244          && elf_section_data (sec)->relocs != NULL
05245          && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
05246              == R_MIPS_64))
05247        return 8;
05248 
05249       return 0;
05250     }
05251   return 4;
05252 }
05253 
05254 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
05255    relocations against two unnamed section symbols to resolve to the
05256    same address.  For example, if we have code like:
05257 
05258        lw     $4,%got_disp(.data)($gp)
05259        lw     $25,%got_disp(.text)($gp)
05260        jalr   $25
05261 
05262    then the linker will resolve both relocations to .data and the program
05263    will jump there rather than to .text.
05264 
05265    We can work around this problem by giving names to local section symbols.
05266    This is also what the MIPSpro tools do.  */
05267 
05268 bfd_boolean
05269 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
05270 {
05271   return SGI_COMPAT (abfd);
05272 }
05273 
05274 /* Work over a section just before writing it out.  This routine is
05275    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
05276    sections that need the SHF_MIPS_GPREL flag by name; there has to be
05277    a better way.  */
05278 
05279 bfd_boolean
05280 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
05281 {
05282   if (hdr->sh_type == SHT_MIPS_REGINFO
05283       && hdr->sh_size > 0)
05284     {
05285       bfd_byte buf[4];
05286 
05287       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
05288       BFD_ASSERT (hdr->contents == NULL);
05289 
05290       if (bfd_seek (abfd,
05291                   hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
05292                   SEEK_SET) != 0)
05293        return FALSE;
05294       H_PUT_32 (abfd, elf_gp (abfd), buf);
05295       if (bfd_bwrite (buf, 4, abfd) != 4)
05296        return FALSE;
05297     }
05298 
05299   if (hdr->sh_type == SHT_MIPS_OPTIONS
05300       && hdr->bfd_section != NULL
05301       && mips_elf_section_data (hdr->bfd_section) != NULL
05302       && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
05303     {
05304       bfd_byte *contents, *l, *lend;
05305 
05306       /* We stored the section contents in the tdata field in the
05307         set_section_contents routine.  We save the section contents
05308         so that we don't have to read them again.
05309         At this point we know that elf_gp is set, so we can look
05310         through the section contents to see if there is an
05311         ODK_REGINFO structure.  */
05312 
05313       contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
05314       l = contents;
05315       lend = contents + hdr->sh_size;
05316       while (l + sizeof (Elf_External_Options) <= lend)
05317        {
05318          Elf_Internal_Options intopt;
05319 
05320          bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
05321                                    &intopt);
05322          if (intopt.size < sizeof (Elf_External_Options))
05323            {
05324              (*_bfd_error_handler)
05325               (_("%B: Warning: bad `%s' option size %u smaller than its header"),
05326               abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
05327              break;
05328            }
05329          if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
05330            {
05331              bfd_byte buf[8];
05332 
05333              if (bfd_seek (abfd,
05334                          (hdr->sh_offset
05335                           + (l - contents)
05336                           + sizeof (Elf_External_Options)
05337                           + (sizeof (Elf64_External_RegInfo) - 8)),
05338                           SEEK_SET) != 0)
05339               return FALSE;
05340              H_PUT_64 (abfd, elf_gp (abfd), buf);
05341              if (bfd_bwrite (buf, 8, abfd) != 8)
05342               return FALSE;
05343            }
05344          else if (intopt.kind == ODK_REGINFO)
05345            {
05346              bfd_byte buf[4];
05347 
05348              if (bfd_seek (abfd,
05349                          (hdr->sh_offset
05350                           + (l - contents)
05351                           + sizeof (Elf_External_Options)
05352                           + (sizeof (Elf32_External_RegInfo) - 4)),
05353                          SEEK_SET) != 0)
05354               return FALSE;
05355              H_PUT_32 (abfd, elf_gp (abfd), buf);
05356              if (bfd_bwrite (buf, 4, abfd) != 4)
05357               return FALSE;
05358            }
05359          l += intopt.size;
05360        }
05361     }
05362 
05363   if (hdr->bfd_section != NULL)
05364     {
05365       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
05366 
05367       if (strcmp (name, ".sdata") == 0
05368          || strcmp (name, ".lit8") == 0
05369          || strcmp (name, ".lit4") == 0)
05370        {
05371          hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
05372          hdr->sh_type = SHT_PROGBITS;
05373        }
05374       else if (strcmp (name, ".sbss") == 0)
05375        {
05376          hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
05377          hdr->sh_type = SHT_NOBITS;
05378        }
05379       else if (strcmp (name, ".srdata") == 0)
05380        {
05381          hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
05382          hdr->sh_type = SHT_PROGBITS;
05383        }
05384       else if (strcmp (name, ".compact_rel") == 0)
05385        {
05386          hdr->sh_flags = 0;
05387          hdr->sh_type = SHT_PROGBITS;
05388        }
05389       else if (strcmp (name, ".rtproc") == 0)
05390        {
05391          if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
05392            {
05393              unsigned int adjust;
05394 
05395              adjust = hdr->sh_size % hdr->sh_addralign;
05396              if (adjust != 0)
05397               hdr->sh_size += hdr->sh_addralign - adjust;
05398            }
05399        }
05400     }
05401 
05402   return TRUE;
05403 }
05404 
05405 /* Handle a MIPS specific section when reading an object file.  This
05406    is called when elfcode.h finds a section with an unknown type.
05407    This routine supports both the 32-bit and 64-bit ELF ABI.
05408 
05409    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
05410    how to.  */
05411 
05412 bfd_boolean
05413 _bfd_mips_elf_section_from_shdr (bfd *abfd,
05414                              Elf_Internal_Shdr *hdr,
05415                              const char *name,
05416                              int shindex)
05417 {
05418   flagword flags = 0;
05419 
05420   /* There ought to be a place to keep ELF backend specific flags, but
05421      at the moment there isn't one.  We just keep track of the
05422      sections by their name, instead.  Fortunately, the ABI gives
05423      suggested names for all the MIPS specific sections, so we will
05424      probably get away with this.  */
05425   switch (hdr->sh_type)
05426     {
05427     case SHT_MIPS_LIBLIST:
05428       if (strcmp (name, ".liblist") != 0)
05429        return FALSE;
05430       break;
05431     case SHT_MIPS_MSYM:
05432       if (strcmp (name, ".msym") != 0)
05433        return FALSE;
05434       break;
05435     case SHT_MIPS_CONFLICT:
05436       if (strcmp (name, ".conflict") != 0)
05437        return FALSE;
05438       break;
05439     case SHT_MIPS_GPTAB:
05440       if (! CONST_STRNEQ (name, ".gptab."))
05441        return FALSE;
05442       break;
05443     case SHT_MIPS_UCODE:
05444       if (strcmp (name, ".ucode") != 0)
05445        return FALSE;
05446       break;
05447     case SHT_MIPS_DEBUG:
05448       if (strcmp (name, ".mdebug") != 0)
05449        return FALSE;
05450       flags = SEC_DEBUGGING;
05451       break;
05452     case SHT_MIPS_REGINFO:
05453       if (strcmp (name, ".reginfo") != 0
05454          || hdr->sh_size != sizeof (Elf32_External_RegInfo))
05455        return FALSE;
05456       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
05457       break;
05458     case SHT_MIPS_IFACE:
05459       if (strcmp (name, ".MIPS.interfaces") != 0)
05460        return FALSE;
05461       break;
05462     case SHT_MIPS_CONTENT:
05463       if (! CONST_STRNEQ (name, ".MIPS.content"))
05464        return FALSE;
05465       break;
05466     case SHT_MIPS_OPTIONS:
05467       if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
05468        return FALSE;
05469       break;
05470     case SHT_MIPS_DWARF:
05471       if (! CONST_STRNEQ (name, ".debug_"))
05472        return FALSE;
05473       break;
05474     case SHT_MIPS_SYMBOL_LIB:
05475       if (strcmp (name, ".MIPS.symlib") != 0)
05476        return FALSE;
05477       break;
05478     case SHT_MIPS_EVENTS:
05479       if (! CONST_STRNEQ (name, ".MIPS.events")
05480          && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
05481        return FALSE;
05482       break;
05483     default:
05484       break;
05485     }
05486 
05487   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
05488     return FALSE;
05489 
05490   if (flags)
05491     {
05492       if (! bfd_set_section_flags (abfd, hdr->bfd_section,
05493                                (bfd_get_section_flags (abfd,
05494                                                     hdr->bfd_section)
05495                                 | flags)))
05496        return FALSE;
05497     }
05498 
05499   /* FIXME: We should record sh_info for a .gptab section.  */
05500 
05501   /* For a .reginfo section, set the gp value in the tdata information
05502      from the contents of this section.  We need the gp value while
05503      processing relocs, so we just get it now.  The .reginfo section
05504      is not used in the 64-bit MIPS ELF ABI.  */
05505   if (hdr->sh_type == SHT_MIPS_REGINFO)
05506     {
05507       Elf32_External_RegInfo ext;
05508       Elf32_RegInfo s;
05509 
05510       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
05511                                   &ext, 0, sizeof ext))
05512        return FALSE;
05513       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
05514       elf_gp (abfd) = s.ri_gp_value;
05515     }
05516 
05517   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
05518      set the gp value based on what we find.  We may see both
05519      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
05520      they should agree.  */
05521   if (hdr->sh_type == SHT_MIPS_OPTIONS)
05522     {
05523       bfd_byte *contents, *l, *lend;
05524 
05525       contents = bfd_malloc (hdr->sh_size);
05526       if (contents == NULL)
05527        return FALSE;
05528       if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
05529                                   0, hdr->sh_size))
05530        {
05531          free (contents);
05532          return FALSE;
05533        }
05534       l = contents;
05535       lend = contents + hdr->sh_size;
05536       while (l + sizeof (Elf_External_Options) <= lend)
05537        {
05538          Elf_Internal_Options intopt;
05539 
05540          bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
05541                                    &intopt);
05542          if (intopt.size < sizeof (Elf_External_Options))
05543            {
05544              (*_bfd_error_handler)
05545               (_("%B: Warning: bad `%s' option size %u smaller than its header"),
05546               abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
05547              break;
05548            }
05549          if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
05550            {
05551              Elf64_Internal_RegInfo intreg;
05552 
05553              bfd_mips_elf64_swap_reginfo_in
05554               (abfd,
05555                ((Elf64_External_RegInfo *)
05556                 (l + sizeof (Elf_External_Options))),
05557                &intreg);
05558              elf_gp (abfd) = intreg.ri_gp_value;
05559            }
05560          else if (intopt.kind == ODK_REGINFO)
05561            {
05562              Elf32_RegInfo intreg;
05563 
05564              bfd_mips_elf32_swap_reginfo_in
05565               (abfd,
05566                ((Elf32_External_RegInfo *)
05567                 (l + sizeof (Elf_External_Options))),
05568                &intreg);
05569              elf_gp (abfd) = intreg.ri_gp_value;
05570            }
05571          l += intopt.size;
05572        }
05573       free (contents);
05574     }
05575 
05576   return TRUE;
05577 }
05578 
05579 /* Set the correct type for a MIPS ELF section.  We do this by the
05580    section name, which is a hack, but ought to work.  This routine is
05581    used by both the 32-bit and the 64-bit ABI.  */
05582 
05583 bfd_boolean
05584 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
05585 {
05586   register const char *name;
05587   unsigned int sh_type;
05588 
05589   name = bfd_get_section_name (abfd, sec);
05590   sh_type = hdr->sh_type;
05591 
05592   if (strcmp (name, ".liblist") == 0)
05593     {
05594       hdr->sh_type = SHT_MIPS_LIBLIST;
05595       hdr->sh_info = sec->size / sizeof (Elf32_Lib);
05596       /* The sh_link field is set in final_write_processing.  */
05597     }
05598   else if (strcmp (name, ".conflict") == 0)
05599     hdr->sh_type = SHT_MIPS_CONFLICT;
05600   else if (CONST_STRNEQ (name, ".gptab."))
05601     {
05602       hdr->sh_type = SHT_MIPS_GPTAB;
05603       hdr->sh_entsize = sizeof (Elf32_External_gptab);
05604       /* The sh_info field is set in final_write_processing.  */
05605     }
05606   else if (strcmp (name, ".ucode") == 0)
05607     hdr->sh_type = SHT_MIPS_UCODE;
05608   else if (strcmp (name, ".mdebug") == 0)
05609     {
05610       hdr->sh_type = SHT_MIPS_DEBUG;
05611       /* In a shared object on IRIX 5.3, the .mdebug section has an
05612          entsize of 0.  FIXME: Does this matter?  */
05613       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
05614        hdr->sh_entsize = 0;
05615       else
05616        hdr->sh_entsize = 1;
05617     }
05618   else if (strcmp (name, ".reginfo") == 0)
05619     {
05620       hdr->sh_type = SHT_MIPS_REGINFO;
05621       /* In a shared object on IRIX 5.3, the .reginfo section has an
05622          entsize of 0x18.  FIXME: Does this matter?  */
05623       if (SGI_COMPAT (abfd))
05624        {
05625          if ((abfd->flags & DYNAMIC) != 0)
05626            hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
05627          else
05628            hdr->sh_entsize = 1;
05629        }
05630       else
05631        hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
05632     }
05633   else if (SGI_COMPAT (abfd)
05634           && (strcmp (name, ".hash") == 0
05635               || strcmp (name, ".dynamic") == 0
05636               || strcmp (name, ".dynstr") == 0))
05637     {
05638       if (SGI_COMPAT (abfd))
05639        hdr->sh_entsize = 0;
05640 #if 0
05641       /* This isn't how the IRIX6 linker behaves.  */
05642       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
05643 #endif
05644     }
05645   else if (strcmp (name, ".got") == 0
05646           || strcmp (name, ".srdata") == 0
05647           || strcmp (name, ".sdata") == 0
05648           || strcmp (name, ".sbss") == 0
05649           || strcmp (name, ".lit4") == 0
05650           || strcmp (name, ".lit8") == 0)
05651     hdr->sh_flags |= SHF_MIPS_GPREL;
05652   else if (strcmp (name, ".MIPS.interfaces") == 0)
05653     {
05654       hdr->sh_type = SHT_MIPS_IFACE;
05655       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
05656     }
05657   else if (CONST_STRNEQ (name, ".MIPS.content"))
05658     {
05659       hdr->sh_type = SHT_MIPS_CONTENT;
05660       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
05661       /* The sh_info field is set in final_write_processing.  */
05662     }
05663   else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
05664     {
05665       hdr->sh_type = SHT_MIPS_OPTIONS;
05666       hdr->sh_entsize = 1;
05667       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
05668     }
05669   else if (CONST_STRNEQ (name, ".debug_"))
05670     hdr->sh_type = SHT_MIPS_DWARF;
05671   else if (strcmp (name, ".MIPS.symlib") == 0)
05672     {
05673       hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
05674       /* The sh_link and sh_info fields are set in
05675          final_write_processing.  */
05676     }
05677   else if (CONST_STRNEQ (name, ".MIPS.events")
05678           || CONST_STRNEQ (name, ".MIPS.post_rel"))
05679     {
05680       hdr->sh_type = SHT_MIPS_EVENTS;
05681       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
05682       /* The sh_link field is set in final_write_processing.  */
05683     }
05684   else if (strcmp (name, ".msym") == 0)
05685     {
05686       hdr->sh_type = SHT_MIPS_MSYM;
05687       hdr->sh_flags |= SHF_ALLOC;
05688       hdr->sh_entsize = 8;
05689     }
05690 
05691   /* In the unlikely event a special section is empty it has to lose its
05692      special meaning.  This may happen e.g. when using `strip' with the
05693      "--only-keep-debug" option.  */
05694   if (sec->size > 0 && !(sec->flags & SEC_HAS_CONTENTS))
05695     hdr->sh_type = sh_type;
05696 
05697   /* The generic elf_fake_sections will set up REL_HDR using the default
05698    kind of relocations.  We used to set up a second header for the
05699    non-default kind of relocations here, but only NewABI would use
05700    these, and the IRIX ld doesn't like resulting empty RELA sections.
05701    Thus we create those header only on demand now.  */
05702 
05703   return TRUE;
05704 }
05705 
05706 /* Given a BFD section, try to locate the corresponding ELF section
05707    index.  This is used by both the 32-bit and the 64-bit ABI.
05708    Actually, it's not clear to me that the 64-bit ABI supports these,
05709    but for non-PIC objects we will certainly want support for at least
05710    the .scommon section.  */
05711 
05712 bfd_boolean
05713 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
05714                                    asection *sec, int *retval)
05715 {
05716   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
05717     {
05718       *retval = SHN_MIPS_SCOMMON;
05719       return TRUE;
05720     }
05721   if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
05722     {
05723       *retval = SHN_MIPS_ACOMMON;
05724       return TRUE;
05725     }
05726   return FALSE;
05727 }
05728 
05729 /* Hook called by the linker routine which adds symbols from an object
05730    file.  We must handle the special MIPS section numbers here.  */
05731 
05732 bfd_boolean
05733 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
05734                             Elf_Internal_Sym *sym, const char **namep,
05735                             flagword *flagsp ATTRIBUTE_UNUSED,
05736                             asection **secp, bfd_vma *valp)
05737 {
05738   if (SGI_COMPAT (abfd)
05739       && (abfd->flags & DYNAMIC) != 0
05740       && strcmp (*namep, "_rld_new_interface") == 0)
05741     {
05742       /* Skip IRIX5 rld entry name.  */
05743       *namep = NULL;
05744       return TRUE;
05745     }
05746 
05747   /* Shared objects may have a dynamic symbol '_gp_disp' defined as
05748      a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
05749      by setting a DT_NEEDED for the shared object.  Since _gp_disp is
05750      a magic symbol resolved by the linker, we ignore this bogus definition
05751      of _gp_disp.  New ABI objects do not suffer from this problem so this
05752      is not done for them. */
05753   if (!NEWABI_P(abfd)
05754       && (sym->st_shndx == SHN_ABS)
05755       && (strcmp (*namep, "_gp_disp") == 0))
05756     {
05757       *namep = NULL;
05758       return TRUE;
05759     }
05760 
05761   switch (sym->st_shndx)
05762     {
05763     case SHN_COMMON:
05764       /* Common symbols less than the GP size are automatically
05765         treated as SHN_MIPS_SCOMMON symbols.  */
05766       if (sym->st_size > elf_gp_size (abfd)
05767          || ELF_ST_TYPE (sym->st_info) == STT_TLS
05768          || IRIX_COMPAT (abfd) == ict_irix6)
05769        break;
05770       /* Fall through.  */
05771     case SHN_MIPS_SCOMMON:
05772       *secp = bfd_make_section_old_way (abfd, ".scommon");
05773       (*secp)->flags |= SEC_IS_COMMON;
05774       *valp = sym->st_size;
05775       break;
05776 
05777     case SHN_MIPS_TEXT:
05778       /* This section is used in a shared object.  */
05779       if (elf_tdata (abfd)->elf_text_section == NULL)
05780        {
05781          asymbol *elf_text_symbol;
05782          asection *elf_text_section;
05783          bfd_size_type amt = sizeof (asection);
05784 
05785          elf_text_section = bfd_zalloc (abfd, amt);
05786          if (elf_text_section == NULL)
05787            return FALSE;
05788 
05789          amt = sizeof (asymbol);
05790          elf_text_symbol = bfd_zalloc (abfd, amt);
05791          if (elf_text_symbol == NULL)
05792            return FALSE;
05793 
05794          /* Initialize the section.  */
05795 
05796          elf_tdata (abfd)->elf_text_section = elf_text_section;
05797          elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
05798 
05799          elf_text_section->symbol = elf_text_symbol;
05800          elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
05801 
05802          elf_text_section->name = ".text";
05803          elf_text_section->flags = SEC_NO_FLAGS;
05804          elf_text_section->output_section = NULL;
05805          elf_text_section->owner = abfd;
05806          elf_text_symbol->name = ".text";
05807          elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
05808          elf_text_symbol->section = elf_text_section;
05809        }
05810       /* This code used to do *secp = bfd_und_section_ptr if
05811          info->shared.  I don't know why, and that doesn't make sense,
05812          so I took it out.  */
05813       *secp = elf_tdata (abfd)->elf_text_section;
05814       break;
05815 
05816     case SHN_MIPS_ACOMMON:
05817       /* Fall through. XXX Can we treat this as allocated data?  */
05818     case SHN_MIPS_DATA:
05819       /* This section is used in a shared object.  */
05820       if (elf_tdata (abfd)->elf_data_section == NULL)
05821        {
05822          asymbol *elf_data_symbol;
05823          asection *elf_data_section;
05824          bfd_size_type amt = sizeof (asection);
05825 
05826          elf_data_section = bfd_zalloc (abfd, amt);
05827          if (elf_data_section == NULL)
05828            return FALSE;
05829 
05830          amt = sizeof (asymbol);
05831          elf_data_symbol = bfd_zalloc (abfd, amt);
05832          if (elf_data_symbol == NULL)
05833            return FALSE;
05834 
05835          /* Initialize the section.  */
05836 
05837          elf_tdata (abfd)->elf_data_section = elf_data_section;
05838          elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
05839 
05840          elf_data_section->symbol = elf_data_symbol;
05841          elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
05842 
05843          elf_data_section->name = ".data";
05844          elf_data_section->flags = SEC_NO_FLAGS;
05845          elf_data_section->output_section = NULL;
05846          elf_data_section->owner = abfd;
05847          elf_data_symbol->name = ".data";
05848          elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
05849          elf_data_symbol->section = elf_data_section;
05850        }
05851       /* This code used to do *secp = bfd_und_section_ptr if
05852          info->shared.  I don't know why, and that doesn't make sense,
05853          so I took it out.  */
05854       *secp = elf_tdata (abfd)->elf_data_section;
05855       break;
05856 
05857     case SHN_MIPS_SUNDEFINED:
05858       *secp = bfd_und_section_ptr;
05859       break;
05860     }
05861 
05862   if (SGI_COMPAT (abfd)
05863       && ! info->shared
05864       && info->hash->creator == abfd->xvec
05865       && strcmp (*namep, "__rld_obj_head") == 0)
05866     {
05867       struct elf_link_hash_entry *h;
05868       struct bfd_link_hash_entry *bh;
05869 
05870       /* Mark __rld_obj_head as dynamic.  */
05871       bh = NULL;
05872       if (! (_bfd_generic_link_add_one_symbol
05873             (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
05874              get_elf_backend_data (abfd)->collect, &bh)))
05875        return FALSE;
05876 
05877       h = (struct elf_link_hash_entry *) bh;
05878       h->non_elf = 0;
05879       h->def_regular = 1;
05880       h->type = STT_OBJECT;
05881 
05882       if (! bfd_elf_link_record_dynamic_symbol (info, h))
05883        return FALSE;
05884 
05885       mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
05886     }
05887 
05888   /* If this is a mips16 text symbol, add 1 to the value to make it
05889      odd.  This will cause something like .word SYM to come up with
05890      the right value when it is loaded into the PC.  */
05891   if (sym->st_other == STO_MIPS16)
05892     ++*valp;
05893 
05894   return TRUE;
05895 }
05896 
05897 /* This hook function is called before the linker writes out a global
05898    symbol.  We mark symbols as small common if appropriate.  This is
05899    also where we undo the increment of the value for a mips16 symbol.  */
05900 
05901 bfd_boolean
05902 _bfd_mips_elf_link_output_symbol_hook
05903   (struct bfd_link_info *info ATTRIBUTE_UNUSED,
05904    const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
05905    asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
05906 {
05907   /* If we see a common symbol, which implies a relocatable link, then
05908      if a symbol was small common in an input file, mark it as small
05909      common in the output file.  */
05910   if (sym->st_shndx == SHN_COMMON
05911       && strcmp (input_sec->name, ".scommon") == 0)
05912     sym->st_shndx = SHN_MIPS_SCOMMON;
05913 
05914   if (sym->st_other == STO_MIPS16)
05915     sym->st_value &= ~1;
05916 
05917   return TRUE;
05918 }
05919 
05920 /* Functions for the dynamic linker.  */
05921 
05922 /* Create dynamic sections when linking against a dynamic object.  */
05923 
05924 bfd_boolean
05925 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
05926 {
05927   struct elf_link_hash_entry *h;
05928   struct bfd_link_hash_entry *bh;
05929   flagword flags;
05930   register asection *s;
05931   const char * const *namep;
05932   struct mips_elf_link_hash_table *htab;
05933 
05934   htab = mips_elf_hash_table (info);
05935   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
05936           | SEC_LINKER_CREATED | SEC_READONLY);
05937 
05938   /* The psABI requires a read-only .dynamic section, but the VxWorks
05939      EABI doesn't.  */
05940   if (!htab->is_vxworks)
05941     {
05942       s = bfd_get_section_by_name (abfd, ".dynamic");
05943       if (s != NULL)
05944        {
05945          if (! bfd_set_section_flags (abfd, s, flags))
05946            return FALSE;
05947        }
05948     }
05949 
05950   /* We need to create .got section.  */
05951   if (! mips_elf_create_got_section (abfd, info, FALSE))
05952     return FALSE;
05953 
05954   if (! mips_elf_rel_dyn_section (info, TRUE))
05955     return FALSE;
05956 
05957   /* Create .stub section.  */
05958   if (bfd_get_section_by_name (abfd,
05959                             MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
05960     {
05961       s = bfd_make_section_with_flags (abfd,
05962                                    MIPS_ELF_STUB_SECTION_NAME (abfd),
05963                                    flags | SEC_CODE);
05964       if (s == NULL
05965          || ! bfd_set_section_alignment (abfd, s,
05966                                      MIPS_ELF_LOG_FILE_ALIGN (abfd)))
05967        return FALSE;
05968     }
05969 
05970   if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
05971       && !info->shared
05972       && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
05973     {
05974       s = bfd_make_section_with_flags (abfd, ".rld_map",
05975                                    flags &~ (flagword) SEC_READONLY);
05976       if (s == NULL
05977          || ! bfd_set_section_alignment (abfd, s,
05978                                      MIPS_ELF_LOG_FILE_ALIGN (abfd)))
05979        return FALSE;
05980     }
05981 
05982   /* On IRIX5, we adjust add some additional symbols and change the
05983      alignments of several sections.  There is no ABI documentation
05984      indicating that this is necessary on IRIX6, nor any evidence that
05985      the linker takes such action.  */
05986   if (IRIX_COMPAT (abfd) == ict_irix5)
05987     {
05988       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
05989        {
05990          bh = NULL;
05991          if (! (_bfd_generic_link_add_one_symbol
05992                (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
05993                 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
05994            return FALSE;
05995 
05996          h = (struct elf_link_hash_entry *) bh;
05997          h->non_elf = 0;
05998          h->def_regular = 1;
05999          h->type = STT_SECTION;
06000 
06001          if (! bfd_elf_link_record_dynamic_symbol (info, h))
06002            return FALSE;
06003        }
06004 
06005       /* We need to create a .compact_rel section.  */
06006       if (SGI_COMPAT (abfd))
06007        {
06008          if (!mips_elf_create_compact_rel_section (abfd, info))
06009            return FALSE;
06010        }
06011 
06012       /* Change alignments of some sections.  */
06013       s = bfd_get_section_by_name (abfd, ".hash");
06014       if (s != NULL)
06015        bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
06016       s = bfd_get_section_by_name (abfd, ".dynsym");
06017       if (s != NULL)
06018        bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
06019       s = bfd_get_section_by_name (abfd, ".dynstr");
06020       if (s != NULL)
06021        bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
06022       s = bfd_get_section_by_name (abfd, ".reginfo");
06023       if (s != NULL)
06024        bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
06025       s = bfd_get_section_by_name (abfd, ".dynamic");
06026       if (s != NULL)
06027        bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
06028     }
06029 
06030   if (!info->shared)
06031     {
06032       const char *name;
06033 
06034       name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
06035       bh = NULL;
06036       if (!(_bfd_generic_link_add_one_symbol
06037            (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
06038             NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
06039        return FALSE;
06040 
06041       h = (struct elf_link_hash_entry *) bh;
06042       h->non_elf = 0;
06043       h->def_regular = 1;
06044       h->type = STT_SECTION;
06045 
06046       if (! bfd_elf_link_record_dynamic_symbol (info, h))
06047        return FALSE;
06048 
06049       if (! mips_elf_hash_table (info)->use_rld_obj_head)
06050        {
06051          /* __rld_map is a four byte word located in the .data section
06052             and is filled in by the rtld to contain a pointer to
06053             the _r_debug structure. Its symbol value will be set in
06054             _bfd_mips_elf_finish_dynamic_symbol.  */
06055          s = bfd_get_section_by_name (abfd, ".rld_map");
06056          BFD_ASSERT (s != NULL);
06057 
06058          name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
06059          bh = NULL;
06060          if (!(_bfd_generic_link_add_one_symbol
06061               (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
06062                get_elf_backend_data (abfd)->collect, &bh)))
06063            return FALSE;
06064 
06065          h = (struct elf_link_hash_entry *) bh;
06066          h->non_elf = 0;
06067          h->def_regular = 1;
06068          h->type = STT_OBJECT;
06069 
06070          if (! bfd_elf_link_record_dynamic_symbol (info, h))
06071            return FALSE;
06072        }
06073     }
06074 
06075   if (htab->is_vxworks)
06076     {
06077       /* Create the .plt, .rela.plt, .dynbss and .rela.bss sections.
06078         Also create the _PROCEDURE_LINKAGE_TABLE symbol.  */
06079       if (!_bfd_elf_create_dynamic_sections (abfd, info))
06080        return FALSE;
06081 
06082       /* Cache the sections created above.  */
06083       htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
06084       htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
06085       htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
06086       htab->splt = bfd_get_section_by_name (abfd, ".plt");
06087       if (!htab->sdynbss
06088          || (!htab->srelbss && !info->shared)
06089          || !htab->srelplt
06090          || !htab->splt)
06091        abort ();
06092 
06093       /* Do the usual VxWorks handling.  */
06094       if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
06095        return FALSE;
06096 
06097       /* Work out the PLT sizes.  */
06098       if (info->shared)
06099        {
06100          htab->plt_header_size
06101            = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
06102          htab->plt_entry_size
06103            = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
06104        }
06105       else
06106        {
06107          htab->plt_header_size
06108            = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
06109          htab->plt_entry_size
06110            = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
06111        }
06112     }
06113 
06114   return TRUE;
06115 }
06116 
06117 /* Look through the relocs for a section during the first phase, and
06118    allocate space in the global offset table.  */
06119 
06120 bfd_boolean
06121 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
06122                          asection *sec, const Elf_Internal_Rela *relocs)
06123 {
06124   const char *name;
06125   bfd *dynobj;
06126   Elf_Internal_Shdr *symtab_hdr;
06127   struct elf_link_hash_entry **sym_hashes;
06128   struct mips_got_info *g;
06129   size_t extsymoff;
06130   const Elf_Internal_Rela *rel;
06131   const Elf_Internal_Rela *rel_end;
06132   asection *sgot;
06133   asection *sreloc;
06134   const struct elf_backend_data *bed;
06135   struct mips_elf_link_hash_table *htab;
06136 
06137   if (info->relocatable)
06138     return TRUE;
06139 
06140   htab = mips_elf_hash_table (info);
06141   dynobj = elf_hash_table (info)->dynobj;
06142   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
06143   sym_hashes = elf_sym_hashes (abfd);
06144   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
06145 
06146   /* Check for the mips16 stub sections.  */
06147 
06148   name = bfd_get_section_name (abfd, sec);
06149   if (FN_STUB_P (name))
06150     {
06151       unsigned long r_symndx;
06152 
06153       /* Look at the relocation information to figure out which symbol
06154          this is for.  */
06155 
06156       r_symndx = ELF_R_SYM (abfd, relocs->r_info);
06157 
06158       if (r_symndx < extsymoff
06159          || sym_hashes[r_symndx - extsymoff] == NULL)
06160        {
06161          asection *o;
06162 
06163          /* This stub is for a local symbol.  This stub will only be
06164              needed if there is some relocation in this BFD, other
06165              than a 16 bit function call, which refers to this symbol.  */
06166          for (o = abfd->sections; o != NULL; o = o->next)
06167            {
06168              Elf_Internal_Rela *sec_relocs;
06169              const Elf_Internal_Rela *r, *rend;
06170 
06171              /* We can ignore stub sections when looking for relocs.  */
06172              if ((o->flags & SEC_RELOC) == 0
06173                 || o->reloc_count == 0
06174                 || mips16_stub_section_p (abfd, o))
06175               continue;
06176 
06177              sec_relocs
06178               = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
06179                                         info->keep_memory);
06180              if (sec_relocs == NULL)
06181               return FALSE;
06182 
06183              rend = sec_relocs + o->reloc_count;
06184              for (r = sec_relocs; r < rend; r++)
06185               if (ELF_R_SYM (abfd, r->r_info) == r_symndx
06186                   && ELF_R_TYPE (abfd, r->r_info) != R_MIPS16_26)
06187                 break;
06188 
06189              if (elf_section_data (o)->relocs != sec_relocs)
06190               free (sec_relocs);
06191 
06192              if (r < rend)
06193               break;
06194            }
06195 
06196          if (o == NULL)
06197            {
06198              /* There is no non-call reloc for this stub, so we do
06199                  not need it.  Since this function is called before
06200                  the linker maps input sections to output sections, we
06201                  can easily discard it by setting the SEC_EXCLUDE
06202                  flag.  */
06203              sec->flags |= SEC_EXCLUDE;
06204              return TRUE;
06205            }
06206 
06207          /* Record this stub in an array of local symbol stubs for
06208              this BFD.  */
06209          if (elf_tdata (abfd)->local_stubs == NULL)
06210            {
06211              unsigned long symcount;
06212              asection **n;
06213              bfd_size_type amt;
06214 
06215              if (elf_bad_symtab (abfd))
06216               symcount = NUM_SHDR_ENTRIES (symtab_hdr);
06217              else
06218               symcount = symtab_hdr->sh_info;
06219              amt = symcount * sizeof (asection *);
06220              n = bfd_zalloc (abfd, amt);
06221              if (n == NULL)
06222               return FALSE;
06223              elf_tdata (abfd)->local_stubs = n;
06224            }
06225 
06226          sec->flags |= SEC_KEEP;
06227          elf_tdata (abfd)->local_stubs[r_symndx] = sec;
06228 
06229          /* We don't need to set mips16_stubs_seen in this case.
06230              That flag is used to see whether we need to look through
06231              the global symbol table for stubs.  We don't need to set
06232              it here, because we just have a local stub.  */
06233        }
06234       else
06235        {
06236          struct mips_elf_link_hash_entry *h;
06237 
06238          h = ((struct mips_elf_link_hash_entry *)
06239               sym_hashes[r_symndx - extsymoff]);
06240 
06241          while (h->root.root.type == bfd_link_hash_indirect
06242                || h->root.root.type == bfd_link_hash_warning)
06243            h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
06244 
06245          /* H is the symbol this stub is for.  */
06246 
06247          /* If we already have an appropriate stub for this function, we
06248             don't need another one, so we can discard this one.  Since
06249             this function is called before the linker maps input sections
06250             to output sections, we can easily discard it by setting the
06251             SEC_EXCLUDE flag.  */
06252          if (h->fn_stub != NULL)
06253            {
06254              sec->flags |= SEC_EXCLUDE;
06255              return TRUE;
06256            }
06257 
06258          sec->flags |= SEC_KEEP;
06259          h->fn_stub = sec;
06260          mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
06261        }
06262     }
06263   else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
06264     {
06265       unsigned long r_symndx;
06266       struct mips_elf_link_hash_entry *h;
06267       asection **loc;
06268 
06269       /* Look at the relocation information to figure out which symbol
06270          this is for.  */
06271 
06272       r_symndx = ELF_R_SYM (abfd, relocs->r_info);
06273 
06274       if (r_symndx < extsymoff
06275          || sym_hashes[r_symndx - extsymoff] == NULL)
06276        {
06277          asection *o;
06278 
06279          /* This stub is for a local symbol.  This stub will only be
06280              needed if there is some relocation (R_MIPS16_26) in this BFD
06281              that refers to this symbol.  */
06282          for (o = abfd->sections; o != NULL; o = o->next)
06283            {
06284              Elf_Internal_Rela *sec_relocs;
06285              const Elf_Internal_Rela *r, *rend;
06286 
06287              /* We can ignore stub sections when looking for relocs.  */
06288              if ((o->flags & SEC_RELOC) == 0
06289                 || o->reloc_count == 0
06290                 || mips16_stub_section_p (abfd, o))
06291               continue;
06292 
06293              sec_relocs
06294               = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
06295                                         info->keep_memory);
06296              if (sec_relocs == NULL)
06297               return FALSE;
06298 
06299              rend = sec_relocs + o->reloc_count;
06300              for (r = sec_relocs; r < rend; r++)
06301               if (ELF_R_SYM (abfd, r->r_info) == r_symndx
06302                   && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
06303                   break;
06304 
06305              if (elf_section_data (o)->relocs != sec_relocs)
06306               free (sec_relocs);
06307 
06308              if (r < rend)
06309               break;
06310            }
06311 
06312          if (o == NULL)
06313            {
06314              /* There is no non-call reloc for this stub, so we do
06315                  not need it.  Since this function is called before
06316                  the linker maps input sections to output sections, we
06317                  can easily discard it by setting the SEC_EXCLUDE
06318                  flag.  */
06319              sec->flags |= SEC_EXCLUDE;
06320              return TRUE;
06321            }
06322 
06323          /* Record this stub in an array of local symbol call_stubs for
06324              this BFD.  */
06325          if (elf_tdata (abfd)->local_call_stubs == NULL)
06326            {
06327              unsigned long symcount;
06328              asection **n;
06329              bfd_size_type amt;
06330 
06331              if (elf_bad_symtab (abfd))
06332               symcount = NUM_SHDR_ENTRIES (symtab_hdr);
06333              else
06334               symcount = symtab_hdr->sh_info;
06335              amt = symcount * sizeof (asection *);
06336              n = bfd_zalloc (abfd, amt);
06337              if (n == NULL)
06338               return FALSE;
06339              elf_tdata (abfd)->local_call_stubs = n;
06340            }
06341 
06342          sec->flags |= SEC_KEEP;
06343          elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
06344 
06345          /* We don't need to set mips16_stubs_seen in this case.
06346              That flag is used to see whether we need to look through
06347              the global symbol table for stubs.  We don't need to set
06348              it here, because we just have a local stub.  */
06349        }
06350       else
06351        {
06352          h = ((struct mips_elf_link_hash_entry *)
06353               sym_hashes[r_symndx - extsymoff]);
06354          
06355          /* H is the symbol this stub is for.  */
06356          
06357          if (CALL_FP_STUB_P (name))
06358            loc = &h->call_fp_stub;
06359          else
06360            loc = &h->call_stub;
06361          
06362          /* If we already have an appropriate stub for this function, we
06363             don't need another one, so we can discard this one.  Since
06364             this function is called before the linker maps input sections
06365             to output sections, we can easily discard it by setting the
06366             SEC_EXCLUDE flag.  */
06367          if (*loc != NULL)
06368            {
06369              sec->flags |= SEC_EXCLUDE;
06370              return TRUE;
06371            }
06372 
06373          sec->flags |= SEC_KEEP;
06374          *loc = sec;
06375          mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
06376        }
06377     }
06378 
06379   if (dynobj == NULL)
06380     {
06381       sgot = NULL;
06382       g = NULL;
06383     }
06384   else
06385     {
06386       sgot = mips_elf_got_section (dynobj, FALSE);
06387       if (sgot == NULL)
06388        g = NULL;
06389       else
06390        {
06391          BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
06392          g = mips_elf_section_data (sgot)->u.got_info;
06393          BFD_ASSERT (g != NULL);
06394        }
06395     }
06396 
06397   sreloc = NULL;
06398   bed = get_elf_backend_data (abfd);
06399   rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
06400   for (rel = relocs; rel < rel_end; ++rel)
06401     {
06402       unsigned long r_symndx;
06403       unsigned int r_type;
06404       struct elf_link_hash_entry *h;
06405 
06406       r_symndx = ELF_R_SYM (abfd, rel->r_info);
06407       r_type = ELF_R_TYPE (abfd, rel->r_info);
06408 
06409       if (r_symndx < extsymoff)
06410        h = NULL;
06411       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
06412        {
06413          (*_bfd_error_handler)
06414            (_("%B: Malformed reloc detected for section %s"),
06415             abfd, name);
06416          bfd_set_error (bfd_error_bad_value);
06417          return FALSE;
06418        }
06419       else
06420        {
06421          h = sym_hashes[r_symndx - extsymoff];
06422 
06423          /* This may be an indirect symbol created because of a version.  */
06424          if (h != NULL)
06425            {
06426              while (h->root.type == bfd_link_hash_indirect)
06427               h = (struct elf_link_hash_entry *) h->root.u.i.link;
06428            }
06429        }
06430 
06431       /* Some relocs require a global offset table.  */
06432       if (dynobj == NULL || sgot == NULL)
06433        {
06434          switch (r_type)
06435            {
06436            case R_MIPS_GOT16:
06437            case R_MIPS_CALL16:
06438            case R_MIPS_CALL_HI16:
06439            case R_MIPS_CALL_LO16:
06440            case R_MIPS_GOT_HI16:
06441            case R_MIPS_GOT_LO16:
06442            case R_MIPS_GOT_PAGE:
06443            case R_MIPS_GOT_OFST:
06444            case R_MIPS_GOT_DISP:
06445            case R_MIPS_TLS_GOTTPREL:
06446            case R_MIPS_TLS_GD:
06447            case R_MIPS_TLS_LDM:
06448              if (dynobj == NULL)
06449               elf_hash_table (info)->dynobj = dynobj = abfd;
06450              if (! mips_elf_create_got_section (dynobj, info, FALSE))
06451               return FALSE;
06452              g = mips_elf_got_info (dynobj, &sgot);
06453              if (htab->is_vxworks && !info->shared)
06454               {
06455                 (*_bfd_error_handler)
06456                   (_("%B: GOT reloc at 0x%lx not expected in executables"),
06457                    abfd, (unsigned long) rel->r_offset);
06458                 bfd_set_error (bfd_error_bad_value);
06459                 return FALSE;
06460               }
06461              break;
06462 
06463            case R_MIPS_32:
06464            case R_MIPS_REL32:
06465            case R_MIPS_64:
06466              /* In VxWorks executables, references to external symbols
06467                are handled using copy relocs or PLT stubs, so there's
06468                no need to add a dynamic relocation here.  */
06469              if (dynobj == NULL
06470                 && (info->shared || (h != NULL && !htab->is_vxworks))
06471                 && (sec->flags & SEC_ALLOC) != 0)
06472               elf_hash_table (info)->dynobj = dynobj = abfd;
06473              break;
06474 
06475            default:
06476              break;
06477            }
06478        }
06479 
06480       if (h)
06481        {
06482          ((struct mips_elf_link_hash_entry *) h)->is_relocation_target = TRUE;
06483 
06484          /* Relocations against the special VxWorks __GOTT_BASE__ and
06485             __GOTT_INDEX__ symbols must be left to the loader.  Allocate
06486             room for them in .rela.dyn.  */
06487          if (is_gott_symbol (info, h))
06488            {
06489              if (sreloc == NULL)
06490               {
06491                 sreloc = mips_elf_rel_dyn_section (info, TRUE);
06492                 if (sreloc == NULL)
06493                   return FALSE;
06494               }
06495              mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
06496            }
06497        }
06498       else if (r_type == R_MIPS_CALL_LO16
06499               || r_type == R_MIPS_GOT_LO16
06500               || r_type == R_MIPS_GOT_DISP
06501               || (r_type == R_MIPS_GOT16 && htab->is_vxworks))
06502        {
06503          /* We may need a local GOT entry for this relocation.  We
06504             don't count R_MIPS_GOT_PAGE because we can estimate the
06505             maximum number of pages needed by looking at the size of
06506             the segment.  Similar comments apply to R_MIPS_GOT16 and
06507             R_MIPS_CALL16, except on VxWorks, where GOT relocations
06508             always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
06509             R_MIPS_CALL_HI16 because these are always followed by an
06510             R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
06511          if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
06512                                             rel->r_addend, g, 0))
06513            return FALSE;
06514        }
06515 
06516       switch (r_type)
06517        {
06518        case R_MIPS_CALL16:
06519          if (h == NULL)
06520            {
06521              (*_bfd_error_handler)
06522               (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
06523                abfd, (unsigned long) rel->r_offset);
06524              bfd_set_error (bfd_error_bad_value);
06525              return FALSE;
06526            }
06527          /* Fall through.  */
06528 
06529        case R_MIPS_CALL_HI16:
06530        case R_MIPS_CALL_LO16:
06531          if (h != NULL)
06532            {
06533              /* VxWorks call relocations point the function's .got.plt
06534                entry, which will be allocated by adjust_dynamic_symbol.
06535                Otherwise, this symbol requires a global GOT entry.  */
06536              if (!htab->is_vxworks
06537                 && !mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
06538               return FALSE;
06539 
06540              /* We need a stub, not a plt entry for the undefined
06541                function.  But we record it as if it needs plt.  See
06542                _bfd_elf_adjust_dynamic_symbol.  */
06543              h->needs_plt = 1;
06544              h->type = STT_FUNC;
06545            }
06546          break;
06547 
06548        case R_MIPS_GOT_PAGE:
06549          /* If this is a global, overridable symbol, GOT_PAGE will
06550             decay to GOT_DISP, so we'll need a GOT entry for it.  */
06551          if (h == NULL)
06552            break;
06553          else
06554            {
06555              struct mips_elf_link_hash_entry *hmips =
06556               (struct mips_elf_link_hash_entry *) h;
06557 
06558              while (hmips->root.root.type == bfd_link_hash_indirect
06559                    || hmips->root.root.type == bfd_link_hash_warning)
06560               hmips = (struct mips_elf_link_hash_entry *)
06561                 hmips->root.root.u.i.link;
06562 
06563              if (hmips->root.def_regular
06564                 && ! (info->shared && ! info->symbolic
06565                      && ! hmips->root.forced_local))
06566               break;
06567            }
06568          /* Fall through.  */
06569 
06570        case R_MIPS_GOT16:
06571        case R_MIPS_GOT_HI16:
06572        case R_MIPS_GOT_LO16:
06573        case R_MIPS_GOT_DISP:
06574          if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
06575            return FALSE;
06576          break;
06577 
06578        case R_MIPS_TLS_GOTTPREL:
06579          if (info->shared)
06580            info->flags |= DF_STATIC_TLS;
06581          /* Fall through */
06582 
06583        case R_MIPS_TLS_LDM:
06584          if (r_type == R_MIPS_TLS_LDM)
06585            {
06586              r_symndx = 0;
06587              h = NULL;
06588            }
06589          /* Fall through */
06590 
06591        case R_MIPS_TLS_GD:
06592          /* This symbol requires a global offset table entry, or two
06593             for TLS GD relocations.  */
06594          {
06595            unsigned char flag = (r_type == R_MIPS_TLS_GD
06596                               ? GOT_TLS_GD
06597                               : r_type == R_MIPS_TLS_LDM
06598                               ? GOT_TLS_LDM
06599                               : GOT_TLS_IE);
06600            if (h != NULL)
06601              {
06602               struct mips_elf_link_hash_entry *hmips =
06603                 (struct mips_elf_link_hash_entry *) h;
06604               hmips->tls_type |= flag;
06605 
06606               if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, flag))
06607                 return FALSE;
06608              }
06609            else
06610              {
06611               BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
06612 
06613               if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
06614                                                  rel->r_addend, g, flag))
06615                 return FALSE;
06616              }
06617          }
06618          break;
06619 
06620        case R_MIPS_32:
06621        case R_MIPS_REL32:
06622        case R_MIPS_64:
06623          /* In VxWorks executables, references to external symbols
06624             are handled using copy relocs or PLT stubs, so there's
06625             no need to add a .rela.dyn entry for this relocation.  */
06626          if ((info->shared || (h != NULL && !htab->is_vxworks))
06627              && (sec->flags & SEC_ALLOC) != 0)
06628            {
06629              if (sreloc == NULL)
06630               {
06631                 sreloc = mips_elf_rel_dyn_section (info, TRUE);
06632                 if (sreloc == NULL)
06633                   return FALSE;
06634               }
06635              if (info->shared)
06636               {
06637                 /* When creating a shared object, we must copy these
06638                    reloc types into the output file as R_MIPS_REL32
06639                    relocs.  Make room for this reloc in .rel(a).dyn.  */
06640                 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
06641                 if (MIPS_ELF_READONLY_SECTION (sec))
06642                   /* We tell the dynamic linker that there are
06643                      relocations against the text segment.  */
06644                   info->flags |= DF_TEXTREL;
06645               }
06646              else
06647               {
06648                 struct mips_elf_link_hash_entry *hmips;
06649 
06650                 /* We only need to copy this reloc if the symbol is
06651                      defined in a dynamic object.  */
06652                 hmips = (struct mips_elf_link_hash_entry *) h;
06653                 ++hmips->possibly_dynamic_relocs;
06654                 if (MIPS_ELF_READONLY_SECTION (sec))
06655                   /* We need it to tell the dynamic linker if there
06656                      are relocations against the text segment.  */
06657                   hmips->readonly_reloc = TRUE;
06658               }
06659 
06660              /* Even though we don't directly need a GOT entry for
06661                this symbol, a symbol must have a dynamic symbol
06662                table index greater that DT_MIPS_GOTSYM if there are
06663                dynamic relocations against it.  This does not apply
06664                to VxWorks, which does not have the usual coupling
06665                between global GOT entries and .dynsym entries.  */
06666              if (h != NULL && !htab->is_vxworks)
06667               {
06668                 if (dynobj == NULL)
06669                   elf_hash_table (info)->dynobj = dynobj = abfd;
06670                 if (! mips_elf_create_got_section (dynobj, info, TRUE))
06671                   return FALSE;
06672                 g = mips_elf_got_info (dynobj, &sgot);
06673                 if (! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
06674                   return FALSE;
06675               }
06676            }
06677 
06678          if (SGI_COMPAT (abfd))
06679            mips_elf_hash_table (info)->compact_rel_size +=
06680              sizeof (Elf32_External_crinfo);
06681          break;
06682 
06683        case R_MIPS_PC16:
06684          if (h)
06685            ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
06686          break;
06687 
06688        case R_MIPS_26:
06689          if (h)
06690            ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
06691          /* Fall through.  */
06692 
06693        case R_MIPS_GPREL16:
06694        case R_MIPS_LITERAL:
06695        case R_MIPS_GPREL32:
06696          if (SGI_COMPAT (abfd))
06697            mips_elf_hash_table (info)->compact_rel_size +=
06698              sizeof (Elf32_External_crinfo);
06699          break;
06700 
06701          /* This relocation describes the C++ object vtable hierarchy.
06702             Reconstruct it for later use during GC.  */
06703        case R_MIPS_GNU_VTINHERIT:
06704          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
06705            return FALSE;
06706          break;
06707 
06708          /* This relocation describes which C++ vtable entries are actually
06709             used.  Record for later use during GC.  */
06710        case R_MIPS_GNU_VTENTRY:
06711          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
06712            return FALSE;
06713          break;
06714 
06715        default:
06716          break;
06717        }
06718 
06719       /* We must not create a stub for a symbol that has relocations
06720         related to taking the function's address.  This doesn't apply to
06721         VxWorks, where CALL relocs refer to a .got.plt entry instead of
06722         a normal .got entry.  */
06723       if (!htab->is_vxworks && h != NULL)
06724        switch (r_type)
06725          {
06726          default:
06727            ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
06728            break;
06729          case R_MIPS_CALL16:
06730          case R_MIPS_CALL_HI16:
06731          case R_MIPS_CALL_LO16:
06732          case R_MIPS_JALR:
06733            break;
06734          }
06735 
06736       /* If this reloc is not a 16 bit call, and it has a global
06737          symbol, then we will need the fn_stub if there is one.
06738          References from a stub section do not count.  */
06739       if (h != NULL
06740          && r_type != R_MIPS16_26
06741          && !mips16_stub_section_p (abfd, sec))
06742        {
06743          struct mips_elf_link_hash_entry *mh;
06744 
06745          mh = (struct mips_elf_link_hash_entry *) h;
06746          mh->need_fn_stub = TRUE;
06747        }
06748     }
06749 
06750   return TRUE;
06751 }
06752 
06753 bfd_boolean
06754 _bfd_mips_relax_section (bfd *abfd, asection *sec,
06755                       struct bfd_link_info *link_info,
06756                       bfd_boolean *again)
06757 {
06758   Elf_Internal_Rela *internal_relocs;
06759   Elf_Internal_Rela *irel, *irelend;
06760   Elf_Internal_Shdr *symtab_hdr;
06761   bfd_byte *contents = NULL;
06762   size_t extsymoff;
06763   bfd_boolean changed_contents = FALSE;
06764   bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
06765   Elf_Internal_Sym *isymbuf = NULL;
06766 
06767   /* We are not currently changing any sizes, so only one pass.  */
06768   *again = FALSE;
06769 
06770   if (link_info->relocatable)
06771     return TRUE;
06772 
06773   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
06774                                           link_info->keep_memory);
06775   if (internal_relocs == NULL)
06776     return TRUE;
06777 
06778   irelend = internal_relocs + sec->reloc_count
06779     * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
06780   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
06781   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
06782 
06783   for (irel = internal_relocs; irel < irelend; irel++)
06784     {
06785       bfd_vma symval;
06786       bfd_signed_vma sym_offset;
06787       unsigned int r_type;
06788       unsigned long r_symndx;
06789       asection *sym_sec;
06790       unsigned long instruction;
06791 
06792       /* Turn jalr into bgezal, and jr into beq, if they're marked
06793         with a JALR relocation, that indicate where they jump to.
06794         This saves some pipeline bubbles.  */
06795       r_type = ELF_R_TYPE (abfd, irel->r_info);
06796       if (r_type != R_MIPS_JALR)
06797        continue;
06798 
06799       r_symndx = ELF_R_SYM (abfd, irel->r_info);
06800       /* Compute the address of the jump target.  */
06801       if (r_symndx >= extsymoff)
06802        {
06803          struct mips_elf_link_hash_entry *h
06804            = ((struct mips_elf_link_hash_entry *)
06805               elf_sym_hashes (abfd) [r_symndx - extsymoff]);
06806 
06807          while (h->root.root.type == bfd_link_hash_indirect
06808                || h->root.root.type == bfd_link_hash_warning)
06809            h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
06810 
06811          /* If a symbol is undefined, or if it may be overridden,
06812             skip it.  */
06813          if (! ((h->root.root.type == bfd_link_hash_defined
06814                 || h->root.root.type == bfd_link_hash_defweak)
06815                && h->root.root.u.def.section)
06816              || (link_info->shared && ! link_info->symbolic
06817                 && !h->root.forced_local))
06818            continue;
06819 
06820          sym_sec = h->root.root.u.def.section;
06821          if (sym_sec->output_section)
06822            symval = (h->root.root.u.def.value
06823                     + sym_sec->output_section->vma
06824                     + sym_sec->output_offset);
06825          else
06826            symval = h->root.root.u.def.value;
06827        }
06828       else
06829        {
06830          Elf_Internal_Sym *isym;
06831 
06832          /* Read this BFD's symbols if we haven't done so already.  */
06833          if (isymbuf == NULL && symtab_hdr->sh_info != 0)
06834            {
06835              isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
06836              if (isymbuf == NULL)
06837               isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
06838                                           symtab_hdr->sh_info, 0,
06839                                           NULL, NULL, NULL);
06840              if (isymbuf == NULL)
06841               goto relax_return;
06842            }
06843 
06844          isym = isymbuf + r_symndx;
06845          if (isym->st_shndx == SHN_UNDEF)
06846            continue;
06847          else if (isym->st_shndx == SHN_ABS)
06848            sym_sec = bfd_abs_section_ptr;
06849          else if (isym->st_shndx == SHN_COMMON)
06850            sym_sec = bfd_com_section_ptr;
06851          else
06852            sym_sec
06853              = bfd_section_from_elf_index (abfd, isym->st_shndx);
06854          symval = isym->st_value
06855            + sym_sec->output_section->vma
06856            + sym_sec->output_offset;
06857        }
06858 
06859       /* Compute branch offset, from delay slot of the jump to the
06860         branch target.  */
06861       sym_offset = (symval + irel->r_addend)
06862        - (sec_start + irel->r_offset + 4);
06863 
06864       /* Branch offset must be properly aligned.  */
06865       if ((sym_offset & 3) != 0)
06866        continue;
06867 
06868       sym_offset >>= 2;
06869 
06870       /* Check that it's in range.  */
06871       if (sym_offset < -0x8000 || sym_offset >= 0x8000)
06872        continue;
06873 
06874       /* Get the section contents if we haven't done so already.  */
06875       if (contents == NULL)
06876        {
06877          /* Get cached copy if it exists.  */
06878          if (elf_section_data (sec)->this_hdr.contents != NULL)
06879            contents = elf_section_data (sec)->this_hdr.contents;
06880          else
06881            {
06882              if (!bfd_malloc_and_get_section (abfd, sec, &contents))
06883               goto relax_return;
06884            }
06885        }
06886 
06887       instruction = bfd_get_32 (abfd, contents + irel->r_offset);
06888 
06889       /* If it was jalr <reg>, turn it into bgezal $zero, <target>.  */
06890       if ((instruction & 0xfc1fffff) == 0x0000f809)
06891        instruction = 0x04110000;
06892       /* If it was jr <reg>, turn it into b <target>.  */
06893       else if ((instruction & 0xfc1fffff) == 0x00000008)
06894        instruction = 0x10000000;
06895       else
06896        continue;
06897 
06898       instruction |= (sym_offset & 0xffff);
06899       bfd_put_32 (abfd, instruction, contents + irel->r_offset);
06900       changed_contents = TRUE;
06901     }
06902 
06903   if (contents != NULL
06904       && elf_section_data (sec)->this_hdr.contents != contents)
06905     {
06906       if (!changed_contents && !link_info->keep_memory)
06907         free (contents);
06908       else
06909         {
06910           /* Cache the section contents for elf_link_input_bfd.  */
06911           elf_section_data (sec)->this_hdr.contents = contents;
06912         }
06913     }
06914   return TRUE;
06915 
06916  relax_return:
06917   if (contents != NULL
06918       && elf_section_data (sec)->this_hdr.contents != contents)
06919     free (contents);
06920   return FALSE;
06921 }
06922 
06923 /* Adjust a symbol defined by a dynamic object and referenced by a
06924    regular object.  The current definition is in some section of the
06925    dynamic object, but we're not including those sections.  We have to
06926    change the definition to something the rest of the link can
06927    understand.  */
06928 
06929 bfd_boolean
06930 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
06931                                  struct elf_link_hash_entry *h)
06932 {
06933   bfd *dynobj;
06934   struct mips_elf_link_hash_entry *hmips;
06935   asection *s;
06936   struct mips_elf_link_hash_table *htab;
06937 
06938   htab = mips_elf_hash_table (info);
06939   dynobj = elf_hash_table (info)->dynobj;
06940 
06941   /* Make sure we know what is going on here.  */
06942   BFD_ASSERT (dynobj != NULL
06943              && (h->needs_plt
06944                 || h->u.weakdef != NULL
06945                 || (h->def_dynamic
06946                     && h->ref_regular
06947                     && !h->def_regular)));
06948 
06949   /* If this symbol is defined in a dynamic object, we need to copy
06950      any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
06951      file.  */
06952   hmips = (struct mips_elf_link_hash_entry *) h;
06953   if (! info->relocatable
06954       && hmips->possibly_dynamic_relocs != 0
06955       && (h->root.type == bfd_link_hash_defweak
06956          || !h->def_regular))
06957     {
06958       mips_elf_allocate_dynamic_relocations
06959        (dynobj, info, hmips->possibly_dynamic_relocs);
06960       if (hmips->readonly_reloc)
06961        /* We tell the dynamic linker that there are relocations
06962           against the text segment.  */
06963        info->flags |= DF_TEXTREL;
06964     }
06965 
06966   /* For a function, create a stub, if allowed.  */
06967   if (! hmips->no_fn_stub
06968       && h->needs_plt)
06969     {
06970       if (! elf_hash_table (info)->dynamic_sections_created)
06971        return TRUE;
06972 
06973       /* If this symbol is not defined in a regular file, then set
06974         the symbol to the stub location.  This is required to make
06975         function pointers compare as equal between the normal
06976         executable and the shared library.  */
06977       if (!h->def_regular)
06978        {
06979          /* We need .stub section.  */
06980          s = bfd_get_section_by_name (dynobj,
06981                                    MIPS_ELF_STUB_SECTION_NAME (dynobj));
06982          BFD_ASSERT (s != NULL);
06983 
06984          h->root.u.def.section = s;
06985          h->root.u.def.value = s->size;
06986 
06987          /* XXX Write this stub address somewhere.  */
06988          h->plt.offset = s->size;
06989 
06990          /* Make room for this stub code.  */
06991          s->size += htab->function_stub_size;
06992 
06993          /* The last half word of the stub will be filled with the index
06994             of this symbol in .dynsym section.  */
06995          return TRUE;
06996        }
06997     }
06998   else if ((h->type == STT_FUNC)
06999           && !h->needs_plt)
07000     {
07001       /* This will set the entry for this symbol in the GOT to 0, and
07002          the dynamic linker will take care of this.  */
07003       h->root.u.def.value = 0;
07004       return TRUE;
07005     }
07006 
07007   /* If this is a weak symbol, and there is a real definition, the
07008      processor independent code will have arranged for us to see the
07009      real definition first, and we can just use the same value.  */
07010   if (h->u.weakdef != NULL)
07011     {
07012       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
07013                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
07014       h->root.u.def.section = h->u.weakdef->root.u.def.section;
07015       h->root.u.def.value = h->u.weakdef->root.u.def.value;
07016       return TRUE;
07017     }
07018 
07019   /* This is a reference to a symbol defined by a dynamic object which
07020      is not a function.  */
07021 
07022   return TRUE;
07023 }
07024 
07025 /* Likewise, for VxWorks.  */
07026 
07027 bfd_boolean
07028 _bfd_mips_vxworks_adjust_dynamic_symbol (struct bfd_link_info *info,
07029                                     struct elf_link_hash_entry *h)
07030 {
07031   bfd *dynobj;
07032   struct mips_elf_link_hash_entry *hmips;
07033   struct mips_elf_link_hash_table *htab;
07034   unsigned int power_of_two;
07035 
07036   htab = mips_elf_hash_table (info);
07037   dynobj = elf_hash_table (info)->dynobj;
07038   hmips = (struct mips_elf_link_hash_entry *) h;
07039 
07040   /* Make sure we know what is going on here.  */
07041   BFD_ASSERT (dynobj != NULL
07042              && (h->needs_plt
07043                 || h->needs_copy
07044                 || h->u.weakdef != NULL
07045                 || (h->def_dynamic
07046                     && h->ref_regular
07047                     && !h->def_regular)));
07048 
07049   /* If the symbol is defined by a dynamic object, we need a PLT stub if
07050      either (a) we want to branch to the symbol or (b) we're linking an
07051      executable that needs a canonical function address.  In the latter
07052      case, the canonical address will be the address of the executable's
07053      load stub.  */
07054   if ((hmips->is_branch_target
07055        || (!info->shared
07056           && h->type == STT_FUNC
07057           && hmips->is_relocation_target))
07058       && h->def_dynamic
07059       && h->ref_regular
07060       && !h->def_regular
07061       && !h->forced_local)
07062     h->needs_plt = 1;
07063 
07064   /* Locally-binding symbols do not need a PLT stub; we can refer to
07065      the functions directly.  */
07066   else if (h->needs_plt
07067           && (SYMBOL_CALLS_LOCAL (info, h)
07068               || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
07069                  && h->root.type == bfd_link_hash_undefweak)))
07070     {
07071       h->needs_plt = 0;
07072       return TRUE;
07073     }
07074 
07075   if (h->needs_plt)
07076     {
07077       /* If this is the first symbol to need a PLT entry, allocate room
07078         for the header, and for the header's .rela.plt.unloaded entries.  */
07079       if (htab->splt->size == 0)
07080        {
07081          htab->splt->size += htab->plt_header_size;
07082          if (!info->shared)
07083            htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
07084        }
07085 
07086       /* Assign the next .plt entry to this symbol.  */
07087       h->plt.offset = htab->splt->size;
07088       htab->splt->size += htab->plt_entry_size;
07089 
07090       /* If the output file has no definition of the symbol, set the
07091         symbol's value to the address of the stub.  For executables,
07092         point at the PLT load stub rather than the lazy resolution stub;
07093         this stub will become the canonical function address.  */
07094       if (!h->def_regular)
07095        {
07096          h->root.u.def.section = htab->splt;
07097          h->root.u.def.value = h->plt.offset;
07098          if (!info->shared)
07099            h->root.u.def.value += 8;
07100        }
07101 
07102       /* Make room for the .got.plt entry and the R_JUMP_SLOT relocation.  */
07103       htab->sgotplt->size += 4;
07104       htab->srelplt->size += sizeof (Elf32_External_Rela);
07105 
07106       /* Make room for the .rela.plt.unloaded relocations.  */
07107       if (!info->shared)
07108        htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
07109 
07110       return TRUE;
07111     }
07112 
07113   /* If a function symbol is defined by a dynamic object, and we do not
07114      need a PLT stub for it, the symbol's value should be zero.  */
07115   if (h->type == STT_FUNC
07116       && h->def_dynamic
07117       && h->ref_regular
07118       && !h->def_regular)
07119     {
07120       h->root.u.def.value = 0;
07121       return TRUE;
07122     }
07123 
07124   /* If this is a weak symbol, and there is a real definition, the
07125      processor independent code will have arranged for us to see the
07126      real definition first, and we can just use the same value.  */
07127   if (h->u.weakdef != NULL)
07128     {
07129       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
07130                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
07131       h->root.u.def.section = h->u.weakdef->root.u.def.section;
07132       h->root.u.def.value = h->u.weakdef->root.u.def.value;
07133       return TRUE;
07134     }
07135 
07136   /* This is a reference to a symbol defined by a dynamic object which
07137      is not a function.  */
07138   if (info->shared)
07139     return TRUE;
07140 
07141   /* We must allocate the symbol in our .dynbss section, which will
07142      become part of the .bss section of the executable.  There will be
07143      an entry for this symbol in the .dynsym section.  The dynamic
07144      object will contain position independent code, so all references
07145      from the dynamic object to this symbol will go through the global
07146      offset table.  The dynamic linker will use the .dynsym entry to
07147      determine the address it must put in the global offset table, so
07148      both the dynamic object and the regular object will refer to the
07149      same memory location for the variable.  */
07150 
07151   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
07152     {
07153       htab->srelbss->size += sizeof (Elf32_External_Rela);
07154       h->needs_copy = 1;
07155     }
07156 
07157   /* We need to figure out the alignment required for this symbol.  */
07158   power_of_two = bfd_log2 (h->size);
07159   if (power_of_two > 4)
07160     power_of_two = 4;
07161 
07162   /* Apply the required alignment.  */
07163   htab->sdynbss->size = BFD_ALIGN (htab->sdynbss->size,
07164                                (bfd_size_type) 1 << power_of_two);
07165   if (power_of_two > bfd_get_section_alignment (dynobj, htab->sdynbss)
07166       && !bfd_set_section_alignment (dynobj, htab->sdynbss, power_of_two))
07167     return FALSE;
07168 
07169   /* Define the symbol as being at this point in the section.  */
07170   h->root.u.def.section = htab->sdynbss;
07171   h->root.u.def.value = htab->sdynbss->size;
07172 
07173   /* Increment the section size to make room for the symbol.  */
07174   htab->sdynbss->size += h->size;
07175 
07176   return TRUE;
07177 }
07178 
07179 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
07180    The number might be exact or a worst-case estimate, depending on how
07181    much information is available to elf_backend_omit_section_dynsym at
07182    the current linking stage.  */
07183 
07184 static bfd_size_type
07185 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
07186 {
07187   bfd_size_type count;
07188 
07189   count = 0;
07190   if (info->shared || elf_hash_table (info)->is_relocatable_executable)
07191     {
07192       asection *p;
07193       const struct elf_backend_data *bed;
07194 
07195       bed = get_elf_backend_data (output_bfd);
07196       for (p = output_bfd->sections; p ; p = p->next)
07197        if ((p->flags & SEC_EXCLUDE) == 0
07198            && (p->flags & SEC_ALLOC) != 0
07199            && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
07200          ++count;
07201     }
07202   return count;
07203 }
07204 
07205 /* This function is called after all the input files have been read,
07206    and the input sections have been assigned to output sections.  We
07207    check for any mips16 stub sections that we can discard.  */
07208 
07209 bfd_boolean
07210 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
07211                                 struct bfd_link_info *info)
07212 {
07213   asection *ri;
07214 
07215   bfd *dynobj;
07216   asection *s;
07217   struct mips_got_info *g;
07218   int i;
07219   bfd_size_type loadable_size = 0;
07220   bfd_size_type local_gotno;
07221   bfd_size_type dynsymcount;
07222   bfd *sub;
07223   struct mips_elf_count_tls_arg count_tls_arg;
07224   struct mips_elf_link_hash_table *htab;
07225 
07226   htab = mips_elf_hash_table (info);
07227 
07228   /* The .reginfo section has a fixed size.  */
07229   ri = bfd_get_section_by_name (output_bfd, ".reginfo");
07230   if (ri != NULL)
07231     bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
07232 
07233   if (! (info->relocatable
07234         || ! mips_elf_hash_table (info)->mips16_stubs_seen))
07235     mips_elf_link_hash_traverse (mips_elf_hash_table (info),
07236                              mips_elf_check_mips16_stubs, NULL);
07237 
07238   dynobj = elf_hash_table (info)->dynobj;
07239   if (dynobj == NULL)
07240     /* Relocatable links don't have it.  */
07241     return TRUE;
07242 
07243   g = mips_elf_got_info (dynobj, &s);
07244   if (s == NULL)
07245     return TRUE;
07246 
07247   /* Calculate the total loadable size of the output.  That
07248      will give us the maximum number of GOT_PAGE entries
07249      required.  */
07250   for (sub = info->input_bfds; sub; sub = sub->link_next)
07251     {
07252       asection *subsection;
07253 
07254       for (subsection = sub->sections;
07255           subsection;
07256           subsection = subsection->next)
07257        {
07258          if ((subsection->flags & SEC_ALLOC) == 0)
07259            continue;
07260          loadable_size += ((subsection->size + 0xf)
07261                          &~ (bfd_size_type) 0xf);
07262        }
07263     }
07264 
07265   /* There has to be a global GOT entry for every symbol with
07266      a dynamic symbol table index of DT_MIPS_GOTSYM or
07267      higher.  Therefore, it make sense to put those symbols
07268      that need GOT entries at the end of the symbol table.  We
07269      do that here.  */
07270   if (! mips_elf_sort_hash_table (info, 1))
07271     return FALSE;
07272 
07273   if (g->global_gotsym != NULL)
07274     i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
07275   else
07276     /* If there are no global symbols, or none requiring
07277        relocations, then GLOBAL_GOTSYM will be NULL.  */
07278     i = 0;
07279 
07280   /* Get a worst-case estimate of the number of dynamic symbols needed.
07281      At this point, dynsymcount does not account for section symbols
07282      and count_section_dynsyms may overestimate the number that will
07283      be needed.  */
07284   dynsymcount = (elf_hash_table (info)->dynsymcount
07285                + count_section_dynsyms (output_bfd, info));
07286 
07287   /* Determine the size of one stub entry.  */
07288   htab->function_stub_size = (dynsymcount > 0x10000
07289                            ? MIPS_FUNCTION_STUB_BIG_SIZE
07290                            : MIPS_FUNCTION_STUB_NORMAL_SIZE);
07291 
07292   /* In the worst case, we'll get one stub per dynamic symbol, plus
07293      one to account for the dummy entry at the end required by IRIX
07294      rld.  */
07295   loadable_size += htab->function_stub_size * (i + 1);
07296 
07297   if (htab->is_vxworks)
07298     /* There's no need to allocate page entries for VxWorks; R_MIPS_GOT16
07299        relocations against local symbols evaluate to "G", and the EABI does
07300        not include R_MIPS_GOT_PAGE.  */
07301     local_gotno = 0;
07302   else
07303     /* Assume there are two loadable segments consisting of contiguous
07304        sections.  Is 5 enough?  */
07305     local_gotno = (loadable_size >> 16) + 5;
07306 
07307   g->local_gotno += local_gotno;
07308   s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
07309 
07310   g->global_gotno = i;
07311   s->size += i * MIPS_ELF_GOT_SIZE (output_bfd);
07312 
07313   /* We need to calculate tls_gotno for global symbols at this point
07314      instead of building it up earlier, to avoid doublecounting
07315      entries for one global symbol from multiple input files.  */
07316   count_tls_arg.info = info;
07317   count_tls_arg.needed = 0;
07318   elf_link_hash_traverse (elf_hash_table (info),
07319                        mips_elf_count_global_tls_entries,
07320                        &count_tls_arg);
07321   g->tls_gotno += count_tls_arg.needed;
07322   s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
07323 
07324   mips_elf_resolve_final_got_entries (g);
07325 
07326   /* VxWorks does not support multiple GOTs.  It initializes $gp to
07327      __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
07328      dynamic loader.  */
07329   if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
07330     {
07331       if (! mips_elf_multi_got (output_bfd, info, g, s, local_gotno))
07332        return FALSE;
07333     }
07334   else
07335     {
07336       /* Set up TLS entries for the first GOT.  */
07337       g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
07338       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
07339     }
07340 
07341   return TRUE;
07342 }
07343 
07344 /* Set the sizes of the dynamic sections.  */
07345 
07346 bfd_boolean
07347 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
07348                                  struct bfd_link_info *info)
07349 {
07350   bfd *dynobj;
07351   asection *s, *sreldyn;
07352   bfd_boolean reltext;
07353   struct mips_elf_link_hash_table *htab;
07354 
07355   htab = mips_elf_hash_table (info);
07356   dynobj = elf_hash_table (info)->dynobj;
07357   BFD_ASSERT (dynobj != NULL);
07358 
07359   if (elf_hash_table (info)->dynamic_sections_created)
07360     {
07361       /* Set the contents of the .interp section to the interpreter.  */
07362       if (info->executable)
07363        {
07364          s = bfd_get_section_by_name (dynobj, ".interp");
07365          BFD_ASSERT (s != NULL);
07366          s->size
07367            = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
07368          s->contents
07369            = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
07370        }
07371     }
07372 
07373   /* The check_relocs and adjust_dynamic_symbol entry points have
07374      determined the sizes of the various dynamic sections.  Allocate
07375      memory for them.  */
07376   reltext = FALSE;
07377   sreldyn = NULL;
07378   for (s = dynobj->sections; s != NULL; s = s->next)
07379     {
07380       const char *name;
07381 
07382       /* It's OK to base decisions on the section name, because none
07383         of the dynobj section names depend upon the input files.  */
07384       name = bfd_get_section_name (dynobj, s);
07385 
07386       if ((s->flags & SEC_LINKER_CREATED) == 0)
07387        continue;
07388 
07389       if (CONST_STRNEQ (name, ".rel"))
07390        {
07391          if (s->size != 0)
07392            {
07393              const char *outname;
07394              asection *target;
07395 
07396              /* If this relocation section applies to a read only
07397                  section, then we probably need a DT_TEXTREL entry.
07398                  If the relocation section is .rel(a).dyn, we always
07399                  assert a DT_TEXTREL entry rather than testing whether
07400                  there exists a relocation to a read only section or
07401                  not.  */
07402              outname = bfd_get_section_name (output_bfd,
07403                                          s->output_section);
07404              target = bfd_get_section_by_name (output_bfd, outname + 4);
07405              if ((target != NULL
07406                  && (target->flags & SEC_READONLY) != 0
07407                  && (target->flags & SEC_ALLOC) != 0)
07408                 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
07409               reltext = TRUE;
07410 
07411              /* We use the reloc_count field as a counter if we need
07412                to copy relocs into the output file.  */
07413              if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
07414               s->reloc_count = 0;
07415 
07416              /* If combreloc is enabled, elf_link_sort_relocs() will
07417                sort relocations, but in a different way than we do,
07418                and before we're done creating relocations.  Also, it
07419                will move them around between input sections'
07420                relocation's contents, so our sorting would be
07421                broken, so don't let it run.  */
07422              info->combreloc = 0;
07423            }
07424        }
07425       else if (htab->is_vxworks && strcmp (name, ".got") == 0)
07426        {
07427          /* Executables do not need a GOT.  */
07428          if (info->shared)
07429            {
07430              /* Allocate relocations for all but the reserved entries.  */
07431              struct mips_got_info *g;
07432              unsigned int count;
07433 
07434              g = mips_elf_got_info (dynobj, NULL);
07435              count = (g->global_gotno
07436                      + g->local_gotno
07437                      - MIPS_RESERVED_GOTNO (info));
07438              mips_elf_allocate_dynamic_relocations (dynobj, info, count);
07439            }
07440        }
07441       else if (!htab->is_vxworks && CONST_STRNEQ (name, ".got"))
07442        {
07443          /* _bfd_mips_elf_always_size_sections() has already done
07444             most of the work, but some symbols may have been mapped
07445             to versions that we must now resolve in the got_entries
07446             hash tables.  */
07447          struct mips_got_info *gg = mips_elf_got_info (dynobj, NULL);
07448          struct mips_got_info *g = gg;
07449          struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
07450          unsigned int needed_relocs = 0;
07451 
07452          if (gg->next)
07453            {
07454              set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (output_bfd);
07455              set_got_offset_arg.info = info;
07456 
07457              /* NOTE 2005-02-03: How can this call, or the next, ever
07458                find any indirect entries to resolve?  They were all
07459                resolved in mips_elf_multi_got.  */
07460              mips_elf_resolve_final_got_entries (gg);
07461              for (g = gg->next; g && g->next != gg; g = g->next)
07462               {
07463                 unsigned int save_assign;
07464 
07465                 mips_elf_resolve_final_got_entries (g);
07466 
07467                 /* Assign offsets to global GOT entries.  */
07468                 save_assign = g->assigned_gotno;
07469                 g->assigned_gotno = g->local_gotno;
07470                 set_got_offset_arg.g = g;
07471                 set_got_offset_arg.needed_relocs = 0;
07472                 htab_traverse (g->got_entries,
07473                              mips_elf_set_global_got_offset,
07474                              &set_got_offset_arg);
07475                 needed_relocs += set_got_offset_arg.needed_relocs;
07476                 BFD_ASSERT (g->assigned_gotno - g->local_gotno
07477                            <= g->global_gotno);
07478 
07479                 g->assigned_gotno = save_assign;
07480                 if (info->shared)
07481                   {
07482                     needed_relocs += g->local_gotno - g->assigned_gotno;
07483                     BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
07484                               + g->next->global_gotno
07485                               + g->next->tls_gotno
07486                               + MIPS_RESERVED_GOTNO (info));
07487                   }
07488               }
07489            }
07490          else
07491            {
07492              struct mips_elf_count_tls_arg arg;
07493              arg.info = info;
07494              arg.needed = 0;
07495 
07496              htab_traverse (gg->got_entries, mips_elf_count_local_tls_relocs,
07497                           &arg);
07498              elf_link_hash_traverse (elf_hash_table (info),
07499                                   mips_elf_count_global_tls_relocs,
07500                                   &arg);
07501 
07502              needed_relocs += arg.needed;
07503            }
07504 
07505          if (needed_relocs)
07506            mips_elf_allocate_dynamic_relocations (dynobj, info,
07507                                              needed_relocs);
07508        }
07509       else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
07510        {
07511          /* IRIX rld assumes that the function stub isn't at the end
07512             of .text section.  So put a dummy.  XXX  */
07513          s->size += htab->function_stub_size;
07514        }
07515       else if (! info->shared
07516               && ! mips_elf_hash_table (info)->use_rld_obj_head
07517               && CONST_STRNEQ (name, ".rld_map"))
07518        {
07519          /* We add a room for __rld_map.  It will be filled in by the
07520             rtld to contain a pointer to the _r_debug structure.  */
07521          s->size += 4;
07522        }
07523       else if (SGI_COMPAT (output_bfd)
07524               && CONST_STRNEQ (name, ".compact_rel"))
07525        s->size += mips_elf_hash_table (info)->compact_rel_size;
07526       else if (! CONST_STRNEQ (name, ".init")
07527               && s != htab->sgotplt
07528               && s != htab->splt)
07529        {
07530          /* It's not one of our sections, so don't allocate space.  */
07531          continue;
07532        }
07533 
07534       if (s->size == 0)
07535        {
07536          s->flags |= SEC_EXCLUDE;
07537          continue;
07538        }
07539 
07540       if ((s->flags & SEC_HAS_CONTENTS) == 0)
07541        continue;
07542 
07543       /* Allocate memory for this section last, since we may increase its
07544         size above.  */
07545       if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) == 0)
07546        {
07547          sreldyn = s;
07548          continue;
07549        }
07550 
07551       /* Allocate memory for the section contents.  */
07552       s->contents = bfd_zalloc (dynobj, s->size);
07553       if (s->contents == NULL)
07554        {
07555          bfd_set_error (bfd_error_no_memory);
07556          return FALSE;
07557        }
07558     }
07559 
07560   /* Allocate memory for the .rel(a).dyn section.  */
07561   if (sreldyn != NULL)
07562     {
07563       sreldyn->contents = bfd_zalloc (dynobj, sreldyn->size);
07564       if (sreldyn->contents == NULL)
07565        {
07566          bfd_set_error (bfd_error_no_memory);
07567          return FALSE;
07568        }
07569     }
07570 
07571   if (elf_hash_table (info)->dynamic_sections_created)
07572     {
07573       /* Add some entries to the .dynamic section.  We fill in the
07574         values later, in _bfd_mips_elf_finish_dynamic_sections, but we
07575         must add the entries now so that we get the correct size for
07576         the .dynamic section.  The DT_DEBUG entry is filled in by the
07577         dynamic linker and used by the debugger.  */
07578       if (info->executable)
07579        {
07580          /* SGI object has the equivalence of DT_DEBUG in the
07581             DT_MIPS_RLD_MAP entry.  */
07582          if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
07583            return FALSE;
07584          if (!SGI_COMPAT (output_bfd))
07585            {
07586              if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
07587               return FALSE;
07588            }
07589        }
07590 
07591       if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
07592        info->flags |= DF_TEXTREL;
07593 
07594       if ((info->flags & DF_TEXTREL) != 0)
07595        {
07596          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
07597            return FALSE;
07598 
07599          /* Clear the DF_TEXTREL flag.  It will be set again if we
07600             write out an actual text relocation; we may not, because
07601             at this point we do not know whether e.g. any .eh_frame
07602             absolute relocations have been converted to PC-relative.  */
07603          info->flags &= ~DF_TEXTREL;
07604        }
07605 
07606       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
07607        return FALSE;
07608 
07609       if (htab->is_vxworks)
07610        {
07611          /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
07612             use any of the DT_MIPS_* tags.  */
07613          if (mips_elf_rel_dyn_section (info, FALSE))
07614            {
07615              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
07616               return FALSE;
07617 
07618              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
07619               return FALSE;
07620 
07621              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
07622               return FALSE;
07623            }
07624          if (htab->splt->size > 0)
07625            {
07626              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
07627               return FALSE;
07628 
07629              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
07630               return FALSE;
07631 
07632              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
07633               return FALSE;
07634            }
07635        }
07636       else
07637        {
07638          if (mips_elf_rel_dyn_section (info, FALSE))
07639            {
07640              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
07641               return FALSE;
07642 
07643              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
07644               return FALSE;
07645 
07646              if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
07647               return FALSE;
07648            }
07649 
07650          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
07651            return FALSE;
07652 
07653          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
07654            return FALSE;
07655 
07656          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
07657            return FALSE;
07658 
07659          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
07660            return FALSE;
07661 
07662          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
07663            return FALSE;
07664 
07665          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
07666            return FALSE;
07667 
07668          if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
07669            return FALSE;
07670 
07671          if (IRIX_COMPAT (dynobj) == ict_irix5
07672              && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
07673            return FALSE;
07674 
07675          if (IRIX_COMPAT (dynobj) == ict_irix6
07676              && (bfd_get_section_by_name
07677                 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
07678              && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
07679            return FALSE;
07680        }
07681     }
07682 
07683   return TRUE;
07684 }
07685 
07686 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
07687    Adjust its R_ADDEND field so that it is correct for the output file.
07688    LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
07689    and sections respectively; both use symbol indexes.  */
07690 
07691 static void
07692 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
07693                      bfd *input_bfd, Elf_Internal_Sym *local_syms,
07694                      asection **local_sections, Elf_Internal_Rela *rel)
07695 {
07696   unsigned int r_type, r_symndx;
07697   Elf_Internal_Sym *sym;
07698   asection *sec;
07699 
07700   if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
07701     {
07702       r_type = ELF_R_TYPE (output_bfd, rel->r_info);
07703       if (r_type == R_MIPS16_GPREL
07704          || r_type == R_MIPS_GPREL16
07705          || r_type == R_MIPS_GPREL32
07706          || r_type == R_MIPS_LITERAL)
07707        {
07708          rel->r_addend += _bfd_get_gp_value (input_bfd);
07709          rel->r_addend -= _bfd_get_gp_value (output_bfd);
07710        }
07711 
07712       r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
07713       sym = local_syms + r_symndx;
07714 
07715       /* Adjust REL's addend to account for section merging.  */
07716       if (!info->relocatable)
07717        {
07718          sec = local_sections[r_symndx];
07719          _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
07720        }