1/* MIPS-specific support for ELF
2   Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3   2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5   Most of the information added by Ian Lance Taylor, Cygnus Support,
6   <ian@cygnus.com>.
7   N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8   <mark@codesourcery.com>
9   Traditional MIPS targets support added by Koundinya.K, Dansk Data
10   Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11
12   This file is part of BFD, the Binary File Descriptor library.
13
14   This program is free software; you can redistribute it and/or modify
15   it under the terms of the GNU General Public License as published by
16   the Free Software Foundation; either version 2 of the License, or
17   (at your option) any later version.
18
19   This program is distributed in the hope that it will be useful,
20   but WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   GNU General Public License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with this program; if not, write to the Free Software
26   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
27
28/* This file handles functionality common to the different MIPS ABI's.  */
29
30#include "sysdep.h"
31#include "bfd.h"
32#include "libbfd.h"
33#include "libiberty.h"
34#include "elf-bfd.h"
35#include "elfxx-mips.h"
36#include "elf/mips.h"
37#include "elf-vxworks.h"
38
39/* Get the ECOFF swapping routines.  */
40#include "coff/sym.h"
41#include "coff/symconst.h"
42#include "coff/ecoff.h"
43#include "coff/mips.h"
44
45#include "hashtab.h"
46
47/* This structure is used to hold information about one GOT entry.
48   There are three types of entry:
49
50      (1) absolute addresses
51	    (abfd == NULL)
52      (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
53	    (abfd != NULL, symndx >= 0)
54      (3) global and forced-local symbols
55	    (abfd != NULL, symndx == -1)
56
57   Type (3) entries are treated differently for different types of GOT.
58   In the "master" GOT -- i.e.  the one that describes every GOT
59   reference needed in the link -- the mips_got_entry is keyed on both
60   the symbol and the input bfd that references it.  If it turns out
61   that we need multiple GOTs, we can then use this information to
62   create separate GOTs for each input bfd.
63
64   However, we want each of these separate GOTs to have at most one
65   entry for a given symbol, so their type (3) entries are keyed only
66   on the symbol.  The input bfd given by the "abfd" field is somewhat
67   arbitrary in this case.
68
69   This means that when there are multiple GOTs, each GOT has a unique
70   mips_got_entry for every symbol within it.  We can therefore use the
71   mips_got_entry fields (tls_type and gotidx) to track the symbol's
72   GOT index.
73
74   However, if it turns out that we need only a single GOT, we continue
75   to use the master GOT to describe it.  There may therefore be several
76   mips_got_entries for the same symbol, each with a different input bfd.
77   We want to make sure that each symbol gets a unique GOT entry, so when
78   there's a single GOT, we use the symbol's hash entry, not the
79   mips_got_entry fields, to track a symbol's GOT index.  */
80struct mips_got_entry
81{
82  /* The input bfd in which the symbol is defined.  */
83  bfd *abfd;
84  /* The index of the symbol, as stored in the relocation r_info, if
85     we have a local symbol; -1 otherwise.  */
86  long symndx;
87  union
88  {
89    /* If abfd == NULL, an address that must be stored in the got.  */
90    bfd_vma address;
91    /* If abfd != NULL && symndx != -1, the addend of the relocation
92       that should be added to the symbol value.  */
93    bfd_vma addend;
94    /* If abfd != NULL && symndx == -1, the hash table entry
95       corresponding to a global symbol in the got (or, local, if
96       h->forced_local).  */
97    struct mips_elf_link_hash_entry *h;
98  } d;
99
100  /* The TLS types included in this GOT entry (specifically, GD and
101     IE).  The GD and IE flags can be added as we encounter new
102     relocations.  LDM can also be set; it will always be alone, not
103     combined with any GD or IE flags.  An LDM GOT entry will be
104     a local symbol entry with r_symndx == 0.  */
105  unsigned char tls_type;
106
107  /* The offset from the beginning of the .got section to the entry
108     corresponding to this symbol+addend.  If it's a global symbol
109     whose offset is yet to be decided, it's going to be -1.  */
110  long gotidx;
111};
112
113/* This structure is used to hold .got information when linking.  */
114
115struct mips_got_info
116{
117  /* The global symbol in the GOT with the lowest index in the dynamic
118     symbol table.  */
119  struct elf_link_hash_entry *global_gotsym;
120  /* The number of global .got entries.  */
121  unsigned int global_gotno;
122  /* The number of .got slots used for TLS.  */
123  unsigned int tls_gotno;
124  /* The first unused TLS .got entry.  Used only during
125     mips_elf_initialize_tls_index.  */
126  unsigned int tls_assigned_gotno;
127  /* The number of local .got entries.  */
128  unsigned int local_gotno;
129  /* The number of local .got entries we have used.  */
130  unsigned int assigned_gotno;
131  /* A hash table holding members of the got.  */
132  struct htab *got_entries;
133  /* A hash table mapping input bfds to other mips_got_info.  NULL
134     unless multi-got was necessary.  */
135  struct htab *bfd2got;
136  /* In multi-got links, a pointer to the next got (err, rather, most
137     of the time, it points to the previous got).  */
138  struct mips_got_info *next;
139  /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
140     for none, or MINUS_TWO for not yet assigned.  This is needed
141     because a single-GOT link may have multiple hash table entries
142     for the LDM.  It does not get initialized in multi-GOT mode.  */
143  bfd_vma tls_ldm_offset;
144};
145
146/* Map an input bfd to a got in a multi-got link.  */
147
148struct mips_elf_bfd2got_hash {
149  bfd *bfd;
150  struct mips_got_info *g;
151};
152
153/* Structure passed when traversing the bfd2got hash table, used to
154   create and merge bfd's gots.  */
155
156struct mips_elf_got_per_bfd_arg
157{
158  /* A hashtable that maps bfds to gots.  */
159  htab_t bfd2got;
160  /* The output bfd.  */
161  bfd *obfd;
162  /* The link information.  */
163  struct bfd_link_info *info;
164  /* A pointer to the primary got, i.e., the one that's going to get
165     the implicit relocations from DT_MIPS_LOCAL_GOTNO and
166     DT_MIPS_GOTSYM.  */
167  struct mips_got_info *primary;
168  /* A non-primary got we're trying to merge with other input bfd's
169     gots.  */
170  struct mips_got_info *current;
171  /* The maximum number of got entries that can be addressed with a
172     16-bit offset.  */
173  unsigned int max_count;
174  /* The number of local and global entries in the primary got.  */
175  unsigned int primary_count;
176  /* The number of local and global entries in the current got.  */
177  unsigned int current_count;
178  /* The total number of global entries which will live in the
179     primary got and be automatically relocated.  This includes
180     those not referenced by the primary GOT but included in
181     the "master" GOT.  */
182  unsigned int global_count;
183};
184
185/* Another structure used to pass arguments for got entries traversal.  */
186
187struct mips_elf_set_global_got_offset_arg
188{
189  struct mips_got_info *g;
190  int value;
191  unsigned int needed_relocs;
192  struct bfd_link_info *info;
193};
194
195/* A structure used to count TLS relocations or GOT entries, for GOT
196   entry or ELF symbol table traversal.  */
197
198struct mips_elf_count_tls_arg
199{
200  struct bfd_link_info *info;
201  unsigned int needed;
202};
203
204struct _mips_elf_section_data
205{
206  struct bfd_elf_section_data elf;
207  union
208  {
209    struct mips_got_info *got_info;
210    bfd_byte *tdata;
211  } u;
212};
213
214#define mips_elf_section_data(sec) \
215  ((struct _mips_elf_section_data *) elf_section_data (sec))
216
217/* This structure is passed to mips_elf_sort_hash_table_f when sorting
218   the dynamic symbols.  */
219
220struct mips_elf_hash_sort_data
221{
222  /* The symbol in the global GOT with the lowest dynamic symbol table
223     index.  */
224  struct elf_link_hash_entry *low;
225  /* The least dynamic symbol table index corresponding to a non-TLS
226     symbol with a GOT entry.  */
227  long min_got_dynindx;
228  /* The greatest dynamic symbol table index corresponding to a symbol
229     with a GOT entry that is not referenced (e.g., a dynamic symbol
230     with dynamic relocations pointing to it from non-primary GOTs).  */
231  long max_unref_got_dynindx;
232  /* The greatest dynamic symbol table index not corresponding to a
233     symbol without a GOT entry.  */
234  long max_non_got_dynindx;
235};
236
237/* The MIPS ELF linker needs additional information for each symbol in
238   the global hash table.  */
239
240struct mips_elf_link_hash_entry
241{
242  struct elf_link_hash_entry root;
243
244  /* External symbol information.  */
245  EXTR esym;
246
247  /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
248     this symbol.  */
249  unsigned int possibly_dynamic_relocs;
250
251  /* If the R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 reloc is against
252     a readonly section.  */
253  bfd_boolean readonly_reloc;
254
255  /* We must not create a stub for a symbol that has relocations
256     related to taking the function's address, i.e. any but
257     R_MIPS_CALL*16 ones -- see "MIPS ABI Supplement, 3rd Edition",
258     p. 4-20.  */
259  bfd_boolean no_fn_stub;
260
261  /* If there is a stub that 32 bit functions should use to call this
262     16 bit function, this points to the section containing the stub.  */
263  asection *fn_stub;
264
265  /* Whether we need the fn_stub; this is set if this symbol appears
266     in any relocs other than a 16 bit call.  */
267  bfd_boolean need_fn_stub;
268
269  /* If there is a stub that 16 bit functions should use to call this
270     32 bit function, this points to the section containing the stub.  */
271  asection *call_stub;
272
273  /* This is like the call_stub field, but it is used if the function
274     being called returns a floating point value.  */
275  asection *call_fp_stub;
276
277  /* Are we forced local?  This will only be set if we have converted
278     the initial global GOT entry to a local GOT entry.  */
279  bfd_boolean forced_local;
280
281  /* Are we referenced by some kind of relocation?  */
282  bfd_boolean is_relocation_target;
283
284  /* Are we referenced by branch relocations?  */
285  bfd_boolean is_branch_target;
286
287#define GOT_NORMAL	0
288#define GOT_TLS_GD	1
289#define GOT_TLS_LDM	2
290#define GOT_TLS_IE	4
291#define GOT_TLS_OFFSET_DONE    0x40
292#define GOT_TLS_DONE    0x80
293  unsigned char tls_type;
294  /* This is only used in single-GOT mode; in multi-GOT mode there
295     is one mips_got_entry per GOT entry, so the offset is stored
296     there.  In single-GOT mode there may be many mips_got_entry
297     structures all referring to the same GOT slot.  It might be
298     possible to use root.got.offset instead, but that field is
299     overloaded already.  */
300  bfd_vma tls_got_offset;
301};
302
303/* MIPS ELF linker hash table.  */
304
305struct mips_elf_link_hash_table
306{
307  struct elf_link_hash_table root;
308#if 0
309  /* We no longer use this.  */
310  /* String section indices for the dynamic section symbols.  */
311  bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
312#endif
313  /* The number of .rtproc entries.  */
314  bfd_size_type procedure_count;
315  /* The size of the .compact_rel section (if SGI_COMPAT).  */
316  bfd_size_type compact_rel_size;
317  /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
318     entry is set to the address of __rld_obj_head as in IRIX5.  */
319  bfd_boolean use_rld_obj_head;
320  /* This is the value of the __rld_map or __rld_obj_head symbol.  */
321  bfd_vma rld_value;
322  /* This is set if we see any mips16 stub sections.  */
323  bfd_boolean mips16_stubs_seen;
324  /* True if we're generating code for VxWorks.  */
325  bfd_boolean is_vxworks;
326  /* Shortcuts to some dynamic sections, or NULL if they are not
327     being used.  */
328  asection *srelbss;
329  asection *sdynbss;
330  asection *srelplt;
331  asection *srelplt2;
332  asection *sgotplt;
333  asection *splt;
334  /* The size of the PLT header in bytes (VxWorks only).  */
335  bfd_vma plt_header_size;
336  /* The size of a PLT entry in bytes (VxWorks only).  */
337  bfd_vma plt_entry_size;
338  /* The size of a function stub entry in bytes.  */
339  bfd_vma function_stub_size;
340};
341
342#define TLS_RELOC_P(r_type) \
343  (r_type == R_MIPS_TLS_DTPMOD32		\
344   || r_type == R_MIPS_TLS_DTPMOD64		\
345   || r_type == R_MIPS_TLS_DTPREL32		\
346   || r_type == R_MIPS_TLS_DTPREL64		\
347   || r_type == R_MIPS_TLS_GD			\
348   || r_type == R_MIPS_TLS_LDM			\
349   || r_type == R_MIPS_TLS_DTPREL_HI16		\
350   || r_type == R_MIPS_TLS_DTPREL_LO16		\
351   || r_type == R_MIPS_TLS_GOTTPREL		\
352   || r_type == R_MIPS_TLS_TPREL32		\
353   || r_type == R_MIPS_TLS_TPREL64		\
354   || r_type == R_MIPS_TLS_TPREL_HI16		\
355   || r_type == R_MIPS_TLS_TPREL_LO16)
356
357/* Structure used to pass information to mips_elf_output_extsym.  */
358
359struct extsym_info
360{
361  bfd *abfd;
362  struct bfd_link_info *info;
363  struct ecoff_debug_info *debug;
364  const struct ecoff_debug_swap *swap;
365  bfd_boolean failed;
366};
367
368/* The names of the runtime procedure table symbols used on IRIX5.  */
369
370static const char * const mips_elf_dynsym_rtproc_names[] =
371{
372  "_procedure_table",
373  "_procedure_string_table",
374  "_procedure_table_size",
375  NULL
376};
377
378/* These structures are used to generate the .compact_rel section on
379   IRIX5.  */
380
381typedef struct
382{
383  unsigned long id1;		/* Always one?  */
384  unsigned long num;		/* Number of compact relocation entries.  */
385  unsigned long id2;		/* Always two?  */
386  unsigned long offset;		/* The file offset of the first relocation.  */
387  unsigned long reserved0;	/* Zero?  */
388  unsigned long reserved1;	/* Zero?  */
389} Elf32_compact_rel;
390
391typedef struct
392{
393  bfd_byte id1[4];
394  bfd_byte num[4];
395  bfd_byte id2[4];
396  bfd_byte offset[4];
397  bfd_byte reserved0[4];
398  bfd_byte reserved1[4];
399} Elf32_External_compact_rel;
400
401typedef struct
402{
403  unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
404  unsigned int rtype : 4;	/* Relocation types. See below.  */
405  unsigned int dist2to : 8;
406  unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
407  unsigned long konst;		/* KONST field. See below.  */
408  unsigned long vaddr;		/* VADDR to be relocated.  */
409} Elf32_crinfo;
410
411typedef struct
412{
413  unsigned int ctype : 1;	/* 1: long 0: short format. See below.  */
414  unsigned int rtype : 4;	/* Relocation types. See below.  */
415  unsigned int dist2to : 8;
416  unsigned int relvaddr : 19;	/* (VADDR - vaddr of the previous entry)/ 4 */
417  unsigned long konst;		/* KONST field. See below.  */
418} Elf32_crinfo2;
419
420typedef struct
421{
422  bfd_byte info[4];
423  bfd_byte konst[4];
424  bfd_byte vaddr[4];
425} Elf32_External_crinfo;
426
427typedef struct
428{
429  bfd_byte info[4];
430  bfd_byte konst[4];
431} Elf32_External_crinfo2;
432
433/* These are the constants used to swap the bitfields in a crinfo.  */
434
435#define CRINFO_CTYPE (0x1)
436#define CRINFO_CTYPE_SH (31)
437#define CRINFO_RTYPE (0xf)
438#define CRINFO_RTYPE_SH (27)
439#define CRINFO_DIST2TO (0xff)
440#define CRINFO_DIST2TO_SH (19)
441#define CRINFO_RELVADDR (0x7ffff)
442#define CRINFO_RELVADDR_SH (0)
443
444/* A compact relocation info has long (3 words) or short (2 words)
445   formats.  A short format doesn't have VADDR field and relvaddr
446   fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
447#define CRF_MIPS_LONG			1
448#define CRF_MIPS_SHORT			0
449
450/* There are 4 types of compact relocation at least. The value KONST
451   has different meaning for each type:
452
453   (type)		(konst)
454   CT_MIPS_REL32	Address in data
455   CT_MIPS_WORD		Address in word (XXX)
456   CT_MIPS_GPHI_LO	GP - vaddr
457   CT_MIPS_JMPAD	Address to jump
458   */
459
460#define CRT_MIPS_REL32			0xa
461#define CRT_MIPS_WORD			0xb
462#define CRT_MIPS_GPHI_LO		0xc
463#define CRT_MIPS_JMPAD			0xd
464
465#define mips_elf_set_cr_format(x,format)	((x).ctype = (format))
466#define mips_elf_set_cr_type(x,type)		((x).rtype = (type))
467#define mips_elf_set_cr_dist2to(x,v)		((x).dist2to = (v))
468#define mips_elf_set_cr_relvaddr(x,d)		((x).relvaddr = (d)<<2)
469
470/* The structure of the runtime procedure descriptor created by the
471   loader for use by the static exception system.  */
472
473typedef struct runtime_pdr {
474	bfd_vma	adr;		/* Memory address of start of procedure.  */
475	long	regmask;	/* Save register mask.  */
476	long	regoffset;	/* Save register offset.  */
477	long	fregmask;	/* Save floating point register mask.  */
478	long	fregoffset;	/* Save floating point register offset.  */
479	long	frameoffset;	/* Frame size.  */
480	short	framereg;	/* Frame pointer register.  */
481	short	pcreg;		/* Offset or reg of return pc.  */
482	long	irpss;		/* Index into the runtime string table.  */
483	long	reserved;
484	struct exception_info *exception_info;/* Pointer to exception array.  */
485} RPDR, *pRPDR;
486#define cbRPDR sizeof (RPDR)
487#define rpdNil ((pRPDR) 0)
488
489static struct mips_got_entry *mips_elf_create_local_got_entry
490  (bfd *, struct bfd_link_info *, bfd *, struct mips_got_info *, asection *,
491   bfd_vma, unsigned long, struct mips_elf_link_hash_entry *, int);
492static bfd_boolean mips_elf_sort_hash_table_f
493  (struct mips_elf_link_hash_entry *, void *);
494static bfd_vma mips_elf_high
495  (bfd_vma);
496static bfd_boolean mips16_stub_section_p
497  (bfd *, asection *);
498static bfd_boolean mips_elf_create_dynamic_relocation
499  (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
500   struct mips_elf_link_hash_entry *, asection *, bfd_vma,
501   bfd_vma *, asection *);
502static hashval_t mips_elf_got_entry_hash
503  (const void *);
504static bfd_vma mips_elf_adjust_gp
505  (bfd *, struct mips_got_info *, bfd *);
506static struct mips_got_info *mips_elf_got_for_ibfd
507  (struct mips_got_info *, bfd *);
508
509/* This will be used when we sort the dynamic relocation records.  */
510static bfd *reldyn_sorting_bfd;
511
512/* Nonzero if ABFD is using the N32 ABI.  */
513#define ABI_N32_P(abfd) \
514  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
515
516/* Nonzero if ABFD is using the N64 ABI.  */
517#define ABI_64_P(abfd) \
518  (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
519
520/* Nonzero if ABFD is using NewABI conventions.  */
521#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
522
523/* The IRIX compatibility level we are striving for.  */
524#define IRIX_COMPAT(abfd) \
525  (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
526
527/* Whether we are trying to be compatible with IRIX at all.  */
528#define SGI_COMPAT(abfd) \
529  (IRIX_COMPAT (abfd) != ict_none)
530
531/* The name of the options section.  */
532#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
533  (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
534
535/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
536   Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
537#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
538  (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
539
540/* Whether the section is readonly.  */
541#define MIPS_ELF_READONLY_SECTION(sec) \
542  ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))		\
543   == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
544
545/* The name of the stub section.  */
546#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
547
548/* The size of an external REL relocation.  */
549#define MIPS_ELF_REL_SIZE(abfd) \
550  (get_elf_backend_data (abfd)->s->sizeof_rel)
551
552/* The size of an external RELA relocation.  */
553#define MIPS_ELF_RELA_SIZE(abfd) \
554  (get_elf_backend_data (abfd)->s->sizeof_rela)
555
556/* The size of an external dynamic table entry.  */
557#define MIPS_ELF_DYN_SIZE(abfd) \
558  (get_elf_backend_data (abfd)->s->sizeof_dyn)
559
560/* The size of the rld_map pointer.  */
561#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
562  (get_elf_backend_data (abfd)->s->arch_size / 8)
563
564/* The size of a GOT entry.  */
565#define MIPS_ELF_GOT_SIZE(abfd) \
566  (get_elf_backend_data (abfd)->s->arch_size / 8)
567
568/* The size of a symbol-table entry.  */
569#define MIPS_ELF_SYM_SIZE(abfd) \
570  (get_elf_backend_data (abfd)->s->sizeof_sym)
571
572/* The default alignment for sections, as a power of two.  */
573#define MIPS_ELF_LOG_FILE_ALIGN(abfd)				\
574  (get_elf_backend_data (abfd)->s->log_file_align)
575
576/* Get word-sized data.  */
577#define MIPS_ELF_GET_WORD(abfd, ptr) \
578  (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
579
580/* Put out word-sized data.  */
581#define MIPS_ELF_PUT_WORD(abfd, val, ptr)	\
582  (ABI_64_P (abfd) 				\
583   ? bfd_put_64 (abfd, val, ptr) 		\
584   : bfd_put_32 (abfd, val, ptr))
585
586/* Add a dynamic symbol table-entry.  */
587#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)	\
588  _bfd_elf_add_dynamic_entry (info, tag, val)
589
590#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)			\
591  (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
592
593/* Determine whether the internal relocation of index REL_IDX is REL
594   (zero) or RELA (non-zero).  The assumption is that, if there are
595   two relocation sections for this section, one of them is REL and
596   the other is RELA.  If the index of the relocation we're testing is
597   in range for the first relocation section, check that the external
598   relocation size is that for RELA.  It is also assumed that, if
599   rel_idx is not in range for the first section, and this first
600   section contains REL relocs, then the relocation is in the second
601   section, that is RELA.  */
602#define MIPS_RELOC_RELA_P(abfd, sec, rel_idx)				\
603  ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr)			\
604    * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel		\
605    > (bfd_vma)(rel_idx))						\
606   == (elf_section_data (sec)->rel_hdr.sh_entsize			\
607       == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela)		\
608	   : sizeof (Elf32_External_Rela))))
609
610/* The name of the dynamic relocation section.  */
611#define MIPS_ELF_REL_DYN_NAME(INFO) \
612  (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
613
614/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
615   from smaller values.  Start with zero, widen, *then* decrement.  */
616#define MINUS_ONE	(((bfd_vma)0) - 1)
617#define MINUS_TWO	(((bfd_vma)0) - 2)
618
619/* The number of local .got entries we reserve.  */
620#define MIPS_RESERVED_GOTNO(INFO) \
621  (mips_elf_hash_table (INFO)->is_vxworks ? 3 : 2)
622
623/* The offset of $gp from the beginning of the .got section.  */
624#define ELF_MIPS_GP_OFFSET(INFO) \
625  (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
626
627/* The maximum size of the GOT for it to be addressable using 16-bit
628   offsets from $gp.  */
629#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
630
631/* Instructions which appear in a stub.  */
632#define STUB_LW(abfd)							\
633  ((ABI_64_P (abfd)							\
634    ? 0xdf998010				/* ld t9,0x8010(gp) */	\
635    : 0x8f998010))              		/* lw t9,0x8010(gp) */
636#define STUB_MOVE(abfd)							\
637   ((ABI_64_P (abfd)							\
638     ? 0x03e0782d				/* daddu t7,ra */	\
639     : 0x03e07821))				/* addu t7,ra */
640#define STUB_LUI(VAL) (0x3c180000 + (VAL))	/* lui t8,VAL */
641#define STUB_JALR 0x0320f809			/* jalr t9,ra */
642#define STUB_ORI(VAL) (0x37180000 + (VAL))	/* ori t8,t8,VAL */
643#define STUB_LI16U(VAL) (0x34180000 + (VAL))	/* ori t8,zero,VAL unsigned */
644#define STUB_LI16S(abfd, VAL)						\
645   ((ABI_64_P (abfd)							\
646    ? (0x64180000 + (VAL))	/* daddiu t8,zero,VAL sign extended */	\
647    : (0x24180000 + (VAL))))	/* addiu t8,zero,VAL sign extended */
648
649#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
650#define MIPS_FUNCTION_STUB_BIG_SIZE 20
651
652/* The name of the dynamic interpreter.  This is put in the .interp
653   section.  */
654
655#define ELF_DYNAMIC_INTERPRETER(abfd) 		\
656   (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" 	\
657    : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" 	\
658    : "/usr/lib/libc.so.1")
659
660#ifdef BFD64
661#define MNAME(bfd,pre,pos) \
662  (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
663#define ELF_R_SYM(bfd, i)					\
664  (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
665#define ELF_R_TYPE(bfd, i)					\
666  (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
667#define ELF_R_INFO(bfd, s, t)					\
668  (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
669#else
670#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
671#define ELF_R_SYM(bfd, i)					\
672  (ELF32_R_SYM (i))
673#define ELF_R_TYPE(bfd, i)					\
674  (ELF32_R_TYPE (i))
675#define ELF_R_INFO(bfd, s, t)					\
676  (ELF32_R_INFO (s, t))
677#endif
678
679  /* The mips16 compiler uses a couple of special sections to handle
680     floating point arguments.
681
682     Section names that look like .mips16.fn.FNNAME contain stubs that
683     copy floating point arguments from the fp regs to the gp regs and
684     then jump to FNNAME.  If any 32 bit function calls FNNAME, the
685     call should be redirected to the stub instead.  If no 32 bit
686     function calls FNNAME, the stub should be discarded.  We need to
687     consider any reference to the function, not just a call, because
688     if the address of the function is taken we will need the stub,
689     since the address might be passed to a 32 bit function.
690
691     Section names that look like .mips16.call.FNNAME contain stubs
692     that copy floating point arguments from the gp regs to the fp
693     regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
694     then any 16 bit function that calls FNNAME should be redirected
695     to the stub instead.  If FNNAME is not a 32 bit function, the
696     stub should be discarded.
697
698     .mips16.call.fp.FNNAME sections are similar, but contain stubs
699     which call FNNAME and then copy the return value from the fp regs
700     to the gp regs.  These stubs store the return value in $18 while
701     calling FNNAME; any function which might call one of these stubs
702     must arrange to save $18 around the call.  (This case is not
703     needed for 32 bit functions that call 16 bit functions, because
704     16 bit functions always return floating point values in both
705     $f0/$f1 and $2/$3.)
706
707     Note that in all cases FNNAME might be defined statically.
708     Therefore, FNNAME is not used literally.  Instead, the relocation
709     information will indicate which symbol the section is for.
710
711     We record any stubs that we find in the symbol table.  */
712
713#define FN_STUB ".mips16.fn."
714#define CALL_STUB ".mips16.call."
715#define CALL_FP_STUB ".mips16.call.fp."
716
717#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
718#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
719#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
720
721/* The format of the first PLT entry in a VxWorks executable.  */
722static const bfd_vma mips_vxworks_exec_plt0_entry[] = {
723  0x3c190000,	/* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)		*/
724  0x27390000,	/* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_)	*/
725  0x8f390008,	/* lw t9, 8(t9)					*/
726  0x00000000,	/* nop						*/
727  0x03200008,	/* jr t9					*/
728  0x00000000	/* nop						*/
729};
730
731/* The format of subsequent PLT entries.  */
732static const bfd_vma mips_vxworks_exec_plt_entry[] = {
733  0x10000000,	/* b .PLT_resolver			*/
734  0x24180000,	/* li t8, <pltindex>			*/
735  0x3c190000,	/* lui t9, %hi(<.got.plt slot>)		*/
736  0x27390000,	/* addiu t9, t9, %lo(<.got.plt slot>)	*/
737  0x8f390000,	/* lw t9, 0(t9)				*/
738  0x00000000,	/* nop					*/
739  0x03200008,	/* jr t9				*/
740  0x00000000	/* nop					*/
741};
742
743/* The format of the first PLT entry in a VxWorks shared object.  */
744static const bfd_vma mips_vxworks_shared_plt0_entry[] = {
745  0x8f990008,	/* lw t9, 8(gp)		*/
746  0x00000000,	/* nop			*/
747  0x03200008,	/* jr t9		*/
748  0x00000000,	/* nop			*/
749  0x00000000,	/* nop			*/
750  0x00000000	/* nop			*/
751};
752
753/* The format of subsequent PLT entries.  */
754static const bfd_vma mips_vxworks_shared_plt_entry[] = {
755  0x10000000,	/* b .PLT_resolver	*/
756  0x24180000	/* li t8, <pltindex>	*/
757};
758
759/* Look up an entry in a MIPS ELF linker hash table.  */
760
761#define mips_elf_link_hash_lookup(table, string, create, copy, follow)	\
762  ((struct mips_elf_link_hash_entry *)					\
763   elf_link_hash_lookup (&(table)->root, (string), (create),		\
764			 (copy), (follow)))
765
766/* Traverse a MIPS ELF linker hash table.  */
767
768#define mips_elf_link_hash_traverse(table, func, info)			\
769  (elf_link_hash_traverse						\
770   (&(table)->root,							\
771    (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),	\
772    (info)))
773
774/* Get the MIPS ELF linker hash table from a link_info structure.  */
775
776#define mips_elf_hash_table(p) \
777  ((struct mips_elf_link_hash_table *) ((p)->hash))
778
779/* Find the base offsets for thread-local storage in this object,
780   for GD/LD and IE/LE respectively.  */
781
782#define TP_OFFSET 0x7000
783#define DTP_OFFSET 0x8000
784
785static bfd_vma
786dtprel_base (struct bfd_link_info *info)
787{
788  /* If tls_sec is NULL, we should have signalled an error already.  */
789  if (elf_hash_table (info)->tls_sec == NULL)
790    return 0;
791  return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
792}
793
794static bfd_vma
795tprel_base (struct bfd_link_info *info)
796{
797  /* If tls_sec is NULL, we should have signalled an error already.  */
798  if (elf_hash_table (info)->tls_sec == NULL)
799    return 0;
800  return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
801}
802
803/* Create an entry in a MIPS ELF linker hash table.  */
804
805static struct bfd_hash_entry *
806mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
807			    struct bfd_hash_table *table, const char *string)
808{
809  struct mips_elf_link_hash_entry *ret =
810    (struct mips_elf_link_hash_entry *) entry;
811
812  /* Allocate the structure if it has not already been allocated by a
813     subclass.  */
814  if (ret == NULL)
815    ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
816  if (ret == NULL)
817    return (struct bfd_hash_entry *) ret;
818
819  /* Call the allocation method of the superclass.  */
820  ret = ((struct mips_elf_link_hash_entry *)
821	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
822				     table, string));
823  if (ret != NULL)
824    {
825      /* Set local fields.  */
826      memset (&ret->esym, 0, sizeof (EXTR));
827      /* We use -2 as a marker to indicate that the information has
828	 not been set.  -1 means there is no associated ifd.  */
829      ret->esym.ifd = -2;
830      ret->possibly_dynamic_relocs = 0;
831      ret->readonly_reloc = FALSE;
832      ret->no_fn_stub = FALSE;
833      ret->fn_stub = NULL;
834      ret->need_fn_stub = FALSE;
835      ret->call_stub = NULL;
836      ret->call_fp_stub = NULL;
837      ret->forced_local = FALSE;
838      ret->is_branch_target = FALSE;
839      ret->is_relocation_target = FALSE;
840      ret->tls_type = GOT_NORMAL;
841    }
842
843  return (struct bfd_hash_entry *) ret;
844}
845
846bfd_boolean
847_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
848{
849  if (!sec->used_by_bfd)
850    {
851      struct _mips_elf_section_data *sdata;
852      bfd_size_type amt = sizeof (*sdata);
853
854      sdata = bfd_zalloc (abfd, amt);
855      if (sdata == NULL)
856	return FALSE;
857      sec->used_by_bfd = sdata;
858    }
859
860  return _bfd_elf_new_section_hook (abfd, sec);
861}
862
863/* Read ECOFF debugging information from a .mdebug section into a
864   ecoff_debug_info structure.  */
865
866bfd_boolean
867_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
868			       struct ecoff_debug_info *debug)
869{
870  HDRR *symhdr;
871  const struct ecoff_debug_swap *swap;
872  char *ext_hdr;
873
874  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
875  memset (debug, 0, sizeof (*debug));
876
877  ext_hdr = bfd_malloc (swap->external_hdr_size);
878  if (ext_hdr == NULL && swap->external_hdr_size != 0)
879    goto error_return;
880
881  if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
882				  swap->external_hdr_size))
883    goto error_return;
884
885  symhdr = &debug->symbolic_header;
886  (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
887
888  /* The symbolic header contains absolute file offsets and sizes to
889     read.  */
890#define READ(ptr, offset, count, size, type)				\
891  if (symhdr->count == 0)						\
892    debug->ptr = NULL;							\
893  else									\
894    {									\
895      bfd_size_type amt = (bfd_size_type) size * symhdr->count;		\
896      debug->ptr = bfd_malloc (amt);					\
897      if (debug->ptr == NULL)						\
898	goto error_return;						\
899      if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0		\
900	  || bfd_bread (debug->ptr, amt, abfd) != amt)			\
901	goto error_return;						\
902    }
903
904  READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
905  READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
906  READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
907  READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
908  READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
909  READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
910	union aux_ext *);
911  READ (ss, cbSsOffset, issMax, sizeof (char), char *);
912  READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
913  READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
914  READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
915  READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
916#undef READ
917
918  debug->fdr = NULL;
919
920  return TRUE;
921
922 error_return:
923  if (ext_hdr != NULL)
924    free (ext_hdr);
925  if (debug->line != NULL)
926    free (debug->line);
927  if (debug->external_dnr != NULL)
928    free (debug->external_dnr);
929  if (debug->external_pdr != NULL)
930    free (debug->external_pdr);
931  if (debug->external_sym != NULL)
932    free (debug->external_sym);
933  if (debug->external_opt != NULL)
934    free (debug->external_opt);
935  if (debug->external_aux != NULL)
936    free (debug->external_aux);
937  if (debug->ss != NULL)
938    free (debug->ss);
939  if (debug->ssext != NULL)
940    free (debug->ssext);
941  if (debug->external_fdr != NULL)
942    free (debug->external_fdr);
943  if (debug->external_rfd != NULL)
944    free (debug->external_rfd);
945  if (debug->external_ext != NULL)
946    free (debug->external_ext);
947  return FALSE;
948}
949
950/* Swap RPDR (runtime procedure table entry) for output.  */
951
952static void
953ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
954{
955  H_PUT_S32 (abfd, in->adr, ex->p_adr);
956  H_PUT_32 (abfd, in->regmask, ex->p_regmask);
957  H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
958  H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
959  H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
960  H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
961
962  H_PUT_16 (abfd, in->framereg, ex->p_framereg);
963  H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
964
965  H_PUT_32 (abfd, in->irpss, ex->p_irpss);
966}
967
968/* Create a runtime procedure table from the .mdebug section.  */
969
970static bfd_boolean
971mips_elf_create_procedure_table (void *handle, bfd *abfd,
972				 struct bfd_link_info *info, asection *s,
973				 struct ecoff_debug_info *debug)
974{
975  const struct ecoff_debug_swap *swap;
976  HDRR *hdr = &debug->symbolic_header;
977  RPDR *rpdr, *rp;
978  struct rpdr_ext *erp;
979  void *rtproc;
980  struct pdr_ext *epdr;
981  struct sym_ext *esym;
982  char *ss, **sv;
983  char *str;
984  bfd_size_type size;
985  bfd_size_type count;
986  unsigned long sindex;
987  unsigned long i;
988  PDR pdr;
989  SYMR sym;
990  const char *no_name_func = _("static procedure (no name)");
991
992  epdr = NULL;
993  rpdr = NULL;
994  esym = NULL;
995  ss = NULL;
996  sv = NULL;
997
998  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
999
1000  sindex = strlen (no_name_func) + 1;
1001  count = hdr->ipdMax;
1002  if (count > 0)
1003    {
1004      size = swap->external_pdr_size;
1005
1006      epdr = bfd_malloc (size * count);
1007      if (epdr == NULL)
1008	goto error_return;
1009
1010      if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1011	goto error_return;
1012
1013      size = sizeof (RPDR);
1014      rp = rpdr = bfd_malloc (size * count);
1015      if (rpdr == NULL)
1016	goto error_return;
1017
1018      size = sizeof (char *);
1019      sv = bfd_malloc (size * count);
1020      if (sv == NULL)
1021	goto error_return;
1022
1023      count = hdr->isymMax;
1024      size = swap->external_sym_size;
1025      esym = bfd_malloc (size * count);
1026      if (esym == NULL)
1027	goto error_return;
1028
1029      if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1030	goto error_return;
1031
1032      count = hdr->issMax;
1033      ss = bfd_malloc (count);
1034      if (ss == NULL)
1035	goto error_return;
1036      if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1037	goto error_return;
1038
1039      count = hdr->ipdMax;
1040      for (i = 0; i < (unsigned long) count; i++, rp++)
1041	{
1042	  (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1043	  (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1044	  rp->adr = sym.value;
1045	  rp->regmask = pdr.regmask;
1046	  rp->regoffset = pdr.regoffset;
1047	  rp->fregmask = pdr.fregmask;
1048	  rp->fregoffset = pdr.fregoffset;
1049	  rp->frameoffset = pdr.frameoffset;
1050	  rp->framereg = pdr.framereg;
1051	  rp->pcreg = pdr.pcreg;
1052	  rp->irpss = sindex;
1053	  sv[i] = ss + sym.iss;
1054	  sindex += strlen (sv[i]) + 1;
1055	}
1056    }
1057
1058  size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1059  size = BFD_ALIGN (size, 16);
1060  rtproc = bfd_alloc (abfd, size);
1061  if (rtproc == NULL)
1062    {
1063      mips_elf_hash_table (info)->procedure_count = 0;
1064      goto error_return;
1065    }
1066
1067  mips_elf_hash_table (info)->procedure_count = count + 2;
1068
1069  erp = rtproc;
1070  memset (erp, 0, sizeof (struct rpdr_ext));
1071  erp++;
1072  str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1073  strcpy (str, no_name_func);
1074  str += strlen (no_name_func) + 1;
1075  for (i = 0; i < count; i++)
1076    {
1077      ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1078      strcpy (str, sv[i]);
1079      str += strlen (sv[i]) + 1;
1080    }
1081  H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1082
1083  /* Set the size and contents of .rtproc section.  */
1084  s->size = size;
1085  s->contents = rtproc;
1086
1087  /* Skip this section later on (I don't think this currently
1088     matters, but someday it might).  */
1089  s->map_head.link_order = NULL;
1090
1091  if (epdr != NULL)
1092    free (epdr);
1093  if (rpdr != NULL)
1094    free (rpdr);
1095  if (esym != NULL)
1096    free (esym);
1097  if (ss != NULL)
1098    free (ss);
1099  if (sv != NULL)
1100    free (sv);
1101
1102  return TRUE;
1103
1104 error_return:
1105  if (epdr != NULL)
1106    free (epdr);
1107  if (rpdr != NULL)
1108    free (rpdr);
1109  if (esym != NULL)
1110    free (esym);
1111  if (ss != NULL)
1112    free (ss);
1113  if (sv != NULL)
1114    free (sv);
1115  return FALSE;
1116}
1117
1118/* Check the mips16 stubs for a particular symbol, and see if we can
1119   discard them.  */
1120
1121static bfd_boolean
1122mips_elf_check_mips16_stubs (struct mips_elf_link_hash_entry *h,
1123			     void *data ATTRIBUTE_UNUSED)
1124{
1125  if (h->root.root.type == bfd_link_hash_warning)
1126    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1127
1128  if (h->fn_stub != NULL
1129      && ! h->need_fn_stub)
1130    {
1131      /* We don't need the fn_stub; the only references to this symbol
1132         are 16 bit calls.  Clobber the size to 0 to prevent it from
1133         being included in the link.  */
1134      h->fn_stub->size = 0;
1135      h->fn_stub->flags &= ~SEC_RELOC;
1136      h->fn_stub->reloc_count = 0;
1137      h->fn_stub->flags |= SEC_EXCLUDE;
1138    }
1139
1140  if (h->call_stub != NULL
1141      && h->root.other == STO_MIPS16)
1142    {
1143      /* We don't need the call_stub; this is a 16 bit function, so
1144         calls from other 16 bit functions are OK.  Clobber the size
1145         to 0 to prevent it from being included in the link.  */
1146      h->call_stub->size = 0;
1147      h->call_stub->flags &= ~SEC_RELOC;
1148      h->call_stub->reloc_count = 0;
1149      h->call_stub->flags |= SEC_EXCLUDE;
1150    }
1151
1152  if (h->call_fp_stub != NULL
1153      && h->root.other == STO_MIPS16)
1154    {
1155      /* We don't need the call_stub; this is a 16 bit function, so
1156         calls from other 16 bit functions are OK.  Clobber the size
1157         to 0 to prevent it from being included in the link.  */
1158      h->call_fp_stub->size = 0;
1159      h->call_fp_stub->flags &= ~SEC_RELOC;
1160      h->call_fp_stub->reloc_count = 0;
1161      h->call_fp_stub->flags |= SEC_EXCLUDE;
1162    }
1163
1164  return TRUE;
1165}
1166
1167/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1168   Most mips16 instructions are 16 bits, but these instructions
1169   are 32 bits.
1170
1171   The format of these instructions is:
1172
1173   +--------------+--------------------------------+
1174   |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
1175   +--------------+--------------------------------+
1176   |                Immediate  15:0                |
1177   +-----------------------------------------------+
1178
1179   JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
1180   Note that the immediate value in the first word is swapped.
1181
1182   When producing a relocatable object file, R_MIPS16_26 is
1183   handled mostly like R_MIPS_26.  In particular, the addend is
1184   stored as a straight 26-bit value in a 32-bit instruction.
1185   (gas makes life simpler for itself by never adjusting a
1186   R_MIPS16_26 reloc to be against a section, so the addend is
1187   always zero).  However, the 32 bit instruction is stored as 2
1188   16-bit values, rather than a single 32-bit value.  In a
1189   big-endian file, the result is the same; in a little-endian
1190   file, the two 16-bit halves of the 32 bit value are swapped.
1191   This is so that a disassembler can recognize the jal
1192   instruction.
1193
1194   When doing a final link, R_MIPS16_26 is treated as a 32 bit
1195   instruction stored as two 16-bit values.  The addend A is the
1196   contents of the targ26 field.  The calculation is the same as
1197   R_MIPS_26.  When storing the calculated value, reorder the
1198   immediate value as shown above, and don't forget to store the
1199   value as two 16-bit values.
1200
1201   To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1202   defined as
1203
1204   big-endian:
1205   +--------+----------------------+
1206   |        |                      |
1207   |        |    targ26-16         |
1208   |31    26|25                   0|
1209   +--------+----------------------+
1210
1211   little-endian:
1212   +----------+------+-------------+
1213   |          |      |             |
1214   |  sub1    |      |     sub2    |
1215   |0        9|10  15|16         31|
1216   +----------+--------------------+
1217   where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1218   ((sub1 << 16) | sub2)).
1219
1220   When producing a relocatable object file, the calculation is
1221   (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1222   When producing a fully linked file, the calculation is
1223   let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1224   ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1225
1226   R_MIPS16_GPREL is used for GP-relative addressing in mips16
1227   mode.  A typical instruction will have a format like this:
1228
1229   +--------------+--------------------------------+
1230   |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
1231   +--------------+--------------------------------+
1232   |    Major     |   rx   |   ry   |   Imm  4:0   |
1233   +--------------+--------------------------------+
1234
1235   EXTEND is the five bit value 11110.  Major is the instruction
1236   opcode.
1237
1238   This is handled exactly like R_MIPS_GPREL16, except that the
1239   addend is retrieved and stored as shown in this diagram; that
1240   is, the Imm fields above replace the V-rel16 field.
1241
1242   All we need to do here is shuffle the bits appropriately.  As
1243   above, the two 16-bit halves must be swapped on a
1244   little-endian system.
1245
1246   R_MIPS16_HI16 and R_MIPS16_LO16 are used in mips16 mode to
1247   access data when neither GP-relative nor PC-relative addressing
1248   can be used.  They are handled like R_MIPS_HI16 and R_MIPS_LO16,
1249   except that the addend is retrieved and stored as shown above
1250   for R_MIPS16_GPREL.
1251  */
1252void
1253_bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1254				 bfd_boolean jal_shuffle, bfd_byte *data)
1255{
1256  bfd_vma extend, insn, val;
1257
1258  if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
1259      && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
1260    return;
1261
1262  /* Pick up the mips16 extend instruction and the real instruction.  */
1263  extend = bfd_get_16 (abfd, data);
1264  insn = bfd_get_16 (abfd, data + 2);
1265  if (r_type == R_MIPS16_26)
1266    {
1267      if (jal_shuffle)
1268	val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1269	      | ((extend & 0x1f) << 21) | insn;
1270      else
1271	val = extend << 16 | insn;
1272    }
1273  else
1274    val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1275	  | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1276  bfd_put_32 (abfd, val, data);
1277}
1278
1279void
1280_bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1281			       bfd_boolean jal_shuffle, bfd_byte *data)
1282{
1283  bfd_vma extend, insn, val;
1284
1285  if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
1286      && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
1287    return;
1288
1289  val = bfd_get_32 (abfd, data);
1290  if (r_type == R_MIPS16_26)
1291    {
1292      if (jal_shuffle)
1293	{
1294	  insn = val & 0xffff;
1295	  extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1296		   | ((val >> 21) & 0x1f);
1297	}
1298      else
1299	{
1300	  insn = val & 0xffff;
1301	  extend = val >> 16;
1302	}
1303    }
1304  else
1305    {
1306      insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1307      extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1308    }
1309  bfd_put_16 (abfd, insn, data + 2);
1310  bfd_put_16 (abfd, extend, data);
1311}
1312
1313bfd_reloc_status_type
1314_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1315			       arelent *reloc_entry, asection *input_section,
1316			       bfd_boolean relocatable, void *data, bfd_vma gp)
1317{
1318  bfd_vma relocation;
1319  bfd_signed_vma val;
1320  bfd_reloc_status_type status;
1321
1322  if (bfd_is_com_section (symbol->section))
1323    relocation = 0;
1324  else
1325    relocation = symbol->value;
1326
1327  relocation += symbol->section->output_section->vma;
1328  relocation += symbol->section->output_offset;
1329
1330  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1331    return bfd_reloc_outofrange;
1332
1333  /* Set val to the offset into the section or symbol.  */
1334  val = reloc_entry->addend;
1335
1336  _bfd_mips_elf_sign_extend (val, 16);
1337
1338  /* Adjust val for the final section location and GP value.  If we
1339     are producing relocatable output, we don't want to do this for
1340     an external symbol.  */
1341  if (! relocatable
1342      || (symbol->flags & BSF_SECTION_SYM) != 0)
1343    val += relocation - gp;
1344
1345  if (reloc_entry->howto->partial_inplace)
1346    {
1347      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1348				       (bfd_byte *) data
1349				       + reloc_entry->address);
1350      if (status != bfd_reloc_ok)
1351	return status;
1352    }
1353  else
1354    reloc_entry->addend = val;
1355
1356  if (relocatable)
1357    reloc_entry->address += input_section->output_offset;
1358
1359  return bfd_reloc_ok;
1360}
1361
1362/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1363   R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
1364   that contains the relocation field and DATA points to the start of
1365   INPUT_SECTION.  */
1366
1367struct mips_hi16
1368{
1369  struct mips_hi16 *next;
1370  bfd_byte *data;
1371  asection *input_section;
1372  arelent rel;
1373};
1374
1375/* FIXME: This should not be a static variable.  */
1376
1377static struct mips_hi16 *mips_hi16_list;
1378
1379/* A howto special_function for REL *HI16 relocations.  We can only
1380   calculate the correct value once we've seen the partnering
1381   *LO16 relocation, so just save the information for later.
1382
1383   The ABI requires that the *LO16 immediately follow the *HI16.
1384   However, as a GNU extension, we permit an arbitrary number of
1385   *HI16s to be associated with a single *LO16.  This significantly
1386   simplies the relocation handling in gcc.  */
1387
1388bfd_reloc_status_type
1389_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1390			  asymbol *symbol ATTRIBUTE_UNUSED, void *data,
1391			  asection *input_section, bfd *output_bfd,
1392			  char **error_message ATTRIBUTE_UNUSED)
1393{
1394  struct mips_hi16 *n;
1395
1396  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1397    return bfd_reloc_outofrange;
1398
1399  n = bfd_malloc (sizeof *n);
1400  if (n == NULL)
1401    return bfd_reloc_outofrange;
1402
1403  n->next = mips_hi16_list;
1404  n->data = data;
1405  n->input_section = input_section;
1406  n->rel = *reloc_entry;
1407  mips_hi16_list = n;
1408
1409  if (output_bfd != NULL)
1410    reloc_entry->address += input_section->output_offset;
1411
1412  return bfd_reloc_ok;
1413}
1414
1415/* A howto special_function for REL R_MIPS_GOT16 relocations.  This is just
1416   like any other 16-bit relocation when applied to global symbols, but is
1417   treated in the same as R_MIPS_HI16 when applied to local symbols.  */
1418
1419bfd_reloc_status_type
1420_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1421			   void *data, asection *input_section,
1422			   bfd *output_bfd, char **error_message)
1423{
1424  if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1425      || bfd_is_und_section (bfd_get_section (symbol))
1426      || bfd_is_com_section (bfd_get_section (symbol)))
1427    /* The relocation is against a global symbol.  */
1428    return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1429					input_section, output_bfd,
1430					error_message);
1431
1432  return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1433				   input_section, output_bfd, error_message);
1434}
1435
1436/* A howto special_function for REL *LO16 relocations.  The *LO16 itself
1437   is a straightforward 16 bit inplace relocation, but we must deal with
1438   any partnering high-part relocations as well.  */
1439
1440bfd_reloc_status_type
1441_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1442			  void *data, asection *input_section,
1443			  bfd *output_bfd, char **error_message)
1444{
1445  bfd_vma vallo;
1446  bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1447
1448  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1449    return bfd_reloc_outofrange;
1450
1451  _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1452				   location);
1453  vallo = bfd_get_32 (abfd, location);
1454  _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1455				 location);
1456
1457  while (mips_hi16_list != NULL)
1458    {
1459      bfd_reloc_status_type ret;
1460      struct mips_hi16 *hi;
1461
1462      hi = mips_hi16_list;
1463
1464      /* R_MIPS_GOT16 relocations are something of a special case.  We
1465	 want to install the addend in the same way as for a R_MIPS_HI16
1466	 relocation (with a rightshift of 16).  However, since GOT16
1467	 relocations can also be used with global symbols, their howto
1468	 has a rightshift of 0.  */
1469      if (hi->rel.howto->type == R_MIPS_GOT16)
1470	hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
1471
1472      /* VALLO is a signed 16-bit number.  Bias it by 0x8000 so that any
1473	 carry or borrow will induce a change of +1 or -1 in the high part.  */
1474      hi->rel.addend += (vallo + 0x8000) & 0xffff;
1475
1476      ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
1477					 hi->input_section, output_bfd,
1478					 error_message);
1479      if (ret != bfd_reloc_ok)
1480	return ret;
1481
1482      mips_hi16_list = hi->next;
1483      free (hi);
1484    }
1485
1486  return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1487				      input_section, output_bfd,
1488				      error_message);
1489}
1490
1491/* A generic howto special_function.  This calculates and installs the
1492   relocation itself, thus avoiding the oft-discussed problems in
1493   bfd_perform_relocation and bfd_install_relocation.  */
1494
1495bfd_reloc_status_type
1496_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1497			     asymbol *symbol, void *data ATTRIBUTE_UNUSED,
1498			     asection *input_section, bfd *output_bfd,
1499			     char **error_message ATTRIBUTE_UNUSED)
1500{
1501  bfd_signed_vma val;
1502  bfd_reloc_status_type status;
1503  bfd_boolean relocatable;
1504
1505  relocatable = (output_bfd != NULL);
1506
1507  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1508    return bfd_reloc_outofrange;
1509
1510  /* Build up the field adjustment in VAL.  */
1511  val = 0;
1512  if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
1513    {
1514      /* Either we're calculating the final field value or we have a
1515	 relocation against a section symbol.  Add in the section's
1516	 offset or address.  */
1517      val += symbol->section->output_section->vma;
1518      val += symbol->section->output_offset;
1519    }
1520
1521  if (!relocatable)
1522    {
1523      /* We're calculating the final field value.  Add in the symbol's value
1524	 and, if pc-relative, subtract the address of the field itself.  */
1525      val += symbol->value;
1526      if (reloc_entry->howto->pc_relative)
1527	{
1528	  val -= input_section->output_section->vma;
1529	  val -= input_section->output_offset;
1530	  val -= reloc_entry->address;
1531	}
1532    }
1533
1534  /* VAL is now the final adjustment.  If we're keeping this relocation
1535     in the output file, and if the relocation uses a separate addend,
1536     we just need to add VAL to that addend.  Otherwise we need to add
1537     VAL to the relocation field itself.  */
1538  if (relocatable && !reloc_entry->howto->partial_inplace)
1539    reloc_entry->addend += val;
1540  else
1541    {
1542      bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1543
1544      /* Add in the separate addend, if any.  */
1545      val += reloc_entry->addend;
1546
1547      /* Add VAL to the relocation field.  */
1548      _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1549				       location);
1550      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1551				       location);
1552      _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1553				     location);
1554
1555      if (status != bfd_reloc_ok)
1556	return status;
1557    }
1558
1559  if (relocatable)
1560    reloc_entry->address += input_section->output_offset;
1561
1562  return bfd_reloc_ok;
1563}
1564
1565/* Swap an entry in a .gptab section.  Note that these routines rely
1566   on the equivalence of the two elements of the union.  */
1567
1568static void
1569bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
1570			      Elf32_gptab *in)
1571{
1572  in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
1573  in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
1574}
1575
1576static void
1577bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
1578			       Elf32_External_gptab *ex)
1579{
1580  H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
1581  H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
1582}
1583
1584static void
1585bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
1586				Elf32_External_compact_rel *ex)
1587{
1588  H_PUT_32 (abfd, in->id1, ex->id1);
1589  H_PUT_32 (abfd, in->num, ex->num);
1590  H_PUT_32 (abfd, in->id2, ex->id2);
1591  H_PUT_32 (abfd, in->offset, ex->offset);
1592  H_PUT_32 (abfd, in->reserved0, ex->reserved0);
1593  H_PUT_32 (abfd, in->reserved1, ex->reserved1);
1594}
1595
1596static void
1597bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
1598			   Elf32_External_crinfo *ex)
1599{
1600  unsigned long l;
1601
1602  l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1603       | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1604       | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1605       | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1606  H_PUT_32 (abfd, l, ex->info);
1607  H_PUT_32 (abfd, in->konst, ex->konst);
1608  H_PUT_32 (abfd, in->vaddr, ex->vaddr);
1609}
1610
1611/* A .reginfo section holds a single Elf32_RegInfo structure.  These
1612   routines swap this structure in and out.  They are used outside of
1613   BFD, so they are globally visible.  */
1614
1615void
1616bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
1617				Elf32_RegInfo *in)
1618{
1619  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1620  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1621  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1622  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1623  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1624  in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
1625}
1626
1627void
1628bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
1629				 Elf32_External_RegInfo *ex)
1630{
1631  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1632  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1633  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1634  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1635  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1636  H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
1637}
1638
1639/* In the 64 bit ABI, the .MIPS.options section holds register
1640   information in an Elf64_Reginfo structure.  These routines swap
1641   them in and out.  They are globally visible because they are used
1642   outside of BFD.  These routines are here so that gas can call them
1643   without worrying about whether the 64 bit ABI has been included.  */
1644
1645void
1646bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
1647				Elf64_Internal_RegInfo *in)
1648{
1649  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1650  in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
1651  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1652  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1653  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1654  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1655  in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
1656}
1657
1658void
1659bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
1660				 Elf64_External_RegInfo *ex)
1661{
1662  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1663  H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
1664  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1665  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1666  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1667  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1668  H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
1669}
1670
1671/* Swap in an options header.  */
1672
1673void
1674bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
1675			      Elf_Internal_Options *in)
1676{
1677  in->kind = H_GET_8 (abfd, ex->kind);
1678  in->size = H_GET_8 (abfd, ex->size);
1679  in->section = H_GET_16 (abfd, ex->section);
1680  in->info = H_GET_32 (abfd, ex->info);
1681}
1682
1683/* Swap out an options header.  */
1684
1685void
1686bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
1687			       Elf_External_Options *ex)
1688{
1689  H_PUT_8 (abfd, in->kind, ex->kind);
1690  H_PUT_8 (abfd, in->size, ex->size);
1691  H_PUT_16 (abfd, in->section, ex->section);
1692  H_PUT_32 (abfd, in->info, ex->info);
1693}
1694
1695/* This function is called via qsort() to sort the dynamic relocation
1696   entries by increasing r_symndx value.  */
1697
1698static int
1699sort_dynamic_relocs (const void *arg1, const void *arg2)
1700{
1701  Elf_Internal_Rela int_reloc1;
1702  Elf_Internal_Rela int_reloc2;
1703  int diff;
1704
1705  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
1706  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
1707
1708  diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
1709  if (diff != 0)
1710    return diff;
1711
1712  if (int_reloc1.r_offset < int_reloc2.r_offset)
1713    return -1;
1714  if (int_reloc1.r_offset > int_reloc2.r_offset)
1715    return 1;
1716  return 0;
1717}
1718
1719/* Like sort_dynamic_relocs, but used for elf64 relocations.  */
1720
1721static int
1722sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
1723			const void *arg2 ATTRIBUTE_UNUSED)
1724{
1725#ifdef BFD64
1726  Elf_Internal_Rela int_reloc1[3];
1727  Elf_Internal_Rela int_reloc2[3];
1728
1729  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1730    (reldyn_sorting_bfd, arg1, int_reloc1);
1731  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1732    (reldyn_sorting_bfd, arg2, int_reloc2);
1733
1734  if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
1735    return -1;
1736  if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
1737    return 1;
1738
1739  if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
1740    return -1;
1741  if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
1742    return 1;
1743  return 0;
1744#else
1745  abort ();
1746#endif
1747}
1748
1749
1750/* This routine is used to write out ECOFF debugging external symbol
1751   information.  It is called via mips_elf_link_hash_traverse.  The
1752   ECOFF external symbol information must match the ELF external
1753   symbol information.  Unfortunately, at this point we don't know
1754   whether a symbol is required by reloc information, so the two
1755   tables may wind up being different.  We must sort out the external
1756   symbol information before we can set the final size of the .mdebug
1757   section, and we must set the size of the .mdebug section before we
1758   can relocate any sections, and we can't know which symbols are
1759   required by relocation until we relocate the sections.
1760   Fortunately, it is relatively unlikely that any symbol will be
1761   stripped but required by a reloc.  In particular, it can not happen
1762   when generating a final executable.  */
1763
1764static bfd_boolean
1765mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
1766{
1767  struct extsym_info *einfo = data;
1768  bfd_boolean strip;
1769  asection *sec, *output_section;
1770
1771  if (h->root.root.type == bfd_link_hash_warning)
1772    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1773
1774  if (h->root.indx == -2)
1775    strip = FALSE;
1776  else if ((h->root.def_dynamic
1777	    || h->root.ref_dynamic
1778	    || h->root.type == bfd_link_hash_new)
1779	   && !h->root.def_regular
1780	   && !h->root.ref_regular)
1781    strip = TRUE;
1782  else if (einfo->info->strip == strip_all
1783	   || (einfo->info->strip == strip_some
1784	       && bfd_hash_lookup (einfo->info->keep_hash,
1785				   h->root.root.root.string,
1786				   FALSE, FALSE) == NULL))
1787    strip = TRUE;
1788  else
1789    strip = FALSE;
1790
1791  if (strip)
1792    return TRUE;
1793
1794  if (h->esym.ifd == -2)
1795    {
1796      h->esym.jmptbl = 0;
1797      h->esym.cobol_main = 0;
1798      h->esym.weakext = 0;
1799      h->esym.reserved = 0;
1800      h->esym.ifd = ifdNil;
1801      h->esym.asym.value = 0;
1802      h->esym.asym.st = stGlobal;
1803
1804      if (h->root.root.type == bfd_link_hash_undefined
1805	  || h->root.root.type == bfd_link_hash_undefweak)
1806	{
1807	  const char *name;
1808
1809	  /* Use undefined class.  Also, set class and type for some
1810             special symbols.  */
1811	  name = h->root.root.root.string;
1812	  if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
1813	      || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
1814	    {
1815	      h->esym.asym.sc = scData;
1816	      h->esym.asym.st = stLabel;
1817	      h->esym.asym.value = 0;
1818	    }
1819	  else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
1820	    {
1821	      h->esym.asym.sc = scAbs;
1822	      h->esym.asym.st = stLabel;
1823	      h->esym.asym.value =
1824		mips_elf_hash_table (einfo->info)->procedure_count;
1825	    }
1826	  else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
1827	    {
1828	      h->esym.asym.sc = scAbs;
1829	      h->esym.asym.st = stLabel;
1830	      h->esym.asym.value = elf_gp (einfo->abfd);
1831	    }
1832	  else
1833	    h->esym.asym.sc = scUndefined;
1834	}
1835      else if (h->root.root.type != bfd_link_hash_defined
1836	  && h->root.root.type != bfd_link_hash_defweak)
1837	h->esym.asym.sc = scAbs;
1838      else
1839	{
1840	  const char *name;
1841
1842	  sec = h->root.root.u.def.section;
1843	  output_section = sec->output_section;
1844
1845	  /* When making a shared library and symbol h is the one from
1846	     the another shared library, OUTPUT_SECTION may be null.  */
1847	  if (output_section == NULL)
1848	    h->esym.asym.sc = scUndefined;
1849	  else
1850	    {
1851	      name = bfd_section_name (output_section->owner, output_section);
1852
1853	      if (strcmp (name, ".text") == 0)
1854		h->esym.asym.sc = scText;
1855	      else if (strcmp (name, ".data") == 0)
1856		h->esym.asym.sc = scData;
1857	      else if (strcmp (name, ".sdata") == 0)
1858		h->esym.asym.sc = scSData;
1859	      else if (strcmp (name, ".rodata") == 0
1860		       || strcmp (name, ".rdata") == 0)
1861		h->esym.asym.sc = scRData;
1862	      else if (strcmp (name, ".bss") == 0)
1863		h->esym.asym.sc = scBss;
1864	      else if (strcmp (name, ".sbss") == 0)
1865		h->esym.asym.sc = scSBss;
1866	      else if (strcmp (name, ".init") == 0)
1867		h->esym.asym.sc = scInit;
1868	      else if (strcmp (name, ".fini") == 0)
1869		h->esym.asym.sc = scFini;
1870	      else
1871		h->esym.asym.sc = scAbs;
1872	    }
1873	}
1874
1875      h->esym.asym.reserved = 0;
1876      h->esym.asym.index = indexNil;
1877    }
1878
1879  if (h->root.root.type == bfd_link_hash_common)
1880    h->esym.asym.value = h->root.root.u.c.size;
1881  else if (h->root.root.type == bfd_link_hash_defined
1882	   || h->root.root.type == bfd_link_hash_defweak)
1883    {
1884      if (h->esym.asym.sc == scCommon)
1885	h->esym.asym.sc = scBss;
1886      else if (h->esym.asym.sc == scSCommon)
1887	h->esym.asym.sc = scSBss;
1888
1889      sec = h->root.root.u.def.section;
1890      output_section = sec->output_section;
1891      if (output_section != NULL)
1892	h->esym.asym.value = (h->root.root.u.def.value
1893			      + sec->output_offset
1894			      + output_section->vma);
1895      else
1896	h->esym.asym.value = 0;
1897    }
1898  else if (h->root.needs_plt)
1899    {
1900      struct mips_elf_link_hash_entry *hd = h;
1901      bfd_boolean no_fn_stub = h->no_fn_stub;
1902
1903      while (hd->root.root.type == bfd_link_hash_indirect)
1904	{
1905	  hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
1906	  no_fn_stub = no_fn_stub || hd->no_fn_stub;
1907	}
1908
1909      if (!no_fn_stub)
1910	{
1911	  /* Set type and value for a symbol with a function stub.  */
1912	  h->esym.asym.st = stProc;
1913	  sec = hd->root.root.u.def.section;
1914	  if (sec == NULL)
1915	    h->esym.asym.value = 0;
1916	  else
1917	    {
1918	      output_section = sec->output_section;
1919	      if (output_section != NULL)
1920		h->esym.asym.value = (hd->root.plt.offset
1921				      + sec->output_offset
1922				      + output_section->vma);
1923	      else
1924		h->esym.asym.value = 0;
1925	    }
1926	}
1927    }
1928
1929  if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1930				      h->root.root.root.string,
1931				      &h->esym))
1932    {
1933      einfo->failed = TRUE;
1934      return FALSE;
1935    }
1936
1937  return TRUE;
1938}
1939
1940/* A comparison routine used to sort .gptab entries.  */
1941
1942static int
1943gptab_compare (const void *p1, const void *p2)
1944{
1945  const Elf32_gptab *a1 = p1;
1946  const Elf32_gptab *a2 = p2;
1947
1948  return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
1949}
1950
1951/* Functions to manage the got entry hash table.  */
1952
1953/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
1954   hash number.  */
1955
1956static INLINE hashval_t
1957mips_elf_hash_bfd_vma (bfd_vma addr)
1958{
1959#ifdef BFD64
1960  return addr + (addr >> 32);
1961#else
1962  return addr;
1963#endif
1964}
1965
1966/* got_entries only match if they're identical, except for gotidx, so
1967   use all fields to compute the hash, and compare the appropriate
1968   union members.  */
1969
1970static hashval_t
1971mips_elf_got_entry_hash (const void *entry_)
1972{
1973  const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
1974
1975  return entry->symndx
1976    + ((entry->tls_type & GOT_TLS_LDM) << 17)
1977    + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
1978       : entry->abfd->id
1979         + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
1980	    : entry->d.h->root.root.root.hash));
1981}
1982
1983static int
1984mips_elf_got_entry_eq (const void *entry1, const void *entry2)
1985{
1986  const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
1987  const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
1988
1989  /* An LDM entry can only match another LDM entry.  */
1990  if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
1991    return 0;
1992
1993  return e1->abfd == e2->abfd && e1->symndx == e2->symndx
1994    && (! e1->abfd ? e1->d.address == e2->d.address
1995	: e1->symndx >= 0 ? e1->d.addend == e2->d.addend
1996	: e1->d.h == e2->d.h);
1997}
1998
1999/* multi_got_entries are still a match in the case of global objects,
2000   even if the input bfd in which they're referenced differs, so the
2001   hash computation and compare functions are adjusted
2002   accordingly.  */
2003
2004static hashval_t
2005mips_elf_multi_got_entry_hash (const void *entry_)
2006{
2007  const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2008
2009  return entry->symndx
2010    + (! entry->abfd
2011       ? mips_elf_hash_bfd_vma (entry->d.address)
2012       : entry->symndx >= 0
2013       ? ((entry->tls_type & GOT_TLS_LDM)
2014	  ? (GOT_TLS_LDM << 17)
2015	  : (entry->abfd->id
2016	     + mips_elf_hash_bfd_vma (entry->d.addend)))
2017       : entry->d.h->root.root.root.hash);
2018}
2019
2020static int
2021mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
2022{
2023  const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2024  const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2025
2026  /* Any two LDM entries match.  */
2027  if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2028    return 1;
2029
2030  /* Nothing else matches an LDM entry.  */
2031  if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2032    return 0;
2033
2034  return e1->symndx == e2->symndx
2035    && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2036	: e1->abfd == NULL || e2->abfd == NULL
2037	? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2038	: e1->d.h == e2->d.h);
2039}
2040
2041/* Return the dynamic relocation section.  If it doesn't exist, try to
2042   create a new it if CREATE_P, otherwise return NULL.  Also return NULL
2043   if creation fails.  */
2044
2045static asection *
2046mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2047{
2048  const char *dname;
2049  asection *sreloc;
2050  bfd *dynobj;
2051
2052  dname = MIPS_ELF_REL_DYN_NAME (info);
2053  dynobj = elf_hash_table (info)->dynobj;
2054  sreloc = bfd_get_section_by_name (dynobj, dname);
2055  if (sreloc == NULL && create_p)
2056    {
2057      sreloc = bfd_make_section_with_flags (dynobj, dname,
2058					    (SEC_ALLOC
2059					     | SEC_LOAD
2060					     | SEC_HAS_CONTENTS
2061					     | SEC_IN_MEMORY
2062					     | SEC_LINKER_CREATED
2063					     | SEC_READONLY));
2064      if (sreloc == NULL
2065	  || ! bfd_set_section_alignment (dynobj, sreloc,
2066					  MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2067	return NULL;
2068    }
2069  return sreloc;
2070}
2071
2072/* Returns the GOT section for ABFD.  */
2073
2074static asection *
2075mips_elf_got_section (bfd *abfd, bfd_boolean maybe_excluded)
2076{
2077  asection *sgot = bfd_get_section_by_name (abfd, ".got");
2078  if (sgot == NULL
2079      || (! maybe_excluded && (sgot->flags & SEC_EXCLUDE) != 0))
2080    return NULL;
2081  return sgot;
2082}
2083
2084/* Returns the GOT information associated with the link indicated by
2085   INFO.  If SGOTP is non-NULL, it is filled in with the GOT
2086   section.  */
2087
2088static struct mips_got_info *
2089mips_elf_got_info (bfd *abfd, asection **sgotp)
2090{
2091  asection *sgot;
2092  struct mips_got_info *g;
2093
2094  sgot = mips_elf_got_section (abfd, TRUE);
2095  BFD_ASSERT (sgot != NULL);
2096  BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
2097  g = mips_elf_section_data (sgot)->u.got_info;
2098  BFD_ASSERT (g != NULL);
2099
2100  if (sgotp)
2101    *sgotp = (sgot->flags & SEC_EXCLUDE) == 0 ? sgot : NULL;
2102
2103  return g;
2104}
2105
2106/* Count the number of relocations needed for a TLS GOT entry, with
2107   access types from TLS_TYPE, and symbol H (or a local symbol if H
2108   is NULL).  */
2109
2110static int
2111mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2112		     struct elf_link_hash_entry *h)
2113{
2114  int indx = 0;
2115  int ret = 0;
2116  bfd_boolean need_relocs = FALSE;
2117  bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2118
2119  if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2120      && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2121    indx = h->dynindx;
2122
2123  if ((info->shared || indx != 0)
2124      && (h == NULL
2125	  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2126	  || h->root.type != bfd_link_hash_undefweak))
2127    need_relocs = TRUE;
2128
2129  if (!need_relocs)
2130    return FALSE;
2131
2132  if (tls_type & GOT_TLS_GD)
2133    {
2134      ret++;
2135      if (indx != 0)
2136	ret++;
2137    }
2138
2139  if (tls_type & GOT_TLS_IE)
2140    ret++;
2141
2142  if ((tls_type & GOT_TLS_LDM) && info->shared)
2143    ret++;
2144
2145  return ret;
2146}
2147
2148/* Count the number of TLS relocations required for the GOT entry in
2149   ARG1, if it describes a local symbol.  */
2150
2151static int
2152mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2153{
2154  struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2155  struct mips_elf_count_tls_arg *arg = arg2;
2156
2157  if (entry->abfd != NULL && entry->symndx != -1)
2158    arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2159
2160  return 1;
2161}
2162
2163/* Count the number of TLS GOT entries required for the global (or
2164   forced-local) symbol in ARG1.  */
2165
2166static int
2167mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2168{
2169  struct mips_elf_link_hash_entry *hm
2170    = (struct mips_elf_link_hash_entry *) arg1;
2171  struct mips_elf_count_tls_arg *arg = arg2;
2172
2173  if (hm->tls_type & GOT_TLS_GD)
2174    arg->needed += 2;
2175  if (hm->tls_type & GOT_TLS_IE)
2176    arg->needed += 1;
2177
2178  return 1;
2179}
2180
2181/* Count the number of TLS relocations required for the global (or
2182   forced-local) symbol in ARG1.  */
2183
2184static int
2185mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2186{
2187  struct mips_elf_link_hash_entry *hm
2188    = (struct mips_elf_link_hash_entry *) arg1;
2189  struct mips_elf_count_tls_arg *arg = arg2;
2190
2191  arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2192
2193  return 1;
2194}
2195
2196/* Output a simple dynamic relocation into SRELOC.  */
2197
2198static void
2199mips_elf_output_dynamic_relocation (bfd *output_bfd,
2200				    asection *sreloc,
2201				    unsigned long indx,
2202				    int r_type,
2203				    bfd_vma offset)
2204{
2205  Elf_Internal_Rela rel[3];
2206
2207  memset (rel, 0, sizeof (rel));
2208
2209  rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2210  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2211
2212  if (ABI_64_P (output_bfd))
2213    {
2214      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2215	(output_bfd, &rel[0],
2216	 (sreloc->contents
2217	  + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
2218    }
2219  else
2220    bfd_elf32_swap_reloc_out
2221      (output_bfd, &rel[0],
2222       (sreloc->contents
2223	+ sreloc->reloc_count * sizeof (Elf32_External_Rel)));
2224  ++sreloc->reloc_count;
2225}
2226
2227/* Initialize a set of TLS GOT entries for one symbol.  */
2228
2229static void
2230mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2231			       unsigned char *tls_type_p,
2232			       struct bfd_link_info *info,
2233			       struct mips_elf_link_hash_entry *h,
2234			       bfd_vma value)
2235{
2236  int indx;
2237  asection *sreloc, *sgot;
2238  bfd_vma offset, offset2;
2239  bfd *dynobj;
2240  bfd_boolean need_relocs = FALSE;
2241
2242  dynobj = elf_hash_table (info)->dynobj;
2243  sgot = mips_elf_got_section (dynobj, FALSE);
2244
2245  indx = 0;
2246  if (h != NULL)
2247    {
2248      bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2249
2250      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2251	  && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2252	indx = h->root.dynindx;
2253    }
2254
2255  if (*tls_type_p & GOT_TLS_DONE)
2256    return;
2257
2258  if ((info->shared || indx != 0)
2259      && (h == NULL
2260	  || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2261	  || h->root.type != bfd_link_hash_undefweak))
2262    need_relocs = TRUE;
2263
2264  /* MINUS_ONE means the symbol is not defined in this object.  It may not
2265     be defined at all; assume that the value doesn't matter in that
2266     case.  Otherwise complain if we would use the value.  */
2267  BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2268	      || h->root.root.type == bfd_link_hash_undefweak);
2269
2270  /* Emit necessary relocations.  */
2271  sreloc = mips_elf_rel_dyn_section (info, FALSE);
2272
2273  /* General Dynamic.  */
2274  if (*tls_type_p & GOT_TLS_GD)
2275    {
2276      offset = got_offset;
2277      offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2278
2279      if (need_relocs)
2280	{
2281	  mips_elf_output_dynamic_relocation
2282	    (abfd, sreloc, indx,
2283	     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2284	     sgot->output_offset + sgot->output_section->vma + offset);
2285
2286	  if (indx)
2287	    mips_elf_output_dynamic_relocation
2288	      (abfd, sreloc, indx,
2289	       ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2290	       sgot->output_offset + sgot->output_section->vma + offset2);
2291	  else
2292	    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2293			       sgot->contents + offset2);
2294	}
2295      else
2296	{
2297	  MIPS_ELF_PUT_WORD (abfd, 1,
2298			     sgot->contents + offset);
2299	  MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2300			     sgot->contents + offset2);
2301	}
2302
2303      got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2304    }
2305
2306  /* Initial Exec model.  */
2307  if (*tls_type_p & GOT_TLS_IE)
2308    {
2309      offset = got_offset;
2310
2311      if (need_relocs)
2312	{
2313	  if (indx == 0)
2314	    MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2315			       sgot->contents + offset);
2316	  else
2317	    MIPS_ELF_PUT_WORD (abfd, 0,
2318			       sgot->contents + offset);
2319
2320	  mips_elf_output_dynamic_relocation
2321	    (abfd, sreloc, indx,
2322	     ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2323	     sgot->output_offset + sgot->output_section->vma + offset);
2324	}
2325      else
2326	MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2327			   sgot->contents + offset);
2328    }
2329
2330  if (*tls_type_p & GOT_TLS_LDM)
2331    {
2332      /* The initial offset is zero, and the LD offsets will include the
2333	 bias by DTP_OFFSET.  */
2334      MIPS_ELF_PUT_WORD (abfd, 0,
2335			 sgot->contents + got_offset
2336			 + MIPS_ELF_GOT_SIZE (abfd));
2337
2338      if (!info->shared)
2339	MIPS_ELF_PUT_WORD (abfd, 1,
2340			   sgot->contents + got_offset);
2341      else
2342	mips_elf_output_dynamic_relocation
2343	  (abfd, sreloc, indx,
2344	   ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2345	   sgot->output_offset + sgot->output_section->vma + got_offset);
2346    }
2347
2348  *tls_type_p |= GOT_TLS_DONE;
2349}
2350
2351/* Return the GOT index to use for a relocation of type R_TYPE against
2352   a symbol accessed using TLS_TYPE models.  The GOT entries for this
2353   symbol in this GOT start at GOT_INDEX.  This function initializes the
2354   GOT entries and corresponding relocations.  */
2355
2356static bfd_vma
2357mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2358		    int r_type, struct bfd_link_info *info,
2359		    struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2360{
2361  BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2362	      || r_type == R_MIPS_TLS_LDM);
2363
2364  mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2365
2366  if (r_type == R_MIPS_TLS_GOTTPREL)
2367    {
2368      BFD_ASSERT (*tls_type & GOT_TLS_IE);
2369      if (*tls_type & GOT_TLS_GD)
2370	return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2371      else
2372	return got_index;
2373    }
2374
2375  if (r_type == R_MIPS_TLS_GD)
2376    {
2377      BFD_ASSERT (*tls_type & GOT_TLS_GD);
2378      return got_index;
2379    }
2380
2381  if (r_type == R_MIPS_TLS_LDM)
2382    {
2383      BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2384      return got_index;
2385    }
2386
2387  return got_index;
2388}
2389
2390/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
2391   for global symbol H.  .got.plt comes before the GOT, so the offset
2392   will be negative.  */
2393
2394static bfd_vma
2395mips_elf_gotplt_index (struct bfd_link_info *info,
2396		       struct elf_link_hash_entry *h)
2397{
2398  bfd_vma plt_index, got_address, got_value;
2399  struct mips_elf_link_hash_table *htab;
2400
2401  htab = mips_elf_hash_table (info);
2402  BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
2403
2404  /* Calculate the index of the symbol's PLT entry.  */
2405  plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
2406
2407  /* Calculate the address of the associated .got.plt entry.  */
2408  got_address = (htab->sgotplt->output_section->vma
2409		 + htab->sgotplt->output_offset
2410		 + plt_index * 4);
2411
2412  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
2413  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
2414	       + htab->root.hgot->root.u.def.section->output_offset
2415	       + htab->root.hgot->root.u.def.value);
2416
2417  return got_address - got_value;
2418}
2419
2420/* Return the GOT offset for address VALUE.   If there is not yet a GOT
2421   entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
2422   create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
2423   offset can be found.  */
2424
2425static bfd_vma
2426mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2427			  bfd_vma value, unsigned long r_symndx,
2428			  struct mips_elf_link_hash_entry *h, int r_type)
2429{
2430  asection *sgot;
2431  struct mips_got_info *g;
2432  struct mips_got_entry *entry;
2433
2434  g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2435
2436  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2437					   value, r_symndx, h, r_type);
2438  if (!entry)
2439    return MINUS_ONE;
2440
2441  if (TLS_RELOC_P (r_type))
2442    {
2443      if (entry->symndx == -1 && g->next == NULL)
2444	/* A type (3) entry in the single-GOT case.  We use the symbol's
2445	   hash table entry to track the index.  */
2446	return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
2447				   r_type, info, h, value);
2448      else
2449	return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
2450				   r_type, info, h, value);
2451    }
2452  else
2453    return entry->gotidx;
2454}
2455
2456/* Returns the GOT index for the global symbol indicated by H.  */
2457
2458static bfd_vma
2459mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
2460			   int r_type, struct bfd_link_info *info)
2461{
2462  bfd_vma index;
2463  asection *sgot;
2464  struct mips_got_info *g, *gg;
2465  long global_got_dynindx = 0;
2466
2467  gg = g = mips_elf_got_info (abfd, &sgot);
2468  if (g->bfd2got && ibfd)
2469    {
2470      struct mips_got_entry e, *p;
2471
2472      BFD_ASSERT (h->dynindx >= 0);
2473
2474      g = mips_elf_got_for_ibfd (g, ibfd);
2475      if (g->next != gg || TLS_RELOC_P (r_type))
2476	{
2477	  e.abfd = ibfd;
2478	  e.symndx = -1;
2479	  e.d.h = (struct mips_elf_link_hash_entry *)h;
2480	  e.tls_type = 0;
2481
2482	  p = htab_find (g->got_entries, &e);
2483
2484	  BFD_ASSERT (p->gotidx > 0);
2485
2486	  if (TLS_RELOC_P (r_type))
2487	    {
2488	      bfd_vma value = MINUS_ONE;
2489	      if ((h->root.type == bfd_link_hash_defined
2490		   || h->root.type == bfd_link_hash_defweak)
2491		  && h->root.u.def.section->output_section)
2492		value = (h->root.u.def.value
2493			 + h->root.u.def.section->output_offset
2494			 + h->root.u.def.section->output_section->vma);
2495
2496	      return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
2497					 info, e.d.h, value);
2498	    }
2499	  else
2500	    return p->gotidx;
2501	}
2502    }
2503
2504  if (gg->global_gotsym != NULL)
2505    global_got_dynindx = gg->global_gotsym->dynindx;
2506
2507  if (TLS_RELOC_P (r_type))
2508    {
2509      struct mips_elf_link_hash_entry *hm
2510	= (struct mips_elf_link_hash_entry *) h;
2511      bfd_vma value = MINUS_ONE;
2512
2513      if ((h->root.type == bfd_link_hash_defined
2514	   || h->root.type == bfd_link_hash_defweak)
2515	  && h->root.u.def.section->output_section)
2516	value = (h->root.u.def.value
2517		 + h->root.u.def.section->output_offset
2518		 + h->root.u.def.section->output_section->vma);
2519
2520      index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
2521				  r_type, info, hm, value);
2522    }
2523  else
2524    {
2525      /* Once we determine the global GOT entry with the lowest dynamic
2526	 symbol table index, we must put all dynamic symbols with greater
2527	 indices into the GOT.  That makes it easy to calculate the GOT
2528	 offset.  */
2529      BFD_ASSERT (h->dynindx >= global_got_dynindx);
2530      index = ((h->dynindx - global_got_dynindx + g->local_gotno)
2531	       * MIPS_ELF_GOT_SIZE (abfd));
2532    }
2533  BFD_ASSERT (index < sgot->size);
2534
2535  return index;
2536}
2537
2538/* Find a GOT page entry that points to within 32KB of VALUE.  These
2539   entries are supposed to be placed at small offsets in the GOT, i.e.,
2540   within 32KB of GP.  Return the index of the GOT entry, or -1 if no
2541   entry could be created.  If OFFSETP is nonnull, use it to return the
2542   offset of the GOT entry from VALUE.  */
2543
2544static bfd_vma
2545mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2546		   bfd_vma value, bfd_vma *offsetp)
2547{
2548  asection *sgot;
2549  struct mips_got_info *g;
2550  bfd_vma page, index;
2551  struct mips_got_entry *entry;
2552
2553  g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2554
2555  page = (value + 0x8000) & ~(bfd_vma) 0xffff;
2556  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2557					   page, 0, NULL, R_MIPS_GOT_PAGE);
2558
2559  if (!entry)
2560    return MINUS_ONE;
2561
2562  index = entry->gotidx;
2563
2564  if (offsetp)
2565    *offsetp = value - entry->d.address;
2566
2567  return index;
2568}
2569
2570/* Find a local GOT entry for an R_MIPS_GOT16 relocation against VALUE.
2571   EXTERNAL is true if the relocation was against a global symbol
2572   that has been forced local.  */
2573
2574static bfd_vma
2575mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2576		      bfd_vma value, bfd_boolean external)
2577{
2578  asection *sgot;
2579  struct mips_got_info *g;
2580  struct mips_got_entry *entry;
2581
2582  /* GOT16 relocations against local symbols are followed by a LO16
2583     relocation; those against global symbols are not.  Thus if the
2584     symbol was originally local, the GOT16 relocation should load the
2585     equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
2586  if (! external)
2587    value = mips_elf_high (value) << 16;
2588
2589  g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2590
2591  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2592					   value, 0, NULL, R_MIPS_GOT16);
2593  if (entry)
2594    return entry->gotidx;
2595  else
2596    return MINUS_ONE;
2597}
2598
2599/* Returns the offset for the entry at the INDEXth position
2600   in the GOT.  */
2601
2602static bfd_vma
2603mips_elf_got_offset_from_index (bfd *dynobj, bfd *output_bfd,
2604				bfd *input_bfd, bfd_vma index)
2605{
2606  asection *sgot;
2607  bfd_vma gp;
2608  struct mips_got_info *g;
2609
2610  g = mips_elf_got_info (dynobj, &sgot);
2611  gp = _bfd_get_gp_value (output_bfd)
2612    + mips_elf_adjust_gp (output_bfd, g, input_bfd);
2613
2614  return sgot->output_section->vma + sgot->output_offset + index - gp;
2615}
2616
2617/* Create and return a local GOT entry for VALUE, which was calculated
2618   from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
2619   be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
2620   instead.  */
2621
2622static struct mips_got_entry *
2623mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
2624				 bfd *ibfd, struct mips_got_info *gg,
2625				 asection *sgot, bfd_vma value,
2626				 unsigned long r_symndx,
2627				 struct mips_elf_link_hash_entry *h,
2628				 int r_type)
2629{
2630  struct mips_got_entry entry, **loc;
2631  struct mips_got_info *g;
2632  struct mips_elf_link_hash_table *htab;
2633
2634  htab = mips_elf_hash_table (info);
2635
2636  entry.abfd = NULL;
2637  entry.symndx = -1;
2638  entry.d.address = value;
2639  entry.tls_type = 0;
2640
2641  g = mips_elf_got_for_ibfd (gg, ibfd);
2642  if (g == NULL)
2643    {
2644      g = mips_elf_got_for_ibfd (gg, abfd);
2645      BFD_ASSERT (g != NULL);
2646    }
2647
2648  /* We might have a symbol, H, if it has been forced local.  Use the
2649     global entry then.  It doesn't matter whether an entry is local
2650     or global for TLS, since the dynamic linker does not
2651     automatically relocate TLS GOT entries.  */
2652  BFD_ASSERT (h == NULL || h->root.forced_local);
2653  if (TLS_RELOC_P (r_type))
2654    {
2655      struct mips_got_entry *p;
2656
2657      entry.abfd = ibfd;
2658      if (r_type == R_MIPS_TLS_LDM)
2659	{
2660	  entry.tls_type = GOT_TLS_LDM;
2661	  entry.symndx = 0;
2662	  entry.d.addend = 0;
2663	}
2664      else if (h == NULL)
2665	{
2666	  entry.symndx = r_symndx;
2667	  entry.d.addend = 0;
2668	}
2669      else
2670	entry.d.h = h;
2671
2672      p = (struct mips_got_entry *)
2673	htab_find (g->got_entries, &entry);
2674
2675      BFD_ASSERT (p);
2676      return p;
2677    }
2678
2679  loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2680						   INSERT);
2681  if (*loc)
2682    return *loc;
2683
2684  entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
2685  entry.tls_type = 0;
2686
2687  *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2688
2689  if (! *loc)
2690    return NULL;
2691
2692  memcpy (*loc, &entry, sizeof entry);
2693
2694  if (g->assigned_gotno >= g->local_gotno)
2695    {
2696      (*loc)->gotidx = -1;
2697      /* We didn't allocate enough space in the GOT.  */
2698      (*_bfd_error_handler)
2699	(_("not enough GOT space for local GOT entries"));
2700      bfd_set_error (bfd_error_bad_value);
2701      return NULL;
2702    }
2703
2704  MIPS_ELF_PUT_WORD (abfd, value,
2705		     (sgot->contents + entry.gotidx));
2706
2707  /* These GOT entries need a dynamic relocation on VxWorks.  */
2708  if (htab->is_vxworks)
2709    {
2710      Elf_Internal_Rela outrel;
2711      asection *s;
2712      bfd_byte *loc;
2713      bfd_vma got_address;
2714
2715      s = mips_elf_rel_dyn_section (info, FALSE);
2716      got_address = (sgot->output_section->vma
2717		     + sgot->output_offset
2718		     + entry.gotidx);
2719
2720      loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
2721      outrel.r_offset = got_address;
2722      outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
2723      outrel.r_addend = value;
2724      bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
2725    }
2726
2727  return *loc;
2728}
2729
2730/* Sort the dynamic symbol table so that symbols that need GOT entries
2731   appear towards the end.  This reduces the amount of GOT space
2732   required.  MAX_LOCAL is used to set the number of local symbols
2733   known to be in the dynamic symbol table.  During
2734   _bfd_mips_elf_size_dynamic_sections, this value is 1.  Afterward, the
2735   section symbols are added and the count is higher.  */
2736
2737static bfd_boolean
2738mips_elf_sort_hash_table (struct bfd_link_info *info, unsigned long max_local)
2739{
2740  struct mips_elf_hash_sort_data hsd;
2741  struct mips_got_info *g;
2742  bfd *dynobj;
2743
2744  dynobj = elf_hash_table (info)->dynobj;
2745
2746  g = mips_elf_got_info (dynobj, NULL);
2747
2748  hsd.low = NULL;
2749  hsd.max_unref_got_dynindx =
2750  hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
2751    /* In the multi-got case, assigned_gotno of the master got_info
2752       indicate the number of entries that aren't referenced in the
2753       primary GOT, but that must have entries because there are
2754       dynamic relocations that reference it.  Since they aren't
2755       referenced, we move them to the end of the GOT, so that they
2756       don't prevent other entries that are referenced from getting
2757       too large offsets.  */
2758    - (g->next ? g->assigned_gotno : 0);
2759  hsd.max_non_got_dynindx = max_local;
2760  mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
2761				elf_hash_table (info)),
2762			       mips_elf_sort_hash_table_f,
2763			       &hsd);
2764
2765  /* There should have been enough room in the symbol table to
2766     accommodate both the GOT and non-GOT symbols.  */
2767  BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
2768  BFD_ASSERT ((unsigned long)hsd.max_unref_got_dynindx
2769	      <= elf_hash_table (info)->dynsymcount);
2770
2771  /* Now we know which dynamic symbol has the lowest dynamic symbol
2772     table index in the GOT.  */
2773  g->global_gotsym = hsd.low;
2774
2775  return TRUE;
2776}
2777
2778/* If H needs a GOT entry, assign it the highest available dynamic
2779   index.  Otherwise, assign it the lowest available dynamic
2780   index.  */
2781
2782static bfd_boolean
2783mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
2784{
2785  struct mips_elf_hash_sort_data *hsd = data;
2786
2787  if (h->root.root.type == bfd_link_hash_warning)
2788    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2789
2790  /* Symbols without dynamic symbol table entries aren't interesting
2791     at all.  */
2792  if (h->root.dynindx == -1)
2793    return TRUE;
2794
2795  /* Global symbols that need GOT entries that are not explicitly
2796     referenced are marked with got offset 2.  Those that are
2797     referenced get a 1, and those that don't need GOT entries get
2798     -1.  */
2799  if (h->root.got.offset == 2)
2800    {
2801      BFD_ASSERT (h->tls_type == GOT_NORMAL);
2802
2803      if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
2804	hsd->low = (struct elf_link_hash_entry *) h;
2805      h->root.dynindx = hsd->max_unref_got_dynindx++;
2806    }
2807  else if (h->root.got.offset != 1)
2808    h->root.dynindx = hsd->max_non_got_dynindx++;
2809  else
2810    {
2811      BFD_ASSERT (h->tls_type == GOT_NORMAL);
2812
2813      h->root.dynindx = --hsd->min_got_dynindx;
2814      hsd->low = (struct elf_link_hash_entry *) h;
2815    }
2816
2817  return TRUE;
2818}
2819
2820/* If H is a symbol that needs a global GOT entry, but has a dynamic
2821   symbol table index lower than any we've seen to date, record it for
2822   posterity.  */
2823
2824static bfd_boolean
2825mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
2826				   bfd *abfd, struct bfd_link_info *info,
2827				   struct mips_got_info *g,
2828				   unsigned char tls_flag)
2829{
2830  struct mips_got_entry entry, **loc;
2831
2832  /* A global symbol in the GOT must also be in the dynamic symbol
2833     table.  */
2834  if (h->dynindx == -1)
2835    {
2836      switch (ELF_ST_VISIBILITY (h->other))
2837	{
2838	case STV_INTERNAL:
2839	case STV_HIDDEN:
2840	  _bfd_mips_elf_hide_symbol (info, h, TRUE);
2841	  break;
2842	}
2843      if (!bfd_elf_link_record_dynamic_symbol (info, h))
2844	return FALSE;
2845    }
2846
2847  /* Make sure we have a GOT to put this entry into.  */
2848  BFD_ASSERT (g != NULL);
2849
2850  entry.abfd = abfd;
2851  entry.symndx = -1;
2852  entry.d.h = (struct mips_elf_link_hash_entry *) h;
2853  entry.tls_type = 0;
2854
2855  loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2856						   INSERT);
2857
2858  /* If we've already marked this entry as needing GOT space, we don't
2859     need to do it again.  */
2860  if (*loc)
2861    {
2862      (*loc)->tls_type |= tls_flag;
2863      return TRUE;
2864    }
2865
2866  *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2867
2868  if (! *loc)
2869    return FALSE;
2870
2871  entry.gotidx = -1;
2872  entry.tls_type = tls_flag;
2873
2874  memcpy (*loc, &entry, sizeof entry);
2875
2876  if (h->got.offset != MINUS_ONE)
2877    return TRUE;
2878
2879  /* By setting this to a value other than -1, we are indicating that
2880     there needs to be a GOT entry for H.  Avoid using zero, as the
2881     generic ELF copy_indirect_symbol tests for <= 0.  */
2882  if (tls_flag == 0)
2883    h->got.offset = 1;
2884
2885  return TRUE;
2886}
2887
2888/* Reserve space in G for a GOT entry containing the value of symbol
2889   SYMNDX in input bfd ABDF, plus ADDEND.  */
2890
2891static bfd_boolean
2892mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
2893				  struct mips_got_info *g,
2894				  unsigned char tls_flag)
2895{
2896  struct mips_got_entry entry, **loc;
2897
2898  entry.abfd = abfd;
2899  entry.symndx = symndx;
2900  entry.d.addend = addend;
2901  entry.tls_type = tls_flag;
2902  loc = (struct mips_got_entry **)
2903    htab_find_slot (g->got_entries, &entry, INSERT);
2904
2905  if (*loc)
2906    {
2907      if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
2908	{
2909	  g->tls_gotno += 2;
2910	  (*loc)->tls_type |= tls_flag;
2911	}
2912      else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
2913	{
2914	  g->tls_gotno += 1;
2915	  (*loc)->tls_type |= tls_flag;
2916	}
2917      return TRUE;
2918    }
2919
2920  if (tls_flag != 0)
2921    {
2922      entry.gotidx = -1;
2923      entry.tls_type = tls_flag;
2924      if (tls_flag == GOT_TLS_IE)
2925	g->tls_gotno += 1;
2926      else if (tls_flag == GOT_TLS_GD)
2927	g->tls_gotno += 2;
2928      else if (g->tls_ldm_offset == MINUS_ONE)
2929	{
2930	  g->tls_ldm_offset = MINUS_TWO;
2931	  g->tls_gotno += 2;
2932	}
2933    }
2934  else
2935    {
2936      entry.gotidx = g->local_gotno++;
2937      entry.tls_type = 0;
2938    }
2939
2940  *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2941
2942  if (! *loc)
2943    return FALSE;
2944
2945  memcpy (*loc, &entry, sizeof entry);
2946
2947  return TRUE;
2948}
2949
2950/* Compute the hash value of the bfd in a bfd2got hash entry.  */
2951
2952static hashval_t
2953mips_elf_bfd2got_entry_hash (const void *entry_)
2954{
2955  const struct mips_elf_bfd2got_hash *entry
2956    = (struct mips_elf_bfd2got_hash *)entry_;
2957
2958  return entry->bfd->id;
2959}
2960
2961/* Check whether two hash entries have the same bfd.  */
2962
2963static int
2964mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
2965{
2966  const struct mips_elf_bfd2got_hash *e1
2967    = (const struct mips_elf_bfd2got_hash *)entry1;
2968  const struct mips_elf_bfd2got_hash *e2
2969    = (const struct mips_elf_bfd2got_hash *)entry2;
2970
2971  return e1->bfd == e2->bfd;
2972}
2973
2974/* In a multi-got link, determine the GOT to be used for IBFD.  G must
2975   be the master GOT data.  */
2976
2977static struct mips_got_info *
2978mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
2979{
2980  struct mips_elf_bfd2got_hash e, *p;
2981
2982  if (! g->bfd2got)
2983    return g;
2984
2985  e.bfd = ibfd;
2986  p = htab_find (g->bfd2got, &e);
2987  return p ? p->g : NULL;
2988}
2989
2990/* Create one separate got for each bfd that has entries in the global
2991   got, such that we can tell how many local and global entries each
2992   bfd requires.  */
2993
2994static int
2995mips_elf_make_got_per_bfd (void **entryp, void *p)
2996{
2997  struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
2998  struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
2999  htab_t bfd2got = arg->bfd2got;
3000  struct mips_got_info *g;
3001  struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
3002  void **bfdgotp;
3003
3004  /* Find the got_info for this GOT entry's input bfd.  Create one if
3005     none exists.  */
3006  bfdgot_entry.bfd = entry->abfd;
3007  bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
3008  bfdgot = (struct mips_elf_bfd2got_hash *)*bfdgotp;
3009
3010  if (bfdgot != NULL)
3011    g = bfdgot->g;
3012  else
3013    {
3014      bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
3015	(arg->obfd, sizeof (struct mips_elf_bfd2got_hash));
3016
3017      if (bfdgot == NULL)
3018	{
3019	  arg->obfd = 0;
3020	  return 0;
3021	}
3022
3023      *bfdgotp = bfdgot;
3024
3025      bfdgot->bfd = entry->abfd;
3026      bfdgot->g = g = (struct mips_got_info *)
3027	bfd_alloc (arg->obfd, sizeof (struct mips_got_info));
3028      if (g == NULL)
3029	{
3030	  arg->obfd = 0;
3031	  return 0;
3032	}
3033
3034      g->global_gotsym = NULL;
3035      g->global_gotno = 0;
3036      g->local_gotno = 0;
3037      g->assigned_gotno = -1;
3038      g->tls_gotno = 0;
3039      g->tls_assigned_gotno = 0;
3040      g->tls_ldm_offset = MINUS_ONE;
3041      g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3042					mips_elf_multi_got_entry_eq, NULL);
3043      if (g->got_entries == NULL)
3044	{
3045	  arg->obfd = 0;
3046	  return 0;
3047	}
3048
3049      g->bfd2got = NULL;
3050      g->next = NULL;
3051    }
3052
3053  /* Insert the GOT entry in the bfd's got entry hash table.  */
3054  entryp = htab_find_slot (g->got_entries, entry, INSERT);
3055  if (*entryp != NULL)
3056    return 1;
3057
3058  *entryp = entry;
3059
3060  if (entry->tls_type)
3061    {
3062      if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3063	g->tls_gotno += 2;
3064      if (entry->tls_type & GOT_TLS_IE)
3065	g->tls_gotno += 1;
3066    }
3067  else if (entry->symndx >= 0 || entry->d.h->forced_local)
3068    ++g->local_gotno;
3069  else
3070    ++g->global_gotno;
3071
3072  return 1;
3073}
3074
3075/* Attempt to merge gots of different input bfds.  Try to use as much
3076   as possible of the primary got, since it doesn't require explicit
3077   dynamic relocations, but don't use bfds that would reference global
3078   symbols out of the addressable range.  Failing the primary got,
3079   attempt to merge with the current got, or finish the current got
3080   and then make make the new got current.  */
3081
3082static int
3083mips_elf_merge_gots (void **bfd2got_, void *p)
3084{
3085  struct mips_elf_bfd2got_hash *bfd2got
3086    = (struct mips_elf_bfd2got_hash *)*bfd2got_;
3087  struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3088  unsigned int lcount = bfd2got->g->local_gotno;
3089  unsigned int gcount = bfd2got->g->global_gotno;
3090  unsigned int tcount = bfd2got->g->tls_gotno;
3091  unsigned int maxcnt = arg->max_count;
3092  bfd_boolean too_many_for_tls = FALSE;
3093
3094  /* We place TLS GOT entries after both locals and globals.  The globals
3095     for the primary GOT may overflow the normal GOT size limit, so be
3096     sure not to merge a GOT which requires TLS with the primary GOT in that
3097     case.  This doesn't affect non-primary GOTs.  */
3098  if (tcount > 0)
3099    {
3100      unsigned int primary_total = lcount + tcount + arg->global_count;
3101      if (primary_total > maxcnt)
3102	too_many_for_tls = TRUE;
3103    }
3104
3105  /* If we don't have a primary GOT and this is not too big, use it as
3106     a starting point for the primary GOT.  */
3107  if (! arg->primary && lcount + gcount + tcount <= maxcnt
3108      && ! too_many_for_tls)
3109    {
3110      arg->primary = bfd2got->g;
3111      arg->primary_count = lcount + gcount;
3112    }
3113  /* If it looks like we can merge this bfd's entries with those of
3114     the primary, merge them.  The heuristics is conservative, but we
3115     don't have to squeeze it too hard.  */
3116  else if (arg->primary && ! too_many_for_tls
3117	   && (arg->primary_count + lcount + gcount + tcount) <= maxcnt)
3118    {
3119      struct mips_got_info *g = bfd2got->g;
3120      int old_lcount = arg->primary->local_gotno;
3121      int old_gcount = arg->primary->global_gotno;
3122      int old_tcount = arg->primary->tls_gotno;
3123
3124      bfd2got->g = arg->primary;
3125
3126      htab_traverse (g->got_entries,
3127		     mips_elf_make_got_per_bfd,
3128		     arg);
3129      if (arg->obfd == NULL)
3130	return 0;
3131
3132      htab_delete (g->got_entries);
3133      /* We don't have to worry about releasing memory of the actual
3134	 got entries, since they're all in the master got_entries hash
3135	 table anyway.  */
3136
3137      BFD_ASSERT (old_lcount + lcount >= arg->primary->local_gotno);
3138      BFD_ASSERT (old_gcount + gcount >= arg->primary->global_gotno);
3139      BFD_ASSERT (old_tcount + tcount >= arg->primary->tls_gotno);
3140
3141      arg->primary_count = arg->primary->local_gotno
3142	+ arg->primary->global_gotno + arg->primary->tls_gotno;
3143    }
3144  /* If we can merge with the last-created got, do it.  */
3145  else if (arg->current
3146	   && arg->current_count + lcount + gcount + tcount <= maxcnt)
3147    {
3148      struct mips_got_info *g = bfd2got->g;
3149      int old_lcount = arg->current->local_gotno;
3150      int old_gcount = arg->current->global_gotno;
3151      int old_tcount = arg->current->tls_gotno;
3152
3153      bfd2got->g = arg->current;
3154
3155      htab_traverse (g->got_entries,
3156		     mips_elf_make_got_per_bfd,
3157		     arg);
3158      if (arg->obfd == NULL)
3159	return 0;
3160
3161      htab_delete (g->got_entries);
3162
3163      BFD_ASSERT (old_lcount + lcount >= arg->current->local_gotno);
3164      BFD_ASSERT (old_gcount + gcount >= arg->current->global_gotno);
3165      BFD_ASSERT (old_tcount + tcount >= arg->current->tls_gotno);
3166
3167      arg->current_count = arg->current->local_gotno
3168	+ arg->current->global_gotno + arg->current->tls_gotno;
3169    }
3170  /* Well, we couldn't merge, so create a new GOT.  Don't check if it
3171     fits; if it turns out that it doesn't, we'll get relocation
3172     overflows anyway.  */
3173  else
3174    {
3175      bfd2got->g->next = arg->current;
3176      arg->current = bfd2got->g;
3177
3178      arg->current_count = lcount + gcount + 2 * tcount;
3179    }
3180
3181  return 1;
3182}
3183
3184/* Set the TLS GOT index for the GOT entry in ENTRYP.  ENTRYP's NEXT field
3185   is null iff there is just a single GOT.  */
3186
3187static int
3188mips_elf_initialize_tls_index (void **entryp, void *p)
3189{
3190  struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3191  struct mips_got_info *g = p;
3192  bfd_vma next_index;
3193  unsigned char tls_type;
3194
3195  /* We're only interested in TLS symbols.  */
3196  if (entry->tls_type == 0)
3197    return 1;
3198
3199  next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
3200
3201  if (entry->symndx == -1 && g->next == NULL)
3202    {
3203      /* A type (3) got entry in the single-GOT case.  We use the symbol's
3204	 hash table entry to track its index.  */
3205      if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
3206	return 1;
3207      entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
3208      entry->d.h->tls_got_offset = next_index;
3209      tls_type = entry->d.h->tls_type;
3210    }
3211  else
3212    {
3213      if (entry->tls_type & GOT_TLS_LDM)
3214	{
3215	  /* There are separate mips_got_entry objects for each input bfd
3216	     that requires an LDM entry.  Make sure that all LDM entries in
3217	     a GOT resolve to the same index.  */
3218	  if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
3219	    {
3220	      entry->gotidx = g->tls_ldm_offset;
3221	      return 1;
3222	    }
3223	  g->tls_ldm_offset = next_index;
3224	}
3225      entry->gotidx = next_index;
3226      tls_type = entry->tls_type;
3227    }
3228
3229  /* Account for the entries we've just allocated.  */
3230  if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3231    g->tls_assigned_gotno += 2;
3232  if (tls_type & GOT_TLS_IE)
3233    g->tls_assigned_gotno += 1;
3234
3235  return 1;
3236}
3237
3238/* If passed a NULL mips_got_info in the argument, set the marker used
3239   to tell whether a global symbol needs a got entry (in the primary
3240   got) to the given VALUE.
3241
3242   If passed a pointer G to a mips_got_info in the argument (it must
3243   not be the primary GOT), compute the offset from the beginning of
3244   the (primary) GOT section to the entry in G corresponding to the
3245   global symbol.  G's assigned_gotno must contain the index of the
3246   first available global GOT entry in G.  VALUE must contain the size
3247   of a GOT entry in bytes.  For each global GOT entry that requires a
3248   dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
3249   marked as not eligible for lazy resolution through a function
3250   stub.  */
3251static int
3252mips_elf_set_global_got_offset (void **entryp, void *p)
3253{
3254  struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3255  struct mips_elf_set_global_got_offset_arg *arg
3256    = (struct mips_elf_set_global_got_offset_arg *)p;
3257  struct mips_got_info *g = arg->g;
3258
3259  if (g && entry->tls_type != GOT_NORMAL)
3260    arg->needed_relocs +=
3261      mips_tls_got_relocs (arg->info, entry->tls_type,
3262			   entry->symndx == -1 ? &entry->d.h->root : NULL);
3263
3264  if (entry->abfd != NULL && entry->symndx == -1
3265      && entry->d.h->root.dynindx != -1
3266      && entry->d.h->tls_type == GOT_NORMAL)
3267    {
3268      if (g)
3269	{
3270	  BFD_ASSERT (g->global_gotsym == NULL);
3271
3272	  entry->gotidx = arg->value * (long) g->assigned_gotno++;
3273	  if (arg->info->shared
3274	      || (elf_hash_table (arg->info)->dynamic_sections_created
3275		  && entry->d.h->root.def_dynamic
3276		  && !entry->d.h->root.def_regular))
3277	    ++arg->needed_relocs;
3278	}
3279      else
3280	entry->d.h->root.got.offset = arg->value;
3281    }
3282
3283  return 1;
3284}
3285
3286/* Mark any global symbols referenced in the GOT we are iterating over
3287   as inelligible for lazy resolution stubs.  */
3288static int
3289mips_elf_set_no_stub (void **entryp, void *p ATTRIBUTE_UNUSED)
3290{
3291  struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3292
3293  if (entry->abfd != NULL
3294      && entry->symndx == -1
3295      && entry->d.h->root.dynindx != -1)
3296    entry->d.h->no_fn_stub = TRUE;
3297
3298  return 1;
3299}
3300
3301/* Follow indirect and warning hash entries so that each got entry
3302   points to the final symbol definition.  P must point to a pointer
3303   to the hash table we're traversing.  Since this traversal may
3304   modify the hash table, we set this pointer to NULL to indicate
3305   we've made a potentially-destructive change to the hash table, so
3306   the traversal must be restarted.  */
3307static int
3308mips_elf_resolve_final_got_entry (void **entryp, void *p)
3309{
3310  struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3311  htab_t got_entries = *(htab_t *)p;
3312
3313  if (entry->abfd != NULL && entry->symndx == -1)
3314    {
3315      struct mips_elf_link_hash_entry *h = entry->d.h;
3316
3317      while (h->root.root.type == bfd_link_hash_indirect
3318 	     || h->root.root.type == bfd_link_hash_warning)
3319	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3320
3321      if (entry->d.h == h)
3322	return 1;
3323
3324      entry->d.h = h;
3325
3326      /* If we can't find this entry with the new bfd hash, re-insert
3327	 it, and get the traversal restarted.  */
3328      if (! htab_find (got_entries, entry))
3329	{
3330	  htab_clear_slot (got_entries, entryp);
3331	  entryp = htab_find_slot (got_entries, entry, INSERT);
3332	  if (! *entryp)
3333	    *entryp = entry;
3334	  /* Abort the traversal, since the whole table may have
3335	     moved, and leave it up to the parent to restart the
3336	     process.  */
3337	  *(htab_t *)p = NULL;
3338	  return 0;
3339	}
3340      /* We might want to decrement the global_gotno count, but it's
3341	 either too early or too late for that at this point.  */
3342    }
3343
3344  return 1;
3345}
3346
3347/* Turn indirect got entries in a got_entries table into their final
3348   locations.  */
3349static void
3350mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3351{
3352  htab_t got_entries;
3353
3354  do
3355    {
3356      got_entries = g->got_entries;
3357
3358      htab_traverse (got_entries,
3359		     mips_elf_resolve_final_got_entry,
3360		     &got_entries);
3361    }
3362  while (got_entries == NULL);
3363}
3364
3365/* Return the offset of an input bfd IBFD's GOT from the beginning of
3366   the primary GOT.  */
3367static bfd_vma
3368mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
3369{
3370  if (g->bfd2got == NULL)
3371    return 0;
3372
3373  g = mips_elf_got_for_ibfd (g, ibfd);
3374  if (! g)
3375    return 0;
3376
3377  BFD_ASSERT (g->next);
3378
3379  g = g->next;
3380
3381  return (g->local_gotno + g->global_gotno + g->tls_gotno)
3382    * MIPS_ELF_GOT_SIZE (abfd);
3383}
3384
3385/* Turn a single GOT that is too big for 16-bit addressing into
3386   a sequence of GOTs, each one 16-bit addressable.  */
3387
3388static bfd_boolean
3389mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
3390		    struct mips_got_info *g, asection *got,
3391		    bfd_size_type pages)
3392{
3393  struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
3394  struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
3395  struct mips_got_info *gg;
3396  unsigned int assign;
3397
3398  g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
3399				mips_elf_bfd2got_entry_eq, NULL);
3400  if (g->bfd2got == NULL)
3401    return FALSE;
3402
3403  got_per_bfd_arg.bfd2got = g->bfd2got;
3404  got_per_bfd_arg.obfd = abfd;
3405  got_per_bfd_arg.info = info;
3406
3407  /* Count how many GOT entries each input bfd requires, creating a
3408     map from bfd to got info while at that.  */
3409  htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
3410  if (got_per_bfd_arg.obfd == NULL)
3411    return FALSE;
3412
3413  got_per_bfd_arg.current = NULL;
3414  got_per_bfd_arg.primary = NULL;
3415  /* Taking out PAGES entries is a worst-case estimate.  We could
3416     compute the maximum number of pages that each separate input bfd
3417     uses, but it's probably not worth it.  */
3418  got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
3419				/ MIPS_ELF_GOT_SIZE (abfd))
3420			       - MIPS_RESERVED_GOTNO (info) - pages);
3421  /* The number of globals that will be included in the primary GOT.
3422     See the calls to mips_elf_set_global_got_offset below for more
3423     information.  */
3424  got_per_bfd_arg.global_count = g->global_gotno;
3425
3426  /* Try to merge the GOTs of input bfds together, as long as they
3427     don't seem to exceed the maximum GOT size, choosing one of them
3428     to be the primary GOT.  */
3429  htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
3430  if (got_per_bfd_arg.obfd == NULL)
3431    return FALSE;
3432
3433  /* If we do not find any suitable primary GOT, create an empty one.  */
3434  if (got_per_bfd_arg.primary == NULL)
3435    {
3436      g->next = (struct mips_got_info *)
3437	bfd_alloc (abfd, sizeof (struct mips_got_info));
3438      if (g->next == NULL)
3439	return FALSE;
3440
3441      g->next->global_gotsym = NULL;
3442      g->next->global_gotno = 0;
3443      g->next->local_gotno = 0;
3444      g->next->tls_gotno = 0;
3445      g->next->assigned_gotno = 0;
3446      g->next->tls_assigned_gotno = 0;
3447      g->next->tls_ldm_offset = MINUS_ONE;
3448      g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3449					      mips_elf_multi_got_entry_eq,
3450					      NULL);
3451      if (g->next->got_entries == NULL)
3452	return FALSE;
3453      g->next->bfd2got = NULL;
3454    }
3455  else
3456    g->next = got_per_bfd_arg.primary;
3457  g->next->next = got_per_bfd_arg.current;
3458
3459  /* GG is now the master GOT, and G is the primary GOT.  */
3460  gg = g;
3461  g = g->next;
3462
3463  /* Map the output bfd to the primary got.  That's what we're going
3464     to use for bfds that use GOT16 or GOT_PAGE relocations that we
3465     didn't mark in check_relocs, and we want a quick way to find it.
3466     We can't just use gg->next because we're going to reverse the
3467     list.  */
3468  {
3469    struct mips_elf_bfd2got_hash *bfdgot;
3470    void **bfdgotp;
3471
3472    bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
3473      (abfd, sizeof (struct mips_elf_bfd2got_hash));
3474
3475    if (bfdgot == NULL)
3476      return FALSE;
3477
3478    bfdgot->bfd = abfd;
3479    bfdgot->g = g;
3480    bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
3481
3482    BFD_ASSERT (*bfdgotp == NULL);
3483    *bfdgotp = bfdgot;
3484  }
3485
3486  /* The IRIX dynamic linker requires every symbol that is referenced
3487     in a dynamic relocation to be present in the primary GOT, so
3488     arrange for them to appear after those that are actually
3489     referenced.
3490
3491     GNU/Linux could very well do without it, but it would slow down
3492     the dynamic linker, since it would have to resolve every dynamic
3493     symbol referenced in other GOTs more than once, without help from
3494     the cache.  Also, knowing that every external symbol has a GOT
3495     helps speed up the resolution of local symbols too, so GNU/Linux
3496     follows IRIX's practice.
3497
3498     The number 2 is used by mips_elf_sort_hash_table_f to count
3499     global GOT symbols that are unreferenced in the primary GOT, with
3500     an initial dynamic index computed from gg->assigned_gotno, where
3501     the number of unreferenced global entries in the primary GOT is
3502     preserved.  */
3503  if (1)
3504    {
3505      gg->assigned_gotno = gg->global_gotno - g->global_gotno;
3506      g->global_gotno = gg->global_gotno;
3507      set_got_offset_arg.value = 2;
3508    }
3509  else
3510    {
3511      /* This could be used for dynamic linkers that don't optimize
3512	 symbol resolution while applying relocations so as to use
3513	 primary GOT entries or assuming the symbol is locally-defined.
3514	 With this code, we assign lower dynamic indices to global
3515	 symbols that are not referenced in the primary GOT, so that
3516	 their entries can be omitted.  */
3517      gg->assigned_gotno = 0;
3518      set_got_offset_arg.value = -1;
3519    }
3520
3521  /* Reorder dynamic symbols as described above (which behavior
3522     depends on the setting of VALUE).  */
3523  set_got_offset_arg.g = NULL;
3524  htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
3525		 &set_got_offset_arg);
3526  set_got_offset_arg.value = 1;
3527  htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
3528		 &set_got_offset_arg);
3529  if (! mips_elf_sort_hash_table (info, 1))
3530    return FALSE;
3531
3532  /* Now go through the GOTs assigning them offset ranges.
3533     [assigned_gotno, local_gotno[ will be set to the range of local
3534     entries in each GOT.  We can then compute the end of a GOT by
3535     adding local_gotno to global_gotno.  We reverse the list and make
3536     it circular since then we'll be able to quickly compute the
3537     beginning of a GOT, by computing the end of its predecessor.  To
3538     avoid special cases for the primary GOT, while still preserving
3539     assertions that are valid for both single- and multi-got links,
3540     we arrange for the main got struct to have the right number of
3541     global entries, but set its local_gotno such that the initial
3542     offset of the primary GOT is zero.  Remember that the primary GOT
3543     will become the last item in the circular linked list, so it
3544     points back to the master GOT.  */
3545  gg->local_gotno = -g->global_gotno;
3546  gg->global_gotno = g->global_gotno;
3547  gg->tls_gotno = 0;
3548  assign = 0;
3549  gg->next = gg;
3550
3551  do
3552    {
3553      struct mips_got_info *gn;
3554
3555      assign += MIPS_RESERVED_GOTNO (info);
3556      g->assigned_gotno = assign;
3557      g->local_gotno += assign + pages;
3558      assign = g->local_gotno + g->global_gotno + g->tls_gotno;
3559
3560      /* Take g out of the direct list, and push it onto the reversed
3561	 list that gg points to.  g->next is guaranteed to be nonnull after
3562	 this operation, as required by mips_elf_initialize_tls_index. */
3563      gn = g->next;
3564      g->next = gg->next;
3565      gg->next = g;
3566
3567      /* Set up any TLS entries.  We always place the TLS entries after
3568	 all non-TLS entries.  */
3569      g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
3570      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
3571
3572      /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
3573      g = gn;
3574
3575      /* Mark global symbols in every non-primary GOT as ineligible for
3576	 stubs.  */
3577      if (g)
3578	htab_traverse (g->got_entries, mips_elf_set_no_stub, NULL);
3579    }
3580  while (g);
3581
3582  got->size = (gg->next->local_gotno
3583		    + gg->next->global_gotno
3584		    + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
3585
3586  return TRUE;
3587}
3588
3589
3590/* Returns the first relocation of type r_type found, beginning with
3591   RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
3592
3593static const Elf_Internal_Rela *
3594mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
3595			  const Elf_Internal_Rela *relocation,
3596			  const Elf_Internal_Rela *relend)
3597{
3598  unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
3599
3600  while (relocation < relend)
3601    {
3602      if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
3603	  && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
3604	return relocation;
3605
3606      ++relocation;
3607    }
3608
3609  /* We didn't find it.  */
3610  return NULL;
3611}
3612
3613/* Return whether a relocation is against a local symbol.  */
3614
3615static bfd_boolean
3616mips_elf_local_relocation_p (bfd *input_bfd,
3617			     const Elf_Internal_Rela *relocation,
3618			     asection **local_sections,
3619			     bfd_boolean check_forced)
3620{
3621  unsigned long r_symndx;
3622  Elf_Internal_Shdr *symtab_hdr;
3623  struct mips_elf_link_hash_entry *h;
3624  size_t extsymoff;
3625
3626  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3627  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3628  extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
3629
3630  if (r_symndx < extsymoff)
3631    return TRUE;
3632  if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
3633    return TRUE;
3634
3635  if (check_forced)
3636    {
3637      /* Look up the hash table to check whether the symbol
3638 	 was forced local.  */
3639      h = (struct mips_elf_link_hash_entry *)
3640	elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
3641      /* Find the real hash-table entry for this symbol.  */
3642      while (h->root.root.type == bfd_link_hash_indirect
3643 	     || h->root.root.type == bfd_link_hash_warning)
3644	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3645      if (h->root.forced_local)
3646	return TRUE;
3647    }
3648
3649  return FALSE;
3650}
3651
3652/* Sign-extend VALUE, which has the indicated number of BITS.  */
3653
3654bfd_vma
3655_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
3656{
3657  if (value & ((bfd_vma) 1 << (bits - 1)))
3658    /* VALUE is negative.  */
3659    value |= ((bfd_vma) - 1) << bits;
3660
3661  return value;
3662}
3663
3664/* Return non-zero if the indicated VALUE has overflowed the maximum
3665   range expressible by a signed number with the indicated number of
3666   BITS.  */
3667
3668static bfd_boolean
3669mips_elf_overflow_p (bfd_vma value, int bits)
3670{
3671  bfd_signed_vma svalue = (bfd_signed_vma) value;
3672
3673  if (svalue > (1 << (bits - 1)) - 1)
3674    /* The value is too big.  */
3675    return TRUE;
3676  else if (svalue < -(1 << (bits - 1)))
3677    /* The value is too small.  */
3678    return TRUE;
3679
3680  /* All is well.  */
3681  return FALSE;
3682}
3683
3684/* Calculate the %high function.  */
3685
3686static bfd_vma
3687mips_elf_high (bfd_vma value)
3688{
3689  return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
3690}
3691
3692/* Calculate the %higher function.  */
3693
3694static bfd_vma
3695mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
3696{
3697#ifdef BFD64
3698  return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
3699#else
3700  abort ();
3701  return MINUS_ONE;
3702#endif
3703}
3704
3705/* Calculate the %highest function.  */
3706
3707static bfd_vma
3708mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
3709{
3710#ifdef BFD64
3711  return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
3712#else
3713  abort ();
3714  return MINUS_ONE;
3715#endif
3716}
3717
3718/* Create the .compact_rel section.  */
3719
3720static bfd_boolean
3721mips_elf_create_compact_rel_section
3722  (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
3723{
3724  flagword flags;
3725  register asection *s;
3726
3727  if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
3728    {
3729      flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
3730	       | SEC_READONLY);
3731
3732      s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
3733      if (s == NULL
3734	  || ! bfd_set_section_alignment (abfd, s,
3735					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
3736	return FALSE;
3737
3738      s->size = sizeof (Elf32_External_compact_rel);
3739    }
3740
3741  return TRUE;
3742}
3743
3744/* Create the .got section to hold the global offset table.  */
3745
3746static bfd_boolean
3747mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info,
3748			     bfd_boolean maybe_exclude)
3749{
3750  flagword flags;
3751  register asection *s;
3752  struct elf_link_hash_entry *h;
3753  struct bfd_link_hash_entry *bh;
3754  struct mips_got_info *g;
3755  bfd_size_type amt;
3756  struct mips_elf_link_hash_table *htab;
3757
3758  htab = mips_elf_hash_table (info);
3759
3760  /* This function may be called more than once.  */
3761  s = mips_elf_got_section (abfd, TRUE);
3762  if (s)
3763    {
3764      if (! maybe_exclude)
3765	s->flags &= ~SEC_EXCLUDE;
3766      return TRUE;
3767    }
3768
3769  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3770	   | SEC_LINKER_CREATED);
3771
3772  if (maybe_exclude)
3773    flags |= SEC_EXCLUDE;
3774
3775  /* We have to use an alignment of 2**4 here because this is hardcoded
3776     in the function stub generation and in the linker script.  */
3777  s = bfd_make_section_with_flags (abfd, ".got", flags);
3778  if (s == NULL
3779      || ! bfd_set_section_alignment (abfd, s, 4))
3780    return FALSE;
3781
3782  /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
3783     linker script because we don't want to define the symbol if we
3784     are not creating a global offset table.  */
3785  bh = NULL;
3786  if (! (_bfd_generic_link_add_one_symbol
3787	 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
3788	  0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
3789    return FALSE;
3790
3791  h = (struct elf_link_hash_entry *) bh;
3792  h->non_elf = 0;
3793  h->def_regular = 1;
3794  h->type = STT_OBJECT;
3795  elf_hash_table (info)->hgot = h;
3796
3797  if (info->shared
3798      && ! bfd_elf_link_record_dynamic_symbol (info, h))
3799    return FALSE;
3800
3801  amt = sizeof (struct mips_got_info);
3802  g = bfd_alloc (abfd, amt);
3803  if (g == NULL)
3804    return FALSE;
3805  g->global_gotsym = NULL;
3806  g->global_gotno = 0;
3807  g->tls_gotno = 0;
3808  g->local_gotno = MIPS_RESERVED_GOTNO (info);
3809  g->assigned_gotno = MIPS_RESERVED_GOTNO (info);
3810  g->bfd2got = NULL;
3811  g->next = NULL;
3812  g->tls_ldm_offset = MINUS_ONE;
3813  g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3814				    mips_elf_got_entry_eq, NULL);
3815  if (g->got_entries == NULL)
3816    return FALSE;
3817  mips_elf_section_data (s)->u.got_info = g;
3818  mips_elf_section_data (s)->elf.this_hdr.sh_flags
3819    |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3820
3821  /* VxWorks also needs a .got.plt section.  */
3822  if (htab->is_vxworks)
3823    {
3824      s = bfd_make_section_with_flags (abfd, ".got.plt",
3825				       SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3826				       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3827      if (s == NULL || !bfd_set_section_alignment (abfd, s, 4))
3828	return FALSE;
3829
3830      htab->sgotplt = s;
3831    }
3832  return TRUE;
3833}
3834
3835/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
3836   __GOTT_INDEX__ symbols.  These symbols are only special for
3837   shared objects; they are not used in executables.  */
3838
3839static bfd_boolean
3840is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
3841{
3842  return (mips_elf_hash_table (info)->is_vxworks
3843	  && info->shared
3844	  && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
3845	      || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
3846}
3847
3848/* Calculate the value produced by the RELOCATION (which comes from
3849   the INPUT_BFD).  The ADDEND is the addend to use for this
3850   RELOCATION; RELOCATION->R_ADDEND is ignored.
3851
3852   The result of the relocation calculation is stored in VALUEP.
3853   REQUIRE_JALXP indicates whether or not the opcode used with this
3854   relocation must be JALX.
3855
3856   This function returns bfd_reloc_continue if the caller need take no
3857   further action regarding this relocation, bfd_reloc_notsupported if
3858   something goes dramatically wrong, bfd_reloc_overflow if an
3859   overflow occurs, and bfd_reloc_ok to indicate success.  */
3860
3861static bfd_reloc_status_type
3862mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
3863			       asection *input_section,
3864			       struct bfd_link_info *info,
3865			       const Elf_Internal_Rela *relocation,
3866			       bfd_vma addend, reloc_howto_type *howto,
3867			       Elf_Internal_Sym *local_syms,
3868			       asection **local_sections, bfd_vma *valuep,
3869			       const char **namep, bfd_boolean *require_jalxp,
3870			       bfd_boolean save_addend)
3871{
3872  /* The eventual value we will return.  */
3873  bfd_vma value;
3874  /* The address of the symbol against which the relocation is
3875     occurring.  */
3876  bfd_vma symbol = 0;
3877  /* The final GP value to be used for the relocatable, executable, or
3878     shared object file being produced.  */
3879  bfd_vma gp = MINUS_ONE;
3880  /* The place (section offset or address) of the storage unit being
3881     relocated.  */
3882  bfd_vma p;
3883  /* The value of GP used to create the relocatable object.  */
3884  bfd_vma gp0 = MINUS_ONE;
3885  /* The offset into the global offset table at which the address of
3886     the relocation entry symbol, adjusted by the addend, resides
3887     during execution.  */
3888  bfd_vma g = MINUS_ONE;
3889  /* The section in which the symbol referenced by the relocation is
3890     located.  */
3891  asection *sec = NULL;
3892  struct mips_elf_link_hash_entry *h = NULL;
3893  /* TRUE if the symbol referred to by this relocation is a local
3894     symbol.  */
3895  bfd_boolean local_p, was_local_p;
3896  /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
3897  bfd_boolean gp_disp_p = FALSE;
3898  /* TRUE if the symbol referred to by this relocation is
3899     "__gnu_local_gp".  */
3900  bfd_boolean gnu_local_gp_p = FALSE;
3901  Elf_Internal_Shdr *symtab_hdr;
3902  size_t extsymoff;
3903  unsigned long r_symndx;
3904  int r_type;
3905  /* TRUE if overflow occurred during the calculation of the
3906     relocation value.  */
3907  bfd_boolean overflowed_p;
3908  /* TRUE if this relocation refers to a MIPS16 function.  */
3909  bfd_boolean target_is_16_bit_code_p = FALSE;
3910  struct mips_elf_link_hash_table *htab;
3911  bfd *dynobj;
3912
3913  dynobj = elf_hash_table (info)->dynobj;
3914  htab = mips_elf_hash_table (info);
3915
3916  /* Parse the relocation.  */
3917  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3918  r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
3919  p = (input_section->output_section->vma
3920       + input_section->output_offset
3921       + relocation->r_offset);
3922
3923  /* Assume that there will be no overflow.  */
3924  overflowed_p = FALSE;
3925
3926  /* Figure out whether or not the symbol is local, and get the offset
3927     used in the array of hash table entries.  */
3928  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3929  local_p = mips_elf_local_relocation_p (input_bfd, relocation,
3930					 local_sections, FALSE);
3931  was_local_p = local_p;
3932  if (! elf_bad_symtab (input_bfd))
3933    extsymoff = symtab_hdr->sh_info;
3934  else
3935    {
3936      /* The symbol table does not follow the rule that local symbols
3937	 must come before globals.  */
3938      extsymoff = 0;
3939    }
3940
3941  /* Figure out the value of the symbol.  */
3942  if (local_p)
3943    {
3944      Elf_Internal_Sym *sym;
3945
3946      sym = local_syms + r_symndx;
3947      sec = local_sections[r_symndx];
3948
3949      symbol = sec->output_section->vma + sec->output_offset;
3950      if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
3951	  || (sec->flags & SEC_MERGE))
3952	symbol += sym->st_value;
3953      if ((sec->flags & SEC_MERGE)
3954	  && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3955	{
3956	  addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
3957	  addend -= symbol;
3958	  addend += sec->output_section->vma + sec->output_offset;
3959	}
3960
3961      /* MIPS16 text labels should be treated as odd.  */
3962      if (sym->st_other == STO_MIPS16)
3963	++symbol;
3964
3965      /* Record the name of this symbol, for our caller.  */
3966      *namep = bfd_elf_string_from_elf_section (input_bfd,
3967						symtab_hdr->sh_link,
3968						sym->st_name);
3969      if (*namep == NULL || **namep == '\0')
3970	*namep = bfd_section_name (input_bfd, sec);
3971
3972      target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
3973    }
3974  else
3975    {
3976      /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
3977
3978      /* For global symbols we look up the symbol in the hash-table.  */
3979      h = ((struct mips_elf_link_hash_entry *)
3980	   elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
3981      /* Find the real hash-table entry for this symbol.  */
3982      while (h->root.root.type == bfd_link_hash_indirect
3983	     || h->root.root.type == bfd_link_hash_warning)
3984	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3985
3986      /* Record the name of this symbol, for our caller.  */
3987      *namep = h->root.root.root.string;
3988
3989      /* See if this is the special _gp_disp symbol.  Note that such a
3990	 symbol must always be a global symbol.  */
3991      if (strcmp (*namep, "_gp_disp") == 0
3992	  && ! NEWABI_P (input_bfd))
3993	{
3994	  /* Relocations against _gp_disp are permitted only with
3995	     R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
3996	  if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16
3997	      && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
3998	    return bfd_reloc_notsupported;
3999
4000	  gp_disp_p = TRUE;
4001	}
4002      /* See if this is the special _gp symbol.  Note that such a
4003	 symbol must always be a global symbol.  */
4004      else if (strcmp (*namep, "__gnu_local_gp") == 0)
4005	gnu_local_gp_p = TRUE;
4006
4007
4008      /* If this symbol is defined, calculate its address.  Note that
4009	 _gp_disp is a magic symbol, always implicitly defined by the
4010	 linker, so it's inappropriate to check to see whether or not
4011	 its defined.  */
4012      else if ((h->root.root.type == bfd_link_hash_defined
4013		|| h->root.root.type == bfd_link_hash_defweak)
4014	       && h->root.root.u.def.section)
4015	{
4016	  sec = h->root.root.u.def.section;
4017	  if (sec->output_section)
4018	    symbol = (h->root.root.u.def.value
4019		      + sec->output_section->vma
4020		      + sec->output_offset);
4021	  else
4022	    symbol = h->root.root.u.def.value;
4023	}
4024      else if (h->root.root.type == bfd_link_hash_undefweak)
4025	/* We allow relocations against undefined weak symbols, giving
4026	   it the value zero, so that you can undefined weak functions
4027	   and check to see if they exist by looking at their
4028	   addresses.  */
4029	symbol = 0;
4030      else if (info->unresolved_syms_in_objects == RM_IGNORE
4031	       && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4032	symbol = 0;
4033      else if (strcmp (*namep, SGI_COMPAT (input_bfd)
4034		       ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
4035	{
4036	  /* If this is a dynamic link, we should have created a
4037	     _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
4038	     in in _bfd_mips_elf_create_dynamic_sections.
4039	     Otherwise, we should define the symbol with a value of 0.
4040	     FIXME: It should probably get into the symbol table
4041	     somehow as well.  */
4042	  BFD_ASSERT (! info->shared);
4043	  BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
4044	  symbol = 0;
4045	}
4046      else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
4047	{
4048	  /* This is an optional symbol - an Irix specific extension to the
4049	     ELF spec.  Ignore it for now.
4050	     XXX - FIXME - there is more to the spec for OPTIONAL symbols
4051	     than simply ignoring them, but we do not handle this for now.
4052	     For information see the "64-bit ELF Object File Specification"
4053	     which is available from here:
4054	     http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
4055	  symbol = 0;
4056	}
4057      else
4058	{
4059	  if (! ((*info->callbacks->undefined_symbol)
4060		 (info, h->root.root.root.string, input_bfd,
4061		  input_section, relocation->r_offset,
4062		  (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
4063		   || ELF_ST_VISIBILITY (h->root.other))))
4064	    return bfd_reloc_undefined;
4065	  symbol = 0;
4066	}
4067
4068      target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
4069    }
4070
4071  /* If this is a 32- or 64-bit call to a 16-bit function with a stub, we
4072     need to redirect the call to the stub, unless we're already *in*
4073     a stub.  */
4074  if (r_type != R_MIPS16_26 && !info->relocatable
4075      && ((h != NULL && h->fn_stub != NULL)
4076	  || (local_p
4077	      && elf_tdata (input_bfd)->local_stubs != NULL
4078	      && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
4079      && !mips16_stub_section_p (input_bfd, input_section))
4080    {
4081      /* This is a 32- or 64-bit call to a 16-bit function.  We should
4082	 have already noticed that we were going to need the
4083	 stub.  */
4084      if (local_p)
4085	sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
4086      else
4087	{
4088	  BFD_ASSERT (h->need_fn_stub);
4089	  sec = h->fn_stub;
4090	}
4091
4092      symbol = sec->output_section->vma + sec->output_offset;
4093      /* The target is 16-bit, but the stub isn't.  */
4094      target_is_16_bit_code_p = FALSE;
4095    }
4096  /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
4097     need to redirect the call to the stub.  */
4098  else if (r_type == R_MIPS16_26 && !info->relocatable
4099	   && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
4100	       || (local_p
4101		   && elf_tdata (input_bfd)->local_call_stubs != NULL
4102		   && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
4103	   && !target_is_16_bit_code_p)
4104    {
4105      if (local_p)
4106	sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
4107      else
4108	{
4109	  /* If both call_stub and call_fp_stub are defined, we can figure
4110	     out which one to use by checking which one appears in the input
4111	     file.  */
4112	  if (h->call_stub != NULL && h->call_fp_stub != NULL)
4113	    {
4114	      asection *o;
4115
4116	      sec = NULL;
4117	      for (o = input_bfd->sections; o != NULL; o = o->next)
4118		{
4119		  if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
4120		    {
4121		      sec = h->call_fp_stub;
4122		      break;
4123		    }
4124		}
4125	      if (sec == NULL)
4126		sec = h->call_stub;
4127	    }
4128	  else if (h->call_stub != NULL)
4129	    sec = h->call_stub;
4130	  else
4131	    sec = h->call_fp_stub;
4132  	}
4133
4134      BFD_ASSERT (sec->size > 0);
4135      symbol = sec->output_section->vma + sec->output_offset;
4136    }
4137
4138  /* Calls from 16-bit code to 32-bit code and vice versa require the
4139     special jalx instruction.  */
4140  *require_jalxp = (!info->relocatable
4141                    && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
4142                        || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
4143
4144  local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4145					 local_sections, TRUE);
4146
4147  /* If we haven't already determined the GOT offset, or the GP value,
4148     and we're going to need it, get it now.  */
4149  switch (r_type)
4150    {
4151    case R_MIPS_GOT_PAGE:
4152    case R_MIPS_GOT_OFST:
4153      /* We need to decay to GOT_DISP/addend if the symbol doesn't
4154	 bind locally.  */
4155      local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
4156      if (local_p || r_type == R_MIPS_GOT_OFST)
4157	break;
4158      /* Fall through.  */
4159
4160    case R_MIPS_CALL16:
4161    case R_MIPS_GOT16:
4162    case R_MIPS_GOT_DISP:
4163    case R_MIPS_GOT_HI16:
4164    case R_MIPS_CALL_HI16:
4165    case R_MIPS_GOT_LO16:
4166    case R_MIPS_CALL_LO16:
4167    case R_MIPS_TLS_GD:
4168    case R_MIPS_TLS_GOTTPREL:
4169    case R_MIPS_TLS_LDM:
4170      /* Find the index into the GOT where this value is located.  */
4171      if (r_type == R_MIPS_TLS_LDM)
4172	{
4173	  g = mips_elf_local_got_index (abfd, input_bfd, info,
4174					0, 0, NULL, r_type);
4175	  if (g == MINUS_ONE)
4176	    return bfd_reloc_outofrange;
4177	}
4178      else if (!local_p)
4179	{
4180	  /* On VxWorks, CALL relocations should refer to the .got.plt
4181	     entry, which is initialized to point at the PLT stub.  */
4182	  if (htab->is_vxworks
4183	      && (r_type == R_MIPS_CALL_HI16
4184		  || r_type == R_MIPS_CALL_LO16
4185		  || r_type == R_MIPS_CALL16))
4186	    {
4187	      BFD_ASSERT (addend == 0);
4188	      BFD_ASSERT (h->root.needs_plt);
4189	      g = mips_elf_gotplt_index (info, &h->root);
4190	    }
4191	  else
4192	    {
4193	      /* GOT_PAGE may take a non-zero addend, that is ignored in a
4194		 GOT_PAGE relocation that decays to GOT_DISP because the
4195		 symbol turns out to be global.  The addend is then added
4196		 as GOT_OFST.  */
4197	      BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
4198	      g = mips_elf_global_got_index (dynobj, input_bfd,
4199					     &h->root, r_type, info);
4200	      if (h->tls_type == GOT_NORMAL
4201		  && (! elf_hash_table(info)->dynamic_sections_created
4202		      || (info->shared
4203			  && (info->symbolic || h->root.forced_local)
4204			  && h->root.def_regular)))
4205		{
4206		  /* This is a static link or a -Bsymbolic link.  The
4207		     symbol is defined locally, or was forced to be local.
4208		     We must initialize this entry in the GOT.  */
4209		  asection *sgot = mips_elf_got_section (dynobj, FALSE);
4210		  MIPS_ELF_PUT_WORD (dynobj, symbol, sgot->contents + g);
4211		}
4212	    }
4213	}
4214      else if (!htab->is_vxworks
4215	       && (r_type == R_MIPS_CALL16 || (r_type == R_MIPS_GOT16)))
4216	/* The calculation below does not involve "g".  */
4217	break;
4218      else
4219	{
4220	  g = mips_elf_local_got_index (abfd, input_bfd, info,
4221					symbol + addend, r_symndx, h, r_type);
4222	  if (g == MINUS_ONE)
4223	    return bfd_reloc_outofrange;
4224	}
4225
4226      /* Convert GOT indices to actual offsets.  */
4227      g = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, g);
4228      break;
4229
4230    case R_MIPS_HI16:
4231    case R_MIPS_LO16:
4232    case R_MIPS_GPREL16:
4233    case R_MIPS_GPREL32:
4234    case R_MIPS_LITERAL:
4235    case R_MIPS16_HI16:
4236    case R_MIPS16_LO16:
4237    case R_MIPS16_GPREL:
4238      gp0 = _bfd_get_gp_value (input_bfd);
4239      gp = _bfd_get_gp_value (abfd);
4240      if (dynobj)
4241	gp += mips_elf_adjust_gp (abfd, mips_elf_got_info (dynobj, NULL),
4242				  input_bfd);
4243      break;
4244
4245    default:
4246      break;
4247    }
4248
4249  if (gnu_local_gp_p)
4250    symbol = gp;
4251
4252  /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
4253     symbols are resolved by the loader.  Add them to .rela.dyn.  */
4254  if (h != NULL && is_gott_symbol (info, &h->root))
4255    {
4256      Elf_Internal_Rela outrel;
4257      bfd_byte *loc;
4258      asection *s;
4259
4260      s = mips_elf_rel_dyn_section (info, FALSE);
4261      loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4262
4263      outrel.r_offset = (input_section->output_section->vma
4264			 + input_section->output_offset
4265			 + relocation->r_offset);
4266      outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
4267      outrel.r_addend = addend;
4268      bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
4269
4270      /* If we've written this relocation for a readonly section,
4271	 we need to set DF_TEXTREL again, so that we do not delete the
4272	 DT_TEXTREL tag.  */
4273      if (MIPS_ELF_READONLY_SECTION (input_section))
4274	info->flags |= DF_TEXTREL;
4275
4276      *valuep = 0;
4277      return bfd_reloc_ok;
4278    }
4279
4280  /* Figure out what kind of relocation is being performed.  */
4281  switch (r_type)
4282    {
4283    case R_MIPS_NONE:
4284      return bfd_reloc_continue;
4285
4286    case R_MIPS_16:
4287      value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
4288      overflowed_p = mips_elf_overflow_p (value, 16);
4289      break;
4290
4291    case R_MIPS_32:
4292    case R_MIPS_REL32:
4293    case R_MIPS_64:
4294      if ((info->shared
4295	   || (!htab->is_vxworks
4296	       && htab->root.dynamic_sections_created
4297	       && h != NULL
4298	       && h->root.def_dynamic
4299	       && !h->root.def_regular))
4300	  && r_symndx != 0
4301	  && (input_section->flags & SEC_ALLOC) != 0)
4302	{
4303	  /* If we're creating a shared library, or this relocation is
4304	     against a symbol in a shared library, then we can't know
4305	     where the symbol will end up.  So, we create a relocation
4306	     record in the output, and leave the job up to the dynamic
4307	     linker.
4308
4309	     In VxWorks executables, references to external symbols
4310	     are handled using copy relocs or PLT stubs, so there's
4311	     no need to add a dynamic relocation here.  */
4312	  value = addend;
4313	  if (!mips_elf_create_dynamic_relocation (abfd,
4314						   info,
4315						   relocation,
4316						   h,
4317						   sec,
4318						   symbol,
4319						   &value,
4320						   input_section))
4321	    return bfd_reloc_undefined;
4322	}
4323      else
4324	{
4325	  if (r_type != R_MIPS_REL32)
4326	    value = symbol + addend;
4327	  else
4328	    value = addend;
4329	}
4330      value &= howto->dst_mask;
4331      break;
4332
4333    case R_MIPS_PC32:
4334      value = symbol + addend - p;
4335      value &= howto->dst_mask;
4336      break;
4337
4338    case R_MIPS16_26:
4339      /* The calculation for R_MIPS16_26 is just the same as for an
4340	 R_MIPS_26.  It's only the storage of the relocated field into
4341	 the output file that's different.  That's handled in
4342	 mips_elf_perform_relocation.  So, we just fall through to the
4343	 R_MIPS_26 case here.  */
4344    case R_MIPS_26:
4345      if (local_p)
4346	value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
4347      else
4348	{
4349	  value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
4350	  if (h->root.root.type != bfd_link_hash_undefweak)
4351	    overflowed_p = (value >> 26) != ((p + 4) >> 28);
4352	}
4353      value &= howto->dst_mask;
4354      break;
4355
4356    case R_MIPS_TLS_DTPREL_HI16:
4357      value = (mips_elf_high (addend + symbol - dtprel_base (info))
4358	       & howto->dst_mask);
4359      break;
4360
4361    case R_MIPS_TLS_DTPREL_LO16:
4362    case R_MIPS_TLS_DTPREL32:
4363    case R_MIPS_TLS_DTPREL64:
4364      value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
4365      break;
4366
4367    case R_MIPS_TLS_TPREL_HI16:
4368      value = (mips_elf_high (addend + symbol - tprel_base (info))
4369	       & howto->dst_mask);
4370      break;
4371
4372    case R_MIPS_TLS_TPREL_LO16:
4373      value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
4374      break;
4375
4376    case R_MIPS_HI16:
4377    case R_MIPS16_HI16:
4378      if (!gp_disp_p)
4379	{
4380	  value = mips_elf_high (addend + symbol);
4381	  value &= howto->dst_mask;
4382	}
4383      else
4384	{
4385	  /* For MIPS16 ABI code we generate this sequence
4386	        0: li      $v0,%hi(_gp_disp)
4387	        4: addiupc $v1,%lo(_gp_disp)
4388	        8: sll     $v0,16
4389	       12: addu    $v0,$v1
4390	       14: move    $gp,$v0
4391	     So the offsets of hi and lo relocs are the same, but the
4392	     $pc is four higher than $t9 would be, so reduce
4393	     both reloc addends by 4. */
4394	  if (r_type == R_MIPS16_HI16)
4395	    value = mips_elf_high (addend + gp - p - 4);
4396	  else
4397	    value = mips_elf_high (addend + gp - p);
4398	  overflowed_p = mips_elf_overflow_p (value, 16);
4399	}
4400      break;
4401
4402    case R_MIPS_LO16:
4403    case R_MIPS16_LO16:
4404      if (!gp_disp_p)
4405	value = (symbol + addend) & howto->dst_mask;
4406      else
4407	{
4408	  /* See the comment for R_MIPS16_HI16 above for the reason
4409	     for this conditional.  */
4410	  if (r_type == R_MIPS16_LO16)
4411	    value = addend + gp - p;
4412	  else
4413	    value = addend + gp - p + 4;
4414	  /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
4415	     for overflow.  But, on, say, IRIX5, relocations against
4416	     _gp_disp are normally generated from the .cpload
4417	     pseudo-op.  It generates code that normally looks like
4418	     this:
4419
4420	       lui    $gp,%hi(_gp_disp)
4421	       addiu  $gp,$gp,%lo(_gp_disp)
4422	       addu   $gp,$gp,$t9
4423
4424	     Here $t9 holds the address of the function being called,
4425	     as required by the MIPS ELF ABI.  The R_MIPS_LO16
4426	     relocation can easily overflow in this situation, but the
4427	     R_MIPS_HI16 relocation will handle the overflow.
4428	     Therefore, we consider this a bug in the MIPS ABI, and do
4429	     not check for overflow here.  */
4430	}
4431      break;
4432
4433    case R_MIPS_LITERAL:
4434      /* Because we don't merge literal sections, we can handle this
4435	 just like R_MIPS_GPREL16.  In the long run, we should merge
4436	 shared literals, and then we will need to additional work
4437	 here.  */
4438
4439      /* Fall through.  */
4440
4441    case R_MIPS16_GPREL:
4442      /* The R_MIPS16_GPREL performs the same calculation as
4443	 R_MIPS_GPREL16, but stores the relocated bits in a different
4444	 order.  We don't need to do anything special here; the
4445	 differences are handled in mips_elf_perform_relocation.  */
4446    case R_MIPS_GPREL16:
4447      /* Only sign-extend the addend if it was extracted from the
4448	 instruction.  If the addend was separate, leave it alone,
4449	 otherwise we may lose significant bits.  */
4450      if (howto->partial_inplace)
4451	addend = _bfd_mips_elf_sign_extend (addend, 16);
4452      value = symbol + addend - gp;
4453      /* If the symbol was local, any earlier relocatable links will
4454	 have adjusted its addend with the gp offset, so compensate
4455	 for that now.  Don't do it for symbols forced local in this
4456	 link, though, since they won't have had the gp offset applied
4457	 to them before.  */
4458      if (was_local_p)
4459	value += gp0;
4460      overflowed_p = mips_elf_overflow_p (value, 16);
4461      break;
4462
4463    case R_MIPS_GOT16:
4464    case R_MIPS_CALL16:
4465      /* VxWorks does not have separate local and global semantics for
4466	 R_MIPS_GOT16; every relocation evaluates to "G".  */
4467      if (!htab->is_vxworks && local_p)
4468	{
4469	  bfd_boolean forced;
4470
4471	  forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
4472						  local_sections, FALSE);
4473	  value = mips_elf_got16_entry (abfd, input_bfd, info,
4474					symbol + addend, forced);
4475	  if (value == MINUS_ONE)
4476	    return bfd_reloc_outofrange;
4477	  value
4478	    = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, value);
4479	  overflowed_p = mips_elf_overflow_p (value, 16);
4480	  break;
4481	}
4482
4483      /* Fall through.  */
4484
4485    case R_MIPS_TLS_GD:
4486    case R_MIPS_TLS_GOTTPREL:
4487    case R_MIPS_TLS_LDM:
4488    case R_MIPS_GOT_DISP:
4489    got_disp:
4490      value = g;
4491      overflowed_p = mips_elf_overflow_p (value, 16);
4492      break;
4493
4494    case R_MIPS_GPREL32:
4495      value = (addend + symbol + gp0 - gp);
4496      if (!save_addend)
4497	value &= howto->dst_mask;
4498      break;
4499
4500    case R_MIPS_PC16:
4501    case R_MIPS_GNU_REL16_S2:
4502      value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
4503      overflowed_p = mips_elf_overflow_p (value, 18);
4504      value >>= howto->rightshift;
4505      value &= howto->dst_mask;
4506      break;
4507
4508    case R_MIPS_GOT_HI16:
4509    case R_MIPS_CALL_HI16:
4510      /* We're allowed to handle these two relocations identically.
4511	 The dynamic linker is allowed to handle the CALL relocations
4512	 differently by creating a lazy evaluation stub.  */
4513      value = g;
4514      value = mips_elf_high (value);
4515      value &= howto->dst_mask;
4516      break;
4517
4518    case R_MIPS_GOT_LO16:
4519    case R_MIPS_CALL_LO16:
4520      value = g & howto->dst_mask;
4521      break;
4522
4523    case R_MIPS_GOT_PAGE:
4524      /* GOT_PAGE relocations that reference non-local symbols decay
4525	 to GOT_DISP.  The corresponding GOT_OFST relocation decays to
4526	 0.  */
4527      if (! local_p)
4528	goto got_disp;
4529      value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
4530      if (value == MINUS_ONE)
4531	return bfd_reloc_outofrange;
4532      value = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, value);
4533      overflowed_p = mips_elf_overflow_p (value, 16);
4534      break;
4535
4536    case R_MIPS_GOT_OFST:
4537      if (local_p)
4538	mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
4539      else
4540	value = addend;
4541      overflowed_p = mips_elf_overflow_p (value, 16);
4542      break;
4543
4544    case R_MIPS_SUB:
4545      value = symbol - addend;
4546      value &= howto->dst_mask;
4547      break;
4548
4549    case R_MIPS_HIGHER:
4550      value = mips_elf_higher (addend + symbol);
4551      value &= howto->dst_mask;
4552      break;
4553
4554    case R_MIPS_HIGHEST:
4555      value = mips_elf_highest (addend + symbol);
4556      value &= howto->dst_mask;
4557      break;
4558
4559    case R_MIPS_SCN_DISP:
4560      value = symbol + addend - sec->output_offset;
4561      value &= howto->dst_mask;
4562      break;
4563
4564    case R_MIPS_JALR:
4565      /* This relocation is only a hint.  In some cases, we optimize
4566	 it into a bal instruction.  But we don't try to optimize
4567	 branches to the PLT; that will wind up wasting time.  */
4568      if (h != NULL && h->root.plt.offset != (bfd_vma) -1)
4569	return bfd_reloc_continue;
4570      value = symbol + addend;
4571      break;
4572
4573    case R_MIPS_PJUMP:
4574    case R_MIPS_GNU_VTINHERIT:
4575    case R_MIPS_GNU_VTENTRY:
4576      /* We don't do anything with these at present.  */
4577      return bfd_reloc_continue;
4578
4579    default:
4580      /* An unrecognized relocation type.  */
4581      return bfd_reloc_notsupported;
4582    }
4583
4584  /* Store the VALUE for our caller.  */
4585  *valuep = value;
4586  return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
4587}
4588
4589/* Obtain the field relocated by RELOCATION.  */
4590
4591static bfd_vma
4592mips_elf_obtain_contents (reloc_howto_type *howto,
4593			  const Elf_Internal_Rela *relocation,
4594			  bfd *input_bfd, bfd_byte *contents)
4595{
4596  bfd_vma x;
4597  bfd_byte *location = contents + relocation->r_offset;
4598
4599  /* Obtain the bytes.  */
4600  x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
4601
4602  return x;
4603}
4604
4605/* It has been determined that the result of the RELOCATION is the
4606   VALUE.  Use HOWTO to place VALUE into the output file at the
4607   appropriate position.  The SECTION is the section to which the
4608   relocation applies.  If REQUIRE_JALX is TRUE, then the opcode used
4609   for the relocation must be either JAL or JALX, and it is
4610   unconditionally converted to JALX.
4611
4612   Returns FALSE if anything goes wrong.  */
4613
4614static bfd_boolean
4615mips_elf_perform_relocation (struct bfd_link_info *info,
4616			     reloc_howto_type *howto,
4617			     const Elf_Internal_Rela *relocation,
4618			     bfd_vma value, bfd *input_bfd,
4619			     asection *input_section, bfd_byte *contents,
4620			     bfd_boolean require_jalx)
4621{
4622  bfd_vma x;
4623  bfd_byte *location;
4624  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4625
4626  /* Figure out where the relocation is occurring.  */
4627  location = contents + relocation->r_offset;
4628
4629  _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
4630
4631  /* Obtain the current value.  */
4632  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
4633
4634  /* Clear the field we are setting.  */
4635  x &= ~howto->dst_mask;
4636
4637  /* Set the field.  */
4638  x |= (value & howto->dst_mask);
4639
4640  /* If required, turn JAL into JALX.  */
4641  if (require_jalx)
4642    {
4643      bfd_boolean ok;
4644      bfd_vma opcode = x >> 26;
4645      bfd_vma jalx_opcode;
4646
4647      /* Check to see if the opcode is already JAL or JALX.  */
4648      if (r_type == R_MIPS16_26)
4649	{
4650	  ok = ((opcode == 0x6) || (opcode == 0x7));
4651	  jalx_opcode = 0x7;
4652	}
4653      else
4654	{
4655	  ok = ((opcode == 0x3) || (opcode == 0x1d));
4656	  jalx_opcode = 0x1d;
4657	}
4658
4659      /* If the opcode is not JAL or JALX, there's a problem.  */
4660      if (!ok)
4661	{
4662	  (*_bfd_error_handler)
4663	    (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
4664	     input_bfd,
4665	     input_section,
4666	     (unsigned long) relocation->r_offset);
4667	  bfd_set_error (bfd_error_bad_value);
4668	  return FALSE;
4669	}
4670
4671      /* Make this the JALX opcode.  */
4672      x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
4673    }
4674
4675  /* On the RM9000, bal is faster than jal, because bal uses branch
4676     prediction hardware.  If we are linking for the RM9000, and we
4677     see jal, and bal fits, use it instead.  Note that this
4678     transformation should be safe for all architectures.  */
4679  if (bfd_get_mach (input_bfd) == bfd_mach_mips9000
4680      && !info->relocatable
4681      && !require_jalx
4682      && ((r_type == R_MIPS_26 && (x >> 26) == 0x3)	    /* jal addr */
4683	  || (r_type == R_MIPS_JALR && x == 0x0320f809)))   /* jalr t9 */
4684    {
4685      bfd_vma addr;
4686      bfd_vma dest;
4687      bfd_signed_vma off;
4688
4689      addr = (input_section->output_section->vma
4690	      + input_section->output_offset
4691	      + relocation->r_offset
4692	      + 4);
4693      if (r_type == R_MIPS_26)
4694	dest = (value << 2) | ((addr >> 28) << 28);
4695      else
4696	dest = value;
4697      off = dest - addr;
4698      if (off <= 0x1ffff && off >= -0x20000)
4699	x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
4700    }
4701
4702  /* Put the value into the output.  */
4703  bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
4704
4705  _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
4706				location);
4707
4708  return TRUE;
4709}
4710
4711/* Returns TRUE if SECTION is a MIPS16 stub section.  */
4712
4713static bfd_boolean
4714mips16_stub_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *section)
4715{
4716  const char *name = bfd_get_section_name (abfd, section);
4717
4718  return FN_STUB_P (name) || CALL_STUB_P (name) || CALL_FP_STUB_P (name);
4719}
4720
4721/* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
4722
4723static void
4724mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4725				       unsigned int n)
4726{
4727  asection *s;
4728  struct mips_elf_link_hash_table *htab;
4729
4730  htab = mips_elf_hash_table (info);
4731  s = mips_elf_rel_dyn_section (info, FALSE);
4732  BFD_ASSERT (s != NULL);
4733
4734  if (htab->is_vxworks)
4735    s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4736  else
4737    {
4738      if (s->size == 0)
4739	{
4740	  /* Make room for a null element.  */
4741	  s->size += MIPS_ELF_REL_SIZE (abfd);
4742	  ++s->reloc_count;
4743	}
4744      s->size += n * MIPS_ELF_REL_SIZE (abfd);
4745    }
4746}
4747
4748/* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
4749   is the original relocation, which is now being transformed into a
4750   dynamic relocation.  The ADDENDP is adjusted if necessary; the
4751   caller should store the result in place of the original addend.  */
4752
4753static bfd_boolean
4754mips_elf_create_dynamic_relocation (bfd *output_bfd,
4755				    struct bfd_link_info *info,
4756				    const Elf_Internal_Rela *rel,
4757				    struct mips_elf_link_hash_entry *h,
4758				    asection *sec, bfd_vma symbol,
4759				    bfd_vma *addendp, asection *input_section)
4760{
4761  Elf_Internal_Rela outrel[3];
4762  asection *sreloc;
4763  bfd *dynobj;
4764  int r_type;
4765  long indx;
4766  bfd_boolean defined_p;
4767  struct mips_elf_link_hash_table *htab;
4768
4769  htab = mips_elf_hash_table (info);
4770  r_type = ELF_R_TYPE (output_bfd, rel->r_info);
4771  dynobj = elf_hash_table (info)->dynobj;
4772  sreloc = mips_elf_rel_dyn_section (info, FALSE);
4773  BFD_ASSERT (sreloc != NULL);
4774  BFD_ASSERT (sreloc->contents != NULL);
4775  BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
4776	      < sreloc->size);
4777
4778  outrel[0].r_offset =
4779    _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
4780  if (ABI_64_P (output_bfd))
4781    {
4782      outrel[1].r_offset =
4783	_bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
4784      outrel[2].r_offset =
4785	_bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
4786    }
4787
4788  if (outrel[0].r_offset == MINUS_ONE)
4789    /* The relocation field has been deleted.  */
4790    return TRUE;
4791
4792  if (outrel[0].r_offset == MINUS_TWO)
4793    {
4794      /* The relocation field has been converted into a relative value of
4795	 some sort.  Functions like _bfd_elf_write_section_eh_frame expect
4796	 the field to be fully relocated, so add in the symbol's value.  */
4797      *addendp += symbol;
4798      return TRUE;
4799    }
4800
4801  /* We must now calculate the dynamic symbol table index to use
4802     in the relocation.  */
4803  if (h != NULL
4804      && (sec == NULL || !h->root.def_regular
4805	  || (info->shared && !info->symbolic && !h->root.forced_local)))
4806    {
4807      indx = h->root.dynindx;
4808      if (SGI_COMPAT (output_bfd))
4809	defined_p = h->root.def_regular;
4810      else
4811	/* ??? glibc's ld.so just adds the final GOT entry to the
4812	   relocation field.  It therefore treats relocs against
4813	   defined symbols in the same way as relocs against
4814	   undefined symbols.  */
4815	defined_p = FALSE;
4816    }
4817  else
4818    {
4819      if (sec != NULL && bfd_is_abs_section (sec))
4820	indx = 0;
4821      else if (sec == NULL || sec->owner == NULL)
4822	{
4823	  bfd_set_error (bfd_error_bad_value);
4824	  return FALSE;
4825	}
4826      else
4827	{
4828	  indx = elf_section_data (sec->output_section)->dynindx;
4829	  if (indx == 0)
4830	    {
4831	      asection *osec = htab->root.text_index_section;
4832	      indx = elf_section_data (osec)->dynindx;
4833	    }
4834	  if (indx == 0)
4835	    abort ();
4836	}
4837
4838      /* Instead of generating a relocation using the section
4839	 symbol, we may as well make it a fully relative
4840	 relocation.  We want to avoid generating relocations to
4841	 local symbols because we used to generate them
4842	 incorrectly, without adding the original symbol value,
4843	 which is mandated by the ABI for section symbols.  In
4844	 order to give dynamic loaders and applications time to
4845	 phase out the incorrect use, we refrain from emitting
4846	 section-relative relocations.  It's not like they're
4847	 useful, after all.  This should be a bit more efficient
4848	 as well.  */
4849      /* ??? Although this behavior is compatible with glibc's ld.so,
4850	 the ABI says that relocations against STN_UNDEF should have
4851	 a symbol value of 0.  Irix rld honors this, so relocations
4852	 against STN_UNDEF have no effect.  */
4853      if (!SGI_COMPAT (output_bfd))
4854	indx = 0;
4855      defined_p = TRUE;
4856    }
4857
4858  /* If the relocation was previously an absolute relocation and
4859     this symbol will not be referred to by the relocation, we must
4860     adjust it by the value we give it in the dynamic symbol table.
4861     Otherwise leave the job up to the dynamic linker.  */
4862  if (defined_p && r_type != R_MIPS_REL32)
4863    *addendp += symbol;
4864
4865  if (htab->is_vxworks)
4866    /* VxWorks uses non-relative relocations for this.  */
4867    outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
4868  else
4869    /* The relocation is always an REL32 relocation because we don't
4870       know where the shared library will wind up at load-time.  */
4871    outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
4872				   R_MIPS_REL32);
4873
4874  /* For strict adherence to the ABI specification, we should
4875     generate a R_MIPS_64 relocation record by itself before the
4876     _REL32/_64 record as well, such that the addend is read in as
4877     a 64-bit value (REL32 is a 32-bit relocation, after all).
4878     However, since none of the existing ELF64 MIPS dynamic
4879     loaders seems to care, we don't waste space with these
4880     artificial relocations.  If this turns out to not be true,
4881     mips_elf_allocate_dynamic_relocation() should be tweaked so
4882     as to make room for a pair of dynamic relocations per
4883     invocation if ABI_64_P, and here we should generate an
4884     additional relocation record with R_MIPS_64 by itself for a
4885     NULL symbol before this relocation record.  */
4886  outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
4887				 ABI_64_P (output_bfd)
4888				 ? R_MIPS_64
4889				 : R_MIPS_NONE);
4890  outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
4891
4892  /* Adjust the output offset of the relocation to reference the
4893     correct location in the output file.  */
4894  outrel[0].r_offset += (input_section->output_section->vma
4895			 + input_section->output_offset);
4896  outrel[1].r_offset += (input_section->output_section->vma
4897			 + input_section->output_offset);
4898  outrel[2].r_offset += (input_section->output_section->vma
4899			 + input_section->output_offset);
4900
4901  /* Put the relocation back out.  We have to use the special
4902     relocation outputter in the 64-bit case since the 64-bit
4903     relocation format is non-standard.  */
4904  if (ABI_64_P (output_bfd))
4905    {
4906      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
4907	(output_bfd, &outrel[0],
4908	 (sreloc->contents
4909	  + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
4910    }
4911  else if (htab->is_vxworks)
4912    {
4913      /* VxWorks uses RELA rather than REL dynamic relocations.  */
4914      outrel[0].r_addend = *addendp;
4915      bfd_elf32_swap_reloca_out
4916	(output_bfd, &outrel[0],
4917	 (sreloc->contents
4918	  + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
4919    }
4920  else
4921    bfd_elf32_swap_reloc_out
4922      (output_bfd, &outrel[0],
4923       (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
4924
4925  /* We've now added another relocation.  */
4926  ++sreloc->reloc_count;
4927
4928  /* Make sure the output section is writable.  The dynamic linker
4929     will be writing to it.  */
4930  elf_section_data (input_section->output_section)->this_hdr.sh_flags
4931    |= SHF_WRITE;
4932
4933  /* On IRIX5, make an entry of compact relocation info.  */
4934  if (IRIX_COMPAT (output_bfd) == ict_irix5)
4935    {
4936      asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4937      bfd_byte *cr;
4938
4939      if (scpt)
4940	{
4941	  Elf32_crinfo cptrel;
4942
4943	  mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
4944	  cptrel.vaddr = (rel->r_offset
4945			  + input_section->output_section->vma
4946			  + input_section->output_offset);
4947	  if (r_type == R_MIPS_REL32)
4948	    mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
4949	  else
4950	    mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
4951	  mips_elf_set_cr_dist2to (cptrel, 0);
4952	  cptrel.konst = *addendp;
4953
4954	  cr = (scpt->contents
4955		+ sizeof (Elf32_External_compact_rel));
4956	  mips_elf_set_cr_relvaddr (cptrel, 0);
4957	  bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4958				     ((Elf32_External_crinfo *) cr
4959				      + scpt->reloc_count));
4960	  ++scpt->reloc_count;
4961	}
4962    }
4963
4964  /* If we've written this relocation for a readonly section,
4965     we need to set DF_TEXTREL again, so that we do not delete the
4966     DT_TEXTREL tag.  */
4967  if (MIPS_ELF_READONLY_SECTION (input_section))
4968    info->flags |= DF_TEXTREL;
4969
4970  return TRUE;
4971}
4972
4973/* Return the MACH for a MIPS e_flags value.  */
4974
4975unsigned long
4976_bfd_elf_mips_mach (flagword flags)
4977{
4978  switch (flags & EF_MIPS_MACH)
4979    {
4980    case E_MIPS_MACH_3900:
4981      return bfd_mach_mips3900;
4982
4983    case E_MIPS_MACH_4010:
4984      return bfd_mach_mips4010;
4985
4986    case E_MIPS_MACH_4100:
4987      return bfd_mach_mips4100;
4988
4989    case E_MIPS_MACH_4111:
4990      return bfd_mach_mips4111;
4991
4992    case E_MIPS_MACH_4120:
4993      return bfd_mach_mips4120;
4994
4995    case E_MIPS_MACH_4650:
4996      return bfd_mach_mips4650;
4997
4998    case E_MIPS_MACH_5400:
4999      return bfd_mach_mips5400;
5000
5001    case E_MIPS_MACH_5500:
5002      return bfd_mach_mips5500;
5003
5004    case E_MIPS_MACH_9000:
5005      return bfd_mach_mips9000;
5006
5007    case E_MIPS_MACH_OCTEON:
5008      return bfd_mach_mips_octeon;
5009
5010    case E_MIPS_MACH_SB1:
5011      return bfd_mach_mips_sb1;
5012
5013    default:
5014      switch (flags & EF_MIPS_ARCH)
5015	{
5016	default:
5017	case E_MIPS_ARCH_1:
5018	  return bfd_mach_mips3000;
5019
5020	case E_MIPS_ARCH_2:
5021	  return bfd_mach_mips6000;
5022
5023	case E_MIPS_ARCH_3:
5024	  return bfd_mach_mips4000;
5025
5026	case E_MIPS_ARCH_4:
5027	  return bfd_mach_mips8000;
5028
5029	case E_MIPS_ARCH_5:
5030	  return bfd_mach_mips5;
5031
5032	case E_MIPS_ARCH_32:
5033	  return bfd_mach_mipsisa32;
5034
5035	case E_MIPS_ARCH_64:
5036	  return bfd_mach_mipsisa64;
5037
5038	case E_MIPS_ARCH_32R2:
5039	  return bfd_mach_mipsisa32r2;
5040
5041	case E_MIPS_ARCH_64R2:
5042	  return bfd_mach_mipsisa64r2;
5043	}
5044    }
5045
5046  return 0;
5047}
5048
5049/* Return printable name for ABI.  */
5050
5051static INLINE char *
5052elf_mips_abi_name (bfd *abfd)
5053{
5054  flagword flags;
5055
5056  flags = elf_elfheader (abfd)->e_flags;
5057  switch (flags & EF_MIPS_ABI)
5058    {
5059    case 0:
5060      if (ABI_N32_P (abfd))
5061	return "N32";
5062      else if (ABI_64_P (abfd))
5063	return "64";
5064      else
5065	return "none";
5066    case E_MIPS_ABI_O32:
5067      return "O32";
5068    case E_MIPS_ABI_O64:
5069      return "O64";
5070    case E_MIPS_ABI_EABI32:
5071      return "EABI32";
5072    case E_MIPS_ABI_EABI64:
5073      return "EABI64";
5074    default:
5075      return "unknown abi";
5076    }
5077}
5078
5079/* MIPS ELF uses two common sections.  One is the usual one, and the
5080   other is for small objects.  All the small objects are kept
5081   together, and then referenced via the gp pointer, which yields
5082   faster assembler code.  This is what we use for the small common
5083   section.  This approach is copied from ecoff.c.  */
5084static asection mips_elf_scom_section;
5085static asymbol mips_elf_scom_symbol;
5086static asymbol *mips_elf_scom_symbol_ptr;
5087
5088/* MIPS ELF also uses an acommon section, which represents an
5089   allocated common symbol which may be overridden by a
5090   definition in a shared library.  */
5091static asection mips_elf_acom_section;
5092static asymbol mips_elf_acom_symbol;
5093static asymbol *mips_elf_acom_symbol_ptr;
5094
5095/* Handle the special MIPS section numbers that a symbol may use.
5096   This is used for both the 32-bit and the 64-bit ABI.  */
5097
5098void
5099_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
5100{
5101  elf_symbol_type *elfsym;
5102
5103  elfsym = (elf_symbol_type *) asym;
5104  switch (elfsym->internal_elf_sym.st_shndx)
5105    {
5106    case SHN_MIPS_ACOMMON:
5107      /* This section is used in a dynamically linked executable file.
5108	 It is an allocated common section.  The dynamic linker can
5109	 either resolve these symbols to something in a shared
5110	 library, or it can just leave them here.  For our purposes,
5111	 we can consider these symbols to be in a new section.  */
5112      if (mips_elf_acom_section.name == NULL)
5113	{
5114	  /* Initialize the acommon section.  */
5115	  mips_elf_acom_section.name = ".acommon";
5116	  mips_elf_acom_section.flags = SEC_ALLOC;
5117	  mips_elf_acom_section.output_section = &mips_elf_acom_section;
5118	  mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
5119	  mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
5120	  mips_elf_acom_symbol.name = ".acommon";
5121	  mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
5122	  mips_elf_acom_symbol.section = &mips_elf_acom_section;
5123	  mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
5124	}
5125      asym->section = &mips_elf_acom_section;
5126      break;
5127
5128    case SHN_COMMON:
5129      /* Common symbols less than the GP size are automatically
5130	 treated as SHN_MIPS_SCOMMON symbols on IRIX5.  */
5131      if (asym->value > elf_gp_size (abfd)
5132	  || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
5133	  || IRIX_COMPAT (abfd) == ict_irix6)
5134	break;
5135      /* Fall through.  */
5136    case SHN_MIPS_SCOMMON:
5137      if (mips_elf_scom_section.name == NULL)
5138	{
5139	  /* Initialize the small common section.  */
5140	  mips_elf_scom_section.name = ".scommon";
5141	  mips_elf_scom_section.flags = SEC_IS_COMMON;
5142	  mips_elf_scom_section.output_section = &mips_elf_scom_section;
5143	  mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
5144	  mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
5145	  mips_elf_scom_symbol.name = ".scommon";
5146	  mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
5147	  mips_elf_scom_symbol.section = &mips_elf_scom_section;
5148	  mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
5149	}
5150      asym->section = &mips_elf_scom_section;
5151      asym->value = elfsym->internal_elf_sym.st_size;
5152      break;
5153
5154    case SHN_MIPS_SUNDEFINED:
5155      asym->section = bfd_und_section_ptr;
5156      break;
5157
5158    case SHN_MIPS_TEXT:
5159      {
5160	asection *section = bfd_get_section_by_name (abfd, ".text");
5161
5162	BFD_ASSERT (SGI_COMPAT (abfd));
5163	if (section != NULL)
5164	  {
5165	    asym->section = section;
5166	    /* MIPS_TEXT is a bit special, the address is not an offset
5167	       to the base of the .text section.  So substract the section
5168	       base address to make it an offset.  */
5169	    asym->value -= section->vma;
5170	  }
5171      }
5172      break;
5173
5174    case SHN_MIPS_DATA:
5175      {
5176	asection *section = bfd_get_section_by_name (abfd, ".data");
5177
5178	BFD_ASSERT (SGI_COMPAT (abfd));
5179	if (section != NULL)
5180	  {
5181	    asym->section = section;
5182	    /* MIPS_DATA is a bit special, the address is not an offset
5183	       to the base of the .data section.  So substract the section
5184	       base address to make it an offset.  */
5185	    asym->value -= section->vma;
5186	  }
5187      }
5188      break;
5189    }
5190}
5191
5192/* Implement elf_backend_eh_frame_address_size.  This differs from
5193   the default in the way it handles EABI64.
5194
5195   EABI64 was originally specified as an LP64 ABI, and that is what
5196   -mabi=eabi normally gives on a 64-bit target.  However, gcc has
5197   historically accepted the combination of -mabi=eabi and -mlong32,
5198   and this ILP32 variation has become semi-official over time.
5199   Both forms use elf32 and have pointer-sized FDE addresses.
5200
5201   If an EABI object was generated by GCC 4.0 or above, it will have
5202   an empty .gcc_compiled_longXX section, where XX is the size of longs
5203   in bits.  Unfortunately, ILP32 objects generated by earlier compilers
5204   have no special marking to distinguish them from LP64 objects.
5205
5206   We don't want users of the official LP64 ABI to be punished for the
5207   existence of the ILP32 variant, but at the same time, we don't want
5208   to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
5209   We therefore take the following approach:
5210
5211      - If ABFD contains a .gcc_compiled_longXX section, use it to
5212        determine the pointer size.
5213
5214      - Otherwise check the type of the first relocation.  Assume that
5215        the LP64 ABI is being used if the relocation is of type R_MIPS_64.
5216
5217      - Otherwise punt.
5218
5219   The second check is enough to detect LP64 objects generated by pre-4.0
5220   compilers because, in the kind of output generated by those compilers,
5221   the first relocation will be associated with either a CIE personality
5222   routine or an FDE start address.  Furthermore, the compilers never
5223   used a special (non-pointer) encoding for this ABI.
5224
5225   Checking the relocation type should also be safe because there is no
5226   reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
5227   did so.  */
5228
5229unsigned int
5230_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
5231{
5232  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
5233    return 8;
5234  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
5235    {
5236      bfd_boolean long32_p, long64_p;
5237
5238      long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
5239      long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
5240      if (long32_p && long64_p)
5241	return 0;
5242      if (long32_p)
5243	return 4;
5244      if (long64_p)
5245	return 8;
5246
5247      if (sec->reloc_count > 0
5248	  && elf_section_data (sec)->relocs != NULL
5249	  && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
5250	      == R_MIPS_64))
5251	return 8;
5252
5253      return 0;
5254    }
5255  return 4;
5256}
5257
5258/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
5259   relocations against two unnamed section symbols to resolve to the
5260   same address.  For example, if we have code like:
5261
5262	lw	$4,%got_disp(.data)($gp)
5263	lw	$25,%got_disp(.text)($gp)
5264	jalr	$25
5265
5266   then the linker will resolve both relocations to .data and the program
5267   will jump there rather than to .text.
5268
5269   We can work around this problem by giving names to local section symbols.
5270   This is also what the MIPSpro tools do.  */
5271
5272bfd_boolean
5273_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
5274{
5275  return SGI_COMPAT (abfd);
5276}
5277
5278/* Work over a section just before writing it out.  This routine is
5279   used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
5280   sections that need the SHF_MIPS_GPREL flag by name; there has to be
5281   a better way.  */
5282
5283bfd_boolean
5284_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
5285{
5286  if (hdr->sh_type == SHT_MIPS_REGINFO
5287      && hdr->sh_size > 0)
5288    {
5289      bfd_byte buf[4];
5290
5291      BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
5292      BFD_ASSERT (hdr->contents == NULL);
5293
5294      if (bfd_seek (abfd,
5295		    hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
5296		    SEEK_SET) != 0)
5297	return FALSE;
5298      H_PUT_32 (abfd, elf_gp (abfd), buf);
5299      if (bfd_bwrite (buf, 4, abfd) != 4)
5300	return FALSE;
5301    }
5302
5303  if (hdr->sh_type == SHT_MIPS_OPTIONS
5304      && hdr->bfd_section != NULL
5305      && mips_elf_section_data (hdr->bfd_section) != NULL
5306      && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
5307    {
5308      bfd_byte *contents, *l, *lend;
5309
5310      /* We stored the section contents in the tdata field in the
5311	 set_section_contents routine.  We save the section contents
5312	 so that we don't have to read them again.
5313	 At this point we know that elf_gp is set, so we can look
5314	 through the section contents to see if there is an
5315	 ODK_REGINFO structure.  */
5316
5317      contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
5318      l = contents;
5319      lend = contents + hdr->sh_size;
5320      while (l + sizeof (Elf_External_Options) <= lend)
5321	{
5322	  Elf_Internal_Options intopt;
5323
5324	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5325					&intopt);
5326	  if (intopt.size < sizeof (Elf_External_Options))
5327	    {
5328	      (*_bfd_error_handler)
5329		(_("%B: Warning: bad `%s' option size %u smaller than its header"),
5330		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
5331	      break;
5332	    }
5333	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5334	    {
5335	      bfd_byte buf[8];
5336
5337	      if (bfd_seek (abfd,
5338			    (hdr->sh_offset
5339			     + (l - contents)
5340			     + sizeof (Elf_External_Options)
5341			     + (sizeof (Elf64_External_RegInfo) - 8)),
5342			     SEEK_SET) != 0)
5343		return FALSE;
5344	      H_PUT_64 (abfd, elf_gp (abfd), buf);
5345	      if (bfd_bwrite (buf, 8, abfd) != 8)
5346		return FALSE;
5347	    }
5348	  else if (intopt.kind == ODK_REGINFO)
5349	    {
5350	      bfd_byte buf[4];
5351
5352	      if (bfd_seek (abfd,
5353			    (hdr->sh_offset
5354			     + (l - contents)
5355			     + sizeof (Elf_External_Options)
5356			     + (sizeof (Elf32_External_RegInfo) - 4)),
5357			    SEEK_SET) != 0)
5358		return FALSE;
5359	      H_PUT_32 (abfd, elf_gp (abfd), buf);
5360	      if (bfd_bwrite (buf, 4, abfd) != 4)
5361		return FALSE;
5362	    }
5363	  l += intopt.size;
5364	}
5365    }
5366
5367  if (hdr->bfd_section != NULL)
5368    {
5369      const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
5370
5371      if (strcmp (name, ".sdata") == 0
5372	  || strcmp (name, ".lit8") == 0
5373	  || strcmp (name, ".lit4") == 0)
5374	{
5375	  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5376	  hdr->sh_type = SHT_PROGBITS;
5377	}
5378      else if (strcmp (name, ".sbss") == 0)
5379	{
5380	  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5381	  hdr->sh_type = SHT_NOBITS;
5382	}
5383      else if (strcmp (name, ".srdata") == 0)
5384	{
5385	  hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
5386	  hdr->sh_type = SHT_PROGBITS;
5387	}
5388      else if (strcmp (name, ".compact_rel") == 0)
5389	{
5390	  hdr->sh_flags = 0;
5391	  hdr->sh_type = SHT_PROGBITS;
5392	}
5393      else if (strcmp (name, ".rtproc") == 0)
5394	{
5395	  if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
5396	    {
5397	      unsigned int adjust;
5398
5399	      adjust = hdr->sh_size % hdr->sh_addralign;
5400	      if (adjust != 0)
5401		hdr->sh_size += hdr->sh_addralign - adjust;
5402	    }
5403	}
5404    }
5405
5406  return TRUE;
5407}
5408
5409/* Handle a MIPS specific section when reading an object file.  This
5410   is called when elfcode.h finds a section with an unknown type.
5411   This routine supports both the 32-bit and 64-bit ELF ABI.
5412
5413   FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
5414   how to.  */
5415
5416bfd_boolean
5417_bfd_mips_elf_section_from_shdr (bfd *abfd,
5418				 Elf_Internal_Shdr *hdr,
5419				 const char *name,
5420				 int shindex)
5421{
5422  flagword flags = 0;
5423
5424  /* There ought to be a place to keep ELF backend specific flags, but
5425     at the moment there isn't one.  We just keep track of the
5426     sections by their name, instead.  Fortunately, the ABI gives
5427     suggested names for all the MIPS specific sections, so we will
5428     probably get away with this.  */
5429  switch (hdr->sh_type)
5430    {
5431    case SHT_MIPS_LIBLIST:
5432      if (strcmp (name, ".liblist") != 0)
5433	return FALSE;
5434      break;
5435    case SHT_MIPS_MSYM:
5436      if (strcmp (name, ".msym") != 0)
5437	return FALSE;
5438      break;
5439    case SHT_MIPS_CONFLICT:
5440      if (strcmp (name, ".conflict") != 0)
5441	return FALSE;
5442      break;
5443    case SHT_MIPS_GPTAB:
5444      if (! CONST_STRNEQ (name, ".gptab."))
5445	return FALSE;
5446      break;
5447    case SHT_MIPS_UCODE:
5448      if (strcmp (name, ".ucode") != 0)
5449	return FALSE;
5450      break;
5451    case SHT_MIPS_DEBUG:
5452      if (strcmp (name, ".mdebug") != 0)
5453	return FALSE;
5454      flags = SEC_DEBUGGING;
5455      break;
5456    case SHT_MIPS_REGINFO:
5457      if (strcmp (name, ".reginfo") != 0
5458	  || hdr->sh_size != sizeof (Elf32_External_RegInfo))
5459	return FALSE;
5460      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
5461      break;
5462    case SHT_MIPS_IFACE:
5463      if (strcmp (name, ".MIPS.interfaces") != 0)
5464	return FALSE;
5465      break;
5466    case SHT_MIPS_CONTENT:
5467      if (! CONST_STRNEQ (name, ".MIPS.content"))
5468	return FALSE;
5469      break;
5470    case SHT_MIPS_OPTIONS:
5471      if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
5472	return FALSE;
5473      break;
5474    case SHT_MIPS_DWARF:
5475      if (! CONST_STRNEQ (name, ".debug_"))
5476	return FALSE;
5477      break;
5478    case SHT_MIPS_SYMBOL_LIB:
5479      if (strcmp (name, ".MIPS.symlib") != 0)
5480	return FALSE;
5481      break;
5482    case SHT_MIPS_EVENTS:
5483      if (! CONST_STRNEQ (name, ".MIPS.events")
5484	  && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
5485	return FALSE;
5486      break;
5487    default:
5488      break;
5489    }
5490
5491  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5492    return FALSE;
5493
5494  if (flags)
5495    {
5496      if (! bfd_set_section_flags (abfd, hdr->bfd_section,
5497				   (bfd_get_section_flags (abfd,
5498							   hdr->bfd_section)
5499				    | flags)))
5500	return FALSE;
5501    }
5502
5503  /* FIXME: We should record sh_info for a .gptab section.  */
5504
5505  /* For a .reginfo section, set the gp value in the tdata information
5506     from the contents of this section.  We need the gp value while
5507     processing relocs, so we just get it now.  The .reginfo section
5508     is not used in the 64-bit MIPS ELF ABI.  */
5509  if (hdr->sh_type == SHT_MIPS_REGINFO)
5510    {
5511      Elf32_External_RegInfo ext;
5512      Elf32_RegInfo s;
5513
5514      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
5515				      &ext, 0, sizeof ext))
5516	return FALSE;
5517      bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
5518      elf_gp (abfd) = s.ri_gp_value;
5519    }
5520
5521  /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
5522     set the gp value based on what we find.  We may see both
5523     SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
5524     they should agree.  */
5525  if (hdr->sh_type == SHT_MIPS_OPTIONS)
5526    {
5527      bfd_byte *contents, *l, *lend;
5528
5529      contents = bfd_malloc (hdr->sh_size);
5530      if (contents == NULL)
5531	return FALSE;
5532      if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
5533				      0, hdr->sh_size))
5534	{
5535	  free (contents);
5536	  return FALSE;
5537	}
5538      l = contents;
5539      lend = contents + hdr->sh_size;
5540      while (l + sizeof (Elf_External_Options) <= lend)
5541	{
5542	  Elf_Internal_Options intopt;
5543
5544	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5545					&intopt);
5546	  if (intopt.size < sizeof (Elf_External_Options))
5547	    {
5548	      (*_bfd_error_handler)
5549		(_("%B: Warning: bad `%s' option size %u smaller than its header"),
5550		abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
5551	      break;
5552	    }
5553	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5554	    {
5555	      Elf64_Internal_RegInfo intreg;
5556
5557	      bfd_mips_elf64_swap_reginfo_in
5558		(abfd,
5559		 ((Elf64_External_RegInfo *)
5560		  (l + sizeof (Elf_External_Options))),
5561		 &intreg);
5562	      elf_gp (abfd) = intreg.ri_gp_value;
5563	    }
5564	  else if (intopt.kind == ODK_REGINFO)
5565	    {
5566	      Elf32_RegInfo intreg;
5567
5568	      bfd_mips_elf32_swap_reginfo_in
5569		(abfd,
5570		 ((Elf32_External_RegInfo *)
5571		  (l + sizeof (Elf_External_Options))),
5572		 &intreg);
5573	      elf_gp (abfd) = intreg.ri_gp_value;
5574	    }
5575	  l += intopt.size;
5576	}
5577      free (contents);
5578    }
5579
5580  return TRUE;
5581}
5582
5583/* Set the correct type for a MIPS ELF section.  We do this by the
5584   section name, which is a hack, but ought to work.  This routine is
5585   used by both the 32-bit and the 64-bit ABI.  */
5586
5587bfd_boolean
5588_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
5589{
5590  const char *name = bfd_get_section_name (abfd, sec);
5591
5592  if (strcmp (name, ".liblist") == 0)
5593    {
5594      hdr->sh_type = SHT_MIPS_LIBLIST;
5595      hdr->sh_info = sec->size / sizeof (Elf32_Lib);
5596      /* The sh_link field is set in final_write_processing.  */
5597    }
5598  else if (strcmp (name, ".conflict") == 0)
5599    hdr->sh_type = SHT_MIPS_CONFLICT;
5600  else if (CONST_STRNEQ (name, ".gptab."))
5601    {
5602      hdr->sh_type = SHT_MIPS_GPTAB;
5603      hdr->sh_entsize = sizeof (Elf32_External_gptab);
5604      /* The sh_info field is set in final_write_processing.  */
5605    }
5606  else if (strcmp (name, ".ucode") == 0)
5607    hdr->sh_type = SHT_MIPS_UCODE;
5608  else if (strcmp (name, ".mdebug") == 0)
5609    {
5610      hdr->sh_type = SHT_MIPS_DEBUG;
5611      /* In a shared object on IRIX 5.3, the .mdebug section has an
5612         entsize of 0.  FIXME: Does this matter?  */
5613      if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
5614	hdr->sh_entsize = 0;
5615      else
5616	hdr->sh_entsize = 1;
5617    }
5618  else if (strcmp (name, ".reginfo") == 0)
5619    {
5620      hdr->sh_type = SHT_MIPS_REGINFO;
5621      /* In a shared object on IRIX 5.3, the .reginfo section has an
5622         entsize of 0x18.  FIXME: Does this matter?  */
5623      if (SGI_COMPAT (abfd))
5624	{
5625	  if ((abfd->flags & DYNAMIC) != 0)
5626	    hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5627	  else
5628	    hdr->sh_entsize = 1;
5629	}
5630      else
5631	hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5632    }
5633  else if (SGI_COMPAT (abfd)
5634	   && (strcmp (name, ".hash") == 0
5635	       || strcmp (name, ".dynamic") == 0
5636	       || strcmp (name, ".dynstr") == 0))
5637    {
5638      if (SGI_COMPAT (abfd))
5639	hdr->sh_entsize = 0;
5640#if 0
5641      /* This isn't how the IRIX6 linker behaves.  */
5642      hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
5643#endif
5644    }
5645  else if (strcmp (name, ".got") == 0
5646	   || strcmp (name, ".srdata") == 0
5647	   || strcmp (name, ".sdata") == 0
5648	   || strcmp (name, ".sbss") == 0
5649	   || strcmp (name, ".lit4") == 0
5650	   || strcmp (name, ".lit8") == 0)
5651    hdr->sh_flags |= SHF_MIPS_GPREL;
5652  else if (strcmp (name, ".MIPS.interfaces") == 0)
5653    {
5654      hdr->sh_type = SHT_MIPS_IFACE;
5655      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5656    }
5657  else if (CONST_STRNEQ (name, ".MIPS.content"))
5658    {
5659      hdr->sh_type = SHT_MIPS_CONTENT;
5660      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5661      /* The sh_info field is set in final_write_processing.  */
5662    }
5663  else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
5664    {
5665      hdr->sh_type = SHT_MIPS_OPTIONS;
5666      hdr->sh_entsize = 1;
5667      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5668    }
5669  else if (CONST_STRNEQ (name, ".debug_"))
5670    hdr->sh_type = SHT_MIPS_DWARF;
5671  else if (strcmp (name, ".MIPS.symlib") == 0)
5672    {
5673      hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
5674      /* The sh_link and sh_info fields are set in
5675         final_write_processing.  */
5676    }
5677  else if (CONST_STRNEQ (name, ".MIPS.events")
5678	   || CONST_STRNEQ (name, ".MIPS.post_rel"))
5679    {
5680      hdr->sh_type = SHT_MIPS_EVENTS;
5681      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5682      /* The sh_link field is set in final_write_processing.  */
5683    }
5684  else if (strcmp (name, ".msym") == 0)
5685    {
5686      hdr->sh_type = SHT_MIPS_MSYM;
5687      hdr->sh_flags |= SHF_ALLOC;
5688      hdr->sh_entsize = 8;
5689    }
5690
5691  /* The generic elf_fake_sections will set up REL_HDR using the default
5692   kind of relocations.  We used to set up a second header for the
5693   non-default kind of relocations here, but only NewABI would use
5694   these, and the IRIX ld doesn't like resulting empty RELA sections.
5695   Thus we create those header only on demand now.  */
5696
5697  return TRUE;
5698}
5699
5700/* Given a BFD section, try to locate the corresponding ELF section
5701   index.  This is used by both the 32-bit and the 64-bit ABI.
5702   Actually, it's not clear to me that the 64-bit ABI supports these,
5703   but for non-PIC objects we will certainly want support for at least
5704   the .scommon section.  */
5705
5706bfd_boolean
5707_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5708					asection *sec, int *retval)
5709{
5710  if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
5711    {
5712      *retval = SHN_MIPS_SCOMMON;
5713      return TRUE;
5714    }
5715  if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
5716    {
5717      *retval = SHN_MIPS_ACOMMON;
5718      return TRUE;
5719    }
5720  return FALSE;
5721}
5722
5723/* Hook called by the linker routine which adds symbols from an object
5724   file.  We must handle the special MIPS section numbers here.  */
5725
5726bfd_boolean
5727_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
5728			       Elf_Internal_Sym *sym, const char **namep,
5729			       flagword *flagsp ATTRIBUTE_UNUSED,
5730			       asection **secp, bfd_vma *valp)
5731{
5732  if (SGI_COMPAT (abfd)
5733      && (abfd->flags & DYNAMIC) != 0
5734      && strcmp (*namep, "_rld_new_interface") == 0)
5735    {
5736      /* Skip IRIX5 rld entry name.  */
5737      *namep = NULL;
5738      return TRUE;
5739    }
5740
5741  /* Shared objects may have a dynamic symbol '_gp_disp' defined as
5742     a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
5743     by setting a DT_NEEDED for the shared object.  Since _gp_disp is
5744     a magic symbol resolved by the linker, we ignore this bogus definition
5745     of _gp_disp.  New ABI objects do not suffer from this problem so this
5746     is not done for them. */
5747  if (!NEWABI_P(abfd)
5748      && (sym->st_shndx == SHN_ABS)
5749      && (strcmp (*namep, "_gp_disp") == 0))
5750    {
5751      *namep = NULL;
5752      return TRUE;
5753    }
5754
5755  switch (sym->st_shndx)
5756    {
5757    case SHN_COMMON:
5758      /* Common symbols less than the GP size are automatically
5759	 treated as SHN_MIPS_SCOMMON symbols.  */
5760      if (sym->st_size > elf_gp_size (abfd)
5761	  || ELF_ST_TYPE (sym->st_info) == STT_TLS
5762	  || IRIX_COMPAT (abfd) == ict_irix6)
5763	break;
5764      /* Fall through.  */
5765    case SHN_MIPS_SCOMMON:
5766      *secp = bfd_make_section_old_way (abfd, ".scommon");
5767      (*secp)->flags |= SEC_IS_COMMON;
5768      *valp = sym->st_size;
5769      break;
5770
5771    case SHN_MIPS_TEXT:
5772      /* This section is used in a shared object.  */
5773      if (elf_tdata (abfd)->elf_text_section == NULL)
5774	{
5775	  asymbol *elf_text_symbol;
5776	  asection *elf_text_section;
5777	  bfd_size_type amt = sizeof (asection);
5778
5779	  elf_text_section = bfd_zalloc (abfd, amt);
5780	  if (elf_text_section == NULL)
5781	    return FALSE;
5782
5783	  amt = sizeof (asymbol);
5784	  elf_text_symbol = bfd_zalloc (abfd, amt);
5785	  if (elf_text_symbol == NULL)
5786	    return FALSE;
5787
5788	  /* Initialize the section.  */
5789
5790	  elf_tdata (abfd)->elf_text_section = elf_text_section;
5791	  elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
5792
5793	  elf_text_section->symbol = elf_text_symbol;
5794	  elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
5795
5796	  elf_text_section->name = ".text";
5797	  elf_text_section->flags = SEC_NO_FLAGS;
5798	  elf_text_section->output_section = NULL;
5799	  elf_text_section->owner = abfd;
5800	  elf_text_symbol->name = ".text";
5801	  elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
5802	  elf_text_symbol->section = elf_text_section;
5803	}
5804      /* This code used to do *secp = bfd_und_section_ptr if
5805         info->shared.  I don't know why, and that doesn't make sense,
5806         so I took it out.  */
5807      *secp = elf_tdata (abfd)->elf_text_section;
5808      break;
5809
5810    case SHN_MIPS_ACOMMON:
5811      /* Fall through. XXX Can we treat this as allocated data?  */
5812    case SHN_MIPS_DATA:
5813      /* This section is used in a shared object.  */
5814      if (elf_tdata (abfd)->elf_data_section == NULL)
5815	{
5816	  asymbol *elf_data_symbol;
5817	  asection *elf_data_section;
5818	  bfd_size_type amt = sizeof (asection);
5819
5820	  elf_data_section = bfd_zalloc (abfd, amt);
5821	  if (elf_data_section == NULL)
5822	    return FALSE;
5823
5824	  amt = sizeof (asymbol);
5825	  elf_data_symbol = bfd_zalloc (abfd, amt);
5826	  if (elf_data_symbol == NULL)
5827	    return FALSE;
5828
5829	  /* Initialize the section.  */
5830
5831	  elf_tdata (abfd)->elf_data_section = elf_data_section;
5832	  elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
5833
5834	  elf_data_section->symbol = elf_data_symbol;
5835	  elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
5836
5837	  elf_data_section->name = ".data";
5838	  elf_data_section->flags = SEC_NO_FLAGS;
5839	  elf_data_section->output_section = NULL;
5840	  elf_data_section->owner = abfd;
5841	  elf_data_symbol->name = ".data";
5842	  elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
5843	  elf_data_symbol->section = elf_data_section;
5844	}
5845      /* This code used to do *secp = bfd_und_section_ptr if
5846         info->shared.  I don't know why, and that doesn't make sense,
5847         so I took it out.  */
5848      *secp = elf_tdata (abfd)->elf_data_section;
5849      break;
5850
5851    case SHN_MIPS_SUNDEFINED:
5852      *secp = bfd_und_section_ptr;
5853      break;
5854    }
5855
5856  if (SGI_COMPAT (abfd)
5857      && ! info->shared
5858      && info->hash->creator == abfd->xvec
5859      && strcmp (*namep, "__rld_obj_head") == 0)
5860    {
5861      struct elf_link_hash_entry *h;
5862      struct bfd_link_hash_entry *bh;
5863
5864      /* Mark __rld_obj_head as dynamic.  */
5865      bh = NULL;
5866      if (! (_bfd_generic_link_add_one_symbol
5867	     (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
5868	      get_elf_backend_data (abfd)->collect, &bh)))
5869	return FALSE;
5870
5871      h = (struct elf_link_hash_entry *) bh;
5872      h->non_elf = 0;
5873      h->def_regular = 1;
5874      h->type = STT_OBJECT;
5875
5876      if (! bfd_elf_link_record_dynamic_symbol (info, h))
5877	return FALSE;
5878
5879      mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
5880    }
5881
5882  /* If this is a mips16 text symbol, add 1 to the value to make it
5883     odd.  This will cause something like .word SYM to come up with
5884     the right value when it is loaded into the PC.  */
5885  if (sym->st_other == STO_MIPS16)
5886    ++*valp;
5887
5888  return TRUE;
5889}
5890
5891/* This hook function is called before the linker writes out a global
5892   symbol.  We mark symbols as small common if appropriate.  This is
5893   also where we undo the increment of the value for a mips16 symbol.  */
5894
5895bfd_boolean
5896_bfd_mips_elf_link_output_symbol_hook
5897  (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5898   const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
5899   asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
5900{
5901  /* If we see a common symbol, which implies a relocatable link, then
5902     if a symbol was small common in an input file, mark it as small
5903     common in the output file.  */
5904  if (sym->st_shndx == SHN_COMMON
5905      && strcmp (input_sec->name, ".scommon") == 0)
5906    sym->st_shndx = SHN_MIPS_SCOMMON;
5907
5908  if (sym->st_other == STO_MIPS16)
5909    sym->st_value &= ~1;
5910
5911  return TRUE;
5912}
5913
5914/* Functions for the dynamic linker.  */
5915
5916/* Create dynamic sections when linking against a dynamic object.  */
5917
5918bfd_boolean
5919_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
5920{
5921  struct elf_link_hash_entry *h;
5922  struct bfd_link_hash_entry *bh;
5923  flagword flags;
5924  register asection *s;
5925  const char * const *namep;
5926  struct mips_elf_link_hash_table *htab;
5927
5928  htab = mips_elf_hash_table (info);
5929  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5930	   | SEC_LINKER_CREATED | SEC_READONLY);
5931
5932  /* The psABI requires a read-only .dynamic section, but the VxWorks
5933     EABI doesn't.  */
5934  if (!htab->is_vxworks)
5935    {
5936      s = bfd_get_section_by_name (abfd, ".dynamic");
5937      if (s != NULL)
5938	{
5939	  if (! bfd_set_section_flags (abfd, s, flags))
5940	    return FALSE;
5941	}
5942    }
5943
5944  /* We need to create .got section.  */
5945  if (! mips_elf_create_got_section (abfd, info, FALSE))
5946    return FALSE;
5947
5948  if (! mips_elf_rel_dyn_section (info, TRUE))
5949    return FALSE;
5950
5951  /* Create .stub section.  */
5952  if (bfd_get_section_by_name (abfd,
5953			       MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
5954    {
5955      s = bfd_make_section_with_flags (abfd,
5956				       MIPS_ELF_STUB_SECTION_NAME (abfd),
5957				       flags | SEC_CODE);
5958      if (s == NULL
5959	  || ! bfd_set_section_alignment (abfd, s,
5960					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5961	return FALSE;
5962    }
5963
5964  if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
5965      && !info->shared
5966      && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5967    {
5968      s = bfd_make_section_with_flags (abfd, ".rld_map",
5969				       flags &~ (flagword) SEC_READONLY);
5970      if (s == NULL
5971	  || ! bfd_set_section_alignment (abfd, s,
5972					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5973	return FALSE;
5974    }
5975
5976  /* On IRIX5, we adjust add some additional symbols and change the
5977     alignments of several sections.  There is no ABI documentation
5978     indicating that this is necessary on IRIX6, nor any evidence that
5979     the linker takes such action.  */
5980  if (IRIX_COMPAT (abfd) == ict_irix5)
5981    {
5982      for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5983	{
5984	  bh = NULL;
5985	  if (! (_bfd_generic_link_add_one_symbol
5986		 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
5987		  NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5988	    return FALSE;
5989
5990	  h = (struct elf_link_hash_entry *) bh;
5991	  h->non_elf = 0;
5992	  h->def_regular = 1;
5993	  h->type = STT_SECTION;
5994
5995	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
5996	    return FALSE;
5997	}
5998
5999      /* We need to create a .compact_rel section.  */
6000      if (SGI_COMPAT (abfd))
6001	{
6002	  if (!mips_elf_create_compact_rel_section (abfd, info))
6003	    return FALSE;
6004	}
6005
6006      /* Change alignments of some sections.  */
6007      s = bfd_get_section_by_name (abfd, ".hash");
6008      if (s != NULL)
6009	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6010      s = bfd_get_section_by_name (abfd, ".dynsym");
6011      if (s != NULL)
6012	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6013      s = bfd_get_section_by_name (abfd, ".dynstr");
6014      if (s != NULL)
6015	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6016      s = bfd_get_section_by_name (abfd, ".reginfo");
6017      if (s != NULL)
6018	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6019      s = bfd_get_section_by_name (abfd, ".dynamic");
6020      if (s != NULL)
6021	bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6022    }
6023
6024  if (!info->shared)
6025    {
6026      const char *name;
6027
6028      name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
6029      bh = NULL;
6030      if (!(_bfd_generic_link_add_one_symbol
6031	    (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
6032	     NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6033	return FALSE;
6034
6035      h = (struct elf_link_hash_entry *) bh;
6036      h->non_elf = 0;
6037      h->def_regular = 1;
6038      h->type = STT_SECTION;
6039
6040      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6041	return FALSE;
6042
6043      if (! mips_elf_hash_table (info)->use_rld_obj_head)
6044	{
6045	  /* __rld_map is a four byte word located in the .data section
6046	     and is filled in by the rtld to contain a pointer to
6047	     the _r_debug structure. Its symbol value will be set in
6048	     _bfd_mips_elf_finish_dynamic_symbol.  */
6049	  s = bfd_get_section_by_name (abfd, ".rld_map");
6050	  BFD_ASSERT (s != NULL);
6051
6052	  name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
6053	  bh = NULL;
6054	  if (!(_bfd_generic_link_add_one_symbol
6055		(info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
6056		 get_elf_backend_data (abfd)->collect, &bh)))
6057	    return FALSE;
6058
6059	  h = (struct elf_link_hash_entry *) bh;
6060	  h->non_elf = 0;
6061	  h->def_regular = 1;
6062	  h->type = STT_OBJECT;
6063
6064	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
6065	    return FALSE;
6066	}
6067    }
6068
6069  if (htab->is_vxworks)
6070    {
6071      /* Create the .plt, .rela.plt, .dynbss and .rela.bss sections.
6072	 Also create the _PROCEDURE_LINKAGE_TABLE symbol.  */
6073      if (!_bfd_elf_create_dynamic_sections (abfd, info))
6074	return FALSE;
6075
6076      /* Cache the sections created above.  */
6077      htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
6078      htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
6079      htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
6080      htab->splt = bfd_get_section_by_name (abfd, ".plt");
6081      if (!htab->sdynbss
6082	  || (!htab->srelbss && !info->shared)
6083	  || !htab->srelplt
6084	  || !htab->splt)
6085	abort ();
6086
6087      /* Do the usual VxWorks handling.  */
6088      if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
6089	return FALSE;
6090
6091      /* Work out the PLT sizes.  */
6092      if (info->shared)
6093	{
6094	  htab->plt_header_size
6095	    = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
6096	  htab->plt_entry_size
6097	    = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
6098	}
6099      else
6100	{
6101	  htab->plt_header_size
6102	    = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
6103	  htab->plt_entry_size
6104	    = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
6105	}
6106    }
6107
6108  return TRUE;
6109}
6110
6111/* Look through the relocs for a section during the first phase, and
6112   allocate space in the global offset table.  */
6113
6114bfd_boolean
6115_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6116			    asection *sec, const Elf_Internal_Rela *relocs)
6117{
6118  const char *name;
6119  bfd *dynobj;
6120  Elf_Internal_Shdr *symtab_hdr;
6121  struct elf_link_hash_entry **sym_hashes;
6122  struct mips_got_info *g;
6123  size_t extsymoff;
6124  const Elf_Internal_Rela *rel;
6125  const Elf_Internal_Rela *rel_end;
6126  asection *sgot;
6127  asection *sreloc;
6128  const struct elf_backend_data *bed;
6129  struct mips_elf_link_hash_table *htab;
6130
6131  if (info->relocatable)
6132    return TRUE;
6133
6134  htab = mips_elf_hash_table (info);
6135  dynobj = elf_hash_table (info)->dynobj;
6136  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6137  sym_hashes = elf_sym_hashes (abfd);
6138  extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6139
6140  /* Check for the mips16 stub sections.  */
6141
6142  name = bfd_get_section_name (abfd, sec);
6143  if (FN_STUB_P (name))
6144    {
6145      unsigned long r_symndx;
6146
6147      /* Look at the relocation information to figure out which symbol
6148         this is for.  */
6149
6150      r_symndx = ELF_R_SYM (abfd, relocs->r_info);
6151
6152      if (r_symndx < extsymoff
6153	  || sym_hashes[r_symndx - extsymoff] == NULL)
6154	{
6155	  asection *o;
6156
6157	  /* This stub is for a local symbol.  This stub will only be
6158             needed if there is some relocation in this BFD, other
6159             than a 16 bit function call, which refers to this symbol.  */
6160	  for (o = abfd->sections; o != NULL; o = o->next)
6161	    {
6162	      Elf_Internal_Rela *sec_relocs;
6163	      const Elf_Internal_Rela *r, *rend;
6164
6165	      /* We can ignore stub sections when looking for relocs.  */
6166	      if ((o->flags & SEC_RELOC) == 0
6167		  || o->reloc_count == 0
6168		  || mips16_stub_section_p (abfd, o))
6169		continue;
6170
6171	      sec_relocs
6172		= _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
6173					     info->keep_memory);
6174	      if (sec_relocs == NULL)
6175		return FALSE;
6176
6177	      rend = sec_relocs + o->reloc_count;
6178	      for (r = sec_relocs; r < rend; r++)
6179		if (ELF_R_SYM (abfd, r->r_info) == r_symndx
6180		    && ELF_R_TYPE (abfd, r->r_info) != R_MIPS16_26)
6181		  break;
6182
6183	      if (elf_section_data (o)->relocs != sec_relocs)
6184		free (sec_relocs);
6185
6186	      if (r < rend)
6187		break;
6188	    }
6189
6190	  if (o == NULL)
6191	    {
6192	      /* There is no non-call reloc for this stub, so we do
6193                 not need it.  Since this function is called before
6194                 the linker maps input sections to output sections, we
6195                 can easily discard it by setting the SEC_EXCLUDE
6196                 flag.  */
6197	      sec->flags |= SEC_EXCLUDE;
6198	      return TRUE;
6199	    }
6200
6201	  /* Record this stub in an array of local symbol stubs for
6202             this BFD.  */
6203	  if (elf_tdata (abfd)->local_stubs == NULL)
6204	    {
6205	      unsigned long symcount;
6206	      asection **n;
6207	      bfd_size_type amt;
6208
6209	      if (elf_bad_symtab (abfd))
6210		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
6211	      else
6212		symcount = symtab_hdr->sh_info;
6213	      amt = symcount * sizeof (asection *);
6214	      n = bfd_zalloc (abfd, amt);
6215	      if (n == NULL)
6216		return FALSE;
6217	      elf_tdata (abfd)->local_stubs = n;
6218	    }
6219
6220	  sec->flags |= SEC_KEEP;
6221	  elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6222
6223	  /* We don't need to set mips16_stubs_seen in this case.
6224             That flag is used to see whether we need to look through
6225             the global symbol table for stubs.  We don't need to set
6226             it here, because we just have a local stub.  */
6227	}
6228      else
6229	{
6230	  struct mips_elf_link_hash_entry *h;
6231
6232	  h = ((struct mips_elf_link_hash_entry *)
6233	       sym_hashes[r_symndx - extsymoff]);
6234
6235	  while (h->root.root.type == bfd_link_hash_indirect
6236		 || h->root.root.type == bfd_link_hash_warning)
6237	    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
6238
6239	  /* H is the symbol this stub is for.  */
6240
6241	  /* If we already have an appropriate stub for this function, we
6242	     don't need another one, so we can discard this one.  Since
6243	     this function is called before the linker maps input sections
6244	     to output sections, we can easily discard it by setting the
6245	     SEC_EXCLUDE flag.  */
6246	  if (h->fn_stub != NULL)
6247	    {
6248	      sec->flags |= SEC_EXCLUDE;
6249	      return TRUE;
6250	    }
6251
6252	  sec->flags |= SEC_KEEP;
6253	  h->fn_stub = sec;
6254	  mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
6255	}
6256    }
6257  else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
6258    {
6259      unsigned long r_symndx;
6260      struct mips_elf_link_hash_entry *h;
6261      asection **loc;
6262
6263      /* Look at the relocation information to figure out which symbol
6264         this is for.  */
6265
6266      r_symndx = ELF_R_SYM (abfd, relocs->r_info);
6267
6268      if (r_symndx < extsymoff
6269	  || sym_hashes[r_symndx - extsymoff] == NULL)
6270	{
6271	  asection *o;
6272
6273