Home | History | Annotate | Line # | Download | only in bfd
elfxx-mips.c revision 1.1
      1  1.1  christos /* MIPS-specific support for ELF
      2  1.1  christos    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
      3  1.1  christos    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    Most of the information added by Ian Lance Taylor, Cygnus Support,
      6  1.1  christos    <ian (at) cygnus.com>.
      7  1.1  christos    N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
      8  1.1  christos    <mark (at) codesourcery.com>
      9  1.1  christos    Traditional MIPS targets support added by Koundinya.K, Dansk Data
     10  1.1  christos    Elektronik & Operations Research Group. <kk (at) ddeorg.soft.net>
     11  1.1  christos 
     12  1.1  christos    This file is part of BFD, the Binary File Descriptor library.
     13  1.1  christos 
     14  1.1  christos    This program is free software; you can redistribute it and/or modify
     15  1.1  christos    it under the terms of the GNU General Public License as published by
     16  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     17  1.1  christos    (at your option) any later version.
     18  1.1  christos 
     19  1.1  christos    This program is distributed in the hope that it will be useful,
     20  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     21  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     22  1.1  christos    GNU General Public License for more details.
     23  1.1  christos 
     24  1.1  christos    You should have received a copy of the GNU General Public License
     25  1.1  christos    along with this program; if not, write to the Free Software
     26  1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     27  1.1  christos    MA 02110-1301, USA.  */
     28  1.1  christos 
     29  1.1  christos 
     30  1.1  christos /* This file handles functionality common to the different MIPS ABI's.  */
     31  1.1  christos 
     32  1.1  christos #include "sysdep.h"
     33  1.1  christos #include "bfd.h"
     34  1.1  christos #include "libbfd.h"
     35  1.1  christos #include "libiberty.h"
     36  1.1  christos #include "elf-bfd.h"
     37  1.1  christos #include "elfxx-mips.h"
     38  1.1  christos #include "elf/mips.h"
     39  1.1  christos #include "elf-vxworks.h"
     40  1.1  christos 
     41  1.1  christos /* Get the ECOFF swapping routines.  */
     42  1.1  christos #include "coff/sym.h"
     43  1.1  christos #include "coff/symconst.h"
     44  1.1  christos #include "coff/ecoff.h"
     45  1.1  christos #include "coff/mips.h"
     46  1.1  christos 
     47  1.1  christos #include "hashtab.h"
     48  1.1  christos 
     49  1.1  christos /* This structure is used to hold information about one GOT entry.
     50  1.1  christos    There are three types of entry:
     51  1.1  christos 
     52  1.1  christos       (1) absolute addresses
     53  1.1  christos 	    (abfd == NULL)
     54  1.1  christos       (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
     55  1.1  christos 	    (abfd != NULL, symndx >= 0)
     56  1.1  christos       (3) SYMBOL addresses, where SYMBOL is not local to an input bfd
     57  1.1  christos 	    (abfd != NULL, symndx == -1)
     58  1.1  christos 
     59  1.1  christos    Type (3) entries are treated differently for different types of GOT.
     60  1.1  christos    In the "master" GOT -- i.e.  the one that describes every GOT
     61  1.1  christos    reference needed in the link -- the mips_got_entry is keyed on both
     62  1.1  christos    the symbol and the input bfd that references it.  If it turns out
     63  1.1  christos    that we need multiple GOTs, we can then use this information to
     64  1.1  christos    create separate GOTs for each input bfd.
     65  1.1  christos 
     66  1.1  christos    However, we want each of these separate GOTs to have at most one
     67  1.1  christos    entry for a given symbol, so their type (3) entries are keyed only
     68  1.1  christos    on the symbol.  The input bfd given by the "abfd" field is somewhat
     69  1.1  christos    arbitrary in this case.
     70  1.1  christos 
     71  1.1  christos    This means that when there are multiple GOTs, each GOT has a unique
     72  1.1  christos    mips_got_entry for every symbol within it.  We can therefore use the
     73  1.1  christos    mips_got_entry fields (tls_type and gotidx) to track the symbol's
     74  1.1  christos    GOT index.
     75  1.1  christos 
     76  1.1  christos    However, if it turns out that we need only a single GOT, we continue
     77  1.1  christos    to use the master GOT to describe it.  There may therefore be several
     78  1.1  christos    mips_got_entries for the same symbol, each with a different input bfd.
     79  1.1  christos    We want to make sure that each symbol gets a unique GOT entry, so when
     80  1.1  christos    there's a single GOT, we use the symbol's hash entry, not the
     81  1.1  christos    mips_got_entry fields, to track a symbol's GOT index.  */
     82  1.1  christos struct mips_got_entry
     83  1.1  christos {
     84  1.1  christos   /* The input bfd in which the symbol is defined.  */
     85  1.1  christos   bfd *abfd;
     86  1.1  christos   /* The index of the symbol, as stored in the relocation r_info, if
     87  1.1  christos      we have a local symbol; -1 otherwise.  */
     88  1.1  christos   long symndx;
     89  1.1  christos   union
     90  1.1  christos   {
     91  1.1  christos     /* If abfd == NULL, an address that must be stored in the got.  */
     92  1.1  christos     bfd_vma address;
     93  1.1  christos     /* If abfd != NULL && symndx != -1, the addend of the relocation
     94  1.1  christos        that should be added to the symbol value.  */
     95  1.1  christos     bfd_vma addend;
     96  1.1  christos     /* If abfd != NULL && symndx == -1, the hash table entry
     97  1.1  christos        corresponding to symbol in the GOT.  The symbol's entry
     98  1.1  christos        is in the local area if h->global_got_area is GGA_NONE,
     99  1.1  christos        otherwise it is in the global area.  */
    100  1.1  christos     struct mips_elf_link_hash_entry *h;
    101  1.1  christos   } d;
    102  1.1  christos 
    103  1.1  christos   /* The TLS types included in this GOT entry (specifically, GD and
    104  1.1  christos      IE).  The GD and IE flags can be added as we encounter new
    105  1.1  christos      relocations.  LDM can also be set; it will always be alone, not
    106  1.1  christos      combined with any GD or IE flags.  An LDM GOT entry will be
    107  1.1  christos      a local symbol entry with r_symndx == 0.  */
    108  1.1  christos   unsigned char tls_type;
    109  1.1  christos 
    110  1.1  christos   /* The offset from the beginning of the .got section to the entry
    111  1.1  christos      corresponding to this symbol+addend.  If it's a global symbol
    112  1.1  christos      whose offset is yet to be decided, it's going to be -1.  */
    113  1.1  christos   long gotidx;
    114  1.1  christos };
    115  1.1  christos 
    116  1.1  christos /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
    117  1.1  christos    The structures form a non-overlapping list that is sorted by increasing
    118  1.1  christos    MIN_ADDEND.  */
    119  1.1  christos struct mips_got_page_range
    120  1.1  christos {
    121  1.1  christos   struct mips_got_page_range *next;
    122  1.1  christos   bfd_signed_vma min_addend;
    123  1.1  christos   bfd_signed_vma max_addend;
    124  1.1  christos };
    125  1.1  christos 
    126  1.1  christos /* This structure describes the range of addends that are applied to page
    127  1.1  christos    relocations against a given symbol.  */
    128  1.1  christos struct mips_got_page_entry
    129  1.1  christos {
    130  1.1  christos   /* The input bfd in which the symbol is defined.  */
    131  1.1  christos   bfd *abfd;
    132  1.1  christos   /* The index of the symbol, as stored in the relocation r_info.  */
    133  1.1  christos   long symndx;
    134  1.1  christos   /* The ranges for this page entry.  */
    135  1.1  christos   struct mips_got_page_range *ranges;
    136  1.1  christos   /* The maximum number of page entries needed for RANGES.  */
    137  1.1  christos   bfd_vma num_pages;
    138  1.1  christos };
    139  1.1  christos 
    140  1.1  christos /* This structure is used to hold .got information when linking.  */
    141  1.1  christos 
    142  1.1  christos struct mips_got_info
    143  1.1  christos {
    144  1.1  christos   /* The global symbol in the GOT with the lowest index in the dynamic
    145  1.1  christos      symbol table.  */
    146  1.1  christos   struct elf_link_hash_entry *global_gotsym;
    147  1.1  christos   /* The number of global .got entries.  */
    148  1.1  christos   unsigned int global_gotno;
    149  1.1  christos   /* The number of global .got entries that are in the GGA_RELOC_ONLY area.  */
    150  1.1  christos   unsigned int reloc_only_gotno;
    151  1.1  christos   /* The number of .got slots used for TLS.  */
    152  1.1  christos   unsigned int tls_gotno;
    153  1.1  christos   /* The first unused TLS .got entry.  Used only during
    154  1.1  christos      mips_elf_initialize_tls_index.  */
    155  1.1  christos   unsigned int tls_assigned_gotno;
    156  1.1  christos   /* The number of local .got entries, eventually including page entries.  */
    157  1.1  christos   unsigned int local_gotno;
    158  1.1  christos   /* The maximum number of page entries needed.  */
    159  1.1  christos   unsigned int page_gotno;
    160  1.1  christos   /* The number of local .got entries we have used.  */
    161  1.1  christos   unsigned int assigned_gotno;
    162  1.1  christos   /* A hash table holding members of the got.  */
    163  1.1  christos   struct htab *got_entries;
    164  1.1  christos   /* A hash table of mips_got_page_entry structures.  */
    165  1.1  christos   struct htab *got_page_entries;
    166  1.1  christos   /* A hash table mapping input bfds to other mips_got_info.  NULL
    167  1.1  christos      unless multi-got was necessary.  */
    168  1.1  christos   struct htab *bfd2got;
    169  1.1  christos   /* In multi-got links, a pointer to the next got (err, rather, most
    170  1.1  christos      of the time, it points to the previous got).  */
    171  1.1  christos   struct mips_got_info *next;
    172  1.1  christos   /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
    173  1.1  christos      for none, or MINUS_TWO for not yet assigned.  This is needed
    174  1.1  christos      because a single-GOT link may have multiple hash table entries
    175  1.1  christos      for the LDM.  It does not get initialized in multi-GOT mode.  */
    176  1.1  christos   bfd_vma tls_ldm_offset;
    177  1.1  christos };
    178  1.1  christos 
    179  1.1  christos /* Map an input bfd to a got in a multi-got link.  */
    180  1.1  christos 
    181  1.1  christos struct mips_elf_bfd2got_hash
    182  1.1  christos {
    183  1.1  christos   bfd *bfd;
    184  1.1  christos   struct mips_got_info *g;
    185  1.1  christos };
    186  1.1  christos 
    187  1.1  christos /* Structure passed when traversing the bfd2got hash table, used to
    188  1.1  christos    create and merge bfd's gots.  */
    189  1.1  christos 
    190  1.1  christos struct mips_elf_got_per_bfd_arg
    191  1.1  christos {
    192  1.1  christos   /* A hashtable that maps bfds to gots.  */
    193  1.1  christos   htab_t bfd2got;
    194  1.1  christos   /* The output bfd.  */
    195  1.1  christos   bfd *obfd;
    196  1.1  christos   /* The link information.  */
    197  1.1  christos   struct bfd_link_info *info;
    198  1.1  christos   /* A pointer to the primary got, i.e., the one that's going to get
    199  1.1  christos      the implicit relocations from DT_MIPS_LOCAL_GOTNO and
    200  1.1  christos      DT_MIPS_GOTSYM.  */
    201  1.1  christos   struct mips_got_info *primary;
    202  1.1  christos   /* A non-primary got we're trying to merge with other input bfd's
    203  1.1  christos      gots.  */
    204  1.1  christos   struct mips_got_info *current;
    205  1.1  christos   /* The maximum number of got entries that can be addressed with a
    206  1.1  christos      16-bit offset.  */
    207  1.1  christos   unsigned int max_count;
    208  1.1  christos   /* The maximum number of page entries needed by each got.  */
    209  1.1  christos   unsigned int max_pages;
    210  1.1  christos   /* The total number of global entries which will live in the
    211  1.1  christos      primary got and be automatically relocated.  This includes
    212  1.1  christos      those not referenced by the primary GOT but included in
    213  1.1  christos      the "master" GOT.  */
    214  1.1  christos   unsigned int global_count;
    215  1.1  christos };
    216  1.1  christos 
    217  1.1  christos /* Another structure used to pass arguments for got entries traversal.  */
    218  1.1  christos 
    219  1.1  christos struct mips_elf_set_global_got_offset_arg
    220  1.1  christos {
    221  1.1  christos   struct mips_got_info *g;
    222  1.1  christos   int value;
    223  1.1  christos   unsigned int needed_relocs;
    224  1.1  christos   struct bfd_link_info *info;
    225  1.1  christos };
    226  1.1  christos 
    227  1.1  christos /* A structure used to count TLS relocations or GOT entries, for GOT
    228  1.1  christos    entry or ELF symbol table traversal.  */
    229  1.1  christos 
    230  1.1  christos struct mips_elf_count_tls_arg
    231  1.1  christos {
    232  1.1  christos   struct bfd_link_info *info;
    233  1.1  christos   unsigned int needed;
    234  1.1  christos };
    235  1.1  christos 
    236  1.1  christos struct _mips_elf_section_data
    237  1.1  christos {
    238  1.1  christos   struct bfd_elf_section_data elf;
    239  1.1  christos   union
    240  1.1  christos   {
    241  1.1  christos     bfd_byte *tdata;
    242  1.1  christos   } u;
    243  1.1  christos };
    244  1.1  christos 
    245  1.1  christos #define mips_elf_section_data(sec) \
    246  1.1  christos   ((struct _mips_elf_section_data *) elf_section_data (sec))
    247  1.1  christos 
    248  1.1  christos #define is_mips_elf(bfd)				\
    249  1.1  christos   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
    250  1.1  christos    && elf_tdata (bfd) != NULL				\
    251  1.1  christos    && elf_object_id (bfd) == MIPS_ELF_DATA)
    252  1.1  christos 
    253  1.1  christos /* The ABI says that every symbol used by dynamic relocations must have
    254  1.1  christos    a global GOT entry.  Among other things, this provides the dynamic
    255  1.1  christos    linker with a free, directly-indexed cache.  The GOT can therefore
    256  1.1  christos    contain symbols that are not referenced by GOT relocations themselves
    257  1.1  christos    (in other words, it may have symbols that are not referenced by things
    258  1.1  christos    like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
    259  1.1  christos 
    260  1.1  christos    GOT relocations are less likely to overflow if we put the associated
    261  1.1  christos    GOT entries towards the beginning.  We therefore divide the global
    262  1.1  christos    GOT entries into two areas: "normal" and "reloc-only".  Entries in
    263  1.1  christos    the first area can be used for both dynamic relocations and GP-relative
    264  1.1  christos    accesses, while those in the "reloc-only" area are for dynamic
    265  1.1  christos    relocations only.
    266  1.1  christos 
    267  1.1  christos    These GGA_* ("Global GOT Area") values are organised so that lower
    268  1.1  christos    values are more general than higher values.  Also, non-GGA_NONE
    269  1.1  christos    values are ordered by the position of the area in the GOT.  */
    270  1.1  christos #define GGA_NORMAL 0
    271  1.1  christos #define GGA_RELOC_ONLY 1
    272  1.1  christos #define GGA_NONE 2
    273  1.1  christos 
    274  1.1  christos /* Information about a non-PIC interface to a PIC function.  There are
    275  1.1  christos    two ways of creating these interfaces.  The first is to add:
    276  1.1  christos 
    277  1.1  christos 	lui	$25,%hi(func)
    278  1.1  christos 	addiu	$25,$25,%lo(func)
    279  1.1  christos 
    280  1.1  christos    immediately before a PIC function "func".  The second is to add:
    281  1.1  christos 
    282  1.1  christos 	lui	$25,%hi(func)
    283  1.1  christos 	j	func
    284  1.1  christos 	addiu	$25,$25,%lo(func)
    285  1.1  christos 
    286  1.1  christos    to a separate trampoline section.
    287  1.1  christos 
    288  1.1  christos    Stubs of the first kind go in a new section immediately before the
    289  1.1  christos    target function.  Stubs of the second kind go in a single section
    290  1.1  christos    pointed to by the hash table's "strampoline" field.  */
    291  1.1  christos struct mips_elf_la25_stub {
    292  1.1  christos   /* The generated section that contains this stub.  */
    293  1.1  christos   asection *stub_section;
    294  1.1  christos 
    295  1.1  christos   /* The offset of the stub from the start of STUB_SECTION.  */
    296  1.1  christos   bfd_vma offset;
    297  1.1  christos 
    298  1.1  christos   /* One symbol for the original function.  Its location is available
    299  1.1  christos      in H->root.root.u.def.  */
    300  1.1  christos   struct mips_elf_link_hash_entry *h;
    301  1.1  christos };
    302  1.1  christos 
    303  1.1  christos /* Macros for populating a mips_elf_la25_stub.  */
    304  1.1  christos 
    305  1.1  christos #define LA25_LUI(VAL) (0x3c190000 | (VAL))	/* lui t9,VAL */
    306  1.1  christos #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
    307  1.1  christos #define LA25_ADDIU(VAL) (0x27390000 | (VAL))	/* addiu t9,t9,VAL */
    308  1.1  christos 
    309  1.1  christos /* This structure is passed to mips_elf_sort_hash_table_f when sorting
    310  1.1  christos    the dynamic symbols.  */
    311  1.1  christos 
    312  1.1  christos struct mips_elf_hash_sort_data
    313  1.1  christos {
    314  1.1  christos   /* The symbol in the global GOT with the lowest dynamic symbol table
    315  1.1  christos      index.  */
    316  1.1  christos   struct elf_link_hash_entry *low;
    317  1.1  christos   /* The least dynamic symbol table index corresponding to a non-TLS
    318  1.1  christos      symbol with a GOT entry.  */
    319  1.1  christos   long min_got_dynindx;
    320  1.1  christos   /* The greatest dynamic symbol table index corresponding to a symbol
    321  1.1  christos      with a GOT entry that is not referenced (e.g., a dynamic symbol
    322  1.1  christos      with dynamic relocations pointing to it from non-primary GOTs).  */
    323  1.1  christos   long max_unref_got_dynindx;
    324  1.1  christos   /* The greatest dynamic symbol table index not corresponding to a
    325  1.1  christos      symbol without a GOT entry.  */
    326  1.1  christos   long max_non_got_dynindx;
    327  1.1  christos };
    328  1.1  christos 
    329  1.1  christos /* The MIPS ELF linker needs additional information for each symbol in
    330  1.1  christos    the global hash table.  */
    331  1.1  christos 
    332  1.1  christos struct mips_elf_link_hash_entry
    333  1.1  christos {
    334  1.1  christos   struct elf_link_hash_entry root;
    335  1.1  christos 
    336  1.1  christos   /* External symbol information.  */
    337  1.1  christos   EXTR esym;
    338  1.1  christos 
    339  1.1  christos   /* The la25 stub we have created for ths symbol, if any.  */
    340  1.1  christos   struct mips_elf_la25_stub *la25_stub;
    341  1.1  christos 
    342  1.1  christos   /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
    343  1.1  christos      this symbol.  */
    344  1.1  christos   unsigned int possibly_dynamic_relocs;
    345  1.1  christos 
    346  1.1  christos   /* If there is a stub that 32 bit functions should use to call this
    347  1.1  christos      16 bit function, this points to the section containing the stub.  */
    348  1.1  christos   asection *fn_stub;
    349  1.1  christos 
    350  1.1  christos   /* If there is a stub that 16 bit functions should use to call this
    351  1.1  christos      32 bit function, this points to the section containing the stub.  */
    352  1.1  christos   asection *call_stub;
    353  1.1  christos 
    354  1.1  christos   /* This is like the call_stub field, but it is used if the function
    355  1.1  christos      being called returns a floating point value.  */
    356  1.1  christos   asection *call_fp_stub;
    357  1.1  christos 
    358  1.1  christos #define GOT_NORMAL	0
    359  1.1  christos #define GOT_TLS_GD	1
    360  1.1  christos #define GOT_TLS_LDM	2
    361  1.1  christos #define GOT_TLS_IE	4
    362  1.1  christos #define GOT_TLS_OFFSET_DONE    0x40
    363  1.1  christos #define GOT_TLS_DONE    0x80
    364  1.1  christos   unsigned char tls_type;
    365  1.1  christos 
    366  1.1  christos   /* This is only used in single-GOT mode; in multi-GOT mode there
    367  1.1  christos      is one mips_got_entry per GOT entry, so the offset is stored
    368  1.1  christos      there.  In single-GOT mode there may be many mips_got_entry
    369  1.1  christos      structures all referring to the same GOT slot.  It might be
    370  1.1  christos      possible to use root.got.offset instead, but that field is
    371  1.1  christos      overloaded already.  */
    372  1.1  christos   bfd_vma tls_got_offset;
    373  1.1  christos 
    374  1.1  christos   /* The highest GGA_* value that satisfies all references to this symbol.  */
    375  1.1  christos   unsigned int global_got_area : 2;
    376  1.1  christos 
    377  1.1  christos   /* True if all GOT relocations against this symbol are for calls.  This is
    378  1.1  christos      a looser condition than no_fn_stub below, because there may be other
    379  1.1  christos      non-call non-GOT relocations against the symbol.  */
    380  1.1  christos   unsigned int got_only_for_calls : 1;
    381  1.1  christos 
    382  1.1  christos   /* True if one of the relocations described by possibly_dynamic_relocs
    383  1.1  christos      is against a readonly section.  */
    384  1.1  christos   unsigned int readonly_reloc : 1;
    385  1.1  christos 
    386  1.1  christos   /* True if there is a relocation against this symbol that must be
    387  1.1  christos      resolved by the static linker (in other words, if the relocation
    388  1.1  christos      cannot possibly be made dynamic).  */
    389  1.1  christos   unsigned int has_static_relocs : 1;
    390  1.1  christos 
    391  1.1  christos   /* True if we must not create a .MIPS.stubs entry for this symbol.
    392  1.1  christos      This is set, for example, if there are relocations related to
    393  1.1  christos      taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
    394  1.1  christos      See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
    395  1.1  christos   unsigned int no_fn_stub : 1;
    396  1.1  christos 
    397  1.1  christos   /* Whether we need the fn_stub; this is true if this symbol appears
    398  1.1  christos      in any relocs other than a 16 bit call.  */
    399  1.1  christos   unsigned int need_fn_stub : 1;
    400  1.1  christos 
    401  1.1  christos   /* True if this symbol is referenced by branch relocations from
    402  1.1  christos      any non-PIC input file.  This is used to determine whether an
    403  1.1  christos      la25 stub is required.  */
    404  1.1  christos   unsigned int has_nonpic_branches : 1;
    405  1.1  christos 
    406  1.1  christos   /* Does this symbol need a traditional MIPS lazy-binding stub
    407  1.1  christos      (as opposed to a PLT entry)?  */
    408  1.1  christos   unsigned int needs_lazy_stub : 1;
    409  1.1  christos };
    410  1.1  christos 
    411  1.1  christos /* MIPS ELF linker hash table.  */
    412  1.1  christos 
    413  1.1  christos struct mips_elf_link_hash_table
    414  1.1  christos {
    415  1.1  christos   struct elf_link_hash_table root;
    416  1.1  christos #if 0
    417  1.1  christos   /* We no longer use this.  */
    418  1.1  christos   /* String section indices for the dynamic section symbols.  */
    419  1.1  christos   bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
    420  1.1  christos #endif
    421  1.1  christos 
    422  1.1  christos   /* The number of .rtproc entries.  */
    423  1.1  christos   bfd_size_type procedure_count;
    424  1.1  christos 
    425  1.1  christos   /* The size of the .compact_rel section (if SGI_COMPAT).  */
    426  1.1  christos   bfd_size_type compact_rel_size;
    427  1.1  christos 
    428  1.1  christos   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
    429  1.1  christos      entry is set to the address of __rld_obj_head as in IRIX5.  */
    430  1.1  christos   bfd_boolean use_rld_obj_head;
    431  1.1  christos 
    432  1.1  christos   /* This is the value of the __rld_map or __rld_obj_head symbol.  */
    433  1.1  christos   bfd_vma rld_value;
    434  1.1  christos 
    435  1.1  christos   /* This is set if we see any mips16 stub sections.  */
    436  1.1  christos   bfd_boolean mips16_stubs_seen;
    437  1.1  christos 
    438  1.1  christos   /* True if we can generate copy relocs and PLTs.  */
    439  1.1  christos   bfd_boolean use_plts_and_copy_relocs;
    440  1.1  christos 
    441  1.1  christos   /* True if we're generating code for VxWorks.  */
    442  1.1  christos   bfd_boolean is_vxworks;
    443  1.1  christos 
    444  1.1  christos   /* True if we already reported the small-data section overflow.  */
    445  1.1  christos   bfd_boolean small_data_overflow_reported;
    446  1.1  christos 
    447  1.1  christos   /* Shortcuts to some dynamic sections, or NULL if they are not
    448  1.1  christos      being used.  */
    449  1.1  christos   asection *srelbss;
    450  1.1  christos   asection *sdynbss;
    451  1.1  christos   asection *srelplt;
    452  1.1  christos   asection *srelplt2;
    453  1.1  christos   asection *sgotplt;
    454  1.1  christos   asection *splt;
    455  1.1  christos   asection *sstubs;
    456  1.1  christos   asection *sgot;
    457  1.1  christos 
    458  1.1  christos   /* The master GOT information.  */
    459  1.1  christos   struct mips_got_info *got_info;
    460  1.1  christos 
    461  1.1  christos   /* The size of the PLT header in bytes.  */
    462  1.1  christos   bfd_vma plt_header_size;
    463  1.1  christos 
    464  1.1  christos   /* The size of a PLT entry in bytes.  */
    465  1.1  christos   bfd_vma plt_entry_size;
    466  1.1  christos 
    467  1.1  christos   /* The number of functions that need a lazy-binding stub.  */
    468  1.1  christos   bfd_vma lazy_stub_count;
    469  1.1  christos 
    470  1.1  christos   /* The size of a function stub entry in bytes.  */
    471  1.1  christos   bfd_vma function_stub_size;
    472  1.1  christos 
    473  1.1  christos   /* The number of reserved entries at the beginning of the GOT.  */
    474  1.1  christos   unsigned int reserved_gotno;
    475  1.1  christos 
    476  1.1  christos   /* The section used for mips_elf_la25_stub trampolines.
    477  1.1  christos      See the comment above that structure for details.  */
    478  1.1  christos   asection *strampoline;
    479  1.1  christos 
    480  1.1  christos   /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
    481  1.1  christos      pairs.  */
    482  1.1  christos   htab_t la25_stubs;
    483  1.1  christos 
    484  1.1  christos   /* A function FN (NAME, IS, OS) that creates a new input section
    485  1.1  christos      called NAME and links it to output section OS.  If IS is nonnull,
    486  1.1  christos      the new section should go immediately before it, otherwise it
    487  1.1  christos      should go at the (current) beginning of OS.
    488  1.1  christos 
    489  1.1  christos      The function returns the new section on success, otherwise it
    490  1.1  christos      returns null.  */
    491  1.1  christos   asection *(*add_stub_section) (const char *, asection *, asection *);
    492  1.1  christos };
    493  1.1  christos 
    494  1.1  christos /* Get the MIPS ELF linker hash table from a link_info structure.  */
    495  1.1  christos 
    496  1.1  christos #define mips_elf_hash_table(p) \
    497  1.1  christos   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
    498  1.1  christos   == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
    499  1.1  christos 
    500  1.1  christos /* A structure used to communicate with htab_traverse callbacks.  */
    501  1.1  christos struct mips_htab_traverse_info
    502  1.1  christos {
    503  1.1  christos   /* The usual link-wide information.  */
    504  1.1  christos   struct bfd_link_info *info;
    505  1.1  christos   bfd *output_bfd;
    506  1.1  christos 
    507  1.1  christos   /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
    508  1.1  christos   bfd_boolean error;
    509  1.1  christos };
    510  1.1  christos 
    511  1.1  christos #define TLS_RELOC_P(r_type) \
    512  1.1  christos   (r_type == R_MIPS_TLS_DTPMOD32		\
    513  1.1  christos    || r_type == R_MIPS_TLS_DTPMOD64		\
    514  1.1  christos    || r_type == R_MIPS_TLS_DTPREL32		\
    515  1.1  christos    || r_type == R_MIPS_TLS_DTPREL64		\
    516  1.1  christos    || r_type == R_MIPS_TLS_GD			\
    517  1.1  christos    || r_type == R_MIPS_TLS_LDM			\
    518  1.1  christos    || r_type == R_MIPS_TLS_DTPREL_HI16		\
    519  1.1  christos    || r_type == R_MIPS_TLS_DTPREL_LO16		\
    520  1.1  christos    || r_type == R_MIPS_TLS_GOTTPREL		\
    521  1.1  christos    || r_type == R_MIPS_TLS_TPREL32		\
    522  1.1  christos    || r_type == R_MIPS_TLS_TPREL64		\
    523  1.1  christos    || r_type == R_MIPS_TLS_TPREL_HI16		\
    524  1.1  christos    || r_type == R_MIPS_TLS_TPREL_LO16)
    525  1.1  christos 
    526  1.1  christos /* Structure used to pass information to mips_elf_output_extsym.  */
    527  1.1  christos 
    528  1.1  christos struct extsym_info
    529  1.1  christos {
    530  1.1  christos   bfd *abfd;
    531  1.1  christos   struct bfd_link_info *info;
    532  1.1  christos   struct ecoff_debug_info *debug;
    533  1.1  christos   const struct ecoff_debug_swap *swap;
    534  1.1  christos   bfd_boolean failed;
    535  1.1  christos };
    536  1.1  christos 
    537  1.1  christos /* The names of the runtime procedure table symbols used on IRIX5.  */
    538  1.1  christos 
    539  1.1  christos static const char * const mips_elf_dynsym_rtproc_names[] =
    540  1.1  christos {
    541  1.1  christos   "_procedure_table",
    542  1.1  christos   "_procedure_string_table",
    543  1.1  christos   "_procedure_table_size",
    544  1.1  christos   NULL
    545  1.1  christos };
    546  1.1  christos 
    547  1.1  christos /* These structures are used to generate the .compact_rel section on
    548  1.1  christos    IRIX5.  */
    549  1.1  christos 
    550  1.1  christos typedef struct
    551  1.1  christos {
    552  1.1  christos   unsigned long id1;		/* Always one?  */
    553  1.1  christos   unsigned long num;		/* Number of compact relocation entries.  */
    554  1.1  christos   unsigned long id2;		/* Always two?  */
    555  1.1  christos   unsigned long offset;		/* The file offset of the first relocation.  */
    556  1.1  christos   unsigned long reserved0;	/* Zero?  */
    557  1.1  christos   unsigned long reserved1;	/* Zero?  */
    558  1.1  christos } Elf32_compact_rel;
    559  1.1  christos 
    560  1.1  christos typedef struct
    561  1.1  christos {
    562  1.1  christos   bfd_byte id1[4];
    563  1.1  christos   bfd_byte num[4];
    564  1.1  christos   bfd_byte id2[4];
    565  1.1  christos   bfd_byte offset[4];
    566  1.1  christos   bfd_byte reserved0[4];
    567  1.1  christos   bfd_byte reserved1[4];
    568  1.1  christos } Elf32_External_compact_rel;
    569  1.1  christos 
    570  1.1  christos typedef struct
    571  1.1  christos {
    572  1.1  christos   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
    573  1.1  christos   unsigned int rtype : 4;	/* Relocation types. See below.  */
    574  1.1  christos   unsigned int dist2to : 8;
    575  1.1  christos   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
    576  1.1  christos   unsigned long konst;		/* KONST field. See below.  */
    577  1.1  christos   unsigned long vaddr;		/* VADDR to be relocated.  */
    578  1.1  christos } Elf32_crinfo;
    579  1.1  christos 
    580  1.1  christos typedef struct
    581  1.1  christos {
    582  1.1  christos   unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
    583  1.1  christos   unsigned int rtype : 4;	/* Relocation types. See below.  */
    584  1.1  christos   unsigned int dist2to : 8;
    585  1.1  christos   unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
    586  1.1  christos   unsigned long konst;		/* KONST field. See below.  */
    587  1.1  christos } Elf32_crinfo2;
    588  1.1  christos 
    589  1.1  christos typedef struct
    590  1.1  christos {
    591  1.1  christos   bfd_byte info[4];
    592  1.1  christos   bfd_byte konst[4];
    593  1.1  christos   bfd_byte vaddr[4];
    594  1.1  christos } Elf32_External_crinfo;
    595  1.1  christos 
    596  1.1  christos typedef struct
    597  1.1  christos {
    598  1.1  christos   bfd_byte info[4];
    599  1.1  christos   bfd_byte konst[4];
    600  1.1  christos } Elf32_External_crinfo2;
    601  1.1  christos 
    602  1.1  christos /* These are the constants used to swap the bitfields in a crinfo.  */
    603  1.1  christos 
    604  1.1  christos #define CRINFO_CTYPE (0x1)
    605  1.1  christos #define CRINFO_CTYPE_SH (31)
    606  1.1  christos #define CRINFO_RTYPE (0xf)
    607  1.1  christos #define CRINFO_RTYPE_SH (27)
    608  1.1  christos #define CRINFO_DIST2TO (0xff)
    609  1.1  christos #define CRINFO_DIST2TO_SH (19)
    610  1.1  christos #define CRINFO_RELVADDR (0x7ffff)
    611  1.1  christos #define CRINFO_RELVADDR_SH (0)
    612  1.1  christos 
    613  1.1  christos /* A compact relocation info has long (3 words) or short (2 words)
    614  1.1  christos    formats.  A short format doesn't have VADDR field and relvaddr
    615  1.1  christos    fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
    616  1.1  christos #define CRF_MIPS_LONG			1
    617  1.1  christos #define CRF_MIPS_SHORT			0
    618  1.1  christos 
    619  1.1  christos /* There are 4 types of compact relocation at least. The value KONST
    620  1.1  christos    has different meaning for each type:
    621  1.1  christos 
    622  1.1  christos    (type)		(konst)
    623  1.1  christos    CT_MIPS_REL32	Address in data
    624  1.1  christos    CT_MIPS_WORD		Address in word (XXX)
    625  1.1  christos    CT_MIPS_GPHI_LO	GP - vaddr
    626  1.1  christos    CT_MIPS_JMPAD	Address to jump
    627  1.1  christos    */
    628  1.1  christos 
    629  1.1  christos #define CRT_MIPS_REL32			0xa
    630  1.1  christos #define CRT_MIPS_WORD			0xb
    631  1.1  christos #define CRT_MIPS_GPHI_LO		0xc
    632  1.1  christos #define CRT_MIPS_JMPAD			0xd
    633  1.1  christos 
    634  1.1  christos #define mips_elf_set_cr_format(x,format)	((x).ctype = (format))
    635  1.1  christos #define mips_elf_set_cr_type(x,type)		((x).rtype = (type))
    636  1.1  christos #define mips_elf_set_cr_dist2to(x,v)		((x).dist2to = (v))
    637  1.1  christos #define mips_elf_set_cr_relvaddr(x,d)		((x).relvaddr = (d)<<2)
    638  1.1  christos 
    639  1.1  christos /* The structure of the runtime procedure descriptor created by the
    641  1.1  christos    loader for use by the static exception system.  */
    642  1.1  christos 
    643  1.1  christos typedef struct runtime_pdr {
    644  1.1  christos 	bfd_vma	adr;		/* Memory address of start of procedure.  */
    645  1.1  christos 	long	regmask;	/* Save register mask.  */
    646  1.1  christos 	long	regoffset;	/* Save register offset.  */
    647  1.1  christos 	long	fregmask;	/* Save floating point register mask.  */
    648  1.1  christos 	long	fregoffset;	/* Save floating point register offset.  */
    649  1.1  christos 	long	frameoffset;	/* Frame size.  */
    650  1.1  christos 	short	framereg;	/* Frame pointer register.  */
    651  1.1  christos 	short	pcreg;		/* Offset or reg of return pc.  */
    652  1.1  christos 	long	irpss;		/* Index into the runtime string table.  */
    653  1.1  christos 	long	reserved;
    654  1.1  christos 	struct exception_info *exception_info;/* Pointer to exception array.  */
    655  1.1  christos } RPDR, *pRPDR;
    656  1.1  christos #define cbRPDR sizeof (RPDR)
    657  1.1  christos #define rpdNil ((pRPDR) 0)
    658  1.1  christos 
    659  1.1  christos static struct mips_got_entry *mips_elf_create_local_got_entry
    661  1.1  christos   (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
    662  1.1  christos    struct mips_elf_link_hash_entry *, int);
    663  1.1  christos static bfd_boolean mips_elf_sort_hash_table_f
    664  1.1  christos   (struct mips_elf_link_hash_entry *, void *);
    665  1.1  christos static bfd_vma mips_elf_high
    666  1.1  christos   (bfd_vma);
    667  1.1  christos static bfd_boolean mips_elf_create_dynamic_relocation
    668  1.1  christos   (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
    669  1.1  christos    struct mips_elf_link_hash_entry *, asection *, bfd_vma,
    670  1.1  christos    bfd_vma *, asection *);
    671  1.1  christos static hashval_t mips_elf_got_entry_hash
    672  1.1  christos   (const void *);
    673  1.1  christos static bfd_vma mips_elf_adjust_gp
    674  1.1  christos   (bfd *, struct mips_got_info *, bfd *);
    675  1.1  christos static struct mips_got_info *mips_elf_got_for_ibfd
    676  1.1  christos   (struct mips_got_info *, bfd *);
    677  1.1  christos 
    678  1.1  christos /* This will be used when we sort the dynamic relocation records.  */
    679  1.1  christos static bfd *reldyn_sorting_bfd;
    680  1.1  christos 
    681  1.1  christos /* True if ABFD is for CPUs with load interlocking that include
    682  1.1  christos    non-MIPS1 CPUs and R3900.  */
    683  1.1  christos #define LOAD_INTERLOCKS_P(abfd) \
    684  1.1  christos   (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
    685  1.1  christos    || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
    686  1.1  christos 
    687  1.1  christos /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
    688  1.1  christos    This should be safe for all architectures.  We enable this predicate
    689  1.1  christos    for RM9000 for now.  */
    690  1.1  christos #define JAL_TO_BAL_P(abfd) \
    691  1.1  christos   ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
    692  1.1  christos 
    693  1.1  christos /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
    694  1.1  christos    This should be safe for all architectures.  We enable this predicate for
    695  1.1  christos    all CPUs.  */
    696  1.1  christos #define JALR_TO_BAL_P(abfd) 1
    697  1.1  christos 
    698  1.1  christos /* True if ABFD is for CPUs that are faster if JR is converted to B.
    699  1.1  christos    This should be safe for all architectures.  We enable this predicate for
    700  1.1  christos    all CPUs.  */
    701  1.1  christos #define JR_TO_B_P(abfd) 1
    702  1.1  christos 
    703  1.1  christos /* True if ABFD is a PIC object.  */
    704  1.1  christos #define PIC_OBJECT_P(abfd) \
    705  1.1  christos   ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
    706  1.1  christos 
    707  1.1  christos /* Nonzero if ABFD is using the N32 ABI.  */
    708  1.1  christos #define ABI_N32_P(abfd) \
    709  1.1  christos   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
    710  1.1  christos 
    711  1.1  christos /* Nonzero if ABFD is using the N64 ABI.  */
    712  1.1  christos #define ABI_64_P(abfd) \
    713  1.1  christos   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
    714  1.1  christos 
    715  1.1  christos /* Nonzero if ABFD is using NewABI conventions.  */
    716  1.1  christos #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
    717  1.1  christos 
    718  1.1  christos /* The IRIX compatibility level we are striving for.  */
    719  1.1  christos #define IRIX_COMPAT(abfd) \
    720  1.1  christos   (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
    721  1.1  christos 
    722  1.1  christos /* Whether we are trying to be compatible with IRIX at all.  */
    723  1.1  christos #define SGI_COMPAT(abfd) \
    724  1.1  christos   (IRIX_COMPAT (abfd) != ict_none)
    725  1.1  christos 
    726  1.1  christos /* The name of the options section.  */
    727  1.1  christos #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
    728  1.1  christos   (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
    729  1.1  christos 
    730  1.1  christos /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
    731  1.1  christos    Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
    732  1.1  christos #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
    733  1.1  christos   (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
    734  1.1  christos 
    735  1.1  christos /* Whether the section is readonly.  */
    736  1.1  christos #define MIPS_ELF_READONLY_SECTION(sec) \
    737  1.1  christos   ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))		\
    738  1.1  christos    == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
    739  1.1  christos 
    740  1.1  christos /* The name of the stub section.  */
    741  1.1  christos #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
    742  1.1  christos 
    743  1.1  christos /* The size of an external REL relocation.  */
    744  1.1  christos #define MIPS_ELF_REL_SIZE(abfd) \
    745  1.1  christos   (get_elf_backend_data (abfd)->s->sizeof_rel)
    746  1.1  christos 
    747  1.1  christos /* The size of an external RELA relocation.  */
    748  1.1  christos #define MIPS_ELF_RELA_SIZE(abfd) \
    749  1.1  christos   (get_elf_backend_data (abfd)->s->sizeof_rela)
    750  1.1  christos 
    751  1.1  christos /* The size of an external dynamic table entry.  */
    752  1.1  christos #define MIPS_ELF_DYN_SIZE(abfd) \
    753  1.1  christos   (get_elf_backend_data (abfd)->s->sizeof_dyn)
    754  1.1  christos 
    755  1.1  christos /* The size of a GOT entry.  */
    756  1.1  christos #define MIPS_ELF_GOT_SIZE(abfd) \
    757  1.1  christos   (get_elf_backend_data (abfd)->s->arch_size / 8)
    758  1.1  christos 
    759  1.1  christos /* The size of a symbol-table entry.  */
    760  1.1  christos #define MIPS_ELF_SYM_SIZE(abfd) \
    761  1.1  christos   (get_elf_backend_data (abfd)->s->sizeof_sym)
    762  1.1  christos 
    763  1.1  christos /* The default alignment for sections, as a power of two.  */
    764  1.1  christos #define MIPS_ELF_LOG_FILE_ALIGN(abfd)				\
    765  1.1  christos   (get_elf_backend_data (abfd)->s->log_file_align)
    766  1.1  christos 
    767  1.1  christos /* Get word-sized data.  */
    768  1.1  christos #define MIPS_ELF_GET_WORD(abfd, ptr) \
    769  1.1  christos   (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
    770  1.1  christos 
    771  1.1  christos /* Put out word-sized data.  */
    772  1.1  christos #define MIPS_ELF_PUT_WORD(abfd, val, ptr)	\
    773  1.1  christos   (ABI_64_P (abfd) 				\
    774  1.1  christos    ? bfd_put_64 (abfd, val, ptr) 		\
    775  1.1  christos    : bfd_put_32 (abfd, val, ptr))
    776  1.1  christos 
    777  1.1  christos /* The opcode for word-sized loads (LW or LD).  */
    778  1.1  christos #define MIPS_ELF_LOAD_WORD(abfd) \
    779  1.1  christos   (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
    780  1.1  christos 
    781  1.1  christos /* Add a dynamic symbol table-entry.  */
    782  1.1  christos #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)	\
    783  1.1  christos   _bfd_elf_add_dynamic_entry (info, tag, val)
    784  1.1  christos 
    785  1.1  christos #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)			\
    786  1.1  christos   (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
    787  1.1  christos 
    788  1.1  christos /* The name of the dynamic relocation section.  */
    789  1.1  christos #define MIPS_ELF_REL_DYN_NAME(INFO) \
    790  1.1  christos   (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
    791  1.1  christos 
    792  1.1  christos /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
    793  1.1  christos    from smaller values.  Start with zero, widen, *then* decrement.  */
    794  1.1  christos #define MINUS_ONE	(((bfd_vma)0) - 1)
    795  1.1  christos #define MINUS_TWO	(((bfd_vma)0) - 2)
    796  1.1  christos 
    797  1.1  christos /* The value to write into got[1] for SVR4 targets, to identify it is
    798  1.1  christos    a GNU object.  The dynamic linker can then use got[1] to store the
    799  1.1  christos    module pointer.  */
    800  1.1  christos #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
    801  1.1  christos   ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
    802  1.1  christos 
    803  1.1  christos /* The offset of $gp from the beginning of the .got section.  */
    804  1.1  christos #define ELF_MIPS_GP_OFFSET(INFO) \
    805  1.1  christos   (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
    806  1.1  christos 
    807  1.1  christos /* The maximum size of the GOT for it to be addressable using 16-bit
    808  1.1  christos    offsets from $gp.  */
    809  1.1  christos #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
    810  1.1  christos 
    811  1.1  christos /* Instructions which appear in a stub.  */
    812  1.1  christos #define STUB_LW(abfd)							\
    813  1.1  christos   ((ABI_64_P (abfd)							\
    814  1.1  christos     ? 0xdf998010				/* ld t9,0x8010(gp) */	\
    815  1.1  christos     : 0x8f998010))              		/* lw t9,0x8010(gp) */
    816  1.1  christos #define STUB_MOVE(abfd)							\
    817  1.1  christos    ((ABI_64_P (abfd)							\
    818  1.1  christos      ? 0x03e0782d				/* daddu t7,ra */	\
    819  1.1  christos      : 0x03e07821))				/* addu t7,ra */
    820  1.1  christos #define STUB_LUI(VAL) (0x3c180000 + (VAL))	/* lui t8,VAL */
    821  1.1  christos #define STUB_JALR 0x0320f809			/* jalr t9,ra */
    822  1.1  christos #define STUB_ORI(VAL) (0x37180000 + (VAL))	/* ori t8,t8,VAL */
    823  1.1  christos #define STUB_LI16U(VAL) (0x34180000 + (VAL))	/* ori t8,zero,VAL unsigned */
    824  1.1  christos #define STUB_LI16S(abfd, VAL)						\
    825  1.1  christos    ((ABI_64_P (abfd)							\
    826  1.1  christos     ? (0x64180000 + (VAL))	/* daddiu t8,zero,VAL sign extended */	\
    827  1.1  christos     : (0x24180000 + (VAL))))	/* addiu t8,zero,VAL sign extended */
    828  1.1  christos 
    829  1.1  christos #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
    830  1.1  christos #define MIPS_FUNCTION_STUB_BIG_SIZE 20
    831  1.1  christos 
    832  1.1  christos /* The name of the dynamic interpreter.  This is put in the .interp
    833  1.1  christos    section.  */
    834  1.1  christos 
    835  1.1  christos #define ELF_DYNAMIC_INTERPRETER(abfd) 		\
    836  1.1  christos    (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" 	\
    837  1.1  christos     : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" 	\
    838  1.1  christos     : "/usr/lib/libc.so.1")
    839  1.1  christos 
    840  1.1  christos #ifdef BFD64
    841  1.1  christos #define MNAME(bfd,pre,pos) \
    842  1.1  christos   (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
    843  1.1  christos #define ELF_R_SYM(bfd, i)					\
    844  1.1  christos   (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
    845  1.1  christos #define ELF_R_TYPE(bfd, i)					\
    846  1.1  christos   (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
    847  1.1  christos #define ELF_R_INFO(bfd, s, t)					\
    848  1.1  christos   (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
    849  1.1  christos #else
    850  1.1  christos #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
    851  1.1  christos #define ELF_R_SYM(bfd, i)					\
    852  1.1  christos   (ELF32_R_SYM (i))
    853  1.1  christos #define ELF_R_TYPE(bfd, i)					\
    854  1.1  christos   (ELF32_R_TYPE (i))
    855  1.1  christos #define ELF_R_INFO(bfd, s, t)					\
    856  1.1  christos   (ELF32_R_INFO (s, t))
    857  1.1  christos #endif
    858  1.1  christos 
    859  1.1  christos   /* The mips16 compiler uses a couple of special sections to handle
    861  1.1  christos      floating point arguments.
    862  1.1  christos 
    863  1.1  christos      Section names that look like .mips16.fn.FNNAME contain stubs that
    864  1.1  christos      copy floating point arguments from the fp regs to the gp regs and
    865  1.1  christos      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
    866  1.1  christos      call should be redirected to the stub instead.  If no 32 bit
    867  1.1  christos      function calls FNNAME, the stub should be discarded.  We need to
    868  1.1  christos      consider any reference to the function, not just a call, because
    869  1.1  christos      if the address of the function is taken we will need the stub,
    870  1.1  christos      since the address might be passed to a 32 bit function.
    871  1.1  christos 
    872  1.1  christos      Section names that look like .mips16.call.FNNAME contain stubs
    873  1.1  christos      that copy floating point arguments from the gp regs to the fp
    874  1.1  christos      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
    875  1.1  christos      then any 16 bit function that calls FNNAME should be redirected
    876  1.1  christos      to the stub instead.  If FNNAME is not a 32 bit function, the
    877  1.1  christos      stub should be discarded.
    878  1.1  christos 
    879  1.1  christos      .mips16.call.fp.FNNAME sections are similar, but contain stubs
    880  1.1  christos      which call FNNAME and then copy the return value from the fp regs
    881  1.1  christos      to the gp regs.  These stubs store the return value in $18 while
    882  1.1  christos      calling FNNAME; any function which might call one of these stubs
    883  1.1  christos      must arrange to save $18 around the call.  (This case is not
    884  1.1  christos      needed for 32 bit functions that call 16 bit functions, because
    885  1.1  christos      16 bit functions always return floating point values in both
    886  1.1  christos      $f0/$f1 and $2/$3.)
    887  1.1  christos 
    888  1.1  christos      Note that in all cases FNNAME might be defined statically.
    889  1.1  christos      Therefore, FNNAME is not used literally.  Instead, the relocation
    890  1.1  christos      information will indicate which symbol the section is for.
    891  1.1  christos 
    892  1.1  christos      We record any stubs that we find in the symbol table.  */
    893  1.1  christos 
    894  1.1  christos #define FN_STUB ".mips16.fn."
    895  1.1  christos #define CALL_STUB ".mips16.call."
    896  1.1  christos #define CALL_FP_STUB ".mips16.call.fp."
    897  1.1  christos 
    898  1.1  christos #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
    899  1.1  christos #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
    900  1.1  christos #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
    901  1.1  christos 
    902  1.1  christos /* The format of the first PLT entry in an O32 executable.  */
    904  1.1  christos static const bfd_vma mips_o32_exec_plt0_entry[] =
    905  1.1  christos {
    906  1.1  christos   0x3c1c0000,	/* lui $28, %hi(&GOTPLT[0])				*/
    907  1.1  christos   0x8f990000,	/* lw $25, %lo(&GOTPLT[0])($28)				*/
    908  1.1  christos   0x279c0000,	/* addiu $28, $28, %lo(&GOTPLT[0])			*/
    909  1.1  christos   0x031cc023,	/* subu $24, $24, $28					*/
    910  1.1  christos   0x03e07821,	/* move $15, $31					*/
    911  1.1  christos   0x0018c082,	/* srl $24, $24, 2					*/
    912  1.1  christos   0x0320f809,	/* jalr $25						*/
    913  1.1  christos   0x2718fffe	/* subu $24, $24, 2					*/
    914  1.1  christos };
    915  1.1  christos 
    916  1.1  christos /* The format of the first PLT entry in an N32 executable.  Different
    917  1.1  christos    because gp ($28) is not available; we use t2 ($14) instead.  */
    918  1.1  christos static const bfd_vma mips_n32_exec_plt0_entry[] =
    919  1.1  christos {
    920  1.1  christos   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
    921  1.1  christos   0x8dd90000,	/* lw $25, %lo(&GOTPLT[0])($14)				*/
    922  1.1  christos   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
    923  1.1  christos   0x030ec023,	/* subu $24, $24, $14					*/
    924  1.1  christos   0x03e07821,	/* move $15, $31					*/
    925  1.1  christos   0x0018c082,	/* srl $24, $24, 2					*/
    926  1.1  christos   0x0320f809,	/* jalr $25						*/
    927  1.1  christos   0x2718fffe	/* subu $24, $24, 2					*/
    928  1.1  christos };
    929  1.1  christos 
    930  1.1  christos /* The format of the first PLT entry in an N64 executable.  Different
    931  1.1  christos    from N32 because of the increased size of GOT entries.  */
    932  1.1  christos static const bfd_vma mips_n64_exec_plt0_entry[] =
    933  1.1  christos {
    934  1.1  christos   0x3c0e0000,	/* lui $14, %hi(&GOTPLT[0])				*/
    935  1.1  christos   0xddd90000,	/* ld $25, %lo(&GOTPLT[0])($14)				*/
    936  1.1  christos   0x25ce0000,	/* addiu $14, $14, %lo(&GOTPLT[0])			*/
    937  1.1  christos   0x030ec023,	/* subu $24, $24, $14					*/
    938  1.1  christos   0x03e07821,	/* move $15, $31					*/
    939  1.1  christos   0x0018c0c2,	/* srl $24, $24, 3					*/
    940  1.1  christos   0x0320f809,	/* jalr $25						*/
    941  1.1  christos   0x2718fffe	/* subu $24, $24, 2					*/
    942  1.1  christos };
    943  1.1  christos 
    944  1.1  christos /* The format of subsequent PLT entries.  */
    945  1.1  christos static const bfd_vma mips_exec_plt_entry[] =
    946  1.1  christos {
    947  1.1  christos   0x3c0f0000,	/* lui $15, %hi(.got.plt entry)			*/
    948  1.1  christos   0x01f90000,	/* l[wd] $25, %lo(.got.plt entry)($15)		*/
    949  1.1  christos   0x25f80000,	/* addiu $24, $15, %lo(.got.plt entry)		*/
    950  1.1  christos   0x03200008	/* jr $25					*/
    951  1.1  christos };
    952  1.1  christos 
    953  1.1  christos /* The format of the first PLT entry in a VxWorks executable.  */
    954  1.1  christos static const bfd_vma mips_vxworks_exec_plt0_entry[] =
    955  1.1  christos {
    956  1.1  christos   0x3c190000,	/* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)		*/
    957  1.1  christos   0x27390000,	/* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_)	*/
    958  1.1  christos   0x8f390008,	/* lw t9, 8(t9)					*/
    959  1.1  christos   0x00000000,	/* nop						*/
    960  1.1  christos   0x03200008,	/* jr t9					*/
    961  1.1  christos   0x00000000	/* nop						*/
    962  1.1  christos };
    963  1.1  christos 
    964  1.1  christos /* The format of subsequent PLT entries.  */
    965  1.1  christos static const bfd_vma mips_vxworks_exec_plt_entry[] =
    966  1.1  christos {
    967  1.1  christos   0x10000000,	/* b .PLT_resolver			*/
    968  1.1  christos   0x24180000,	/* li t8, <pltindex>			*/
    969  1.1  christos   0x3c190000,	/* lui t9, %hi(<.got.plt slot>)		*/
    970  1.1  christos   0x27390000,	/* addiu t9, t9, %lo(<.got.plt slot>)	*/
    971  1.1  christos   0x8f390000,	/* lw t9, 0(t9)				*/
    972  1.1  christos   0x00000000,	/* nop					*/
    973  1.1  christos   0x03200008,	/* jr t9				*/
    974  1.1  christos   0x00000000	/* nop					*/
    975  1.1  christos };
    976  1.1  christos 
    977  1.1  christos /* The format of the first PLT entry in a VxWorks shared object.  */
    978  1.1  christos static const bfd_vma mips_vxworks_shared_plt0_entry[] =
    979  1.1  christos {
    980  1.1  christos   0x8f990008,	/* lw t9, 8(gp)		*/
    981  1.1  christos   0x00000000,	/* nop			*/
    982  1.1  christos   0x03200008,	/* jr t9		*/
    983  1.1  christos   0x00000000,	/* nop			*/
    984  1.1  christos   0x00000000,	/* nop			*/
    985  1.1  christos   0x00000000	/* nop			*/
    986  1.1  christos };
    987  1.1  christos 
    988  1.1  christos /* The format of subsequent PLT entries.  */
    989  1.1  christos static const bfd_vma mips_vxworks_shared_plt_entry[] =
    990  1.1  christos {
    991  1.1  christos   0x10000000,	/* b .PLT_resolver	*/
    992  1.1  christos   0x24180000	/* li t8, <pltindex>	*/
    993  1.1  christos };
    994  1.1  christos 
    995  1.1  christos /* Look up an entry in a MIPS ELF linker hash table.  */
    997  1.1  christos 
    998  1.1  christos #define mips_elf_link_hash_lookup(table, string, create, copy, follow)	\
    999  1.1  christos   ((struct mips_elf_link_hash_entry *)					\
   1000  1.1  christos    elf_link_hash_lookup (&(table)->root, (string), (create),		\
   1001  1.1  christos 			 (copy), (follow)))
   1002  1.1  christos 
   1003  1.1  christos /* Traverse a MIPS ELF linker hash table.  */
   1004  1.1  christos 
   1005  1.1  christos #define mips_elf_link_hash_traverse(table, func, info)			\
   1006  1.1  christos   (elf_link_hash_traverse						\
   1007  1.1  christos    (&(table)->root,							\
   1008  1.1  christos     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),	\
   1009  1.1  christos     (info)))
   1010  1.1  christos 
   1011  1.1  christos /* Find the base offsets for thread-local storage in this object,
   1012  1.1  christos    for GD/LD and IE/LE respectively.  */
   1013  1.1  christos 
   1014  1.1  christos #define TP_OFFSET 0x7000
   1015  1.1  christos #define DTP_OFFSET 0x8000
   1016  1.1  christos 
   1017  1.1  christos static bfd_vma
   1018  1.1  christos dtprel_base (struct bfd_link_info *info)
   1019  1.1  christos {
   1020  1.1  christos   /* If tls_sec is NULL, we should have signalled an error already.  */
   1021  1.1  christos   if (elf_hash_table (info)->tls_sec == NULL)
   1022  1.1  christos     return 0;
   1023  1.1  christos   return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
   1024  1.1  christos }
   1025  1.1  christos 
   1026  1.1  christos static bfd_vma
   1027  1.1  christos tprel_base (struct bfd_link_info *info)
   1028  1.1  christos {
   1029  1.1  christos   /* If tls_sec is NULL, we should have signalled an error already.  */
   1030  1.1  christos   if (elf_hash_table (info)->tls_sec == NULL)
   1031  1.1  christos     return 0;
   1032  1.1  christos   return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
   1033  1.1  christos }
   1034  1.1  christos 
   1035  1.1  christos /* Create an entry in a MIPS ELF linker hash table.  */
   1036  1.1  christos 
   1037  1.1  christos static struct bfd_hash_entry *
   1038  1.1  christos mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   1039  1.1  christos 			    struct bfd_hash_table *table, const char *string)
   1040  1.1  christos {
   1041  1.1  christos   struct mips_elf_link_hash_entry *ret =
   1042  1.1  christos     (struct mips_elf_link_hash_entry *) entry;
   1043  1.1  christos 
   1044  1.1  christos   /* Allocate the structure if it has not already been allocated by a
   1045  1.1  christos      subclass.  */
   1046  1.1  christos   if (ret == NULL)
   1047  1.1  christos     ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
   1048  1.1  christos   if (ret == NULL)
   1049  1.1  christos     return (struct bfd_hash_entry *) ret;
   1050  1.1  christos 
   1051  1.1  christos   /* Call the allocation method of the superclass.  */
   1052  1.1  christos   ret = ((struct mips_elf_link_hash_entry *)
   1053  1.1  christos 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
   1054  1.1  christos 				     table, string));
   1055  1.1  christos   if (ret != NULL)
   1056  1.1  christos     {
   1057  1.1  christos       /* Set local fields.  */
   1058  1.1  christos       memset (&ret->esym, 0, sizeof (EXTR));
   1059  1.1  christos       /* We use -2 as a marker to indicate that the information has
   1060  1.1  christos 	 not been set.  -1 means there is no associated ifd.  */
   1061  1.1  christos       ret->esym.ifd = -2;
   1062  1.1  christos       ret->la25_stub = 0;
   1063  1.1  christos       ret->possibly_dynamic_relocs = 0;
   1064  1.1  christos       ret->fn_stub = NULL;
   1065  1.1  christos       ret->call_stub = NULL;
   1066  1.1  christos       ret->call_fp_stub = NULL;
   1067  1.1  christos       ret->tls_type = GOT_NORMAL;
   1068  1.1  christos       ret->global_got_area = GGA_NONE;
   1069  1.1  christos       ret->got_only_for_calls = TRUE;
   1070  1.1  christos       ret->readonly_reloc = FALSE;
   1071  1.1  christos       ret->has_static_relocs = FALSE;
   1072  1.1  christos       ret->no_fn_stub = FALSE;
   1073  1.1  christos       ret->need_fn_stub = FALSE;
   1074  1.1  christos       ret->has_nonpic_branches = FALSE;
   1075  1.1  christos       ret->needs_lazy_stub = FALSE;
   1076  1.1  christos     }
   1077  1.1  christos 
   1078  1.1  christos   return (struct bfd_hash_entry *) ret;
   1079  1.1  christos }
   1080  1.1  christos 
   1081  1.1  christos bfd_boolean
   1082  1.1  christos _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
   1083  1.1  christos {
   1084  1.1  christos   if (!sec->used_by_bfd)
   1085  1.1  christos     {
   1086  1.1  christos       struct _mips_elf_section_data *sdata;
   1087  1.1  christos       bfd_size_type amt = sizeof (*sdata);
   1088  1.1  christos 
   1089  1.1  christos       sdata = bfd_zalloc (abfd, amt);
   1090  1.1  christos       if (sdata == NULL)
   1091  1.1  christos 	return FALSE;
   1092  1.1  christos       sec->used_by_bfd = sdata;
   1093  1.1  christos     }
   1094  1.1  christos 
   1095  1.1  christos   return _bfd_elf_new_section_hook (abfd, sec);
   1096  1.1  christos }
   1097  1.1  christos 
   1098  1.1  christos /* Read ECOFF debugging information from a .mdebug section into a
   1100  1.1  christos    ecoff_debug_info structure.  */
   1101  1.1  christos 
   1102  1.1  christos bfd_boolean
   1103  1.1  christos _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
   1104  1.1  christos 			       struct ecoff_debug_info *debug)
   1105  1.1  christos {
   1106  1.1  christos   HDRR *symhdr;
   1107  1.1  christos   const struct ecoff_debug_swap *swap;
   1108  1.1  christos   char *ext_hdr;
   1109  1.1  christos 
   1110  1.1  christos   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   1111  1.1  christos   memset (debug, 0, sizeof (*debug));
   1112  1.1  christos 
   1113  1.1  christos   ext_hdr = bfd_malloc (swap->external_hdr_size);
   1114  1.1  christos   if (ext_hdr == NULL && swap->external_hdr_size != 0)
   1115  1.1  christos     goto error_return;
   1116  1.1  christos 
   1117  1.1  christos   if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
   1118  1.1  christos 				  swap->external_hdr_size))
   1119  1.1  christos     goto error_return;
   1120  1.1  christos 
   1121  1.1  christos   symhdr = &debug->symbolic_header;
   1122  1.1  christos   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
   1123  1.1  christos 
   1124  1.1  christos   /* The symbolic header contains absolute file offsets and sizes to
   1125  1.1  christos      read.  */
   1126  1.1  christos #define READ(ptr, offset, count, size, type)				\
   1127  1.1  christos   if (symhdr->count == 0)						\
   1128  1.1  christos     debug->ptr = NULL;							\
   1129  1.1  christos   else									\
   1130  1.1  christos     {									\
   1131  1.1  christos       bfd_size_type amt = (bfd_size_type) size * symhdr->count;		\
   1132  1.1  christos       debug->ptr = bfd_malloc (amt);					\
   1133  1.1  christos       if (debug->ptr == NULL)						\
   1134  1.1  christos 	goto error_return;						\
   1135  1.1  christos       if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0		\
   1136  1.1  christos 	  || bfd_bread (debug->ptr, amt, abfd) != amt)			\
   1137  1.1  christos 	goto error_return;						\
   1138  1.1  christos     }
   1139  1.1  christos 
   1140  1.1  christos   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
   1141  1.1  christos   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
   1142  1.1  christos   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
   1143  1.1  christos   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
   1144  1.1  christos   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
   1145  1.1  christos   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
   1146  1.1  christos 	union aux_ext *);
   1147  1.1  christos   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
   1148  1.1  christos   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
   1149  1.1  christos   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
   1150  1.1  christos   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
   1151  1.1  christos   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
   1152  1.1  christos #undef READ
   1153  1.1  christos 
   1154  1.1  christos   debug->fdr = NULL;
   1155  1.1  christos 
   1156  1.1  christos   return TRUE;
   1157  1.1  christos 
   1158  1.1  christos  error_return:
   1159  1.1  christos   if (ext_hdr != NULL)
   1160  1.1  christos     free (ext_hdr);
   1161  1.1  christos   if (debug->line != NULL)
   1162  1.1  christos     free (debug->line);
   1163  1.1  christos   if (debug->external_dnr != NULL)
   1164  1.1  christos     free (debug->external_dnr);
   1165  1.1  christos   if (debug->external_pdr != NULL)
   1166  1.1  christos     free (debug->external_pdr);
   1167  1.1  christos   if (debug->external_sym != NULL)
   1168  1.1  christos     free (debug->external_sym);
   1169  1.1  christos   if (debug->external_opt != NULL)
   1170  1.1  christos     free (debug->external_opt);
   1171  1.1  christos   if (debug->external_aux != NULL)
   1172  1.1  christos     free (debug->external_aux);
   1173  1.1  christos   if (debug->ss != NULL)
   1174  1.1  christos     free (debug->ss);
   1175  1.1  christos   if (debug->ssext != NULL)
   1176  1.1  christos     free (debug->ssext);
   1177  1.1  christos   if (debug->external_fdr != NULL)
   1178  1.1  christos     free (debug->external_fdr);
   1179  1.1  christos   if (debug->external_rfd != NULL)
   1180  1.1  christos     free (debug->external_rfd);
   1181  1.1  christos   if (debug->external_ext != NULL)
   1182  1.1  christos     free (debug->external_ext);
   1183  1.1  christos   return FALSE;
   1184  1.1  christos }
   1185  1.1  christos 
   1186  1.1  christos /* Swap RPDR (runtime procedure table entry) for output.  */
   1188  1.1  christos 
   1189  1.1  christos static void
   1190  1.1  christos ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
   1191  1.1  christos {
   1192  1.1  christos   H_PUT_S32 (abfd, in->adr, ex->p_adr);
   1193  1.1  christos   H_PUT_32 (abfd, in->regmask, ex->p_regmask);
   1194  1.1  christos   H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
   1195  1.1  christos   H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
   1196  1.1  christos   H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
   1197  1.1  christos   H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
   1198  1.1  christos 
   1199  1.1  christos   H_PUT_16 (abfd, in->framereg, ex->p_framereg);
   1200  1.1  christos   H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
   1201  1.1  christos 
   1202  1.1  christos   H_PUT_32 (abfd, in->irpss, ex->p_irpss);
   1203  1.1  christos }
   1204  1.1  christos 
   1205  1.1  christos /* Create a runtime procedure table from the .mdebug section.  */
   1206  1.1  christos 
   1207  1.1  christos static bfd_boolean
   1208  1.1  christos mips_elf_create_procedure_table (void *handle, bfd *abfd,
   1209  1.1  christos 				 struct bfd_link_info *info, asection *s,
   1210  1.1  christos 				 struct ecoff_debug_info *debug)
   1211  1.1  christos {
   1212  1.1  christos   const struct ecoff_debug_swap *swap;
   1213  1.1  christos   HDRR *hdr = &debug->symbolic_header;
   1214  1.1  christos   RPDR *rpdr, *rp;
   1215  1.1  christos   struct rpdr_ext *erp;
   1216  1.1  christos   void *rtproc;
   1217  1.1  christos   struct pdr_ext *epdr;
   1218  1.1  christos   struct sym_ext *esym;
   1219  1.1  christos   char *ss, **sv;
   1220  1.1  christos   char *str;
   1221  1.1  christos   bfd_size_type size;
   1222  1.1  christos   bfd_size_type count;
   1223  1.1  christos   unsigned long sindex;
   1224  1.1  christos   unsigned long i;
   1225  1.1  christos   PDR pdr;
   1226  1.1  christos   SYMR sym;
   1227  1.1  christos   const char *no_name_func = _("static procedure (no name)");
   1228  1.1  christos 
   1229  1.1  christos   epdr = NULL;
   1230  1.1  christos   rpdr = NULL;
   1231  1.1  christos   esym = NULL;
   1232  1.1  christos   ss = NULL;
   1233  1.1  christos   sv = NULL;
   1234  1.1  christos 
   1235  1.1  christos   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   1236  1.1  christos 
   1237  1.1  christos   sindex = strlen (no_name_func) + 1;
   1238  1.1  christos   count = hdr->ipdMax;
   1239  1.1  christos   if (count > 0)
   1240  1.1  christos     {
   1241  1.1  christos       size = swap->external_pdr_size;
   1242  1.1  christos 
   1243  1.1  christos       epdr = bfd_malloc (size * count);
   1244  1.1  christos       if (epdr == NULL)
   1245  1.1  christos 	goto error_return;
   1246  1.1  christos 
   1247  1.1  christos       if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
   1248  1.1  christos 	goto error_return;
   1249  1.1  christos 
   1250  1.1  christos       size = sizeof (RPDR);
   1251  1.1  christos       rp = rpdr = bfd_malloc (size * count);
   1252  1.1  christos       if (rpdr == NULL)
   1253  1.1  christos 	goto error_return;
   1254  1.1  christos 
   1255  1.1  christos       size = sizeof (char *);
   1256  1.1  christos       sv = bfd_malloc (size * count);
   1257  1.1  christos       if (sv == NULL)
   1258  1.1  christos 	goto error_return;
   1259  1.1  christos 
   1260  1.1  christos       count = hdr->isymMax;
   1261  1.1  christos       size = swap->external_sym_size;
   1262  1.1  christos       esym = bfd_malloc (size * count);
   1263  1.1  christos       if (esym == NULL)
   1264  1.1  christos 	goto error_return;
   1265  1.1  christos 
   1266  1.1  christos       if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
   1267  1.1  christos 	goto error_return;
   1268  1.1  christos 
   1269  1.1  christos       count = hdr->issMax;
   1270  1.1  christos       ss = bfd_malloc (count);
   1271  1.1  christos       if (ss == NULL)
   1272  1.1  christos 	goto error_return;
   1273  1.1  christos       if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
   1274  1.1  christos 	goto error_return;
   1275  1.1  christos 
   1276  1.1  christos       count = hdr->ipdMax;
   1277  1.1  christos       for (i = 0; i < (unsigned long) count; i++, rp++)
   1278  1.1  christos 	{
   1279  1.1  christos 	  (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
   1280  1.1  christos 	  (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
   1281  1.1  christos 	  rp->adr = sym.value;
   1282  1.1  christos 	  rp->regmask = pdr.regmask;
   1283  1.1  christos 	  rp->regoffset = pdr.regoffset;
   1284  1.1  christos 	  rp->fregmask = pdr.fregmask;
   1285  1.1  christos 	  rp->fregoffset = pdr.fregoffset;
   1286  1.1  christos 	  rp->frameoffset = pdr.frameoffset;
   1287  1.1  christos 	  rp->framereg = pdr.framereg;
   1288  1.1  christos 	  rp->pcreg = pdr.pcreg;
   1289  1.1  christos 	  rp->irpss = sindex;
   1290  1.1  christos 	  sv[i] = ss + sym.iss;
   1291  1.1  christos 	  sindex += strlen (sv[i]) + 1;
   1292  1.1  christos 	}
   1293  1.1  christos     }
   1294  1.1  christos 
   1295  1.1  christos   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
   1296  1.1  christos   size = BFD_ALIGN (size, 16);
   1297  1.1  christos   rtproc = bfd_alloc (abfd, size);
   1298  1.1  christos   if (rtproc == NULL)
   1299  1.1  christos     {
   1300  1.1  christos       mips_elf_hash_table (info)->procedure_count = 0;
   1301  1.1  christos       goto error_return;
   1302  1.1  christos     }
   1303  1.1  christos 
   1304  1.1  christos   mips_elf_hash_table (info)->procedure_count = count + 2;
   1305  1.1  christos 
   1306  1.1  christos   erp = rtproc;
   1307  1.1  christos   memset (erp, 0, sizeof (struct rpdr_ext));
   1308  1.1  christos   erp++;
   1309  1.1  christos   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
   1310  1.1  christos   strcpy (str, no_name_func);
   1311  1.1  christos   str += strlen (no_name_func) + 1;
   1312  1.1  christos   for (i = 0; i < count; i++)
   1313  1.1  christos     {
   1314  1.1  christos       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
   1315  1.1  christos       strcpy (str, sv[i]);
   1316  1.1  christos       str += strlen (sv[i]) + 1;
   1317  1.1  christos     }
   1318  1.1  christos   H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
   1319  1.1  christos 
   1320  1.1  christos   /* Set the size and contents of .rtproc section.  */
   1321  1.1  christos   s->size = size;
   1322  1.1  christos   s->contents = rtproc;
   1323  1.1  christos 
   1324  1.1  christos   /* Skip this section later on (I don't think this currently
   1325  1.1  christos      matters, but someday it might).  */
   1326  1.1  christos   s->map_head.link_order = NULL;
   1327  1.1  christos 
   1328  1.1  christos   if (epdr != NULL)
   1329  1.1  christos     free (epdr);
   1330  1.1  christos   if (rpdr != NULL)
   1331  1.1  christos     free (rpdr);
   1332  1.1  christos   if (esym != NULL)
   1333  1.1  christos     free (esym);
   1334  1.1  christos   if (ss != NULL)
   1335  1.1  christos     free (ss);
   1336  1.1  christos   if (sv != NULL)
   1337  1.1  christos     free (sv);
   1338  1.1  christos 
   1339  1.1  christos   return TRUE;
   1340  1.1  christos 
   1341  1.1  christos  error_return:
   1342  1.1  christos   if (epdr != NULL)
   1343  1.1  christos     free (epdr);
   1344  1.1  christos   if (rpdr != NULL)
   1345  1.1  christos     free (rpdr);
   1346  1.1  christos   if (esym != NULL)
   1347  1.1  christos     free (esym);
   1348  1.1  christos   if (ss != NULL)
   1349  1.1  christos     free (ss);
   1350  1.1  christos   if (sv != NULL)
   1351  1.1  christos     free (sv);
   1352  1.1  christos   return FALSE;
   1353  1.1  christos }
   1354  1.1  christos 
   1355  1.1  christos /* We're going to create a stub for H.  Create a symbol for the stub's
   1357  1.1  christos    value and size, to help make the disassembly easier to read.  */
   1358  1.1  christos 
   1359  1.1  christos static bfd_boolean
   1360  1.1  christos mips_elf_create_stub_symbol (struct bfd_link_info *info,
   1361  1.1  christos 			     struct mips_elf_link_hash_entry *h,
   1362  1.1  christos 			     const char *prefix, asection *s, bfd_vma value,
   1363  1.1  christos 			     bfd_vma size)
   1364  1.1  christos {
   1365  1.1  christos   struct bfd_link_hash_entry *bh;
   1366  1.1  christos   struct elf_link_hash_entry *elfh;
   1367  1.1  christos   const char *name;
   1368  1.1  christos 
   1369  1.1  christos   /* Create a new symbol.  */
   1370  1.1  christos   name = ACONCAT ((prefix, h->root.root.root.string, NULL));
   1371  1.1  christos   bh = NULL;
   1372  1.1  christos   if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
   1373  1.1  christos 					 BSF_LOCAL, s, value, NULL,
   1374  1.1  christos 					 TRUE, FALSE, &bh))
   1375  1.1  christos     return FALSE;
   1376  1.1  christos 
   1377  1.1  christos   /* Make it a local function.  */
   1378  1.1  christos   elfh = (struct elf_link_hash_entry *) bh;
   1379  1.1  christos   elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   1380  1.1  christos   elfh->size = size;
   1381  1.1  christos   elfh->forced_local = 1;
   1382  1.1  christos   return TRUE;
   1383  1.1  christos }
   1384  1.1  christos 
   1385  1.1  christos /* We're about to redefine H.  Create a symbol to represent H's
   1386  1.1  christos    current value and size, to help make the disassembly easier
   1387  1.1  christos    to read.  */
   1388  1.1  christos 
   1389  1.1  christos static bfd_boolean
   1390  1.1  christos mips_elf_create_shadow_symbol (struct bfd_link_info *info,
   1391  1.1  christos 			       struct mips_elf_link_hash_entry *h,
   1392  1.1  christos 			       const char *prefix)
   1393  1.1  christos {
   1394  1.1  christos   struct bfd_link_hash_entry *bh;
   1395  1.1  christos   struct elf_link_hash_entry *elfh;
   1396  1.1  christos   const char *name;
   1397  1.1  christos   asection *s;
   1398  1.1  christos   bfd_vma value;
   1399  1.1  christos 
   1400  1.1  christos   /* Read the symbol's value.  */
   1401  1.1  christos   BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
   1402  1.1  christos 	      || h->root.root.type == bfd_link_hash_defweak);
   1403  1.1  christos   s = h->root.root.u.def.section;
   1404  1.1  christos   value = h->root.root.u.def.value;
   1405  1.1  christos 
   1406  1.1  christos   /* Create a new symbol.  */
   1407  1.1  christos   name = ACONCAT ((prefix, h->root.root.root.string, NULL));
   1408  1.1  christos   bh = NULL;
   1409  1.1  christos   if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
   1410  1.1  christos 					 BSF_LOCAL, s, value, NULL,
   1411  1.1  christos 					 TRUE, FALSE, &bh))
   1412  1.1  christos     return FALSE;
   1413  1.1  christos 
   1414  1.1  christos   /* Make it local and copy the other attributes from H.  */
   1415  1.1  christos   elfh = (struct elf_link_hash_entry *) bh;
   1416  1.1  christos   elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
   1417  1.1  christos   elfh->other = h->root.other;
   1418  1.1  christos   elfh->size = h->root.size;
   1419  1.1  christos   elfh->forced_local = 1;
   1420  1.1  christos   return TRUE;
   1421  1.1  christos }
   1422  1.1  christos 
   1423  1.1  christos /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
   1424  1.1  christos    function rather than to a hard-float stub.  */
   1425  1.1  christos 
   1426  1.1  christos static bfd_boolean
   1427  1.1  christos section_allows_mips16_refs_p (asection *section)
   1428  1.1  christos {
   1429  1.1  christos   const char *name;
   1430  1.1  christos 
   1431  1.1  christos   name = bfd_get_section_name (section->owner, section);
   1432  1.1  christos   return (FN_STUB_P (name)
   1433  1.1  christos 	  || CALL_STUB_P (name)
   1434  1.1  christos 	  || CALL_FP_STUB_P (name)
   1435  1.1  christos 	  || strcmp (name, ".pdr") == 0);
   1436  1.1  christos }
   1437  1.1  christos 
   1438  1.1  christos /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
   1439  1.1  christos    stub section of some kind.  Return the R_SYMNDX of the target
   1440  1.1  christos    function, or 0 if we can't decide which function that is.  */
   1441  1.1  christos 
   1442  1.1  christos static unsigned long
   1443  1.1  christos mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
   1444  1.1  christos 		    const Elf_Internal_Rela *relocs,
   1445  1.1  christos 		    const Elf_Internal_Rela *relend)
   1446  1.1  christos {
   1447  1.1  christos   const Elf_Internal_Rela *rel;
   1448  1.1  christos 
   1449  1.1  christos   /* Trust the first R_MIPS_NONE relocation, if any.  */
   1450  1.1  christos   for (rel = relocs; rel < relend; rel++)
   1451  1.1  christos     if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
   1452  1.1  christos       return ELF_R_SYM (sec->owner, rel->r_info);
   1453  1.1  christos 
   1454  1.1  christos   /* Otherwise trust the first relocation, whatever its kind.  This is
   1455  1.1  christos      the traditional behavior.  */
   1456  1.1  christos   if (relocs < relend)
   1457  1.1  christos     return ELF_R_SYM (sec->owner, relocs->r_info);
   1458  1.1  christos 
   1459  1.1  christos   return 0;
   1460  1.1  christos }
   1461  1.1  christos 
   1462  1.1  christos /* Check the mips16 stubs for a particular symbol, and see if we can
   1463  1.1  christos    discard them.  */
   1464  1.1  christos 
   1465  1.1  christos static void
   1466  1.1  christos mips_elf_check_mips16_stubs (struct bfd_link_info *info,
   1467  1.1  christos 			     struct mips_elf_link_hash_entry *h)
   1468  1.1  christos {
   1469  1.1  christos   /* Dynamic symbols must use the standard call interface, in case other
   1470  1.1  christos      objects try to call them.  */
   1471  1.1  christos   if (h->fn_stub != NULL
   1472  1.1  christos       && h->root.dynindx != -1)
   1473  1.1  christos     {
   1474  1.1  christos       mips_elf_create_shadow_symbol (info, h, ".mips16.");
   1475  1.1  christos       h->need_fn_stub = TRUE;
   1476  1.1  christos     }
   1477  1.1  christos 
   1478  1.1  christos   if (h->fn_stub != NULL
   1479  1.1  christos       && ! h->need_fn_stub)
   1480  1.1  christos     {
   1481  1.1  christos       /* We don't need the fn_stub; the only references to this symbol
   1482  1.1  christos          are 16 bit calls.  Clobber the size to 0 to prevent it from
   1483  1.1  christos          being included in the link.  */
   1484  1.1  christos       h->fn_stub->size = 0;
   1485  1.1  christos       h->fn_stub->flags &= ~SEC_RELOC;
   1486  1.1  christos       h->fn_stub->reloc_count = 0;
   1487  1.1  christos       h->fn_stub->flags |= SEC_EXCLUDE;
   1488  1.1  christos     }
   1489  1.1  christos 
   1490  1.1  christos   if (h->call_stub != NULL
   1491  1.1  christos       && ELF_ST_IS_MIPS16 (h->root.other))
   1492  1.1  christos     {
   1493  1.1  christos       /* We don't need the call_stub; this is a 16 bit function, so
   1494  1.1  christos          calls from other 16 bit functions are OK.  Clobber the size
   1495  1.1  christos          to 0 to prevent it from being included in the link.  */
   1496  1.1  christos       h->call_stub->size = 0;
   1497  1.1  christos       h->call_stub->flags &= ~SEC_RELOC;
   1498  1.1  christos       h->call_stub->reloc_count = 0;
   1499  1.1  christos       h->call_stub->flags |= SEC_EXCLUDE;
   1500  1.1  christos     }
   1501  1.1  christos 
   1502  1.1  christos   if (h->call_fp_stub != NULL
   1503  1.1  christos       && ELF_ST_IS_MIPS16 (h->root.other))
   1504  1.1  christos     {
   1505  1.1  christos       /* We don't need the call_stub; this is a 16 bit function, so
   1506  1.1  christos          calls from other 16 bit functions are OK.  Clobber the size
   1507  1.1  christos          to 0 to prevent it from being included in the link.  */
   1508  1.1  christos       h->call_fp_stub->size = 0;
   1509  1.1  christos       h->call_fp_stub->flags &= ~SEC_RELOC;
   1510  1.1  christos       h->call_fp_stub->reloc_count = 0;
   1511  1.1  christos       h->call_fp_stub->flags |= SEC_EXCLUDE;
   1512  1.1  christos     }
   1513  1.1  christos }
   1514  1.1  christos 
   1515  1.1  christos /* Hashtable callbacks for mips_elf_la25_stubs.  */
   1516  1.1  christos 
   1517  1.1  christos static hashval_t
   1518  1.1  christos mips_elf_la25_stub_hash (const void *entry_)
   1519  1.1  christos {
   1520  1.1  christos   const struct mips_elf_la25_stub *entry;
   1521  1.1  christos 
   1522  1.1  christos   entry = (struct mips_elf_la25_stub *) entry_;
   1523  1.1  christos   return entry->h->root.root.u.def.section->id
   1524  1.1  christos     + entry->h->root.root.u.def.value;
   1525  1.1  christos }
   1526  1.1  christos 
   1527  1.1  christos static int
   1528  1.1  christos mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
   1529  1.1  christos {
   1530  1.1  christos   const struct mips_elf_la25_stub *entry1, *entry2;
   1531  1.1  christos 
   1532  1.1  christos   entry1 = (struct mips_elf_la25_stub *) entry1_;
   1533  1.1  christos   entry2 = (struct mips_elf_la25_stub *) entry2_;
   1534  1.1  christos   return ((entry1->h->root.root.u.def.section
   1535  1.1  christos 	   == entry2->h->root.root.u.def.section)
   1536  1.1  christos 	  && (entry1->h->root.root.u.def.value
   1537  1.1  christos 	      == entry2->h->root.root.u.def.value));
   1538  1.1  christos }
   1539  1.1  christos 
   1540  1.1  christos /* Called by the linker to set up the la25 stub-creation code.  FN is
   1541  1.1  christos    the linker's implementation of add_stub_function.  Return true on
   1542  1.1  christos    success.  */
   1543  1.1  christos 
   1544  1.1  christos bfd_boolean
   1545  1.1  christos _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
   1546  1.1  christos 			  asection *(*fn) (const char *, asection *,
   1547  1.1  christos 					   asection *))
   1548  1.1  christos {
   1549  1.1  christos   struct mips_elf_link_hash_table *htab;
   1550  1.1  christos 
   1551  1.1  christos   htab = mips_elf_hash_table (info);
   1552  1.1  christos   if (htab == NULL)
   1553  1.1  christos     return FALSE;
   1554  1.1  christos 
   1555  1.1  christos   htab->add_stub_section = fn;
   1556  1.1  christos   htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
   1557  1.1  christos 				      mips_elf_la25_stub_eq, NULL);
   1558  1.1  christos   if (htab->la25_stubs == NULL)
   1559  1.1  christos     return FALSE;
   1560  1.1  christos 
   1561  1.1  christos   return TRUE;
   1562  1.1  christos }
   1563  1.1  christos 
   1564  1.1  christos /* Return true if H is a locally-defined PIC function, in the sense
   1565  1.1  christos    that it might need $25 to be valid on entry.  Note that MIPS16
   1566  1.1  christos    functions never need $25 to be valid on entry; they set up $gp
   1567  1.1  christos    using PC-relative instructions instead.  */
   1568  1.1  christos 
   1569  1.1  christos static bfd_boolean
   1570  1.1  christos mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
   1571  1.1  christos {
   1572  1.1  christos   return ((h->root.root.type == bfd_link_hash_defined
   1573  1.1  christos 	   || h->root.root.type == bfd_link_hash_defweak)
   1574  1.1  christos 	  && h->root.def_regular
   1575  1.1  christos 	  && !bfd_is_abs_section (h->root.root.u.def.section)
   1576  1.1  christos 	  && !ELF_ST_IS_MIPS16 (h->root.other)
   1577  1.1  christos 	  && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
   1578  1.1  christos 	      || ELF_ST_IS_MIPS_PIC (h->root.other)));
   1579  1.1  christos }
   1580  1.1  christos 
   1581  1.1  christos /* STUB describes an la25 stub that we have decided to implement
   1582  1.1  christos    by inserting an LUI/ADDIU pair before the target function.
   1583  1.1  christos    Create the section and redirect the function symbol to it.  */
   1584  1.1  christos 
   1585  1.1  christos static bfd_boolean
   1586  1.1  christos mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
   1587  1.1  christos 			 struct bfd_link_info *info)
   1588  1.1  christos {
   1589  1.1  christos   struct mips_elf_link_hash_table *htab;
   1590  1.1  christos   char *name;
   1591  1.1  christos   asection *s, *input_section;
   1592  1.1  christos   unsigned int align;
   1593  1.1  christos 
   1594  1.1  christos   htab = mips_elf_hash_table (info);
   1595  1.1  christos   if (htab == NULL)
   1596  1.1  christos     return FALSE;
   1597  1.1  christos 
   1598  1.1  christos   /* Create a unique name for the new section.  */
   1599  1.1  christos   name = bfd_malloc (11 + sizeof (".text.stub."));
   1600  1.1  christos   if (name == NULL)
   1601  1.1  christos     return FALSE;
   1602  1.1  christos   sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
   1603  1.1  christos 
   1604  1.1  christos   /* Create the section.  */
   1605  1.1  christos   input_section = stub->h->root.root.u.def.section;
   1606  1.1  christos   s = htab->add_stub_section (name, input_section,
   1607  1.1  christos 			      input_section->output_section);
   1608  1.1  christos   if (s == NULL)
   1609  1.1  christos     return FALSE;
   1610  1.1  christos 
   1611  1.1  christos   /* Make sure that any padding goes before the stub.  */
   1612  1.1  christos   align = input_section->alignment_power;
   1613  1.1  christos   if (!bfd_set_section_alignment (s->owner, s, align))
   1614  1.1  christos     return FALSE;
   1615  1.1  christos   if (align > 3)
   1616  1.1  christos     s->size = (1 << align) - 8;
   1617  1.1  christos 
   1618  1.1  christos   /* Create a symbol for the stub.  */
   1619  1.1  christos   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
   1620  1.1  christos   stub->stub_section = s;
   1621  1.1  christos   stub->offset = s->size;
   1622  1.1  christos 
   1623  1.1  christos   /* Allocate room for it.  */
   1624  1.1  christos   s->size += 8;
   1625  1.1  christos   return TRUE;
   1626  1.1  christos }
   1627  1.1  christos 
   1628  1.1  christos /* STUB describes an la25 stub that we have decided to implement
   1629  1.1  christos    with a separate trampoline.  Allocate room for it and redirect
   1630  1.1  christos    the function symbol to it.  */
   1631  1.1  christos 
   1632  1.1  christos static bfd_boolean
   1633  1.1  christos mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
   1634  1.1  christos 			      struct bfd_link_info *info)
   1635  1.1  christos {
   1636  1.1  christos   struct mips_elf_link_hash_table *htab;
   1637  1.1  christos   asection *s;
   1638  1.1  christos 
   1639  1.1  christos   htab = mips_elf_hash_table (info);
   1640  1.1  christos   if (htab == NULL)
   1641  1.1  christos     return FALSE;
   1642  1.1  christos 
   1643  1.1  christos   /* Create a trampoline section, if we haven't already.  */
   1644  1.1  christos   s = htab->strampoline;
   1645  1.1  christos   if (s == NULL)
   1646  1.1  christos     {
   1647  1.1  christos       asection *input_section = stub->h->root.root.u.def.section;
   1648  1.1  christos       s = htab->add_stub_section (".text", NULL,
   1649  1.1  christos 				  input_section->output_section);
   1650  1.1  christos       if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
   1651  1.1  christos 	return FALSE;
   1652  1.1  christos       htab->strampoline = s;
   1653  1.1  christos     }
   1654  1.1  christos 
   1655  1.1  christos   /* Create a symbol for the stub.  */
   1656  1.1  christos   mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
   1657  1.1  christos   stub->stub_section = s;
   1658  1.1  christos   stub->offset = s->size;
   1659  1.1  christos 
   1660  1.1  christos   /* Allocate room for it.  */
   1661  1.1  christos   s->size += 16;
   1662  1.1  christos   return TRUE;
   1663  1.1  christos }
   1664  1.1  christos 
   1665  1.1  christos /* H describes a symbol that needs an la25 stub.  Make sure that an
   1666  1.1  christos    appropriate stub exists and point H at it.  */
   1667  1.1  christos 
   1668  1.1  christos static bfd_boolean
   1669  1.1  christos mips_elf_add_la25_stub (struct bfd_link_info *info,
   1670  1.1  christos 			struct mips_elf_link_hash_entry *h)
   1671  1.1  christos {
   1672  1.1  christos   struct mips_elf_link_hash_table *htab;
   1673  1.1  christos   struct mips_elf_la25_stub search, *stub;
   1674  1.1  christos   bfd_boolean use_trampoline_p;
   1675  1.1  christos   asection *s;
   1676  1.1  christos   bfd_vma value;
   1677  1.1  christos   void **slot;
   1678  1.1  christos 
   1679  1.1  christos   /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
   1680  1.1  christos      of the section and if we would need no more than 2 nops.  */
   1681  1.1  christos   s = h->root.root.u.def.section;
   1682  1.1  christos   value = h->root.root.u.def.value;
   1683  1.1  christos   use_trampoline_p = (value != 0 || s->alignment_power > 4);
   1684  1.1  christos 
   1685  1.1  christos   /* Describe the stub we want.  */
   1686  1.1  christos   search.stub_section = NULL;
   1687  1.1  christos   search.offset = 0;
   1688  1.1  christos   search.h = h;
   1689  1.1  christos 
   1690  1.1  christos   /* See if we've already created an equivalent stub.  */
   1691  1.1  christos   htab = mips_elf_hash_table (info);
   1692  1.1  christos   if (htab == NULL)
   1693  1.1  christos     return FALSE;
   1694  1.1  christos 
   1695  1.1  christos   slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
   1696  1.1  christos   if (slot == NULL)
   1697  1.1  christos     return FALSE;
   1698  1.1  christos 
   1699  1.1  christos   stub = (struct mips_elf_la25_stub *) *slot;
   1700  1.1  christos   if (stub != NULL)
   1701  1.1  christos     {
   1702  1.1  christos       /* We can reuse the existing stub.  */
   1703  1.1  christos       h->la25_stub = stub;
   1704  1.1  christos       return TRUE;
   1705  1.1  christos     }
   1706  1.1  christos 
   1707  1.1  christos   /* Create a permanent copy of ENTRY and add it to the hash table.  */
   1708  1.1  christos   stub = bfd_malloc (sizeof (search));
   1709  1.1  christos   if (stub == NULL)
   1710  1.1  christos     return FALSE;
   1711  1.1  christos   *stub = search;
   1712  1.1  christos   *slot = stub;
   1713  1.1  christos 
   1714  1.1  christos   h->la25_stub = stub;
   1715  1.1  christos   return (use_trampoline_p
   1716  1.1  christos 	  ? mips_elf_add_la25_trampoline (stub, info)
   1717  1.1  christos 	  : mips_elf_add_la25_intro (stub, info));
   1718  1.1  christos }
   1719  1.1  christos 
   1720  1.1  christos /* A mips_elf_link_hash_traverse callback that is called before sizing
   1721  1.1  christos    sections.  DATA points to a mips_htab_traverse_info structure.  */
   1722  1.1  christos 
   1723  1.1  christos static bfd_boolean
   1724  1.1  christos mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
   1725  1.1  christos {
   1726  1.1  christos   struct mips_htab_traverse_info *hti;
   1727  1.1  christos 
   1728  1.1  christos   hti = (struct mips_htab_traverse_info *) data;
   1729  1.1  christos   if (h->root.root.type == bfd_link_hash_warning)
   1730  1.1  christos     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   1731  1.1  christos 
   1732  1.1  christos   if (!hti->info->relocatable)
   1733  1.1  christos     mips_elf_check_mips16_stubs (hti->info, h);
   1734  1.1  christos 
   1735  1.1  christos   if (mips_elf_local_pic_function_p (h))
   1736  1.1  christos     {
   1737  1.1  christos       /* H is a function that might need $25 to be valid on entry.
   1738  1.1  christos 	 If we're creating a non-PIC relocatable object, mark H as
   1739  1.1  christos 	 being PIC.  If we're creating a non-relocatable object with
   1740  1.1  christos 	 non-PIC branches and jumps to H, make sure that H has an la25
   1741  1.1  christos 	 stub.  */
   1742  1.1  christos       if (hti->info->relocatable)
   1743  1.1  christos 	{
   1744  1.1  christos 	  if (!PIC_OBJECT_P (hti->output_bfd))
   1745  1.1  christos 	    h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
   1746  1.1  christos 	}
   1747  1.1  christos       else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
   1748  1.1  christos 	{
   1749  1.1  christos 	  hti->error = TRUE;
   1750  1.1  christos 	  return FALSE;
   1751  1.1  christos 	}
   1752  1.1  christos     }
   1753  1.1  christos   return TRUE;
   1754  1.1  christos }
   1755  1.1  christos 
   1756  1.1  christos /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
   1758  1.1  christos    Most mips16 instructions are 16 bits, but these instructions
   1759  1.1  christos    are 32 bits.
   1760  1.1  christos 
   1761  1.1  christos    The format of these instructions is:
   1762  1.1  christos 
   1763  1.1  christos    +--------------+--------------------------------+
   1764  1.1  christos    |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
   1765  1.1  christos    +--------------+--------------------------------+
   1766  1.1  christos    |                Immediate  15:0                |
   1767  1.1  christos    +-----------------------------------------------+
   1768  1.1  christos 
   1769  1.1  christos    JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
   1770  1.1  christos    Note that the immediate value in the first word is swapped.
   1771  1.1  christos 
   1772  1.1  christos    When producing a relocatable object file, R_MIPS16_26 is
   1773  1.1  christos    handled mostly like R_MIPS_26.  In particular, the addend is
   1774  1.1  christos    stored as a straight 26-bit value in a 32-bit instruction.
   1775  1.1  christos    (gas makes life simpler for itself by never adjusting a
   1776  1.1  christos    R_MIPS16_26 reloc to be against a section, so the addend is
   1777  1.1  christos    always zero).  However, the 32 bit instruction is stored as 2
   1778  1.1  christos    16-bit values, rather than a single 32-bit value.  In a
   1779  1.1  christos    big-endian file, the result is the same; in a little-endian
   1780  1.1  christos    file, the two 16-bit halves of the 32 bit value are swapped.
   1781  1.1  christos    This is so that a disassembler can recognize the jal
   1782  1.1  christos    instruction.
   1783  1.1  christos 
   1784  1.1  christos    When doing a final link, R_MIPS16_26 is treated as a 32 bit
   1785  1.1  christos    instruction stored as two 16-bit values.  The addend A is the
   1786  1.1  christos    contents of the targ26 field.  The calculation is the same as
   1787  1.1  christos    R_MIPS_26.  When storing the calculated value, reorder the
   1788  1.1  christos    immediate value as shown above, and don't forget to store the
   1789  1.1  christos    value as two 16-bit values.
   1790  1.1  christos 
   1791  1.1  christos    To put it in MIPS ABI terms, the relocation field is T-targ26-16,
   1792  1.1  christos    defined as
   1793  1.1  christos 
   1794  1.1  christos    big-endian:
   1795  1.1  christos    +--------+----------------------+
   1796  1.1  christos    |        |                      |
   1797  1.1  christos    |        |    targ26-16         |
   1798  1.1  christos    |31    26|25                   0|
   1799  1.1  christos    +--------+----------------------+
   1800  1.1  christos 
   1801  1.1  christos    little-endian:
   1802  1.1  christos    +----------+------+-------------+
   1803  1.1  christos    |          |      |             |
   1804  1.1  christos    |  sub1    |      |     sub2    |
   1805  1.1  christos    |0        9|10  15|16         31|
   1806  1.1  christos    +----------+--------------------+
   1807  1.1  christos    where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
   1808  1.1  christos    ((sub1 << 16) | sub2)).
   1809  1.1  christos 
   1810  1.1  christos    When producing a relocatable object file, the calculation is
   1811  1.1  christos    (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
   1812  1.1  christos    When producing a fully linked file, the calculation is
   1813  1.1  christos    let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
   1814  1.1  christos    ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
   1815  1.1  christos 
   1816  1.1  christos    The table below lists the other MIPS16 instruction relocations.
   1817  1.1  christos    Each one is calculated in the same way as the non-MIPS16 relocation
   1818  1.1  christos    given on the right, but using the extended MIPS16 layout of 16-bit
   1819  1.1  christos    immediate fields:
   1820  1.1  christos 
   1821  1.1  christos 	R_MIPS16_GPREL		R_MIPS_GPREL16
   1822  1.1  christos 	R_MIPS16_GOT16		R_MIPS_GOT16
   1823  1.1  christos 	R_MIPS16_CALL16		R_MIPS_CALL16
   1824  1.1  christos 	R_MIPS16_HI16		R_MIPS_HI16
   1825  1.1  christos 	R_MIPS16_LO16		R_MIPS_LO16
   1826  1.1  christos 
   1827  1.1  christos    A typical instruction will have a format like this:
   1828  1.1  christos 
   1829  1.1  christos    +--------------+--------------------------------+
   1830  1.1  christos    |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
   1831  1.1  christos    +--------------+--------------------------------+
   1832  1.1  christos    |    Major     |   rx   |   ry   |   Imm  4:0   |
   1833  1.1  christos    +--------------+--------------------------------+
   1834  1.1  christos 
   1835  1.1  christos    EXTEND is the five bit value 11110.  Major is the instruction
   1836  1.1  christos    opcode.
   1837  1.1  christos 
   1838  1.1  christos    All we need to do here is shuffle the bits appropriately.
   1839  1.1  christos    As above, the two 16-bit halves must be swapped on a
   1840  1.1  christos    little-endian system.  */
   1841  1.1  christos 
   1842  1.1  christos static inline bfd_boolean
   1843  1.1  christos mips16_reloc_p (int r_type)
   1844  1.1  christos {
   1845  1.1  christos   switch (r_type)
   1846  1.1  christos     {
   1847  1.1  christos     case R_MIPS16_26:
   1848  1.1  christos     case R_MIPS16_GPREL:
   1849  1.1  christos     case R_MIPS16_GOT16:
   1850  1.1  christos     case R_MIPS16_CALL16:
   1851  1.1  christos     case R_MIPS16_HI16:
   1852  1.1  christos     case R_MIPS16_LO16:
   1853  1.1  christos       return TRUE;
   1854  1.1  christos 
   1855  1.1  christos     default:
   1856  1.1  christos       return FALSE;
   1857  1.1  christos     }
   1858  1.1  christos }
   1859  1.1  christos 
   1860  1.1  christos static inline bfd_boolean
   1861  1.1  christos got16_reloc_p (int r_type)
   1862  1.1  christos {
   1863  1.1  christos   return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
   1864  1.1  christos }
   1865  1.1  christos 
   1866  1.1  christos static inline bfd_boolean
   1867  1.1  christos call16_reloc_p (int r_type)
   1868  1.1  christos {
   1869  1.1  christos   return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
   1870  1.1  christos }
   1871  1.1  christos 
   1872  1.1  christos static inline bfd_boolean
   1873  1.1  christos hi16_reloc_p (int r_type)
   1874  1.1  christos {
   1875  1.1  christos   return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
   1876  1.1  christos }
   1877  1.1  christos 
   1878  1.1  christos static inline bfd_boolean
   1879  1.1  christos lo16_reloc_p (int r_type)
   1880  1.1  christos {
   1881  1.1  christos   return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
   1882  1.1  christos }
   1883  1.1  christos 
   1884  1.1  christos static inline bfd_boolean
   1885  1.1  christos mips16_call_reloc_p (int r_type)
   1886  1.1  christos {
   1887  1.1  christos   return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
   1888  1.1  christos }
   1889  1.1  christos 
   1890  1.1  christos static inline bfd_boolean
   1891  1.1  christos jal_reloc_p (int r_type)
   1892  1.1  christos {
   1893  1.1  christos   return r_type == R_MIPS_26 || r_type == R_MIPS16_26;
   1894  1.1  christos }
   1895  1.1  christos 
   1896  1.1  christos void
   1897  1.1  christos _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
   1898  1.1  christos 				 bfd_boolean jal_shuffle, bfd_byte *data)
   1899  1.1  christos {
   1900  1.1  christos   bfd_vma extend, insn, val;
   1901  1.1  christos 
   1902  1.1  christos   if (!mips16_reloc_p (r_type))
   1903  1.1  christos     return;
   1904  1.1  christos 
   1905  1.1  christos   /* Pick up the mips16 extend instruction and the real instruction.  */
   1906  1.1  christos   extend = bfd_get_16 (abfd, data);
   1907  1.1  christos   insn = bfd_get_16 (abfd, data + 2);
   1908  1.1  christos   if (r_type == R_MIPS16_26)
   1909  1.1  christos     {
   1910  1.1  christos       if (jal_shuffle)
   1911  1.1  christos 	val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
   1912  1.1  christos 	      | ((extend & 0x1f) << 21) | insn;
   1913  1.1  christos       else
   1914  1.1  christos 	val = extend << 16 | insn;
   1915  1.1  christos     }
   1916  1.1  christos   else
   1917  1.1  christos     val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
   1918  1.1  christos 	  | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
   1919  1.1  christos   bfd_put_32 (abfd, val, data);
   1920  1.1  christos }
   1921  1.1  christos 
   1922  1.1  christos void
   1923  1.1  christos _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
   1924  1.1  christos 			       bfd_boolean jal_shuffle, bfd_byte *data)
   1925  1.1  christos {
   1926  1.1  christos   bfd_vma extend, insn, val;
   1927  1.1  christos 
   1928  1.1  christos   if (!mips16_reloc_p (r_type))
   1929  1.1  christos     return;
   1930  1.1  christos 
   1931  1.1  christos   val = bfd_get_32 (abfd, data);
   1932  1.1  christos   if (r_type == R_MIPS16_26)
   1933  1.1  christos     {
   1934  1.1  christos       if (jal_shuffle)
   1935  1.1  christos 	{
   1936  1.1  christos 	  insn = val & 0xffff;
   1937  1.1  christos 	  extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
   1938  1.1  christos 		   | ((val >> 21) & 0x1f);
   1939  1.1  christos 	}
   1940  1.1  christos       else
   1941  1.1  christos 	{
   1942  1.1  christos 	  insn = val & 0xffff;
   1943  1.1  christos 	  extend = val >> 16;
   1944  1.1  christos 	}
   1945  1.1  christos     }
   1946  1.1  christos   else
   1947  1.1  christos     {
   1948  1.1  christos       insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
   1949  1.1  christos       extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
   1950  1.1  christos     }
   1951  1.1  christos   bfd_put_16 (abfd, insn, data + 2);
   1952  1.1  christos   bfd_put_16 (abfd, extend, data);
   1953  1.1  christos }
   1954  1.1  christos 
   1955  1.1  christos bfd_reloc_status_type
   1956  1.1  christos _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
   1957  1.1  christos 			       arelent *reloc_entry, asection *input_section,
   1958  1.1  christos 			       bfd_boolean relocatable, void *data, bfd_vma gp)
   1959  1.1  christos {
   1960  1.1  christos   bfd_vma relocation;
   1961  1.1  christos   bfd_signed_vma val;
   1962  1.1  christos   bfd_reloc_status_type status;
   1963  1.1  christos 
   1964  1.1  christos   if (bfd_is_com_section (symbol->section))
   1965  1.1  christos     relocation = 0;
   1966  1.1  christos   else
   1967  1.1  christos     relocation = symbol->value;
   1968  1.1  christos 
   1969  1.1  christos   relocation += symbol->section->output_section->vma;
   1970  1.1  christos   relocation += symbol->section->output_offset;
   1971  1.1  christos 
   1972  1.1  christos   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   1973  1.1  christos     return bfd_reloc_outofrange;
   1974  1.1  christos 
   1975  1.1  christos   /* Set val to the offset into the section or symbol.  */
   1976  1.1  christos   val = reloc_entry->addend;
   1977  1.1  christos 
   1978  1.1  christos   _bfd_mips_elf_sign_extend (val, 16);
   1979  1.1  christos 
   1980  1.1  christos   /* Adjust val for the final section location and GP value.  If we
   1981  1.1  christos      are producing relocatable output, we don't want to do this for
   1982  1.1  christos      an external symbol.  */
   1983  1.1  christos   if (! relocatable
   1984  1.1  christos       || (symbol->flags & BSF_SECTION_SYM) != 0)
   1985  1.1  christos     val += relocation - gp;
   1986  1.1  christos 
   1987  1.1  christos   if (reloc_entry->howto->partial_inplace)
   1988  1.1  christos     {
   1989  1.1  christos       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
   1990  1.1  christos 				       (bfd_byte *) data
   1991  1.1  christos 				       + reloc_entry->address);
   1992  1.1  christos       if (status != bfd_reloc_ok)
   1993  1.1  christos 	return status;
   1994  1.1  christos     }
   1995  1.1  christos   else
   1996  1.1  christos     reloc_entry->addend = val;
   1997  1.1  christos 
   1998  1.1  christos   if (relocatable)
   1999  1.1  christos     reloc_entry->address += input_section->output_offset;
   2000  1.1  christos 
   2001  1.1  christos   return bfd_reloc_ok;
   2002  1.1  christos }
   2003  1.1  christos 
   2004  1.1  christos /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
   2005  1.1  christos    R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
   2006  1.1  christos    that contains the relocation field and DATA points to the start of
   2007  1.1  christos    INPUT_SECTION.  */
   2008  1.1  christos 
   2009  1.1  christos struct mips_hi16
   2010  1.1  christos {
   2011  1.1  christos   struct mips_hi16 *next;
   2012  1.1  christos   bfd_byte *data;
   2013  1.1  christos   asection *input_section;
   2014  1.1  christos   arelent rel;
   2015  1.1  christos };
   2016  1.1  christos 
   2017  1.1  christos /* FIXME: This should not be a static variable.  */
   2018  1.1  christos 
   2019  1.1  christos static struct mips_hi16 *mips_hi16_list;
   2020  1.1  christos 
   2021  1.1  christos /* A howto special_function for REL *HI16 relocations.  We can only
   2022  1.1  christos    calculate the correct value once we've seen the partnering
   2023  1.1  christos    *LO16 relocation, so just save the information for later.
   2024  1.1  christos 
   2025  1.1  christos    The ABI requires that the *LO16 immediately follow the *HI16.
   2026  1.1  christos    However, as a GNU extension, we permit an arbitrary number of
   2027  1.1  christos    *HI16s to be associated with a single *LO16.  This significantly
   2028  1.1  christos    simplies the relocation handling in gcc.  */
   2029  1.1  christos 
   2030  1.1  christos bfd_reloc_status_type
   2031  1.1  christos _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   2032  1.1  christos 			  asymbol *symbol ATTRIBUTE_UNUSED, void *data,
   2033  1.1  christos 			  asection *input_section, bfd *output_bfd,
   2034  1.1  christos 			  char **error_message ATTRIBUTE_UNUSED)
   2035  1.1  christos {
   2036  1.1  christos   struct mips_hi16 *n;
   2037  1.1  christos 
   2038  1.1  christos   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2039  1.1  christos     return bfd_reloc_outofrange;
   2040  1.1  christos 
   2041  1.1  christos   n = bfd_malloc (sizeof *n);
   2042  1.1  christos   if (n == NULL)
   2043  1.1  christos     return bfd_reloc_outofrange;
   2044  1.1  christos 
   2045  1.1  christos   n->next = mips_hi16_list;
   2046  1.1  christos   n->data = data;
   2047  1.1  christos   n->input_section = input_section;
   2048  1.1  christos   n->rel = *reloc_entry;
   2049  1.1  christos   mips_hi16_list = n;
   2050  1.1  christos 
   2051  1.1  christos   if (output_bfd != NULL)
   2052  1.1  christos     reloc_entry->address += input_section->output_offset;
   2053  1.1  christos 
   2054  1.1  christos   return bfd_reloc_ok;
   2055  1.1  christos }
   2056  1.1  christos 
   2057  1.1  christos /* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
   2058  1.1  christos    like any other 16-bit relocation when applied to global symbols, but is
   2059  1.1  christos    treated in the same as R_MIPS_HI16 when applied to local symbols.  */
   2060  1.1  christos 
   2061  1.1  christos bfd_reloc_status_type
   2062  1.1  christos _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2063  1.1  christos 			   void *data, asection *input_section,
   2064  1.1  christos 			   bfd *output_bfd, char **error_message)
   2065  1.1  christos {
   2066  1.1  christos   if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
   2067  1.1  christos       || bfd_is_und_section (bfd_get_section (symbol))
   2068  1.1  christos       || bfd_is_com_section (bfd_get_section (symbol)))
   2069  1.1  christos     /* The relocation is against a global symbol.  */
   2070  1.1  christos     return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2071  1.1  christos 					input_section, output_bfd,
   2072  1.1  christos 					error_message);
   2073  1.1  christos 
   2074  1.1  christos   return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
   2075  1.1  christos 				   input_section, output_bfd, error_message);
   2076  1.1  christos }
   2077  1.1  christos 
   2078  1.1  christos /* A howto special_function for REL *LO16 relocations.  The *LO16 itself
   2079  1.1  christos    is a straightforward 16 bit inplace relocation, but we must deal with
   2080  1.1  christos    any partnering high-part relocations as well.  */
   2081  1.1  christos 
   2082  1.1  christos bfd_reloc_status_type
   2083  1.1  christos _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2084  1.1  christos 			  void *data, asection *input_section,
   2085  1.1  christos 			  bfd *output_bfd, char **error_message)
   2086  1.1  christos {
   2087  1.1  christos   bfd_vma vallo;
   2088  1.1  christos   bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
   2089  1.1  christos 
   2090  1.1  christos   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2091  1.1  christos     return bfd_reloc_outofrange;
   2092  1.1  christos 
   2093  1.1  christos   _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
   2094  1.1  christos 				   location);
   2095  1.1  christos   vallo = bfd_get_32 (abfd, location);
   2096  1.1  christos   _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
   2097  1.1  christos 				 location);
   2098  1.1  christos 
   2099  1.1  christos   while (mips_hi16_list != NULL)
   2100  1.1  christos     {
   2101  1.1  christos       bfd_reloc_status_type ret;
   2102  1.1  christos       struct mips_hi16 *hi;
   2103  1.1  christos 
   2104  1.1  christos       hi = mips_hi16_list;
   2105  1.1  christos 
   2106  1.1  christos       /* R_MIPS*_GOT16 relocations are something of a special case.  We
   2107  1.1  christos 	 want to install the addend in the same way as for a R_MIPS*_HI16
   2108  1.1  christos 	 relocation (with a rightshift of 16).  However, since GOT16
   2109  1.1  christos 	 relocations can also be used with global symbols, their howto
   2110  1.1  christos 	 has a rightshift of 0.  */
   2111  1.1  christos       if (hi->rel.howto->type == R_MIPS_GOT16)
   2112  1.1  christos 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
   2113  1.1  christos       else if (hi->rel.howto->type == R_MIPS16_GOT16)
   2114  1.1  christos 	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
   2115  1.1  christos 
   2116  1.1  christos       /* VALLO is a signed 16-bit number.  Bias it by 0x8000 so that any
   2117  1.1  christos 	 carry or borrow will induce a change of +1 or -1 in the high part.  */
   2118  1.1  christos       hi->rel.addend += (vallo + 0x8000) & 0xffff;
   2119  1.1  christos 
   2120  1.1  christos       ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
   2121  1.1  christos 					 hi->input_section, output_bfd,
   2122  1.1  christos 					 error_message);
   2123  1.1  christos       if (ret != bfd_reloc_ok)
   2124  1.1  christos 	return ret;
   2125  1.1  christos 
   2126  1.1  christos       mips_hi16_list = hi->next;
   2127  1.1  christos       free (hi);
   2128  1.1  christos     }
   2129  1.1  christos 
   2130  1.1  christos   return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2131  1.1  christos 				      input_section, output_bfd,
   2132  1.1  christos 				      error_message);
   2133  1.1  christos }
   2134  1.1  christos 
   2135  1.1  christos /* A generic howto special_function.  This calculates and installs the
   2136  1.1  christos    relocation itself, thus avoiding the oft-discussed problems in
   2137  1.1  christos    bfd_perform_relocation and bfd_install_relocation.  */
   2138  1.1  christos 
   2139  1.1  christos bfd_reloc_status_type
   2140  1.1  christos _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   2141  1.1  christos 			     asymbol *symbol, void *data ATTRIBUTE_UNUSED,
   2142  1.1  christos 			     asection *input_section, bfd *output_bfd,
   2143  1.1  christos 			     char **error_message ATTRIBUTE_UNUSED)
   2144  1.1  christos {
   2145  1.1  christos   bfd_signed_vma val;
   2146  1.1  christos   bfd_reloc_status_type status;
   2147  1.1  christos   bfd_boolean relocatable;
   2148  1.1  christos 
   2149  1.1  christos   relocatable = (output_bfd != NULL);
   2150  1.1  christos 
   2151  1.1  christos   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2152  1.1  christos     return bfd_reloc_outofrange;
   2153  1.1  christos 
   2154  1.1  christos   /* Build up the field adjustment in VAL.  */
   2155  1.1  christos   val = 0;
   2156  1.1  christos   if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
   2157  1.1  christos     {
   2158  1.1  christos       /* Either we're calculating the final field value or we have a
   2159  1.1  christos 	 relocation against a section symbol.  Add in the section's
   2160  1.1  christos 	 offset or address.  */
   2161  1.1  christos       val += symbol->section->output_section->vma;
   2162  1.1  christos       val += symbol->section->output_offset;
   2163  1.1  christos     }
   2164  1.1  christos 
   2165  1.1  christos   if (!relocatable)
   2166  1.1  christos     {
   2167  1.1  christos       /* We're calculating the final field value.  Add in the symbol's value
   2168  1.1  christos 	 and, if pc-relative, subtract the address of the field itself.  */
   2169  1.1  christos       val += symbol->value;
   2170  1.1  christos       if (reloc_entry->howto->pc_relative)
   2171  1.1  christos 	{
   2172  1.1  christos 	  val -= input_section->output_section->vma;
   2173  1.1  christos 	  val -= input_section->output_offset;
   2174  1.1  christos 	  val -= reloc_entry->address;
   2175  1.1  christos 	}
   2176  1.1  christos     }
   2177  1.1  christos 
   2178  1.1  christos   /* VAL is now the final adjustment.  If we're keeping this relocation
   2179  1.1  christos      in the output file, and if the relocation uses a separate addend,
   2180  1.1  christos      we just need to add VAL to that addend.  Otherwise we need to add
   2181  1.1  christos      VAL to the relocation field itself.  */
   2182  1.1  christos   if (relocatable && !reloc_entry->howto->partial_inplace)
   2183  1.1  christos     reloc_entry->addend += val;
   2184  1.1  christos   else
   2185  1.1  christos     {
   2186  1.1  christos       bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
   2187  1.1  christos 
   2188  1.1  christos       /* Add in the separate addend, if any.  */
   2189  1.1  christos       val += reloc_entry->addend;
   2190  1.1  christos 
   2191  1.1  christos       /* Add VAL to the relocation field.  */
   2192  1.1  christos       _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
   2193  1.1  christos 				       location);
   2194  1.1  christos       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
   2195  1.1  christos 				       location);
   2196  1.1  christos       _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
   2197  1.1  christos 				     location);
   2198  1.1  christos 
   2199  1.1  christos       if (status != bfd_reloc_ok)
   2200  1.1  christos 	return status;
   2201  1.1  christos     }
   2202  1.1  christos 
   2203  1.1  christos   if (relocatable)
   2204  1.1  christos     reloc_entry->address += input_section->output_offset;
   2205  1.1  christos 
   2206  1.1  christos   return bfd_reloc_ok;
   2207  1.1  christos }
   2208  1.1  christos 
   2209  1.1  christos /* Swap an entry in a .gptab section.  Note that these routines rely
   2211  1.1  christos    on the equivalence of the two elements of the union.  */
   2212  1.1  christos 
   2213  1.1  christos static void
   2214  1.1  christos bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
   2215  1.1  christos 			      Elf32_gptab *in)
   2216  1.1  christos {
   2217  1.1  christos   in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
   2218  1.1  christos   in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
   2219  1.1  christos }
   2220  1.1  christos 
   2221  1.1  christos static void
   2222  1.1  christos bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
   2223  1.1  christos 			       Elf32_External_gptab *ex)
   2224  1.1  christos {
   2225  1.1  christos   H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
   2226  1.1  christos   H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
   2227  1.1  christos }
   2228  1.1  christos 
   2229  1.1  christos static void
   2230  1.1  christos bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
   2231  1.1  christos 				Elf32_External_compact_rel *ex)
   2232  1.1  christos {
   2233  1.1  christos   H_PUT_32 (abfd, in->id1, ex->id1);
   2234  1.1  christos   H_PUT_32 (abfd, in->num, ex->num);
   2235  1.1  christos   H_PUT_32 (abfd, in->id2, ex->id2);
   2236  1.1  christos   H_PUT_32 (abfd, in->offset, ex->offset);
   2237  1.1  christos   H_PUT_32 (abfd, in->reserved0, ex->reserved0);
   2238  1.1  christos   H_PUT_32 (abfd, in->reserved1, ex->reserved1);
   2239  1.1  christos }
   2240  1.1  christos 
   2241  1.1  christos static void
   2242  1.1  christos bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
   2243  1.1  christos 			   Elf32_External_crinfo *ex)
   2244  1.1  christos {
   2245  1.1  christos   unsigned long l;
   2246  1.1  christos 
   2247  1.1  christos   l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
   2248  1.1  christos        | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
   2249  1.1  christos        | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
   2250  1.1  christos        | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
   2251  1.1  christos   H_PUT_32 (abfd, l, ex->info);
   2252  1.1  christos   H_PUT_32 (abfd, in->konst, ex->konst);
   2253  1.1  christos   H_PUT_32 (abfd, in->vaddr, ex->vaddr);
   2254  1.1  christos }
   2255  1.1  christos 
   2256  1.1  christos /* A .reginfo section holds a single Elf32_RegInfo structure.  These
   2258  1.1  christos    routines swap this structure in and out.  They are used outside of
   2259  1.1  christos    BFD, so they are globally visible.  */
   2260  1.1  christos 
   2261  1.1  christos void
   2262  1.1  christos bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
   2263  1.1  christos 				Elf32_RegInfo *in)
   2264  1.1  christos {
   2265  1.1  christos   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
   2266  1.1  christos   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
   2267  1.1  christos   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
   2268  1.1  christos   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
   2269  1.1  christos   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
   2270  1.1  christos   in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
   2271  1.1  christos }
   2272  1.1  christos 
   2273  1.1  christos void
   2274  1.1  christos bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
   2275  1.1  christos 				 Elf32_External_RegInfo *ex)
   2276  1.1  christos {
   2277  1.1  christos   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
   2278  1.1  christos   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
   2279  1.1  christos   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
   2280  1.1  christos   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
   2281  1.1  christos   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
   2282  1.1  christos   H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
   2283  1.1  christos }
   2284  1.1  christos 
   2285  1.1  christos /* In the 64 bit ABI, the .MIPS.options section holds register
   2286  1.1  christos    information in an Elf64_Reginfo structure.  These routines swap
   2287  1.1  christos    them in and out.  They are globally visible because they are used
   2288  1.1  christos    outside of BFD.  These routines are here so that gas can call them
   2289  1.1  christos    without worrying about whether the 64 bit ABI has been included.  */
   2290  1.1  christos 
   2291  1.1  christos void
   2292  1.1  christos bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
   2293  1.1  christos 				Elf64_Internal_RegInfo *in)
   2294  1.1  christos {
   2295  1.1  christos   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
   2296  1.1  christos   in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
   2297  1.1  christos   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
   2298  1.1  christos   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
   2299  1.1  christos   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
   2300  1.1  christos   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
   2301  1.1  christos   in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
   2302  1.1  christos }
   2303  1.1  christos 
   2304  1.1  christos void
   2305  1.1  christos bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
   2306  1.1  christos 				 Elf64_External_RegInfo *ex)
   2307  1.1  christos {
   2308  1.1  christos   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
   2309  1.1  christos   H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
   2310  1.1  christos   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
   2311  1.1  christos   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
   2312  1.1  christos   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
   2313  1.1  christos   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
   2314  1.1  christos   H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
   2315  1.1  christos }
   2316  1.1  christos 
   2317  1.1  christos /* Swap in an options header.  */
   2318  1.1  christos 
   2319  1.1  christos void
   2320  1.1  christos bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
   2321  1.1  christos 			      Elf_Internal_Options *in)
   2322  1.1  christos {
   2323  1.1  christos   in->kind = H_GET_8 (abfd, ex->kind);
   2324  1.1  christos   in->size = H_GET_8 (abfd, ex->size);
   2325  1.1  christos   in->section = H_GET_16 (abfd, ex->section);
   2326  1.1  christos   in->info = H_GET_32 (abfd, ex->info);
   2327  1.1  christos }
   2328  1.1  christos 
   2329  1.1  christos /* Swap out an options header.  */
   2330  1.1  christos 
   2331  1.1  christos void
   2332  1.1  christos bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
   2333  1.1  christos 			       Elf_External_Options *ex)
   2334  1.1  christos {
   2335  1.1  christos   H_PUT_8 (abfd, in->kind, ex->kind);
   2336  1.1  christos   H_PUT_8 (abfd, in->size, ex->size);
   2337  1.1  christos   H_PUT_16 (abfd, in->section, ex->section);
   2338  1.1  christos   H_PUT_32 (abfd, in->info, ex->info);
   2339  1.1  christos }
   2340  1.1  christos 
   2341  1.1  christos /* This function is called via qsort() to sort the dynamic relocation
   2343  1.1  christos    entries by increasing r_symndx value.  */
   2344  1.1  christos 
   2345  1.1  christos static int
   2346  1.1  christos sort_dynamic_relocs (const void *arg1, const void *arg2)
   2347  1.1  christos {
   2348  1.1  christos   Elf_Internal_Rela int_reloc1;
   2349  1.1  christos   Elf_Internal_Rela int_reloc2;
   2350  1.1  christos   int diff;
   2351  1.1  christos 
   2352  1.1  christos   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
   2353  1.1  christos   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
   2354  1.1  christos 
   2355  1.1  christos   diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
   2356  1.1  christos   if (diff != 0)
   2357  1.1  christos     return diff;
   2358  1.1  christos 
   2359  1.1  christos   if (int_reloc1.r_offset < int_reloc2.r_offset)
   2360  1.1  christos     return -1;
   2361  1.1  christos   if (int_reloc1.r_offset > int_reloc2.r_offset)
   2362  1.1  christos     return 1;
   2363  1.1  christos   return 0;
   2364  1.1  christos }
   2365  1.1  christos 
   2366  1.1  christos /* Like sort_dynamic_relocs, but used for elf64 relocations.  */
   2367  1.1  christos 
   2368  1.1  christos static int
   2369  1.1  christos sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
   2370  1.1  christos 			const void *arg2 ATTRIBUTE_UNUSED)
   2371  1.1  christos {
   2372  1.1  christos #ifdef BFD64
   2373  1.1  christos   Elf_Internal_Rela int_reloc1[3];
   2374  1.1  christos   Elf_Internal_Rela int_reloc2[3];
   2375  1.1  christos 
   2376  1.1  christos   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
   2377  1.1  christos     (reldyn_sorting_bfd, arg1, int_reloc1);
   2378  1.1  christos   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
   2379  1.1  christos     (reldyn_sorting_bfd, arg2, int_reloc2);
   2380  1.1  christos 
   2381  1.1  christos   if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
   2382  1.1  christos     return -1;
   2383  1.1  christos   if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
   2384  1.1  christos     return 1;
   2385  1.1  christos 
   2386  1.1  christos   if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
   2387  1.1  christos     return -1;
   2388  1.1  christos   if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
   2389  1.1  christos     return 1;
   2390  1.1  christos   return 0;
   2391  1.1  christos #else
   2392  1.1  christos   abort ();
   2393  1.1  christos #endif
   2394  1.1  christos }
   2395  1.1  christos 
   2396  1.1  christos 
   2397  1.1  christos /* This routine is used to write out ECOFF debugging external symbol
   2398  1.1  christos    information.  It is called via mips_elf_link_hash_traverse.  The
   2399  1.1  christos    ECOFF external symbol information must match the ELF external
   2400  1.1  christos    symbol information.  Unfortunately, at this point we don't know
   2401  1.1  christos    whether a symbol is required by reloc information, so the two
   2402  1.1  christos    tables may wind up being different.  We must sort out the external
   2403  1.1  christos    symbol information before we can set the final size of the .mdebug
   2404  1.1  christos    section, and we must set the size of the .mdebug section before we
   2405  1.1  christos    can relocate any sections, and we can't know which symbols are
   2406  1.1  christos    required by relocation until we relocate the sections.
   2407  1.1  christos    Fortunately, it is relatively unlikely that any symbol will be
   2408  1.1  christos    stripped but required by a reloc.  In particular, it can not happen
   2409  1.1  christos    when generating a final executable.  */
   2410  1.1  christos 
   2411  1.1  christos static bfd_boolean
   2412  1.1  christos mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
   2413  1.1  christos {
   2414  1.1  christos   struct extsym_info *einfo = data;
   2415  1.1  christos   bfd_boolean strip;
   2416  1.1  christos   asection *sec, *output_section;
   2417  1.1  christos 
   2418  1.1  christos   if (h->root.root.type == bfd_link_hash_warning)
   2419  1.1  christos     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   2420  1.1  christos 
   2421  1.1  christos   if (h->root.indx == -2)
   2422  1.1  christos     strip = FALSE;
   2423  1.1  christos   else if ((h->root.def_dynamic
   2424  1.1  christos 	    || h->root.ref_dynamic
   2425  1.1  christos 	    || h->root.type == bfd_link_hash_new)
   2426  1.1  christos 	   && !h->root.def_regular
   2427  1.1  christos 	   && !h->root.ref_regular)
   2428  1.1  christos     strip = TRUE;
   2429  1.1  christos   else if (einfo->info->strip == strip_all
   2430  1.1  christos 	   || (einfo->info->strip == strip_some
   2431  1.1  christos 	       && bfd_hash_lookup (einfo->info->keep_hash,
   2432  1.1  christos 				   h->root.root.root.string,
   2433  1.1  christos 				   FALSE, FALSE) == NULL))
   2434  1.1  christos     strip = TRUE;
   2435  1.1  christos   else
   2436  1.1  christos     strip = FALSE;
   2437  1.1  christos 
   2438  1.1  christos   if (strip)
   2439  1.1  christos     return TRUE;
   2440  1.1  christos 
   2441  1.1  christos   if (h->esym.ifd == -2)
   2442  1.1  christos     {
   2443  1.1  christos       h->esym.jmptbl = 0;
   2444  1.1  christos       h->esym.cobol_main = 0;
   2445  1.1  christos       h->esym.weakext = 0;
   2446  1.1  christos       h->esym.reserved = 0;
   2447  1.1  christos       h->esym.ifd = ifdNil;
   2448  1.1  christos       h->esym.asym.value = 0;
   2449  1.1  christos       h->esym.asym.st = stGlobal;
   2450  1.1  christos 
   2451  1.1  christos       if (h->root.root.type == bfd_link_hash_undefined
   2452  1.1  christos 	  || h->root.root.type == bfd_link_hash_undefweak)
   2453  1.1  christos 	{
   2454  1.1  christos 	  const char *name;
   2455  1.1  christos 
   2456  1.1  christos 	  /* Use undefined class.  Also, set class and type for some
   2457  1.1  christos              special symbols.  */
   2458  1.1  christos 	  name = h->root.root.root.string;
   2459  1.1  christos 	  if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
   2460  1.1  christos 	      || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
   2461  1.1  christos 	    {
   2462  1.1  christos 	      h->esym.asym.sc = scData;
   2463  1.1  christos 	      h->esym.asym.st = stLabel;
   2464  1.1  christos 	      h->esym.asym.value = 0;
   2465  1.1  christos 	    }
   2466  1.1  christos 	  else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
   2467  1.1  christos 	    {
   2468  1.1  christos 	      h->esym.asym.sc = scAbs;
   2469  1.1  christos 	      h->esym.asym.st = stLabel;
   2470  1.1  christos 	      h->esym.asym.value =
   2471  1.1  christos 		mips_elf_hash_table (einfo->info)->procedure_count;
   2472  1.1  christos 	    }
   2473  1.1  christos 	  else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
   2474  1.1  christos 	    {
   2475  1.1  christos 	      h->esym.asym.sc = scAbs;
   2476  1.1  christos 	      h->esym.asym.st = stLabel;
   2477  1.1  christos 	      h->esym.asym.value = elf_gp (einfo->abfd);
   2478  1.1  christos 	    }
   2479  1.1  christos 	  else
   2480  1.1  christos 	    h->esym.asym.sc = scUndefined;
   2481  1.1  christos 	}
   2482  1.1  christos       else if (h->root.root.type != bfd_link_hash_defined
   2483  1.1  christos 	  && h->root.root.type != bfd_link_hash_defweak)
   2484  1.1  christos 	h->esym.asym.sc = scAbs;
   2485  1.1  christos       else
   2486  1.1  christos 	{
   2487  1.1  christos 	  const char *name;
   2488  1.1  christos 
   2489  1.1  christos 	  sec = h->root.root.u.def.section;
   2490  1.1  christos 	  output_section = sec->output_section;
   2491  1.1  christos 
   2492  1.1  christos 	  /* When making a shared library and symbol h is the one from
   2493  1.1  christos 	     the another shared library, OUTPUT_SECTION may be null.  */
   2494  1.1  christos 	  if (output_section == NULL)
   2495  1.1  christos 	    h->esym.asym.sc = scUndefined;
   2496  1.1  christos 	  else
   2497  1.1  christos 	    {
   2498  1.1  christos 	      name = bfd_section_name (output_section->owner, output_section);
   2499  1.1  christos 
   2500  1.1  christos 	      if (strcmp (name, ".text") == 0)
   2501  1.1  christos 		h->esym.asym.sc = scText;
   2502  1.1  christos 	      else if (strcmp (name, ".data") == 0)
   2503  1.1  christos 		h->esym.asym.sc = scData;
   2504  1.1  christos 	      else if (strcmp (name, ".sdata") == 0)
   2505  1.1  christos 		h->esym.asym.sc = scSData;
   2506  1.1  christos 	      else if (strcmp (name, ".rodata") == 0
   2507  1.1  christos 		       || strcmp (name, ".rdata") == 0)
   2508  1.1  christos 		h->esym.asym.sc = scRData;
   2509  1.1  christos 	      else if (strcmp (name, ".bss") == 0)
   2510  1.1  christos 		h->esym.asym.sc = scBss;
   2511  1.1  christos 	      else if (strcmp (name, ".sbss") == 0)
   2512  1.1  christos 		h->esym.asym.sc = scSBss;
   2513  1.1  christos 	      else if (strcmp (name, ".init") == 0)
   2514  1.1  christos 		h->esym.asym.sc = scInit;
   2515  1.1  christos 	      else if (strcmp (name, ".fini") == 0)
   2516  1.1  christos 		h->esym.asym.sc = scFini;
   2517  1.1  christos 	      else
   2518  1.1  christos 		h->esym.asym.sc = scAbs;
   2519  1.1  christos 	    }
   2520  1.1  christos 	}
   2521  1.1  christos 
   2522  1.1  christos       h->esym.asym.reserved = 0;
   2523  1.1  christos       h->esym.asym.index = indexNil;
   2524  1.1  christos     }
   2525  1.1  christos 
   2526  1.1  christos   if (h->root.root.type == bfd_link_hash_common)
   2527  1.1  christos     h->esym.asym.value = h->root.root.u.c.size;
   2528  1.1  christos   else if (h->root.root.type == bfd_link_hash_defined
   2529  1.1  christos 	   || h->root.root.type == bfd_link_hash_defweak)
   2530  1.1  christos     {
   2531  1.1  christos       if (h->esym.asym.sc == scCommon)
   2532  1.1  christos 	h->esym.asym.sc = scBss;
   2533  1.1  christos       else if (h->esym.asym.sc == scSCommon)
   2534  1.1  christos 	h->esym.asym.sc = scSBss;
   2535  1.1  christos 
   2536  1.1  christos       sec = h->root.root.u.def.section;
   2537  1.1  christos       output_section = sec->output_section;
   2538  1.1  christos       if (output_section != NULL)
   2539  1.1  christos 	h->esym.asym.value = (h->root.root.u.def.value
   2540  1.1  christos 			      + sec->output_offset
   2541  1.1  christos 			      + output_section->vma);
   2542  1.1  christos       else
   2543  1.1  christos 	h->esym.asym.value = 0;
   2544  1.1  christos     }
   2545  1.1  christos   else
   2546  1.1  christos     {
   2547  1.1  christos       struct mips_elf_link_hash_entry *hd = h;
   2548  1.1  christos 
   2549  1.1  christos       while (hd->root.root.type == bfd_link_hash_indirect)
   2550  1.1  christos 	hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
   2551  1.1  christos 
   2552  1.1  christos       if (hd->needs_lazy_stub)
   2553  1.1  christos 	{
   2554  1.1  christos 	  /* Set type and value for a symbol with a function stub.  */
   2555  1.1  christos 	  h->esym.asym.st = stProc;
   2556  1.1  christos 	  sec = hd->root.root.u.def.section;
   2557  1.1  christos 	  if (sec == NULL)
   2558  1.1  christos 	    h->esym.asym.value = 0;
   2559  1.1  christos 	  else
   2560  1.1  christos 	    {
   2561  1.1  christos 	      output_section = sec->output_section;
   2562  1.1  christos 	      if (output_section != NULL)
   2563  1.1  christos 		h->esym.asym.value = (hd->root.plt.offset
   2564  1.1  christos 				      + sec->output_offset
   2565  1.1  christos 				      + output_section->vma);
   2566  1.1  christos 	      else
   2567  1.1  christos 		h->esym.asym.value = 0;
   2568  1.1  christos 	    }
   2569  1.1  christos 	}
   2570  1.1  christos     }
   2571  1.1  christos 
   2572  1.1  christos   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
   2573  1.1  christos 				      h->root.root.root.string,
   2574  1.1  christos 				      &h->esym))
   2575  1.1  christos     {
   2576  1.1  christos       einfo->failed = TRUE;
   2577  1.1  christos       return FALSE;
   2578  1.1  christos     }
   2579  1.1  christos 
   2580  1.1  christos   return TRUE;
   2581  1.1  christos }
   2582  1.1  christos 
   2583  1.1  christos /* A comparison routine used to sort .gptab entries.  */
   2584  1.1  christos 
   2585  1.1  christos static int
   2586  1.1  christos gptab_compare (const void *p1, const void *p2)
   2587  1.1  christos {
   2588  1.1  christos   const Elf32_gptab *a1 = p1;
   2589  1.1  christos   const Elf32_gptab *a2 = p2;
   2590  1.1  christos 
   2591  1.1  christos   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
   2592  1.1  christos }
   2593  1.1  christos 
   2594  1.1  christos /* Functions to manage the got entry hash table.  */
   2596  1.1  christos 
   2597  1.1  christos /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
   2598  1.1  christos    hash number.  */
   2599  1.1  christos 
   2600  1.1  christos static INLINE hashval_t
   2601  1.1  christos mips_elf_hash_bfd_vma (bfd_vma addr)
   2602  1.1  christos {
   2603  1.1  christos #ifdef BFD64
   2604  1.1  christos   return addr + (addr >> 32);
   2605  1.1  christos #else
   2606  1.1  christos   return addr;
   2607  1.1  christos #endif
   2608  1.1  christos }
   2609  1.1  christos 
   2610  1.1  christos /* got_entries only match if they're identical, except for gotidx, so
   2611  1.1  christos    use all fields to compute the hash, and compare the appropriate
   2612  1.1  christos    union members.  */
   2613  1.1  christos 
   2614  1.1  christos static hashval_t
   2615  1.1  christos mips_elf_got_entry_hash (const void *entry_)
   2616  1.1  christos {
   2617  1.1  christos   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
   2618  1.1  christos 
   2619  1.1  christos   return entry->symndx
   2620  1.1  christos     + ((entry->tls_type & GOT_TLS_LDM) << 17)
   2621  1.1  christos     + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
   2622  1.1  christos        : entry->abfd->id
   2623  1.1  christos          + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
   2624  1.1  christos 	    : entry->d.h->root.root.root.hash));
   2625  1.1  christos }
   2626  1.1  christos 
   2627  1.1  christos static int
   2628  1.1  christos mips_elf_got_entry_eq (const void *entry1, const void *entry2)
   2629  1.1  christos {
   2630  1.1  christos   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
   2631  1.1  christos   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
   2632  1.1  christos 
   2633  1.1  christos   /* An LDM entry can only match another LDM entry.  */
   2634  1.1  christos   if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
   2635  1.1  christos     return 0;
   2636  1.1  christos 
   2637  1.1  christos   return e1->abfd == e2->abfd && e1->symndx == e2->symndx
   2638  1.1  christos     && (! e1->abfd ? e1->d.address == e2->d.address
   2639  1.1  christos 	: e1->symndx >= 0 ? e1->d.addend == e2->d.addend
   2640  1.1  christos 	: e1->d.h == e2->d.h);
   2641  1.1  christos }
   2642  1.1  christos 
   2643  1.1  christos /* multi_got_entries are still a match in the case of global objects,
   2644  1.1  christos    even if the input bfd in which they're referenced differs, so the
   2645  1.1  christos    hash computation and compare functions are adjusted
   2646  1.1  christos    accordingly.  */
   2647  1.1  christos 
   2648  1.1  christos static hashval_t
   2649  1.1  christos mips_elf_multi_got_entry_hash (const void *entry_)
   2650  1.1  christos {
   2651  1.1  christos   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
   2652  1.1  christos 
   2653  1.1  christos   return entry->symndx
   2654  1.1  christos     + (! entry->abfd
   2655  1.1  christos        ? mips_elf_hash_bfd_vma (entry->d.address)
   2656  1.1  christos        : entry->symndx >= 0
   2657  1.1  christos        ? ((entry->tls_type & GOT_TLS_LDM)
   2658  1.1  christos 	  ? (GOT_TLS_LDM << 17)
   2659  1.1  christos 	  : (entry->abfd->id
   2660  1.1  christos 	     + mips_elf_hash_bfd_vma (entry->d.addend)))
   2661  1.1  christos        : entry->d.h->root.root.root.hash);
   2662  1.1  christos }
   2663  1.1  christos 
   2664  1.1  christos static int
   2665  1.1  christos mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
   2666  1.1  christos {
   2667  1.1  christos   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
   2668  1.1  christos   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
   2669  1.1  christos 
   2670  1.1  christos   /* Any two LDM entries match.  */
   2671  1.1  christos   if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
   2672  1.1  christos     return 1;
   2673  1.1  christos 
   2674  1.1  christos   /* Nothing else matches an LDM entry.  */
   2675  1.1  christos   if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
   2676  1.1  christos     return 0;
   2677  1.1  christos 
   2678  1.1  christos   return e1->symndx == e2->symndx
   2679  1.1  christos     && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
   2680  1.1  christos 	: e1->abfd == NULL || e2->abfd == NULL
   2681  1.1  christos 	? e1->abfd == e2->abfd && e1->d.address == e2->d.address
   2682  1.1  christos 	: e1->d.h == e2->d.h);
   2683  1.1  christos }
   2684  1.1  christos 
   2685  1.1  christos static hashval_t
   2686  1.1  christos mips_got_page_entry_hash (const void *entry_)
   2687  1.1  christos {
   2688  1.1  christos   const struct mips_got_page_entry *entry;
   2689  1.1  christos 
   2690  1.1  christos   entry = (const struct mips_got_page_entry *) entry_;
   2691  1.1  christos   return entry->abfd->id + entry->symndx;
   2692  1.1  christos }
   2693  1.1  christos 
   2694  1.1  christos static int
   2695  1.1  christos mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
   2696  1.1  christos {
   2697  1.1  christos   const struct mips_got_page_entry *entry1, *entry2;
   2698  1.1  christos 
   2699  1.1  christos   entry1 = (const struct mips_got_page_entry *) entry1_;
   2700  1.1  christos   entry2 = (const struct mips_got_page_entry *) entry2_;
   2701  1.1  christos   return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
   2702  1.1  christos }
   2703  1.1  christos 
   2704  1.1  christos /* Return the dynamic relocation section.  If it doesn't exist, try to
   2706  1.1  christos    create a new it if CREATE_P, otherwise return NULL.  Also return NULL
   2707  1.1  christos    if creation fails.  */
   2708  1.1  christos 
   2709  1.1  christos static asection *
   2710  1.1  christos mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
   2711  1.1  christos {
   2712  1.1  christos   const char *dname;
   2713  1.1  christos   asection *sreloc;
   2714  1.1  christos   bfd *dynobj;
   2715  1.1  christos 
   2716  1.1  christos   dname = MIPS_ELF_REL_DYN_NAME (info);
   2717  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   2718  1.1  christos   sreloc = bfd_get_section_by_name (dynobj, dname);
   2719  1.1  christos   if (sreloc == NULL && create_p)
   2720  1.1  christos     {
   2721  1.1  christos       sreloc = bfd_make_section_with_flags (dynobj, dname,
   2722  1.1  christos 					    (SEC_ALLOC
   2723  1.1  christos 					     | SEC_LOAD
   2724  1.1  christos 					     | SEC_HAS_CONTENTS
   2725  1.1  christos 					     | SEC_IN_MEMORY
   2726  1.1  christos 					     | SEC_LINKER_CREATED
   2727  1.1  christos 					     | SEC_READONLY));
   2728  1.1  christos       if (sreloc == NULL
   2729  1.1  christos 	  || ! bfd_set_section_alignment (dynobj, sreloc,
   2730  1.1  christos 					  MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
   2731  1.1  christos 	return NULL;
   2732  1.1  christos     }
   2733  1.1  christos   return sreloc;
   2734  1.1  christos }
   2735  1.1  christos 
   2736  1.1  christos /* Count the number of relocations needed for a TLS GOT entry, with
   2737  1.1  christos    access types from TLS_TYPE, and symbol H (or a local symbol if H
   2738  1.1  christos    is NULL).  */
   2739  1.1  christos 
   2740  1.1  christos static int
   2741  1.1  christos mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
   2742  1.1  christos 		     struct elf_link_hash_entry *h)
   2743  1.1  christos {
   2744  1.1  christos   int indx = 0;
   2745  1.1  christos   int ret = 0;
   2746  1.1  christos   bfd_boolean need_relocs = FALSE;
   2747  1.1  christos   bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
   2748  1.1  christos 
   2749  1.1  christos   if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
   2750  1.1  christos       && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
   2751  1.1  christos     indx = h->dynindx;
   2752  1.1  christos 
   2753  1.1  christos   if ((info->shared || indx != 0)
   2754  1.1  christos       && (h == NULL
   2755  1.1  christos 	  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   2756  1.1  christos 	  || h->root.type != bfd_link_hash_undefweak))
   2757  1.1  christos     need_relocs = TRUE;
   2758  1.1  christos 
   2759  1.1  christos   if (!need_relocs)
   2760  1.1  christos     return FALSE;
   2761  1.1  christos 
   2762  1.1  christos   if (tls_type & GOT_TLS_GD)
   2763  1.1  christos     {
   2764  1.1  christos       ret++;
   2765  1.1  christos       if (indx != 0)
   2766  1.1  christos 	ret++;
   2767  1.1  christos     }
   2768  1.1  christos 
   2769  1.1  christos   if (tls_type & GOT_TLS_IE)
   2770  1.1  christos     ret++;
   2771  1.1  christos 
   2772  1.1  christos   if ((tls_type & GOT_TLS_LDM) && info->shared)
   2773  1.1  christos     ret++;
   2774  1.1  christos 
   2775  1.1  christos   return ret;
   2776  1.1  christos }
   2777  1.1  christos 
   2778  1.1  christos /* Count the number of TLS relocations required for the GOT entry in
   2779  1.1  christos    ARG1, if it describes a local symbol.  */
   2780  1.1  christos 
   2781  1.1  christos static int
   2782  1.1  christos mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
   2783  1.1  christos {
   2784  1.1  christos   struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
   2785  1.1  christos   struct mips_elf_count_tls_arg *arg = arg2;
   2786  1.1  christos 
   2787  1.1  christos   if (entry->abfd != NULL && entry->symndx != -1)
   2788  1.1  christos     arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
   2789  1.1  christos 
   2790  1.1  christos   return 1;
   2791  1.1  christos }
   2792  1.1  christos 
   2793  1.1  christos /* Count the number of TLS GOT entries required for the global (or
   2794  1.1  christos    forced-local) symbol in ARG1.  */
   2795  1.1  christos 
   2796  1.1  christos static int
   2797  1.1  christos mips_elf_count_global_tls_entries (void *arg1, void *arg2)
   2798  1.1  christos {
   2799  1.1  christos   struct mips_elf_link_hash_entry *hm
   2800  1.1  christos     = (struct mips_elf_link_hash_entry *) arg1;
   2801  1.1  christos   struct mips_elf_count_tls_arg *arg = arg2;
   2802  1.1  christos 
   2803  1.1  christos   if (hm->tls_type & GOT_TLS_GD)
   2804  1.1  christos     arg->needed += 2;
   2805  1.1  christos   if (hm->tls_type & GOT_TLS_IE)
   2806  1.1  christos     arg->needed += 1;
   2807  1.1  christos 
   2808  1.1  christos   return 1;
   2809  1.1  christos }
   2810  1.1  christos 
   2811  1.1  christos /* Count the number of TLS relocations required for the global (or
   2812  1.1  christos    forced-local) symbol in ARG1.  */
   2813  1.1  christos 
   2814  1.1  christos static int
   2815  1.1  christos mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
   2816  1.1  christos {
   2817  1.1  christos   struct mips_elf_link_hash_entry *hm
   2818  1.1  christos     = (struct mips_elf_link_hash_entry *) arg1;
   2819  1.1  christos   struct mips_elf_count_tls_arg *arg = arg2;
   2820  1.1  christos 
   2821  1.1  christos   arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
   2822  1.1  christos 
   2823  1.1  christos   return 1;
   2824  1.1  christos }
   2825  1.1  christos 
   2826  1.1  christos /* Output a simple dynamic relocation into SRELOC.  */
   2827  1.1  christos 
   2828  1.1  christos static void
   2829  1.1  christos mips_elf_output_dynamic_relocation (bfd *output_bfd,
   2830  1.1  christos 				    asection *sreloc,
   2831  1.1  christos 				    unsigned long reloc_index,
   2832  1.1  christos 				    unsigned long indx,
   2833  1.1  christos 				    int r_type,
   2834  1.1  christos 				    bfd_vma offset)
   2835  1.1  christos {
   2836  1.1  christos   Elf_Internal_Rela rel[3];
   2837  1.1  christos 
   2838  1.1  christos   memset (rel, 0, sizeof (rel));
   2839  1.1  christos 
   2840  1.1  christos   rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
   2841  1.1  christos   rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
   2842  1.1  christos 
   2843  1.1  christos   if (ABI_64_P (output_bfd))
   2844  1.1  christos     {
   2845  1.1  christos       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
   2846  1.1  christos 	(output_bfd, &rel[0],
   2847  1.1  christos 	 (sreloc->contents
   2848  1.1  christos 	  + reloc_index * sizeof (Elf64_Mips_External_Rel)));
   2849  1.1  christos     }
   2850  1.1  christos   else
   2851  1.1  christos     bfd_elf32_swap_reloc_out
   2852  1.1  christos       (output_bfd, &rel[0],
   2853  1.1  christos        (sreloc->contents
   2854  1.1  christos 	+ reloc_index * sizeof (Elf32_External_Rel)));
   2855  1.1  christos }
   2856  1.1  christos 
   2857  1.1  christos /* Initialize a set of TLS GOT entries for one symbol.  */
   2858  1.1  christos 
   2859  1.1  christos static void
   2860  1.1  christos mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
   2861  1.1  christos 			       unsigned char *tls_type_p,
   2862  1.1  christos 			       struct bfd_link_info *info,
   2863  1.1  christos 			       struct mips_elf_link_hash_entry *h,
   2864  1.1  christos 			       bfd_vma value)
   2865  1.1  christos {
   2866  1.1  christos   struct mips_elf_link_hash_table *htab;
   2867  1.1  christos   int indx;
   2868  1.1  christos   asection *sreloc, *sgot;
   2869  1.1  christos   bfd_vma offset, offset2;
   2870  1.1  christos   bfd_boolean need_relocs = FALSE;
   2871  1.1  christos 
   2872  1.1  christos   htab = mips_elf_hash_table (info);
   2873  1.1  christos   if (htab == NULL)
   2874  1.1  christos     return;
   2875  1.1  christos 
   2876  1.1  christos   sgot = htab->sgot;
   2877  1.1  christos 
   2878  1.1  christos   indx = 0;
   2879  1.1  christos   if (h != NULL)
   2880  1.1  christos     {
   2881  1.1  christos       bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
   2882  1.1  christos 
   2883  1.1  christos       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
   2884  1.1  christos 	  && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
   2885  1.1  christos 	indx = h->root.dynindx;
   2886  1.1  christos     }
   2887  1.1  christos 
   2888  1.1  christos   if (*tls_type_p & GOT_TLS_DONE)
   2889  1.1  christos     return;
   2890  1.1  christos 
   2891  1.1  christos   if ((info->shared || indx != 0)
   2892  1.1  christos       && (h == NULL
   2893  1.1  christos 	  || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
   2894  1.1  christos 	  || h->root.type != bfd_link_hash_undefweak))
   2895  1.1  christos     need_relocs = TRUE;
   2896  1.1  christos 
   2897  1.1  christos   /* MINUS_ONE means the symbol is not defined in this object.  It may not
   2898  1.1  christos      be defined at all; assume that the value doesn't matter in that
   2899  1.1  christos      case.  Otherwise complain if we would use the value.  */
   2900  1.1  christos   BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
   2901  1.1  christos 	      || h->root.root.type == bfd_link_hash_undefweak);
   2902  1.1  christos 
   2903  1.1  christos   /* Emit necessary relocations.  */
   2904  1.1  christos   sreloc = mips_elf_rel_dyn_section (info, FALSE);
   2905  1.1  christos 
   2906  1.1  christos   /* General Dynamic.  */
   2907  1.1  christos   if (*tls_type_p & GOT_TLS_GD)
   2908  1.1  christos     {
   2909  1.1  christos       offset = got_offset;
   2910  1.1  christos       offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
   2911  1.1  christos 
   2912  1.1  christos       if (need_relocs)
   2913  1.1  christos 	{
   2914  1.1  christos 	  mips_elf_output_dynamic_relocation
   2915  1.1  christos 	    (abfd, sreloc, sreloc->reloc_count++, indx,
   2916  1.1  christos 	     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
   2917  1.1  christos 	     sgot->output_offset + sgot->output_section->vma + offset);
   2918  1.1  christos 
   2919  1.1  christos 	  if (indx)
   2920  1.1  christos 	    mips_elf_output_dynamic_relocation
   2921  1.1  christos 	      (abfd, sreloc, sreloc->reloc_count++, indx,
   2922  1.1  christos 	       ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
   2923  1.1  christos 	       sgot->output_offset + sgot->output_section->vma + offset2);
   2924  1.1  christos 	  else
   2925  1.1  christos 	    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
   2926  1.1  christos 			       sgot->contents + offset2);
   2927  1.1  christos 	}
   2928  1.1  christos       else
   2929  1.1  christos 	{
   2930  1.1  christos 	  MIPS_ELF_PUT_WORD (abfd, 1,
   2931  1.1  christos 			     sgot->contents + offset);
   2932  1.1  christos 	  MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
   2933  1.1  christos 			     sgot->contents + offset2);
   2934  1.1  christos 	}
   2935  1.1  christos 
   2936  1.1  christos       got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
   2937  1.1  christos     }
   2938  1.1  christos 
   2939  1.1  christos   /* Initial Exec model.  */
   2940  1.1  christos   if (*tls_type_p & GOT_TLS_IE)
   2941  1.1  christos     {
   2942  1.1  christos       offset = got_offset;
   2943  1.1  christos 
   2944  1.1  christos       if (need_relocs)
   2945  1.1  christos 	{
   2946  1.1  christos 	  if (indx == 0)
   2947  1.1  christos 	    MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
   2948  1.1  christos 			       sgot->contents + offset);
   2949  1.1  christos 	  else
   2950  1.1  christos 	    MIPS_ELF_PUT_WORD (abfd, 0,
   2951  1.1  christos 			       sgot->contents + offset);
   2952  1.1  christos 
   2953  1.1  christos 	  mips_elf_output_dynamic_relocation
   2954  1.1  christos 	    (abfd, sreloc, sreloc->reloc_count++, indx,
   2955  1.1  christos 	     ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
   2956  1.1  christos 	     sgot->output_offset + sgot->output_section->vma + offset);
   2957  1.1  christos 	}
   2958  1.1  christos       else
   2959  1.1  christos 	MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
   2960  1.1  christos 			   sgot->contents + offset);
   2961  1.1  christos     }
   2962  1.1  christos 
   2963  1.1  christos   if (*tls_type_p & GOT_TLS_LDM)
   2964  1.1  christos     {
   2965  1.1  christos       /* The initial offset is zero, and the LD offsets will include the
   2966  1.1  christos 	 bias by DTP_OFFSET.  */
   2967  1.1  christos       MIPS_ELF_PUT_WORD (abfd, 0,
   2968  1.1  christos 			 sgot->contents + got_offset
   2969  1.1  christos 			 + MIPS_ELF_GOT_SIZE (abfd));
   2970  1.1  christos 
   2971  1.1  christos       if (!info->shared)
   2972  1.1  christos 	MIPS_ELF_PUT_WORD (abfd, 1,
   2973  1.1  christos 			   sgot->contents + got_offset);
   2974  1.1  christos       else
   2975  1.1  christos 	mips_elf_output_dynamic_relocation
   2976  1.1  christos 	  (abfd, sreloc, sreloc->reloc_count++, indx,
   2977  1.1  christos 	   ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
   2978  1.1  christos 	   sgot->output_offset + sgot->output_section->vma + got_offset);
   2979  1.1  christos     }
   2980  1.1  christos 
   2981  1.1  christos   *tls_type_p |= GOT_TLS_DONE;
   2982  1.1  christos }
   2983  1.1  christos 
   2984  1.1  christos /* Return the GOT index to use for a relocation of type R_TYPE against
   2985  1.1  christos    a symbol accessed using TLS_TYPE models.  The GOT entries for this
   2986  1.1  christos    symbol in this GOT start at GOT_INDEX.  This function initializes the
   2987  1.1  christos    GOT entries and corresponding relocations.  */
   2988  1.1  christos 
   2989  1.1  christos static bfd_vma
   2990  1.1  christos mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
   2991  1.1  christos 		    int r_type, struct bfd_link_info *info,
   2992  1.1  christos 		    struct mips_elf_link_hash_entry *h, bfd_vma symbol)
   2993  1.1  christos {
   2994  1.1  christos   BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
   2995  1.1  christos 	      || r_type == R_MIPS_TLS_LDM);
   2996  1.1  christos 
   2997  1.1  christos   mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
   2998  1.1  christos 
   2999  1.1  christos   if (r_type == R_MIPS_TLS_GOTTPREL)
   3000  1.1  christos     {
   3001  1.1  christos       BFD_ASSERT (*tls_type & GOT_TLS_IE);
   3002  1.1  christos       if (*tls_type & GOT_TLS_GD)
   3003  1.1  christos 	return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
   3004  1.1  christos       else
   3005  1.1  christos 	return got_index;
   3006  1.1  christos     }
   3007  1.1  christos 
   3008  1.1  christos   if (r_type == R_MIPS_TLS_GD)
   3009  1.1  christos     {
   3010  1.1  christos       BFD_ASSERT (*tls_type & GOT_TLS_GD);
   3011  1.1  christos       return got_index;
   3012  1.1  christos     }
   3013  1.1  christos 
   3014  1.1  christos   if (r_type == R_MIPS_TLS_LDM)
   3015  1.1  christos     {
   3016  1.1  christos       BFD_ASSERT (*tls_type & GOT_TLS_LDM);
   3017  1.1  christos       return got_index;
   3018  1.1  christos     }
   3019  1.1  christos 
   3020  1.1  christos   return got_index;
   3021  1.1  christos }
   3022  1.1  christos 
   3023  1.1  christos /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
   3024  1.1  christos    for global symbol H.  .got.plt comes before the GOT, so the offset
   3025  1.1  christos    will be negative.  */
   3026  1.1  christos 
   3027  1.1  christos static bfd_vma
   3028  1.1  christos mips_elf_gotplt_index (struct bfd_link_info *info,
   3029  1.1  christos 		       struct elf_link_hash_entry *h)
   3030  1.1  christos {
   3031  1.1  christos   bfd_vma plt_index, got_address, got_value;
   3032  1.1  christos   struct mips_elf_link_hash_table *htab;
   3033  1.1  christos 
   3034  1.1  christos   htab = mips_elf_hash_table (info);
   3035  1.1  christos   BFD_ASSERT (htab != NULL);
   3036  1.1  christos 
   3037  1.1  christos   BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
   3038  1.1  christos 
   3039  1.1  christos   /* This function only works for VxWorks, because a non-VxWorks .got.plt
   3040  1.1  christos      section starts with reserved entries.  */
   3041  1.1  christos   BFD_ASSERT (htab->is_vxworks);
   3042  1.1  christos 
   3043  1.1  christos   /* Calculate the index of the symbol's PLT entry.  */
   3044  1.1  christos   plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
   3045  1.1  christos 
   3046  1.1  christos   /* Calculate the address of the associated .got.plt entry.  */
   3047  1.1  christos   got_address = (htab->sgotplt->output_section->vma
   3048  1.1  christos 		 + htab->sgotplt->output_offset
   3049  1.1  christos 		 + plt_index * 4);
   3050  1.1  christos 
   3051  1.1  christos   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
   3052  1.1  christos   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
   3053  1.1  christos 	       + htab->root.hgot->root.u.def.section->output_offset
   3054  1.1  christos 	       + htab->root.hgot->root.u.def.value);
   3055  1.1  christos 
   3056  1.1  christos   return got_address - got_value;
   3057  1.1  christos }
   3058  1.1  christos 
   3059  1.1  christos /* Return the GOT offset for address VALUE.   If there is not yet a GOT
   3060  1.1  christos    entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
   3061  1.1  christos    create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
   3062  1.1  christos    offset can be found.  */
   3063  1.1  christos 
   3064  1.1  christos static bfd_vma
   3065  1.1  christos mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3066  1.1  christos 			  bfd_vma value, unsigned long r_symndx,
   3067  1.1  christos 			  struct mips_elf_link_hash_entry *h, int r_type)
   3068  1.1  christos {
   3069  1.1  christos   struct mips_elf_link_hash_table *htab;
   3070  1.1  christos   struct mips_got_entry *entry;
   3071  1.1  christos 
   3072  1.1  christos   htab = mips_elf_hash_table (info);
   3073  1.1  christos   BFD_ASSERT (htab != NULL);
   3074  1.1  christos 
   3075  1.1  christos   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
   3076  1.1  christos 					   r_symndx, h, r_type);
   3077  1.1  christos   if (!entry)
   3078  1.1  christos     return MINUS_ONE;
   3079  1.1  christos 
   3080  1.1  christos   if (TLS_RELOC_P (r_type))
   3081  1.1  christos     {
   3082  1.1  christos       if (entry->symndx == -1 && htab->got_info->next == NULL)
   3083  1.1  christos 	/* A type (3) entry in the single-GOT case.  We use the symbol's
   3084  1.1  christos 	   hash table entry to track the index.  */
   3085  1.1  christos 	return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
   3086  1.1  christos 				   r_type, info, h, value);
   3087  1.1  christos       else
   3088  1.1  christos 	return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
   3089  1.1  christos 				   r_type, info, h, value);
   3090  1.1  christos     }
   3091  1.1  christos   else
   3092  1.1  christos     return entry->gotidx;
   3093  1.1  christos }
   3094  1.1  christos 
   3095  1.1  christos /* Returns the GOT index for the global symbol indicated by H.  */
   3096  1.1  christos 
   3097  1.1  christos static bfd_vma
   3098  1.1  christos mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
   3099  1.1  christos 			   int r_type, struct bfd_link_info *info)
   3100  1.1  christos {
   3101  1.1  christos   struct mips_elf_link_hash_table *htab;
   3102  1.1  christos   bfd_vma got_index;
   3103  1.1  christos   struct mips_got_info *g, *gg;
   3104  1.1  christos   long global_got_dynindx = 0;
   3105  1.1  christos 
   3106  1.1  christos   htab = mips_elf_hash_table (info);
   3107  1.1  christos   BFD_ASSERT (htab != NULL);
   3108  1.1  christos 
   3109  1.1  christos   gg = g = htab->got_info;
   3110  1.1  christos   if (g->bfd2got && ibfd)
   3111  1.1  christos     {
   3112  1.1  christos       struct mips_got_entry e, *p;
   3113  1.1  christos 
   3114  1.1  christos       BFD_ASSERT (h->dynindx >= 0);
   3115  1.1  christos 
   3116  1.1  christos       g = mips_elf_got_for_ibfd (g, ibfd);
   3117  1.1  christos       if (g->next != gg || TLS_RELOC_P (r_type))
   3118  1.1  christos 	{
   3119  1.1  christos 	  e.abfd = ibfd;
   3120  1.1  christos 	  e.symndx = -1;
   3121  1.1  christos 	  e.d.h = (struct mips_elf_link_hash_entry *)h;
   3122  1.1  christos 	  e.tls_type = 0;
   3123  1.1  christos 
   3124  1.1  christos 	  p = htab_find (g->got_entries, &e);
   3125  1.1  christos 
   3126  1.1  christos 	  BFD_ASSERT (p->gotidx > 0);
   3127  1.1  christos 
   3128  1.1  christos 	  if (TLS_RELOC_P (r_type))
   3129  1.1  christos 	    {
   3130  1.1  christos 	      bfd_vma value = MINUS_ONE;
   3131  1.1  christos 	      if ((h->root.type == bfd_link_hash_defined
   3132  1.1  christos 		   || h->root.type == bfd_link_hash_defweak)
   3133  1.1  christos 		  && h->root.u.def.section->output_section)
   3134  1.1  christos 		value = (h->root.u.def.value
   3135  1.1  christos 			 + h->root.u.def.section->output_offset
   3136  1.1  christos 			 + h->root.u.def.section->output_section->vma);
   3137  1.1  christos 
   3138  1.1  christos 	      return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
   3139  1.1  christos 					 info, e.d.h, value);
   3140  1.1  christos 	    }
   3141  1.1  christos 	  else
   3142  1.1  christos 	    return p->gotidx;
   3143  1.1  christos 	}
   3144  1.1  christos     }
   3145  1.1  christos 
   3146  1.1  christos   if (gg->global_gotsym != NULL)
   3147  1.1  christos     global_got_dynindx = gg->global_gotsym->dynindx;
   3148  1.1  christos 
   3149  1.1  christos   if (TLS_RELOC_P (r_type))
   3150  1.1  christos     {
   3151  1.1  christos       struct mips_elf_link_hash_entry *hm
   3152  1.1  christos 	= (struct mips_elf_link_hash_entry *) h;
   3153  1.1  christos       bfd_vma value = MINUS_ONE;
   3154  1.1  christos 
   3155  1.1  christos       if ((h->root.type == bfd_link_hash_defined
   3156  1.1  christos 	   || h->root.type == bfd_link_hash_defweak)
   3157  1.1  christos 	  && h->root.u.def.section->output_section)
   3158  1.1  christos 	value = (h->root.u.def.value
   3159  1.1  christos 		 + h->root.u.def.section->output_offset
   3160  1.1  christos 		 + h->root.u.def.section->output_section->vma);
   3161  1.1  christos 
   3162  1.1  christos       got_index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
   3163  1.1  christos 				      r_type, info, hm, value);
   3164  1.1  christos     }
   3165  1.1  christos   else
   3166  1.1  christos     {
   3167  1.1  christos       /* Once we determine the global GOT entry with the lowest dynamic
   3168  1.1  christos 	 symbol table index, we must put all dynamic symbols with greater
   3169  1.1  christos 	 indices into the GOT.  That makes it easy to calculate the GOT
   3170  1.1  christos 	 offset.  */
   3171  1.1  christos       BFD_ASSERT (h->dynindx >= global_got_dynindx);
   3172  1.1  christos       got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
   3173  1.1  christos 		   * MIPS_ELF_GOT_SIZE (abfd));
   3174  1.1  christos     }
   3175  1.1  christos   BFD_ASSERT (got_index < htab->sgot->size);
   3176  1.1  christos 
   3177  1.1  christos   return got_index;
   3178  1.1  christos }
   3179  1.1  christos 
   3180  1.1  christos /* Find a GOT page entry that points to within 32KB of VALUE.  These
   3181  1.1  christos    entries are supposed to be placed at small offsets in the GOT, i.e.,
   3182  1.1  christos    within 32KB of GP.  Return the index of the GOT entry, or -1 if no
   3183  1.1  christos    entry could be created.  If OFFSETP is nonnull, use it to return the
   3184  1.1  christos    offset of the GOT entry from VALUE.  */
   3185  1.1  christos 
   3186  1.1  christos static bfd_vma
   3187  1.1  christos mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3188  1.1  christos 		   bfd_vma value, bfd_vma *offsetp)
   3189  1.1  christos {
   3190  1.1  christos   bfd_vma page, got_index;
   3191  1.1  christos   struct mips_got_entry *entry;
   3192  1.1  christos 
   3193  1.1  christos   page = (value + 0x8000) & ~(bfd_vma) 0xffff;
   3194  1.1  christos   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
   3195  1.1  christos 					   NULL, R_MIPS_GOT_PAGE);
   3196  1.1  christos 
   3197  1.1  christos   if (!entry)
   3198  1.1  christos     return MINUS_ONE;
   3199  1.1  christos 
   3200  1.1  christos   got_index = entry->gotidx;
   3201  1.1  christos 
   3202  1.1  christos   if (offsetp)
   3203  1.1  christos     *offsetp = value - entry->d.address;
   3204  1.1  christos 
   3205  1.1  christos   return got_index;
   3206  1.1  christos }
   3207  1.1  christos 
   3208  1.1  christos /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
   3209  1.1  christos    EXTERNAL is true if the relocation was originally against a global
   3210  1.1  christos    symbol that binds locally.  */
   3211  1.1  christos 
   3212  1.1  christos static bfd_vma
   3213  1.1  christos mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
   3214  1.1  christos 		      bfd_vma value, bfd_boolean external)
   3215  1.1  christos {
   3216  1.1  christos   struct mips_got_entry *entry;
   3217  1.1  christos 
   3218  1.1  christos   /* GOT16 relocations against local symbols are followed by a LO16
   3219  1.1  christos      relocation; those against global symbols are not.  Thus if the
   3220  1.1  christos      symbol was originally local, the GOT16 relocation should load the
   3221  1.1  christos      equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
   3222  1.1  christos   if (! external)
   3223  1.1  christos     value = mips_elf_high (value) << 16;
   3224  1.1  christos 
   3225  1.1  christos   /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
   3226  1.1  christos      R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
   3227  1.1  christos      same in all cases.  */
   3228  1.1  christos   entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
   3229  1.1  christos 					   NULL, R_MIPS_GOT16);
   3230  1.1  christos   if (entry)
   3231  1.1  christos     return entry->gotidx;
   3232  1.1  christos   else
   3233  1.1  christos     return MINUS_ONE;
   3234  1.1  christos }
   3235  1.1  christos 
   3236  1.1  christos /* Returns the offset for the entry at the INDEXth position
   3237  1.1  christos    in the GOT.  */
   3238  1.1  christos 
   3239  1.1  christos static bfd_vma
   3240  1.1  christos mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
   3241  1.1  christos 				bfd *input_bfd, bfd_vma got_index)
   3242  1.1  christos {
   3243  1.1  christos   struct mips_elf_link_hash_table *htab;
   3244  1.1  christos   asection *sgot;
   3245  1.1  christos   bfd_vma gp;
   3246  1.1  christos 
   3247  1.1  christos   htab = mips_elf_hash_table (info);
   3248  1.1  christos   BFD_ASSERT (htab != NULL);
   3249  1.1  christos 
   3250  1.1  christos   sgot = htab->sgot;
   3251  1.1  christos   gp = _bfd_get_gp_value (output_bfd)
   3252  1.1  christos     + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
   3253  1.1  christos 
   3254  1.1  christos   return sgot->output_section->vma + sgot->output_offset + got_index - gp;
   3255  1.1  christos }
   3256  1.1  christos 
   3257  1.1  christos /* Create and return a local GOT entry for VALUE, which was calculated
   3258  1.1  christos    from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
   3259  1.1  christos    be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
   3260  1.1  christos    instead.  */
   3261  1.1  christos 
   3262  1.1  christos static struct mips_got_entry *
   3263  1.1  christos mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
   3264  1.1  christos 				 bfd *ibfd, bfd_vma value,
   3265  1.1  christos 				 unsigned long r_symndx,
   3266  1.1  christos 				 struct mips_elf_link_hash_entry *h,
   3267  1.1  christos 				 int r_type)
   3268  1.1  christos {
   3269  1.1  christos   struct mips_got_entry entry, **loc;
   3270  1.1  christos   struct mips_got_info *g;
   3271  1.1  christos   struct mips_elf_link_hash_table *htab;
   3272  1.1  christos 
   3273  1.1  christos   htab = mips_elf_hash_table (info);
   3274  1.1  christos   BFD_ASSERT (htab != NULL);
   3275  1.1  christos 
   3276  1.1  christos   entry.abfd = NULL;
   3277  1.1  christos   entry.symndx = -1;
   3278  1.1  christos   entry.d.address = value;
   3279  1.1  christos   entry.tls_type = 0;
   3280  1.1  christos 
   3281  1.1  christos   g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
   3282  1.1  christos   if (g == NULL)
   3283  1.1  christos     {
   3284  1.1  christos       g = mips_elf_got_for_ibfd (htab->got_info, abfd);
   3285  1.1  christos       BFD_ASSERT (g != NULL);
   3286  1.1  christos     }
   3287  1.1  christos 
   3288  1.1  christos   /* This function shouldn't be called for symbols that live in the global
   3289  1.1  christos      area of the GOT.  */
   3290  1.1  christos   BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
   3291  1.1  christos   if (TLS_RELOC_P (r_type))
   3292  1.1  christos     {
   3293  1.1  christos       struct mips_got_entry *p;
   3294  1.1  christos 
   3295  1.1  christos       entry.abfd = ibfd;
   3296  1.1  christos       if (r_type == R_MIPS_TLS_LDM)
   3297  1.1  christos 	{
   3298  1.1  christos 	  entry.tls_type = GOT_TLS_LDM;
   3299  1.1  christos 	  entry.symndx = 0;
   3300  1.1  christos 	  entry.d.addend = 0;
   3301  1.1  christos 	}
   3302  1.1  christos       else if (h == NULL)
   3303  1.1  christos 	{
   3304  1.1  christos 	  entry.symndx = r_symndx;
   3305  1.1  christos 	  entry.d.addend = 0;
   3306  1.1  christos 	}
   3307  1.1  christos       else
   3308  1.1  christos 	entry.d.h = h;
   3309  1.1  christos 
   3310  1.1  christos       p = (struct mips_got_entry *)
   3311  1.1  christos 	htab_find (g->got_entries, &entry);
   3312  1.1  christos 
   3313  1.1  christos       BFD_ASSERT (p);
   3314  1.1  christos       return p;
   3315  1.1  christos     }
   3316  1.1  christos 
   3317  1.1  christos   loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
   3318  1.1  christos 						   INSERT);
   3319  1.1  christos   if (*loc)
   3320  1.1  christos     return *loc;
   3321  1.1  christos 
   3322  1.1  christos   entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
   3323  1.1  christos   entry.tls_type = 0;
   3324  1.1  christos 
   3325  1.1  christos   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
   3326  1.1  christos 
   3327  1.1  christos   if (! *loc)
   3328  1.1  christos     return NULL;
   3329  1.1  christos 
   3330  1.1  christos   memcpy (*loc, &entry, sizeof entry);
   3331  1.1  christos 
   3332  1.1  christos   if (g->assigned_gotno > g->local_gotno)
   3333  1.1  christos     {
   3334  1.1  christos       (*loc)->gotidx = -1;
   3335  1.1  christos       /* We didn't allocate enough space in the GOT.  */
   3336  1.1  christos       (*_bfd_error_handler)
   3337  1.1  christos 	(_("not enough GOT space for local GOT entries"));
   3338  1.1  christos       bfd_set_error (bfd_error_bad_value);
   3339  1.1  christos       return NULL;
   3340  1.1  christos     }
   3341  1.1  christos 
   3342  1.1  christos   MIPS_ELF_PUT_WORD (abfd, value,
   3343  1.1  christos 		     (htab->sgot->contents + entry.gotidx));
   3344  1.1  christos 
   3345  1.1  christos   /* These GOT entries need a dynamic relocation on VxWorks.  */
   3346  1.1  christos   if (htab->is_vxworks)
   3347  1.1  christos     {
   3348  1.1  christos       Elf_Internal_Rela outrel;
   3349  1.1  christos       asection *s;
   3350  1.1  christos       bfd_byte *rloc;
   3351  1.1  christos       bfd_vma got_address;
   3352  1.1  christos 
   3353  1.1  christos       s = mips_elf_rel_dyn_section (info, FALSE);
   3354  1.1  christos       got_address = (htab->sgot->output_section->vma
   3355  1.1  christos 		     + htab->sgot->output_offset
   3356  1.1  christos 		     + entry.gotidx);
   3357  1.1  christos 
   3358  1.1  christos       rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
   3359  1.1  christos       outrel.r_offset = got_address;
   3360  1.1  christos       outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
   3361  1.1  christos       outrel.r_addend = value;
   3362  1.1  christos       bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
   3363  1.1  christos     }
   3364  1.1  christos 
   3365  1.1  christos   return *loc;
   3366  1.1  christos }
   3367  1.1  christos 
   3368  1.1  christos /* Return the number of dynamic section symbols required by OUTPUT_BFD.
   3369  1.1  christos    The number might be exact or a worst-case estimate, depending on how
   3370  1.1  christos    much information is available to elf_backend_omit_section_dynsym at
   3371  1.1  christos    the current linking stage.  */
   3372  1.1  christos 
   3373  1.1  christos static bfd_size_type
   3374  1.1  christos count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
   3375  1.1  christos {
   3376  1.1  christos   bfd_size_type count;
   3377  1.1  christos 
   3378  1.1  christos   count = 0;
   3379  1.1  christos   if (info->shared || elf_hash_table (info)->is_relocatable_executable)
   3380  1.1  christos     {
   3381  1.1  christos       asection *p;
   3382  1.1  christos       const struct elf_backend_data *bed;
   3383  1.1  christos 
   3384  1.1  christos       bed = get_elf_backend_data (output_bfd);
   3385  1.1  christos       for (p = output_bfd->sections; p ; p = p->next)
   3386  1.1  christos 	if ((p->flags & SEC_EXCLUDE) == 0
   3387  1.1  christos 	    && (p->flags & SEC_ALLOC) != 0
   3388  1.1  christos 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
   3389  1.1  christos 	  ++count;
   3390  1.1  christos     }
   3391  1.1  christos   return count;
   3392  1.1  christos }
   3393  1.1  christos 
   3394  1.1  christos /* Sort the dynamic symbol table so that symbols that need GOT entries
   3395  1.1  christos    appear towards the end.  */
   3396  1.1  christos 
   3397  1.1  christos static bfd_boolean
   3398  1.1  christos mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
   3399  1.1  christos {
   3400  1.1  christos   struct mips_elf_link_hash_table *htab;
   3401  1.1  christos   struct mips_elf_hash_sort_data hsd;
   3402  1.1  christos   struct mips_got_info *g;
   3403  1.1  christos 
   3404  1.1  christos   if (elf_hash_table (info)->dynsymcount == 0)
   3405  1.1  christos     return TRUE;
   3406  1.1  christos 
   3407  1.1  christos   htab = mips_elf_hash_table (info);
   3408  1.1  christos   BFD_ASSERT (htab != NULL);
   3409  1.1  christos 
   3410  1.1  christos   g = htab->got_info;
   3411  1.1  christos   if (g == NULL)
   3412  1.1  christos     return TRUE;
   3413  1.1  christos 
   3414  1.1  christos   hsd.low = NULL;
   3415  1.1  christos   hsd.max_unref_got_dynindx
   3416  1.1  christos     = hsd.min_got_dynindx
   3417  1.1  christos     = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
   3418  1.1  christos   hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
   3419  1.1  christos   mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
   3420  1.1  christos 				elf_hash_table (info)),
   3421  1.1  christos 			       mips_elf_sort_hash_table_f,
   3422  1.1  christos 			       &hsd);
   3423  1.1  christos 
   3424  1.1  christos   /* There should have been enough room in the symbol table to
   3425  1.1  christos      accommodate both the GOT and non-GOT symbols.  */
   3426  1.1  christos   BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
   3427  1.1  christos   BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
   3428  1.1  christos 	      == elf_hash_table (info)->dynsymcount);
   3429  1.1  christos   BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
   3430  1.1  christos 	      == g->global_gotno);
   3431  1.1  christos 
   3432  1.1  christos   /* Now we know which dynamic symbol has the lowest dynamic symbol
   3433  1.1  christos      table index in the GOT.  */
   3434  1.1  christos   g->global_gotsym = hsd.low;
   3435  1.1  christos 
   3436  1.1  christos   return TRUE;
   3437  1.1  christos }
   3438  1.1  christos 
   3439  1.1  christos /* If H needs a GOT entry, assign it the highest available dynamic
   3440  1.1  christos    index.  Otherwise, assign it the lowest available dynamic
   3441  1.1  christos    index.  */
   3442  1.1  christos 
   3443  1.1  christos static bfd_boolean
   3444  1.1  christos mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
   3445  1.1  christos {
   3446  1.1  christos   struct mips_elf_hash_sort_data *hsd = data;
   3447  1.1  christos 
   3448  1.1  christos   if (h->root.root.type == bfd_link_hash_warning)
   3449  1.1  christos     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   3450  1.1  christos 
   3451  1.1  christos   /* Symbols without dynamic symbol table entries aren't interesting
   3452  1.1  christos      at all.  */
   3453  1.1  christos   if (h->root.dynindx == -1)
   3454  1.1  christos     return TRUE;
   3455  1.1  christos 
   3456  1.1  christos   switch (h->global_got_area)
   3457  1.1  christos     {
   3458  1.1  christos     case GGA_NONE:
   3459  1.1  christos       h->root.dynindx = hsd->max_non_got_dynindx++;
   3460  1.1  christos       break;
   3461  1.1  christos 
   3462  1.1  christos     case GGA_NORMAL:
   3463  1.1  christos       BFD_ASSERT (h->tls_type == GOT_NORMAL);
   3464  1.1  christos 
   3465  1.1  christos       h->root.dynindx = --hsd->min_got_dynindx;
   3466  1.1  christos       hsd->low = (struct elf_link_hash_entry *) h;
   3467  1.1  christos       break;
   3468  1.1  christos 
   3469  1.1  christos     case GGA_RELOC_ONLY:
   3470  1.1  christos       BFD_ASSERT (h->tls_type == GOT_NORMAL);
   3471  1.1  christos 
   3472  1.1  christos       if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
   3473  1.1  christos 	hsd->low = (struct elf_link_hash_entry *) h;
   3474  1.1  christos       h->root.dynindx = hsd->max_unref_got_dynindx++;
   3475  1.1  christos       break;
   3476  1.1  christos     }
   3477  1.1  christos 
   3478  1.1  christos   return TRUE;
   3479  1.1  christos }
   3480  1.1  christos 
   3481  1.1  christos /* If H is a symbol that needs a global GOT entry, but has a dynamic
   3482  1.1  christos    symbol table index lower than any we've seen to date, record it for
   3483  1.1  christos    posterity.  FOR_CALL is true if the caller is only interested in
   3484  1.1  christos    using the GOT entry for calls.  */
   3485  1.1  christos 
   3486  1.1  christos static bfd_boolean
   3487  1.1  christos mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
   3488  1.1  christos 				   bfd *abfd, struct bfd_link_info *info,
   3489  1.1  christos 				   bfd_boolean for_call,
   3490  1.1  christos 				   unsigned char tls_flag)
   3491  1.1  christos {
   3492  1.1  christos   struct mips_elf_link_hash_table *htab;
   3493  1.1  christos   struct mips_elf_link_hash_entry *hmips;
   3494  1.1  christos   struct mips_got_entry entry, **loc;
   3495  1.1  christos   struct mips_got_info *g;
   3496  1.1  christos 
   3497  1.1  christos   htab = mips_elf_hash_table (info);
   3498  1.1  christos   BFD_ASSERT (htab != NULL);
   3499  1.1  christos 
   3500  1.1  christos   hmips = (struct mips_elf_link_hash_entry *) h;
   3501  1.1  christos   if (!for_call)
   3502  1.1  christos     hmips->got_only_for_calls = FALSE;
   3503  1.1  christos 
   3504  1.1  christos   /* A global symbol in the GOT must also be in the dynamic symbol
   3505  1.1  christos      table.  */
   3506  1.1  christos   if (h->dynindx == -1)
   3507  1.1  christos     {
   3508  1.1  christos       switch (ELF_ST_VISIBILITY (h->other))
   3509  1.1  christos 	{
   3510  1.1  christos 	case STV_INTERNAL:
   3511  1.1  christos 	case STV_HIDDEN:
   3512  1.1  christos 	  _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
   3513  1.1  christos 	  break;
   3514  1.1  christos 	}
   3515  1.1  christos       if (!bfd_elf_link_record_dynamic_symbol (info, h))
   3516  1.1  christos 	return FALSE;
   3517  1.1  christos     }
   3518  1.1  christos 
   3519  1.1  christos   /* Make sure we have a GOT to put this entry into.  */
   3520  1.1  christos   g = htab->got_info;
   3521  1.1  christos   BFD_ASSERT (g != NULL);
   3522  1.1  christos 
   3523  1.1  christos   entry.abfd = abfd;
   3524  1.1  christos   entry.symndx = -1;
   3525  1.1  christos   entry.d.h = (struct mips_elf_link_hash_entry *) h;
   3526  1.1  christos   entry.tls_type = 0;
   3527  1.1  christos 
   3528  1.1  christos   loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
   3529  1.1  christos 						   INSERT);
   3530  1.1  christos 
   3531  1.1  christos   /* If we've already marked this entry as needing GOT space, we don't
   3532  1.1  christos      need to do it again.  */
   3533  1.1  christos   if (*loc)
   3534  1.1  christos     {
   3535  1.1  christos       (*loc)->tls_type |= tls_flag;
   3536  1.1  christos       return TRUE;
   3537  1.1  christos     }
   3538  1.1  christos 
   3539  1.1  christos   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
   3540  1.1  christos 
   3541  1.1  christos   if (! *loc)
   3542  1.1  christos     return FALSE;
   3543  1.1  christos 
   3544  1.1  christos   entry.gotidx = -1;
   3545  1.1  christos   entry.tls_type = tls_flag;
   3546  1.1  christos 
   3547  1.1  christos   memcpy (*loc, &entry, sizeof entry);
   3548  1.1  christos 
   3549  1.1  christos   if (tls_flag == 0)
   3550  1.1  christos     hmips->global_got_area = GGA_NORMAL;
   3551  1.1  christos 
   3552  1.1  christos   return TRUE;
   3553  1.1  christos }
   3554  1.1  christos 
   3555  1.1  christos /* Reserve space in G for a GOT entry containing the value of symbol
   3556  1.1  christos    SYMNDX in input bfd ABDF, plus ADDEND.  */
   3557  1.1  christos 
   3558  1.1  christos static bfd_boolean
   3559  1.1  christos mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
   3560  1.1  christos 				  struct bfd_link_info *info,
   3561  1.1  christos 				  unsigned char tls_flag)
   3562  1.1  christos {
   3563  1.1  christos   struct mips_elf_link_hash_table *htab;
   3564  1.1  christos   struct mips_got_info *g;
   3565  1.1  christos   struct mips_got_entry entry, **loc;
   3566  1.1  christos 
   3567  1.1  christos   htab = mips_elf_hash_table (info);
   3568  1.1  christos   BFD_ASSERT (htab != NULL);
   3569  1.1  christos 
   3570  1.1  christos   g = htab->got_info;
   3571  1.1  christos   BFD_ASSERT (g != NULL);
   3572  1.1  christos 
   3573  1.1  christos   entry.abfd = abfd;
   3574  1.1  christos   entry.symndx = symndx;
   3575  1.1  christos   entry.d.addend = addend;
   3576  1.1  christos   entry.tls_type = tls_flag;
   3577  1.1  christos   loc = (struct mips_got_entry **)
   3578  1.1  christos     htab_find_slot (g->got_entries, &entry, INSERT);
   3579  1.1  christos 
   3580  1.1  christos   if (*loc)
   3581  1.1  christos     {
   3582  1.1  christos       if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
   3583  1.1  christos 	{
   3584  1.1  christos 	  g->tls_gotno += 2;
   3585  1.1  christos 	  (*loc)->tls_type |= tls_flag;
   3586  1.1  christos 	}
   3587  1.1  christos       else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
   3588  1.1  christos 	{
   3589  1.1  christos 	  g->tls_gotno += 1;
   3590  1.1  christos 	  (*loc)->tls_type |= tls_flag;
   3591  1.1  christos 	}
   3592  1.1  christos       return TRUE;
   3593  1.1  christos     }
   3594  1.1  christos 
   3595  1.1  christos   if (tls_flag != 0)
   3596  1.1  christos     {
   3597  1.1  christos       entry.gotidx = -1;
   3598  1.1  christos       entry.tls_type = tls_flag;
   3599  1.1  christos       if (tls_flag == GOT_TLS_IE)
   3600  1.1  christos 	g->tls_gotno += 1;
   3601  1.1  christos       else if (tls_flag == GOT_TLS_GD)
   3602  1.1  christos 	g->tls_gotno += 2;
   3603  1.1  christos       else if (g->tls_ldm_offset == MINUS_ONE)
   3604  1.1  christos 	{
   3605  1.1  christos 	  g->tls_ldm_offset = MINUS_TWO;
   3606  1.1  christos 	  g->tls_gotno += 2;
   3607  1.1  christos 	}
   3608  1.1  christos     }
   3609  1.1  christos   else
   3610  1.1  christos     {
   3611  1.1  christos       entry.gotidx = g->local_gotno++;
   3612  1.1  christos       entry.tls_type = 0;
   3613  1.1  christos     }
   3614  1.1  christos 
   3615  1.1  christos   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
   3616  1.1  christos 
   3617  1.1  christos   if (! *loc)
   3618  1.1  christos     return FALSE;
   3619  1.1  christos 
   3620  1.1  christos   memcpy (*loc, &entry, sizeof entry);
   3621  1.1  christos 
   3622  1.1  christos   return TRUE;
   3623  1.1  christos }
   3624  1.1  christos 
   3625  1.1  christos /* Return the maximum number of GOT page entries required for RANGE.  */
   3626  1.1  christos 
   3627  1.1  christos static bfd_vma
   3628  1.1  christos mips_elf_pages_for_range (const struct mips_got_page_range *range)
   3629  1.1  christos {
   3630  1.1  christos   return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
   3631  1.1  christos }
   3632  1.1  christos 
   3633  1.1  christos /* Record that ABFD has a page relocation against symbol SYMNDX and
   3634  1.1  christos    that ADDEND is the addend for that relocation.
   3635  1.1  christos 
   3636  1.1  christos    This function creates an upper bound on the number of GOT slots
   3637  1.1  christos    required; no attempt is made to combine references to non-overridable
   3638  1.1  christos    global symbols across multiple input files.  */
   3639  1.1  christos 
   3640  1.1  christos static bfd_boolean
   3641  1.1  christos mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
   3642  1.1  christos 				long symndx, bfd_signed_vma addend)
   3643  1.1  christos {
   3644  1.1  christos   struct mips_elf_link_hash_table *htab;
   3645  1.1  christos   struct mips_got_info *g;
   3646  1.1  christos   struct mips_got_page_entry lookup, *entry;
   3647  1.1  christos   struct mips_got_page_range **range_ptr, *range;
   3648  1.1  christos   bfd_vma old_pages, new_pages;
   3649  1.1  christos   void **loc;
   3650  1.1  christos 
   3651  1.1  christos   htab = mips_elf_hash_table (info);
   3652  1.1  christos   BFD_ASSERT (htab != NULL);
   3653  1.1  christos 
   3654  1.1  christos   g = htab->got_info;
   3655  1.1  christos   BFD_ASSERT (g != NULL);
   3656  1.1  christos 
   3657  1.1  christos   /* Find the mips_got_page_entry hash table entry for this symbol.  */
   3658  1.1  christos   lookup.abfd = abfd;
   3659  1.1  christos   lookup.symndx = symndx;
   3660  1.1  christos   loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
   3661  1.1  christos   if (loc == NULL)
   3662  1.1  christos     return FALSE;
   3663  1.1  christos 
   3664  1.1  christos   /* Create a mips_got_page_entry if this is the first time we've
   3665  1.1  christos      seen the symbol.  */
   3666  1.1  christos   entry = (struct mips_got_page_entry *) *loc;
   3667  1.1  christos   if (!entry)
   3668  1.1  christos     {
   3669  1.1  christos       entry = bfd_alloc (abfd, sizeof (*entry));
   3670  1.1  christos       if (!entry)
   3671  1.1  christos 	return FALSE;
   3672  1.1  christos 
   3673  1.1  christos       entry->abfd = abfd;
   3674  1.1  christos       entry->symndx = symndx;
   3675  1.1  christos       entry->ranges = NULL;
   3676  1.1  christos       entry->num_pages = 0;
   3677  1.1  christos       *loc = entry;
   3678  1.1  christos     }
   3679  1.1  christos 
   3680  1.1  christos   /* Skip over ranges whose maximum extent cannot share a page entry
   3681  1.1  christos      with ADDEND.  */
   3682  1.1  christos   range_ptr = &entry->ranges;
   3683  1.1  christos   while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
   3684  1.1  christos     range_ptr = &(*range_ptr)->next;
   3685  1.1  christos 
   3686  1.1  christos   /* If we scanned to the end of the list, or found a range whose
   3687  1.1  christos      minimum extent cannot share a page entry with ADDEND, create
   3688  1.1  christos      a new singleton range.  */
   3689  1.1  christos   range = *range_ptr;
   3690  1.1  christos   if (!range || addend < range->min_addend - 0xffff)
   3691  1.1  christos     {
   3692  1.1  christos       range = bfd_alloc (abfd, sizeof (*range));
   3693  1.1  christos       if (!range)
   3694  1.1  christos 	return FALSE;
   3695  1.1  christos 
   3696  1.1  christos       range->next = *range_ptr;
   3697  1.1  christos       range->min_addend = addend;
   3698  1.1  christos       range->max_addend = addend;
   3699  1.1  christos 
   3700  1.1  christos       *range_ptr = range;
   3701  1.1  christos       entry->num_pages++;
   3702  1.1  christos       g->page_gotno++;
   3703  1.1  christos       return TRUE;
   3704  1.1  christos     }
   3705  1.1  christos 
   3706  1.1  christos   /* Remember how many pages the old range contributed.  */
   3707  1.1  christos   old_pages = mips_elf_pages_for_range (range);
   3708  1.1  christos 
   3709  1.1  christos   /* Update the ranges.  */
   3710  1.1  christos   if (addend < range->min_addend)
   3711  1.1  christos     range->min_addend = addend;
   3712  1.1  christos   else if (addend > range->max_addend)
   3713  1.1  christos     {
   3714  1.1  christos       if (range->next && addend >= range->next->min_addend - 0xffff)
   3715  1.1  christos 	{
   3716  1.1  christos 	  old_pages += mips_elf_pages_for_range (range->next);
   3717  1.1  christos 	  range->max_addend = range->next->max_addend;
   3718  1.1  christos 	  range->next = range->next->next;
   3719  1.1  christos 	}
   3720  1.1  christos       else
   3721  1.1  christos 	range->max_addend = addend;
   3722  1.1  christos     }
   3723  1.1  christos 
   3724  1.1  christos   /* Record any change in the total estimate.  */
   3725  1.1  christos   new_pages = mips_elf_pages_for_range (range);
   3726  1.1  christos   if (old_pages != new_pages)
   3727  1.1  christos     {
   3728  1.1  christos       entry->num_pages += new_pages - old_pages;
   3729  1.1  christos       g->page_gotno += new_pages - old_pages;
   3730  1.1  christos     }
   3731  1.1  christos 
   3732  1.1  christos   return TRUE;
   3733  1.1  christos }
   3734  1.1  christos 
   3735  1.1  christos /* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
   3736  1.1  christos 
   3737  1.1  christos static void
   3738  1.1  christos mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
   3739  1.1  christos 				       unsigned int n)
   3740  1.1  christos {
   3741  1.1  christos   asection *s;
   3742  1.1  christos   struct mips_elf_link_hash_table *htab;
   3743  1.1  christos 
   3744  1.1  christos   htab = mips_elf_hash_table (info);
   3745  1.1  christos   BFD_ASSERT (htab != NULL);
   3746  1.1  christos 
   3747  1.1  christos   s = mips_elf_rel_dyn_section (info, FALSE);
   3748  1.1  christos   BFD_ASSERT (s != NULL);
   3749  1.1  christos 
   3750  1.1  christos   if (htab->is_vxworks)
   3751  1.1  christos     s->size += n * MIPS_ELF_RELA_SIZE (abfd);
   3752  1.1  christos   else
   3753  1.1  christos     {
   3754  1.1  christos       if (s->size == 0)
   3755  1.1  christos 	{
   3756  1.1  christos 	  /* Make room for a null element.  */
   3757  1.1  christos 	  s->size += MIPS_ELF_REL_SIZE (abfd);
   3758  1.1  christos 	  ++s->reloc_count;
   3759  1.1  christos 	}
   3760  1.1  christos       s->size += n * MIPS_ELF_REL_SIZE (abfd);
   3761  1.1  christos     }
   3762  1.1  christos }
   3763  1.1  christos 
   3764  1.1  christos /* A htab_traverse callback for GOT entries.  Set boolean *DATA to true
   3766  1.1  christos    if the GOT entry is for an indirect or warning symbol.  */
   3767  1.1  christos 
   3768  1.1  christos static int
   3769  1.1  christos mips_elf_check_recreate_got (void **entryp, void *data)
   3770  1.1  christos {
   3771  1.1  christos   struct mips_got_entry *entry;
   3772  1.1  christos   bfd_boolean *must_recreate;
   3773  1.1  christos 
   3774  1.1  christos   entry = (struct mips_got_entry *) *entryp;
   3775  1.1  christos   must_recreate = (bfd_boolean *) data;
   3776  1.1  christos   if (entry->abfd != NULL && entry->symndx == -1)
   3777  1.1  christos     {
   3778  1.1  christos       struct mips_elf_link_hash_entry *h;
   3779  1.1  christos 
   3780  1.1  christos       h = entry->d.h;
   3781  1.1  christos       if (h->root.root.type == bfd_link_hash_indirect
   3782  1.1  christos 	  || h->root.root.type == bfd_link_hash_warning)
   3783  1.1  christos 	{
   3784  1.1  christos 	  *must_recreate = TRUE;
   3785  1.1  christos 	  return 0;
   3786  1.1  christos 	}
   3787  1.1  christos     }
   3788  1.1  christos   return 1;
   3789  1.1  christos }
   3790  1.1  christos 
   3791  1.1  christos /* A htab_traverse callback for GOT entries.  Add all entries to
   3792  1.1  christos    hash table *DATA, converting entries for indirect and warning
   3793  1.1  christos    symbols into entries for the target symbol.  Set *DATA to null
   3794  1.1  christos    on error.  */
   3795  1.1  christos 
   3796  1.1  christos static int
   3797  1.1  christos mips_elf_recreate_got (void **entryp, void *data)
   3798  1.1  christos {
   3799  1.1  christos   htab_t *new_got;
   3800  1.1  christos   struct mips_got_entry *entry;
   3801  1.1  christos   void **slot;
   3802  1.1  christos 
   3803  1.1  christos   new_got = (htab_t *) data;
   3804  1.1  christos   entry = (struct mips_got_entry *) *entryp;
   3805  1.1  christos   if (entry->abfd != NULL && entry->symndx == -1)
   3806  1.1  christos     {
   3807  1.1  christos       struct mips_elf_link_hash_entry *h;
   3808  1.1  christos 
   3809  1.1  christos       h = entry->d.h;
   3810  1.1  christos       while (h->root.root.type == bfd_link_hash_indirect
   3811  1.1  christos 	     || h->root.root.type == bfd_link_hash_warning)
   3812  1.1  christos 	{
   3813  1.1  christos 	  BFD_ASSERT (h->global_got_area == GGA_NONE);
   3814  1.1  christos 	  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   3815  1.1  christos 	}
   3816  1.1  christos       entry->d.h = h;
   3817  1.1  christos     }
   3818  1.1  christos   slot = htab_find_slot (*new_got, entry, INSERT);
   3819  1.1  christos   if (slot == NULL)
   3820  1.1  christos     {
   3821  1.1  christos       *new_got = NULL;
   3822  1.1  christos       return 0;
   3823  1.1  christos     }
   3824  1.1  christos   if (*slot == NULL)
   3825  1.1  christos     *slot = entry;
   3826  1.1  christos   else
   3827  1.1  christos     free (entry);
   3828  1.1  christos   return 1;
   3829  1.1  christos }
   3830  1.1  christos 
   3831  1.1  christos /* If any entries in G->got_entries are for indirect or warning symbols,
   3832  1.1  christos    replace them with entries for the target symbol.  */
   3833  1.1  christos 
   3834  1.1  christos static bfd_boolean
   3835  1.1  christos mips_elf_resolve_final_got_entries (struct mips_got_info *g)
   3836  1.1  christos {
   3837  1.1  christos   bfd_boolean must_recreate;
   3838  1.1  christos   htab_t new_got;
   3839  1.1  christos 
   3840  1.1  christos   must_recreate = FALSE;
   3841  1.1  christos   htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
   3842  1.1  christos   if (must_recreate)
   3843  1.1  christos     {
   3844  1.1  christos       new_got = htab_create (htab_size (g->got_entries),
   3845  1.1  christos 			     mips_elf_got_entry_hash,
   3846  1.1  christos 			     mips_elf_got_entry_eq, NULL);
   3847  1.1  christos       htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
   3848  1.1  christos       if (new_got == NULL)
   3849  1.1  christos 	return FALSE;
   3850  1.1  christos 
   3851  1.1  christos       /* Each entry in g->got_entries has either been copied to new_got
   3852  1.1  christos 	 or freed.  Now delete the hash table itself.  */
   3853  1.1  christos       htab_delete (g->got_entries);
   3854  1.1  christos       g->got_entries = new_got;
   3855  1.1  christos     }
   3856  1.1  christos   return TRUE;
   3857  1.1  christos }
   3858  1.1  christos 
   3859  1.1  christos /* A mips_elf_link_hash_traverse callback for which DATA points
   3860  1.1  christos    to the link_info structure.  Count the number of type (3) entries
   3861  1.1  christos    in the master GOT.  */
   3862  1.1  christos 
   3863  1.1  christos static int
   3864  1.1  christos mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
   3865  1.1  christos {
   3866  1.1  christos   struct bfd_link_info *info;
   3867  1.1  christos   struct mips_elf_link_hash_table *htab;
   3868  1.1  christos   struct mips_got_info *g;
   3869  1.1  christos 
   3870  1.1  christos   info = (struct bfd_link_info *) data;
   3871  1.1  christos   htab = mips_elf_hash_table (info);
   3872  1.1  christos   g = htab->got_info;
   3873  1.1  christos   if (h->global_got_area != GGA_NONE)
   3874  1.1  christos     {
   3875  1.1  christos       /* Make a final decision about whether the symbol belongs in the
   3876  1.1  christos 	 local or global GOT.  Symbols that bind locally can (and in the
   3877  1.1  christos 	 case of forced-local symbols, must) live in the local GOT.
   3878  1.1  christos 	 Those that are aren't in the dynamic symbol table must also
   3879  1.1  christos 	 live in the local GOT.
   3880  1.1  christos 
   3881  1.1  christos 	 Note that the former condition does not always imply the
   3882  1.1  christos 	 latter: symbols do not bind locally if they are completely
   3883  1.1  christos 	 undefined.  We'll report undefined symbols later if appropriate.  */
   3884  1.1  christos       if (h->root.dynindx == -1
   3885  1.1  christos 	  || (h->got_only_for_calls
   3886  1.1  christos 	      ? SYMBOL_CALLS_LOCAL (info, &h->root)
   3887  1.1  christos 	      : SYMBOL_REFERENCES_LOCAL (info, &h->root)))
   3888  1.1  christos 	{
   3889  1.1  christos 	  /* The symbol belongs in the local GOT.  We no longer need this
   3890  1.1  christos 	     entry if it was only used for relocations; those relocations
   3891  1.1  christos 	     will be against the null or section symbol instead of H.  */
   3892  1.1  christos 	  if (h->global_got_area != GGA_RELOC_ONLY)
   3893  1.1  christos 	    g->local_gotno++;
   3894  1.1  christos 	  h->global_got_area = GGA_NONE;
   3895  1.1  christos 	}
   3896  1.1  christos       else if (htab->is_vxworks
   3897  1.1  christos 	       && h->got_only_for_calls
   3898  1.1  christos 	       && h->root.plt.offset != MINUS_ONE)
   3899  1.1  christos 	/* On VxWorks, calls can refer directly to the .got.plt entry;
   3900  1.1  christos 	   they don't need entries in the regular GOT.  .got.plt entries
   3901  1.1  christos 	   will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
   3902  1.1  christos 	h->global_got_area = GGA_NONE;
   3903  1.1  christos       else
   3904  1.1  christos 	{
   3905  1.1  christos 	  g->global_gotno++;
   3906  1.1  christos 	  if (h->global_got_area == GGA_RELOC_ONLY)
   3907  1.1  christos 	    g->reloc_only_gotno++;
   3908  1.1  christos 	}
   3909  1.1  christos     }
   3910  1.1  christos   return 1;
   3911  1.1  christos }
   3912  1.1  christos 
   3913  1.1  christos /* Compute the hash value of the bfd in a bfd2got hash entry.  */
   3915  1.1  christos 
   3916  1.1  christos static hashval_t
   3917  1.1  christos mips_elf_bfd2got_entry_hash (const void *entry_)
   3918  1.1  christos {
   3919  1.1  christos   const struct mips_elf_bfd2got_hash *entry
   3920  1.1  christos     = (struct mips_elf_bfd2got_hash *)entry_;
   3921  1.1  christos 
   3922  1.1  christos   return entry->bfd->id;
   3923  1.1  christos }
   3924  1.1  christos 
   3925  1.1  christos /* Check whether two hash entries have the same bfd.  */
   3926  1.1  christos 
   3927  1.1  christos static int
   3928  1.1  christos mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
   3929  1.1  christos {
   3930  1.1  christos   const struct mips_elf_bfd2got_hash *e1
   3931  1.1  christos     = (const struct mips_elf_bfd2got_hash *)entry1;
   3932  1.1  christos   const struct mips_elf_bfd2got_hash *e2
   3933  1.1  christos     = (const struct mips_elf_bfd2got_hash *)entry2;
   3934  1.1  christos 
   3935  1.1  christos   return e1->bfd == e2->bfd;
   3936  1.1  christos }
   3937  1.1  christos 
   3938  1.1  christos /* In a multi-got link, determine the GOT to be used for IBFD.  G must
   3939  1.1  christos    be the master GOT data.  */
   3940  1.1  christos 
   3941  1.1  christos static struct mips_got_info *
   3942  1.1  christos mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
   3943  1.1  christos {
   3944  1.1  christos   struct mips_elf_bfd2got_hash e, *p;
   3945  1.1  christos 
   3946  1.1  christos   if (! g->bfd2got)
   3947  1.1  christos     return g;
   3948  1.1  christos 
   3949  1.1  christos   e.bfd = ibfd;
   3950  1.1  christos   p = htab_find (g->bfd2got, &e);
   3951  1.1  christos   return p ? p->g : NULL;
   3952  1.1  christos }
   3953  1.1  christos 
   3954  1.1  christos /* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
   3955  1.1  christos    Return NULL if an error occured.  */
   3956  1.1  christos 
   3957  1.1  christos static struct mips_got_info *
   3958  1.1  christos mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
   3959  1.1  christos 			  bfd *input_bfd)
   3960  1.1  christos {
   3961  1.1  christos   struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
   3962  1.1  christos   struct mips_got_info *g;
   3963  1.1  christos   void **bfdgotp;
   3964  1.1  christos 
   3965  1.1  christos   bfdgot_entry.bfd = input_bfd;
   3966  1.1  christos   bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
   3967  1.1  christos   bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
   3968  1.1  christos 
   3969  1.1  christos   if (bfdgot == NULL)
   3970  1.1  christos     {
   3971  1.1  christos       bfdgot = ((struct mips_elf_bfd2got_hash *)
   3972  1.1  christos 		bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
   3973  1.1  christos       if (bfdgot == NULL)
   3974  1.1  christos 	return NULL;
   3975  1.1  christos 
   3976  1.1  christos       *bfdgotp = bfdgot;
   3977  1.1  christos 
   3978  1.1  christos       g = ((struct mips_got_info *)
   3979  1.1  christos 	   bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
   3980  1.1  christos       if (g == NULL)
   3981  1.1  christos 	return NULL;
   3982  1.1  christos 
   3983  1.1  christos       bfdgot->bfd = input_bfd;
   3984  1.1  christos       bfdgot->g = g;
   3985  1.1  christos 
   3986  1.1  christos       g->global_gotsym = NULL;
   3987  1.1  christos       g->global_gotno = 0;
   3988  1.1  christos       g->reloc_only_gotno = 0;
   3989  1.1  christos       g->local_gotno = 0;
   3990  1.1  christos       g->page_gotno = 0;
   3991  1.1  christos       g->assigned_gotno = -1;
   3992  1.1  christos       g->tls_gotno = 0;
   3993  1.1  christos       g->tls_assigned_gotno = 0;
   3994  1.1  christos       g->tls_ldm_offset = MINUS_ONE;
   3995  1.1  christos       g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
   3996  1.1  christos 					mips_elf_multi_got_entry_eq, NULL);
   3997  1.1  christos       if (g->got_entries == NULL)
   3998  1.1  christos 	return NULL;
   3999  1.1  christos 
   4000  1.1  christos       g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
   4001  1.1  christos 					     mips_got_page_entry_eq, NULL);
   4002  1.1  christos       if (g->got_page_entries == NULL)
   4003  1.1  christos 	return NULL;
   4004  1.1  christos 
   4005  1.1  christos       g->bfd2got = NULL;
   4006  1.1  christos       g->next = NULL;
   4007  1.1  christos     }
   4008  1.1  christos 
   4009  1.1  christos   return bfdgot->g;
   4010  1.1  christos }
   4011  1.1  christos 
   4012  1.1  christos /* A htab_traverse callback for the entries in the master got.
   4013  1.1  christos    Create one separate got for each bfd that has entries in the global
   4014  1.1  christos    got, such that we can tell how many local and global entries each
   4015  1.1  christos    bfd requires.  */
   4016  1.1  christos 
   4017  1.1  christos static int
   4018  1.1  christos mips_elf_make_got_per_bfd (void **entryp, void *p)
   4019  1.1  christos {
   4020  1.1  christos   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
   4021  1.1  christos   struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
   4022  1.1  christos   struct mips_got_info *g;
   4023  1.1  christos 
   4024  1.1  christos   g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
   4025  1.1  christos   if (g == NULL)
   4026  1.1  christos     {
   4027  1.1  christos       arg->obfd = NULL;
   4028  1.1  christos       return 0;
   4029  1.1  christos     }
   4030  1.1  christos 
   4031  1.1  christos   /* Insert the GOT entry in the bfd's got entry hash table.  */
   4032  1.1  christos   entryp = htab_find_slot (g->got_entries, entry, INSERT);
   4033  1.1  christos   if (*entryp != NULL)
   4034  1.1  christos     return 1;
   4035  1.1  christos 
   4036  1.1  christos   *entryp = entry;
   4037  1.1  christos 
   4038  1.1  christos   if (entry->tls_type)
   4039  1.1  christos     {
   4040  1.1  christos       if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
   4041  1.1  christos 	g->tls_gotno += 2;
   4042  1.1  christos       if (entry->tls_type & GOT_TLS_IE)
   4043  1.1  christos 	g->tls_gotno += 1;
   4044  1.1  christos     }
   4045  1.1  christos   else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
   4046  1.1  christos     ++g->local_gotno;
   4047  1.1  christos   else
   4048  1.1  christos     ++g->global_gotno;
   4049  1.1  christos 
   4050  1.1  christos   return 1;
   4051  1.1  christos }
   4052  1.1  christos 
   4053  1.1  christos /* A htab_traverse callback for the page entries in the master got.
   4054  1.1  christos    Associate each page entry with the bfd's got.  */
   4055  1.1  christos 
   4056  1.1  christos static int
   4057  1.1  christos mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
   4058  1.1  christos {
   4059  1.1  christos   struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
   4060  1.1  christos   struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
   4061  1.1  christos   struct mips_got_info *g;
   4062  1.1  christos 
   4063  1.1  christos   g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
   4064  1.1  christos   if (g == NULL)
   4065  1.1  christos     {
   4066  1.1  christos       arg->obfd = NULL;
   4067  1.1  christos       return 0;
   4068  1.1  christos     }
   4069  1.1  christos 
   4070  1.1  christos   /* Insert the GOT entry in the bfd's got entry hash table.  */
   4071  1.1  christos   entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
   4072  1.1  christos   if (*entryp != NULL)
   4073  1.1  christos     return 1;
   4074  1.1  christos 
   4075  1.1  christos   *entryp = entry;
   4076  1.1  christos   g->page_gotno += entry->num_pages;
   4077  1.1  christos   return 1;
   4078  1.1  christos }
   4079  1.1  christos 
   4080  1.1  christos /* Consider merging the got described by BFD2GOT with TO, using the
   4081  1.1  christos    information given by ARG.  Return -1 if this would lead to overflow,
   4082  1.1  christos    1 if they were merged successfully, and 0 if a merge failed due to
   4083  1.1  christos    lack of memory.  (These values are chosen so that nonnegative return
   4084  1.1  christos    values can be returned by a htab_traverse callback.)  */
   4085  1.1  christos 
   4086  1.1  christos static int
   4087  1.1  christos mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
   4088  1.1  christos 			 struct mips_got_info *to,
   4089  1.1  christos 			 struct mips_elf_got_per_bfd_arg *arg)
   4090  1.1  christos {
   4091  1.1  christos   struct mips_got_info *from = bfd2got->g;
   4092  1.1  christos   unsigned int estimate;
   4093  1.1  christos 
   4094  1.1  christos   /* Work out how many page entries we would need for the combined GOT.  */
   4095  1.1  christos   estimate = arg->max_pages;
   4096  1.1  christos   if (estimate >= from->page_gotno + to->page_gotno)
   4097  1.1  christos     estimate = from->page_gotno + to->page_gotno;
   4098  1.1  christos 
   4099  1.1  christos   /* And conservatively estimate how many local, global and TLS entries
   4100  1.1  christos      would be needed.  */
   4101  1.1  christos   estimate += (from->local_gotno
   4102  1.1  christos 	       + from->global_gotno
   4103  1.1  christos 	       + from->tls_gotno
   4104  1.1  christos 	       + to->local_gotno
   4105  1.1  christos 	       + to->global_gotno
   4106  1.1  christos 	       + to->tls_gotno);
   4107  1.1  christos 
   4108  1.1  christos   /* Bail out if the combined GOT might be too big.  */
   4109  1.1  christos   if (estimate > arg->max_count)
   4110  1.1  christos     return -1;
   4111  1.1  christos 
   4112  1.1  christos   /* Commit to the merge.  Record that TO is now the bfd for this got.  */
   4113  1.1  christos   bfd2got->g = to;
   4114  1.1  christos 
   4115  1.1  christos   /* Transfer the bfd's got information from FROM to TO.  */
   4116  1.1  christos   htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
   4117  1.1  christos   if (arg->obfd == NULL)
   4118  1.1  christos     return 0;
   4119  1.1  christos 
   4120  1.1  christos   htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
   4121  1.1  christos   if (arg->obfd == NULL)
   4122  1.1  christos     return 0;
   4123  1.1  christos 
   4124  1.1  christos   /* We don't have to worry about releasing memory of the actual
   4125  1.1  christos      got entries, since they're all in the master got_entries hash
   4126  1.1  christos      table anyway.  */
   4127  1.1  christos   htab_delete (from->got_entries);
   4128  1.1  christos   htab_delete (from->got_page_entries);
   4129  1.1  christos   return 1;
   4130  1.1  christos }
   4131  1.1  christos 
   4132  1.1  christos /* Attempt to merge gots of different input bfds.  Try to use as much
   4133  1.1  christos    as possible of the primary got, since it doesn't require explicit
   4134  1.1  christos    dynamic relocations, but don't use bfds that would reference global
   4135  1.1  christos    symbols out of the addressable range.  Failing the primary got,
   4136  1.1  christos    attempt to merge with the current got, or finish the current got
   4137  1.1  christos    and then make make the new got current.  */
   4138  1.1  christos 
   4139  1.1  christos static int
   4140  1.1  christos mips_elf_merge_gots (void **bfd2got_, void *p)
   4141  1.1  christos {
   4142  1.1  christos   struct mips_elf_bfd2got_hash *bfd2got
   4143  1.1  christos     = (struct mips_elf_bfd2got_hash *)*bfd2got_;
   4144  1.1  christos   struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
   4145  1.1  christos   struct mips_got_info *g;
   4146  1.1  christos   unsigned int estimate;
   4147  1.1  christos   int result;
   4148  1.1  christos 
   4149  1.1  christos   g = bfd2got->g;
   4150  1.1  christos 
   4151  1.1  christos   /* Work out the number of page, local and TLS entries.  */
   4152  1.1  christos   estimate = arg->max_pages;
   4153  1.1  christos   if (estimate > g->page_gotno)
   4154  1.1  christos     estimate = g->page_gotno;
   4155  1.1  christos   estimate += g->local_gotno + g->tls_gotno;
   4156  1.1  christos 
   4157  1.1  christos   /* We place TLS GOT entries after both locals and globals.  The globals
   4158  1.1  christos      for the primary GOT may overflow the normal GOT size limit, so be
   4159  1.1  christos      sure not to merge a GOT which requires TLS with the primary GOT in that
   4160  1.1  christos      case.  This doesn't affect non-primary GOTs.  */
   4161  1.1  christos   estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
   4162  1.1  christos 
   4163  1.1  christos   if (estimate <= arg->max_count)
   4164  1.1  christos     {
   4165  1.1  christos       /* If we don't have a primary GOT, use it as
   4166  1.1  christos 	 a starting point for the primary GOT.  */
   4167  1.1  christos       if (!arg->primary)
   4168  1.1  christos 	{
   4169  1.1  christos 	  arg->primary = bfd2got->g;
   4170  1.1  christos 	  return 1;
   4171  1.1  christos 	}
   4172  1.1  christos 
   4173  1.1  christos       /* Try merging with the primary GOT.  */
   4174  1.1  christos       result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
   4175  1.1  christos       if (result >= 0)
   4176  1.1  christos 	return result;
   4177  1.1  christos     }
   4178  1.1  christos 
   4179  1.1  christos   /* If we can merge with the last-created got, do it.  */
   4180  1.1  christos   if (arg->current)
   4181  1.1  christos     {
   4182  1.1  christos       result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
   4183  1.1  christos       if (result >= 0)
   4184  1.1  christos 	return result;
   4185  1.1  christos     }
   4186  1.1  christos 
   4187  1.1  christos   /* Well, we couldn't merge, so create a new GOT.  Don't check if it
   4188  1.1  christos      fits; if it turns out that it doesn't, we'll get relocation
   4189  1.1  christos      overflows anyway.  */
   4190  1.1  christos   g->next = arg->current;
   4191  1.1  christos   arg->current = g;
   4192  1.1  christos 
   4193  1.1  christos   return 1;
   4194  1.1  christos }
   4195  1.1  christos 
   4196  1.1  christos /* Set the TLS GOT index for the GOT entry in ENTRYP.  ENTRYP's NEXT field
   4197  1.1  christos    is null iff there is just a single GOT.  */
   4198  1.1  christos 
   4199  1.1  christos static int
   4200  1.1  christos mips_elf_initialize_tls_index (void **entryp, void *p)
   4201  1.1  christos {
   4202  1.1  christos   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
   4203  1.1  christos   struct mips_got_info *g = p;
   4204  1.1  christos   bfd_vma next_index;
   4205  1.1  christos   unsigned char tls_type;
   4206  1.1  christos 
   4207  1.1  christos   /* We're only interested in TLS symbols.  */
   4208  1.1  christos   if (entry->tls_type == 0)
   4209  1.1  christos     return 1;
   4210  1.1  christos 
   4211  1.1  christos   next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
   4212  1.1  christos 
   4213  1.1  christos   if (entry->symndx == -1 && g->next == NULL)
   4214  1.1  christos     {
   4215  1.1  christos       /* A type (3) got entry in the single-GOT case.  We use the symbol's
   4216  1.1  christos 	 hash table entry to track its index.  */
   4217  1.1  christos       if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
   4218  1.1  christos 	return 1;
   4219  1.1  christos       entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
   4220  1.1  christos       entry->d.h->tls_got_offset = next_index;
   4221  1.1  christos       tls_type = entry->d.h->tls_type;
   4222  1.1  christos     }
   4223  1.1  christos   else
   4224  1.1  christos     {
   4225  1.1  christos       if (entry->tls_type & GOT_TLS_LDM)
   4226  1.1  christos 	{
   4227  1.1  christos 	  /* There are separate mips_got_entry objects for each input bfd
   4228  1.1  christos 	     that requires an LDM entry.  Make sure that all LDM entries in
   4229  1.1  christos 	     a GOT resolve to the same index.  */
   4230  1.1  christos 	  if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
   4231  1.1  christos 	    {
   4232  1.1  christos 	      entry->gotidx = g->tls_ldm_offset;
   4233  1.1  christos 	      return 1;
   4234  1.1  christos 	    }
   4235  1.1  christos 	  g->tls_ldm_offset = next_index;
   4236  1.1  christos 	}
   4237  1.1  christos       entry->gotidx = next_index;
   4238  1.1  christos       tls_type = entry->tls_type;
   4239  1.1  christos     }
   4240  1.1  christos 
   4241  1.1  christos   /* Account for the entries we've just allocated.  */
   4242  1.1  christos   if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
   4243  1.1  christos     g->tls_assigned_gotno += 2;
   4244  1.1  christos   if (tls_type & GOT_TLS_IE)
   4245  1.1  christos     g->tls_assigned_gotno += 1;
   4246  1.1  christos 
   4247  1.1  christos   return 1;
   4248  1.1  christos }
   4249  1.1  christos 
   4250  1.1  christos /* If passed a NULL mips_got_info in the argument, set the marker used
   4251  1.1  christos    to tell whether a global symbol needs a got entry (in the primary
   4252  1.1  christos    got) to the given VALUE.
   4253  1.1  christos 
   4254  1.1  christos    If passed a pointer G to a mips_got_info in the argument (it must
   4255  1.1  christos    not be the primary GOT), compute the offset from the beginning of
   4256  1.1  christos    the (primary) GOT section to the entry in G corresponding to the
   4257  1.1  christos    global symbol.  G's assigned_gotno must contain the index of the
   4258  1.1  christos    first available global GOT entry in G.  VALUE must contain the size
   4259  1.1  christos    of a GOT entry in bytes.  For each global GOT entry that requires a
   4260  1.1  christos    dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
   4261  1.1  christos    marked as not eligible for lazy resolution through a function
   4262  1.1  christos    stub.  */
   4263  1.1  christos static int
   4264  1.1  christos mips_elf_set_global_got_offset (void **entryp, void *p)
   4265  1.1  christos {
   4266  1.1  christos   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
   4267  1.1  christos   struct mips_elf_set_global_got_offset_arg *arg
   4268  1.1  christos     = (struct mips_elf_set_global_got_offset_arg *)p;
   4269  1.1  christos   struct mips_got_info *g = arg->g;
   4270  1.1  christos 
   4271  1.1  christos   if (g && entry->tls_type != GOT_NORMAL)
   4272  1.1  christos     arg->needed_relocs +=
   4273  1.1  christos       mips_tls_got_relocs (arg->info, entry->tls_type,
   4274  1.1  christos 			   entry->symndx == -1 ? &entry->d.h->root : NULL);
   4275  1.1  christos 
   4276  1.1  christos   if (entry->abfd != NULL
   4277  1.1  christos       && entry->symndx == -1
   4278  1.1  christos       && entry->d.h->global_got_area != GGA_NONE)
   4279  1.1  christos     {
   4280  1.1  christos       if (g)
   4281  1.1  christos 	{
   4282  1.1  christos 	  BFD_ASSERT (g->global_gotsym == NULL);
   4283  1.1  christos 
   4284  1.1  christos 	  entry->gotidx = arg->value * (long) g->assigned_gotno++;
   4285  1.1  christos 	  if (arg->info->shared
   4286  1.1  christos 	      || (elf_hash_table (arg->info)->dynamic_sections_created
   4287  1.1  christos 		  && entry->d.h->root.def_dynamic
   4288  1.1  christos 		  && !entry->d.h->root.def_regular))
   4289  1.1  christos 	    ++arg->needed_relocs;
   4290  1.1  christos 	}
   4291  1.1  christos       else
   4292  1.1  christos 	entry->d.h->global_got_area = arg->value;
   4293  1.1  christos     }
   4294  1.1  christos 
   4295  1.1  christos   return 1;
   4296  1.1  christos }
   4297  1.1  christos 
   4298  1.1  christos /* A htab_traverse callback for GOT entries for which DATA is the
   4299  1.1  christos    bfd_link_info.  Forbid any global symbols from having traditional
   4300  1.1  christos    lazy-binding stubs.  */
   4301  1.1  christos 
   4302  1.1  christos static int
   4303  1.1  christos mips_elf_forbid_lazy_stubs (void **entryp, void *data)
   4304  1.1  christos {
   4305  1.1  christos   struct bfd_link_info *info;
   4306  1.1  christos   struct mips_elf_link_hash_table *htab;
   4307  1.1  christos   struct mips_got_entry *entry;
   4308  1.1  christos 
   4309  1.1  christos   entry = (struct mips_got_entry *) *entryp;
   4310  1.1  christos   info = (struct bfd_link_info *) data;
   4311  1.1  christos   htab = mips_elf_hash_table (info);
   4312  1.1  christos   BFD_ASSERT (htab != NULL);
   4313  1.1  christos 
   4314  1.1  christos   if (entry->abfd != NULL
   4315  1.1  christos       && entry->symndx == -1
   4316  1.1  christos       && entry->d.h->needs_lazy_stub)
   4317  1.1  christos     {
   4318  1.1  christos       entry->d.h->needs_lazy_stub = FALSE;
   4319  1.1  christos       htab->lazy_stub_count--;
   4320  1.1  christos     }
   4321  1.1  christos 
   4322  1.1  christos   return 1;
   4323  1.1  christos }
   4324  1.1  christos 
   4325  1.1  christos /* Return the offset of an input bfd IBFD's GOT from the beginning of
   4326  1.1  christos    the primary GOT.  */
   4327  1.1  christos static bfd_vma
   4328  1.1  christos mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
   4329  1.1  christos {
   4330  1.1  christos   if (g->bfd2got == NULL)
   4331  1.1  christos     return 0;
   4332  1.1  christos 
   4333  1.1  christos   g = mips_elf_got_for_ibfd (g, ibfd);
   4334  1.1  christos   if (! g)
   4335  1.1  christos     return 0;
   4336  1.1  christos 
   4337  1.1  christos   BFD_ASSERT (g->next);
   4338  1.1  christos 
   4339  1.1  christos   g = g->next;
   4340  1.1  christos 
   4341  1.1  christos   return (g->local_gotno + g->global_gotno + g->tls_gotno)
   4342  1.1  christos     * MIPS_ELF_GOT_SIZE (abfd);
   4343  1.1  christos }
   4344  1.1  christos 
   4345  1.1  christos /* Turn a single GOT that is too big for 16-bit addressing into
   4346  1.1  christos    a sequence of GOTs, each one 16-bit addressable.  */
   4347  1.1  christos 
   4348  1.1  christos static bfd_boolean
   4349  1.1  christos mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
   4350  1.1  christos 		    asection *got, bfd_size_type pages)
   4351  1.1  christos {
   4352  1.1  christos   struct mips_elf_link_hash_table *htab;
   4353  1.1  christos   struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
   4354  1.1  christos   struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
   4355  1.1  christos   struct mips_got_info *g, *gg;
   4356  1.1  christos   unsigned int assign, needed_relocs;
   4357  1.1  christos   bfd *dynobj;
   4358  1.1  christos 
   4359  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   4360  1.1  christos   htab = mips_elf_hash_table (info);
   4361  1.1  christos   BFD_ASSERT (htab != NULL);
   4362  1.1  christos 
   4363  1.1  christos   g = htab->got_info;
   4364  1.1  christos   g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
   4365  1.1  christos 				mips_elf_bfd2got_entry_eq, NULL);
   4366  1.1  christos   if (g->bfd2got == NULL)
   4367  1.1  christos     return FALSE;
   4368  1.1  christos 
   4369  1.1  christos   got_per_bfd_arg.bfd2got = g->bfd2got;
   4370  1.1  christos   got_per_bfd_arg.obfd = abfd;
   4371  1.1  christos   got_per_bfd_arg.info = info;
   4372  1.1  christos 
   4373  1.1  christos   /* Count how many GOT entries each input bfd requires, creating a
   4374  1.1  christos      map from bfd to got info while at that.  */
   4375  1.1  christos   htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
   4376  1.1  christos   if (got_per_bfd_arg.obfd == NULL)
   4377  1.1  christos     return FALSE;
   4378  1.1  christos 
   4379  1.1  christos   /* Also count how many page entries each input bfd requires.  */
   4380  1.1  christos   htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
   4381  1.1  christos 		 &got_per_bfd_arg);
   4382  1.1  christos   if (got_per_bfd_arg.obfd == NULL)
   4383  1.1  christos     return FALSE;
   4384  1.1  christos 
   4385  1.1  christos   got_per_bfd_arg.current = NULL;
   4386  1.1  christos   got_per_bfd_arg.primary = NULL;
   4387  1.1  christos   got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
   4388  1.1  christos 				/ MIPS_ELF_GOT_SIZE (abfd))
   4389  1.1  christos 			       - htab->reserved_gotno);
   4390  1.1  christos   got_per_bfd_arg.max_pages = pages;
   4391  1.1  christos   /* The number of globals that will be included in the primary GOT.
   4392  1.1  christos      See the calls to mips_elf_set_global_got_offset below for more
   4393  1.1  christos      information.  */
   4394  1.1  christos   got_per_bfd_arg.global_count = g->global_gotno;
   4395  1.1  christos 
   4396  1.1  christos   /* Try to merge the GOTs of input bfds together, as long as they
   4397  1.1  christos      don't seem to exceed the maximum GOT size, choosing one of them
   4398  1.1  christos      to be the primary GOT.  */
   4399  1.1  christos   htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
   4400  1.1  christos   if (got_per_bfd_arg.obfd == NULL)
   4401  1.1  christos     return FALSE;
   4402  1.1  christos 
   4403  1.1  christos   /* If we do not find any suitable primary GOT, create an empty one.  */
   4404  1.1  christos   if (got_per_bfd_arg.primary == NULL)
   4405  1.1  christos     {
   4406  1.1  christos       g->next = (struct mips_got_info *)
   4407  1.1  christos 	bfd_alloc (abfd, sizeof (struct mips_got_info));
   4408  1.1  christos       if (g->next == NULL)
   4409  1.1  christos 	return FALSE;
   4410  1.1  christos 
   4411  1.1  christos       g->next->global_gotsym = NULL;
   4412  1.1  christos       g->next->global_gotno = 0;
   4413  1.1  christos       g->next->reloc_only_gotno = 0;
   4414  1.1  christos       g->next->local_gotno = 0;
   4415  1.1  christos       g->next->page_gotno = 0;
   4416  1.1  christos       g->next->tls_gotno = 0;
   4417  1.1  christos       g->next->assigned_gotno = 0;
   4418  1.1  christos       g->next->tls_assigned_gotno = 0;
   4419  1.1  christos       g->next->tls_ldm_offset = MINUS_ONE;
   4420  1.1  christos       g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
   4421  1.1  christos 					      mips_elf_multi_got_entry_eq,
   4422  1.1  christos 					      NULL);
   4423  1.1  christos       if (g->next->got_entries == NULL)
   4424  1.1  christos 	return FALSE;
   4425  1.1  christos       g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
   4426  1.1  christos 						   mips_got_page_entry_eq,
   4427  1.1  christos 						   NULL);
   4428  1.1  christos       if (g->next->got_page_entries == NULL)
   4429  1.1  christos 	return FALSE;
   4430  1.1  christos       g->next->bfd2got = NULL;
   4431  1.1  christos     }
   4432  1.1  christos   else
   4433  1.1  christos     g->next = got_per_bfd_arg.primary;
   4434  1.1  christos   g->next->next = got_per_bfd_arg.current;
   4435  1.1  christos 
   4436  1.1  christos   /* GG is now the master GOT, and G is the primary GOT.  */
   4437  1.1  christos   gg = g;
   4438  1.1  christos   g = g->next;
   4439  1.1  christos 
   4440  1.1  christos   /* Map the output bfd to the primary got.  That's what we're going
   4441  1.1  christos      to use for bfds that use GOT16 or GOT_PAGE relocations that we
   4442  1.1  christos      didn't mark in check_relocs, and we want a quick way to find it.
   4443  1.1  christos      We can't just use gg->next because we're going to reverse the
   4444  1.1  christos      list.  */
   4445  1.1  christos   {
   4446  1.1  christos     struct mips_elf_bfd2got_hash *bfdgot;
   4447  1.1  christos     void **bfdgotp;
   4448  1.1  christos 
   4449  1.1  christos     bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
   4450  1.1  christos       (abfd, sizeof (struct mips_elf_bfd2got_hash));
   4451  1.1  christos 
   4452  1.1  christos     if (bfdgot == NULL)
   4453  1.1  christos       return FALSE;
   4454  1.1  christos 
   4455  1.1  christos     bfdgot->bfd = abfd;
   4456  1.1  christos     bfdgot->g = g;
   4457  1.1  christos     bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
   4458  1.1  christos 
   4459  1.1  christos     BFD_ASSERT (*bfdgotp == NULL);
   4460  1.1  christos     *bfdgotp = bfdgot;
   4461  1.1  christos   }
   4462  1.1  christos 
   4463  1.1  christos   /* Every symbol that is referenced in a dynamic relocation must be
   4464  1.1  christos      present in the primary GOT, so arrange for them to appear after
   4465  1.1  christos      those that are actually referenced.  */
   4466  1.1  christos   gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
   4467  1.1  christos   g->global_gotno = gg->global_gotno;
   4468  1.1  christos 
   4469  1.1  christos   set_got_offset_arg.g = NULL;
   4470  1.1  christos   set_got_offset_arg.value = GGA_RELOC_ONLY;
   4471  1.1  christos   htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
   4472  1.1  christos 		 &set_got_offset_arg);
   4473  1.1  christos   set_got_offset_arg.value = GGA_NORMAL;
   4474  1.1  christos   htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
   4475  1.1  christos 		 &set_got_offset_arg);
   4476  1.1  christos 
   4477  1.1  christos   /* Now go through the GOTs assigning them offset ranges.
   4478  1.1  christos      [assigned_gotno, local_gotno[ will be set to the range of local
   4479  1.1  christos      entries in each GOT.  We can then compute the end of a GOT by
   4480  1.1  christos      adding local_gotno to global_gotno.  We reverse the list and make
   4481  1.1  christos      it circular since then we'll be able to quickly compute the
   4482  1.1  christos      beginning of a GOT, by computing the end of its predecessor.  To
   4483  1.1  christos      avoid special cases for the primary GOT, while still preserving
   4484  1.1  christos      assertions that are valid for both single- and multi-got links,
   4485  1.1  christos      we arrange for the main got struct to have the right number of
   4486  1.1  christos      global entries, but set its local_gotno such that the initial
   4487  1.1  christos      offset of the primary GOT is zero.  Remember that the primary GOT
   4488  1.1  christos      will become the last item in the circular linked list, so it
   4489  1.1  christos      points back to the master GOT.  */
   4490  1.1  christos   gg->local_gotno = -g->global_gotno;
   4491  1.1  christos   gg->global_gotno = g->global_gotno;
   4492  1.1  christos   gg->tls_gotno = 0;
   4493  1.1  christos   assign = 0;
   4494  1.1  christos   gg->next = gg;
   4495  1.1  christos 
   4496  1.1  christos   do
   4497  1.1  christos     {
   4498  1.1  christos       struct mips_got_info *gn;
   4499  1.1  christos 
   4500  1.1  christos       assign += htab->reserved_gotno;
   4501  1.1  christos       g->assigned_gotno = assign;
   4502  1.1  christos       g->local_gotno += assign;
   4503  1.1  christos       g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
   4504  1.1  christos       assign = g->local_gotno + g->global_gotno + g->tls_gotno;
   4505  1.1  christos 
   4506  1.1  christos       /* Take g out of the direct list, and push it onto the reversed
   4507  1.1  christos 	 list that gg points to.  g->next is guaranteed to be nonnull after
   4508  1.1  christos 	 this operation, as required by mips_elf_initialize_tls_index. */
   4509  1.1  christos       gn = g->next;
   4510  1.1  christos       g->next = gg->next;
   4511  1.1  christos       gg->next = g;
   4512  1.1  christos 
   4513  1.1  christos       /* Set up any TLS entries.  We always place the TLS entries after
   4514  1.1  christos 	 all non-TLS entries.  */
   4515  1.1  christos       g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
   4516  1.1  christos       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
   4517  1.1  christos 
   4518  1.1  christos       /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
   4519  1.1  christos       g = gn;
   4520  1.1  christos 
   4521  1.1  christos       /* Forbid global symbols in every non-primary GOT from having
   4522  1.1  christos 	 lazy-binding stubs.  */
   4523  1.1  christos       if (g)
   4524  1.1  christos 	htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
   4525  1.1  christos     }
   4526  1.1  christos   while (g);
   4527  1.1  christos 
   4528  1.1  christos   got->size = (gg->next->local_gotno
   4529  1.1  christos 	       + gg->next->global_gotno
   4530  1.1  christos 	       + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
   4531  1.1  christos 
   4532  1.1  christos   needed_relocs = 0;
   4533  1.1  christos   set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
   4534  1.1  christos   set_got_offset_arg.info = info;
   4535  1.1  christos   for (g = gg->next; g && g->next != gg; g = g->next)
   4536  1.1  christos     {
   4537  1.1  christos       unsigned int save_assign;
   4538  1.1  christos 
   4539  1.1  christos       /* Assign offsets to global GOT entries.  */
   4540  1.1  christos       save_assign = g->assigned_gotno;
   4541  1.1  christos       g->assigned_gotno = g->local_gotno;
   4542  1.1  christos       set_got_offset_arg.g = g;
   4543  1.1  christos       set_got_offset_arg.needed_relocs = 0;
   4544  1.1  christos       htab_traverse (g->got_entries,
   4545  1.1  christos 		     mips_elf_set_global_got_offset,
   4546  1.1  christos 		     &set_got_offset_arg);
   4547  1.1  christos       needed_relocs += set_got_offset_arg.needed_relocs;
   4548  1.1  christos       BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
   4549  1.1  christos 
   4550  1.1  christos       g->assigned_gotno = save_assign;
   4551  1.1  christos       if (info->shared)
   4552  1.1  christos 	{
   4553  1.1  christos 	  needed_relocs += g->local_gotno - g->assigned_gotno;
   4554  1.1  christos 	  BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
   4555  1.1  christos 		      + g->next->global_gotno
   4556  1.1  christos 		      + g->next->tls_gotno
   4557  1.1  christos 		      + htab->reserved_gotno);
   4558  1.1  christos 	}
   4559  1.1  christos     }
   4560  1.1  christos 
   4561  1.1  christos   if (needed_relocs)
   4562  1.1  christos     mips_elf_allocate_dynamic_relocations (dynobj, info,
   4563  1.1  christos 					   needed_relocs);
   4564  1.1  christos 
   4565  1.1  christos   return TRUE;
   4566  1.1  christos }
   4567  1.1  christos 
   4568  1.1  christos 
   4569  1.1  christos /* Returns the first relocation of type r_type found, beginning with
   4571  1.1  christos    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
   4572  1.1  christos 
   4573  1.1  christos static const Elf_Internal_Rela *
   4574  1.1  christos mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
   4575  1.1  christos 			  const Elf_Internal_Rela *relocation,
   4576  1.1  christos 			  const Elf_Internal_Rela *relend)
   4577  1.1  christos {
   4578  1.1  christos   unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
   4579  1.1  christos 
   4580  1.1  christos   while (relocation < relend)
   4581  1.1  christos     {
   4582  1.1  christos       if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
   4583  1.1  christos 	  && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
   4584  1.1  christos 	return relocation;
   4585  1.1  christos 
   4586  1.1  christos       ++relocation;
   4587  1.1  christos     }
   4588  1.1  christos 
   4589  1.1  christos   /* We didn't find it.  */
   4590  1.1  christos   return NULL;
   4591  1.1  christos }
   4592  1.1  christos 
   4593  1.1  christos /* Return whether an input relocation is against a local symbol.  */
   4594  1.1  christos 
   4595  1.1  christos static bfd_boolean
   4596  1.1  christos mips_elf_local_relocation_p (bfd *input_bfd,
   4597  1.1  christos 			     const Elf_Internal_Rela *relocation,
   4598  1.1  christos 			     asection **local_sections)
   4599  1.1  christos {
   4600  1.1  christos   unsigned long r_symndx;
   4601  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   4602  1.1  christos   size_t extsymoff;
   4603  1.1  christos 
   4604  1.1  christos   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
   4605  1.1  christos   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   4606  1.1  christos   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
   4607  1.1  christos 
   4608  1.1  christos   if (r_symndx < extsymoff)
   4609  1.1  christos     return TRUE;
   4610  1.1  christos   if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
   4611  1.1  christos     return TRUE;
   4612  1.1  christos 
   4613  1.1  christos   return FALSE;
   4614  1.1  christos }
   4615  1.1  christos 
   4616  1.1  christos /* Sign-extend VALUE, which has the indicated number of BITS.  */
   4618  1.1  christos 
   4619  1.1  christos bfd_vma
   4620  1.1  christos _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
   4621  1.1  christos {
   4622  1.1  christos   if (value & ((bfd_vma) 1 << (bits - 1)))
   4623  1.1  christos     /* VALUE is negative.  */
   4624  1.1  christos     value |= ((bfd_vma) - 1) << bits;
   4625  1.1  christos 
   4626  1.1  christos   return value;
   4627  1.1  christos }
   4628  1.1  christos 
   4629  1.1  christos /* Return non-zero if the indicated VALUE has overflowed the maximum
   4630  1.1  christos    range expressible by a signed number with the indicated number of
   4631  1.1  christos    BITS.  */
   4632  1.1  christos 
   4633  1.1  christos static bfd_boolean
   4634  1.1  christos mips_elf_overflow_p (bfd_vma value, int bits)
   4635  1.1  christos {
   4636  1.1  christos   bfd_signed_vma svalue = (bfd_signed_vma) value;
   4637  1.1  christos 
   4638  1.1  christos   if (svalue > (1 << (bits - 1)) - 1)
   4639  1.1  christos     /* The value is too big.  */
   4640  1.1  christos     return TRUE;
   4641  1.1  christos   else if (svalue < -(1 << (bits - 1)))
   4642  1.1  christos     /* The value is too small.  */
   4643  1.1  christos     return TRUE;
   4644  1.1  christos 
   4645  1.1  christos   /* All is well.  */
   4646  1.1  christos   return FALSE;
   4647  1.1  christos }
   4648  1.1  christos 
   4649  1.1  christos /* Calculate the %high function.  */
   4650  1.1  christos 
   4651  1.1  christos static bfd_vma
   4652  1.1  christos mips_elf_high (bfd_vma value)
   4653  1.1  christos {
   4654  1.1  christos   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
   4655  1.1  christos }
   4656  1.1  christos 
   4657  1.1  christos /* Calculate the %higher function.  */
   4658  1.1  christos 
   4659  1.1  christos static bfd_vma
   4660  1.1  christos mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
   4661  1.1  christos {
   4662  1.1  christos #ifdef BFD64
   4663  1.1  christos   return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
   4664  1.1  christos #else
   4665  1.1  christos   abort ();
   4666  1.1  christos   return MINUS_ONE;
   4667  1.1  christos #endif
   4668  1.1  christos }
   4669  1.1  christos 
   4670  1.1  christos /* Calculate the %highest function.  */
   4671  1.1  christos 
   4672  1.1  christos static bfd_vma
   4673  1.1  christos mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
   4674  1.1  christos {
   4675  1.1  christos #ifdef BFD64
   4676  1.1  christos   return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
   4677  1.1  christos #else
   4678  1.1  christos   abort ();
   4679  1.1  christos   return MINUS_ONE;
   4680  1.1  christos #endif
   4681  1.1  christos }
   4682  1.1  christos 
   4683  1.1  christos /* Create the .compact_rel section.  */
   4685  1.1  christos 
   4686  1.1  christos static bfd_boolean
   4687  1.1  christos mips_elf_create_compact_rel_section
   4688  1.1  christos   (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
   4689  1.1  christos {
   4690  1.1  christos   flagword flags;
   4691  1.1  christos   register asection *s;
   4692  1.1  christos 
   4693  1.1  christos   if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
   4694  1.1  christos     {
   4695  1.1  christos       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
   4696  1.1  christos 	       | SEC_READONLY);
   4697  1.1  christos 
   4698  1.1  christos       s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
   4699  1.1  christos       if (s == NULL
   4700  1.1  christos 	  || ! bfd_set_section_alignment (abfd, s,
   4701  1.1  christos 					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   4702  1.1  christos 	return FALSE;
   4703  1.1  christos 
   4704  1.1  christos       s->size = sizeof (Elf32_External_compact_rel);
   4705  1.1  christos     }
   4706  1.1  christos 
   4707  1.1  christos   return TRUE;
   4708  1.1  christos }
   4709  1.1  christos 
   4710  1.1  christos /* Create the .got section to hold the global offset table.  */
   4711  1.1  christos 
   4712  1.1  christos static bfd_boolean
   4713  1.1  christos mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
   4714  1.1  christos {
   4715  1.1  christos   flagword flags;
   4716  1.1  christos   register asection *s;
   4717  1.1  christos   struct elf_link_hash_entry *h;
   4718  1.1  christos   struct bfd_link_hash_entry *bh;
   4719  1.1  christos   struct mips_got_info *g;
   4720  1.1  christos   bfd_size_type amt;
   4721  1.1  christos   struct mips_elf_link_hash_table *htab;
   4722  1.1  christos 
   4723  1.1  christos   htab = mips_elf_hash_table (info);
   4724  1.1  christos   BFD_ASSERT (htab != NULL);
   4725  1.1  christos 
   4726  1.1  christos   /* This function may be called more than once.  */
   4727  1.1  christos   if (htab->sgot)
   4728  1.1  christos     return TRUE;
   4729  1.1  christos 
   4730  1.1  christos   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   4731  1.1  christos 	   | SEC_LINKER_CREATED);
   4732  1.1  christos 
   4733  1.1  christos   /* We have to use an alignment of 2**4 here because this is hardcoded
   4734  1.1  christos      in the function stub generation and in the linker script.  */
   4735  1.1  christos   s = bfd_make_section_with_flags (abfd, ".got", flags);
   4736  1.1  christos   if (s == NULL
   4737  1.1  christos       || ! bfd_set_section_alignment (abfd, s, 4))
   4738  1.1  christos     return FALSE;
   4739  1.1  christos   htab->sgot = s;
   4740  1.1  christos 
   4741  1.1  christos   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
   4742  1.1  christos      linker script because we don't want to define the symbol if we
   4743  1.1  christos      are not creating a global offset table.  */
   4744  1.1  christos   bh = NULL;
   4745  1.1  christos   if (! (_bfd_generic_link_add_one_symbol
   4746  1.1  christos 	 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
   4747  1.1  christos 	  0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
   4748  1.1  christos     return FALSE;
   4749  1.1  christos 
   4750  1.1  christos   h = (struct elf_link_hash_entry *) bh;
   4751  1.1  christos   h->non_elf = 0;
   4752  1.1  christos   h->def_regular = 1;
   4753  1.1  christos   h->type = STT_OBJECT;
   4754  1.1  christos   elf_hash_table (info)->hgot = h;
   4755  1.1  christos 
   4756  1.1  christos   if (info->shared
   4757  1.1  christos       && ! bfd_elf_link_record_dynamic_symbol (info, h))
   4758  1.1  christos     return FALSE;
   4759  1.1  christos 
   4760  1.1  christos   amt = sizeof (struct mips_got_info);
   4761  1.1  christos   g = bfd_alloc (abfd, amt);
   4762  1.1  christos   if (g == NULL)
   4763  1.1  christos     return FALSE;
   4764  1.1  christos   g->global_gotsym = NULL;
   4765  1.1  christos   g->global_gotno = 0;
   4766  1.1  christos   g->reloc_only_gotno = 0;
   4767  1.1  christos   g->tls_gotno = 0;
   4768  1.1  christos   g->local_gotno = 0;
   4769  1.1  christos   g->page_gotno = 0;
   4770  1.1  christos   g->assigned_gotno = 0;
   4771  1.1  christos   g->bfd2got = NULL;
   4772  1.1  christos   g->next = NULL;
   4773  1.1  christos   g->tls_ldm_offset = MINUS_ONE;
   4774  1.1  christos   g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
   4775  1.1  christos 				    mips_elf_got_entry_eq, NULL);
   4776  1.1  christos   if (g->got_entries == NULL)
   4777  1.1  christos     return FALSE;
   4778  1.1  christos   g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
   4779  1.1  christos 					 mips_got_page_entry_eq, NULL);
   4780  1.1  christos   if (g->got_page_entries == NULL)
   4781  1.1  christos     return FALSE;
   4782  1.1  christos   htab->got_info = g;
   4783  1.1  christos   mips_elf_section_data (s)->elf.this_hdr.sh_flags
   4784  1.1  christos     |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
   4785  1.1  christos 
   4786  1.1  christos   /* We also need a .got.plt section when generating PLTs.  */
   4787  1.1  christos   s = bfd_make_section_with_flags (abfd, ".got.plt",
   4788  1.1  christos 				   SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
   4789  1.1  christos 				   | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4790  1.1  christos   if (s == NULL)
   4791  1.1  christos     return FALSE;
   4792  1.1  christos   htab->sgotplt = s;
   4793  1.1  christos 
   4794  1.1  christos   return TRUE;
   4795  1.1  christos }
   4796  1.1  christos 
   4797  1.1  christos /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
   4799  1.1  christos    __GOTT_INDEX__ symbols.  These symbols are only special for
   4800  1.1  christos    shared objects; they are not used in executables.  */
   4801  1.1  christos 
   4802  1.1  christos static bfd_boolean
   4803  1.1  christos is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
   4804  1.1  christos {
   4805  1.1  christos   return (mips_elf_hash_table (info)->is_vxworks
   4806  1.1  christos 	  && info->shared
   4807  1.1  christos 	  && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
   4808  1.1  christos 	      || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
   4809  1.1  christos }
   4810  1.1  christos 
   4811  1.1  christos /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
   4812  1.1  christos    require an la25 stub.  See also mips_elf_local_pic_function_p,
   4813  1.1  christos    which determines whether the destination function ever requires a
   4814  1.1  christos    stub.  */
   4815  1.1  christos 
   4816  1.1  christos static bfd_boolean
   4817  1.1  christos mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
   4818  1.1  christos {
   4819  1.1  christos   /* We specifically ignore branches and jumps from EF_PIC objects,
   4820  1.1  christos      where the onus is on the compiler or programmer to perform any
   4821  1.1  christos      necessary initialization of $25.  Sometimes such initialization
   4822  1.1  christos      is unnecessary; for example, -mno-shared functions do not use
   4823  1.1  christos      the incoming value of $25, and may therefore be called directly.  */
   4824  1.1  christos   if (PIC_OBJECT_P (input_bfd))
   4825  1.1  christos     return FALSE;
   4826  1.1  christos 
   4827  1.1  christos   switch (r_type)
   4828  1.1  christos     {
   4829  1.1  christos     case R_MIPS_26:
   4830  1.1  christos     case R_MIPS_PC16:
   4831  1.1  christos     case R_MIPS16_26:
   4832  1.1  christos       return TRUE;
   4833  1.1  christos 
   4834  1.1  christos     default:
   4835  1.1  christos       return FALSE;
   4836  1.1  christos     }
   4837  1.1  christos }
   4838  1.1  christos 
   4839  1.1  christos /* Calculate the value produced by the RELOCATION (which comes from
   4841  1.1  christos    the INPUT_BFD).  The ADDEND is the addend to use for this
   4842  1.1  christos    RELOCATION; RELOCATION->R_ADDEND is ignored.
   4843  1.1  christos 
   4844  1.1  christos    The result of the relocation calculation is stored in VALUEP.
   4845  1.1  christos    On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
   4846  1.1  christos    is a MIPS16 jump to non-MIPS16 code, or vice versa.
   4847  1.1  christos 
   4848  1.1  christos    This function returns bfd_reloc_continue if the caller need take no
   4849  1.1  christos    further action regarding this relocation, bfd_reloc_notsupported if
   4850  1.1  christos    something goes dramatically wrong, bfd_reloc_overflow if an
   4851  1.1  christos    overflow occurs, and bfd_reloc_ok to indicate success.  */
   4852  1.1  christos 
   4853  1.1  christos static bfd_reloc_status_type
   4854  1.1  christos mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
   4855  1.1  christos 			       asection *input_section,
   4856  1.1  christos 			       struct bfd_link_info *info,
   4857  1.1  christos 			       const Elf_Internal_Rela *relocation,
   4858  1.1  christos 			       bfd_vma addend, reloc_howto_type *howto,
   4859  1.1  christos 			       Elf_Internal_Sym *local_syms,
   4860  1.1  christos 			       asection **local_sections, bfd_vma *valuep,
   4861  1.1  christos 			       const char **namep,
   4862  1.1  christos 			       bfd_boolean *cross_mode_jump_p,
   4863  1.1  christos 			       bfd_boolean save_addend)
   4864  1.1  christos {
   4865  1.1  christos   /* The eventual value we will return.  */
   4866  1.1  christos   bfd_vma value;
   4867  1.1  christos   /* The address of the symbol against which the relocation is
   4868  1.1  christos      occurring.  */
   4869  1.1  christos   bfd_vma symbol = 0;
   4870  1.1  christos   /* The final GP value to be used for the relocatable, executable, or
   4871  1.1  christos      shared object file being produced.  */
   4872  1.1  christos   bfd_vma gp;
   4873  1.1  christos   /* The place (section offset or address) of the storage unit being
   4874  1.1  christos      relocated.  */
   4875  1.1  christos   bfd_vma p;
   4876  1.1  christos   /* The value of GP used to create the relocatable object.  */
   4877  1.1  christos   bfd_vma gp0;
   4878  1.1  christos   /* The offset into the global offset table at which the address of
   4879  1.1  christos      the relocation entry symbol, adjusted by the addend, resides
   4880  1.1  christos      during execution.  */
   4881  1.1  christos   bfd_vma g = MINUS_ONE;
   4882  1.1  christos   /* The section in which the symbol referenced by the relocation is
   4883  1.1  christos      located.  */
   4884  1.1  christos   asection *sec = NULL;
   4885  1.1  christos   struct mips_elf_link_hash_entry *h = NULL;
   4886  1.1  christos   /* TRUE if the symbol referred to by this relocation is a local
   4887  1.1  christos      symbol.  */
   4888  1.1  christos   bfd_boolean local_p, was_local_p;
   4889  1.1  christos   /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
   4890  1.1  christos   bfd_boolean gp_disp_p = FALSE;
   4891  1.1  christos   /* TRUE if the symbol referred to by this relocation is
   4892  1.1  christos      "__gnu_local_gp".  */
   4893  1.1  christos   bfd_boolean gnu_local_gp_p = FALSE;
   4894  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   4895  1.1  christos   size_t extsymoff;
   4896  1.1  christos   unsigned long r_symndx;
   4897  1.1  christos   int r_type;
   4898  1.1  christos   /* TRUE if overflow occurred during the calculation of the
   4899  1.1  christos      relocation value.  */
   4900  1.1  christos   bfd_boolean overflowed_p;
   4901  1.1  christos   /* TRUE if this relocation refers to a MIPS16 function.  */
   4902  1.1  christos   bfd_boolean target_is_16_bit_code_p = FALSE;
   4903  1.1  christos   struct mips_elf_link_hash_table *htab;
   4904  1.1  christos   bfd *dynobj;
   4905  1.1  christos 
   4906  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   4907  1.1  christos   htab = mips_elf_hash_table (info);
   4908  1.1  christos   BFD_ASSERT (htab != NULL);
   4909  1.1  christos 
   4910  1.1  christos   /* Parse the relocation.  */
   4911  1.1  christos   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
   4912  1.1  christos   r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   4913  1.1  christos   p = (input_section->output_section->vma
   4914  1.1  christos        + input_section->output_offset
   4915  1.1  christos        + relocation->r_offset);
   4916  1.1  christos 
   4917  1.1  christos   /* Assume that there will be no overflow.  */
   4918  1.1  christos   overflowed_p = FALSE;
   4919  1.1  christos 
   4920  1.1  christos   /* Figure out whether or not the symbol is local, and get the offset
   4921  1.1  christos      used in the array of hash table entries.  */
   4922  1.1  christos   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   4923  1.1  christos   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
   4924  1.1  christos 					 local_sections);
   4925  1.1  christos   was_local_p = local_p;
   4926  1.1  christos   if (! elf_bad_symtab (input_bfd))
   4927  1.1  christos     extsymoff = symtab_hdr->sh_info;
   4928  1.1  christos   else
   4929  1.1  christos     {
   4930  1.1  christos       /* The symbol table does not follow the rule that local symbols
   4931  1.1  christos 	 must come before globals.  */
   4932  1.1  christos       extsymoff = 0;
   4933  1.1  christos     }
   4934  1.1  christos 
   4935  1.1  christos   /* Figure out the value of the symbol.  */
   4936  1.1  christos   if (local_p)
   4937  1.1  christos     {
   4938  1.1  christos       Elf_Internal_Sym *sym;
   4939  1.1  christos 
   4940  1.1  christos       sym = local_syms + r_symndx;
   4941  1.1  christos       sec = local_sections[r_symndx];
   4942  1.1  christos 
   4943  1.1  christos       symbol = sec->output_section->vma + sec->output_offset;
   4944  1.1  christos       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
   4945  1.1  christos 	  || (sec->flags & SEC_MERGE))
   4946  1.1  christos 	symbol += sym->st_value;
   4947  1.1  christos       if ((sec->flags & SEC_MERGE)
   4948  1.1  christos 	  && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   4949  1.1  christos 	{
   4950  1.1  christos 	  addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
   4951  1.1  christos 	  addend -= symbol;
   4952  1.1  christos 	  addend += sec->output_section->vma + sec->output_offset;
   4953  1.1  christos 	}
   4954  1.1  christos 
   4955  1.1  christos       /* MIPS16 text labels should be treated as odd.  */
   4956  1.1  christos       if (ELF_ST_IS_MIPS16 (sym->st_other))
   4957  1.1  christos 	++symbol;
   4958  1.1  christos 
   4959  1.1  christos       /* Record the name of this symbol, for our caller.  */
   4960  1.1  christos       *namep = bfd_elf_string_from_elf_section (input_bfd,
   4961  1.1  christos 						symtab_hdr->sh_link,
   4962  1.1  christos 						sym->st_name);
   4963  1.1  christos       if (*namep == '\0')
   4964  1.1  christos 	*namep = bfd_section_name (input_bfd, sec);
   4965  1.1  christos 
   4966  1.1  christos       target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
   4967  1.1  christos     }
   4968  1.1  christos   else
   4969  1.1  christos     {
   4970  1.1  christos       /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
   4971  1.1  christos 
   4972  1.1  christos       /* For global symbols we look up the symbol in the hash-table.  */
   4973  1.1  christos       h = ((struct mips_elf_link_hash_entry *)
   4974  1.1  christos 	   elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
   4975  1.1  christos       /* Find the real hash-table entry for this symbol.  */
   4976  1.1  christos       while (h->root.root.type == bfd_link_hash_indirect
   4977  1.1  christos 	     || h->root.root.type == bfd_link_hash_warning)
   4978  1.1  christos 	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   4979  1.1  christos 
   4980  1.1  christos       /* Record the name of this symbol, for our caller.  */
   4981  1.1  christos       *namep = h->root.root.root.string;
   4982  1.1  christos 
   4983  1.1  christos       /* See if this is the special _gp_disp symbol.  Note that such a
   4984  1.1  christos 	 symbol must always be a global symbol.  */
   4985  1.1  christos       if (strcmp (*namep, "_gp_disp") == 0
   4986  1.1  christos 	  && ! NEWABI_P (input_bfd))
   4987  1.1  christos 	{
   4988  1.1  christos 	  /* Relocations against _gp_disp are permitted only with
   4989  1.1  christos 	     R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
   4990  1.1  christos 	  if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
   4991  1.1  christos 	    return bfd_reloc_notsupported;
   4992  1.1  christos 
   4993  1.1  christos 	  gp_disp_p = TRUE;
   4994  1.1  christos 	}
   4995  1.1  christos       /* See if this is the special _gp symbol.  Note that such a
   4996  1.1  christos 	 symbol must always be a global symbol.  */
   4997  1.1  christos       else if (strcmp (*namep, "__gnu_local_gp") == 0)
   4998  1.1  christos 	gnu_local_gp_p = TRUE;
   4999  1.1  christos 
   5000  1.1  christos 
   5001  1.1  christos       /* If this symbol is defined, calculate its address.  Note that
   5002  1.1  christos 	 _gp_disp is a magic symbol, always implicitly defined by the
   5003  1.1  christos 	 linker, so it's inappropriate to check to see whether or not
   5004  1.1  christos 	 its defined.  */
   5005  1.1  christos       else if ((h->root.root.type == bfd_link_hash_defined
   5006  1.1  christos 		|| h->root.root.type == bfd_link_hash_defweak)
   5007  1.1  christos 	       && h->root.root.u.def.section)
   5008  1.1  christos 	{
   5009  1.1  christos 	  sec = h->root.root.u.def.section;
   5010  1.1  christos 	  if (sec->output_section)
   5011  1.1  christos 	    symbol = (h->root.root.u.def.value
   5012  1.1  christos 		      + sec->output_section->vma
   5013  1.1  christos 		      + sec->output_offset);
   5014  1.1  christos 	  else
   5015  1.1  christos 	    symbol = h->root.root.u.def.value;
   5016  1.1  christos 	}
   5017  1.1  christos       else if (h->root.root.type == bfd_link_hash_undefweak)
   5018  1.1  christos 	/* We allow relocations against undefined weak symbols, giving
   5019  1.1  christos 	   it the value zero, so that you can undefined weak functions
   5020  1.1  christos 	   and check to see if they exist by looking at their
   5021  1.1  christos 	   addresses.  */
   5022  1.1  christos 	symbol = 0;
   5023  1.1  christos       else if (info->unresolved_syms_in_objects == RM_IGNORE
   5024  1.1  christos 	       && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
   5025  1.1  christos 	symbol = 0;
   5026  1.1  christos       else if (strcmp (*namep, SGI_COMPAT (input_bfd)
   5027  1.1  christos 		       ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
   5028  1.1  christos 	{
   5029  1.1  christos 	  /* If this is a dynamic link, we should have created a
   5030  1.1  christos 	     _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
   5031  1.1  christos 	     in in _bfd_mips_elf_create_dynamic_sections.
   5032  1.1  christos 	     Otherwise, we should define the symbol with a value of 0.
   5033  1.1  christos 	     FIXME: It should probably get into the symbol table
   5034  1.1  christos 	     somehow as well.  */
   5035  1.1  christos 	  BFD_ASSERT (! info->shared);
   5036  1.1  christos 	  BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
   5037  1.1  christos 	  symbol = 0;
   5038  1.1  christos 	}
   5039  1.1  christos       else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
   5040  1.1  christos 	{
   5041  1.1  christos 	  /* This is an optional symbol - an Irix specific extension to the
   5042  1.1  christos 	     ELF spec.  Ignore it for now.
   5043  1.1  christos 	     XXX - FIXME - there is more to the spec for OPTIONAL symbols
   5044  1.1  christos 	     than simply ignoring them, but we do not handle this for now.
   5045  1.1  christos 	     For information see the "64-bit ELF Object File Specification"
   5046  1.1  christos 	     which is available from here:
   5047  1.1  christos 	     http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
   5048  1.1  christos 	  symbol = 0;
   5049  1.1  christos 	}
   5050  1.1  christos       else if ((*info->callbacks->undefined_symbol)
   5051  1.1  christos 	       (info, h->root.root.root.string, input_bfd,
   5052  1.1  christos 		input_section, relocation->r_offset,
   5053  1.1  christos 		(info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
   5054  1.1  christos 		 || ELF_ST_VISIBILITY (h->root.other)))
   5055  1.1  christos 	{
   5056  1.1  christos 	  return bfd_reloc_undefined;
   5057  1.1  christos 	}
   5058  1.1  christos       else
   5059  1.1  christos 	{
   5060  1.1  christos 	  return bfd_reloc_notsupported;
   5061  1.1  christos 	}
   5062  1.1  christos 
   5063  1.1  christos       target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
   5064  1.1  christos     }
   5065  1.1  christos 
   5066  1.1  christos   /* If this is a reference to a 16-bit function with a stub, we need
   5067  1.1  christos      to redirect the relocation to the stub unless:
   5068  1.1  christos 
   5069  1.1  christos      (a) the relocation is for a MIPS16 JAL;
   5070  1.1  christos 
   5071  1.1  christos      (b) the relocation is for a MIPS16 PIC call, and there are no
   5072  1.1  christos 	 non-MIPS16 uses of the GOT slot; or
   5073  1.1  christos 
   5074  1.1  christos      (c) the section allows direct references to MIPS16 functions.  */
   5075  1.1  christos   if (r_type != R_MIPS16_26
   5076  1.1  christos       && !info->relocatable
   5077  1.1  christos       && ((h != NULL
   5078  1.1  christos 	   && h->fn_stub != NULL
   5079  1.1  christos 	   && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
   5080  1.1  christos 	  || (local_p
   5081  1.1  christos 	      && elf_tdata (input_bfd)->local_stubs != NULL
   5082  1.1  christos 	      && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
   5083  1.1  christos       && !section_allows_mips16_refs_p (input_section))
   5084  1.1  christos     {
   5085  1.1  christos       /* This is a 32- or 64-bit call to a 16-bit function.  We should
   5086  1.1  christos 	 have already noticed that we were going to need the
   5087  1.1  christos 	 stub.  */
   5088  1.1  christos       if (local_p)
   5089  1.1  christos 	sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
   5090  1.1  christos       else
   5091  1.1  christos 	{
   5092  1.1  christos 	  BFD_ASSERT (h->need_fn_stub);
   5093  1.1  christos 	  sec = h->fn_stub;
   5094  1.1  christos 	}
   5095  1.1  christos 
   5096  1.1  christos       symbol = sec->output_section->vma + sec->output_offset;
   5097  1.1  christos       /* The target is 16-bit, but the stub isn't.  */
   5098  1.1  christos       target_is_16_bit_code_p = FALSE;
   5099  1.1  christos     }
   5100  1.1  christos   /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
   5101  1.1  christos      need to redirect the call to the stub.  Note that we specifically
   5102  1.1  christos      exclude R_MIPS16_CALL16 from this behavior; indirect calls should
   5103  1.1  christos      use an indirect stub instead.  */
   5104  1.1  christos   else if (r_type == R_MIPS16_26 && !info->relocatable
   5105  1.1  christos 	   && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
   5106  1.1  christos 	       || (local_p
   5107  1.1  christos 		   && elf_tdata (input_bfd)->local_call_stubs != NULL
   5108  1.1  christos 		   && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
   5109  1.1  christos 	   && !target_is_16_bit_code_p)
   5110  1.1  christos     {
   5111  1.1  christos       if (local_p)
   5112  1.1  christos 	sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
   5113  1.1  christos       else
   5114  1.1  christos 	{
   5115  1.1  christos 	  /* If both call_stub and call_fp_stub are defined, we can figure
   5116  1.1  christos 	     out which one to use by checking which one appears in the input
   5117  1.1  christos 	     file.  */
   5118  1.1  christos 	  if (h->call_stub != NULL && h->call_fp_stub != NULL)
   5119  1.1  christos 	    {
   5120  1.1  christos 	      asection *o;
   5121  1.1  christos 
   5122  1.1  christos 	      sec = NULL;
   5123  1.1  christos 	      for (o = input_bfd->sections; o != NULL; o = o->next)
   5124  1.1  christos 		{
   5125  1.1  christos 		  if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
   5126  1.1  christos 		    {
   5127  1.1  christos 		      sec = h->call_fp_stub;
   5128  1.1  christos 		      break;
   5129  1.1  christos 		    }
   5130  1.1  christos 		}
   5131  1.1  christos 	      if (sec == NULL)
   5132  1.1  christos 		sec = h->call_stub;
   5133  1.1  christos 	    }
   5134  1.1  christos 	  else if (h->call_stub != NULL)
   5135  1.1  christos 	    sec = h->call_stub;
   5136  1.1  christos 	  else
   5137  1.1  christos 	    sec = h->call_fp_stub;
   5138  1.1  christos   	}
   5139  1.1  christos 
   5140  1.1  christos       BFD_ASSERT (sec->size > 0);
   5141  1.1  christos       symbol = sec->output_section->vma + sec->output_offset;
   5142  1.1  christos     }
   5143  1.1  christos   /* If this is a direct call to a PIC function, redirect to the
   5144  1.1  christos      non-PIC stub.  */
   5145  1.1  christos   else if (h != NULL && h->la25_stub
   5146  1.1  christos 	   && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
   5147  1.1  christos     symbol = (h->la25_stub->stub_section->output_section->vma
   5148  1.1  christos 	      + h->la25_stub->stub_section->output_offset
   5149  1.1  christos 	      + h->la25_stub->offset);
   5150  1.1  christos 
   5151  1.1  christos   /* Calls from 16-bit code to 32-bit code and vice versa require the
   5152  1.1  christos      mode change.  */
   5153  1.1  christos   *cross_mode_jump_p = !info->relocatable
   5154  1.1  christos 		       && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
   5155  1.1  christos 			   || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
   5156  1.1  christos 			       && target_is_16_bit_code_p));
   5157  1.1  christos 
   5158  1.1  christos   local_p = h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->root);
   5159  1.1  christos 
   5160  1.1  christos   gp0 = _bfd_get_gp_value (input_bfd);
   5161  1.1  christos   gp = _bfd_get_gp_value (abfd);
   5162  1.1  christos   if (htab->got_info)
   5163  1.1  christos     gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
   5164  1.1  christos 
   5165  1.1  christos   if (gnu_local_gp_p)
   5166  1.1  christos     symbol = gp;
   5167  1.1  christos 
   5168  1.1  christos   /* Global R_MIPS_GOT_PAGE relocations are equivalent to R_MIPS_GOT_DISP.
   5169  1.1  christos      The addend is applied by the corresponding R_MIPS_GOT_OFST.  */
   5170  1.1  christos   if (r_type == R_MIPS_GOT_PAGE && !local_p)
   5171  1.1  christos     {
   5172  1.1  christos       r_type = R_MIPS_GOT_DISP;
   5173  1.1  christos       addend = 0;
   5174  1.1  christos     }
   5175  1.1  christos 
   5176  1.1  christos   /* If we haven't already determined the GOT offset, and we're going
   5177  1.1  christos      to need it, get it now.  */
   5178  1.1  christos   switch (r_type)
   5179  1.1  christos     {
   5180  1.1  christos     case R_MIPS16_CALL16:
   5181  1.1  christos     case R_MIPS16_GOT16:
   5182  1.1  christos     case R_MIPS_CALL16:
   5183  1.1  christos     case R_MIPS_GOT16:
   5184  1.1  christos     case R_MIPS_GOT_DISP:
   5185  1.1  christos     case R_MIPS_GOT_HI16:
   5186  1.1  christos     case R_MIPS_CALL_HI16:
   5187  1.1  christos     case R_MIPS_GOT_LO16:
   5188  1.1  christos     case R_MIPS_CALL_LO16:
   5189  1.1  christos     case R_MIPS_TLS_GD:
   5190  1.1  christos     case R_MIPS_TLS_GOTTPREL:
   5191  1.1  christos     case R_MIPS_TLS_LDM:
   5192  1.1  christos       /* Find the index into the GOT where this value is located.  */
   5193  1.1  christos       if (r_type == R_MIPS_TLS_LDM)
   5194  1.1  christos 	{
   5195  1.1  christos 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
   5196  1.1  christos 					0, 0, NULL, r_type);
   5197  1.1  christos 	  if (g == MINUS_ONE)
   5198  1.1  christos 	    return bfd_reloc_outofrange;
   5199  1.1  christos 	}
   5200  1.1  christos       else if (!local_p)
   5201  1.1  christos 	{
   5202  1.1  christos 	  /* On VxWorks, CALL relocations should refer to the .got.plt
   5203  1.1  christos 	     entry, which is initialized to point at the PLT stub.  */
   5204  1.1  christos 	  if (htab->is_vxworks
   5205  1.1  christos 	      && (r_type == R_MIPS_CALL_HI16
   5206  1.1  christos 		  || r_type == R_MIPS_CALL_LO16
   5207  1.1  christos 		  || call16_reloc_p (r_type)))
   5208  1.1  christos 	    {
   5209  1.1  christos 	      BFD_ASSERT (addend == 0);
   5210  1.1  christos 	      BFD_ASSERT (h->root.needs_plt);
   5211  1.1  christos 	      g = mips_elf_gotplt_index (info, &h->root);
   5212  1.1  christos 	    }
   5213  1.1  christos 	  else
   5214  1.1  christos 	    {
   5215  1.1  christos 	      BFD_ASSERT (addend == 0);
   5216  1.1  christos 	      g = mips_elf_global_got_index (dynobj, input_bfd,
   5217  1.1  christos 					     &h->root, r_type, info);
   5218  1.1  christos 	      if (h->tls_type == GOT_NORMAL
   5219  1.1  christos 		  && !elf_hash_table (info)->dynamic_sections_created)
   5220  1.1  christos 		/* This is a static link.  We must initialize the GOT entry.  */
   5221  1.1  christos 		MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
   5222  1.1  christos 	    }
   5223  1.1  christos 	}
   5224  1.1  christos       else if (!htab->is_vxworks
   5225  1.1  christos 	       && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
   5226  1.1  christos 	/* The calculation below does not involve "g".  */
   5227  1.1  christos 	break;
   5228  1.1  christos       else
   5229  1.1  christos 	{
   5230  1.1  christos 	  g = mips_elf_local_got_index (abfd, input_bfd, info,
   5231  1.1  christos 					symbol + addend, r_symndx, h, r_type);
   5232  1.1  christos 	  if (g == MINUS_ONE)
   5233  1.1  christos 	    return bfd_reloc_outofrange;
   5234  1.1  christos 	}
   5235  1.1  christos 
   5236  1.1  christos       /* Convert GOT indices to actual offsets.  */
   5237  1.1  christos       g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
   5238  1.1  christos       break;
   5239  1.1  christos     }
   5240  1.1  christos 
   5241  1.1  christos   /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
   5242  1.1  christos      symbols are resolved by the loader.  Add them to .rela.dyn.  */
   5243  1.1  christos   if (h != NULL && is_gott_symbol (info, &h->root))
   5244  1.1  christos     {
   5245  1.1  christos       Elf_Internal_Rela outrel;
   5246  1.1  christos       bfd_byte *loc;
   5247  1.1  christos       asection *s;
   5248  1.1  christos 
   5249  1.1  christos       s = mips_elf_rel_dyn_section (info, FALSE);
   5250  1.1  christos       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
   5251  1.1  christos 
   5252  1.1  christos       outrel.r_offset = (input_section->output_section->vma
   5253  1.1  christos 			 + input_section->output_offset
   5254  1.1  christos 			 + relocation->r_offset);
   5255  1.1  christos       outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
   5256  1.1  christos       outrel.r_addend = addend;
   5257  1.1  christos       bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
   5258  1.1  christos 
   5259  1.1  christos       /* If we've written this relocation for a readonly section,
   5260  1.1  christos 	 we need to set DF_TEXTREL again, so that we do not delete the
   5261  1.1  christos 	 DT_TEXTREL tag.  */
   5262  1.1  christos       if (MIPS_ELF_READONLY_SECTION (input_section))
   5263  1.1  christos 	info->flags |= DF_TEXTREL;
   5264  1.1  christos 
   5265  1.1  christos       *valuep = 0;
   5266  1.1  christos       return bfd_reloc_ok;
   5267  1.1  christos     }
   5268  1.1  christos 
   5269  1.1  christos   /* Figure out what kind of relocation is being performed.  */
   5270  1.1  christos   switch (r_type)
   5271  1.1  christos     {
   5272  1.1  christos     case R_MIPS_NONE:
   5273  1.1  christos       return bfd_reloc_continue;
   5274  1.1  christos 
   5275  1.1  christos     case R_MIPS_16:
   5276  1.1  christos       value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
   5277  1.1  christos       overflowed_p = mips_elf_overflow_p (value, 16);
   5278  1.1  christos       break;
   5279  1.1  christos 
   5280  1.1  christos     case R_MIPS_32:
   5281  1.1  christos     case R_MIPS_REL32:
   5282  1.1  christos     case R_MIPS_64:
   5283  1.1  christos       if ((info->shared
   5284  1.1  christos 	   || (htab->root.dynamic_sections_created
   5285  1.1  christos 	       && h != NULL
   5286  1.1  christos 	       && h->root.def_dynamic
   5287  1.1  christos 	       && !h->root.def_regular
   5288  1.1  christos 	       && !h->has_static_relocs))
   5289  1.1  christos 	  && r_symndx != STN_UNDEF
   5290  1.1  christos 	  && (h == NULL
   5291  1.1  christos 	      || h->root.root.type != bfd_link_hash_undefweak
   5292  1.1  christos 	      || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
   5293  1.1  christos 	  && (input_section->flags & SEC_ALLOC) != 0)
   5294  1.1  christos 	{
   5295  1.1  christos 	  /* If we're creating a shared library, then we can't know
   5296  1.1  christos 	     where the symbol will end up.  So, we create a relocation
   5297  1.1  christos 	     record in the output, and leave the job up to the dynamic
   5298  1.1  christos 	     linker.  We must do the same for executable references to
   5299  1.1  christos 	     shared library symbols, unless we've decided to use copy
   5300  1.1  christos 	     relocs or PLTs instead.  */
   5301  1.1  christos 	  value = addend;
   5302  1.1  christos 	  if (!mips_elf_create_dynamic_relocation (abfd,
   5303  1.1  christos 						   info,
   5304  1.1  christos 						   relocation,
   5305  1.1  christos 						   h,
   5306  1.1  christos 						   sec,
   5307  1.1  christos 						   symbol,
   5308  1.1  christos 						   &value,
   5309  1.1  christos 						   input_section))
   5310  1.1  christos 	    return bfd_reloc_undefined;
   5311  1.1  christos 	}
   5312  1.1  christos       else
   5313  1.1  christos 	{
   5314  1.1  christos 	  if (r_type != R_MIPS_REL32)
   5315  1.1  christos 	    value = symbol + addend;
   5316  1.1  christos 	  else
   5317  1.1  christos 	    value = addend;
   5318  1.1  christos 	}
   5319  1.1  christos       value &= howto->dst_mask;
   5320  1.1  christos       break;
   5321  1.1  christos 
   5322  1.1  christos     case R_MIPS_PC32:
   5323  1.1  christos       value = symbol + addend - p;
   5324  1.1  christos       value &= howto->dst_mask;
   5325  1.1  christos       break;
   5326  1.1  christos 
   5327  1.1  christos     case R_MIPS16_26:
   5328  1.1  christos       /* The calculation for R_MIPS16_26 is just the same as for an
   5329  1.1  christos 	 R_MIPS_26.  It's only the storage of the relocated field into
   5330  1.1  christos 	 the output file that's different.  That's handled in
   5331  1.1  christos 	 mips_elf_perform_relocation.  So, we just fall through to the
   5332  1.1  christos 	 R_MIPS_26 case here.  */
   5333  1.1  christos     case R_MIPS_26:
   5334  1.1  christos       if (was_local_p)
   5335  1.1  christos 	value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
   5336  1.1  christos       else
   5337  1.1  christos 	{
   5338  1.1  christos 	  value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
   5339  1.1  christos 	  if (h->root.root.type != bfd_link_hash_undefweak)
   5340  1.1  christos 	    overflowed_p = (value >> 26) != ((p + 4) >> 28);
   5341  1.1  christos 	}
   5342  1.1  christos       value &= howto->dst_mask;
   5343  1.1  christos       break;
   5344  1.1  christos 
   5345  1.1  christos     case R_MIPS_TLS_DTPREL_HI16:
   5346  1.1  christos       value = (mips_elf_high (addend + symbol - dtprel_base (info))
   5347  1.1  christos 	       & howto->dst_mask);
   5348  1.1  christos       break;
   5349  1.1  christos 
   5350  1.1  christos     case R_MIPS_TLS_DTPREL_LO16:
   5351  1.1  christos     case R_MIPS_TLS_DTPREL32:
   5352  1.1  christos     case R_MIPS_TLS_DTPREL64:
   5353  1.1  christos       value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
   5354  1.1  christos       break;
   5355  1.1  christos 
   5356  1.1  christos     case R_MIPS_TLS_TPREL_HI16:
   5357  1.1  christos       value = (mips_elf_high (addend + symbol - tprel_base (info))
   5358  1.1  christos 	       & howto->dst_mask);
   5359  1.1  christos       break;
   5360  1.1  christos 
   5361  1.1  christos     case R_MIPS_TLS_TPREL_LO16:
   5362  1.1  christos       value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
   5363  1.1  christos       break;
   5364  1.1  christos 
   5365  1.1  christos     case R_MIPS_HI16:
   5366  1.1  christos     case R_MIPS16_HI16:
   5367  1.1  christos       if (!gp_disp_p)
   5368  1.1  christos 	{
   5369  1.1  christos 	  value = mips_elf_high (addend + symbol);
   5370  1.1  christos 	  value &= howto->dst_mask;
   5371  1.1  christos 	}
   5372  1.1  christos       else
   5373  1.1  christos 	{
   5374  1.1  christos 	  /* For MIPS16 ABI code we generate this sequence
   5375  1.1  christos 	        0: li      $v0,%hi(_gp_disp)
   5376  1.1  christos 	        4: addiupc $v1,%lo(_gp_disp)
   5377  1.1  christos 	        8: sll     $v0,16
   5378  1.1  christos 	       12: addu    $v0,$v1
   5379  1.1  christos 	       14: move    $gp,$v0
   5380  1.1  christos 	     So the offsets of hi and lo relocs are the same, but the
   5381  1.1  christos 	     $pc is four higher than $t9 would be, so reduce
   5382  1.1  christos 	     both reloc addends by 4. */
   5383  1.1  christos 	  if (r_type == R_MIPS16_HI16)
   5384  1.1  christos 	    value = mips_elf_high (addend + gp - p - 4);
   5385  1.1  christos 	  else
   5386  1.1  christos 	    value = mips_elf_high (addend + gp - p);
   5387  1.1  christos 	  overflowed_p = mips_elf_overflow_p (value, 16);
   5388  1.1  christos 	}
   5389  1.1  christos       break;
   5390  1.1  christos 
   5391  1.1  christos     case R_MIPS_LO16:
   5392  1.1  christos     case R_MIPS16_LO16:
   5393  1.1  christos       if (!gp_disp_p)
   5394  1.1  christos 	value = (symbol + addend) & howto->dst_mask;
   5395  1.1  christos       else
   5396  1.1  christos 	{
   5397  1.1  christos 	  /* See the comment for R_MIPS16_HI16 above for the reason
   5398  1.1  christos 	     for this conditional.  */
   5399  1.1  christos 	  if (r_type == R_MIPS16_LO16)
   5400  1.1  christos 	    value = addend + gp - p;
   5401  1.1  christos 	  else
   5402  1.1  christos 	    value = addend + gp - p + 4;
   5403  1.1  christos 	  /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
   5404  1.1  christos 	     for overflow.  But, on, say, IRIX5, relocations against
   5405  1.1  christos 	     _gp_disp are normally generated from the .cpload
   5406  1.1  christos 	     pseudo-op.  It generates code that normally looks like
   5407  1.1  christos 	     this:
   5408  1.1  christos 
   5409  1.1  christos 	       lui    $gp,%hi(_gp_disp)
   5410  1.1  christos 	       addiu  $gp,$gp,%lo(_gp_disp)
   5411  1.1  christos 	       addu   $gp,$gp,$t9
   5412  1.1  christos 
   5413  1.1  christos 	     Here $t9 holds the address of the function being called,
   5414  1.1  christos 	     as required by the MIPS ELF ABI.  The R_MIPS_LO16
   5415  1.1  christos 	     relocation can easily overflow in this situation, but the
   5416  1.1  christos 	     R_MIPS_HI16 relocation will handle the overflow.
   5417  1.1  christos 	     Therefore, we consider this a bug in the MIPS ABI, and do
   5418  1.1  christos 	     not check for overflow here.  */
   5419  1.1  christos 	}
   5420  1.1  christos       break;
   5421  1.1  christos 
   5422  1.1  christos     case R_MIPS_LITERAL:
   5423  1.1  christos       /* Because we don't merge literal sections, we can handle this
   5424  1.1  christos 	 just like R_MIPS_GPREL16.  In the long run, we should merge
   5425  1.1  christos 	 shared literals, and then we will need to additional work
   5426  1.1  christos 	 here.  */
   5427  1.1  christos 
   5428  1.1  christos       /* Fall through.  */
   5429  1.1  christos 
   5430  1.1  christos     case R_MIPS16_GPREL:
   5431  1.1  christos       /* The R_MIPS16_GPREL performs the same calculation as
   5432  1.1  christos 	 R_MIPS_GPREL16, but stores the relocated bits in a different
   5433  1.1  christos 	 order.  We don't need to do anything special here; the
   5434  1.1  christos 	 differences are handled in mips_elf_perform_relocation.  */
   5435  1.1  christos     case R_MIPS_GPREL16:
   5436  1.1  christos       /* Only sign-extend the addend if it was extracted from the
   5437  1.1  christos 	 instruction.  If the addend was separate, leave it alone,
   5438  1.1  christos 	 otherwise we may lose significant bits.  */
   5439  1.1  christos       if (howto->partial_inplace)
   5440  1.1  christos 	addend = _bfd_mips_elf_sign_extend (addend, 16);
   5441  1.1  christos       value = symbol + addend - gp;
   5442  1.1  christos       /* If the symbol was local, any earlier relocatable links will
   5443  1.1  christos 	 have adjusted its addend with the gp offset, so compensate
   5444  1.1  christos 	 for that now.  Don't do it for symbols forced local in this
   5445  1.1  christos 	 link, though, since they won't have had the gp offset applied
   5446  1.1  christos 	 to them before.  */
   5447  1.1  christos       if (was_local_p)
   5448  1.1  christos 	value += gp0;
   5449  1.1  christos       overflowed_p = mips_elf_overflow_p (value, 16);
   5450  1.1  christos       break;
   5451  1.1  christos 
   5452  1.1  christos     case R_MIPS16_GOT16:
   5453  1.1  christos     case R_MIPS16_CALL16:
   5454  1.1  christos     case R_MIPS_GOT16:
   5455  1.1  christos     case R_MIPS_CALL16:
   5456  1.1  christos       /* VxWorks does not have separate local and global semantics for
   5457  1.1  christos 	 R_MIPS*_GOT16; every relocation evaluates to "G".  */
   5458  1.1  christos       if (!htab->is_vxworks && local_p)
   5459  1.1  christos 	{
   5460  1.1  christos 	  value = mips_elf_got16_entry (abfd, input_bfd, info,
   5461  1.1  christos 					symbol + addend, !was_local_p);
   5462  1.1  christos 	  if (value == MINUS_ONE)
   5463  1.1  christos 	    return bfd_reloc_outofrange;
   5464  1.1  christos 	  value
   5465  1.1  christos 	    = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
   5466  1.1  christos 	  overflowed_p = mips_elf_overflow_p (value, 16);
   5467  1.1  christos 	  break;
   5468  1.1  christos 	}
   5469  1.1  christos 
   5470  1.1  christos       /* Fall through.  */
   5471  1.1  christos 
   5472  1.1  christos     case R_MIPS_TLS_GD:
   5473  1.1  christos     case R_MIPS_TLS_GOTTPREL:
   5474  1.1  christos     case R_MIPS_TLS_LDM:
   5475  1.1  christos     case R_MIPS_GOT_DISP:
   5476  1.1  christos       value = g;
   5477  1.1  christos       overflowed_p = mips_elf_overflow_p (value, 16);
   5478  1.1  christos       break;
   5479  1.1  christos 
   5480  1.1  christos     case R_MIPS_GPREL32:
   5481  1.1  christos       value = (addend + symbol + gp0 - gp);
   5482  1.1  christos       if (!save_addend)
   5483  1.1  christos 	value &= howto->dst_mask;
   5484  1.1  christos       break;
   5485  1.1  christos 
   5486  1.1  christos     case R_MIPS_PC16:
   5487  1.1  christos     case R_MIPS_GNU_REL16_S2:
   5488  1.1  christos       value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
   5489  1.1  christos       overflowed_p = mips_elf_overflow_p (value, 18);
   5490  1.1  christos       value >>= howto->rightshift;
   5491  1.1  christos       value &= howto->dst_mask;
   5492  1.1  christos       break;
   5493  1.1  christos 
   5494  1.1  christos     case R_MIPS_GOT_HI16:
   5495  1.1  christos     case R_MIPS_CALL_HI16:
   5496  1.1  christos       /* We're allowed to handle these two relocations identically.
   5497  1.1  christos 	 The dynamic linker is allowed to handle the CALL relocations
   5498  1.1  christos 	 differently by creating a lazy evaluation stub.  */
   5499  1.1  christos       value = g;
   5500  1.1  christos       value = mips_elf_high (value);
   5501  1.1  christos       value &= howto->dst_mask;
   5502  1.1  christos       break;
   5503  1.1  christos 
   5504  1.1  christos     case R_MIPS_GOT_LO16:
   5505  1.1  christos     case R_MIPS_CALL_LO16:
   5506  1.1  christos       value = g & howto->dst_mask;
   5507  1.1  christos       break;
   5508  1.1  christos 
   5509  1.1  christos     case R_MIPS_GOT_PAGE:
   5510  1.1  christos       value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
   5511  1.1  christos       if (value == MINUS_ONE)
   5512  1.1  christos 	return bfd_reloc_outofrange;
   5513  1.1  christos       value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
   5514  1.1  christos       overflowed_p = mips_elf_overflow_p (value, 16);
   5515  1.1  christos       break;
   5516  1.1  christos 
   5517  1.1  christos     case R_MIPS_GOT_OFST:
   5518  1.1  christos       if (local_p)
   5519  1.1  christos 	mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
   5520  1.1  christos       else
   5521  1.1  christos 	value = addend;
   5522  1.1  christos       overflowed_p = mips_elf_overflow_p (value, 16);
   5523  1.1  christos       break;
   5524  1.1  christos 
   5525  1.1  christos     case R_MIPS_SUB:
   5526  1.1  christos       value = symbol - addend;
   5527  1.1  christos       value &= howto->dst_mask;
   5528  1.1  christos       break;
   5529  1.1  christos 
   5530  1.1  christos     case R_MIPS_HIGHER:
   5531  1.1  christos       value = mips_elf_higher (addend + symbol);
   5532  1.1  christos       value &= howto->dst_mask;
   5533  1.1  christos       break;
   5534  1.1  christos 
   5535  1.1  christos     case R_MIPS_HIGHEST:
   5536  1.1  christos       value = mips_elf_highest (addend + symbol);
   5537  1.1  christos       value &= howto->dst_mask;
   5538  1.1  christos       break;
   5539  1.1  christos 
   5540  1.1  christos     case R_MIPS_SCN_DISP:
   5541  1.1  christos       value = symbol + addend - sec->output_offset;
   5542  1.1  christos       value &= howto->dst_mask;
   5543  1.1  christos       break;
   5544  1.1  christos 
   5545  1.1  christos     case R_MIPS_JALR:
   5546  1.1  christos       /* This relocation is only a hint.  In some cases, we optimize
   5547  1.1  christos 	 it into a bal instruction.  But we don't try to optimize
   5548  1.1  christos 	 when the symbol does not resolve locally.  */
   5549  1.1  christos       if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
   5550  1.1  christos 	return bfd_reloc_continue;
   5551  1.1  christos       value = symbol + addend;
   5552  1.1  christos       break;
   5553  1.1  christos 
   5554  1.1  christos     case R_MIPS_PJUMP:
   5555  1.1  christos     case R_MIPS_GNU_VTINHERIT:
   5556  1.1  christos     case R_MIPS_GNU_VTENTRY:
   5557  1.1  christos       /* We don't do anything with these at present.  */
   5558  1.1  christos       return bfd_reloc_continue;
   5559  1.1  christos 
   5560  1.1  christos     default:
   5561  1.1  christos       /* An unrecognized relocation type.  */
   5562  1.1  christos       return bfd_reloc_notsupported;
   5563  1.1  christos     }
   5564  1.1  christos 
   5565  1.1  christos   /* Store the VALUE for our caller.  */
   5566  1.1  christos   *valuep = value;
   5567  1.1  christos   return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
   5568  1.1  christos }
   5569  1.1  christos 
   5570  1.1  christos /* Obtain the field relocated by RELOCATION.  */
   5571  1.1  christos 
   5572  1.1  christos static bfd_vma
   5573  1.1  christos mips_elf_obtain_contents (reloc_howto_type *howto,
   5574  1.1  christos 			  const Elf_Internal_Rela *relocation,
   5575  1.1  christos 			  bfd *input_bfd, bfd_byte *contents)
   5576  1.1  christos {
   5577  1.1  christos   bfd_vma x;
   5578  1.1  christos   bfd_byte *location = contents + relocation->r_offset;
   5579  1.1  christos 
   5580  1.1  christos   /* Obtain the bytes.  */
   5581  1.1  christos   x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
   5582  1.1  christos 
   5583  1.1  christos   return x;
   5584  1.1  christos }
   5585  1.1  christos 
   5586  1.1  christos /* It has been determined that the result of the RELOCATION is the
   5587  1.1  christos    VALUE.  Use HOWTO to place VALUE into the output file at the
   5588  1.1  christos    appropriate position.  The SECTION is the section to which the
   5589  1.1  christos    relocation applies.
   5590  1.1  christos    CROSS_MODE_JUMP_P is true if the relocation field
   5591  1.1  christos    is a MIPS16 jump to non-MIPS16 code, or vice versa.
   5592  1.1  christos 
   5593  1.1  christos    Returns FALSE if anything goes wrong.  */
   5594  1.1  christos 
   5595  1.1  christos static bfd_boolean
   5596  1.1  christos mips_elf_perform_relocation (struct bfd_link_info *info,
   5597  1.1  christos 			     reloc_howto_type *howto,
   5598  1.1  christos 			     const Elf_Internal_Rela *relocation,
   5599  1.1  christos 			     bfd_vma value, bfd *input_bfd,
   5600  1.1  christos 			     asection *input_section, bfd_byte *contents,
   5601  1.1  christos 			     bfd_boolean cross_mode_jump_p)
   5602  1.1  christos {
   5603  1.1  christos   bfd_vma x;
   5604  1.1  christos   bfd_byte *location;
   5605  1.1  christos   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
   5606  1.1  christos 
   5607  1.1  christos   /* Figure out where the relocation is occurring.  */
   5608  1.1  christos   location = contents + relocation->r_offset;
   5609  1.1  christos 
   5610  1.1  christos   _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
   5611  1.1  christos 
   5612  1.1  christos   /* Obtain the current value.  */
   5613  1.1  christos   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
   5614  1.1  christos 
   5615  1.1  christos   /* Clear the field we are setting.  */
   5616  1.1  christos   x &= ~howto->dst_mask;
   5617  1.1  christos 
   5618  1.1  christos   /* Set the field.  */
   5619  1.1  christos   x |= (value & howto->dst_mask);
   5620  1.1  christos 
   5621  1.1  christos   /* If required, turn JAL into JALX.  */
   5622  1.1  christos   if (cross_mode_jump_p && jal_reloc_p (r_type))
   5623  1.1  christos     {
   5624  1.1  christos       bfd_boolean ok;
   5625  1.1  christos       bfd_vma opcode = x >> 26;
   5626  1.1  christos       bfd_vma jalx_opcode;
   5627  1.1  christos 
   5628  1.1  christos       /* Check to see if the opcode is already JAL or JALX.  */
   5629  1.1  christos       if (r_type == R_MIPS16_26)
   5630  1.1  christos 	{
   5631  1.1  christos 	  ok = ((opcode == 0x6) || (opcode == 0x7));
   5632  1.1  christos 	  jalx_opcode = 0x7;
   5633  1.1  christos 	}
   5634  1.1  christos       else
   5635  1.1  christos 	{
   5636  1.1  christos 	  ok = ((opcode == 0x3) || (opcode == 0x1d));
   5637  1.1  christos 	  jalx_opcode = 0x1d;
   5638  1.1  christos 	}
   5639  1.1  christos 
   5640  1.1  christos       /* If the opcode is not JAL or JALX, there's a problem.  */
   5641  1.1  christos       if (!ok)
   5642  1.1  christos 	{
   5643  1.1  christos 	  (*_bfd_error_handler)
   5644  1.1  christos 	    (_("%B: %A+0x%lx: Direct jumps between ISA modes are not allowed; consider recompiling with interlinking enabled."),
   5645  1.1  christos 	     input_bfd,
   5646  1.1  christos 	     input_section,
   5647  1.1  christos 	     (unsigned long) relocation->r_offset);
   5648  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   5649  1.1  christos 	  return FALSE;
   5650  1.1  christos 	}
   5651  1.1  christos 
   5652  1.1  christos       /* Make this the JALX opcode.  */
   5653  1.1  christos       x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
   5654  1.1  christos     }
   5655  1.1  christos 
   5656  1.1  christos   /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
   5657  1.1  christos      range.  */
   5658  1.1  christos   if (!info->relocatable
   5659  1.1  christos       && !cross_mode_jump_p
   5660  1.1  christos       && ((JAL_TO_BAL_P (input_bfd)
   5661  1.1  christos 	   && r_type == R_MIPS_26
   5662  1.1  christos 	   && (x >> 26) == 0x3)		/* jal addr */
   5663  1.1  christos 	  || (JALR_TO_BAL_P (input_bfd)
   5664  1.1  christos 	      && r_type == R_MIPS_JALR
   5665  1.1  christos 	      && x == 0x0320f809)	/* jalr t9 */
   5666  1.1  christos 	  || (JR_TO_B_P (input_bfd)
   5667  1.1  christos 	      && r_type == R_MIPS_JALR
   5668  1.1  christos 	      && x == 0x03200008)))	/* jr t9 */
   5669  1.1  christos     {
   5670  1.1  christos       bfd_vma addr;
   5671  1.1  christos       bfd_vma dest;
   5672  1.1  christos       bfd_signed_vma off;
   5673  1.1  christos 
   5674  1.1  christos       addr = (input_section->output_section->vma
   5675  1.1  christos 	      + input_section->output_offset
   5676  1.1  christos 	      + relocation->r_offset
   5677  1.1  christos 	      + 4);
   5678  1.1  christos       if (r_type == R_MIPS_26)
   5679  1.1  christos 	dest = (value << 2) | ((addr >> 28) << 28);
   5680  1.1  christos       else
   5681  1.1  christos 	dest = value;
   5682  1.1  christos       off = dest - addr;
   5683  1.1  christos       if (off <= 0x1ffff && off >= -0x20000)
   5684  1.1  christos 	{
   5685  1.1  christos 	  if (x == 0x03200008)	/* jr t9 */
   5686  1.1  christos 	    x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
   5687  1.1  christos 	  else
   5688  1.1  christos 	    x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
   5689  1.1  christos 	}
   5690  1.1  christos     }
   5691  1.1  christos 
   5692  1.1  christos   /* Put the value into the output.  */
   5693  1.1  christos   bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
   5694  1.1  christos 
   5695  1.1  christos   _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
   5696  1.1  christos 				location);
   5697  1.1  christos 
   5698  1.1  christos   return TRUE;
   5699  1.1  christos }
   5700  1.1  christos 
   5701  1.1  christos /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
   5703  1.1  christos    is the original relocation, which is now being transformed into a
   5704  1.1  christos    dynamic relocation.  The ADDENDP is adjusted if necessary; the
   5705  1.1  christos    caller should store the result in place of the original addend.  */
   5706  1.1  christos 
   5707  1.1  christos static bfd_boolean
   5708  1.1  christos mips_elf_create_dynamic_relocation (bfd *output_bfd,
   5709  1.1  christos 				    struct bfd_link_info *info,
   5710  1.1  christos 				    const Elf_Internal_Rela *rel,
   5711  1.1  christos 				    struct mips_elf_link_hash_entry *h,
   5712  1.1  christos 				    asection *sec, bfd_vma symbol,
   5713  1.1  christos 				    bfd_vma *addendp, asection *input_section)
   5714  1.1  christos {
   5715  1.1  christos   Elf_Internal_Rela outrel[3];
   5716  1.1  christos   asection *sreloc;
   5717  1.1  christos   bfd *dynobj;
   5718  1.1  christos   int r_type;
   5719  1.1  christos   long indx;
   5720  1.1  christos   bfd_boolean defined_p;
   5721  1.1  christos   struct mips_elf_link_hash_table *htab;
   5722  1.1  christos 
   5723  1.1  christos   htab = mips_elf_hash_table (info);
   5724  1.1  christos   BFD_ASSERT (htab != NULL);
   5725  1.1  christos 
   5726  1.1  christos   r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   5727  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   5728  1.1  christos   sreloc = mips_elf_rel_dyn_section (info, FALSE);
   5729  1.1  christos   BFD_ASSERT (sreloc != NULL);
   5730  1.1  christos   BFD_ASSERT (sreloc->contents != NULL);
   5731  1.1  christos   BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
   5732  1.1  christos 	      < sreloc->size);
   5733  1.1  christos 
   5734  1.1  christos   outrel[0].r_offset =
   5735  1.1  christos     _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
   5736  1.1  christos   if (ABI_64_P (output_bfd))
   5737  1.1  christos     {
   5738  1.1  christos       outrel[1].r_offset =
   5739  1.1  christos 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
   5740  1.1  christos       outrel[2].r_offset =
   5741  1.1  christos 	_bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
   5742  1.1  christos     }
   5743  1.1  christos 
   5744  1.1  christos   if (outrel[0].r_offset == MINUS_ONE)
   5745  1.1  christos     /* The relocation field has been deleted.  */
   5746  1.1  christos     return TRUE;
   5747  1.1  christos 
   5748  1.1  christos   if (outrel[0].r_offset == MINUS_TWO)
   5749  1.1  christos     {
   5750  1.1  christos       /* The relocation field has been converted into a relative value of
   5751  1.1  christos 	 some sort.  Functions like _bfd_elf_write_section_eh_frame expect
   5752  1.1  christos 	 the field to be fully relocated, so add in the symbol's value.  */
   5753  1.1  christos       *addendp += symbol;
   5754  1.1  christos       return TRUE;
   5755  1.1  christos     }
   5756  1.1  christos 
   5757  1.1  christos   /* We must now calculate the dynamic symbol table index to use
   5758  1.1  christos      in the relocation.  */
   5759  1.1  christos   if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
   5760  1.1  christos     {
   5761  1.1  christos       BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
   5762  1.1  christos       indx = h->root.dynindx;
   5763  1.1  christos       if (SGI_COMPAT (output_bfd))
   5764  1.1  christos 	defined_p = h->root.def_regular;
   5765  1.1  christos       else
   5766  1.1  christos 	/* ??? glibc's ld.so just adds the final GOT entry to the
   5767  1.1  christos 	   relocation field.  It therefore treats relocs against
   5768  1.1  christos 	   defined symbols in the same way as relocs against
   5769  1.1  christos 	   undefined symbols.  */
   5770  1.1  christos 	defined_p = FALSE;
   5771  1.1  christos     }
   5772  1.1  christos   else
   5773  1.1  christos     {
   5774  1.1  christos       if (sec != NULL && bfd_is_abs_section (sec))
   5775  1.1  christos 	indx = 0;
   5776  1.1  christos       else if (sec == NULL || sec->owner == NULL)
   5777  1.1  christos 	{
   5778  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   5779  1.1  christos 	  return FALSE;
   5780  1.1  christos 	}
   5781  1.1  christos       else
   5782  1.1  christos 	{
   5783  1.1  christos 	  indx = elf_section_data (sec->output_section)->dynindx;
   5784  1.1  christos 	  if (indx == 0)
   5785  1.1  christos 	    {
   5786  1.1  christos 	      asection *osec = htab->root.text_index_section;
   5787  1.1  christos 	      indx = elf_section_data (osec)->dynindx;
   5788  1.1  christos 	    }
   5789  1.1  christos 	  if (indx == 0)
   5790  1.1  christos 	    abort ();
   5791  1.1  christos 	}
   5792  1.1  christos 
   5793  1.1  christos       /* Instead of generating a relocation using the section
   5794  1.1  christos 	 symbol, we may as well make it a fully relative
   5795  1.1  christos 	 relocation.  We want to avoid generating relocations to
   5796  1.1  christos 	 local symbols because we used to generate them
   5797  1.1  christos 	 incorrectly, without adding the original symbol value,
   5798  1.1  christos 	 which is mandated by the ABI for section symbols.  In
   5799  1.1  christos 	 order to give dynamic loaders and applications time to
   5800  1.1  christos 	 phase out the incorrect use, we refrain from emitting
   5801  1.1  christos 	 section-relative relocations.  It's not like they're
   5802  1.1  christos 	 useful, after all.  This should be a bit more efficient
   5803  1.1  christos 	 as well.  */
   5804  1.1  christos       /* ??? Although this behavior is compatible with glibc's ld.so,
   5805  1.1  christos 	 the ABI says that relocations against STN_UNDEF should have
   5806  1.1  christos 	 a symbol value of 0.  Irix rld honors this, so relocations
   5807  1.1  christos 	 against STN_UNDEF have no effect.  */
   5808  1.1  christos       if (!SGI_COMPAT (output_bfd))
   5809  1.1  christos 	indx = 0;
   5810  1.1  christos       defined_p = TRUE;
   5811  1.1  christos     }
   5812  1.1  christos 
   5813  1.1  christos   /* If the relocation was previously an absolute relocation and
   5814  1.1  christos      this symbol will not be referred to by the relocation, we must
   5815  1.1  christos      adjust it by the value we give it in the dynamic symbol table.
   5816  1.1  christos      Otherwise leave the job up to the dynamic linker.  */
   5817  1.1  christos   if (defined_p && r_type != R_MIPS_REL32)
   5818  1.1  christos     *addendp += symbol;
   5819  1.1  christos 
   5820  1.1  christos   if (htab->is_vxworks)
   5821  1.1  christos     /* VxWorks uses non-relative relocations for this.  */
   5822  1.1  christos     outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
   5823  1.1  christos   else
   5824  1.1  christos     /* The relocation is always an REL32 relocation because we don't
   5825  1.1  christos        know where the shared library will wind up at load-time.  */
   5826  1.1  christos     outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
   5827  1.1  christos 				   R_MIPS_REL32);
   5828  1.1  christos 
   5829  1.1  christos   /* For strict adherence to the ABI specification, we should
   5830  1.1  christos      generate a R_MIPS_64 relocation record by itself before the
   5831  1.1  christos      _REL32/_64 record as well, such that the addend is read in as
   5832  1.1  christos      a 64-bit value (REL32 is a 32-bit relocation, after all).
   5833  1.1  christos      However, since none of the existing ELF64 MIPS dynamic
   5834  1.1  christos      loaders seems to care, we don't waste space with these
   5835  1.1  christos      artificial relocations.  If this turns out to not be true,
   5836  1.1  christos      mips_elf_allocate_dynamic_relocation() should be tweaked so
   5837  1.1  christos      as to make room for a pair of dynamic relocations per
   5838  1.1  christos      invocation if ABI_64_P, and here we should generate an
   5839  1.1  christos      additional relocation record with R_MIPS_64 by itself for a
   5840  1.1  christos      NULL symbol before this relocation record.  */
   5841  1.1  christos   outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
   5842  1.1  christos 				 ABI_64_P (output_bfd)
   5843  1.1  christos 				 ? R_MIPS_64
   5844  1.1  christos 				 : R_MIPS_NONE);
   5845  1.1  christos   outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
   5846  1.1  christos 
   5847  1.1  christos   /* Adjust the output offset of the relocation to reference the
   5848  1.1  christos      correct location in the output file.  */
   5849  1.1  christos   outrel[0].r_offset += (input_section->output_section->vma
   5850  1.1  christos 			 + input_section->output_offset);
   5851  1.1  christos   outrel[1].r_offset += (input_section->output_section->vma
   5852  1.1  christos 			 + input_section->output_offset);
   5853  1.1  christos   outrel[2].r_offset += (input_section->output_section->vma
   5854  1.1  christos 			 + input_section->output_offset);
   5855  1.1  christos 
   5856  1.1  christos   /* Put the relocation back out.  We have to use the special
   5857  1.1  christos      relocation outputter in the 64-bit case since the 64-bit
   5858  1.1  christos      relocation format is non-standard.  */
   5859  1.1  christos   if (ABI_64_P (output_bfd))
   5860  1.1  christos     {
   5861  1.1  christos       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
   5862  1.1  christos 	(output_bfd, &outrel[0],
   5863  1.1  christos 	 (sreloc->contents
   5864  1.1  christos 	  + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
   5865  1.1  christos     }
   5866  1.1  christos   else if (htab->is_vxworks)
   5867  1.1  christos     {
   5868  1.1  christos       /* VxWorks uses RELA rather than REL dynamic relocations.  */
   5869  1.1  christos       outrel[0].r_addend = *addendp;
   5870  1.1  christos       bfd_elf32_swap_reloca_out
   5871  1.1  christos 	(output_bfd, &outrel[0],
   5872  1.1  christos 	 (sreloc->contents
   5873  1.1  christos 	  + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
   5874  1.1  christos     }
   5875  1.1  christos   else
   5876  1.1  christos     bfd_elf32_swap_reloc_out
   5877  1.1  christos       (output_bfd, &outrel[0],
   5878  1.1  christos        (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
   5879  1.1  christos 
   5880  1.1  christos   /* We've now added another relocation.  */
   5881  1.1  christos   ++sreloc->reloc_count;
   5882  1.1  christos 
   5883  1.1  christos   /* Make sure the output section is writable.  The dynamic linker
   5884  1.1  christos      will be writing to it.  */
   5885  1.1  christos   elf_section_data (input_section->output_section)->this_hdr.sh_flags
   5886  1.1  christos     |= SHF_WRITE;
   5887  1.1  christos 
   5888  1.1  christos   /* On IRIX5, make an entry of compact relocation info.  */
   5889  1.1  christos   if (IRIX_COMPAT (output_bfd) == ict_irix5)
   5890  1.1  christos     {
   5891  1.1  christos       asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
   5892  1.1  christos       bfd_byte *cr;
   5893  1.1  christos 
   5894  1.1  christos       if (scpt)
   5895  1.1  christos 	{
   5896  1.1  christos 	  Elf32_crinfo cptrel;
   5897  1.1  christos 
   5898  1.1  christos 	  mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
   5899  1.1  christos 	  cptrel.vaddr = (rel->r_offset
   5900  1.1  christos 			  + input_section->output_section->vma
   5901  1.1  christos 			  + input_section->output_offset);
   5902  1.1  christos 	  if (r_type == R_MIPS_REL32)
   5903  1.1  christos 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
   5904  1.1  christos 	  else
   5905  1.1  christos 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
   5906  1.1  christos 	  mips_elf_set_cr_dist2to (cptrel, 0);
   5907  1.1  christos 	  cptrel.konst = *addendp;
   5908  1.1  christos 
   5909  1.1  christos 	  cr = (scpt->contents
   5910  1.1  christos 		+ sizeof (Elf32_External_compact_rel));
   5911  1.1  christos 	  mips_elf_set_cr_relvaddr (cptrel, 0);
   5912  1.1  christos 	  bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
   5913  1.1  christos 				     ((Elf32_External_crinfo *) cr
   5914  1.1  christos 				      + scpt->reloc_count));
   5915  1.1  christos 	  ++scpt->reloc_count;
   5916  1.1  christos 	}
   5917  1.1  christos     }
   5918  1.1  christos 
   5919  1.1  christos   /* If we've written this relocation for a readonly section,
   5920  1.1  christos      we need to set DF_TEXTREL again, so that we do not delete the
   5921  1.1  christos      DT_TEXTREL tag.  */
   5922  1.1  christos   if (MIPS_ELF_READONLY_SECTION (input_section))
   5923  1.1  christos     info->flags |= DF_TEXTREL;
   5924  1.1  christos 
   5925  1.1  christos   return TRUE;
   5926  1.1  christos }
   5927  1.1  christos 
   5928  1.1  christos /* Return the MACH for a MIPS e_flags value.  */
   5930  1.1  christos 
   5931  1.1  christos unsigned long
   5932  1.1  christos _bfd_elf_mips_mach (flagword flags)
   5933  1.1  christos {
   5934  1.1  christos   switch (flags & EF_MIPS_MACH)
   5935  1.1  christos     {
   5936  1.1  christos     case E_MIPS_MACH_3900:
   5937  1.1  christos       return bfd_mach_mips3900;
   5938  1.1  christos 
   5939  1.1  christos     case E_MIPS_MACH_4010:
   5940  1.1  christos       return bfd_mach_mips4010;
   5941  1.1  christos 
   5942  1.1  christos     case E_MIPS_MACH_4100:
   5943  1.1  christos       return bfd_mach_mips4100;
   5944  1.1  christos 
   5945  1.1  christos     case E_MIPS_MACH_4111:
   5946  1.1  christos       return bfd_mach_mips4111;
   5947  1.1  christos 
   5948  1.1  christos     case E_MIPS_MACH_4120:
   5949  1.1  christos       return bfd_mach_mips4120;
   5950  1.1  christos 
   5951  1.1  christos     case E_MIPS_MACH_4650:
   5952  1.1  christos       return bfd_mach_mips4650;
   5953  1.1  christos 
   5954  1.1  christos     case E_MIPS_MACH_5400:
   5955  1.1  christos       return bfd_mach_mips5400;
   5956  1.1  christos 
   5957  1.1  christos     case E_MIPS_MACH_5500:
   5958  1.1  christos       return bfd_mach_mips5500;
   5959  1.1  christos 
   5960  1.1  christos     case E_MIPS_MACH_9000:
   5961  1.1  christos       return bfd_mach_mips9000;
   5962  1.1  christos 
   5963  1.1  christos     case E_MIPS_MACH_SB1:
   5964  1.1  christos       return bfd_mach_mips_sb1;
   5965  1.1  christos 
   5966  1.1  christos     case E_MIPS_MACH_LS2E:
   5967  1.1  christos       return bfd_mach_mips_loongson_2e;
   5968  1.1  christos 
   5969  1.1  christos     case E_MIPS_MACH_LS2F:
   5970  1.1  christos       return bfd_mach_mips_loongson_2f;
   5971  1.1  christos 
   5972  1.1  christos     case E_MIPS_MACH_LS3A:
   5973  1.1  christos       return bfd_mach_mips_loongson_3a;
   5974  1.1  christos 
   5975  1.1  christos     case E_MIPS_MACH_OCTEON:
   5976  1.1  christos       return bfd_mach_mips_octeon;
   5977  1.1  christos 
   5978  1.1  christos     case E_MIPS_MACH_XLR:
   5979  1.1  christos       return bfd_mach_mips_xlr;
   5980  1.1  christos 
   5981  1.1  christos     default:
   5982  1.1  christos       switch (flags & EF_MIPS_ARCH)
   5983  1.1  christos 	{
   5984  1.1  christos 	default:
   5985  1.1  christos 	case E_MIPS_ARCH_1:
   5986  1.1  christos 	  return bfd_mach_mips3000;
   5987  1.1  christos 
   5988  1.1  christos 	case E_MIPS_ARCH_2:
   5989  1.1  christos 	  return bfd_mach_mips6000;
   5990  1.1  christos 
   5991  1.1  christos 	case E_MIPS_ARCH_3:
   5992  1.1  christos 	  return bfd_mach_mips4000;
   5993  1.1  christos 
   5994  1.1  christos 	case E_MIPS_ARCH_4:
   5995  1.1  christos 	  return bfd_mach_mips8000;
   5996  1.1  christos 
   5997  1.1  christos 	case E_MIPS_ARCH_5:
   5998  1.1  christos 	  return bfd_mach_mips5;
   5999  1.1  christos 
   6000  1.1  christos 	case E_MIPS_ARCH_32:
   6001  1.1  christos 	  return bfd_mach_mipsisa32;
   6002  1.1  christos 
   6003  1.1  christos 	case E_MIPS_ARCH_64:
   6004  1.1  christos 	  return bfd_mach_mipsisa64;
   6005  1.1  christos 
   6006  1.1  christos 	case E_MIPS_ARCH_32R2:
   6007  1.1  christos 	  return bfd_mach_mipsisa32r2;
   6008  1.1  christos 
   6009  1.1  christos 	case E_MIPS_ARCH_64R2:
   6010  1.1  christos 	  return bfd_mach_mipsisa64r2;
   6011  1.1  christos 	}
   6012  1.1  christos     }
   6013  1.1  christos 
   6014  1.1  christos   return 0;
   6015  1.1  christos }
   6016  1.1  christos 
   6017  1.1  christos /* Return printable name for ABI.  */
   6018  1.1  christos 
   6019  1.1  christos static INLINE char *
   6020  1.1  christos elf_mips_abi_name (bfd *abfd)
   6021  1.1  christos {
   6022  1.1  christos   flagword flags;
   6023  1.1  christos 
   6024  1.1  christos   flags = elf_elfheader (abfd)->e_flags;
   6025  1.1  christos   switch (flags & EF_MIPS_ABI)
   6026  1.1  christos     {
   6027  1.1  christos     case 0:
   6028  1.1  christos       if (ABI_N32_P (abfd))
   6029  1.1  christos 	return "N32";
   6030  1.1  christos       else if (ABI_64_P (abfd))
   6031  1.1  christos 	return "64";
   6032  1.1  christos       else
   6033  1.1  christos 	return "none";
   6034  1.1  christos     case E_MIPS_ABI_O32:
   6035  1.1  christos       return "O32";
   6036  1.1  christos     case E_MIPS_ABI_O64:
   6037  1.1  christos       return "O64";
   6038  1.1  christos     case E_MIPS_ABI_EABI32:
   6039  1.1  christos       return "EABI32";
   6040  1.1  christos     case E_MIPS_ABI_EABI64:
   6041  1.1  christos       return "EABI64";
   6042  1.1  christos     default:
   6043  1.1  christos       return "unknown abi";
   6044  1.1  christos     }
   6045  1.1  christos }
   6046  1.1  christos 
   6047  1.1  christos /* MIPS ELF uses two common sections.  One is the usual one, and the
   6049  1.1  christos    other is for small objects.  All the small objects are kept
   6050  1.1  christos    together, and then referenced via the gp pointer, which yields
   6051  1.1  christos    faster assembler code.  This is what we use for the small common
   6052  1.1  christos    section.  This approach is copied from ecoff.c.  */
   6053  1.1  christos static asection mips_elf_scom_section;
   6054  1.1  christos static asymbol mips_elf_scom_symbol;
   6055  1.1  christos static asymbol *mips_elf_scom_symbol_ptr;
   6056  1.1  christos 
   6057  1.1  christos /* MIPS ELF also uses an acommon section, which represents an
   6058  1.1  christos    allocated common symbol which may be overridden by a
   6059  1.1  christos    definition in a shared library.  */
   6060  1.1  christos static asection mips_elf_acom_section;
   6061  1.1  christos static asymbol mips_elf_acom_symbol;
   6062  1.1  christos static asymbol *mips_elf_acom_symbol_ptr;
   6063  1.1  christos 
   6064  1.1  christos /* This is used for both the 32-bit and the 64-bit ABI.  */
   6065  1.1  christos 
   6066  1.1  christos void
   6067  1.1  christos _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
   6068  1.1  christos {
   6069  1.1  christos   elf_symbol_type *elfsym;
   6070  1.1  christos 
   6071  1.1  christos   /* Handle the special MIPS section numbers that a symbol may use.  */
   6072  1.1  christos   elfsym = (elf_symbol_type *) asym;
   6073  1.1  christos   switch (elfsym->internal_elf_sym.st_shndx)
   6074  1.1  christos     {
   6075  1.1  christos     case SHN_MIPS_ACOMMON:
   6076  1.1  christos       /* This section is used in a dynamically linked executable file.
   6077  1.1  christos 	 It is an allocated common section.  The dynamic linker can
   6078  1.1  christos 	 either resolve these symbols to something in a shared
   6079  1.1  christos 	 library, or it can just leave them here.  For our purposes,
   6080  1.1  christos 	 we can consider these symbols to be in a new section.  */
   6081  1.1  christos       if (mips_elf_acom_section.name == NULL)
   6082  1.1  christos 	{
   6083  1.1  christos 	  /* Initialize the acommon section.  */
   6084  1.1  christos 	  mips_elf_acom_section.name = ".acommon";
   6085  1.1  christos 	  mips_elf_acom_section.flags = SEC_ALLOC;
   6086  1.1  christos 	  mips_elf_acom_section.output_section = &mips_elf_acom_section;
   6087  1.1  christos 	  mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
   6088  1.1  christos 	  mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
   6089  1.1  christos 	  mips_elf_acom_symbol.name = ".acommon";
   6090  1.1  christos 	  mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
   6091  1.1  christos 	  mips_elf_acom_symbol.section = &mips_elf_acom_section;
   6092  1.1  christos 	  mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
   6093  1.1  christos 	}
   6094  1.1  christos       asym->section = &mips_elf_acom_section;
   6095  1.1  christos       break;
   6096  1.1  christos 
   6097  1.1  christos     case SHN_COMMON:
   6098  1.1  christos       /* Common symbols less than the GP size are automatically
   6099  1.1  christos 	 treated as SHN_MIPS_SCOMMON symbols on IRIX5.  */
   6100  1.1  christos       if (asym->value > elf_gp_size (abfd)
   6101  1.1  christos 	  || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
   6102  1.1  christos 	  || IRIX_COMPAT (abfd) == ict_irix6)
   6103  1.1  christos 	break;
   6104  1.1  christos       /* Fall through.  */
   6105  1.1  christos     case SHN_MIPS_SCOMMON:
   6106  1.1  christos       if (mips_elf_scom_section.name == NULL)
   6107  1.1  christos 	{
   6108  1.1  christos 	  /* Initialize the small common section.  */
   6109  1.1  christos 	  mips_elf_scom_section.name = ".scommon";
   6110  1.1  christos 	  mips_elf_scom_section.flags = SEC_IS_COMMON;
   6111  1.1  christos 	  mips_elf_scom_section.output_section = &mips_elf_scom_section;
   6112  1.1  christos 	  mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
   6113  1.1  christos 	  mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
   6114  1.1  christos 	  mips_elf_scom_symbol.name = ".scommon";
   6115  1.1  christos 	  mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
   6116  1.1  christos 	  mips_elf_scom_symbol.section = &mips_elf_scom_section;
   6117  1.1  christos 	  mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
   6118  1.1  christos 	}
   6119  1.1  christos       asym->section = &mips_elf_scom_section;
   6120  1.1  christos       asym->value = elfsym->internal_elf_sym.st_size;
   6121  1.1  christos       break;
   6122  1.1  christos 
   6123  1.1  christos     case SHN_MIPS_SUNDEFINED:
   6124  1.1  christos       asym->section = bfd_und_section_ptr;
   6125  1.1  christos       break;
   6126  1.1  christos 
   6127  1.1  christos     case SHN_MIPS_TEXT:
   6128  1.1  christos       {
   6129  1.1  christos 	asection *section = bfd_get_section_by_name (abfd, ".text");
   6130  1.1  christos 
   6131  1.1  christos 	BFD_ASSERT (SGI_COMPAT (abfd));
   6132  1.1  christos 	if (section != NULL)
   6133  1.1  christos 	  {
   6134  1.1  christos 	    asym->section = section;
   6135  1.1  christos 	    /* MIPS_TEXT is a bit special, the address is not an offset
   6136  1.1  christos 	       to the base of the .text section.  So substract the section
   6137  1.1  christos 	       base address to make it an offset.  */
   6138  1.1  christos 	    asym->value -= section->vma;
   6139  1.1  christos 	  }
   6140  1.1  christos       }
   6141  1.1  christos       break;
   6142  1.1  christos 
   6143  1.1  christos     case SHN_MIPS_DATA:
   6144  1.1  christos       {
   6145  1.1  christos 	asection *section = bfd_get_section_by_name (abfd, ".data");
   6146  1.1  christos 
   6147  1.1  christos 	BFD_ASSERT (SGI_COMPAT (abfd));
   6148  1.1  christos 	if (section != NULL)
   6149  1.1  christos 	  {
   6150  1.1  christos 	    asym->section = section;
   6151  1.1  christos 	    /* MIPS_DATA is a bit special, the address is not an offset
   6152  1.1  christos 	       to the base of the .data section.  So substract the section
   6153  1.1  christos 	       base address to make it an offset.  */
   6154  1.1  christos 	    asym->value -= section->vma;
   6155  1.1  christos 	  }
   6156  1.1  christos       }
   6157  1.1  christos       break;
   6158  1.1  christos     }
   6159  1.1  christos 
   6160  1.1  christos   /* If this is an odd-valued function symbol, assume it's a MIPS16 one.  */
   6161  1.1  christos   if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
   6162  1.1  christos       && (asym->value & 1) != 0)
   6163  1.1  christos     {
   6164  1.1  christos       asym->value--;
   6165  1.1  christos       elfsym->internal_elf_sym.st_other
   6166  1.1  christos 	= ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
   6167  1.1  christos     }
   6168  1.1  christos }
   6169  1.1  christos 
   6170  1.1  christos /* Implement elf_backend_eh_frame_address_size.  This differs from
   6172  1.1  christos    the default in the way it handles EABI64.
   6173  1.1  christos 
   6174  1.1  christos    EABI64 was originally specified as an LP64 ABI, and that is what
   6175  1.1  christos    -mabi=eabi normally gives on a 64-bit target.  However, gcc has
   6176  1.1  christos    historically accepted the combination of -mabi=eabi and -mlong32,
   6177  1.1  christos    and this ILP32 variation has become semi-official over time.
   6178  1.1  christos    Both forms use elf32 and have pointer-sized FDE addresses.
   6179  1.1  christos 
   6180  1.1  christos    If an EABI object was generated by GCC 4.0 or above, it will have
   6181  1.1  christos    an empty .gcc_compiled_longXX section, where XX is the size of longs
   6182  1.1  christos    in bits.  Unfortunately, ILP32 objects generated by earlier compilers
   6183  1.1  christos    have no special marking to distinguish them from LP64 objects.
   6184  1.1  christos 
   6185  1.1  christos    We don't want users of the official LP64 ABI to be punished for the
   6186  1.1  christos    existence of the ILP32 variant, but at the same time, we don't want
   6187  1.1  christos    to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
   6188  1.1  christos    We therefore take the following approach:
   6189  1.1  christos 
   6190  1.1  christos       - If ABFD contains a .gcc_compiled_longXX section, use it to
   6191  1.1  christos         determine the pointer size.
   6192  1.1  christos 
   6193  1.1  christos       - Otherwise check the type of the first relocation.  Assume that
   6194  1.1  christos         the LP64 ABI is being used if the relocation is of type R_MIPS_64.
   6195  1.1  christos 
   6196  1.1  christos       - Otherwise punt.
   6197  1.1  christos 
   6198  1.1  christos    The second check is enough to detect LP64 objects generated by pre-4.0
   6199  1.1  christos    compilers because, in the kind of output generated by those compilers,
   6200  1.1  christos    the first relocation will be associated with either a CIE personality
   6201  1.1  christos    routine or an FDE start address.  Furthermore, the compilers never
   6202  1.1  christos    used a special (non-pointer) encoding for this ABI.
   6203  1.1  christos 
   6204  1.1  christos    Checking the relocation type should also be safe because there is no
   6205  1.1  christos    reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
   6206  1.1  christos    did so.  */
   6207  1.1  christos 
   6208  1.1  christos unsigned int
   6209  1.1  christos _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
   6210  1.1  christos {
   6211  1.1  christos   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   6212  1.1  christos     return 8;
   6213  1.1  christos   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
   6214  1.1  christos     {
   6215  1.1  christos       bfd_boolean long32_p, long64_p;
   6216  1.1  christos 
   6217  1.1  christos       long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
   6218  1.1  christos       long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
   6219  1.1  christos       if (long32_p && long64_p)
   6220  1.1  christos 	return 0;
   6221  1.1  christos       if (long32_p)
   6222  1.1  christos 	return 4;
   6223  1.1  christos       if (long64_p)
   6224  1.1  christos 	return 8;
   6225  1.1  christos 
   6226  1.1  christos       if (sec->reloc_count > 0
   6227  1.1  christos 	  && elf_section_data (sec)->relocs != NULL
   6228  1.1  christos 	  && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
   6229  1.1  christos 	      == R_MIPS_64))
   6230  1.1  christos 	return 8;
   6231  1.1  christos 
   6232  1.1  christos       return 0;
   6233  1.1  christos     }
   6234  1.1  christos   return 4;
   6235  1.1  christos }
   6236  1.1  christos 
   6237  1.1  christos /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
   6239  1.1  christos    relocations against two unnamed section symbols to resolve to the
   6240  1.1  christos    same address.  For example, if we have code like:
   6241  1.1  christos 
   6242  1.1  christos 	lw	$4,%got_disp(.data)($gp)
   6243  1.1  christos 	lw	$25,%got_disp(.text)($gp)
   6244  1.1  christos 	jalr	$25
   6245  1.1  christos 
   6246  1.1  christos    then the linker will resolve both relocations to .data and the program
   6247  1.1  christos    will jump there rather than to .text.
   6248  1.1  christos 
   6249  1.1  christos    We can work around this problem by giving names to local section symbols.
   6250  1.1  christos    This is also what the MIPSpro tools do.  */
   6251  1.1  christos 
   6252  1.1  christos bfd_boolean
   6253  1.1  christos _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
   6254  1.1  christos {
   6255  1.1  christos   return SGI_COMPAT (abfd);
   6256  1.1  christos }
   6257  1.1  christos 
   6258  1.1  christos /* Work over a section just before writing it out.  This routine is
   6260  1.1  christos    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
   6261  1.1  christos    sections that need the SHF_MIPS_GPREL flag by name; there has to be
   6262  1.1  christos    a better way.  */
   6263  1.1  christos 
   6264  1.1  christos bfd_boolean
   6265  1.1  christos _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
   6266  1.1  christos {
   6267  1.1  christos   if (hdr->sh_type == SHT_MIPS_REGINFO
   6268  1.1  christos       && hdr->sh_size > 0)
   6269  1.1  christos     {
   6270  1.1  christos       bfd_byte buf[4];
   6271  1.1  christos 
   6272  1.1  christos       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
   6273  1.1  christos       BFD_ASSERT (hdr->contents == NULL);
   6274  1.1  christos 
   6275  1.1  christos       if (bfd_seek (abfd,
   6276  1.1  christos 		    hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
   6277  1.1  christos 		    SEEK_SET) != 0)
   6278  1.1  christos 	return FALSE;
   6279  1.1  christos       H_PUT_32 (abfd, elf_gp (abfd), buf);
   6280  1.1  christos       if (bfd_bwrite (buf, 4, abfd) != 4)
   6281  1.1  christos 	return FALSE;
   6282  1.1  christos     }
   6283  1.1  christos 
   6284  1.1  christos   if (hdr->sh_type == SHT_MIPS_OPTIONS
   6285  1.1  christos       && hdr->bfd_section != NULL
   6286  1.1  christos       && mips_elf_section_data (hdr->bfd_section) != NULL
   6287  1.1  christos       && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
   6288  1.1  christos     {
   6289  1.1  christos       bfd_byte *contents, *l, *lend;
   6290  1.1  christos 
   6291  1.1  christos       /* We stored the section contents in the tdata field in the
   6292  1.1  christos 	 set_section_contents routine.  We save the section contents
   6293  1.1  christos 	 so that we don't have to read them again.
   6294  1.1  christos 	 At this point we know that elf_gp is set, so we can look
   6295  1.1  christos 	 through the section contents to see if there is an
   6296  1.1  christos 	 ODK_REGINFO structure.  */
   6297  1.1  christos 
   6298  1.1  christos       contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
   6299  1.1  christos       l = contents;
   6300  1.1  christos       lend = contents + hdr->sh_size;
   6301  1.1  christos       while (l + sizeof (Elf_External_Options) <= lend)
   6302  1.1  christos 	{
   6303  1.1  christos 	  Elf_Internal_Options intopt;
   6304  1.1  christos 
   6305  1.1  christos 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
   6306  1.1  christos 					&intopt);
   6307  1.1  christos 	  if (intopt.size < sizeof (Elf_External_Options))
   6308  1.1  christos 	    {
   6309  1.1  christos 	      (*_bfd_error_handler)
   6310  1.1  christos 		(_("%B: Warning: bad `%s' option size %u smaller than its header"),
   6311  1.1  christos 		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
   6312  1.1  christos 	      break;
   6313  1.1  christos 	    }
   6314  1.1  christos 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
   6315  1.1  christos 	    {
   6316  1.1  christos 	      bfd_byte buf[8];
   6317  1.1  christos 
   6318  1.1  christos 	      if (bfd_seek (abfd,
   6319  1.1  christos 			    (hdr->sh_offset
   6320  1.1  christos 			     + (l - contents)
   6321  1.1  christos 			     + sizeof (Elf_External_Options)
   6322  1.1  christos 			     + (sizeof (Elf64_External_RegInfo) - 8)),
   6323  1.1  christos 			     SEEK_SET) != 0)
   6324  1.1  christos 		return FALSE;
   6325  1.1  christos 	      H_PUT_64 (abfd, elf_gp (abfd), buf);
   6326  1.1  christos 	      if (bfd_bwrite (buf, 8, abfd) != 8)
   6327  1.1  christos 		return FALSE;
   6328  1.1  christos 	    }
   6329  1.1  christos 	  else if (intopt.kind == ODK_REGINFO)
   6330  1.1  christos 	    {
   6331  1.1  christos 	      bfd_byte buf[4];
   6332  1.1  christos 
   6333  1.1  christos 	      if (bfd_seek (abfd,
   6334  1.1  christos 			    (hdr->sh_offset
   6335  1.1  christos 			     + (l - contents)
   6336  1.1  christos 			     + sizeof (Elf_External_Options)
   6337  1.1  christos 			     + (sizeof (Elf32_External_RegInfo) - 4)),
   6338  1.1  christos 			    SEEK_SET) != 0)
   6339  1.1  christos 		return FALSE;
   6340  1.1  christos 	      H_PUT_32 (abfd, elf_gp (abfd), buf);
   6341  1.1  christos 	      if (bfd_bwrite (buf, 4, abfd) != 4)
   6342  1.1  christos 		return FALSE;
   6343  1.1  christos 	    }
   6344  1.1  christos 	  l += intopt.size;
   6345  1.1  christos 	}
   6346  1.1  christos     }
   6347  1.1  christos 
   6348  1.1  christos   if (hdr->bfd_section != NULL)
   6349  1.1  christos     {
   6350  1.1  christos       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
   6351  1.1  christos 
   6352  1.1  christos       /* .sbss is not handled specially here because the GNU/Linux
   6353  1.1  christos 	 prelinker can convert .sbss from NOBITS to PROGBITS and
   6354  1.1  christos 	 changing it back to NOBITS breaks the binary.  The entry in
   6355  1.1  christos 	 _bfd_mips_elf_special_sections will ensure the correct flags
   6356  1.1  christos 	 are set on .sbss if BFD creates it without reading it from an
   6357  1.1  christos 	 input file, and without special handling here the flags set
   6358  1.1  christos 	 on it in an input file will be followed.  */
   6359  1.1  christos       if (strcmp (name, ".sdata") == 0
   6360  1.1  christos 	  || strcmp (name, ".lit8") == 0
   6361  1.1  christos 	  || strcmp (name, ".lit4") == 0)
   6362  1.1  christos 	{
   6363  1.1  christos 	  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
   6364  1.1  christos 	  hdr->sh_type = SHT_PROGBITS;
   6365  1.1  christos 	}
   6366  1.1  christos       else if (strcmp (name, ".srdata") == 0)
   6367  1.1  christos 	{
   6368  1.1  christos 	  hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
   6369  1.1  christos 	  hdr->sh_type = SHT_PROGBITS;
   6370  1.1  christos 	}
   6371  1.1  christos       else if (strcmp (name, ".compact_rel") == 0)
   6372  1.1  christos 	{
   6373  1.1  christos 	  hdr->sh_flags = 0;
   6374  1.1  christos 	  hdr->sh_type = SHT_PROGBITS;
   6375  1.1  christos 	}
   6376  1.1  christos       else if (strcmp (name, ".rtproc") == 0)
   6377  1.1  christos 	{
   6378  1.1  christos 	  if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
   6379  1.1  christos 	    {
   6380  1.1  christos 	      unsigned int adjust;
   6381  1.1  christos 
   6382  1.1  christos 	      adjust = hdr->sh_size % hdr->sh_addralign;
   6383  1.1  christos 	      if (adjust != 0)
   6384  1.1  christos 		hdr->sh_size += hdr->sh_addralign - adjust;
   6385  1.1  christos 	    }
   6386  1.1  christos 	}
   6387  1.1  christos     }
   6388  1.1  christos 
   6389  1.1  christos   return TRUE;
   6390  1.1  christos }
   6391  1.1  christos 
   6392  1.1  christos /* Handle a MIPS specific section when reading an object file.  This
   6393  1.1  christos    is called when elfcode.h finds a section with an unknown type.
   6394  1.1  christos    This routine supports both the 32-bit and 64-bit ELF ABI.
   6395  1.1  christos 
   6396  1.1  christos    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
   6397  1.1  christos    how to.  */
   6398  1.1  christos 
   6399  1.1  christos bfd_boolean
   6400  1.1  christos _bfd_mips_elf_section_from_shdr (bfd *abfd,
   6401  1.1  christos 				 Elf_Internal_Shdr *hdr,
   6402  1.1  christos 				 const char *name,
   6403  1.1  christos 				 int shindex)
   6404  1.1  christos {
   6405  1.1  christos   flagword flags = 0;
   6406  1.1  christos 
   6407  1.1  christos   /* There ought to be a place to keep ELF backend specific flags, but
   6408  1.1  christos      at the moment there isn't one.  We just keep track of the
   6409  1.1  christos      sections by their name, instead.  Fortunately, the ABI gives
   6410  1.1  christos      suggested names for all the MIPS specific sections, so we will
   6411  1.1  christos      probably get away with this.  */
   6412  1.1  christos   switch (hdr->sh_type)
   6413  1.1  christos     {
   6414  1.1  christos     case SHT_MIPS_LIBLIST:
   6415  1.1  christos       if (strcmp (name, ".liblist") != 0)
   6416  1.1  christos 	return FALSE;
   6417  1.1  christos       break;
   6418  1.1  christos     case SHT_MIPS_MSYM:
   6419  1.1  christos       if (strcmp (name, ".msym") != 0)
   6420  1.1  christos 	return FALSE;
   6421  1.1  christos       break;
   6422  1.1  christos     case SHT_MIPS_CONFLICT:
   6423  1.1  christos       if (strcmp (name, ".conflict") != 0)
   6424  1.1  christos 	return FALSE;
   6425  1.1  christos       break;
   6426  1.1  christos     case SHT_MIPS_GPTAB:
   6427  1.1  christos       if (! CONST_STRNEQ (name, ".gptab."))
   6428  1.1  christos 	return FALSE;
   6429  1.1  christos       break;
   6430  1.1  christos     case SHT_MIPS_UCODE:
   6431  1.1  christos       if (strcmp (name, ".ucode") != 0)
   6432  1.1  christos 	return FALSE;
   6433  1.1  christos       break;
   6434  1.1  christos     case SHT_MIPS_DEBUG:
   6435  1.1  christos       if (strcmp (name, ".mdebug") != 0)
   6436  1.1  christos 	return FALSE;
   6437  1.1  christos       flags = SEC_DEBUGGING;
   6438  1.1  christos       break;
   6439  1.1  christos     case SHT_MIPS_REGINFO:
   6440  1.1  christos       if (strcmp (name, ".reginfo") != 0
   6441  1.1  christos 	  || hdr->sh_size != sizeof (Elf32_External_RegInfo))
   6442  1.1  christos 	return FALSE;
   6443  1.1  christos       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
   6444  1.1  christos       break;
   6445  1.1  christos     case SHT_MIPS_IFACE:
   6446  1.1  christos       if (strcmp (name, ".MIPS.interfaces") != 0)
   6447  1.1  christos 	return FALSE;
   6448  1.1  christos       break;
   6449  1.1  christos     case SHT_MIPS_CONTENT:
   6450  1.1  christos       if (! CONST_STRNEQ (name, ".MIPS.content"))
   6451  1.1  christos 	return FALSE;
   6452  1.1  christos       break;
   6453  1.1  christos     case SHT_MIPS_OPTIONS:
   6454  1.1  christos       if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
   6455  1.1  christos 	return FALSE;
   6456  1.1  christos       break;
   6457  1.1  christos     case SHT_MIPS_DWARF:
   6458  1.1  christos       if (! CONST_STRNEQ (name, ".debug_")
   6459  1.1  christos           && ! CONST_STRNEQ (name, ".zdebug_"))
   6460  1.1  christos 	return FALSE;
   6461  1.1  christos       break;
   6462  1.1  christos     case SHT_MIPS_SYMBOL_LIB:
   6463  1.1  christos       if (strcmp (name, ".MIPS.symlib") != 0)
   6464  1.1  christos 	return FALSE;
   6465  1.1  christos       break;
   6466  1.1  christos     case SHT_MIPS_EVENTS:
   6467  1.1  christos       if (! CONST_STRNEQ (name, ".MIPS.events")
   6468  1.1  christos 	  && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
   6469  1.1  christos 	return FALSE;
   6470  1.1  christos       break;
   6471  1.1  christos     default:
   6472  1.1  christos       break;
   6473  1.1  christos     }
   6474  1.1  christos 
   6475  1.1  christos   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   6476  1.1  christos     return FALSE;
   6477  1.1  christos 
   6478  1.1  christos   if (flags)
   6479  1.1  christos     {
   6480  1.1  christos       if (! bfd_set_section_flags (abfd, hdr->bfd_section,
   6481  1.1  christos 				   (bfd_get_section_flags (abfd,
   6482  1.1  christos 							   hdr->bfd_section)
   6483  1.1  christos 				    | flags)))
   6484  1.1  christos 	return FALSE;
   6485  1.1  christos     }
   6486  1.1  christos 
   6487  1.1  christos   /* FIXME: We should record sh_info for a .gptab section.  */
   6488  1.1  christos 
   6489  1.1  christos   /* For a .reginfo section, set the gp value in the tdata information
   6490  1.1  christos      from the contents of this section.  We need the gp value while
   6491  1.1  christos      processing relocs, so we just get it now.  The .reginfo section
   6492  1.1  christos      is not used in the 64-bit MIPS ELF ABI.  */
   6493  1.1  christos   if (hdr->sh_type == SHT_MIPS_REGINFO)
   6494  1.1  christos     {
   6495  1.1  christos       Elf32_External_RegInfo ext;
   6496  1.1  christos       Elf32_RegInfo s;
   6497  1.1  christos 
   6498  1.1  christos       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
   6499  1.1  christos 				      &ext, 0, sizeof ext))
   6500  1.1  christos 	return FALSE;
   6501  1.1  christos       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
   6502  1.1  christos       elf_gp (abfd) = s.ri_gp_value;
   6503  1.1  christos     }
   6504  1.1  christos 
   6505  1.1  christos   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
   6506  1.1  christos      set the gp value based on what we find.  We may see both
   6507  1.1  christos      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
   6508  1.1  christos      they should agree.  */
   6509  1.1  christos   if (hdr->sh_type == SHT_MIPS_OPTIONS)
   6510  1.1  christos     {
   6511  1.1  christos       bfd_byte *contents, *l, *lend;
   6512  1.1  christos 
   6513  1.1  christos       contents = bfd_malloc (hdr->sh_size);
   6514  1.1  christos       if (contents == NULL)
   6515  1.1  christos 	return FALSE;
   6516  1.1  christos       if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
   6517  1.1  christos 				      0, hdr->sh_size))
   6518  1.1  christos 	{
   6519  1.1  christos 	  free (contents);
   6520  1.1  christos 	  return FALSE;
   6521  1.1  christos 	}
   6522  1.1  christos       l = contents;
   6523  1.1  christos       lend = contents + hdr->sh_size;
   6524  1.1  christos       while (l + sizeof (Elf_External_Options) <= lend)
   6525  1.1  christos 	{
   6526  1.1  christos 	  Elf_Internal_Options intopt;
   6527  1.1  christos 
   6528  1.1  christos 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
   6529  1.1  christos 					&intopt);
   6530  1.1  christos 	  if (intopt.size < sizeof (Elf_External_Options))
   6531  1.1  christos 	    {
   6532  1.1  christos 	      (*_bfd_error_handler)
   6533  1.1  christos 		(_("%B: Warning: bad `%s' option size %u smaller than its header"),
   6534  1.1  christos 		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
   6535  1.1  christos 	      break;
   6536  1.1  christos 	    }
   6537  1.1  christos 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
   6538  1.1  christos 	    {
   6539  1.1  christos 	      Elf64_Internal_RegInfo intreg;
   6540  1.1  christos 
   6541  1.1  christos 	      bfd_mips_elf64_swap_reginfo_in
   6542  1.1  christos 		(abfd,
   6543  1.1  christos 		 ((Elf64_External_RegInfo *)
   6544  1.1  christos 		  (l + sizeof (Elf_External_Options))),
   6545  1.1  christos 		 &intreg);
   6546  1.1  christos 	      elf_gp (abfd) = intreg.ri_gp_value;
   6547  1.1  christos 	    }
   6548  1.1  christos 	  else if (intopt.kind == ODK_REGINFO)
   6549  1.1  christos 	    {
   6550  1.1  christos 	      Elf32_RegInfo intreg;
   6551  1.1  christos 
   6552  1.1  christos 	      bfd_mips_elf32_swap_reginfo_in
   6553  1.1  christos 		(abfd,
   6554  1.1  christos 		 ((Elf32_External_RegInfo *)
   6555  1.1  christos 		  (l + sizeof (Elf_External_Options))),
   6556  1.1  christos 		 &intreg);
   6557  1.1  christos 	      elf_gp (abfd) = intreg.ri_gp_value;
   6558  1.1  christos 	    }
   6559  1.1  christos 	  l += intopt.size;
   6560  1.1  christos 	}
   6561  1.1  christos       free (contents);
   6562  1.1  christos     }
   6563  1.1  christos 
   6564  1.1  christos   return TRUE;
   6565  1.1  christos }
   6566  1.1  christos 
   6567  1.1  christos /* Set the correct type for a MIPS ELF section.  We do this by the
   6568  1.1  christos    section name, which is a hack, but ought to work.  This routine is
   6569  1.1  christos    used by both the 32-bit and the 64-bit ABI.  */
   6570  1.1  christos 
   6571  1.1  christos bfd_boolean
   6572  1.1  christos _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
   6573  1.1  christos {
   6574  1.1  christos   const char *name = bfd_get_section_name (abfd, sec);
   6575  1.1  christos 
   6576  1.1  christos   if (strcmp (name, ".liblist") == 0)
   6577  1.1  christos     {
   6578  1.1  christos       hdr->sh_type = SHT_MIPS_LIBLIST;
   6579  1.1  christos       hdr->sh_info = sec->size / sizeof (Elf32_Lib);
   6580  1.1  christos       /* The sh_link field is set in final_write_processing.  */
   6581  1.1  christos     }
   6582  1.1  christos   else if (strcmp (name, ".conflict") == 0)
   6583  1.1  christos     hdr->sh_type = SHT_MIPS_CONFLICT;
   6584  1.1  christos   else if (CONST_STRNEQ (name, ".gptab."))
   6585  1.1  christos     {
   6586  1.1  christos       hdr->sh_type = SHT_MIPS_GPTAB;
   6587  1.1  christos       hdr->sh_entsize = sizeof (Elf32_External_gptab);
   6588  1.1  christos       /* The sh_info field is set in final_write_processing.  */
   6589  1.1  christos     }
   6590  1.1  christos   else if (strcmp (name, ".ucode") == 0)
   6591  1.1  christos     hdr->sh_type = SHT_MIPS_UCODE;
   6592  1.1  christos   else if (strcmp (name, ".mdebug") == 0)
   6593  1.1  christos     {
   6594  1.1  christos       hdr->sh_type = SHT_MIPS_DEBUG;
   6595  1.1  christos       /* In a shared object on IRIX 5.3, the .mdebug section has an
   6596  1.1  christos          entsize of 0.  FIXME: Does this matter?  */
   6597  1.1  christos       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
   6598  1.1  christos 	hdr->sh_entsize = 0;
   6599  1.1  christos       else
   6600  1.1  christos 	hdr->sh_entsize = 1;
   6601  1.1  christos     }
   6602  1.1  christos   else if (strcmp (name, ".reginfo") == 0)
   6603  1.1  christos     {
   6604  1.1  christos       hdr->sh_type = SHT_MIPS_REGINFO;
   6605  1.1  christos       /* In a shared object on IRIX 5.3, the .reginfo section has an
   6606  1.1  christos          entsize of 0x18.  FIXME: Does this matter?  */
   6607  1.1  christos       if (SGI_COMPAT (abfd))
   6608  1.1  christos 	{
   6609  1.1  christos 	  if ((abfd->flags & DYNAMIC) != 0)
   6610  1.1  christos 	    hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
   6611  1.1  christos 	  else
   6612  1.1  christos 	    hdr->sh_entsize = 1;
   6613  1.1  christos 	}
   6614  1.1  christos       else
   6615  1.1  christos 	hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
   6616  1.1  christos     }
   6617  1.1  christos   else if (SGI_COMPAT (abfd)
   6618  1.1  christos 	   && (strcmp (name, ".hash") == 0
   6619  1.1  christos 	       || strcmp (name, ".dynamic") == 0
   6620  1.1  christos 	       || strcmp (name, ".dynstr") == 0))
   6621  1.1  christos     {
   6622  1.1  christos       if (SGI_COMPAT (abfd))
   6623  1.1  christos 	hdr->sh_entsize = 0;
   6624  1.1  christos #if 0
   6625  1.1  christos       /* This isn't how the IRIX6 linker behaves.  */
   6626  1.1  christos       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
   6627  1.1  christos #endif
   6628  1.1  christos     }
   6629  1.1  christos   else if (strcmp (name, ".got") == 0
   6630  1.1  christos 	   || strcmp (name, ".srdata") == 0
   6631  1.1  christos 	   || strcmp (name, ".sdata") == 0
   6632  1.1  christos 	   || strcmp (name, ".sbss") == 0
   6633  1.1  christos 	   || strcmp (name, ".lit4") == 0
   6634  1.1  christos 	   || strcmp (name, ".lit8") == 0)
   6635  1.1  christos     hdr->sh_flags |= SHF_MIPS_GPREL;
   6636  1.1  christos   else if (strcmp (name, ".MIPS.interfaces") == 0)
   6637  1.1  christos     {
   6638  1.1  christos       hdr->sh_type = SHT_MIPS_IFACE;
   6639  1.1  christos       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   6640  1.1  christos     }
   6641  1.1  christos   else if (CONST_STRNEQ (name, ".MIPS.content"))
   6642  1.1  christos     {
   6643  1.1  christos       hdr->sh_type = SHT_MIPS_CONTENT;
   6644  1.1  christos       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   6645  1.1  christos       /* The sh_info field is set in final_write_processing.  */
   6646  1.1  christos     }
   6647  1.1  christos   else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
   6648  1.1  christos     {
   6649  1.1  christos       hdr->sh_type = SHT_MIPS_OPTIONS;
   6650  1.1  christos       hdr->sh_entsize = 1;
   6651  1.1  christos       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   6652  1.1  christos     }
   6653  1.1  christos   else if (CONST_STRNEQ (name, ".debug_")
   6654  1.1  christos            || CONST_STRNEQ (name, ".zdebug_"))
   6655  1.1  christos     {
   6656  1.1  christos       hdr->sh_type = SHT_MIPS_DWARF;
   6657  1.1  christos 
   6658  1.1  christos       /* Irix facilities such as libexc expect a single .debug_frame
   6659  1.1  christos 	 per executable, the system ones have NOSTRIP set and the linker
   6660  1.1  christos 	 doesn't merge sections with different flags so ...  */
   6661  1.1  christos       if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
   6662  1.1  christos 	hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   6663  1.1  christos     }
   6664  1.1  christos   else if (strcmp (name, ".MIPS.symlib") == 0)
   6665  1.1  christos     {
   6666  1.1  christos       hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
   6667  1.1  christos       /* The sh_link and sh_info fields are set in
   6668  1.1  christos          final_write_processing.  */
   6669  1.1  christos     }
   6670  1.1  christos   else if (CONST_STRNEQ (name, ".MIPS.events")
   6671  1.1  christos 	   || CONST_STRNEQ (name, ".MIPS.post_rel"))
   6672  1.1  christos     {
   6673  1.1  christos       hdr->sh_type = SHT_MIPS_EVENTS;
   6674  1.1  christos       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
   6675  1.1  christos       /* The sh_link field is set in final_write_processing.  */
   6676  1.1  christos     }
   6677  1.1  christos   else if (strcmp (name, ".msym") == 0)
   6678  1.1  christos     {
   6679  1.1  christos       hdr->sh_type = SHT_MIPS_MSYM;
   6680  1.1  christos       hdr->sh_flags |= SHF_ALLOC;
   6681  1.1  christos       hdr->sh_entsize = 8;
   6682  1.1  christos     }
   6683  1.1  christos 
   6684  1.1  christos   /* The generic elf_fake_sections will set up REL_HDR using the default
   6685  1.1  christos    kind of relocations.  We used to set up a second header for the
   6686  1.1  christos    non-default kind of relocations here, but only NewABI would use
   6687  1.1  christos    these, and the IRIX ld doesn't like resulting empty RELA sections.
   6688  1.1  christos    Thus we create those header only on demand now.  */
   6689  1.1  christos 
   6690  1.1  christos   return TRUE;
   6691  1.1  christos }
   6692  1.1  christos 
   6693  1.1  christos /* Given a BFD section, try to locate the corresponding ELF section
   6694  1.1  christos    index.  This is used by both the 32-bit and the 64-bit ABI.
   6695  1.1  christos    Actually, it's not clear to me that the 64-bit ABI supports these,
   6696  1.1  christos    but for non-PIC objects we will certainly want support for at least
   6697  1.1  christos    the .scommon section.  */
   6698  1.1  christos 
   6699  1.1  christos bfd_boolean
   6700  1.1  christos _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
   6701  1.1  christos 					asection *sec, int *retval)
   6702  1.1  christos {
   6703  1.1  christos   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
   6704  1.1  christos     {
   6705  1.1  christos       *retval = SHN_MIPS_SCOMMON;
   6706  1.1  christos       return TRUE;
   6707  1.1  christos     }
   6708  1.1  christos   if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
   6709  1.1  christos     {
   6710  1.1  christos       *retval = SHN_MIPS_ACOMMON;
   6711  1.1  christos       return TRUE;
   6712  1.1  christos     }
   6713  1.1  christos   return FALSE;
   6714  1.1  christos }
   6715  1.1  christos 
   6716  1.1  christos /* Hook called by the linker routine which adds symbols from an object
   6718  1.1  christos    file.  We must handle the special MIPS section numbers here.  */
   6719  1.1  christos 
   6720  1.1  christos bfd_boolean
   6721  1.1  christos _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
   6722  1.1  christos 			       Elf_Internal_Sym *sym, const char **namep,
   6723  1.1  christos 			       flagword *flagsp ATTRIBUTE_UNUSED,
   6724  1.1  christos 			       asection **secp, bfd_vma *valp)
   6725  1.1  christos {
   6726  1.1  christos   if (SGI_COMPAT (abfd)
   6727  1.1  christos       && (abfd->flags & DYNAMIC) != 0
   6728  1.1  christos       && strcmp (*namep, "_rld_new_interface") == 0)
   6729  1.1  christos     {
   6730  1.1  christos       /* Skip IRIX5 rld entry name.  */
   6731  1.1  christos       *namep = NULL;
   6732  1.1  christos       return TRUE;
   6733  1.1  christos     }
   6734  1.1  christos 
   6735  1.1  christos   /* Shared objects may have a dynamic symbol '_gp_disp' defined as
   6736  1.1  christos      a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
   6737  1.1  christos      by setting a DT_NEEDED for the shared object.  Since _gp_disp is
   6738  1.1  christos      a magic symbol resolved by the linker, we ignore this bogus definition
   6739  1.1  christos      of _gp_disp.  New ABI objects do not suffer from this problem so this
   6740  1.1  christos      is not done for them. */
   6741  1.1  christos   if (!NEWABI_P(abfd)
   6742  1.1  christos       && (sym->st_shndx == SHN_ABS)
   6743  1.1  christos       && (strcmp (*namep, "_gp_disp") == 0))
   6744  1.1  christos     {
   6745  1.1  christos       *namep = NULL;
   6746  1.1  christos       return TRUE;
   6747  1.1  christos     }
   6748  1.1  christos 
   6749  1.1  christos   switch (sym->st_shndx)
   6750  1.1  christos     {
   6751  1.1  christos     case SHN_COMMON:
   6752  1.1  christos       /* Common symbols less than the GP size are automatically
   6753  1.1  christos 	 treated as SHN_MIPS_SCOMMON symbols.  */
   6754  1.1  christos       if (sym->st_size > elf_gp_size (abfd)
   6755  1.1  christos 	  || ELF_ST_TYPE (sym->st_info) == STT_TLS
   6756  1.1  christos 	  || IRIX_COMPAT (abfd) == ict_irix6)
   6757  1.1  christos 	break;
   6758  1.1  christos       /* Fall through.  */
   6759  1.1  christos     case SHN_MIPS_SCOMMON:
   6760  1.1  christos       *secp = bfd_make_section_old_way (abfd, ".scommon");
   6761  1.1  christos       (*secp)->flags |= SEC_IS_COMMON;
   6762  1.1  christos       *valp = sym->st_size;
   6763  1.1  christos       break;
   6764  1.1  christos 
   6765  1.1  christos     case SHN_MIPS_TEXT:
   6766  1.1  christos       /* This section is used in a shared object.  */
   6767  1.1  christos       if (elf_tdata (abfd)->elf_text_section == NULL)
   6768  1.1  christos 	{
   6769  1.1  christos 	  asymbol *elf_text_symbol;
   6770  1.1  christos 	  asection *elf_text_section;
   6771  1.1  christos 	  bfd_size_type amt = sizeof (asection);
   6772  1.1  christos 
   6773  1.1  christos 	  elf_text_section = bfd_zalloc (abfd, amt);
   6774  1.1  christos 	  if (elf_text_section == NULL)
   6775  1.1  christos 	    return FALSE;
   6776  1.1  christos 
   6777  1.1  christos 	  amt = sizeof (asymbol);
   6778  1.1  christos 	  elf_text_symbol = bfd_zalloc (abfd, amt);
   6779  1.1  christos 	  if (elf_text_symbol == NULL)
   6780  1.1  christos 	    return FALSE;
   6781  1.1  christos 
   6782  1.1  christos 	  /* Initialize the section.  */
   6783  1.1  christos 
   6784  1.1  christos 	  elf_tdata (abfd)->elf_text_section = elf_text_section;
   6785  1.1  christos 	  elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
   6786  1.1  christos 
   6787  1.1  christos 	  elf_text_section->symbol = elf_text_symbol;
   6788  1.1  christos 	  elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
   6789  1.1  christos 
   6790  1.1  christos 	  elf_text_section->name = ".text";
   6791  1.1  christos 	  elf_text_section->flags = SEC_NO_FLAGS;
   6792  1.1  christos 	  elf_text_section->output_section = NULL;
   6793  1.1  christos 	  elf_text_section->owner = abfd;
   6794  1.1  christos 	  elf_text_symbol->name = ".text";
   6795  1.1  christos 	  elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
   6796  1.1  christos 	  elf_text_symbol->section = elf_text_section;
   6797  1.1  christos 	}
   6798  1.1  christos       /* This code used to do *secp = bfd_und_section_ptr if
   6799  1.1  christos          info->shared.  I don't know why, and that doesn't make sense,
   6800  1.1  christos          so I took it out.  */
   6801  1.1  christos       *secp = elf_tdata (abfd)->elf_text_section;
   6802  1.1  christos       break;
   6803  1.1  christos 
   6804  1.1  christos     case SHN_MIPS_ACOMMON:
   6805  1.1  christos       /* Fall through. XXX Can we treat this as allocated data?  */
   6806  1.1  christos     case SHN_MIPS_DATA:
   6807  1.1  christos       /* This section is used in a shared object.  */
   6808  1.1  christos       if (elf_tdata (abfd)->elf_data_section == NULL)
   6809  1.1  christos 	{
   6810  1.1  christos 	  asymbol *elf_data_symbol;
   6811  1.1  christos 	  asection *elf_data_section;
   6812  1.1  christos 	  bfd_size_type amt = sizeof (asection);
   6813  1.1  christos 
   6814  1.1  christos 	  elf_data_section = bfd_zalloc (abfd, amt);
   6815  1.1  christos 	  if (elf_data_section == NULL)
   6816  1.1  christos 	    return FALSE;
   6817  1.1  christos 
   6818  1.1  christos 	  amt = sizeof (asymbol);
   6819  1.1  christos 	  elf_data_symbol = bfd_zalloc (abfd, amt);
   6820  1.1  christos 	  if (elf_data_symbol == NULL)
   6821  1.1  christos 	    return FALSE;
   6822  1.1  christos 
   6823  1.1  christos 	  /* Initialize the section.  */
   6824  1.1  christos 
   6825  1.1  christos 	  elf_tdata (abfd)->elf_data_section = elf_data_section;
   6826  1.1  christos 	  elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
   6827  1.1  christos 
   6828  1.1  christos 	  elf_data_section->symbol = elf_data_symbol;
   6829  1.1  christos 	  elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
   6830  1.1  christos 
   6831  1.1  christos 	  elf_data_section->name = ".data";
   6832  1.1  christos 	  elf_data_section->flags = SEC_NO_FLAGS;
   6833  1.1  christos 	  elf_data_section->output_section = NULL;
   6834  1.1  christos 	  elf_data_section->owner = abfd;
   6835  1.1  christos 	  elf_data_symbol->name = ".data";
   6836  1.1  christos 	  elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
   6837  1.1  christos 	  elf_data_symbol->section = elf_data_section;
   6838  1.1  christos 	}
   6839  1.1  christos       /* This code used to do *secp = bfd_und_section_ptr if
   6840  1.1  christos          info->shared.  I don't know why, and that doesn't make sense,
   6841  1.1  christos          so I took it out.  */
   6842  1.1  christos       *secp = elf_tdata (abfd)->elf_data_section;
   6843  1.1  christos       break;
   6844  1.1  christos 
   6845  1.1  christos     case SHN_MIPS_SUNDEFINED:
   6846  1.1  christos       *secp = bfd_und_section_ptr;
   6847  1.1  christos       break;
   6848  1.1  christos     }
   6849  1.1  christos 
   6850  1.1  christos   if (SGI_COMPAT (abfd)
   6851  1.1  christos       && ! info->shared
   6852  1.1  christos       && info->output_bfd->xvec == abfd->xvec
   6853  1.1  christos       && strcmp (*namep, "__rld_obj_head") == 0)
   6854  1.1  christos     {
   6855  1.1  christos       struct elf_link_hash_entry *h;
   6856  1.1  christos       struct bfd_link_hash_entry *bh;
   6857  1.1  christos 
   6858  1.1  christos       /* Mark __rld_obj_head as dynamic.  */
   6859  1.1  christos       bh = NULL;
   6860  1.1  christos       if (! (_bfd_generic_link_add_one_symbol
   6861  1.1  christos 	     (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
   6862  1.1  christos 	      get_elf_backend_data (abfd)->collect, &bh)))
   6863  1.1  christos 	return FALSE;
   6864  1.1  christos 
   6865  1.1  christos       h = (struct elf_link_hash_entry *) bh;
   6866  1.1  christos       h->non_elf = 0;
   6867  1.1  christos       h->def_regular = 1;
   6868  1.1  christos       h->type = STT_OBJECT;
   6869  1.1  christos 
   6870  1.1  christos       if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6871  1.1  christos 	return FALSE;
   6872  1.1  christos 
   6873  1.1  christos       mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
   6874  1.1  christos     }
   6875  1.1  christos 
   6876  1.1  christos   /* If this is a mips16 text symbol, add 1 to the value to make it
   6877  1.1  christos      odd.  This will cause something like .word SYM to come up with
   6878  1.1  christos      the right value when it is loaded into the PC.  */
   6879  1.1  christos   if (ELF_ST_IS_MIPS16 (sym->st_other))
   6880  1.1  christos     ++*valp;
   6881  1.1  christos 
   6882  1.1  christos   return TRUE;
   6883  1.1  christos }
   6884  1.1  christos 
   6885  1.1  christos /* This hook function is called before the linker writes out a global
   6886  1.1  christos    symbol.  We mark symbols as small common if appropriate.  This is
   6887  1.1  christos    also where we undo the increment of the value for a mips16 symbol.  */
   6888  1.1  christos 
   6889  1.1  christos int
   6890  1.1  christos _bfd_mips_elf_link_output_symbol_hook
   6891  1.1  christos   (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   6892  1.1  christos    const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
   6893  1.1  christos    asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
   6894  1.1  christos {
   6895  1.1  christos   /* If we see a common symbol, which implies a relocatable link, then
   6896  1.1  christos      if a symbol was small common in an input file, mark it as small
   6897  1.1  christos      common in the output file.  */
   6898  1.1  christos   if (sym->st_shndx == SHN_COMMON
   6899  1.1  christos       && strcmp (input_sec->name, ".scommon") == 0)
   6900  1.1  christos     sym->st_shndx = SHN_MIPS_SCOMMON;
   6901  1.1  christos 
   6902  1.1  christos   if (ELF_ST_IS_MIPS16 (sym->st_other))
   6903  1.1  christos     sym->st_value &= ~1;
   6904  1.1  christos 
   6905  1.1  christos   return 1;
   6906  1.1  christos }
   6907  1.1  christos 
   6908  1.1  christos /* Functions for the dynamic linker.  */
   6910  1.1  christos 
   6911  1.1  christos /* Create dynamic sections when linking against a dynamic object.  */
   6912  1.1  christos 
   6913  1.1  christos bfd_boolean
   6914  1.1  christos _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   6915  1.1  christos {
   6916  1.1  christos   struct elf_link_hash_entry *h;
   6917  1.1  christos   struct bfd_link_hash_entry *bh;
   6918  1.1  christos   flagword flags;
   6919  1.1  christos   register asection *s;
   6920  1.1  christos   const char * const *namep;
   6921  1.1  christos   struct mips_elf_link_hash_table *htab;
   6922  1.1  christos 
   6923  1.1  christos   htab = mips_elf_hash_table (info);
   6924  1.1  christos   BFD_ASSERT (htab != NULL);
   6925  1.1  christos 
   6926  1.1  christos   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   6927  1.1  christos 	   | SEC_LINKER_CREATED | SEC_READONLY);
   6928  1.1  christos 
   6929  1.1  christos   /* The psABI requires a read-only .dynamic section, but the VxWorks
   6930  1.1  christos      EABI doesn't.  */
   6931  1.1  christos   if (!htab->is_vxworks)
   6932  1.1  christos     {
   6933  1.1  christos       s = bfd_get_section_by_name (abfd, ".dynamic");
   6934  1.1  christos       if (s != NULL)
   6935  1.1  christos 	{
   6936  1.1  christos 	  if (! bfd_set_section_flags (abfd, s, flags))
   6937  1.1  christos 	    return FALSE;
   6938  1.1  christos 	}
   6939  1.1  christos     }
   6940  1.1  christos 
   6941  1.1  christos   /* We need to create .got section.  */
   6942  1.1  christos   if (!mips_elf_create_got_section (abfd, info))
   6943  1.1  christos     return FALSE;
   6944  1.1  christos 
   6945  1.1  christos   if (! mips_elf_rel_dyn_section (info, TRUE))
   6946  1.1  christos     return FALSE;
   6947  1.1  christos 
   6948  1.1  christos   /* Create .stub section.  */
   6949  1.1  christos   s = bfd_make_section_with_flags (abfd,
   6950  1.1  christos 				   MIPS_ELF_STUB_SECTION_NAME (abfd),
   6951  1.1  christos 				   flags | SEC_CODE);
   6952  1.1  christos   if (s == NULL
   6953  1.1  christos       || ! bfd_set_section_alignment (abfd, s,
   6954  1.1  christos 				      MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   6955  1.1  christos     return FALSE;
   6956  1.1  christos   htab->sstubs = s;
   6957  1.1  christos 
   6958  1.1  christos   if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
   6959  1.1  christos       && !info->shared
   6960  1.1  christos       && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
   6961  1.1  christos     {
   6962  1.1  christos       s = bfd_make_section_with_flags (abfd, ".rld_map",
   6963  1.1  christos 				       flags &~ (flagword) SEC_READONLY);
   6964  1.1  christos       if (s == NULL
   6965  1.1  christos 	  || ! bfd_set_section_alignment (abfd, s,
   6966  1.1  christos 					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
   6967  1.1  christos 	return FALSE;
   6968  1.1  christos     }
   6969  1.1  christos 
   6970  1.1  christos   /* On IRIX5, we adjust add some additional symbols and change the
   6971  1.1  christos      alignments of several sections.  There is no ABI documentation
   6972  1.1  christos      indicating that this is necessary on IRIX6, nor any evidence that
   6973  1.1  christos      the linker takes such action.  */
   6974  1.1  christos   if (IRIX_COMPAT (abfd) == ict_irix5)
   6975  1.1  christos     {
   6976  1.1  christos       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
   6977  1.1  christos 	{
   6978  1.1  christos 	  bh = NULL;
   6979  1.1  christos 	  if (! (_bfd_generic_link_add_one_symbol
   6980  1.1  christos 		 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
   6981  1.1  christos 		  NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
   6982  1.1  christos 	    return FALSE;
   6983  1.1  christos 
   6984  1.1  christos 	  h = (struct elf_link_hash_entry *) bh;
   6985  1.1  christos 	  h->non_elf = 0;
   6986  1.1  christos 	  h->def_regular = 1;
   6987  1.1  christos 	  h->type = STT_SECTION;
   6988  1.1  christos 
   6989  1.1  christos 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   6990  1.1  christos 	    return FALSE;
   6991  1.1  christos 	}
   6992  1.1  christos 
   6993  1.1  christos       /* We need to create a .compact_rel section.  */
   6994  1.1  christos       if (SGI_COMPAT (abfd))
   6995  1.1  christos 	{
   6996  1.1  christos 	  if (!mips_elf_create_compact_rel_section (abfd, info))
   6997  1.1  christos 	    return FALSE;
   6998  1.1  christos 	}
   6999  1.1  christos 
   7000  1.1  christos       /* Change alignments of some sections.  */
   7001  1.1  christos       s = bfd_get_section_by_name (abfd, ".hash");
   7002  1.1  christos       if (s != NULL)
   7003  1.1  christos 	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7004  1.1  christos       s = bfd_get_section_by_name (abfd, ".dynsym");
   7005  1.1  christos       if (s != NULL)
   7006  1.1  christos 	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7007  1.1  christos       s = bfd_get_section_by_name (abfd, ".dynstr");
   7008  1.1  christos       if (s != NULL)
   7009  1.1  christos 	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7010  1.1  christos       s = bfd_get_section_by_name (abfd, ".reginfo");
   7011  1.1  christos       if (s != NULL)
   7012  1.1  christos 	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7013  1.1  christos       s = bfd_get_section_by_name (abfd, ".dynamic");
   7014  1.1  christos       if (s != NULL)
   7015  1.1  christos 	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
   7016  1.1  christos     }
   7017  1.1  christos 
   7018  1.1  christos   if (!info->shared)
   7019  1.1  christos     {
   7020  1.1  christos       const char *name;
   7021  1.1  christos 
   7022  1.1  christos       name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
   7023  1.1  christos       bh = NULL;
   7024  1.1  christos       if (!(_bfd_generic_link_add_one_symbol
   7025  1.1  christos 	    (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
   7026  1.1  christos 	     NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
   7027  1.1  christos 	return FALSE;
   7028  1.1  christos 
   7029  1.1  christos       h = (struct elf_link_hash_entry *) bh;
   7030  1.1  christos       h->non_elf = 0;
   7031  1.1  christos       h->def_regular = 1;
   7032  1.1  christos       h->type = STT_SECTION;
   7033  1.1  christos 
   7034  1.1  christos       if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7035  1.1  christos 	return FALSE;
   7036  1.1  christos 
   7037  1.1  christos       if (! mips_elf_hash_table (info)->use_rld_obj_head)
   7038  1.1  christos 	{
   7039  1.1  christos 	  /* __rld_map is a four byte word located in the .data section
   7040  1.1  christos 	     and is filled in by the rtld to contain a pointer to
   7041  1.1  christos 	     the _r_debug structure. Its symbol value will be set in
   7042  1.1  christos 	     _bfd_mips_elf_finish_dynamic_symbol.  */
   7043  1.1  christos 	  s = bfd_get_section_by_name (abfd, ".rld_map");
   7044  1.1  christos 	  BFD_ASSERT (s != NULL);
   7045  1.1  christos 
   7046  1.1  christos 	  name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
   7047  1.1  christos 	  bh = NULL;
   7048  1.1  christos 	  if (!(_bfd_generic_link_add_one_symbol
   7049  1.1  christos 		(info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
   7050  1.1  christos 		 get_elf_backend_data (abfd)->collect, &bh)))
   7051  1.1  christos 	    return FALSE;
   7052  1.1  christos 
   7053  1.1  christos 	  h = (struct elf_link_hash_entry *) bh;
   7054  1.1  christos 	  h->non_elf = 0;
   7055  1.1  christos 	  h->def_regular = 1;
   7056  1.1  christos 	  h->type = STT_OBJECT;
   7057  1.1  christos 
   7058  1.1  christos 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   7059  1.1  christos 	    return FALSE;
   7060  1.1  christos 	}
   7061  1.1  christos     }
   7062  1.1  christos 
   7063  1.1  christos   /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
   7064  1.1  christos      Also create the _PROCEDURE_LINKAGE_TABLE symbol.  */
   7065  1.1  christos   if (!_bfd_elf_create_dynamic_sections (abfd, info))
   7066  1.1  christos     return FALSE;
   7067  1.1  christos 
   7068  1.1  christos   /* Cache the sections created above.  */
   7069  1.1  christos   htab->splt = bfd_get_section_by_name (abfd, ".plt");
   7070  1.1  christos   htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
   7071  1.1  christos   if (htab->is_vxworks)
   7072  1.1  christos     {
   7073  1.1  christos       htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
   7074  1.1  christos       htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
   7075  1.1  christos     }
   7076  1.1  christos   else
   7077  1.1  christos     htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
   7078  1.1  christos   if (!htab->sdynbss
   7079  1.1  christos       || (htab->is_vxworks && !htab->srelbss && !info->shared)
   7080  1.1  christos       || !htab->srelplt
   7081  1.1  christos       || !htab->splt)
   7082  1.1  christos     abort ();
   7083  1.1  christos 
   7084  1.1  christos   if (htab->is_vxworks)
   7085  1.1  christos     {
   7086  1.1  christos       /* Do the usual VxWorks handling.  */
   7087  1.1  christos       if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
   7088  1.1  christos 	return FALSE;
   7089  1.1  christos 
   7090  1.1  christos       /* Work out the PLT sizes.  */
   7091  1.1  christos       if (info->shared)
   7092  1.1  christos 	{
   7093  1.1  christos 	  htab->plt_header_size
   7094  1.1  christos 	    = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
   7095  1.1  christos 	  htab->plt_entry_size
   7096  1.1  christos 	    = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
   7097  1.1  christos 	}
   7098  1.1  christos       else
   7099  1.1  christos 	{
   7100  1.1  christos 	  htab->plt_header_size
   7101  1.1  christos 	    = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
   7102  1.1  christos 	  htab->plt_entry_size
   7103  1.1  christos 	    = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
   7104  1.1  christos 	}
   7105  1.1  christos     }
   7106  1.1  christos   else if (!info->shared)
   7107  1.1  christos     {
   7108  1.1  christos       /* All variants of the plt0 entry are the same size.  */
   7109  1.1  christos       htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
   7110  1.1  christos       htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
   7111  1.1  christos     }
   7112  1.1  christos 
   7113  1.1  christos   return TRUE;
   7114  1.1  christos }
   7115  1.1  christos 
   7116  1.1  christos /* Return true if relocation REL against section SEC is a REL rather than
   7118  1.1  christos    RELA relocation.  RELOCS is the first relocation in the section and
   7119  1.1  christos    ABFD is the bfd that contains SEC.  */
   7120  1.1  christos 
   7121  1.1  christos static bfd_boolean
   7122  1.1  christos mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
   7123  1.1  christos 			   const Elf_Internal_Rela *relocs,
   7124  1.1  christos 			   const Elf_Internal_Rela *rel)
   7125  1.1  christos {
   7126  1.1  christos   Elf_Internal_Shdr *rel_hdr;
   7127  1.1  christos   const struct elf_backend_data *bed;
   7128  1.1  christos 
   7129  1.1  christos   /* To determine which flavor of relocation this is, we depend on the
   7130  1.1  christos      fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
   7131  1.1  christos   rel_hdr = elf_section_data (sec)->rel.hdr;
   7132  1.1  christos   if (rel_hdr == NULL)
   7133  1.1  christos     return FALSE;
   7134  1.1  christos   bed = get_elf_backend_data (abfd);
   7135  1.1  christos   return ((size_t) (rel - relocs)
   7136  1.1  christos 	  < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
   7137  1.1  christos }
   7138  1.1  christos 
   7139  1.1  christos /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
   7140  1.1  christos    HOWTO is the relocation's howto and CONTENTS points to the contents
   7141  1.1  christos    of the section that REL is against.  */
   7142  1.1  christos 
   7143  1.1  christos static bfd_vma
   7144  1.1  christos mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
   7145  1.1  christos 			  reloc_howto_type *howto, bfd_byte *contents)
   7146  1.1  christos {
   7147  1.1  christos   bfd_byte *location;
   7148  1.1  christos   unsigned int r_type;
   7149  1.1  christos   bfd_vma addend;
   7150  1.1  christos 
   7151  1.1  christos   r_type = ELF_R_TYPE (abfd, rel->r_info);
   7152  1.1  christos   location = contents + rel->r_offset;
   7153  1.1  christos 
   7154  1.1  christos   /* Get the addend, which is stored in the input file.  */
   7155  1.1  christos   _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
   7156  1.1  christos   addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
   7157  1.1  christos   _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
   7158  1.1  christos 
   7159  1.1  christos   return addend & howto->src_mask;
   7160  1.1  christos }
   7161  1.1  christos 
   7162  1.1  christos /* REL is a relocation in ABFD that needs a partnering LO16 relocation
   7163  1.1  christos    and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
   7164  1.1  christos    and update *ADDEND with the final addend.  Return true on success
   7165  1.1  christos    or false if the LO16 could not be found.  RELEND is the exclusive
   7166  1.1  christos    upper bound on the relocations for REL's section.  */
   7167  1.1  christos 
   7168  1.1  christos static bfd_boolean
   7169  1.1  christos mips_elf_add_lo16_rel_addend (bfd *abfd,
   7170  1.1  christos 			      const Elf_Internal_Rela *rel,
   7171  1.1  christos 			      const Elf_Internal_Rela *relend,
   7172  1.1  christos 			      bfd_byte *contents, bfd_vma *addend)
   7173  1.1  christos {
   7174  1.1  christos   unsigned int r_type, lo16_type;
   7175  1.1  christos   const Elf_Internal_Rela *lo16_relocation;
   7176  1.1  christos   reloc_howto_type *lo16_howto;
   7177  1.1  christos   bfd_vma l;
   7178  1.1  christos 
   7179  1.1  christos   r_type = ELF_R_TYPE (abfd, rel->r_info);
   7180  1.1  christos   if (mips16_reloc_p (r_type))
   7181  1.1  christos     lo16_type = R_MIPS16_LO16;
   7182  1.1  christos   else
   7183  1.1  christos     lo16_type = R_MIPS_LO16;
   7184  1.1  christos 
   7185  1.1  christos   /* The combined value is the sum of the HI16 addend, left-shifted by
   7186  1.1  christos      sixteen bits, and the LO16 addend, sign extended.  (Usually, the
   7187  1.1  christos      code does a `lui' of the HI16 value, and then an `addiu' of the
   7188  1.1  christos      LO16 value.)
   7189  1.1  christos 
   7190  1.1  christos      Scan ahead to find a matching LO16 relocation.
   7191  1.1  christos 
   7192  1.1  christos      According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
   7193  1.1  christos      be immediately following.  However, for the IRIX6 ABI, the next
   7194  1.1  christos      relocation may be a composed relocation consisting of several
   7195  1.1  christos      relocations for the same address.  In that case, the R_MIPS_LO16
   7196  1.1  christos      relocation may occur as one of these.  We permit a similar
   7197  1.1  christos      extension in general, as that is useful for GCC.
   7198  1.1  christos 
   7199  1.1  christos      In some cases GCC dead code elimination removes the LO16 but keeps
   7200  1.1  christos      the corresponding HI16.  This is strictly speaking a violation of
   7201  1.1  christos      the ABI but not immediately harmful.  */
   7202  1.1  christos   lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
   7203  1.1  christos   if (lo16_relocation == NULL)
   7204  1.1  christos     return FALSE;
   7205  1.1  christos 
   7206  1.1  christos   /* Obtain the addend kept there.  */
   7207  1.1  christos   lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
   7208  1.1  christos   l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
   7209  1.1  christos 
   7210  1.1  christos   l <<= lo16_howto->rightshift;
   7211  1.1  christos   l = _bfd_mips_elf_sign_extend (l, 16);
   7212  1.1  christos 
   7213  1.1  christos   *addend <<= 16;
   7214  1.1  christos   *addend += l;
   7215  1.1  christos   return TRUE;
   7216  1.1  christos }
   7217  1.1  christos 
   7218  1.1  christos /* Try to read the contents of section SEC in bfd ABFD.  Return true and
   7219  1.1  christos    store the contents in *CONTENTS on success.  Assume that *CONTENTS
   7220  1.1  christos    already holds the contents if it is nonull on entry.  */
   7221  1.1  christos 
   7222  1.1  christos static bfd_boolean
   7223  1.1  christos mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
   7224  1.1  christos {
   7225  1.1  christos   if (*contents)
   7226  1.1  christos     return TRUE;
   7227  1.1  christos 
   7228  1.1  christos   /* Get cached copy if it exists.  */
   7229  1.1  christos   if (elf_section_data (sec)->this_hdr.contents != NULL)
   7230  1.1  christos     {
   7231  1.1  christos       *contents = elf_section_data (sec)->this_hdr.contents;
   7232  1.1  christos       return TRUE;
   7233  1.1  christos     }
   7234  1.1  christos 
   7235  1.1  christos   return bfd_malloc_and_get_section (abfd, sec, contents);
   7236  1.1  christos }
   7237  1.1  christos 
   7238  1.1  christos /* Look through the relocs for a section during the first phase, and
   7239  1.1  christos    allocate space in the global offset table.  */
   7240  1.1  christos 
   7241  1.1  christos bfd_boolean
   7242  1.1  christos _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   7243  1.1  christos 			    asection *sec, const Elf_Internal_Rela *relocs)
   7244  1.1  christos {
   7245  1.1  christos   const char *name;
   7246  1.1  christos   bfd *dynobj;
   7247  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   7248  1.1  christos   struct elf_link_hash_entry **sym_hashes;
   7249  1.1  christos   size_t extsymoff;
   7250  1.1  christos   const Elf_Internal_Rela *rel;
   7251  1.1  christos   const Elf_Internal_Rela *rel_end;
   7252  1.1  christos   asection *sreloc;
   7253  1.1  christos   const struct elf_backend_data *bed;
   7254  1.1  christos   struct mips_elf_link_hash_table *htab;
   7255  1.1  christos   bfd_byte *contents;
   7256  1.1  christos   bfd_vma addend;
   7257  1.1  christos   reloc_howto_type *howto;
   7258  1.1  christos 
   7259  1.1  christos   if (info->relocatable)
   7260  1.1  christos     return TRUE;
   7261  1.1  christos 
   7262  1.1  christos   htab = mips_elf_hash_table (info);
   7263  1.1  christos   BFD_ASSERT (htab != NULL);
   7264  1.1  christos 
   7265  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   7266  1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   7267  1.1  christos   sym_hashes = elf_sym_hashes (abfd);
   7268  1.1  christos   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
   7269  1.1  christos 
   7270  1.1  christos   bed = get_elf_backend_data (abfd);
   7271  1.1  christos   rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
   7272  1.1  christos 
   7273  1.1  christos   /* Check for the mips16 stub sections.  */
   7274  1.1  christos 
   7275  1.1  christos   name = bfd_get_section_name (abfd, sec);
   7276  1.1  christos   if (FN_STUB_P (name))
   7277  1.1  christos     {
   7278  1.1  christos       unsigned long r_symndx;
   7279  1.1  christos 
   7280  1.1  christos       /* Look at the relocation information to figure out which symbol
   7281  1.1  christos          this is for.  */
   7282  1.1  christos 
   7283  1.1  christos       r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
   7284  1.1  christos       if (r_symndx == 0)
   7285  1.1  christos 	{
   7286  1.1  christos 	  (*_bfd_error_handler)
   7287  1.1  christos 	    (_("%B: Warning: cannot determine the target function for"
   7288  1.1  christos 	       " stub section `%s'"),
   7289  1.1  christos 	     abfd, name);
   7290  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   7291  1.1  christos 	  return FALSE;
   7292  1.1  christos 	}
   7293  1.1  christos 
   7294  1.1  christos       if (r_symndx < extsymoff
   7295  1.1  christos 	  || sym_hashes[r_symndx - extsymoff] == NULL)
   7296  1.1  christos 	{
   7297  1.1  christos 	  asection *o;
   7298  1.1  christos 
   7299  1.1  christos 	  /* This stub is for a local symbol.  This stub will only be
   7300  1.1  christos              needed if there is some relocation in this BFD, other
   7301  1.1  christos              than a 16 bit function call, which refers to this symbol.  */
   7302  1.1  christos 	  for (o = abfd->sections; o != NULL; o = o->next)
   7303  1.1  christos 	    {
   7304  1.1  christos 	      Elf_Internal_Rela *sec_relocs;
   7305  1.1  christos 	      const Elf_Internal_Rela *r, *rend;
   7306  1.1  christos 
   7307  1.1  christos 	      /* We can ignore stub sections when looking for relocs.  */
   7308  1.1  christos 	      if ((o->flags & SEC_RELOC) == 0
   7309  1.1  christos 		  || o->reloc_count == 0
   7310  1.1  christos 		  || section_allows_mips16_refs_p (o))
   7311  1.1  christos 		continue;
   7312  1.1  christos 
   7313  1.1  christos 	      sec_relocs
   7314  1.1  christos 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   7315  1.1  christos 					     info->keep_memory);
   7316  1.1  christos 	      if (sec_relocs == NULL)
   7317  1.1  christos 		return FALSE;
   7318  1.1  christos 
   7319  1.1  christos 	      rend = sec_relocs + o->reloc_count;
   7320  1.1  christos 	      for (r = sec_relocs; r < rend; r++)
   7321  1.1  christos 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
   7322  1.1  christos 		    && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
   7323  1.1  christos 		  break;
   7324  1.1  christos 
   7325  1.1  christos 	      if (elf_section_data (o)->relocs != sec_relocs)
   7326  1.1  christos 		free (sec_relocs);
   7327  1.1  christos 
   7328  1.1  christos 	      if (r < rend)
   7329  1.1  christos 		break;
   7330  1.1  christos 	    }
   7331  1.1  christos 
   7332  1.1  christos 	  if (o == NULL)
   7333  1.1  christos 	    {
   7334  1.1  christos 	      /* There is no non-call reloc for this stub, so we do
   7335  1.1  christos                  not need it.  Since this function is called before
   7336  1.1  christos                  the linker maps input sections to output sections, we
   7337  1.1  christos                  can easily discard it by setting the SEC_EXCLUDE
   7338  1.1  christos                  flag.  */
   7339  1.1  christos 	      sec->flags |= SEC_EXCLUDE;
   7340  1.1  christos 	      return TRUE;
   7341  1.1  christos 	    }
   7342  1.1  christos 
   7343  1.1  christos 	  /* Record this stub in an array of local symbol stubs for
   7344  1.1  christos              this BFD.  */
   7345  1.1  christos 	  if (elf_tdata (abfd)->local_stubs == NULL)
   7346  1.1  christos 	    {
   7347  1.1  christos 	      unsigned long symcount;
   7348  1.1  christos 	      asection **n;
   7349  1.1  christos 	      bfd_size_type amt;
   7350  1.1  christos 
   7351  1.1  christos 	      if (elf_bad_symtab (abfd))
   7352  1.1  christos 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
   7353  1.1  christos 	      else
   7354  1.1  christos 		symcount = symtab_hdr->sh_info;
   7355  1.1  christos 	      amt = symcount * sizeof (asection *);
   7356  1.1  christos 	      n = bfd_zalloc (abfd, amt);
   7357  1.1  christos 	      if (n == NULL)
   7358  1.1  christos 		return FALSE;
   7359  1.1  christos 	      elf_tdata (abfd)->local_stubs = n;
   7360  1.1  christos 	    }
   7361  1.1  christos 
   7362  1.1  christos 	  sec->flags |= SEC_KEEP;
   7363  1.1  christos 	  elf_tdata (abfd)->local_stubs[r_symndx] = sec;
   7364  1.1  christos 
   7365  1.1  christos 	  /* We don't need to set mips16_stubs_seen in this case.
   7366  1.1  christos              That flag is used to see whether we need to look through
   7367  1.1  christos              the global symbol table for stubs.  We don't need to set
   7368  1.1  christos              it here, because we just have a local stub.  */
   7369  1.1  christos 	}
   7370  1.1  christos       else
   7371  1.1  christos 	{
   7372  1.1  christos 	  struct mips_elf_link_hash_entry *h;
   7373  1.1  christos 
   7374  1.1  christos 	  h = ((struct mips_elf_link_hash_entry *)
   7375  1.1  christos 	       sym_hashes[r_symndx - extsymoff]);
   7376  1.1  christos 
   7377  1.1  christos 	  while (h->root.root.type == bfd_link_hash_indirect
   7378  1.1  christos 		 || h->root.root.type == bfd_link_hash_warning)
   7379  1.1  christos 	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   7380  1.1  christos 
   7381  1.1  christos 	  /* H is the symbol this stub is for.  */
   7382  1.1  christos 
   7383  1.1  christos 	  /* If we already have an appropriate stub for this function, we
   7384  1.1  christos 	     don't need another one, so we can discard this one.  Since
   7385  1.1  christos 	     this function is called before the linker maps input sections
   7386  1.1  christos 	     to output sections, we can easily discard it by setting the
   7387  1.1  christos 	     SEC_EXCLUDE flag.  */
   7388  1.1  christos 	  if (h->fn_stub != NULL)
   7389  1.1  christos 	    {
   7390  1.1  christos 	      sec->flags |= SEC_EXCLUDE;
   7391  1.1  christos 	      return TRUE;
   7392  1.1  christos 	    }
   7393  1.1  christos 
   7394  1.1  christos 	  sec->flags |= SEC_KEEP;
   7395  1.1  christos 	  h->fn_stub = sec;
   7396  1.1  christos 	  mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
   7397  1.1  christos 	}
   7398  1.1  christos     }
   7399  1.1  christos   else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
   7400  1.1  christos     {
   7401  1.1  christos       unsigned long r_symndx;
   7402  1.1  christos       struct mips_elf_link_hash_entry *h;
   7403  1.1  christos       asection **loc;
   7404  1.1  christos 
   7405  1.1  christos       /* Look at the relocation information to figure out which symbol
   7406  1.1  christos          this is for.  */
   7407  1.1  christos 
   7408  1.1  christos       r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
   7409  1.1  christos       if (r_symndx == 0)
   7410  1.1  christos 	{
   7411  1.1  christos 	  (*_bfd_error_handler)
   7412  1.1  christos 	    (_("%B: Warning: cannot determine the target function for"
   7413  1.1  christos 	       " stub section `%s'"),
   7414  1.1  christos 	     abfd, name);
   7415  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   7416  1.1  christos 	  return FALSE;
   7417  1.1  christos 	}
   7418  1.1  christos 
   7419  1.1  christos       if (r_symndx < extsymoff
   7420  1.1  christos 	  || sym_hashes[r_symndx - extsymoff] == NULL)
   7421  1.1  christos 	{
   7422  1.1  christos 	  asection *o;
   7423  1.1  christos 
   7424  1.1  christos 	  /* This stub is for a local symbol.  This stub will only be
   7425  1.1  christos              needed if there is some relocation (R_MIPS16_26) in this BFD
   7426  1.1  christos              that refers to this symbol.  */
   7427  1.1  christos 	  for (o = abfd->sections; o != NULL; o = o->next)
   7428  1.1  christos 	    {
   7429  1.1  christos 	      Elf_Internal_Rela *sec_relocs;
   7430  1.1  christos 	      const Elf_Internal_Rela *r, *rend;
   7431  1.1  christos 
   7432  1.1  christos 	      /* We can ignore stub sections when looking for relocs.  */
   7433  1.1  christos 	      if ((o->flags & SEC_RELOC) == 0
   7434  1.1  christos 		  || o->reloc_count == 0
   7435  1.1  christos 		  || section_allows_mips16_refs_p (o))
   7436  1.1  christos 		continue;
   7437  1.1  christos 
   7438  1.1  christos 	      sec_relocs
   7439  1.1  christos 		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   7440  1.1  christos 					     info->keep_memory);
   7441  1.1  christos 	      if (sec_relocs == NULL)
   7442  1.1  christos 		return FALSE;
   7443  1.1  christos 
   7444  1.1  christos 	      rend = sec_relocs + o->reloc_count;
   7445  1.1  christos 	      for (r = sec_relocs; r < rend; r++)
   7446  1.1  christos 		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
   7447  1.1  christos 		    && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
   7448  1.1  christos 		    break;
   7449  1.1  christos 
   7450  1.1  christos 	      if (elf_section_data (o)->relocs != sec_relocs)
   7451  1.1  christos 		free (sec_relocs);
   7452  1.1  christos 
   7453  1.1  christos 	      if (r < rend)
   7454  1.1  christos 		break;
   7455  1.1  christos 	    }
   7456  1.1  christos 
   7457  1.1  christos 	  if (o == NULL)
   7458  1.1  christos 	    {
   7459  1.1  christos 	      /* There is no non-call reloc for this stub, so we do
   7460  1.1  christos                  not need it.  Since this function is called before
   7461  1.1  christos                  the linker maps input sections to output sections, we
   7462  1.1  christos                  can easily discard it by setting the SEC_EXCLUDE
   7463  1.1  christos                  flag.  */
   7464  1.1  christos 	      sec->flags |= SEC_EXCLUDE;
   7465  1.1  christos 	      return TRUE;
   7466  1.1  christos 	    }
   7467  1.1  christos 
   7468  1.1  christos 	  /* Record this stub in an array of local symbol call_stubs for
   7469  1.1  christos              this BFD.  */
   7470  1.1  christos 	  if (elf_tdata (abfd)->local_call_stubs == NULL)
   7471  1.1  christos 	    {
   7472  1.1  christos 	      unsigned long symcount;
   7473  1.1  christos 	      asection **n;
   7474  1.1  christos 	      bfd_size_type amt;
   7475  1.1  christos 
   7476  1.1  christos 	      if (elf_bad_symtab (abfd))
   7477  1.1  christos 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
   7478  1.1  christos 	      else
   7479  1.1  christos 		symcount = symtab_hdr->sh_info;
   7480  1.1  christos 	      amt = symcount * sizeof (asection *);
   7481  1.1  christos 	      n = bfd_zalloc (abfd, amt);
   7482  1.1  christos 	      if (n == NULL)
   7483  1.1  christos 		return FALSE;
   7484  1.1  christos 	      elf_tdata (abfd)->local_call_stubs = n;
   7485  1.1  christos 	    }
   7486  1.1  christos 
   7487  1.1  christos 	  sec->flags |= SEC_KEEP;
   7488  1.1  christos 	  elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
   7489  1.1  christos 
   7490  1.1  christos 	  /* We don't need to set mips16_stubs_seen in this case.
   7491  1.1  christos              That flag is used to see whether we need to look through
   7492  1.1  christos              the global symbol table for stubs.  We don't need to set
   7493  1.1  christos              it here, because we just have a local stub.  */
   7494  1.1  christos 	}
   7495  1.1  christos       else
   7496  1.1  christos 	{
   7497  1.1  christos 	  h = ((struct mips_elf_link_hash_entry *)
   7498  1.1  christos 	       sym_hashes[r_symndx - extsymoff]);
   7499  1.1  christos 
   7500  1.1  christos 	  /* H is the symbol this stub is for.  */
   7501  1.1  christos 
   7502  1.1  christos 	  if (CALL_FP_STUB_P (name))
   7503  1.1  christos 	    loc = &h->call_fp_stub;
   7504  1.1  christos 	  else
   7505  1.1  christos 	    loc = &h->call_stub;
   7506  1.1  christos 
   7507  1.1  christos 	  /* If we already have an appropriate stub for this function, we
   7508  1.1  christos 	     don't need another one, so we can discard this one.  Since
   7509  1.1  christos 	     this function is called before the linker maps input sections
   7510  1.1  christos 	     to output sections, we can easily discard it by setting the
   7511  1.1  christos 	     SEC_EXCLUDE flag.  */
   7512  1.1  christos 	  if (*loc != NULL)
   7513  1.1  christos 	    {
   7514  1.1  christos 	      sec->flags |= SEC_EXCLUDE;
   7515  1.1  christos 	      return TRUE;
   7516  1.1  christos 	    }
   7517  1.1  christos 
   7518  1.1  christos 	  sec->flags |= SEC_KEEP;
   7519  1.1  christos 	  *loc = sec;
   7520  1.1  christos 	  mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
   7521  1.1  christos 	}
   7522  1.1  christos     }
   7523  1.1  christos 
   7524  1.1  christos   sreloc = NULL;
   7525  1.1  christos   contents = NULL;
   7526  1.1  christos   for (rel = relocs; rel < rel_end; ++rel)
   7527  1.1  christos     {
   7528  1.1  christos       unsigned long r_symndx;
   7529  1.1  christos       unsigned int r_type;
   7530  1.1  christos       struct elf_link_hash_entry *h;
   7531  1.1  christos       bfd_boolean can_make_dynamic_p;
   7532  1.1  christos 
   7533  1.1  christos       r_symndx = ELF_R_SYM (abfd, rel->r_info);
   7534  1.1  christos       r_type = ELF_R_TYPE (abfd, rel->r_info);
   7535  1.1  christos 
   7536  1.1  christos       if (r_symndx < extsymoff)
   7537  1.1  christos 	h = NULL;
   7538  1.1  christos       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
   7539  1.1  christos 	{
   7540  1.1  christos 	  (*_bfd_error_handler)
   7541  1.1  christos 	    (_("%B: Malformed reloc detected for section %s"),
   7542  1.1  christos 	     abfd, name);
   7543  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   7544  1.1  christos 	  return FALSE;
   7545  1.1  christos 	}
   7546  1.1  christos       else
   7547  1.1  christos 	{
   7548  1.1  christos 	  h = sym_hashes[r_symndx - extsymoff];
   7549  1.1  christos 	  while (h != NULL
   7550  1.1  christos 		 && (h->root.type == bfd_link_hash_indirect
   7551  1.1  christos 		     || h->root.type == bfd_link_hash_warning))
   7552  1.1  christos 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   7553  1.1  christos 	}
   7554  1.1  christos 
   7555  1.1  christos       /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
   7556  1.1  christos 	 relocation into a dynamic one.  */
   7557  1.1  christos       can_make_dynamic_p = FALSE;
   7558  1.1  christos       switch (r_type)
   7559  1.1  christos 	{
   7560  1.1  christos 	case R_MIPS16_GOT16:
   7561  1.1  christos 	case R_MIPS16_CALL16:
   7562  1.1  christos 	case R_MIPS_GOT16:
   7563  1.1  christos 	case R_MIPS_CALL16:
   7564  1.1  christos 	case R_MIPS_CALL_HI16:
   7565  1.1  christos 	case R_MIPS_CALL_LO16:
   7566  1.1  christos 	case R_MIPS_GOT_HI16:
   7567  1.1  christos 	case R_MIPS_GOT_LO16:
   7568  1.1  christos 	case R_MIPS_GOT_PAGE:
   7569  1.1  christos 	case R_MIPS_GOT_OFST:
   7570  1.1  christos 	case R_MIPS_GOT_DISP:
   7571  1.1  christos 	case R_MIPS_TLS_GOTTPREL:
   7572  1.1  christos 	case R_MIPS_TLS_GD:
   7573  1.1  christos 	case R_MIPS_TLS_LDM:
   7574  1.1  christos 	  if (dynobj == NULL)
   7575  1.1  christos 	    elf_hash_table (info)->dynobj = dynobj = abfd;
   7576  1.1  christos 	  if (!mips_elf_create_got_section (dynobj, info))
   7577  1.1  christos 	    return FALSE;
   7578  1.1  christos 	  if (htab->is_vxworks && !info->shared)
   7579  1.1  christos 	    {
   7580  1.1  christos 	      (*_bfd_error_handler)
   7581  1.1  christos 		(_("%B: GOT reloc at 0x%lx not expected in executables"),
   7582  1.1  christos 		 abfd, (unsigned long) rel->r_offset);
   7583  1.1  christos 	      bfd_set_error (bfd_error_bad_value);
   7584  1.1  christos 	      return FALSE;
   7585  1.1  christos 	    }
   7586  1.1  christos 	  break;
   7587  1.1  christos 
   7588  1.1  christos 	  /* This is just a hint; it can safely be ignored.  Don't set
   7589  1.1  christos 	     has_static_relocs for the corresponding symbol.  */
   7590  1.1  christos 	case R_MIPS_JALR:
   7591  1.1  christos 	  break;
   7592  1.1  christos 
   7593  1.1  christos 	case R_MIPS_32:
   7594  1.1  christos 	case R_MIPS_REL32:
   7595  1.1  christos 	case R_MIPS_64:
   7596  1.1  christos 	  /* In VxWorks executables, references to external symbols
   7597  1.1  christos 	     must be handled using copy relocs or PLT entries; it is not
   7598  1.1  christos 	     possible to convert this relocation into a dynamic one.
   7599  1.1  christos 
   7600  1.1  christos 	     For executables that use PLTs and copy-relocs, we have a
   7601  1.1  christos 	     choice between converting the relocation into a dynamic
   7602  1.1  christos 	     one or using copy relocations or PLT entries.  It is
   7603  1.1  christos 	     usually better to do the former, unless the relocation is
   7604  1.1  christos 	     against a read-only section.  */
   7605  1.1  christos 	  if ((info->shared
   7606  1.1  christos 	       || (h != NULL
   7607  1.1  christos 		   && !htab->is_vxworks
   7608  1.1  christos 		   && strcmp (h->root.root.string, "__gnu_local_gp") != 0
   7609  1.1  christos 		   && !(!info->nocopyreloc
   7610  1.1  christos 			&& !PIC_OBJECT_P (abfd)
   7611  1.1  christos 			&& MIPS_ELF_READONLY_SECTION (sec))))
   7612  1.1  christos 	      && (sec->flags & SEC_ALLOC) != 0)
   7613  1.1  christos 	    {
   7614  1.1  christos 	      can_make_dynamic_p = TRUE;
   7615  1.1  christos 	      if (dynobj == NULL)
   7616  1.1  christos 		elf_hash_table (info)->dynobj = dynobj = abfd;
   7617  1.1  christos 	      break;
   7618  1.1  christos 	    }
   7619  1.1  christos 	  /* For sections that are not SEC_ALLOC a copy reloc would be
   7620  1.1  christos 	     output if possible (implying questionable semantics for
   7621  1.1  christos 	     read-only data objects) or otherwise the final link would
   7622  1.1  christos 	     fail as ld.so will not process them and could not therefore
   7623  1.1  christos 	     handle any outstanding dynamic relocations.
   7624  1.1  christos 
   7625  1.1  christos 	     For such sections that are also SEC_DEBUGGING, we can avoid
   7626  1.1  christos 	     these problems by simply ignoring any relocs as these
   7627  1.1  christos 	     sections have a predefined use and we know it is safe to do
   7628  1.1  christos 	     so.
   7629  1.1  christos 
   7630  1.1  christos 	     This is needed in cases such as a global symbol definition
   7631  1.1  christos 	     in a shared library causing a common symbol from an object
   7632  1.1  christos 	     file to be converted to an undefined reference.  If that
   7633  1.1  christos 	     happens, then all the relocations against this symbol from
   7634  1.1  christos 	     SEC_DEBUGGING sections in the object file will resolve to
   7635  1.1  christos 	     nil.  */
   7636  1.1  christos 	  if ((sec->flags & SEC_DEBUGGING) != 0)
   7637  1.1  christos 	    break;
   7638  1.1  christos 	  /* Fall through.  */
   7639  1.1  christos 
   7640  1.1  christos 	default:
   7641  1.1  christos 	  /* Most static relocations require pointer equality, except
   7642  1.1  christos 	     for branches.  */
   7643  1.1  christos 	  if (h)
   7644  1.1  christos 	    h->pointer_equality_needed = TRUE;
   7645  1.1  christos 	  /* Fall through.  */
   7646  1.1  christos 
   7647  1.1  christos 	case R_MIPS_26:
   7648  1.1  christos 	case R_MIPS_PC16:
   7649  1.1  christos 	case R_MIPS16_26:
   7650  1.1  christos 	  if (h)
   7651  1.1  christos 	    ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
   7652  1.1  christos 	  break;
   7653  1.1  christos 	}
   7654  1.1  christos 
   7655  1.1  christos       if (h)
   7656  1.1  christos 	{
   7657  1.1  christos 	  /* Relocations against the special VxWorks __GOTT_BASE__ and
   7658  1.1  christos 	     __GOTT_INDEX__ symbols must be left to the loader.  Allocate
   7659  1.1  christos 	     room for them in .rela.dyn.  */
   7660  1.1  christos 	  if (is_gott_symbol (info, h))
   7661  1.1  christos 	    {
   7662  1.1  christos 	      if (sreloc == NULL)
   7663  1.1  christos 		{
   7664  1.1  christos 		  sreloc = mips_elf_rel_dyn_section (info, TRUE);
   7665  1.1  christos 		  if (sreloc == NULL)
   7666  1.1  christos 		    return FALSE;
   7667  1.1  christos 		}
   7668  1.1  christos 	      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   7669  1.1  christos 	      if (MIPS_ELF_READONLY_SECTION (sec))
   7670  1.1  christos 		/* We tell the dynamic linker that there are
   7671  1.1  christos 		   relocations against the text segment.  */
   7672  1.1  christos 		info->flags |= DF_TEXTREL;
   7673  1.1  christos 	    }
   7674  1.1  christos 	}
   7675  1.1  christos       else if (r_type == R_MIPS_CALL_LO16
   7676  1.1  christos 	       || r_type == R_MIPS_GOT_LO16
   7677  1.1  christos 	       || r_type == R_MIPS_GOT_DISP
   7678  1.1  christos 	       || (got16_reloc_p (r_type) && htab->is_vxworks))
   7679  1.1  christos 	{
   7680  1.1  christos 	  /* We may need a local GOT entry for this relocation.  We
   7681  1.1  christos 	     don't count R_MIPS_GOT_PAGE because we can estimate the
   7682  1.1  christos 	     maximum number of pages needed by looking at the size of
   7683  1.1  christos 	     the segment.  Similar comments apply to R_MIPS*_GOT16 and
   7684  1.1  christos 	     R_MIPS*_CALL16, except on VxWorks, where GOT relocations
   7685  1.1  christos 	     always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
   7686  1.1  christos 	     R_MIPS_CALL_HI16 because these are always followed by an
   7687  1.1  christos 	     R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
   7688  1.1  christos 	  if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
   7689  1.1  christos 						 rel->r_addend, info, 0))
   7690  1.1  christos 	    return FALSE;
   7691  1.1  christos 	}
   7692  1.1  christos 
   7693  1.1  christos       if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
   7694  1.1  christos 	((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
   7695  1.1  christos 
   7696  1.1  christos       switch (r_type)
   7697  1.1  christos 	{
   7698  1.1  christos 	case R_MIPS_CALL16:
   7699  1.1  christos 	case R_MIPS16_CALL16:
   7700  1.1  christos 	  if (h == NULL)
   7701  1.1  christos 	    {
   7702  1.1  christos 	      (*_bfd_error_handler)
   7703  1.1  christos 		(_("%B: CALL16 reloc at 0x%lx not against global symbol"),
   7704  1.1  christos 		 abfd, (unsigned long) rel->r_offset);
   7705  1.1  christos 	      bfd_set_error (bfd_error_bad_value);
   7706  1.1  christos 	      return FALSE;
   7707  1.1  christos 	    }
   7708  1.1  christos 	  /* Fall through.  */
   7709  1.1  christos 
   7710  1.1  christos 	case R_MIPS_CALL_HI16:
   7711  1.1  christos 	case R_MIPS_CALL_LO16:
   7712  1.1  christos 	  if (h != NULL)
   7713  1.1  christos 	    {
   7714  1.1  christos 	      /* Make sure there is room in the regular GOT to hold the
   7715  1.1  christos 		 function's address.  We may eliminate it in favour of
   7716  1.1  christos 		 a .got.plt entry later; see mips_elf_count_got_symbols.  */
   7717  1.1  christos 	      if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE, 0))
   7718  1.1  christos 		return FALSE;
   7719  1.1  christos 
   7720  1.1  christos 	      /* We need a stub, not a plt entry for the undefined
   7721  1.1  christos 		 function.  But we record it as if it needs plt.  See
   7722  1.1  christos 		 _bfd_elf_adjust_dynamic_symbol.  */
   7723  1.1  christos 	      h->needs_plt = 1;
   7724  1.1  christos 	      h->type = STT_FUNC;
   7725  1.1  christos 	    }
   7726  1.1  christos 	  break;
   7727  1.1  christos 
   7728  1.1  christos 	case R_MIPS_GOT_PAGE:
   7729  1.1  christos 	  /* If this is a global, overridable symbol, GOT_PAGE will
   7730  1.1  christos 	     decay to GOT_DISP, so we'll need a GOT entry for it.  */
   7731  1.1  christos 	  if (h)
   7732  1.1  christos 	    {
   7733  1.1  christos 	      struct mips_elf_link_hash_entry *hmips =
   7734  1.1  christos 		(struct mips_elf_link_hash_entry *) h;
   7735  1.1  christos 
   7736  1.1  christos 	      /* This symbol is definitely not overridable.  */
   7737  1.1  christos 	      if (hmips->root.def_regular
   7738  1.1  christos 		  && ! (info->shared && ! info->symbolic
   7739  1.1  christos 			&& ! hmips->root.forced_local))
   7740  1.1  christos 		h = NULL;
   7741  1.1  christos 	    }
   7742  1.1  christos 	  /* Fall through.  */
   7743  1.1  christos 
   7744  1.1  christos 	case R_MIPS16_GOT16:
   7745  1.1  christos 	case R_MIPS_GOT16:
   7746  1.1  christos 	case R_MIPS_GOT_HI16:
   7747  1.1  christos 	case R_MIPS_GOT_LO16:
   7748  1.1  christos 	  if (!h || r_type == R_MIPS_GOT_PAGE)
   7749  1.1  christos 	    {
   7750  1.1  christos 	      /* This relocation needs (or may need, if h != NULL) a
   7751  1.1  christos 		 page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
   7752  1.1  christos 		 know for sure until we know whether the symbol is
   7753  1.1  christos 		 preemptible.  */
   7754  1.1  christos 	      if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
   7755  1.1  christos 		{
   7756  1.1  christos 		  if (!mips_elf_get_section_contents (abfd, sec, &contents))
   7757  1.1  christos 		    return FALSE;
   7758  1.1  christos 		  howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
   7759  1.1  christos 		  addend = mips_elf_read_rel_addend (abfd, rel,
   7760  1.1  christos 						     howto, contents);
   7761  1.1  christos 		  if (got16_reloc_p (r_type))
   7762  1.1  christos 		    mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
   7763  1.1  christos 						  contents, &addend);
   7764  1.1  christos 		  else
   7765  1.1  christos 		    addend <<= howto->rightshift;
   7766  1.1  christos 		}
   7767  1.1  christos 	      else
   7768  1.1  christos 		addend = rel->r_addend;
   7769  1.1  christos 	      if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
   7770  1.1  christos 						   addend))
   7771  1.1  christos 		return FALSE;
   7772  1.1  christos 	      break;
   7773  1.1  christos 	    }
   7774  1.1  christos 	  /* Fall through.  */
   7775  1.1  christos 
   7776  1.1  christos 	case R_MIPS_GOT_DISP:
   7777  1.1  christos 	  if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
   7778  1.1  christos 						       FALSE, 0))
   7779  1.1  christos 	    return FALSE;
   7780  1.1  christos 	  break;
   7781  1.1  christos 
   7782  1.1  christos 	case R_MIPS_TLS_GOTTPREL:
   7783  1.1  christos 	  if (info->shared)
   7784  1.1  christos 	    info->flags |= DF_STATIC_TLS;
   7785  1.1  christos 	  /* Fall through */
   7786  1.1  christos 
   7787  1.1  christos 	case R_MIPS_TLS_LDM:
   7788  1.1  christos 	  if (r_type == R_MIPS_TLS_LDM)
   7789  1.1  christos 	    {
   7790  1.1  christos 	      r_symndx = STN_UNDEF;
   7791  1.1  christos 	      h = NULL;
   7792  1.1  christos 	    }
   7793  1.1  christos 	  /* Fall through */
   7794  1.1  christos 
   7795  1.1  christos 	case R_MIPS_TLS_GD:
   7796  1.1  christos 	  /* This symbol requires a global offset table entry, or two
   7797  1.1  christos 	     for TLS GD relocations.  */
   7798  1.1  christos 	  {
   7799  1.1  christos 	    unsigned char flag = (r_type == R_MIPS_TLS_GD
   7800  1.1  christos 				  ? GOT_TLS_GD
   7801  1.1  christos 				  : r_type == R_MIPS_TLS_LDM
   7802  1.1  christos 				  ? GOT_TLS_LDM
   7803  1.1  christos 				  : GOT_TLS_IE);
   7804  1.1  christos 	    if (h != NULL)
   7805  1.1  christos 	      {
   7806  1.1  christos 		struct mips_elf_link_hash_entry *hmips =
   7807  1.1  christos 		  (struct mips_elf_link_hash_entry *) h;
   7808  1.1  christos 		hmips->tls_type |= flag;
   7809  1.1  christos 
   7810  1.1  christos 		if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
   7811  1.1  christos 							     FALSE, flag))
   7812  1.1  christos 		  return FALSE;
   7813  1.1  christos 	      }
   7814  1.1  christos 	    else
   7815  1.1  christos 	      {
   7816  1.1  christos 		BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != STN_UNDEF);
   7817  1.1  christos 
   7818  1.1  christos 		if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
   7819  1.1  christos 						       rel->r_addend,
   7820  1.1  christos 						       info, flag))
   7821  1.1  christos 		  return FALSE;
   7822  1.1  christos 	      }
   7823  1.1  christos 	  }
   7824  1.1  christos 	  break;
   7825  1.1  christos 
   7826  1.1  christos 	case R_MIPS_32:
   7827  1.1  christos 	case R_MIPS_REL32:
   7828  1.1  christos 	case R_MIPS_64:
   7829  1.1  christos 	  /* In VxWorks executables, references to external symbols
   7830  1.1  christos 	     are handled using copy relocs or PLT stubs, so there's
   7831  1.1  christos 	     no need to add a .rela.dyn entry for this relocation.  */
   7832  1.1  christos 	  if (can_make_dynamic_p)
   7833  1.1  christos 	    {
   7834  1.1  christos 	      if (sreloc == NULL)
   7835  1.1  christos 		{
   7836  1.1  christos 		  sreloc = mips_elf_rel_dyn_section (info, TRUE);
   7837  1.1  christos 		  if (sreloc == NULL)
   7838  1.1  christos 		    return FALSE;
   7839  1.1  christos 		}
   7840  1.1  christos 	      if (info->shared && h == NULL)
   7841  1.1  christos 		{
   7842  1.1  christos 		  /* When creating a shared object, we must copy these
   7843  1.1  christos 		     reloc types into the output file as R_MIPS_REL32
   7844  1.1  christos 		     relocs.  Make room for this reloc in .rel(a).dyn.  */
   7845  1.1  christos 		  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   7846  1.1  christos 		  if (MIPS_ELF_READONLY_SECTION (sec))
   7847  1.1  christos 		    /* We tell the dynamic linker that there are
   7848  1.1  christos 		       relocations against the text segment.  */
   7849  1.1  christos 		    info->flags |= DF_TEXTREL;
   7850  1.1  christos 		}
   7851  1.1  christos 	      else
   7852  1.1  christos 		{
   7853  1.1  christos 		  struct mips_elf_link_hash_entry *hmips;
   7854  1.1  christos 
   7855  1.1  christos 		  /* For a shared object, we must copy this relocation
   7856  1.1  christos 		     unless the symbol turns out to be undefined and
   7857  1.1  christos 		     weak with non-default visibility, in which case
   7858  1.1  christos 		     it will be left as zero.
   7859  1.1  christos 
   7860  1.1  christos 		     We could elide R_MIPS_REL32 for locally binding symbols
   7861  1.1  christos 		     in shared libraries, but do not yet do so.
   7862  1.1  christos 
   7863  1.1  christos 		     For an executable, we only need to copy this
   7864  1.1  christos 		     reloc if the symbol is defined in a dynamic
   7865  1.1  christos 		     object.  */
   7866  1.1  christos 		  hmips = (struct mips_elf_link_hash_entry *) h;
   7867  1.1  christos 		  ++hmips->possibly_dynamic_relocs;
   7868  1.1  christos 		  if (MIPS_ELF_READONLY_SECTION (sec))
   7869  1.1  christos 		    /* We need it to tell the dynamic linker if there
   7870  1.1  christos 		       are relocations against the text segment.  */
   7871  1.1  christos 		    hmips->readonly_reloc = TRUE;
   7872  1.1  christos 		}
   7873  1.1  christos 	    }
   7874  1.1  christos 
   7875  1.1  christos 	  if (SGI_COMPAT (abfd))
   7876  1.1  christos 	    mips_elf_hash_table (info)->compact_rel_size +=
   7877  1.1  christos 	      sizeof (Elf32_External_crinfo);
   7878  1.1  christos 	  break;
   7879  1.1  christos 
   7880  1.1  christos 	case R_MIPS_26:
   7881  1.1  christos 	case R_MIPS_GPREL16:
   7882  1.1  christos 	case R_MIPS_LITERAL:
   7883  1.1  christos 	case R_MIPS_GPREL32:
   7884  1.1  christos 	  if (SGI_COMPAT (abfd))
   7885  1.1  christos 	    mips_elf_hash_table (info)->compact_rel_size +=
   7886  1.1  christos 	      sizeof (Elf32_External_crinfo);
   7887  1.1  christos 	  break;
   7888  1.1  christos 
   7889  1.1  christos 	  /* This relocation describes the C++ object vtable hierarchy.
   7890  1.1  christos 	     Reconstruct it for later use during GC.  */
   7891  1.1  christos 	case R_MIPS_GNU_VTINHERIT:
   7892  1.1  christos 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   7893  1.1  christos 	    return FALSE;
   7894  1.1  christos 	  break;
   7895  1.1  christos 
   7896  1.1  christos 	  /* This relocation describes which C++ vtable entries are actually
   7897  1.1  christos 	     used.  Record for later use during GC.  */
   7898  1.1  christos 	case R_MIPS_GNU_VTENTRY:
   7899  1.1  christos 	  BFD_ASSERT (h != NULL);
   7900  1.1  christos 	  if (h != NULL
   7901  1.1  christos 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
   7902  1.1  christos 	    return FALSE;
   7903  1.1  christos 	  break;
   7904  1.1  christos 
   7905  1.1  christos 	default:
   7906  1.1  christos 	  break;
   7907  1.1  christos 	}
   7908  1.1  christos 
   7909  1.1  christos       /* We must not create a stub for a symbol that has relocations
   7910  1.1  christos 	 related to taking the function's address.  This doesn't apply to
   7911  1.1  christos 	 VxWorks, where CALL relocs refer to a .got.plt entry instead of
   7912  1.1  christos 	 a normal .got entry.  */
   7913  1.1  christos       if (!htab->is_vxworks && h != NULL)
   7914  1.1  christos 	switch (r_type)
   7915  1.1  christos 	  {
   7916  1.1  christos 	  default:
   7917  1.1  christos 	    ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
   7918  1.1  christos 	    break;
   7919  1.1  christos 	  case R_MIPS16_CALL16:
   7920  1.1  christos 	  case R_MIPS_CALL16:
   7921  1.1  christos 	  case R_MIPS_CALL_HI16:
   7922  1.1  christos 	  case R_MIPS_CALL_LO16:
   7923  1.1  christos 	  case R_MIPS_JALR:
   7924  1.1  christos 	    break;
   7925  1.1  christos 	  }
   7926  1.1  christos 
   7927  1.1  christos       /* See if this reloc would need to refer to a MIPS16 hard-float stub,
   7928  1.1  christos 	 if there is one.  We only need to handle global symbols here;
   7929  1.1  christos 	 we decide whether to keep or delete stubs for local symbols
   7930  1.1  christos 	 when processing the stub's relocations.  */
   7931  1.1  christos       if (h != NULL
   7932  1.1  christos 	  && !mips16_call_reloc_p (r_type)
   7933  1.1  christos 	  && !section_allows_mips16_refs_p (sec))
   7934  1.1  christos 	{
   7935  1.1  christos 	  struct mips_elf_link_hash_entry *mh;
   7936  1.1  christos 
   7937  1.1  christos 	  mh = (struct mips_elf_link_hash_entry *) h;
   7938  1.1  christos 	  mh->need_fn_stub = TRUE;
   7939  1.1  christos 	}
   7940  1.1  christos 
   7941  1.1  christos       /* Refuse some position-dependent relocations when creating a
   7942  1.1  christos 	 shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
   7943  1.1  christos 	 not PIC, but we can create dynamic relocations and the result
   7944  1.1  christos 	 will be fine.  Also do not refuse R_MIPS_LO16, which can be
   7945  1.1  christos 	 combined with R_MIPS_GOT16.  */
   7946  1.1  christos       if (info->shared)
   7947  1.1  christos 	{
   7948  1.1  christos 	  switch (r_type)
   7949  1.1  christos 	    {
   7950  1.1  christos 	    case R_MIPS16_HI16:
   7951  1.1  christos 	    case R_MIPS_HI16:
   7952  1.1  christos 	    case R_MIPS_HIGHER:
   7953  1.1  christos 	    case R_MIPS_HIGHEST:
   7954  1.1  christos 	      /* Don't refuse a high part relocation if it's against
   7955  1.1  christos 		 no symbol (e.g. part of a compound relocation).  */
   7956  1.1  christos 	      if (r_symndx == STN_UNDEF)
   7957  1.1  christos 		break;
   7958  1.1  christos 
   7959  1.1  christos 	      /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
   7960  1.1  christos 		 and has a special meaning.  */
   7961  1.1  christos 	      if (!NEWABI_P (abfd) && h != NULL
   7962  1.1  christos 		  && strcmp (h->root.root.string, "_gp_disp") == 0)
   7963  1.1  christos 		break;
   7964  1.1  christos 
   7965  1.1  christos 	      /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
   7966  1.1  christos 	      if (is_gott_symbol (info, h))
   7967  1.1  christos 		break;
   7968  1.1  christos 
   7969  1.1  christos 	      /* FALLTHROUGH */
   7970  1.1  christos 
   7971  1.1  christos 	    case R_MIPS16_26:
   7972  1.1  christos 	    case R_MIPS_26:
   7973  1.1  christos 	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
   7974  1.1  christos 	      (*_bfd_error_handler)
   7975  1.1  christos 		(_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
   7976  1.1  christos 		 abfd, howto->name,
   7977  1.1  christos 		 (h) ? h->root.root.string : "a local symbol");
   7978  1.1  christos 	      bfd_set_error (bfd_error_bad_value);
   7979  1.1  christos 	      return FALSE;
   7980  1.1  christos 	    default:
   7981  1.1  christos 	      break;
   7982  1.1  christos 	    }
   7983  1.1  christos 	}
   7984  1.1  christos     }
   7985  1.1  christos 
   7986  1.1  christos   return TRUE;
   7987  1.1  christos }
   7988  1.1  christos 
   7989  1.1  christos bfd_boolean
   7991  1.1  christos _bfd_mips_relax_section (bfd *abfd, asection *sec,
   7992  1.1  christos 			 struct bfd_link_info *link_info,
   7993  1.1  christos 			 bfd_boolean *again)
   7994  1.1  christos {
   7995  1.1  christos   Elf_Internal_Rela *internal_relocs;
   7996  1.1  christos   Elf_Internal_Rela *irel, *irelend;
   7997  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   7998  1.1  christos   bfd_byte *contents = NULL;
   7999  1.1  christos   size_t extsymoff;
   8000  1.1  christos   bfd_boolean changed_contents = FALSE;
   8001  1.1  christos   bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
   8002  1.1  christos   Elf_Internal_Sym *isymbuf = NULL;
   8003  1.1  christos 
   8004  1.1  christos   /* We are not currently changing any sizes, so only one pass.  */
   8005  1.1  christos   *again = FALSE;
   8006  1.1  christos 
   8007  1.1  christos   if (link_info->relocatable)
   8008  1.1  christos     return TRUE;
   8009  1.1  christos 
   8010  1.1  christos   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   8011  1.1  christos 					       link_info->keep_memory);
   8012  1.1  christos   if (internal_relocs == NULL)
   8013  1.1  christos     return TRUE;
   8014  1.1  christos 
   8015  1.1  christos   irelend = internal_relocs + sec->reloc_count
   8016  1.1  christos     * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
   8017  1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8018  1.1  christos   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
   8019  1.1  christos 
   8020  1.1  christos   for (irel = internal_relocs; irel < irelend; irel++)
   8021  1.1  christos     {
   8022  1.1  christos       bfd_vma symval;
   8023  1.1  christos       bfd_signed_vma sym_offset;
   8024  1.1  christos       unsigned int r_type;
   8025  1.1  christos       unsigned long r_symndx;
   8026  1.1  christos       asection *sym_sec;
   8027  1.1  christos       unsigned long instruction;
   8028  1.1  christos 
   8029  1.1  christos       /* Turn jalr into bgezal, and jr into beq, if they're marked
   8030  1.1  christos 	 with a JALR relocation, that indicate where they jump to.
   8031  1.1  christos 	 This saves some pipeline bubbles.  */
   8032  1.1  christos       r_type = ELF_R_TYPE (abfd, irel->r_info);
   8033  1.1  christos       if (r_type != R_MIPS_JALR)
   8034  1.1  christos 	continue;
   8035  1.1  christos 
   8036  1.1  christos       r_symndx = ELF_R_SYM (abfd, irel->r_info);
   8037  1.1  christos       /* Compute the address of the jump target.  */
   8038  1.1  christos       if (r_symndx >= extsymoff)
   8039  1.1  christos 	{
   8040  1.1  christos 	  struct mips_elf_link_hash_entry *h
   8041  1.1  christos 	    = ((struct mips_elf_link_hash_entry *)
   8042  1.1  christos 	       elf_sym_hashes (abfd) [r_symndx - extsymoff]);
   8043  1.1  christos 
   8044  1.1  christos 	  while (h->root.root.type == bfd_link_hash_indirect
   8045  1.1  christos 		 || h->root.root.type == bfd_link_hash_warning)
   8046  1.1  christos 	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
   8047  1.1  christos 
   8048  1.1  christos 	  /* If a symbol is undefined, or if it may be overridden,
   8049  1.1  christos 	     skip it.  */
   8050  1.1  christos 	  if (! ((h->root.root.type == bfd_link_hash_defined
   8051  1.1  christos 		  || h->root.root.type == bfd_link_hash_defweak)
   8052  1.1  christos 		 && h->root.root.u.def.section)
   8053  1.1  christos 	      || (link_info->shared && ! link_info->symbolic
   8054  1.1  christos 		  && !h->root.forced_local))
   8055  1.1  christos 	    continue;
   8056  1.1  christos 
   8057  1.1  christos 	  sym_sec = h->root.root.u.def.section;
   8058  1.1  christos 	  if (sym_sec->output_section)
   8059  1.1  christos 	    symval = (h->root.root.u.def.value
   8060  1.1  christos 		      + sym_sec->output_section->vma
   8061  1.1  christos 		      + sym_sec->output_offset);
   8062  1.1  christos 	  else
   8063  1.1  christos 	    symval = h->root.root.u.def.value;
   8064  1.1  christos 	}
   8065  1.1  christos       else
   8066  1.1  christos 	{
   8067  1.1  christos 	  Elf_Internal_Sym *isym;
   8068  1.1  christos 
   8069  1.1  christos 	  /* Read this BFD's symbols if we haven't done so already.  */
   8070  1.1  christos 	  if (isymbuf == NULL && symtab_hdr->sh_info != 0)
   8071  1.1  christos 	    {
   8072  1.1  christos 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   8073  1.1  christos 	      if (isymbuf == NULL)
   8074  1.1  christos 		isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   8075  1.1  christos 						symtab_hdr->sh_info, 0,
   8076  1.1  christos 						NULL, NULL, NULL);
   8077  1.1  christos 	      if (isymbuf == NULL)
   8078  1.1  christos 		goto relax_return;
   8079  1.1  christos 	    }
   8080  1.1  christos 
   8081  1.1  christos 	  isym = isymbuf + r_symndx;
   8082  1.1  christos 	  if (isym->st_shndx == SHN_UNDEF)
   8083  1.1  christos 	    continue;
   8084  1.1  christos 	  else if (isym->st_shndx == SHN_ABS)
   8085  1.1  christos 	    sym_sec = bfd_abs_section_ptr;
   8086  1.1  christos 	  else if (isym->st_shndx == SHN_COMMON)
   8087  1.1  christos 	    sym_sec = bfd_com_section_ptr;
   8088  1.1  christos 	  else
   8089  1.1  christos 	    sym_sec
   8090  1.1  christos 	      = bfd_section_from_elf_index (abfd, isym->st_shndx);
   8091  1.1  christos 	  symval = isym->st_value
   8092  1.1  christos 	    + sym_sec->output_section->vma
   8093  1.1  christos 	    + sym_sec->output_offset;
   8094  1.1  christos 	}
   8095  1.1  christos 
   8096  1.1  christos       /* Compute branch offset, from delay slot of the jump to the
   8097  1.1  christos 	 branch target.  */
   8098  1.1  christos       sym_offset = (symval + irel->r_addend)
   8099  1.1  christos 	- (sec_start + irel->r_offset + 4);
   8100  1.1  christos 
   8101  1.1  christos       /* Branch offset must be properly aligned.  */
   8102  1.1  christos       if ((sym_offset & 3) != 0)
   8103  1.1  christos 	continue;
   8104  1.1  christos 
   8105  1.1  christos       sym_offset >>= 2;
   8106  1.1  christos 
   8107  1.1  christos       /* Check that it's in range.  */
   8108  1.1  christos       if (sym_offset < -0x8000 || sym_offset >= 0x8000)
   8109  1.1  christos 	continue;
   8110  1.1  christos 
   8111  1.1  christos       /* Get the section contents if we haven't done so already.  */
   8112  1.1  christos       if (!mips_elf_get_section_contents (abfd, sec, &contents))
   8113  1.1  christos 	goto relax_return;
   8114  1.1  christos 
   8115  1.1  christos       instruction = bfd_get_32 (abfd, contents + irel->r_offset);
   8116  1.1  christos 
   8117  1.1  christos       /* If it was jalr <reg>, turn it into bgezal $zero, <target>.  */
   8118  1.1  christos       if ((instruction & 0xfc1fffff) == 0x0000f809)
   8119  1.1  christos 	instruction = 0x04110000;
   8120  1.1  christos       /* If it was jr <reg>, turn it into b <target>.  */
   8121  1.1  christos       else if ((instruction & 0xfc1fffff) == 0x00000008)
   8122  1.1  christos 	instruction = 0x10000000;
   8123  1.1  christos       else
   8124  1.1  christos 	continue;
   8125  1.1  christos 
   8126  1.1  christos       instruction |= (sym_offset & 0xffff);
   8127  1.1  christos       bfd_put_32 (abfd, instruction, contents + irel->r_offset);
   8128  1.1  christos       changed_contents = TRUE;
   8129  1.1  christos     }
   8130  1.1  christos 
   8131  1.1  christos   if (contents != NULL
   8132  1.1  christos       && elf_section_data (sec)->this_hdr.contents != contents)
   8133  1.1  christos     {
   8134  1.1  christos       if (!changed_contents && !link_info->keep_memory)
   8135  1.1  christos         free (contents);
   8136  1.1  christos       else
   8137  1.1  christos         {
   8138  1.1  christos           /* Cache the section contents for elf_link_input_bfd.  */
   8139  1.1  christos           elf_section_data (sec)->this_hdr.contents = contents;
   8140  1.1  christos         }
   8141  1.1  christos     }
   8142  1.1  christos   return TRUE;
   8143  1.1  christos 
   8144  1.1  christos  relax_return:
   8145  1.1  christos   if (contents != NULL
   8146  1.1  christos       && elf_section_data (sec)->this_hdr.contents != contents)
   8147  1.1  christos     free (contents);
   8148  1.1  christos   return FALSE;
   8149  1.1  christos }
   8150  1.1  christos 
   8151  1.1  christos /* Allocate space for global sym dynamic relocs.  */
   8153  1.1  christos 
   8154  1.1  christos static bfd_boolean
   8155  1.1  christos allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   8156  1.1  christos {
   8157  1.1  christos   struct bfd_link_info *info = inf;
   8158  1.1  christos   bfd *dynobj;
   8159  1.1  christos   struct mips_elf_link_hash_entry *hmips;
   8160  1.1  christos   struct mips_elf_link_hash_table *htab;
   8161  1.1  christos 
   8162  1.1  christos   htab = mips_elf_hash_table (info);
   8163  1.1  christos   BFD_ASSERT (htab != NULL);
   8164  1.1  christos 
   8165  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   8166  1.1  christos   hmips = (struct mips_elf_link_hash_entry *) h;
   8167  1.1  christos 
   8168  1.1  christos   /* VxWorks executables are handled elsewhere; we only need to
   8169  1.1  christos      allocate relocations in shared objects.  */
   8170  1.1  christos   if (htab->is_vxworks && !info->shared)
   8171  1.1  christos     return TRUE;
   8172  1.1  christos 
   8173  1.1  christos   /* Ignore indirect and warning symbols.  All relocations against
   8174  1.1  christos      such symbols will be redirected to the target symbol.  */
   8175  1.1  christos   if (h->root.type == bfd_link_hash_indirect
   8176  1.1  christos       || h->root.type == bfd_link_hash_warning)
   8177  1.1  christos     return TRUE;
   8178  1.1  christos 
   8179  1.1  christos   /* If this symbol is defined in a dynamic object, or we are creating
   8180  1.1  christos      a shared library, we will need to copy any R_MIPS_32 or
   8181  1.1  christos      R_MIPS_REL32 relocs against it into the output file.  */
   8182  1.1  christos   if (! info->relocatable
   8183  1.1  christos       && hmips->possibly_dynamic_relocs != 0
   8184  1.1  christos       && (h->root.type == bfd_link_hash_defweak
   8185  1.1  christos 	  || !h->def_regular
   8186  1.1  christos 	  || info->shared))
   8187  1.1  christos     {
   8188  1.1  christos       bfd_boolean do_copy = TRUE;
   8189  1.1  christos 
   8190  1.1  christos       if (h->root.type == bfd_link_hash_undefweak)
   8191  1.1  christos 	{
   8192  1.1  christos 	  /* Do not copy relocations for undefined weak symbols with
   8193  1.1  christos 	     non-default visibility.  */
   8194  1.1  christos 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   8195  1.1  christos 	    do_copy = FALSE;
   8196  1.1  christos 
   8197  1.1  christos 	  /* Make sure undefined weak symbols are output as a dynamic
   8198  1.1  christos 	     symbol in PIEs.  */
   8199  1.1  christos 	  else if (h->dynindx == -1 && !h->forced_local)
   8200  1.1  christos 	    {
   8201  1.1  christos 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8202  1.1  christos 		return FALSE;
   8203  1.1  christos 	    }
   8204  1.1  christos 	}
   8205  1.1  christos 
   8206  1.1  christos       if (do_copy)
   8207  1.1  christos 	{
   8208  1.1  christos 	  /* Even though we don't directly need a GOT entry for this symbol,
   8209  1.1  christos 	     the SVR4 psABI requires it to have a dynamic symbol table
   8210  1.1  christos 	     index greater that DT_MIPS_GOTSYM if there are dynamic
   8211  1.1  christos 	     relocations against it.
   8212  1.1  christos 
   8213  1.1  christos 	     VxWorks does not enforce the same mapping between the GOT
   8214  1.1  christos 	     and the symbol table, so the same requirement does not
   8215  1.1  christos 	     apply there.  */
   8216  1.1  christos 	  if (!htab->is_vxworks)
   8217  1.1  christos 	    {
   8218  1.1  christos 	      if (hmips->global_got_area > GGA_RELOC_ONLY)
   8219  1.1  christos 		hmips->global_got_area = GGA_RELOC_ONLY;
   8220  1.1  christos 	      hmips->got_only_for_calls = FALSE;
   8221  1.1  christos 	    }
   8222  1.1  christos 
   8223  1.1  christos 	  mips_elf_allocate_dynamic_relocations
   8224  1.1  christos 	    (dynobj, info, hmips->possibly_dynamic_relocs);
   8225  1.1  christos 	  if (hmips->readonly_reloc)
   8226  1.1  christos 	    /* We tell the dynamic linker that there are relocations
   8227  1.1  christos 	       against the text segment.  */
   8228  1.1  christos 	    info->flags |= DF_TEXTREL;
   8229  1.1  christos 	}
   8230  1.1  christos     }
   8231  1.1  christos 
   8232  1.1  christos   return TRUE;
   8233  1.1  christos }
   8234  1.1  christos 
   8235  1.1  christos /* Adjust a symbol defined by a dynamic object and referenced by a
   8236  1.1  christos    regular object.  The current definition is in some section of the
   8237  1.1  christos    dynamic object, but we're not including those sections.  We have to
   8238  1.1  christos    change the definition to something the rest of the link can
   8239  1.1  christos    understand.  */
   8240  1.1  christos 
   8241  1.1  christos bfd_boolean
   8242  1.1  christos _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   8243  1.1  christos 				     struct elf_link_hash_entry *h)
   8244  1.1  christos {
   8245  1.1  christos   bfd *dynobj;
   8246  1.1  christos   struct mips_elf_link_hash_entry *hmips;
   8247  1.1  christos   struct mips_elf_link_hash_table *htab;
   8248  1.1  christos 
   8249  1.1  christos   htab = mips_elf_hash_table (info);
   8250  1.1  christos   BFD_ASSERT (htab != NULL);
   8251  1.1  christos 
   8252  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   8253  1.1  christos   hmips = (struct mips_elf_link_hash_entry *) h;
   8254  1.1  christos 
   8255  1.1  christos   /* Make sure we know what is going on here.  */
   8256  1.1  christos   BFD_ASSERT (dynobj != NULL
   8257  1.1  christos 	      && (h->needs_plt
   8258  1.1  christos 		  || h->u.weakdef != NULL
   8259  1.1  christos 		  || (h->def_dynamic
   8260  1.1  christos 		      && h->ref_regular
   8261  1.1  christos 		      && !h->def_regular)));
   8262  1.1  christos 
   8263  1.1  christos   hmips = (struct mips_elf_link_hash_entry *) h;
   8264  1.1  christos 
   8265  1.1  christos   /* If there are call relocations against an externally-defined symbol,
   8266  1.1  christos      see whether we can create a MIPS lazy-binding stub for it.  We can
   8267  1.1  christos      only do this if all references to the function are through call
   8268  1.1  christos      relocations, and in that case, the traditional lazy-binding stubs
   8269  1.1  christos      are much more efficient than PLT entries.
   8270  1.1  christos 
   8271  1.1  christos      Traditional stubs are only available on SVR4 psABI-based systems;
   8272  1.1  christos      VxWorks always uses PLTs instead.  */
   8273  1.1  christos   if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
   8274  1.1  christos     {
   8275  1.1  christos       if (! elf_hash_table (info)->dynamic_sections_created)
   8276  1.1  christos 	return TRUE;
   8277  1.1  christos 
   8278  1.1  christos       /* If this symbol is not defined in a regular file, then set
   8279  1.1  christos 	 the symbol to the stub location.  This is required to make
   8280  1.1  christos 	 function pointers compare as equal between the normal
   8281  1.1  christos 	 executable and the shared library.  */
   8282  1.1  christos       if (!h->def_regular)
   8283  1.1  christos 	{
   8284  1.1  christos 	  hmips->needs_lazy_stub = TRUE;
   8285  1.1  christos 	  htab->lazy_stub_count++;
   8286  1.1  christos 	  return TRUE;
   8287  1.1  christos 	}
   8288  1.1  christos     }
   8289  1.1  christos   /* As above, VxWorks requires PLT entries for externally-defined
   8290  1.1  christos      functions that are only accessed through call relocations.
   8291  1.1  christos 
   8292  1.1  christos      Both VxWorks and non-VxWorks targets also need PLT entries if there
   8293  1.1  christos      are static-only relocations against an externally-defined function.
   8294  1.1  christos      This can technically occur for shared libraries if there are
   8295  1.1  christos      branches to the symbol, although it is unlikely that this will be
   8296  1.1  christos      used in practice due to the short ranges involved.  It can occur
   8297  1.1  christos      for any relative or absolute relocation in executables; in that
   8298  1.1  christos      case, the PLT entry becomes the function's canonical address.  */
   8299  1.1  christos   else if (((h->needs_plt && !hmips->no_fn_stub)
   8300  1.1  christos 	    || (h->type == STT_FUNC && hmips->has_static_relocs))
   8301  1.1  christos 	   && htab->use_plts_and_copy_relocs
   8302  1.1  christos 	   && !SYMBOL_CALLS_LOCAL (info, h)
   8303  1.1  christos 	   && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   8304  1.1  christos 		&& h->root.type == bfd_link_hash_undefweak))
   8305  1.1  christos     {
   8306  1.1  christos       /* If this is the first symbol to need a PLT entry, allocate room
   8307  1.1  christos 	 for the header.  */
   8308  1.1  christos       if (htab->splt->size == 0)
   8309  1.1  christos 	{
   8310  1.1  christos 	  BFD_ASSERT (htab->sgotplt->size == 0);
   8311  1.1  christos 
   8312  1.1  christos 	  /* If we're using the PLT additions to the psABI, each PLT
   8313  1.1  christos 	     entry is 16 bytes and the PLT0 entry is 32 bytes.
   8314  1.1  christos 	     Encourage better cache usage by aligning.  We do this
   8315  1.1  christos 	     lazily to avoid pessimizing traditional objects.  */
   8316  1.1  christos 	  if (!htab->is_vxworks
   8317  1.1  christos 	      && !bfd_set_section_alignment (dynobj, htab->splt, 5))
   8318  1.1  christos 	    return FALSE;
   8319  1.1  christos 
   8320  1.1  christos 	  /* Make sure that .got.plt is word-aligned.  We do this lazily
   8321  1.1  christos 	     for the same reason as above.  */
   8322  1.1  christos 	  if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
   8323  1.1  christos 					  MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
   8324  1.1  christos 	    return FALSE;
   8325  1.1  christos 
   8326  1.1  christos 	  htab->splt->size += htab->plt_header_size;
   8327  1.1  christos 
   8328  1.1  christos 	  /* On non-VxWorks targets, the first two entries in .got.plt
   8329  1.1  christos 	     are reserved.  */
   8330  1.1  christos 	  if (!htab->is_vxworks)
   8331  1.1  christos 	    htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
   8332  1.1  christos 
   8333  1.1  christos 	  /* On VxWorks, also allocate room for the header's
   8334  1.1  christos 	     .rela.plt.unloaded entries.  */
   8335  1.1  christos 	  if (htab->is_vxworks && !info->shared)
   8336  1.1  christos 	    htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
   8337  1.1  christos 	}
   8338  1.1  christos 
   8339  1.1  christos       /* Assign the next .plt entry to this symbol.  */
   8340  1.1  christos       h->plt.offset = htab->splt->size;
   8341  1.1  christos       htab->splt->size += htab->plt_entry_size;
   8342  1.1  christos 
   8343  1.1  christos       /* If the output file has no definition of the symbol, set the
   8344  1.1  christos 	 symbol's value to the address of the stub.  */
   8345  1.1  christos       if (!info->shared && !h->def_regular)
   8346  1.1  christos 	{
   8347  1.1  christos 	  h->root.u.def.section = htab->splt;
   8348  1.1  christos 	  h->root.u.def.value = h->plt.offset;
   8349  1.1  christos 	  /* For VxWorks, point at the PLT load stub rather than the
   8350  1.1  christos 	     lazy resolution stub; this stub will become the canonical
   8351  1.1  christos 	     function address.  */
   8352  1.1  christos 	  if (htab->is_vxworks)
   8353  1.1  christos 	    h->root.u.def.value += 8;
   8354  1.1  christos 	}
   8355  1.1  christos 
   8356  1.1  christos       /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
   8357  1.1  christos 	 relocation.  */
   8358  1.1  christos       htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
   8359  1.1  christos       htab->srelplt->size += (htab->is_vxworks
   8360  1.1  christos 			      ? MIPS_ELF_RELA_SIZE (dynobj)
   8361  1.1  christos 			      : MIPS_ELF_REL_SIZE (dynobj));
   8362  1.1  christos 
   8363  1.1  christos       /* Make room for the .rela.plt.unloaded relocations.  */
   8364  1.1  christos       if (htab->is_vxworks && !info->shared)
   8365  1.1  christos 	htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
   8366  1.1  christos 
   8367  1.1  christos       /* All relocations against this symbol that could have been made
   8368  1.1  christos 	 dynamic will now refer to the PLT entry instead.  */
   8369  1.1  christos       hmips->possibly_dynamic_relocs = 0;
   8370  1.1  christos 
   8371  1.1  christos       return TRUE;
   8372  1.1  christos     }
   8373  1.1  christos 
   8374  1.1  christos   /* If this is a weak symbol, and there is a real definition, the
   8375  1.1  christos      processor independent code will have arranged for us to see the
   8376  1.1  christos      real definition first, and we can just use the same value.  */
   8377  1.1  christos   if (h->u.weakdef != NULL)
   8378  1.1  christos     {
   8379  1.1  christos       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   8380  1.1  christos 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   8381  1.1  christos       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   8382  1.1  christos       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   8383  1.1  christos       return TRUE;
   8384  1.1  christos     }
   8385  1.1  christos 
   8386  1.1  christos   /* Otherwise, there is nothing further to do for symbols defined
   8387  1.1  christos      in regular objects.  */
   8388  1.1  christos   if (h->def_regular)
   8389  1.1  christos     return TRUE;
   8390  1.1  christos 
   8391  1.1  christos   /* There's also nothing more to do if we'll convert all relocations
   8392  1.1  christos      against this symbol into dynamic relocations.  */
   8393  1.1  christos   if (!hmips->has_static_relocs)
   8394  1.1  christos     return TRUE;
   8395  1.1  christos 
   8396  1.1  christos   /* We're now relying on copy relocations.  Complain if we have
   8397  1.1  christos      some that we can't convert.  */
   8398  1.1  christos   if (!htab->use_plts_and_copy_relocs || info->shared)
   8399  1.1  christos     {
   8400  1.1  christos       (*_bfd_error_handler) (_("non-dynamic relocations refer to "
   8401  1.1  christos 			       "dynamic symbol %s"),
   8402  1.1  christos 			     h->root.root.string);
   8403  1.1  christos       bfd_set_error (bfd_error_bad_value);
   8404  1.1  christos       return FALSE;
   8405  1.1  christos     }
   8406  1.1  christos 
   8407  1.1  christos   /* We must allocate the symbol in our .dynbss section, which will
   8408  1.1  christos      become part of the .bss section of the executable.  There will be
   8409  1.1  christos      an entry for this symbol in the .dynsym section.  The dynamic
   8410  1.1  christos      object will contain position independent code, so all references
   8411  1.1  christos      from the dynamic object to this symbol will go through the global
   8412  1.1  christos      offset table.  The dynamic linker will use the .dynsym entry to
   8413  1.1  christos      determine the address it must put in the global offset table, so
   8414  1.1  christos      both the dynamic object and the regular object will refer to the
   8415  1.1  christos      same memory location for the variable.  */
   8416  1.1  christos 
   8417  1.1  christos   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
   8418  1.1  christos     {
   8419  1.1  christos       if (htab->is_vxworks)
   8420  1.1  christos 	htab->srelbss->size += sizeof (Elf32_External_Rela);
   8421  1.1  christos       else
   8422  1.1  christos 	mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
   8423  1.1  christos       h->needs_copy = 1;
   8424  1.1  christos     }
   8425  1.1  christos 
   8426  1.1  christos   /* All relocations against this symbol that could have been made
   8427  1.1  christos      dynamic will now refer to the local copy instead.  */
   8428  1.1  christos   hmips->possibly_dynamic_relocs = 0;
   8429  1.1  christos 
   8430  1.1  christos   return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
   8431  1.1  christos }
   8432  1.1  christos 
   8433  1.1  christos /* This function is called after all the input files have been read,
   8435  1.1  christos    and the input sections have been assigned to output sections.  We
   8436  1.1  christos    check for any mips16 stub sections that we can discard.  */
   8437  1.1  christos 
   8438  1.1  christos bfd_boolean
   8439  1.1  christos _bfd_mips_elf_always_size_sections (bfd *output_bfd,
   8440  1.1  christos 				    struct bfd_link_info *info)
   8441  1.1  christos {
   8442  1.1  christos   asection *ri;
   8443  1.1  christos   struct mips_elf_link_hash_table *htab;
   8444  1.1  christos   struct mips_htab_traverse_info hti;
   8445  1.1  christos 
   8446  1.1  christos   htab = mips_elf_hash_table (info);
   8447  1.1  christos   BFD_ASSERT (htab != NULL);
   8448  1.1  christos 
   8449  1.1  christos   /* The .reginfo section has a fixed size.  */
   8450  1.1  christos   ri = bfd_get_section_by_name (output_bfd, ".reginfo");
   8451  1.1  christos   if (ri != NULL)
   8452  1.1  christos     bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
   8453  1.1  christos 
   8454  1.1  christos   hti.info = info;
   8455  1.1  christos   hti.output_bfd = output_bfd;
   8456  1.1  christos   hti.error = FALSE;
   8457  1.1  christos   mips_elf_link_hash_traverse (mips_elf_hash_table (info),
   8458  1.1  christos 			       mips_elf_check_symbols, &hti);
   8459  1.1  christos   if (hti.error)
   8460  1.1  christos     return FALSE;
   8461  1.1  christos 
   8462  1.1  christos   return TRUE;
   8463  1.1  christos }
   8464  1.1  christos 
   8465  1.1  christos /* If the link uses a GOT, lay it out and work out its size.  */
   8466  1.1  christos 
   8467  1.1  christos static bfd_boolean
   8468  1.1  christos mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
   8469  1.1  christos {
   8470  1.1  christos   bfd *dynobj;
   8471  1.1  christos   asection *s;
   8472  1.1  christos   struct mips_got_info *g;
   8473  1.1  christos   bfd_size_type loadable_size = 0;
   8474  1.1  christos   bfd_size_type page_gotno;
   8475  1.1  christos   bfd *sub;
   8476  1.1  christos   struct mips_elf_count_tls_arg count_tls_arg;
   8477  1.1  christos   struct mips_elf_link_hash_table *htab;
   8478  1.1  christos 
   8479  1.1  christos   htab = mips_elf_hash_table (info);
   8480  1.1  christos   BFD_ASSERT (htab != NULL);
   8481  1.1  christos 
   8482  1.1  christos   s = htab->sgot;
   8483  1.1  christos   if (s == NULL)
   8484  1.1  christos     return TRUE;
   8485  1.1  christos 
   8486  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   8487  1.1  christos   g = htab->got_info;
   8488  1.1  christos 
   8489  1.1  christos   /* Allocate room for the reserved entries.  VxWorks always reserves
   8490  1.1  christos      3 entries; other objects only reserve 2 entries.  */
   8491  1.1  christos   BFD_ASSERT (g->assigned_gotno == 0);
   8492  1.1  christos   if (htab->is_vxworks)
   8493  1.1  christos     htab->reserved_gotno = 3;
   8494  1.1  christos   else
   8495  1.1  christos     htab->reserved_gotno = 2;
   8496  1.1  christos   g->local_gotno += htab->reserved_gotno;
   8497  1.1  christos   g->assigned_gotno = htab->reserved_gotno;
   8498  1.1  christos 
   8499  1.1  christos   /* Replace entries for indirect and warning symbols with entries for
   8500  1.1  christos      the target symbol.  */
   8501  1.1  christos   if (!mips_elf_resolve_final_got_entries (g))
   8502  1.1  christos     return FALSE;
   8503  1.1  christos 
   8504  1.1  christos   /* Count the number of GOT symbols.  */
   8505  1.1  christos   mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
   8506  1.1  christos 
   8507  1.1  christos   /* Calculate the total loadable size of the output.  That
   8508  1.1  christos      will give us the maximum number of GOT_PAGE entries
   8509  1.1  christos      required.  */
   8510  1.1  christos   for (sub = info->input_bfds; sub; sub = sub->link_next)
   8511  1.1  christos     {
   8512  1.1  christos       asection *subsection;
   8513  1.1  christos 
   8514  1.1  christos       for (subsection = sub->sections;
   8515  1.1  christos 	   subsection;
   8516  1.1  christos 	   subsection = subsection->next)
   8517  1.1  christos 	{
   8518  1.1  christos 	  if ((subsection->flags & SEC_ALLOC) == 0)
   8519  1.1  christos 	    continue;
   8520  1.1  christos 	  loadable_size += ((subsection->size + 0xf)
   8521  1.1  christos 			    &~ (bfd_size_type) 0xf);
   8522  1.1  christos 	}
   8523  1.1  christos     }
   8524  1.1  christos 
   8525  1.1  christos   if (htab->is_vxworks)
   8526  1.1  christos     /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
   8527  1.1  christos        relocations against local symbols evaluate to "G", and the EABI does
   8528  1.1  christos        not include R_MIPS_GOT_PAGE.  */
   8529  1.1  christos     page_gotno = 0;
   8530  1.1  christos   else
   8531  1.1  christos     /* Assume there are two loadable segments consisting of contiguous
   8532  1.1  christos        sections.  Is 5 enough?  */
   8533  1.1  christos     page_gotno = (loadable_size >> 16) + 5;
   8534  1.1  christos 
   8535  1.1  christos   /* Choose the smaller of the two estimates; both are intended to be
   8536  1.1  christos      conservative.  */
   8537  1.1  christos   if (page_gotno > g->page_gotno)
   8538  1.1  christos     page_gotno = g->page_gotno;
   8539  1.1  christos 
   8540  1.1  christos   g->local_gotno += page_gotno;
   8541  1.1  christos   s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   8542  1.1  christos   s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   8543  1.1  christos 
   8544  1.1  christos   /* We need to calculate tls_gotno for global symbols at this point
   8545  1.1  christos      instead of building it up earlier, to avoid doublecounting
   8546  1.1  christos      entries for one global symbol from multiple input files.  */
   8547  1.1  christos   count_tls_arg.info = info;
   8548  1.1  christos   count_tls_arg.needed = 0;
   8549  1.1  christos   elf_link_hash_traverse (elf_hash_table (info),
   8550  1.1  christos 			  mips_elf_count_global_tls_entries,
   8551  1.1  christos 			  &count_tls_arg);
   8552  1.1  christos   g->tls_gotno += count_tls_arg.needed;
   8553  1.1  christos   s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
   8554  1.1  christos 
   8555  1.1  christos   /* VxWorks does not support multiple GOTs.  It initializes $gp to
   8556  1.1  christos      __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
   8557  1.1  christos      dynamic loader.  */
   8558  1.1  christos   if (htab->is_vxworks)
   8559  1.1  christos     {
   8560  1.1  christos       /* VxWorks executables do not need a GOT.  */
   8561  1.1  christos       if (info->shared)
   8562  1.1  christos 	{
   8563  1.1  christos 	  /* Each VxWorks GOT entry needs an explicit relocation.  */
   8564  1.1  christos 	  unsigned int count;
   8565  1.1  christos 
   8566  1.1  christos 	  count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
   8567  1.1  christos 	  if (count)
   8568  1.1  christos 	    mips_elf_allocate_dynamic_relocations (dynobj, info, count);
   8569  1.1  christos 	}
   8570  1.1  christos     }
   8571  1.1  christos   else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
   8572  1.1  christos     {
   8573  1.1  christos       if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
   8574  1.1  christos 	return FALSE;
   8575  1.1  christos     }
   8576  1.1  christos   else
   8577  1.1  christos     {
   8578  1.1  christos       struct mips_elf_count_tls_arg arg;
   8579  1.1  christos 
   8580  1.1  christos       /* Set up TLS entries.  */
   8581  1.1  christos       g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
   8582  1.1  christos       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
   8583  1.1  christos 
   8584  1.1  christos       /* Allocate room for the TLS relocations.  */
   8585  1.1  christos       arg.info = info;
   8586  1.1  christos       arg.needed = 0;
   8587  1.1  christos       htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
   8588  1.1  christos       elf_link_hash_traverse (elf_hash_table (info),
   8589  1.1  christos 			      mips_elf_count_global_tls_relocs,
   8590  1.1  christos 			      &arg);
   8591  1.1  christos       if (arg.needed)
   8592  1.1  christos 	mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
   8593  1.1  christos     }
   8594  1.1  christos 
   8595  1.1  christos   return TRUE;
   8596  1.1  christos }
   8597  1.1  christos 
   8598  1.1  christos /* Estimate the size of the .MIPS.stubs section.  */
   8599  1.1  christos 
   8600  1.1  christos static void
   8601  1.1  christos mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
   8602  1.1  christos {
   8603  1.1  christos   struct mips_elf_link_hash_table *htab;
   8604  1.1  christos   bfd_size_type dynsymcount;
   8605  1.1  christos 
   8606  1.1  christos   htab = mips_elf_hash_table (info);
   8607  1.1  christos   BFD_ASSERT (htab != NULL);
   8608  1.1  christos 
   8609  1.1  christos   if (htab->lazy_stub_count == 0)
   8610  1.1  christos     return;
   8611  1.1  christos 
   8612  1.1  christos   /* IRIX rld assumes that a function stub isn't at the end of the .text
   8613  1.1  christos      section, so add a dummy entry to the end.  */
   8614  1.1  christos   htab->lazy_stub_count++;
   8615  1.1  christos 
   8616  1.1  christos   /* Get a worst-case estimate of the number of dynamic symbols needed.
   8617  1.1  christos      At this point, dynsymcount does not account for section symbols
   8618  1.1  christos      and count_section_dynsyms may overestimate the number that will
   8619  1.1  christos      be needed.  */
   8620  1.1  christos   dynsymcount = (elf_hash_table (info)->dynsymcount
   8621  1.1  christos 		 + count_section_dynsyms (output_bfd, info));
   8622  1.1  christos 
   8623  1.1  christos   /* Determine the size of one stub entry.  */
   8624  1.1  christos   htab->function_stub_size = (dynsymcount > 0x10000
   8625  1.1  christos 			      ? MIPS_FUNCTION_STUB_BIG_SIZE
   8626  1.1  christos 			      : MIPS_FUNCTION_STUB_NORMAL_SIZE);
   8627  1.1  christos 
   8628  1.1  christos   htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
   8629  1.1  christos }
   8630  1.1  christos 
   8631  1.1  christos /* A mips_elf_link_hash_traverse callback for which DATA points to the
   8632  1.1  christos    MIPS hash table.  If H needs a traditional MIPS lazy-binding stub,
   8633  1.1  christos    allocate an entry in the stubs section.  */
   8634  1.1  christos 
   8635  1.1  christos static bfd_boolean
   8636  1.1  christos mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
   8637  1.1  christos {
   8638  1.1  christos   struct mips_elf_link_hash_table *htab;
   8639  1.1  christos 
   8640  1.1  christos   htab = (struct mips_elf_link_hash_table *) data;
   8641  1.1  christos   if (h->needs_lazy_stub)
   8642  1.1  christos     {
   8643  1.1  christos       h->root.root.u.def.section = htab->sstubs;
   8644  1.1  christos       h->root.root.u.def.value = htab->sstubs->size;
   8645  1.1  christos       h->root.plt.offset = htab->sstubs->size;
   8646  1.1  christos       htab->sstubs->size += htab->function_stub_size;
   8647  1.1  christos     }
   8648  1.1  christos   return TRUE;
   8649  1.1  christos }
   8650  1.1  christos 
   8651  1.1  christos /* Allocate offsets in the stubs section to each symbol that needs one.
   8652  1.1  christos    Set the final size of the .MIPS.stub section.  */
   8653  1.1  christos 
   8654  1.1  christos static void
   8655  1.1  christos mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
   8656  1.1  christos {
   8657  1.1  christos   struct mips_elf_link_hash_table *htab;
   8658  1.1  christos 
   8659  1.1  christos   htab = mips_elf_hash_table (info);
   8660  1.1  christos   BFD_ASSERT (htab != NULL);
   8661  1.1  christos 
   8662  1.1  christos   if (htab->lazy_stub_count == 0)
   8663  1.1  christos     return;
   8664  1.1  christos 
   8665  1.1  christos   htab->sstubs->size = 0;
   8666  1.1  christos   mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, htab);
   8667  1.1  christos   htab->sstubs->size += htab->function_stub_size;
   8668  1.1  christos   BFD_ASSERT (htab->sstubs->size
   8669  1.1  christos 	      == htab->lazy_stub_count * htab->function_stub_size);
   8670  1.1  christos }
   8671  1.1  christos 
   8672  1.1  christos /* Set the sizes of the dynamic sections.  */
   8673  1.1  christos 
   8674  1.1  christos bfd_boolean
   8675  1.1  christos _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
   8676  1.1  christos 				     struct bfd_link_info *info)
   8677  1.1  christos {
   8678  1.1  christos   bfd *dynobj;
   8679  1.1  christos   asection *s, *sreldyn;
   8680  1.1  christos   bfd_boolean reltext;
   8681  1.1  christos   struct mips_elf_link_hash_table *htab;
   8682  1.1  christos 
   8683  1.1  christos   htab = mips_elf_hash_table (info);
   8684  1.1  christos   BFD_ASSERT (htab != NULL);
   8685  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   8686  1.1  christos   BFD_ASSERT (dynobj != NULL);
   8687  1.1  christos 
   8688  1.1  christos   if (elf_hash_table (info)->dynamic_sections_created)
   8689  1.1  christos     {
   8690  1.1  christos       /* Set the contents of the .interp section to the interpreter.  */
   8691  1.1  christos       if (info->executable)
   8692  1.1  christos 	{
   8693  1.1  christos 	  s = bfd_get_section_by_name (dynobj, ".interp");
   8694  1.1  christos 	  BFD_ASSERT (s != NULL);
   8695  1.1  christos 	  s->size
   8696  1.1  christos 	    = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
   8697  1.1  christos 	  s->contents
   8698  1.1  christos 	    = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
   8699  1.1  christos 	}
   8700  1.1  christos 
   8701  1.1  christos       /* Create a symbol for the PLT, if we know that we are using it.  */
   8702  1.1  christos       if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
   8703  1.1  christos 	{
   8704  1.1  christos 	  struct elf_link_hash_entry *h;
   8705  1.1  christos 
   8706  1.1  christos 	  BFD_ASSERT (htab->use_plts_and_copy_relocs);
   8707  1.1  christos 
   8708  1.1  christos 	  h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
   8709  1.1  christos 					   "_PROCEDURE_LINKAGE_TABLE_");
   8710  1.1  christos 	  htab->root.hplt = h;
   8711  1.1  christos 	  if (h == NULL)
   8712  1.1  christos 	    return FALSE;
   8713  1.1  christos 	  h->type = STT_FUNC;
   8714  1.1  christos 	}
   8715  1.1  christos     }
   8716  1.1  christos 
   8717  1.1  christos   /* Allocate space for global sym dynamic relocs.  */
   8718  1.1  christos   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
   8719  1.1  christos 
   8720  1.1  christos   mips_elf_estimate_stub_size (output_bfd, info);
   8721  1.1  christos 
   8722  1.1  christos   if (!mips_elf_lay_out_got (output_bfd, info))
   8723  1.1  christos     return FALSE;
   8724  1.1  christos 
   8725  1.1  christos   mips_elf_lay_out_lazy_stubs (info);
   8726  1.1  christos 
   8727  1.1  christos   /* The check_relocs and adjust_dynamic_symbol entry points have
   8728  1.1  christos      determined the sizes of the various dynamic sections.  Allocate
   8729  1.1  christos      memory for them.  */
   8730  1.1  christos   reltext = FALSE;
   8731  1.1  christos   for (s = dynobj->sections; s != NULL; s = s->next)
   8732  1.1  christos     {
   8733  1.1  christos       const char *name;
   8734  1.1  christos 
   8735  1.1  christos       /* It's OK to base decisions on the section name, because none
   8736  1.1  christos 	 of the dynobj section names depend upon the input files.  */
   8737  1.1  christos       name = bfd_get_section_name (dynobj, s);
   8738  1.1  christos 
   8739  1.1  christos       if ((s->flags & SEC_LINKER_CREATED) == 0)
   8740  1.1  christos 	continue;
   8741  1.1  christos 
   8742  1.1  christos       if (CONST_STRNEQ (name, ".rel"))
   8743  1.1  christos 	{
   8744  1.1  christos 	  if (s->size != 0)
   8745  1.1  christos 	    {
   8746  1.1  christos 	      const char *outname;
   8747  1.1  christos 	      asection *target;
   8748  1.1  christos 
   8749  1.1  christos 	      /* If this relocation section applies to a read only
   8750  1.1  christos                  section, then we probably need a DT_TEXTREL entry.
   8751  1.1  christos                  If the relocation section is .rel(a).dyn, we always
   8752  1.1  christos                  assert a DT_TEXTREL entry rather than testing whether
   8753  1.1  christos                  there exists a relocation to a read only section or
   8754  1.1  christos                  not.  */
   8755  1.1  christos 	      outname = bfd_get_section_name (output_bfd,
   8756  1.1  christos 					      s->output_section);
   8757  1.1  christos 	      target = bfd_get_section_by_name (output_bfd, outname + 4);
   8758  1.1  christos 	      if ((target != NULL
   8759  1.1  christos 		   && (target->flags & SEC_READONLY) != 0
   8760  1.1  christos 		   && (target->flags & SEC_ALLOC) != 0)
   8761  1.1  christos 		  || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
   8762  1.1  christos 		reltext = TRUE;
   8763  1.1  christos 
   8764  1.1  christos 	      /* We use the reloc_count field as a counter if we need
   8765  1.1  christos 		 to copy relocs into the output file.  */
   8766  1.1  christos 	      if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
   8767  1.1  christos 		s->reloc_count = 0;
   8768  1.1  christos 
   8769  1.1  christos 	      /* If combreloc is enabled, elf_link_sort_relocs() will
   8770  1.1  christos 		 sort relocations, but in a different way than we do,
   8771  1.1  christos 		 and before we're done creating relocations.  Also, it
   8772  1.1  christos 		 will move them around between input sections'
   8773  1.1  christos 		 relocation's contents, so our sorting would be
   8774  1.1  christos 		 broken, so don't let it run.  */
   8775  1.1  christos 	      info->combreloc = 0;
   8776  1.1  christos 	    }
   8777  1.1  christos 	}
   8778  1.1  christos       else if (! info->shared
   8779  1.1  christos 	       && ! mips_elf_hash_table (info)->use_rld_obj_head
   8780  1.1  christos 	       && CONST_STRNEQ (name, ".rld_map"))
   8781  1.1  christos 	{
   8782  1.1  christos 	  /* We add a room for __rld_map.  It will be filled in by the
   8783  1.1  christos 	     rtld to contain a pointer to the _r_debug structure.  */
   8784  1.1  christos 	  s->size += 4;
   8785  1.1  christos 	}
   8786  1.1  christos       else if (SGI_COMPAT (output_bfd)
   8787  1.1  christos 	       && CONST_STRNEQ (name, ".compact_rel"))
   8788  1.1  christos 	s->size += mips_elf_hash_table (info)->compact_rel_size;
   8789  1.1  christos       else if (s == htab->splt)
   8790  1.1  christos 	{
   8791  1.1  christos 	  /* If the last PLT entry has a branch delay slot, allocate
   8792  1.1  christos 	     room for an extra nop to fill the delay slot.  This is
   8793  1.1  christos 	     for CPUs without load interlocking.  */
   8794  1.1  christos 	  if (! LOAD_INTERLOCKS_P (output_bfd)
   8795  1.1  christos 	      && ! htab->is_vxworks && s->size > 0)
   8796  1.1  christos 	    s->size += 4;
   8797  1.1  christos 	}
   8798  1.1  christos       else if (! CONST_STRNEQ (name, ".init")
   8799  1.1  christos 	       && s != htab->sgot
   8800  1.1  christos 	       && s != htab->sgotplt
   8801  1.1  christos 	       && s != htab->sstubs
   8802  1.1  christos 	       && s != htab->sdynbss)
   8803  1.1  christos 	{
   8804  1.1  christos 	  /* It's not one of our sections, so don't allocate space.  */
   8805  1.1  christos 	  continue;
   8806  1.1  christos 	}
   8807  1.1  christos 
   8808  1.1  christos       if (s->size == 0)
   8809  1.1  christos 	{
   8810  1.1  christos 	  s->flags |= SEC_EXCLUDE;
   8811  1.1  christos 	  continue;
   8812  1.1  christos 	}
   8813  1.1  christos 
   8814  1.1  christos       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   8815  1.1  christos 	continue;
   8816  1.1  christos 
   8817  1.1  christos       /* Allocate memory for the section contents.  */
   8818  1.1  christos       s->contents = bfd_zalloc (dynobj, s->size);
   8819  1.1  christos       if (s->contents == NULL)
   8820  1.1  christos 	{
   8821  1.1  christos 	  bfd_set_error (bfd_error_no_memory);
   8822  1.1  christos 	  return FALSE;
   8823  1.1  christos 	}
   8824  1.1  christos     }
   8825  1.1  christos 
   8826  1.1  christos   if (elf_hash_table (info)->dynamic_sections_created)
   8827  1.1  christos     {
   8828  1.1  christos       /* Add some entries to the .dynamic section.  We fill in the
   8829  1.1  christos 	 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
   8830  1.1  christos 	 must add the entries now so that we get the correct size for
   8831  1.1  christos 	 the .dynamic section.  */
   8832  1.1  christos 
   8833  1.1  christos       /* SGI object has the equivalence of DT_DEBUG in the
   8834  1.1  christos 	 DT_MIPS_RLD_MAP entry.  This must come first because glibc
   8835  1.1  christos 	 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
   8836  1.1  christos 	 looks at the first one it sees.  */
   8837  1.1  christos       if (!info->shared
   8838  1.1  christos 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
   8839  1.1  christos 	return FALSE;
   8840  1.1  christos 
   8841  1.1  christos       /* The DT_DEBUG entry may be filled in by the dynamic linker and
   8842  1.1  christos 	 used by the debugger.  */
   8843  1.1  christos       if (info->executable
   8844  1.1  christos 	  && !SGI_COMPAT (output_bfd)
   8845  1.1  christos 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
   8846  1.1  christos 	return FALSE;
   8847  1.1  christos 
   8848  1.1  christos       if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
   8849  1.1  christos 	info->flags |= DF_TEXTREL;
   8850  1.1  christos 
   8851  1.1  christos       if ((info->flags & DF_TEXTREL) != 0)
   8852  1.1  christos 	{
   8853  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
   8854  1.1  christos 	    return FALSE;
   8855  1.1  christos 
   8856  1.1  christos 	  /* Clear the DF_TEXTREL flag.  It will be set again if we
   8857  1.1  christos 	     write out an actual text relocation; we may not, because
   8858  1.1  christos 	     at this point we do not know whether e.g. any .eh_frame
   8859  1.1  christos 	     absolute relocations have been converted to PC-relative.  */
   8860  1.1  christos 	  info->flags &= ~DF_TEXTREL;
   8861  1.1  christos 	}
   8862  1.1  christos 
   8863  1.1  christos       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
   8864  1.1  christos 	return FALSE;
   8865  1.1  christos 
   8866  1.1  christos       sreldyn = mips_elf_rel_dyn_section (info, FALSE);
   8867  1.1  christos       if (htab->is_vxworks)
   8868  1.1  christos 	{
   8869  1.1  christos 	  /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
   8870  1.1  christos 	     use any of the DT_MIPS_* tags.  */
   8871  1.1  christos 	  if (sreldyn && sreldyn->size > 0)
   8872  1.1  christos 	    {
   8873  1.1  christos 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
   8874  1.1  christos 		return FALSE;
   8875  1.1  christos 
   8876  1.1  christos 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
   8877  1.1  christos 		return FALSE;
   8878  1.1  christos 
   8879  1.1  christos 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
   8880  1.1  christos 		return FALSE;
   8881  1.1  christos 	    }
   8882  1.1  christos 	}
   8883  1.1  christos       else
   8884  1.1  christos 	{
   8885  1.1  christos 	  if (sreldyn && sreldyn->size > 0)
   8886  1.1  christos 	    {
   8887  1.1  christos 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
   8888  1.1  christos 		return FALSE;
   8889  1.1  christos 
   8890  1.1  christos 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
   8891  1.1  christos 		return FALSE;
   8892  1.1  christos 
   8893  1.1  christos 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
   8894  1.1  christos 		return FALSE;
   8895  1.1  christos 	    }
   8896  1.1  christos 
   8897  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
   8898  1.1  christos 	    return FALSE;
   8899  1.1  christos 
   8900  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
   8901  1.1  christos 	    return FALSE;
   8902  1.1  christos 
   8903  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
   8904  1.1  christos 	    return FALSE;
   8905  1.1  christos 
   8906  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
   8907  1.1  christos 	    return FALSE;
   8908  1.1  christos 
   8909  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
   8910  1.1  christos 	    return FALSE;
   8911  1.1  christos 
   8912  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
   8913  1.1  christos 	    return FALSE;
   8914  1.1  christos 
   8915  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
   8916  1.1  christos 	    return FALSE;
   8917  1.1  christos 
   8918  1.1  christos 	  if (IRIX_COMPAT (dynobj) == ict_irix5
   8919  1.1  christos 	      && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
   8920  1.1  christos 	    return FALSE;
   8921  1.1  christos 
   8922  1.1  christos 	  if (IRIX_COMPAT (dynobj) == ict_irix6
   8923  1.1  christos 	      && (bfd_get_section_by_name
   8924  1.1  christos 		  (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
   8925  1.1  christos 	      && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
   8926  1.1  christos 	    return FALSE;
   8927  1.1  christos 	}
   8928  1.1  christos       if (htab->splt->size > 0)
   8929  1.1  christos 	{
   8930  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
   8931  1.1  christos 	    return FALSE;
   8932  1.1  christos 
   8933  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
   8934  1.1  christos 	    return FALSE;
   8935  1.1  christos 
   8936  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
   8937  1.1  christos 	    return FALSE;
   8938  1.1  christos 
   8939  1.1  christos 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
   8940  1.1  christos 	    return FALSE;
   8941  1.1  christos 	}
   8942  1.1  christos       if (htab->is_vxworks
   8943  1.1  christos 	  && !elf_vxworks_add_dynamic_entries (output_bfd, info))
   8944  1.1  christos 	return FALSE;
   8945  1.1  christos     }
   8946  1.1  christos 
   8947  1.1  christos   return TRUE;
   8948  1.1  christos }
   8949  1.1  christos 
   8950  1.1  christos /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
   8952  1.1  christos    Adjust its R_ADDEND field so that it is correct for the output file.
   8953  1.1  christos    LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
   8954  1.1  christos    and sections respectively; both use symbol indexes.  */
   8955  1.1  christos 
   8956  1.1  christos static void
   8957  1.1  christos mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
   8958  1.1  christos 			bfd *input_bfd, Elf_Internal_Sym *local_syms,
   8959  1.1  christos 			asection **local_sections, Elf_Internal_Rela *rel)
   8960  1.1  christos {
   8961  1.1  christos   unsigned int r_type, r_symndx;
   8962  1.1  christos   Elf_Internal_Sym *sym;
   8963  1.1  christos   asection *sec;
   8964  1.1  christos 
   8965  1.1  christos   if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
   8966  1.1  christos     {
   8967  1.1  christos       r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   8968  1.1  christos       if (r_type == R_MIPS16_GPREL
   8969  1.1  christos 	  || r_type == R_MIPS_GPREL16
   8970  1.1  christos 	  || r_type == R_MIPS_GPREL32
   8971  1.1  christos 	  || r_type == R_MIPS_LITERAL)
   8972  1.1  christos 	{
   8973  1.1  christos 	  rel->r_addend += _bfd_get_gp_value (input_bfd);
   8974  1.1  christos 	  rel->r_addend -= _bfd_get_gp_value (output_bfd);
   8975  1.1  christos 	}
   8976  1.1  christos 
   8977  1.1  christos       r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
   8978  1.1  christos       sym = local_syms + r_symndx;
   8979  1.1  christos 
   8980  1.1  christos       /* Adjust REL's addend to account for section merging.  */
   8981  1.1  christos       if (!info->relocatable)
   8982  1.1  christos 	{
   8983  1.1  christos 	  sec = local_sections[r_symndx];
   8984  1.1  christos 	  _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   8985  1.1  christos 	}
   8986  1.1  christos 
   8987  1.1  christos       /* This would normally be done by the rela_normal code in elflink.c.  */
   8988  1.1  christos       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   8989  1.1  christos 	rel->r_addend += local_sections[r_symndx]->output_offset;
   8990  1.1  christos     }
   8991  1.1  christos }
   8992  1.1  christos 
   8993  1.1  christos /* Relocate a MIPS ELF section.  */
   8994  1.1  christos 
   8995  1.1  christos bfd_boolean
   8996  1.1  christos _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
   8997  1.1  christos 				bfd *input_bfd, asection *input_section,
   8998  1.1  christos 				bfd_byte *contents, Elf_Internal_Rela *relocs,
   8999  1.1  christos 				Elf_Internal_Sym *local_syms,
   9000  1.1  christos 				asection **local_sections)
   9001  1.1  christos {
   9002  1.1  christos   Elf_Internal_Rela *rel;
   9003  1.1  christos   const Elf_Internal_Rela *relend;
   9004  1.1  christos   bfd_vma addend = 0;
   9005  1.1  christos   bfd_boolean use_saved_addend_p = FALSE;
   9006  1.1  christos   const struct elf_backend_data *bed;
   9007  1.1  christos 
   9008  1.1  christos   bed = get_elf_backend_data (output_bfd);
   9009  1.1  christos   relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
   9010  1.1  christos   for (rel = relocs; rel < relend; ++rel)
   9011  1.1  christos     {
   9012  1.1  christos       const char *name;
   9013  1.1  christos       bfd_vma value = 0;
   9014  1.1  christos       reloc_howto_type *howto;
   9015  1.1  christos       bfd_boolean cross_mode_jump_p;
   9016  1.1  christos       /* TRUE if the relocation is a RELA relocation, rather than a
   9017  1.1  christos          REL relocation.  */
   9018  1.1  christos       bfd_boolean rela_relocation_p = TRUE;
   9019  1.1  christos       unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
   9020  1.1  christos       const char *msg;
   9021  1.1  christos       unsigned long r_symndx;
   9022  1.1  christos       asection *sec;
   9023  1.1  christos       Elf_Internal_Shdr *symtab_hdr;
   9024  1.1  christos       struct elf_link_hash_entry *h;
   9025  1.1  christos       bfd_boolean rel_reloc;
   9026  1.1  christos 
   9027  1.1  christos       rel_reloc = (NEWABI_P (input_bfd)
   9028  1.1  christos 		   && mips_elf_rel_relocation_p (input_bfd, input_section,
   9029  1.1  christos 						 relocs, rel));
   9030  1.1  christos       /* Find the relocation howto for this relocation.  */
   9031  1.1  christos       howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
   9032  1.1  christos 
   9033  1.1  christos       r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
   9034  1.1  christos       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   9035  1.1  christos       if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
   9036  1.1  christos 	{
   9037  1.1  christos 	  sec = local_sections[r_symndx];
   9038  1.1  christos 	  h = NULL;
   9039  1.1  christos 	}
   9040  1.1  christos       else
   9041  1.1  christos 	{
   9042  1.1  christos 	  unsigned long extsymoff;
   9043  1.1  christos 
   9044  1.1  christos 	  extsymoff = 0;
   9045  1.1  christos 	  if (!elf_bad_symtab (input_bfd))
   9046  1.1  christos 	    extsymoff = symtab_hdr->sh_info;
   9047  1.1  christos 	  h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
   9048  1.1  christos 	  while (h->root.type == bfd_link_hash_indirect
   9049  1.1  christos 		 || h->root.type == bfd_link_hash_warning)
   9050  1.1  christos 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   9051  1.1  christos 
   9052  1.1  christos 	  sec = NULL;
   9053  1.1  christos 	  if (h->root.type == bfd_link_hash_defined
   9054  1.1  christos 	      || h->root.type == bfd_link_hash_defweak)
   9055  1.1  christos 	    sec = h->root.u.def.section;
   9056  1.1  christos 	}
   9057  1.1  christos 
   9058  1.1  christos       if (sec != NULL && elf_discarded_section (sec))
   9059  1.1  christos 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   9060  1.1  christos 					 rel, relend, howto, contents);
   9061  1.1  christos 
   9062  1.1  christos       if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
   9063  1.1  christos 	{
   9064  1.1  christos 	  /* Some 32-bit code uses R_MIPS_64.  In particular, people use
   9065  1.1  christos 	     64-bit code, but make sure all their addresses are in the
   9066  1.1  christos 	     lowermost or uppermost 32-bit section of the 64-bit address
   9067  1.1  christos 	     space.  Thus, when they use an R_MIPS_64 they mean what is
   9068  1.1  christos 	     usually meant by R_MIPS_32, with the exception that the
   9069  1.1  christos 	     stored value is sign-extended to 64 bits.  */
   9070  1.1  christos 	  howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
   9071  1.1  christos 
   9072  1.1  christos 	  /* On big-endian systems, we need to lie about the position
   9073  1.1  christos 	     of the reloc.  */
   9074  1.1  christos 	  if (bfd_big_endian (input_bfd))
   9075  1.1  christos 	    rel->r_offset += 4;
   9076  1.1  christos 	}
   9077  1.1  christos 
   9078  1.1  christos       if (!use_saved_addend_p)
   9079  1.1  christos 	{
   9080  1.1  christos 	  /* If these relocations were originally of the REL variety,
   9081  1.1  christos 	     we must pull the addend out of the field that will be
   9082  1.1  christos 	     relocated.  Otherwise, we simply use the contents of the
   9083  1.1  christos 	     RELA relocation.  */
   9084  1.1  christos 	  if (mips_elf_rel_relocation_p (input_bfd, input_section,
   9085  1.1  christos 					 relocs, rel))
   9086  1.1  christos 	    {
   9087  1.1  christos 	      rela_relocation_p = FALSE;
   9088  1.1  christos 	      addend = mips_elf_read_rel_addend (input_bfd, rel,
   9089  1.1  christos 						 howto, contents);
   9090  1.1  christos 	      if (hi16_reloc_p (r_type)
   9091  1.1  christos 		  || (got16_reloc_p (r_type)
   9092  1.1  christos 		      && mips_elf_local_relocation_p (input_bfd, rel,
   9093  1.1  christos 						      local_sections)))
   9094  1.1  christos 		{
   9095  1.1  christos 		  if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
   9096  1.1  christos 						     contents, &addend))
   9097  1.1  christos 		    {
   9098  1.1  christos 		      if (h)
   9099  1.1  christos 			name = h->root.root.string;
   9100  1.1  christos 		      else
   9101  1.1  christos 			name = bfd_elf_sym_name (input_bfd, symtab_hdr,
   9102  1.1  christos 						 local_syms + r_symndx,
   9103  1.1  christos 						 sec);
   9104  1.1  christos 		      (*_bfd_error_handler)
   9105  1.1  christos 			(_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
   9106  1.1  christos 			 input_bfd, input_section, name, howto->name,
   9107  1.1  christos 			 rel->r_offset);
   9108  1.1  christos 		    }
   9109  1.1  christos 		}
   9110  1.1  christos 	      else
   9111  1.1  christos 		addend <<= howto->rightshift;
   9112  1.1  christos 	    }
   9113  1.1  christos 	  else
   9114  1.1  christos 	    addend = rel->r_addend;
   9115  1.1  christos 	  mips_elf_adjust_addend (output_bfd, info, input_bfd,
   9116  1.1  christos 				  local_syms, local_sections, rel);
   9117  1.1  christos 	}
   9118  1.1  christos 
   9119  1.1  christos       if (info->relocatable)
   9120  1.1  christos 	{
   9121  1.1  christos 	  if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
   9122  1.1  christos 	      && bfd_big_endian (input_bfd))
   9123  1.1  christos 	    rel->r_offset -= 4;
   9124  1.1  christos 
   9125  1.1  christos 	  if (!rela_relocation_p && rel->r_addend)
   9126  1.1  christos 	    {
   9127  1.1  christos 	      addend += rel->r_addend;
   9128  1.1  christos 	      if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
   9129  1.1  christos 		addend = mips_elf_high (addend);
   9130  1.1  christos 	      else if (r_type == R_MIPS_HIGHER)
   9131  1.1  christos 		addend = mips_elf_higher (addend);
   9132  1.1  christos 	      else if (r_type == R_MIPS_HIGHEST)
   9133  1.1  christos 		addend = mips_elf_highest (addend);
   9134  1.1  christos 	      else
   9135  1.1  christos 		addend >>= howto->rightshift;
   9136  1.1  christos 
   9137  1.1  christos 	      /* We use the source mask, rather than the destination
   9138  1.1  christos 		 mask because the place to which we are writing will be
   9139  1.1  christos 		 source of the addend in the final link.  */
   9140  1.1  christos 	      addend &= howto->src_mask;
   9141  1.1  christos 
   9142  1.1  christos 	      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
   9143  1.1  christos 		/* See the comment above about using R_MIPS_64 in the 32-bit
   9144  1.1  christos 		   ABI.  Here, we need to update the addend.  It would be
   9145  1.1  christos 		   possible to get away with just using the R_MIPS_32 reloc
   9146  1.1  christos 		   but for endianness.  */
   9147  1.1  christos 		{
   9148  1.1  christos 		  bfd_vma sign_bits;
   9149  1.1  christos 		  bfd_vma low_bits;
   9150  1.1  christos 		  bfd_vma high_bits;
   9151  1.1  christos 
   9152  1.1  christos 		  if (addend & ((bfd_vma) 1 << 31))
   9153  1.1  christos #ifdef BFD64
   9154  1.1  christos 		    sign_bits = ((bfd_vma) 1 << 32) - 1;
   9155  1.1  christos #else
   9156  1.1  christos 		    sign_bits = -1;
   9157  1.1  christos #endif
   9158  1.1  christos 		  else
   9159  1.1  christos 		    sign_bits = 0;
   9160  1.1  christos 
   9161  1.1  christos 		  /* If we don't know that we have a 64-bit type,
   9162  1.1  christos 		     do two separate stores.  */
   9163  1.1  christos 		  if (bfd_big_endian (input_bfd))
   9164  1.1  christos 		    {
   9165  1.1  christos 		      /* Store the sign-bits (which are most significant)
   9166  1.1  christos 			 first.  */
   9167  1.1  christos 		      low_bits = sign_bits;
   9168  1.1  christos 		      high_bits = addend;
   9169  1.1  christos 		    }
   9170  1.1  christos 		  else
   9171  1.1  christos 		    {
   9172  1.1  christos 		      low_bits = addend;
   9173  1.1  christos 		      high_bits = sign_bits;
   9174  1.1  christos 		    }
   9175  1.1  christos 		  bfd_put_32 (input_bfd, low_bits,
   9176  1.1  christos 			      contents + rel->r_offset);
   9177  1.1  christos 		  bfd_put_32 (input_bfd, high_bits,
   9178  1.1  christos 			      contents + rel->r_offset + 4);
   9179  1.1  christos 		  continue;
   9180  1.1  christos 		}
   9181  1.1  christos 
   9182  1.1  christos 	      if (! mips_elf_perform_relocation (info, howto, rel, addend,
   9183  1.1  christos 						 input_bfd, input_section,
   9184  1.1  christos 						 contents, FALSE))
   9185  1.1  christos 		return FALSE;
   9186  1.1  christos 	    }
   9187  1.1  christos 
   9188  1.1  christos 	  /* Go on to the next relocation.  */
   9189  1.1  christos 	  continue;
   9190  1.1  christos 	}
   9191  1.1  christos 
   9192  1.1  christos       /* In the N32 and 64-bit ABIs there may be multiple consecutive
   9193  1.1  christos 	 relocations for the same offset.  In that case we are
   9194  1.1  christos 	 supposed to treat the output of each relocation as the addend
   9195  1.1  christos 	 for the next.  */
   9196  1.1  christos       if (rel + 1 < relend
   9197  1.1  christos 	  && rel->r_offset == rel[1].r_offset
   9198  1.1  christos 	  && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
   9199  1.1  christos 	use_saved_addend_p = TRUE;
   9200  1.1  christos       else
   9201  1.1  christos 	use_saved_addend_p = FALSE;
   9202  1.1  christos 
   9203  1.1  christos       /* Figure out what value we are supposed to relocate.  */
   9204  1.1  christos       switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
   9205  1.1  christos 					     input_section, info, rel,
   9206  1.1  christos 					     addend, howto, local_syms,
   9207  1.1  christos 					     local_sections, &value,
   9208  1.1  christos 					     &name, &cross_mode_jump_p,
   9209  1.1  christos 					     use_saved_addend_p))
   9210  1.1  christos 	{
   9211  1.1  christos 	case bfd_reloc_continue:
   9212  1.1  christos 	  /* There's nothing to do.  */
   9213  1.1  christos 	  continue;
   9214  1.1  christos 
   9215  1.1  christos 	case bfd_reloc_undefined:
   9216  1.1  christos 	  /* mips_elf_calculate_relocation already called the
   9217  1.1  christos 	     undefined_symbol callback.  There's no real point in
   9218  1.1  christos 	     trying to perform the relocation at this point, so we
   9219  1.1  christos 	     just skip ahead to the next relocation.  */
   9220  1.1  christos 	  continue;
   9221  1.1  christos 
   9222  1.1  christos 	case bfd_reloc_notsupported:
   9223  1.1  christos 	  msg = _("internal error: unsupported relocation error");
   9224  1.1  christos 	  info->callbacks->warning
   9225  1.1  christos 	    (info, msg, name, input_bfd, input_section, rel->r_offset);
   9226  1.1  christos 	  return FALSE;
   9227  1.1  christos 
   9228  1.1  christos 	case bfd_reloc_overflow:
   9229  1.1  christos 	  if (use_saved_addend_p)
   9230  1.1  christos 	    /* Ignore overflow until we reach the last relocation for
   9231  1.1  christos 	       a given location.  */
   9232  1.1  christos 	    ;
   9233  1.1  christos 	  else
   9234  1.1  christos 	    {
   9235  1.1  christos 	      struct mips_elf_link_hash_table *htab;
   9236  1.1  christos 
   9237  1.1  christos 	      htab = mips_elf_hash_table (info);
   9238  1.1  christos 	      BFD_ASSERT (htab != NULL);
   9239  1.1  christos 	      BFD_ASSERT (name != NULL);
   9240  1.1  christos 	      if (!htab->small_data_overflow_reported
   9241  1.1  christos 		  && (gprel16_reloc_p (howto->type)
   9242  1.1  christos 		      || howto->type == R_MIPS_LITERAL))
   9243  1.1  christos 		{
   9244  1.1  christos 		  msg = _("small-data section exceeds 64KB;"
   9245  1.1  christos 			  " lower small-data size limit (see option -G)");
   9246  1.1  christos 
   9247  1.1  christos 		  htab->small_data_overflow_reported = TRUE;
   9248  1.1  christos 		  (*info->callbacks->einfo) ("%P: %s\n", msg);
   9249  1.1  christos 		}
   9250  1.1  christos 	      if (! ((*info->callbacks->reloc_overflow)
   9251  1.1  christos 		     (info, NULL, name, howto->name, (bfd_vma) 0,
   9252  1.1  christos 		      input_bfd, input_section, rel->r_offset)))
   9253  1.1  christos 		return FALSE;
   9254  1.1  christos 	    }
   9255  1.1  christos 	  break;
   9256  1.1  christos 
   9257  1.1  christos 	case bfd_reloc_ok:
   9258  1.1  christos 	  break;
   9259  1.1  christos 
   9260  1.1  christos 	default:
   9261  1.1  christos 	  abort ();
   9262  1.1  christos 	  break;
   9263  1.1  christos 	}
   9264  1.1  christos 
   9265  1.1  christos       /* If we've got another relocation for the address, keep going
   9266  1.1  christos 	 until we reach the last one.  */
   9267  1.1  christos       if (use_saved_addend_p)
   9268  1.1  christos 	{
   9269  1.1  christos 	  addend = value;
   9270  1.1  christos 	  continue;
   9271  1.1  christos 	}
   9272  1.1  christos 
   9273  1.1  christos       if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
   9274  1.1  christos 	/* See the comment above about using R_MIPS_64 in the 32-bit
   9275  1.1  christos 	   ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
   9276  1.1  christos 	   that calculated the right value.  Now, however, we
   9277  1.1  christos 	   sign-extend the 32-bit result to 64-bits, and store it as a
   9278  1.1  christos 	   64-bit value.  We are especially generous here in that we
   9279  1.1  christos 	   go to extreme lengths to support this usage on systems with
   9280  1.1  christos 	   only a 32-bit VMA.  */
   9281  1.1  christos 	{
   9282  1.1  christos 	  bfd_vma sign_bits;
   9283  1.1  christos 	  bfd_vma low_bits;
   9284  1.1  christos 	  bfd_vma high_bits;
   9285  1.1  christos 
   9286  1.1  christos 	  if (value & ((bfd_vma) 1 << 31))
   9287  1.1  christos #ifdef BFD64
   9288  1.1  christos 	    sign_bits = ((bfd_vma) 1 << 32) - 1;
   9289  1.1  christos #else
   9290  1.1  christos 	    sign_bits = -1;
   9291  1.1  christos #endif
   9292  1.1  christos 	  else
   9293  1.1  christos 	    sign_bits = 0;
   9294  1.1  christos 
   9295  1.1  christos 	  /* If we don't know that we have a 64-bit type,
   9296  1.1  christos 	     do two separate stores.  */
   9297  1.1  christos 	  if (bfd_big_endian (input_bfd))
   9298  1.1  christos 	    {
   9299  1.1  christos 	      /* Undo what we did above.  */
   9300  1.1  christos 	      rel->r_offset -= 4;
   9301  1.1  christos 	      /* Store the sign-bits (which are most significant)
   9302  1.1  christos 		 first.  */
   9303  1.1  christos 	      low_bits = sign_bits;
   9304  1.1  christos 	      high_bits = value;
   9305  1.1  christos 	    }
   9306  1.1  christos 	  else
   9307  1.1  christos 	    {
   9308  1.1  christos 	      low_bits = value;
   9309  1.1  christos 	      high_bits = sign_bits;
   9310  1.1  christos 	    }
   9311  1.1  christos 	  bfd_put_32 (input_bfd, low_bits,
   9312  1.1  christos 		      contents + rel->r_offset);
   9313  1.1  christos 	  bfd_put_32 (input_bfd, high_bits,
   9314  1.1  christos 		      contents + rel->r_offset + 4);
   9315  1.1  christos 	  continue;
   9316  1.1  christos 	}
   9317  1.1  christos 
   9318  1.1  christos       /* Actually perform the relocation.  */
   9319  1.1  christos       if (! mips_elf_perform_relocation (info, howto, rel, value,
   9320  1.1  christos 					 input_bfd, input_section,
   9321  1.1  christos 					 contents, cross_mode_jump_p))
   9322  1.1  christos 	return FALSE;
   9323  1.1  christos     }
   9324  1.1  christos 
   9325  1.1  christos   return TRUE;
   9326  1.1  christos }
   9327  1.1  christos 
   9328  1.1  christos /* A function that iterates over each entry in la25_stubs and fills
   9330  1.1  christos    in the code for each one.  DATA points to a mips_htab_traverse_info.  */
   9331  1.1  christos 
   9332  1.1  christos static int
   9333  1.1  christos mips_elf_create_la25_stub (void **slot, void *data)
   9334  1.1  christos {
   9335  1.1  christos   struct mips_htab_traverse_info *hti;
   9336  1.1  christos   struct mips_elf_link_hash_table *htab;
   9337  1.1  christos   struct mips_elf_la25_stub *stub;
   9338  1.1  christos   asection *s;
   9339  1.1  christos   bfd_byte *loc;
   9340  1.1  christos   bfd_vma offset, target, target_high, target_low;
   9341  1.1  christos 
   9342  1.1  christos   stub = (struct mips_elf_la25_stub *) *slot;
   9343  1.1  christos   hti = (struct mips_htab_traverse_info *) data;
   9344  1.1  christos   htab = mips_elf_hash_table (hti->info);
   9345  1.1  christos   BFD_ASSERT (htab != NULL);
   9346  1.1  christos 
   9347  1.1  christos   /* Create the section contents, if we haven't already.  */
   9348  1.1  christos   s = stub->stub_section;
   9349  1.1  christos   loc = s->contents;
   9350  1.1  christos   if (loc == NULL)
   9351  1.1  christos     {
   9352  1.1  christos       loc = bfd_malloc (s->size);
   9353  1.1  christos       if (loc == NULL)
   9354  1.1  christos 	{
   9355  1.1  christos 	  hti->error = TRUE;
   9356  1.1  christos 	  return FALSE;
   9357  1.1  christos 	}
   9358  1.1  christos       s->contents = loc;
   9359  1.1  christos     }
   9360  1.1  christos 
   9361  1.1  christos   /* Work out where in the section this stub should go.  */
   9362  1.1  christos   offset = stub->offset;
   9363  1.1  christos 
   9364  1.1  christos   /* Work out the target address.  */
   9365  1.1  christos   target = (stub->h->root.root.u.def.section->output_section->vma
   9366  1.1  christos 	    + stub->h->root.root.u.def.section->output_offset
   9367  1.1  christos 	    + stub->h->root.root.u.def.value);
   9368  1.1  christos   target_high = ((target + 0x8000) >> 16) & 0xffff;
   9369  1.1  christos   target_low = (target & 0xffff);
   9370  1.1  christos 
   9371  1.1  christos   if (stub->stub_section != htab->strampoline)
   9372  1.1  christos     {
   9373  1.1  christos       /* This is a simple LUI/ADIDU stub.  Zero out the beginning
   9374  1.1  christos 	 of the section and write the two instructions at the end.  */
   9375  1.1  christos       memset (loc, 0, offset);
   9376  1.1  christos       loc += offset;
   9377  1.1  christos       bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
   9378  1.1  christos       bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
   9379  1.1  christos     }
   9380  1.1  christos   else
   9381  1.1  christos     {
   9382  1.1  christos       /* This is trampoline.  */
   9383  1.1  christos       loc += offset;
   9384  1.1  christos       bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
   9385  1.1  christos       bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
   9386  1.1  christos       bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
   9387  1.1  christos       bfd_put_32 (hti->output_bfd, 0, loc + 12);
   9388  1.1  christos     }
   9389  1.1  christos   return TRUE;
   9390  1.1  christos }
   9391  1.1  christos 
   9392  1.1  christos /* If NAME is one of the special IRIX6 symbols defined by the linker,
   9393  1.1  christos    adjust it appropriately now.  */
   9394  1.1  christos 
   9395  1.1  christos static void
   9396  1.1  christos mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
   9397  1.1  christos 				      const char *name, Elf_Internal_Sym *sym)
   9398  1.1  christos {
   9399  1.1  christos   /* The linker script takes care of providing names and values for
   9400  1.1  christos      these, but we must place them into the right sections.  */
   9401  1.1  christos   static const char* const text_section_symbols[] = {
   9402  1.1  christos     "_ftext",
   9403  1.1  christos     "_etext",
   9404  1.1  christos     "__dso_displacement",
   9405  1.1  christos     "__elf_header",
   9406  1.1  christos     "__program_header_table",
   9407  1.1  christos     NULL
   9408  1.1  christos   };
   9409  1.1  christos 
   9410  1.1  christos   static const char* const data_section_symbols[] = {
   9411  1.1  christos     "_fdata",
   9412  1.1  christos     "_edata",
   9413  1.1  christos     "_end",
   9414  1.1  christos     "_fbss",
   9415  1.1  christos     NULL
   9416  1.1  christos   };
   9417  1.1  christos 
   9418  1.1  christos   const char* const *p;
   9419  1.1  christos   int i;
   9420  1.1  christos 
   9421  1.1  christos   for (i = 0; i < 2; ++i)
   9422  1.1  christos     for (p = (i == 0) ? text_section_symbols : data_section_symbols;
   9423  1.1  christos 	 *p;
   9424  1.1  christos 	 ++p)
   9425  1.1  christos       if (strcmp (*p, name) == 0)
   9426  1.1  christos 	{
   9427  1.1  christos 	  /* All of these symbols are given type STT_SECTION by the
   9428  1.1  christos 	     IRIX6 linker.  */
   9429  1.1  christos 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   9430  1.1  christos 	  sym->st_other = STO_PROTECTED;
   9431  1.1  christos 
   9432  1.1  christos 	  /* The IRIX linker puts these symbols in special sections.  */
   9433  1.1  christos 	  if (i == 0)
   9434  1.1  christos 	    sym->st_shndx = SHN_MIPS_TEXT;
   9435  1.1  christos 	  else
   9436  1.1  christos 	    sym->st_shndx = SHN_MIPS_DATA;
   9437  1.1  christos 
   9438  1.1  christos 	  break;
   9439  1.1  christos 	}
   9440  1.1  christos }
   9441  1.1  christos 
   9442  1.1  christos /* Finish up dynamic symbol handling.  We set the contents of various
   9443  1.1  christos    dynamic sections here.  */
   9444  1.1  christos 
   9445  1.1  christos bfd_boolean
   9446  1.1  christos _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
   9447  1.1  christos 				     struct bfd_link_info *info,
   9448  1.1  christos 				     struct elf_link_hash_entry *h,
   9449  1.1  christos 				     Elf_Internal_Sym *sym)
   9450  1.1  christos {
   9451  1.1  christos   bfd *dynobj;
   9452  1.1  christos   asection *sgot;
   9453  1.1  christos   struct mips_got_info *g, *gg;
   9454  1.1  christos   const char *name;
   9455  1.1  christos   int idx;
   9456  1.1  christos   struct mips_elf_link_hash_table *htab;
   9457  1.1  christos   struct mips_elf_link_hash_entry *hmips;
   9458  1.1  christos 
   9459  1.1  christos   htab = mips_elf_hash_table (info);
   9460  1.1  christos   BFD_ASSERT (htab != NULL);
   9461  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   9462  1.1  christos   hmips = (struct mips_elf_link_hash_entry *) h;
   9463  1.1  christos 
   9464  1.1  christos   BFD_ASSERT (!htab->is_vxworks);
   9465  1.1  christos 
   9466  1.1  christos   if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
   9467  1.1  christos     {
   9468  1.1  christos       /* We've decided to create a PLT entry for this symbol.  */
   9469  1.1  christos       bfd_byte *loc;
   9470  1.1  christos       bfd_vma header_address, plt_index, got_address;
   9471  1.1  christos       bfd_vma got_address_high, got_address_low, load;
   9472  1.1  christos       const bfd_vma *plt_entry;
   9473  1.1  christos 
   9474  1.1  christos       BFD_ASSERT (htab->use_plts_and_copy_relocs);
   9475  1.1  christos       BFD_ASSERT (h->dynindx != -1);
   9476  1.1  christos       BFD_ASSERT (htab->splt != NULL);
   9477  1.1  christos       BFD_ASSERT (h->plt.offset <= htab->splt->size);
   9478  1.1  christos       BFD_ASSERT (!h->def_regular);
   9479  1.1  christos 
   9480  1.1  christos       /* Calculate the address of the PLT header.  */
   9481  1.1  christos       header_address = (htab->splt->output_section->vma
   9482  1.1  christos 			+ htab->splt->output_offset);
   9483  1.1  christos 
   9484  1.1  christos       /* Calculate the index of the entry.  */
   9485  1.1  christos       plt_index = ((h->plt.offset - htab->plt_header_size)
   9486  1.1  christos 		   / htab->plt_entry_size);
   9487  1.1  christos 
   9488  1.1  christos       /* Calculate the address of the .got.plt entry.  */
   9489  1.1  christos       got_address = (htab->sgotplt->output_section->vma
   9490  1.1  christos 		     + htab->sgotplt->output_offset
   9491  1.1  christos 		     + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
   9492  1.1  christos       got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
   9493  1.1  christos       got_address_low = got_address & 0xffff;
   9494  1.1  christos 
   9495  1.1  christos       /* Initially point the .got.plt entry at the PLT header.  */
   9496  1.1  christos       loc = (htab->sgotplt->contents
   9497  1.1  christos 	     + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
   9498  1.1  christos       if (ABI_64_P (output_bfd))
   9499  1.1  christos 	bfd_put_64 (output_bfd, header_address, loc);
   9500  1.1  christos       else
   9501  1.1  christos 	bfd_put_32 (output_bfd, header_address, loc);
   9502  1.1  christos 
   9503  1.1  christos       /* Find out where the .plt entry should go.  */
   9504  1.1  christos       loc = htab->splt->contents + h->plt.offset;
   9505  1.1  christos 
   9506  1.1  christos       /* Pick the load opcode.  */
   9507  1.1  christos       load = MIPS_ELF_LOAD_WORD (output_bfd);
   9508  1.1  christos 
   9509  1.1  christos       /* Fill in the PLT entry itself.  */
   9510  1.1  christos       plt_entry = mips_exec_plt_entry;
   9511  1.1  christos       bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
   9512  1.1  christos       bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
   9513  1.1  christos 
   9514  1.1  christos       if (! LOAD_INTERLOCKS_P (output_bfd))
   9515  1.1  christos 	{
   9516  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
   9517  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   9518  1.1  christos 	}
   9519  1.1  christos       else
   9520  1.1  christos 	{
   9521  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
   9522  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
   9523  1.1  christos 	}
   9524  1.1  christos 
   9525  1.1  christos       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
   9526  1.1  christos       mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
   9527  1.1  christos 					  plt_index, h->dynindx,
   9528  1.1  christos 					  R_MIPS_JUMP_SLOT, got_address);
   9529  1.1  christos 
   9530  1.1  christos       /* We distinguish between PLT entries and lazy-binding stubs by
   9531  1.1  christos 	 giving the former an st_other value of STO_MIPS_PLT.  Set the
   9532  1.1  christos 	 flag and leave the value if there are any relocations in the
   9533  1.1  christos 	 binary where pointer equality matters.  */
   9534  1.1  christos       sym->st_shndx = SHN_UNDEF;
   9535  1.1  christos       if (h->pointer_equality_needed)
   9536  1.1  christos 	sym->st_other = STO_MIPS_PLT;
   9537  1.1  christos       else
   9538  1.1  christos 	sym->st_value = 0;
   9539  1.1  christos     }
   9540  1.1  christos   else if (h->plt.offset != MINUS_ONE)
   9541  1.1  christos     {
   9542  1.1  christos       /* We've decided to create a lazy-binding stub.  */
   9543  1.1  christos       bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
   9544  1.1  christos 
   9545  1.1  christos       /* This symbol has a stub.  Set it up.  */
   9546  1.1  christos 
   9547  1.1  christos       BFD_ASSERT (h->dynindx != -1);
   9548  1.1  christos 
   9549  1.1  christos       BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
   9550  1.1  christos                   || (h->dynindx <= 0xffff));
   9551  1.1  christos 
   9552  1.1  christos       /* Values up to 2^31 - 1 are allowed.  Larger values would cause
   9553  1.1  christos 	 sign extension at runtime in the stub, resulting in a negative
   9554  1.1  christos 	 index value.  */
   9555  1.1  christos       if (h->dynindx & ~0x7fffffff)
   9556  1.1  christos 	return FALSE;
   9557  1.1  christos 
   9558  1.1  christos       /* Fill the stub.  */
   9559  1.1  christos       idx = 0;
   9560  1.1  christos       bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
   9561  1.1  christos       idx += 4;
   9562  1.1  christos       bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
   9563  1.1  christos       idx += 4;
   9564  1.1  christos       if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
   9565  1.1  christos         {
   9566  1.1  christos           bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
   9567  1.1  christos                       stub + idx);
   9568  1.1  christos           idx += 4;
   9569  1.1  christos         }
   9570  1.1  christos       bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
   9571  1.1  christos       idx += 4;
   9572  1.1  christos 
   9573  1.1  christos       /* If a large stub is not required and sign extension is not a
   9574  1.1  christos          problem, then use legacy code in the stub.  */
   9575  1.1  christos       if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
   9576  1.1  christos 	bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
   9577  1.1  christos       else if (h->dynindx & ~0x7fff)
   9578  1.1  christos         bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
   9579  1.1  christos       else
   9580  1.1  christos         bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
   9581  1.1  christos 		    stub + idx);
   9582  1.1  christos 
   9583  1.1  christos       BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
   9584  1.1  christos       memcpy (htab->sstubs->contents + h->plt.offset,
   9585  1.1  christos 	      stub, htab->function_stub_size);
   9586  1.1  christos 
   9587  1.1  christos       /* Mark the symbol as undefined.  plt.offset != -1 occurs
   9588  1.1  christos 	 only for the referenced symbol.  */
   9589  1.1  christos       sym->st_shndx = SHN_UNDEF;
   9590  1.1  christos 
   9591  1.1  christos       /* The run-time linker uses the st_value field of the symbol
   9592  1.1  christos 	 to reset the global offset table entry for this external
   9593  1.1  christos 	 to its stub address when unlinking a shared object.  */
   9594  1.1  christos       sym->st_value = (htab->sstubs->output_section->vma
   9595  1.1  christos 		       + htab->sstubs->output_offset
   9596  1.1  christos 		       + h->plt.offset);
   9597  1.1  christos     }
   9598  1.1  christos 
   9599  1.1  christos   /* If we have a MIPS16 function with a stub, the dynamic symbol must
   9600  1.1  christos      refer to the stub, since only the stub uses the standard calling
   9601  1.1  christos      conventions.  */
   9602  1.1  christos   if (h->dynindx != -1 && hmips->fn_stub != NULL)
   9603  1.1  christos     {
   9604  1.1  christos       BFD_ASSERT (hmips->need_fn_stub);
   9605  1.1  christos       sym->st_value = (hmips->fn_stub->output_section->vma
   9606  1.1  christos 		       + hmips->fn_stub->output_offset);
   9607  1.1  christos       sym->st_size = hmips->fn_stub->size;
   9608  1.1  christos       sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
   9609  1.1  christos     }
   9610  1.1  christos 
   9611  1.1  christos   BFD_ASSERT (h->dynindx != -1
   9612  1.1  christos 	      || h->forced_local);
   9613  1.1  christos 
   9614  1.1  christos   sgot = htab->sgot;
   9615  1.1  christos   g = htab->got_info;
   9616  1.1  christos   BFD_ASSERT (g != NULL);
   9617  1.1  christos 
   9618  1.1  christos   /* Run through the global symbol table, creating GOT entries for all
   9619  1.1  christos      the symbols that need them.  */
   9620  1.1  christos   if (hmips->global_got_area != GGA_NONE)
   9621  1.1  christos     {
   9622  1.1  christos       bfd_vma offset;
   9623  1.1  christos       bfd_vma value;
   9624  1.1  christos 
   9625  1.1  christos       value = sym->st_value;
   9626  1.1  christos       offset = mips_elf_global_got_index (dynobj, output_bfd, h,
   9627  1.1  christos 					  R_MIPS_GOT16, info);
   9628  1.1  christos       MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
   9629  1.1  christos     }
   9630  1.1  christos 
   9631  1.1  christos   if (hmips->global_got_area != GGA_NONE && g->next && h->type != STT_TLS)
   9632  1.1  christos     {
   9633  1.1  christos       struct mips_got_entry e, *p;
   9634  1.1  christos       bfd_vma entry;
   9635  1.1  christos       bfd_vma offset;
   9636  1.1  christos 
   9637  1.1  christos       gg = g;
   9638  1.1  christos 
   9639  1.1  christos       e.abfd = output_bfd;
   9640  1.1  christos       e.symndx = -1;
   9641  1.1  christos       e.d.h = hmips;
   9642  1.1  christos       e.tls_type = 0;
   9643  1.1  christos 
   9644  1.1  christos       for (g = g->next; g->next != gg; g = g->next)
   9645  1.1  christos 	{
   9646  1.1  christos 	  if (g->got_entries
   9647  1.1  christos 	      && (p = (struct mips_got_entry *) htab_find (g->got_entries,
   9648  1.1  christos 							   &e)))
   9649  1.1  christos 	    {
   9650  1.1  christos 	      offset = p->gotidx;
   9651  1.1  christos 	      if (info->shared
   9652  1.1  christos 		  || (elf_hash_table (info)->dynamic_sections_created
   9653  1.1  christos 		      && p->d.h != NULL
   9654  1.1  christos 		      && p->d.h->root.def_dynamic
   9655  1.1  christos 		      && !p->d.h->root.def_regular))
   9656  1.1  christos 		{
   9657  1.1  christos 		  /* Create an R_MIPS_REL32 relocation for this entry.  Due to
   9658  1.1  christos 		     the various compatibility problems, it's easier to mock
   9659  1.1  christos 		     up an R_MIPS_32 or R_MIPS_64 relocation and leave
   9660  1.1  christos 		     mips_elf_create_dynamic_relocation to calculate the
   9661  1.1  christos 		     appropriate addend.  */
   9662  1.1  christos 		  Elf_Internal_Rela rel[3];
   9663  1.1  christos 
   9664  1.1  christos 		  memset (rel, 0, sizeof (rel));
   9665  1.1  christos 		  if (ABI_64_P (output_bfd))
   9666  1.1  christos 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
   9667  1.1  christos 		  else
   9668  1.1  christos 		    rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
   9669  1.1  christos 		  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
   9670  1.1  christos 
   9671  1.1  christos 		  entry = 0;
   9672  1.1  christos 		  if (! (mips_elf_create_dynamic_relocation
   9673  1.1  christos 			 (output_bfd, info, rel,
   9674  1.1  christos 			  e.d.h, NULL, sym->st_value, &entry, sgot)))
   9675  1.1  christos 		    return FALSE;
   9676  1.1  christos 		}
   9677  1.1  christos 	      else
   9678  1.1  christos 		entry = sym->st_value;
   9679  1.1  christos 	      MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
   9680  1.1  christos 	    }
   9681  1.1  christos 	}
   9682  1.1  christos     }
   9683  1.1  christos 
   9684  1.1  christos   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
   9685  1.1  christos   name = h->root.root.string;
   9686  1.1  christos   if (strcmp (name, "_DYNAMIC") == 0
   9687  1.1  christos       || h == elf_hash_table (info)->hgot)
   9688  1.1  christos     sym->st_shndx = SHN_ABS;
   9689  1.1  christos   else if (strcmp (name, "_DYNAMIC_LINK") == 0
   9690  1.1  christos 	   || strcmp (name, "_DYNAMIC_LINKING") == 0)
   9691  1.1  christos     {
   9692  1.1  christos       sym->st_shndx = SHN_ABS;
   9693  1.1  christos       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   9694  1.1  christos       sym->st_value = 1;
   9695  1.1  christos     }
   9696  1.1  christos   else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
   9697  1.1  christos     {
   9698  1.1  christos       sym->st_shndx = SHN_ABS;
   9699  1.1  christos       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   9700  1.1  christos       sym->st_value = elf_gp (output_bfd);
   9701  1.1  christos     }
   9702  1.1  christos   else if (SGI_COMPAT (output_bfd))
   9703  1.1  christos     {
   9704  1.1  christos       if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
   9705  1.1  christos 	  || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
   9706  1.1  christos 	{
   9707  1.1  christos 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   9708  1.1  christos 	  sym->st_other = STO_PROTECTED;
   9709  1.1  christos 	  sym->st_value = 0;
   9710  1.1  christos 	  sym->st_shndx = SHN_MIPS_DATA;
   9711  1.1  christos 	}
   9712  1.1  christos       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
   9713  1.1  christos 	{
   9714  1.1  christos 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   9715  1.1  christos 	  sym->st_other = STO_PROTECTED;
   9716  1.1  christos 	  sym->st_value = mips_elf_hash_table (info)->procedure_count;
   9717  1.1  christos 	  sym->st_shndx = SHN_ABS;
   9718  1.1  christos 	}
   9719  1.1  christos       else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
   9720  1.1  christos 	{
   9721  1.1  christos 	  if (h->type == STT_FUNC)
   9722  1.1  christos 	    sym->st_shndx = SHN_MIPS_TEXT;
   9723  1.1  christos 	  else if (h->type == STT_OBJECT)
   9724  1.1  christos 	    sym->st_shndx = SHN_MIPS_DATA;
   9725  1.1  christos 	}
   9726  1.1  christos     }
   9727  1.1  christos 
   9728  1.1  christos   /* Emit a copy reloc, if needed.  */
   9729  1.1  christos   if (h->needs_copy)
   9730  1.1  christos     {
   9731  1.1  christos       asection *s;
   9732  1.1  christos       bfd_vma symval;
   9733  1.1  christos 
   9734  1.1  christos       BFD_ASSERT (h->dynindx != -1);
   9735  1.1  christos       BFD_ASSERT (htab->use_plts_and_copy_relocs);
   9736  1.1  christos 
   9737  1.1  christos       s = mips_elf_rel_dyn_section (info, FALSE);
   9738  1.1  christos       symval = (h->root.u.def.section->output_section->vma
   9739  1.1  christos 		+ h->root.u.def.section->output_offset
   9740  1.1  christos 		+ h->root.u.def.value);
   9741  1.1  christos       mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
   9742  1.1  christos 					  h->dynindx, R_MIPS_COPY, symval);
   9743  1.1  christos     }
   9744  1.1  christos 
   9745  1.1  christos   /* Handle the IRIX6-specific symbols.  */
   9746  1.1  christos   if (IRIX_COMPAT (output_bfd) == ict_irix6)
   9747  1.1  christos     mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
   9748  1.1  christos 
   9749  1.1  christos   if (! info->shared)
   9750  1.1  christos     {
   9751  1.1  christos       if (! mips_elf_hash_table (info)->use_rld_obj_head
   9752  1.1  christos 	  && (strcmp (name, "__rld_map") == 0
   9753  1.1  christos 	      || strcmp (name, "__RLD_MAP") == 0))
   9754  1.1  christos 	{
   9755  1.1  christos 	  asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
   9756  1.1  christos 	  BFD_ASSERT (s != NULL);
   9757  1.1  christos 	  sym->st_value = s->output_section->vma + s->output_offset;
   9758  1.1  christos 	  bfd_put_32 (output_bfd, 0, s->contents);
   9759  1.1  christos 	  if (mips_elf_hash_table (info)->rld_value == 0)
   9760  1.1  christos 	    mips_elf_hash_table (info)->rld_value = sym->st_value;
   9761  1.1  christos 	}
   9762  1.1  christos       else if (mips_elf_hash_table (info)->use_rld_obj_head
   9763  1.1  christos 	       && strcmp (name, "__rld_obj_head") == 0)
   9764  1.1  christos 	{
   9765  1.1  christos 	  /* IRIX6 does not use a .rld_map section.  */
   9766  1.1  christos 	  if (IRIX_COMPAT (output_bfd) == ict_irix5
   9767  1.1  christos               || IRIX_COMPAT (output_bfd) == ict_none)
   9768  1.1  christos 	    BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
   9769  1.1  christos 			!= NULL);
   9770  1.1  christos 	  mips_elf_hash_table (info)->rld_value = sym->st_value;
   9771  1.1  christos 	}
   9772  1.1  christos     }
   9773  1.1  christos 
   9774  1.1  christos   /* Keep dynamic MIPS16 symbols odd.  This allows the dynamic linker to
   9775  1.1  christos      treat MIPS16 symbols like any other.  */
   9776  1.1  christos   if (ELF_ST_IS_MIPS16 (sym->st_other))
   9777  1.1  christos     {
   9778  1.1  christos       BFD_ASSERT (sym->st_value & 1);
   9779  1.1  christos       sym->st_other -= STO_MIPS16;
   9780  1.1  christos     }
   9781  1.1  christos 
   9782  1.1  christos   return TRUE;
   9783  1.1  christos }
   9784  1.1  christos 
   9785  1.1  christos /* Likewise, for VxWorks.  */
   9786  1.1  christos 
   9787  1.1  christos bfd_boolean
   9788  1.1  christos _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
   9789  1.1  christos 					 struct bfd_link_info *info,
   9790  1.1  christos 					 struct elf_link_hash_entry *h,
   9791  1.1  christos 					 Elf_Internal_Sym *sym)
   9792  1.1  christos {
   9793  1.1  christos   bfd *dynobj;
   9794  1.1  christos   asection *sgot;
   9795  1.1  christos   struct mips_got_info *g;
   9796  1.1  christos   struct mips_elf_link_hash_table *htab;
   9797  1.1  christos   struct mips_elf_link_hash_entry *hmips;
   9798  1.1  christos 
   9799  1.1  christos   htab = mips_elf_hash_table (info);
   9800  1.1  christos   BFD_ASSERT (htab != NULL);
   9801  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   9802  1.1  christos   hmips = (struct mips_elf_link_hash_entry *) h;
   9803  1.1  christos 
   9804  1.1  christos   if (h->plt.offset != (bfd_vma) -1)
   9805  1.1  christos     {
   9806  1.1  christos       bfd_byte *loc;
   9807  1.1  christos       bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
   9808  1.1  christos       Elf_Internal_Rela rel;
   9809  1.1  christos       static const bfd_vma *plt_entry;
   9810  1.1  christos 
   9811  1.1  christos       BFD_ASSERT (h->dynindx != -1);
   9812  1.1  christos       BFD_ASSERT (htab->splt != NULL);
   9813  1.1  christos       BFD_ASSERT (h->plt.offset <= htab->splt->size);
   9814  1.1  christos 
   9815  1.1  christos       /* Calculate the address of the .plt entry.  */
   9816  1.1  christos       plt_address = (htab->splt->output_section->vma
   9817  1.1  christos 		     + htab->splt->output_offset
   9818  1.1  christos 		     + h->plt.offset);
   9819  1.1  christos 
   9820  1.1  christos       /* Calculate the index of the entry.  */
   9821  1.1  christos       plt_index = ((h->plt.offset - htab->plt_header_size)
   9822  1.1  christos 		   / htab->plt_entry_size);
   9823  1.1  christos 
   9824  1.1  christos       /* Calculate the address of the .got.plt entry.  */
   9825  1.1  christos       got_address = (htab->sgotplt->output_section->vma
   9826  1.1  christos 		     + htab->sgotplt->output_offset
   9827  1.1  christos 		     + plt_index * 4);
   9828  1.1  christos 
   9829  1.1  christos       /* Calculate the offset of the .got.plt entry from
   9830  1.1  christos 	 _GLOBAL_OFFSET_TABLE_.  */
   9831  1.1  christos       got_offset = mips_elf_gotplt_index (info, h);
   9832  1.1  christos 
   9833  1.1  christos       /* Calculate the offset for the branch at the start of the PLT
   9834  1.1  christos 	 entry.  The branch jumps to the beginning of .plt.  */
   9835  1.1  christos       branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
   9836  1.1  christos 
   9837  1.1  christos       /* Fill in the initial value of the .got.plt entry.  */
   9838  1.1  christos       bfd_put_32 (output_bfd, plt_address,
   9839  1.1  christos 		  htab->sgotplt->contents + plt_index * 4);
   9840  1.1  christos 
   9841  1.1  christos       /* Find out where the .plt entry should go.  */
   9842  1.1  christos       loc = htab->splt->contents + h->plt.offset;
   9843  1.1  christos 
   9844  1.1  christos       if (info->shared)
   9845  1.1  christos 	{
   9846  1.1  christos 	  plt_entry = mips_vxworks_shared_plt_entry;
   9847  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
   9848  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
   9849  1.1  christos 	}
   9850  1.1  christos       else
   9851  1.1  christos 	{
   9852  1.1  christos 	  bfd_vma got_address_high, got_address_low;
   9853  1.1  christos 
   9854  1.1  christos 	  plt_entry = mips_vxworks_exec_plt_entry;
   9855  1.1  christos 	  got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
   9856  1.1  christos 	  got_address_low = got_address & 0xffff;
   9857  1.1  christos 
   9858  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
   9859  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
   9860  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
   9861  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
   9862  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   9863  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   9864  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
   9865  1.1  christos 	  bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
   9866  1.1  christos 
   9867  1.1  christos 	  loc = (htab->srelplt2->contents
   9868  1.1  christos 		 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
   9869  1.1  christos 
   9870  1.1  christos 	  /* Emit a relocation for the .got.plt entry.  */
   9871  1.1  christos 	  rel.r_offset = got_address;
   9872  1.1  christos 	  rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
   9873  1.1  christos 	  rel.r_addend = h->plt.offset;
   9874  1.1  christos 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   9875  1.1  christos 
   9876  1.1  christos 	  /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
   9877  1.1  christos 	  loc += sizeof (Elf32_External_Rela);
   9878  1.1  christos 	  rel.r_offset = plt_address + 8;
   9879  1.1  christos 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   9880  1.1  christos 	  rel.r_addend = got_offset;
   9881  1.1  christos 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   9882  1.1  christos 
   9883  1.1  christos 	  /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
   9884  1.1  christos 	  loc += sizeof (Elf32_External_Rela);
   9885  1.1  christos 	  rel.r_offset += 4;
   9886  1.1  christos 	  rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   9887  1.1  christos 	  bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   9888  1.1  christos 	}
   9889  1.1  christos 
   9890  1.1  christos       /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
   9891  1.1  christos       loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
   9892  1.1  christos       rel.r_offset = got_address;
   9893  1.1  christos       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
   9894  1.1  christos       rel.r_addend = 0;
   9895  1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   9896  1.1  christos 
   9897  1.1  christos       if (!h->def_regular)
   9898  1.1  christos 	sym->st_shndx = SHN_UNDEF;
   9899  1.1  christos     }
   9900  1.1  christos 
   9901  1.1  christos   BFD_ASSERT (h->dynindx != -1 || h->forced_local);
   9902  1.1  christos 
   9903  1.1  christos   sgot = htab->sgot;
   9904  1.1  christos   g = htab->got_info;
   9905  1.1  christos   BFD_ASSERT (g != NULL);
   9906  1.1  christos 
   9907  1.1  christos   /* See if this symbol has an entry in the GOT.  */
   9908  1.1  christos   if (hmips->global_got_area != GGA_NONE)
   9909  1.1  christos     {
   9910  1.1  christos       bfd_vma offset;
   9911  1.1  christos       Elf_Internal_Rela outrel;
   9912  1.1  christos       bfd_byte *loc;
   9913  1.1  christos       asection *s;
   9914  1.1  christos 
   9915  1.1  christos       /* Install the symbol value in the GOT.   */
   9916  1.1  christos       offset = mips_elf_global_got_index (dynobj, output_bfd, h,
   9917  1.1  christos 					  R_MIPS_GOT16, info);
   9918  1.1  christos       MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
   9919  1.1  christos 
   9920  1.1  christos       /* Add a dynamic relocation for it.  */
   9921  1.1  christos       s = mips_elf_rel_dyn_section (info, FALSE);
   9922  1.1  christos       loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
   9923  1.1  christos       outrel.r_offset = (sgot->output_section->vma
   9924  1.1  christos 			 + sgot->output_offset
   9925  1.1  christos 			 + offset);
   9926  1.1  christos       outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
   9927  1.1  christos       outrel.r_addend = 0;
   9928  1.1  christos       bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
   9929  1.1  christos     }
   9930  1.1  christos 
   9931  1.1  christos   /* Emit a copy reloc, if needed.  */
   9932  1.1  christos   if (h->needs_copy)
   9933  1.1  christos     {
   9934  1.1  christos       Elf_Internal_Rela rel;
   9935  1.1  christos 
   9936  1.1  christos       BFD_ASSERT (h->dynindx != -1);
   9937  1.1  christos 
   9938  1.1  christos       rel.r_offset = (h->root.u.def.section->output_section->vma
   9939  1.1  christos 		      + h->root.u.def.section->output_offset
   9940  1.1  christos 		      + h->root.u.def.value);
   9941  1.1  christos       rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
   9942  1.1  christos       rel.r_addend = 0;
   9943  1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rel,
   9944  1.1  christos 				 htab->srelbss->contents
   9945  1.1  christos 				 + (htab->srelbss->reloc_count
   9946  1.1  christos 				    * sizeof (Elf32_External_Rela)));
   9947  1.1  christos       ++htab->srelbss->reloc_count;
   9948  1.1  christos     }
   9949  1.1  christos 
   9950  1.1  christos   /* If this is a mips16 symbol, force the value to be even.  */
   9951  1.1  christos   if (ELF_ST_IS_MIPS16 (sym->st_other))
   9952  1.1  christos     sym->st_value &= ~1;
   9953  1.1  christos 
   9954  1.1  christos   return TRUE;
   9955  1.1  christos }
   9956  1.1  christos 
   9957  1.1  christos /* Write out a plt0 entry to the beginning of .plt.  */
   9958  1.1  christos 
   9959  1.1  christos static void
   9960  1.1  christos mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
   9961  1.1  christos {
   9962  1.1  christos   bfd_byte *loc;
   9963  1.1  christos   bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
   9964  1.1  christos   static const bfd_vma *plt_entry;
   9965  1.1  christos   struct mips_elf_link_hash_table *htab;
   9966  1.1  christos 
   9967  1.1  christos   htab = mips_elf_hash_table (info);
   9968  1.1  christos   BFD_ASSERT (htab != NULL);
   9969  1.1  christos 
   9970  1.1  christos   if (ABI_64_P (output_bfd))
   9971  1.1  christos     plt_entry = mips_n64_exec_plt0_entry;
   9972  1.1  christos   else if (ABI_N32_P (output_bfd))
   9973  1.1  christos     plt_entry = mips_n32_exec_plt0_entry;
   9974  1.1  christos   else
   9975  1.1  christos     plt_entry = mips_o32_exec_plt0_entry;
   9976  1.1  christos 
   9977  1.1  christos   /* Calculate the value of .got.plt.  */
   9978  1.1  christos   gotplt_value = (htab->sgotplt->output_section->vma
   9979  1.1  christos 		  + htab->sgotplt->output_offset);
   9980  1.1  christos   gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
   9981  1.1  christos   gotplt_value_low = gotplt_value & 0xffff;
   9982  1.1  christos 
   9983  1.1  christos   /* The PLT sequence is not safe for N64 if .got.plt's address can
   9984  1.1  christos      not be loaded in two instructions.  */
   9985  1.1  christos   BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
   9986  1.1  christos 	      || ~(gotplt_value | 0x7fffffff) == 0);
   9987  1.1  christos 
   9988  1.1  christos   /* Install the PLT header.  */
   9989  1.1  christos   loc = htab->splt->contents;
   9990  1.1  christos   bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
   9991  1.1  christos   bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
   9992  1.1  christos   bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
   9993  1.1  christos   bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   9994  1.1  christos   bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   9995  1.1  christos   bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   9996  1.1  christos   bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
   9997  1.1  christos   bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
   9998  1.1  christos }
   9999  1.1  christos 
   10000  1.1  christos /* Install the PLT header for a VxWorks executable and finalize the
   10001  1.1  christos    contents of .rela.plt.unloaded.  */
   10002  1.1  christos 
   10003  1.1  christos static void
   10004  1.1  christos mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
   10005  1.1  christos {
   10006  1.1  christos   Elf_Internal_Rela rela;
   10007  1.1  christos   bfd_byte *loc;
   10008  1.1  christos   bfd_vma got_value, got_value_high, got_value_low, plt_address;
   10009  1.1  christos   static const bfd_vma *plt_entry;
   10010  1.1  christos   struct mips_elf_link_hash_table *htab;
   10011  1.1  christos 
   10012  1.1  christos   htab = mips_elf_hash_table (info);
   10013  1.1  christos   BFD_ASSERT (htab != NULL);
   10014  1.1  christos 
   10015  1.1  christos   plt_entry = mips_vxworks_exec_plt0_entry;
   10016  1.1  christos 
   10017  1.1  christos   /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
   10018  1.1  christos   got_value = (htab->root.hgot->root.u.def.section->output_section->vma
   10019  1.1  christos 	       + htab->root.hgot->root.u.def.section->output_offset
   10020  1.1  christos 	       + htab->root.hgot->root.u.def.value);
   10021  1.1  christos 
   10022  1.1  christos   got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
   10023  1.1  christos   got_value_low = got_value & 0xffff;
   10024  1.1  christos 
   10025  1.1  christos   /* Calculate the address of the PLT header.  */
   10026  1.1  christos   plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
   10027  1.1  christos 
   10028  1.1  christos   /* Install the PLT header.  */
   10029  1.1  christos   loc = htab->splt->contents;
   10030  1.1  christos   bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
   10031  1.1  christos   bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
   10032  1.1  christos   bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
   10033  1.1  christos   bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
   10034  1.1  christos   bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
   10035  1.1  christos   bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
   10036  1.1  christos 
   10037  1.1  christos   /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
   10038  1.1  christos   loc = htab->srelplt2->contents;
   10039  1.1  christos   rela.r_offset = plt_address;
   10040  1.1  christos   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   10041  1.1  christos   rela.r_addend = 0;
   10042  1.1  christos   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   10043  1.1  christos   loc += sizeof (Elf32_External_Rela);
   10044  1.1  christos 
   10045  1.1  christos   /* Output the relocation for the following addiu of
   10046  1.1  christos      %lo(_GLOBAL_OFFSET_TABLE_).  */
   10047  1.1  christos   rela.r_offset += 4;
   10048  1.1  christos   rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   10049  1.1  christos   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   10050  1.1  christos   loc += sizeof (Elf32_External_Rela);
   10051  1.1  christos 
   10052  1.1  christos   /* Fix up the remaining relocations.  They may have the wrong
   10053  1.1  christos      symbol index for _G_O_T_ or _P_L_T_ depending on the order
   10054  1.1  christos      in which symbols were output.  */
   10055  1.1  christos   while (loc < htab->srelplt2->contents + htab->srelplt2->size)
   10056  1.1  christos     {
   10057  1.1  christos       Elf_Internal_Rela rel;
   10058  1.1  christos 
   10059  1.1  christos       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   10060  1.1  christos       rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
   10061  1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   10062  1.1  christos       loc += sizeof (Elf32_External_Rela);
   10063  1.1  christos 
   10064  1.1  christos       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   10065  1.1  christos       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
   10066  1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   10067  1.1  christos       loc += sizeof (Elf32_External_Rela);
   10068  1.1  christos 
   10069  1.1  christos       bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
   10070  1.1  christos       rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
   10071  1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
   10072  1.1  christos       loc += sizeof (Elf32_External_Rela);
   10073  1.1  christos     }
   10074  1.1  christos }
   10075  1.1  christos 
   10076  1.1  christos /* Install the PLT header for a VxWorks shared library.  */
   10077  1.1  christos 
   10078  1.1  christos static void
   10079  1.1  christos mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
   10080  1.1  christos {
   10081  1.1  christos   unsigned int i;
   10082  1.1  christos   struct mips_elf_link_hash_table *htab;
   10083  1.1  christos 
   10084  1.1  christos   htab = mips_elf_hash_table (info);
   10085  1.1  christos   BFD_ASSERT (htab != NULL);
   10086  1.1  christos 
   10087  1.1  christos   /* We just need to copy the entry byte-by-byte.  */
   10088  1.1  christos   for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
   10089  1.1  christos     bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
   10090  1.1  christos 		htab->splt->contents + i * 4);
   10091  1.1  christos }
   10092  1.1  christos 
   10093  1.1  christos /* Finish up the dynamic sections.  */
   10094  1.1  christos 
   10095  1.1  christos bfd_boolean
   10096  1.1  christos _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
   10097  1.1  christos 				       struct bfd_link_info *info)
   10098  1.1  christos {
   10099  1.1  christos   bfd *dynobj;
   10100  1.1  christos   asection *sdyn;
   10101  1.1  christos   asection *sgot;
   10102  1.1  christos   struct mips_got_info *gg, *g;
   10103  1.1  christos   struct mips_elf_link_hash_table *htab;
   10104  1.1  christos 
   10105  1.1  christos   htab = mips_elf_hash_table (info);
   10106  1.1  christos   BFD_ASSERT (htab != NULL);
   10107  1.1  christos 
   10108  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   10109  1.1  christos 
   10110  1.1  christos   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
   10111  1.1  christos 
   10112  1.1  christos   sgot = htab->sgot;
   10113  1.1  christos   gg = htab->got_info;
   10114  1.1  christos 
   10115  1.1  christos   if (elf_hash_table (info)->dynamic_sections_created)
   10116  1.1  christos     {
   10117  1.1  christos       bfd_byte *b;
   10118  1.1  christos       int dyn_to_skip = 0, dyn_skipped = 0;
   10119  1.1  christos 
   10120  1.1  christos       BFD_ASSERT (sdyn != NULL);
   10121  1.1  christos       BFD_ASSERT (gg != NULL);
   10122  1.1  christos 
   10123  1.1  christos       g = mips_elf_got_for_ibfd (gg, output_bfd);
   10124  1.1  christos       BFD_ASSERT (g != NULL);
   10125  1.1  christos 
   10126  1.1  christos       for (b = sdyn->contents;
   10127  1.1  christos 	   b < sdyn->contents + sdyn->size;
   10128  1.1  christos 	   b += MIPS_ELF_DYN_SIZE (dynobj))
   10129  1.1  christos 	{
   10130  1.1  christos 	  Elf_Internal_Dyn dyn;
   10131  1.1  christos 	  const char *name;
   10132  1.1  christos 	  size_t elemsize;
   10133  1.1  christos 	  asection *s;
   10134  1.1  christos 	  bfd_boolean swap_out_p;
   10135  1.1  christos 
   10136  1.1  christos 	  /* Read in the current dynamic entry.  */
   10137  1.1  christos 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
   10138  1.1  christos 
   10139  1.1  christos 	  /* Assume that we're going to modify it and write it out.  */
   10140  1.1  christos 	  swap_out_p = TRUE;
   10141  1.1  christos 
   10142  1.1  christos 	  switch (dyn.d_tag)
   10143  1.1  christos 	    {
   10144  1.1  christos 	    case DT_RELENT:
   10145  1.1  christos 	      dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
   10146  1.1  christos 	      break;
   10147  1.1  christos 
   10148  1.1  christos 	    case DT_RELAENT:
   10149  1.1  christos 	      BFD_ASSERT (htab->is_vxworks);
   10150  1.1  christos 	      dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
   10151  1.1  christos 	      break;
   10152  1.1  christos 
   10153  1.1  christos 	    case DT_STRSZ:
   10154  1.1  christos 	      /* Rewrite DT_STRSZ.  */
   10155  1.1  christos 	      dyn.d_un.d_val =
   10156  1.1  christos 		_bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
   10157  1.1  christos 	      break;
   10158  1.1  christos 
   10159  1.1  christos 	    case DT_PLTGOT:
   10160  1.1  christos 	      s = htab->sgot;
   10161  1.1  christos 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   10162  1.1  christos 	      break;
   10163  1.1  christos 
   10164  1.1  christos 	    case DT_MIPS_PLTGOT:
   10165  1.1  christos 	      s = htab->sgotplt;
   10166  1.1  christos 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   10167  1.1  christos 	      break;
   10168  1.1  christos 
   10169  1.1  christos 	    case DT_MIPS_RLD_VERSION:
   10170  1.1  christos 	      dyn.d_un.d_val = 1; /* XXX */
   10171  1.1  christos 	      break;
   10172  1.1  christos 
   10173  1.1  christos 	    case DT_MIPS_FLAGS:
   10174  1.1  christos 	      dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
   10175  1.1  christos 	      break;
   10176  1.1  christos 
   10177  1.1  christos 	    case DT_MIPS_TIME_STAMP:
   10178  1.1  christos 	      {
   10179  1.1  christos 		time_t t;
   10180  1.1  christos 		time (&t);
   10181  1.1  christos 		dyn.d_un.d_val = t;
   10182  1.1  christos 	      }
   10183  1.1  christos 	      break;
   10184  1.1  christos 
   10185  1.1  christos 	    case DT_MIPS_ICHECKSUM:
   10186  1.1  christos 	      /* XXX FIXME: */
   10187  1.1  christos 	      swap_out_p = FALSE;
   10188  1.1  christos 	      break;
   10189  1.1  christos 
   10190  1.1  christos 	    case DT_MIPS_IVERSION:
   10191  1.1  christos 	      /* XXX FIXME: */
   10192  1.1  christos 	      swap_out_p = FALSE;
   10193  1.1  christos 	      break;
   10194  1.1  christos 
   10195  1.1  christos 	    case DT_MIPS_BASE_ADDRESS:
   10196  1.1  christos 	      s = output_bfd->sections;
   10197  1.1  christos 	      BFD_ASSERT (s != NULL);
   10198  1.1  christos 	      dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
   10199  1.1  christos 	      break;
   10200  1.1  christos 
   10201  1.1  christos 	    case DT_MIPS_LOCAL_GOTNO:
   10202  1.1  christos 	      dyn.d_un.d_val = g->local_gotno;
   10203  1.1  christos 	      break;
   10204  1.1  christos 
   10205  1.1  christos 	    case DT_MIPS_UNREFEXTNO:
   10206  1.1  christos 	      /* The index into the dynamic symbol table which is the
   10207  1.1  christos 		 entry of the first external symbol that is not
   10208  1.1  christos 		 referenced within the same object.  */
   10209  1.1  christos 	      dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
   10210  1.1  christos 	      break;
   10211  1.1  christos 
   10212  1.1  christos 	    case DT_MIPS_GOTSYM:
   10213  1.1  christos 	      if (gg->global_gotsym)
   10214  1.1  christos 		{
   10215  1.1  christos 		  dyn.d_un.d_val = gg->global_gotsym->dynindx;
   10216  1.1  christos 		  break;
   10217  1.1  christos 		}
   10218  1.1  christos 	      /* In case if we don't have global got symbols we default
   10219  1.1  christos 		 to setting DT_MIPS_GOTSYM to the same value as
   10220  1.1  christos 		 DT_MIPS_SYMTABNO, so we just fall through.  */
   10221  1.1  christos 
   10222  1.1  christos 	    case DT_MIPS_SYMTABNO:
   10223  1.1  christos 	      name = ".dynsym";
   10224  1.1  christos 	      elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
   10225  1.1  christos 	      s = bfd_get_section_by_name (output_bfd, name);
   10226  1.1  christos 	      BFD_ASSERT (s != NULL);
   10227  1.1  christos 
   10228  1.1  christos 	      dyn.d_un.d_val = s->size / elemsize;
   10229  1.1  christos 	      break;
   10230  1.1  christos 
   10231  1.1  christos 	    case DT_MIPS_HIPAGENO:
   10232  1.1  christos 	      dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
   10233  1.1  christos 	      break;
   10234  1.1  christos 
   10235  1.1  christos 	    case DT_MIPS_RLD_MAP:
   10236  1.1  christos 	      dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
   10237  1.1  christos 	      break;
   10238  1.1  christos 
   10239  1.1  christos 	    case DT_MIPS_OPTIONS:
   10240  1.1  christos 	      s = (bfd_get_section_by_name
   10241  1.1  christos 		   (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
   10242  1.1  christos 	      dyn.d_un.d_ptr = s->vma;
   10243  1.1  christos 	      break;
   10244  1.1  christos 
   10245  1.1  christos 	    case DT_RELASZ:
   10246  1.1  christos 	      BFD_ASSERT (htab->is_vxworks);
   10247  1.1  christos 	      /* The count does not include the JUMP_SLOT relocations.  */
   10248  1.1  christos 	      if (htab->srelplt)
   10249  1.1  christos 		dyn.d_un.d_val -= htab->srelplt->size;
   10250  1.1  christos 	      break;
   10251  1.1  christos 
   10252  1.1  christos 	    case DT_PLTREL:
   10253  1.1  christos 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   10254  1.1  christos 	      if (htab->is_vxworks)
   10255  1.1  christos 		dyn.d_un.d_val = DT_RELA;
   10256  1.1  christos 	      else
   10257  1.1  christos 		dyn.d_un.d_val = DT_REL;
   10258  1.1  christos 	      break;
   10259  1.1  christos 
   10260  1.1  christos 	    case DT_PLTRELSZ:
   10261  1.1  christos 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   10262  1.1  christos 	      dyn.d_un.d_val = htab->srelplt->size;
   10263  1.1  christos 	      break;
   10264  1.1  christos 
   10265  1.1  christos 	    case DT_JMPREL:
   10266  1.1  christos 	      BFD_ASSERT (htab->use_plts_and_copy_relocs);
   10267  1.1  christos 	      dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
   10268  1.1  christos 				+ htab->srelplt->output_offset);
   10269  1.1  christos 	      break;
   10270  1.1  christos 
   10271  1.1  christos 	    case DT_TEXTREL:
   10272  1.1  christos 	      /* If we didn't need any text relocations after all, delete
   10273  1.1  christos 		 the dynamic tag.  */
   10274  1.1  christos 	      if (!(info->flags & DF_TEXTREL))
   10275  1.1  christos 		{
   10276  1.1  christos 		  dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
   10277  1.1  christos 		  swap_out_p = FALSE;
   10278  1.1  christos 		}
   10279  1.1  christos 	      break;
   10280  1.1  christos 
   10281  1.1  christos 	    case DT_FLAGS:
   10282  1.1  christos 	      /* If we didn't need any text relocations after all, clear
   10283  1.1  christos 		 DF_TEXTREL from DT_FLAGS.  */
   10284  1.1  christos 	      if (!(info->flags & DF_TEXTREL))
   10285  1.1  christos 		dyn.d_un.d_val &= ~DF_TEXTREL;
   10286  1.1  christos 	      else
   10287  1.1  christos 		swap_out_p = FALSE;
   10288  1.1  christos 	      break;
   10289  1.1  christos 
   10290  1.1  christos 	    default:
   10291  1.1  christos 	      swap_out_p = FALSE;
   10292  1.1  christos 	      if (htab->is_vxworks
   10293  1.1  christos 		  && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
   10294  1.1  christos 		swap_out_p = TRUE;
   10295  1.1  christos 	      break;
   10296  1.1  christos 	    }
   10297  1.1  christos 
   10298  1.1  christos 	  if (swap_out_p || dyn_skipped)
   10299  1.1  christos 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
   10300  1.1  christos 	      (dynobj, &dyn, b - dyn_skipped);
   10301  1.1  christos 
   10302  1.1  christos 	  if (dyn_to_skip)
   10303  1.1  christos 	    {
   10304  1.1  christos 	      dyn_skipped += dyn_to_skip;
   10305  1.1  christos 	      dyn_to_skip = 0;
   10306  1.1  christos 	    }
   10307  1.1  christos 	}
   10308  1.1  christos 
   10309  1.1  christos       /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
   10310  1.1  christos       if (dyn_skipped > 0)
   10311  1.1  christos 	memset (b - dyn_skipped, 0, dyn_skipped);
   10312  1.1  christos     }
   10313  1.1  christos 
   10314  1.1  christos   if (sgot != NULL && sgot->size > 0
   10315  1.1  christos       && !bfd_is_abs_section (sgot->output_section))
   10316  1.1  christos     {
   10317  1.1  christos       if (htab->is_vxworks)
   10318  1.1  christos 	{
   10319  1.1  christos 	  /* The first entry of the global offset table points to the
   10320  1.1  christos 	     ".dynamic" section.  The second is initialized by the
   10321  1.1  christos 	     loader and contains the shared library identifier.
   10322  1.1  christos 	     The third is also initialized by the loader and points
   10323  1.1  christos 	     to the lazy resolution stub.  */
   10324  1.1  christos 	  MIPS_ELF_PUT_WORD (output_bfd,
   10325  1.1  christos 			     sdyn->output_offset + sdyn->output_section->vma,
   10326  1.1  christos 			     sgot->contents);
   10327  1.1  christos 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
   10328  1.1  christos 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
   10329  1.1  christos 	  MIPS_ELF_PUT_WORD (output_bfd, 0,
   10330  1.1  christos 			     sgot->contents
   10331  1.1  christos 			     + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
   10332  1.1  christos 	}
   10333  1.1  christos       else
   10334  1.1  christos 	{
   10335  1.1  christos 	  /* The first entry of the global offset table will be filled at
   10336  1.1  christos 	     runtime. The second entry will be used by some runtime loaders.
   10337  1.1  christos 	     This isn't the case of IRIX rld.  */
   10338  1.1  christos 	  MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
   10339  1.1  christos 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
   10340  1.1  christos 			     sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
   10341  1.1  christos 	}
   10342  1.1  christos 
   10343  1.1  christos       elf_section_data (sgot->output_section)->this_hdr.sh_entsize
   10344  1.1  christos 	 = MIPS_ELF_GOT_SIZE (output_bfd);
   10345  1.1  christos     }
   10346  1.1  christos 
   10347  1.1  christos   /* Generate dynamic relocations for the non-primary gots.  */
   10348  1.1  christos   if (gg != NULL && gg->next)
   10349  1.1  christos     {
   10350  1.1  christos       Elf_Internal_Rela rel[3];
   10351  1.1  christos       bfd_vma addend = 0;
   10352  1.1  christos 
   10353  1.1  christos       memset (rel, 0, sizeof (rel));
   10354  1.1  christos       rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
   10355  1.1  christos 
   10356  1.1  christos       for (g = gg->next; g->next != gg; g = g->next)
   10357  1.1  christos 	{
   10358  1.1  christos 	  bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
   10359  1.1  christos 	    + g->next->tls_gotno;
   10360  1.1  christos 
   10361  1.1  christos 	  MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
   10362  1.1  christos 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
   10363  1.1  christos 	  MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
   10364  1.1  christos 			     sgot->contents
   10365  1.1  christos 			     + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
   10366  1.1  christos 
   10367  1.1  christos 	  if (! info->shared)
   10368  1.1  christos 	    continue;
   10369  1.1  christos 
   10370  1.1  christos 	  while (got_index < g->assigned_gotno)
   10371  1.1  christos 	    {
   10372  1.1  christos 	      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
   10373  1.1  christos 		= got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
   10374  1.1  christos 	      if (!(mips_elf_create_dynamic_relocation
   10375  1.1  christos 		    (output_bfd, info, rel, NULL,
   10376  1.1  christos 		     bfd_abs_section_ptr,
   10377  1.1  christos 		     0, &addend, sgot)))
   10378  1.1  christos 		return FALSE;
   10379  1.1  christos 	      BFD_ASSERT (addend == 0);
   10380  1.1  christos 	    }
   10381  1.1  christos 	}
   10382  1.1  christos     }
   10383  1.1  christos 
   10384  1.1  christos   /* The generation of dynamic relocations for the non-primary gots
   10385  1.1  christos      adds more dynamic relocations.  We cannot count them until
   10386  1.1  christos      here.  */
   10387  1.1  christos 
   10388  1.1  christos   if (elf_hash_table (info)->dynamic_sections_created)
   10389  1.1  christos     {
   10390  1.1  christos       bfd_byte *b;
   10391  1.1  christos       bfd_boolean swap_out_p;
   10392  1.1  christos 
   10393  1.1  christos       BFD_ASSERT (sdyn != NULL);
   10394  1.1  christos 
   10395  1.1  christos       for (b = sdyn->contents;
   10396  1.1  christos 	   b < sdyn->contents + sdyn->size;
   10397  1.1  christos 	   b += MIPS_ELF_DYN_SIZE (dynobj))
   10398  1.1  christos 	{
   10399  1.1  christos 	  Elf_Internal_Dyn dyn;
   10400  1.1  christos 	  asection *s;
   10401  1.1  christos 
   10402  1.1  christos 	  /* Read in the current dynamic entry.  */
   10403  1.1  christos 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
   10404  1.1  christos 
   10405  1.1  christos 	  /* Assume that we're going to modify it and write it out.  */
   10406  1.1  christos 	  swap_out_p = TRUE;
   10407  1.1  christos 
   10408  1.1  christos 	  switch (dyn.d_tag)
   10409  1.1  christos 	    {
   10410  1.1  christos 	    case DT_RELSZ:
   10411  1.1  christos 	      /* Reduce DT_RELSZ to account for any relocations we
   10412  1.1  christos 		 decided not to make.  This is for the n64 irix rld,
   10413  1.1  christos 		 which doesn't seem to apply any relocations if there
   10414  1.1  christos 		 are trailing null entries.  */
   10415  1.1  christos 	      s = mips_elf_rel_dyn_section (info, FALSE);
   10416  1.1  christos 	      dyn.d_un.d_val = (s->reloc_count
   10417  1.1  christos 				* (ABI_64_P (output_bfd)
   10418  1.1  christos 				   ? sizeof (Elf64_Mips_External_Rel)
   10419  1.1  christos 				   : sizeof (Elf32_External_Rel)));
   10420  1.1  christos 	      /* Adjust the section size too.  Tools like the prelinker
   10421  1.1  christos 		 can reasonably expect the values to the same.  */
   10422  1.1  christos 	      elf_section_data (s->output_section)->this_hdr.sh_size
   10423  1.1  christos 		= dyn.d_un.d_val;
   10424  1.1  christos 	      break;
   10425  1.1  christos 
   10426  1.1  christos 	    default:
   10427  1.1  christos 	      swap_out_p = FALSE;
   10428  1.1  christos 	      break;
   10429  1.1  christos 	    }
   10430  1.1  christos 
   10431  1.1  christos 	  if (swap_out_p)
   10432  1.1  christos 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
   10433  1.1  christos 	      (dynobj, &dyn, b);
   10434  1.1  christos 	}
   10435  1.1  christos     }
   10436  1.1  christos 
   10437  1.1  christos   {
   10438  1.1  christos     asection *s;
   10439  1.1  christos     Elf32_compact_rel cpt;
   10440  1.1  christos 
   10441  1.1  christos     if (SGI_COMPAT (output_bfd))
   10442  1.1  christos       {
   10443  1.1  christos 	/* Write .compact_rel section out.  */
   10444  1.1  christos 	s = bfd_get_section_by_name (dynobj, ".compact_rel");
   10445  1.1  christos 	if (s != NULL)
   10446  1.1  christos 	  {
   10447  1.1  christos 	    cpt.id1 = 1;
   10448  1.1  christos 	    cpt.num = s->reloc_count;
   10449  1.1  christos 	    cpt.id2 = 2;
   10450  1.1  christos 	    cpt.offset = (s->output_section->filepos
   10451  1.1  christos 			  + sizeof (Elf32_External_compact_rel));
   10452  1.1  christos 	    cpt.reserved0 = 0;
   10453  1.1  christos 	    cpt.reserved1 = 0;
   10454  1.1  christos 	    bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
   10455  1.1  christos 					    ((Elf32_External_compact_rel *)
   10456  1.1  christos 					     s->contents));
   10457  1.1  christos 
   10458  1.1  christos 	    /* Clean up a dummy stub function entry in .text.  */
   10459  1.1  christos 	    if (htab->sstubs != NULL)
   10460  1.1  christos 	      {
   10461  1.1  christos 		file_ptr dummy_offset;
   10462  1.1  christos 
   10463  1.1  christos 		BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
   10464  1.1  christos 		dummy_offset = htab->sstubs->size - htab->function_stub_size;
   10465  1.1  christos 		memset (htab->sstubs->contents + dummy_offset, 0,
   10466  1.1  christos 			htab->function_stub_size);
   10467  1.1  christos 	      }
   10468  1.1  christos 	  }
   10469  1.1  christos       }
   10470  1.1  christos 
   10471  1.1  christos     /* The psABI says that the dynamic relocations must be sorted in
   10472  1.1  christos        increasing order of r_symndx.  The VxWorks EABI doesn't require
   10473  1.1  christos        this, and because the code below handles REL rather than RELA
   10474  1.1  christos        relocations, using it for VxWorks would be outright harmful.  */
   10475  1.1  christos     if (!htab->is_vxworks)
   10476  1.1  christos       {
   10477  1.1  christos 	s = mips_elf_rel_dyn_section (info, FALSE);
   10478  1.1  christos 	if (s != NULL
   10479  1.1  christos 	    && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
   10480  1.1  christos 	  {
   10481  1.1  christos 	    reldyn_sorting_bfd = output_bfd;
   10482  1.1  christos 
   10483  1.1  christos 	    if (ABI_64_P (output_bfd))
   10484  1.1  christos 	      qsort ((Elf64_External_Rel *) s->contents + 1,
   10485  1.1  christos 		     s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
   10486  1.1  christos 		     sort_dynamic_relocs_64);
   10487  1.1  christos 	    else
   10488  1.1  christos 	      qsort ((Elf32_External_Rel *) s->contents + 1,
   10489  1.1  christos 		     s->reloc_count - 1, sizeof (Elf32_External_Rel),
   10490  1.1  christos 		     sort_dynamic_relocs);
   10491  1.1  christos 	  }
   10492  1.1  christos       }
   10493  1.1  christos   }
   10494  1.1  christos 
   10495  1.1  christos   if (htab->splt && htab->splt->size > 0)
   10496  1.1  christos     {
   10497  1.1  christos       if (htab->is_vxworks)
   10498  1.1  christos 	{
   10499  1.1  christos 	  if (info->shared)
   10500  1.1  christos 	    mips_vxworks_finish_shared_plt (output_bfd, info);
   10501  1.1  christos 	  else
   10502  1.1  christos 	    mips_vxworks_finish_exec_plt (output_bfd, info);
   10503  1.1  christos 	}
   10504  1.1  christos       else
   10505  1.1  christos 	{
   10506  1.1  christos 	  BFD_ASSERT (!info->shared);
   10507  1.1  christos 	  mips_finish_exec_plt (output_bfd, info);
   10508  1.1  christos 	}
   10509  1.1  christos     }
   10510  1.1  christos   return TRUE;
   10511  1.1  christos }
   10512  1.1  christos 
   10513  1.1  christos 
   10514  1.1  christos /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
   10515  1.1  christos 
   10516  1.1  christos static void
   10517  1.1  christos mips_set_isa_flags (bfd *abfd)
   10518  1.1  christos {
   10519  1.1  christos   flagword val;
   10520  1.1  christos 
   10521  1.1  christos   switch (bfd_get_mach (abfd))
   10522  1.1  christos     {
   10523  1.1  christos     default:
   10524  1.1  christos     case bfd_mach_mips3000:
   10525  1.1  christos       val = E_MIPS_ARCH_1;
   10526  1.1  christos       break;
   10527  1.1  christos 
   10528  1.1  christos     case bfd_mach_mips3900:
   10529  1.1  christos       val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
   10530  1.1  christos       break;
   10531  1.1  christos 
   10532  1.1  christos     case bfd_mach_mips6000:
   10533  1.1  christos       val = E_MIPS_ARCH_2;
   10534  1.1  christos       break;
   10535  1.1  christos 
   10536  1.1  christos     case bfd_mach_mips4000:
   10537  1.1  christos     case bfd_mach_mips4300:
   10538  1.1  christos     case bfd_mach_mips4400:
   10539  1.1  christos     case bfd_mach_mips4600:
   10540  1.1  christos       val = E_MIPS_ARCH_3;
   10541  1.1  christos       break;
   10542  1.1  christos 
   10543  1.1  christos     case bfd_mach_mips4010:
   10544  1.1  christos       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
   10545  1.1  christos       break;
   10546  1.1  christos 
   10547  1.1  christos     case bfd_mach_mips4100:
   10548  1.1  christos       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
   10549  1.1  christos       break;
   10550  1.1  christos 
   10551  1.1  christos     case bfd_mach_mips4111:
   10552  1.1  christos       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
   10553  1.1  christos       break;
   10554  1.1  christos 
   10555  1.1  christos     case bfd_mach_mips4120:
   10556  1.1  christos       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
   10557  1.1  christos       break;
   10558  1.1  christos 
   10559  1.1  christos     case bfd_mach_mips4650:
   10560  1.1  christos       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
   10561  1.1  christos       break;
   10562  1.1  christos 
   10563  1.1  christos     case bfd_mach_mips5400:
   10564  1.1  christos       val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
   10565  1.1  christos       break;
   10566  1.1  christos 
   10567  1.1  christos     case bfd_mach_mips5500:
   10568  1.1  christos       val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
   10569  1.1  christos       break;
   10570  1.1  christos 
   10571  1.1  christos     case bfd_mach_mips9000:
   10572  1.1  christos       val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
   10573  1.1  christos       break;
   10574  1.1  christos 
   10575  1.1  christos     case bfd_mach_mips5000:
   10576  1.1  christos     case bfd_mach_mips7000:
   10577  1.1  christos     case bfd_mach_mips8000:
   10578  1.1  christos     case bfd_mach_mips10000:
   10579  1.1  christos     case bfd_mach_mips12000:
   10580  1.1  christos     case bfd_mach_mips14000:
   10581  1.1  christos     case bfd_mach_mips16000:
   10582  1.1  christos       val = E_MIPS_ARCH_4;
   10583  1.1  christos       break;
   10584  1.1  christos 
   10585  1.1  christos     case bfd_mach_mips5:
   10586  1.1  christos       val = E_MIPS_ARCH_5;
   10587  1.1  christos       break;
   10588  1.1  christos 
   10589  1.1  christos     case bfd_mach_mips_loongson_2e:
   10590  1.1  christos       val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
   10591  1.1  christos       break;
   10592  1.1  christos 
   10593  1.1  christos     case bfd_mach_mips_loongson_2f:
   10594  1.1  christos       val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
   10595  1.1  christos       break;
   10596  1.1  christos 
   10597  1.1  christos     case bfd_mach_mips_sb1:
   10598  1.1  christos       val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
   10599  1.1  christos       break;
   10600  1.1  christos 
   10601  1.1  christos     case bfd_mach_mips_loongson_3a:
   10602  1.1  christos       val = E_MIPS_ARCH_64 | E_MIPS_MACH_LS3A;
   10603  1.1  christos       break;
   10604  1.1  christos 
   10605  1.1  christos     case bfd_mach_mips_octeon:
   10606  1.1  christos       val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
   10607  1.1  christos       break;
   10608  1.1  christos 
   10609  1.1  christos     case bfd_mach_mips_xlr:
   10610  1.1  christos       val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
   10611  1.1  christos       break;
   10612  1.1  christos 
   10613  1.1  christos     case bfd_mach_mipsisa32:
   10614  1.1  christos       val = E_MIPS_ARCH_32;
   10615  1.1  christos       break;
   10616  1.1  christos 
   10617  1.1  christos     case bfd_mach_mipsisa64:
   10618  1.1  christos       val = E_MIPS_ARCH_64;
   10619  1.1  christos       break;
   10620  1.1  christos 
   10621  1.1  christos     case bfd_mach_mipsisa32r2:
   10622  1.1  christos       val = E_MIPS_ARCH_32R2;
   10623  1.1  christos       break;
   10624  1.1  christos 
   10625  1.1  christos     case bfd_mach_mipsisa64r2:
   10626  1.1  christos       val = E_MIPS_ARCH_64R2;
   10627  1.1  christos       break;
   10628  1.1  christos     }
   10629  1.1  christos   elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
   10630  1.1  christos   elf_elfheader (abfd)->e_flags |= val;
   10631  1.1  christos 
   10632  1.1  christos }
   10633  1.1  christos 
   10634  1.1  christos 
   10635  1.1  christos /* The final processing done just before writing out a MIPS ELF object
   10636  1.1  christos    file.  This gets the MIPS architecture right based on the machine
   10637  1.1  christos    number.  This is used by both the 32-bit and the 64-bit ABI.  */
   10638  1.1  christos 
   10639  1.1  christos void
   10640  1.1  christos _bfd_mips_elf_final_write_processing (bfd *abfd,
   10641  1.1  christos 				      bfd_boolean linker ATTRIBUTE_UNUSED)
   10642  1.1  christos {
   10643  1.1  christos   unsigned int i;
   10644  1.1  christos   Elf_Internal_Shdr **hdrpp;
   10645  1.1  christos   const char *name;
   10646  1.1  christos   asection *sec;
   10647  1.1  christos 
   10648  1.1  christos   /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
   10649  1.1  christos      is nonzero.  This is for compatibility with old objects, which used
   10650  1.1  christos      a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
   10651  1.1  christos   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
   10652  1.1  christos     mips_set_isa_flags (abfd);
   10653  1.1  christos 
   10654  1.1  christos   /* Set the sh_info field for .gptab sections and other appropriate
   10655  1.1  christos      info for each special section.  */
   10656  1.1  christos   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
   10657  1.1  christos        i < elf_numsections (abfd);
   10658  1.1  christos        i++, hdrpp++)
   10659  1.1  christos     {
   10660  1.1  christos       switch ((*hdrpp)->sh_type)
   10661  1.1  christos 	{
   10662  1.1  christos 	case SHT_MIPS_MSYM:
   10663  1.1  christos 	case SHT_MIPS_LIBLIST:
   10664  1.1  christos 	  sec = bfd_get_section_by_name (abfd, ".dynstr");
   10665  1.1  christos 	  if (sec != NULL)
   10666  1.1  christos 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   10667  1.1  christos 	  break;
   10668  1.1  christos 
   10669  1.1  christos 	case SHT_MIPS_GPTAB:
   10670  1.1  christos 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   10671  1.1  christos 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
   10672  1.1  christos 	  BFD_ASSERT (name != NULL
   10673  1.1  christos 		      && CONST_STRNEQ (name, ".gptab."));
   10674  1.1  christos 	  sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
   10675  1.1  christos 	  BFD_ASSERT (sec != NULL);
   10676  1.1  christos 	  (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
   10677  1.1  christos 	  break;
   10678  1.1  christos 
   10679  1.1  christos 	case SHT_MIPS_CONTENT:
   10680  1.1  christos 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   10681  1.1  christos 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
   10682  1.1  christos 	  BFD_ASSERT (name != NULL
   10683  1.1  christos 		      && CONST_STRNEQ (name, ".MIPS.content"));
   10684  1.1  christos 	  sec = bfd_get_section_by_name (abfd,
   10685  1.1  christos 					 name + sizeof ".MIPS.content" - 1);
   10686  1.1  christos 	  BFD_ASSERT (sec != NULL);
   10687  1.1  christos 	  (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   10688  1.1  christos 	  break;
   10689  1.1  christos 
   10690  1.1  christos 	case SHT_MIPS_SYMBOL_LIB:
   10691  1.1  christos 	  sec = bfd_get_section_by_name (abfd, ".dynsym");
   10692  1.1  christos 	  if (sec != NULL)
   10693  1.1  christos 	    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   10694  1.1  christos 	  sec = bfd_get_section_by_name (abfd, ".liblist");
   10695  1.1  christos 	  if (sec != NULL)
   10696  1.1  christos 	    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
   10697  1.1  christos 	  break;
   10698  1.1  christos 
   10699  1.1  christos 	case SHT_MIPS_EVENTS:
   10700  1.1  christos 	  BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
   10701  1.1  christos 	  name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
   10702  1.1  christos 	  BFD_ASSERT (name != NULL);
   10703  1.1  christos 	  if (CONST_STRNEQ (name, ".MIPS.events"))
   10704  1.1  christos 	    sec = bfd_get_section_by_name (abfd,
   10705  1.1  christos 					   name + sizeof ".MIPS.events" - 1);
   10706  1.1  christos 	  else
   10707  1.1  christos 	    {
   10708  1.1  christos 	      BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
   10709  1.1  christos 	      sec = bfd_get_section_by_name (abfd,
   10710  1.1  christos 					     (name
   10711  1.1  christos 					      + sizeof ".MIPS.post_rel" - 1));
   10712  1.1  christos 	    }
   10713  1.1  christos 	  BFD_ASSERT (sec != NULL);
   10714  1.1  christos 	  (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
   10715  1.1  christos 	  break;
   10716  1.1  christos 
   10717  1.1  christos 	}
   10718  1.1  christos     }
   10719  1.1  christos }
   10720  1.1  christos 
   10721  1.1  christos /* When creating an IRIX5 executable, we need REGINFO and RTPROC
   10723  1.1  christos    segments.  */
   10724  1.1  christos 
   10725  1.1  christos int
   10726  1.1  christos _bfd_mips_elf_additional_program_headers (bfd *abfd,
   10727  1.1  christos 					  struct bfd_link_info *info ATTRIBUTE_UNUSED)
   10728  1.1  christos {
   10729  1.1  christos   asection *s;
   10730  1.1  christos   int ret = 0;
   10731  1.1  christos 
   10732  1.1  christos   /* See if we need a PT_MIPS_REGINFO segment.  */
   10733  1.1  christos   s = bfd_get_section_by_name (abfd, ".reginfo");
   10734  1.1  christos   if (s && (s->flags & SEC_LOAD))
   10735  1.1  christos     ++ret;
   10736  1.1  christos 
   10737  1.1  christos   /* See if we need a PT_MIPS_OPTIONS segment.  */
   10738  1.1  christos   if (IRIX_COMPAT (abfd) == ict_irix6
   10739  1.1  christos       && bfd_get_section_by_name (abfd,
   10740  1.1  christos 				  MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
   10741  1.1  christos     ++ret;
   10742  1.1  christos 
   10743  1.1  christos   /* See if we need a PT_MIPS_RTPROC segment.  */
   10744  1.1  christos   if (IRIX_COMPAT (abfd) == ict_irix5
   10745  1.1  christos       && bfd_get_section_by_name (abfd, ".dynamic")
   10746  1.1  christos       && bfd_get_section_by_name (abfd, ".mdebug"))
   10747  1.1  christos     ++ret;
   10748  1.1  christos 
   10749  1.1  christos   /* Allocate a PT_NULL header in dynamic objects.  See
   10750  1.1  christos      _bfd_mips_elf_modify_segment_map for details.  */
   10751  1.1  christos   if (!SGI_COMPAT (abfd)
   10752  1.1  christos       && bfd_get_section_by_name (abfd, ".dynamic"))
   10753  1.1  christos     ++ret;
   10754  1.1  christos 
   10755  1.1  christos   return ret;
   10756  1.1  christos }
   10757  1.1  christos 
   10758  1.1  christos /* Modify the segment map for an IRIX5 executable.  */
   10759  1.1  christos 
   10760  1.1  christos bfd_boolean
   10761  1.1  christos _bfd_mips_elf_modify_segment_map (bfd *abfd,
   10762  1.1  christos 				  struct bfd_link_info *info)
   10763  1.1  christos {
   10764  1.1  christos   asection *s;
   10765  1.1  christos   struct elf_segment_map *m, **pm;
   10766  1.1  christos   bfd_size_type amt;
   10767  1.1  christos 
   10768  1.1  christos   /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
   10769  1.1  christos      segment.  */
   10770  1.1  christos   s = bfd_get_section_by_name (abfd, ".reginfo");
   10771  1.1  christos   if (s != NULL && (s->flags & SEC_LOAD) != 0)
   10772  1.1  christos     {
   10773  1.1  christos       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
   10774  1.1  christos 	if (m->p_type == PT_MIPS_REGINFO)
   10775  1.1  christos 	  break;
   10776  1.1  christos       if (m == NULL)
   10777  1.1  christos 	{
   10778  1.1  christos 	  amt = sizeof *m;
   10779  1.1  christos 	  m = bfd_zalloc (abfd, amt);
   10780  1.1  christos 	  if (m == NULL)
   10781  1.1  christos 	    return FALSE;
   10782  1.1  christos 
   10783  1.1  christos 	  m->p_type = PT_MIPS_REGINFO;
   10784  1.1  christos 	  m->count = 1;
   10785  1.1  christos 	  m->sections[0] = s;
   10786  1.1  christos 
   10787  1.1  christos 	  /* We want to put it after the PHDR and INTERP segments.  */
   10788  1.1  christos 	  pm = &elf_tdata (abfd)->segment_map;
   10789  1.1  christos 	  while (*pm != NULL
   10790  1.1  christos 		 && ((*pm)->p_type == PT_PHDR
   10791  1.1  christos 		     || (*pm)->p_type == PT_INTERP))
   10792  1.1  christos 	    pm = &(*pm)->next;
   10793  1.1  christos 
   10794  1.1  christos 	  m->next = *pm;
   10795  1.1  christos 	  *pm = m;
   10796  1.1  christos 	}
   10797  1.1  christos     }
   10798  1.1  christos 
   10799  1.1  christos   /* For IRIX 6, we don't have .mdebug sections, nor does anything but
   10800  1.1  christos      .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
   10801  1.1  christos      PT_MIPS_OPTIONS segment immediately following the program header
   10802  1.1  christos      table.  */
   10803  1.1  christos   if (NEWABI_P (abfd)
   10804  1.1  christos       /* On non-IRIX6 new abi, we'll have already created a segment
   10805  1.1  christos 	 for this section, so don't create another.  I'm not sure this
   10806  1.1  christos 	 is not also the case for IRIX 6, but I can't test it right
   10807  1.1  christos 	 now.  */
   10808  1.1  christos       && IRIX_COMPAT (abfd) == ict_irix6)
   10809  1.1  christos     {
   10810  1.1  christos       for (s = abfd->sections; s; s = s->next)
   10811  1.1  christos 	if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
   10812  1.1  christos 	  break;
   10813  1.1  christos 
   10814  1.1  christos       if (s)
   10815  1.1  christos 	{
   10816  1.1  christos 	  struct elf_segment_map *options_segment;
   10817  1.1  christos 
   10818  1.1  christos 	  pm = &elf_tdata (abfd)->segment_map;
   10819  1.1  christos 	  while (*pm != NULL
   10820  1.1  christos 		 && ((*pm)->p_type == PT_PHDR
   10821  1.1  christos 		     || (*pm)->p_type == PT_INTERP))
   10822  1.1  christos 	    pm = &(*pm)->next;
   10823  1.1  christos 
   10824  1.1  christos 	  if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
   10825  1.1  christos 	    {
   10826  1.1  christos 	      amt = sizeof (struct elf_segment_map);
   10827  1.1  christos 	      options_segment = bfd_zalloc (abfd, amt);
   10828  1.1  christos 	      options_segment->next = *pm;
   10829  1.1  christos 	      options_segment->p_type = PT_MIPS_OPTIONS;
   10830  1.1  christos 	      options_segment->p_flags = PF_R;
   10831  1.1  christos 	      options_segment->p_flags_valid = TRUE;
   10832  1.1  christos 	      options_segment->count = 1;
   10833  1.1  christos 	      options_segment->sections[0] = s;
   10834  1.1  christos 	      *pm = options_segment;
   10835  1.1  christos 	    }
   10836  1.1  christos 	}
   10837  1.1  christos     }
   10838  1.1  christos   else
   10839  1.1  christos     {
   10840  1.1  christos       if (IRIX_COMPAT (abfd) == ict_irix5)
   10841  1.1  christos 	{
   10842  1.1  christos 	  /* If there are .dynamic and .mdebug sections, we make a room
   10843  1.1  christos 	     for the RTPROC header.  FIXME: Rewrite without section names.  */
   10844  1.1  christos 	  if (bfd_get_section_by_name (abfd, ".interp") == NULL
   10845  1.1  christos 	      && bfd_get_section_by_name (abfd, ".dynamic") != NULL
   10846  1.1  christos 	      && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
   10847  1.1  christos 	    {
   10848  1.1  christos 	      for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
   10849  1.1  christos 		if (m->p_type == PT_MIPS_RTPROC)
   10850  1.1  christos 		  break;
   10851  1.1  christos 	      if (m == NULL)
   10852  1.1  christos 		{
   10853  1.1  christos 		  amt = sizeof *m;
   10854  1.1  christos 		  m = bfd_zalloc (abfd, amt);
   10855  1.1  christos 		  if (m == NULL)
   10856  1.1  christos 		    return FALSE;
   10857  1.1  christos 
   10858  1.1  christos 		  m->p_type = PT_MIPS_RTPROC;
   10859  1.1  christos 
   10860  1.1  christos 		  s = bfd_get_section_by_name (abfd, ".rtproc");
   10861  1.1  christos 		  if (s == NULL)
   10862  1.1  christos 		    {
   10863  1.1  christos 		      m->count = 0;
   10864  1.1  christos 		      m->p_flags = 0;
   10865  1.1  christos 		      m->p_flags_valid = 1;
   10866  1.1  christos 		    }
   10867  1.1  christos 		  else
   10868  1.1  christos 		    {
   10869  1.1  christos 		      m->count = 1;
   10870  1.1  christos 		      m->sections[0] = s;
   10871  1.1  christos 		    }
   10872  1.1  christos 
   10873  1.1  christos 		  /* We want to put it after the DYNAMIC segment.  */
   10874  1.1  christos 		  pm = &elf_tdata (abfd)->segment_map;
   10875  1.1  christos 		  while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
   10876  1.1  christos 		    pm = &(*pm)->next;
   10877  1.1  christos 		  if (*pm != NULL)
   10878  1.1  christos 		    pm = &(*pm)->next;
   10879  1.1  christos 
   10880  1.1  christos 		  m->next = *pm;
   10881  1.1  christos 		  *pm = m;
   10882  1.1  christos 		}
   10883  1.1  christos 	    }
   10884  1.1  christos 	}
   10885  1.1  christos       /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
   10886  1.1  christos 	 .dynstr, .dynsym, and .hash sections, and everything in
   10887  1.1  christos 	 between.  */
   10888  1.1  christos       for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
   10889  1.1  christos 	   pm = &(*pm)->next)
   10890  1.1  christos 	if ((*pm)->p_type == PT_DYNAMIC)
   10891  1.1  christos 	  break;
   10892  1.1  christos       m = *pm;
   10893  1.1  christos       if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
   10894  1.1  christos 	{
   10895  1.1  christos 	  /* For a normal mips executable the permissions for the PT_DYNAMIC
   10896  1.1  christos 	     segment are read, write and execute. We do that here since
   10897  1.1  christos 	     the code in elf.c sets only the read permission. This matters
   10898  1.1  christos 	     sometimes for the dynamic linker.  */
   10899  1.1  christos 	  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
   10900  1.1  christos 	    {
   10901  1.1  christos 	      m->p_flags = PF_R | PF_W | PF_X;
   10902  1.1  christos 	      m->p_flags_valid = 1;
   10903  1.1  christos 	    }
   10904  1.1  christos 	}
   10905  1.1  christos       /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
   10906  1.1  christos 	 glibc's dynamic linker has traditionally derived the number of
   10907  1.1  christos 	 tags from the p_filesz field, and sometimes allocates stack
   10908  1.1  christos 	 arrays of that size.  An overly-big PT_DYNAMIC segment can
   10909  1.1  christos 	 be actively harmful in such cases.  Making PT_DYNAMIC contain
   10910  1.1  christos 	 other sections can also make life hard for the prelinker,
   10911  1.1  christos 	 which might move one of the other sections to a different
   10912  1.1  christos 	 PT_LOAD segment.  */
   10913  1.1  christos       if (SGI_COMPAT (abfd)
   10914  1.1  christos 	  && m != NULL
   10915  1.1  christos 	  && m->count == 1
   10916  1.1  christos 	  && strcmp (m->sections[0]->name, ".dynamic") == 0)
   10917  1.1  christos 	{
   10918  1.1  christos 	  static const char *sec_names[] =
   10919  1.1  christos 	  {
   10920  1.1  christos 	    ".dynamic", ".dynstr", ".dynsym", ".hash"
   10921  1.1  christos 	  };
   10922  1.1  christos 	  bfd_vma low, high;
   10923  1.1  christos 	  unsigned int i, c;
   10924  1.1  christos 	  struct elf_segment_map *n;
   10925  1.1  christos 
   10926  1.1  christos 	  low = ~(bfd_vma) 0;
   10927  1.1  christos 	  high = 0;
   10928  1.1  christos 	  for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
   10929  1.1  christos 	    {
   10930  1.1  christos 	      s = bfd_get_section_by_name (abfd, sec_names[i]);
   10931  1.1  christos 	      if (s != NULL && (s->flags & SEC_LOAD) != 0)
   10932  1.1  christos 		{
   10933  1.1  christos 		  bfd_size_type sz;
   10934  1.1  christos 
   10935  1.1  christos 		  if (low > s->vma)
   10936  1.1  christos 		    low = s->vma;
   10937  1.1  christos 		  sz = s->size;
   10938  1.1  christos 		  if (high < s->vma + sz)
   10939  1.1  christos 		    high = s->vma + sz;
   10940  1.1  christos 		}
   10941  1.1  christos 	    }
   10942  1.1  christos 
   10943  1.1  christos 	  c = 0;
   10944  1.1  christos 	  for (s = abfd->sections; s != NULL; s = s->next)
   10945  1.1  christos 	    if ((s->flags & SEC_LOAD) != 0
   10946  1.1  christos 		&& s->vma >= low
   10947  1.1  christos 		&& s->vma + s->size <= high)
   10948  1.1  christos 	      ++c;
   10949  1.1  christos 
   10950  1.1  christos 	  amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
   10951  1.1  christos 	  n = bfd_zalloc (abfd, amt);
   10952  1.1  christos 	  if (n == NULL)
   10953  1.1  christos 	    return FALSE;
   10954  1.1  christos 	  *n = *m;
   10955  1.1  christos 	  n->count = c;
   10956  1.1  christos 
   10957  1.1  christos 	  i = 0;
   10958  1.1  christos 	  for (s = abfd->sections; s != NULL; s = s->next)
   10959  1.1  christos 	    {
   10960  1.1  christos 	      if ((s->flags & SEC_LOAD) != 0
   10961  1.1  christos 		  && s->vma >= low
   10962  1.1  christos 		  && s->vma + s->size <= high)
   10963  1.1  christos 		{
   10964  1.1  christos 		  n->sections[i] = s;
   10965  1.1  christos 		  ++i;
   10966  1.1  christos 		}
   10967  1.1  christos 	    }
   10968  1.1  christos 
   10969  1.1  christos 	  *pm = n;
   10970  1.1  christos 	}
   10971  1.1  christos     }
   10972  1.1  christos 
   10973  1.1  christos   /* Allocate a spare program header in dynamic objects so that tools
   10974  1.1  christos      like the prelinker can add an extra PT_LOAD entry.
   10975  1.1  christos 
   10976  1.1  christos      If the prelinker needs to make room for a new PT_LOAD entry, its
   10977  1.1  christos      standard procedure is to move the first (read-only) sections into
   10978  1.1  christos      the new (writable) segment.  However, the MIPS ABI requires
   10979  1.1  christos      .dynamic to be in a read-only segment, and the section will often
   10980  1.1  christos      start within sizeof (ElfNN_Phdr) bytes of the last program header.
   10981  1.1  christos 
   10982  1.1  christos      Although the prelinker could in principle move .dynamic to a
   10983  1.1  christos      writable segment, it seems better to allocate a spare program
   10984  1.1  christos      header instead, and avoid the need to move any sections.
   10985  1.1  christos      There is a long tradition of allocating spare dynamic tags,
   10986  1.1  christos      so allocating a spare program header seems like a natural
   10987  1.1  christos      extension.
   10988  1.1  christos 
   10989  1.1  christos      If INFO is NULL, we may be copying an already prelinked binary
   10990  1.1  christos      with objcopy or strip, so do not add this header.  */
   10991  1.1  christos   if (info != NULL
   10992  1.1  christos       && !SGI_COMPAT (abfd)
   10993  1.1  christos       && bfd_get_section_by_name (abfd, ".dynamic"))
   10994  1.1  christos     {
   10995  1.1  christos       for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
   10996  1.1  christos 	if ((*pm)->p_type == PT_NULL)
   10997  1.1  christos 	  break;
   10998  1.1  christos       if (*pm == NULL)
   10999  1.1  christos 	{
   11000  1.1  christos 	  m = bfd_zalloc (abfd, sizeof (*m));
   11001  1.1  christos 	  if (m == NULL)
   11002  1.1  christos 	    return FALSE;
   11003  1.1  christos 
   11004  1.1  christos 	  m->p_type = PT_NULL;
   11005  1.1  christos 	  *pm = m;
   11006  1.1  christos 	}
   11007  1.1  christos     }
   11008  1.1  christos 
   11009  1.1  christos   return TRUE;
   11010  1.1  christos }
   11011  1.1  christos 
   11012  1.1  christos /* Return the section that should be marked against GC for a given
   11014  1.1  christos    relocation.  */
   11015  1.1  christos 
   11016  1.1  christos asection *
   11017  1.1  christos _bfd_mips_elf_gc_mark_hook (asection *sec,
   11018  1.1  christos 			    struct bfd_link_info *info,
   11019  1.1  christos 			    Elf_Internal_Rela *rel,
   11020  1.1  christos 			    struct elf_link_hash_entry *h,
   11021  1.1  christos 			    Elf_Internal_Sym *sym)
   11022  1.1  christos {
   11023  1.1  christos   /* ??? Do mips16 stub sections need to be handled special?  */
   11024  1.1  christos 
   11025  1.1  christos   if (h != NULL)
   11026  1.1  christos     switch (ELF_R_TYPE (sec->owner, rel->r_info))
   11027  1.1  christos       {
   11028  1.1  christos       case R_MIPS_GNU_VTINHERIT:
   11029  1.1  christos       case R_MIPS_GNU_VTENTRY:
   11030  1.1  christos 	return NULL;
   11031  1.1  christos       }
   11032  1.1  christos 
   11033  1.1  christos   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   11034  1.1  christos }
   11035  1.1  christos 
   11036  1.1  christos /* Update the got entry reference counts for the section being removed.  */
   11037  1.1  christos 
   11038  1.1  christos bfd_boolean
   11039  1.1  christos _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
   11040  1.1  christos 			     struct bfd_link_info *info ATTRIBUTE_UNUSED,
   11041  1.1  christos 			     asection *sec ATTRIBUTE_UNUSED,
   11042  1.1  christos 			     const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
   11043  1.1  christos {
   11044  1.1  christos #if 0
   11045  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   11046  1.1  christos   struct elf_link_hash_entry **sym_hashes;
   11047  1.1  christos   bfd_signed_vma *local_got_refcounts;
   11048  1.1  christos   const Elf_Internal_Rela *rel, *relend;
   11049  1.1  christos   unsigned long r_symndx;
   11050  1.1  christos   struct elf_link_hash_entry *h;
   11051  1.1  christos 
   11052  1.1  christos   if (info->relocatable)
   11053  1.1  christos     return TRUE;
   11054  1.1  christos 
   11055  1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   11056  1.1  christos   sym_hashes = elf_sym_hashes (abfd);
   11057  1.1  christos   local_got_refcounts = elf_local_got_refcounts (abfd);
   11058  1.1  christos 
   11059  1.1  christos   relend = relocs + sec->reloc_count;
   11060  1.1  christos   for (rel = relocs; rel < relend; rel++)
   11061  1.1  christos     switch (ELF_R_TYPE (abfd, rel->r_info))
   11062  1.1  christos       {
   11063  1.1  christos       case R_MIPS16_GOT16:
   11064  1.1  christos       case R_MIPS16_CALL16:
   11065  1.1  christos       case R_MIPS_GOT16:
   11066  1.1  christos       case R_MIPS_CALL16:
   11067  1.1  christos       case R_MIPS_CALL_HI16:
   11068  1.1  christos       case R_MIPS_CALL_LO16:
   11069  1.1  christos       case R_MIPS_GOT_HI16:
   11070  1.1  christos       case R_MIPS_GOT_LO16:
   11071  1.1  christos       case R_MIPS_GOT_DISP:
   11072  1.1  christos       case R_MIPS_GOT_PAGE:
   11073  1.1  christos       case R_MIPS_GOT_OFST:
   11074  1.1  christos 	/* ??? It would seem that the existing MIPS code does no sort
   11075  1.1  christos 	   of reference counting or whatnot on its GOT and PLT entries,
   11076  1.1  christos 	   so it is not possible to garbage collect them at this time.  */
   11077  1.1  christos 	break;
   11078  1.1  christos 
   11079  1.1  christos       default:
   11080  1.1  christos 	break;
   11081  1.1  christos       }
   11082  1.1  christos #endif
   11083  1.1  christos 
   11084  1.1  christos   return TRUE;
   11085  1.1  christos }
   11086  1.1  christos 
   11087  1.1  christos /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
   11089  1.1  christos    hiding the old indirect symbol.  Process additional relocation
   11090  1.1  christos    information.  Also called for weakdefs, in which case we just let
   11091  1.1  christos    _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
   11092  1.1  christos 
   11093  1.1  christos void
   11094  1.1  christos _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
   11095  1.1  christos 				    struct elf_link_hash_entry *dir,
   11096  1.1  christos 				    struct elf_link_hash_entry *ind)
   11097  1.1  christos {
   11098  1.1  christos   struct mips_elf_link_hash_entry *dirmips, *indmips;
   11099  1.1  christos 
   11100  1.1  christos   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   11101  1.1  christos 
   11102  1.1  christos   dirmips = (struct mips_elf_link_hash_entry *) dir;
   11103  1.1  christos   indmips = (struct mips_elf_link_hash_entry *) ind;
   11104  1.1  christos   /* Any absolute non-dynamic relocations against an indirect or weak
   11105  1.1  christos      definition will be against the target symbol.  */
   11106  1.1  christos   if (indmips->has_static_relocs)
   11107  1.1  christos     dirmips->has_static_relocs = TRUE;
   11108  1.1  christos 
   11109  1.1  christos   if (ind->root.type != bfd_link_hash_indirect)
   11110  1.1  christos     return;
   11111  1.1  christos 
   11112  1.1  christos   dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
   11113  1.1  christos   if (indmips->readonly_reloc)
   11114  1.1  christos     dirmips->readonly_reloc = TRUE;
   11115  1.1  christos   if (indmips->no_fn_stub)
   11116  1.1  christos     dirmips->no_fn_stub = TRUE;
   11117  1.1  christos   if (indmips->fn_stub)
   11118  1.1  christos     {
   11119  1.1  christos       dirmips->fn_stub = indmips->fn_stub;
   11120  1.1  christos       indmips->fn_stub = NULL;
   11121  1.1  christos     }
   11122  1.1  christos   if (indmips->need_fn_stub)
   11123  1.1  christos     {
   11124  1.1  christos       dirmips->need_fn_stub = TRUE;
   11125  1.1  christos       indmips->need_fn_stub = FALSE;
   11126  1.1  christos     }
   11127  1.1  christos   if (indmips->call_stub)
   11128  1.1  christos     {
   11129  1.1  christos       dirmips->call_stub = indmips->call_stub;
   11130  1.1  christos       indmips->call_stub = NULL;
   11131  1.1  christos     }
   11132  1.1  christos   if (indmips->call_fp_stub)
   11133  1.1  christos     {
   11134  1.1  christos       dirmips->call_fp_stub = indmips->call_fp_stub;
   11135  1.1  christos       indmips->call_fp_stub = NULL;
   11136  1.1  christos     }
   11137  1.1  christos   if (indmips->global_got_area < dirmips->global_got_area)
   11138  1.1  christos     dirmips->global_got_area = indmips->global_got_area;
   11139  1.1  christos   if (indmips->global_got_area < GGA_NONE)
   11140  1.1  christos     indmips->global_got_area = GGA_NONE;
   11141  1.1  christos   if (indmips->has_nonpic_branches)
   11142  1.1  christos     dirmips->has_nonpic_branches = TRUE;
   11143  1.1  christos 
   11144  1.1  christos   if (dirmips->tls_type == 0)
   11145  1.1  christos     dirmips->tls_type = indmips->tls_type;
   11146  1.1  christos }
   11147  1.1  christos 
   11148  1.1  christos #define PDR_SIZE 32
   11150  1.1  christos 
   11151  1.1  christos bfd_boolean
   11152  1.1  christos _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
   11153  1.1  christos 			    struct bfd_link_info *info)
   11154  1.1  christos {
   11155  1.1  christos   asection *o;
   11156  1.1  christos   bfd_boolean ret = FALSE;
   11157  1.1  christos   unsigned char *tdata;
   11158  1.1  christos   size_t i, skip;
   11159  1.1  christos 
   11160  1.1  christos   o = bfd_get_section_by_name (abfd, ".pdr");
   11161  1.1  christos   if (! o)
   11162  1.1  christos     return FALSE;
   11163  1.1  christos   if (o->size == 0)
   11164  1.1  christos     return FALSE;
   11165  1.1  christos   if (o->size % PDR_SIZE != 0)
   11166  1.1  christos     return FALSE;
   11167  1.1  christos   if (o->output_section != NULL
   11168  1.1  christos       && bfd_is_abs_section (o->output_section))
   11169  1.1  christos     return FALSE;
   11170  1.1  christos 
   11171  1.1  christos   tdata = bfd_zmalloc (o->size / PDR_SIZE);
   11172  1.1  christos   if (! tdata)
   11173  1.1  christos     return FALSE;
   11174  1.1  christos 
   11175  1.1  christos   cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
   11176  1.1  christos 					    info->keep_memory);
   11177  1.1  christos   if (!cookie->rels)
   11178  1.1  christos     {
   11179  1.1  christos       free (tdata);
   11180  1.1  christos       return FALSE;
   11181  1.1  christos     }
   11182  1.1  christos 
   11183  1.1  christos   cookie->rel = cookie->rels;
   11184  1.1  christos   cookie->relend = cookie->rels + o->reloc_count;
   11185  1.1  christos 
   11186  1.1  christos   for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
   11187  1.1  christos     {
   11188  1.1  christos       if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
   11189  1.1  christos 	{
   11190  1.1  christos 	  tdata[i] = 1;
   11191  1.1  christos 	  skip ++;
   11192  1.1  christos 	}
   11193  1.1  christos     }
   11194  1.1  christos 
   11195  1.1  christos   if (skip != 0)
   11196  1.1  christos     {
   11197  1.1  christos       mips_elf_section_data (o)->u.tdata = tdata;
   11198  1.1  christos       o->size -= skip * PDR_SIZE;
   11199  1.1  christos       ret = TRUE;
   11200  1.1  christos     }
   11201  1.1  christos   else
   11202  1.1  christos     free (tdata);
   11203  1.1  christos 
   11204  1.1  christos   if (! info->keep_memory)
   11205  1.1  christos     free (cookie->rels);
   11206  1.1  christos 
   11207  1.1  christos   return ret;
   11208  1.1  christos }
   11209  1.1  christos 
   11210  1.1  christos bfd_boolean
   11211  1.1  christos _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
   11212  1.1  christos {
   11213  1.1  christos   if (strcmp (sec->name, ".pdr") == 0)
   11214  1.1  christos     return TRUE;
   11215  1.1  christos   return FALSE;
   11216  1.1  christos }
   11217  1.1  christos 
   11218  1.1  christos bfd_boolean
   11219  1.1  christos _bfd_mips_elf_write_section (bfd *output_bfd,
   11220  1.1  christos 			     struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   11221  1.1  christos                              asection *sec, bfd_byte *contents)
   11222  1.1  christos {
   11223  1.1  christos   bfd_byte *to, *from, *end;
   11224  1.1  christos   int i;
   11225  1.1  christos 
   11226  1.1  christos   if (strcmp (sec->name, ".pdr") != 0)
   11227  1.1  christos     return FALSE;
   11228  1.1  christos 
   11229  1.1  christos   if (mips_elf_section_data (sec)->u.tdata == NULL)
   11230  1.1  christos     return FALSE;
   11231  1.1  christos 
   11232  1.1  christos   to = contents;
   11233  1.1  christos   end = contents + sec->size;
   11234  1.1  christos   for (from = contents, i = 0;
   11235  1.1  christos        from < end;
   11236  1.1  christos        from += PDR_SIZE, i++)
   11237  1.1  christos     {
   11238  1.1  christos       if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
   11239  1.1  christos 	continue;
   11240  1.1  christos       if (to != from)
   11241  1.1  christos 	memcpy (to, from, PDR_SIZE);
   11242  1.1  christos       to += PDR_SIZE;
   11243  1.1  christos     }
   11244  1.1  christos   bfd_set_section_contents (output_bfd, sec->output_section, contents,
   11245  1.1  christos 			    sec->output_offset, sec->size);
   11246  1.1  christos   return TRUE;
   11247  1.1  christos }
   11248  1.1  christos 
   11249  1.1  christos /* MIPS ELF uses a special find_nearest_line routine in order the
   11251  1.1  christos    handle the ECOFF debugging information.  */
   11252  1.1  christos 
   11253  1.1  christos struct mips_elf_find_line
   11254  1.1  christos {
   11255  1.1  christos   struct ecoff_debug_info d;
   11256  1.1  christos   struct ecoff_find_line i;
   11257  1.1  christos };
   11258  1.1  christos 
   11259  1.1  christos bfd_boolean
   11260  1.1  christos _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
   11261  1.1  christos 				 asymbol **symbols, bfd_vma offset,
   11262  1.1  christos 				 const char **filename_ptr,
   11263  1.1  christos 				 const char **functionname_ptr,
   11264  1.1  christos 				 unsigned int *line_ptr)
   11265  1.1  christos {
   11266  1.1  christos   asection *msec;
   11267  1.1  christos 
   11268  1.1  christos   if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
   11269  1.1  christos 				     filename_ptr, functionname_ptr,
   11270  1.1  christos 				     line_ptr))
   11271  1.1  christos     return TRUE;
   11272  1.1  christos 
   11273  1.1  christos   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
   11274  1.1  christos 				     filename_ptr, functionname_ptr,
   11275  1.1  christos 				     line_ptr, ABI_64_P (abfd) ? 8 : 0,
   11276  1.1  christos 				     &elf_tdata (abfd)->dwarf2_find_line_info))
   11277  1.1  christos     return TRUE;
   11278  1.1  christos 
   11279  1.1  christos   msec = bfd_get_section_by_name (abfd, ".mdebug");
   11280  1.1  christos   if (msec != NULL)
   11281  1.1  christos     {
   11282  1.1  christos       flagword origflags;
   11283  1.1  christos       struct mips_elf_find_line *fi;
   11284  1.1  christos       const struct ecoff_debug_swap * const swap =
   11285  1.1  christos 	get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
   11286  1.1  christos 
   11287  1.1  christos       /* If we are called during a link, mips_elf_final_link may have
   11288  1.1  christos 	 cleared the SEC_HAS_CONTENTS field.  We force it back on here
   11289  1.1  christos 	 if appropriate (which it normally will be).  */
   11290  1.1  christos       origflags = msec->flags;
   11291  1.1  christos       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
   11292  1.1  christos 	msec->flags |= SEC_HAS_CONTENTS;
   11293  1.1  christos 
   11294  1.1  christos       fi = elf_tdata (abfd)->find_line_info;
   11295  1.1  christos       if (fi == NULL)
   11296  1.1  christos 	{
   11297  1.1  christos 	  bfd_size_type external_fdr_size;
   11298  1.1  christos 	  char *fraw_src;
   11299  1.1  christos 	  char *fraw_end;
   11300  1.1  christos 	  struct fdr *fdr_ptr;
   11301  1.1  christos 	  bfd_size_type amt = sizeof (struct mips_elf_find_line);
   11302  1.1  christos 
   11303  1.1  christos 	  fi = bfd_zalloc (abfd, amt);
   11304  1.1  christos 	  if (fi == NULL)
   11305  1.1  christos 	    {
   11306  1.1  christos 	      msec->flags = origflags;
   11307  1.1  christos 	      return FALSE;
   11308  1.1  christos 	    }
   11309  1.1  christos 
   11310  1.1  christos 	  if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
   11311  1.1  christos 	    {
   11312  1.1  christos 	      msec->flags = origflags;
   11313  1.1  christos 	      return FALSE;
   11314  1.1  christos 	    }
   11315  1.1  christos 
   11316  1.1  christos 	  /* Swap in the FDR information.  */
   11317  1.1  christos 	  amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
   11318  1.1  christos 	  fi->d.fdr = bfd_alloc (abfd, amt);
   11319  1.1  christos 	  if (fi->d.fdr == NULL)
   11320  1.1  christos 	    {
   11321  1.1  christos 	      msec->flags = origflags;
   11322  1.1  christos 	      return FALSE;
   11323  1.1  christos 	    }
   11324  1.1  christos 	  external_fdr_size = swap->external_fdr_size;
   11325  1.1  christos 	  fdr_ptr = fi->d.fdr;
   11326  1.1  christos 	  fraw_src = (char *) fi->d.external_fdr;
   11327  1.1  christos 	  fraw_end = (fraw_src
   11328  1.1  christos 		      + fi->d.symbolic_header.ifdMax * external_fdr_size);
   11329  1.1  christos 	  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
   11330  1.1  christos 	    (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
   11331  1.1  christos 
   11332  1.1  christos 	  elf_tdata (abfd)->find_line_info = fi;
   11333  1.1  christos 
   11334  1.1  christos 	  /* Note that we don't bother to ever free this information.
   11335  1.1  christos              find_nearest_line is either called all the time, as in
   11336  1.1  christos              objdump -l, so the information should be saved, or it is
   11337  1.1  christos              rarely called, as in ld error messages, so the memory
   11338  1.1  christos              wasted is unimportant.  Still, it would probably be a
   11339  1.1  christos              good idea for free_cached_info to throw it away.  */
   11340  1.1  christos 	}
   11341  1.1  christos 
   11342  1.1  christos       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
   11343  1.1  christos 				  &fi->i, filename_ptr, functionname_ptr,
   11344  1.1  christos 				  line_ptr))
   11345  1.1  christos 	{
   11346  1.1  christos 	  msec->flags = origflags;
   11347  1.1  christos 	  return TRUE;
   11348  1.1  christos 	}
   11349  1.1  christos 
   11350  1.1  christos       msec->flags = origflags;
   11351  1.1  christos     }
   11352  1.1  christos 
   11353  1.1  christos   /* Fall back on the generic ELF find_nearest_line routine.  */
   11354  1.1  christos 
   11355  1.1  christos   return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
   11356  1.1  christos 				     filename_ptr, functionname_ptr,
   11357  1.1  christos 				     line_ptr);
   11358  1.1  christos }
   11359  1.1  christos 
   11360  1.1  christos bfd_boolean
   11361  1.1  christos _bfd_mips_elf_find_inliner_info (bfd *abfd,
   11362  1.1  christos 				 const char **filename_ptr,
   11363  1.1  christos 				 const char **functionname_ptr,
   11364  1.1  christos 				 unsigned int *line_ptr)
   11365  1.1  christos {
   11366  1.1  christos   bfd_boolean found;
   11367  1.1  christos   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   11368  1.1  christos 					 functionname_ptr, line_ptr,
   11369  1.1  christos 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   11370  1.1  christos   return found;
   11371  1.1  christos }
   11372  1.1  christos 
   11373  1.1  christos 
   11374  1.1  christos /* When are writing out the .options or .MIPS.options section,
   11376  1.1  christos    remember the bytes we are writing out, so that we can install the
   11377  1.1  christos    GP value in the section_processing routine.  */
   11378  1.1  christos 
   11379  1.1  christos bfd_boolean
   11380  1.1  christos _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
   11381  1.1  christos 				    const void *location,
   11382  1.1  christos 				    file_ptr offset, bfd_size_type count)
   11383  1.1  christos {
   11384  1.1  christos   if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
   11385  1.1  christos     {
   11386  1.1  christos       bfd_byte *c;
   11387  1.1  christos 
   11388  1.1  christos       if (elf_section_data (section) == NULL)
   11389  1.1  christos 	{
   11390  1.1  christos 	  bfd_size_type amt = sizeof (struct bfd_elf_section_data);
   11391  1.1  christos 	  section->used_by_bfd = bfd_zalloc (abfd, amt);
   11392  1.1  christos 	  if (elf_section_data (section) == NULL)
   11393  1.1  christos 	    return FALSE;
   11394  1.1  christos 	}
   11395  1.1  christos       c = mips_elf_section_data (section)->u.tdata;
   11396  1.1  christos       if (c == NULL)
   11397  1.1  christos 	{
   11398  1.1  christos 	  c = bfd_zalloc (abfd, section->size);
   11399  1.1  christos 	  if (c == NULL)
   11400  1.1  christos 	    return FALSE;
   11401  1.1  christos 	  mips_elf_section_data (section)->u.tdata = c;
   11402  1.1  christos 	}
   11403  1.1  christos 
   11404  1.1  christos       memcpy (c + offset, location, count);
   11405  1.1  christos     }
   11406  1.1  christos 
   11407  1.1  christos   return _bfd_elf_set_section_contents (abfd, section, location, offset,
   11408  1.1  christos 					count);
   11409  1.1  christos }
   11410  1.1  christos 
   11411  1.1  christos /* This is almost identical to bfd_generic_get_... except that some
   11412  1.1  christos    MIPS relocations need to be handled specially.  Sigh.  */
   11413  1.1  christos 
   11414  1.1  christos bfd_byte *
   11415  1.1  christos _bfd_elf_mips_get_relocated_section_contents
   11416  1.1  christos   (bfd *abfd,
   11417  1.1  christos    struct bfd_link_info *link_info,
   11418  1.1  christos    struct bfd_link_order *link_order,
   11419  1.1  christos    bfd_byte *data,
   11420  1.1  christos    bfd_boolean relocatable,
   11421  1.1  christos    asymbol **symbols)
   11422  1.1  christos {
   11423  1.1  christos   /* Get enough memory to hold the stuff */
   11424  1.1  christos   bfd *input_bfd = link_order->u.indirect.section->owner;
   11425  1.1  christos   asection *input_section = link_order->u.indirect.section;
   11426  1.1  christos   bfd_size_type sz;
   11427  1.1  christos 
   11428  1.1  christos   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
   11429  1.1  christos   arelent **reloc_vector = NULL;
   11430  1.1  christos   long reloc_count;
   11431  1.1  christos 
   11432  1.1  christos   if (reloc_size < 0)
   11433  1.1  christos     goto error_return;
   11434  1.1  christos 
   11435  1.1  christos   reloc_vector = bfd_malloc (reloc_size);
   11436  1.1  christos   if (reloc_vector == NULL && reloc_size != 0)
   11437  1.1  christos     goto error_return;
   11438  1.1  christos 
   11439  1.1  christos   /* read in the section */
   11440  1.1  christos   sz = input_section->rawsize ? input_section->rawsize : input_section->size;
   11441  1.1  christos   if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
   11442  1.1  christos     goto error_return;
   11443  1.1  christos 
   11444  1.1  christos   reloc_count = bfd_canonicalize_reloc (input_bfd,
   11445  1.1  christos 					input_section,
   11446  1.1  christos 					reloc_vector,
   11447  1.1  christos 					symbols);
   11448  1.1  christos   if (reloc_count < 0)
   11449  1.1  christos     goto error_return;
   11450  1.1  christos 
   11451  1.1  christos   if (reloc_count > 0)
   11452  1.1  christos     {
   11453  1.1  christos       arelent **parent;
   11454  1.1  christos       /* for mips */
   11455  1.1  christos       int gp_found;
   11456  1.1  christos       bfd_vma gp = 0x12345678;	/* initialize just to shut gcc up */
   11457  1.1  christos 
   11458  1.1  christos       {
   11459  1.1  christos 	struct bfd_hash_entry *h;
   11460  1.1  christos 	struct bfd_link_hash_entry *lh;
   11461  1.1  christos 	/* Skip all this stuff if we aren't mixing formats.  */
   11462  1.1  christos 	if (abfd && input_bfd
   11463  1.1  christos 	    && abfd->xvec == input_bfd->xvec)
   11464  1.1  christos 	  lh = 0;
   11465  1.1  christos 	else
   11466  1.1  christos 	  {
   11467  1.1  christos 	    h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
   11468  1.1  christos 	    lh = (struct bfd_link_hash_entry *) h;
   11469  1.1  christos 	  }
   11470  1.1  christos       lookup:
   11471  1.1  christos 	if (lh)
   11472  1.1  christos 	  {
   11473  1.1  christos 	    switch (lh->type)
   11474  1.1  christos 	      {
   11475  1.1  christos 	      case bfd_link_hash_undefined:
   11476  1.1  christos 	      case bfd_link_hash_undefweak:
   11477  1.1  christos 	      case bfd_link_hash_common:
   11478  1.1  christos 		gp_found = 0;
   11479  1.1  christos 		break;
   11480  1.1  christos 	      case bfd_link_hash_defined:
   11481  1.1  christos 	      case bfd_link_hash_defweak:
   11482  1.1  christos 		gp_found = 1;
   11483  1.1  christos 		gp = lh->u.def.value;
   11484  1.1  christos 		break;
   11485  1.1  christos 	      case bfd_link_hash_indirect:
   11486  1.1  christos 	      case bfd_link_hash_warning:
   11487  1.1  christos 		lh = lh->u.i.link;
   11488  1.1  christos 		/* @@FIXME  ignoring warning for now */
   11489  1.1  christos 		goto lookup;
   11490  1.1  christos 	      case bfd_link_hash_new:
   11491  1.1  christos 	      default:
   11492  1.1  christos 		abort ();
   11493  1.1  christos 	      }
   11494  1.1  christos 	  }
   11495  1.1  christos 	else
   11496  1.1  christos 	  gp_found = 0;
   11497  1.1  christos       }
   11498  1.1  christos       /* end mips */
   11499  1.1  christos       for (parent = reloc_vector; *parent != NULL; parent++)
   11500  1.1  christos 	{
   11501  1.1  christos 	  char *error_message = NULL;
   11502  1.1  christos 	  bfd_reloc_status_type r;
   11503  1.1  christos 
   11504  1.1  christos 	  /* Specific to MIPS: Deal with relocation types that require
   11505  1.1  christos 	     knowing the gp of the output bfd.  */
   11506  1.1  christos 	  asymbol *sym = *(*parent)->sym_ptr_ptr;
   11507  1.1  christos 
   11508  1.1  christos 	  /* If we've managed to find the gp and have a special
   11509  1.1  christos 	     function for the relocation then go ahead, else default
   11510  1.1  christos 	     to the generic handling.  */
   11511  1.1  christos 	  if (gp_found
   11512  1.1  christos 	      && (*parent)->howto->special_function
   11513  1.1  christos 	      == _bfd_mips_elf32_gprel16_reloc)
   11514  1.1  christos 	    r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
   11515  1.1  christos 					       input_section, relocatable,
   11516  1.1  christos 					       data, gp);
   11517  1.1  christos 	  else
   11518  1.1  christos 	    r = bfd_perform_relocation (input_bfd, *parent, data,
   11519  1.1  christos 					input_section,
   11520  1.1  christos 					relocatable ? abfd : NULL,
   11521  1.1  christos 					&error_message);
   11522  1.1  christos 
   11523  1.1  christos 	  if (relocatable)
   11524  1.1  christos 	    {
   11525  1.1  christos 	      asection *os = input_section->output_section;
   11526  1.1  christos 
   11527  1.1  christos 	      /* A partial link, so keep the relocs */
   11528  1.1  christos 	      os->orelocation[os->reloc_count] = *parent;
   11529  1.1  christos 	      os->reloc_count++;
   11530  1.1  christos 	    }
   11531  1.1  christos 
   11532  1.1  christos 	  if (r != bfd_reloc_ok)
   11533  1.1  christos 	    {
   11534  1.1  christos 	      switch (r)
   11535  1.1  christos 		{
   11536  1.1  christos 		case bfd_reloc_undefined:
   11537  1.1  christos 		  if (!((*link_info->callbacks->undefined_symbol)
   11538  1.1  christos 			(link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
   11539  1.1  christos 			 input_bfd, input_section, (*parent)->address, TRUE)))
   11540  1.1  christos 		    goto error_return;
   11541  1.1  christos 		  break;
   11542  1.1  christos 		case bfd_reloc_dangerous:
   11543  1.1  christos 		  BFD_ASSERT (error_message != NULL);
   11544  1.1  christos 		  if (!((*link_info->callbacks->reloc_dangerous)
   11545  1.1  christos 			(link_info, error_message, input_bfd, input_section,
   11546  1.1  christos 			 (*parent)->address)))
   11547  1.1  christos 		    goto error_return;
   11548  1.1  christos 		  break;
   11549  1.1  christos 		case bfd_reloc_overflow:
   11550  1.1  christos 		  if (!((*link_info->callbacks->reloc_overflow)
   11551  1.1  christos 			(link_info, NULL,
   11552  1.1  christos 			 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
   11553  1.1  christos 			 (*parent)->howto->name, (*parent)->addend,
   11554  1.1  christos 			 input_bfd, input_section, (*parent)->address)))
   11555  1.1  christos 		    goto error_return;
   11556  1.1  christos 		  break;
   11557  1.1  christos 		case bfd_reloc_outofrange:
   11558  1.1  christos 		default:
   11559  1.1  christos 		  abort ();
   11560  1.1  christos 		  break;
   11561  1.1  christos 		}
   11562  1.1  christos 
   11563  1.1  christos 	    }
   11564  1.1  christos 	}
   11565  1.1  christos     }
   11566  1.1  christos   if (reloc_vector != NULL)
   11567  1.1  christos     free (reloc_vector);
   11568  1.1  christos   return data;
   11569  1.1  christos 
   11570  1.1  christos error_return:
   11571  1.1  christos   if (reloc_vector != NULL)
   11572  1.1  christos     free (reloc_vector);
   11573  1.1  christos   return NULL;
   11574  1.1  christos }
   11575  1.1  christos 
   11576  1.1  christos /* Create a MIPS ELF linker hash table.  */
   11578  1.1  christos 
   11579  1.1  christos struct bfd_link_hash_table *
   11580  1.1  christos _bfd_mips_elf_link_hash_table_create (bfd *abfd)
   11581  1.1  christos {
   11582  1.1  christos   struct mips_elf_link_hash_table *ret;
   11583  1.1  christos   bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
   11584  1.1  christos 
   11585  1.1  christos   ret = bfd_malloc (amt);
   11586  1.1  christos   if (ret == NULL)
   11587  1.1  christos     return NULL;
   11588  1.1  christos 
   11589  1.1  christos   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
   11590  1.1  christos 				      mips_elf_link_hash_newfunc,
   11591  1.1  christos 				      sizeof (struct mips_elf_link_hash_entry),
   11592  1.1  christos 				      MIPS_ELF_DATA))
   11593  1.1  christos     {
   11594  1.1  christos       free (ret);
   11595  1.1  christos       return NULL;
   11596  1.1  christos     }
   11597  1.1  christos 
   11598  1.1  christos #if 0
   11599  1.1  christos   /* We no longer use this.  */
   11600  1.1  christos   for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
   11601  1.1  christos     ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
   11602  1.1  christos #endif
   11603  1.1  christos   ret->procedure_count = 0;
   11604  1.1  christos   ret->compact_rel_size = 0;
   11605  1.1  christos   ret->use_rld_obj_head = FALSE;
   11606  1.1  christos   ret->rld_value = 0;
   11607  1.1  christos   ret->mips16_stubs_seen = FALSE;
   11608  1.1  christos   ret->use_plts_and_copy_relocs = FALSE;
   11609  1.1  christos   ret->is_vxworks = FALSE;
   11610  1.1  christos   ret->small_data_overflow_reported = FALSE;
   11611  1.1  christos   ret->srelbss = NULL;
   11612  1.1  christos   ret->sdynbss = NULL;
   11613  1.1  christos   ret->srelplt = NULL;
   11614  1.1  christos   ret->srelplt2 = NULL;
   11615  1.1  christos   ret->sgotplt = NULL;
   11616  1.1  christos   ret->splt = NULL;
   11617  1.1  christos   ret->sstubs = NULL;
   11618  1.1  christos   ret->sgot = NULL;
   11619  1.1  christos   ret->got_info = NULL;
   11620  1.1  christos   ret->plt_header_size = 0;
   11621  1.1  christos   ret->plt_entry_size = 0;
   11622  1.1  christos   ret->lazy_stub_count = 0;
   11623  1.1  christos   ret->function_stub_size = 0;
   11624  1.1  christos   ret->strampoline = NULL;
   11625  1.1  christos   ret->la25_stubs = NULL;
   11626  1.1  christos   ret->add_stub_section = NULL;
   11627  1.1  christos 
   11628  1.1  christos   return &ret->root.root;
   11629  1.1  christos }
   11630  1.1  christos 
   11631  1.1  christos /* Likewise, but indicate that the target is VxWorks.  */
   11632  1.1  christos 
   11633  1.1  christos struct bfd_link_hash_table *
   11634  1.1  christos _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
   11635  1.1  christos {
   11636  1.1  christos   struct bfd_link_hash_table *ret;
   11637  1.1  christos 
   11638  1.1  christos   ret = _bfd_mips_elf_link_hash_table_create (abfd);
   11639  1.1  christos   if (ret)
   11640  1.1  christos     {
   11641  1.1  christos       struct mips_elf_link_hash_table *htab;
   11642  1.1  christos 
   11643  1.1  christos       htab = (struct mips_elf_link_hash_table *) ret;
   11644  1.1  christos       htab->use_plts_and_copy_relocs = TRUE;
   11645  1.1  christos       htab->is_vxworks = TRUE;
   11646  1.1  christos     }
   11647  1.1  christos   return ret;
   11648  1.1  christos }
   11649  1.1  christos 
   11650  1.1  christos /* A function that the linker calls if we are allowed to use PLTs
   11651  1.1  christos    and copy relocs.  */
   11652  1.1  christos 
   11653  1.1  christos void
   11654  1.1  christos _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
   11655  1.1  christos {
   11656  1.1  christos   mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
   11657  1.1  christos }
   11658  1.1  christos 
   11659  1.1  christos /* We need to use a special link routine to handle the .reginfo and
   11661  1.1  christos    the .mdebug sections.  We need to merge all instances of these
   11662  1.1  christos    sections together, not write them all out sequentially.  */
   11663  1.1  christos 
   11664  1.1  christos bfd_boolean
   11665  1.1  christos _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   11666  1.1  christos {
   11667  1.1  christos   asection *o;
   11668  1.1  christos   struct bfd_link_order *p;
   11669  1.1  christos   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
   11670  1.1  christos   asection *rtproc_sec;
   11671  1.1  christos   Elf32_RegInfo reginfo;
   11672  1.1  christos   struct ecoff_debug_info debug;
   11673  1.1  christos   struct mips_htab_traverse_info hti;
   11674  1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11675  1.1  christos   const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
   11676  1.1  christos   HDRR *symhdr = &debug.symbolic_header;
   11677  1.1  christos   void *mdebug_handle = NULL;
   11678  1.1  christos   asection *s;
   11679  1.1  christos   EXTR esym;
   11680  1.1  christos   unsigned int i;
   11681  1.1  christos   bfd_size_type amt;
   11682  1.1  christos   struct mips_elf_link_hash_table *htab;
   11683  1.1  christos 
   11684  1.1  christos   static const char * const secname[] =
   11685  1.1  christos   {
   11686  1.1  christos     ".text", ".init", ".fini", ".data",
   11687  1.1  christos     ".rodata", ".sdata", ".sbss", ".bss"
   11688  1.1  christos   };
   11689  1.1  christos   static const int sc[] =
   11690  1.1  christos   {
   11691  1.1  christos     scText, scInit, scFini, scData,
   11692  1.1  christos     scRData, scSData, scSBss, scBss
   11693  1.1  christos   };
   11694  1.1  christos 
   11695  1.1  christos   /* Sort the dynamic symbols so that those with GOT entries come after
   11696  1.1  christos      those without.  */
   11697  1.1  christos   htab = mips_elf_hash_table (info);
   11698  1.1  christos   BFD_ASSERT (htab != NULL);
   11699  1.1  christos 
   11700  1.1  christos   if (!mips_elf_sort_hash_table (abfd, info))
   11701  1.1  christos     return FALSE;
   11702  1.1  christos 
   11703  1.1  christos   /* Create any scheduled LA25 stubs.  */
   11704  1.1  christos   hti.info = info;
   11705  1.1  christos   hti.output_bfd = abfd;
   11706  1.1  christos   hti.error = FALSE;
   11707  1.1  christos   htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
   11708  1.1  christos   if (hti.error)
   11709  1.1  christos     return FALSE;
   11710  1.1  christos 
   11711  1.1  christos   /* Get a value for the GP register.  */
   11712  1.1  christos   if (elf_gp (abfd) == 0)
   11713  1.1  christos     {
   11714  1.1  christos       struct bfd_link_hash_entry *h;
   11715  1.1  christos 
   11716  1.1  christos       h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
   11717  1.1  christos       if (h != NULL && h->type == bfd_link_hash_defined)
   11718  1.1  christos 	elf_gp (abfd) = (h->u.def.value
   11719  1.1  christos 			 + h->u.def.section->output_section->vma
   11720  1.1  christos 			 + h->u.def.section->output_offset);
   11721  1.1  christos       else if (htab->is_vxworks
   11722  1.1  christos 	       && (h = bfd_link_hash_lookup (info->hash,
   11723  1.1  christos 					     "_GLOBAL_OFFSET_TABLE_",
   11724  1.1  christos 					     FALSE, FALSE, TRUE))
   11725  1.1  christos 	       && h->type == bfd_link_hash_defined)
   11726  1.1  christos 	elf_gp (abfd) = (h->u.def.section->output_section->vma
   11727  1.1  christos 			 + h->u.def.section->output_offset
   11728  1.1  christos 			 + h->u.def.value);
   11729  1.1  christos       else if (info->relocatable)
   11730  1.1  christos 	{
   11731  1.1  christos 	  bfd_vma lo = MINUS_ONE;
   11732  1.1  christos 
   11733  1.1  christos 	  /* Find the GP-relative section with the lowest offset.  */
   11734  1.1  christos 	  for (o = abfd->sections; o != NULL; o = o->next)
   11735  1.1  christos 	    if (o->vma < lo
   11736  1.1  christos 		&& (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
   11737  1.1  christos 	      lo = o->vma;
   11738  1.1  christos 
   11739  1.1  christos 	  /* And calculate GP relative to that.  */
   11740  1.1  christos 	  elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
   11741  1.1  christos 	}
   11742  1.1  christos       else
   11743  1.1  christos 	{
   11744  1.1  christos 	  /* If the relocate_section function needs to do a reloc
   11745  1.1  christos 	     involving the GP value, it should make a reloc_dangerous
   11746  1.1  christos 	     callback to warn that GP is not defined.  */
   11747  1.1  christos 	}
   11748  1.1  christos     }
   11749  1.1  christos 
   11750  1.1  christos   /* Go through the sections and collect the .reginfo and .mdebug
   11751  1.1  christos      information.  */
   11752  1.1  christos   reginfo_sec = NULL;
   11753  1.1  christos   mdebug_sec = NULL;
   11754  1.1  christos   gptab_data_sec = NULL;
   11755  1.1  christos   gptab_bss_sec = NULL;
   11756  1.1  christos   for (o = abfd->sections; o != NULL; o = o->next)
   11757  1.1  christos     {
   11758  1.1  christos       if (strcmp (o->name, ".reginfo") == 0)
   11759  1.1  christos 	{
   11760  1.1  christos 	  memset (&reginfo, 0, sizeof reginfo);
   11761  1.1  christos 
   11762  1.1  christos 	  /* We have found the .reginfo section in the output file.
   11763  1.1  christos 	     Look through all the link_orders comprising it and merge
   11764  1.1  christos 	     the information together.  */
   11765  1.1  christos 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   11766  1.1  christos 	    {
   11767  1.1  christos 	      asection *input_section;
   11768  1.1  christos 	      bfd *input_bfd;
   11769  1.1  christos 	      Elf32_External_RegInfo ext;
   11770  1.1  christos 	      Elf32_RegInfo sub;
   11771  1.1  christos 
   11772  1.1  christos 	      if (p->type != bfd_indirect_link_order)
   11773  1.1  christos 		{
   11774  1.1  christos 		  if (p->type == bfd_data_link_order)
   11775  1.1  christos 		    continue;
   11776  1.1  christos 		  abort ();
   11777  1.1  christos 		}
   11778  1.1  christos 
   11779  1.1  christos 	      input_section = p->u.indirect.section;
   11780  1.1  christos 	      input_bfd = input_section->owner;
   11781  1.1  christos 
   11782  1.1  christos 	      if (! bfd_get_section_contents (input_bfd, input_section,
   11783  1.1  christos 					      &ext, 0, sizeof ext))
   11784  1.1  christos 		return FALSE;
   11785  1.1  christos 
   11786  1.1  christos 	      bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
   11787  1.1  christos 
   11788  1.1  christos 	      reginfo.ri_gprmask |= sub.ri_gprmask;
   11789  1.1  christos 	      reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
   11790  1.1  christos 	      reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
   11791  1.1  christos 	      reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
   11792  1.1  christos 	      reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
   11793  1.1  christos 
   11794  1.1  christos 	      /* ri_gp_value is set by the function
   11795  1.1  christos 		 mips_elf32_section_processing when the section is
   11796  1.1  christos 		 finally written out.  */
   11797  1.1  christos 
   11798  1.1  christos 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   11799  1.1  christos 		 elf_link_input_bfd ignores this section.  */
   11800  1.1  christos 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   11801  1.1  christos 	    }
   11802  1.1  christos 
   11803  1.1  christos 	  /* Size has been set in _bfd_mips_elf_always_size_sections.  */
   11804  1.1  christos 	  BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
   11805  1.1  christos 
   11806  1.1  christos 	  /* Skip this section later on (I don't think this currently
   11807  1.1  christos 	     matters, but someday it might).  */
   11808  1.1  christos 	  o->map_head.link_order = NULL;
   11809  1.1  christos 
   11810  1.1  christos 	  reginfo_sec = o;
   11811  1.1  christos 	}
   11812  1.1  christos 
   11813  1.1  christos       if (strcmp (o->name, ".mdebug") == 0)
   11814  1.1  christos 	{
   11815  1.1  christos 	  struct extsym_info einfo;
   11816  1.1  christos 	  bfd_vma last;
   11817  1.1  christos 
   11818  1.1  christos 	  /* We have found the .mdebug section in the output file.
   11819  1.1  christos 	     Look through all the link_orders comprising it and merge
   11820  1.1  christos 	     the information together.  */
   11821  1.1  christos 	  symhdr->magic = swap->sym_magic;
   11822  1.1  christos 	  /* FIXME: What should the version stamp be?  */
   11823  1.1  christos 	  symhdr->vstamp = 0;
   11824  1.1  christos 	  symhdr->ilineMax = 0;
   11825  1.1  christos 	  symhdr->cbLine = 0;
   11826  1.1  christos 	  symhdr->idnMax = 0;
   11827  1.1  christos 	  symhdr->ipdMax = 0;
   11828  1.1  christos 	  symhdr->isymMax = 0;
   11829  1.1  christos 	  symhdr->ioptMax = 0;
   11830  1.1  christos 	  symhdr->iauxMax = 0;
   11831  1.1  christos 	  symhdr->issMax = 0;
   11832  1.1  christos 	  symhdr->issExtMax = 0;
   11833  1.1  christos 	  symhdr->ifdMax = 0;
   11834  1.1  christos 	  symhdr->crfd = 0;
   11835  1.1  christos 	  symhdr->iextMax = 0;
   11836  1.1  christos 
   11837  1.1  christos 	  /* We accumulate the debugging information itself in the
   11838  1.1  christos 	     debug_info structure.  */
   11839  1.1  christos 	  debug.line = NULL;
   11840  1.1  christos 	  debug.external_dnr = NULL;
   11841  1.1  christos 	  debug.external_pdr = NULL;
   11842  1.1  christos 	  debug.external_sym = NULL;
   11843  1.1  christos 	  debug.external_opt = NULL;
   11844  1.1  christos 	  debug.external_aux = NULL;
   11845  1.1  christos 	  debug.ss = NULL;
   11846  1.1  christos 	  debug.ssext = debug.ssext_end = NULL;
   11847  1.1  christos 	  debug.external_fdr = NULL;
   11848  1.1  christos 	  debug.external_rfd = NULL;
   11849  1.1  christos 	  debug.external_ext = debug.external_ext_end = NULL;
   11850  1.1  christos 
   11851  1.1  christos 	  mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
   11852  1.1  christos 	  if (mdebug_handle == NULL)
   11853  1.1  christos 	    return FALSE;
   11854  1.1  christos 
   11855  1.1  christos 	  esym.jmptbl = 0;
   11856  1.1  christos 	  esym.cobol_main = 0;
   11857  1.1  christos 	  esym.weakext = 0;
   11858  1.1  christos 	  esym.reserved = 0;
   11859  1.1  christos 	  esym.ifd = ifdNil;
   11860  1.1  christos 	  esym.asym.iss = issNil;
   11861  1.1  christos 	  esym.asym.st = stLocal;
   11862  1.1  christos 	  esym.asym.reserved = 0;
   11863  1.1  christos 	  esym.asym.index = indexNil;
   11864  1.1  christos 	  last = 0;
   11865  1.1  christos 	  for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
   11866  1.1  christos 	    {
   11867  1.1  christos 	      esym.asym.sc = sc[i];
   11868  1.1  christos 	      s = bfd_get_section_by_name (abfd, secname[i]);
   11869  1.1  christos 	      if (s != NULL)
   11870  1.1  christos 		{
   11871  1.1  christos 		  esym.asym.value = s->vma;
   11872  1.1  christos 		  last = s->vma + s->size;
   11873  1.1  christos 		}
   11874  1.1  christos 	      else
   11875  1.1  christos 		esym.asym.value = last;
   11876  1.1  christos 	      if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
   11877  1.1  christos 						 secname[i], &esym))
   11878  1.1  christos 		return FALSE;
   11879  1.1  christos 	    }
   11880  1.1  christos 
   11881  1.1  christos 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   11882  1.1  christos 	    {
   11883  1.1  christos 	      asection *input_section;
   11884  1.1  christos 	      bfd *input_bfd;
   11885  1.1  christos 	      const struct ecoff_debug_swap *input_swap;
   11886  1.1  christos 	      struct ecoff_debug_info input_debug;
   11887  1.1  christos 	      char *eraw_src;
   11888  1.1  christos 	      char *eraw_end;
   11889  1.1  christos 
   11890  1.1  christos 	      if (p->type != bfd_indirect_link_order)
   11891  1.1  christos 		{
   11892  1.1  christos 		  if (p->type == bfd_data_link_order)
   11893  1.1  christos 		    continue;
   11894  1.1  christos 		  abort ();
   11895  1.1  christos 		}
   11896  1.1  christos 
   11897  1.1  christos 	      input_section = p->u.indirect.section;
   11898  1.1  christos 	      input_bfd = input_section->owner;
   11899  1.1  christos 
   11900  1.1  christos 	      if (!is_mips_elf (input_bfd))
   11901  1.1  christos 		{
   11902  1.1  christos 		  /* I don't know what a non MIPS ELF bfd would be
   11903  1.1  christos 		     doing with a .mdebug section, but I don't really
   11904  1.1  christos 		     want to deal with it.  */
   11905  1.1  christos 		  continue;
   11906  1.1  christos 		}
   11907  1.1  christos 
   11908  1.1  christos 	      input_swap = (get_elf_backend_data (input_bfd)
   11909  1.1  christos 			    ->elf_backend_ecoff_debug_swap);
   11910  1.1  christos 
   11911  1.1  christos 	      BFD_ASSERT (p->size == input_section->size);
   11912  1.1  christos 
   11913  1.1  christos 	      /* The ECOFF linking code expects that we have already
   11914  1.1  christos 		 read in the debugging information and set up an
   11915  1.1  christos 		 ecoff_debug_info structure, so we do that now.  */
   11916  1.1  christos 	      if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
   11917  1.1  christos 						   &input_debug))
   11918  1.1  christos 		return FALSE;
   11919  1.1  christos 
   11920  1.1  christos 	      if (! (bfd_ecoff_debug_accumulate
   11921  1.1  christos 		     (mdebug_handle, abfd, &debug, swap, input_bfd,
   11922  1.1  christos 		      &input_debug, input_swap, info)))
   11923  1.1  christos 		return FALSE;
   11924  1.1  christos 
   11925  1.1  christos 	      /* Loop through the external symbols.  For each one with
   11926  1.1  christos 		 interesting information, try to find the symbol in
   11927  1.1  christos 		 the linker global hash table and save the information
   11928  1.1  christos 		 for the output external symbols.  */
   11929  1.1  christos 	      eraw_src = input_debug.external_ext;
   11930  1.1  christos 	      eraw_end = (eraw_src
   11931  1.1  christos 			  + (input_debug.symbolic_header.iextMax
   11932  1.1  christos 			     * input_swap->external_ext_size));
   11933  1.1  christos 	      for (;
   11934  1.1  christos 		   eraw_src < eraw_end;
   11935  1.1  christos 		   eraw_src += input_swap->external_ext_size)
   11936  1.1  christos 		{
   11937  1.1  christos 		  EXTR ext;
   11938  1.1  christos 		  const char *name;
   11939  1.1  christos 		  struct mips_elf_link_hash_entry *h;
   11940  1.1  christos 
   11941  1.1  christos 		  (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
   11942  1.1  christos 		  if (ext.asym.sc == scNil
   11943  1.1  christos 		      || ext.asym.sc == scUndefined
   11944  1.1  christos 		      || ext.asym.sc == scSUndefined)
   11945  1.1  christos 		    continue;
   11946  1.1  christos 
   11947  1.1  christos 		  name = input_debug.ssext + ext.asym.iss;
   11948  1.1  christos 		  h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
   11949  1.1  christos 						 name, FALSE, FALSE, TRUE);
   11950  1.1  christos 		  if (h == NULL || h->esym.ifd != -2)
   11951  1.1  christos 		    continue;
   11952  1.1  christos 
   11953  1.1  christos 		  if (ext.ifd != -1)
   11954  1.1  christos 		    {
   11955  1.1  christos 		      BFD_ASSERT (ext.ifd
   11956  1.1  christos 				  < input_debug.symbolic_header.ifdMax);
   11957  1.1  christos 		      ext.ifd = input_debug.ifdmap[ext.ifd];
   11958  1.1  christos 		    }
   11959  1.1  christos 
   11960  1.1  christos 		  h->esym = ext;
   11961  1.1  christos 		}
   11962  1.1  christos 
   11963  1.1  christos 	      /* Free up the information we just read.  */
   11964  1.1  christos 	      free (input_debug.line);
   11965  1.1  christos 	      free (input_debug.external_dnr);
   11966  1.1  christos 	      free (input_debug.external_pdr);
   11967  1.1  christos 	      free (input_debug.external_sym);
   11968  1.1  christos 	      free (input_debug.external_opt);
   11969  1.1  christos 	      free (input_debug.external_aux);
   11970  1.1  christos 	      free (input_debug.ss);
   11971  1.1  christos 	      free (input_debug.ssext);
   11972  1.1  christos 	      free (input_debug.external_fdr);
   11973  1.1  christos 	      free (input_debug.external_rfd);
   11974  1.1  christos 	      free (input_debug.external_ext);
   11975  1.1  christos 
   11976  1.1  christos 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   11977  1.1  christos 		 elf_link_input_bfd ignores this section.  */
   11978  1.1  christos 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   11979  1.1  christos 	    }
   11980  1.1  christos 
   11981  1.1  christos 	  if (SGI_COMPAT (abfd) && info->shared)
   11982  1.1  christos 	    {
   11983  1.1  christos 	      /* Create .rtproc section.  */
   11984  1.1  christos 	      rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
   11985  1.1  christos 	      if (rtproc_sec == NULL)
   11986  1.1  christos 		{
   11987  1.1  christos 		  flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
   11988  1.1  christos 				    | SEC_LINKER_CREATED | SEC_READONLY);
   11989  1.1  christos 
   11990  1.1  christos 		  rtproc_sec = bfd_make_section_with_flags (abfd,
   11991  1.1  christos 							    ".rtproc",
   11992  1.1  christos 							    flags);
   11993  1.1  christos 		  if (rtproc_sec == NULL
   11994  1.1  christos 		      || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
   11995  1.1  christos 		    return FALSE;
   11996  1.1  christos 		}
   11997  1.1  christos 
   11998  1.1  christos 	      if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
   11999  1.1  christos 						     info, rtproc_sec,
   12000  1.1  christos 						     &debug))
   12001  1.1  christos 		return FALSE;
   12002  1.1  christos 	    }
   12003  1.1  christos 
   12004  1.1  christos 	  /* Build the external symbol information.  */
   12005  1.1  christos 	  einfo.abfd = abfd;
   12006  1.1  christos 	  einfo.info = info;
   12007  1.1  christos 	  einfo.debug = &debug;
   12008  1.1  christos 	  einfo.swap = swap;
   12009  1.1  christos 	  einfo.failed = FALSE;
   12010  1.1  christos 	  mips_elf_link_hash_traverse (mips_elf_hash_table (info),
   12011  1.1  christos 				       mips_elf_output_extsym, &einfo);
   12012  1.1  christos 	  if (einfo.failed)
   12013  1.1  christos 	    return FALSE;
   12014  1.1  christos 
   12015  1.1  christos 	  /* Set the size of the .mdebug section.  */
   12016  1.1  christos 	  o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
   12017  1.1  christos 
   12018  1.1  christos 	  /* Skip this section later on (I don't think this currently
   12019  1.1  christos 	     matters, but someday it might).  */
   12020  1.1  christos 	  o->map_head.link_order = NULL;
   12021  1.1  christos 
   12022  1.1  christos 	  mdebug_sec = o;
   12023  1.1  christos 	}
   12024  1.1  christos 
   12025  1.1  christos       if (CONST_STRNEQ (o->name, ".gptab."))
   12026  1.1  christos 	{
   12027  1.1  christos 	  const char *subname;
   12028  1.1  christos 	  unsigned int c;
   12029  1.1  christos 	  Elf32_gptab *tab;
   12030  1.1  christos 	  Elf32_External_gptab *ext_tab;
   12031  1.1  christos 	  unsigned int j;
   12032  1.1  christos 
   12033  1.1  christos 	  /* The .gptab.sdata and .gptab.sbss sections hold
   12034  1.1  christos 	     information describing how the small data area would
   12035  1.1  christos 	     change depending upon the -G switch.  These sections
   12036  1.1  christos 	     not used in executables files.  */
   12037  1.1  christos 	  if (! info->relocatable)
   12038  1.1  christos 	    {
   12039  1.1  christos 	      for (p = o->map_head.link_order; p != NULL; p = p->next)
   12040  1.1  christos 		{
   12041  1.1  christos 		  asection *input_section;
   12042  1.1  christos 
   12043  1.1  christos 		  if (p->type != bfd_indirect_link_order)
   12044  1.1  christos 		    {
   12045  1.1  christos 		      if (p->type == bfd_data_link_order)
   12046  1.1  christos 			continue;
   12047  1.1  christos 		      abort ();
   12048  1.1  christos 		    }
   12049  1.1  christos 
   12050  1.1  christos 		  input_section = p->u.indirect.section;
   12051  1.1  christos 
   12052  1.1  christos 		  /* Hack: reset the SEC_HAS_CONTENTS flag so that
   12053  1.1  christos 		     elf_link_input_bfd ignores this section.  */
   12054  1.1  christos 		  input_section->flags &= ~SEC_HAS_CONTENTS;
   12055  1.1  christos 		}
   12056  1.1  christos 
   12057  1.1  christos 	      /* Skip this section later on (I don't think this
   12058  1.1  christos 		 currently matters, but someday it might).  */
   12059  1.1  christos 	      o->map_head.link_order = NULL;
   12060  1.1  christos 
   12061  1.1  christos 	      /* Really remove the section.  */
   12062  1.1  christos 	      bfd_section_list_remove (abfd, o);
   12063  1.1  christos 	      --abfd->section_count;
   12064  1.1  christos 
   12065  1.1  christos 	      continue;
   12066  1.1  christos 	    }
   12067  1.1  christos 
   12068  1.1  christos 	  /* There is one gptab for initialized data, and one for
   12069  1.1  christos 	     uninitialized data.  */
   12070  1.1  christos 	  if (strcmp (o->name, ".gptab.sdata") == 0)
   12071  1.1  christos 	    gptab_data_sec = o;
   12072  1.1  christos 	  else if (strcmp (o->name, ".gptab.sbss") == 0)
   12073  1.1  christos 	    gptab_bss_sec = o;
   12074  1.1  christos 	  else
   12075  1.1  christos 	    {
   12076  1.1  christos 	      (*_bfd_error_handler)
   12077  1.1  christos 		(_("%s: illegal section name `%s'"),
   12078  1.1  christos 		 bfd_get_filename (abfd), o->name);
   12079  1.1  christos 	      bfd_set_error (bfd_error_nonrepresentable_section);
   12080  1.1  christos 	      return FALSE;
   12081  1.1  christos 	    }
   12082  1.1  christos 
   12083  1.1  christos 	  /* The linker script always combines .gptab.data and
   12084  1.1  christos 	     .gptab.sdata into .gptab.sdata, and likewise for
   12085  1.1  christos 	     .gptab.bss and .gptab.sbss.  It is possible that there is
   12086  1.1  christos 	     no .sdata or .sbss section in the output file, in which
   12087  1.1  christos 	     case we must change the name of the output section.  */
   12088  1.1  christos 	  subname = o->name + sizeof ".gptab" - 1;
   12089  1.1  christos 	  if (bfd_get_section_by_name (abfd, subname) == NULL)
   12090  1.1  christos 	    {
   12091  1.1  christos 	      if (o == gptab_data_sec)
   12092  1.1  christos 		o->name = ".gptab.data";
   12093  1.1  christos 	      else
   12094  1.1  christos 		o->name = ".gptab.bss";
   12095  1.1  christos 	      subname = o->name + sizeof ".gptab" - 1;
   12096  1.1  christos 	      BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
   12097  1.1  christos 	    }
   12098  1.1  christos 
   12099  1.1  christos 	  /* Set up the first entry.  */
   12100  1.1  christos 	  c = 1;
   12101  1.1  christos 	  amt = c * sizeof (Elf32_gptab);
   12102  1.1  christos 	  tab = bfd_malloc (amt);
   12103  1.1  christos 	  if (tab == NULL)
   12104  1.1  christos 	    return FALSE;
   12105  1.1  christos 	  tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
   12106  1.1  christos 	  tab[0].gt_header.gt_unused = 0;
   12107  1.1  christos 
   12108  1.1  christos 	  /* Combine the input sections.  */
   12109  1.1  christos 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
   12110  1.1  christos 	    {
   12111  1.1  christos 	      asection *input_section;
   12112  1.1  christos 	      bfd *input_bfd;
   12113  1.1  christos 	      bfd_size_type size;
   12114  1.1  christos 	      unsigned long last;
   12115  1.1  christos 	      bfd_size_type gpentry;
   12116  1.1  christos 
   12117  1.1  christos 	      if (p->type != bfd_indirect_link_order)
   12118  1.1  christos 		{
   12119  1.1  christos 		  if (p->type == bfd_data_link_order)
   12120  1.1  christos 		    continue;
   12121  1.1  christos 		  abort ();
   12122  1.1  christos 		}
   12123  1.1  christos 
   12124  1.1  christos 	      input_section = p->u.indirect.section;
   12125  1.1  christos 	      input_bfd = input_section->owner;
   12126  1.1  christos 
   12127  1.1  christos 	      /* Combine the gptab entries for this input section one
   12128  1.1  christos 		 by one.  We know that the input gptab entries are
   12129  1.1  christos 		 sorted by ascending -G value.  */
   12130  1.1  christos 	      size = input_section->size;
   12131  1.1  christos 	      last = 0;
   12132  1.1  christos 	      for (gpentry = sizeof (Elf32_External_gptab);
   12133  1.1  christos 		   gpentry < size;
   12134  1.1  christos 		   gpentry += sizeof (Elf32_External_gptab))
   12135  1.1  christos 		{
   12136  1.1  christos 		  Elf32_External_gptab ext_gptab;
   12137  1.1  christos 		  Elf32_gptab int_gptab;
   12138  1.1  christos 		  unsigned long val;
   12139  1.1  christos 		  unsigned long add;
   12140  1.1  christos 		  bfd_boolean exact;
   12141  1.1  christos 		  unsigned int look;
   12142  1.1  christos 
   12143  1.1  christos 		  if (! (bfd_get_section_contents
   12144  1.1  christos 			 (input_bfd, input_section, &ext_gptab, gpentry,
   12145  1.1  christos 			  sizeof (Elf32_External_gptab))))
   12146  1.1  christos 		    {
   12147  1.1  christos 		      free (tab);
   12148  1.1  christos 		      return FALSE;
   12149  1.1  christos 		    }
   12150  1.1  christos 
   12151  1.1  christos 		  bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
   12152  1.1  christos 						&int_gptab);
   12153  1.1  christos 		  val = int_gptab.gt_entry.gt_g_value;
   12154  1.1  christos 		  add = int_gptab.gt_entry.gt_bytes - last;
   12155  1.1  christos 
   12156  1.1  christos 		  exact = FALSE;
   12157  1.1  christos 		  for (look = 1; look < c; look++)
   12158  1.1  christos 		    {
   12159  1.1  christos 		      if (tab[look].gt_entry.gt_g_value >= val)
   12160  1.1  christos 			tab[look].gt_entry.gt_bytes += add;
   12161  1.1  christos 
   12162  1.1  christos 		      if (tab[look].gt_entry.gt_g_value == val)
   12163  1.1  christos 			exact = TRUE;
   12164  1.1  christos 		    }
   12165  1.1  christos 
   12166  1.1  christos 		  if (! exact)
   12167  1.1  christos 		    {
   12168  1.1  christos 		      Elf32_gptab *new_tab;
   12169  1.1  christos 		      unsigned int max;
   12170  1.1  christos 
   12171  1.1  christos 		      /* We need a new table entry.  */
   12172  1.1  christos 		      amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
   12173  1.1  christos 		      new_tab = bfd_realloc (tab, amt);
   12174  1.1  christos 		      if (new_tab == NULL)
   12175  1.1  christos 			{
   12176  1.1  christos 			  free (tab);
   12177  1.1  christos 			  return FALSE;
   12178  1.1  christos 			}
   12179  1.1  christos 		      tab = new_tab;
   12180  1.1  christos 		      tab[c].gt_entry.gt_g_value = val;
   12181  1.1  christos 		      tab[c].gt_entry.gt_bytes = add;
   12182  1.1  christos 
   12183  1.1  christos 		      /* Merge in the size for the next smallest -G
   12184  1.1  christos 			 value, since that will be implied by this new
   12185  1.1  christos 			 value.  */
   12186  1.1  christos 		      max = 0;
   12187  1.1  christos 		      for (look = 1; look < c; look++)
   12188  1.1  christos 			{
   12189  1.1  christos 			  if (tab[look].gt_entry.gt_g_value < val
   12190  1.1  christos 			      && (max == 0
   12191  1.1  christos 				  || (tab[look].gt_entry.gt_g_value
   12192  1.1  christos 				      > tab[max].gt_entry.gt_g_value)))
   12193  1.1  christos 			    max = look;
   12194  1.1  christos 			}
   12195  1.1  christos 		      if (max != 0)
   12196  1.1  christos 			tab[c].gt_entry.gt_bytes +=
   12197  1.1  christos 			  tab[max].gt_entry.gt_bytes;
   12198  1.1  christos 
   12199  1.1  christos 		      ++c;
   12200  1.1  christos 		    }
   12201  1.1  christos 
   12202  1.1  christos 		  last = int_gptab.gt_entry.gt_bytes;
   12203  1.1  christos 		}
   12204  1.1  christos 
   12205  1.1  christos 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
   12206  1.1  christos 		 elf_link_input_bfd ignores this section.  */
   12207  1.1  christos 	      input_section->flags &= ~SEC_HAS_CONTENTS;
   12208  1.1  christos 	    }
   12209  1.1  christos 
   12210  1.1  christos 	  /* The table must be sorted by -G value.  */
   12211  1.1  christos 	  if (c > 2)
   12212  1.1  christos 	    qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
   12213  1.1  christos 
   12214  1.1  christos 	  /* Swap out the table.  */
   12215  1.1  christos 	  amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
   12216  1.1  christos 	  ext_tab = bfd_alloc (abfd, amt);
   12217  1.1  christos 	  if (ext_tab == NULL)
   12218  1.1  christos 	    {
   12219  1.1  christos 	      free (tab);
   12220  1.1  christos 	      return FALSE;
   12221  1.1  christos 	    }
   12222  1.1  christos 
   12223  1.1  christos 	  for (j = 0; j < c; j++)
   12224  1.1  christos 	    bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
   12225  1.1  christos 	  free (tab);
   12226  1.1  christos 
   12227  1.1  christos 	  o->size = c * sizeof (Elf32_External_gptab);
   12228  1.1  christos 	  o->contents = (bfd_byte *) ext_tab;
   12229  1.1  christos 
   12230  1.1  christos 	  /* Skip this section later on (I don't think this currently
   12231  1.1  christos 	     matters, but someday it might).  */
   12232  1.1  christos 	  o->map_head.link_order = NULL;
   12233  1.1  christos 	}
   12234  1.1  christos     }
   12235  1.1  christos 
   12236  1.1  christos   /* Invoke the regular ELF backend linker to do all the work.  */
   12237  1.1  christos   if (!bfd_elf_final_link (abfd, info))
   12238  1.1  christos     return FALSE;
   12239  1.1  christos 
   12240  1.1  christos   /* Now write out the computed sections.  */
   12241  1.1  christos 
   12242  1.1  christos   if (reginfo_sec != NULL)
   12243  1.1  christos     {
   12244  1.1  christos       Elf32_External_RegInfo ext;
   12245  1.1  christos 
   12246  1.1  christos       bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
   12247  1.1  christos       if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
   12248  1.1  christos 	return FALSE;
   12249  1.1  christos     }
   12250  1.1  christos 
   12251  1.1  christos   if (mdebug_sec != NULL)
   12252  1.1  christos     {
   12253  1.1  christos       BFD_ASSERT (abfd->output_has_begun);
   12254  1.1  christos       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
   12255  1.1  christos 					       swap, info,
   12256  1.1  christos 					       mdebug_sec->filepos))
   12257  1.1  christos 	return FALSE;
   12258  1.1  christos 
   12259  1.1  christos       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
   12260  1.1  christos     }
   12261  1.1  christos 
   12262  1.1  christos   if (gptab_data_sec != NULL)
   12263  1.1  christos     {
   12264  1.1  christos       if (! bfd_set_section_contents (abfd, gptab_data_sec,
   12265  1.1  christos 				      gptab_data_sec->contents,
   12266  1.1  christos 				      0, gptab_data_sec->size))
   12267  1.1  christos 	return FALSE;
   12268  1.1  christos     }
   12269  1.1  christos 
   12270  1.1  christos   if (gptab_bss_sec != NULL)
   12271  1.1  christos     {
   12272  1.1  christos       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
   12273  1.1  christos 				      gptab_bss_sec->contents,
   12274  1.1  christos 				      0, gptab_bss_sec->size))
   12275  1.1  christos 	return FALSE;
   12276  1.1  christos     }
   12277  1.1  christos 
   12278  1.1  christos   if (SGI_COMPAT (abfd))
   12279  1.1  christos     {
   12280  1.1  christos       rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
   12281  1.1  christos       if (rtproc_sec != NULL)
   12282  1.1  christos 	{
   12283  1.1  christos 	  if (! bfd_set_section_contents (abfd, rtproc_sec,
   12284  1.1  christos 					  rtproc_sec->contents,
   12285  1.1  christos 					  0, rtproc_sec->size))
   12286  1.1  christos 	    return FALSE;
   12287  1.1  christos 	}
   12288  1.1  christos     }
   12289  1.1  christos 
   12290  1.1  christos   return TRUE;
   12291  1.1  christos }
   12292  1.1  christos 
   12293  1.1  christos /* Structure for saying that BFD machine EXTENSION extends BASE.  */
   12295  1.1  christos 
   12296  1.1  christos struct mips_mach_extension {
   12297  1.1  christos   unsigned long extension, base;
   12298  1.1  christos };
   12299  1.1  christos 
   12300  1.1  christos 
   12301  1.1  christos /* An array describing how BFD machines relate to one another.  The entries
   12302  1.1  christos    are ordered topologically with MIPS I extensions listed last.  */
   12303  1.1  christos 
   12304  1.1  christos static const struct mips_mach_extension mips_mach_extensions[] = {
   12305  1.1  christos   /* MIPS64r2 extensions.  */
   12306  1.1  christos   { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
   12307  1.1  christos 
   12308  1.1  christos   /* MIPS64 extensions.  */
   12309  1.1  christos   { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
   12310  1.1  christos   { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
   12311  1.1  christos   { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
   12312  1.1  christos   { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64 },
   12313  1.1  christos 
   12314  1.1  christos   /* MIPS V extensions.  */
   12315  1.1  christos   { bfd_mach_mipsisa64, bfd_mach_mips5 },
   12316  1.1  christos 
   12317  1.1  christos   /* R10000 extensions.  */
   12318  1.1  christos   { bfd_mach_mips12000, bfd_mach_mips10000 },
   12319  1.1  christos   { bfd_mach_mips14000, bfd_mach_mips10000 },
   12320  1.1  christos   { bfd_mach_mips16000, bfd_mach_mips10000 },
   12321  1.1  christos 
   12322  1.1  christos   /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
   12323  1.1  christos      vr5400 ISA, but doesn't include the multimedia stuff.  It seems
   12324  1.1  christos      better to allow vr5400 and vr5500 code to be merged anyway, since
   12325  1.1  christos      many libraries will just use the core ISA.  Perhaps we could add
   12326  1.1  christos      some sort of ASE flag if this ever proves a problem.  */
   12327  1.1  christos   { bfd_mach_mips5500, bfd_mach_mips5400 },
   12328  1.1  christos   { bfd_mach_mips5400, bfd_mach_mips5000 },
   12329  1.1  christos 
   12330  1.1  christos   /* MIPS IV extensions.  */
   12331  1.1  christos   { bfd_mach_mips5, bfd_mach_mips8000 },
   12332  1.1  christos   { bfd_mach_mips10000, bfd_mach_mips8000 },
   12333  1.1  christos   { bfd_mach_mips5000, bfd_mach_mips8000 },
   12334  1.1  christos   { bfd_mach_mips7000, bfd_mach_mips8000 },
   12335  1.1  christos   { bfd_mach_mips9000, bfd_mach_mips8000 },
   12336  1.1  christos 
   12337  1.1  christos   /* VR4100 extensions.  */
   12338  1.1  christos   { bfd_mach_mips4120, bfd_mach_mips4100 },
   12339  1.1  christos   { bfd_mach_mips4111, bfd_mach_mips4100 },
   12340  1.1  christos 
   12341  1.1  christos   /* MIPS III extensions.  */
   12342  1.1  christos   { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
   12343  1.1  christos   { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
   12344  1.1  christos   { bfd_mach_mips8000, bfd_mach_mips4000 },
   12345  1.1  christos   { bfd_mach_mips4650, bfd_mach_mips4000 },
   12346  1.1  christos   { bfd_mach_mips4600, bfd_mach_mips4000 },
   12347  1.1  christos   { bfd_mach_mips4400, bfd_mach_mips4000 },
   12348  1.1  christos   { bfd_mach_mips4300, bfd_mach_mips4000 },
   12349  1.1  christos   { bfd_mach_mips4100, bfd_mach_mips4000 },
   12350  1.1  christos   { bfd_mach_mips4010, bfd_mach_mips4000 },
   12351  1.1  christos 
   12352  1.1  christos   /* MIPS32 extensions.  */
   12353  1.1  christos   { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
   12354  1.1  christos 
   12355  1.1  christos   /* MIPS II extensions.  */
   12356  1.1  christos   { bfd_mach_mips4000, bfd_mach_mips6000 },
   12357  1.1  christos   { bfd_mach_mipsisa32, bfd_mach_mips6000 },
   12358  1.1  christos 
   12359  1.1  christos   /* MIPS I extensions.  */
   12360  1.1  christos   { bfd_mach_mips6000, bfd_mach_mips3000 },
   12361  1.1  christos   { bfd_mach_mips3900, bfd_mach_mips3000 }
   12362  1.1  christos };
   12363  1.1  christos 
   12364  1.1  christos 
   12365  1.1  christos /* Return true if bfd machine EXTENSION is an extension of machine BASE.  */
   12366  1.1  christos 
   12367  1.1  christos static bfd_boolean
   12368  1.1  christos mips_mach_extends_p (unsigned long base, unsigned long extension)
   12369  1.1  christos {
   12370  1.1  christos   size_t i;
   12371  1.1  christos 
   12372  1.1  christos   if (extension == base)
   12373  1.1  christos     return TRUE;
   12374  1.1  christos 
   12375  1.1  christos   if (base == bfd_mach_mipsisa32
   12376  1.1  christos       && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
   12377  1.1  christos     return TRUE;
   12378  1.1  christos 
   12379  1.1  christos   if (base == bfd_mach_mipsisa32r2
   12380  1.1  christos       && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
   12381  1.1  christos     return TRUE;
   12382  1.1  christos 
   12383  1.1  christos   for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
   12384  1.1  christos     if (extension == mips_mach_extensions[i].extension)
   12385  1.1  christos       {
   12386  1.1  christos 	extension = mips_mach_extensions[i].base;
   12387  1.1  christos 	if (extension == base)
   12388  1.1  christos 	  return TRUE;
   12389  1.1  christos       }
   12390  1.1  christos 
   12391  1.1  christos   return FALSE;
   12392  1.1  christos }
   12393  1.1  christos 
   12394  1.1  christos 
   12395  1.1  christos /* Return true if the given ELF header flags describe a 32-bit binary.  */
   12396  1.1  christos 
   12397  1.1  christos static bfd_boolean
   12398  1.1  christos mips_32bit_flags_p (flagword flags)
   12399  1.1  christos {
   12400  1.1  christos   return ((flags & EF_MIPS_32BITMODE) != 0
   12401  1.1  christos 	  || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
   12402  1.1  christos 	  || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
   12403  1.1  christos 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
   12404  1.1  christos 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
   12405  1.1  christos 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
   12406  1.1  christos 	  || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
   12407  1.1  christos }
   12408  1.1  christos 
   12409  1.1  christos 
   12410  1.1  christos /* Merge object attributes from IBFD into OBFD.  Raise an error if
   12411  1.1  christos    there are conflicting attributes.  */
   12412  1.1  christos static bfd_boolean
   12413  1.1  christos mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
   12414  1.1  christos {
   12415  1.1  christos   obj_attribute *in_attr;
   12416  1.1  christos   obj_attribute *out_attr;
   12417  1.1  christos 
   12418  1.1  christos   if (!elf_known_obj_attributes_proc (obfd)[0].i)
   12419  1.1  christos     {
   12420  1.1  christos       /* This is the first object.  Copy the attributes.  */
   12421  1.1  christos       _bfd_elf_copy_obj_attributes (ibfd, obfd);
   12422  1.1  christos 
   12423  1.1  christos       /* Use the Tag_null value to indicate the attributes have been
   12424  1.1  christos 	 initialized.  */
   12425  1.1  christos       elf_known_obj_attributes_proc (obfd)[0].i = 1;
   12426  1.1  christos 
   12427  1.1  christos       return TRUE;
   12428  1.1  christos     }
   12429  1.1  christos 
   12430  1.1  christos   /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
   12431  1.1  christos      non-conflicting ones.  */
   12432  1.1  christos   in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   12433  1.1  christos   out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   12434  1.1  christos   if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
   12435  1.1  christos     {
   12436  1.1  christos       out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
   12437  1.1  christos       if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
   12438  1.1  christos 	out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
   12439  1.1  christos       else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
   12440  1.1  christos 	;
   12441  1.1  christos       else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
   12442  1.1  christos 	_bfd_error_handler
   12443  1.1  christos 	  (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
   12444  1.1  christos 	   in_attr[Tag_GNU_MIPS_ABI_FP].i);
   12445  1.1  christos       else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
   12446  1.1  christos 	_bfd_error_handler
   12447  1.1  christos 	  (_("Warning: %B uses unknown floating point ABI %d"), obfd,
   12448  1.1  christos 	   out_attr[Tag_GNU_MIPS_ABI_FP].i);
   12449  1.1  christos       else
   12450  1.1  christos 	switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
   12451  1.1  christos 	  {
   12452  1.1  christos 	  case 1:
   12453  1.1  christos 	    switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
   12454  1.1  christos 	      {
   12455  1.1  christos 	      case 2:
   12456  1.1  christos 		_bfd_error_handler
   12457  1.1  christos 		  (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
   12458  1.1  christos 		   obfd, ibfd);
   12459  1.1  christos 		break;
   12460  1.1  christos 
   12461  1.1  christos 	      case 3:
   12462  1.1  christos 		_bfd_error_handler
   12463  1.1  christos 		  (_("Warning: %B uses hard float, %B uses soft float"),
   12464  1.1  christos 		   obfd, ibfd);
   12465  1.1  christos 		break;
   12466  1.1  christos 
   12467  1.1  christos 	      case 4:
   12468  1.1  christos 		_bfd_error_handler
   12469  1.1  christos 		  (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
   12470  1.1  christos 		   obfd, ibfd);
   12471  1.1  christos 		break;
   12472  1.1  christos 
   12473  1.1  christos 	      default:
   12474  1.1  christos 		abort ();
   12475  1.1  christos 	      }
   12476  1.1  christos 	    break;
   12477  1.1  christos 
   12478  1.1  christos 	  case 2:
   12479  1.1  christos 	    switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
   12480  1.1  christos 	      {
   12481  1.1  christos 	      case 1:
   12482  1.1  christos 		_bfd_error_handler
   12483  1.1  christos 		  (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
   12484  1.1  christos 		   ibfd, obfd);
   12485  1.1  christos 		break;
   12486  1.1  christos 
   12487  1.1  christos 	      case 3:
   12488  1.1  christos 		_bfd_error_handler
   12489  1.1  christos 		  (_("Warning: %B uses hard float, %B uses soft float"),
   12490  1.1  christos 		   obfd, ibfd);
   12491  1.1  christos 		break;
   12492  1.1  christos 
   12493  1.1  christos 	      case 4:
   12494  1.1  christos 		_bfd_error_handler
   12495  1.1  christos 		  (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
   12496  1.1  christos 		   obfd, ibfd);
   12497  1.1  christos 		break;
   12498  1.1  christos 
   12499  1.1  christos 	      default:
   12500  1.1  christos 		abort ();
   12501  1.1  christos 	      }
   12502  1.1  christos 	    break;
   12503  1.1  christos 
   12504  1.1  christos 	  case 3:
   12505  1.1  christos 	    switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
   12506  1.1  christos 	      {
   12507  1.1  christos 	      case 1:
   12508  1.1  christos 	      case 2:
   12509  1.1  christos 	      case 4:
   12510  1.1  christos 		_bfd_error_handler
   12511  1.1  christos 		  (_("Warning: %B uses hard float, %B uses soft float"),
   12512  1.1  christos 		   ibfd, obfd);
   12513  1.1  christos 		break;
   12514  1.1  christos 
   12515  1.1  christos 	      default:
   12516  1.1  christos 		abort ();
   12517  1.1  christos 	      }
   12518  1.1  christos 	    break;
   12519  1.1  christos 
   12520  1.1  christos 	  case 4:
   12521  1.1  christos 	    switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
   12522  1.1  christos 	      {
   12523  1.1  christos 	      case 1:
   12524  1.1  christos 		_bfd_error_handler
   12525  1.1  christos 		  (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
   12526  1.1  christos 		   ibfd, obfd);
   12527  1.1  christos 		break;
   12528  1.1  christos 
   12529  1.1  christos 	      case 2:
   12530  1.1  christos 		_bfd_error_handler
   12531  1.1  christos 		  (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
   12532  1.1  christos 		   ibfd, obfd);
   12533  1.1  christos 		break;
   12534  1.1  christos 
   12535  1.1  christos 	      case 3:
   12536  1.1  christos 		_bfd_error_handler
   12537  1.1  christos 		  (_("Warning: %B uses hard float, %B uses soft float"),
   12538  1.1  christos 		   obfd, ibfd);
   12539  1.1  christos 		break;
   12540  1.1  christos 
   12541  1.1  christos 	      default:
   12542  1.1  christos 		abort ();
   12543  1.1  christos 	      }
   12544  1.1  christos 	    break;
   12545  1.1  christos 
   12546  1.1  christos 	  default:
   12547  1.1  christos 	    abort ();
   12548  1.1  christos 	  }
   12549  1.1  christos     }
   12550  1.1  christos 
   12551  1.1  christos   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   12552  1.1  christos   _bfd_elf_merge_object_attributes (ibfd, obfd);
   12553  1.1  christos 
   12554  1.1  christos   return TRUE;
   12555  1.1  christos }
   12556  1.1  christos 
   12557  1.1  christos /* Merge backend specific data from an object file to the output
   12558  1.1  christos    object file when linking.  */
   12559  1.1  christos 
   12560  1.1  christos bfd_boolean
   12561  1.1  christos _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   12562  1.1  christos {
   12563  1.1  christos   flagword old_flags;
   12564  1.1  christos   flagword new_flags;
   12565  1.1  christos   bfd_boolean ok;
   12566  1.1  christos   bfd_boolean null_input_bfd = TRUE;
   12567  1.1  christos   asection *sec;
   12568  1.1  christos 
   12569  1.1  christos   /* Check if we have the same endianess */
   12570  1.1  christos   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
   12571  1.1  christos     {
   12572  1.1  christos       (*_bfd_error_handler)
   12573  1.1  christos 	(_("%B: endianness incompatible with that of the selected emulation"),
   12574  1.1  christos 	 ibfd);
   12575  1.1  christos       return FALSE;
   12576  1.1  christos     }
   12577  1.1  christos 
   12578  1.1  christos   if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
   12579  1.1  christos     return TRUE;
   12580  1.1  christos 
   12581  1.1  christos   if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
   12582  1.1  christos     {
   12583  1.1  christos       (*_bfd_error_handler)
   12584  1.1  christos 	(_("%B: ABI is incompatible with that of the selected emulation"),
   12585  1.1  christos 	 ibfd);
   12586  1.1  christos       return FALSE;
   12587  1.1  christos     }
   12588  1.1  christos 
   12589  1.1  christos   if (!mips_elf_merge_obj_attributes (ibfd, obfd))
   12590  1.1  christos     return FALSE;
   12591  1.1  christos 
   12592  1.1  christos   new_flags = elf_elfheader (ibfd)->e_flags;
   12593  1.1  christos   elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
   12594  1.1  christos   old_flags = elf_elfheader (obfd)->e_flags;
   12595  1.1  christos 
   12596  1.1  christos   if (! elf_flags_init (obfd))
   12597  1.1  christos     {
   12598  1.1  christos       elf_flags_init (obfd) = TRUE;
   12599  1.1  christos       elf_elfheader (obfd)->e_flags = new_flags;
   12600  1.1  christos       elf_elfheader (obfd)->e_ident[EI_CLASS]
   12601  1.1  christos 	= elf_elfheader (ibfd)->e_ident[EI_CLASS];
   12602  1.1  christos 
   12603  1.1  christos       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   12604  1.1  christos 	  && (bfd_get_arch_info (obfd)->the_default
   12605  1.1  christos 	      || mips_mach_extends_p (bfd_get_mach (obfd),
   12606  1.1  christos 				      bfd_get_mach (ibfd))))
   12607  1.1  christos 	{
   12608  1.1  christos 	  if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
   12609  1.1  christos 				   bfd_get_mach (ibfd)))
   12610  1.1  christos 	    return FALSE;
   12611  1.1  christos 	}
   12612  1.1  christos 
   12613  1.1  christos       return TRUE;
   12614  1.1  christos     }
   12615  1.1  christos 
   12616  1.1  christos   /* Check flag compatibility.  */
   12617  1.1  christos 
   12618  1.1  christos   new_flags &= ~EF_MIPS_NOREORDER;
   12619  1.1  christos   old_flags &= ~EF_MIPS_NOREORDER;
   12620  1.1  christos 
   12621  1.1  christos   /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
   12622  1.1  christos      doesn't seem to matter.  */
   12623  1.1  christos   new_flags &= ~EF_MIPS_XGOT;
   12624  1.1  christos   old_flags &= ~EF_MIPS_XGOT;
   12625  1.1  christos 
   12626  1.1  christos   /* MIPSpro generates ucode info in n64 objects.  Again, we should
   12627  1.1  christos      just be able to ignore this.  */
   12628  1.1  christos   new_flags &= ~EF_MIPS_UCODE;
   12629  1.1  christos   old_flags &= ~EF_MIPS_UCODE;
   12630  1.1  christos 
   12631  1.1  christos   /* DSOs should only be linked with CPIC code.  */
   12632  1.1  christos   if ((ibfd->flags & DYNAMIC) != 0)
   12633  1.1  christos     new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
   12634  1.1  christos 
   12635  1.1  christos   if (new_flags == old_flags)
   12636  1.1  christos     return TRUE;
   12637  1.1  christos 
   12638  1.1  christos   /* Check to see if the input BFD actually contains any sections.
   12639  1.1  christos      If not, its flags may not have been initialised either, but it cannot
   12640  1.1  christos      actually cause any incompatibility.  */
   12641  1.1  christos   for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   12642  1.1  christos     {
   12643  1.1  christos       /* Ignore synthetic sections and empty .text, .data and .bss sections
   12644  1.1  christos 	 which are automatically generated by gas.  Also ignore fake
   12645  1.1  christos 	 (s)common sections, since merely defining a common symbol does
   12646  1.1  christos 	 not affect compatibility.  */
   12647  1.1  christos       if ((sec->flags & SEC_IS_COMMON) == 0
   12648  1.1  christos 	  && strcmp (sec->name, ".reginfo")
   12649  1.1  christos 	  && strcmp (sec->name, ".mdebug")
   12650  1.1  christos 	  && (sec->size != 0
   12651  1.1  christos 	      || (strcmp (sec->name, ".text")
   12652  1.1  christos 		  && strcmp (sec->name, ".data")
   12653  1.1  christos 		  && strcmp (sec->name, ".bss"))))
   12654  1.1  christos 	{
   12655  1.1  christos 	  null_input_bfd = FALSE;
   12656  1.1  christos 	  break;
   12657  1.1  christos 	}
   12658  1.1  christos     }
   12659  1.1  christos   if (null_input_bfd)
   12660  1.1  christos     return TRUE;
   12661  1.1  christos 
   12662  1.1  christos   ok = TRUE;
   12663  1.1  christos 
   12664  1.1  christos   if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
   12665  1.1  christos       != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
   12666  1.1  christos     {
   12667  1.1  christos       (*_bfd_error_handler)
   12668  1.1  christos 	(_("%B: warning: linking abicalls files with non-abicalls files"),
   12669  1.1  christos 	 ibfd);
   12670  1.1  christos       ok = TRUE;
   12671  1.1  christos     }
   12672  1.1  christos 
   12673  1.1  christos   if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
   12674  1.1  christos     elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
   12675  1.1  christos   if (! (new_flags & EF_MIPS_PIC))
   12676  1.1  christos     elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
   12677  1.1  christos 
   12678  1.1  christos   new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
   12679  1.1  christos   old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
   12680  1.1  christos 
   12681  1.1  christos   /* Compare the ISAs.  */
   12682  1.1  christos   if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
   12683  1.1  christos     {
   12684  1.1  christos       (*_bfd_error_handler)
   12685  1.1  christos 	(_("%B: linking 32-bit code with 64-bit code"),
   12686  1.1  christos 	 ibfd);
   12687  1.1  christos       ok = FALSE;
   12688  1.1  christos     }
   12689  1.1  christos   else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
   12690  1.1  christos     {
   12691  1.1  christos       /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
   12692  1.1  christos       if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
   12693  1.1  christos 	{
   12694  1.1  christos 	  /* Copy the architecture info from IBFD to OBFD.  Also copy
   12695  1.1  christos 	     the 32-bit flag (if set) so that we continue to recognise
   12696  1.1  christos 	     OBFD as a 32-bit binary.  */
   12697  1.1  christos 	  bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
   12698  1.1  christos 	  elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
   12699  1.1  christos 	  elf_elfheader (obfd)->e_flags
   12700  1.1  christos 	    |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   12701  1.1  christos 
   12702  1.1  christos 	  /* Copy across the ABI flags if OBFD doesn't use them
   12703  1.1  christos 	     and if that was what caused us to treat IBFD as 32-bit.  */
   12704  1.1  christos 	  if ((old_flags & EF_MIPS_ABI) == 0
   12705  1.1  christos 	      && mips_32bit_flags_p (new_flags)
   12706  1.1  christos 	      && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
   12707  1.1  christos 	    elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
   12708  1.1  christos 	}
   12709  1.1  christos       else
   12710  1.1  christos 	{
   12711  1.1  christos 	  /* The ISAs aren't compatible.  */
   12712  1.1  christos 	  (*_bfd_error_handler)
   12713  1.1  christos 	    (_("%B: linking %s module with previous %s modules"),
   12714  1.1  christos 	     ibfd,
   12715  1.1  christos 	     bfd_printable_name (ibfd),
   12716  1.1  christos 	     bfd_printable_name (obfd));
   12717  1.1  christos 	  ok = FALSE;
   12718  1.1  christos 	}
   12719  1.1  christos     }
   12720  1.1  christos 
   12721  1.1  christos   new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   12722  1.1  christos   old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
   12723  1.1  christos 
   12724  1.1  christos   /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
   12725  1.1  christos      does set EI_CLASS differently from any 32-bit ABI.  */
   12726  1.1  christos   if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
   12727  1.1  christos       || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   12728  1.1  christos 	  != elf_elfheader (obfd)->e_ident[EI_CLASS]))
   12729  1.1  christos     {
   12730  1.1  christos       /* Only error if both are set (to different values).  */
   12731  1.1  christos       if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
   12732  1.1  christos 	  || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
   12733  1.1  christos 	      != elf_elfheader (obfd)->e_ident[EI_CLASS]))
   12734  1.1  christos 	{
   12735  1.1  christos 	  (*_bfd_error_handler)
   12736  1.1  christos 	    (_("%B: ABI mismatch: linking %s module with previous %s modules"),
   12737  1.1  christos 	     ibfd,
   12738  1.1  christos 	     elf_mips_abi_name (ibfd),
   12739  1.1  christos 	     elf_mips_abi_name (obfd));
   12740  1.1  christos 	  ok = FALSE;
   12741  1.1  christos 	}
   12742  1.1  christos       new_flags &= ~EF_MIPS_ABI;
   12743  1.1  christos       old_flags &= ~EF_MIPS_ABI;
   12744  1.1  christos     }
   12745  1.1  christos 
   12746  1.1  christos   /* For now, allow arbitrary mixing of ASEs (retain the union).  */
   12747  1.1  christos   if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
   12748  1.1  christos     {
   12749  1.1  christos       elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
   12750  1.1  christos 
   12751  1.1  christos       new_flags &= ~ EF_MIPS_ARCH_ASE;
   12752  1.1  christos       old_flags &= ~ EF_MIPS_ARCH_ASE;
   12753  1.1  christos     }
   12754  1.1  christos 
   12755  1.1  christos   /* Warn about any other mismatches */
   12756  1.1  christos   if (new_flags != old_flags)
   12757  1.1  christos     {
   12758  1.1  christos       (*_bfd_error_handler)
   12759  1.1  christos 	(_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
   12760  1.1  christos 	 ibfd, (unsigned long) new_flags,
   12761  1.1  christos 	 (unsigned long) old_flags);
   12762  1.1  christos       ok = FALSE;
   12763  1.1  christos     }
   12764  1.1  christos 
   12765  1.1  christos   if (! ok)
   12766  1.1  christos     {
   12767  1.1  christos       bfd_set_error (bfd_error_bad_value);
   12768  1.1  christos       return FALSE;
   12769  1.1  christos     }
   12770  1.1  christos 
   12771  1.1  christos   return TRUE;
   12772  1.1  christos }
   12773  1.1  christos 
   12774  1.1  christos /* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
   12775  1.1  christos 
   12776  1.1  christos bfd_boolean
   12777  1.1  christos _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
   12778  1.1  christos {
   12779  1.1  christos   BFD_ASSERT (!elf_flags_init (abfd)
   12780  1.1  christos 	      || elf_elfheader (abfd)->e_flags == flags);
   12781  1.1  christos 
   12782  1.1  christos   elf_elfheader (abfd)->e_flags = flags;
   12783  1.1  christos   elf_flags_init (abfd) = TRUE;
   12784  1.1  christos   return TRUE;
   12785  1.1  christos }
   12786  1.1  christos 
   12787  1.1  christos char *
   12788  1.1  christos _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
   12789  1.1  christos {
   12790  1.1  christos   switch (dtag)
   12791  1.1  christos     {
   12792  1.1  christos     default: return "";
   12793  1.1  christos     case DT_MIPS_RLD_VERSION:
   12794  1.1  christos       return "MIPS_RLD_VERSION";
   12795  1.1  christos     case DT_MIPS_TIME_STAMP:
   12796  1.1  christos       return "MIPS_TIME_STAMP";
   12797  1.1  christos     case DT_MIPS_ICHECKSUM:
   12798  1.1  christos       return "MIPS_ICHECKSUM";
   12799  1.1  christos     case DT_MIPS_IVERSION:
   12800  1.1  christos       return "MIPS_IVERSION";
   12801  1.1  christos     case DT_MIPS_FLAGS:
   12802  1.1  christos       return "MIPS_FLAGS";
   12803  1.1  christos     case DT_MIPS_BASE_ADDRESS:
   12804  1.1  christos       return "MIPS_BASE_ADDRESS";
   12805  1.1  christos     case DT_MIPS_MSYM:
   12806  1.1  christos       return "MIPS_MSYM";
   12807  1.1  christos     case DT_MIPS_CONFLICT:
   12808  1.1  christos       return "MIPS_CONFLICT";
   12809  1.1  christos     case DT_MIPS_LIBLIST:
   12810  1.1  christos       return "MIPS_LIBLIST";
   12811  1.1  christos     case DT_MIPS_LOCAL_GOTNO:
   12812  1.1  christos       return "MIPS_LOCAL_GOTNO";
   12813  1.1  christos     case DT_MIPS_CONFLICTNO:
   12814  1.1  christos       return "MIPS_CONFLICTNO";
   12815  1.1  christos     case DT_MIPS_LIBLISTNO:
   12816  1.1  christos       return "MIPS_LIBLISTNO";
   12817  1.1  christos     case DT_MIPS_SYMTABNO:
   12818  1.1  christos       return "MIPS_SYMTABNO";
   12819  1.1  christos     case DT_MIPS_UNREFEXTNO:
   12820  1.1  christos       return "MIPS_UNREFEXTNO";
   12821  1.1  christos     case DT_MIPS_GOTSYM:
   12822  1.1  christos       return "MIPS_GOTSYM";
   12823  1.1  christos     case DT_MIPS_HIPAGENO:
   12824  1.1  christos       return "MIPS_HIPAGENO";
   12825  1.1  christos     case DT_MIPS_RLD_MAP:
   12826  1.1  christos       return "MIPS_RLD_MAP";
   12827  1.1  christos     case DT_MIPS_DELTA_CLASS:
   12828  1.1  christos       return "MIPS_DELTA_CLASS";
   12829  1.1  christos     case DT_MIPS_DELTA_CLASS_NO:
   12830  1.1  christos       return "MIPS_DELTA_CLASS_NO";
   12831  1.1  christos     case DT_MIPS_DELTA_INSTANCE:
   12832  1.1  christos       return "MIPS_DELTA_INSTANCE";
   12833  1.1  christos     case DT_MIPS_DELTA_INSTANCE_NO:
   12834  1.1  christos       return "MIPS_DELTA_INSTANCE_NO";
   12835  1.1  christos     case DT_MIPS_DELTA_RELOC:
   12836  1.1  christos       return "MIPS_DELTA_RELOC";
   12837  1.1  christos     case DT_MIPS_DELTA_RELOC_NO:
   12838  1.1  christos       return "MIPS_DELTA_RELOC_NO";
   12839  1.1  christos     case DT_MIPS_DELTA_SYM:
   12840  1.1  christos       return "MIPS_DELTA_SYM";
   12841  1.1  christos     case DT_MIPS_DELTA_SYM_NO:
   12842  1.1  christos       return "MIPS_DELTA_SYM_NO";
   12843  1.1  christos     case DT_MIPS_DELTA_CLASSSYM:
   12844  1.1  christos       return "MIPS_DELTA_CLASSSYM";
   12845  1.1  christos     case DT_MIPS_DELTA_CLASSSYM_NO:
   12846  1.1  christos       return "MIPS_DELTA_CLASSSYM_NO";
   12847  1.1  christos     case DT_MIPS_CXX_FLAGS:
   12848  1.1  christos       return "MIPS_CXX_FLAGS";
   12849  1.1  christos     case DT_MIPS_PIXIE_INIT:
   12850  1.1  christos       return "MIPS_PIXIE_INIT";
   12851  1.1  christos     case DT_MIPS_SYMBOL_LIB:
   12852  1.1  christos       return "MIPS_SYMBOL_LIB";
   12853  1.1  christos     case DT_MIPS_LOCALPAGE_GOTIDX:
   12854  1.1  christos       return "MIPS_LOCALPAGE_GOTIDX";
   12855  1.1  christos     case DT_MIPS_LOCAL_GOTIDX:
   12856  1.1  christos       return "MIPS_LOCAL_GOTIDX";
   12857  1.1  christos     case DT_MIPS_HIDDEN_GOTIDX:
   12858  1.1  christos       return "MIPS_HIDDEN_GOTIDX";
   12859  1.1  christos     case DT_MIPS_PROTECTED_GOTIDX:
   12860  1.1  christos       return "MIPS_PROTECTED_GOT_IDX";
   12861  1.1  christos     case DT_MIPS_OPTIONS:
   12862  1.1  christos       return "MIPS_OPTIONS";
   12863  1.1  christos     case DT_MIPS_INTERFACE:
   12864  1.1  christos       return "MIPS_INTERFACE";
   12865  1.1  christos     case DT_MIPS_DYNSTR_ALIGN:
   12866  1.1  christos       return "DT_MIPS_DYNSTR_ALIGN";
   12867  1.1  christos     case DT_MIPS_INTERFACE_SIZE:
   12868  1.1  christos       return "DT_MIPS_INTERFACE_SIZE";
   12869  1.1  christos     case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
   12870  1.1  christos       return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
   12871  1.1  christos     case DT_MIPS_PERF_SUFFIX:
   12872  1.1  christos       return "DT_MIPS_PERF_SUFFIX";
   12873  1.1  christos     case DT_MIPS_COMPACT_SIZE:
   12874  1.1  christos       return "DT_MIPS_COMPACT_SIZE";
   12875  1.1  christos     case DT_MIPS_GP_VALUE:
   12876  1.1  christos       return "DT_MIPS_GP_VALUE";
   12877  1.1  christos     case DT_MIPS_AUX_DYNAMIC:
   12878  1.1  christos       return "DT_MIPS_AUX_DYNAMIC";
   12879  1.1  christos     case DT_MIPS_PLTGOT:
   12880  1.1  christos       return "DT_MIPS_PLTGOT";
   12881  1.1  christos     case DT_MIPS_RWPLT:
   12882  1.1  christos       return "DT_MIPS_RWPLT";
   12883  1.1  christos     }
   12884  1.1  christos }
   12885  1.1  christos 
   12886  1.1  christos bfd_boolean
   12887  1.1  christos _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
   12888  1.1  christos {
   12889  1.1  christos   FILE *file = ptr;
   12890  1.1  christos 
   12891  1.1  christos   BFD_ASSERT (abfd != NULL && ptr != NULL);
   12892  1.1  christos 
   12893  1.1  christos   /* Print normal ELF private data.  */
   12894  1.1  christos   _bfd_elf_print_private_bfd_data (abfd, ptr);
   12895  1.1  christos 
   12896  1.1  christos   /* xgettext:c-format */
   12897  1.1  christos   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
   12898  1.1  christos 
   12899  1.1  christos   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
   12900  1.1  christos     fprintf (file, _(" [abi=O32]"));
   12901  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
   12902  1.1  christos     fprintf (file, _(" [abi=O64]"));
   12903  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
   12904  1.1  christos     fprintf (file, _(" [abi=EABI32]"));
   12905  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
   12906  1.1  christos     fprintf (file, _(" [abi=EABI64]"));
   12907  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
   12908  1.1  christos     fprintf (file, _(" [abi unknown]"));
   12909  1.1  christos   else if (ABI_N32_P (abfd))
   12910  1.1  christos     fprintf (file, _(" [abi=N32]"));
   12911  1.1  christos   else if (ABI_64_P (abfd))
   12912  1.1  christos     fprintf (file, _(" [abi=64]"));
   12913  1.1  christos   else
   12914  1.1  christos     fprintf (file, _(" [no abi set]"));
   12915  1.1  christos 
   12916  1.1  christos   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
   12917  1.1  christos     fprintf (file, " [mips1]");
   12918  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
   12919  1.1  christos     fprintf (file, " [mips2]");
   12920  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
   12921  1.1  christos     fprintf (file, " [mips3]");
   12922  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
   12923  1.1  christos     fprintf (file, " [mips4]");
   12924  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
   12925  1.1  christos     fprintf (file, " [mips5]");
   12926  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
   12927  1.1  christos     fprintf (file, " [mips32]");
   12928  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
   12929  1.1  christos     fprintf (file, " [mips64]");
   12930  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
   12931  1.1  christos     fprintf (file, " [mips32r2]");
   12932  1.1  christos   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
   12933  1.1  christos     fprintf (file, " [mips64r2]");
   12934  1.1  christos   else
   12935  1.1  christos     fprintf (file, _(" [unknown ISA]"));
   12936  1.1  christos 
   12937  1.1  christos   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
   12938  1.1  christos     fprintf (file, " [mdmx]");
   12939  1.1  christos 
   12940  1.1  christos   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
   12941  1.1  christos     fprintf (file, " [mips16]");
   12942  1.1  christos 
   12943  1.1  christos   if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
   12944  1.1  christos     fprintf (file, " [32bitmode]");
   12945  1.1  christos   else
   12946  1.1  christos     fprintf (file, _(" [not 32bitmode]"));
   12947  1.1  christos 
   12948  1.1  christos   if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
   12949  1.1  christos     fprintf (file, " [noreorder]");
   12950  1.1  christos 
   12951  1.1  christos   if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
   12952  1.1  christos     fprintf (file, " [PIC]");
   12953  1.1  christos 
   12954  1.1  christos   if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
   12955  1.1  christos     fprintf (file, " [CPIC]");
   12956  1.1  christos 
   12957  1.1  christos   if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
   12958  1.1  christos     fprintf (file, " [XGOT]");
   12959  1.1  christos 
   12960  1.1  christos   if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
   12961  1.1  christos     fprintf (file, " [UCODE]");
   12962  1.1  christos 
   12963  1.1  christos   fputc ('\n', file);
   12964  1.1  christos 
   12965  1.1  christos   return TRUE;
   12966  1.1  christos }
   12967  1.1  christos 
   12968  1.1  christos const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
   12969  1.1  christos {
   12970  1.1  christos   { STRING_COMMA_LEN (".lit4"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   12971  1.1  christos   { STRING_COMMA_LEN (".lit8"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   12972  1.1  christos   { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
   12973  1.1  christos   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   12974  1.1  christos   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
   12975  1.1  christos   { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
   12976  1.1  christos   { NULL,                     0,  0, 0,              0 }
   12977  1.1  christos };
   12978  1.1  christos 
   12979  1.1  christos /* Merge non visibility st_other attributes.  Ensure that the
   12980  1.1  christos    STO_OPTIONAL flag is copied into h->other, even if this is not a
   12981  1.1  christos    definiton of the symbol.  */
   12982  1.1  christos void
   12983  1.1  christos _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
   12984  1.1  christos 				      const Elf_Internal_Sym *isym,
   12985  1.1  christos 				      bfd_boolean definition,
   12986  1.1  christos 				      bfd_boolean dynamic ATTRIBUTE_UNUSED)
   12987  1.1  christos {
   12988  1.1  christos   if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
   12989  1.1  christos     {
   12990  1.1  christos       unsigned char other;
   12991  1.1  christos 
   12992  1.1  christos       other = (definition ? isym->st_other : h->other);
   12993  1.1  christos       other &= ~ELF_ST_VISIBILITY (-1);
   12994  1.1  christos       h->other = other | ELF_ST_VISIBILITY (h->other);
   12995  1.1  christos     }
   12996  1.1  christos 
   12997  1.1  christos   if (!definition
   12998  1.1  christos       && ELF_MIPS_IS_OPTIONAL (isym->st_other))
   12999  1.1  christos     h->other |= STO_OPTIONAL;
   13000  1.1  christos }
   13001  1.1  christos 
   13002                /* Decide whether an undefined symbol is special and can be ignored.
   13003                   This is the case for OPTIONAL symbols on IRIX.  */
   13004                bfd_boolean
   13005                _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
   13006                {
   13007                  return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
   13008                }
   13009                
   13010                bfd_boolean
   13011                _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
   13012                {
   13013                  return (sym->st_shndx == SHN_COMMON
   13014                	  || sym->st_shndx == SHN_MIPS_ACOMMON
   13015                	  || sym->st_shndx == SHN_MIPS_SCOMMON);
   13016                }
   13017                
   13018                /* Return address for Ith PLT stub in section PLT, for relocation REL
   13019                   or (bfd_vma) -1 if it should not be included.  */
   13020                
   13021                bfd_vma
   13022                _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
   13023                			   const arelent *rel ATTRIBUTE_UNUSED)
   13024                {
   13025                  return (plt->vma
   13026                	  + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
   13027                	  + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
   13028                }
   13029                
   13030                void
   13031                _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
   13032                {
   13033                  struct mips_elf_link_hash_table *htab;
   13034                  Elf_Internal_Ehdr *i_ehdrp;
   13035                
   13036                  i_ehdrp = elf_elfheader (abfd);
   13037                  if (link_info)
   13038                    {
   13039                      htab = mips_elf_hash_table (link_info);
   13040                      BFD_ASSERT (htab != NULL);
   13041                
   13042                      if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
   13043                	i_ehdrp->e_ident[EI_ABIVERSION] = 1;
   13044                    }
   13045                }
   13046