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