5c2e5b4f25bd9515bc3fdd23da4dbd882a5f6c0c
[platform/upstream/binutils.git] / bfd / elfxx-mips.c
1 /* MIPS-specific support for ELF
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
27
28 /* This file handles functionality common to the different MIPS ABI's.  */
29
30 #include "bfd.h"
31 #include "sysdep.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
38 /* Get the ECOFF swapping routines.  */
39 #include "coff/sym.h"
40 #include "coff/symconst.h"
41 #include "coff/ecoff.h"
42 #include "coff/mips.h"
43
44 #include "hashtab.h"
45
46 /* This structure is used to hold .got entries while estimating got
47    sizes.  */
48 struct mips_got_entry
49 {
50   /* The input bfd in which the symbol is defined.  */
51   bfd *abfd;
52   /* The index of the symbol, as stored in the relocation r_info, if
53      we have a local symbol; -1 otherwise.  */
54   long symndx;
55   union
56   {
57     /* If abfd == NULL, an address that must be stored in the got.  */
58     bfd_vma address;
59     /* If abfd != NULL && symndx != -1, the addend of the relocation
60        that should be added to the symbol value.  */
61     bfd_vma addend;
62     /* If abfd != NULL && symndx == -1, the hash table entry
63        corresponding to a global symbol in the got (or, local, if
64        h->forced_local).  */
65     struct mips_elf_link_hash_entry *h;
66   } d;
67
68   /* The TLS types included in this GOT entry (specifically, GD and
69      IE).  The GD and IE flags can be added as we encounter new
70      relocations.  LDM can also be set; it will always be alone, not
71      combined with any GD or IE flags.  An LDM GOT entry will be
72      a local symbol entry with r_symndx == 0.  */
73   unsigned char tls_type;
74
75   /* The offset from the beginning of the .got section to the entry
76      corresponding to this symbol+addend.  If it's a global symbol
77      whose offset is yet to be decided, it's going to be -1.  */
78   long gotidx;
79 };
80
81 /* This structure is used to hold .got information when linking.  */
82
83 struct mips_got_info
84 {
85   /* The global symbol in the GOT with the lowest index in the dynamic
86      symbol table.  */
87   struct elf_link_hash_entry *global_gotsym;
88   /* The number of global .got entries.  */
89   unsigned int global_gotno;
90   /* The number of .got slots used for TLS.  */
91   unsigned int tls_gotno;
92   /* The first unused TLS .got entry.  Used only during
93      mips_elf_initialize_tls_index.  */
94   unsigned int tls_assigned_gotno;
95   /* The number of local .got entries.  */
96   unsigned int local_gotno;
97   /* The number of local .got entries we have used.  */
98   unsigned int assigned_gotno;
99   /* A hash table holding members of the got.  */
100   struct htab *got_entries;
101   /* A hash table mapping input bfds to other mips_got_info.  NULL
102      unless multi-got was necessary.  */
103   struct htab *bfd2got;
104   /* In multi-got links, a pointer to the next got (err, rather, most
105      of the time, it points to the previous got).  */
106   struct mips_got_info *next;
107   /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
108      for none, or MINUS_TWO for not yet assigned.  This is needed
109      because a single-GOT link may have multiple hash table entries
110      for the LDM.  It does not get initialized in multi-GOT mode.  */
111   bfd_vma tls_ldm_offset;
112 };
113
114 /* Map an input bfd to a got in a multi-got link.  */
115
116 struct mips_elf_bfd2got_hash {
117   bfd *bfd;
118   struct mips_got_info *g;
119 };
120
121 /* Structure passed when traversing the bfd2got hash table, used to
122    create and merge bfd's gots.  */
123
124 struct mips_elf_got_per_bfd_arg
125 {
126   /* A hashtable that maps bfds to gots.  */
127   htab_t bfd2got;
128   /* The output bfd.  */
129   bfd *obfd;
130   /* The link information.  */
131   struct bfd_link_info *info;
132   /* A pointer to the primary got, i.e., the one that's going to get
133      the implicit relocations from DT_MIPS_LOCAL_GOTNO and
134      DT_MIPS_GOTSYM.  */
135   struct mips_got_info *primary;
136   /* A non-primary got we're trying to merge with other input bfd's
137      gots.  */
138   struct mips_got_info *current;
139   /* The maximum number of got entries that can be addressed with a
140      16-bit offset.  */
141   unsigned int max_count;
142   /* The number of local and global entries in the primary got.  */
143   unsigned int primary_count;
144   /* The number of local and global entries in the current got.  */
145   unsigned int current_count;
146   /* The total number of global entries which will live in the
147      primary got and be automatically relocated.  This includes
148      those not referenced by the primary GOT but included in
149      the "master" GOT.  */
150   unsigned int global_count;
151 };
152
153 /* Another structure used to pass arguments for got entries traversal.  */
154
155 struct mips_elf_set_global_got_offset_arg
156 {
157   struct mips_got_info *g;
158   int value;
159   unsigned int needed_relocs;
160   struct bfd_link_info *info;
161 };
162
163 /* A structure used to count TLS relocations or GOT entries, for GOT
164    entry or ELF symbol table traversal.  */
165
166 struct mips_elf_count_tls_arg
167 {
168   struct bfd_link_info *info;
169   unsigned int needed;
170 };
171
172 struct _mips_elf_section_data
173 {
174   struct bfd_elf_section_data elf;
175   union
176   {
177     struct mips_got_info *got_info;
178     bfd_byte *tdata;
179   } u;
180 };
181
182 #define mips_elf_section_data(sec) \
183   ((struct _mips_elf_section_data *) elf_section_data (sec))
184
185 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
186    the dynamic symbols.  */
187
188 struct mips_elf_hash_sort_data
189 {
190   /* The symbol in the global GOT with the lowest dynamic symbol table
191      index.  */
192   struct elf_link_hash_entry *low;
193   /* The least dynamic symbol table index corresponding to a non-TLS
194      symbol with a GOT entry.  */
195   long min_got_dynindx;
196   /* The greatest dynamic symbol table index corresponding to a symbol
197      with a GOT entry that is not referenced (e.g., a dynamic symbol
198      with dynamic relocations pointing to it from non-primary GOTs).  */
199   long max_unref_got_dynindx;
200   /* The greatest dynamic symbol table index not corresponding to a
201      symbol without a GOT entry.  */
202   long max_non_got_dynindx;
203 };
204
205 /* The MIPS ELF linker needs additional information for each symbol in
206    the global hash table.  */
207
208 struct mips_elf_link_hash_entry
209 {
210   struct elf_link_hash_entry root;
211
212   /* External symbol information.  */
213   EXTR esym;
214
215   /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
216      this symbol.  */
217   unsigned int possibly_dynamic_relocs;
218
219   /* If the R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 reloc is against
220      a readonly section.  */
221   bfd_boolean readonly_reloc;
222
223   /* We must not create a stub for a symbol that has relocations
224      related to taking the function's address, i.e. any but
225      R_MIPS_CALL*16 ones -- see "MIPS ABI Supplement, 3rd Edition",
226      p. 4-20.  */
227   bfd_boolean no_fn_stub;
228
229   /* If there is a stub that 32 bit functions should use to call this
230      16 bit function, this points to the section containing the stub.  */
231   asection *fn_stub;
232
233   /* Whether we need the fn_stub; this is set if this symbol appears
234      in any relocs other than a 16 bit call.  */
235   bfd_boolean need_fn_stub;
236
237   /* If there is a stub that 16 bit functions should use to call this
238      32 bit function, this points to the section containing the stub.  */
239   asection *call_stub;
240
241   /* This is like the call_stub field, but it is used if the function
242      being called returns a floating point value.  */
243   asection *call_fp_stub;
244
245   /* Are we forced local?  .*/
246   bfd_boolean forced_local;
247
248 #define GOT_NORMAL      0
249 #define GOT_TLS_GD      1
250 #define GOT_TLS_LDM     2
251 #define GOT_TLS_IE      4
252 #define GOT_TLS_OFFSET_DONE    0x40
253 #define GOT_TLS_DONE    0x80
254   unsigned char tls_type;
255   /* This is only used in single-GOT mode; in multi-GOT mode there
256      is one mips_got_entry per GOT entry, so the offset is stored
257      there.  In single-GOT mode there may be many mips_got_entry
258      structures all referring to the same GOT slot.  It might be
259      possible to use root.got.offset instead, but that field is
260      overloaded already.  */
261   bfd_vma tls_got_offset;
262 };
263
264 /* MIPS ELF linker hash table.  */
265
266 struct mips_elf_link_hash_table
267 {
268   struct elf_link_hash_table root;
269 #if 0
270   /* We no longer use this.  */
271   /* String section indices for the dynamic section symbols.  */
272   bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
273 #endif
274   /* The number of .rtproc entries.  */
275   bfd_size_type procedure_count;
276   /* The size of the .compact_rel section (if SGI_COMPAT).  */
277   bfd_size_type compact_rel_size;
278   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
279      entry is set to the address of __rld_obj_head as in IRIX5.  */
280   bfd_boolean use_rld_obj_head;
281   /* This is the value of the __rld_map or __rld_obj_head symbol.  */
282   bfd_vma rld_value;
283   /* This is set if we see any mips16 stub sections.  */
284   bfd_boolean mips16_stubs_seen;
285 };
286
287 #define TLS_RELOC_P(r_type) \
288   (r_type == R_MIPS_TLS_DTPMOD32                \
289    || r_type == R_MIPS_TLS_DTPMOD64             \
290    || r_type == R_MIPS_TLS_DTPREL32             \
291    || r_type == R_MIPS_TLS_DTPREL64             \
292    || r_type == R_MIPS_TLS_GD                   \
293    || r_type == R_MIPS_TLS_LDM                  \
294    || r_type == R_MIPS_TLS_DTPREL_HI16          \
295    || r_type == R_MIPS_TLS_DTPREL_LO16          \
296    || r_type == R_MIPS_TLS_GOTTPREL             \
297    || r_type == R_MIPS_TLS_TPREL32              \
298    || r_type == R_MIPS_TLS_TPREL64              \
299    || r_type == R_MIPS_TLS_TPREL_HI16           \
300    || r_type == R_MIPS_TLS_TPREL_LO16)
301
302 /* Structure used to pass information to mips_elf_output_extsym.  */
303
304 struct extsym_info
305 {
306   bfd *abfd;
307   struct bfd_link_info *info;
308   struct ecoff_debug_info *debug;
309   const struct ecoff_debug_swap *swap;
310   bfd_boolean failed;
311 };
312
313 /* The names of the runtime procedure table symbols used on IRIX5.  */
314
315 static const char * const mips_elf_dynsym_rtproc_names[] =
316 {
317   "_procedure_table",
318   "_procedure_string_table",
319   "_procedure_table_size",
320   NULL
321 };
322
323 /* These structures are used to generate the .compact_rel section on
324    IRIX5.  */
325
326 typedef struct
327 {
328   unsigned long id1;            /* Always one?  */
329   unsigned long num;            /* Number of compact relocation entries.  */
330   unsigned long id2;            /* Always two?  */
331   unsigned long offset;         /* The file offset of the first relocation.  */
332   unsigned long reserved0;      /* Zero?  */
333   unsigned long reserved1;      /* Zero?  */
334 } Elf32_compact_rel;
335
336 typedef struct
337 {
338   bfd_byte id1[4];
339   bfd_byte num[4];
340   bfd_byte id2[4];
341   bfd_byte offset[4];
342   bfd_byte reserved0[4];
343   bfd_byte reserved1[4];
344 } Elf32_External_compact_rel;
345
346 typedef struct
347 {
348   unsigned int ctype : 1;       /* 1: long 0: short format. See below.  */
349   unsigned int rtype : 4;       /* Relocation types. See below.  */
350   unsigned int dist2to : 8;
351   unsigned int relvaddr : 19;   /* (VADDR - vaddr of the previous entry)/ 4 */
352   unsigned long konst;          /* KONST field. See below.  */
353   unsigned long vaddr;          /* VADDR to be relocated.  */
354 } Elf32_crinfo;
355
356 typedef struct
357 {
358   unsigned int ctype : 1;       /* 1: long 0: short format. See below.  */
359   unsigned int rtype : 4;       /* Relocation types. See below.  */
360   unsigned int dist2to : 8;
361   unsigned int relvaddr : 19;   /* (VADDR - vaddr of the previous entry)/ 4 */
362   unsigned long konst;          /* KONST field. See below.  */
363 } Elf32_crinfo2;
364
365 typedef struct
366 {
367   bfd_byte info[4];
368   bfd_byte konst[4];
369   bfd_byte vaddr[4];
370 } Elf32_External_crinfo;
371
372 typedef struct
373 {
374   bfd_byte info[4];
375   bfd_byte konst[4];
376 } Elf32_External_crinfo2;
377
378 /* These are the constants used to swap the bitfields in a crinfo.  */
379
380 #define CRINFO_CTYPE (0x1)
381 #define CRINFO_CTYPE_SH (31)
382 #define CRINFO_RTYPE (0xf)
383 #define CRINFO_RTYPE_SH (27)
384 #define CRINFO_DIST2TO (0xff)
385 #define CRINFO_DIST2TO_SH (19)
386 #define CRINFO_RELVADDR (0x7ffff)
387 #define CRINFO_RELVADDR_SH (0)
388
389 /* A compact relocation info has long (3 words) or short (2 words)
390    formats.  A short format doesn't have VADDR field and relvaddr
391    fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
392 #define CRF_MIPS_LONG                   1
393 #define CRF_MIPS_SHORT                  0
394
395 /* There are 4 types of compact relocation at least. The value KONST
396    has different meaning for each type:
397
398    (type)               (konst)
399    CT_MIPS_REL32        Address in data
400    CT_MIPS_WORD         Address in word (XXX)
401    CT_MIPS_GPHI_LO      GP - vaddr
402    CT_MIPS_JMPAD        Address to jump
403    */
404
405 #define CRT_MIPS_REL32                  0xa
406 #define CRT_MIPS_WORD                   0xb
407 #define CRT_MIPS_GPHI_LO                0xc
408 #define CRT_MIPS_JMPAD                  0xd
409
410 #define mips_elf_set_cr_format(x,format)        ((x).ctype = (format))
411 #define mips_elf_set_cr_type(x,type)            ((x).rtype = (type))
412 #define mips_elf_set_cr_dist2to(x,v)            ((x).dist2to = (v))
413 #define mips_elf_set_cr_relvaddr(x,d)           ((x).relvaddr = (d)<<2)
414 \f
415 /* The structure of the runtime procedure descriptor created by the
416    loader for use by the static exception system.  */
417
418 typedef struct runtime_pdr {
419         bfd_vma adr;            /* Memory address of start of procedure.  */
420         long    regmask;        /* Save register mask.  */
421         long    regoffset;      /* Save register offset.  */
422         long    fregmask;       /* Save floating point register mask.  */
423         long    fregoffset;     /* Save floating point register offset.  */
424         long    frameoffset;    /* Frame size.  */
425         short   framereg;       /* Frame pointer register.  */
426         short   pcreg;          /* Offset or reg of return pc.  */
427         long    irpss;          /* Index into the runtime string table.  */
428         long    reserved;
429         struct exception_info *exception_info;/* Pointer to exception array.  */
430 } RPDR, *pRPDR;
431 #define cbRPDR sizeof (RPDR)
432 #define rpdNil ((pRPDR) 0)
433 \f
434 static struct mips_got_entry *mips_elf_create_local_got_entry
435   (bfd *, bfd *, struct mips_got_info *, asection *, bfd_vma, unsigned long,
436    struct mips_elf_link_hash_entry *, int);
437 static bfd_boolean mips_elf_sort_hash_table_f
438   (struct mips_elf_link_hash_entry *, void *);
439 static bfd_vma mips_elf_high
440   (bfd_vma);
441 static bfd_boolean mips_elf_stub_section_p
442   (bfd *, asection *);
443 static bfd_boolean mips_elf_create_dynamic_relocation
444   (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
445    struct mips_elf_link_hash_entry *, asection *, bfd_vma,
446    bfd_vma *, asection *);
447 static hashval_t mips_elf_got_entry_hash
448   (const void *);
449 static bfd_vma mips_elf_adjust_gp
450   (bfd *, struct mips_got_info *, bfd *);
451 static struct mips_got_info *mips_elf_got_for_ibfd
452   (struct mips_got_info *, bfd *);
453
454 /* This will be used when we sort the dynamic relocation records.  */
455 static bfd *reldyn_sorting_bfd;
456
457 /* Nonzero if ABFD is using the N32 ABI.  */
458
459 #define ABI_N32_P(abfd) \
460   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
461
462 /* Nonzero if ABFD is using the N64 ABI.  */
463 #define ABI_64_P(abfd) \
464   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
465
466 /* Nonzero if ABFD is using NewABI conventions.  */
467 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
468
469 /* The IRIX compatibility level we are striving for.  */
470 #define IRIX_COMPAT(abfd) \
471   (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
472
473 /* Whether we are trying to be compatible with IRIX at all.  */
474 #define SGI_COMPAT(abfd) \
475   (IRIX_COMPAT (abfd) != ict_none)
476
477 /* The name of the options section.  */
478 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
479   (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
480
481 /* The name of the stub section.  */
482 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
483
484 /* The size of an external REL relocation.  */
485 #define MIPS_ELF_REL_SIZE(abfd) \
486   (get_elf_backend_data (abfd)->s->sizeof_rel)
487
488 /* The size of an external dynamic table entry.  */
489 #define MIPS_ELF_DYN_SIZE(abfd) \
490   (get_elf_backend_data (abfd)->s->sizeof_dyn)
491
492 /* The size of a GOT entry.  */
493 #define MIPS_ELF_GOT_SIZE(abfd) \
494   (get_elf_backend_data (abfd)->s->arch_size / 8)
495
496 /* The size of a symbol-table entry.  */
497 #define MIPS_ELF_SYM_SIZE(abfd) \
498   (get_elf_backend_data (abfd)->s->sizeof_sym)
499
500 /* The default alignment for sections, as a power of two.  */
501 #define MIPS_ELF_LOG_FILE_ALIGN(abfd)                           \
502   (get_elf_backend_data (abfd)->s->log_file_align)
503
504 /* Get word-sized data.  */
505 #define MIPS_ELF_GET_WORD(abfd, ptr) \
506   (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
507
508 /* Put out word-sized data.  */
509 #define MIPS_ELF_PUT_WORD(abfd, val, ptr)       \
510   (ABI_64_P (abfd)                              \
511    ? bfd_put_64 (abfd, val, ptr)                \
512    : bfd_put_32 (abfd, val, ptr))
513
514 /* Add a dynamic symbol table-entry.  */
515 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)      \
516   _bfd_elf_add_dynamic_entry (info, tag, val)
517
518 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)                      \
519   (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
520
521 /* Determine whether the internal relocation of index REL_IDX is REL
522    (zero) or RELA (non-zero).  The assumption is that, if there are
523    two relocation sections for this section, one of them is REL and
524    the other is RELA.  If the index of the relocation we're testing is
525    in range for the first relocation section, check that the external
526    relocation size is that for RELA.  It is also assumed that, if
527    rel_idx is not in range for the first section, and this first
528    section contains REL relocs, then the relocation is in the second
529    section, that is RELA.  */
530 #define MIPS_RELOC_RELA_P(abfd, sec, rel_idx)                           \
531   ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr)                 \
532     * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel              \
533     > (bfd_vma)(rel_idx))                                               \
534    == (elf_section_data (sec)->rel_hdr.sh_entsize                       \
535        == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela)               \
536            : sizeof (Elf32_External_Rela))))
537
538 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
539    from smaller values.  Start with zero, widen, *then* decrement.  */
540 #define MINUS_ONE       (((bfd_vma)0) - 1)
541 #define MINUS_TWO       (((bfd_vma)0) - 2)
542
543 /* The number of local .got entries we reserve.  */
544 #define MIPS_RESERVED_GOTNO (2)
545
546 /* The offset of $gp from the beginning of the .got section.  */
547 #define ELF_MIPS_GP_OFFSET(abfd) (0x7ff0)
548
549 /* The maximum size of the GOT for it to be addressable using 16-bit
550    offsets from $gp.  */
551 #define MIPS_ELF_GOT_MAX_SIZE(abfd) (ELF_MIPS_GP_OFFSET(abfd) + 0x7fff)
552
553 /* Instructions which appear in a stub.  */
554 #define STUB_LW(abfd)                                           \
555   ((ABI_64_P (abfd)                                             \
556     ? 0xdf998010                /* ld t9,0x8010(gp) */          \
557     : 0x8f998010))              /* lw t9,0x8010(gp) */
558 #define STUB_MOVE(abfd)                                         \
559    ((ABI_64_P (abfd)                                            \
560      ? 0x03e0782d               /* daddu t7,ra */               \
561      : 0x03e07821))             /* addu t7,ra */
562 #define STUB_JALR 0x0320f809    /* jalr t9,ra */
563 #define STUB_LI16(abfd)                                         \
564   ((ABI_64_P (abfd)                                             \
565    ? 0x64180000                 /* daddiu t8,zero,0 */          \
566    : 0x24180000))               /* addiu t8,zero,0 */
567 #define MIPS_FUNCTION_STUB_SIZE (16)
568
569 /* The name of the dynamic interpreter.  This is put in the .interp
570    section.  */
571
572 #define ELF_DYNAMIC_INTERPRETER(abfd)           \
573    (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1"   \
574     : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1"  \
575     : "/usr/lib/libc.so.1")
576
577 #ifdef BFD64
578 #define MNAME(bfd,pre,pos) \
579   (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
580 #define ELF_R_SYM(bfd, i)                                       \
581   (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
582 #define ELF_R_TYPE(bfd, i)                                      \
583   (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
584 #define ELF_R_INFO(bfd, s, t)                                   \
585   (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
586 #else
587 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
588 #define ELF_R_SYM(bfd, i)                                       \
589   (ELF32_R_SYM (i))
590 #define ELF_R_TYPE(bfd, i)                                      \
591   (ELF32_R_TYPE (i))
592 #define ELF_R_INFO(bfd, s, t)                                   \
593   (ELF32_R_INFO (s, t))
594 #endif
595 \f
596   /* The mips16 compiler uses a couple of special sections to handle
597      floating point arguments.
598
599      Section names that look like .mips16.fn.FNNAME contain stubs that
600      copy floating point arguments from the fp regs to the gp regs and
601      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
602      call should be redirected to the stub instead.  If no 32 bit
603      function calls FNNAME, the stub should be discarded.  We need to
604      consider any reference to the function, not just a call, because
605      if the address of the function is taken we will need the stub,
606      since the address might be passed to a 32 bit function.
607
608      Section names that look like .mips16.call.FNNAME contain stubs
609      that copy floating point arguments from the gp regs to the fp
610      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
611      then any 16 bit function that calls FNNAME should be redirected
612      to the stub instead.  If FNNAME is not a 32 bit function, the
613      stub should be discarded.
614
615      .mips16.call.fp.FNNAME sections are similar, but contain stubs
616      which call FNNAME and then copy the return value from the fp regs
617      to the gp regs.  These stubs store the return value in $18 while
618      calling FNNAME; any function which might call one of these stubs
619      must arrange to save $18 around the call.  (This case is not
620      needed for 32 bit functions that call 16 bit functions, because
621      16 bit functions always return floating point values in both
622      $f0/$f1 and $2/$3.)
623
624      Note that in all cases FNNAME might be defined statically.
625      Therefore, FNNAME is not used literally.  Instead, the relocation
626      information will indicate which symbol the section is for.
627
628      We record any stubs that we find in the symbol table.  */
629
630 #define FN_STUB ".mips16.fn."
631 #define CALL_STUB ".mips16.call."
632 #define CALL_FP_STUB ".mips16.call.fp."
633 \f
634 /* Look up an entry in a MIPS ELF linker hash table.  */
635
636 #define mips_elf_link_hash_lookup(table, string, create, copy, follow)  \
637   ((struct mips_elf_link_hash_entry *)                                  \
638    elf_link_hash_lookup (&(table)->root, (string), (create),            \
639                          (copy), (follow)))
640
641 /* Traverse a MIPS ELF linker hash table.  */
642
643 #define mips_elf_link_hash_traverse(table, func, info)                  \
644   (elf_link_hash_traverse                                               \
645    (&(table)->root,                                                     \
646     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),    \
647     (info)))
648
649 /* Get the MIPS ELF linker hash table from a link_info structure.  */
650
651 #define mips_elf_hash_table(p) \
652   ((struct mips_elf_link_hash_table *) ((p)->hash))
653
654 /* Find the base offsets for thread-local storage in this object,
655    for GD/LD and IE/LE respectively.  */
656
657 #define TP_OFFSET 0x7000
658 #define DTP_OFFSET 0x8000
659
660 static bfd_vma
661 dtprel_base (struct bfd_link_info *info)
662 {
663   /* If tls_sec is NULL, we should have signalled an error already.  */
664   if (elf_hash_table (info)->tls_sec == NULL)
665     return 0;
666   return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
667 }
668
669 static bfd_vma
670 tprel_base (struct bfd_link_info *info)
671 {
672   /* If tls_sec is NULL, we should have signalled an error already.  */
673   if (elf_hash_table (info)->tls_sec == NULL)
674     return 0;
675   return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
676 }
677
678 /* Create an entry in a MIPS ELF linker hash table.  */
679
680 static struct bfd_hash_entry *
681 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
682                             struct bfd_hash_table *table, const char *string)
683 {
684   struct mips_elf_link_hash_entry *ret =
685     (struct mips_elf_link_hash_entry *) entry;
686
687   /* Allocate the structure if it has not already been allocated by a
688      subclass.  */
689   if (ret == NULL)
690     ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
691   if (ret == NULL)
692     return (struct bfd_hash_entry *) ret;
693
694   /* Call the allocation method of the superclass.  */
695   ret = ((struct mips_elf_link_hash_entry *)
696          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
697                                      table, string));
698   if (ret != NULL)
699     {
700       /* Set local fields.  */
701       memset (&ret->esym, 0, sizeof (EXTR));
702       /* We use -2 as a marker to indicate that the information has
703          not been set.  -1 means there is no associated ifd.  */
704       ret->esym.ifd = -2;
705       ret->possibly_dynamic_relocs = 0;
706       ret->readonly_reloc = FALSE;
707       ret->no_fn_stub = FALSE;
708       ret->fn_stub = NULL;
709       ret->need_fn_stub = FALSE;
710       ret->call_stub = NULL;
711       ret->call_fp_stub = NULL;
712       ret->forced_local = FALSE;
713       ret->tls_type = GOT_NORMAL;
714     }
715
716   return (struct bfd_hash_entry *) ret;
717 }
718
719 bfd_boolean
720 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
721 {
722   struct _mips_elf_section_data *sdata;
723   bfd_size_type amt = sizeof (*sdata);
724
725   sdata = bfd_zalloc (abfd, amt);
726   if (sdata == NULL)
727     return FALSE;
728   sec->used_by_bfd = sdata;
729
730   return _bfd_elf_new_section_hook (abfd, sec);
731 }
732 \f
733 /* Read ECOFF debugging information from a .mdebug section into a
734    ecoff_debug_info structure.  */
735
736 bfd_boolean
737 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
738                                struct ecoff_debug_info *debug)
739 {
740   HDRR *symhdr;
741   const struct ecoff_debug_swap *swap;
742   char *ext_hdr;
743
744   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
745   memset (debug, 0, sizeof (*debug));
746
747   ext_hdr = bfd_malloc (swap->external_hdr_size);
748   if (ext_hdr == NULL && swap->external_hdr_size != 0)
749     goto error_return;
750
751   if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
752                                   swap->external_hdr_size))
753     goto error_return;
754
755   symhdr = &debug->symbolic_header;
756   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
757
758   /* The symbolic header contains absolute file offsets and sizes to
759      read.  */
760 #define READ(ptr, offset, count, size, type)                            \
761   if (symhdr->count == 0)                                               \
762     debug->ptr = NULL;                                                  \
763   else                                                                  \
764     {                                                                   \
765       bfd_size_type amt = (bfd_size_type) size * symhdr->count;         \
766       debug->ptr = bfd_malloc (amt);                                    \
767       if (debug->ptr == NULL)                                           \
768         goto error_return;                                              \
769       if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0                \
770           || bfd_bread (debug->ptr, amt, abfd) != amt)                  \
771         goto error_return;                                              \
772     }
773
774   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
775   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
776   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
777   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
778   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
779   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
780         union aux_ext *);
781   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
782   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
783   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
784   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
785   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
786 #undef READ
787
788   debug->fdr = NULL;
789
790   return TRUE;
791
792  error_return:
793   if (ext_hdr != NULL)
794     free (ext_hdr);
795   if (debug->line != NULL)
796     free (debug->line);
797   if (debug->external_dnr != NULL)
798     free (debug->external_dnr);
799   if (debug->external_pdr != NULL)
800     free (debug->external_pdr);
801   if (debug->external_sym != NULL)
802     free (debug->external_sym);
803   if (debug->external_opt != NULL)
804     free (debug->external_opt);
805   if (debug->external_aux != NULL)
806     free (debug->external_aux);
807   if (debug->ss != NULL)
808     free (debug->ss);
809   if (debug->ssext != NULL)
810     free (debug->ssext);
811   if (debug->external_fdr != NULL)
812     free (debug->external_fdr);
813   if (debug->external_rfd != NULL)
814     free (debug->external_rfd);
815   if (debug->external_ext != NULL)
816     free (debug->external_ext);
817   return FALSE;
818 }
819 \f
820 /* Swap RPDR (runtime procedure table entry) for output.  */
821
822 static void
823 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
824 {
825   H_PUT_S32 (abfd, in->adr, ex->p_adr);
826   H_PUT_32 (abfd, in->regmask, ex->p_regmask);
827   H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
828   H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
829   H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
830   H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
831
832   H_PUT_16 (abfd, in->framereg, ex->p_framereg);
833   H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
834
835   H_PUT_32 (abfd, in->irpss, ex->p_irpss);
836 }
837
838 /* Create a runtime procedure table from the .mdebug section.  */
839
840 static bfd_boolean
841 mips_elf_create_procedure_table (void *handle, bfd *abfd,
842                                  struct bfd_link_info *info, asection *s,
843                                  struct ecoff_debug_info *debug)
844 {
845   const struct ecoff_debug_swap *swap;
846   HDRR *hdr = &debug->symbolic_header;
847   RPDR *rpdr, *rp;
848   struct rpdr_ext *erp;
849   void *rtproc;
850   struct pdr_ext *epdr;
851   struct sym_ext *esym;
852   char *ss, **sv;
853   char *str;
854   bfd_size_type size;
855   bfd_size_type count;
856   unsigned long sindex;
857   unsigned long i;
858   PDR pdr;
859   SYMR sym;
860   const char *no_name_func = _("static procedure (no name)");
861
862   epdr = NULL;
863   rpdr = NULL;
864   esym = NULL;
865   ss = NULL;
866   sv = NULL;
867
868   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
869
870   sindex = strlen (no_name_func) + 1;
871   count = hdr->ipdMax;
872   if (count > 0)
873     {
874       size = swap->external_pdr_size;
875
876       epdr = bfd_malloc (size * count);
877       if (epdr == NULL)
878         goto error_return;
879
880       if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
881         goto error_return;
882
883       size = sizeof (RPDR);
884       rp = rpdr = bfd_malloc (size * count);
885       if (rpdr == NULL)
886         goto error_return;
887
888       size = sizeof (char *);
889       sv = bfd_malloc (size * count);
890       if (sv == NULL)
891         goto error_return;
892
893       count = hdr->isymMax;
894       size = swap->external_sym_size;
895       esym = bfd_malloc (size * count);
896       if (esym == NULL)
897         goto error_return;
898
899       if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
900         goto error_return;
901
902       count = hdr->issMax;
903       ss = bfd_malloc (count);
904       if (ss == NULL)
905         goto error_return;
906       if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
907         goto error_return;
908
909       count = hdr->ipdMax;
910       for (i = 0; i < (unsigned long) count; i++, rp++)
911         {
912           (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
913           (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
914           rp->adr = sym.value;
915           rp->regmask = pdr.regmask;
916           rp->regoffset = pdr.regoffset;
917           rp->fregmask = pdr.fregmask;
918           rp->fregoffset = pdr.fregoffset;
919           rp->frameoffset = pdr.frameoffset;
920           rp->framereg = pdr.framereg;
921           rp->pcreg = pdr.pcreg;
922           rp->irpss = sindex;
923           sv[i] = ss + sym.iss;
924           sindex += strlen (sv[i]) + 1;
925         }
926     }
927
928   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
929   size = BFD_ALIGN (size, 16);
930   rtproc = bfd_alloc (abfd, size);
931   if (rtproc == NULL)
932     {
933       mips_elf_hash_table (info)->procedure_count = 0;
934       goto error_return;
935     }
936
937   mips_elf_hash_table (info)->procedure_count = count + 2;
938
939   erp = rtproc;
940   memset (erp, 0, sizeof (struct rpdr_ext));
941   erp++;
942   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
943   strcpy (str, no_name_func);
944   str += strlen (no_name_func) + 1;
945   for (i = 0; i < count; i++)
946     {
947       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
948       strcpy (str, sv[i]);
949       str += strlen (sv[i]) + 1;
950     }
951   H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
952
953   /* Set the size and contents of .rtproc section.  */
954   s->size = size;
955   s->contents = rtproc;
956
957   /* Skip this section later on (I don't think this currently
958      matters, but someday it might).  */
959   s->link_order_head = NULL;
960
961   if (epdr != NULL)
962     free (epdr);
963   if (rpdr != NULL)
964     free (rpdr);
965   if (esym != NULL)
966     free (esym);
967   if (ss != NULL)
968     free (ss);
969   if (sv != NULL)
970     free (sv);
971
972   return TRUE;
973
974  error_return:
975   if (epdr != NULL)
976     free (epdr);
977   if (rpdr != NULL)
978     free (rpdr);
979   if (esym != NULL)
980     free (esym);
981   if (ss != NULL)
982     free (ss);
983   if (sv != NULL)
984     free (sv);
985   return FALSE;
986 }
987
988 /* Check the mips16 stubs for a particular symbol, and see if we can
989    discard them.  */
990
991 static bfd_boolean
992 mips_elf_check_mips16_stubs (struct mips_elf_link_hash_entry *h,
993                              void *data ATTRIBUTE_UNUSED)
994 {
995   if (h->root.root.type == bfd_link_hash_warning)
996     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
997
998   if (h->fn_stub != NULL
999       && ! h->need_fn_stub)
1000     {
1001       /* We don't need the fn_stub; the only references to this symbol
1002          are 16 bit calls.  Clobber the size to 0 to prevent it from
1003          being included in the link.  */
1004       h->fn_stub->size = 0;
1005       h->fn_stub->flags &= ~SEC_RELOC;
1006       h->fn_stub->reloc_count = 0;
1007       h->fn_stub->flags |= SEC_EXCLUDE;
1008     }
1009
1010   if (h->call_stub != NULL
1011       && h->root.other == STO_MIPS16)
1012     {
1013       /* We don't need the call_stub; this is a 16 bit function, so
1014          calls from other 16 bit functions are OK.  Clobber the size
1015          to 0 to prevent it from being included in the link.  */
1016       h->call_stub->size = 0;
1017       h->call_stub->flags &= ~SEC_RELOC;
1018       h->call_stub->reloc_count = 0;
1019       h->call_stub->flags |= SEC_EXCLUDE;
1020     }
1021
1022   if (h->call_fp_stub != NULL
1023       && h->root.other == STO_MIPS16)
1024     {
1025       /* We don't need the call_stub; this is a 16 bit function, so
1026          calls from other 16 bit functions are OK.  Clobber the size
1027          to 0 to prevent it from being included in the link.  */
1028       h->call_fp_stub->size = 0;
1029       h->call_fp_stub->flags &= ~SEC_RELOC;
1030       h->call_fp_stub->reloc_count = 0;
1031       h->call_fp_stub->flags |= SEC_EXCLUDE;
1032     }
1033
1034   return TRUE;
1035 }
1036 \f
1037 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1038    Most mips16 instructions are 16 bits, but these instructions
1039    are 32 bits.
1040
1041    The format of these instructions is:
1042
1043    +--------------+--------------------------------+
1044    |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
1045    +--------------+--------------------------------+
1046    |                Immediate  15:0                |
1047    +-----------------------------------------------+
1048
1049    JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
1050    Note that the immediate value in the first word is swapped.
1051
1052    When producing a relocatable object file, R_MIPS16_26 is
1053    handled mostly like R_MIPS_26.  In particular, the addend is
1054    stored as a straight 26-bit value in a 32-bit instruction.
1055    (gas makes life simpler for itself by never adjusting a
1056    R_MIPS16_26 reloc to be against a section, so the addend is
1057    always zero).  However, the 32 bit instruction is stored as 2
1058    16-bit values, rather than a single 32-bit value.  In a
1059    big-endian file, the result is the same; in a little-endian
1060    file, the two 16-bit halves of the 32 bit value are swapped.
1061    This is so that a disassembler can recognize the jal
1062    instruction.
1063
1064    When doing a final link, R_MIPS16_26 is treated as a 32 bit
1065    instruction stored as two 16-bit values.  The addend A is the
1066    contents of the targ26 field.  The calculation is the same as
1067    R_MIPS_26.  When storing the calculated value, reorder the
1068    immediate value as shown above, and don't forget to store the
1069    value as two 16-bit values.
1070
1071    To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1072    defined as
1073
1074    big-endian:
1075    +--------+----------------------+
1076    |        |                      |
1077    |        |    targ26-16         |
1078    |31    26|25                   0|
1079    +--------+----------------------+
1080
1081    little-endian:
1082    +----------+------+-------------+
1083    |          |      |             |
1084    |  sub1    |      |     sub2    |
1085    |0        9|10  15|16         31|
1086    +----------+--------------------+
1087    where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1088    ((sub1 << 16) | sub2)).
1089
1090    When producing a relocatable object file, the calculation is
1091    (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1092    When producing a fully linked file, the calculation is
1093    let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1094    ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1095
1096    R_MIPS16_GPREL is used for GP-relative addressing in mips16
1097    mode.  A typical instruction will have a format like this:
1098
1099    +--------------+--------------------------------+
1100    |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
1101    +--------------+--------------------------------+
1102    |    Major     |   rx   |   ry   |   Imm  4:0   |
1103    +--------------+--------------------------------+
1104
1105    EXTEND is the five bit value 11110.  Major is the instruction
1106    opcode.
1107
1108    This is handled exactly like R_MIPS_GPREL16, except that the
1109    addend is retrieved and stored as shown in this diagram; that
1110    is, the Imm fields above replace the V-rel16 field.
1111
1112    All we need to do here is shuffle the bits appropriately.  As
1113    above, the two 16-bit halves must be swapped on a
1114    little-endian system.
1115
1116    R_MIPS16_HI16 and R_MIPS16_LO16 are used in mips16 mode to
1117    access data when neither GP-relative nor PC-relative addressing
1118    can be used.  They are handled like R_MIPS_HI16 and R_MIPS_LO16,
1119    except that the addend is retrieved and stored as shown above
1120    for R_MIPS16_GPREL.
1121   */
1122 void
1123 _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1124                                  bfd_boolean jal_shuffle, bfd_byte *data)
1125 {
1126   bfd_vma extend, insn, val;
1127
1128   if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
1129       && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
1130     return;
1131
1132   /* Pick up the mips16 extend instruction and the real instruction.  */
1133   extend = bfd_get_16 (abfd, data);
1134   insn = bfd_get_16 (abfd, data + 2);
1135   if (r_type == R_MIPS16_26)
1136     {
1137       if (jal_shuffle)
1138         val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1139               | ((extend & 0x1f) << 21) | insn;
1140       else
1141         val = extend << 16 | insn;
1142     }
1143   else
1144     val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1145           | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1146   bfd_put_32 (abfd, val, data);
1147 }
1148
1149 void
1150 _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1151                                bfd_boolean jal_shuffle, bfd_byte *data)
1152 {
1153   bfd_vma extend, insn, val;
1154
1155   if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
1156       && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
1157     return;
1158
1159   val = bfd_get_32 (abfd, data);
1160   if (r_type == R_MIPS16_26)
1161     {
1162       if (jal_shuffle)
1163         {
1164           insn = val & 0xffff;
1165           extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1166                    | ((val >> 21) & 0x1f);
1167         }
1168       else
1169         {
1170           insn = val & 0xffff;
1171           extend = val >> 16;
1172         }
1173     }
1174   else
1175     {
1176       insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1177       extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1178     }
1179   bfd_put_16 (abfd, insn, data + 2);
1180   bfd_put_16 (abfd, extend, data);
1181 }
1182
1183 bfd_reloc_status_type
1184 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1185                                arelent *reloc_entry, asection *input_section,
1186                                bfd_boolean relocatable, void *data, bfd_vma gp)
1187 {
1188   bfd_vma relocation;
1189   bfd_signed_vma val;
1190   bfd_reloc_status_type status;
1191
1192   if (bfd_is_com_section (symbol->section))
1193     relocation = 0;
1194   else
1195     relocation = symbol->value;
1196
1197   relocation += symbol->section->output_section->vma;
1198   relocation += symbol->section->output_offset;
1199
1200   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1201     return bfd_reloc_outofrange;
1202
1203   /* Set val to the offset into the section or symbol.  */
1204   val = reloc_entry->addend;
1205
1206   _bfd_mips_elf_sign_extend (val, 16);
1207
1208   /* Adjust val for the final section location and GP value.  If we
1209      are producing relocatable output, we don't want to do this for
1210      an external symbol.  */
1211   if (! relocatable
1212       || (symbol->flags & BSF_SECTION_SYM) != 0)
1213     val += relocation - gp;
1214
1215   if (reloc_entry->howto->partial_inplace)
1216     {
1217       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1218                                        (bfd_byte *) data
1219                                        + reloc_entry->address);
1220       if (status != bfd_reloc_ok)
1221         return status;
1222     }
1223   else
1224     reloc_entry->addend = val;
1225
1226   if (relocatable)
1227     reloc_entry->address += input_section->output_offset;
1228
1229   return bfd_reloc_ok;
1230 }
1231
1232 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1233    R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
1234    that contains the relocation field and DATA points to the start of
1235    INPUT_SECTION.  */
1236
1237 struct mips_hi16
1238 {
1239   struct mips_hi16 *next;
1240   bfd_byte *data;
1241   asection *input_section;
1242   arelent rel;
1243 };
1244
1245 /* FIXME: This should not be a static variable.  */
1246
1247 static struct mips_hi16 *mips_hi16_list;
1248
1249 /* A howto special_function for REL *HI16 relocations.  We can only
1250    calculate the correct value once we've seen the partnering
1251    *LO16 relocation, so just save the information for later.
1252
1253    The ABI requires that the *LO16 immediately follow the *HI16.
1254    However, as a GNU extension, we permit an arbitrary number of
1255    *HI16s to be associated with a single *LO16.  This significantly
1256    simplies the relocation handling in gcc.  */
1257
1258 bfd_reloc_status_type
1259 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1260                           asymbol *symbol ATTRIBUTE_UNUSED, void *data,
1261                           asection *input_section, bfd *output_bfd,
1262                           char **error_message ATTRIBUTE_UNUSED)
1263 {
1264   struct mips_hi16 *n;
1265
1266   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1267     return bfd_reloc_outofrange;
1268
1269   n = bfd_malloc (sizeof *n);
1270   if (n == NULL)
1271     return bfd_reloc_outofrange;
1272
1273   n->next = mips_hi16_list;
1274   n->data = data;
1275   n->input_section = input_section;
1276   n->rel = *reloc_entry;
1277   mips_hi16_list = n;
1278
1279   if (output_bfd != NULL)
1280     reloc_entry->address += input_section->output_offset;
1281
1282   return bfd_reloc_ok;
1283 }
1284
1285 /* A howto special_function for REL R_MIPS_GOT16 relocations.  This is just
1286    like any other 16-bit relocation when applied to global symbols, but is
1287    treated in the same as R_MIPS_HI16 when applied to local symbols.  */
1288
1289 bfd_reloc_status_type
1290 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1291                            void *data, asection *input_section,
1292                            bfd *output_bfd, char **error_message)
1293 {
1294   if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1295       || bfd_is_und_section (bfd_get_section (symbol))
1296       || bfd_is_com_section (bfd_get_section (symbol)))
1297     /* The relocation is against a global symbol.  */
1298     return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1299                                         input_section, output_bfd,
1300                                         error_message);
1301
1302   return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1303                                    input_section, output_bfd, error_message);
1304 }
1305
1306 /* A howto special_function for REL *LO16 relocations.  The *LO16 itself
1307    is a straightforward 16 bit inplace relocation, but we must deal with
1308    any partnering high-part relocations as well.  */
1309
1310 bfd_reloc_status_type
1311 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1312                           void *data, asection *input_section,
1313                           bfd *output_bfd, char **error_message)
1314 {
1315   bfd_vma vallo;
1316   bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1317
1318   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1319     return bfd_reloc_outofrange;
1320
1321   _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1322                                    location);
1323   vallo = bfd_get_32 (abfd, location);
1324   _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1325                                  location);
1326
1327   while (mips_hi16_list != NULL)
1328     {
1329       bfd_reloc_status_type ret;
1330       struct mips_hi16 *hi;
1331
1332       hi = mips_hi16_list;
1333
1334       /* R_MIPS_GOT16 relocations are something of a special case.  We
1335          want to install the addend in the same way as for a R_MIPS_HI16
1336          relocation (with a rightshift of 16).  However, since GOT16
1337          relocations can also be used with global symbols, their howto
1338          has a rightshift of 0.  */
1339       if (hi->rel.howto->type == R_MIPS_GOT16)
1340         hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
1341
1342       /* VALLO is a signed 16-bit number.  Bias it by 0x8000 so that any
1343          carry or borrow will induce a change of +1 or -1 in the high part.  */
1344       hi->rel.addend += (vallo + 0x8000) & 0xffff;
1345
1346       ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
1347                                          hi->input_section, output_bfd,
1348                                          error_message);
1349       if (ret != bfd_reloc_ok)
1350         return ret;
1351
1352       mips_hi16_list = hi->next;
1353       free (hi);
1354     }
1355
1356   return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1357                                       input_section, output_bfd,
1358                                       error_message);
1359 }
1360
1361 /* A generic howto special_function.  This calculates and installs the
1362    relocation itself, thus avoiding the oft-discussed problems in
1363    bfd_perform_relocation and bfd_install_relocation.  */
1364
1365 bfd_reloc_status_type
1366 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1367                              asymbol *symbol, void *data ATTRIBUTE_UNUSED,
1368                              asection *input_section, bfd *output_bfd,
1369                              char **error_message ATTRIBUTE_UNUSED)
1370 {
1371   bfd_signed_vma val;
1372   bfd_reloc_status_type status;
1373   bfd_boolean relocatable;
1374
1375   relocatable = (output_bfd != NULL);
1376
1377   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1378     return bfd_reloc_outofrange;
1379
1380   /* Build up the field adjustment in VAL.  */
1381   val = 0;
1382   if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
1383     {
1384       /* Either we're calculating the final field value or we have a
1385          relocation against a section symbol.  Add in the section's
1386          offset or address.  */
1387       val += symbol->section->output_section->vma;
1388       val += symbol->section->output_offset;
1389     }
1390
1391   if (!relocatable)
1392     {
1393       /* We're calculating the final field value.  Add in the symbol's value
1394          and, if pc-relative, subtract the address of the field itself.  */
1395       val += symbol->value;
1396       if (reloc_entry->howto->pc_relative)
1397         {
1398           val -= input_section->output_section->vma;
1399           val -= input_section->output_offset;
1400           val -= reloc_entry->address;
1401         }
1402     }
1403
1404   /* VAL is now the final adjustment.  If we're keeping this relocation
1405      in the output file, and if the relocation uses a separate addend,
1406      we just need to add VAL to that addend.  Otherwise we need to add
1407      VAL to the relocation field itself.  */
1408   if (relocatable && !reloc_entry->howto->partial_inplace)
1409     reloc_entry->addend += val;
1410   else
1411     {
1412       bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1413
1414       /* Add in the separate addend, if any.  */
1415       val += reloc_entry->addend;
1416
1417       /* Add VAL to the relocation field.  */
1418       _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1419                                        location);
1420       status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1421                                        location);
1422       _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1423                                      location);
1424
1425       if (status != bfd_reloc_ok)
1426         return status;
1427     }
1428
1429   if (relocatable)
1430     reloc_entry->address += input_section->output_offset;
1431
1432   return bfd_reloc_ok;
1433 }
1434 \f
1435 /* Swap an entry in a .gptab section.  Note that these routines rely
1436    on the equivalence of the two elements of the union.  */
1437
1438 static void
1439 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
1440                               Elf32_gptab *in)
1441 {
1442   in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
1443   in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
1444 }
1445
1446 static void
1447 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
1448                                Elf32_External_gptab *ex)
1449 {
1450   H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
1451   H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
1452 }
1453
1454 static void
1455 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
1456                                 Elf32_External_compact_rel *ex)
1457 {
1458   H_PUT_32 (abfd, in->id1, ex->id1);
1459   H_PUT_32 (abfd, in->num, ex->num);
1460   H_PUT_32 (abfd, in->id2, ex->id2);
1461   H_PUT_32 (abfd, in->offset, ex->offset);
1462   H_PUT_32 (abfd, in->reserved0, ex->reserved0);
1463   H_PUT_32 (abfd, in->reserved1, ex->reserved1);
1464 }
1465
1466 static void
1467 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
1468                            Elf32_External_crinfo *ex)
1469 {
1470   unsigned long l;
1471
1472   l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1473        | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1474        | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1475        | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1476   H_PUT_32 (abfd, l, ex->info);
1477   H_PUT_32 (abfd, in->konst, ex->konst);
1478   H_PUT_32 (abfd, in->vaddr, ex->vaddr);
1479 }
1480 \f
1481 /* A .reginfo section holds a single Elf32_RegInfo structure.  These
1482    routines swap this structure in and out.  They are used outside of
1483    BFD, so they are globally visible.  */
1484
1485 void
1486 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
1487                                 Elf32_RegInfo *in)
1488 {
1489   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1490   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1491   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1492   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1493   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1494   in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
1495 }
1496
1497 void
1498 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
1499                                  Elf32_External_RegInfo *ex)
1500 {
1501   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1502   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1503   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1504   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1505   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1506   H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
1507 }
1508
1509 /* In the 64 bit ABI, the .MIPS.options section holds register
1510    information in an Elf64_Reginfo structure.  These routines swap
1511    them in and out.  They are globally visible because they are used
1512    outside of BFD.  These routines are here so that gas can call them
1513    without worrying about whether the 64 bit ABI has been included.  */
1514
1515 void
1516 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
1517                                 Elf64_Internal_RegInfo *in)
1518 {
1519   in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1520   in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
1521   in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1522   in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1523   in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1524   in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1525   in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
1526 }
1527
1528 void
1529 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
1530                                  Elf64_External_RegInfo *ex)
1531 {
1532   H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1533   H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
1534   H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1535   H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1536   H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1537   H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1538   H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
1539 }
1540
1541 /* Swap in an options header.  */
1542
1543 void
1544 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
1545                               Elf_Internal_Options *in)
1546 {
1547   in->kind = H_GET_8 (abfd, ex->kind);
1548   in->size = H_GET_8 (abfd, ex->size);
1549   in->section = H_GET_16 (abfd, ex->section);
1550   in->info = H_GET_32 (abfd, ex->info);
1551 }
1552
1553 /* Swap out an options header.  */
1554
1555 void
1556 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
1557                                Elf_External_Options *ex)
1558 {
1559   H_PUT_8 (abfd, in->kind, ex->kind);
1560   H_PUT_8 (abfd, in->size, ex->size);
1561   H_PUT_16 (abfd, in->section, ex->section);
1562   H_PUT_32 (abfd, in->info, ex->info);
1563 }
1564 \f
1565 /* This function is called via qsort() to sort the dynamic relocation
1566    entries by increasing r_symndx value.  */
1567
1568 static int
1569 sort_dynamic_relocs (const void *arg1, const void *arg2)
1570 {
1571   Elf_Internal_Rela int_reloc1;
1572   Elf_Internal_Rela int_reloc2;
1573
1574   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
1575   bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
1576
1577   return ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
1578 }
1579
1580 /* Like sort_dynamic_relocs, but used for elf64 relocations.  */
1581
1582 static int
1583 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
1584                         const void *arg2 ATTRIBUTE_UNUSED)
1585 {
1586 #ifdef BFD64
1587   Elf_Internal_Rela int_reloc1[3];
1588   Elf_Internal_Rela int_reloc2[3];
1589
1590   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1591     (reldyn_sorting_bfd, arg1, int_reloc1);
1592   (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1593     (reldyn_sorting_bfd, arg2, int_reloc2);
1594
1595   return (ELF64_R_SYM (int_reloc1[0].r_info)
1596           - ELF64_R_SYM (int_reloc2[0].r_info));
1597 #else
1598   abort ();
1599 #endif
1600 }
1601
1602
1603 /* This routine is used to write out ECOFF debugging external symbol
1604    information.  It is called via mips_elf_link_hash_traverse.  The
1605    ECOFF external symbol information must match the ELF external
1606    symbol information.  Unfortunately, at this point we don't know
1607    whether a symbol is required by reloc information, so the two
1608    tables may wind up being different.  We must sort out the external
1609    symbol information before we can set the final size of the .mdebug
1610    section, and we must set the size of the .mdebug section before we
1611    can relocate any sections, and we can't know which symbols are
1612    required by relocation until we relocate the sections.
1613    Fortunately, it is relatively unlikely that any symbol will be
1614    stripped but required by a reloc.  In particular, it can not happen
1615    when generating a final executable.  */
1616
1617 static bfd_boolean
1618 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
1619 {
1620   struct extsym_info *einfo = data;
1621   bfd_boolean strip;
1622   asection *sec, *output_section;
1623
1624   if (h->root.root.type == bfd_link_hash_warning)
1625     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1626
1627   if (h->root.indx == -2)
1628     strip = FALSE;
1629   else if ((h->root.def_dynamic
1630             || h->root.ref_dynamic
1631             || h->root.type == bfd_link_hash_new)
1632            && !h->root.def_regular
1633            && !h->root.ref_regular)
1634     strip = TRUE;
1635   else if (einfo->info->strip == strip_all
1636            || (einfo->info->strip == strip_some
1637                && bfd_hash_lookup (einfo->info->keep_hash,
1638                                    h->root.root.root.string,
1639                                    FALSE, FALSE) == NULL))
1640     strip = TRUE;
1641   else
1642     strip = FALSE;
1643
1644   if (strip)
1645     return TRUE;
1646
1647   if (h->esym.ifd == -2)
1648     {
1649       h->esym.jmptbl = 0;
1650       h->esym.cobol_main = 0;
1651       h->esym.weakext = 0;
1652       h->esym.reserved = 0;
1653       h->esym.ifd = ifdNil;
1654       h->esym.asym.value = 0;
1655       h->esym.asym.st = stGlobal;
1656
1657       if (h->root.root.type == bfd_link_hash_undefined
1658           || h->root.root.type == bfd_link_hash_undefweak)
1659         {
1660           const char *name;
1661
1662           /* Use undefined class.  Also, set class and type for some
1663              special symbols.  */
1664           name = h->root.root.root.string;
1665           if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
1666               || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
1667             {
1668               h->esym.asym.sc = scData;
1669               h->esym.asym.st = stLabel;
1670               h->esym.asym.value = 0;
1671             }
1672           else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
1673             {
1674               h->esym.asym.sc = scAbs;
1675               h->esym.asym.st = stLabel;
1676               h->esym.asym.value =
1677                 mips_elf_hash_table (einfo->info)->procedure_count;
1678             }
1679           else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
1680             {
1681               h->esym.asym.sc = scAbs;
1682               h->esym.asym.st = stLabel;
1683               h->esym.asym.value = elf_gp (einfo->abfd);
1684             }
1685           else
1686             h->esym.asym.sc = scUndefined;
1687         }
1688       else if (h->root.root.type != bfd_link_hash_defined
1689           && h->root.root.type != bfd_link_hash_defweak)
1690         h->esym.asym.sc = scAbs;
1691       else
1692         {
1693           const char *name;
1694
1695           sec = h->root.root.u.def.section;
1696           output_section = sec->output_section;
1697
1698           /* When making a shared library and symbol h is the one from
1699              the another shared library, OUTPUT_SECTION may be null.  */
1700           if (output_section == NULL)
1701             h->esym.asym.sc = scUndefined;
1702           else
1703             {
1704               name = bfd_section_name (output_section->owner, output_section);
1705
1706               if (strcmp (name, ".text") == 0)
1707                 h->esym.asym.sc = scText;
1708               else if (strcmp (name, ".data") == 0)
1709                 h->esym.asym.sc = scData;
1710               else if (strcmp (name, ".sdata") == 0)
1711                 h->esym.asym.sc = scSData;
1712               else if (strcmp (name, ".rodata") == 0
1713                        || strcmp (name, ".rdata") == 0)
1714                 h->esym.asym.sc = scRData;
1715               else if (strcmp (name, ".bss") == 0)
1716                 h->esym.asym.sc = scBss;
1717               else if (strcmp (name, ".sbss") == 0)
1718                 h->esym.asym.sc = scSBss;
1719               else if (strcmp (name, ".init") == 0)
1720                 h->esym.asym.sc = scInit;
1721               else if (strcmp (name, ".fini") == 0)
1722                 h->esym.asym.sc = scFini;
1723               else
1724                 h->esym.asym.sc = scAbs;
1725             }
1726         }
1727
1728       h->esym.asym.reserved = 0;
1729       h->esym.asym.index = indexNil;
1730     }
1731
1732   if (h->root.root.type == bfd_link_hash_common)
1733     h->esym.asym.value = h->root.root.u.c.size;
1734   else if (h->root.root.type == bfd_link_hash_defined
1735            || h->root.root.type == bfd_link_hash_defweak)
1736     {
1737       if (h->esym.asym.sc == scCommon)
1738         h->esym.asym.sc = scBss;
1739       else if (h->esym.asym.sc == scSCommon)
1740         h->esym.asym.sc = scSBss;
1741
1742       sec = h->root.root.u.def.section;
1743       output_section = sec->output_section;
1744       if (output_section != NULL)
1745         h->esym.asym.value = (h->root.root.u.def.value
1746                               + sec->output_offset
1747                               + output_section->vma);
1748       else
1749         h->esym.asym.value = 0;
1750     }
1751   else if (h->root.needs_plt)
1752     {
1753       struct mips_elf_link_hash_entry *hd = h;
1754       bfd_boolean no_fn_stub = h->no_fn_stub;
1755
1756       while (hd->root.root.type == bfd_link_hash_indirect)
1757         {
1758           hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
1759           no_fn_stub = no_fn_stub || hd->no_fn_stub;
1760         }
1761
1762       if (!no_fn_stub)
1763         {
1764           /* Set type and value for a symbol with a function stub.  */
1765           h->esym.asym.st = stProc;
1766           sec = hd->root.root.u.def.section;
1767           if (sec == NULL)
1768             h->esym.asym.value = 0;
1769           else
1770             {
1771               output_section = sec->output_section;
1772               if (output_section != NULL)
1773                 h->esym.asym.value = (hd->root.plt.offset
1774                                       + sec->output_offset
1775                                       + output_section->vma);
1776               else
1777                 h->esym.asym.value = 0;
1778             }
1779         }
1780     }
1781
1782   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1783                                       h->root.root.root.string,
1784                                       &h->esym))
1785     {
1786       einfo->failed = TRUE;
1787       return FALSE;
1788     }
1789
1790   return TRUE;
1791 }
1792
1793 /* A comparison routine used to sort .gptab entries.  */
1794
1795 static int
1796 gptab_compare (const void *p1, const void *p2)
1797 {
1798   const Elf32_gptab *a1 = p1;
1799   const Elf32_gptab *a2 = p2;
1800
1801   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
1802 }
1803 \f
1804 /* Functions to manage the got entry hash table.  */
1805
1806 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
1807    hash number.  */
1808
1809 static INLINE hashval_t
1810 mips_elf_hash_bfd_vma (bfd_vma addr)
1811 {
1812 #ifdef BFD64
1813   return addr + (addr >> 32);
1814 #else
1815   return addr;
1816 #endif
1817 }
1818
1819 /* got_entries only match if they're identical, except for gotidx, so
1820    use all fields to compute the hash, and compare the appropriate
1821    union members.  */
1822
1823 static hashval_t
1824 mips_elf_got_entry_hash (const void *entry_)
1825 {
1826   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
1827
1828   return entry->symndx
1829     + ((entry->tls_type & GOT_TLS_LDM) << 17)
1830     + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
1831        : entry->abfd->id
1832          + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
1833             : entry->d.h->root.root.root.hash));
1834 }
1835
1836 static int
1837 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
1838 {
1839   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
1840   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
1841
1842   /* An LDM entry can only match another LDM entry.  */
1843   if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
1844     return 0;
1845
1846   return e1->abfd == e2->abfd && e1->symndx == e2->symndx
1847     && (! e1->abfd ? e1->d.address == e2->d.address
1848         : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
1849         : e1->d.h == e2->d.h);
1850 }
1851
1852 /* multi_got_entries are still a match in the case of global objects,
1853    even if the input bfd in which they're referenced differs, so the
1854    hash computation and compare functions are adjusted
1855    accordingly.  */
1856
1857 static hashval_t
1858 mips_elf_multi_got_entry_hash (const void *entry_)
1859 {
1860   const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
1861
1862   return entry->symndx
1863     + (! entry->abfd
1864        ? mips_elf_hash_bfd_vma (entry->d.address)
1865        : entry->symndx >= 0
1866        ? ((entry->tls_type & GOT_TLS_LDM)
1867           ? (GOT_TLS_LDM << 17)
1868           : (entry->abfd->id
1869              + mips_elf_hash_bfd_vma (entry->d.addend)))
1870        : entry->d.h->root.root.root.hash);
1871 }
1872
1873 static int
1874 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
1875 {
1876   const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
1877   const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
1878
1879   /* Any two LDM entries match.  */
1880   if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
1881     return 1;
1882
1883   /* Nothing else matches an LDM entry.  */
1884   if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
1885     return 0;
1886
1887   return e1->symndx == e2->symndx
1888     && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
1889         : e1->abfd == NULL || e2->abfd == NULL
1890         ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
1891         : e1->d.h == e2->d.h);
1892 }
1893 \f
1894 /* Returns the dynamic relocation section for DYNOBJ.  */
1895
1896 static asection *
1897 mips_elf_rel_dyn_section (bfd *dynobj, bfd_boolean create_p)
1898 {
1899   static const char dname[] = ".rel.dyn";
1900   asection *sreloc;
1901
1902   sreloc = bfd_get_section_by_name (dynobj, dname);
1903   if (sreloc == NULL && create_p)
1904     {
1905       sreloc = bfd_make_section (dynobj, dname);
1906       if (sreloc == NULL
1907           || ! bfd_set_section_flags (dynobj, sreloc,
1908                                       (SEC_ALLOC
1909                                        | SEC_LOAD
1910                                        | SEC_HAS_CONTENTS
1911                                        | SEC_IN_MEMORY
1912                                        | SEC_LINKER_CREATED
1913                                        | SEC_READONLY))
1914           || ! bfd_set_section_alignment (dynobj, sreloc,
1915                                           MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
1916         return NULL;
1917     }
1918   return sreloc;
1919 }
1920
1921 /* Returns the GOT section for ABFD.  */
1922
1923 static asection *
1924 mips_elf_got_section (bfd *abfd, bfd_boolean maybe_excluded)
1925 {
1926   asection *sgot = bfd_get_section_by_name (abfd, ".got");
1927   if (sgot == NULL
1928       || (! maybe_excluded && (sgot->flags & SEC_EXCLUDE) != 0))
1929     return NULL;
1930   return sgot;
1931 }
1932
1933 /* Returns the GOT information associated with the link indicated by
1934    INFO.  If SGOTP is non-NULL, it is filled in with the GOT
1935    section.  */
1936
1937 static struct mips_got_info *
1938 mips_elf_got_info (bfd *abfd, asection **sgotp)
1939 {
1940   asection *sgot;
1941   struct mips_got_info *g;
1942
1943   sgot = mips_elf_got_section (abfd, TRUE);
1944   BFD_ASSERT (sgot != NULL);
1945   BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
1946   g = mips_elf_section_data (sgot)->u.got_info;
1947   BFD_ASSERT (g != NULL);
1948
1949   if (sgotp)
1950     *sgotp = (sgot->flags & SEC_EXCLUDE) == 0 ? sgot : NULL;
1951
1952   return g;
1953 }
1954
1955 /* Count the number of relocations needed for a TLS GOT entry, with
1956    access types from TLS_TYPE, and symbol H (or a local symbol if H
1957    is NULL).  */
1958
1959 static int
1960 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
1961                      struct elf_link_hash_entry *h)
1962 {
1963   int indx = 0;
1964   int ret = 0;
1965   bfd_boolean need_relocs = FALSE;
1966   bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
1967
1968   if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1969       && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
1970     indx = h->dynindx;
1971
1972   if ((info->shared || indx != 0)
1973       && (h == NULL
1974           || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1975           || h->root.type != bfd_link_hash_undefweak))
1976     need_relocs = TRUE;
1977
1978   if (!need_relocs)
1979     return FALSE;
1980
1981   if (tls_type & GOT_TLS_GD)
1982     {
1983       ret++;
1984       if (indx != 0)
1985         ret++;
1986     }
1987
1988   if (tls_type & GOT_TLS_IE)
1989     ret++;
1990
1991   if ((tls_type & GOT_TLS_LDM) && info->shared)
1992     ret++;
1993
1994   return ret;
1995 }
1996
1997 /* Count the number of TLS relocations required for the GOT entry in
1998    ARG1, if it describes a local symbol.  */
1999
2000 static int
2001 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2002 {
2003   struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2004   struct mips_elf_count_tls_arg *arg = arg2;
2005
2006   if (entry->abfd != NULL && entry->symndx != -1)
2007     arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2008
2009   return 1;
2010 }
2011
2012 /* Count the number of TLS GOT entries required for the global (or
2013    forced-local) symbol in ARG1.  */
2014
2015 static int
2016 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2017 {
2018   struct mips_elf_link_hash_entry *hm
2019     = (struct mips_elf_link_hash_entry *) arg1;
2020   struct mips_elf_count_tls_arg *arg = arg2;
2021
2022   if (hm->tls_type & GOT_TLS_GD)
2023     arg->needed += 2;
2024   if (hm->tls_type & GOT_TLS_IE)
2025     arg->needed += 1;
2026
2027   return 1;
2028 }
2029
2030 /* Count the number of TLS relocations required for the global (or
2031    forced-local) symbol in ARG1.  */
2032
2033 static int
2034 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2035 {
2036   struct mips_elf_link_hash_entry *hm
2037     = (struct mips_elf_link_hash_entry *) arg1;
2038   struct mips_elf_count_tls_arg *arg = arg2;
2039
2040   arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2041
2042   return 1;
2043 }
2044
2045 /* Output a simple dynamic relocation into SRELOC.  */
2046
2047 static void
2048 mips_elf_output_dynamic_relocation (bfd *output_bfd,
2049                                     asection *sreloc,
2050                                     unsigned long indx,
2051                                     int r_type,
2052                                     bfd_vma offset)
2053 {
2054   Elf_Internal_Rela rel[3];
2055
2056   memset (rel, 0, sizeof (rel));
2057
2058   rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2059   rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2060
2061   if (ABI_64_P (output_bfd))
2062     {
2063       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2064         (output_bfd, &rel[0],
2065          (sreloc->contents
2066           + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
2067     }
2068   else
2069     bfd_elf32_swap_reloc_out
2070       (output_bfd, &rel[0],
2071        (sreloc->contents
2072         + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
2073   ++sreloc->reloc_count;
2074 }
2075
2076 /* Initialize a set of TLS GOT entries for one symbol.  */
2077
2078 static void
2079 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2080                                unsigned char *tls_type_p,
2081                                struct bfd_link_info *info,
2082                                struct mips_elf_link_hash_entry *h,
2083                                bfd_vma value)
2084 {
2085   int indx;
2086   asection *sreloc, *sgot;
2087   bfd_vma offset, offset2;
2088   bfd *dynobj;
2089   bfd_boolean need_relocs = FALSE;
2090
2091   dynobj = elf_hash_table (info)->dynobj;
2092   sgot = mips_elf_got_section (dynobj, FALSE);
2093
2094   indx = 0;
2095   if (h != NULL)
2096     {
2097       bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2098
2099       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2100           && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2101         indx = h->root.dynindx;
2102     }
2103
2104   if (*tls_type_p & GOT_TLS_DONE)
2105     return;
2106
2107   if ((info->shared || indx != 0)
2108       && (h == NULL
2109           || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2110           || h->root.type != bfd_link_hash_undefweak))
2111     need_relocs = TRUE;
2112
2113   /* MINUS_ONE means the symbol is not defined in this object.  It may not
2114      be defined at all; assume that the value doesn't matter in that
2115      case.  Otherwise complain if we would use the value.  */
2116   BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2117               || h->root.root.type == bfd_link_hash_undefweak);
2118
2119   /* Emit necessary relocations.  */
2120   sreloc = mips_elf_rel_dyn_section (dynobj, FALSE);
2121
2122   /* General Dynamic.  */
2123   if (*tls_type_p & GOT_TLS_GD)
2124     {
2125       offset = got_offset;
2126       offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2127
2128       if (need_relocs)
2129         {
2130           mips_elf_output_dynamic_relocation
2131             (abfd, sreloc, indx,
2132              ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2133              sgot->output_offset + sgot->output_section->vma + offset);
2134
2135           if (indx)
2136             mips_elf_output_dynamic_relocation
2137               (abfd, sreloc, indx,
2138                ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2139                sgot->output_offset + sgot->output_section->vma + offset2);
2140           else
2141             MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2142                                sgot->contents + offset2);
2143         }
2144       else
2145         {
2146           MIPS_ELF_PUT_WORD (abfd, 1,
2147                              sgot->contents + offset);
2148           MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2149                              sgot->contents + offset2);
2150         }
2151
2152       got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2153     }
2154
2155   /* Initial Exec model.  */
2156   if (*tls_type_p & GOT_TLS_IE)
2157     {
2158       offset = got_offset;
2159
2160       if (need_relocs)
2161         {
2162           if (indx == 0)
2163             MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2164                                sgot->contents + offset);
2165           else
2166             MIPS_ELF_PUT_WORD (abfd, 0,
2167                                sgot->contents + offset);
2168
2169           mips_elf_output_dynamic_relocation
2170             (abfd, sreloc, indx,
2171              ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2172              sgot->output_offset + sgot->output_section->vma + offset);
2173         }
2174       else
2175         MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2176                            sgot->contents + offset);
2177     }
2178
2179   if (*tls_type_p & GOT_TLS_LDM)
2180     {
2181       /* The initial offset is zero, and the LD offsets will include the
2182          bias by DTP_OFFSET.  */
2183       MIPS_ELF_PUT_WORD (abfd, 0,
2184                          sgot->contents + got_offset
2185                          + MIPS_ELF_GOT_SIZE (abfd));
2186
2187       if (!info->shared)
2188         MIPS_ELF_PUT_WORD (abfd, 1,
2189                            sgot->contents + got_offset);
2190       else
2191         mips_elf_output_dynamic_relocation
2192           (abfd, sreloc, indx,
2193            ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2194            sgot->output_offset + sgot->output_section->vma + got_offset);
2195     }
2196
2197   *tls_type_p |= GOT_TLS_DONE;
2198 }
2199
2200 /* Return the GOT index to use for a relocation of type R_TYPE against
2201    a symbol accessed using TLS_TYPE models.  The GOT entries for this
2202    symbol in this GOT start at GOT_INDEX.  This function initializes the
2203    GOT entries and corresponding relocations.  */
2204
2205 static bfd_vma
2206 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2207                     int r_type, struct bfd_link_info *info,
2208                     struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2209 {
2210   BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2211               || r_type == R_MIPS_TLS_LDM);
2212
2213   mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2214
2215   if (r_type == R_MIPS_TLS_GOTTPREL)
2216     {
2217       BFD_ASSERT (*tls_type & GOT_TLS_IE);
2218       if (*tls_type & GOT_TLS_GD)
2219         return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2220       else
2221         return got_index;
2222     }
2223
2224   if (r_type == R_MIPS_TLS_GD)
2225     {
2226       BFD_ASSERT (*tls_type & GOT_TLS_GD);
2227       return got_index;
2228     }
2229
2230   if (r_type == R_MIPS_TLS_LDM)
2231     {
2232       BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2233       return got_index;
2234     }
2235
2236   return got_index;
2237 }
2238
2239 /* Returns the GOT offset at which the indicated address can be found.
2240    If there is not yet a GOT entry for this value, create one.  If
2241    R_SYMNDX refers to a TLS symbol, create a TLS GOT entry instead.
2242    Returns -1 if no satisfactory GOT offset can be found.  */
2243
2244 static bfd_vma
2245 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2246                           bfd_vma value, unsigned long r_symndx,
2247                           struct mips_elf_link_hash_entry *h, int r_type)
2248 {
2249   asection *sgot;
2250   struct mips_got_info *g;
2251   struct mips_got_entry *entry;
2252
2253   g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2254
2255   entry = mips_elf_create_local_got_entry (abfd, ibfd, g, sgot, value,
2256                                            r_symndx, h, r_type);
2257   if (!entry)
2258     return MINUS_ONE;
2259
2260   if (TLS_RELOC_P (r_type))
2261     return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type, r_type,
2262                                info, h, value);
2263   else
2264     return entry->gotidx;
2265 }
2266
2267 /* Returns the GOT index for the global symbol indicated by H.  */
2268
2269 static bfd_vma
2270 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
2271                            int r_type, struct bfd_link_info *info)
2272 {
2273   bfd_vma index;
2274   asection *sgot;
2275   struct mips_got_info *g, *gg;
2276   long global_got_dynindx = 0;
2277
2278   gg = g = mips_elf_got_info (abfd, &sgot);
2279   if (g->bfd2got && ibfd)
2280     {
2281       struct mips_got_entry e, *p;
2282
2283       BFD_ASSERT (h->dynindx >= 0);
2284
2285       g = mips_elf_got_for_ibfd (g, ibfd);
2286       if (g->next != gg || TLS_RELOC_P (r_type))
2287         {
2288           e.abfd = ibfd;
2289           e.symndx = -1;
2290           e.d.h = (struct mips_elf_link_hash_entry *)h;
2291           e.tls_type = 0;
2292
2293           p = htab_find (g->got_entries, &e);
2294
2295           BFD_ASSERT (p->gotidx > 0);
2296
2297           if (TLS_RELOC_P (r_type))
2298             {
2299               bfd_vma value = MINUS_ONE;
2300               if ((h->root.type == bfd_link_hash_defined
2301                    || h->root.type == bfd_link_hash_defweak)
2302                   && h->root.u.def.section->output_section)
2303                 value = (h->root.u.def.value
2304                          + h->root.u.def.section->output_offset
2305                          + h->root.u.def.section->output_section->vma);
2306
2307               return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
2308                                          info, e.d.h, value);
2309             }
2310           else
2311             return p->gotidx;
2312         }
2313     }
2314
2315   if (gg->global_gotsym != NULL)
2316     global_got_dynindx = gg->global_gotsym->dynindx;
2317
2318   if (TLS_RELOC_P (r_type))
2319     {
2320       struct mips_elf_link_hash_entry *hm
2321         = (struct mips_elf_link_hash_entry *) h;
2322       bfd_vma value = MINUS_ONE;
2323
2324       if ((h->root.type == bfd_link_hash_defined
2325            || h->root.type == bfd_link_hash_defweak)
2326           && h->root.u.def.section->output_section)
2327         value = (h->root.u.def.value
2328                  + h->root.u.def.section->output_offset
2329                  + h->root.u.def.section->output_section->vma);
2330
2331       index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
2332                                   r_type, info, hm, value);
2333     }
2334   else
2335     {
2336       /* Once we determine the global GOT entry with the lowest dynamic
2337          symbol table index, we must put all dynamic symbols with greater
2338          indices into the GOT.  That makes it easy to calculate the GOT
2339          offset.  */
2340       BFD_ASSERT (h->dynindx >= global_got_dynindx);
2341       index = ((h->dynindx - global_got_dynindx + g->local_gotno)
2342                * MIPS_ELF_GOT_SIZE (abfd));
2343     }
2344   BFD_ASSERT (index < sgot->size);
2345
2346   return index;
2347 }
2348
2349 /* Find a GOT entry that is within 32KB of the VALUE.  These entries
2350    are supposed to be placed at small offsets in the GOT, i.e.,
2351    within 32KB of GP.  Return the index into the GOT for this page,
2352    and store the offset from this entry to the desired address in
2353    OFFSETP, if it is non-NULL.  */
2354
2355 static bfd_vma
2356 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2357                    bfd_vma value, bfd_vma *offsetp)
2358 {
2359   asection *sgot;
2360   struct mips_got_info *g;
2361   bfd_vma index;
2362   struct mips_got_entry *entry;
2363
2364   g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2365
2366   entry = mips_elf_create_local_got_entry (abfd, ibfd, g, sgot,
2367                                            (value + 0x8000)
2368                                            & (~(bfd_vma)0xffff), 0,
2369                                            NULL, R_MIPS_GOT_PAGE);
2370
2371   if (!entry)
2372     return MINUS_ONE;
2373
2374   index = entry->gotidx;
2375
2376   if (offsetp)
2377     *offsetp = value - entry->d.address;
2378
2379   return index;
2380 }
2381
2382 /* Find a GOT entry whose higher-order 16 bits are the same as those
2383    for value.  Return the index into the GOT for this entry.  */
2384
2385 static bfd_vma
2386 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2387                       bfd_vma value, bfd_boolean external)
2388 {
2389   asection *sgot;
2390   struct mips_got_info *g;
2391   struct mips_got_entry *entry;
2392
2393   if (! external)
2394     {
2395       /* Although the ABI says that it is "the high-order 16 bits" that we
2396          want, it is really the %high value.  The complete value is
2397          calculated with a `addiu' of a LO16 relocation, just as with a
2398          HI16/LO16 pair.  */
2399       value = mips_elf_high (value) << 16;
2400     }
2401
2402   g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2403
2404   entry = mips_elf_create_local_got_entry (abfd, ibfd, g, sgot, value, 0, NULL,
2405                                            R_MIPS_GOT16);
2406   if (entry)
2407     return entry->gotidx;
2408   else
2409     return MINUS_ONE;
2410 }
2411
2412 /* Returns the offset for the entry at the INDEXth position
2413    in the GOT.  */
2414
2415 static bfd_vma
2416 mips_elf_got_offset_from_index (bfd *dynobj, bfd *output_bfd,
2417                                 bfd *input_bfd, bfd_vma index)
2418 {
2419   asection *sgot;
2420   bfd_vma gp;
2421   struct mips_got_info *g;
2422
2423   g = mips_elf_got_info (dynobj, &sgot);
2424   gp = _bfd_get_gp_value (output_bfd)
2425     + mips_elf_adjust_gp (output_bfd, g, input_bfd);
2426
2427   return sgot->output_section->vma + sgot->output_offset + index - gp;
2428 }
2429
2430 /* Create a local GOT entry for VALUE.  Return the index of the entry,
2431    or -1 if it could not be created.  If R_SYMNDX refers to a TLS symbol,
2432    create a TLS entry instead.  */
2433
2434 static struct mips_got_entry *
2435 mips_elf_create_local_got_entry (bfd *abfd, bfd *ibfd,
2436                                  struct mips_got_info *gg,
2437                                  asection *sgot, bfd_vma value,
2438                                  unsigned long r_symndx,
2439                                  struct mips_elf_link_hash_entry *h,
2440                                  int r_type)
2441 {
2442   struct mips_got_entry entry, **loc;
2443   struct mips_got_info *g;
2444
2445   entry.abfd = NULL;
2446   entry.symndx = -1;
2447   entry.d.address = value;
2448   entry.tls_type = 0;
2449
2450   g = mips_elf_got_for_ibfd (gg, ibfd);
2451   if (g == NULL)
2452     {
2453       g = mips_elf_got_for_ibfd (gg, abfd);
2454       BFD_ASSERT (g != NULL);
2455     }
2456
2457   /* We might have a symbol, H, if it has been forced local.  Use the
2458      global entry then.  It doesn't matter whether an entry is local
2459      or global for TLS, since the dynamic linker does not
2460      automatically relocate TLS GOT entries.  */
2461   BFD_ASSERT (h == NULL || h->forced_local);
2462   if (TLS_RELOC_P (r_type))
2463     {
2464       struct mips_got_entry *p;
2465
2466       entry.abfd = ibfd;
2467       if (r_type == R_MIPS_TLS_LDM)
2468         {
2469           entry.tls_type = GOT_TLS_LDM;
2470           entry.symndx = 0;
2471           entry.d.addend = 0;
2472         }
2473       else if (h == NULL)
2474         {
2475           entry.symndx = r_symndx;
2476           entry.d.addend = 0;
2477         }
2478       else
2479         entry.d.h = h;
2480
2481       p = (struct mips_got_entry *)
2482         htab_find (g->got_entries, &entry);
2483
2484       BFD_ASSERT (p);
2485       return p;
2486     }
2487
2488   loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2489                                                    INSERT);
2490   if (*loc)
2491     return *loc;
2492
2493   entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
2494   entry.tls_type = 0;
2495
2496   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2497
2498   if (! *loc)
2499     return NULL;
2500
2501   memcpy (*loc, &entry, sizeof entry);
2502
2503   if (g->assigned_gotno >= g->local_gotno)
2504     {
2505       (*loc)->gotidx = -1;
2506       /* We didn't allocate enough space in the GOT.  */
2507       (*_bfd_error_handler)
2508         (_("not enough GOT space for local GOT entries"));
2509       bfd_set_error (bfd_error_bad_value);
2510       return NULL;
2511     }
2512
2513   MIPS_ELF_PUT_WORD (abfd, value,
2514                      (sgot->contents + entry.gotidx));
2515
2516   return *loc;
2517 }
2518
2519 /* Sort the dynamic symbol table so that symbols that need GOT entries
2520    appear towards the end.  This reduces the amount of GOT space
2521    required.  MAX_LOCAL is used to set the number of local symbols
2522    known to be in the dynamic symbol table.  During
2523    _bfd_mips_elf_size_dynamic_sections, this value is 1.  Afterward, the
2524    section symbols are added and the count is higher.  */
2525
2526 static bfd_boolean
2527 mips_elf_sort_hash_table (struct bfd_link_info *info, unsigned long max_local)
2528 {
2529   struct mips_elf_hash_sort_data hsd;
2530   struct mips_got_info *g;
2531   bfd *dynobj;
2532
2533   dynobj = elf_hash_table (info)->dynobj;
2534
2535   g = mips_elf_got_info (dynobj, NULL);
2536
2537   hsd.low = NULL;
2538   hsd.max_unref_got_dynindx =
2539   hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
2540     /* In the multi-got case, assigned_gotno of the master got_info
2541        indicate the number of entries that aren't referenced in the
2542        primary GOT, but that must have entries because there are
2543        dynamic relocations that reference it.  Since they aren't
2544        referenced, we move them to the end of the GOT, so that they
2545        don't prevent other entries that are referenced from getting
2546        too large offsets.  */
2547     - (g->next ? g->assigned_gotno : 0);
2548   hsd.max_non_got_dynindx = max_local;
2549   mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
2550                                 elf_hash_table (info)),
2551                                mips_elf_sort_hash_table_f,
2552                                &hsd);
2553
2554   /* There should have been enough room in the symbol table to
2555      accommodate both the GOT and non-GOT symbols.  */
2556   BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
2557   BFD_ASSERT ((unsigned long)hsd.max_unref_got_dynindx
2558               <= elf_hash_table (info)->dynsymcount);
2559
2560   /* Now we know which dynamic symbol has the lowest dynamic symbol
2561      table index in the GOT.  */
2562   g->global_gotsym = hsd.low;
2563
2564   return TRUE;
2565 }
2566
2567 /* If H needs a GOT entry, assign it the highest available dynamic
2568    index.  Otherwise, assign it the lowest available dynamic
2569    index.  */
2570
2571 static bfd_boolean
2572 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
2573 {
2574   struct mips_elf_hash_sort_data *hsd = data;
2575
2576   if (h->root.root.type == bfd_link_hash_warning)
2577     h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2578
2579   /* Symbols without dynamic symbol table entries aren't interesting
2580      at all.  */
2581   if (h->root.dynindx == -1)
2582     return TRUE;
2583
2584   /* Global symbols that need GOT entries that are not explicitly
2585      referenced are marked with got offset 2.  Those that are
2586      referenced get a 1, and those that don't need GOT entries get
2587      -1.  */
2588   if (h->root.got.offset == 2)
2589     {
2590       BFD_ASSERT (h->tls_type == GOT_NORMAL);
2591
2592       if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
2593         hsd->low = (struct elf_link_hash_entry *) h;
2594       h->root.dynindx = hsd->max_unref_got_dynindx++;
2595     }
2596   else if (h->root.got.offset != 1)
2597     h->root.dynindx = hsd->max_non_got_dynindx++;
2598   else
2599     {
2600       BFD_ASSERT (h->tls_type == GOT_NORMAL);
2601
2602       h->root.dynindx = --hsd->min_got_dynindx;
2603       hsd->low = (struct elf_link_hash_entry *) h;
2604     }
2605
2606   return TRUE;
2607 }
2608
2609 /* If H is a symbol that needs a global GOT entry, but has a dynamic
2610    symbol table index lower than any we've seen to date, record it for
2611    posterity.  */
2612
2613 static bfd_boolean
2614 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
2615                                    bfd *abfd, struct bfd_link_info *info,
2616                                    struct mips_got_info *g,
2617                                    unsigned char tls_flag)
2618 {
2619   struct mips_got_entry entry, **loc;
2620
2621   /* A global symbol in the GOT must also be in the dynamic symbol
2622      table.  */
2623   if (h->dynindx == -1)
2624     {
2625       switch (ELF_ST_VISIBILITY (h->other))
2626         {
2627         case STV_INTERNAL:
2628         case STV_HIDDEN:
2629           _bfd_mips_elf_hide_symbol (info, h, TRUE);
2630           break;
2631         }
2632       if (!bfd_elf_link_record_dynamic_symbol (info, h))
2633         return FALSE;
2634     }
2635
2636   entry.abfd = abfd;
2637   entry.symndx = -1;
2638   entry.d.h = (struct mips_elf_link_hash_entry *) h;
2639   entry.tls_type = 0;
2640
2641   loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2642                                                    INSERT);
2643
2644   /* If we've already marked this entry as needing GOT space, we don't
2645      need to do it again.  */
2646   if (*loc)
2647     {
2648       (*loc)->tls_type |= tls_flag;
2649       return TRUE;
2650     }
2651
2652   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2653
2654   if (! *loc)
2655     return FALSE;
2656
2657   entry.gotidx = -1;
2658   entry.tls_type = tls_flag;
2659
2660   memcpy (*loc, &entry, sizeof entry);
2661
2662   if (h->got.offset != MINUS_ONE)
2663     return TRUE;
2664
2665   /* By setting this to a value other than -1, we are indicating that
2666      there needs to be a GOT entry for H.  Avoid using zero, as the
2667      generic ELF copy_indirect_symbol tests for <= 0.  */
2668   if (tls_flag == 0)
2669     h->got.offset = 1;
2670
2671   return TRUE;
2672 }
2673
2674 /* Reserve space in G for a GOT entry containing the value of symbol
2675    SYMNDX in input bfd ABDF, plus ADDEND.  */
2676
2677 static bfd_boolean
2678 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
2679                                   struct mips_got_info *g,
2680                                   unsigned char tls_flag)
2681 {
2682   struct mips_got_entry entry, **loc;
2683
2684   entry.abfd = abfd;
2685   entry.symndx = symndx;
2686   entry.d.addend = addend;
2687   entry.tls_type = tls_flag;
2688   loc = (struct mips_got_entry **)
2689     htab_find_slot (g->got_entries, &entry, INSERT);
2690
2691   if (*loc)
2692     {
2693       if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
2694         {
2695           g->tls_gotno += 2;
2696           (*loc)->tls_type |= tls_flag;
2697         }
2698       else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
2699         {
2700           g->tls_gotno += 1;
2701           (*loc)->tls_type |= tls_flag;
2702         }
2703       return TRUE;
2704     }
2705
2706   if (tls_flag != 0)
2707     {
2708       entry.gotidx = -1;
2709       entry.tls_type = tls_flag;
2710       if (tls_flag == GOT_TLS_IE)
2711         g->tls_gotno += 1;
2712       else if (tls_flag == GOT_TLS_GD)
2713         g->tls_gotno += 2;
2714       else if (g->tls_ldm_offset == MINUS_ONE)
2715         {
2716           g->tls_ldm_offset = MINUS_TWO;
2717           g->tls_gotno += 2;
2718         }
2719     }
2720   else
2721     {
2722       entry.gotidx = g->local_gotno++;
2723       entry.tls_type = 0;
2724     }
2725
2726   *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2727
2728   if (! *loc)
2729     return FALSE;
2730
2731   memcpy (*loc, &entry, sizeof entry);
2732
2733   return TRUE;
2734 }
2735 \f
2736 /* Compute the hash value of the bfd in a bfd2got hash entry.  */
2737
2738 static hashval_t
2739 mips_elf_bfd2got_entry_hash (const void *entry_)
2740 {
2741   const struct mips_elf_bfd2got_hash *entry
2742     = (struct mips_elf_bfd2got_hash *)entry_;
2743
2744   return entry->bfd->id;
2745 }
2746
2747 /* Check whether two hash entries have the same bfd.  */
2748
2749 static int
2750 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
2751 {
2752   const struct mips_elf_bfd2got_hash *e1
2753     = (const struct mips_elf_bfd2got_hash *)entry1;
2754   const struct mips_elf_bfd2got_hash *e2
2755     = (const struct mips_elf_bfd2got_hash *)entry2;
2756
2757   return e1->bfd == e2->bfd;
2758 }
2759
2760 /* In a multi-got link, determine the GOT to be used for IBDF.  G must
2761    be the master GOT data.  */
2762
2763 static struct mips_got_info *
2764 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
2765 {
2766   struct mips_elf_bfd2got_hash e, *p;
2767
2768   if (! g->bfd2got)
2769     return g;
2770
2771   e.bfd = ibfd;
2772   p = htab_find (g->bfd2got, &e);
2773   return p ? p->g : NULL;
2774 }
2775
2776 /* Create one separate got for each bfd that has entries in the global
2777    got, such that we can tell how many local and global entries each
2778    bfd requires.  */
2779
2780 static int
2781 mips_elf_make_got_per_bfd (void **entryp, void *p)
2782 {
2783   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
2784   struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
2785   htab_t bfd2got = arg->bfd2got;
2786   struct mips_got_info *g;
2787   struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
2788   void **bfdgotp;
2789
2790   /* Find the got_info for this GOT entry's input bfd.  Create one if
2791      none exists.  */
2792   bfdgot_entry.bfd = entry->abfd;
2793   bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
2794   bfdgot = (struct mips_elf_bfd2got_hash *)*bfdgotp;
2795
2796   if (bfdgot != NULL)
2797     g = bfdgot->g;
2798   else
2799     {
2800       bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
2801         (arg->obfd, sizeof (struct mips_elf_bfd2got_hash));
2802
2803       if (bfdgot == NULL)
2804         {
2805           arg->obfd = 0;
2806           return 0;
2807         }
2808
2809       *bfdgotp = bfdgot;
2810
2811       bfdgot->bfd = entry->abfd;
2812       bfdgot->g = g = (struct mips_got_info *)
2813         bfd_alloc (arg->obfd, sizeof (struct mips_got_info));
2814       if (g == NULL)
2815         {
2816           arg->obfd = 0;
2817           return 0;
2818         }
2819
2820       g->global_gotsym = NULL;
2821       g->global_gotno = 0;
2822       g->local_gotno = 0;
2823       g->assigned_gotno = -1;
2824       g->tls_gotno = 0;
2825       g->tls_assigned_gotno = 0;
2826       g->tls_ldm_offset = MINUS_ONE;
2827       g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
2828                                         mips_elf_multi_got_entry_eq, NULL);
2829       if (g->got_entries == NULL)
2830         {
2831           arg->obfd = 0;
2832           return 0;
2833         }
2834
2835       g->bfd2got = NULL;
2836       g->next = NULL;
2837     }
2838
2839   /* Insert the GOT entry in the bfd's got entry hash table.  */
2840   entryp = htab_find_slot (g->got_entries, entry, INSERT);
2841   if (*entryp != NULL)
2842     return 1;
2843
2844   *entryp = entry;
2845
2846   if (entry->tls_type)
2847     {
2848       if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
2849         g->tls_gotno += 2;
2850       if (entry->tls_type & GOT_TLS_IE)
2851         g->tls_gotno += 1;
2852     }
2853   else if (entry->symndx >= 0 || entry->d.h->forced_local)
2854     ++g->local_gotno;
2855   else
2856     ++g->global_gotno;
2857
2858   return 1;
2859 }
2860
2861 /* Attempt to merge gots of different input bfds.  Try to use as much
2862    as possible of the primary got, since it doesn't require explicit
2863    dynamic relocations, but don't use bfds that would reference global
2864    symbols out of the addressable range.  Failing the primary got,
2865    attempt to merge with the current got, or finish the current got
2866    and then make make the new got current.  */
2867
2868 static int
2869 mips_elf_merge_gots (void **bfd2got_, void *p)
2870 {
2871   struct mips_elf_bfd2got_hash *bfd2got
2872     = (struct mips_elf_bfd2got_hash *)*bfd2got_;
2873   struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
2874   unsigned int lcount = bfd2got->g->local_gotno;
2875   unsigned int gcount = bfd2got->g->global_gotno;
2876   unsigned int tcount = bfd2got->g->tls_gotno;
2877   unsigned int maxcnt = arg->max_count;
2878   bfd_boolean too_many_for_tls = FALSE;
2879
2880   /* We place TLS GOT entries after both locals and globals.  The globals
2881      for the primary GOT may overflow the normal GOT size limit, so be
2882      sure not to merge a GOT which requires TLS with the primary GOT in that
2883      case.  This doesn't affect non-primary GOTs.  */
2884   if (tcount > 0)
2885     {
2886       unsigned int primary_total = lcount + tcount + arg->global_count;
2887       if (primary_total * MIPS_ELF_GOT_SIZE (bfd2got->bfd)
2888            >= MIPS_ELF_GOT_MAX_SIZE (bfd2got->bfd))
2889         too_many_for_tls = TRUE;
2890     }
2891
2892   /* If we don't have a primary GOT and this is not too big, use it as
2893      a starting point for the primary GOT.  */
2894   if (! arg->primary && lcount + gcount + tcount <= maxcnt
2895       && ! too_many_for_tls)
2896     {
2897       arg->primary = bfd2got->g;
2898       arg->primary_count = lcount + gcount;
2899     }
2900   /* If it looks like we can merge this bfd's entries with those of
2901      the primary, merge them.  The heuristics is conservative, but we
2902      don't have to squeeze it too hard.  */
2903   else if (arg->primary && ! too_many_for_tls
2904            && (arg->primary_count + lcount + gcount + tcount) <= maxcnt)
2905     {
2906       struct mips_got_info *g = bfd2got->g;
2907       int old_lcount = arg->primary->local_gotno;
2908       int old_gcount = arg->primary->global_gotno;
2909       int old_tcount = arg->primary->tls_gotno;
2910
2911       bfd2got->g = arg->primary;
2912
2913       htab_traverse (g->got_entries,
2914                      mips_elf_make_got_per_bfd,
2915                      arg);
2916       if (arg->obfd == NULL)
2917         return 0;
2918
2919       htab_delete (g->got_entries);
2920       /* We don't have to worry about releasing memory of the actual
2921          got entries, since they're all in the master got_entries hash
2922          table anyway.  */
2923
2924       BFD_ASSERT (old_lcount + lcount >= arg->primary->local_gotno);
2925       BFD_ASSERT (old_gcount + gcount >= arg->primary->global_gotno);
2926       BFD_ASSERT (old_tcount + tcount >= arg->primary->tls_gotno);
2927
2928       arg->primary_count = arg->primary->local_gotno
2929         + arg->primary->global_gotno + arg->primary->tls_gotno;
2930     }
2931   /* If we can merge with the last-created got, do it.  */
2932   else if (arg->current
2933            && arg->current_count + lcount + gcount + tcount <= maxcnt)
2934     {
2935       struct mips_got_info *g = bfd2got->g;
2936       int old_lcount = arg->current->local_gotno;
2937       int old_gcount = arg->current->global_gotno;
2938       int old_tcount = arg->current->tls_gotno;
2939
2940       bfd2got->g = arg->current;
2941
2942       htab_traverse (g->got_entries,
2943                      mips_elf_make_got_per_bfd,
2944                      arg);
2945       if (arg->obfd == NULL)
2946         return 0;
2947
2948       htab_delete (g->got_entries);
2949
2950       BFD_ASSERT (old_lcount + lcount >= arg->current->local_gotno);
2951       BFD_ASSERT (old_gcount + gcount >= arg->current->global_gotno);
2952       BFD_ASSERT (old_tcount + tcount >= arg->current->tls_gotno);
2953
2954       arg->current_count = arg->current->local_gotno
2955         + arg->current->global_gotno + arg->current->tls_gotno;
2956     }
2957   /* Well, we couldn't merge, so create a new GOT.  Don't check if it
2958      fits; if it turns out that it doesn't, we'll get relocation
2959      overflows anyway.  */
2960   else
2961     {
2962       bfd2got->g->next = arg->current;
2963       arg->current = bfd2got->g;
2964
2965       arg->current_count = lcount + gcount + 2 * tcount;
2966     }
2967
2968   return 1;
2969 }
2970
2971 /* Set the TLS GOT index for the GOT entry in ENTRYP.  */
2972
2973 static int
2974 mips_elf_initialize_tls_index (void **entryp, void *p)
2975 {
2976   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
2977   struct mips_got_info *g = p;
2978
2979   /* We're only interested in TLS symbols.  */
2980   if (entry->tls_type == 0)
2981     return 1;
2982
2983   if (entry->symndx == -1)
2984     {
2985       /* There may be multiple mips_got_entry structs for a global variable
2986          if there is just one GOT.  Just do this once.  */
2987       if (g->next == NULL)
2988         {
2989           if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
2990             return 1;
2991           entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
2992         }
2993     }
2994   else if (entry->tls_type & GOT_TLS_LDM)
2995     {
2996       /* Similarly, there may be multiple structs for the LDM entry.  */
2997       if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
2998         {
2999           entry->gotidx = g->tls_ldm_offset;
3000           return 1;
3001         }
3002     }
3003
3004   /* Initialize the GOT offset.  */
3005   entry->gotidx = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
3006   if (g->next == NULL && entry->symndx == -1)
3007     entry->d.h->tls_got_offset = entry->gotidx;
3008
3009   if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3010     g->tls_assigned_gotno += 2;
3011   if (entry->tls_type & GOT_TLS_IE)
3012     g->tls_assigned_gotno += 1;
3013
3014   if (entry->tls_type & GOT_TLS_LDM)
3015     g->tls_ldm_offset = entry->gotidx;
3016
3017   return 1;
3018 }
3019
3020 /* If passed a NULL mips_got_info in the argument, set the marker used
3021    to tell whether a global symbol needs a got entry (in the primary
3022    got) to the given VALUE.
3023
3024    If passed a pointer G to a mips_got_info in the argument (it must
3025    not be the primary GOT), compute the offset from the beginning of
3026    the (primary) GOT section to the entry in G corresponding to the
3027    global symbol.  G's assigned_gotno must contain the index of the
3028    first available global GOT entry in G.  VALUE must contain the size
3029    of a GOT entry in bytes.  For each global GOT entry that requires a
3030    dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
3031    marked as not eligible for lazy resolution through a function
3032    stub.  */
3033 static int
3034 mips_elf_set_global_got_offset (void **entryp, void *p)
3035 {
3036   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3037   struct mips_elf_set_global_got_offset_arg *arg
3038     = (struct mips_elf_set_global_got_offset_arg *)p;
3039   struct mips_got_info *g = arg->g;
3040
3041   if (g && entry->tls_type != GOT_NORMAL)
3042     arg->needed_relocs +=
3043       mips_tls_got_relocs (arg->info, entry->tls_type,
3044                            entry->symndx == -1 ? &entry->d.h->root : NULL);
3045
3046   if (entry->abfd != NULL && entry->symndx == -1
3047       && entry->d.h->root.dynindx != -1
3048       && entry->d.h->tls_type == GOT_NORMAL)
3049     {
3050       if (g)
3051         {
3052           BFD_ASSERT (g->global_gotsym == NULL);
3053
3054           entry->gotidx = arg->value * (long) g->assigned_gotno++;
3055           if (arg->info->shared
3056               || (elf_hash_table (arg->info)->dynamic_sections_created
3057                   && entry->d.h->root.def_dynamic
3058                   && !entry->d.h->root.def_regular))
3059             ++arg->needed_relocs;
3060         }
3061       else
3062         entry->d.h->root.got.offset = arg->value;
3063     }
3064
3065   return 1;
3066 }
3067
3068 /* Mark any global symbols referenced in the GOT we are iterating over
3069    as inelligible for lazy resolution stubs.  */
3070 static int
3071 mips_elf_set_no_stub (void **entryp, void *p ATTRIBUTE_UNUSED)
3072 {
3073   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3074
3075   if (entry->abfd != NULL
3076       && entry->symndx == -1
3077       && entry->d.h->root.dynindx != -1)
3078     entry->d.h->no_fn_stub = TRUE;
3079
3080   return 1;
3081 }
3082
3083 /* Follow indirect and warning hash entries so that each got entry
3084    points to the final symbol definition.  P must point to a pointer
3085    to the hash table we're traversing.  Since this traversal may
3086    modify the hash table, we set this pointer to NULL to indicate
3087    we've made a potentially-destructive change to the hash table, so
3088    the traversal must be restarted.  */
3089 static int
3090 mips_elf_resolve_final_got_entry (void **entryp, void *p)
3091 {
3092   struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3093   htab_t got_entries = *(htab_t *)p;
3094
3095   if (entry->abfd != NULL && entry->symndx == -1)
3096     {
3097       struct mips_elf_link_hash_entry *h = entry->d.h;
3098
3099       while (h->root.root.type == bfd_link_hash_indirect
3100              || h->root.root.type == bfd_link_hash_warning)
3101         h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3102
3103       if (entry->d.h == h)
3104         return 1;
3105
3106       entry->d.h = h;
3107
3108       /* If we can't find this entry with the new bfd hash, re-insert
3109          it, and get the traversal restarted.  */
3110       if (! htab_find (got_entries, entry))
3111         {
3112           htab_clear_slot (got_entries, entryp);
3113           entryp = htab_find_slot (got_entries, entry, INSERT);
3114           if (! *entryp)
3115             *entryp = entry;
3116           /* Abort the traversal, since the whole table may have
3117              moved, and leave it up to the parent to restart the
3118              process.  */
3119           *(htab_t *)p = NULL;
3120           return 0;
3121         }
3122       /* We might want to decrement the global_gotno count, but it's
3123          either too early or too late for that at this point.  */
3124     }
3125
3126   return 1;
3127 }
3128
3129 /* Turn indirect got entries in a got_entries table into their final
3130    locations.  */
3131 static void
3132 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3133 {
3134   htab_t got_entries;
3135
3136   do
3137     {
3138       got_entries = g->got_entries;
3139
3140       htab_traverse (got_entries,
3141                      mips_elf_resolve_final_got_entry,
3142                      &got_entries);
3143     }
3144   while (got_entries == NULL);
3145 }
3146
3147 /* Return the offset of an input bfd IBFD's GOT from the beginning of
3148    the primary GOT.  */
3149 static bfd_vma
3150 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
3151 {
3152   if (g->bfd2got == NULL)
3153     return 0;
3154
3155   g = mips_elf_got_for_ibfd (g, ibfd);
3156   if (! g)
3157     return 0;
3158
3159   BFD_ASSERT (g->next);
3160
3161   g = g->next;
3162
3163   return (g->local_gotno + g->global_gotno + g->tls_gotno)
3164     * MIPS_ELF_GOT_SIZE (abfd);
3165 }
3166
3167 /* Turn a single GOT that is too big for 16-bit addressing into
3168    a sequence of GOTs, each one 16-bit addressable.  */
3169
3170 static bfd_boolean
3171 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
3172                     struct mips_got_info *g, asection *got,
3173                     bfd_size_type pages)
3174 {
3175   struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
3176   struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
3177   struct mips_got_info *gg;
3178   unsigned int assign;
3179
3180   g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
3181                                 mips_elf_bfd2got_entry_eq, NULL);
3182   if (g->bfd2got == NULL)
3183     return FALSE;
3184
3185   got_per_bfd_arg.bfd2got = g->bfd2got;
3186   got_per_bfd_arg.obfd = abfd;
3187   got_per_bfd_arg.info = info;
3188
3189   /* Count how many GOT entries each input bfd requires, creating a
3190      map from bfd to got info while at that.  */
3191   htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
3192   if (got_per_bfd_arg.obfd == NULL)
3193     return FALSE;
3194
3195   got_per_bfd_arg.current = NULL;
3196   got_per_bfd_arg.primary = NULL;
3197   /* Taking out PAGES entries is a worst-case estimate.  We could
3198      compute the maximum number of pages that each separate input bfd
3199      uses, but it's probably not worth it.  */
3200   got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (abfd)
3201                                 / MIPS_ELF_GOT_SIZE (abfd))
3202                                - MIPS_RESERVED_GOTNO - pages);
3203   /* The number of globals that will be included in the primary GOT.
3204      See the calls to mips_elf_set_global_got_offset below for more
3205      information.  */
3206   got_per_bfd_arg.global_count = g->global_gotno;
3207
3208   /* Try to merge the GOTs of input bfds together, as long as they
3209      don't seem to exceed the maximum GOT size, choosing one of them
3210      to be the primary GOT.  */
3211   htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
3212   if (got_per_bfd_arg.obfd == NULL)
3213     return FALSE;
3214
3215   /* If we do not find any suitable primary GOT, create an empty one.  */
3216   if (got_per_bfd_arg.primary == NULL)
3217     {
3218       g->next = (struct mips_got_info *)
3219         bfd_alloc (abfd, sizeof (struct mips_got_info));
3220       if (g->next == NULL)
3221         return FALSE;
3222
3223       g->next->global_gotsym = NULL;
3224       g->next->global_gotno = 0;
3225       g->next->local_gotno = 0;
3226       g->next->tls_gotno = 0;
3227       g->next->assigned_gotno = 0;
3228       g->next->tls_assigned_gotno = 0;
3229       g->next->tls_ldm_offset = MINUS_ONE;
3230       g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3231                                               mips_elf_multi_got_entry_eq,
3232                                               NULL);
3233       if (g->next->got_entries == NULL)
3234         return FALSE;
3235       g->next->bfd2got = NULL;
3236     }
3237   else
3238     g->next = got_per_bfd_arg.primary;
3239   g->next->next = got_per_bfd_arg.current;
3240
3241   /* GG is now the master GOT, and G is the primary GOT.  */
3242   gg = g;
3243   g = g->next;
3244
3245   /* Map the output bfd to the primary got.  That's what we're going
3246      to use for bfds that use GOT16 or GOT_PAGE relocations that we
3247      didn't mark in check_relocs, and we want a quick way to find it.
3248      We can't just use gg->next because we're going to reverse the
3249      list.  */
3250   {
3251     struct mips_elf_bfd2got_hash *bfdgot;
3252     void **bfdgotp;
3253
3254     bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
3255       (abfd, sizeof (struct mips_elf_bfd2got_hash));
3256
3257     if (bfdgot == NULL)
3258       return FALSE;
3259
3260     bfdgot->bfd = abfd;
3261     bfdgot->g = g;
3262     bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
3263
3264     BFD_ASSERT (*bfdgotp == NULL);
3265     *bfdgotp = bfdgot;
3266   }
3267
3268   /* The IRIX dynamic linker requires every symbol that is referenced
3269      in a dynamic relocation to be present in the primary GOT, so
3270      arrange for them to appear after those that are actually
3271      referenced.
3272
3273      GNU/Linux could very well do without it, but it would slow down
3274      the dynamic linker, since it would have to resolve every dynamic
3275      symbol referenced in other GOTs more than once, without help from
3276      the cache.  Also, knowing that every external symbol has a GOT
3277      helps speed up the resolution of local symbols too, so GNU/Linux
3278      follows IRIX's practice.
3279
3280      The number 2 is used by mips_elf_sort_hash_table_f to count
3281      global GOT symbols that are unreferenced in the primary GOT, with
3282      an initial dynamic index computed from gg->assigned_gotno, where
3283      the number of unreferenced global entries in the primary GOT is
3284      preserved.  */
3285   if (1)
3286     {
3287       gg->assigned_gotno = gg->global_gotno - g->global_gotno;
3288       g->global_gotno = gg->global_gotno;
3289       set_got_offset_arg.value = 2;
3290     }
3291   else
3292     {
3293       /* This could be used for dynamic linkers that don't optimize
3294          symbol resolution while applying relocations so as to use
3295          primary GOT entries or assuming the symbol is locally-defined.
3296          With this code, we assign lower dynamic indices to global
3297          symbols that are not referenced in the primary GOT, so that
3298          their entries can be omitted.  */
3299       gg->assigned_gotno = 0;
3300       set_got_offset_arg.value = -1;
3301     }
3302
3303   /* Reorder dynamic symbols as described above (which behavior
3304      depends on the setting of VALUE).  */
3305   set_got_offset_arg.g = NULL;
3306   htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
3307                  &set_got_offset_arg);
3308   set_got_offset_arg.value = 1;
3309   htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
3310                  &set_got_offset_arg);
3311   if (! mips_elf_sort_hash_table (info, 1))
3312     return FALSE;
3313
3314   /* Now go through the GOTs assigning them offset ranges.
3315      [assigned_gotno, local_gotno[ will be set to the range of local
3316      entries in each GOT.  We can then compute the end of a GOT by
3317      adding local_gotno to global_gotno.  We reverse the list and make
3318      it circular since then we'll be able to quickly compute the
3319      beginning of a GOT, by computing the end of its predecessor.  To
3320      avoid special cases for the primary GOT, while still preserving
3321      assertions that are valid for both single- and multi-got links,
3322      we arrange for the main got struct to have the right number of
3323      global entries, but set its local_gotno such that the initial
3324      offset of the primary GOT is zero.  Remember that the primary GOT
3325      will become the last item in the circular linked list, so it
3326      points back to the master GOT.  */
3327   gg->local_gotno = -g->global_gotno;
3328   gg->global_gotno = g->global_gotno;
3329   gg->tls_gotno = 0;
3330   assign = 0;
3331   gg->next = gg;
3332
3333   do
3334     {
3335       struct mips_got_info *gn;
3336
3337       assign += MIPS_RESERVED_GOTNO;
3338       g->assigned_gotno = assign;
3339       g->local_gotno += assign + pages;
3340       assign = g->local_gotno + g->global_gotno + g->tls_gotno;
3341
3342       /* Set up any TLS entries.  We always place the TLS entries after
3343          all non-TLS entries.  */
3344       g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
3345       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
3346
3347       /* Take g out of the direct list, and push it onto the reversed
3348          list that gg points to.  */
3349       gn = g->next;
3350       g->next = gg->next;
3351       gg->next = g;
3352       g = gn;
3353
3354       /* Mark global symbols in every non-primary GOT as ineligible for
3355          stubs.  */
3356       if (g)
3357         htab_traverse (g->got_entries, mips_elf_set_no_stub, NULL);
3358     }
3359   while (g);
3360
3361   got->size = (gg->next->local_gotno
3362                     + gg->next->global_gotno
3363                     + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
3364
3365   return TRUE;
3366 }
3367
3368 \f
3369 /* Returns the first relocation of type r_type found, beginning with
3370    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
3371
3372 static const Elf_Internal_Rela *
3373 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
3374                           const Elf_Internal_Rela *relocation,
3375                           const Elf_Internal_Rela *relend)
3376 {
3377   while (relocation < relend)
3378     {
3379       if (ELF_R_TYPE (abfd, relocation->r_info) == r_type)
3380         return relocation;
3381
3382       ++relocation;
3383     }
3384
3385   /* We didn't find it.  */
3386   bfd_set_error (bfd_error_bad_value);
3387   return NULL;
3388 }
3389
3390 /* Return whether a relocation is against a local symbol.  */
3391
3392 static bfd_boolean
3393 mips_elf_local_relocation_p (bfd *input_bfd,
3394                              const Elf_Internal_Rela *relocation,
3395                              asection **local_sections,
3396                              bfd_boolean check_forced)
3397 {
3398   unsigned long r_symndx;
3399   Elf_Internal_Shdr *symtab_hdr;
3400   struct mips_elf_link_hash_entry *h;
3401   size_t extsymoff;
3402
3403   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3404   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3405   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
3406
3407   if (r_symndx < extsymoff)
3408     return TRUE;
3409   if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
3410     return TRUE;
3411
3412   if (check_forced)
3413     {
3414       /* Look up the hash table to check whether the symbol
3415          was forced local.  */
3416       h = (struct mips_elf_link_hash_entry *)
3417         elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
3418       /* Find the real hash-table entry for this symbol.  */
3419       while (h->root.root.type == bfd_link_hash_indirect
3420              || h->root.root.type == bfd_link_hash_warning)
3421         h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3422       if (h->root.forced_local)
3423         return TRUE;
3424     }
3425
3426   return FALSE;
3427 }
3428 \f
3429 /* Sign-extend VALUE, which has the indicated number of BITS.  */
3430
3431 bfd_vma
3432 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
3433 {
3434   if (value & ((bfd_vma) 1 << (bits - 1)))
3435     /* VALUE is negative.  */
3436     value |= ((bfd_vma) - 1) << bits;
3437
3438   return value;
3439 }
3440
3441 /* Return non-zero if the indicated VALUE has overflowed the maximum
3442    range expressible by a signed number with the indicated number of
3443    BITS.  */
3444
3445 static bfd_boolean
3446 mips_elf_overflow_p (bfd_vma value, int bits)
3447 {
3448   bfd_signed_vma svalue = (bfd_signed_vma) value;
3449
3450   if (svalue > (1 << (bits - 1)) - 1)
3451     /* The value is too big.  */
3452     return TRUE;
3453   else if (svalue < -(1 << (bits - 1)))
3454     /* The value is too small.  */
3455     return TRUE;
3456
3457   /* All is well.  */
3458   return FALSE;
3459 }
3460
3461 /* Calculate the %high function.  */
3462
3463 static bfd_vma
3464 mips_elf_high (bfd_vma value)
3465 {
3466   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
3467 }
3468
3469 /* Calculate the %higher function.  */
3470
3471 static bfd_vma
3472 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
3473 {
3474 #ifdef BFD64
3475   return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
3476 #else
3477   abort ();
3478   return MINUS_ONE;
3479 #endif
3480 }
3481
3482 /* Calculate the %highest function.  */
3483
3484 static bfd_vma
3485 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
3486 {
3487 #ifdef BFD64
3488   return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
3489 #else
3490   abort ();
3491   return MINUS_ONE;
3492 #endif
3493 }
3494 \f
3495 /* Create the .compact_rel section.  */
3496
3497 static bfd_boolean
3498 mips_elf_create_compact_rel_section
3499   (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
3500 {
3501   flagword flags;
3502   register asection *s;
3503
3504   if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
3505     {
3506       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
3507                | SEC_READONLY);
3508
3509       s = bfd_make_section (abfd, ".compact_rel");
3510       if (s == NULL
3511           || ! bfd_set_section_flags (abfd, s, flags)
3512           || ! bfd_set_section_alignment (abfd, s,
3513                                           MIPS_ELF_LOG_FILE_ALIGN (abfd)))
3514         return FALSE;
3515
3516       s->size = sizeof (Elf32_External_compact_rel);
3517     }
3518
3519   return TRUE;
3520 }
3521
3522 /* Create the .got section to hold the global offset table.  */
3523
3524 static bfd_boolean
3525 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info,
3526                              bfd_boolean maybe_exclude)
3527 {
3528   flagword flags;
3529   register asection *s;
3530   struct elf_link_hash_entry *h;
3531   struct bfd_link_hash_entry *bh;
3532   struct mips_got_info *g;
3533   bfd_size_type amt;
3534
3535   /* This function may be called more than once.  */
3536   s = mips_elf_got_section (abfd, TRUE);
3537   if (s)
3538     {
3539       if (! maybe_exclude)
3540         s->flags &= ~SEC_EXCLUDE;
3541       return TRUE;
3542     }
3543
3544   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3545            | SEC_LINKER_CREATED);
3546
3547   if (maybe_exclude)
3548     flags |= SEC_EXCLUDE;
3549
3550   /* We have to use an alignment of 2**4 here because this is hardcoded
3551      in the function stub generation and in the linker script.  */
3552   s = bfd_make_section (abfd, ".got");
3553   if (s == NULL
3554       || ! bfd_set_section_flags (abfd, s, flags)
3555       || ! bfd_set_section_alignment (abfd, s, 4))
3556     return FALSE;
3557
3558   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
3559      linker script because we don't want to define the symbol if we
3560      are not creating a global offset table.  */
3561   bh = NULL;
3562   if (! (_bfd_generic_link_add_one_symbol
3563          (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
3564           0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
3565     return FALSE;
3566
3567   h = (struct elf_link_hash_entry *) bh;
3568   h->non_elf = 0;
3569   h->def_regular = 1;
3570   h->type = STT_OBJECT;
3571
3572   if (info->shared
3573       && ! bfd_elf_link_record_dynamic_symbol (info, h))
3574     return FALSE;
3575
3576   amt = sizeof (struct mips_got_info);
3577   g = bfd_alloc (abfd, amt);
3578   if (g == NULL)
3579     return FALSE;
3580   g->global_gotsym = NULL;
3581   g->global_gotno = 0;
3582   g->tls_gotno = 0;
3583   g->local_gotno = MIPS_RESERVED_GOTNO;
3584   g->assigned_gotno = MIPS_RESERVED_GOTNO;
3585   g->bfd2got = NULL;
3586   g->next = NULL;
3587   g->tls_ldm_offset = MINUS_ONE;
3588   g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3589                                     mips_elf_got_entry_eq, NULL);
3590   if (g->got_entries == NULL)
3591     return FALSE;
3592   mips_elf_section_data (s)->u.got_info = g;
3593   mips_elf_section_data (s)->elf.this_hdr.sh_flags
3594     |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3595
3596   return TRUE;
3597 }
3598 \f
3599 /* Calculate the value produced by the RELOCATION (which comes from
3600    the INPUT_BFD).  The ADDEND is the addend to use for this
3601    RELOCATION; RELOCATION->R_ADDEND is ignored.
3602
3603    The result of the relocation calculation is stored in VALUEP.
3604    REQUIRE_JALXP indicates whether or not the opcode used with this
3605    relocation must be JALX.
3606
3607    This function returns bfd_reloc_continue if the caller need take no
3608    further action regarding this relocation, bfd_reloc_notsupported if
3609    something goes dramatically wrong, bfd_reloc_overflow if an
3610    overflow occurs, and bfd_reloc_ok to indicate success.  */
3611
3612 static bfd_reloc_status_type
3613 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
3614                                asection *input_section,
3615                                struct bfd_link_info *info,
3616                                const Elf_Internal_Rela *relocation,
3617                                bfd_vma addend, reloc_howto_type *howto,
3618                                Elf_Internal_Sym *local_syms,
3619                                asection **local_sections, bfd_vma *valuep,
3620                                const char **namep, bfd_boolean *require_jalxp,
3621                                bfd_boolean save_addend)
3622 {
3623   /* The eventual value we will return.  */
3624   bfd_vma value;
3625   /* The address of the symbol against which the relocation is
3626      occurring.  */
3627   bfd_vma symbol = 0;
3628   /* The final GP value to be used for the relocatable, executable, or
3629      shared object file being produced.  */
3630   bfd_vma gp = MINUS_ONE;
3631   /* The place (section offset or address) of the storage unit being
3632      relocated.  */
3633   bfd_vma p;
3634   /* The value of GP used to create the relocatable object.  */
3635   bfd_vma gp0 = MINUS_ONE;
3636   /* The offset into the global offset table at which the address of
3637      the relocation entry symbol, adjusted by the addend, resides
3638      during execution.  */
3639   bfd_vma g = MINUS_ONE;
3640   /* The section in which the symbol referenced by the relocation is
3641      located.  */
3642   asection *sec = NULL;
3643   struct mips_elf_link_hash_entry *h = NULL;
3644   /* TRUE if the symbol referred to by this relocation is a local
3645      symbol.  */
3646   bfd_boolean local_p, was_local_p;
3647   /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
3648   bfd_boolean gp_disp_p = FALSE;
3649   Elf_Internal_Shdr *symtab_hdr;
3650   size_t extsymoff;
3651   unsigned long r_symndx;
3652   int r_type;
3653   /* TRUE if overflow occurred during the calculation of the
3654      relocation value.  */
3655   bfd_boolean overflowed_p;
3656   /* TRUE if this relocation refers to a MIPS16 function.  */
3657   bfd_boolean target_is_16_bit_code_p = FALSE;
3658
3659   /* Parse the relocation.  */
3660   r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3661   r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
3662   p = (input_section->output_section->vma
3663        + input_section->output_offset
3664        + relocation->r_offset);
3665
3666   /* Assume that there will be no overflow.  */
3667   overflowed_p = FALSE;
3668
3669   /* Figure out whether or not the symbol is local, and get the offset
3670      used in the array of hash table entries.  */
3671   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3672   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
3673                                          local_sections, FALSE);
3674   was_local_p = local_p;
3675   if (! elf_bad_symtab (input_bfd))
3676     extsymoff = symtab_hdr->sh_info;
3677   else
3678     {
3679       /* The symbol table does not follow the rule that local symbols
3680          must come before globals.  */
3681       extsymoff = 0;
3682     }
3683
3684   /* Figure out the value of the symbol.  */
3685   if (local_p)
3686     {
3687       Elf_Internal_Sym *sym;
3688
3689       sym = local_syms + r_symndx;
3690       sec = local_sections[r_symndx];
3691
3692       symbol = sec->output_section->vma + sec->output_offset;
3693       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
3694           || (sec->flags & SEC_MERGE))
3695         symbol += sym->st_value;
3696       if ((sec->flags & SEC_MERGE)
3697           && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3698         {
3699           addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
3700           addend -= symbol;
3701           addend += sec->output_section->vma + sec->output_offset;
3702         }
3703
3704       /* MIPS16 text labels should be treated as odd.  */
3705       if (sym->st_other == STO_MIPS16)
3706         ++symbol;
3707
3708       /* Record the name of this symbol, for our caller.  */
3709       *namep = bfd_elf_string_from_elf_section (input_bfd,
3710                                                 symtab_hdr->sh_link,
3711                                                 sym->st_name);
3712       if (*namep == '\0')
3713         *namep = bfd_section_name (input_bfd, sec);
3714
3715       target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
3716     }
3717   else
3718     {
3719       /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
3720
3721       /* For global symbols we look up the symbol in the hash-table.  */
3722       h = ((struct mips_elf_link_hash_entry *)
3723            elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
3724       /* Find the real hash-table entry for this symbol.  */
3725       while (h->root.root.type == bfd_link_hash_indirect
3726              || h->root.root.type == bfd_link_hash_warning)
3727         h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3728
3729       /* Record the name of this symbol, for our caller.  */
3730       *namep = h->root.root.root.string;
3731
3732       /* See if this is the special _gp_disp symbol.  Note that such a
3733          symbol must always be a global symbol.  */
3734       if (strcmp (*namep, "_gp_disp") == 0
3735           && ! NEWABI_P (input_bfd))
3736         {
3737           /* Relocations against _gp_disp are permitted only with
3738              R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
3739           if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16
3740               && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
3741             return bfd_reloc_notsupported;
3742
3743           gp_disp_p = TRUE;
3744         }
3745       /* If this symbol is defined, calculate its address.  Note that
3746          _gp_disp is a magic symbol, always implicitly defined by the
3747          linker, so it's inappropriate to check to see whether or not
3748          its defined.  */
3749       else if ((h->root.root.type == bfd_link_hash_defined
3750                 || h->root.root.type == bfd_link_hash_defweak)
3751                && h->root.root.u.def.section)
3752         {
3753           sec = h->root.root.u.def.section;
3754           if (sec->output_section)
3755             symbol = (h->root.root.u.def.value
3756                       + sec->output_section->vma
3757                       + sec->output_offset);
3758           else
3759             symbol = h->root.root.u.def.value;
3760         }
3761       else if (h->root.root.type == bfd_link_hash_undefweak)
3762         /* We allow relocations against undefined weak symbols, giving
3763            it the value zero, so that you can undefined weak functions
3764            and check to see if they exist by looking at their
3765            addresses.  */
3766         symbol = 0;
3767       else if (info->unresolved_syms_in_objects == RM_IGNORE
3768                && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
3769         symbol = 0;
3770       else if (strcmp (*namep, SGI_COMPAT (input_bfd)
3771                        ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
3772         {
3773           /* If this is a dynamic link, we should have created a
3774              _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
3775              in in _bfd_mips_elf_create_dynamic_sections.
3776              Otherwise, we should define the symbol with a value of 0.
3777              FIXME: It should probably get into the symbol table
3778              somehow as well.  */
3779           BFD_ASSERT (! info->shared);
3780           BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
3781           symbol = 0;
3782         }
3783       else
3784         {
3785           if (! ((*info->callbacks->undefined_symbol)
3786                  (info, h->root.root.root.string, input_bfd,
3787                   input_section, relocation->r_offset,
3788                   (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
3789                    || ELF_ST_VISIBILITY (h->root.other))))
3790             return bfd_reloc_undefined;
3791           symbol = 0;
3792         }
3793
3794       target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
3795     }
3796
3797   /* If this is a 32- or 64-bit call to a 16-bit function with a stub, we
3798      need to redirect the call to the stub, unless we're already *in*
3799      a stub.  */
3800   if (r_type != R_MIPS16_26 && !info->relocatable
3801       && ((h != NULL && h->fn_stub != NULL)
3802           || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
3803               && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
3804       && !mips_elf_stub_section_p (input_bfd, input_section))
3805     {
3806       /* This is a 32- or 64-bit call to a 16-bit function.  We should
3807          have already noticed that we were going to need the
3808          stub.  */
3809       if (local_p)
3810         sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
3811       else
3812         {
3813           BFD_ASSERT (h->need_fn_stub);
3814           sec = h->fn_stub;
3815         }
3816
3817       symbol = sec->output_section->vma + sec->output_offset;
3818     }
3819   /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
3820      need to redirect the call to the stub.  */
3821   else if (r_type == R_MIPS16_26 && !info->relocatable
3822            && h != NULL
3823            && (h->call_stub != NULL || h->call_fp_stub != NULL)
3824            && !target_is_16_bit_code_p)
3825     {
3826       /* If both call_stub and call_fp_stub are defined, we can figure
3827          out which one to use by seeing which one appears in the input
3828          file.  */
3829       if (h->call_stub != NULL && h->call_fp_stub != NULL)
3830         {
3831           asection *o;
3832
3833           sec = NULL;
3834           for (o = input_bfd->sections; o != NULL; o = o->next)
3835             {
3836               if (strncmp (bfd_get_section_name (input_bfd, o),
3837                            CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
3838                 {
3839                   sec = h->call_fp_stub;
3840                   break;
3841                 }
3842             }
3843           if (sec == NULL)
3844             sec = h->call_stub;
3845         }
3846       else if (h->call_stub != NULL)
3847         sec = h->call_stub;
3848       else
3849         sec = h->call_fp_stub;
3850
3851       BFD_ASSERT (sec->size > 0);
3852       symbol = sec->output_section->vma + sec->output_offset;
3853     }
3854
3855   /* Calls from 16-bit code to 32-bit code and vice versa require the
3856      special jalx instruction.  */
3857   *require_jalxp = (!info->relocatable
3858                     && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
3859                         || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
3860
3861   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
3862                                          local_sections, TRUE);
3863
3864   /* If we haven't already determined the GOT offset, or the GP value,
3865      and we're going to need it, get it now.  */
3866   switch (r_type)
3867     {
3868     case R_MIPS_GOT_PAGE:
3869     case R_MIPS_GOT_OFST:
3870       /* We need to decay to GOT_DISP/addend if the symbol doesn't
3871          bind locally.  */
3872       local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
3873       if (local_p || r_type == R_MIPS_GOT_OFST)
3874         break;
3875       /* Fall through.  */
3876
3877     case R_MIPS_CALL16:
3878     case R_MIPS_GOT16:
3879     case R_MIPS_GOT_DISP:
3880     case R_MIPS_GOT_HI16:
3881     case R_MIPS_CALL_HI16:
3882     case R_MIPS_GOT_LO16:
3883     case R_MIPS_CALL_LO16:
3884     case R_MIPS_TLS_GD:
3885     case R_MIPS_TLS_GOTTPREL:
3886     case R_MIPS_TLS_LDM:
3887       /* Find the index into the GOT where this value is located.  */
3888       if (r_type == R_MIPS_TLS_LDM)
3889         {
3890           g = mips_elf_local_got_index (abfd, input_bfd, info, 0, 0, NULL,
3891                                         r_type);
3892           if (g == MINUS_ONE)
3893             return bfd_reloc_outofrange;
3894         }
3895       else if (!local_p)
3896         {
3897           /* GOT_PAGE may take a non-zero addend, that is ignored in a
3898              GOT_PAGE relocation that decays to GOT_DISP because the
3899              symbol turns out to be global.  The addend is then added
3900              as GOT_OFST.  */
3901           BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
3902           g = mips_elf_global_got_index (elf_hash_table (info)->dynobj,
3903                                          input_bfd,
3904                                          (struct elf_link_hash_entry *) h,
3905                                          r_type, info);
3906           if (h->tls_type == GOT_NORMAL
3907               && (! elf_hash_table(info)->dynamic_sections_created
3908                   || (info->shared
3909                       && (info->symbolic || h->root.dynindx == -1)
3910                       && h->root.def_regular)))
3911             {
3912               /* This is a static link or a -Bsymbolic link.  The
3913                  symbol is defined locally, or was forced to be local.
3914                  We must initialize this entry in the GOT.  */
3915               bfd *tmpbfd = elf_hash_table (info)->dynobj;
3916               asection *sgot = mips_elf_got_section (tmpbfd, FALSE);
3917               MIPS_ELF_PUT_WORD (tmpbfd, symbol, sgot->contents + g);
3918             }
3919         }
3920       else if (r_type == R_MIPS_GOT16 || r_type == R_MIPS_CALL16)
3921         /* There's no need to create a local GOT entry here; the
3922            calculation for a local GOT16 entry does not involve G.  */
3923         break;
3924       else
3925         {
3926           g = mips_elf_local_got_index (abfd, input_bfd,
3927                                         info, symbol + addend, r_symndx, h,
3928                                         r_type);
3929           if (g == MINUS_ONE)
3930             return bfd_reloc_outofrange;
3931         }
3932
3933       /* Convert GOT indices to actual offsets.  */
3934       g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
3935                                           abfd, input_bfd, g);
3936       break;
3937
3938     case R_MIPS_HI16:
3939     case R_MIPS_LO16:
3940     case R_MIPS_GPREL16:
3941     case R_MIPS_GPREL32:
3942     case R_MIPS_LITERAL:
3943     case R_MIPS16_HI16:
3944     case R_MIPS16_LO16:
3945     case R_MIPS16_GPREL:
3946       gp0 = _bfd_get_gp_value (input_bfd);
3947       gp = _bfd_get_gp_value (abfd);
3948       if (elf_hash_table (info)->dynobj)
3949         gp += mips_elf_adjust_gp (abfd,
3950                                   mips_elf_got_info
3951                                   (elf_hash_table (info)->dynobj, NULL),
3952                                   input_bfd);
3953       break;
3954
3955     default:
3956       break;
3957     }
3958
3959   /* Figure out what kind of relocation is being performed.  */
3960   switch (r_type)
3961     {
3962     case R_MIPS_NONE:
3963       return bfd_reloc_continue;
3964
3965     case R_MIPS_16:
3966       value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
3967       overflowed_p = mips_elf_overflow_p (value, 16);
3968       break;
3969
3970     case R_MIPS_32:
3971     case R_MIPS_REL32:
3972     case R_MIPS_64:
3973       if ((info->shared
3974            || (elf_hash_table (info)->dynamic_sections_created
3975                && h != NULL
3976                && h->root.def_dynamic
3977                && !h->root.def_regular))
3978           && r_symndx != 0
3979           && (input_section->flags & SEC_ALLOC) != 0)
3980         {
3981           /* If we're creating a shared library, or this relocation is
3982              against a symbol in a shared library, then we can't know
3983              where the symbol will end up.  So, we create a relocation
3984              record in the output, and leave the job up to the dynamic
3985              linker.  */
3986           value = addend;
3987           if (!mips_elf_create_dynamic_relocation (abfd,
3988                                                    info,
3989                                                    relocation,
3990                                                    h,
3991                                                    sec,
3992                                                    symbol,
3993                                                    &value,
3994                                                    input_section))
3995             return bfd_reloc_undefined;
3996         }
3997       else
3998         {
3999           if (r_type != R_MIPS_REL32)
4000             value = symbol + addend;
4001           else
4002             value = addend;
4003         }
4004       value &= howto->dst_mask;
4005       break;
4006
4007     case R_MIPS_PC32:
4008       value = symbol + addend - p;
4009       value &= howto->dst_mask;
4010       break;
4011
4012     case R_MIPS_GNU_REL16_S2:
4013       value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
4014       overflowed_p = mips_elf_overflow_p (value, 18);
4015       value = (value >> 2) & howto->dst_mask;
4016       break;
4017
4018     case R_MIPS16_26:
4019       /* The calculation for R_MIPS16_26 is just the same as for an
4020          R_MIPS_26.  It's only the storage of the relocated field into
4021          the output file that's different.  That's handled in
4022          mips_elf_perform_relocation.  So, we just fall through to the
4023          R_MIPS_26 case here.  */
4024     case R_MIPS_26:
4025       if (local_p)
4026         value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
4027       else
4028         {
4029           value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
4030           if (h->root.root.type != bfd_link_hash_undefweak)
4031             overflowed_p = (value >> 26) != ((p + 4) >> 28);
4032         }
4033       value &= howto->dst_mask;
4034       break;
4035
4036     case R_MIPS_TLS_DTPREL_HI16:
4037       value = (mips_elf_high (addend + symbol - dtprel_base (info))
4038                & howto->dst_mask);
4039       break;
4040
4041     case R_MIPS_TLS_DTPREL_LO16:
4042       value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
4043       break;
4044
4045     case R_MIPS_TLS_TPREL_HI16:
4046       value = (mips_elf_high (addend + symbol - tprel_base (info))
4047                & howto->dst_mask);
4048       break;
4049
4050     case R_MIPS_TLS_TPREL_LO16:
4051       value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
4052       break;
4053
4054     case R_MIPS_HI16:
4055     case R_MIPS16_HI16:
4056       if (!gp_disp_p)
4057         {
4058           value = mips_elf_high (addend + symbol);
4059           value &= howto->dst_mask;
4060         }
4061       else
4062         {
4063           /* For MIPS16 ABI code we generate this sequence
4064                 0: li      $v0,%hi(_gp_disp)
4065                 4: addiupc $v1,%lo(_gp_disp)
4066                 8: sll     $v0,16
4067                12: addu    $v0,$v1
4068                14: move    $gp,$v0
4069              So the offsets of hi and lo relocs are the same, but the
4070              $pc is four higher than $t9 would be, so reduce
4071              both reloc addends by 4. */
4072           if (r_type == R_MIPS16_HI16)
4073             value = mips_elf_high (addend + gp - p - 4);
4074           else
4075             value = mips_elf_high (addend + gp - p);
4076           overflowed_p = mips_elf_overflow_p (value, 16);
4077         }
4078       break;
4079
4080     case R_MIPS_LO16:
4081     case R_MIPS16_LO16:
4082       if (!gp_disp_p)
4083         value = (symbol + addend) & howto->dst_mask;
4084       else
4085         {
4086           /* See the comment for R_MIPS16_HI16 above for the reason
4087              for this conditional.  */
4088           if (r_type == R_MIPS16_LO16)
4089             value = addend + gp - p;
4090           else
4091             value = addend + gp - p + 4;
4092           /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
4093              for overflow.  But, on, say, IRIX5, relocations against
4094              _gp_disp are normally generated from the .cpload
4095              pseudo-op.  It generates code that normally looks like
4096              this:
4097
4098                lui    $gp,%hi(_gp_disp)
4099                addiu  $gp,$gp,%lo(_gp_disp)
4100                addu   $gp,$gp,$t9
4101
4102              Here $t9 holds the address of the function being called,
4103              as required by the MIPS ELF ABI.  The R_MIPS_LO16
4104              relocation can easily overflow in this situation, but the
4105              R_MIPS_HI16 relocation will handle the overflow.
4106              Therefore, we consider this a bug in the MIPS ABI, and do
4107              not check for overflow here.  */
4108         }
4109       break;
4110
4111     case R_MIPS_LITERAL:
4112       /* Because we don't merge literal sections, we can handle this
4113          just like R_MIPS_GPREL16.  In the long run, we should merge
4114          shared literals, and then we will need to additional work
4115          here.  */
4116
4117       /* Fall through.  */
4118
4119     case R_MIPS16_GPREL:
4120       /* The R_MIPS16_GPREL performs the same calculation as
4121          R_MIPS_GPREL16, but stores the relocated bits in a different
4122          order.  We don't need to do anything special here; the
4123          differences are handled in mips_elf_perform_relocation.  */
4124     case R_MIPS_GPREL16:
4125       /* Only sign-extend the addend if it was extracted from the
4126          instruction.  If the addend was separate, leave it alone,
4127          otherwise we may lose significant bits.  */
4128       if (howto->partial_inplace)
4129         addend = _bfd_mips_elf_sign_extend (addend, 16);
4130       value = symbol + addend - gp;
4131       /* If the symbol was local, any earlier relocatable links will
4132          have adjusted its addend with the gp offset, so compensate
4133          for that now.  Don't do it for symbols forced local in this
4134          link, though, since they won't have had the gp offset applied
4135          to them before.  */
4136       if (was_local_p)
4137         value += gp0;
4138       overflowed_p = mips_elf_overflow_p (value, 16);
4139       break;
4140
4141     case R_MIPS_GOT16:
4142     case R_MIPS_CALL16:
4143       if (local_p)
4144         {
4145           bfd_boolean forced;
4146
4147           /* The special case is when the symbol is forced to be local.  We
4148              need the full address in the GOT since no R_MIPS_LO16 relocation
4149              follows.  */
4150           forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
4151                                                   local_sections, FALSE);
4152           value = mips_elf_got16_entry (abfd, input_bfd, info,
4153                                         symbol + addend, forced);
4154           if (value == MINUS_ONE)
4155             return bfd_reloc_outofrange;
4156           value
4157             = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
4158                                               abfd, input_bfd, value);
4159           overflowed_p = mips_elf_overflow_p (value, 16);
4160           break;
4161         }
4162
4163       /* Fall through.  */
4164
4165     case R_MIPS_TLS_GD:
4166     case R_MIPS_TLS_GOTTPREL:
4167     case R_MIPS_TLS_LDM:
4168     case R_MIPS_GOT_DISP:
4169     got_disp:
4170       value = g;
4171       overflowed_p = mips_elf_overflow_p (value, 16);
4172       break;
4173
4174     case R_MIPS_GPREL32:
4175       value = (addend + symbol + gp0 - gp);
4176       if (!save_addend)
4177         value &= howto->dst_mask;
4178       break;
4179
4180     case R_MIPS_PC16:
4181       value = _bfd_mips_elf_sign_extend (addend, 16) + symbol - p;
4182       overflowed_p = mips_elf_overflow_p (value, 16);
4183       break;
4184
4185     case R_MIPS_GOT_HI16:
4186     case R_MIPS_CALL_HI16:
4187       /* We're allowed to handle these two relocations identically.
4188          The dynamic linker is allowed to handle the CALL relocations
4189          differently by creating a lazy evaluation stub.  */
4190       value = g;
4191       value = mips_elf_high (value);
4192       value &= howto->dst_mask;
4193       break;
4194
4195     case R_MIPS_GOT_LO16:
4196     case R_MIPS_CALL_LO16:
4197       value = g & howto->dst_mask;
4198       break;
4199
4200     case R_MIPS_GOT_PAGE:
4201       /* GOT_PAGE relocations that reference non-local symbols decay
4202          to GOT_DISP.  The corresponding GOT_OFST relocation decays to
4203          0.  */
4204       if (! local_p)
4205         goto got_disp;
4206       value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
4207       if (value == MINUS_ONE)
4208         return bfd_reloc_outofrange;
4209       value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
4210                                               abfd, input_bfd, value);
4211       overflowed_p = mips_elf_overflow_p (value, 16);
4212       break;
4213
4214     case R_MIPS_GOT_OFST:
4215       if (local_p)
4216         mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
4217       else
4218         value = addend;
4219       overflowed_p = mips_elf_overflow_p (value, 16);
4220       break;
4221
4222     case R_MIPS_SUB:
4223       value = symbol - addend;
4224       value &= howto->dst_mask;
4225       break;
4226
4227     case R_MIPS_HIGHER:
4228       value = mips_elf_higher (addend + symbol);
4229       value &= howto->dst_mask;
4230       break;
4231
4232     case R_MIPS_HIGHEST:
4233       value = mips_elf_highest (addend + symbol);
4234       value &= howto->dst_mask;
4235       break;
4236
4237     case R_MIPS_SCN_DISP:
4238       value = symbol + addend - sec->output_offset;
4239       value &= howto->dst_mask;
4240       break;
4241
4242     case R_MIPS_JALR:
4243       /* This relocation is only a hint.  In some cases, we optimize
4244          it into a bal instruction.  But we don't try to optimize
4245          branches to the PLT; that will wind up wasting time.  */
4246       if (h != NULL && h->root.plt.offset != (bfd_vma) -1)
4247         return bfd_reloc_continue;
4248       value = symbol + addend;
4249       break;
4250
4251     case R_MIPS_PJUMP:
4252     case R_MIPS_GNU_VTINHERIT:
4253     case R_MIPS_GNU_VTENTRY:
4254       /* We don't do anything with these at present.  */
4255       return bfd_reloc_continue;
4256
4257     default:
4258       /* An unrecognized relocation type.  */
4259       return bfd_reloc_notsupported;
4260     }
4261
4262   /* Store the VALUE for our caller.  */
4263   *valuep = value;
4264   return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
4265 }
4266
4267 /* Obtain the field relocated by RELOCATION.  */
4268
4269 static bfd_vma
4270 mips_elf_obtain_contents (reloc_howto_type *howto,
4271                           const Elf_Internal_Rela *relocation,
4272                           bfd *input_bfd, bfd_byte *contents)
4273 {
4274   bfd_vma x;
4275   bfd_byte *location = contents + relocation->r_offset;
4276
4277   /* Obtain the bytes.  */
4278   x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
4279
4280   return x;
4281 }
4282
4283 /* It has been determined that the result of the RELOCATION is the
4284    VALUE.  Use HOWTO to place VALUE into the output file at the
4285    appropriate position.  The SECTION is the section to which the
4286    relocation applies.  If REQUIRE_JALX is TRUE, then the opcode used
4287    for the relocation must be either JAL or JALX, and it is
4288    unconditionally converted to JALX.
4289
4290    Returns FALSE if anything goes wrong.  */
4291
4292 static bfd_boolean
4293 mips_elf_perform_relocation (struct bfd_link_info *info,
4294                              reloc_howto_type *howto,
4295                              const Elf_Internal_Rela *relocation,
4296                              bfd_vma value, bfd *input_bfd,
4297                              asection *input_section, bfd_byte *contents,
4298                              bfd_boolean require_jalx)
4299 {
4300   bfd_vma x;
4301   bfd_byte *location;
4302   int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4303
4304   /* Figure out where the relocation is occurring.  */
4305   location = contents + relocation->r_offset;
4306
4307   _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
4308
4309   /* Obtain the current value.  */
4310   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
4311
4312   /* Clear the field we are setting.  */
4313   x &= ~howto->dst_mask;
4314
4315   /* Set the field.  */
4316   x |= (value & howto->dst_mask);
4317
4318   /* If required, turn JAL into JALX.  */
4319   if (require_jalx)
4320     {
4321       bfd_boolean ok;
4322       bfd_vma opcode = x >> 26;
4323       bfd_vma jalx_opcode;
4324
4325       /* Check to see if the opcode is already JAL or JALX.  */
4326       if (r_type == R_MIPS16_26)
4327         {
4328           ok = ((opcode == 0x6) || (opcode == 0x7));
4329           jalx_opcode = 0x7;
4330         }
4331       else
4332         {
4333           ok = ((opcode == 0x3) || (opcode == 0x1d));
4334           jalx_opcode = 0x1d;
4335         }
4336
4337       /* If the opcode is not JAL or JALX, there's a problem.  */
4338       if (!ok)
4339         {
4340           (*_bfd_error_handler)
4341             (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
4342              input_bfd,
4343              input_section,
4344              (unsigned long) relocation->r_offset);
4345           bfd_set_error (bfd_error_bad_value);
4346           return FALSE;
4347         }
4348
4349       /* Make this the JALX opcode.  */
4350       x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
4351     }
4352
4353   /* On the RM9000, bal is faster than jal, because bal uses branch
4354      prediction hardware.  If we are linking for the RM9000, and we
4355      see jal, and bal fits, use it instead.  Note that this
4356      transformation should be safe for all architectures.  */
4357   if (bfd_get_mach (input_bfd) == bfd_mach_mips9000
4358       && !info->relocatable
4359       && !require_jalx
4360       && ((r_type == R_MIPS_26 && (x >> 26) == 0x3)         /* jal addr */
4361           || (r_type == R_MIPS_JALR && x == 0x0320f809)))   /* jalr t9 */
4362     {
4363       bfd_vma addr;
4364       bfd_vma dest;
4365       bfd_signed_vma off;
4366
4367       addr = (input_section->output_section->vma
4368               + input_section->output_offset
4369               + relocation->r_offset
4370               + 4);
4371       if (r_type == R_MIPS_26)
4372         dest = (value << 2) | ((addr >> 28) << 28);
4373       else
4374         dest = value;
4375       off = dest - addr;
4376       if (off <= 0x1ffff && off >= -0x20000)
4377         x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
4378     }
4379
4380   /* Put the value into the output.  */
4381   bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
4382
4383   _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
4384                                 location);
4385
4386   return TRUE;
4387 }
4388
4389 /* Returns TRUE if SECTION is a MIPS16 stub section.  */
4390
4391 static bfd_boolean
4392 mips_elf_stub_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *section)
4393 {
4394   const char *name = bfd_get_section_name (abfd, section);
4395
4396   return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
4397           || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
4398           || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
4399 }
4400 \f
4401 /* Add room for N relocations to the .rel.dyn section in ABFD.  */
4402
4403 static void
4404 mips_elf_allocate_dynamic_relocations (bfd *abfd, unsigned int n)
4405 {
4406   asection *s;
4407
4408   s = mips_elf_rel_dyn_section (abfd, FALSE);
4409   BFD_ASSERT (s != NULL);
4410
4411   if (s->size == 0)
4412     {
4413       /* Make room for a null element.  */
4414       s->size += MIPS_ELF_REL_SIZE (abfd);
4415       ++s->reloc_count;
4416     }
4417   s->size += n * MIPS_ELF_REL_SIZE (abfd);
4418 }
4419
4420 /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
4421    is the original relocation, which is now being transformed into a
4422    dynamic relocation.  The ADDENDP is adjusted if necessary; the
4423    caller should store the result in place of the original addend.  */
4424
4425 static bfd_boolean
4426 mips_elf_create_dynamic_relocation (bfd *output_bfd,
4427                                     struct bfd_link_info *info,
4428                                     const Elf_Internal_Rela *rel,
4429                                     struct mips_elf_link_hash_entry *h,
4430                                     asection *sec, bfd_vma symbol,
4431                                     bfd_vma *addendp, asection *input_section)
4432 {
4433   Elf_Internal_Rela outrel[3];
4434   asection *sreloc;
4435   bfd *dynobj;
4436   int r_type;
4437   long indx;
4438   bfd_boolean defined_p;
4439
4440   r_type = ELF_R_TYPE (output_bfd, rel->r_info);
4441   dynobj = elf_hash_table (info)->dynobj;
4442   sreloc = mips_elf_rel_dyn_section (dynobj, FALSE);
4443   BFD_ASSERT (sreloc != NULL);
4444   BFD_ASSERT (sreloc->contents != NULL);
4445   BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
4446               < sreloc->size);
4447
4448   outrel[0].r_offset =
4449     _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
4450   outrel[1].r_offset =
4451     _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
4452   outrel[2].r_offset =
4453     _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
4454
4455   if (outrel[0].r_offset == MINUS_ONE)
4456     /* The relocation field has been deleted.  */
4457     return TRUE;
4458
4459   if (outrel[0].r_offset == MINUS_TWO)
4460     {
4461       /* The relocation field has been converted into a relative value of
4462          some sort.  Functions like _bfd_elf_write_section_eh_frame expect
4463          the field to be fully relocated, so add in the symbol's value.  */
4464       *addendp += symbol;
4465       return TRUE;
4466     }
4467
4468   /* We must now calculate the dynamic symbol table index to use
4469      in the relocation.  */
4470   if (h != NULL
4471       && (! info->symbolic || !h->root.def_regular)
4472       /* h->root.dynindx may be -1 if this symbol was marked to
4473          become local.  */
4474       && h->root.dynindx != -1)
4475     {
4476       indx = h->root.dynindx;
4477       if (SGI_COMPAT (output_bfd))
4478         defined_p = h->root.def_regular;
4479       else
4480         /* ??? glibc's ld.so just adds the final GOT entry to the
4481            relocation field.  It therefore treats relocs against
4482            defined symbols in the same way as relocs against
4483            undefined symbols.  */
4484         defined_p = FALSE;
4485     }
4486   else
4487     {
4488       if (sec != NULL && bfd_is_abs_section (sec))
4489         indx = 0;
4490       else if (sec == NULL || sec->owner == NULL)
4491         {
4492           bfd_set_error (bfd_error_bad_value);
4493           return FALSE;
4494         }
4495       else
4496         {
4497           indx = elf_section_data (sec->output_section)->dynindx;
4498           if (indx == 0)
4499             abort ();
4500         }
4501
4502       /* Instead of generating a relocation using the section
4503          symbol, we may as well make it a fully relative
4504          relocation.  We want to avoid generating relocations to
4505          local symbols because we used to generate them
4506          incorrectly, without adding the original symbol value,
4507          which is mandated by the ABI for section symbols.  In
4508          order to give dynamic loaders and applications time to
4509          phase out the incorrect use, we refrain from emitting
4510          section-relative relocations.  It's not like they're
4511          useful, after all.  This should be a bit more efficient
4512          as well.  */
4513       /* ??? Although this behavior is compatible with glibc's ld.so,
4514          the ABI says that relocations against STN_UNDEF should have
4515          a symbol value of 0.  Irix rld honors this, so relocations
4516          against STN_UNDEF have no effect.  */
4517       if (!SGI_COMPAT (output_bfd))
4518         indx = 0;
4519       defined_p = TRUE;
4520     }
4521
4522   /* If the relocation was previously an absolute relocation and
4523      this symbol will not be referred to by the relocation, we must
4524      adjust it by the value we give it in the dynamic symbol table.
4525      Otherwise leave the job up to the dynamic linker.  */
4526   if (defined_p && r_type != R_MIPS_REL32)
4527     *addendp += symbol;
4528
4529   /* The relocation is always an REL32 relocation because we don't
4530      know where the shared library will wind up at load-time.  */
4531   outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
4532                                  R_MIPS_REL32);
4533   /* For strict adherence to the ABI specification, we should
4534      generate a R_MIPS_64 relocation record by itself before the
4535      _REL32/_64 record as well, such that the addend is read in as
4536      a 64-bit value (REL32 is a 32-bit relocation, after all).
4537      However, since none of the existing ELF64 MIPS dynamic
4538      loaders seems to care, we don't waste space with these
4539      artificial relocations.  If this turns out to not be true,
4540      mips_elf_allocate_dynamic_relocation() should be tweaked so
4541      as to make room for a pair of dynamic relocations per
4542      invocation if ABI_64_P, and here we should generate an
4543      additional relocation record with R_MIPS_64 by itself for a
4544      NULL symbol before this relocation record.  */
4545   outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
4546                                  ABI_64_P (output_bfd)
4547                                  ? R_MIPS_64
4548                                  : R_MIPS_NONE);
4549   outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
4550
4551   /* Adjust the output offset of the relocation to reference the
4552      correct location in the output file.  */
4553   outrel[0].r_offset += (input_section->output_section->vma
4554                          + input_section->output_offset);
4555   outrel[1].r_offset += (input_section->output_section->vma
4556                          + input_section->output_offset);
4557   outrel[2].r_offset += (input_section->output_section->vma
4558                          + input_section->output_offset);
4559
4560   /* Put the relocation back out.  We have to use the special
4561      relocation outputter in the 64-bit case since the 64-bit
4562      relocation format is non-standard.  */
4563   if (ABI_64_P (output_bfd))
4564     {
4565       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
4566         (output_bfd, &outrel[0],
4567          (sreloc->contents
4568           + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
4569     }
4570   else
4571     bfd_elf32_swap_reloc_out
4572       (output_bfd, &outrel[0],
4573        (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
4574
4575   /* We've now added another relocation.  */
4576   ++sreloc->reloc_count;
4577
4578   /* Make sure the output section is writable.  The dynamic linker
4579      will be writing to it.  */
4580   elf_section_data (input_section->output_section)->this_hdr.sh_flags
4581     |= SHF_WRITE;
4582
4583   /* On IRIX5, make an entry of compact relocation info.  */
4584   if (IRIX_COMPAT (output_bfd) == ict_irix5)
4585     {
4586       asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4587       bfd_byte *cr;
4588
4589       if (scpt)
4590         {
4591           Elf32_crinfo cptrel;
4592
4593           mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
4594           cptrel.vaddr = (rel->r_offset
4595                           + input_section->output_section->vma
4596                           + input_section->output_offset);
4597           if (r_type == R_MIPS_REL32)
4598             mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
4599           else
4600             mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
4601           mips_elf_set_cr_dist2to (cptrel, 0);
4602           cptrel.konst = *addendp;
4603
4604           cr = (scpt->contents
4605                 + sizeof (Elf32_External_compact_rel));
4606           mips_elf_set_cr_relvaddr (cptrel, 0);
4607           bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4608                                      ((Elf32_External_crinfo *) cr
4609                                       + scpt->reloc_count));
4610           ++scpt->reloc_count;
4611         }
4612     }
4613
4614   return TRUE;
4615 }
4616 \f
4617 /* Return the MACH for a MIPS e_flags value.  */
4618
4619 unsigned long
4620 _bfd_elf_mips_mach (flagword flags)
4621 {
4622   switch (flags & EF_MIPS_MACH)
4623     {
4624     case E_MIPS_MACH_3900:
4625       return bfd_mach_mips3900;
4626
4627     case E_MIPS_MACH_4010:
4628       return bfd_mach_mips4010;
4629
4630     case E_MIPS_MACH_4100:
4631       return bfd_mach_mips4100;
4632
4633     case E_MIPS_MACH_4111:
4634       return bfd_mach_mips4111;
4635
4636     case E_MIPS_MACH_4120:
4637       return bfd_mach_mips4120;
4638
4639     case E_MIPS_MACH_4650:
4640       return bfd_mach_mips4650;
4641
4642     case E_MIPS_MACH_5400:
4643       return bfd_mach_mips5400;
4644
4645     case E_MIPS_MACH_5500:
4646       return bfd_mach_mips5500;
4647
4648     case E_MIPS_MACH_9000:
4649       return bfd_mach_mips9000;
4650
4651     case E_MIPS_MACH_SB1:
4652       return bfd_mach_mips_sb1;
4653
4654     default:
4655       switch (flags & EF_MIPS_ARCH)
4656         {
4657         default:
4658         case E_MIPS_ARCH_1:
4659           return bfd_mach_mips3000;
4660           break;
4661
4662         case E_MIPS_ARCH_2:
4663           return bfd_mach_mips6000;
4664           break;
4665
4666         case E_MIPS_ARCH_3:
4667           return bfd_mach_mips4000;
4668           break;
4669
4670         case E_MIPS_ARCH_4:
4671           return bfd_mach_mips8000;
4672           break;
4673
4674         case E_MIPS_ARCH_5:
4675           return bfd_mach_mips5;
4676           break;
4677
4678         case E_MIPS_ARCH_32:
4679           return bfd_mach_mipsisa32;
4680           break;
4681
4682         case E_MIPS_ARCH_64:
4683           return bfd_mach_mipsisa64;
4684           break;
4685
4686         case E_MIPS_ARCH_32R2:
4687           return bfd_mach_mipsisa32r2;
4688           break;
4689
4690         case E_MIPS_ARCH_64R2:
4691           return bfd_mach_mipsisa64r2;
4692           break;
4693         }
4694     }
4695
4696   return 0;
4697 }
4698
4699 /* Return printable name for ABI.  */
4700
4701 static INLINE char *
4702 elf_mips_abi_name (bfd *abfd)
4703 {
4704   flagword flags;
4705
4706   flags = elf_elfheader (abfd)->e_flags;
4707   switch (flags & EF_MIPS_ABI)
4708     {
4709     case 0:
4710       if (ABI_N32_P (abfd))
4711         return "N32";
4712       else if (ABI_64_P (abfd))
4713         return "64";
4714       else
4715         return "none";
4716     case E_MIPS_ABI_O32:
4717       return "O32";
4718     case E_MIPS_ABI_O64:
4719       return "O64";
4720     case E_MIPS_ABI_EABI32:
4721       return "EABI32";
4722     case E_MIPS_ABI_EABI64:
4723       return "EABI64";
4724     default:
4725       return "unknown abi";
4726     }
4727 }
4728 \f
4729 /* MIPS ELF uses two common sections.  One is the usual one, and the
4730    other is for small objects.  All the small objects are kept
4731    together, and then referenced via the gp pointer, which yields
4732    faster assembler code.  This is what we use for the small common
4733    section.  This approach is copied from ecoff.c.  */
4734 static asection mips_elf_scom_section;
4735 static asymbol mips_elf_scom_symbol;
4736 static asymbol *mips_elf_scom_symbol_ptr;
4737
4738 /* MIPS ELF also uses an acommon section, which represents an
4739    allocated common symbol which may be overridden by a
4740    definition in a shared library.  */
4741 static asection mips_elf_acom_section;
4742 static asymbol mips_elf_acom_symbol;
4743 static asymbol *mips_elf_acom_symbol_ptr;
4744
4745 /* Handle the special MIPS section numbers that a symbol may use.
4746    This is used for both the 32-bit and the 64-bit ABI.  */
4747
4748 void
4749 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
4750 {
4751   elf_symbol_type *elfsym;
4752
4753   elfsym = (elf_symbol_type *) asym;
4754   switch (elfsym->internal_elf_sym.st_shndx)
4755     {
4756     case SHN_MIPS_ACOMMON:
4757       /* This section is used in a dynamically linked executable file.
4758          It is an allocated common section.  The dynamic linker can
4759          either resolve these symbols to something in a shared
4760          library, or it can just leave them here.  For our purposes,
4761          we can consider these symbols to be in a new section.  */
4762       if (mips_elf_acom_section.name == NULL)
4763         {
4764           /* Initialize the acommon section.  */
4765           mips_elf_acom_section.name = ".acommon";
4766           mips_elf_acom_section.flags = SEC_ALLOC;
4767           mips_elf_acom_section.output_section = &mips_elf_acom_section;
4768           mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
4769           mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
4770           mips_elf_acom_symbol.name = ".acommon";
4771           mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
4772           mips_elf_acom_symbol.section = &mips_elf_acom_section;
4773           mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
4774         }
4775       asym->section = &mips_elf_acom_section;
4776       break;
4777
4778     case SHN_COMMON:
4779       /* Common symbols less than the GP size are automatically
4780          treated as SHN_MIPS_SCOMMON symbols on IRIX5.  */
4781       if (asym->value > elf_gp_size (abfd)
4782           || IRIX_COMPAT (abfd) == ict_irix6)
4783         break;
4784       /* Fall through.  */
4785     case SHN_MIPS_SCOMMON:
4786       if (mips_elf_scom_section.name == NULL)
4787         {
4788           /* Initialize the small common section.  */
4789           mips_elf_scom_section.name = ".scommon";
4790           mips_elf_scom_section.flags = SEC_IS_COMMON;
4791           mips_elf_scom_section.output_section = &mips_elf_scom_section;
4792           mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
4793           mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
4794           mips_elf_scom_symbol.name = ".scommon";
4795           mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
4796           mips_elf_scom_symbol.section = &mips_elf_scom_section;
4797           mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
4798         }
4799       asym->section = &mips_elf_scom_section;
4800       asym->value = elfsym->internal_elf_sym.st_size;
4801       break;
4802
4803     case SHN_MIPS_SUNDEFINED:
4804       asym->section = bfd_und_section_ptr;
4805       break;
4806
4807     case SHN_MIPS_TEXT:
4808       {
4809         asection *section = bfd_get_section_by_name (abfd, ".text");
4810
4811         BFD_ASSERT (SGI_COMPAT (abfd));
4812         if (section != NULL)
4813           {
4814             asym->section = section;
4815             /* MIPS_TEXT is a bit special, the address is not an offset
4816                to the base of the .text section.  So substract the section
4817                base address to make it an offset.  */
4818             asym->value -= section->vma;
4819           }
4820       }
4821       break;
4822
4823     case SHN_MIPS_DATA:
4824       {
4825         asection *section = bfd_get_section_by_name (abfd, ".data");
4826
4827         BFD_ASSERT (SGI_COMPAT (abfd));
4828         if (section != NULL)
4829           {
4830             asym->section = section;
4831             /* MIPS_DATA is a bit special, the address is not an offset
4832                to the base of the .data section.  So substract the section
4833                base address to make it an offset.  */
4834             asym->value -= section->vma;
4835           }
4836       }
4837       break;
4838     }
4839 }
4840 \f
4841 /* Implement elf_backend_eh_frame_address_size.  This differs from
4842    the default in the way it handles EABI64.
4843
4844    EABI64 was originally specified as an LP64 ABI, and that is what
4845    -mabi=eabi normally gives on a 64-bit target.  However, gcc has
4846    historically accepted the combination of -mabi=eabi and -mlong32,
4847    and this ILP32 variation has become semi-official over time.
4848    Both forms use elf32 and have pointer-sized FDE addresses.
4849
4850    If an EABI object was generated by GCC 4.0 or above, it will have
4851    an empty .gcc_compiled_longXX section, where XX is the size of longs
4852    in bits.  Unfortunately, ILP32 objects generated by earlier compilers
4853    have no special marking to distinguish them from LP64 objects.
4854
4855    We don't want users of the official LP64 ABI to be punished for the
4856    existence of the ILP32 variant, but at the same time, we don't want
4857    to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
4858    We therefore take the following approach:
4859
4860       - If ABFD contains a .gcc_compiled_longXX section, use it to
4861         determine the pointer size.
4862
4863       - Otherwise check the type of the first relocation.  Assume that
4864         the LP64 ABI is being used if the relocation is of type R_MIPS_64.
4865
4866       - Otherwise punt.
4867
4868    The second check is enough to detect LP64 objects generated by pre-4.0
4869    compilers because, in the kind of output generated by those compilers,
4870    the first relocation will be associated with either a CIE personality
4871    routine or an FDE start address.  Furthermore, the compilers never
4872    used a special (non-pointer) encoding for this ABI.
4873
4874    Checking the relocation type should also be safe because there is no
4875    reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
4876    did so.  */
4877
4878 unsigned int
4879 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
4880 {
4881   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
4882     return 8;
4883   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
4884     {
4885       bfd_boolean long32_p, long64_p;
4886
4887       long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
4888       long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
4889       if (long32_p && long64_p)
4890         return 0;
4891       if (long32_p)
4892         return 4;
4893       if (long64_p)
4894         return 8;
4895
4896       if (sec->reloc_count > 0
4897           && elf_section_data (sec)->relocs != NULL
4898           && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
4899               == R_MIPS_64))
4900         return 8;
4901
4902       return 0;
4903     }
4904   return 4;
4905 }
4906 \f
4907 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
4908    relocations against two unnamed section symbols to resolve to the
4909    same address.  For example, if we have code like:
4910
4911         lw      $4,%got_disp(.data)($gp)
4912         lw      $25,%got_disp(.text)($gp)
4913         jalr    $25
4914
4915    then the linker will resolve both relocations to .data and the program
4916    will jump there rather than to .text.
4917
4918    We can work around this problem by giving names to local section symbols.
4919    This is also what the MIPSpro tools do.  */
4920
4921 bfd_boolean
4922 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
4923 {
4924   return SGI_COMPAT (abfd);
4925 }
4926 \f
4927 /* Work over a section just before writing it out.  This routine is
4928    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
4929    sections that need the SHF_MIPS_GPREL flag by name; there has to be
4930    a better way.  */
4931
4932 bfd_boolean
4933 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
4934 {
4935   if (hdr->sh_type == SHT_MIPS_REGINFO
4936       && hdr->sh_size > 0)
4937     {
4938       bfd_byte buf[4];
4939
4940       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
4941       BFD_ASSERT (hdr->contents == NULL);
4942
4943       if (bfd_seek (abfd,
4944                     hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
4945                     SEEK_SET) != 0)
4946         return FALSE;
4947       H_PUT_32 (abfd, elf_gp (abfd), buf);
4948       if (bfd_bwrite (buf, 4, abfd) != 4)
4949         return FALSE;
4950     }
4951
4952   if (hdr->sh_type == SHT_MIPS_OPTIONS
4953       && hdr->bfd_section != NULL
4954       && mips_elf_section_data (hdr->bfd_section) != NULL
4955       && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
4956     {
4957       bfd_byte *contents, *l, *lend;
4958
4959       /* We stored the section contents in the tdata field in the
4960          set_section_contents routine.  We save the section contents
4961          so that we don't have to read them again.
4962          At this point we know that elf_gp is set, so we can look
4963          through the section contents to see if there is an
4964          ODK_REGINFO structure.  */
4965
4966       contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
4967       l = contents;
4968       lend = contents + hdr->sh_size;
4969       while (l + sizeof (Elf_External_Options) <= lend)
4970         {
4971           Elf_Internal_Options intopt;
4972
4973           bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
4974                                         &intopt);
4975           if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
4976             {
4977               bfd_byte buf[8];
4978
4979               if (bfd_seek (abfd,
4980                             (hdr->sh_offset
4981                              + (l - contents)
4982                              + sizeof (Elf_External_Options)
4983                              + (sizeof (Elf64_External_RegInfo) - 8)),
4984                              SEEK_SET) != 0)
4985                 return FALSE;
4986               H_PUT_64 (abfd, elf_gp (abfd), buf);
4987               if (bfd_bwrite (buf, 8, abfd) != 8)
4988                 return FALSE;
4989             }
4990           else if (intopt.kind == ODK_REGINFO)
4991             {
4992               bfd_byte buf[4];
4993
4994               if (bfd_seek (abfd,
4995                             (hdr->sh_offset
4996                              + (l - contents)
4997                              + sizeof (Elf_External_Options)
4998                              + (sizeof (Elf32_External_RegInfo) - 4)),
4999                             SEEK_SET) != 0)
5000                 return FALSE;
5001               H_PUT_32 (abfd, elf_gp (abfd), buf);
5002               if (bfd_bwrite (buf, 4, abfd) != 4)
5003                 return FALSE;
5004             }
5005           l += intopt.size;
5006         }
5007     }
5008
5009   if (hdr->bfd_section != NULL)
5010     {
5011       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
5012
5013       if (strcmp (name, ".sdata") == 0
5014           || strcmp (name, ".lit8") == 0
5015           || strcmp (name, ".lit4") == 0)
5016         {
5017           hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5018           hdr->sh_type = SHT_PROGBITS;
5019         }
5020       else if (strcmp (name, ".sbss") == 0)
5021         {
5022           hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5023           hdr->sh_type = SHT_NOBITS;
5024         }
5025       else if (strcmp (name, ".srdata") == 0)
5026         {
5027           hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
5028           hdr->sh_type = SHT_PROGBITS;
5029         }
5030       else if (strcmp (name, ".compact_rel") == 0)
5031         {
5032           hdr->sh_flags = 0;
5033           hdr->sh_type = SHT_PROGBITS;
5034         }
5035       else if (strcmp (name, ".rtproc") == 0)
5036         {
5037           if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
5038             {
5039               unsigned int adjust;
5040
5041               adjust = hdr->sh_size % hdr->sh_addralign;
5042               if (adjust != 0)
5043                 hdr->sh_size += hdr->sh_addralign - adjust;
5044             }
5045         }
5046     }
5047
5048   return TRUE;
5049 }
5050
5051 /* Handle a MIPS specific section when reading an object file.  This
5052    is called when elfcode.h finds a section with an unknown type.
5053    This routine supports both the 32-bit and 64-bit ELF ABI.
5054
5055    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
5056    how to.  */
5057
5058 bfd_boolean
5059 _bfd_mips_elf_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
5060                                  const char *name)
5061 {
5062   flagword flags = 0;
5063
5064   /* There ought to be a place to keep ELF backend specific flags, but
5065      at the moment there isn't one.  We just keep track of the
5066      sections by their name, instead.  Fortunately, the ABI gives
5067      suggested names for all the MIPS specific sections, so we will
5068      probably get away with this.  */
5069   switch (hdr->sh_type)
5070     {
5071     case SHT_MIPS_LIBLIST:
5072       if (strcmp (name, ".liblist") != 0)
5073         return FALSE;
5074       break;
5075     case SHT_MIPS_MSYM:
5076       if (strcmp (name, ".msym") != 0)
5077         return FALSE;
5078       break;
5079     case SHT_MIPS_CONFLICT:
5080       if (strcmp (name, ".conflict") != 0)
5081         return FALSE;
5082       break;
5083     case SHT_MIPS_GPTAB:
5084       if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
5085         return FALSE;
5086       break;
5087     case SHT_MIPS_UCODE:
5088       if (strcmp (name, ".ucode") != 0)
5089         return FALSE;
5090       break;
5091     case SHT_MIPS_DEBUG:
5092       if (strcmp (name, ".mdebug") != 0)
5093         return FALSE;
5094       flags = SEC_DEBUGGING;
5095       break;
5096     case SHT_MIPS_REGINFO:
5097       if (strcmp (name, ".reginfo") != 0
5098           || hdr->sh_size != sizeof (Elf32_External_RegInfo))
5099         return FALSE;
5100       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
5101       break;
5102     case SHT_MIPS_IFACE:
5103       if (strcmp (name, ".MIPS.interfaces") != 0)
5104         return FALSE;
5105       break;
5106     case SHT_MIPS_CONTENT:
5107       if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
5108         return FALSE;
5109       break;
5110     case SHT_MIPS_OPTIONS:
5111       if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
5112         return FALSE;
5113       break;
5114     case SHT_MIPS_DWARF:
5115       if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
5116         return FALSE;
5117       break;
5118     case SHT_MIPS_SYMBOL_LIB:
5119       if (strcmp (name, ".MIPS.symlib") != 0)
5120         return FALSE;
5121       break;
5122     case SHT_MIPS_EVENTS:
5123       if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
5124           && strncmp (name, ".MIPS.post_rel",
5125                       sizeof ".MIPS.post_rel" - 1) != 0)
5126         return FALSE;
5127       break;
5128     default:
5129       return FALSE;
5130     }
5131
5132   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
5133     return FALSE;
5134
5135   if (flags)
5136     {
5137       if (! bfd_set_section_flags (abfd, hdr->bfd_section,
5138                                    (bfd_get_section_flags (abfd,
5139                                                            hdr->bfd_section)
5140                                     | flags)))
5141         return FALSE;
5142     }
5143
5144   /* FIXME: We should record sh_info for a .gptab section.  */
5145
5146   /* For a .reginfo section, set the gp value in the tdata information
5147      from the contents of this section.  We need the gp value while
5148      processing relocs, so we just get it now.  The .reginfo section
5149      is not used in the 64-bit MIPS ELF ABI.  */
5150   if (hdr->sh_type == SHT_MIPS_REGINFO)
5151     {
5152       Elf32_External_RegInfo ext;
5153       Elf32_RegInfo s;
5154
5155       if (! bfd_get_section_contents (abfd, hdr->bfd_section,
5156                                       &ext, 0, sizeof ext))
5157         return FALSE;
5158       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
5159       elf_gp (abfd) = s.ri_gp_value;
5160     }
5161
5162   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
5163      set the gp value based on what we find.  We may see both
5164      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
5165      they should agree.  */
5166   if (hdr->sh_type == SHT_MIPS_OPTIONS)
5167     {
5168       bfd_byte *contents, *l, *lend;
5169
5170       contents = bfd_malloc (hdr->sh_size);
5171       if (contents == NULL)
5172         return FALSE;
5173       if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
5174                                       0, hdr->sh_size))
5175         {
5176           free (contents);
5177           return FALSE;
5178         }
5179       l = contents;
5180       lend = contents + hdr->sh_size;
5181       while (l + sizeof (Elf_External_Options) <= lend)
5182         {
5183           Elf_Internal_Options intopt;
5184
5185           bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5186                                         &intopt);
5187           if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5188             {
5189               Elf64_Internal_RegInfo intreg;
5190
5191               bfd_mips_elf64_swap_reginfo_in
5192                 (abfd,
5193                  ((Elf64_External_RegInfo *)
5194                   (l + sizeof (Elf_External_Options))),
5195                  &intreg);
5196               elf_gp (abfd) = intreg.ri_gp_value;
5197             }
5198           else if (intopt.kind == ODK_REGINFO)
5199             {
5200               Elf32_RegInfo intreg;
5201
5202               bfd_mips_elf32_swap_reginfo_in
5203                 (abfd,
5204                  ((Elf32_External_RegInfo *)
5205                   (l + sizeof (Elf_External_Options))),
5206                  &intreg);
5207               elf_gp (abfd) = intreg.ri_gp_value;
5208             }
5209           l += intopt.size;
5210         }
5211       free (contents);
5212     }
5213
5214   return TRUE;
5215 }
5216
5217 /* Set the correct type for a MIPS ELF section.  We do this by the
5218    section name, which is a hack, but ought to work.  This routine is
5219    used by both the 32-bit and the 64-bit ABI.  */
5220
5221 bfd_boolean
5222 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
5223 {
5224   register const char *name;
5225
5226   name = bfd_get_section_name (abfd, sec);
5227
5228   if (strcmp (name, ".liblist") == 0)
5229     {
5230       hdr->sh_type = SHT_MIPS_LIBLIST;
5231       hdr->sh_info = sec->size / sizeof (Elf32_Lib);
5232       /* The sh_link field is set in final_write_processing.  */
5233     }
5234   else if (strcmp (name, ".conflict") == 0)
5235     hdr->sh_type = SHT_MIPS_CONFLICT;
5236   else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
5237     {
5238       hdr->sh_type = SHT_MIPS_GPTAB;
5239       hdr->sh_entsize = sizeof (Elf32_External_gptab);
5240       /* The sh_info field is set in final_write_processing.  */
5241     }
5242   else if (strcmp (name, ".ucode") == 0)
5243     hdr->sh_type = SHT_MIPS_UCODE;
5244   else if (strcmp (name, ".mdebug") == 0)
5245     {
5246       hdr->sh_type = SHT_MIPS_DEBUG;
5247       /* In a shared object on IRIX 5.3, the .mdebug section has an
5248          entsize of 0.  FIXME: Does this matter?  */
5249       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
5250         hdr->sh_entsize = 0;
5251       else
5252         hdr->sh_entsize = 1;
5253     }
5254   else if (strcmp (name, ".reginfo") == 0)
5255     {
5256       hdr->sh_type = SHT_MIPS_REGINFO;
5257       /* In a shared object on IRIX 5.3, the .reginfo section has an
5258          entsize of 0x18.  FIXME: Does this matter?  */
5259       if (SGI_COMPAT (abfd))
5260         {
5261           if ((abfd->flags & DYNAMIC) != 0)
5262             hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5263           else
5264             hdr->sh_entsize = 1;
5265         }
5266       else
5267         hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5268     }
5269   else if (SGI_COMPAT (abfd)
5270            && (strcmp (name, ".hash") == 0
5271                || strcmp (name, ".dynamic") == 0
5272                || strcmp (name, ".dynstr") == 0))
5273     {
5274       if (SGI_COMPAT (abfd))
5275         hdr->sh_entsize = 0;
5276 #if 0
5277       /* This isn't how the IRIX6 linker behaves.  */
5278       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
5279 #endif
5280     }
5281   else if (strcmp (name, ".got") == 0
5282            || strcmp (name, ".srdata") == 0
5283            || strcmp (name, ".sdata") == 0
5284            || strcmp (name, ".sbss") == 0
5285            || strcmp (name, ".lit4") == 0
5286            || strcmp (name, ".lit8") == 0)
5287     hdr->sh_flags |= SHF_MIPS_GPREL;
5288   else if (strcmp (name, ".MIPS.interfaces") == 0)
5289     {
5290       hdr->sh_type = SHT_MIPS_IFACE;
5291       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5292     }
5293   else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
5294     {
5295       hdr->sh_type = SHT_MIPS_CONTENT;
5296       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5297       /* The sh_info field is set in final_write_processing.  */
5298     }
5299   else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
5300     {
5301       hdr->sh_type = SHT_MIPS_OPTIONS;
5302       hdr->sh_entsize = 1;
5303       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5304     }
5305   else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
5306     hdr->sh_type = SHT_MIPS_DWARF;
5307   else if (strcmp (name, ".MIPS.symlib") == 0)
5308     {
5309       hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
5310       /* The sh_link and sh_info fields are set in
5311          final_write_processing.  */
5312     }
5313   else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
5314            || strncmp (name, ".MIPS.post_rel",
5315                        sizeof ".MIPS.post_rel" - 1) == 0)
5316     {
5317       hdr->sh_type = SHT_MIPS_EVENTS;
5318       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5319       /* The sh_link field is set in final_write_processing.  */
5320     }
5321   else if (strcmp (name, ".msym") == 0)
5322     {
5323       hdr->sh_type = SHT_MIPS_MSYM;
5324       hdr->sh_flags |= SHF_ALLOC;
5325       hdr->sh_entsize = 8;
5326     }
5327
5328   /* The generic elf_fake_sections will set up REL_HDR using the default
5329    kind of relocations.  We used to set up a second header for the
5330    non-default kind of relocations here, but only NewABI would use
5331    these, and the IRIX ld doesn't like resulting empty RELA sections.
5332    Thus we create those header only on demand now.  */
5333
5334   return TRUE;
5335 }
5336
5337 /* Given a BFD section, try to locate the corresponding ELF section
5338    index.  This is used by both the 32-bit and the 64-bit ABI.
5339    Actually, it's not clear to me that the 64-bit ABI supports these,
5340    but for non-PIC objects we will certainly want support for at least
5341    the .scommon section.  */
5342
5343 bfd_boolean
5344 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5345                                         asection *sec, int *retval)
5346 {
5347   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
5348     {
5349       *retval = SHN_MIPS_SCOMMON;
5350       return TRUE;
5351     }
5352   if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
5353     {
5354       *retval = SHN_MIPS_ACOMMON;
5355       return TRUE;
5356     }
5357   return FALSE;
5358 }
5359 \f
5360 /* Hook called by the linker routine which adds symbols from an object
5361    file.  We must handle the special MIPS section numbers here.  */
5362
5363 bfd_boolean
5364 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
5365                                Elf_Internal_Sym *sym, const char **namep,
5366                                flagword *flagsp ATTRIBUTE_UNUSED,
5367                                asection **secp, bfd_vma *valp)
5368 {
5369   if (SGI_COMPAT (abfd)
5370       && (abfd->flags & DYNAMIC) != 0
5371       && strcmp (*namep, "_rld_new_interface") == 0)
5372     {
5373       /* Skip IRIX5 rld entry name.  */
5374       *namep = NULL;
5375       return TRUE;
5376     }
5377
5378   switch (sym->st_shndx)
5379     {
5380     case SHN_COMMON:
5381       /* Common symbols less than the GP size are automatically
5382          treated as SHN_MIPS_SCOMMON symbols.  */
5383       if (sym->st_size > elf_gp_size (abfd)
5384           || IRIX_COMPAT (abfd) == ict_irix6)
5385         break;
5386       /* Fall through.  */
5387     case SHN_MIPS_SCOMMON:
5388       *secp = bfd_make_section_old_way (abfd, ".scommon");
5389       (*secp)->flags |= SEC_IS_COMMON;
5390       *valp = sym->st_size;
5391       break;
5392
5393     case SHN_MIPS_TEXT:
5394       /* This section is used in a shared object.  */
5395       if (elf_tdata (abfd)->elf_text_section == NULL)
5396         {
5397           asymbol *elf_text_symbol;
5398           asection *elf_text_section;
5399           bfd_size_type amt = sizeof (asection);
5400
5401           elf_text_section = bfd_zalloc (abfd, amt);
5402           if (elf_text_section == NULL)
5403             return FALSE;
5404
5405           amt = sizeof (asymbol);
5406           elf_text_symbol = bfd_zalloc (abfd, amt);
5407           if (elf_text_symbol == NULL)
5408             return FALSE;
5409
5410           /* Initialize the section.  */
5411
5412           elf_tdata (abfd)->elf_text_section = elf_text_section;
5413           elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
5414
5415           elf_text_section->symbol = elf_text_symbol;
5416           elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
5417
5418           elf_text_section->name = ".text";
5419           elf_text_section->flags = SEC_NO_FLAGS;
5420           elf_text_section->output_section = NULL;
5421           elf_text_section->owner = abfd;
5422           elf_text_symbol->name = ".text";
5423           elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
5424           elf_text_symbol->section = elf_text_section;
5425         }
5426       /* This code used to do *secp = bfd_und_section_ptr if
5427          info->shared.  I don't know why, and that doesn't make sense,
5428          so I took it out.  */
5429       *secp = elf_tdata (abfd)->elf_text_section;
5430       break;
5431
5432     case SHN_MIPS_ACOMMON:
5433       /* Fall through. XXX Can we treat this as allocated data?  */
5434     case SHN_MIPS_DATA:
5435       /* This section is used in a shared object.  */
5436       if (elf_tdata (abfd)->elf_data_section == NULL)
5437         {
5438           asymbol *elf_data_symbol;
5439           asection *elf_data_section;
5440           bfd_size_type amt = sizeof (asection);
5441
5442           elf_data_section = bfd_zalloc (abfd, amt);
5443           if (elf_data_section == NULL)
5444             return FALSE;
5445
5446           amt = sizeof (asymbol);
5447           elf_data_symbol = bfd_zalloc (abfd, amt);
5448           if (elf_data_symbol == NULL)
5449             return FALSE;
5450
5451           /* Initialize the section.  */
5452
5453           elf_tdata (abfd)->elf_data_section = elf_data_section;
5454           elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
5455
5456           elf_data_section->symbol = elf_data_symbol;
5457           elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
5458
5459           elf_data_section->name = ".data";
5460           elf_data_section->flags = SEC_NO_FLAGS;
5461           elf_data_section->output_section = NULL;
5462           elf_data_section->owner = abfd;
5463           elf_data_symbol->name = ".data";
5464           elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
5465           elf_data_symbol->section = elf_data_section;
5466         }
5467       /* This code used to do *secp = bfd_und_section_ptr if
5468          info->shared.  I don't know why, and that doesn't make sense,
5469          so I took it out.  */
5470       *secp = elf_tdata (abfd)->elf_data_section;
5471       break;
5472
5473     case SHN_MIPS_SUNDEFINED:
5474       *secp = bfd_und_section_ptr;
5475       break;
5476     }
5477
5478   if (SGI_COMPAT (abfd)
5479       && ! info->shared
5480       && info->hash->creator == abfd->xvec
5481       && strcmp (*namep, "__rld_obj_head") == 0)
5482     {
5483       struct elf_link_hash_entry *h;
5484       struct bfd_link_hash_entry *bh;
5485
5486       /* Mark __rld_obj_head as dynamic.  */
5487       bh = NULL;
5488       if (! (_bfd_generic_link_add_one_symbol
5489              (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
5490               get_elf_backend_data (abfd)->collect, &bh)))
5491         return FALSE;
5492
5493       h = (struct elf_link_hash_entry *) bh;
5494       h->non_elf = 0;
5495       h->def_regular = 1;
5496       h->type = STT_OBJECT;
5497
5498       if (! bfd_elf_link_record_dynamic_symbol (info, h))
5499         return FALSE;
5500
5501       mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
5502     }
5503
5504   /* If this is a mips16 text symbol, add 1 to the value to make it
5505      odd.  This will cause something like .word SYM to come up with
5506      the right value when it is loaded into the PC.  */
5507   if (sym->st_other == STO_MIPS16)
5508     ++*valp;
5509
5510   return TRUE;
5511 }
5512
5513 /* This hook function is called before the linker writes out a global
5514    symbol.  We mark symbols as small common if appropriate.  This is
5515    also where we undo the increment of the value for a mips16 symbol.  */
5516
5517 bfd_boolean
5518 _bfd_mips_elf_link_output_symbol_hook
5519   (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5520    const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
5521    asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
5522 {
5523   /* If we see a common symbol, which implies a relocatable link, then
5524      if a symbol was small common in an input file, mark it as small
5525      common in the output file.  */
5526   if (sym->st_shndx == SHN_COMMON
5527       && strcmp (input_sec->name, ".scommon") == 0)
5528     sym->st_shndx = SHN_MIPS_SCOMMON;
5529
5530   if (sym->st_other == STO_MIPS16)
5531     sym->st_value &= ~1;
5532
5533   return TRUE;
5534 }
5535 \f
5536 /* Functions for the dynamic linker.  */
5537
5538 /* Create dynamic sections when linking against a dynamic object.  */
5539
5540 bfd_boolean
5541 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
5542 {
5543   struct elf_link_hash_entry *h;
5544   struct bfd_link_hash_entry *bh;
5545   flagword flags;
5546   register asection *s;
5547   const char * const *namep;
5548
5549   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5550            | SEC_LINKER_CREATED | SEC_READONLY);
5551
5552   /* Mips ABI requests the .dynamic section to be read only.  */
5553   s = bfd_get_section_by_name (abfd, ".dynamic");
5554   if (s != NULL)
5555     {
5556       if (! bfd_set_section_flags (abfd, s, flags))
5557         return FALSE;
5558     }
5559
5560   /* We need to create .got section.  */
5561   if (! mips_elf_create_got_section (abfd, info, FALSE))
5562     return FALSE;
5563
5564   if (! mips_elf_rel_dyn_section (elf_hash_table (info)->dynobj, TRUE))
5565     return FALSE;
5566
5567   /* Create .stub section.  */
5568   if (bfd_get_section_by_name (abfd,
5569                                MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
5570     {
5571       s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
5572       if (s == NULL
5573           || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
5574           || ! bfd_set_section_alignment (abfd, s,
5575                                           MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5576         return FALSE;
5577     }
5578
5579   if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
5580       && !info->shared
5581       && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5582     {
5583       s = bfd_make_section (abfd, ".rld_map");
5584       if (s == NULL
5585           || ! bfd_set_section_flags (abfd, s, flags &~ (flagword) SEC_READONLY)
5586           || ! bfd_set_section_alignment (abfd, s,
5587                                           MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5588         return FALSE;
5589     }
5590
5591   /* On IRIX5, we adjust add some additional symbols and change the
5592      alignments of several sections.  There is no ABI documentation
5593      indicating that this is necessary on IRIX6, nor any evidence that
5594      the linker takes such action.  */
5595   if (IRIX_COMPAT (abfd) == ict_irix5)
5596     {
5597       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5598         {
5599           bh = NULL;
5600           if (! (_bfd_generic_link_add_one_symbol
5601                  (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
5602                   NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5603             return FALSE;
5604
5605           h = (struct elf_link_hash_entry *) bh;
5606           h->non_elf = 0;
5607           h->def_regular = 1;
5608           h->type = STT_SECTION;
5609
5610           if (! bfd_elf_link_record_dynamic_symbol (info, h))
5611             return FALSE;
5612         }
5613
5614       /* We need to create a .compact_rel section.  */
5615       if (SGI_COMPAT (abfd))
5616         {
5617           if (!mips_elf_create_compact_rel_section (abfd, info))
5618             return FALSE;
5619         }
5620
5621       /* Change alignments of some sections.  */
5622       s = bfd_get_section_by_name (abfd, ".hash");
5623       if (s != NULL)
5624         bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5625       s = bfd_get_section_by_name (abfd, ".dynsym");
5626       if (s != NULL)
5627         bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5628       s = bfd_get_section_by_name (abfd, ".dynstr");
5629       if (s != NULL)
5630         bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5631       s = bfd_get_section_by_name (abfd, ".reginfo");
5632       if (s != NULL)
5633         bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5634       s = bfd_get_section_by_name (abfd, ".dynamic");
5635       if (s != NULL)
5636         bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5637     }
5638
5639   if (!info->shared)
5640     {
5641       const char *name;
5642
5643       name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
5644       bh = NULL;
5645       if (!(_bfd_generic_link_add_one_symbol
5646             (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
5647              NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5648         return FALSE;
5649
5650       h = (struct elf_link_hash_entry *) bh;
5651       h->non_elf = 0;
5652       h->def_regular = 1;
5653       h->type = STT_SECTION;
5654
5655       if (! bfd_elf_link_record_dynamic_symbol (info, h))
5656         return FALSE;
5657
5658       if (! mips_elf_hash_table (info)->use_rld_obj_head)
5659         {
5660           /* __rld_map is a four byte word located in the .data section
5661              and is filled in by the rtld to contain a pointer to
5662              the _r_debug structure. Its symbol value will be set in
5663              _bfd_mips_elf_finish_dynamic_symbol.  */
5664           s = bfd_get_section_by_name (abfd, ".rld_map");
5665           BFD_ASSERT (s != NULL);
5666
5667           name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
5668           bh = NULL;
5669           if (!(_bfd_generic_link_add_one_symbol
5670                 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
5671                  get_elf_backend_data (abfd)->collect, &bh)))
5672             return FALSE;
5673
5674           h = (struct elf_link_hash_entry *) bh;
5675           h->non_elf = 0;
5676           h->def_regular = 1;
5677           h->type = STT_OBJECT;
5678
5679           if (! bfd_elf_link_record_dynamic_symbol (info, h))
5680             return FALSE;
5681         }
5682     }
5683
5684   return TRUE;
5685 }
5686 \f
5687 /* Look through the relocs for a section during the first phase, and
5688    allocate space in the global offset table.  */
5689
5690 bfd_boolean
5691 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5692                             asection *sec, const Elf_Internal_Rela *relocs)
5693 {
5694   const char *name;
5695   bfd *dynobj;
5696   Elf_Internal_Shdr *symtab_hdr;
5697   struct elf_link_hash_entry **sym_hashes;
5698   struct mips_got_info *g;
5699   size_t extsymoff;
5700   const Elf_Internal_Rela *rel;
5701   const Elf_Internal_Rela *rel_end;
5702   asection *sgot;
5703   asection *sreloc;
5704   const struct elf_backend_data *bed;
5705
5706   if (info->relocatable)
5707     return TRUE;
5708
5709   dynobj = elf_hash_table (info)->dynobj;
5710   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5711   sym_hashes = elf_sym_hashes (abfd);
5712   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
5713
5714   /* Check for the mips16 stub sections.  */
5715
5716   name = bfd_get_section_name (abfd, sec);
5717   if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
5718     {
5719       unsigned long r_symndx;
5720
5721       /* Look at the relocation information to figure out which symbol
5722          this is for.  */
5723
5724       r_symndx = ELF_R_SYM (abfd, relocs->r_info);
5725
5726       if (r_symndx < extsymoff
5727           || sym_hashes[r_symndx - extsymoff] == NULL)
5728         {
5729           asection *o;
5730
5731           /* This stub is for a local symbol.  This stub will only be
5732              needed if there is some relocation in this BFD, other
5733              than a 16 bit function call, which refers to this symbol.  */
5734           for (o = abfd->sections; o != NULL; o = o->next)
5735             {
5736               Elf_Internal_Rela *sec_relocs;
5737               const Elf_Internal_Rela *r, *rend;
5738
5739               /* We can ignore stub sections when looking for relocs.  */
5740               if ((o->flags & SEC_RELOC) == 0
5741                   || o->reloc_count == 0
5742                   || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
5743                               sizeof FN_STUB - 1) == 0
5744                   || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
5745                               sizeof CALL_STUB - 1) == 0
5746                   || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
5747                               sizeof CALL_FP_STUB - 1) == 0)
5748                 continue;
5749
5750               sec_relocs
5751                 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
5752                                              info->keep_memory);
5753               if (sec_relocs == NULL)
5754                 return FALSE;
5755
5756               rend = sec_relocs + o->reloc_count;
5757               for (r = sec_relocs; r < rend; r++)
5758                 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
5759                     && ELF_R_TYPE (abfd, r->r_info) != R_MIPS16_26)
5760                   break;
5761
5762               if (elf_section_data (o)->relocs != sec_relocs)
5763                 free (sec_relocs);
5764
5765               if (r < rend)
5766                 break;
5767             }
5768
5769           if (o == NULL)
5770             {
5771               /* There is no non-call reloc for this stub, so we do
5772                  not need it.  Since this function is called before
5773                  the linker maps input sections to output sections, we
5774                  can easily discard it by setting the SEC_EXCLUDE
5775                  flag.  */
5776               sec->flags |= SEC_EXCLUDE;
5777               return TRUE;
5778             }
5779
5780           /* Record this stub in an array of local symbol stubs for
5781              this BFD.  */
5782           if (elf_tdata (abfd)->local_stubs == NULL)
5783             {
5784               unsigned long symcount;
5785               asection **n;
5786               bfd_size_type amt;
5787
5788               if (elf_bad_symtab (abfd))
5789                 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
5790               else
5791                 symcount = symtab_hdr->sh_info;
5792               amt = symcount * sizeof (asection *);
5793               n = bfd_zalloc (abfd, amt);
5794               if (n == NULL)
5795                 return FALSE;
5796               elf_tdata (abfd)->local_stubs = n;
5797             }
5798
5799           elf_tdata (abfd)->local_stubs[r_symndx] = sec;
5800
5801           /* We don't need to set mips16_stubs_seen in this case.
5802              That flag is used to see whether we need to look through
5803              the global symbol table for stubs.  We don't need to set
5804              it here, because we just have a local stub.  */
5805         }
5806       else
5807         {
5808           struct mips_elf_link_hash_entry *h;
5809
5810           h = ((struct mips_elf_link_hash_entry *)
5811                sym_hashes[r_symndx - extsymoff]);
5812
5813           /* H is the symbol this stub is for.  */
5814
5815           h->fn_stub = sec;
5816           mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
5817         }
5818     }
5819   else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
5820            || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5821     {
5822       unsigned long r_symndx;
5823       struct mips_elf_link_hash_entry *h;
5824       asection **loc;
5825
5826       /* Look at the relocation information to figure out which symbol
5827          this is for.  */
5828
5829       r_symndx = ELF_R_SYM (abfd, relocs->r_info);
5830
5831       if (r_symndx < extsymoff
5832           || sym_hashes[r_symndx - extsymoff] == NULL)
5833         {
5834           /* This stub was actually built for a static symbol defined
5835              in the same file.  We assume that all static symbols in
5836              mips16 code are themselves mips16, so we can simply
5837              discard this stub.  Since this function is called before
5838              the linker maps input sections to output sections, we can
5839              easily discard it by setting the SEC_EXCLUDE flag.  */
5840           sec->flags |= SEC_EXCLUDE;
5841           return TRUE;
5842         }
5843
5844       h = ((struct mips_elf_link_hash_entry *)
5845            sym_hashes[r_symndx - extsymoff]);
5846
5847       /* H is the symbol this stub is for.  */
5848
5849       if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5850         loc = &h->call_fp_stub;
5851       else
5852         loc = &h->call_stub;
5853
5854       /* If we already have an appropriate stub for this function, we
5855          don't need another one, so we can discard this one.  Since
5856          this function is called before the linker maps input sections
5857          to output sections, we can easily discard it by setting the
5858          SEC_EXCLUDE flag.  We can also discard this section if we
5859          happen to already know that this is a mips16 function; it is
5860          not necessary to check this here, as it is checked later, but
5861          it is slightly faster to check now.  */
5862       if (*loc != NULL || h->root.other == STO_MIPS16)
5863         {
5864           sec->flags |= SEC_EXCLUDE;
5865           return TRUE;
5866         }
5867
5868       *loc = sec;
5869       mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
5870     }
5871
5872   if (dynobj == NULL)
5873     {
5874       sgot = NULL;
5875       g = NULL;
5876     }
5877   else
5878     {
5879       sgot = mips_elf_got_section (dynobj, FALSE);
5880       if (sgot == NULL)
5881         g = NULL;
5882       else
5883         {
5884           BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
5885           g = mips_elf_section_data (sgot)->u.got_info;
5886           BFD_ASSERT (g != NULL);
5887         }
5888     }
5889
5890   sreloc = NULL;
5891   bed = get_elf_backend_data (abfd);
5892   rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5893   for (rel = relocs; rel < rel_end; ++rel)
5894     {
5895       unsigned long r_symndx;
5896       unsigned int r_type;
5897       struct elf_link_hash_entry *h;
5898
5899       r_symndx = ELF_R_SYM (abfd, rel->r_info);
5900       r_type = ELF_R_TYPE (abfd, rel->r_info);
5901
5902       if (r_symndx < extsymoff)
5903         h = NULL;
5904       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
5905         {
5906           (*_bfd_error_handler)
5907             (_("%B: Malformed reloc detected for section %s"),
5908              abfd, name);
5909           bfd_set_error (bfd_error_bad_value);
5910           return FALSE;
5911         }
5912       else
5913         {
5914           h = sym_hashes[r_symndx - extsymoff];
5915
5916           /* This may be an indirect symbol created because of a version.  */
5917           if (h != NULL)
5918             {
5919               while (h->root.type == bfd_link_hash_indirect)
5920                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5921             }
5922         }
5923
5924       /* Some relocs require a global offset table.  */
5925       if (dynobj == NULL || sgot == NULL)
5926         {
5927           switch (r_type)
5928             {
5929             case R_MIPS_GOT16:
5930             case R_MIPS_CALL16:
5931             case R_MIPS_CALL_HI16:
5932             case R_MIPS_CALL_LO16:
5933             case R_MIPS_GOT_HI16:
5934             case R_MIPS_GOT_LO16:
5935             case R_MIPS_GOT_PAGE:
5936             case R_MIPS_GOT_OFST:
5937             case R_MIPS_GOT_DISP:
5938             case R_MIPS_TLS_GD:
5939             case R_MIPS_TLS_LDM:
5940               if (dynobj == NULL)
5941                 elf_hash_table (info)->dynobj = dynobj = abfd;
5942               if (! mips_elf_create_got_section (dynobj, info, FALSE))
5943                 return FALSE;
5944               g = mips_elf_got_info (dynobj, &sgot);
5945               break;
5946
5947             case R_MIPS_32:
5948             case R_MIPS_REL32:
5949             case R_MIPS_64:
5950               if (dynobj == NULL
5951                   && (info->shared || h != NULL)
5952                   && (sec->flags & SEC_ALLOC) != 0)
5953                 elf_hash_table (info)->dynobj = dynobj = abfd;
5954               break;
5955
5956             default:
5957               break;
5958             }
5959         }
5960
5961       if (!h && (r_type == R_MIPS_CALL_LO16
5962                  || r_type == R_MIPS_GOT_LO16
5963                  || r_type == R_MIPS_GOT_DISP))
5964         {
5965           /* We may need a local GOT entry for this relocation.  We
5966              don't count R_MIPS_GOT_PAGE because we can estimate the
5967              maximum number of pages needed by looking at the size of
5968              the segment.  Similar comments apply to R_MIPS_GOT16 and
5969              R_MIPS_CALL16.  We don't count R_MIPS_GOT_HI16, or
5970              R_MIPS_CALL_HI16 because these are always followed by an
5971              R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
5972           if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
5973                                                   rel->r_addend, g, 0))
5974             return FALSE;
5975         }
5976
5977       switch (r_type)
5978         {
5979         case R_MIPS_CALL16:
5980           if (h == NULL)
5981             {
5982               (*_bfd_error_handler)
5983                 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
5984                  abfd, (unsigned long) rel->r_offset);
5985               bfd_set_error (bfd_error_bad_value);
5986               return FALSE;
5987             }
5988           /* Fall through.  */
5989
5990         case R_MIPS_CALL_HI16:
5991         case R_MIPS_CALL_LO16:
5992           if (h != NULL)
5993             {
5994               /* This symbol requires a global offset table entry.  */
5995               if (! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
5996                 return FALSE;
5997
5998               /* We need a stub, not a plt entry for the undefined
5999                  function.  But we record it as if it needs plt.  See
6000                  _bfd_elf_adjust_dynamic_symbol.  */
6001               h->needs_plt = 1;
6002               h->type = STT_FUNC;
6003             }
6004           break;
6005
6006         case R_MIPS_GOT_PAGE:
6007           /* If this is a global, overridable symbol, GOT_PAGE will
6008              decay to GOT_DISP, so we'll need a GOT entry for it.  */
6009           if (h == NULL)
6010             break;
6011           else
6012             {
6013               struct mips_elf_link_hash_entry *hmips =
6014                 (struct mips_elf_link_hash_entry *) h;
6015
6016               while (hmips->root.root.type == bfd_link_hash_indirect
6017                      || hmips->root.root.type == bfd_link_hash_warning)
6018                 hmips = (struct mips_elf_link_hash_entry *)
6019                   hmips->root.root.u.i.link;
6020
6021               if (hmips->root.def_regular
6022                   && ! (info->shared && ! info->symbolic
6023                         && ! hmips->root.forced_local))
6024                 break;
6025             }
6026           /* Fall through.  */
6027
6028         case R_MIPS_GOT16:
6029         case R_MIPS_GOT_HI16:
6030         case R_MIPS_GOT_LO16:
6031         case R_MIPS_GOT_DISP:
6032           if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
6033             return FALSE;
6034           break;
6035
6036         case R_MIPS_TLS_GOTTPREL:
6037           if (info->shared)
6038             info->flags |= DF_STATIC_TLS;
6039           /* Fall through */
6040
6041         case R_MIPS_TLS_LDM:
6042           if (r_type == R_MIPS_TLS_LDM)
6043             {
6044               r_symndx = 0;
6045               h = NULL;
6046             }
6047           /* Fall through */
6048
6049         case R_MIPS_TLS_GD:
6050           /* This symbol requires a global offset table entry, or two
6051              for TLS GD relocations.  */
6052           {
6053             unsigned char flag = (r_type == R_MIPS_TLS_GD
6054                                   ? GOT_TLS_GD
6055                                   : r_type == R_MIPS_TLS_LDM
6056                                   ? GOT_TLS_LDM
6057                                   : GOT_TLS_IE);
6058             if (h != NULL)
6059               {
6060                 struct mips_elf_link_hash_entry *hmips =
6061                   (struct mips_elf_link_hash_entry *) h;
6062                 hmips->tls_type |= flag;
6063
6064                 if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, flag))
6065                   return FALSE;
6066               }
6067             else
6068               {
6069                 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
6070
6071                 if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
6072                                                         rel->r_addend, g, flag))
6073                   return FALSE;
6074               }
6075           }
6076           break;
6077
6078         case R_MIPS_32:
6079         case R_MIPS_REL32:
6080         case R_MIPS_64:
6081           if ((info->shared || h != NULL)
6082               && (sec->flags & SEC_ALLOC) != 0)
6083             {
6084               if (sreloc == NULL)
6085                 {
6086                   sreloc = mips_elf_rel_dyn_section (dynobj, TRUE);
6087                   if (sreloc == NULL)
6088                     return FALSE;
6089                 }
6090 #define MIPS_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
6091               if (info->shared)
6092                 {
6093                   /* When creating a shared object, we must copy these
6094                      reloc types into the output file as R_MIPS_REL32
6095                      relocs.  We make room for this reloc in the
6096                      .rel.dyn reloc section.  */
6097                   mips_elf_allocate_dynamic_relocations (dynobj, 1);
6098                   if ((sec->flags & MIPS_READONLY_SECTION)
6099                       == MIPS_READONLY_SECTION)
6100                     /* We tell the dynamic linker that there are
6101                        relocations against the text segment.  */
6102                     info->flags |= DF_TEXTREL;
6103                 }
6104               else
6105                 {
6106                   struct mips_elf_link_hash_entry *hmips;
6107
6108                   /* We only need to copy this reloc if the symbol is
6109                      defined in a dynamic object.  */
6110                   hmips = (struct mips_elf_link_hash_entry *) h;
6111                   ++hmips->possibly_dynamic_relocs;
6112                   if ((sec->flags & MIPS_READONLY_SECTION)
6113                       == MIPS_READONLY_SECTION)
6114                     /* We need it to tell the dynamic linker if there
6115                        are relocations against the text segment.  */
6116                     hmips->readonly_reloc = TRUE;
6117                 }
6118
6119               /* Even though we don't directly need a GOT entry for
6120                  this symbol, a symbol must have a dynamic symbol
6121                  table index greater that DT_MIPS_GOTSYM if there are
6122                  dynamic relocations against it.  */
6123               if (h != NULL)
6124                 {
6125                   if (dynobj == NULL)
6126                     elf_hash_table (info)->dynobj = dynobj = abfd;
6127                   if (! mips_elf_create_got_section (dynobj, info, TRUE))
6128                     return FALSE;
6129                   g = mips_elf_got_info (dynobj, &sgot);
6130                   if (! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
6131                     return FALSE;
6132                 }
6133             }
6134
6135           if (SGI_COMPAT (abfd))
6136             mips_elf_hash_table (info)->compact_rel_size +=
6137               sizeof (Elf32_External_crinfo);
6138           break;
6139
6140         case R_MIPS_26:
6141         case R_MIPS_GPREL16:
6142         case R_MIPS_LITERAL:
6143         case R_MIPS_GPREL32:
6144           if (SGI_COMPAT (abfd))
6145             mips_elf_hash_table (info)->compact_rel_size +=
6146               sizeof (Elf32_External_crinfo);
6147           break;
6148
6149           /* This relocation describes the C++ object vtable hierarchy.
6150              Reconstruct it for later use during GC.  */
6151         case R_MIPS_GNU_VTINHERIT:
6152           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6153             return FALSE;
6154           break;
6155
6156           /* This relocation describes which C++ vtable entries are actually
6157              used.  Record for later use during GC.  */
6158         case R_MIPS_GNU_VTENTRY:
6159           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6160             return FALSE;
6161           break;
6162
6163         default:
6164           break;
6165         }
6166
6167       /* We must not create a stub for a symbol that has relocations
6168          related to taking the function's address.  */
6169       switch (r_type)
6170         {
6171         default:
6172           if (h != NULL)
6173             {
6174               struct mips_elf_link_hash_entry *mh;
6175
6176               mh = (struct mips_elf_link_hash_entry *) h;
6177               mh->no_fn_stub = TRUE;
6178             }
6179           break;
6180         case R_MIPS_CALL16:
6181         case R_MIPS_CALL_HI16:
6182         case R_MIPS_CALL_LO16:
6183         case R_MIPS_JALR:
6184           break;
6185         }
6186
6187       /* If this reloc is not a 16 bit call, and it has a global
6188          symbol, then we will need the fn_stub if there is one.
6189          References from a stub section do not count.  */
6190       if (h != NULL
6191           && r_type != R_MIPS16_26
6192           && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6193                       sizeof FN_STUB - 1) != 0
6194           && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6195                       sizeof CALL_STUB - 1) != 0
6196           && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6197                       sizeof CALL_FP_STUB - 1) != 0)
6198         {
6199           struct mips_elf_link_hash_entry *mh;
6200
6201           mh = (struct mips_elf_link_hash_entry *) h;
6202           mh->need_fn_stub = TRUE;
6203         }
6204     }
6205
6206   return TRUE;
6207 }
6208 \f
6209 bfd_boolean
6210 _bfd_mips_relax_section (bfd *abfd, asection *sec,
6211                          struct bfd_link_info *link_info,
6212                          bfd_boolean *again)
6213 {
6214   Elf_Internal_Rela *internal_relocs;
6215   Elf_Internal_Rela *irel, *irelend;
6216   Elf_Internal_Shdr *symtab_hdr;
6217   bfd_byte *contents = NULL;
6218   size_t extsymoff;
6219   bfd_boolean changed_contents = FALSE;
6220   bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
6221   Elf_Internal_Sym *isymbuf = NULL;
6222
6223   /* We are not currently changing any sizes, so only one pass.  */
6224   *again = FALSE;
6225
6226   if (link_info->relocatable)
6227     return TRUE;
6228
6229   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
6230                                                link_info->keep_memory);
6231   if (internal_relocs == NULL)
6232     return TRUE;
6233
6234   irelend = internal_relocs + sec->reloc_count
6235     * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
6236   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6237   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6238
6239   for (irel = internal_relocs; irel < irelend; irel++)
6240     {
6241       bfd_vma symval;
6242       bfd_signed_vma sym_offset;
6243       unsigned int r_type;
6244       unsigned long r_symndx;
6245       asection *sym_sec;
6246       unsigned long instruction;
6247
6248       /* Turn jalr into bgezal, and jr into beq, if they're marked
6249          with a JALR relocation, that indicate where they jump to.
6250          This saves some pipeline bubbles.  */
6251       r_type = ELF_R_TYPE (abfd, irel->r_info);
6252       if (r_type != R_MIPS_JALR)
6253         continue;
6254
6255       r_symndx = ELF_R_SYM (abfd, irel->r_info);
6256       /* Compute the address of the jump target.  */
6257       if (r_symndx >= extsymoff)
6258         {
6259           struct mips_elf_link_hash_entry *h
6260             = ((struct mips_elf_link_hash_entry *)
6261                elf_sym_hashes (abfd) [r_symndx - extsymoff]);
6262
6263           while (h->root.root.type == bfd_link_hash_indirect
6264                  || h->root.root.type == bfd_link_hash_warning)
6265             h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
6266
6267           /* If a symbol is undefined, or if it may be overridden,
6268              skip it.  */
6269           if (! ((h->root.root.type == bfd_link_hash_defined
6270                   || h->root.root.type == bfd_link_hash_defweak)
6271                  && h->root.root.u.def.section)
6272               || (link_info->shared && ! link_info->symbolic
6273                   && !h->root.forced_local))
6274             continue;
6275
6276           sym_sec = h->root.root.u.def.section;
6277           if (sym_sec->output_section)
6278             symval = (h->root.root.u.def.value
6279                       + sym_sec->output_section->vma
6280                       + sym_sec->output_offset);
6281           else
6282             symval = h->root.root.u.def.value;
6283         }
6284       else
6285         {
6286           Elf_Internal_Sym *isym;
6287
6288           /* Read this BFD's symbols if we haven't done so already.  */
6289           if (isymbuf == NULL && symtab_hdr->sh_info != 0)
6290             {
6291               isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6292               if (isymbuf == NULL)
6293                 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6294                                                 symtab_hdr->sh_info, 0,
6295                                                 NULL, NULL, NULL);
6296               if (isymbuf == NULL)
6297                 goto relax_return;
6298             }
6299
6300           isym = isymbuf + r_symndx;
6301           if (isym->st_shndx == SHN_UNDEF)
6302             continue;
6303           else if (isym->st_shndx == SHN_ABS)
6304             sym_sec = bfd_abs_section_ptr;
6305           else if (isym->st_shndx == SHN_COMMON)
6306             sym_sec = bfd_com_section_ptr;
6307           else
6308             sym_sec
6309               = bfd_section_from_elf_index (abfd, isym->st_shndx);
6310           symval = isym->st_value
6311             + sym_sec->output_section->vma
6312             + sym_sec->output_offset;
6313         }
6314
6315       /* Compute branch offset, from delay slot of the jump to the
6316          branch target.  */
6317       sym_offset = (symval + irel->r_addend)
6318         - (sec_start + irel->r_offset + 4);
6319
6320       /* Branch offset must be properly aligned.  */
6321       if ((sym_offset & 3) != 0)
6322         continue;
6323
6324       sym_offset >>= 2;
6325
6326       /* Check that it's in range.  */
6327       if (sym_offset < -0x8000 || sym_offset >= 0x8000)
6328         continue;
6329
6330       /* Get the section contents if we haven't done so already.  */
6331       if (contents == NULL)
6332         {
6333           /* Get cached copy if it exists.  */
6334           if (elf_section_data (sec)->this_hdr.contents != NULL)
6335             contents = elf_section_data (sec)->this_hdr.contents;
6336           else
6337             {
6338               if (!bfd_malloc_and_get_section (abfd, sec, &contents))
6339                 goto relax_return;
6340             }
6341         }
6342
6343       instruction = bfd_get_32 (abfd, contents + irel->r_offset);
6344
6345       /* If it was jalr <reg>, turn it into bgezal $zero, <target>.  */
6346       if ((instruction & 0xfc1fffff) == 0x0000f809)
6347         instruction = 0x04110000;
6348       /* If it was jr <reg>, turn it into b <target>.  */
6349       else if ((instruction & 0xfc1fffff) == 0x00000008)
6350         instruction = 0x10000000;
6351       else
6352         continue;
6353
6354       instruction |= (sym_offset & 0xffff);
6355       bfd_put_32 (abfd, instruction, contents + irel->r_offset);
6356       changed_contents = TRUE;
6357     }
6358
6359   if (contents != NULL
6360       && elf_section_data (sec)->this_hdr.contents != contents)
6361     {
6362       if (!changed_contents && !link_info->keep_memory)
6363         free (contents);
6364       else
6365         {
6366           /* Cache the section contents for elf_link_input_bfd.  */
6367           elf_section_data (sec)->this_hdr.contents = contents;
6368         }
6369     }
6370   return TRUE;
6371
6372  relax_return:
6373   if (contents != NULL
6374       && elf_section_data (sec)->this_hdr.contents != contents)
6375     free (contents);
6376   return FALSE;
6377 }
6378 \f
6379 /* Adjust a symbol defined by a dynamic object and referenced by a
6380    regular object.  The current definition is in some section of the
6381    dynamic object, but we're not including those sections.  We have to
6382    change the definition to something the rest of the link can
6383    understand.  */
6384
6385 bfd_boolean
6386 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6387                                      struct elf_link_hash_entry *h)
6388 {
6389   bfd *dynobj;
6390   struct mips_elf_link_hash_entry *hmips;
6391   asection *s;
6392
6393   dynobj = elf_hash_table (info)->dynobj;
6394
6395   /* Make sure we know what is going on here.  */
6396   BFD_ASSERT (dynobj != NULL
6397               && (h->needs_plt
6398                   || h->u.weakdef != NULL
6399                   || (h->def_dynamic
6400                       && h->ref_regular
6401                       && !h->def_regular)));
6402
6403   /* If this symbol is defined in a dynamic object, we need to copy
6404      any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6405      file.  */
6406   hmips = (struct mips_elf_link_hash_entry *) h;
6407   if (! info->relocatable
6408       && hmips->possibly_dynamic_relocs != 0
6409       && (h->root.type == bfd_link_hash_defweak
6410           || !h->def_regular))
6411     {
6412       mips_elf_allocate_dynamic_relocations (dynobj,
6413                                              hmips->possibly_dynamic_relocs);
6414       if (hmips->readonly_reloc)
6415         /* We tell the dynamic linker that there are relocations
6416            against the text segment.  */
6417         info->flags |= DF_TEXTREL;
6418     }
6419
6420   /* For a function, create a stub, if allowed.  */
6421   if (! hmips->no_fn_stub
6422       && h->needs_plt)
6423     {
6424       if (! elf_hash_table (info)->dynamic_sections_created)
6425         return TRUE;
6426
6427       /* If this symbol is not defined in a regular file, then set
6428          the symbol to the stub location.  This is required to make
6429          function pointers compare as equal between the normal
6430          executable and the shared library.  */
6431       if (!h->def_regular)
6432         {
6433           /* We need .stub section.  */
6434           s = bfd_get_section_by_name (dynobj,
6435                                        MIPS_ELF_STUB_SECTION_NAME (dynobj));
6436           BFD_ASSERT (s != NULL);
6437
6438           h->root.u.def.section = s;
6439           h->root.u.def.value = s->size;
6440
6441           /* XXX Write this stub address somewhere.  */
6442           h->plt.offset = s->size;
6443
6444           /* Make room for this stub code.  */
6445           s->size += MIPS_FUNCTION_STUB_SIZE;
6446
6447           /* The last half word of the stub will be filled with the index
6448              of this symbol in .dynsym section.  */
6449           return TRUE;
6450         }
6451     }
6452   else if ((h->type == STT_FUNC)
6453            && !h->needs_plt)
6454     {
6455       /* This will set the entry for this symbol in the GOT to 0, and
6456          the dynamic linker will take care of this.  */
6457       h->root.u.def.value = 0;
6458       return TRUE;
6459     }
6460
6461   /* If this is a weak symbol, and there is a real definition, the
6462      processor independent code will have arranged for us to see the
6463      real definition first, and we can just use the same value.  */
6464   if (h->u.weakdef != NULL)
6465     {
6466       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6467                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
6468       h->root.u.def.section = h->u.weakdef->root.u.def.section;
6469       h->root.u.def.value = h->u.weakdef->root.u.def.value;
6470       return TRUE;
6471     }
6472
6473   /* This is a reference to a symbol defined by a dynamic object which
6474      is not a function.  */
6475
6476   return TRUE;
6477 }
6478 \f
6479 /* This function is called after all the input files have been read,
6480    and the input sections have been assigned to output sections.  We
6481    check for any mips16 stub sections that we can discard.  */
6482
6483 bfd_boolean
6484 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
6485                                     struct bfd_link_info *info)
6486 {
6487   asection *ri;
6488
6489   bfd *dynobj;
6490   asection *s;
6491   struct mips_got_info *g;
6492   int i;
6493   bfd_size_type loadable_size = 0;
6494   bfd_size_type local_gotno;
6495   bfd *sub;
6496   struct mips_elf_count_tls_arg count_tls_arg;
6497
6498   /* The .reginfo section has a fixed size.  */
6499   ri = bfd_get_section_by_name (output_bfd, ".reginfo");
6500   if (ri != NULL)
6501     bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
6502
6503   if (! (info->relocatable
6504          || ! mips_elf_hash_table (info)->mips16_stubs_seen))
6505     mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6506                                  mips_elf_check_mips16_stubs, NULL);
6507
6508   dynobj = elf_hash_table (info)->dynobj;
6509   if (dynobj == NULL)
6510     /* Relocatable links don't have it.  */
6511     return TRUE;
6512
6513   g = mips_elf_got_info (dynobj, &s);
6514   if (s == NULL)
6515     return TRUE;
6516
6517   /* Calculate the total loadable size of the output.  That
6518      will give us the maximum number of GOT_PAGE entries
6519      required.  */
6520   for (sub = info->input_bfds; sub; sub = sub->link_next)
6521     {
6522       asection *subsection;
6523
6524       for (subsection = sub->sections;
6525            subsection;
6526            subsection = subsection->next)
6527         {
6528           if ((subsection->flags & SEC_ALLOC) == 0)
6529             continue;
6530           loadable_size += ((subsection->size + 0xf)
6531                             &~ (bfd_size_type) 0xf);
6532         }
6533     }
6534
6535   /* There has to be a global GOT entry for every symbol with
6536      a dynamic symbol table index of DT_MIPS_GOTSYM or
6537      higher.  Therefore, it make sense to put those symbols
6538      that need GOT entries at the end of the symbol table.  We
6539      do that here.  */
6540   if (! mips_elf_sort_hash_table (info, 1))
6541     return FALSE;
6542
6543   if (g->global_gotsym != NULL)
6544     i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
6545   else
6546     /* If there are no global symbols, or none requiring
6547        relocations, then GLOBAL_GOTSYM will be NULL.  */
6548     i = 0;
6549
6550   /* In the worst case, we'll get one stub per dynamic symbol, plus
6551      one to account for the dummy entry at the end required by IRIX
6552      rld.  */
6553   loadable_size += MIPS_FUNCTION_STUB_SIZE * (i + 1);
6554
6555   /* Assume there are two loadable segments consisting of
6556      contiguous sections.  Is 5 enough?  */
6557   local_gotno = (loadable_size >> 16) + 5;
6558
6559   g->local_gotno += local_gotno;
6560   s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
6561
6562   g->global_gotno = i;
6563   s->size += i * MIPS_ELF_GOT_SIZE (output_bfd);
6564
6565   /* We need to calculate tls_gotno for global symbols at this point
6566      instead of building it up earlier, to avoid doublecounting
6567      entries for one global symbol from multiple input files.  */
6568   count_tls_arg.info = info;
6569   count_tls_arg.needed = 0;
6570   elf_link_hash_traverse (elf_hash_table (info),
6571                           mips_elf_count_global_tls_entries,
6572                           &count_tls_arg);
6573   g->tls_gotno += count_tls_arg.needed;
6574   s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
6575
6576   mips_elf_resolve_final_got_entries (g);
6577
6578   if (s->size > MIPS_ELF_GOT_MAX_SIZE (output_bfd))
6579     {
6580       if (! mips_elf_multi_got (output_bfd, info, g, s, local_gotno))
6581         return FALSE;
6582     }
6583   else
6584     {
6585       /* Set up TLS entries for the first GOT.  */
6586       g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
6587       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
6588     }
6589
6590   return TRUE;
6591 }
6592
6593 /* Set the sizes of the dynamic sections.  */
6594
6595 bfd_boolean
6596 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
6597                                      struct bfd_link_info *info)
6598 {
6599   bfd *dynobj;
6600   asection *s;
6601   bfd_boolean reltext;
6602
6603   dynobj = elf_hash_table (info)->dynobj;
6604   BFD_ASSERT (dynobj != NULL);
6605
6606   if (elf_hash_table (info)->dynamic_sections_created)
6607     {
6608       /* Set the contents of the .interp section to the interpreter.  */
6609       if (info->executable)
6610         {
6611           s = bfd_get_section_by_name (dynobj, ".interp");
6612           BFD_ASSERT (s != NULL);
6613           s->size
6614             = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
6615           s->contents
6616             = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
6617         }
6618     }
6619
6620   /* The check_relocs and adjust_dynamic_symbol entry points have
6621      determined the sizes of the various dynamic sections.  Allocate
6622      memory for them.  */
6623   reltext = FALSE;
6624   for (s = dynobj->sections; s != NULL; s = s->next)
6625     {
6626       const char *name;
6627       bfd_boolean strip;
6628
6629       /* It's OK to base decisions on the section name, because none
6630          of the dynobj section names depend upon the input files.  */
6631       name = bfd_get_section_name (dynobj, s);
6632
6633       if ((s->flags & SEC_LINKER_CREATED) == 0)
6634         continue;
6635
6636       strip = FALSE;
6637
6638       if (strncmp (name, ".rel", 4) == 0)
6639         {
6640           if (s->size == 0)
6641             {
6642               /* We only strip the section if the output section name
6643                  has the same name.  Otherwise, there might be several
6644                  input sections for this output section.  FIXME: This
6645                  code is probably not needed these days anyhow, since
6646                  the linker now does not create empty output sections.  */
6647               if (s->output_section != NULL
6648                   && strcmp (name,
6649                              bfd_get_section_name (s->output_section->owner,
6650                                                    s->output_section)) == 0)
6651                 strip = TRUE;
6652             }
6653           else
6654             {
6655               const char *outname;
6656               asection *target;
6657
6658               /* If this relocation section applies to a read only
6659                  section, then we probably need a DT_TEXTREL entry.
6660                  If the relocation section is .rel.dyn, we always
6661                  assert a DT_TEXTREL entry rather than testing whether
6662                  there exists a relocation to a read only section or
6663                  not.  */
6664               outname = bfd_get_section_name (output_bfd,
6665                                               s->output_section);
6666               target = bfd_get_section_by_name (output_bfd, outname + 4);
6667               if ((target != NULL
6668                    && (target->flags & SEC_READONLY) != 0
6669                    && (target->flags & SEC_ALLOC) != 0)
6670                   || strcmp (outname, ".rel.dyn") == 0)
6671                 reltext = TRUE;
6672
6673               /* We use the reloc_count field as a counter if we need
6674                  to copy relocs into the output file.  */
6675               if (strcmp (name, ".rel.dyn") != 0)
6676                 s->reloc_count = 0;
6677
6678               /* If combreloc is enabled, elf_link_sort_relocs() will
6679                  sort relocations, but in a different way than we do,
6680                  and before we're done creating relocations.  Also, it
6681                  will move them around between input sections'
6682                  relocation's contents, so our sorting would be
6683                  broken, so don't let it run.  */
6684               info->combreloc = 0;
6685             }
6686         }
6687       else if (strncmp (name, ".got", 4) == 0)
6688         {
6689           /* _bfd_mips_elf_always_size_sections() has already done
6690              most of the work, but some symbols may have been mapped
6691              to versions that we must now resolve in the got_entries
6692              hash tables.  */
6693           struct mips_got_info *gg = mips_elf_got_info (dynobj, NULL);
6694           struct mips_got_info *g = gg;
6695           struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
6696           unsigned int needed_relocs = 0;
6697
6698           if (gg->next)
6699             {
6700               set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (output_bfd);
6701               set_got_offset_arg.info = info;
6702
6703               /* NOTE 2005-02-03: How can this call, or the next, ever
6704                  find any indirect entries to resolve?  They were all
6705                  resolved in mips_elf_multi_got.  */
6706               mips_elf_resolve_final_got_entries (gg);
6707               for (g = gg->next; g && g->next != gg; g = g->next)
6708                 {
6709                   unsigned int save_assign;
6710
6711                   mips_elf_resolve_final_got_entries (g);
6712
6713                   /* Assign offsets to global GOT entries.  */
6714                   save_assign = g->assigned_gotno;
6715                   g->assigned_gotno = g->local_gotno;
6716                   set_got_offset_arg.g = g;
6717                   set_got_offset_arg.needed_relocs = 0;
6718                   htab_traverse (g->got_entries,
6719                                  mips_elf_set_global_got_offset,
6720                                  &set_got_offset_arg);
6721                   needed_relocs += set_got_offset_arg.needed_relocs;
6722                   BFD_ASSERT (g->assigned_gotno - g->local_gotno
6723                               <= g->global_gotno);
6724
6725                   g->assigned_gotno = save_assign;
6726                   if (info->shared)
6727                     {
6728                       needed_relocs += g->local_gotno - g->assigned_gotno;
6729                       BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
6730                                   + g->next->global_gotno
6731                                   + g->next->tls_gotno
6732                                   + MIPS_RESERVED_GOTNO);
6733                     }
6734                 }
6735             }
6736           else
6737             {
6738               struct mips_elf_count_tls_arg arg;
6739               arg.info = info;
6740               arg.needed = 0;
6741
6742               htab_traverse (gg->got_entries, mips_elf_count_local_tls_relocs,
6743                              &arg);
6744               elf_link_hash_traverse (elf_hash_table (info),
6745                                       mips_elf_count_global_tls_relocs,
6746                                       &arg);
6747
6748               needed_relocs += arg.needed;
6749             }
6750
6751           if (needed_relocs)
6752             mips_elf_allocate_dynamic_relocations (dynobj, needed_relocs);
6753         }
6754       else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
6755         {
6756           /* IRIX rld assumes that the function stub isn't at the end
6757              of .text section. So put a dummy. XXX  */
6758           s->size += MIPS_FUNCTION_STUB_SIZE;
6759         }
6760       else if (! info->shared
6761                && ! mips_elf_hash_table (info)->use_rld_obj_head
6762                && strncmp (name, ".rld_map", 8) == 0)
6763         {
6764           /* We add a room for __rld_map. It will be filled in by the
6765              rtld to contain a pointer to the _r_debug structure.  */
6766           s->size += 4;
6767         }
6768       else if (SGI_COMPAT (output_bfd)
6769                && strncmp (name, ".compact_rel", 12) == 0)
6770         s->size += mips_elf_hash_table (info)->compact_rel_size;
6771       else if (strncmp (name, ".init", 5) != 0)
6772         {
6773           /* It's not one of our sections, so don't allocate space.  */
6774           continue;
6775         }
6776
6777       if (strip)
6778         {
6779           _bfd_strip_section_from_output (info, s);
6780           continue;
6781         }
6782
6783       /* Allocate memory for the section contents.  */
6784       s->contents = bfd_zalloc (dynobj, s->size);
6785       if (s->contents == NULL && s->size != 0)
6786         {
6787           bfd_set_error (bfd_error_no_memory);
6788           return FALSE;
6789         }
6790     }
6791
6792   if (elf_hash_table (info)->dynamic_sections_created)
6793     {
6794       /* Add some entries to the .dynamic section.  We fill in the
6795          values later, in _bfd_mips_elf_finish_dynamic_sections, but we
6796          must add the entries now so that we get the correct size for
6797          the .dynamic section.  The DT_DEBUG entry is filled in by the
6798          dynamic linker and used by the debugger.  */
6799       if (! info->shared)
6800         {
6801           /* SGI object has the equivalence of DT_DEBUG in the
6802              DT_MIPS_RLD_MAP entry.  */
6803           if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
6804             return FALSE;
6805           if (!SGI_COMPAT (output_bfd))
6806             {
6807               if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
6808                 return FALSE;
6809             }
6810         }
6811       else
6812         {
6813           /* Shared libraries on traditional mips have DT_DEBUG.  */
6814           if (!SGI_COMPAT (output_bfd))
6815             {
6816               if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
6817                 return FALSE;
6818             }
6819         }
6820
6821       if (reltext && SGI_COMPAT (output_bfd))
6822         info->flags |= DF_TEXTREL;
6823
6824       if ((info->flags & DF_TEXTREL) != 0)
6825         {
6826           if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
6827             return FALSE;
6828         }
6829
6830       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
6831         return FALSE;
6832
6833       if (mips_elf_rel_dyn_section (dynobj, FALSE))
6834         {
6835           if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
6836             return FALSE;
6837
6838           if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
6839             return FALSE;
6840
6841           if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
6842             return FALSE;
6843         }
6844
6845       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
6846         return FALSE;
6847
6848       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
6849         return FALSE;
6850
6851       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
6852         return FALSE;
6853
6854       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
6855         return FALSE;
6856
6857       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
6858         return FALSE;
6859
6860       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
6861         return FALSE;
6862
6863       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
6864         return FALSE;
6865
6866       if (IRIX_COMPAT (dynobj) == ict_irix5
6867           && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
6868         return FALSE;
6869
6870       if (IRIX_COMPAT (dynobj) == ict_irix6
6871           && (bfd_get_section_by_name
6872               (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
6873           && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
6874         return FALSE;
6875     }
6876
6877   return TRUE;
6878 }
6879 \f
6880 /* Relocate a MIPS ELF section.  */
6881
6882 bfd_boolean
6883 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
6884                                 bfd *input_bfd, asection *input_section,
6885                                 bfd_byte *contents, Elf_Internal_Rela *relocs,
6886                                 Elf_Internal_Sym *local_syms,
6887                                 asection **local_sections)
6888 {
6889   Elf_Internal_Rela *rel;
6890   const Elf_Internal_Rela *relend;
6891   bfd_vma addend = 0;
6892   bfd_boolean use_saved_addend_p = FALSE;
6893   const struct elf_backend_data *bed;
6894
6895   bed = get_elf_backend_data (output_bfd);
6896   relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
6897   for (rel = relocs; rel < relend; ++rel)
6898     {
6899       const char *name;
6900       bfd_vma value;
6901       reloc_howto_type *howto;
6902       bfd_boolean require_jalx;
6903       /* TRUE if the relocation is a RELA relocation, rather than a
6904          REL relocation.  */
6905       bfd_boolean rela_relocation_p = TRUE;
6906       unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6907       const char *msg;
6908
6909       /* Find the relocation howto for this relocation.  */
6910       if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
6911         {
6912           /* Some 32-bit code uses R_MIPS_64.  In particular, people use
6913              64-bit code, but make sure all their addresses are in the
6914              lowermost or uppermost 32-bit section of the 64-bit address
6915              space.  Thus, when they use an R_MIPS_64 they mean what is
6916              usually meant by R_MIPS_32, with the exception that the
6917              stored value is sign-extended to 64 bits.  */
6918           howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
6919
6920           /* On big-endian systems, we need to lie about the position
6921              of the reloc.  */
6922           if (bfd_big_endian (input_bfd))
6923             rel->r_offset += 4;
6924         }
6925       else
6926         /* NewABI defaults to RELA relocations.  */
6927         howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
6928                                          NEWABI_P (input_bfd)
6929                                          && (MIPS_RELOC_RELA_P
6930                                              (input_bfd, input_section,
6931                                               rel - relocs)));
6932
6933       if (!use_saved_addend_p)
6934         {
6935           Elf_Internal_Shdr *rel_hdr;
6936
6937           /* If these relocations were originally of the REL variety,
6938              we must pull the addend out of the field that will be
6939              relocated.  Otherwise, we simply use the contents of the
6940              RELA relocation.  To determine which flavor or relocation
6941              this is, we depend on the fact that the INPUT_SECTION's
6942              REL_HDR is read before its REL_HDR2.  */
6943           rel_hdr = &elf_section_data (input_section)->rel_hdr;
6944           if ((size_t) (rel - relocs)
6945               >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
6946             rel_hdr = elf_section_data (input_section)->rel_hdr2;
6947           if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
6948             {
6949               bfd_byte *location = contents + rel->r_offset;
6950
6951               /* Note that this is a REL relocation.  */
6952               rela_relocation_p = FALSE;
6953
6954               /* Get the addend, which is stored in the input file.  */
6955               _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE,
6956                                                location);
6957               addend = mips_elf_obtain_contents (howto, rel, input_bfd,
6958                                                  contents);
6959               _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, FALSE,
6960                                             location);
6961
6962               addend &= howto->src_mask;
6963
6964               /* For some kinds of relocations, the ADDEND is a
6965                  combination of the addend stored in two different
6966                  relocations.   */
6967               if (r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16
6968                   || (r_type == R_MIPS_GOT16
6969                       && mips_elf_local_relocation_p (input_bfd, rel,
6970                                                       local_sections, FALSE)))
6971                 {
6972                   bfd_vma l;
6973                   const Elf_Internal_Rela *lo16_relocation;
6974                   reloc_howto_type *lo16_howto;
6975                   bfd_byte *lo16_location;
6976                   int lo16_type;
6977
6978                   if (r_type == R_MIPS16_HI16)
6979                     lo16_type = R_MIPS16_LO16;
6980                   else
6981                     lo16_type = R_MIPS_LO16;
6982
6983                   /* The combined value is the sum of the HI16 addend,
6984                      left-shifted by sixteen bits, and the LO16
6985                      addend, sign extended.  (Usually, the code does
6986                      a `lui' of the HI16 value, and then an `addiu' of
6987                      the LO16 value.)
6988
6989                      Scan ahead to find a matching LO16 relocation.
6990
6991                      According to the MIPS ELF ABI, the R_MIPS_LO16
6992                      relocation must be immediately following.
6993                      However, for the IRIX6 ABI, the next relocation
6994                      may be a composed relocation consisting of
6995                      several relocations for the same address.  In
6996                      that case, the R_MIPS_LO16 relocation may occur
6997                      as one of these.  We permit a similar extension
6998                      in general, as that is useful for GCC.  */
6999                   lo16_relocation = mips_elf_next_relocation (input_bfd,
7000                                                               lo16_type,
7001                                                               rel, relend);
7002                   if (lo16_relocation == NULL)
7003                     return FALSE;
7004
7005                   lo16_location = contents + lo16_relocation->r_offset;
7006
7007                   /* Obtain the addend kept there.  */
7008                   lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd,
7009                                                         lo16_type, FALSE);
7010                   _bfd_mips16_elf_reloc_unshuffle (input_bfd, lo16_type, FALSE,
7011                                                    lo16_location);
7012                   l = mips_elf_obtain_contents (lo16_howto, lo16_relocation,
7013                                                 input_bfd, contents);
7014                   _bfd_mips16_elf_reloc_shuffle (input_bfd, lo16_type, FALSE,
7015                                                  lo16_location);
7016                   l &= lo16_howto->src_mask;
7017                   l <<= lo16_howto->rightshift;
7018                   l = _bfd_mips_elf_sign_extend (l, 16);
7019
7020                   addend <<= 16;
7021
7022                   /* Compute the combined addend.  */
7023                   addend += l;
7024                 }
7025               else
7026                 addend <<= howto->rightshift;
7027             }
7028           else
7029             addend = rel->r_addend;
7030         }
7031
7032       if (info->relocatable)
7033         {
7034           Elf_Internal_Sym *sym;
7035           unsigned long r_symndx;
7036
7037           if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
7038               && bfd_big_endian (input_bfd))
7039             rel->r_offset -= 4;
7040
7041           /* Since we're just relocating, all we need to do is copy
7042              the relocations back out to the object file, unless
7043              they're against a section symbol, in which case we need
7044              to adjust by the section offset, or unless they're GP
7045              relative in which case we need to adjust by the amount
7046              that we're adjusting GP in this relocatable object.  */
7047
7048           if (! mips_elf_local_relocation_p (input_bfd, rel, local_sections,
7049                                              FALSE))
7050             /* There's nothing to do for non-local relocations.  */
7051             continue;
7052
7053           if (r_type == R_MIPS16_GPREL
7054               || r_type == R_MIPS_GPREL16
7055               || r_type == R_MIPS_GPREL32
7056               || r_type == R_MIPS_LITERAL)
7057             addend -= (_bfd_get_gp_value (output_bfd)
7058                        - _bfd_get_gp_value (input_bfd));
7059
7060           r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
7061           sym = local_syms + r_symndx;
7062           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7063             /* Adjust the addend appropriately.  */
7064             addend += local_sections[r_symndx]->output_offset;
7065
7066           if (rela_relocation_p)
7067             /* If this is a RELA relocation, just update the addend.  */
7068             rel->r_addend = addend;
7069           else
7070             {
7071               if (r_type == R_MIPS_HI16
7072                   || r_type == R_MIPS_GOT16)
7073                 addend = mips_elf_high (addend);
7074               else if (r_type == R_MIPS_HIGHER)
7075                 addend = mips_elf_higher (addend);
7076               else if (r_type == R_MIPS_HIGHEST)
7077                 addend = mips_elf_highest (addend);
7078               else
7079                 addend >>= howto->rightshift;
7080
7081               /* We use the source mask, rather than the destination
7082                  mask because the place to which we are writing will be
7083                  source of the addend in the final link.  */
7084               addend &= howto->src_mask;
7085
7086               if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
7087                 /* See the comment above about using R_MIPS_64 in the 32-bit
7088                    ABI.  Here, we need to update the addend.  It would be
7089                    possible to get away with just using the R_MIPS_32 reloc
7090                    but for endianness.  */
7091                 {
7092                   bfd_vma sign_bits;
7093                   bfd_vma low_bits;
7094                   bfd_vma high_bits;
7095
7096                   if (addend & ((bfd_vma) 1 << 31))
7097 #ifdef BFD64
7098                     sign_bits = ((bfd_vma) 1 << 32) - 1;
7099 #else
7100                     sign_bits = -1;
7101 #endif
7102                   else
7103                     sign_bits = 0;
7104
7105                   /* If we don't know that we have a 64-bit type,
7106                      do two separate stores.  */
7107                   if (bfd_big_endian (input_bfd))
7108                     {
7109                       /* Store the sign-bits (which are most significant)
7110                          first.  */
7111                       low_bits = sign_bits;
7112                       high_bits = addend;
7113                     }
7114                   else
7115                     {
7116                       low_bits = addend;
7117                       high_bits = sign_bits;
7118                     }
7119                   bfd_put_32 (input_bfd, low_bits,
7120                               contents + rel->r_offset);
7121                   bfd_put_32 (input_bfd, high_bits,
7122                               contents + rel->r_offset + 4);
7123                   continue;
7124                 }
7125
7126               if (! mips_elf_perform_relocation (info, howto, rel, addend,
7127                                                  input_bfd, input_section,
7128                                                  contents, FALSE))
7129                 return FALSE;
7130             }
7131
7132           /* Go on to the next relocation.  */
7133           continue;
7134         }
7135
7136       /* In the N32 and 64-bit ABIs there may be multiple consecutive
7137          relocations for the same offset.  In that case we are
7138          supposed to treat the output of each relocation as the addend
7139          for the next.  */
7140       if (rel + 1 < relend
7141           && rel->r_offset == rel[1].r_offset
7142           && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
7143         use_saved_addend_p = TRUE;
7144       else
7145         use_saved_addend_p = FALSE;
7146
7147       /* Figure out what value we are supposed to relocate.  */
7148       switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
7149                                              input_section, info, rel,
7150                                              addend, howto, local_syms,
7151                                              local_sections, &value,
7152                                              &name, &require_jalx,
7153                                              use_saved_addend_p))
7154         {
7155         case bfd_reloc_continue:
7156           /* There's nothing to do.  */
7157           continue;
7158
7159         case bfd_reloc_undefined:
7160           /* mips_elf_calculate_relocation already called the
7161              undefined_symbol callback.  There's no real point in
7162              trying to perform the relocation at this point, so we
7163              just skip ahead to the next relocation.  */
7164           continue;
7165
7166         case bfd_reloc_notsupported:
7167           msg = _("internal error: unsupported relocation error");
7168           info->callbacks->warning
7169             (info, msg, name, input_bfd, input_section, rel->r_offset);
7170           return FALSE;
7171
7172         case bfd_reloc_overflow:
7173           if (use_saved_addend_p)
7174             /* Ignore overflow until we reach the last relocation for
7175                a given location.  */
7176             ;
7177           else
7178             {
7179               BFD_ASSERT (name != NULL);
7180               if (! ((*info->callbacks->reloc_overflow)
7181                      (info, NULL, name, howto->name, (bfd_vma) 0,
7182                       input_bfd, input_section, rel->r_offset)))
7183                 return FALSE;
7184             }
7185           break;
7186
7187         case bfd_reloc_ok:
7188           break;
7189
7190         default:
7191           abort ();
7192           break;
7193         }
7194
7195       /* If we've got another relocation for the address, keep going
7196          until we reach the last one.  */
7197       if (use_saved_addend_p)
7198         {
7199           addend = value;
7200           continue;
7201         }
7202
7203       if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
7204         /* See the comment above about using R_MIPS_64 in the 32-bit
7205            ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
7206            that calculated the right value.  Now, however, we
7207            sign-extend the 32-bit result to 64-bits, and store it as a
7208            64-bit value.  We are especially generous here in that we
7209            go to extreme lengths to support this usage on systems with
7210            only a 32-bit VMA.  */
7211         {
7212           bfd_vma sign_bits;
7213           bfd_vma low_bits;
7214           bfd_vma high_bits;
7215
7216           if (value & ((bfd_vma) 1 << 31))
7217 #ifdef BFD64
7218             sign_bits = ((bfd_vma) 1 << 32) - 1;
7219 #else
7220             sign_bits = -1;
7221 #endif
7222           else
7223             sign_bits = 0;
7224
7225           /* If we don't know that we have a 64-bit type,
7226              do two separate stores.  */
7227           if (bfd_big_endian (input_bfd))
7228             {
7229               /* Undo what we did above.  */
7230               rel->r_offset -= 4;
7231               /* Store the sign-bits (which are most significant)
7232                  first.  */
7233               low_bits = sign_bits;
7234               high_bits = value;
7235             }
7236           else
7237             {
7238               low_bits = value;
7239               high_bits = sign_bits;
7240             }
7241           bfd_put_32 (input_bfd, low_bits,
7242                       contents + rel->r_offset);
7243           bfd_put_32 (input_bfd, high_bits,
7244                       contents + rel->r_offset + 4);
7245           continue;
7246         }
7247
7248       /* Actually perform the relocation.  */
7249       if (! mips_elf_perform_relocation (info, howto, rel, value,
7250                                          input_bfd, input_section,
7251                                          contents, require_jalx))
7252         return FALSE;
7253     }
7254
7255   return TRUE;
7256 }
7257 \f
7258 /* If NAME is one of the special IRIX6 symbols defined by the linker,
7259    adjust it appropriately now.  */
7260
7261 static void
7262 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
7263                                       const char *name, Elf_Internal_Sym *sym)
7264 {
7265   /* The linker script takes care of providing names and values for
7266      these, but we must place them into the right sections.  */
7267   static const char* const text_section_symbols[] = {
7268     "_ftext",
7269     "_etext",
7270     "__dso_displacement",
7271     "__elf_header",
7272     "__program_header_table",
7273     NULL
7274   };
7275
7276   static const char* const data_section_symbols[] = {
7277     "_fdata",
7278     "_edata",
7279     "_end",
7280     "_fbss",
7281     NULL
7282   };
7283
7284   const char* const *p;
7285   int i;
7286
7287   for (i = 0; i < 2; ++i)
7288     for (p = (i == 0) ? text_section_symbols : data_section_symbols;
7289          *p;
7290          ++p)
7291       if (strcmp (*p, name) == 0)
7292         {
7293           /* All of these symbols are given type STT_SECTION by the
7294              IRIX6 linker.  */
7295           sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7296           sym->st_other = STO_PROTECTED;
7297
7298           /* The IRIX linker puts these symbols in special sections.  */
7299           if (i == 0)
7300             sym->st_shndx = SHN_MIPS_TEXT;
7301           else
7302             sym->st_shndx = SHN_MIPS_DATA;
7303
7304           break;
7305         }
7306 }
7307
7308 /* Finish up dynamic symbol handling.  We set the contents of various
7309    dynamic sections here.  */
7310
7311 bfd_boolean
7312 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
7313                                      struct bfd_link_info *info,
7314                                      struct elf_link_hash_entry *h,
7315                                      Elf_Internal_Sym *sym)
7316 {
7317   bfd *dynobj;
7318   asection *sgot;
7319   struct mips_got_info *g, *gg;
7320   const char *name;
7321
7322   dynobj = elf_hash_table (info)->dynobj;
7323
7324   if (h->plt.offset != MINUS_ONE)
7325     {
7326       asection *s;
7327       bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7328
7329       /* This symbol has a stub.  Set it up.  */
7330
7331       BFD_ASSERT (h->dynindx != -1);
7332
7333       s = bfd_get_section_by_name (dynobj,
7334                                    MIPS_ELF_STUB_SECTION_NAME (dynobj));
7335       BFD_ASSERT (s != NULL);
7336
7337       /* FIXME: Can h->dynindex be more than 64K?  */
7338       if (h->dynindx & 0xffff0000)
7339         return FALSE;
7340
7341       /* Fill the stub.  */
7342       bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub);
7343       bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + 4);
7344       bfd_put_32 (output_bfd, STUB_JALR, stub + 8);
7345       bfd_put_32 (output_bfd, STUB_LI16 (output_bfd) + h->dynindx, stub + 12);
7346
7347       BFD_ASSERT (h->plt.offset <= s->size);
7348       memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7349
7350       /* Mark the symbol as undefined.  plt.offset != -1 occurs
7351          only for the referenced symbol.  */
7352       sym->st_shndx = SHN_UNDEF;
7353
7354       /* The run-time linker uses the st_value field of the symbol
7355          to reset the global offset table entry for this external
7356          to its stub address when unlinking a shared object.  */
7357       sym->st_value = (s->output_section->vma + s->output_offset
7358                        + h->plt.offset);
7359     }
7360
7361   BFD_ASSERT (h->dynindx != -1
7362               || h->forced_local);
7363
7364   sgot = mips_elf_got_section (dynobj, FALSE);
7365   BFD_ASSERT (sgot != NULL);
7366   BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
7367   g = mips_elf_section_data (sgot)->u.got_info;
7368   BFD_ASSERT (g != NULL);
7369
7370   /* Run through the global symbol table, creating GOT entries for all
7371      the symbols that need them.  */
7372   if (g->global_gotsym != NULL
7373       && h->dynindx >= g->global_gotsym->dynindx)
7374     {
7375       bfd_vma offset;
7376       bfd_vma value;
7377
7378       value = sym->st_value;
7379       offset = mips_elf_global_got_index (dynobj, output_bfd, h, R_MIPS_GOT16, info);
7380       MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
7381     }
7382
7383   if (g->next && h->dynindx != -1 && h->type != STT_TLS)
7384     {
7385       struct mips_got_entry e, *p;
7386       bfd_vma entry;
7387       bfd_vma offset;
7388
7389       gg = g;
7390
7391       e.abfd = output_bfd;
7392       e.symndx = -1;
7393       e.d.h = (struct mips_elf_link_hash_entry *)h;
7394       e.tls_type = 0;
7395
7396       for (g = g->next; g->next != gg; g = g->next)
7397         {
7398           if (g->got_entries
7399               && (p = (struct mips_got_entry *) htab_find (g->got_entries,
7400                                                            &e)))
7401             {
7402               offset = p->gotidx;
7403               if (info->shared
7404                   || (elf_hash_table (info)->dynamic_sections_created
7405                       && p->d.h != NULL
7406                       && p->d.h->root.def_dynamic
7407                       && !p->d.h->root.def_regular))
7408                 {
7409                   /* Create an R_MIPS_REL32 relocation for this entry.  Due to
7410                      the various compatibility problems, it's easier to mock
7411                      up an R_MIPS_32 or R_MIPS_64 relocation and leave
7412                      mips_elf_create_dynamic_relocation to calculate the
7413                      appropriate addend.  */
7414                   Elf_Internal_Rela rel[3];
7415
7416                   memset (rel, 0, sizeof (rel));
7417                   if (ABI_64_P (output_bfd))
7418                     rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
7419                   else
7420                     rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
7421                   rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
7422
7423                   entry = 0;
7424                   if (! (mips_elf_create_dynamic_relocation
7425                          (output_bfd, info, rel,
7426                           e.d.h, NULL, sym->st_value, &entry, sgot)))
7427                     return FALSE;
7428                 }
7429               else
7430                 entry = sym->st_value;
7431               MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
7432             }
7433         }
7434     }
7435
7436   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
7437   name = h->root.root.string;
7438   if (strcmp (name, "_DYNAMIC") == 0
7439       || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7440     sym->st_shndx = SHN_ABS;
7441   else if (strcmp (name, "_DYNAMIC_LINK") == 0
7442            || strcmp (name, "_DYNAMIC_LINKING") == 0)
7443     {
7444       sym->st_shndx = SHN_ABS;
7445       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7446       sym->st_value = 1;
7447     }
7448   else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
7449     {
7450       sym->st_shndx = SHN_ABS;
7451       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7452       sym->st_value = elf_gp (output_bfd);
7453     }
7454   else if (SGI_COMPAT (output_bfd))
7455     {
7456       if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7457           || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7458         {
7459           sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7460           sym->st_other = STO_PROTECTED;
7461           sym->st_value = 0;
7462           sym->st_shndx = SHN_MIPS_DATA;
7463         }
7464       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7465         {
7466           sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7467           sym->st_other = STO_PROTECTED;
7468           sym->st_value = mips_elf_hash_table (info)->procedure_count;
7469           sym->st_shndx = SHN_ABS;
7470         }
7471       else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7472         {
7473           if (h->type == STT_FUNC)
7474             sym->st_shndx = SHN_MIPS_TEXT;
7475           else if (h->type == STT_OBJECT)
7476             sym->st_shndx = SHN_MIPS_DATA;
7477         }
7478     }
7479
7480   /* Handle the IRIX6-specific symbols.  */
7481   if (IRIX_COMPAT (output_bfd) == ict_irix6)
7482     mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
7483
7484   if (! info->shared)
7485     {
7486       if (! mips_elf_hash_table (info)->use_rld_obj_head
7487           && (strcmp (name, "__rld_map") == 0
7488               || strcmp (name, "__RLD_MAP") == 0))
7489         {
7490           asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7491           BFD_ASSERT (s != NULL);
7492           sym->st_value = s->output_section->vma + s->output_offset;
7493           bfd_put_32 (output_bfd, 0, s->contents);
7494           if (mips_elf_hash_table (info)->rld_value == 0)
7495             mips_elf_hash_table (info)->rld_value = sym->st_value;
7496         }
7497       else if (mips_elf_hash_table (info)->use_rld_obj_head
7498                && strcmp (name, "__rld_obj_head") == 0)
7499         {
7500           /* IRIX6 does not use a .rld_map section.  */
7501           if (IRIX_COMPAT (output_bfd) == ict_irix5
7502               || IRIX_COMPAT (output_bfd) == ict_none)
7503             BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
7504                         != NULL);
7505           mips_elf_hash_table (info)->rld_value = sym->st_value;
7506         }
7507     }
7508
7509   /* If this is a mips16 symbol, force the value to be even.  */
7510   if (sym->st_other == STO_MIPS16)
7511     sym->st_value &= ~1;
7512
7513   return TRUE;
7514 }
7515
7516 /* Finish up the dynamic sections.  */
7517
7518 bfd_boolean
7519 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
7520                                        struct bfd_link_info *info)
7521 {
7522   bfd *dynobj;
7523   asection *sdyn;
7524   asection *sgot;
7525   struct mips_got_info *gg, *g;
7526
7527   dynobj = elf_hash_table (info)->dynobj;
7528
7529   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7530
7531   sgot = mips_elf_got_section (dynobj, FALSE);
7532   if (sgot == NULL)
7533     gg = g = NULL;
7534   else
7535     {
7536       BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
7537       gg = mips_elf_section_data (sgot)->u.got_info;
7538       BFD_ASSERT (gg != NULL);
7539       g = mips_elf_got_for_ibfd (gg, output_bfd);
7540       BFD_ASSERT (g != NULL);
7541     }
7542
7543   if (elf_hash_table (info)->dynamic_sections_created)
7544     {
7545       bfd_byte *b;
7546
7547       BFD_ASSERT (sdyn != NULL);
7548       BFD_ASSERT (g != NULL);
7549
7550       for (b = sdyn->contents;
7551            b < sdyn->contents + sdyn->size;
7552            b += MIPS_ELF_DYN_SIZE (dynobj))
7553         {
7554           Elf_Internal_Dyn dyn;
7555           const char *name;
7556           size_t elemsize;
7557           asection *s;
7558           bfd_boolean swap_out_p;
7559
7560           /* Read in the current dynamic entry.  */
7561           (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
7562
7563           /* Assume that we're going to modify it and write it out.  */
7564           swap_out_p = TRUE;
7565
7566           switch (dyn.d_tag)
7567             {
7568             case DT_RELENT:
7569               s = mips_elf_rel_dyn_section (dynobj, FALSE);
7570               BFD_ASSERT (s != NULL);
7571               dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
7572               break;
7573
7574             case DT_STRSZ:
7575               /* Rewrite DT_STRSZ.  */
7576               dyn.d_un.d_val =
7577                 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7578               break;
7579
7580             case DT_PLTGOT:
7581               name = ".got";
7582               s = bfd_get_section_by_name (output_bfd, name);
7583               BFD_ASSERT (s != NULL);
7584               dyn.d_un.d_ptr = s->vma;
7585               break;
7586
7587             case DT_MIPS_RLD_VERSION:
7588               dyn.d_un.d_val = 1; /* XXX */
7589               break;
7590
7591             case DT_MIPS_FLAGS:
7592               dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7593               break;
7594
7595             case DT_MIPS_TIME_STAMP:
7596               time ((time_t *) &dyn.d_un.d_val);
7597               break;
7598
7599             case DT_MIPS_ICHECKSUM:
7600               /* XXX FIXME: */
7601               swap_out_p = FALSE;
7602               break;
7603
7604             case DT_MIPS_IVERSION:
7605               /* XXX FIXME: */
7606               swap_out_p = FALSE;
7607               break;
7608
7609             case DT_MIPS_BASE_ADDRESS:
7610               s = output_bfd->sections;
7611               BFD_ASSERT (s != NULL);
7612               dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
7613               break;
7614
7615             case DT_MIPS_LOCAL_GOTNO:
7616               dyn.d_un.d_val = g->local_gotno;
7617               break;
7618
7619             case DT_MIPS_UNREFEXTNO:
7620               /* The index into the dynamic symbol table which is the
7621                  entry of the first external symbol that is not
7622                  referenced within the same object.  */
7623               dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
7624               break;
7625
7626             case DT_MIPS_GOTSYM:
7627               if (gg->global_gotsym)
7628                 {
7629                   dyn.d_un.d_val = gg->global_gotsym->dynindx;
7630                   break;
7631                 }
7632               /* In case if we don't have global got symbols we default
7633                  to setting DT_MIPS_GOTSYM to the same value as
7634                  DT_MIPS_SYMTABNO, so we just fall through.  */
7635
7636             case DT_MIPS_SYMTABNO:
7637               name = ".dynsym";
7638               elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
7639               s = bfd_get_section_by_name (output_bfd, name);
7640               BFD_ASSERT (s != NULL);
7641
7642               dyn.d_un.d_val = s->size / elemsize;
7643               break;
7644
7645             case DT_MIPS_HIPAGENO:
7646               dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
7647               break;
7648
7649             case DT_MIPS_RLD_MAP:
7650               dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
7651               break;
7652
7653             case DT_MIPS_OPTIONS:
7654               s = (bfd_get_section_by_name
7655                    (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
7656               dyn.d_un.d_ptr = s->vma;
7657               break;
7658
7659             case DT_RELSZ:
7660               /* Reduce DT_RELSZ to account for any relocations we
7661                  decided not to make.  This is for the n64 irix rld,
7662                  which doesn't seem to apply any relocations if there
7663                  are trailing null entries.  */
7664               s = mips_elf_rel_dyn_section (dynobj, FALSE);
7665               dyn.d_un.d_val = (s->reloc_count
7666                                 * (ABI_64_P (output_bfd)
7667                                    ? sizeof (Elf64_Mips_External_Rel)
7668                                    : sizeof (Elf32_External_Rel)));
7669               break;
7670
7671             default:
7672               swap_out_p = FALSE;
7673               break;
7674             }
7675
7676           if (swap_out_p)
7677             (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
7678               (dynobj, &dyn, b);
7679         }
7680     }
7681
7682   /* The first entry of the global offset table will be filled at
7683      runtime. The second entry will be used by some runtime loaders.
7684      This isn't the case of IRIX rld.  */
7685   if (sgot != NULL && sgot->size > 0)
7686     {
7687       MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents);
7688       MIPS_ELF_PUT_WORD (output_bfd, 0x80000000,
7689                          sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
7690     }
7691
7692   if (sgot != NULL)
7693     elf_section_data (sgot->output_section)->this_hdr.sh_entsize
7694       = MIPS_ELF_GOT_SIZE (output_bfd);
7695
7696   /* Generate dynamic relocations for the non-primary gots.  */
7697   if (gg != NULL && gg->next)
7698     {
7699       Elf_Internal_Rela rel[3];
7700       bfd_vma addend = 0;
7701
7702       memset (rel, 0, sizeof (rel));
7703       rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
7704
7705       for (g = gg->next; g->next != gg; g = g->next)
7706         {
7707           bfd_vma index = g->next->local_gotno + g->next->global_gotno
7708             + g->next->tls_gotno;
7709
7710           MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
7711                              + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
7712           MIPS_ELF_PUT_WORD (output_bfd, 0x80000000, sgot->contents
7713                              + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
7714
7715           if (! info->shared)
7716             continue;
7717
7718           while (index < g->assigned_gotno)
7719             {
7720               rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
7721                 = index++ * MIPS_ELF_GOT_SIZE (output_bfd);
7722               if (!(mips_elf_create_dynamic_relocation
7723                     (output_bfd, info, rel, NULL,
7724                      bfd_abs_section_ptr,
7725                      0, &addend, sgot)))
7726                 return FALSE;
7727               BFD_ASSERT (addend == 0);
7728             }
7729         }
7730     }
7731
7732   {
7733     asection *s;
7734     Elf32_compact_rel cpt;
7735
7736     if (SGI_COMPAT (output_bfd))
7737       {
7738         /* Write .compact_rel section out.  */
7739         s = bfd_get_section_by_name (dynobj, ".compact_rel");
7740         if (s != NULL)
7741           {
7742             cpt.id1 = 1;
7743             cpt.num = s->reloc_count;
7744             cpt.id2 = 2;
7745             cpt.offset = (s->output_section->filepos
7746                           + sizeof (Elf32_External_compact_rel));
7747             cpt.reserved0 = 0;
7748             cpt.reserved1 = 0;
7749             bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7750                                             ((Elf32_External_compact_rel *)
7751                                              s->contents));
7752
7753             /* Clean up a dummy stub function entry in .text.  */
7754             s = bfd_get_section_by_name (dynobj,
7755                                          MIPS_ELF_STUB_SECTION_NAME (dynobj));
7756             if (s != NULL)
7757               {
7758                 file_ptr dummy_offset;
7759
7760                 BFD_ASSERT (s->size >= MIPS_FUNCTION_STUB_SIZE);
7761                 dummy_offset = s->size - MIPS_FUNCTION_STUB_SIZE;
7762                 memset (s->contents + dummy_offset, 0,
7763                         MIPS_FUNCTION_STUB_SIZE);
7764               }
7765           }
7766       }
7767
7768     /* We need to sort the entries of the dynamic relocation section.  */
7769
7770     s = mips_elf_rel_dyn_section (dynobj, FALSE);
7771
7772     if (s != NULL
7773         && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
7774       {
7775         reldyn_sorting_bfd = output_bfd;
7776
7777         if (ABI_64_P (output_bfd))
7778           qsort ((Elf64_External_Rel *) s->contents + 1, s->reloc_count - 1,
7779                  sizeof (Elf64_Mips_External_Rel), sort_dynamic_relocs_64);
7780         else
7781           qsort ((Elf32_External_Rel *) s->contents + 1, s->reloc_count - 1,
7782                  sizeof (Elf32_External_Rel), sort_dynamic_relocs);
7783       }
7784   }
7785
7786   return TRUE;
7787 }
7788
7789
7790 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
7791
7792 static void
7793 mips_set_isa_flags (bfd *abfd)
7794 {
7795   flagword val;
7796
7797   switch (bfd_get_mach (abfd))
7798     {
7799     default:
7800     case bfd_mach_mips3000:
7801       val = E_MIPS_ARCH_1;
7802       break;
7803
7804     case bfd_mach_mips3900:
7805       val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
7806       break;
7807
7808     case bfd_mach_mips6000:
7809       val = E_MIPS_ARCH_2;
7810       break;
7811
7812     case bfd_mach_mips4000:
7813     case bfd_mach_mips4300:
7814     case bfd_mach_mips4400:
7815     case bfd_mach_mips4600:
7816       val = E_MIPS_ARCH_3;
7817       break;
7818
7819     case bfd_mach_mips4010:
7820       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
7821       break;
7822
7823     case bfd_mach_mips4100:
7824       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
7825       break;
7826
7827     case bfd_mach_mips4111:
7828       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
7829       break;
7830
7831     case bfd_mach_mips4120:
7832       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
7833       break;
7834
7835     case bfd_mach_mips4650:
7836       val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
7837       break;
7838
7839     case bfd_mach_mips5400:
7840       val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
7841       break;
7842
7843     case bfd_mach_mips5500:
7844       val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
7845       break;
7846
7847     case bfd_mach_mips9000:
7848       val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
7849       break;
7850
7851     case bfd_mach_mips5000:
7852     case bfd_mach_mips7000:
7853     case bfd_mach_mips8000:
7854     case bfd_mach_mips10000:
7855     case bfd_mach_mips12000:
7856       val = E_MIPS_ARCH_4;
7857       break;
7858
7859     case bfd_mach_mips5:
7860       val = E_MIPS_ARCH_5;
7861       break;
7862
7863     case bfd_mach_mips_sb1:
7864       val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
7865       break;
7866
7867     case bfd_mach_mipsisa32:
7868       val = E_MIPS_ARCH_32;
7869       break;
7870
7871     case bfd_mach_mipsisa64:
7872       val = E_MIPS_ARCH_64;
7873       break;
7874
7875     case bfd_mach_mipsisa32r2:
7876       val = E_MIPS_ARCH_32R2;
7877       break;
7878
7879     case bfd_mach_mipsisa64r2:
7880       val = E_MIPS_ARCH_64R2;
7881       break;
7882     }
7883   elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
7884   elf_elfheader (abfd)->e_flags |= val;
7885
7886 }
7887
7888
7889 /* The final processing done just before writing out a MIPS ELF object
7890    file.  This gets the MIPS architecture right based on the machine
7891    number.  This is used by both the 32-bit and the 64-bit ABI.  */
7892
7893 void
7894 _bfd_mips_elf_final_write_processing (bfd *abfd,
7895                                       bfd_boolean linker ATTRIBUTE_UNUSED)
7896 {
7897   unsigned int i;
7898   Elf_Internal_Shdr **hdrpp;
7899   const char *name;
7900   asection *sec;
7901
7902   /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
7903      is nonzero.  This is for compatibility with old objects, which used
7904      a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
7905   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
7906     mips_set_isa_flags (abfd);
7907
7908   /* Set the sh_info field for .gptab sections and other appropriate
7909      info for each special section.  */
7910   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
7911        i < elf_numsections (abfd);
7912        i++, hdrpp++)
7913     {
7914       switch ((*hdrpp)->sh_type)
7915         {
7916         case SHT_MIPS_MSYM:
7917         case SHT_MIPS_LIBLIST:
7918           sec = bfd_get_section_by_name (abfd, ".dynstr");
7919           if (sec != NULL)
7920             (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
7921           break;
7922
7923         case SHT_MIPS_GPTAB:
7924           BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
7925           name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
7926           BFD_ASSERT (name != NULL
7927                       && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
7928           sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
7929           BFD_ASSERT (sec != NULL);
7930           (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
7931           break;
7932
7933         case SHT_MIPS_CONTENT:
7934           BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
7935           name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
7936           BFD_ASSERT (name != NULL
7937                       && strncmp (name, ".MIPS.content",
7938                                   sizeof ".MIPS.content" - 1) == 0);
7939           sec = bfd_get_section_by_name (abfd,
7940                                          name + sizeof ".MIPS.content" - 1);
7941           BFD_ASSERT (sec != NULL);
7942           (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
7943           break;
7944
7945         case SHT_MIPS_SYMBOL_LIB:
7946           sec = bfd_get_section_by_name (abfd, ".dynsym");
7947           if (sec != NULL)
7948             (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
7949           sec = bfd_get_section_by_name (abfd, ".liblist");
7950           if (sec != NULL)
7951             (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
7952           break;
7953
7954         case SHT_MIPS_EVENTS:
7955           BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
7956           name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
7957           BFD_ASSERT (name != NULL);
7958           if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
7959             sec = bfd_get_section_by_name (abfd,
7960                                            name + sizeof ".MIPS.events" - 1);
7961           else
7962             {
7963               BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
7964                                    sizeof ".MIPS.post_rel" - 1) == 0);
7965               sec = bfd_get_section_by_name (abfd,
7966                                              (name
7967                                               + sizeof ".MIPS.post_rel" - 1));
7968             }
7969           BFD_ASSERT (sec != NULL);
7970           (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
7971           break;
7972
7973         }
7974     }
7975 }
7976 \f
7977 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
7978    segments.  */
7979
7980 int
7981 _bfd_mips_elf_additional_program_headers (bfd *abfd)
7982 {
7983   asection *s;
7984   int ret = 0;
7985
7986   /* See if we need a PT_MIPS_REGINFO segment.  */
7987   s = bfd_get_section_by_name (abfd, ".reginfo");
7988   if (s && (s->flags & SEC_LOAD))
7989     ++ret;
7990
7991   /* See if we need a PT_MIPS_OPTIONS segment.  */
7992   if (IRIX_COMPAT (abfd) == ict_irix6
7993       && bfd_get_section_by_name (abfd,
7994                                   MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
7995     ++ret;
7996
7997   /* See if we need a PT_MIPS_RTPROC segment.  */
7998   if (IRIX_COMPAT (abfd) == ict_irix5
7999       && bfd_get_section_by_name (abfd, ".dynamic")
8000       && bfd_get_section_by_name (abfd, ".mdebug"))
8001     ++ret;
8002
8003   return ret;
8004 }
8005
8006 /* Modify the segment map for an IRIX5 executable.  */
8007
8008 bfd_boolean
8009 _bfd_mips_elf_modify_segment_map (bfd *abfd,
8010                                   struct bfd_link_info *info ATTRIBUTE_UNUSED)
8011 {
8012   asection *s;
8013   struct elf_segment_map *m, **pm;
8014   bfd_size_type amt;
8015
8016   /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
8017      segment.  */
8018   s = bfd_get_section_by_name (abfd, ".reginfo");
8019   if (s != NULL && (s->flags & SEC_LOAD) != 0)
8020     {
8021       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
8022         if (m->p_type == PT_MIPS_REGINFO)
8023           break;
8024       if (m == NULL)
8025         {
8026           amt = sizeof *m;
8027           m = bfd_zalloc (abfd, amt);
8028           if (m == NULL)
8029             return FALSE;
8030
8031           m->p_type = PT_MIPS_REGINFO;
8032           m->count = 1;
8033           m->sections[0] = s;
8034
8035           /* We want to put it after the PHDR and INTERP segments.  */
8036           pm = &elf_tdata (abfd)->segment_map;
8037           while (*pm != NULL
8038                  && ((*pm)->p_type == PT_PHDR
8039                      || (*pm)->p_type == PT_INTERP))
8040             pm = &(*pm)->next;
8041
8042           m->next = *pm;
8043           *pm = m;
8044         }
8045     }
8046
8047   /* For IRIX 6, we don't have .mdebug sections, nor does anything but
8048      .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
8049      PT_MIPS_OPTIONS segment immediately following the program header
8050      table.  */
8051   if (NEWABI_P (abfd)
8052       /* On non-IRIX6 new abi, we'll have already created a segment
8053          for this section, so don't create another.  I'm not sure this
8054          is not also the case for IRIX 6, but I can't test it right
8055          now.  */
8056       && IRIX_COMPAT (abfd) == ict_irix6)
8057     {
8058       for (s = abfd->sections; s; s = s->next)
8059         if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
8060           break;
8061
8062       if (s)
8063         {
8064           struct elf_segment_map *options_segment;
8065
8066           pm = &elf_tdata (abfd)->segment_map;
8067           while (*pm != NULL
8068                  && ((*pm)->p_type == PT_PHDR
8069                      || (*pm)->p_type == PT_INTERP))
8070             pm = &(*pm)->next;
8071
8072           amt = sizeof (struct elf_segment_map);
8073           options_segment = bfd_zalloc (abfd, amt);
8074           options_segment->next = *pm;
8075           options_segment->p_type = PT_MIPS_OPTIONS;
8076           options_segment->p_flags = PF_R;
8077           options_segment->p_flags_valid = TRUE;
8078           options_segment->count = 1;
8079           options_segment->sections[0] = s;
8080           *pm = options_segment;
8081         }
8082     }
8083   else
8084     {
8085       if (IRIX_COMPAT (abfd) == ict_irix5)
8086         {
8087           /* If there are .dynamic and .mdebug sections, we make a room
8088              for the RTPROC header.  FIXME: Rewrite without section names.  */
8089           if (bfd_get_section_by_name (abfd, ".interp") == NULL
8090               && bfd_get_section_by_name (abfd, ".dynamic") != NULL
8091               && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
8092             {
8093               for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
8094                 if (m->p_type == PT_MIPS_RTPROC)
8095                   break;
8096               if (m == NULL)
8097                 {
8098                   amt = sizeof *m;
8099                   m = bfd_zalloc (abfd, amt);
8100                   if (m == NULL)
8101                     return FALSE;
8102
8103                   m->p_type = PT_MIPS_RTPROC;
8104
8105                   s = bfd_get_section_by_name (abfd, ".rtproc");
8106                   if (s == NULL)
8107                     {
8108                       m->count = 0;
8109                       m->p_flags = 0;
8110                       m->p_flags_valid = 1;
8111                     }
8112                   else
8113                     {
8114                       m->count = 1;
8115                       m->sections[0] = s;
8116                     }
8117
8118                   /* We want to put it after the DYNAMIC segment.  */
8119                   pm = &elf_tdata (abfd)->segment_map;
8120                   while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
8121                     pm = &(*pm)->next;
8122                   if (*pm != NULL)
8123                     pm = &(*pm)->next;
8124
8125                   m->next = *pm;
8126                   *pm = m;
8127                 }
8128             }
8129         }
8130       /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
8131          .dynstr, .dynsym, and .hash sections, and everything in
8132          between.  */
8133       for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
8134            pm = &(*pm)->next)
8135         if ((*pm)->p_type == PT_DYNAMIC)
8136           break;
8137       m = *pm;
8138       if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
8139         {
8140           /* For a normal mips executable the permissions for the PT_DYNAMIC
8141              segment are read, write and execute. We do that here since
8142              the code in elf.c sets only the read permission. This matters
8143              sometimes for the dynamic linker.  */
8144           if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
8145             {
8146               m->p_flags = PF_R | PF_W | PF_X;
8147               m->p_flags_valid = 1;
8148             }
8149         }
8150       if (m != NULL
8151           && m->count == 1 && strcmp (m->sections[0]->name, ".dynamic") == 0)
8152         {
8153           static const char *sec_names[] =
8154           {
8155             ".dynamic", ".dynstr", ".dynsym", ".hash"
8156           };
8157           bfd_vma low, high;
8158           unsigned int i, c;
8159           struct elf_segment_map *n;
8160
8161           low = ~(bfd_vma) 0;
8162           high = 0;
8163           for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
8164             {
8165               s = bfd_get_section_by_name (abfd, sec_names[i]);
8166               if (s != NULL && (s->flags & SEC_LOAD) != 0)
8167                 {
8168                   bfd_size_type sz;
8169
8170                   if (low > s->vma)
8171                     low = s->vma;
8172                   sz = s->size;
8173                   if (high < s->vma + sz)
8174                     high = s->vma + sz;
8175                 }
8176             }
8177
8178           c = 0;
8179           for (s = abfd->sections; s != NULL; s = s->next)
8180             if ((s->flags & SEC_LOAD) != 0
8181                 && s->vma >= low
8182                 && s->vma + s->size <= high)
8183               ++c;
8184
8185           amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
8186           n = bfd_zalloc (abfd, amt);
8187           if (n == NULL)
8188             return FALSE;
8189           *n = *m;
8190           n->count = c;
8191
8192           i = 0;
8193           for (s = abfd->sections; s != NULL; s = s->next)
8194             {
8195               if ((s->flags & SEC_LOAD) != 0
8196                   && s->vma >= low
8197                   && s->vma + s->size <= high)
8198                 {
8199                   n->sections[i] = s;
8200                   ++i;
8201                 }
8202             }
8203
8204           *pm = n;
8205         }
8206     }
8207
8208   return TRUE;
8209 }
8210 \f
8211 /* Return the section that should be marked against GC for a given
8212    relocation.  */
8213
8214 asection *
8215 _bfd_mips_elf_gc_mark_hook (asection *sec,
8216                             struct bfd_link_info *info ATTRIBUTE_UNUSED,
8217                             Elf_Internal_Rela *rel,
8218                             struct elf_link_hash_entry *h,
8219                             Elf_Internal_Sym *sym)
8220 {
8221   /* ??? Do mips16 stub sections need to be handled special?  */
8222
8223   if (h != NULL)
8224     {
8225       switch (ELF_R_TYPE (sec->owner, rel->r_info))
8226         {
8227         case R_MIPS_GNU_VTINHERIT:
8228         case R_MIPS_GNU_VTENTRY:
8229           break;
8230
8231         default:
8232           switch (h->root.type)
8233             {
8234             case bfd_link_hash_defined:
8235             case bfd_link_hash_defweak:
8236               return h->root.u.def.section;
8237
8238             case bfd_link_hash_common:
8239               return h->root.u.c.p->section;
8240
8241             default:
8242               break;
8243             }
8244         }
8245     }
8246   else
8247     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
8248
8249   return NULL;
8250 }
8251
8252 /* Update the got entry reference counts for the section being removed.  */
8253
8254 bfd_boolean
8255 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
8256                              struct bfd_link_info *info ATTRIBUTE_UNUSED,
8257                              asection *sec ATTRIBUTE_UNUSED,
8258                              const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
8259 {
8260 #if 0
8261   Elf_Internal_Shdr *symtab_hdr;
8262   struct elf_link_hash_entry **sym_hashes;
8263   bfd_signed_vma *local_got_refcounts;
8264   const Elf_Internal_Rela *rel, *relend;
8265   unsigned long r_symndx;
8266   struct elf_link_hash_entry *h;
8267
8268   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8269   sym_hashes = elf_sym_hashes (abfd);
8270   local_got_refcounts = elf_local_got_refcounts (abfd);
8271
8272   relend = relocs + sec->reloc_count;
8273   for (rel = relocs; rel < relend; rel++)
8274     switch (ELF_R_TYPE (abfd, rel->r_info))
8275       {
8276       case R_MIPS_GOT16:
8277       case R_MIPS_CALL16:
8278       case R_MIPS_CALL_HI16:
8279       case R_MIPS_CALL_LO16:
8280       case R_MIPS_GOT_HI16:
8281       case R_MIPS_GOT_LO16:
8282       case R_MIPS_GOT_DISP:
8283       case R_MIPS_GOT_PAGE:
8284       case R_MIPS_GOT_OFST:
8285         /* ??? It would seem that the existing MIPS code does no sort
8286            of reference counting or whatnot on its GOT and PLT entries,
8287            so it is not possible to garbage collect them at this time.  */
8288         break;
8289
8290       default:
8291         break;
8292       }
8293 #endif
8294
8295   return TRUE;
8296 }
8297 \f
8298 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
8299    hiding the old indirect symbol.  Process additional relocation
8300    information.  Also called for weakdefs, in which case we just let
8301    _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
8302
8303 void
8304 _bfd_mips_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
8305                                     struct elf_link_hash_entry *dir,
8306                                     struct elf_link_hash_entry *ind)
8307 {
8308   struct mips_elf_link_hash_entry *dirmips, *indmips;
8309
8310   _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
8311
8312   if (ind->root.type != bfd_link_hash_indirect)
8313     return;
8314
8315   dirmips = (struct mips_elf_link_hash_entry *) dir;
8316   indmips = (struct mips_elf_link_hash_entry *) ind;
8317   dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
8318   if (indmips->readonly_reloc)
8319     dirmips->readonly_reloc = TRUE;
8320   if (indmips->no_fn_stub)
8321     dirmips->no_fn_stub = TRUE;
8322
8323   if (dirmips->tls_type == 0)
8324     dirmips->tls_type = indmips->tls_type;
8325   else
8326     BFD_ASSERT (indmips->tls_type == 0);
8327 }
8328
8329 void
8330 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
8331                            struct elf_link_hash_entry *entry,
8332                            bfd_boolean force_local)
8333 {
8334   bfd *dynobj;
8335   asection *got;
8336   struct mips_got_info *g;
8337   struct mips_elf_link_hash_entry *h;
8338
8339   h = (struct mips_elf_link_hash_entry *) entry;
8340   if (h->forced_local)
8341     return;
8342   h->forced_local = force_local;
8343
8344   dynobj = elf_hash_table (info)->dynobj;
8345   if (dynobj != NULL && force_local && h->root.type != STT_TLS)
8346     {
8347       got = mips_elf_got_section (dynobj, FALSE);
8348       g = mips_elf_section_data (got)->u.got_info;
8349
8350       if (g->next)
8351         {
8352           struct mips_got_entry e;
8353           struct mips_got_info *gg = g;
8354
8355           /* Since we're turning what used to be a global symbol into a
8356              local one, bump up the number of local entries of each GOT
8357              that had an entry for it.  This will automatically decrease
8358              the number of global entries, since global_gotno is actually
8359              the upper limit of global entries.  */
8360           e.abfd = dynobj;
8361           e.symndx = -1;
8362           e.d.h = h;
8363           e.tls_type = 0;
8364
8365           for (g = g->next; g != gg; g = g->next)
8366             if (htab_find (g->got_entries, &e))
8367               {
8368                 BFD_ASSERT (g->global_gotno > 0);
8369                 g->local_gotno++;
8370                 g->global_gotno--;
8371               }
8372
8373           /* If this was a global symbol forced into the primary GOT, we
8374              no longer need an entry for it.  We can't release the entry
8375              at this point, but we must at least stop counting it as one
8376              of the symbols that required a forced got entry.  */
8377           if (h->root.got.offset == 2)
8378             {
8379               BFD_ASSERT (gg->assigned_gotno > 0);
8380               gg->assigned_gotno--;
8381             }
8382         }
8383       else if (g->global_gotno == 0 && g->global_gotsym == NULL)
8384         /* If we haven't got through GOT allocation yet, just bump up the
8385            number of local entries, as this symbol won't be counted as
8386            global.  */
8387         g->local_gotno++;
8388       else if (h->root.got.offset == 1)
8389         {
8390           /* If we're past non-multi-GOT allocation and this symbol had
8391              been marked for a global got entry, give it a local entry
8392              instead.  */
8393           BFD_ASSERT (g->global_gotno > 0);
8394           g->local_gotno++;
8395           g->global_gotno--;
8396         }
8397     }
8398
8399   _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
8400 }
8401 \f
8402 #define PDR_SIZE 32
8403
8404 bfd_boolean
8405 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
8406                             struct bfd_link_info *info)
8407 {
8408   asection *o;
8409   bfd_boolean ret = FALSE;
8410   unsigned char *tdata;
8411   size_t i, skip;
8412
8413   o = bfd_get_section_by_name (abfd, ".pdr");
8414   if (! o)
8415     return FALSE;
8416   if (o->size == 0)
8417     return FALSE;
8418   if (o->size % PDR_SIZE != 0)
8419     return FALSE;
8420   if (o->output_section != NULL
8421       && bfd_is_abs_section (o->output_section))
8422     return FALSE;
8423
8424   tdata = bfd_zmalloc (o->size / PDR_SIZE);
8425   if (! tdata)
8426     return FALSE;
8427
8428   cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8429                                             info->keep_memory);
8430   if (!cookie->rels)
8431     {
8432       free (tdata);
8433       return FALSE;
8434     }
8435
8436   cookie->rel = cookie->rels;
8437   cookie->relend = cookie->rels + o->reloc_count;
8438
8439   for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
8440     {
8441       if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
8442         {
8443           tdata[i] = 1;
8444           skip ++;
8445         }
8446     }
8447
8448   if (skip != 0)
8449     {
8450       mips_elf_section_data (o)->u.tdata = tdata;
8451       o->size -= skip * PDR_SIZE;
8452       ret = TRUE;
8453     }
8454   else
8455     free (tdata);
8456
8457   if (! info->keep_memory)
8458     free (cookie->rels);
8459
8460   return ret;
8461 }
8462
8463 bfd_boolean
8464 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
8465 {
8466   if (strcmp (sec->name, ".pdr") == 0)
8467     return TRUE;
8468   return FALSE;
8469 }
8470
8471 bfd_boolean
8472 _bfd_mips_elf_write_section (bfd *output_bfd, asection *sec,
8473                              bfd_byte *contents)
8474 {
8475   bfd_byte *to, *from, *end;
8476   int i;
8477
8478   if (strcmp (sec->name, ".pdr") != 0)
8479     return FALSE;
8480
8481   if (mips_elf_section_data (sec)->u.tdata == NULL)
8482     return FALSE;
8483
8484   to = contents;
8485   end = contents + sec->size;
8486   for (from = contents, i = 0;
8487        from < end;
8488        from += PDR_SIZE, i++)
8489     {
8490       if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
8491         continue;
8492       if (to != from)
8493         memcpy (to, from, PDR_SIZE);
8494       to += PDR_SIZE;
8495     }
8496   bfd_set_section_contents (output_bfd, sec->output_section, contents,
8497                             sec->output_offset, sec->size);
8498   return TRUE;
8499 }
8500 \f
8501 /* MIPS ELF uses a special find_nearest_line routine in order the
8502    handle the ECOFF debugging information.  */
8503
8504 struct mips_elf_find_line
8505 {
8506   struct ecoff_debug_info d;
8507   struct ecoff_find_line i;
8508 };
8509
8510 bfd_boolean
8511 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
8512                                  asymbol **symbols, bfd_vma offset,
8513                                  const char **filename_ptr,
8514                                  const char **functionname_ptr,
8515                                  unsigned int *line_ptr)
8516 {
8517   asection *msec;
8518
8519   if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
8520                                      filename_ptr, functionname_ptr,
8521                                      line_ptr))
8522     return TRUE;
8523
8524   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
8525                                      filename_ptr, functionname_ptr,
8526                                      line_ptr, ABI_64_P (abfd) ? 8 : 0,
8527                                      &elf_tdata (abfd)->dwarf2_find_line_info))
8528     return TRUE;
8529
8530   msec = bfd_get_section_by_name (abfd, ".mdebug");
8531   if (msec != NULL)
8532     {
8533       flagword origflags;
8534       struct mips_elf_find_line *fi;
8535       const struct ecoff_debug_swap * const swap =
8536         get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
8537
8538       /* If we are called during a link, mips_elf_final_link may have
8539          cleared the SEC_HAS_CONTENTS field.  We force it back on here
8540          if appropriate (which it normally will be).  */
8541       origflags = msec->flags;
8542       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
8543         msec->flags |= SEC_HAS_CONTENTS;
8544
8545       fi = elf_tdata (abfd)->find_line_info;
8546       if (fi == NULL)
8547         {
8548           bfd_size_type external_fdr_size;
8549           char *fraw_src;
8550           char *fraw_end;
8551           struct fdr *fdr_ptr;
8552           bfd_size_type amt = sizeof (struct mips_elf_find_line);
8553
8554           fi = bfd_zalloc (abfd, amt);
8555           if (fi == NULL)
8556             {
8557               msec->flags = origflags;
8558               return FALSE;
8559             }
8560
8561           if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
8562             {
8563               msec->flags = origflags;
8564               return FALSE;
8565             }
8566
8567           /* Swap in the FDR information.  */
8568           amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
8569           fi->d.fdr = bfd_alloc (abfd, amt);
8570           if (fi->d.fdr == NULL)
8571             {
8572               msec->flags = origflags;
8573               return FALSE;
8574             }
8575           external_fdr_size = swap->external_fdr_size;
8576           fdr_ptr = fi->d.fdr;
8577           fraw_src = (char *) fi->d.external_fdr;
8578           fraw_end = (fraw_src
8579                       + fi->d.symbolic_header.ifdMax * external_fdr_size);
8580           for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
8581             (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
8582
8583           elf_tdata (abfd)->find_line_info = fi;
8584
8585           /* Note that we don't bother to ever free this information.
8586              find_nearest_line is either called all the time, as in
8587              objdump -l, so the information should be saved, or it is
8588              rarely called, as in ld error messages, so the memory
8589              wasted is unimportant.  Still, it would probably be a
8590              good idea for free_cached_info to throw it away.  */
8591         }
8592
8593       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
8594                                   &fi->i, filename_ptr, functionname_ptr,
8595                                   line_ptr))
8596         {
8597           msec->flags = origflags;
8598           return TRUE;
8599         }
8600
8601       msec->flags = origflags;
8602     }
8603
8604   /* Fall back on the generic ELF find_nearest_line routine.  */
8605
8606   return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
8607                                      filename_ptr, functionname_ptr,
8608                                      line_ptr);
8609 }
8610 \f
8611 /* When are writing out the .options or .MIPS.options section,
8612    remember the bytes we are writing out, so that we can install the
8613    GP value in the section_processing routine.  */
8614
8615 bfd_boolean
8616 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
8617                                     const void *location,
8618                                     file_ptr offset, bfd_size_type count)
8619 {
8620   if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
8621     {
8622       bfd_byte *c;
8623
8624       if (elf_section_data (section) == NULL)
8625         {
8626           bfd_size_type amt = sizeof (struct bfd_elf_section_data);
8627           section->used_by_bfd = bfd_zalloc (abfd, amt);
8628           if (elf_section_data (section) == NULL)
8629             return FALSE;
8630         }
8631       c = mips_elf_section_data (section)->u.tdata;
8632       if (c == NULL)
8633         {
8634           c = bfd_zalloc (abfd, section->size);
8635           if (c == NULL)
8636             return FALSE;
8637           mips_elf_section_data (section)->u.tdata = c;
8638         }
8639
8640       memcpy (c + offset, location, count);
8641     }
8642
8643   return _bfd_elf_set_section_contents (abfd, section, location, offset,
8644                                         count);
8645 }
8646
8647 /* This is almost identical to bfd_generic_get_... except that some
8648    MIPS relocations need to be handled specially.  Sigh.  */
8649
8650 bfd_byte *
8651 _bfd_elf_mips_get_relocated_section_contents
8652   (bfd *abfd,
8653    struct bfd_link_info *link_info,
8654    struct bfd_link_order *link_order,
8655    bfd_byte *data,
8656    bfd_boolean relocatable,
8657    asymbol **symbols)
8658 {
8659   /* Get enough memory to hold the stuff */
8660   bfd *input_bfd = link_order->u.indirect.section->owner;
8661   asection *input_section = link_order->u.indirect.section;
8662   bfd_size_type sz;
8663
8664   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
8665   arelent **reloc_vector = NULL;
8666   long reloc_count;
8667
8668   if (reloc_size < 0)
8669     goto error_return;
8670
8671   reloc_vector = bfd_malloc (reloc_size);
8672   if (reloc_vector == NULL && reloc_size != 0)
8673     goto error_return;
8674
8675   /* read in the section */
8676   sz = input_section->rawsize ? input_section->rawsize : input_section->size;
8677   if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
8678     goto error_return;
8679
8680   reloc_count = bfd_canonicalize_reloc (input_bfd,
8681                                         input_section,
8682                                         reloc_vector,
8683                                         symbols);
8684   if (reloc_count < 0)
8685     goto error_return;
8686
8687   if (reloc_count > 0)
8688     {
8689       arelent **parent;
8690       /* for mips */
8691       int gp_found;
8692       bfd_vma gp = 0x12345678;  /* initialize just to shut gcc up */
8693
8694       {
8695         struct bfd_hash_entry *h;
8696         struct bfd_link_hash_entry *lh;
8697         /* Skip all this stuff if we aren't mixing formats.  */
8698         if (abfd && input_bfd
8699             && abfd->xvec == input_bfd->xvec)
8700           lh = 0;
8701         else
8702           {
8703             h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
8704             lh = (struct bfd_link_hash_entry *) h;
8705           }
8706       lookup:
8707         if (lh)
8708           {
8709             switch (lh->type)
8710               {
8711               case bfd_link_hash_undefined:
8712               case bfd_link_hash_undefweak:
8713               case bfd_link_hash_common:
8714                 gp_found = 0;
8715                 break;
8716               case bfd_link_hash_defined:
8717               case bfd_link_hash_defweak:
8718                 gp_found = 1;
8719                 gp = lh->u.def.value;
8720                 break;
8721               case bfd_link_hash_indirect:
8722               case bfd_link_hash_warning:
8723                 lh = lh->u.i.link;
8724                 /* @@FIXME  ignoring warning for now */
8725                 goto lookup;
8726               case bfd_link_hash_new:
8727               default:
8728                 abort ();
8729               }
8730           }
8731         else
8732           gp_found = 0;
8733       }
8734       /* end mips */
8735       for (parent = reloc_vector; *parent != NULL; parent++)
8736         {
8737           char *error_message = NULL;
8738           bfd_reloc_status_type r;
8739
8740           /* Specific to MIPS: Deal with relocation types that require
8741              knowing the gp of the output bfd.  */
8742           asymbol *sym = *(*parent)->sym_ptr_ptr;
8743           if (bfd_is_abs_section (sym->section) && abfd)
8744             {
8745               /* The special_function wouldn't get called anyway.  */
8746             }
8747           else if (!gp_found)
8748             {
8749               /* The gp isn't there; let the special function code
8750                  fall over on its own.  */
8751             }
8752           else if ((*parent)->howto->special_function
8753                    == _bfd_mips_elf32_gprel16_reloc)
8754             {
8755               /* bypass special_function call */
8756               r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
8757                                                  input_section, relocatable,
8758                                                  data, gp);
8759               goto skip_bfd_perform_relocation;
8760             }
8761           /* end mips specific stuff */
8762
8763           r = bfd_perform_relocation (input_bfd, *parent, data, input_section,
8764                                       relocatable ? abfd : NULL,
8765                                       &error_message);
8766         skip_bfd_perform_relocation:
8767
8768           if (relocatable)
8769             {
8770               asection *os = input_section->output_section;
8771
8772               /* A partial link, so keep the relocs */
8773               os->orelocation[os->reloc_count] = *parent;
8774               os->reloc_count++;
8775             }
8776
8777           if (r != bfd_reloc_ok)
8778             {
8779               switch (r)
8780                 {
8781                 case bfd_reloc_undefined:
8782                   if (!((*link_info->callbacks->undefined_symbol)
8783                         (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8784                          input_bfd, input_section, (*parent)->address,
8785                          TRUE)))
8786                     goto error_return;
8787                   break;
8788                 case bfd_reloc_dangerous:
8789                   BFD_ASSERT (error_message != NULL);
8790                   if (!((*link_info->callbacks->reloc_dangerous)
8791                         (link_info, error_message, input_bfd, input_section,
8792                          (*parent)->address)))
8793                     goto error_return;
8794                   break;
8795                 case bfd_reloc_overflow:
8796                   if (!((*link_info->callbacks->reloc_overflow)
8797                         (link_info, NULL,
8798                          bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8799                          (*parent)->howto->name, (*parent)->addend,
8800                          input_bfd, input_section, (*parent)->address)))
8801                     goto error_return;
8802                   break;
8803                 case bfd_reloc_outofrange:
8804                 default:
8805                   abort ();
8806                   break;
8807                 }
8808
8809             }
8810         }
8811     }
8812   if (reloc_vector != NULL)
8813     free (reloc_vector);
8814   return data;
8815
8816 error_return:
8817   if (reloc_vector != NULL)
8818     free (reloc_vector);
8819   return NULL;
8820 }
8821 \f
8822 /* Create a MIPS ELF linker hash table.  */
8823
8824 struct bfd_link_hash_table *
8825 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
8826 {
8827   struct mips_elf_link_hash_table *ret;
8828   bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
8829
8830   ret = bfd_malloc (amt);
8831   if (ret == NULL)
8832     return NULL;
8833
8834   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
8835                                        mips_elf_link_hash_newfunc))
8836     {
8837       free (ret);
8838       return NULL;
8839     }
8840
8841 #if 0
8842   /* We no longer use this.  */
8843   for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
8844     ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
8845 #endif
8846   ret->procedure_count = 0;
8847   ret->compact_rel_size = 0;
8848   ret->use_rld_obj_head = FALSE;
8849   ret->rld_value = 0;
8850   ret->mips16_stubs_seen = FALSE;
8851
8852   return &ret->root.root;
8853 }
8854 \f
8855 /* We need to use a special link routine to handle the .reginfo and
8856    the .mdebug sections.  We need to merge all instances of these
8857    sections together, not write them all out sequentially.  */
8858
8859 bfd_boolean
8860 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
8861 {
8862   asection **secpp;
8863   asection *o;
8864   struct bfd_link_order *p;
8865   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
8866   asection *rtproc_sec;
8867   Elf32_RegInfo reginfo;
8868   struct ecoff_debug_info debug;
8869   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8870   const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
8871   HDRR *symhdr = &debug.symbolic_header;
8872   void *mdebug_handle = NULL;
8873   asection *s;
8874   EXTR esym;
8875   unsigned int i;
8876   bfd_size_type amt;
8877
8878   static const char * const secname[] =
8879   {
8880     ".text", ".init", ".fini", ".data",
8881     ".rodata", ".sdata", ".sbss", ".bss"
8882   };
8883   static const int sc[] =
8884   {
8885     scText, scInit, scFini, scData,
8886     scRData, scSData, scSBss, scBss
8887   };
8888
8889   /* We'd carefully arranged the dynamic symbol indices, and then the
8890      generic size_dynamic_sections renumbered them out from under us.
8891      Rather than trying somehow to prevent the renumbering, just do
8892      the sort again.  */
8893   if (elf_hash_table (info)->dynamic_sections_created)
8894     {
8895       bfd *dynobj;
8896       asection *got;
8897       struct mips_got_info *g;
8898       bfd_size_type dynsecsymcount;
8899
8900       /* When we resort, we must tell mips_elf_sort_hash_table what
8901          the lowest index it may use is.  That's the number of section
8902          symbols we're going to add.  The generic ELF linker only
8903          adds these symbols when building a shared object.  Note that
8904          we count the sections after (possibly) removing the .options
8905          section above.  */
8906
8907       dynsecsymcount = 0;
8908       if (info->shared)
8909         {
8910           asection * p;
8911
8912           for (p = abfd->sections; p ; p = p->next)
8913             if ((p->flags & SEC_EXCLUDE) == 0
8914                 && (p->flags & SEC_ALLOC) != 0
8915                 && !(*bed->elf_backend_omit_section_dynsym) (abfd, info, p))
8916               ++ dynsecsymcount;
8917         }
8918       
8919       if (! mips_elf_sort_hash_table (info, dynsecsymcount + 1))
8920         return FALSE;
8921
8922       /* Make sure we didn't grow the global .got region.  */
8923       dynobj = elf_hash_table (info)->dynobj;
8924       got = mips_elf_got_section (dynobj, FALSE);
8925       g = mips_elf_section_data (got)->u.got_info;
8926
8927       if (g->global_gotsym != NULL)
8928         BFD_ASSERT ((elf_hash_table (info)->dynsymcount
8929                      - g->global_gotsym->dynindx)
8930                     <= g->global_gotno);
8931     }
8932
8933   /* Get a value for the GP register.  */
8934   if (elf_gp (abfd) == 0)
8935     {
8936       struct bfd_link_hash_entry *h;
8937
8938       h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
8939       if (h != NULL && h->type == bfd_link_hash_defined)
8940         elf_gp (abfd) = (h->u.def.value
8941                          + h->u.def.section->output_section->vma
8942                          + h->u.def.section->output_offset);
8943       else if (info->relocatable)
8944         {
8945           bfd_vma lo = MINUS_ONE;
8946
8947           /* Find the GP-relative section with the lowest offset.  */
8948           for (o = abfd->sections; o != NULL; o = o->next)
8949             if (o->vma < lo
8950                 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
8951               lo = o->vma;
8952
8953           /* And calculate GP relative to that.  */
8954           elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
8955         }
8956       else
8957         {
8958           /* If the relocate_section function needs to do a reloc
8959              involving the GP value, it should make a reloc_dangerous
8960              callback to warn that GP is not defined.  */
8961         }
8962     }
8963
8964   /* Go through the sections and collect the .reginfo and .mdebug
8965      information.  */
8966   reginfo_sec = NULL;
8967   mdebug_sec = NULL;
8968   gptab_data_sec = NULL;
8969   gptab_bss_sec = NULL;
8970   for (o = abfd->sections; o != NULL; o = o->next)
8971     {
8972       if (strcmp (o->name, ".reginfo") == 0)
8973         {
8974           memset (&reginfo, 0, sizeof reginfo);
8975
8976           /* We have found the .reginfo section in the output file.
8977              Look through all the link_orders comprising it and merge
8978              the information together.  */
8979           for (p = o->link_order_head; p != NULL; p = p->next)
8980             {
8981               asection *input_section;
8982               bfd *input_bfd;
8983               Elf32_External_RegInfo ext;
8984               Elf32_RegInfo sub;
8985
8986               if (p->type != bfd_indirect_link_order)
8987                 {
8988                   if (p->type == bfd_data_link_order)
8989                     continue;
8990                   abort ();
8991                 }
8992
8993               input_section = p->u.indirect.section;
8994               input_bfd = input_section->owner;
8995
8996               if (! bfd_get_section_contents (input_bfd, input_section,
8997                                               &ext, 0, sizeof ext))
8998                 return FALSE;
8999
9000               bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
9001
9002               reginfo.ri_gprmask |= sub.ri_gprmask;
9003               reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
9004               reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
9005               reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
9006               reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
9007
9008               /* ri_gp_value is set by the function
9009                  mips_elf32_section_processing when the section is
9010                  finally written out.  */
9011
9012               /* Hack: reset the SEC_HAS_CONTENTS flag so that
9013                  elf_link_input_bfd ignores this section.  */
9014               input_section->flags &= ~SEC_HAS_CONTENTS;
9015             }
9016
9017           /* Size has been set in _bfd_mips_elf_always_size_sections.  */
9018           BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
9019
9020           /* Skip this section later on (I don't think this currently
9021              matters, but someday it might).  */
9022           o->link_order_head = NULL;
9023
9024           reginfo_sec = o;
9025         }
9026
9027       if (strcmp (o->name, ".mdebug") == 0)
9028         {
9029           struct extsym_info einfo;
9030           bfd_vma last;
9031
9032           /* We have found the .mdebug section in the output file.
9033              Look through all the link_orders comprising it and merge
9034              the information together.  */
9035           symhdr->magic = swap->sym_magic;
9036           /* FIXME: What should the version stamp be?  */
9037           symhdr->vstamp = 0;
9038           symhdr->ilineMax = 0;
9039           symhdr->cbLine = 0;
9040           symhdr->idnMax = 0;
9041           symhdr->ipdMax = 0;
9042           symhdr->isymMax = 0;
9043           symhdr->ioptMax = 0;
9044           symhdr->iauxMax = 0;
9045           symhdr->issMax = 0;
9046           symhdr->issExtMax = 0;
9047           symhdr->ifdMax = 0;
9048           symhdr->crfd = 0;
9049           symhdr->iextMax = 0;
9050
9051           /* We accumulate the debugging information itself in the
9052              debug_info structure.  */
9053           debug.line = NULL;
9054           debug.external_dnr = NULL;
9055           debug.external_pdr = NULL;
9056           debug.external_sym = NULL;
9057           debug.external_opt = NULL;
9058           debug.external_aux = NULL;
9059           debug.ss = NULL;
9060           debug.ssext = debug.ssext_end = NULL;
9061           debug.external_fdr = NULL;
9062           debug.external_rfd = NULL;
9063           debug.external_ext = debug.external_ext_end = NULL;
9064
9065           mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9066           if (mdebug_handle == NULL)
9067             return FALSE;
9068
9069           esym.jmptbl = 0;
9070           esym.cobol_main = 0;
9071           esym.weakext = 0;
9072           esym.reserved = 0;
9073           esym.ifd = ifdNil;
9074           esym.asym.iss = issNil;
9075           esym.asym.st = stLocal;
9076           esym.asym.reserved = 0;
9077           esym.asym.index = indexNil;
9078           last = 0;
9079           for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
9080             {
9081               esym.asym.sc = sc[i];
9082               s = bfd_get_section_by_name (abfd, secname[i]);
9083               if (s != NULL)
9084                 {
9085                   esym.asym.value = s->vma;
9086                   last = s->vma + s->size;
9087                 }
9088               else
9089                 esym.asym.value = last;
9090               if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
9091                                                  secname[i], &esym))
9092                 return FALSE;
9093             }
9094
9095           for (p = o->link_order_head; p != NULL; p = p->next)
9096             {
9097               asection *input_section;
9098               bfd *input_bfd;
9099               const struct ecoff_debug_swap *input_swap;
9100               struct ecoff_debug_info input_debug;
9101               char *eraw_src;
9102               char *eraw_end;
9103
9104               if (p->type != bfd_indirect_link_order)
9105                 {
9106                   if (p->type == bfd_data_link_order)
9107                     continue;
9108                   abort ();
9109                 }
9110
9111               input_section = p->u.indirect.section;
9112               input_bfd = input_section->owner;
9113
9114               if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
9115                   || (get_elf_backend_data (input_bfd)
9116                       ->elf_backend_ecoff_debug_swap) == NULL)
9117                 {
9118                   /* I don't know what a non MIPS ELF bfd would be
9119                      doing with a .mdebug section, but I don't really
9120                      want to deal with it.  */
9121                   continue;
9122                 }
9123
9124               input_swap = (get_elf_backend_data (input_bfd)
9125                             ->elf_backend_ecoff_debug_swap);
9126
9127               BFD_ASSERT (p->size == input_section->size);
9128
9129               /* The ECOFF linking code expects that we have already
9130                  read in the debugging information and set up an
9131                  ecoff_debug_info structure, so we do that now.  */
9132               if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
9133                                                    &input_debug))
9134                 return FALSE;
9135
9136               if (! (bfd_ecoff_debug_accumulate
9137                      (mdebug_handle, abfd, &debug, swap, input_bfd,
9138                       &input_debug, input_swap, info)))
9139                 return FALSE;
9140
9141               /* Loop through the external symbols.  For each one with
9142                  interesting information, try to find the symbol in
9143                  the linker global hash table and save the information
9144                  for the output external symbols.  */
9145               eraw_src = input_debug.external_ext;
9146               eraw_end = (eraw_src
9147                           + (input_debug.symbolic_header.iextMax
9148                              * input_swap->external_ext_size));
9149               for (;
9150                    eraw_src < eraw_end;
9151                    eraw_src += input_swap->external_ext_size)
9152                 {
9153                   EXTR ext;
9154                   const char *name;
9155                   struct mips_elf_link_hash_entry *h;
9156
9157                   (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
9158                   if (ext.asym.sc == scNil
9159                       || ext.asym.sc == scUndefined
9160                       || ext.asym.sc == scSUndefined)
9161                     continue;
9162
9163                   name = input_debug.ssext + ext.asym.iss;
9164                   h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
9165                                                  name, FALSE, FALSE, TRUE);
9166                   if (h == NULL || h->esym.ifd != -2)
9167                     continue;
9168
9169                   if (ext.ifd != -1)
9170                     {
9171                       BFD_ASSERT (ext.ifd
9172                                   < input_debug.symbolic_header.ifdMax);
9173                       ext.ifd = input_debug.ifdmap[ext.ifd];
9174                     }
9175
9176                   h->esym = ext;
9177                 }
9178
9179               /* Free up the information we just read.  */
9180               free (input_debug.line);
9181               free (input_debug.external_dnr);
9182               free (input_debug.external_pdr);
9183               free (input_debug.external_sym);
9184               free (input_debug.external_opt);
9185               free (input_debug.external_aux);
9186               free (input_debug.ss);
9187               free (input_debug.ssext);
9188               free (input_debug.external_fdr);
9189               free (input_debug.external_rfd);
9190               free (input_debug.external_ext);
9191
9192               /* Hack: reset the SEC_HAS_CONTENTS flag so that
9193                  elf_link_input_bfd ignores this section.  */
9194               input_section->flags &= ~SEC_HAS_CONTENTS;
9195             }
9196
9197           if (SGI_COMPAT (abfd) && info->shared)
9198             {
9199               /* Create .rtproc section.  */
9200               rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
9201               if (rtproc_sec == NULL)
9202                 {
9203                   flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
9204                                     | SEC_LINKER_CREATED | SEC_READONLY);
9205
9206                   rtproc_sec = bfd_make_section (abfd, ".rtproc");
9207                   if (rtproc_sec == NULL
9208                       || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
9209                       || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
9210                     return FALSE;
9211                 }
9212
9213               if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
9214                                                      info, rtproc_sec,
9215                                                      &debug))
9216                 return FALSE;
9217             }
9218
9219           /* Build the external symbol information.  */
9220           einfo.abfd = abfd;
9221           einfo.info = info;
9222           einfo.debug = &debug;
9223           einfo.swap = swap;
9224           einfo.failed = FALSE;
9225           mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9226                                        mips_elf_output_extsym, &einfo);
9227           if (einfo.failed)
9228             return FALSE;
9229
9230           /* Set the size of the .mdebug section.  */
9231           o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
9232
9233           /* Skip this section later on (I don't think this currently
9234              matters, but someday it might).  */
9235           o->link_order_head = NULL;
9236
9237           mdebug_sec = o;
9238         }
9239
9240       if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
9241         {
9242           const char *subname;
9243           unsigned int c;
9244           Elf32_gptab *tab;
9245           Elf32_External_gptab *ext_tab;
9246           unsigned int j;
9247
9248           /* The .gptab.sdata and .gptab.sbss sections hold
9249              information describing how the small data area would
9250              change depending upon the -G switch.  These sections
9251              not used in executables files.  */
9252           if (! info->relocatable)
9253             {
9254               for (p = o->link_order_head; p != NULL; p = p->next)
9255                 {
9256                   asection *input_section;
9257
9258                   if (p->type != bfd_indirect_link_order)
9259                     {
9260                       if (p->type == bfd_data_link_order)
9261                         continue;
9262                       abort ();
9263                     }
9264
9265                   input_section = p->u.indirect.section;
9266
9267                   /* Hack: reset the SEC_HAS_CONTENTS flag so that
9268                      elf_link_input_bfd ignores this section.  */
9269                   input_section->flags &= ~SEC_HAS_CONTENTS;
9270                 }
9271
9272               /* Skip this section later on (I don't think this
9273                  currently matters, but someday it might).  */
9274               o->link_order_head = NULL;
9275
9276               /* Really remove the section.  */
9277               for (secpp = &abfd->sections;
9278                    *secpp != o;
9279                    secpp = &(*secpp)->next)
9280                 ;
9281               bfd_section_list_remove (abfd, secpp);
9282               --abfd->section_count;
9283
9284               continue;
9285             }
9286
9287           /* There is one gptab for initialized data, and one for
9288              uninitialized data.  */
9289           if (strcmp (o->name, ".gptab.sdata") == 0)
9290             gptab_data_sec = o;
9291           else if (strcmp (o->name, ".gptab.sbss") == 0)
9292             gptab_bss_sec = o;
9293           else
9294             {
9295               (*_bfd_error_handler)
9296                 (_("%s: illegal section name `%s'"),
9297                  bfd_get_filename (abfd), o->name);
9298               bfd_set_error (bfd_error_nonrepresentable_section);
9299               return FALSE;
9300             }
9301
9302           /* The linker script always combines .gptab.data and
9303              .gptab.sdata into .gptab.sdata, and likewise for
9304              .gptab.bss and .gptab.sbss.  It is possible that there is
9305              no .sdata or .sbss section in the output file, in which
9306              case we must change the name of the output section.  */
9307           subname = o->name + sizeof ".gptab" - 1;
9308           if (bfd_get_section_by_name (abfd, subname) == NULL)
9309             {
9310               if (o == gptab_data_sec)
9311                 o->name = ".gptab.data";
9312               else
9313                 o->name = ".gptab.bss";
9314               subname = o->name + sizeof ".gptab" - 1;
9315               BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
9316             }
9317
9318           /* Set up the first entry.  */
9319           c = 1;
9320           amt = c * sizeof (Elf32_gptab);
9321           tab = bfd_malloc (amt);
9322           if (tab == NULL)
9323             return FALSE;
9324           tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
9325           tab[0].gt_header.gt_unused = 0;
9326
9327           /* Combine the input sections.  */
9328           for (p = o->link_order_head; p != NULL; p = p->next)
9329             {
9330               asection *input_section;
9331               bfd *input_bfd;
9332               bfd_size_type size;
9333               unsigned long last;
9334               bfd_size_type gpentry;
9335
9336               if (p->type != bfd_indirect_link_order)
9337                 {
9338                   if (p->type == bfd_data_link_order)
9339                     continue;
9340                   abort ();
9341                 }
9342
9343               input_section = p->u.indirect.section;
9344               input_bfd = input_section->owner;
9345
9346               /* Combine the gptab entries for this input section one
9347                  by one.  We know that the input gptab entries are
9348                  sorted by ascending -G value.  */
9349               size = input_section->size;
9350               last = 0;
9351               for (gpentry = sizeof (Elf32_External_gptab);
9352                    gpentry < size;
9353                    gpentry += sizeof (Elf32_External_gptab))
9354                 {
9355                   Elf32_External_gptab ext_gptab;
9356                   Elf32_gptab int_gptab;
9357                   unsigned long val;
9358                   unsigned long add;
9359                   bfd_boolean exact;
9360                   unsigned int look;
9361
9362                   if (! (bfd_get_section_contents
9363                          (input_bfd, input_section, &ext_gptab, gpentry,
9364                           sizeof (Elf32_External_gptab))))
9365                     {
9366                       free (tab);
9367                       return FALSE;
9368                     }
9369
9370                   bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
9371                                                 &int_gptab);
9372                   val = int_gptab.gt_entry.gt_g_value;
9373                   add = int_gptab.gt_entry.gt_bytes - last;
9374
9375                   exact = FALSE;
9376                   for (look = 1; look < c; look++)
9377                     {
9378                       if (tab[look].gt_entry.gt_g_value >= val)
9379                         tab[look].gt_entry.gt_bytes += add;
9380
9381                       if (tab[look].gt_entry.gt_g_value == val)
9382                         exact = TRUE;
9383                     }
9384
9385                   if (! exact)
9386                     {
9387                       Elf32_gptab *new_tab;
9388                       unsigned int max;
9389
9390                       /* We need a new table entry.  */
9391                       amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9392                       new_tab = bfd_realloc (tab, amt);
9393                       if (new_tab == NULL)
9394                         {
9395                           free (tab);
9396                           return FALSE;
9397                         }
9398                       tab = new_tab;
9399                       tab[c].gt_entry.gt_g_value = val;
9400                       tab[c].gt_entry.gt_bytes = add;
9401
9402                       /* Merge in the size for the next smallest -G
9403                          value, since that will be implied by this new
9404                          value.  */
9405                       max = 0;
9406                       for (look = 1; look < c; look++)
9407                         {
9408                           if (tab[look].gt_entry.gt_g_value < val
9409                               && (max == 0
9410                                   || (tab[look].gt_entry.gt_g_value
9411                                       > tab[max].gt_entry.gt_g_value)))
9412                             max = look;
9413                         }
9414                       if (max != 0)
9415                         tab[c].gt_entry.gt_bytes +=
9416                           tab[max].gt_entry.gt_bytes;
9417
9418                       ++c;
9419                     }
9420
9421                   last = int_gptab.gt_entry.gt_bytes;
9422                 }
9423
9424               /* Hack: reset the SEC_HAS_CONTENTS flag so that
9425                  elf_link_input_bfd ignores this section.  */
9426               input_section->flags &= ~SEC_HAS_CONTENTS;
9427             }
9428
9429           /* The table must be sorted by -G value.  */
9430           if (c > 2)
9431             qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
9432
9433           /* Swap out the table.  */
9434           amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9435           ext_tab = bfd_alloc (abfd, amt);
9436           if (ext_tab == NULL)
9437             {
9438               free (tab);
9439               return FALSE;
9440             }
9441
9442           for (j = 0; j < c; j++)
9443             bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
9444           free (tab);
9445
9446           o->size = c * sizeof (Elf32_External_gptab);
9447           o->contents = (bfd_byte *) ext_tab;
9448
9449           /* Skip this section later on (I don't think this currently
9450              matters, but someday it might).  */
9451           o->link_order_head = NULL;
9452         }
9453     }
9454
9455   /* Invoke the regular ELF backend linker to do all the work.  */
9456   if (!bfd_elf_final_link (abfd, info))
9457     return FALSE;
9458
9459   /* Now write out the computed sections.  */
9460
9461   if (reginfo_sec != NULL)
9462     {
9463       Elf32_External_RegInfo ext;
9464
9465       bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9466       if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
9467         return FALSE;
9468     }
9469
9470   if (mdebug_sec != NULL)
9471     {
9472       BFD_ASSERT (abfd->output_has_begun);
9473       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
9474                                                swap, info,
9475                                                mdebug_sec->filepos))
9476         return FALSE;
9477
9478       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
9479     }
9480
9481   if (gptab_data_sec != NULL)
9482     {
9483       if (! bfd_set_section_contents (abfd, gptab_data_sec,
9484                                       gptab_data_sec->contents,
9485                                       0, gptab_data_sec->size))
9486         return FALSE;
9487     }
9488
9489   if (gptab_bss_sec != NULL)
9490     {
9491       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
9492                                       gptab_bss_sec->contents,
9493                                       0, gptab_bss_sec->size))
9494         return FALSE;
9495     }
9496
9497   if (SGI_COMPAT (abfd))
9498     {
9499       rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
9500       if (rtproc_sec != NULL)
9501         {
9502           if (! bfd_set_section_contents (abfd, rtproc_sec,
9503                                           rtproc_sec->contents,
9504                                           0, rtproc_sec->size))
9505             return FALSE;
9506         }
9507     }
9508
9509   return TRUE;
9510 }
9511 \f
9512 /* Structure for saying that BFD machine EXTENSION extends BASE.  */
9513
9514 struct mips_mach_extension {
9515   unsigned long extension, base;
9516 };
9517
9518
9519 /* An array describing how BFD machines relate to one another.  The entries
9520    are ordered topologically with MIPS I extensions listed last.  */
9521
9522 static const struct mips_mach_extension mips_mach_extensions[] = {
9523   /* MIPS64 extensions.  */
9524   { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
9525   { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
9526
9527   /* MIPS V extensions.  */
9528   { bfd_mach_mipsisa64, bfd_mach_mips5 },
9529
9530   /* R10000 extensions.  */
9531   { bfd_mach_mips12000, bfd_mach_mips10000 },
9532
9533   /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
9534      vr5400 ISA, but doesn't include the multimedia stuff.  It seems
9535      better to allow vr5400 and vr5500 code to be merged anyway, since
9536      many libraries will just use the core ISA.  Perhaps we could add
9537      some sort of ASE flag if this ever proves a problem.  */
9538   { bfd_mach_mips5500, bfd_mach_mips5400 },
9539   { bfd_mach_mips5400, bfd_mach_mips5000 },
9540
9541   /* MIPS IV extensions.  */
9542   { bfd_mach_mips5, bfd_mach_mips8000 },
9543   { bfd_mach_mips10000, bfd_mach_mips8000 },
9544   { bfd_mach_mips5000, bfd_mach_mips8000 },
9545   { bfd_mach_mips7000, bfd_mach_mips8000 },
9546   { bfd_mach_mips9000, bfd_mach_mips8000 },
9547
9548   /* VR4100 extensions.  */
9549   { bfd_mach_mips4120, bfd_mach_mips4100 },
9550   { bfd_mach_mips4111, bfd_mach_mips4100 },
9551
9552   /* MIPS III extensions.  */
9553   { bfd_mach_mips8000, bfd_mach_mips4000 },
9554   { bfd_mach_mips4650, bfd_mach_mips4000 },
9555   { bfd_mach_mips4600, bfd_mach_mips4000 },
9556   { bfd_mach_mips4400, bfd_mach_mips4000 },
9557   { bfd_mach_mips4300, bfd_mach_mips4000 },
9558   { bfd_mach_mips4100, bfd_mach_mips4000 },
9559   { bfd_mach_mips4010, bfd_mach_mips4000 },
9560
9561   /* MIPS32 extensions.  */
9562   { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
9563
9564   /* MIPS II extensions.  */
9565   { bfd_mach_mips4000, bfd_mach_mips6000 },
9566   { bfd_mach_mipsisa32, bfd_mach_mips6000 },
9567
9568   /* MIPS I extensions.  */
9569   { bfd_mach_mips6000, bfd_mach_mips3000 },
9570   { bfd_mach_mips3900, bfd_mach_mips3000 }
9571 };
9572
9573
9574 /* Return true if bfd machine EXTENSION is an extension of machine BASE.  */
9575
9576 static bfd_boolean
9577 mips_mach_extends_p (unsigned long base, unsigned long extension)
9578 {
9579   size_t i;
9580
9581   for (i = 0; extension != base && i < ARRAY_SIZE (mips_mach_extensions); i++)
9582     if (extension == mips_mach_extensions[i].extension)
9583       extension = mips_mach_extensions[i].base;
9584
9585   return extension == base;
9586 }
9587
9588
9589 /* Return true if the given ELF header flags describe a 32-bit binary.  */
9590
9591 static bfd_boolean
9592 mips_32bit_flags_p (flagword flags)
9593 {
9594   return ((flags & EF_MIPS_32BITMODE) != 0
9595           || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
9596           || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
9597           || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
9598           || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
9599           || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
9600           || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
9601 }
9602
9603
9604 /* Merge backend specific data from an object file to the output
9605    object file when linking.  */
9606
9607 bfd_boolean
9608 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
9609 {
9610   flagword old_flags;
9611   flagword new_flags;
9612   bfd_boolean ok;
9613   bfd_boolean null_input_bfd = TRUE;
9614   asection *sec;
9615
9616   /* Check if we have the same endianess */
9617   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
9618     {
9619       (*_bfd_error_handler)
9620         (_("%B: endianness incompatible with that of the selected emulation"),
9621          ibfd);
9622       return FALSE;
9623     }
9624
9625   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
9626       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
9627     return TRUE;
9628
9629   if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
9630     {
9631       (*_bfd_error_handler)
9632         (_("%B: ABI is incompatible with that of the selected emulation"),
9633          ibfd);
9634       return FALSE;
9635     }
9636
9637   new_flags = elf_elfheader (ibfd)->e_flags;
9638   elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
9639   old_flags = elf_elfheader (obfd)->e_flags;
9640
9641   if (! elf_flags_init (obfd))
9642     {
9643       elf_flags_init (obfd) = TRUE;
9644       elf_elfheader (obfd)->e_flags = new_flags;
9645       elf_elfheader (obfd)->e_ident[EI_CLASS]
9646         = elf_elfheader (ibfd)->e_ident[EI_CLASS];
9647
9648       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
9649           && bfd_get_arch_info (obfd)->the_default)
9650         {
9651           if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
9652                                    bfd_get_mach (ibfd)))
9653             return FALSE;
9654         }
9655
9656       return TRUE;
9657     }
9658
9659   /* Check flag compatibility.  */
9660
9661   new_flags &= ~EF_MIPS_NOREORDER;
9662   old_flags &= ~EF_MIPS_NOREORDER;
9663
9664   /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
9665      doesn't seem to matter.  */
9666   new_flags &= ~EF_MIPS_XGOT;
9667   old_flags &= ~EF_MIPS_XGOT;
9668
9669   /* MIPSpro generates ucode info in n64 objects.  Again, we should
9670      just be able to ignore this.  */
9671   new_flags &= ~EF_MIPS_UCODE;
9672   old_flags &= ~EF_MIPS_UCODE;
9673
9674   if (new_flags == old_flags)
9675     return TRUE;
9676
9677   /* Check to see if the input BFD actually contains any sections.
9678      If not, its flags may not have been initialised either, but it cannot
9679      actually cause any incompatibility.  */
9680   for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9681     {
9682       /* Ignore synthetic sections and empty .text, .data and .bss sections
9683           which are automatically generated by gas.  */
9684       if (strcmp (sec->name, ".reginfo")
9685           && strcmp (sec->name, ".mdebug")
9686           && (sec->size != 0
9687               || (strcmp (sec->name, ".text")
9688                   && strcmp (sec->name, ".data")
9689                   && strcmp (sec->name, ".bss"))))
9690         {
9691           null_input_bfd = FALSE;
9692           break;
9693         }
9694     }
9695   if (null_input_bfd)
9696     return TRUE;
9697
9698   ok = TRUE;
9699
9700   if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
9701       != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
9702     {
9703       (*_bfd_error_handler)
9704         (_("%B: warning: linking PIC files with non-PIC files"),
9705          ibfd);
9706       ok = TRUE;
9707     }
9708
9709   if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
9710     elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
9711   if (! (new_flags & EF_MIPS_PIC))
9712     elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
9713
9714   new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
9715   old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
9716
9717   /* Compare the ISAs.  */
9718   if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
9719     {
9720       (*_bfd_error_handler)
9721         (_("%B: linking 32-bit code with 64-bit code"),
9722          ibfd);
9723       ok = FALSE;
9724     }
9725   else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
9726     {
9727       /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
9728       if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
9729         {
9730           /* Copy the architecture info from IBFD to OBFD.  Also copy
9731              the 32-bit flag (if set) so that we continue to recognise
9732              OBFD as a 32-bit binary.  */
9733           bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
9734           elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
9735           elf_elfheader (obfd)->e_flags
9736             |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
9737
9738           /* Copy across the ABI flags if OBFD doesn't use them
9739              and if that was what caused us to treat IBFD as 32-bit.  */
9740           if ((old_flags & EF_MIPS_ABI) == 0
9741               && mips_32bit_flags_p (new_flags)
9742               && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
9743             elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
9744         }
9745       else
9746         {
9747           /* The ISAs aren't compatible.  */
9748           (*_bfd_error_handler)
9749             (_("%B: linking %s module with previous %s modules"),
9750              ibfd,
9751              bfd_printable_name (ibfd),
9752              bfd_printable_name (obfd));
9753           ok = FALSE;
9754         }
9755     }
9756
9757   new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
9758   old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
9759
9760   /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
9761      does set EI_CLASS differently from any 32-bit ABI.  */
9762   if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
9763       || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
9764           != elf_elfheader (obfd)->e_ident[EI_CLASS]))
9765     {
9766       /* Only error if both are set (to different values).  */
9767       if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
9768           || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
9769               != elf_elfheader (obfd)->e_ident[EI_CLASS]))
9770         {
9771           (*_bfd_error_handler)
9772             (_("%B: ABI mismatch: linking %s module with previous %s modules"),
9773              ibfd,
9774              elf_mips_abi_name (ibfd),
9775              elf_mips_abi_name (obfd));
9776           ok = FALSE;
9777         }
9778       new_flags &= ~EF_MIPS_ABI;
9779       old_flags &= ~EF_MIPS_ABI;
9780     }
9781
9782   /* For now, allow arbitrary mixing of ASEs (retain the union).  */
9783   if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
9784     {
9785       elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
9786
9787       new_flags &= ~ EF_MIPS_ARCH_ASE;
9788       old_flags &= ~ EF_MIPS_ARCH_ASE;
9789     }
9790
9791   /* Warn about any other mismatches */
9792   if (new_flags != old_flags)
9793     {
9794       (*_bfd_error_handler)
9795         (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
9796          ibfd, (unsigned long) new_flags,
9797          (unsigned long) old_flags);
9798       ok = FALSE;
9799     }
9800
9801   if (! ok)
9802     {
9803       bfd_set_error (bfd_error_bad_value);
9804       return FALSE;
9805     }
9806
9807   return TRUE;
9808 }
9809
9810 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
9811
9812 bfd_boolean
9813 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
9814 {
9815   BFD_ASSERT (!elf_flags_init (abfd)
9816               || elf_elfheader (abfd)->e_flags == flags);
9817
9818   elf_elfheader (abfd)->e_flags = flags;
9819   elf_flags_init (abfd) = TRUE;
9820   return TRUE;
9821 }
9822
9823 bfd_boolean
9824 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
9825 {
9826   FILE *file = ptr;
9827
9828   BFD_ASSERT (abfd != NULL && ptr != NULL);
9829
9830   /* Print normal ELF private data.  */
9831   _bfd_elf_print_private_bfd_data (abfd, ptr);
9832
9833   /* xgettext:c-format */
9834   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
9835
9836   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
9837     fprintf (file, _(" [abi=O32]"));
9838   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
9839     fprintf (file, _(" [abi=O64]"));
9840   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
9841     fprintf (file, _(" [abi=EABI32]"));
9842   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
9843     fprintf (file, _(" [abi=EABI64]"));
9844   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
9845     fprintf (file, _(" [abi unknown]"));
9846   else if (ABI_N32_P (abfd))
9847     fprintf (file, _(" [abi=N32]"));
9848   else if (ABI_64_P (abfd))
9849     fprintf (file, _(" [abi=64]"));
9850   else
9851     fprintf (file, _(" [no abi set]"));
9852
9853   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
9854     fprintf (file, _(" [mips1]"));
9855   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
9856     fprintf (file, _(" [mips2]"));
9857   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
9858     fprintf (file, _(" [mips3]"));
9859   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
9860     fprintf (file, _(" [mips4]"));
9861   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
9862     fprintf (file, _(" [mips5]"));
9863   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
9864     fprintf (file, _(" [mips32]"));
9865   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
9866     fprintf (file, _(" [mips64]"));
9867   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
9868     fprintf (file, _(" [mips32r2]"));
9869   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
9870     fprintf (file, _(" [mips64r2]"));
9871   else
9872     fprintf (file, _(" [unknown ISA]"));
9873
9874   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
9875     fprintf (file, _(" [mdmx]"));
9876
9877   if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
9878     fprintf (file, _(" [mips16]"));
9879
9880   if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
9881     fprintf (file, _(" [32bitmode]"));
9882   else
9883     fprintf (file, _(" [not 32bitmode]"));
9884
9885   fputc ('\n', file);
9886
9887   return TRUE;
9888 }
9889
9890 struct bfd_elf_special_section const _bfd_mips_elf_special_sections[]=
9891 {
9892   { ".sdata",  6, -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9893   { ".sbss",   5, -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9894   { ".lit4",   5,  0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9895   { ".lit8",   5,  0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9896   { ".ucode",  6,  0, SHT_MIPS_UCODE, 0 },
9897   { ".mdebug", 7,  0, SHT_MIPS_DEBUG, 0 },
9898   { NULL,      0,  0, 0,              0 }
9899 };