xref: /openbsd/gnu/usr.bin/binutils/bfd/elf32-mips.c (revision 78b63d65)
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 (val >= 0x8000 && val < 0xffff8000)
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 
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 
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 
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 
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 (ibfd->xvec->byteorder != obfd->xvec->byteorder
2464       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2465     {
2466       const char *msg;
2467 
2468       if (bfd_big_endian (ibfd))
2469 	msg = _("%s: compiled for a big endian system and target is little endian");
2470       else
2471 	msg = _("%s: compiled for a little endian system and target is big endian");
2472 
2473       (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2474 
2475       bfd_set_error (bfd_error_wrong_format);
2476       return false;
2477     }
2478 
2479   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2480       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2481     return true;
2482 
2483   new_flags = elf_elfheader (ibfd)->e_flags;
2484   elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2485   old_flags = elf_elfheader (obfd)->e_flags;
2486 
2487   if (! elf_flags_init (obfd))
2488     {
2489       elf_flags_init (obfd) = true;
2490       elf_elfheader (obfd)->e_flags = new_flags;
2491       elf_elfheader (obfd)->e_ident[EI_CLASS]
2492 	= elf_elfheader (ibfd)->e_ident[EI_CLASS];
2493 
2494       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2495 	  && bfd_get_arch_info (obfd)->the_default)
2496 	{
2497 	  if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2498 				   bfd_get_mach (ibfd)))
2499 	    return false;
2500 	}
2501 
2502       return true;
2503     }
2504 
2505   /* Check flag compatibility.  */
2506 
2507   new_flags &= ~EF_MIPS_NOREORDER;
2508   old_flags &= ~EF_MIPS_NOREORDER;
2509 
2510   if (new_flags == old_flags)
2511     return true;
2512 
2513   ok = true;
2514 
2515   if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2516     {
2517       new_flags &= ~EF_MIPS_PIC;
2518       old_flags &= ~EF_MIPS_PIC;
2519       (*_bfd_error_handler)
2520 	(_("%s: linking PIC files with non-PIC files"),
2521 	 bfd_get_filename (ibfd));
2522       ok = false;
2523     }
2524 
2525   if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2526     {
2527       new_flags &= ~EF_MIPS_CPIC;
2528       old_flags &= ~EF_MIPS_CPIC;
2529       (*_bfd_error_handler)
2530 	(_("%s: linking abicalls files with non-abicalls files"),
2531 	 bfd_get_filename (ibfd));
2532       ok = false;
2533     }
2534 
2535   /* Compare the ISA's. */
2536   if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2537       != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2538     {
2539       int new_mach = new_flags & EF_MIPS_MACH;
2540       int old_mach = old_flags & EF_MIPS_MACH;
2541       int new_isa = elf_mips_isa (new_flags);
2542       int old_isa = elf_mips_isa (old_flags);
2543 
2544       /* If either has no machine specified, just compare the general isa's.
2545 	 Some combinations of machines are ok, if the isa's match. */
2546       if (! new_mach
2547 	  || ! old_mach
2548 	  || new_mach == old_mach
2549 	  )
2550 	{
2551 	  /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2552 	     and -mips4 code.  They will normally use the same data sizes and
2553 	     calling conventions.  */
2554 
2555 	  if ((new_isa == 1 || new_isa == 2)
2556 	      ? (old_isa != 1 && old_isa != 2)
2557 	      : (old_isa == 1 || old_isa == 2))
2558 	    {
2559 	      (*_bfd_error_handler)
2560 	       (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2561 		bfd_get_filename (ibfd), new_isa, old_isa);
2562 	      ok = false;
2563 	    }
2564 	}
2565 
2566       else
2567 	{
2568 	  (*_bfd_error_handler)
2569 	    (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2570 	     bfd_get_filename (ibfd),
2571 	     elf_mips_mach (new_flags),
2572 	     elf_mips_mach (old_flags));
2573 	  ok = false;
2574 	}
2575 
2576       new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2577       old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2578     }
2579 
2580   /* Compare ABI's.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
2581      does set EI_CLASS differently from any 32-bit ABI.  */
2582   if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
2583       || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2584 	  != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2585     {
2586       /* Only error if both are set (to different values). */
2587       if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
2588 	  || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2589 	      != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2590 	{
2591 	  (*_bfd_error_handler)
2592 	    (_("%s: ABI mismatch: linking %s module with previous %s modules"),
2593 	     bfd_get_filename (ibfd),
2594 	     elf_mips_abi_name (ibfd),
2595 	     elf_mips_abi_name (obfd));
2596 	  ok = false;
2597 	}
2598       new_flags &= ~EF_MIPS_ABI;
2599       old_flags &= ~EF_MIPS_ABI;
2600     }
2601 
2602   /* Warn about any other mismatches */
2603   if (new_flags != old_flags)
2604     {
2605       (*_bfd_error_handler)
2606 	(_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2607 	 bfd_get_filename (ibfd), (unsigned long) new_flags,
2608 	 (unsigned long) old_flags);
2609       ok = false;
2610     }
2611 
2612   if (! ok)
2613     {
2614       bfd_set_error (bfd_error_bad_value);
2615       return false;
2616     }
2617 
2618   return true;
2619 }
2620 
2621 boolean
2622 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
2623      bfd *abfd;
2624      PTR ptr;
2625 {
2626   FILE *file = (FILE *) ptr;
2627 
2628   BFD_ASSERT (abfd != NULL && ptr != NULL);
2629 
2630   /* Print normal ELF private data.  */
2631   _bfd_elf_print_private_bfd_data (abfd, ptr);
2632 
2633   /* xgettext:c-format */
2634   fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2635 
2636   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
2637     fprintf (file, _ (" [abi=O32]"));
2638   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
2639     fprintf (file, _ (" [abi=O64]"));
2640   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
2641     fprintf (file, _ (" [abi=EABI32]"));
2642   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
2643     fprintf (file, _ (" [abi=EABI64]"));
2644   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
2645     fprintf (file, _ (" [abi unknown]"));
2646   else if (ABI_N32_P (abfd))
2647     fprintf (file, _ (" [abi=N32]"));
2648   else if (ABI_64_P (abfd))
2649     fprintf (file, _ (" [abi=64]"));
2650   else
2651     fprintf (file, _ (" [no abi set]"));
2652 
2653   if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
2654     fprintf (file, _ (" [mips1]"));
2655   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
2656     fprintf (file, _ (" [mips2]"));
2657   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
2658     fprintf (file, _ (" [mips3]"));
2659   else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
2660     fprintf (file, _ (" [mips4]"));
2661   else
2662     fprintf (file, _ (" [unknown ISA]"));
2663 
2664   if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
2665     fprintf (file, _ (" [32bitmode]"));
2666   else
2667     fprintf (file, _ (" [not 32bitmode]"));
2668 
2669   fputc ('\n', file);
2670 
2671   return true;
2672 }
2673 
2674 /* Handle a MIPS specific section when reading an object file.  This
2675    is called when elfcode.h finds a section with an unknown type.
2676    This routine supports both the 32-bit and 64-bit ELF ABI.
2677 
2678    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2679    how to.  */
2680 
2681 boolean
2682 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2683      bfd *abfd;
2684      Elf_Internal_Shdr *hdr;
2685      char *name;
2686 {
2687   flagword flags = 0;
2688 
2689   /* There ought to be a place to keep ELF backend specific flags, but
2690      at the moment there isn't one.  We just keep track of the
2691      sections by their name, instead.  Fortunately, the ABI gives
2692      suggested names for all the MIPS specific sections, so we will
2693      probably get away with this.  */
2694   switch (hdr->sh_type)
2695     {
2696     case SHT_MIPS_LIBLIST:
2697       if (strcmp (name, ".liblist") != 0)
2698 	return false;
2699       break;
2700     case SHT_MIPS_MSYM:
2701       if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) != 0)
2702 	return false;
2703       break;
2704     case SHT_MIPS_CONFLICT:
2705       if (strcmp (name, ".conflict") != 0)
2706 	return false;
2707       break;
2708     case SHT_MIPS_GPTAB:
2709       if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2710 	return false;
2711       break;
2712     case SHT_MIPS_UCODE:
2713       if (strcmp (name, ".ucode") != 0)
2714 	return false;
2715       break;
2716     case SHT_MIPS_DEBUG:
2717       if (strcmp (name, ".mdebug") != 0)
2718 	return false;
2719       flags = SEC_DEBUGGING;
2720       break;
2721     case SHT_MIPS_REGINFO:
2722       if (strcmp (name, ".reginfo") != 0
2723 	  || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2724 	return false;
2725       flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
2726       break;
2727     case SHT_MIPS_IFACE:
2728       if (strcmp (name, ".MIPS.interfaces") != 0)
2729 	return false;
2730       break;
2731     case SHT_MIPS_CONTENT:
2732       if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2733 	return false;
2734       break;
2735     case SHT_MIPS_OPTIONS:
2736       if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
2737 	return false;
2738       break;
2739     case SHT_MIPS_DWARF:
2740       if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2741 	return false;
2742       break;
2743     case SHT_MIPS_SYMBOL_LIB:
2744       if (strcmp (name, ".MIPS.symlib") != 0)
2745 	return false;
2746       break;
2747     case SHT_MIPS_EVENTS:
2748       if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2749 	  && strncmp (name, ".MIPS.post_rel",
2750 		      sizeof ".MIPS.post_rel" - 1) != 0)
2751 	return false;
2752       break;
2753     default:
2754       return false;
2755     }
2756 
2757   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2758     return false;
2759 
2760   if (flags)
2761     {
2762       if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2763 				   (bfd_get_section_flags (abfd,
2764 							   hdr->bfd_section)
2765 				    | flags)))
2766 	return false;
2767     }
2768 
2769   /* FIXME: We should record sh_info for a .gptab section.  */
2770 
2771   /* For a .reginfo section, set the gp value in the tdata information
2772      from the contents of this section.  We need the gp value while
2773      processing relocs, so we just get it now.  The .reginfo section
2774      is not used in the 64-bit MIPS ELF ABI.  */
2775   if (hdr->sh_type == SHT_MIPS_REGINFO)
2776     {
2777       Elf32_External_RegInfo ext;
2778       Elf32_RegInfo s;
2779 
2780       if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2781 				      (file_ptr) 0, sizeof ext))
2782 	return false;
2783       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2784       elf_gp (abfd) = s.ri_gp_value;
2785     }
2786 
2787   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2788      set the gp value based on what we find.  We may see both
2789      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2790      they should agree.  */
2791   if (hdr->sh_type == SHT_MIPS_OPTIONS)
2792     {
2793       bfd_byte *contents, *l, *lend;
2794 
2795       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2796       if (contents == NULL)
2797 	return false;
2798       if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2799 				      (file_ptr) 0, hdr->sh_size))
2800 	{
2801 	  free (contents);
2802 	  return false;
2803 	}
2804       l = contents;
2805       lend = contents + hdr->sh_size;
2806       while (l + sizeof (Elf_External_Options) <= lend)
2807 	{
2808 	  Elf_Internal_Options intopt;
2809 
2810 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2811 					&intopt);
2812 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
2813 	    {
2814 	      Elf64_Internal_RegInfo intreg;
2815 
2816 	      bfd_mips_elf64_swap_reginfo_in
2817 		(abfd,
2818 		 ((Elf64_External_RegInfo *)
2819 		  (l + sizeof (Elf_External_Options))),
2820 		 &intreg);
2821 	      elf_gp (abfd) = intreg.ri_gp_value;
2822 	    }
2823 	  else if (intopt.kind == ODK_REGINFO)
2824 	    {
2825 	      Elf32_RegInfo intreg;
2826 
2827 	      bfd_mips_elf32_swap_reginfo_in
2828 		(abfd,
2829 		 ((Elf32_External_RegInfo *)
2830 		  (l + sizeof (Elf_External_Options))),
2831 		 &intreg);
2832 	      elf_gp (abfd) = intreg.ri_gp_value;
2833 	    }
2834 	  l += intopt.size;
2835 	}
2836       free (contents);
2837     }
2838 
2839   return true;
2840 }
2841 
2842 /* Set the correct type for a MIPS ELF section.  We do this by the
2843    section name, which is a hack, but ought to work.  This routine is
2844    used by both the 32-bit and the 64-bit ABI.  */
2845 
2846 boolean
2847 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2848      bfd *abfd;
2849      Elf32_Internal_Shdr *hdr;
2850      asection *sec;
2851 {
2852   register const char *name;
2853 
2854   name = bfd_get_section_name (abfd, sec);
2855 
2856   if (strcmp (name, ".liblist") == 0)
2857     {
2858       hdr->sh_type = SHT_MIPS_LIBLIST;
2859       hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2860       /* The sh_link field is set in final_write_processing.  */
2861     }
2862   else if (strcmp (name, ".conflict") == 0)
2863     hdr->sh_type = SHT_MIPS_CONFLICT;
2864   else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2865     {
2866       hdr->sh_type = SHT_MIPS_GPTAB;
2867       hdr->sh_entsize = sizeof (Elf32_External_gptab);
2868       /* The sh_info field is set in final_write_processing.  */
2869     }
2870   else if (strcmp (name, ".ucode") == 0)
2871     hdr->sh_type = SHT_MIPS_UCODE;
2872   else if (strcmp (name, ".mdebug") == 0)
2873     {
2874       hdr->sh_type = SHT_MIPS_DEBUG;
2875       /* In a shared object on Irix 5.3, the .mdebug section has an
2876          entsize of 0.  FIXME: Does this matter?  */
2877       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2878 	hdr->sh_entsize = 0;
2879       else
2880 	hdr->sh_entsize = 1;
2881     }
2882   else if (strcmp (name, ".reginfo") == 0)
2883     {
2884       hdr->sh_type = SHT_MIPS_REGINFO;
2885       /* In a shared object on Irix 5.3, the .reginfo section has an
2886          entsize of 0x18.  FIXME: Does this matter?  */
2887       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2888 	hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2889       else
2890 	hdr->sh_entsize = 1;
2891     }
2892   else if (SGI_COMPAT (abfd)
2893 	   && (strcmp (name, ".hash") == 0
2894 	       || strcmp (name, ".dynamic") == 0
2895 	       || strcmp (name, ".dynstr") == 0))
2896     {
2897       hdr->sh_entsize = 0;
2898 #if 0
2899       /* This isn't how the Irix 6 linker behaves.  */
2900       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2901 #endif
2902     }
2903   else if (strcmp (name, ".got") == 0
2904 	   || strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0
2905 	   || strcmp (name, ".sdata") == 0
2906 	   || strcmp (name, ".sbss") == 0
2907 	   || strcmp (name, ".lit4") == 0
2908 	   || strcmp (name, ".lit8") == 0)
2909     hdr->sh_flags |= SHF_MIPS_GPREL;
2910   else if (strcmp (name, ".MIPS.interfaces") == 0)
2911     {
2912       hdr->sh_type = SHT_MIPS_IFACE;
2913       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2914     }
2915   else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
2916     {
2917       hdr->sh_type = SHT_MIPS_CONTENT;
2918       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2919       /* The sh_info field is set in final_write_processing.  */
2920     }
2921   else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
2922     {
2923       hdr->sh_type = SHT_MIPS_OPTIONS;
2924       hdr->sh_entsize = 1;
2925       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2926     }
2927   else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2928     hdr->sh_type = SHT_MIPS_DWARF;
2929   else if (strcmp (name, ".MIPS.symlib") == 0)
2930     {
2931       hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2932       /* The sh_link and sh_info fields are set in
2933          final_write_processing.  */
2934     }
2935   else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2936 	   || strncmp (name, ".MIPS.post_rel",
2937 		       sizeof ".MIPS.post_rel" - 1) == 0)
2938     {
2939       hdr->sh_type = SHT_MIPS_EVENTS;
2940       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2941       /* The sh_link field is set in final_write_processing.  */
2942     }
2943   else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) == 0)
2944     {
2945       hdr->sh_type = SHT_MIPS_MSYM;
2946       hdr->sh_flags |= SHF_ALLOC;
2947       hdr->sh_entsize = 8;
2948     }
2949 
2950   /* The generic elf_fake_sections will set up REL_HDR using the
2951      default kind of relocations.  But, we may actually need both
2952      kinds of relocations, so we set up the second header here.  */
2953   if ((sec->flags & SEC_RELOC) != 0)
2954     {
2955       struct bfd_elf_section_data *esd;
2956 
2957       esd = elf_section_data (sec);
2958       BFD_ASSERT (esd->rel_hdr2 == NULL);
2959       esd->rel_hdr2
2960 	= (Elf_Internal_Shdr *) bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2961       if (!esd->rel_hdr2)
2962 	return false;
2963       _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
2964 				!elf_section_data (sec)->use_rela_p);
2965     }
2966 
2967   return true;
2968 }
2969 
2970 /* Given a BFD section, try to locate the corresponding ELF section
2971    index.  This is used by both the 32-bit and the 64-bit ABI.
2972    Actually, it's not clear to me that the 64-bit ABI supports these,
2973    but for non-PIC objects we will certainly want support for at least
2974    the .scommon section.  */
2975 
2976 boolean
2977 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2978      bfd *abfd ATTRIBUTE_UNUSED;
2979      Elf32_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
2980      asection *sec;
2981      int *retval;
2982 {
2983   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2984     {
2985       *retval = SHN_MIPS_SCOMMON;
2986       return true;
2987     }
2988   if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2989     {
2990       *retval = SHN_MIPS_ACOMMON;
2991       return true;
2992     }
2993   return false;
2994 }
2995 
2996 /* When are writing out the .options or .MIPS.options section,
2997    remember the bytes we are writing out, so that we can install the
2998    GP value in the section_processing routine.  */
2999 
3000 boolean
3001 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
3002      bfd *abfd;
3003      sec_ptr section;
3004      PTR location;
3005      file_ptr offset;
3006      bfd_size_type count;
3007 {
3008   if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
3009     {
3010       bfd_byte *c;
3011 
3012       if (elf_section_data (section) == NULL)
3013 	{
3014 	  section->used_by_bfd =
3015 	    (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
3016 	  if (elf_section_data (section) == NULL)
3017 	    return false;
3018 	}
3019       c = (bfd_byte *) elf_section_data (section)->tdata;
3020       if (c == NULL)
3021 	{
3022 	  bfd_size_type size;
3023 
3024 	  if (section->_cooked_size != 0)
3025 	    size = section->_cooked_size;
3026 	  else
3027 	    size = section->_raw_size;
3028 	  c = (bfd_byte *) bfd_zalloc (abfd, size);
3029 	  if (c == NULL)
3030 	    return false;
3031 	  elf_section_data (section)->tdata = (PTR) c;
3032 	}
3033 
3034       memcpy (c + offset, location, count);
3035     }
3036 
3037   return _bfd_elf_set_section_contents (abfd, section, location, offset,
3038 					count);
3039 }
3040 
3041 /* Work over a section just before writing it out.  This routine is
3042    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
3043    sections that need the SHF_MIPS_GPREL flag by name; there has to be
3044    a better way.  */
3045 
3046 boolean
3047 _bfd_mips_elf_section_processing (abfd, hdr)
3048      bfd *abfd;
3049      Elf_Internal_Shdr *hdr;
3050 {
3051   if (hdr->sh_type == SHT_MIPS_REGINFO
3052       && hdr->sh_size > 0)
3053     {
3054       bfd_byte buf[4];
3055 
3056       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
3057       BFD_ASSERT (hdr->contents == NULL);
3058 
3059       if (bfd_seek (abfd,
3060 		    hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
3061 		    SEEK_SET) == -1)
3062 	return false;
3063       bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
3064       if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
3065 	return false;
3066     }
3067 
3068   if (hdr->sh_type == SHT_MIPS_OPTIONS
3069       && hdr->bfd_section != NULL
3070       && elf_section_data (hdr->bfd_section) != NULL
3071       && elf_section_data (hdr->bfd_section)->tdata != NULL)
3072     {
3073       bfd_byte *contents, *l, *lend;
3074 
3075       /* We stored the section contents in the elf_section_data tdata
3076 	 field in the set_section_contents routine.  We save the
3077 	 section contents so that we don't have to read them again.
3078 	 At this point we know that elf_gp is set, so we can look
3079 	 through the section contents to see if there is an
3080 	 ODK_REGINFO structure.  */
3081 
3082       contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
3083       l = contents;
3084       lend = contents + hdr->sh_size;
3085       while (l + sizeof (Elf_External_Options) <= lend)
3086 	{
3087 	  Elf_Internal_Options intopt;
3088 
3089 	  bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
3090 					&intopt);
3091 	  if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
3092 	    {
3093 	      bfd_byte buf[8];
3094 
3095 	      if (bfd_seek (abfd,
3096 			    (hdr->sh_offset
3097 			     + (l - contents)
3098 			     + sizeof (Elf_External_Options)
3099 			     + (sizeof (Elf64_External_RegInfo) - 8)),
3100 			     SEEK_SET) == -1)
3101 		return false;
3102 	      bfd_h_put_64 (abfd, elf_gp (abfd), buf);
3103 	      if (bfd_write (buf, 1, 8, abfd) != 8)
3104 		return false;
3105 	    }
3106 	  else if (intopt.kind == ODK_REGINFO)
3107 	    {
3108 	      bfd_byte buf[4];
3109 
3110 	      if (bfd_seek (abfd,
3111 			    (hdr->sh_offset
3112 			     + (l - contents)
3113 			     + sizeof (Elf_External_Options)
3114 			     + (sizeof (Elf32_External_RegInfo) - 4)),
3115 			     SEEK_SET) == -1)
3116 		return false;
3117 	      bfd_h_put_32 (abfd, elf_gp (abfd), buf);
3118 	      if (bfd_write (buf, 1, 4, abfd) != 4)
3119 		return false;
3120 	    }
3121 	  l += intopt.size;
3122 	}
3123     }
3124 
3125   if (hdr->bfd_section != NULL)
3126     {
3127       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
3128 
3129       if (strcmp (name, ".sdata") == 0
3130 	  || strcmp (name, ".lit8") == 0
3131 	  || strcmp (name, ".lit4") == 0)
3132 	{
3133 	  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3134 	  hdr->sh_type = SHT_PROGBITS;
3135 	}
3136       else if (strcmp (name, ".sbss") == 0)
3137 	{
3138 	  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3139 	  hdr->sh_type = SHT_NOBITS;
3140 	}
3141       else if (strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0)
3142 	{
3143 	  hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
3144 	  hdr->sh_type = SHT_PROGBITS;
3145 	}
3146       else if (strcmp (name, ".compact_rel") == 0)
3147 	{
3148 	  hdr->sh_flags = 0;
3149 	  hdr->sh_type = SHT_PROGBITS;
3150 	}
3151       else if (strcmp (name, ".rtproc") == 0)
3152 	{
3153 	  if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
3154 	    {
3155 	      unsigned int adjust;
3156 
3157 	      adjust = hdr->sh_size % hdr->sh_addralign;
3158 	      if (adjust != 0)
3159 		hdr->sh_size += hdr->sh_addralign - adjust;
3160 	    }
3161 	}
3162     }
3163 
3164   return true;
3165 }
3166 
3167 
3168 /* MIPS ELF uses two common sections.  One is the usual one, and the
3169    other is for small objects.  All the small objects are kept
3170    together, and then referenced via the gp pointer, which yields
3171    faster assembler code.  This is what we use for the small common
3172    section.  This approach is copied from ecoff.c.  */
3173 static asection mips_elf_scom_section;
3174 static asymbol mips_elf_scom_symbol;
3175 static asymbol *mips_elf_scom_symbol_ptr;
3176 
3177 /* MIPS ELF also uses an acommon section, which represents an
3178    allocated common symbol which may be overridden by a
3179    definition in a shared library.  */
3180 static asection mips_elf_acom_section;
3181 static asymbol mips_elf_acom_symbol;
3182 static asymbol *mips_elf_acom_symbol_ptr;
3183 
3184 /* The Irix 5 support uses two virtual sections, which represent
3185    text/data symbols defined in dynamic objects.  */
3186 static asection mips_elf_text_section;
3187 static asection *mips_elf_text_section_ptr;
3188 static asymbol mips_elf_text_symbol;
3189 static asymbol *mips_elf_text_symbol_ptr;
3190 
3191 static asection mips_elf_data_section;
3192 static asection *mips_elf_data_section_ptr;
3193 static asymbol mips_elf_data_symbol;
3194 static asymbol *mips_elf_data_symbol_ptr;
3195 
3196 /* Handle the special MIPS section numbers that a symbol may use.
3197    This is used for both the 32-bit and the 64-bit ABI.  */
3198 
3199 void
3200 _bfd_mips_elf_symbol_processing (abfd, asym)
3201      bfd *abfd;
3202      asymbol *asym;
3203 {
3204   elf_symbol_type *elfsym;
3205 
3206   elfsym = (elf_symbol_type *) asym;
3207   switch (elfsym->internal_elf_sym.st_shndx)
3208     {
3209     case SHN_MIPS_ACOMMON:
3210       /* This section is used in a dynamically linked executable file.
3211 	 It is an allocated common section.  The dynamic linker can
3212 	 either resolve these symbols to something in a shared
3213 	 library, or it can just leave them here.  For our purposes,
3214 	 we can consider these symbols to be in a new section.  */
3215       if (mips_elf_acom_section.name == NULL)
3216 	{
3217 	  /* Initialize the acommon section.  */
3218 	  mips_elf_acom_section.name = ".acommon";
3219 	  mips_elf_acom_section.flags = SEC_ALLOC;
3220 	  mips_elf_acom_section.output_section = &mips_elf_acom_section;
3221 	  mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
3222 	  mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
3223 	  mips_elf_acom_symbol.name = ".acommon";
3224 	  mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
3225 	  mips_elf_acom_symbol.section = &mips_elf_acom_section;
3226 	  mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
3227 	}
3228       asym->section = &mips_elf_acom_section;
3229       break;
3230 
3231     case SHN_COMMON:
3232       /* Common symbols less than the GP size are automatically
3233 	 treated as SHN_MIPS_SCOMMON symbols on IRIX5.  */
3234       if (asym->value > elf_gp_size (abfd)
3235 	  || IRIX_COMPAT (abfd) == ict_irix6)
3236 	break;
3237       /* Fall through.  */
3238     case SHN_MIPS_SCOMMON:
3239       if (mips_elf_scom_section.name == NULL)
3240 	{
3241 	  /* Initialize the small common section.  */
3242 	  mips_elf_scom_section.name = ".scommon";
3243 	  mips_elf_scom_section.flags = SEC_IS_COMMON;
3244 	  mips_elf_scom_section.output_section = &mips_elf_scom_section;
3245 	  mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
3246 	  mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
3247 	  mips_elf_scom_symbol.name = ".scommon";
3248 	  mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
3249 	  mips_elf_scom_symbol.section = &mips_elf_scom_section;
3250 	  mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
3251 	}
3252       asym->section = &mips_elf_scom_section;
3253       asym->value = elfsym->internal_elf_sym.st_size;
3254       break;
3255 
3256     case SHN_MIPS_SUNDEFINED:
3257       asym->section = bfd_und_section_ptr;
3258       break;
3259 
3260 #if 0 /* for SGI_COMPAT */
3261     case SHN_MIPS_TEXT:
3262       asym->section = mips_elf_text_section_ptr;
3263       break;
3264 
3265     case SHN_MIPS_DATA:
3266       asym->section = mips_elf_data_section_ptr;
3267       break;
3268 #endif
3269     }
3270 }
3271 
3272 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
3273    segments.  */
3274 
3275 int
3276 _bfd_mips_elf_additional_program_headers (abfd)
3277      bfd *abfd;
3278 {
3279   asection *s;
3280   int ret = 0;
3281 
3282   if (!SGI_COMPAT (abfd))
3283     return 0;
3284 
3285   /* See if we need a PT_MIPS_REGINFO segment.  */
3286   s = bfd_get_section_by_name (abfd, ".reginfo");
3287   if (s && (s->flags & SEC_LOAD))
3288     ++ret;
3289 
3290   /* See if we need a PT_MIPS_OPTIONS segment.  */
3291   if (IRIX_COMPAT (abfd) == ict_irix6
3292       && bfd_get_section_by_name (abfd,
3293 				  MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
3294     ++ret;
3295 
3296   /* See if we need a PT_MIPS_RTPROC segment.  */
3297   if (IRIX_COMPAT (abfd) == ict_irix5
3298       && bfd_get_section_by_name (abfd, ".dynamic")
3299       && bfd_get_section_by_name (abfd, ".mdebug"))
3300     ++ret;
3301 
3302   return ret;
3303 }
3304 
3305 /* Modify the segment map for an Irix 5 executable.  */
3306 
3307 boolean
3308 _bfd_mips_elf_modify_segment_map (abfd)
3309      bfd *abfd;
3310 {
3311   asection *s;
3312   struct elf_segment_map *m, **pm;
3313 
3314   if (! SGI_COMPAT (abfd))
3315     return true;
3316 
3317   /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
3318      segment.  */
3319   s = bfd_get_section_by_name (abfd, ".reginfo");
3320   if (s != NULL && (s->flags & SEC_LOAD) != 0)
3321     {
3322       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3323 	if (m->p_type == PT_MIPS_REGINFO)
3324 	  break;
3325       if (m == NULL)
3326 	{
3327 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3328 	  if (m == NULL)
3329 	    return false;
3330 
3331 	  m->p_type = PT_MIPS_REGINFO;
3332 	  m->count = 1;
3333 	  m->sections[0] = s;
3334 
3335 	  /* We want to put it after the PHDR and INTERP segments.  */
3336 	  pm = &elf_tdata (abfd)->segment_map;
3337 	  while (*pm != NULL
3338 		 && ((*pm)->p_type == PT_PHDR
3339 		     || (*pm)->p_type == PT_INTERP))
3340 	    pm = &(*pm)->next;
3341 
3342 	  m->next = *pm;
3343 	  *pm = m;
3344 	}
3345     }
3346 
3347   /* For IRIX 6, we don't have .mdebug sections, nor does anything but
3348      .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
3349      PT_OPTIONS segement immediately following the program header
3350      table.  */
3351   if (IRIX_COMPAT (abfd) == ict_irix6)
3352     {
3353       asection *s;
3354 
3355       for (s = abfd->sections; s; s = s->next)
3356 	if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
3357 	  break;
3358 
3359       if (s)
3360 	{
3361 	  struct elf_segment_map *options_segment;
3362 
3363 	  /* Usually, there's a program header table.  But, sometimes
3364 	     there's not (like when running the `ld' testsuite).  So,
3365 	     if there's no program header table, we just put the
3366 	     options segement at the end.  */
3367 	  for (pm = &elf_tdata (abfd)->segment_map;
3368 	       *pm != NULL;
3369 	       pm = &(*pm)->next)
3370 	    if ((*pm)->p_type == PT_PHDR)
3371 	      break;
3372 
3373 	  options_segment = bfd_zalloc (abfd,
3374 					sizeof (struct elf_segment_map));
3375 	  options_segment->next = *pm;
3376 	  options_segment->p_type = PT_MIPS_OPTIONS;
3377 	  options_segment->p_flags = PF_R;
3378 	  options_segment->p_flags_valid = true;
3379 	  options_segment->count = 1;
3380 	  options_segment->sections[0] = s;
3381 	  *pm = options_segment;
3382 	}
3383     }
3384   else
3385     {
3386       /* If there are .dynamic and .mdebug sections, we make a room
3387 	 for the RTPROC header.  FIXME: Rewrite without section names.  */
3388       if (bfd_get_section_by_name (abfd, ".interp") == NULL
3389 	  && bfd_get_section_by_name (abfd, ".dynamic") != NULL
3390 	  && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
3391 	{
3392 	  for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3393 	    if (m->p_type == PT_MIPS_RTPROC)
3394 	      break;
3395 	  if (m == NULL)
3396 	    {
3397 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3398 	      if (m == NULL)
3399 		return false;
3400 
3401 	      m->p_type = PT_MIPS_RTPROC;
3402 
3403 	      s = bfd_get_section_by_name (abfd, ".rtproc");
3404 	      if (s == NULL)
3405 		{
3406 		  m->count = 0;
3407 		  m->p_flags = 0;
3408 		  m->p_flags_valid = 1;
3409 		}
3410 	      else
3411 		{
3412 		  m->count = 1;
3413 		  m->sections[0] = s;
3414 		}
3415 
3416 	      /* We want to put it after the DYNAMIC segment.  */
3417 	      pm = &elf_tdata (abfd)->segment_map;
3418 	      while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
3419 		pm = &(*pm)->next;
3420 	      if (*pm != NULL)
3421 		pm = &(*pm)->next;
3422 
3423 	      m->next = *pm;
3424 	      *pm = m;
3425 	    }
3426 	}
3427 
3428       /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic,
3429 	 .dynstr, .dynsym, and .hash sections, and everything in
3430 	 between.  */
3431       for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
3432 	if ((*pm)->p_type == PT_DYNAMIC)
3433 	  break;
3434       m = *pm;
3435       if (m != NULL
3436 	  && m->count == 1
3437 	  && strcmp (m->sections[0]->name, ".dynamic") == 0)
3438 	{
3439 	  static const char *sec_names[] =
3440 	  { ".dynamic", ".dynstr", ".dynsym", ".hash" };
3441 	  bfd_vma low, high;
3442 	  unsigned int i, c;
3443 	  struct elf_segment_map *n;
3444 
3445 	  low = 0xffffffff;
3446 	  high = 0;
3447 	  for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3448 	    {
3449 	      s = bfd_get_section_by_name (abfd, sec_names[i]);
3450 	      if (s != NULL && (s->flags & SEC_LOAD) != 0)
3451 		{
3452 		  bfd_size_type sz;
3453 
3454 		  if (low > s->vma)
3455 		    low = s->vma;
3456 		  sz = s->_cooked_size;
3457 		  if (sz == 0)
3458 		    sz = s->_raw_size;
3459 		  if (high < s->vma + sz)
3460 		    high = s->vma + sz;
3461 		}
3462 	    }
3463 
3464 	  c = 0;
3465 	  for (s = abfd->sections; s != NULL; s = s->next)
3466 	    if ((s->flags & SEC_LOAD) != 0
3467 		&& s->vma >= low
3468 		&& ((s->vma
3469 		     + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3470 		    <= high))
3471 	      ++c;
3472 
3473 	  n = ((struct elf_segment_map *)
3474 	       bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3475 	  if (n == NULL)
3476 	    return false;
3477 	  *n = *m;
3478 	  n->count = c;
3479 
3480 	  i = 0;
3481 	  for (s = abfd->sections; s != NULL; s = s->next)
3482 	    {
3483 	      if ((s->flags & SEC_LOAD) != 0
3484 		  && s->vma >= low
3485 		  && ((s->vma
3486 		       + (s->_cooked_size != 0 ?
3487 			  s->_cooked_size : s->_raw_size))
3488 		      <= high))
3489 		{
3490 		  n->sections[i] = s;
3491 		  ++i;
3492 		}
3493 	    }
3494 
3495 	  *pm = n;
3496 	}
3497     }
3498 
3499   return true;
3500 }
3501 
3502 /* The structure of the runtime procedure descriptor created by the
3503    loader for use by the static exception system.  */
3504 
3505 typedef struct runtime_pdr {
3506 	bfd_vma	adr;		/* memory address of start of procedure */
3507 	long	regmask;	/* save register mask */
3508 	long	regoffset;	/* save register offset */
3509 	long	fregmask;	/* save floating point register mask */
3510 	long	fregoffset;	/* save floating point register offset */
3511 	long	frameoffset;	/* frame size */
3512 	short	framereg;	/* frame pointer register */
3513 	short	pcreg;		/* offset or reg of return pc */
3514 	long	irpss;		/* index into the runtime string table */
3515 	long	reserved;
3516 	struct exception_info *exception_info;/* pointer to exception array */
3517 } RPDR, *pRPDR;
3518 #define cbRPDR sizeof(RPDR)
3519 #define rpdNil ((pRPDR) 0)
3520 
3521 /* Swap RPDR (runtime procedure table entry) for output.  */
3522 
3523 static void ecoff_swap_rpdr_out
3524   PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3525 
3526 static void
3527 ecoff_swap_rpdr_out (abfd, in, ex)
3528      bfd *abfd;
3529      const RPDR *in;
3530      struct rpdr_ext *ex;
3531 {
3532   /* ecoff_put_off was defined in ecoffswap.h.  */
3533   ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3534   bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3535   bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3536   bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3537   bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3538   bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3539 
3540   bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3541   bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3542 
3543   bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3544 #if 0 /* FIXME */
3545   ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3546 #endif
3547 }
3548 
3549 /* Read ECOFF debugging information from a .mdebug section into a
3550    ecoff_debug_info structure.  */
3551 
3552 boolean
3553 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3554      bfd *abfd;
3555      asection *section;
3556      struct ecoff_debug_info *debug;
3557 {
3558   HDRR *symhdr;
3559   const struct ecoff_debug_swap *swap;
3560   char *ext_hdr = NULL;
3561 
3562   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3563   memset (debug, 0, sizeof(*debug));
3564 
3565   ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3566   if (ext_hdr == NULL && swap->external_hdr_size != 0)
3567     goto error_return;
3568 
3569   if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3570 				swap->external_hdr_size)
3571       == false)
3572     goto error_return;
3573 
3574   symhdr = &debug->symbolic_header;
3575   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3576 
3577   /* The symbolic header contains absolute file offsets and sizes to
3578      read.  */
3579 #define READ(ptr, offset, count, size, type)				\
3580   if (symhdr->count == 0)						\
3581     debug->ptr = NULL;							\
3582   else									\
3583     {									\
3584       debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count));	\
3585       if (debug->ptr == NULL)						\
3586 	goto error_return;						\
3587       if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0	\
3588 	  || (bfd_read (debug->ptr, size, symhdr->count,		\
3589 			abfd) != size * symhdr->count))			\
3590 	goto error_return;						\
3591     }
3592 
3593   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3594   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3595   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3596   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3597   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3598   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3599 	union aux_ext *);
3600   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3601   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3602   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3603   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3604   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3605 #undef READ
3606 
3607   debug->fdr = NULL;
3608   debug->adjust = NULL;
3609 
3610   return true;
3611 
3612  error_return:
3613   if (ext_hdr != NULL)
3614     free (ext_hdr);
3615   if (debug->line != NULL)
3616     free (debug->line);
3617   if (debug->external_dnr != NULL)
3618     free (debug->external_dnr);
3619   if (debug->external_pdr != NULL)
3620     free (debug->external_pdr);
3621   if (debug->external_sym != NULL)
3622     free (debug->external_sym);
3623   if (debug->external_opt != NULL)
3624     free (debug->external_opt);
3625   if (debug->external_aux != NULL)
3626     free (debug->external_aux);
3627   if (debug->ss != NULL)
3628     free (debug->ss);
3629   if (debug->ssext != NULL)
3630     free (debug->ssext);
3631   if (debug->external_fdr != NULL)
3632     free (debug->external_fdr);
3633   if (debug->external_rfd != NULL)
3634     free (debug->external_rfd);
3635   if (debug->external_ext != NULL)
3636     free (debug->external_ext);
3637   return false;
3638 }
3639 
3640 /* MIPS ELF local labels start with '$', not 'L'.  */
3641 
3642 /*ARGSUSED*/
3643 static boolean
3644 mips_elf_is_local_label_name (abfd, name)
3645      bfd *abfd;
3646      const char *name;
3647 {
3648   if (name[0] == '$')
3649     return true;
3650 
3651   /* On Irix 6, the labels go back to starting with '.', so we accept
3652      the generic ELF local label syntax as well.  */
3653   return _bfd_elf_is_local_label_name (abfd, name);
3654 }
3655 
3656 /* MIPS ELF uses a special find_nearest_line routine in order the
3657    handle the ECOFF debugging information.  */
3658 
3659 struct mips_elf_find_line
3660 {
3661   struct ecoff_debug_info d;
3662   struct ecoff_find_line i;
3663 };
3664 
3665 boolean
3666 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3667 				 functionname_ptr, line_ptr)
3668      bfd *abfd;
3669      asection *section;
3670      asymbol **symbols;
3671      bfd_vma offset;
3672      const char **filename_ptr;
3673      const char **functionname_ptr;
3674      unsigned int *line_ptr;
3675 {
3676   asection *msec;
3677 
3678   if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
3679 				     filename_ptr, functionname_ptr,
3680 				     line_ptr))
3681     return true;
3682 
3683   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3684 				     filename_ptr, functionname_ptr,
3685 				     line_ptr,
3686 				     ABI_64_P (abfd) ? 8 : 0))
3687     return true;
3688 
3689   msec = bfd_get_section_by_name (abfd, ".mdebug");
3690   if (msec != NULL)
3691     {
3692       flagword origflags;
3693       struct mips_elf_find_line *fi;
3694       const struct ecoff_debug_swap * const swap =
3695 	get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3696 
3697       /* If we are called during a link, mips_elf_final_link may have
3698 	 cleared the SEC_HAS_CONTENTS field.  We force it back on here
3699 	 if appropriate (which it normally will be).  */
3700       origflags = msec->flags;
3701       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3702 	msec->flags |= SEC_HAS_CONTENTS;
3703 
3704       fi = elf_tdata (abfd)->find_line_info;
3705       if (fi == NULL)
3706 	{
3707 	  bfd_size_type external_fdr_size;
3708 	  char *fraw_src;
3709 	  char *fraw_end;
3710 	  struct fdr *fdr_ptr;
3711 
3712 	  fi = ((struct mips_elf_find_line *)
3713 		bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3714 	  if (fi == NULL)
3715 	    {
3716 	      msec->flags = origflags;
3717 	      return false;
3718 	    }
3719 
3720 	  if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3721 	    {
3722 	      msec->flags = origflags;
3723 	      return false;
3724 	    }
3725 
3726 	  /* Swap in the FDR information.  */
3727 	  fi->d.fdr = ((struct fdr *)
3728 		       bfd_alloc (abfd,
3729 				  (fi->d.symbolic_header.ifdMax *
3730 				   sizeof (struct fdr))));
3731 	  if (fi->d.fdr == NULL)
3732 	    {
3733 	      msec->flags = origflags;
3734 	      return false;
3735 	    }
3736 	  external_fdr_size = swap->external_fdr_size;
3737 	  fdr_ptr = fi->d.fdr;
3738 	  fraw_src = (char *) fi->d.external_fdr;
3739 	  fraw_end = (fraw_src
3740 		      + fi->d.symbolic_header.ifdMax * external_fdr_size);
3741 	  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3742 	    (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3743 
3744 	  elf_tdata (abfd)->find_line_info = fi;
3745 
3746 	  /* Note that we don't bother to ever free this information.
3747              find_nearest_line is either called all the time, as in
3748              objdump -l, so the information should be saved, or it is
3749              rarely called, as in ld error messages, so the memory
3750              wasted is unimportant.  Still, it would probably be a
3751              good idea for free_cached_info to throw it away.  */
3752 	}
3753 
3754       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3755 				  &fi->i, filename_ptr, functionname_ptr,
3756 				  line_ptr))
3757 	{
3758 	  msec->flags = origflags;
3759 	  return true;
3760 	}
3761 
3762       msec->flags = origflags;
3763     }
3764 
3765   /* Fall back on the generic ELF find_nearest_line routine.  */
3766 
3767   return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3768 				     filename_ptr, functionname_ptr,
3769 				     line_ptr);
3770 }
3771 
3772   /* The mips16 compiler uses a couple of special sections to handle
3773      floating point arguments.
3774 
3775      Section names that look like .mips16.fn.FNNAME contain stubs that
3776      copy floating point arguments from the fp regs to the gp regs and
3777      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
3778      call should be redirected to the stub instead.  If no 32 bit
3779      function calls FNNAME, the stub should be discarded.  We need to
3780      consider any reference to the function, not just a call, because
3781      if the address of the function is taken we will need the stub,
3782      since the address might be passed to a 32 bit function.
3783 
3784      Section names that look like .mips16.call.FNNAME contain stubs
3785      that copy floating point arguments from the gp regs to the fp
3786      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
3787      then any 16 bit function that calls FNNAME should be redirected
3788      to the stub instead.  If FNNAME is not a 32 bit function, the
3789      stub should be discarded.
3790 
3791      .mips16.call.fp.FNNAME sections are similar, but contain stubs
3792      which call FNNAME and then copy the return value from the fp regs
3793      to the gp regs.  These stubs store the return value in $18 while
3794      calling FNNAME; any function which might call one of these stubs
3795      must arrange to save $18 around the call.  (This case is not
3796      needed for 32 bit functions that call 16 bit functions, because
3797      16 bit functions always return floating point values in both
3798      $f0/$f1 and $2/$3.)
3799 
3800      Note that in all cases FNNAME might be defined statically.
3801      Therefore, FNNAME is not used literally.  Instead, the relocation
3802      information will indicate which symbol the section is for.
3803 
3804      We record any stubs that we find in the symbol table.  */
3805 
3806 #define FN_STUB ".mips16.fn."
3807 #define CALL_STUB ".mips16.call."
3808 #define CALL_FP_STUB ".mips16.call.fp."
3809 
3810 /* MIPS ELF linker hash table.  */
3811 
3812 struct mips_elf_link_hash_table
3813 {
3814   struct elf_link_hash_table root;
3815 #if 0
3816   /* We no longer use this.  */
3817   /* String section indices for the dynamic section symbols.  */
3818   bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3819 #endif
3820   /* The number of .rtproc entries.  */
3821   bfd_size_type procedure_count;
3822   /* The size of the .compact_rel section (if SGI_COMPAT).  */
3823   bfd_size_type compact_rel_size;
3824   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3825      entry is set to the address of __rld_obj_head as in Irix 5. */
3826   boolean use_rld_obj_head;
3827   /* This is the value of the __rld_map or __rld_obj_head symbol.  */
3828   bfd_vma rld_value;
3829   /* This is set if we see any mips16 stub sections. */
3830   boolean mips16_stubs_seen;
3831 };
3832 
3833 /* Look up an entry in a MIPS ELF linker hash table.  */
3834 
3835 #define mips_elf_link_hash_lookup(table, string, create, copy, follow)	\
3836   ((struct mips_elf_link_hash_entry *)					\
3837    elf_link_hash_lookup (&(table)->root, (string), (create),		\
3838 			 (copy), (follow)))
3839 
3840 /* Traverse a MIPS ELF linker hash table.  */
3841 
3842 #define mips_elf_link_hash_traverse(table, func, info)			\
3843   (elf_link_hash_traverse						\
3844    (&(table)->root,							\
3845     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),	\
3846     (info)))
3847 
3848 /* Get the MIPS ELF linker hash table from a link_info structure.  */
3849 
3850 #define mips_elf_hash_table(p) \
3851   ((struct mips_elf_link_hash_table *) ((p)->hash))
3852 
3853 static boolean mips_elf_output_extsym
3854   PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3855 
3856 /* Create an entry in a MIPS ELF linker hash table.  */
3857 
3858 static struct bfd_hash_entry *
3859 mips_elf_link_hash_newfunc (entry, table, string)
3860      struct bfd_hash_entry *entry;
3861      struct bfd_hash_table *table;
3862      const char *string;
3863 {
3864   struct mips_elf_link_hash_entry *ret =
3865     (struct mips_elf_link_hash_entry *) entry;
3866 
3867   /* Allocate the structure if it has not already been allocated by a
3868      subclass.  */
3869   if (ret == (struct mips_elf_link_hash_entry *) NULL)
3870     ret = ((struct mips_elf_link_hash_entry *)
3871 	   bfd_hash_allocate (table,
3872 			      sizeof (struct mips_elf_link_hash_entry)));
3873   if (ret == (struct mips_elf_link_hash_entry *) NULL)
3874     return (struct bfd_hash_entry *) ret;
3875 
3876   /* Call the allocation method of the superclass.  */
3877   ret = ((struct mips_elf_link_hash_entry *)
3878 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3879 				     table, string));
3880   if (ret != (struct mips_elf_link_hash_entry *) NULL)
3881     {
3882       /* Set local fields.  */
3883       memset (&ret->esym, 0, sizeof (EXTR));
3884       /* We use -2 as a marker to indicate that the information has
3885 	 not been set.  -1 means there is no associated ifd.  */
3886       ret->esym.ifd = -2;
3887       ret->possibly_dynamic_relocs = 0;
3888       ret->min_dyn_reloc_index = 0;
3889       ret->fn_stub = NULL;
3890       ret->need_fn_stub = false;
3891       ret->call_stub = NULL;
3892       ret->call_fp_stub = NULL;
3893     }
3894 
3895   return (struct bfd_hash_entry *) ret;
3896 }
3897 
3898 /* Create a MIPS ELF linker hash table.  */
3899 
3900 struct bfd_link_hash_table *
3901 _bfd_mips_elf_link_hash_table_create (abfd)
3902      bfd *abfd;
3903 {
3904   struct mips_elf_link_hash_table *ret;
3905 
3906   ret = ((struct mips_elf_link_hash_table *)
3907 	 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3908   if (ret == (struct mips_elf_link_hash_table *) NULL)
3909     return NULL;
3910 
3911   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3912 				       mips_elf_link_hash_newfunc))
3913     {
3914       bfd_release (abfd, ret);
3915       return NULL;
3916     }
3917 
3918 #if 0
3919   /* We no longer use this.  */
3920   for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3921     ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3922 #endif
3923   ret->procedure_count = 0;
3924   ret->compact_rel_size = 0;
3925   ret->use_rld_obj_head = false;
3926   ret->rld_value = 0;
3927   ret->mips16_stubs_seen = false;
3928 
3929   return &ret->root.root;
3930 }
3931 
3932 /* Hook called by the linker routine which adds symbols from an object
3933    file.  We must handle the special MIPS section numbers here.  */
3934 
3935 /*ARGSUSED*/
3936 boolean
3937 _bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3938      bfd *abfd;
3939      struct bfd_link_info *info;
3940      const Elf_Internal_Sym *sym;
3941      const char **namep;
3942      flagword *flagsp ATTRIBUTE_UNUSED;
3943      asection **secp;
3944      bfd_vma *valp;
3945 {
3946   if (SGI_COMPAT (abfd)
3947       && (abfd->flags & DYNAMIC) != 0
3948       && strcmp (*namep, "_rld_new_interface") == 0)
3949     {
3950       /* Skip Irix 5 rld entry name.  */
3951       *namep = NULL;
3952       return true;
3953     }
3954 
3955   switch (sym->st_shndx)
3956     {
3957     case SHN_COMMON:
3958       /* Common symbols less than the GP size are automatically
3959 	 treated as SHN_MIPS_SCOMMON symbols.  */
3960       if (sym->st_size > elf_gp_size (abfd)
3961 	  || IRIX_COMPAT (abfd) == ict_irix6)
3962 	break;
3963       /* Fall through.  */
3964     case SHN_MIPS_SCOMMON:
3965       *secp = bfd_make_section_old_way (abfd, ".scommon");
3966       (*secp)->flags |= SEC_IS_COMMON;
3967       *valp = sym->st_size;
3968       break;
3969 
3970     case SHN_MIPS_TEXT:
3971       /* This section is used in a shared object.  */
3972       if (mips_elf_text_section_ptr == NULL)
3973 	{
3974 	  /* Initialize the section.  */
3975 	  mips_elf_text_section.name = ".text";
3976 	  mips_elf_text_section.flags = SEC_NO_FLAGS;
3977 	  mips_elf_text_section.output_section = NULL;
3978 	  mips_elf_text_section.symbol = &mips_elf_text_symbol;
3979 	  mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3980 	  mips_elf_text_symbol.name = ".text";
3981 	  mips_elf_text_symbol.flags = BSF_SECTION_SYM | BSF_DYNAMIC;
3982 	  mips_elf_text_symbol.section = &mips_elf_text_section;
3983 	  mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3984 	  mips_elf_text_section_ptr = &mips_elf_text_section;
3985 	}
3986       /* This code used to do *secp = bfd_und_section_ptr if
3987          info->shared.  I don't know why, and that doesn't make sense,
3988          so I took it out.  */
3989       *secp = mips_elf_text_section_ptr;
3990       break;
3991 
3992     case SHN_MIPS_ACOMMON:
3993       /* Fall through. XXX Can we treat this as allocated data?  */
3994     case SHN_MIPS_DATA:
3995       /* This section is used in a shared object.  */
3996       if (mips_elf_data_section_ptr == NULL)
3997 	{
3998 	  /* Initialize the section.  */
3999 	  mips_elf_data_section.name = ".data";
4000 	  mips_elf_data_section.flags = SEC_NO_FLAGS;
4001 	  mips_elf_data_section.output_section = NULL;
4002 	  mips_elf_data_section.symbol = &mips_elf_data_symbol;
4003 	  mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
4004 	  mips_elf_data_symbol.name = ".data";
4005 	  mips_elf_data_symbol.flags = BSF_SECTION_SYM | BSF_DYNAMIC;
4006 	  mips_elf_data_symbol.section = &mips_elf_data_section;
4007 	  mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
4008 	  mips_elf_data_section_ptr = &mips_elf_data_section;
4009 	}
4010       /* This code used to do *secp = bfd_und_section_ptr if
4011          info->shared.  I don't know why, and that doesn't make sense,
4012          so I took it out.  */
4013       *secp = mips_elf_data_section_ptr;
4014       break;
4015 
4016     case SHN_MIPS_SUNDEFINED:
4017       *secp = bfd_und_section_ptr;
4018       break;
4019     }
4020 
4021   if (SGI_COMPAT (abfd)
4022       && ! info->shared
4023       && info->hash->creator == abfd->xvec
4024       && strcmp (*namep, "__rld_obj_head") == 0)
4025     {
4026       struct elf_link_hash_entry *h;
4027 
4028       /* Mark __rld_obj_head as dynamic.  */
4029       h = NULL;
4030       if (! (_bfd_generic_link_add_one_symbol
4031 	     (info, abfd, *namep, BSF_GLOBAL, *secp,
4032 	      (bfd_vma) *valp, (const char *) NULL, false,
4033 	      get_elf_backend_data (abfd)->collect,
4034 	      (struct bfd_link_hash_entry **) &h)))
4035 	return false;
4036       h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
4037       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4038       h->type = STT_OBJECT;
4039 
4040       if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4041 	return false;
4042 
4043       mips_elf_hash_table (info)->use_rld_obj_head = true;
4044     }
4045 
4046   /* If this is a mips16 text symbol, add 1 to the value to make it
4047      odd.  This will cause something like .word SYM to come up with
4048      the right value when it is loaded into the PC.  */
4049   if (sym->st_other == STO_MIPS16)
4050     ++*valp;
4051 
4052   return true;
4053 }
4054 
4055 /* Structure used to pass information to mips_elf_output_extsym.  */
4056 
4057 struct extsym_info
4058 {
4059   bfd *abfd;
4060   struct bfd_link_info *info;
4061   struct ecoff_debug_info *debug;
4062   const struct ecoff_debug_swap *swap;
4063   boolean failed;
4064 };
4065 
4066 /* This routine is used to write out ECOFF debugging external symbol
4067    information.  It is called via mips_elf_link_hash_traverse.  The
4068    ECOFF external symbol information must match the ELF external
4069    symbol information.  Unfortunately, at this point we don't know
4070    whether a symbol is required by reloc information, so the two
4071    tables may wind up being different.  We must sort out the external
4072    symbol information before we can set the final size of the .mdebug
4073    section, and we must set the size of the .mdebug section before we
4074    can relocate any sections, and we can't know which symbols are
4075    required by relocation until we relocate the sections.
4076    Fortunately, it is relatively unlikely that any symbol will be
4077    stripped but required by a reloc.  In particular, it can not happen
4078    when generating a final executable.  */
4079 
4080 static boolean
4081 mips_elf_output_extsym (h, data)
4082      struct mips_elf_link_hash_entry *h;
4083      PTR data;
4084 {
4085   struct extsym_info *einfo = (struct extsym_info *) data;
4086   boolean strip;
4087   asection *sec, *output_section;
4088 
4089   if (h->root.indx == -2)
4090     strip = false;
4091   else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4092 	    || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4093 	   && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4094 	   && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4095     strip = true;
4096   else if (einfo->info->strip == strip_all
4097 	   || (einfo->info->strip == strip_some
4098 	       && bfd_hash_lookup (einfo->info->keep_hash,
4099 				   h->root.root.root.string,
4100 				   false, false) == NULL))
4101     strip = true;
4102   else
4103     strip = false;
4104 
4105   if (strip)
4106     return true;
4107 
4108   if (h->esym.ifd == -2)
4109     {
4110       h->esym.jmptbl = 0;
4111       h->esym.cobol_main = 0;
4112       h->esym.weakext = 0;
4113       h->esym.reserved = 0;
4114       h->esym.ifd = ifdNil;
4115       h->esym.asym.value = 0;
4116       h->esym.asym.st = stGlobal;
4117 
4118       if (SGI_COMPAT (einfo->abfd)
4119 	  && (h->root.root.type == bfd_link_hash_undefined
4120 	      || h->root.root.type == bfd_link_hash_undefweak))
4121 	{
4122 	  const char *name;
4123 
4124 	  /* Use undefined class.  Also, set class and type for some
4125              special symbols.  */
4126 	  name = h->root.root.root.string;
4127 	  if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
4128 	      || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
4129 	    {
4130 	      h->esym.asym.sc = scData;
4131 	      h->esym.asym.st = stLabel;
4132 	      h->esym.asym.value = 0;
4133 	    }
4134 	  else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
4135 	    {
4136 	      h->esym.asym.sc = scAbs;
4137 	      h->esym.asym.st = stLabel;
4138 	      h->esym.asym.value =
4139 		mips_elf_hash_table (einfo->info)->procedure_count;
4140 	    }
4141 	  else if (strcmp (name, "_gp_disp") == 0)
4142 	    {
4143 	      h->esym.asym.sc = scAbs;
4144 	      h->esym.asym.st = stLabel;
4145 	      h->esym.asym.value = elf_gp (einfo->abfd);
4146 	    }
4147 	  else
4148 	    h->esym.asym.sc = scUndefined;
4149 	}
4150       else if (h->root.root.type != bfd_link_hash_defined
4151 	  && h->root.root.type != bfd_link_hash_defweak)
4152 	h->esym.asym.sc = scAbs;
4153       else
4154 	{
4155 	  const char *name;
4156 
4157 	  sec = h->root.root.u.def.section;
4158 	  output_section = sec->output_section;
4159 
4160 	  /* When making a shared library and symbol h is the one from
4161 	     the another shared library, OUTPUT_SECTION may be null.  */
4162 	  if (output_section == NULL)
4163 	    h->esym.asym.sc = scUndefined;
4164 	  else
4165 	    {
4166 	      name = bfd_section_name (output_section->owner, output_section);
4167 
4168 	      if (strcmp (name, ".text") == 0)
4169 		h->esym.asym.sc = scText;
4170 	      else if (strcmp (name, ".data") == 0)
4171 		h->esym.asym.sc = scData;
4172 	      else if (strcmp (name, ".sdata") == 0)
4173 		h->esym.asym.sc = scSData;
4174 	      else if (strcmp (name, ".rodata") == 0
4175 		       || strcmp (name, ".rdata") == 0)
4176 		h->esym.asym.sc = scRData;
4177 	      else if (strcmp (name, ".bss") == 0)
4178 		h->esym.asym.sc = scBss;
4179 	      else if (strcmp (name, ".sbss") == 0)
4180 		h->esym.asym.sc = scSBss;
4181 	      else if (strcmp (name, ".init") == 0)
4182 		h->esym.asym.sc = scInit;
4183 	      else if (strcmp (name, ".fini") == 0)
4184 		h->esym.asym.sc = scFini;
4185 	      else
4186 		h->esym.asym.sc = scAbs;
4187 	    }
4188 	}
4189 
4190       h->esym.asym.reserved = 0;
4191       h->esym.asym.index = indexNil;
4192     }
4193 
4194   if (h->root.root.type == bfd_link_hash_common)
4195     h->esym.asym.value = h->root.root.u.c.size;
4196   else if (h->root.root.type == bfd_link_hash_defined
4197 	   || h->root.root.type == bfd_link_hash_defweak)
4198     {
4199       if (h->esym.asym.sc == scCommon)
4200 	h->esym.asym.sc = scBss;
4201       else if (h->esym.asym.sc == scSCommon)
4202 	h->esym.asym.sc = scSBss;
4203 
4204       sec = h->root.root.u.def.section;
4205       output_section = sec->output_section;
4206       if (output_section != NULL)
4207 	h->esym.asym.value = (h->root.root.u.def.value
4208 			      + sec->output_offset
4209 			      + output_section->vma);
4210       else
4211 	h->esym.asym.value = 0;
4212     }
4213   else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4214     {
4215       /* Set type and value for a symbol with a function stub.  */
4216       h->esym.asym.st = stProc;
4217       sec = h->root.root.u.def.section;
4218       if (sec == NULL)
4219 	h->esym.asym.value = 0;
4220       else
4221 	{
4222 	  output_section = sec->output_section;
4223 	  if (output_section != NULL)
4224 	    h->esym.asym.value = (h->root.plt.offset
4225 				  + sec->output_offset
4226 				  + output_section->vma);
4227 	  else
4228 	    h->esym.asym.value = 0;
4229 	}
4230 #if 0 /* FIXME?  */
4231       h->esym.ifd = 0;
4232 #endif
4233     }
4234 
4235   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
4236 				      h->root.root.root.string,
4237 				      &h->esym))
4238     {
4239       einfo->failed = true;
4240       return false;
4241     }
4242 
4243   return true;
4244 }
4245 
4246 /* Create a runtime procedure table from the .mdebug section.  */
4247 
4248 static boolean
4249 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
4250      PTR handle;
4251      bfd *abfd;
4252      struct bfd_link_info *info;
4253      asection *s;
4254      struct ecoff_debug_info *debug;
4255 {
4256   const struct ecoff_debug_swap *swap;
4257   HDRR *hdr = &debug->symbolic_header;
4258   RPDR *rpdr, *rp;
4259   struct rpdr_ext *erp;
4260   PTR rtproc;
4261   struct pdr_ext *epdr;
4262   struct sym_ext *esym;
4263   char *ss, **sv;
4264   char *str;
4265   unsigned long size, count;
4266   unsigned long sindex;
4267   unsigned long i;
4268   PDR pdr;
4269   SYMR sym;
4270   const char *no_name_func = _("static procedure (no name)");
4271 
4272   epdr = NULL;
4273   rpdr = NULL;
4274   esym = NULL;
4275   ss = NULL;
4276   sv = NULL;
4277 
4278   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4279 
4280   sindex = strlen (no_name_func) + 1;
4281   count = hdr->ipdMax;
4282   if (count > 0)
4283     {
4284       size = swap->external_pdr_size;
4285 
4286       epdr = (struct pdr_ext *) bfd_malloc (size * count);
4287       if (epdr == NULL)
4288 	goto error_return;
4289 
4290       if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
4291 	goto error_return;
4292 
4293       size = sizeof (RPDR);
4294       rp = rpdr = (RPDR *) bfd_malloc (size * count);
4295       if (rpdr == NULL)
4296 	goto error_return;
4297 
4298       sv = (char **) bfd_malloc (sizeof (char *) * count);
4299       if (sv == NULL)
4300 	goto error_return;
4301 
4302       count = hdr->isymMax;
4303       size = swap->external_sym_size;
4304       esym = (struct sym_ext *) bfd_malloc (size * count);
4305       if (esym == NULL)
4306 	goto error_return;
4307 
4308       if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
4309 	goto error_return;
4310 
4311       count = hdr->issMax;
4312       ss = (char *) bfd_malloc (count);
4313       if (ss == NULL)
4314 	goto error_return;
4315       if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
4316 	goto error_return;
4317 
4318       count = hdr->ipdMax;
4319       for (i = 0; i < count; i++, rp++)
4320 	{
4321 	  (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
4322 	  (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
4323 	  rp->adr = sym.value;
4324 	  rp->regmask = pdr.regmask;
4325 	  rp->regoffset = pdr.regoffset;
4326 	  rp->fregmask = pdr.fregmask;
4327 	  rp->fregoffset = pdr.fregoffset;
4328 	  rp->frameoffset = pdr.frameoffset;
4329 	  rp->framereg = pdr.framereg;
4330 	  rp->pcreg = pdr.pcreg;
4331 	  rp->irpss = sindex;
4332 	  sv[i] = ss + sym.iss;
4333 	  sindex += strlen (sv[i]) + 1;
4334 	}
4335     }
4336 
4337   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
4338   size = BFD_ALIGN (size, 16);
4339   rtproc = (PTR) bfd_alloc (abfd, size);
4340   if (rtproc == NULL)
4341     {
4342       mips_elf_hash_table (info)->procedure_count = 0;
4343       goto error_return;
4344     }
4345 
4346   mips_elf_hash_table (info)->procedure_count = count + 2;
4347 
4348   erp = (struct rpdr_ext *) rtproc;
4349   memset (erp, 0, sizeof (struct rpdr_ext));
4350   erp++;
4351   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
4352   strcpy (str, no_name_func);
4353   str += strlen (no_name_func) + 1;
4354   for (i = 0; i < count; i++)
4355     {
4356       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
4357       strcpy (str, sv[i]);
4358       str += strlen (sv[i]) + 1;
4359     }
4360   ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
4361 
4362   /* Set the size and contents of .rtproc section.  */
4363   s->_raw_size = size;
4364   s->contents = (bfd_byte *) rtproc;
4365 
4366   /* Skip this section later on (I don't think this currently
4367      matters, but someday it might).  */
4368   s->link_order_head = (struct bfd_link_order *) NULL;
4369 
4370   if (epdr != NULL)
4371     free (epdr);
4372   if (rpdr != NULL)
4373     free (rpdr);
4374   if (esym != NULL)
4375     free (esym);
4376   if (ss != NULL)
4377     free (ss);
4378   if (sv != NULL)
4379     free (sv);
4380 
4381   return true;
4382 
4383  error_return:
4384   if (epdr != NULL)
4385     free (epdr);
4386   if (rpdr != NULL)
4387     free (rpdr);
4388   if (esym != NULL)
4389     free (esym);
4390   if (ss != NULL)
4391     free (ss);
4392   if (sv != NULL)
4393     free (sv);
4394   return false;
4395 }
4396 
4397 /* A comparison routine used to sort .gptab entries.  */
4398 
4399 static int
4400 gptab_compare (p1, p2)
4401      const PTR p1;
4402      const PTR p2;
4403 {
4404   const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
4405   const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
4406 
4407   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
4408 }
4409 
4410 /* We need to use a special link routine to handle the .reginfo and
4411    the .mdebug sections.  We need to merge all instances of these
4412    sections together, not write them all out sequentially.  */
4413 
4414 boolean
4415 _bfd_mips_elf_final_link (abfd, info)
4416      bfd *abfd;
4417      struct bfd_link_info *info;
4418 {
4419   asection **secpp;
4420   asection *o;
4421   struct bfd_link_order *p;
4422   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4423   asection *rtproc_sec;
4424   Elf32_RegInfo reginfo;
4425   struct ecoff_debug_info debug;
4426   const struct ecoff_debug_swap *swap
4427     = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4428   HDRR *symhdr = &debug.symbolic_header;
4429   PTR mdebug_handle = NULL;
4430 
4431   /* If all the things we linked together were PIC, but we're
4432      producing an executable (rather than a shared object), then the
4433      resulting file is CPIC (i.e., it calls PIC code.)  */
4434   if (!info->shared
4435       && !info->relocateable
4436       && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
4437     {
4438       elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
4439       elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
4440     }
4441 
4442   /* We'd carefully arranged the dynamic symbol indices, and then the
4443      generic size_dynamic_sections renumbered them out from under us.
4444      Rather than trying somehow to prevent the renumbering, just do
4445      the sort again.  */
4446   if (elf_hash_table (info)->dynamic_sections_created)
4447     {
4448       bfd *dynobj;
4449       asection *got;
4450       struct mips_got_info *g;
4451 
4452       /* When we resort, we must tell mips_elf_sort_hash_table what
4453 	 the lowest index it may use is.  That's the number of section
4454 	 symbols we're going to add.  The generic ELF linker only
4455 	 adds these symbols when building a shared object.  Note that
4456 	 we count the sections after (possibly) removing the .options
4457 	 section above.  */
4458       if (!mips_elf_sort_hash_table (info, (info->shared
4459 					    ? bfd_count_sections (abfd) + 1
4460 					    : 1)))
4461         return false;
4462 
4463       /* Make sure we didn't grow the global .got region.  */
4464       dynobj = elf_hash_table (info)->dynobj;
4465       got = bfd_get_section_by_name (dynobj, ".got");
4466       g = (struct mips_got_info *) elf_section_data (got)->tdata;
4467 
4468       if (g->global_gotsym != NULL)
4469 	BFD_ASSERT ((elf_hash_table (info)->dynsymcount
4470 		     - g->global_gotsym->dynindx)
4471 		    <= g->global_gotno);
4472     }
4473 
4474   /* On IRIX5, we omit the .options section.  On IRIX6, however, we
4475      include it, even though we don't process it quite right.  (Some
4476      entries are supposed to be merged.)  Empirically, we seem to be
4477      better off including it then not.  */
4478   if (IRIX_COMPAT (abfd) == ict_irix5)
4479     for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4480       {
4481 	if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
4482 	  {
4483 	    for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4484 	      if (p->type == bfd_indirect_link_order)
4485 		p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4486 	    (*secpp)->link_order_head = NULL;
4487 	    *secpp = (*secpp)->next;
4488 	    --abfd->section_count;
4489 
4490 	    break;
4491 	  }
4492       }
4493 
4494   /* Get a value for the GP register.  */
4495   if (elf_gp (abfd) == 0)
4496     {
4497       struct bfd_link_hash_entry *h;
4498 
4499       h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4500       if (h != (struct bfd_link_hash_entry *) NULL
4501 	  && h->type == bfd_link_hash_defined)
4502 	elf_gp (abfd) = (h->u.def.value
4503 			 + h->u.def.section->output_section->vma
4504 			 + h->u.def.section->output_offset);
4505       else if (info->relocateable)
4506 	{
4507 	  bfd_vma lo;
4508 
4509 	  /* Find the GP-relative section with the lowest offset.  */
4510 	  lo = (bfd_vma) -1;
4511 	  for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4512 	    if (o->vma < lo
4513 		&& (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
4514 	      lo = o->vma;
4515 
4516 	  /* And calculate GP relative to that.  */
4517 	  elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4518 	}
4519       else
4520 	{
4521 	  /* If the relocate_section function needs to do a reloc
4522 	     involving the GP value, it should make a reloc_dangerous
4523 	     callback to warn that GP is not defined.  */
4524 	}
4525     }
4526 
4527   /* Go through the sections and collect the .reginfo and .mdebug
4528      information.  */
4529   reginfo_sec = NULL;
4530   mdebug_sec = NULL;
4531   gptab_data_sec = NULL;
4532   gptab_bss_sec = NULL;
4533   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4534     {
4535       if (strcmp (o->name, ".reginfo") == 0)
4536 	{
4537 	  memset (&reginfo, 0, sizeof reginfo);
4538 
4539 	  /* We have found the .reginfo section in the output file.
4540 	     Look through all the link_orders comprising it and merge
4541 	     the information together.  */
4542 	  for (p = o->link_order_head;
4543 	       p != (struct bfd_link_order *) NULL;
4544 	       p = p->next)
4545 	    {
4546 	      asection *input_section;
4547 	      bfd *input_bfd;
4548 	      Elf32_External_RegInfo ext;
4549 	      Elf32_RegInfo sub;
4550 
4551 	      if (p->type != bfd_indirect_link_order)
4552 		{
4553 		  if (p->type == bfd_fill_link_order)
4554 		    continue;
4555 		  abort ();
4556 		}
4557 
4558 	      input_section = p->u.indirect.section;
4559 	      input_bfd = input_section->owner;
4560 
4561 	      /* The linker emulation code has probably clobbered the
4562                  size to be zero bytes.  */
4563 	      if (input_section->_raw_size == 0)
4564 		input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4565 
4566 	      if (! bfd_get_section_contents (input_bfd, input_section,
4567 					      (PTR) &ext,
4568 					      (file_ptr) 0,
4569 					      sizeof ext))
4570 		return false;
4571 
4572 	      bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4573 
4574 	      reginfo.ri_gprmask |= sub.ri_gprmask;
4575 	      reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4576 	      reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4577 	      reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4578 	      reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4579 
4580 	      /* ri_gp_value is set by the function
4581 		 mips_elf32_section_processing when the section is
4582 		 finally written out.  */
4583 
4584 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
4585 		 elf_link_input_bfd ignores this section.  */
4586 	      input_section->flags &=~ SEC_HAS_CONTENTS;
4587 	    }
4588 
4589 	  /* Size has been set in mips_elf_always_size_sections  */
4590 	  BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4591 
4592 	  /* Skip this section later on (I don't think this currently
4593 	     matters, but someday it might).  */
4594 	  o->link_order_head = (struct bfd_link_order *) NULL;
4595 
4596 	  reginfo_sec = o;
4597 	}
4598 
4599       if (strcmp (o->name, ".mdebug") == 0)
4600 	{
4601 	  struct extsym_info einfo;
4602 
4603 	  /* We have found the .mdebug section in the output file.
4604 	     Look through all the link_orders comprising it and merge
4605 	     the information together.  */
4606 	  symhdr->magic = swap->sym_magic;
4607 	  /* FIXME: What should the version stamp be?  */
4608 	  symhdr->vstamp = 0;
4609 	  symhdr->ilineMax = 0;
4610 	  symhdr->cbLine = 0;
4611 	  symhdr->idnMax = 0;
4612 	  symhdr->ipdMax = 0;
4613 	  symhdr->isymMax = 0;
4614 	  symhdr->ioptMax = 0;
4615 	  symhdr->iauxMax = 0;
4616 	  symhdr->issMax = 0;
4617 	  symhdr->issExtMax = 0;
4618 	  symhdr->ifdMax = 0;
4619 	  symhdr->crfd = 0;
4620 	  symhdr->iextMax = 0;
4621 
4622 	  /* We accumulate the debugging information itself in the
4623 	     debug_info structure.  */
4624 	  debug.line = NULL;
4625 	  debug.external_dnr = NULL;
4626 	  debug.external_pdr = NULL;
4627 	  debug.external_sym = NULL;
4628 	  debug.external_opt = NULL;
4629 	  debug.external_aux = NULL;
4630 	  debug.ss = NULL;
4631 	  debug.ssext = debug.ssext_end = NULL;
4632 	  debug.external_fdr = NULL;
4633 	  debug.external_rfd = NULL;
4634 	  debug.external_ext = debug.external_ext_end = NULL;
4635 
4636 	  mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4637 	  if (mdebug_handle == (PTR) NULL)
4638 	    return false;
4639 
4640 	  if (SGI_COMPAT (abfd))
4641 	    {
4642 	      asection *s;
4643 	      EXTR esym;
4644 	      bfd_vma last;
4645 	      unsigned int i;
4646 	      static const char * const name[] =
4647 		{ ".text", ".init", ".fini", ".data",
4648 		    ".rodata", ".sdata", ".sbss", ".bss" };
4649 	      static const int sc[] = { scText, scInit, scFini, scData,
4650 					  scRData, scSData, scSBss, scBss };
4651 
4652 	      esym.jmptbl = 0;
4653 	      esym.cobol_main = 0;
4654 	      esym.weakext = 0;
4655 	      esym.reserved = 0;
4656 	      esym.ifd = ifdNil;
4657 	      esym.asym.iss = issNil;
4658 	      esym.asym.st = stLocal;
4659 	      esym.asym.reserved = 0;
4660 	      esym.asym.index = indexNil;
4661 	      last = 0;
4662 	      for (i = 0; i < 8; i++)
4663 		{
4664 		  esym.asym.sc = sc[i];
4665 		  s = bfd_get_section_by_name (abfd, name[i]);
4666 		  if (s != NULL)
4667 		    {
4668 		      esym.asym.value = s->vma;
4669 		      last = s->vma + s->_raw_size;
4670 		    }
4671 		  else
4672 		    esym.asym.value = last;
4673 
4674 		  if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4675 						      name[i], &esym))
4676 		    return false;
4677 		}
4678 	    }
4679 
4680 	  for (p = o->link_order_head;
4681 	       p != (struct bfd_link_order *) NULL;
4682 	       p = p->next)
4683 	    {
4684 	      asection *input_section;
4685 	      bfd *input_bfd;
4686 	      const struct ecoff_debug_swap *input_swap;
4687 	      struct ecoff_debug_info input_debug;
4688 	      char *eraw_src;
4689 	      char *eraw_end;
4690 
4691 	      if (p->type != bfd_indirect_link_order)
4692 		{
4693 		  if (p->type == bfd_fill_link_order)
4694 		    continue;
4695 		  abort ();
4696 		}
4697 
4698 	      input_section = p->u.indirect.section;
4699 	      input_bfd = input_section->owner;
4700 
4701 	      if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4702 		  || (get_elf_backend_data (input_bfd)
4703 		      ->elf_backend_ecoff_debug_swap) == NULL)
4704 		{
4705 		  /* I don't know what a non MIPS ELF bfd would be
4706 		     doing with a .mdebug section, but I don't really
4707 		     want to deal with it.  */
4708 		  continue;
4709 		}
4710 
4711 	      input_swap = (get_elf_backend_data (input_bfd)
4712 			    ->elf_backend_ecoff_debug_swap);
4713 
4714 	      BFD_ASSERT (p->size == input_section->_raw_size);
4715 
4716 	      /* The ECOFF linking code expects that we have already
4717 		 read in the debugging information and set up an
4718 		 ecoff_debug_info structure, so we do that now.  */
4719 	      if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4720 						   &input_debug))
4721 		return false;
4722 
4723 	      if (! (bfd_ecoff_debug_accumulate
4724 		     (mdebug_handle, abfd, &debug, swap, input_bfd,
4725 		      &input_debug, input_swap, info)))
4726 		return false;
4727 
4728 	      /* Loop through the external symbols.  For each one with
4729 		 interesting information, try to find the symbol in
4730 		 the linker global hash table and save the information
4731 		 for the output external symbols.  */
4732 	      eraw_src = input_debug.external_ext;
4733 	      eraw_end = (eraw_src
4734 			  + (input_debug.symbolic_header.iextMax
4735 			     * input_swap->external_ext_size));
4736 	      for (;
4737 		   eraw_src < eraw_end;
4738 		   eraw_src += input_swap->external_ext_size)
4739 		{
4740 		  EXTR ext;
4741 		  const char *name;
4742 		  struct mips_elf_link_hash_entry *h;
4743 
4744 		  (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4745 		  if (ext.asym.sc == scNil
4746 		      || ext.asym.sc == scUndefined
4747 		      || ext.asym.sc == scSUndefined)
4748 		    continue;
4749 
4750 		  name = input_debug.ssext + ext.asym.iss;
4751 		  h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4752 						 name, false, false, true);
4753 		  if (h == NULL || h->esym.ifd != -2)
4754 		    continue;
4755 
4756 		  if (ext.ifd != -1)
4757 		    {
4758 		      BFD_ASSERT (ext.ifd
4759 				  < input_debug.symbolic_header.ifdMax);
4760 		      ext.ifd = input_debug.ifdmap[ext.ifd];
4761 		    }
4762 
4763 		  h->esym = ext;
4764 		}
4765 
4766 	      /* Free up the information we just read.  */
4767 	      free (input_debug.line);
4768 	      free (input_debug.external_dnr);
4769 	      free (input_debug.external_pdr);
4770 	      free (input_debug.external_sym);
4771 	      free (input_debug.external_opt);
4772 	      free (input_debug.external_aux);
4773 	      free (input_debug.ss);
4774 	      free (input_debug.ssext);
4775 	      free (input_debug.external_fdr);
4776 	      free (input_debug.external_rfd);
4777 	      free (input_debug.external_ext);
4778 
4779 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
4780 		 elf_link_input_bfd ignores this section.  */
4781 	      input_section->flags &=~ SEC_HAS_CONTENTS;
4782 	    }
4783 
4784 	  if (SGI_COMPAT (abfd) && info->shared)
4785 	    {
4786 	      /* Create .rtproc section.  */
4787 	      rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4788 	      if (rtproc_sec == NULL)
4789 		{
4790 		  flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4791 				    | SEC_LINKER_CREATED | SEC_READONLY);
4792 
4793 		  rtproc_sec = bfd_make_section (abfd, ".rtproc");
4794 		  if (rtproc_sec == NULL
4795 		      || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4796 		      || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4797 		    return false;
4798 		}
4799 
4800 	      if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4801 						     info, rtproc_sec, &debug))
4802 		return false;
4803 	    }
4804 
4805 	  /* Build the external symbol information.  */
4806 	  einfo.abfd = abfd;
4807 	  einfo.info = info;
4808 	  einfo.debug = &debug;
4809 	  einfo.swap = swap;
4810 	  einfo.failed = false;
4811 	  mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4812 				       mips_elf_output_extsym,
4813 				       (PTR) &einfo);
4814 	  if (einfo.failed)
4815 	    return false;
4816 
4817 	  /* Set the size of the .mdebug section.  */
4818 	  o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4819 
4820 	  /* Skip this section later on (I don't think this currently
4821 	     matters, but someday it might).  */
4822 	  o->link_order_head = (struct bfd_link_order *) NULL;
4823 
4824 	  mdebug_sec = o;
4825 	}
4826 
4827       if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4828 	{
4829 	  const char *subname;
4830 	  unsigned int c;
4831 	  Elf32_gptab *tab;
4832 	  Elf32_External_gptab *ext_tab;
4833 	  unsigned int i;
4834 
4835 	  /* The .gptab.sdata and .gptab.sbss sections hold
4836 	     information describing how the small data area would
4837 	     change depending upon the -G switch.  These sections
4838 	     not used in executables files.  */
4839 	  if (! info->relocateable)
4840 	    {
4841 	      asection **secpp;
4842 
4843 	      for (p = o->link_order_head;
4844 		   p != (struct bfd_link_order *) NULL;
4845 		   p = p->next)
4846 		{
4847 		  asection *input_section;
4848 
4849 		  if (p->type != bfd_indirect_link_order)
4850 		    {
4851 		      if (p->type == bfd_fill_link_order)
4852 			continue;
4853 		      abort ();
4854 		    }
4855 
4856 		  input_section = p->u.indirect.section;
4857 
4858 		  /* Hack: reset the SEC_HAS_CONTENTS flag so that
4859 		     elf_link_input_bfd ignores this section.  */
4860 		  input_section->flags &=~ SEC_HAS_CONTENTS;
4861 		}
4862 
4863 	      /* Skip this section later on (I don't think this
4864 		 currently matters, but someday it might).  */
4865 	      o->link_order_head = (struct bfd_link_order *) NULL;
4866 
4867 	      /* Really remove the section.  */
4868 	      for (secpp = &abfd->sections;
4869 		   *secpp != o;
4870 		   secpp = &(*secpp)->next)
4871 		;
4872 	      *secpp = (*secpp)->next;
4873 	      --abfd->section_count;
4874 
4875 	      continue;
4876 	    }
4877 
4878 	  /* There is one gptab for initialized data, and one for
4879 	     uninitialized data.  */
4880 	  if (strcmp (o->name, ".gptab.sdata") == 0)
4881 	    gptab_data_sec = o;
4882 	  else if (strcmp (o->name, ".gptab.sbss") == 0)
4883 	    gptab_bss_sec = o;
4884 	  else
4885 	    {
4886 	      (*_bfd_error_handler)
4887 		(_("%s: illegal section name `%s'"),
4888 		 bfd_get_filename (abfd), o->name);
4889 	      bfd_set_error (bfd_error_nonrepresentable_section);
4890 	      return false;
4891 	    }
4892 
4893 	  /* The linker script always combines .gptab.data and
4894 	     .gptab.sdata into .gptab.sdata, and likewise for
4895 	     .gptab.bss and .gptab.sbss.  It is possible that there is
4896 	     no .sdata or .sbss section in the output file, in which
4897 	     case we must change the name of the output section.  */
4898 	  subname = o->name + sizeof ".gptab" - 1;
4899 	  if (bfd_get_section_by_name (abfd, subname) == NULL)
4900 	    {
4901 	      if (o == gptab_data_sec)
4902 		o->name = ".gptab.data";
4903 	      else
4904 		o->name = ".gptab.bss";
4905 	      subname = o->name + sizeof ".gptab" - 1;
4906 	      BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4907 	    }
4908 
4909 	  /* Set up the first entry.  */
4910 	  c = 1;
4911 	  tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4912 	  if (tab == NULL)
4913 	    return false;
4914 	  tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4915 	  tab[0].gt_header.gt_unused = 0;
4916 
4917 	  /* Combine the input sections.  */
4918 	  for (p = o->link_order_head;
4919 	       p != (struct bfd_link_order *) NULL;
4920 	       p = p->next)
4921 	    {
4922 	      asection *input_section;
4923 	      bfd *input_bfd;
4924 	      bfd_size_type size;
4925 	      unsigned long last;
4926 	      bfd_size_type gpentry;
4927 
4928 	      if (p->type != bfd_indirect_link_order)
4929 		{
4930 		  if (p->type == bfd_fill_link_order)
4931 		    continue;
4932 		  abort ();
4933 		}
4934 
4935 	      input_section = p->u.indirect.section;
4936 	      input_bfd = input_section->owner;
4937 
4938 	      /* Combine the gptab entries for this input section one
4939 		 by one.  We know that the input gptab entries are
4940 		 sorted by ascending -G value.  */
4941 	      size = bfd_section_size (input_bfd, input_section);
4942 	      last = 0;
4943 	      for (gpentry = sizeof (Elf32_External_gptab);
4944 		   gpentry < size;
4945 		   gpentry += sizeof (Elf32_External_gptab))
4946 		{
4947 		  Elf32_External_gptab ext_gptab;
4948 		  Elf32_gptab int_gptab;
4949 		  unsigned long val;
4950 		  unsigned long add;
4951 		  boolean exact;
4952 		  unsigned int look;
4953 
4954 		  if (! (bfd_get_section_contents
4955 			 (input_bfd, input_section, (PTR) &ext_gptab,
4956 			  gpentry, sizeof (Elf32_External_gptab))))
4957 		    {
4958 		      free (tab);
4959 		      return false;
4960 		    }
4961 
4962 		  bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4963 						&int_gptab);
4964 		  val = int_gptab.gt_entry.gt_g_value;
4965 		  add = int_gptab.gt_entry.gt_bytes - last;
4966 
4967 		  exact = false;
4968 		  for (look = 1; look < c; look++)
4969 		    {
4970 		      if (tab[look].gt_entry.gt_g_value >= val)
4971 			tab[look].gt_entry.gt_bytes += add;
4972 
4973 		      if (tab[look].gt_entry.gt_g_value == val)
4974 			exact = true;
4975 		    }
4976 
4977 		  if (! exact)
4978 		    {
4979 		      Elf32_gptab *new_tab;
4980 		      unsigned int max;
4981 
4982 		      /* We need a new table entry.  */
4983 		      new_tab = ((Elf32_gptab *)
4984 				 bfd_realloc ((PTR) tab,
4985 					      (c + 1) * sizeof (Elf32_gptab)));
4986 		      if (new_tab == NULL)
4987 			{
4988 			  free (tab);
4989 			  return false;
4990 			}
4991 		      tab = new_tab;
4992 		      tab[c].gt_entry.gt_g_value = val;
4993 		      tab[c].gt_entry.gt_bytes = add;
4994 
4995 		      /* Merge in the size for the next smallest -G
4996 			 value, since that will be implied by this new
4997 			 value.  */
4998 		      max = 0;
4999 		      for (look = 1; look < c; look++)
5000 			{
5001 			  if (tab[look].gt_entry.gt_g_value < val
5002 			      && (max == 0
5003 				  || (tab[look].gt_entry.gt_g_value
5004 				      > tab[max].gt_entry.gt_g_value)))
5005 			    max = look;
5006 			}
5007 		      if (max != 0)
5008 			tab[c].gt_entry.gt_bytes +=
5009 			  tab[max].gt_entry.gt_bytes;
5010 
5011 		      ++c;
5012 		    }
5013 
5014 		  last = int_gptab.gt_entry.gt_bytes;
5015 		}
5016 
5017 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
5018 		 elf_link_input_bfd ignores this section.  */
5019 	      input_section->flags &=~ SEC_HAS_CONTENTS;
5020 	    }
5021 
5022 	  /* The table must be sorted by -G value.  */
5023 	  if (c > 2)
5024 	    qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
5025 
5026 	  /* Swap out the table.  */
5027 	  ext_tab = ((Elf32_External_gptab *)
5028 		     bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
5029 	  if (ext_tab == NULL)
5030 	    {
5031 	      free (tab);
5032 	      return false;
5033 	    }
5034 
5035 	  for (i = 0; i < c; i++)
5036 	    bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
5037 	  free (tab);
5038 
5039 	  o->_raw_size = c * sizeof (Elf32_External_gptab);
5040 	  o->contents = (bfd_byte *) ext_tab;
5041 
5042 	  /* Skip this section later on (I don't think this currently
5043 	     matters, but someday it might).  */
5044 	  o->link_order_head = (struct bfd_link_order *) NULL;
5045 	}
5046     }
5047 
5048   /* Invoke the regular ELF backend linker to do all the work.  */
5049   if (ABI_64_P (abfd))
5050     {
5051 #ifdef BFD64
5052       if (!bfd_elf64_bfd_final_link (abfd, info))
5053 	return false;
5054 #else
5055       abort ();
5056       return false;
5057 #endif /* BFD64 */
5058     }
5059   else if (!bfd_elf32_bfd_final_link (abfd, info))
5060     return false;
5061 
5062   /* Now write out the computed sections.  */
5063 
5064   if (reginfo_sec != (asection *) NULL)
5065     {
5066       Elf32_External_RegInfo ext;
5067 
5068       bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
5069       if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
5070 				      (file_ptr) 0, sizeof ext))
5071 	return false;
5072     }
5073 
5074   if (mdebug_sec != (asection *) NULL)
5075     {
5076       BFD_ASSERT (abfd->output_has_begun);
5077       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
5078 					       swap, info,
5079 					       mdebug_sec->filepos))
5080 	return false;
5081 
5082       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
5083     }
5084 
5085   if (gptab_data_sec != (asection *) NULL)
5086     {
5087       if (! bfd_set_section_contents (abfd, gptab_data_sec,
5088 				      gptab_data_sec->contents,
5089 				      (file_ptr) 0,
5090 				      gptab_data_sec->_raw_size))
5091 	return false;
5092     }
5093 
5094   if (gptab_bss_sec != (asection *) NULL)
5095     {
5096       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
5097 				      gptab_bss_sec->contents,
5098 				      (file_ptr) 0,
5099 				      gptab_bss_sec->_raw_size))
5100 	return false;
5101     }
5102 
5103   if (SGI_COMPAT (abfd))
5104     {
5105       rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
5106       if (rtproc_sec != NULL)
5107 	{
5108 	  if (! bfd_set_section_contents (abfd, rtproc_sec,
5109 					  rtproc_sec->contents,
5110 					  (file_ptr) 0,
5111 					  rtproc_sec->_raw_size))
5112 	    return false;
5113 	}
5114     }
5115 
5116   return true;
5117 }
5118 
5119 /* Returns the GOT section for ABFD.  */
5120 
5121 static asection *
5122 mips_elf_got_section (abfd)
5123      bfd *abfd;
5124 {
5125   return bfd_get_section_by_name (abfd, ".got");
5126 }
5127 
5128 /* Returns the GOT information associated with the link indicated by
5129    INFO.  If SGOTP is non-NULL, it is filled in with the GOT
5130    section.  */
5131 
5132 static struct mips_got_info *
5133 mips_elf_got_info (abfd, sgotp)
5134      bfd *abfd;
5135      asection **sgotp;
5136 {
5137   asection *sgot;
5138   struct mips_got_info *g;
5139 
5140   sgot = mips_elf_got_section (abfd);
5141   BFD_ASSERT (sgot != NULL);
5142   BFD_ASSERT (elf_section_data (sgot) != NULL);
5143   g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5144   BFD_ASSERT (g != NULL);
5145 
5146   if (sgotp)
5147     *sgotp = sgot;
5148   return g;
5149 }
5150 
5151 /* Return whether a relocation is against a local symbol.  */
5152 
5153 static boolean
5154 mips_elf_local_relocation_p (input_bfd, relocation, local_sections)
5155      bfd *input_bfd;
5156      const Elf_Internal_Rela *relocation;
5157      asection **local_sections;
5158 {
5159   unsigned long r_symndx;
5160   Elf_Internal_Shdr *symtab_hdr;
5161 
5162   r_symndx = ELF32_R_SYM (relocation->r_info);
5163   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5164   if (! elf_bad_symtab (input_bfd))
5165     return r_symndx < symtab_hdr->sh_info;
5166   else
5167     {
5168       /* The symbol table does not follow the rule that local symbols
5169 	 must come before globals.  */
5170       return local_sections[r_symndx] != NULL;
5171     }
5172 }
5173 
5174 /* Sign-extend VALUE, which has the indicated number of BITS.  */
5175 
5176 static bfd_vma
5177 mips_elf_sign_extend (value, bits)
5178      bfd_vma value;
5179      int bits;
5180 {
5181   if (value & ((bfd_vma)1 << (bits - 1)))
5182     /* VALUE is negative.  */
5183     value |= ((bfd_vma) - 1) << bits;
5184 
5185   return value;
5186 }
5187 
5188 /* Return non-zero if the indicated VALUE has overflowed the maximum
5189    range expressable by a signed number with the indicated number of
5190    BITS.  */
5191 
5192 static boolean
5193 mips_elf_overflow_p (value, bits)
5194      bfd_vma value;
5195      int bits;
5196 {
5197   bfd_signed_vma svalue = (bfd_signed_vma) value;
5198 
5199   if (svalue > (1 << (bits - 1)) - 1)
5200     /* The value is too big.  */
5201     return true;
5202   else if (svalue < -(1 << (bits - 1)))
5203     /* The value is too small.  */
5204     return true;
5205 
5206   /* All is well.  */
5207   return false;
5208 }
5209 
5210 /* Calculate the %high function.  */
5211 
5212 static bfd_vma
5213 mips_elf_high (value)
5214      bfd_vma value;
5215 {
5216   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5217 }
5218 
5219 /* Calculate the %higher function.  */
5220 
5221 static bfd_vma
5222 mips_elf_higher (value)
5223      bfd_vma value ATTRIBUTE_UNUSED;
5224 {
5225 #ifdef BFD64
5226   return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5227 #else
5228   abort ();
5229   return (bfd_vma) -1;
5230 #endif
5231 }
5232 
5233 /* Calculate the %highest function.  */
5234 
5235 static bfd_vma
5236 mips_elf_highest (value)
5237      bfd_vma value ATTRIBUTE_UNUSED;
5238 {
5239 #ifdef BFD64
5240   return ((value + (bfd_vma) 0x800080008000) >> 48) & 0xffff;
5241 #else
5242   abort ();
5243   return (bfd_vma) -1;
5244 #endif
5245 }
5246 
5247 /* Returns the GOT index for the global symbol indicated by H.  */
5248 
5249 static bfd_vma
5250 mips_elf_global_got_index (abfd, h)
5251      bfd *abfd;
5252      struct elf_link_hash_entry *h;
5253 {
5254   bfd_vma index;
5255   asection *sgot;
5256   struct mips_got_info *g;
5257 
5258   g = mips_elf_got_info (abfd, &sgot);
5259 
5260   /* Once we determine the global GOT entry with the lowest dynamic
5261      symbol table index, we must put all dynamic symbols with greater
5262      indices into the GOT.  That makes it easy to calculate the GOT
5263      offset.  */
5264   BFD_ASSERT (h->dynindx >= g->global_gotsym->dynindx);
5265   index = ((h->dynindx - g->global_gotsym->dynindx + g->local_gotno)
5266 	   * MIPS_ELF_GOT_SIZE (abfd));
5267   BFD_ASSERT (index < sgot->_raw_size);
5268 
5269   return index;
5270 }
5271 
5272 /* Returns the offset for the entry at the INDEXth position
5273    in the GOT.  */
5274 
5275 static bfd_vma
5276 mips_elf_got_offset_from_index (dynobj, output_bfd, index)
5277      bfd *dynobj;
5278      bfd *output_bfd;
5279      bfd_vma index;
5280 {
5281   asection *sgot;
5282   bfd_vma gp;
5283 
5284   sgot = mips_elf_got_section (dynobj);
5285   gp = _bfd_get_gp_value (output_bfd);
5286   return (sgot->output_section->vma + sgot->output_offset + index -
5287 	  gp);
5288 }
5289 
5290 /* If H is a symbol that needs a global GOT entry, but has a dynamic
5291    symbol table index lower than any we've seen to date, record it for
5292    posterity.  */
5293 
5294 static boolean
5295 mips_elf_record_global_got_symbol (h, info, g)
5296      struct elf_link_hash_entry *h;
5297      struct bfd_link_info *info;
5298      struct mips_got_info *g ATTRIBUTE_UNUSED;
5299 {
5300   /* A global symbol in the GOT must also be in the dynamic symbol
5301      table.  */
5302   if (h->dynindx == -1
5303       && !bfd_elf32_link_record_dynamic_symbol (info, h))
5304     return false;
5305 
5306   /* If we've already marked this entry as need GOT space, we don't
5307      need to do it again.  */
5308   if (h->got.offset != (bfd_vma) - 1)
5309     return true;
5310 
5311   /* By setting this to a value other than -1, we are indicating that
5312      there needs to be a GOT entry for H.  */
5313   h->got.offset = 0;
5314 
5315   return true;
5316 }
5317 
5318 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
5319    the dynamic symbols.  */
5320 
5321 struct mips_elf_hash_sort_data
5322 {
5323   /* The symbol in the global GOT with the lowest dynamic symbol table
5324      index.  */
5325   struct elf_link_hash_entry *low;
5326   /* The least dynamic symbol table index corresponding to a symbol
5327      with a GOT entry.  */
5328   long min_got_dynindx;
5329   /* The greatest dynamic symbol table index not corresponding to a
5330      symbol without a GOT entry.  */
5331   long max_non_got_dynindx;
5332 };
5333 
5334 /* If H needs a GOT entry, assign it the highest available dynamic
5335    index.  Otherwise, assign it the lowest available dynamic
5336    index.  */
5337 
5338 static boolean
5339 mips_elf_sort_hash_table_f (h, data)
5340      struct mips_elf_link_hash_entry *h;
5341      PTR data;
5342 {
5343   struct mips_elf_hash_sort_data *hsd
5344     = (struct mips_elf_hash_sort_data *) data;
5345 
5346   /* Symbols without dynamic symbol table entries aren't interesting
5347      at all.  */
5348   if (h->root.dynindx == -1)
5349     return true;
5350 
5351   if (h->root.got.offset != 0)
5352     h->root.dynindx = hsd->max_non_got_dynindx++;
5353   else
5354     {
5355       h->root.dynindx = --hsd->min_got_dynindx;
5356       hsd->low = (struct elf_link_hash_entry *) h;
5357     }
5358 
5359   return true;
5360 }
5361 
5362 /* Sort the dynamic symbol table so that symbols that need GOT entries
5363    appear towards the end.  This reduces the amount of GOT space
5364    required.  MAX_LOCAL is used to set the number of local symbols
5365    known to be in the dynamic symbol table.  During
5366    mips_elf_size_dynamic_sections, this value is 1.  Afterward, the
5367    section symbols are added and the count is higher.  */
5368 
5369 static boolean
5370 mips_elf_sort_hash_table (info, max_local)
5371      struct bfd_link_info *info;
5372      unsigned long max_local;
5373 {
5374   struct mips_elf_hash_sort_data hsd;
5375   struct mips_got_info *g;
5376   bfd *dynobj;
5377 
5378   dynobj = elf_hash_table (info)->dynobj;
5379 
5380   hsd.low = NULL;
5381   hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount;
5382   hsd.max_non_got_dynindx = max_local;
5383   mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
5384 				elf_hash_table (info)),
5385 			       mips_elf_sort_hash_table_f,
5386 			       &hsd);
5387 
5388   /* There shoud have been enough room in the symbol table to
5389      accomodate both the GOT and non-GOT symbols.  */
5390   BFD_ASSERT (hsd.min_got_dynindx == hsd.max_non_got_dynindx);
5391 
5392   /* Now we know which dynamic symbol has the lowest dynamic symbol
5393      table index in the GOT.  */
5394   g = mips_elf_got_info (dynobj, NULL);
5395   g->global_gotsym = hsd.low;
5396 
5397   return true;
5398 }
5399 
5400 /* Create a local GOT entry for VALUE.  Return the index of the entry,
5401    or -1 if it could not be created.  */
5402 
5403 static bfd_vma
5404 mips_elf_create_local_got_entry (abfd, g, sgot, value)
5405      bfd *abfd;
5406      struct mips_got_info *g;
5407      asection *sgot;
5408      bfd_vma value;
5409 {
5410   if (g->assigned_gotno >= g->local_gotno)
5411     {
5412       /* We didn't allocate enough space in the GOT.  */
5413       (*_bfd_error_handler)
5414 	(_("not enough GOT space for local GOT entries"));
5415       bfd_set_error (bfd_error_bad_value);
5416       return (bfd_vma) -1;
5417     }
5418 
5419   MIPS_ELF_PUT_WORD (abfd, value,
5420 		     (sgot->contents
5421 		      + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno));
5422   return MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
5423 }
5424 
5425 /* Returns the GOT offset at which the indicated address can be found.
5426    If there is not yet a GOT entry for this value, create one.  Returns
5427    -1 if no satisfactory GOT offset can be found.  */
5428 
5429 static bfd_vma
5430 mips_elf_local_got_index (abfd, info, value)
5431      bfd *abfd;
5432      struct bfd_link_info *info;
5433      bfd_vma value;
5434 {
5435   asection *sgot;
5436   struct mips_got_info *g;
5437   bfd_byte *entry;
5438 
5439   g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5440 
5441   /* Look to see if we already have an appropriate entry.  */
5442   for (entry = (sgot->contents
5443 		+ MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5444        entry != sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5445        entry += MIPS_ELF_GOT_SIZE (abfd))
5446     {
5447       bfd_vma address = MIPS_ELF_GET_WORD (abfd, entry);
5448       if (address == value)
5449 	return entry - sgot->contents;
5450     }
5451 
5452   return mips_elf_create_local_got_entry (abfd, g, sgot, value);
5453 }
5454 
5455 /* Find a GOT entry that is within 32KB of the VALUE.  These entries
5456    are supposed to be placed at small offsets in the GOT, i.e.,
5457    within 32KB of GP.  Return the index into the GOT for this page,
5458    and store the offset from this entry to the desired address in
5459    OFFSETP, if it is non-NULL.  */
5460 
5461 static bfd_vma
5462 mips_elf_got_page (abfd, info, value, offsetp)
5463      bfd *abfd;
5464      struct bfd_link_info *info;
5465      bfd_vma value;
5466      bfd_vma *offsetp;
5467 {
5468   asection *sgot;
5469   struct mips_got_info *g;
5470   bfd_byte *entry;
5471   bfd_byte *last_entry;
5472   bfd_vma index = 0;
5473   bfd_vma address;
5474 
5475   g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5476 
5477   /* Look to see if we aleady have an appropriate entry.  */
5478   last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5479   for (entry = (sgot->contents
5480 		+ MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5481        entry != last_entry;
5482        entry += MIPS_ELF_GOT_SIZE (abfd))
5483     {
5484       address = MIPS_ELF_GET_WORD (abfd, entry);
5485 
5486       if (!mips_elf_overflow_p (value - address, 16))
5487 	{
5488 	  /* This entry will serve as the page pointer.  We can add a
5489 	     16-bit number to it to get the actual address.  */
5490 	  index = entry - sgot->contents;
5491 	  break;
5492 	}
5493     }
5494 
5495   /* If we didn't have an appropriate entry, we create one now.  */
5496   if (entry == last_entry)
5497     index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5498 
5499   if (offsetp)
5500     {
5501       address = MIPS_ELF_GET_WORD (abfd, entry);
5502       *offsetp = value - address;
5503     }
5504 
5505   return index;
5506 }
5507 
5508 /* Find a GOT entry whose higher-order 16 bits are the same as those
5509    for value.  Return the index into the GOT for this entry.  */
5510 
5511 static bfd_vma
5512 mips_elf_got16_entry (abfd, info, value)
5513      bfd *abfd;
5514      struct bfd_link_info *info;
5515      bfd_vma value;
5516 {
5517   asection *sgot;
5518   struct mips_got_info *g;
5519   bfd_byte *entry;
5520   bfd_byte *last_entry;
5521   bfd_vma index = 0;
5522   bfd_vma address;
5523 
5524   /* Although the ABI says that it is "the high-order 16 bits" that we
5525      want, it is really the %high value.  The complete value is
5526      calculated with a `addiu' of a LO16 relocation, just as with a
5527      HI16/LO16 pair.  */
5528   value = mips_elf_high (value) << 16;
5529   g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5530 
5531   /* Look to see if we already have an appropriate entry.  */
5532   last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5533   for (entry = (sgot->contents
5534 		+ MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5535        entry != last_entry;
5536        entry += MIPS_ELF_GOT_SIZE (abfd))
5537     {
5538       address = MIPS_ELF_GET_WORD (abfd, entry);
5539       if ((address & 0xffff0000) == value)
5540 	{
5541 	  /* This entry has the right high-order 16 bits.  */
5542 	  index = entry - sgot->contents;
5543 	  break;
5544 	}
5545     }
5546 
5547   /* If we didn't have an appropriate entry, we create one now.  */
5548   if (entry == last_entry)
5549     index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5550 
5551   return index;
5552 }
5553 
5554 /* Returns the first relocation of type r_type found, beginning with
5555    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
5556 
5557 static const Elf_Internal_Rela *
5558 mips_elf_next_relocation (r_type, relocation, relend)
5559      unsigned int r_type;
5560      const Elf_Internal_Rela *relocation;
5561      const Elf_Internal_Rela *relend;
5562 {
5563   /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be
5564      immediately following.  However, for the IRIX6 ABI, the next
5565      relocation may be a composed relocation consisting of several
5566      relocations for the same address.  In that case, the R_MIPS_LO16
5567      relocation may occur as one of these.  We permit a similar
5568      extension in general, as that is useful for GCC.  */
5569   while (relocation < relend)
5570     {
5571       if (ELF32_R_TYPE (relocation->r_info) == r_type)
5572 	return relocation;
5573 
5574       ++relocation;
5575     }
5576 
5577   /* We didn't find it.  */
5578   bfd_set_error (bfd_error_bad_value);
5579   return NULL;
5580 }
5581 
5582 /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
5583    is the original relocation, which is now being transformed into a
5584    dyanmic relocation.  The ADDENDP is adjusted if necessary; the
5585    caller should store the result in place of the original addend.  */
5586 
5587 static boolean
5588 mips_elf_create_dynamic_relocation (output_bfd, info, rel, h, sec,
5589 				    symbol, addendp, input_section)
5590      bfd *output_bfd;
5591      struct bfd_link_info *info;
5592      const Elf_Internal_Rela *rel;
5593      struct mips_elf_link_hash_entry *h;
5594      asection *sec;
5595      bfd_vma symbol;
5596      bfd_vma *addendp;
5597      asection *input_section;
5598 {
5599   Elf_Internal_Rel outrel;
5600   boolean skip;
5601   asection *sreloc;
5602   bfd *dynobj;
5603   int r_type;
5604 
5605   r_type = ELF32_R_TYPE (rel->r_info);
5606   dynobj = elf_hash_table (info)->dynobj;
5607   sreloc
5608     = bfd_get_section_by_name (dynobj,
5609 			       MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd));
5610   BFD_ASSERT (sreloc != NULL);
5611 
5612   skip = false;
5613 
5614   /* We begin by assuming that the offset for the dynamic relocation
5615      is the same as for the original relocation.  We'll adjust this
5616      later to reflect the correct output offsets.  */
5617   if (elf_section_data (input_section)->stab_info == NULL)
5618     outrel.r_offset = rel->r_offset;
5619   else
5620     {
5621       /* Except that in a stab section things are more complex.
5622 	 Because we compress stab information, the offset given in the
5623 	 relocation may not be the one we want; we must let the stabs
5624 	 machinery tell us the offset.  */
5625       outrel.r_offset
5626 	= (_bfd_stab_section_offset
5627 	   (output_bfd, &elf_hash_table (info)->stab_info,
5628 	    input_section,
5629 	    &elf_section_data (input_section)->stab_info,
5630 	    rel->r_offset));
5631       /* If we didn't need the relocation at all, this value will be
5632 	 -1.  */
5633       if (outrel.r_offset == (bfd_vma) -1)
5634 	skip = true;
5635     }
5636 
5637   /* If we've decided to skip this relocation, just output an emtpy
5638      record.  Note that R_MIPS_NONE == 0, so that this call to memset
5639      is a way of setting R_TYPE to R_MIPS_NONE.  */
5640   if (skip)
5641     memset (&outrel, 0, sizeof (outrel));
5642   else
5643     {
5644       long indx;
5645       bfd_vma section_offset;
5646 
5647       /* We must now calculate the dynamic symbol table index to use
5648 	 in the relocation.  */
5649       if (h != NULL
5650 	  && (! info->symbolic || (h->root.elf_link_hash_flags
5651 				   & ELF_LINK_HASH_DEF_REGULAR) == 0))
5652 	{
5653 	  indx = h->root.dynindx;
5654 	  BFD_ASSERT (indx != -1);
5655 	}
5656       else
5657 	{
5658 	  if (sec != NULL && bfd_is_abs_section (sec))
5659 	    indx = 0;
5660 	  else if (sec == NULL || sec->owner == NULL)
5661 	    {
5662 	      bfd_set_error (bfd_error_bad_value);
5663 	      return false;
5664 	    }
5665 	  else
5666 	    {
5667 	      indx = elf_section_data (sec->output_section)->dynindx;
5668 	      if (indx == 0)
5669 		abort ();
5670 	    }
5671 
5672 	  /* Figure out how far the target of the relocation is from
5673 	     the beginning of its section.  */
5674 	  section_offset = symbol - sec->output_section->vma;
5675 	  /* The relocation we're building is section-relative.
5676 	     Therefore, the original addend must be adjusted by the
5677 	     section offset.  */
5678 	  *addendp += symbol - sec->output_section->vma;
5679 	  /* Now, the relocation is just against the section.  */
5680 	  symbol = sec->output_section->vma;
5681 	}
5682 
5683       /* If the relocation was previously an absolute relocation, we
5684 	 must adjust it by the value we give it in the dynamic symbol
5685 	 table.  */
5686       if (r_type != R_MIPS_REL32)
5687 	*addendp += symbol;
5688 
5689       /* The relocation is always an REL32 relocation because we don't
5690 	 know where the shared library will wind up at load-time.  */
5691       outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5692 
5693       /* Adjust the output offset of the relocation to reference the
5694 	 correct location in the output file.  */
5695       outrel.r_offset += (input_section->output_section->vma
5696 			  + input_section->output_offset);
5697     }
5698 
5699   /* Put the relocation back out.  We have to use the special
5700      relocation outputter in the 64-bit case since the 64-bit
5701      relocation format is non-standard.  */
5702   if (ABI_64_P (output_bfd))
5703     {
5704       (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5705 	(output_bfd, &outrel,
5706 	 (sreloc->contents
5707 	  + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5708     }
5709   else
5710     bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5711 			      (((Elf32_External_Rel *)
5712 				sreloc->contents)
5713 			       + sreloc->reloc_count));
5714 
5715   /* Record the index of the first relocation referencing H.  This
5716      information is later emitted in the .msym section.  */
5717   if (h != NULL
5718       && (h->min_dyn_reloc_index == 0
5719 	  || sreloc->reloc_count < h->min_dyn_reloc_index))
5720     h->min_dyn_reloc_index = sreloc->reloc_count;
5721 
5722   /* We've now added another relocation.  */
5723   ++sreloc->reloc_count;
5724 
5725   /* Make sure the output section is writable.  The dynamic linker
5726      will be writing to it.  */
5727   elf_section_data (input_section->output_section)->this_hdr.sh_flags
5728     |= SHF_WRITE;
5729 
5730   /* On IRIX5, make an entry of compact relocation info.  */
5731   if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
5732     {
5733       asection* scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5734       bfd_byte *cr;
5735 
5736       if (scpt)
5737 	{
5738 	  Elf32_crinfo cptrel;
5739 
5740 	  mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5741 	  cptrel.vaddr = (rel->r_offset
5742 			  + input_section->output_section->vma
5743 			  + input_section->output_offset);
5744 	  if (r_type == R_MIPS_REL32)
5745 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5746 	  else
5747 	    mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5748 	  mips_elf_set_cr_dist2to (cptrel, 0);
5749 	  cptrel.konst = *addendp;
5750 
5751 	  cr = (scpt->contents
5752 		+ sizeof (Elf32_External_compact_rel));
5753 	  bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5754 				     ((Elf32_External_crinfo *) cr
5755 				      + scpt->reloc_count));
5756 	  ++scpt->reloc_count;
5757 	}
5758     }
5759 
5760   return true;
5761 }
5762 
5763 /* Calculate the value produced by the RELOCATION (which comes from
5764    the INPUT_BFD).  The ADDEND is the addend to use for this
5765    RELOCATION; RELOCATION->R_ADDEND is ignored.
5766 
5767    The result of the relocation calculation is stored in VALUEP.
5768    REQUIRE_JALXP indicates whether or not the opcode used with this
5769    relocation must be JALX.
5770 
5771    This function returns bfd_reloc_continue if the caller need take no
5772    further action regarding this relocation, bfd_reloc_notsupported if
5773    something goes dramatically wrong, bfd_reloc_overflow if an
5774    overflow occurs, and bfd_reloc_ok to indicate success.  */
5775 
5776 static bfd_reloc_status_type
5777 mips_elf_calculate_relocation (abfd,
5778 			       input_bfd,
5779 			       input_section,
5780 			       info,
5781 			       relocation,
5782 			       addend,
5783 			       howto,
5784 			       local_syms,
5785 			       local_sections,
5786 			       valuep,
5787 			       namep,
5788 			       require_jalxp)
5789      bfd *abfd;
5790      bfd *input_bfd;
5791      asection *input_section;
5792      struct bfd_link_info *info;
5793      const Elf_Internal_Rela *relocation;
5794      bfd_vma addend;
5795      reloc_howto_type *howto;
5796      Elf_Internal_Sym *local_syms;
5797      asection **local_sections;
5798      bfd_vma *valuep;
5799      const char **namep;
5800      boolean *require_jalxp;
5801 {
5802   /* The eventual value we will return.  */
5803   bfd_vma value;
5804   /* The address of the symbol against which the relocation is
5805      occurring.  */
5806   bfd_vma symbol = 0;
5807   /* The final GP value to be used for the relocatable, executable, or
5808      shared object file being produced.  */
5809   bfd_vma gp = (bfd_vma) - 1;
5810   /* The place (section offset or address) of the storage unit being
5811      relocated.  */
5812   bfd_vma p;
5813   /* The value of GP used to create the relocatable object.  */
5814   bfd_vma gp0 = (bfd_vma) - 1;
5815   /* The offset into the global offset table at which the address of
5816      the relocation entry symbol, adjusted by the addend, resides
5817      during execution.  */
5818   bfd_vma g = (bfd_vma) - 1;
5819   /* The section in which the symbol referenced by the relocation is
5820      located.  */
5821   asection *sec = NULL;
5822   struct mips_elf_link_hash_entry* h = NULL;
5823   /* True if the symbol referred to by this relocation is a local
5824      symbol.  */
5825   boolean local_p;
5826   /* True if the symbol referred to by this relocation is "_gp_disp".  */
5827   boolean gp_disp_p = false;
5828   Elf_Internal_Shdr *symtab_hdr;
5829   size_t extsymoff;
5830   unsigned long r_symndx;
5831   int r_type;
5832   /* True if overflow occurred during the calculation of the
5833      relocation value.  */
5834   boolean overflowed_p;
5835   /* True if this relocation refers to a MIPS16 function.  */
5836   boolean target_is_16_bit_code_p = false;
5837 
5838   /* Parse the relocation.  */
5839   r_symndx = ELF32_R_SYM (relocation->r_info);
5840   r_type = ELF32_R_TYPE (relocation->r_info);
5841   p = (input_section->output_section->vma
5842        + input_section->output_offset
5843        + relocation->r_offset);
5844 
5845   /* Assume that there will be no overflow.  */
5846   overflowed_p = false;
5847 
5848   /* Figure out whether or not the symbol is local, and get the offset
5849      used in the array of hash table entries.  */
5850   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5851   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5852 					 local_sections);
5853   if (! elf_bad_symtab (input_bfd))
5854     extsymoff = symtab_hdr->sh_info;
5855   else
5856     {
5857       /* The symbol table does not follow the rule that local symbols
5858 	 must come before globals.  */
5859       extsymoff = 0;
5860     }
5861 
5862   /* Figure out the value of the symbol.  */
5863   if (local_p)
5864     {
5865       Elf_Internal_Sym *sym;
5866 
5867       sym = local_syms + r_symndx;
5868       sec = local_sections[r_symndx];
5869 
5870       symbol = sec->output_section->vma + sec->output_offset;
5871       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5872 	symbol += sym->st_value;
5873 
5874       /* MIPS16 text labels should be treated as odd.  */
5875       if (sym->st_other == STO_MIPS16)
5876 	++symbol;
5877 
5878       /* Record the name of this symbol, for our caller.  */
5879       *namep = bfd_elf_string_from_elf_section (input_bfd,
5880 						symtab_hdr->sh_link,
5881 						sym->st_name);
5882       if (*namep == '\0')
5883 	*namep = bfd_section_name (input_bfd, sec);
5884 
5885       target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
5886     }
5887   else
5888     {
5889       /* For global symbols we look up the symbol in the hash-table.  */
5890       h = ((struct mips_elf_link_hash_entry *)
5891 	   elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5892       /* Find the real hash-table entry for this symbol.  */
5893       while (h->root.type == bfd_link_hash_indirect
5894 	     || h->root.type == bfd_link_hash_warning)
5895 	h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5896 
5897       /* Record the name of this symbol, for our caller.  */
5898       *namep = h->root.root.root.string;
5899 
5900       /* See if this is the special _gp_disp symbol.  Note that such a
5901 	 symbol must always be a global symbol.  */
5902       if (strcmp (h->root.root.root.string, "_gp_disp") == 0)
5903 	{
5904 	  /* Relocations against _gp_disp are permitted only with
5905 	     R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
5906 	  if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16)
5907 	    return bfd_reloc_notsupported;
5908 
5909 	  gp_disp_p = true;
5910 	}
5911       /* If this symbol is defined, calculate its address.  Note that
5912 	 _gp_disp is a magic symbol, always implicitly defined by the
5913 	 linker, so it's inappropriate to check to see whether or not
5914 	 its defined.  */
5915       else if ((h->root.root.type == bfd_link_hash_defined
5916 		|| h->root.root.type == bfd_link_hash_defweak)
5917 	       && h->root.root.u.def.section)
5918 	{
5919 	  sec = h->root.root.u.def.section;
5920 	  if (sec->output_section)
5921 	    symbol = (h->root.root.u.def.value
5922 		      + sec->output_section->vma
5923 		      + sec->output_offset);
5924 	  else
5925 	    symbol = h->root.root.u.def.value;
5926 	}
5927       else if (h->root.root.type == bfd_link_hash_undefweak)
5928 	/* We allow relocations against undefined weak symbols, giving
5929 	   it the value zero, so that you can undefined weak functions
5930 	   and check to see if they exist by looking at their
5931 	   addresses.  */
5932 	symbol = 0;
5933       else if (info->shared && !info->symbolic && !info->no_undefined
5934 	       && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5935 	symbol = 0;
5936       else if (strcmp (h->root.root.root.string, "_DYNAMIC_LINK") == 0)
5937 	{
5938 	  /* If this is a dynamic link, we should have created a
5939 	     _DYNAMIC_LINK symbol in mips_elf_create_dynamic_sections.
5940 	     Otherwise, we should define the symbol with a value of 0.
5941 	     FIXME: It should probably get into the symbol table
5942 	     somehow as well.  */
5943 	  BFD_ASSERT (! info->shared);
5944 	  BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5945 	  symbol = 0;
5946 	}
5947       else
5948 	{
5949 	  if (! ((*info->callbacks->undefined_symbol)
5950 		 (info, h->root.root.root.string, input_bfd,
5951 		  input_section, relocation->r_offset,
5952 		  (!info->shared || info->no_undefined
5953 		   || ELF_ST_VISIBILITY (h->root.other)))))
5954 	    return bfd_reloc_undefined;
5955 	  symbol = 0;
5956 	}
5957 
5958       target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
5959     }
5960 
5961   /* If this is a 32-bit call to a 16-bit function with a stub, we
5962      need to redirect the call to the stub, unless we're already *in*
5963      a stub.  */
5964   if (r_type != R_MIPS16_26 && !info->relocateable
5965       && ((h != NULL && h->fn_stub != NULL)
5966 	  || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
5967 	      && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5968       && !mips_elf_stub_section_p (input_bfd, input_section))
5969     {
5970       /* This is a 32-bit call to a 16-bit function.  We should
5971 	 have already noticed that we were going to need the
5972 	 stub.  */
5973       if (local_p)
5974 	sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5975       else
5976 	{
5977 	  BFD_ASSERT (h->need_fn_stub);
5978 	  sec = h->fn_stub;
5979 	}
5980 
5981       symbol = sec->output_section->vma + sec->output_offset;
5982     }
5983   /* If this is a 16-bit call to a 32-bit function with a stub, we
5984      need to redirect the call to the stub.  */
5985   else if (r_type == R_MIPS16_26 && !info->relocateable
5986 	   && h != NULL
5987 	   && (h->call_stub != NULL || h->call_fp_stub != NULL)
5988 	   && !target_is_16_bit_code_p)
5989     {
5990       /* If both call_stub and call_fp_stub are defined, we can figure
5991 	 out which one to use by seeing which one appears in the input
5992 	 file.  */
5993       if (h->call_stub != NULL && h->call_fp_stub != NULL)
5994 	{
5995 	  asection *o;
5996 
5997 	  sec = NULL;
5998 	  for (o = input_bfd->sections; o != NULL; o = o->next)
5999 	    {
6000 	      if (strncmp (bfd_get_section_name (input_bfd, o),
6001 			   CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6002 		{
6003 		  sec = h->call_fp_stub;
6004 		  break;
6005 		}
6006 	    }
6007 	  if (sec == NULL)
6008 	    sec = h->call_stub;
6009 	}
6010       else if (h->call_stub != NULL)
6011 	sec = h->call_stub;
6012       else
6013 	sec = h->call_fp_stub;
6014 
6015       BFD_ASSERT (sec->_raw_size > 0);
6016       symbol = sec->output_section->vma + sec->output_offset;
6017     }
6018 
6019   /* Calls from 16-bit code to 32-bit code and vice versa require the
6020      special jalx instruction.  */
6021   *require_jalxp = (!info->relocateable
6022 		    && ((r_type == R_MIPS16_26) != target_is_16_bit_code_p));
6023 
6024   /* If we haven't already determined the GOT offset, or the GP value,
6025      and we're going to need it, get it now.  */
6026   switch (r_type)
6027     {
6028     case R_MIPS_CALL16:
6029     case R_MIPS_GOT16:
6030     case R_MIPS_GOT_DISP:
6031     case R_MIPS_GOT_HI16:
6032     case R_MIPS_CALL_HI16:
6033     case R_MIPS_GOT_LO16:
6034     case R_MIPS_CALL_LO16:
6035       /* Find the index into the GOT where this value is located.  */
6036       if (!local_p)
6037 	{
6038 	  BFD_ASSERT (addend == 0);
6039 	  g = mips_elf_global_got_index
6040 	    (elf_hash_table (info)->dynobj,
6041 	     (struct elf_link_hash_entry*) h);
6042 	}
6043       else if (r_type == R_MIPS_GOT16)
6044 	/* There's no need to create a local GOT entry here; the
6045 	   calculation for a local GOT16 entry does not involve G.  */
6046 	break;
6047       else
6048 	{
6049 	  g = mips_elf_local_got_index (abfd, info, symbol + addend);
6050 	  if (g == (bfd_vma) -1)
6051 	    return false;
6052 	}
6053 
6054       /* Convert GOT indices to actual offsets.  */
6055       g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6056 					  abfd, g);
6057       break;
6058 
6059     case R_MIPS_HI16:
6060     case R_MIPS_LO16:
6061     case R_MIPS_GPREL16:
6062     case R_MIPS_GPREL32:
6063     case R_MIPS_LITERAL:
6064       gp0 = _bfd_get_gp_value (input_bfd);
6065       gp = _bfd_get_gp_value (abfd);
6066       break;
6067 
6068     default:
6069       break;
6070     }
6071 
6072   /* Figure out what kind of relocation is being performed.  */
6073   switch (r_type)
6074     {
6075     case R_MIPS_NONE:
6076       return bfd_reloc_continue;
6077 
6078     case R_MIPS_16:
6079       value = symbol + mips_elf_sign_extend (addend, 16);
6080       overflowed_p = mips_elf_overflow_p (value, 16);
6081       break;
6082 
6083     case R_MIPS_32:
6084     case R_MIPS_REL32:
6085     case R_MIPS_64:
6086       if ((info->shared
6087 	   || (elf_hash_table (info)->dynamic_sections_created
6088 	       && h != NULL
6089 	       && ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
6090 		   == 0)))
6091 	  && (input_section->flags & SEC_ALLOC) != 0)
6092 	{
6093 	  /* If we're creating a shared library, or this relocation is
6094 	     against a symbol in a shared library, then we can't know
6095 	     where the symbol will end up.  So, we create a relocation
6096 	     record in the output, and leave the job up to the dynamic
6097 	     linker.  */
6098 	  value = addend;
6099 	  if (!mips_elf_create_dynamic_relocation (abfd,
6100 						   info,
6101 						   relocation,
6102 						   h,
6103 						   sec,
6104 						   symbol,
6105 						   &value,
6106 						   input_section))
6107 	    return false;
6108 	}
6109       else
6110 	{
6111 	  if (r_type != R_MIPS_REL32)
6112 	    value = symbol + addend;
6113 	  else
6114 	    value = addend;
6115 	}
6116       value &= howto->dst_mask;
6117       break;
6118 
6119     case R_MIPS_PC32:
6120     case R_MIPS_PC64:
6121     case R_MIPS_GNU_REL_LO16:
6122       value = symbol + addend - p;
6123       value &= howto->dst_mask;
6124       break;
6125 
6126     case R_MIPS_GNU_REL16_S2:
6127       value = symbol + mips_elf_sign_extend (addend << 2, 18) - p;
6128       overflowed_p = mips_elf_overflow_p (value, 18);
6129       value = (value >> 2) & howto->dst_mask;
6130       break;
6131 
6132     case R_MIPS_GNU_REL_HI16:
6133       value = mips_elf_high (addend + symbol - p);
6134       value &= howto->dst_mask;
6135       break;
6136 
6137     case R_MIPS16_26:
6138       /* The calculation for R_MIPS_26 is just the same as for an
6139 	 R_MIPS_26.  It's only the storage of the relocated field into
6140 	 the output file that's different.  That's handled in
6141 	 mips_elf_perform_relocation.  So, we just fall through to the
6142 	 R_MIPS_26 case here.  */
6143     case R_MIPS_26:
6144       if (local_p)
6145 	value = (((addend << 2) | (p & 0xf0000000)) + symbol) >> 2;
6146       else
6147 	value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2;
6148       value &= howto->dst_mask;
6149       break;
6150 
6151     case R_MIPS_HI16:
6152       if (!gp_disp_p)
6153 	{
6154 	  value = mips_elf_high (addend + symbol);
6155 	  value &= howto->dst_mask;
6156 	}
6157       else
6158 	{
6159 	  value = mips_elf_high (addend + gp - p);
6160 	  overflowed_p = mips_elf_overflow_p (value, 16);
6161 	}
6162       break;
6163 
6164     case R_MIPS_LO16:
6165       if (!gp_disp_p)
6166 	value = (symbol + addend) & howto->dst_mask;
6167       else
6168 	{
6169 	  value = addend + gp - p + 4;
6170 	  /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6171 	     for overflow.  But, on, say, Irix 5, relocations against
6172 	     _gp_disp are normally generated from the .cpload
6173 	     pseudo-op.  It generates code that normally looks like
6174 	     this:
6175 
6176 	       lui    $gp,%hi(_gp_disp)
6177 	       addiu  $gp,$gp,%lo(_gp_disp)
6178 	       addu   $gp,$gp,$t9
6179 
6180 	     Here $t9 holds the address of the function being called,
6181 	     as required by the MIPS ELF ABI.  The R_MIPS_LO16
6182 	     relocation can easily overflow in this situation, but the
6183 	     R_MIPS_HI16 relocation will handle the overflow.
6184 	     Therefore, we consider this a bug in the MIPS ABI, and do
6185 	     not check for overflow here.  */
6186 	}
6187       break;
6188 
6189     case R_MIPS_LITERAL:
6190       /* Because we don't merge literal sections, we can handle this
6191 	 just like R_MIPS_GPREL16.  In the long run, we should merge
6192 	 shared literals, and then we will need to additional work
6193 	 here.  */
6194 
6195       /* Fall through.  */
6196 
6197     case R_MIPS16_GPREL:
6198       /* The R_MIPS16_GPREL performs the same calculation as
6199 	 R_MIPS_GPREL16, but stores the relocated bits in a different
6200 	 order.  We don't need to do anything special here; the
6201 	 differences are handled in mips_elf_perform_relocation.  */
6202     case R_MIPS_GPREL16:
6203       if (local_p)
6204 	value = mips_elf_sign_extend (addend, 16) + symbol + gp0 - gp;
6205       else
6206 	value = mips_elf_sign_extend (addend, 16) + symbol - gp;
6207       overflowed_p = mips_elf_overflow_p (value, 16);
6208       break;
6209 
6210     case R_MIPS_GOT16:
6211       if (local_p)
6212 	{
6213 	  value = mips_elf_got16_entry (abfd, info, symbol + addend);
6214 	  if (value == (bfd_vma) -1)
6215 	    return false;
6216 	  value
6217 	    = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6218 					      abfd,
6219 					      value);
6220 	  overflowed_p = mips_elf_overflow_p (value, 16);
6221 	  break;
6222 	}
6223 
6224       /* Fall through.  */
6225 
6226     case R_MIPS_CALL16:
6227     case R_MIPS_GOT_DISP:
6228       value = g;
6229       overflowed_p = mips_elf_overflow_p (value, 16);
6230       break;
6231 
6232     case R_MIPS_GPREL32:
6233       value = (addend + symbol + gp0 - gp) & howto->dst_mask;
6234       break;
6235 
6236     case R_MIPS_PC16:
6237       value = mips_elf_sign_extend (addend, 16) + symbol - p;
6238       value = (bfd_vma) ((bfd_signed_vma) value / 4);
6239       overflowed_p = mips_elf_overflow_p (value, 16);
6240       break;
6241 
6242     case R_MIPS_GOT_HI16:
6243     case R_MIPS_CALL_HI16:
6244       /* We're allowed to handle these two relocations identically.
6245 	 The dynamic linker is allowed to handle the CALL relocations
6246 	 differently by creating a lazy evaluation stub.  */
6247       value = g;
6248       value = mips_elf_high (value);
6249       value &= howto->dst_mask;
6250       break;
6251 
6252     case R_MIPS_GOT_LO16:
6253     case R_MIPS_CALL_LO16:
6254       value = g & howto->dst_mask;
6255       break;
6256 
6257     case R_MIPS_GOT_PAGE:
6258       value = mips_elf_got_page (abfd, info, symbol + addend, NULL);
6259       if (value == (bfd_vma) -1)
6260 	return false;
6261       value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6262 					      abfd,
6263 					      value);
6264       overflowed_p = mips_elf_overflow_p (value, 16);
6265       break;
6266 
6267     case R_MIPS_GOT_OFST:
6268       mips_elf_got_page (abfd, info, symbol + addend, &value);
6269       overflowed_p = mips_elf_overflow_p (value, 16);
6270       break;
6271 
6272     case R_MIPS_SUB:
6273       value = symbol - addend;
6274       value &= howto->dst_mask;
6275       break;
6276 
6277     case R_MIPS_HIGHER:
6278       value = mips_elf_higher (addend + symbol);
6279       value &= howto->dst_mask;
6280       break;
6281 
6282     case R_MIPS_HIGHEST:
6283       value = mips_elf_highest (addend + symbol);
6284       value &= howto->dst_mask;
6285       break;
6286 
6287     case R_MIPS_SCN_DISP:
6288       value = symbol + addend - sec->output_offset;
6289       value &= howto->dst_mask;
6290       break;
6291 
6292     case R_MIPS_PJUMP:
6293     case R_MIPS_JALR:
6294       /* Both of these may be ignored.  R_MIPS_JALR is an optimization
6295 	 hint; we could improve performance by honoring that hint.  */
6296       return bfd_reloc_continue;
6297 
6298     case R_MIPS_GNU_VTINHERIT:
6299     case R_MIPS_GNU_VTENTRY:
6300       /* We don't do anything with these at present.  */
6301       return bfd_reloc_continue;
6302 
6303     default:
6304       /* An unrecognized relocation type.  */
6305       return bfd_reloc_notsupported;
6306     }
6307 
6308   /* Store the VALUE for our caller.  */
6309   *valuep = value;
6310   return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6311 }
6312 
6313 /* Obtain the field relocated by RELOCATION.  */
6314 
6315 static bfd_vma
6316 mips_elf_obtain_contents (howto, relocation, input_bfd, contents)
6317      reloc_howto_type *howto;
6318      const Elf_Internal_Rela *relocation;
6319      bfd *input_bfd;
6320      bfd_byte *contents;
6321 {
6322   bfd_vma x;
6323   bfd_byte *location = contents + relocation->r_offset;
6324 
6325   /* Obtain the bytes.  */
6326   x = bfd_get (8 * bfd_get_reloc_size (howto), input_bfd, location);
6327 
6328   if ((ELF32_R_TYPE (relocation->r_info) == R_MIPS16_26
6329        || ELF32_R_TYPE (relocation->r_info) == R_MIPS16_GPREL)
6330       && bfd_little_endian (input_bfd))
6331     /* The two 16-bit words will be reversed on a little-endian
6332        system.  See mips_elf_perform_relocation for more details.  */
6333     x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6334 
6335   return x;
6336 }
6337 
6338 /* It has been determined that the result of the RELOCATION is the
6339    VALUE.  Use HOWTO to place VALUE into the output file at the
6340    appropriate position.  The SECTION is the section to which the
6341    relocation applies.  If REQUIRE_JALX is true, then the opcode used
6342    for the relocation must be either JAL or JALX, and it is
6343    unconditionally converted to JALX.
6344 
6345    Returns false if anything goes wrong.  */
6346 
6347 static boolean
6348 mips_elf_perform_relocation (info, howto, relocation, value,
6349 			     input_bfd, input_section,
6350 			     contents, require_jalx)
6351      struct bfd_link_info *info;
6352      reloc_howto_type *howto;
6353      const Elf_Internal_Rela *relocation;
6354      bfd_vma value;
6355      bfd *input_bfd;
6356      asection *input_section;
6357      bfd_byte *contents;
6358      boolean require_jalx;
6359 {
6360   bfd_vma x;
6361   bfd_byte *location;
6362   int r_type = ELF32_R_TYPE (relocation->r_info);
6363 
6364   /* Figure out where the relocation is occurring.  */
6365   location = contents + relocation->r_offset;
6366 
6367   /* Obtain the current value.  */
6368   x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6369 
6370   /* Clear the field we are setting.  */
6371   x &= ~howto->dst_mask;
6372 
6373   /* If this is the R_MIPS16_26 relocation, we must store the
6374      value in a funny way.  */
6375   if (r_type == R_MIPS16_26)
6376     {
6377       /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
6378 	 Most mips16 instructions are 16 bits, but these instructions
6379 	 are 32 bits.
6380 
6381 	 The format of these instructions is:
6382 
6383 	 +--------------+--------------------------------+
6384 	 !     JALX     ! X!   Imm 20:16  !   Imm 25:21  !
6385 	 +--------------+--------------------------------+
6386 	 !	  	  Immediate  15:0		    !
6387 	 +-----------------------------------------------+
6388 
6389 	 JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
6390 	 Note that the immediate value in the first word is swapped.
6391 
6392 	 When producing a relocateable object file, R_MIPS16_26 is
6393 	 handled mostly like R_MIPS_26.  In particular, the addend is
6394 	 stored as a straight 26-bit value in a 32-bit instruction.
6395 	 (gas makes life simpler for itself by never adjusting a
6396 	 R_MIPS16_26 reloc to be against a section, so the addend is
6397 	 always zero).  However, the 32 bit instruction is stored as 2
6398 	 16-bit values, rather than a single 32-bit value.  In a
6399 	 big-endian file, the result is the same; in a little-endian
6400 	 file, the two 16-bit halves of the 32 bit value are swapped.
6401 	 This is so that a disassembler can recognize the jal
6402 	 instruction.
6403 
6404 	 When doing a final link, R_MIPS16_26 is treated as a 32 bit
6405 	 instruction stored as two 16-bit values.  The addend A is the
6406 	 contents of the targ26 field.  The calculation is the same as
6407 	 R_MIPS_26.  When storing the calculated value, reorder the
6408 	 immediate value as shown above, and don't forget to store the
6409 	 value as two 16-bit values.
6410 
6411 	 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
6412 	 defined as
6413 
6414 	 big-endian:
6415 	 +--------+----------------------+
6416 	 |        |                      |
6417 	 |        |    targ26-16         |
6418 	 |31    26|25                   0|
6419 	 +--------+----------------------+
6420 
6421 	 little-endian:
6422 	 +----------+------+-------------+
6423 	 |          |      |             |
6424 	 |  sub1    |      |     sub2    |
6425 	 |0        9|10  15|16         31|
6426 	 +----------+--------------------+
6427 	 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
6428 	 ((sub1 << 16) | sub2)).
6429 
6430 	 When producing a relocateable object file, the calculation is
6431 	 (((A < 2) | (P & 0xf0000000) + S) >> 2)
6432 	 When producing a fully linked file, the calculation is
6433 	 let R = (((A < 2) | (P & 0xf0000000) + S) >> 2)
6434 	 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)  */
6435 
6436       if (!info->relocateable)
6437 	/* Shuffle the bits according to the formula above.  */
6438 	value = (((value & 0x1f0000) << 5)
6439 		 | ((value & 0x3e00000) >> 5)
6440 		 | (value & 0xffff));
6441 
6442     }
6443   else if (r_type == R_MIPS16_GPREL)
6444     {
6445       /* R_MIPS16_GPREL is used for GP-relative addressing in mips16
6446 	 mode.  A typical instruction will have a format like this:
6447 
6448 	 +--------------+--------------------------------+
6449 	 !    EXTEND    !     Imm 10:5    !   Imm 15:11  !
6450 	 +--------------+--------------------------------+
6451 	 !    Major     !   rx   !   ry   !   Imm  4:0   !
6452 	 +--------------+--------------------------------+
6453 
6454 	 EXTEND is the five bit value 11110.  Major is the instruction
6455 	 opcode.
6456 
6457 	 This is handled exactly like R_MIPS_GPREL16, except that the
6458 	 addend is retrieved and stored as shown in this diagram; that
6459 	 is, the Imm fields above replace the V-rel16 field.
6460 
6461          All we need to do here is shuffle the bits appropriately.  As
6462 	 above, the two 16-bit halves must be swapped on a
6463 	 little-endian system.  */
6464       value = (((value & 0x7e0) << 16)
6465 	       | ((value & 0xf800) << 5)
6466 	       | (value & 0x1f));
6467     }
6468 
6469   /* Set the field.  */
6470   x |= (value & howto->dst_mask);
6471 
6472   /* If required, turn JAL into JALX.  */
6473   if (require_jalx)
6474     {
6475       boolean ok;
6476       bfd_vma opcode = x >> 26;
6477       bfd_vma jalx_opcode;
6478 
6479       /* Check to see if the opcode is already JAL or JALX.  */
6480       if (r_type == R_MIPS16_26)
6481 	{
6482 	  ok = ((opcode == 0x6) || (opcode == 0x7));
6483 	  jalx_opcode = 0x7;
6484 	}
6485       else
6486 	{
6487 	  ok = ((opcode == 0x3) || (opcode == 0x1d));
6488 	  jalx_opcode = 0x1d;
6489 	}
6490 
6491       /* If the opcode is not JAL or JALX, there's a problem.  */
6492       if (!ok)
6493 	{
6494 	  (*_bfd_error_handler)
6495 	    (_("%s: %s+0x%lx: jump to stub routine which is not jal"),
6496 	     bfd_get_filename (input_bfd),
6497 	     input_section->name,
6498 	     (unsigned long) relocation->r_offset);
6499 	  bfd_set_error (bfd_error_bad_value);
6500 	  return false;
6501 	}
6502 
6503       /* Make this the JALX opcode.  */
6504       x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6505     }
6506 
6507   /* Swap the high- and low-order 16 bits on little-endian systems
6508      when doing a MIPS16 relocation.  */
6509   if ((r_type == R_MIPS16_GPREL || r_type == R_MIPS16_26)
6510       && bfd_little_endian (input_bfd))
6511     x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6512 
6513   /* Put the value into the output.  */
6514   bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
6515   return true;
6516 }
6517 
6518 /* Returns true if SECTION is a MIPS16 stub section.  */
6519 
6520 static boolean
6521 mips_elf_stub_section_p (abfd, section)
6522      bfd *abfd ATTRIBUTE_UNUSED;
6523      asection *section;
6524 {
6525   const char *name = bfd_get_section_name (abfd, section);
6526 
6527   return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
6528 	  || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6529 	  || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
6530 }
6531 
6532 /* Relocate a MIPS ELF section.  */
6533 
6534 boolean
6535 _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
6536 				contents, relocs, local_syms, local_sections)
6537      bfd *output_bfd;
6538      struct bfd_link_info *info;
6539      bfd *input_bfd;
6540      asection *input_section;
6541      bfd_byte *contents;
6542      Elf_Internal_Rela *relocs;
6543      Elf_Internal_Sym *local_syms;
6544      asection **local_sections;
6545 {
6546   Elf_Internal_Rela *rel;
6547   const Elf_Internal_Rela *relend;
6548   bfd_vma addend = 0;
6549   boolean use_saved_addend_p = false;
6550   struct elf_backend_data *bed;
6551 
6552   bed = get_elf_backend_data (output_bfd);
6553   relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
6554   for (rel = relocs; rel < relend; ++rel)
6555     {
6556       const char *name;
6557       bfd_vma value;
6558       reloc_howto_type *howto;
6559       boolean require_jalx;
6560       /* True if the relocation is a RELA relocation, rather than a
6561          REL relocation.  */
6562       boolean rela_relocation_p = true;
6563       int r_type = ELF32_R_TYPE (rel->r_info);
6564 
6565       /* Find the relocation howto for this relocation.  */
6566       if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
6567 	{
6568 	  /* Some 32-bit code uses R_MIPS_64.  In particular, people use
6569 	     64-bit code, but make sure all their addresses are in the
6570 	     lowermost or uppermost 32-bit section of the 64-bit address
6571 	     space.  Thus, when they use an R_MIPS_64 they mean what is
6572 	     usually meant by R_MIPS_32, with the exception that the
6573 	     stored value is sign-extended to 64 bits.  */
6574 	  howto = elf_mips_howto_table + R_MIPS_32;
6575 
6576 	  /* On big-endian systems, we need to lie about the position
6577 	     of the reloc.  */
6578 	  if (bfd_big_endian (input_bfd))
6579 	      rel->r_offset += 4;
6580 	}
6581       else
6582 	howto = mips_rtype_to_howto (r_type);
6583 
6584       if (!use_saved_addend_p)
6585 	{
6586 	  Elf_Internal_Shdr *rel_hdr;
6587 
6588 	  /* If these relocations were originally of the REL variety,
6589 	     we must pull the addend out of the field that will be
6590 	     relocated.  Otherwise, we simply use the contents of the
6591 	     RELA relocation.  To determine which flavor or relocation
6592 	     this is, we depend on the fact that the INPUT_SECTION's
6593 	     REL_HDR is read before its REL_HDR2.  */
6594 	  rel_hdr = &elf_section_data (input_section)->rel_hdr;
6595 	  if ((size_t) (rel - relocs)
6596 	      >= (rel_hdr->sh_size / rel_hdr->sh_entsize
6597 		  * bed->s->int_rels_per_ext_rel))
6598 	    rel_hdr = elf_section_data (input_section)->rel_hdr2;
6599 	  if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
6600 	    {
6601 	      /* Note that this is a REL relocation.  */
6602 	      rela_relocation_p = false;
6603 
6604 	      /* Get the addend, which is stored in the input file.  */
6605 	      addend = mips_elf_obtain_contents (howto,
6606 						 rel,
6607 						 input_bfd,
6608 						 contents);
6609 	      addend &= howto->src_mask;
6610 
6611 	      /* For some kinds of relocations, the ADDEND is a
6612 		 combination of the addend stored in two different
6613 		 relocations.   */
6614 	      if (r_type == R_MIPS_HI16
6615 		  || r_type == R_MIPS_GNU_REL_HI16
6616 		  || (r_type == R_MIPS_GOT16
6617 		      && mips_elf_local_relocation_p (input_bfd, rel,
6618 						      local_sections)))
6619 		{
6620 		  bfd_vma l;
6621 		  const Elf_Internal_Rela *lo16_relocation;
6622 		  reloc_howto_type *lo16_howto;
6623 		  int lo;
6624 
6625 		  /* The combined value is the sum of the HI16 addend,
6626 		     left-shifted by sixteen bits, and the LO16
6627 		     addend, sign extended.  (Usually, the code does
6628 		     a `lui' of the HI16 value, and then an `addiu' of
6629 		     the LO16 value.)
6630 
6631 		     Scan ahead to find a matching LO16 relocation.  */
6632 		  if (r_type == R_MIPS_GNU_REL_HI16)
6633 		    lo = R_MIPS_GNU_REL_LO16;
6634 		  else
6635 		    lo = R_MIPS_LO16;
6636 		  lo16_relocation
6637 		    = mips_elf_next_relocation (lo, rel, relend);
6638 		  if (lo16_relocation == NULL)
6639 		    return false;
6640 
6641 		  /* Obtain the addend kept there.  */
6642 		  lo16_howto = mips_rtype_to_howto (lo);
6643 		  l = mips_elf_obtain_contents (lo16_howto,
6644 						lo16_relocation,
6645 						input_bfd, contents);
6646 		  l &= lo16_howto->src_mask;
6647 		  l = mips_elf_sign_extend (l, 16);
6648 
6649 		  addend <<= 16;
6650 
6651 		  /* Compute the combined addend.  */
6652 		  addend += l;
6653 		}
6654 	      else if (r_type == R_MIPS16_GPREL)
6655 		{
6656 		  /* The addend is scrambled in the object file.  See
6657 		     mips_elf_perform_relocation for details on the
6658 		     format.  */
6659 		  addend = (((addend & 0x1f0000) >> 5)
6660 			    | ((addend & 0x7e00000) >> 16)
6661 			    | (addend & 0x1f));
6662 		}
6663 	    }
6664 	  else
6665 	    addend = rel->r_addend;
6666 	}
6667 
6668       if (info->relocateable)
6669 	{
6670 	  Elf_Internal_Sym *sym;
6671 	  unsigned long r_symndx;
6672 
6673 	  if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd)
6674 	      && bfd_big_endian (input_bfd))
6675 	    rel->r_offset -= 4;
6676 
6677 	  /* Since we're just relocating, all we need to do is copy
6678 	     the relocations back out to the object file, unless
6679 	     they're against a section symbol, in which case we need
6680 	     to adjust by the section offset, or unless they're GP
6681 	     relative in which case we need to adjust by the amount
6682 	     that we're adjusting GP in this relocateable object.  */
6683 
6684 	  if (!mips_elf_local_relocation_p (input_bfd, rel, local_sections))
6685 	    /* There's nothing to do for non-local relocations.  */
6686 	    continue;
6687 
6688 	  if (r_type == R_MIPS16_GPREL
6689 	      || r_type == R_MIPS_GPREL16
6690 	      || r_type == R_MIPS_GPREL32
6691 	      || r_type == R_MIPS_LITERAL)
6692 	    addend -= (_bfd_get_gp_value (output_bfd)
6693 		       - _bfd_get_gp_value (input_bfd));
6694 	  else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26
6695 		   || r_type == R_MIPS_GNU_REL16_S2)
6696 	    /* The addend is stored without its two least
6697 	       significant bits (which are always zero.)  In a
6698 	       non-relocateable link, calculate_relocation will do
6699 	       this shift; here, we must do it ourselves.  */
6700 	    addend <<= 2;
6701 
6702 	  r_symndx = ELF32_R_SYM (rel->r_info);
6703 	  sym = local_syms + r_symndx;
6704 	  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
6705 	    /* Adjust the addend appropriately.  */
6706 	    addend += local_sections[r_symndx]->output_offset;
6707 
6708 	  /* If the relocation is for a R_MIPS_HI16 or R_MIPS_GOT16,
6709 	     then we only want to write out the high-order 16 bits.
6710 	     The subsequent R_MIPS_LO16 will handle the low-order bits.  */
6711 	  if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16
6712 	      || r_type == R_MIPS_GNU_REL_HI16)
6713 	    addend = mips_elf_high (addend);
6714 	  /* If the relocation is for an R_MIPS_26 relocation, then
6715 	     the two low-order bits are not stored in the object file;
6716 	     they are implicitly zero.  */
6717 	  else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26
6718 		   || r_type == R_MIPS_GNU_REL16_S2)
6719 	    addend >>= 2;
6720 
6721 	  if (rela_relocation_p)
6722 	    /* If this is a RELA relocation, just update the addend.
6723 	       We have to cast away constness for REL.  */
6724 	    rel->r_addend = addend;
6725 	  else
6726 	    {
6727 	      /* Otherwise, we have to write the value back out.  Note
6728 		 that we use the source mask, rather than the
6729 		 destination mask because the place to which we are
6730 		 writing will be source of the addend in the final
6731 		 link.  */
6732 	      addend &= howto->src_mask;
6733 
6734 	      if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
6735 		/* See the comment above about using R_MIPS_64 in the 32-bit
6736 		   ABI.  Here, we need to update the addend.  It would be
6737 		   possible to get away with just using the R_MIPS_32 reloc
6738 		   but for endianness.  */
6739 		{
6740 		  bfd_vma sign_bits;
6741 		  bfd_vma low_bits;
6742 		  bfd_vma high_bits;
6743 
6744 		  if (addend & 0x80000000u)
6745 		    sign_bits = 0xffffffffu;
6746 		  else
6747 		    sign_bits = 0;
6748 
6749 		  /* If we don't know that we have a 64-bit type,
6750 		     do two separate stores.  */
6751 		  if (bfd_big_endian (input_bfd))
6752 		    {
6753 		      /* Store the sign-bits (which are most significant)
6754 			 first.  */
6755 		      low_bits = sign_bits;
6756 		      high_bits = addend;
6757 		    }
6758 		  else
6759 		    {
6760 		      low_bits = addend;
6761 		      high_bits = sign_bits;
6762 		    }
6763 		  bfd_put_32 (input_bfd, low_bits,
6764 			      contents + rel->r_offset);
6765 		  bfd_put_32 (input_bfd, high_bits,
6766 			      contents + rel->r_offset + 4);
6767 		  continue;
6768 		}
6769 
6770 	      if (!mips_elf_perform_relocation (info, howto, rel, addend,
6771 						input_bfd,  input_section,
6772 						contents, false))
6773 		return false;
6774 	    }
6775 
6776 	  /* Go on to the next relocation.  */
6777 	  continue;
6778 	}
6779 
6780       /* In the N32 and 64-bit ABIs there may be multiple consecutive
6781 	 relocations for the same offset.  In that case we are
6782 	 supposed to treat the output of each relocation as the addend
6783 	 for the next.  */
6784       if (rel + 1 < relend
6785 	  && rel->r_offset == rel[1].r_offset
6786 	  && ELF32_R_TYPE (rel[1].r_info) != R_MIPS_NONE)
6787 	use_saved_addend_p = true;
6788       else
6789 	use_saved_addend_p = false;
6790 
6791       /* Figure out what value we are supposed to relocate.  */
6792       switch (mips_elf_calculate_relocation (output_bfd,
6793 					     input_bfd,
6794 					     input_section,
6795 					     info,
6796 					     rel,
6797 					     addend,
6798 					     howto,
6799 					     local_syms,
6800 					     local_sections,
6801 					     &value,
6802 					     &name,
6803 					     &require_jalx))
6804 	{
6805 	case bfd_reloc_continue:
6806 	  /* There's nothing to do.  */
6807 	  continue;
6808 
6809 	case bfd_reloc_undefined:
6810 	  /* mips_elf_calculate_relocation already called the
6811 	     undefined_symbol callback.  There's no real point in
6812 	     trying to perform the relocation at this point, so we
6813 	     just skip ahead to the next relocation.  */
6814 	  continue;
6815 
6816 	case bfd_reloc_notsupported:
6817 	  abort ();
6818 	  break;
6819 
6820 	case bfd_reloc_overflow:
6821 	  if (use_saved_addend_p)
6822 	    /* Ignore overflow until we reach the last relocation for
6823 	       a given location.  */
6824 	    ;
6825 	  else
6826 	    {
6827 	      BFD_ASSERT (name != NULL);
6828 	      if (! ((*info->callbacks->reloc_overflow)
6829 		     (info, name, howto->name, (bfd_vma) 0,
6830 		      input_bfd, input_section, rel->r_offset)))
6831 		return false;
6832 	    }
6833 	  break;
6834 
6835 	case bfd_reloc_ok:
6836 	  break;
6837 
6838 	default:
6839 	  abort ();
6840 	  break;
6841 	}
6842 
6843       /* If we've got another relocation for the address, keep going
6844 	 until we reach the last one.  */
6845       if (use_saved_addend_p)
6846 	{
6847 	  addend = value;
6848 	  continue;
6849 	}
6850 
6851       if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
6852 	/* See the comment above about using R_MIPS_64 in the 32-bit
6853 	   ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
6854 	   that calculated the right value.  Now, however, we
6855 	   sign-extend the 32-bit result to 64-bits, and store it as a
6856 	   64-bit value.  We are especially generous here in that we
6857 	   go to extreme lengths to support this usage on systems with
6858 	   only a 32-bit VMA.  */
6859 	{
6860 	  bfd_vma sign_bits;
6861 	  bfd_vma low_bits;
6862 	  bfd_vma high_bits;
6863 
6864 	  if (value & 0x80000000u)
6865 	    sign_bits = 0xffffffffu;
6866 	  else
6867 	    sign_bits = 0;
6868 
6869 	  /* If we don't know that we have a 64-bit type,
6870 	     do two separate stores.  */
6871 	  if (bfd_big_endian (input_bfd))
6872 	    {
6873 	      /* Undo what we did above.  */
6874 	      rel->r_offset -= 4;
6875 	      /* Store the sign-bits (which are most significant)
6876 		 first.  */
6877 	      low_bits = sign_bits;
6878 	      high_bits = value;
6879 	    }
6880 	  else
6881 	    {
6882 	      low_bits = value;
6883 	      high_bits = sign_bits;
6884 	    }
6885 	  bfd_put_32 (input_bfd, low_bits,
6886 		      contents + rel->r_offset);
6887 	  bfd_put_32 (input_bfd, high_bits,
6888 		      contents + rel->r_offset + 4);
6889 	  continue;
6890 	}
6891 
6892       /* Actually perform the relocation.  */
6893       if (!mips_elf_perform_relocation (info, howto, rel, value, input_bfd,
6894 					input_section, contents,
6895 					require_jalx))
6896 	return false;
6897     }
6898 
6899   return true;
6900 }
6901 
6902 /* This hook function is called before the linker writes out a global
6903    symbol.  We mark symbols as small common if appropriate.  This is
6904    also where we undo the increment of the value for a mips16 symbol.  */
6905 
6906 /*ARGSIGNORED*/
6907 boolean
6908 _bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
6909      bfd *abfd ATTRIBUTE_UNUSED;
6910      struct bfd_link_info *info ATTRIBUTE_UNUSED;
6911      const char *name ATTRIBUTE_UNUSED;
6912      Elf_Internal_Sym *sym;
6913      asection *input_sec;
6914 {
6915   /* If we see a common symbol, which implies a relocatable link, then
6916      if a symbol was small common in an input file, mark it as small
6917      common in the output file.  */
6918   if (sym->st_shndx == SHN_COMMON
6919       && strcmp (input_sec->name, ".scommon") == 0)
6920     sym->st_shndx = SHN_MIPS_SCOMMON;
6921 
6922   if (sym->st_other == STO_MIPS16
6923       && (sym->st_value & 1) != 0)
6924     --sym->st_value;
6925 
6926   return true;
6927 }
6928 
6929 /* Functions for the dynamic linker.  */
6930 
6931 /* The name of the dynamic interpreter.  This is put in the .interp
6932    section.  */
6933 
6934 #define ELF_DYNAMIC_INTERPRETER(abfd) 		\
6935    (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" 	\
6936     : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" 	\
6937     : "/usr/lib/libc.so.1")
6938 
6939 /* Create dynamic sections when linking against a dynamic object.  */
6940 
6941 boolean
6942 _bfd_mips_elf_create_dynamic_sections (abfd, info)
6943      bfd *abfd;
6944      struct bfd_link_info *info;
6945 {
6946   struct elf_link_hash_entry *h;
6947   flagword flags;
6948   register asection *s;
6949   const char * const *namep;
6950 
6951   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6952 	   | SEC_LINKER_CREATED | SEC_READONLY);
6953 
6954   /* Mips ABI requests the .dynamic section to be read only.  */
6955   s = bfd_get_section_by_name (abfd, ".dynamic");
6956   if (s != NULL)
6957     {
6958       if (! bfd_set_section_flags (abfd, s, flags))
6959 	return false;
6960     }
6961 
6962   /* We need to create .got section.  */
6963   if (! mips_elf_create_got_section (abfd, info))
6964     return false;
6965 
6966   /* Create the .msym section on IRIX6.  It is used by the dynamic
6967      linker to speed up dynamic relocations, and to avoid computing
6968      the ELF hash for symbols.  */
6969   if (IRIX_COMPAT (abfd) == ict_irix6
6970       && !mips_elf_create_msym_section (abfd))
6971     return false;
6972 
6973   /* Create .stub section.  */
6974   if (bfd_get_section_by_name (abfd,
6975 			       MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
6976     {
6977       s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
6978       if (s == NULL
6979 	  || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
6980 	  || ! bfd_set_section_alignment (abfd, s,
6981 					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6982 	return false;
6983     }
6984 
6985   if (IRIX_COMPAT (abfd) == ict_irix5
6986       && !info->shared
6987       && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6988     {
6989       s = bfd_make_section (abfd, ".rld_map");
6990       if (s == NULL
6991 	  || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
6992 	  || ! bfd_set_section_alignment (abfd, s,
6993 					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6994 	return false;
6995     }
6996 
6997   /* On IRIX5, we adjust add some additional symbols and change the
6998      alignments of several sections.  There is no ABI documentation
6999      indicating that this is necessary on IRIX6, nor any evidence that
7000      the linker takes such action.  */
7001   if (IRIX_COMPAT (abfd) == ict_irix5)
7002     {
7003       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7004 	{
7005 	  h = NULL;
7006 	  if (! (_bfd_generic_link_add_one_symbol
7007 		 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
7008 		  (bfd_vma) 0, (const char *) NULL, false,
7009 		  get_elf_backend_data (abfd)->collect,
7010 		  (struct bfd_link_hash_entry **) &h)))
7011 	    return false;
7012 	  h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
7013 	  h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7014 	  h->type = STT_SECTION;
7015 
7016 	  if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7017 	    return false;
7018 	}
7019 
7020       /* We need to create a .compact_rel section.  */
7021       if (! mips_elf_create_compact_rel_section (abfd, info))
7022 	return false;
7023 
7024       /* Change aligments of some sections.  */
7025       s = bfd_get_section_by_name (abfd, ".hash");
7026       if (s != NULL)
7027 	bfd_set_section_alignment (abfd, s, 4);
7028       s = bfd_get_section_by_name (abfd, ".dynsym");
7029       if (s != NULL)
7030 	bfd_set_section_alignment (abfd, s, 4);
7031       s = bfd_get_section_by_name (abfd, ".dynstr");
7032       if (s != NULL)
7033 	bfd_set_section_alignment (abfd, s, 4);
7034       s = bfd_get_section_by_name (abfd, ".reginfo");
7035       if (s != NULL)
7036 	bfd_set_section_alignment (abfd, s, 4);
7037       s = bfd_get_section_by_name (abfd, ".dynamic");
7038       if (s != NULL)
7039 	bfd_set_section_alignment (abfd, s, 4);
7040     }
7041 
7042   if (!info->shared)
7043     {
7044       h = NULL;
7045       if (! (_bfd_generic_link_add_one_symbol
7046 	     (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
7047 	      (bfd_vma) 0, (const char *) NULL, false,
7048 	      get_elf_backend_data (abfd)->collect,
7049 	      (struct bfd_link_hash_entry **) &h)))
7050 	return false;
7051       h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
7052       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7053       h->type = STT_SECTION;
7054 
7055       if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7056 	return false;
7057 
7058       if (! mips_elf_hash_table (info)->use_rld_obj_head)
7059 	{
7060 	  /* __rld_map is a four byte word located in the .data section
7061 	     and is filled in by the rtld to contain a pointer to
7062 	     the _r_debug structure. Its symbol value will be set in
7063 	     mips_elf_finish_dynamic_symbol.  */
7064 	  s = bfd_get_section_by_name (abfd, ".rld_map");
7065 	  BFD_ASSERT (s != NULL);
7066 
7067 	  h = NULL;
7068 	  if (! (_bfd_generic_link_add_one_symbol
7069 		 (info, abfd, "__rld_map", BSF_GLOBAL, s,
7070 		  (bfd_vma) 0, (const char *) NULL, false,
7071 		  get_elf_backend_data (abfd)->collect,
7072 		  (struct bfd_link_hash_entry **) &h)))
7073 	    return false;
7074 	  h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
7075 	  h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7076 	  h->type = STT_OBJECT;
7077 
7078 	  if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7079 	    return false;
7080 	}
7081     }
7082 
7083   return true;
7084 }
7085 
7086 /* Create the .compact_rel section.  */
7087 
7088 static boolean
7089 mips_elf_create_compact_rel_section (abfd, info)
7090      bfd *abfd;
7091      struct bfd_link_info *info ATTRIBUTE_UNUSED;
7092 {
7093   flagword flags;
7094   register asection *s;
7095 
7096   if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
7097     {
7098       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
7099 	       | SEC_READONLY);
7100 
7101       s = bfd_make_section (abfd, ".compact_rel");
7102       if (s == NULL
7103 	  || ! bfd_set_section_flags (abfd, s, flags)
7104 	  || ! bfd_set_section_alignment (abfd, s,
7105 					  MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7106 	return false;
7107 
7108       s->_raw_size = sizeof (Elf32_External_compact_rel);
7109     }
7110 
7111   return true;
7112 }
7113 
7114 /* Create the .got section to hold the global offset table. */
7115 
7116 static boolean
7117 mips_elf_create_got_section (abfd, info)
7118      bfd *abfd;
7119      struct bfd_link_info *info;
7120 {
7121   flagword flags;
7122   register asection *s;
7123   struct elf_link_hash_entry *h;
7124   struct mips_got_info *g;
7125 
7126   /* This function may be called more than once.  */
7127   if (mips_elf_got_section (abfd))
7128     return true;
7129 
7130   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7131 	   | SEC_LINKER_CREATED);
7132 
7133   s = bfd_make_section (abfd, ".got");
7134   if (s == NULL
7135       || ! bfd_set_section_flags (abfd, s, flags)
7136       || ! bfd_set_section_alignment (abfd, s, 4))
7137     return false;
7138 
7139   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
7140      linker script because we don't want to define the symbol if we
7141      are not creating a global offset table.  */
7142   h = NULL;
7143   if (! (_bfd_generic_link_add_one_symbol
7144 	 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
7145 	  (bfd_vma) 0, (const char *) NULL, false,
7146 	  get_elf_backend_data (abfd)->collect,
7147 	  (struct bfd_link_hash_entry **) &h)))
7148     return false;
7149   h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
7150   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7151   h->type = STT_OBJECT;
7152 
7153   if (info->shared
7154       && ! bfd_elf32_link_record_dynamic_symbol (info, h))
7155     return false;
7156 
7157   /* The first several global offset table entries are reserved.  */
7158   s->_raw_size = MIPS_RESERVED_GOTNO * MIPS_ELF_GOT_SIZE (abfd);
7159 
7160   g = (struct mips_got_info *) bfd_alloc (abfd,
7161 					  sizeof (struct mips_got_info));
7162   if (g == NULL)
7163     return false;
7164   g->global_gotsym = NULL;
7165   g->local_gotno = MIPS_RESERVED_GOTNO;
7166   g->assigned_gotno = MIPS_RESERVED_GOTNO;
7167   if (elf_section_data (s) == NULL)
7168     {
7169       s->used_by_bfd =
7170 	(PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
7171       if (elf_section_data (s) == NULL)
7172 	return false;
7173     }
7174   elf_section_data (s)->tdata = (PTR) g;
7175   elf_section_data (s)->this_hdr.sh_flags
7176     |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7177 
7178   return true;
7179 }
7180 
7181 /* Returns the .msym section for ABFD, creating it if it does not
7182    already exist.  Returns NULL to indicate error.  */
7183 
7184 static asection *
7185 mips_elf_create_msym_section (abfd)
7186      bfd *abfd;
7187 {
7188   asection *s;
7189 
7190   s = bfd_get_section_by_name (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
7191   if (!s)
7192     {
7193       s = bfd_make_section (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
7194       if (!s
7195 	  || !bfd_set_section_flags (abfd, s,
7196 				     SEC_ALLOC
7197 				     | SEC_LOAD
7198 				     | SEC_HAS_CONTENTS
7199 				     | SEC_LINKER_CREATED
7200 				     | SEC_READONLY)
7201 	  || !bfd_set_section_alignment (abfd, s,
7202 					 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7203 	return NULL;
7204     }
7205 
7206   return s;
7207 }
7208 
7209 /* Add room for N relocations to the .rel.dyn section in ABFD.  */
7210 
7211 static void
7212 mips_elf_allocate_dynamic_relocations (abfd, n)
7213      bfd *abfd;
7214      unsigned int n;
7215 {
7216   asection *s;
7217 
7218   s = bfd_get_section_by_name (abfd, MIPS_ELF_REL_DYN_SECTION_NAME (abfd));
7219   BFD_ASSERT (s != NULL);
7220 
7221   if (s->_raw_size == 0)
7222     {
7223       /* Make room for a null element. */
7224       s->_raw_size += MIPS_ELF_REL_SIZE (abfd);
7225       ++s->reloc_count;
7226     }
7227   s->_raw_size += n * MIPS_ELF_REL_SIZE (abfd);
7228 }
7229 
7230 /* Look through the relocs for a section during the first phase, and
7231    allocate space in the global offset table.  */
7232 
7233 boolean
7234 _bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
7235      bfd *abfd;
7236      struct bfd_link_info *info;
7237      asection *sec;
7238      const Elf_Internal_Rela *relocs;
7239 {
7240   const char *name;
7241   bfd *dynobj;
7242   Elf_Internal_Shdr *symtab_hdr;
7243   struct elf_link_hash_entry **sym_hashes;
7244   struct mips_got_info *g;
7245   size_t extsymoff;
7246   const Elf_Internal_Rela *rel;
7247   const Elf_Internal_Rela *rel_end;
7248   asection *sgot;
7249   asection *sreloc;
7250   struct elf_backend_data *bed;
7251 
7252   if (info->relocateable)
7253     return true;
7254 
7255   dynobj = elf_hash_table (info)->dynobj;
7256   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7257   sym_hashes = elf_sym_hashes (abfd);
7258   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7259 
7260   /* Check for the mips16 stub sections.  */
7261 
7262   name = bfd_get_section_name (abfd, sec);
7263   if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
7264     {
7265       unsigned long r_symndx;
7266 
7267       /* Look at the relocation information to figure out which symbol
7268          this is for.  */
7269 
7270       r_symndx = ELF32_R_SYM (relocs->r_info);
7271 
7272       if (r_symndx < extsymoff
7273 	  || sym_hashes[r_symndx - extsymoff] == NULL)
7274 	{
7275 	  asection *o;
7276 
7277 	  /* This stub is for a local symbol.  This stub will only be
7278              needed if there is some relocation in this BFD, other
7279              than a 16 bit function call, which refers to this symbol.  */
7280 	  for (o = abfd->sections; o != NULL; o = o->next)
7281 	    {
7282 	      Elf_Internal_Rela *sec_relocs;
7283 	      const Elf_Internal_Rela *r, *rend;
7284 
7285 	      /* We can ignore stub sections when looking for relocs.  */
7286 	      if ((o->flags & SEC_RELOC) == 0
7287 		  || o->reloc_count == 0
7288 		  || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
7289 			      sizeof FN_STUB - 1) == 0
7290 		  || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
7291 			      sizeof CALL_STUB - 1) == 0
7292 		  || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
7293 			      sizeof CALL_FP_STUB - 1) == 0)
7294 		continue;
7295 
7296 	      sec_relocs = (_bfd_elf32_link_read_relocs
7297 			    (abfd, o, (PTR) NULL,
7298 			     (Elf_Internal_Rela *) NULL,
7299 			     info->keep_memory));
7300 	      if (sec_relocs == NULL)
7301 		return false;
7302 
7303 	      rend = sec_relocs + o->reloc_count;
7304 	      for (r = sec_relocs; r < rend; r++)
7305 		if (ELF32_R_SYM (r->r_info) == r_symndx
7306 		    && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
7307 		  break;
7308 
7309 	      if (! info->keep_memory)
7310 		free (sec_relocs);
7311 
7312 	      if (r < rend)
7313 		break;
7314 	    }
7315 
7316 	  if (o == NULL)
7317 	    {
7318 	      /* There is no non-call reloc for this stub, so we do
7319                  not need it.  Since this function is called before
7320                  the linker maps input sections to output sections, we
7321                  can easily discard it by setting the SEC_EXCLUDE
7322                  flag.  */
7323 	      sec->flags |= SEC_EXCLUDE;
7324 	      return true;
7325 	    }
7326 
7327 	  /* Record this stub in an array of local symbol stubs for
7328              this BFD. */
7329 	  if (elf_tdata (abfd)->local_stubs == NULL)
7330 	    {
7331 	      unsigned long symcount;
7332 	      asection **n;
7333 
7334 	      if (elf_bad_symtab (abfd))
7335 		symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
7336 	      else
7337 		symcount = symtab_hdr->sh_info;
7338 	      n = (asection **) bfd_zalloc (abfd,
7339 					    symcount * sizeof (asection *));
7340 	      if (n == NULL)
7341 		return false;
7342 	      elf_tdata (abfd)->local_stubs = n;
7343 	    }
7344 
7345 	  elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7346 
7347 	  /* We don't need to set mips16_stubs_seen in this case.
7348              That flag is used to see whether we need to look through
7349              the global symbol table for stubs.  We don't need to set
7350              it here, because we just have a local stub.  */
7351 	}
7352       else
7353 	{
7354 	  struct mips_elf_link_hash_entry *h;
7355 
7356 	  h = ((struct mips_elf_link_hash_entry *)
7357 	       sym_hashes[r_symndx - extsymoff]);
7358 
7359 	  /* H is the symbol this stub is for.  */
7360 
7361 	  h->fn_stub = sec;
7362 	  mips_elf_hash_table (info)->mips16_stubs_seen = true;
7363 	}
7364     }
7365   else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
7366 	   || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
7367     {
7368       unsigned long r_symndx;
7369       struct mips_elf_link_hash_entry *h;
7370       asection **loc;
7371 
7372       /* Look at the relocation information to figure out which symbol
7373          this is for.  */
7374 
7375       r_symndx = ELF32_R_SYM (relocs->r_info);
7376 
7377       if (r_symndx < extsymoff
7378 	  || sym_hashes[r_symndx - extsymoff] == NULL)
7379 	{
7380 	  /* This stub was actually built for a static symbol defined
7381 	     in the same file.  We assume that all static symbols in
7382 	     mips16 code are themselves mips16, so we can simply
7383 	     discard this stub.  Since this function is called before
7384 	     the linker maps input sections to output sections, we can
7385 	     easily discard it by setting the SEC_EXCLUDE flag.  */
7386 	  sec->flags |= SEC_EXCLUDE;
7387 	  return true;
7388 	}
7389 
7390       h = ((struct mips_elf_link_hash_entry *)
7391 	   sym_hashes[r_symndx - extsymoff]);
7392 
7393       /* H is the symbol this stub is for.  */
7394 
7395       if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
7396 	loc = &h->call_fp_stub;
7397       else
7398 	loc = &h->call_stub;
7399 
7400       /* If we already have an appropriate stub for this function, we
7401 	 don't need another one, so we can discard this one.  Since
7402 	 this function is called before the linker maps input sections
7403 	 to output sections, we can easily discard it by setting the
7404 	 SEC_EXCLUDE flag.  We can also discard this section if we
7405 	 happen to already know that this is a mips16 function; it is
7406 	 not necessary to check this here, as it is checked later, but
7407 	 it is slightly faster to check now.  */
7408       if (*loc != NULL || h->root.other == STO_MIPS16)
7409 	{
7410 	  sec->flags |= SEC_EXCLUDE;
7411 	  return true;
7412 	}
7413 
7414       *loc = sec;
7415       mips_elf_hash_table (info)->mips16_stubs_seen = true;
7416     }
7417 
7418   if (dynobj == NULL)
7419     {
7420       sgot = NULL;
7421       g = NULL;
7422     }
7423   else
7424     {
7425       sgot = mips_elf_got_section (dynobj);
7426       if (sgot == NULL)
7427 	g = NULL;
7428       else
7429 	{
7430 	  BFD_ASSERT (elf_section_data (sgot) != NULL);
7431 	  g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7432 	  BFD_ASSERT (g != NULL);
7433 	}
7434     }
7435 
7436   sreloc = NULL;
7437   bed = get_elf_backend_data (abfd);
7438   rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7439   for (rel = relocs; rel < rel_end; ++rel)
7440     {
7441       unsigned long r_symndx;
7442       int r_type;
7443       struct elf_link_hash_entry *h;
7444 
7445       r_symndx = ELF32_R_SYM (rel->r_info);
7446       r_type = ELF32_R_TYPE (rel->r_info);
7447 
7448       if (r_symndx < extsymoff)
7449 	h = NULL;
7450       else
7451 	{
7452 	  h = sym_hashes[r_symndx - extsymoff];
7453 
7454 	  /* This may be an indirect symbol created because of a version.  */
7455 	  if (h != NULL)
7456 	    {
7457 	      while (h->root.type == bfd_link_hash_indirect)
7458 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
7459 	    }
7460 	}
7461 
7462       /* Some relocs require a global offset table.  */
7463       if (dynobj == NULL || sgot == NULL)
7464 	{
7465 	  switch (r_type)
7466 	    {
7467 	    case R_MIPS_GOT16:
7468 	    case R_MIPS_CALL16:
7469 	    case R_MIPS_CALL_HI16:
7470 	    case R_MIPS_CALL_LO16:
7471 	    case R_MIPS_GOT_HI16:
7472 	    case R_MIPS_GOT_LO16:
7473 	    case R_MIPS_GOT_PAGE:
7474 	    case R_MIPS_GOT_OFST:
7475 	    case R_MIPS_GOT_DISP:
7476 	      if (dynobj == NULL)
7477 		elf_hash_table (info)->dynobj = dynobj = abfd;
7478 	      if (! mips_elf_create_got_section (dynobj, info))
7479 		return false;
7480 	      g = mips_elf_got_info (dynobj, &sgot);
7481 	      break;
7482 
7483 	    case R_MIPS_32:
7484 	    case R_MIPS_REL32:
7485 	    case R_MIPS_64:
7486 	      if (dynobj == NULL
7487 		  && (info->shared || h != NULL)
7488 		  && (sec->flags & SEC_ALLOC) != 0)
7489 		elf_hash_table (info)->dynobj = dynobj = abfd;
7490 	      break;
7491 
7492 	    default:
7493 	      break;
7494 	    }
7495 	}
7496 
7497       if (!h && (r_type == R_MIPS_CALL_LO16
7498 		 || r_type == R_MIPS_GOT_LO16
7499 		 || r_type == R_MIPS_GOT_DISP))
7500 	{
7501 	  /* We may need a local GOT entry for this relocation.  We
7502 	     don't count R_MIPS_GOT_PAGE because we can estimate the
7503 	     maximum number of pages needed by looking at the size of
7504 	     the segment.  Similar comments apply to R_MIPS_GOT16.  We
7505 	     don't count R_MIPS_GOT_HI16, or R_MIPS_CALL_HI16 because
7506 	     these are always followed by an R_MIPS_GOT_LO16 or
7507 	     R_MIPS_CALL_LO16.
7508 
7509 	     This estimation is very conservative since we can merge
7510 	     duplicate entries in the GOT.  In order to be less
7511 	     conservative, we could actually build the GOT here,
7512 	     rather than in relocate_section.  */
7513 	  g->local_gotno++;
7514 	  sgot->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
7515 	}
7516 
7517       switch (r_type)
7518 	{
7519 	case R_MIPS_CALL16:
7520 	  if (h == NULL)
7521 	    {
7522 	      (*_bfd_error_handler)
7523 		(_("%s: CALL16 reloc at 0x%lx not against global symbol"),
7524 		 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
7525 	      bfd_set_error (bfd_error_bad_value);
7526 	      return false;
7527 	    }
7528 	  /* Fall through.  */
7529 
7530 	case R_MIPS_CALL_HI16:
7531 	case R_MIPS_CALL_LO16:
7532 	  if (h != NULL)
7533 	    {
7534 	      /* This symbol requires a global offset table entry.  */
7535 	      if (!mips_elf_record_global_got_symbol (h, info, g))
7536 		return false;
7537 
7538 	      /* We need a stub, not a plt entry for the undefined
7539 		 function.  But we record it as if it needs plt.  See
7540 		 elf_adjust_dynamic_symbol in elflink.h.  */
7541 	      h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
7542 	      h->type = STT_FUNC;
7543 	    }
7544 	  break;
7545 
7546 	case R_MIPS_GOT16:
7547 	case R_MIPS_GOT_HI16:
7548 	case R_MIPS_GOT_LO16:
7549 	case R_MIPS_GOT_DISP:
7550 	  /* This symbol requires a global offset table entry.  */
7551 	  if (h && !mips_elf_record_global_got_symbol (h, info, g))
7552 	    return false;
7553 	  break;
7554 
7555 	case R_MIPS_32:
7556 	case R_MIPS_REL32:
7557 	case R_MIPS_64:
7558 	  if ((info->shared || h != NULL)
7559 	      && (sec->flags & SEC_ALLOC) != 0)
7560 	    {
7561 	      if (sreloc == NULL)
7562 		{
7563 		  const char *name = MIPS_ELF_REL_DYN_SECTION_NAME (dynobj);
7564 
7565 		  sreloc = bfd_get_section_by_name (dynobj, name);
7566 		  if (sreloc == NULL)
7567 		    {
7568 		      sreloc = bfd_make_section (dynobj, name);
7569 		      if (sreloc == NULL
7570 			  || ! bfd_set_section_flags (dynobj, sreloc,
7571 						      (SEC_ALLOC
7572 						       | SEC_LOAD
7573 						       | SEC_HAS_CONTENTS
7574 						       | SEC_IN_MEMORY
7575 						       | SEC_LINKER_CREATED
7576 						       | SEC_READONLY))
7577 			  || ! bfd_set_section_alignment (dynobj, sreloc,
7578 							  4))
7579 			return false;
7580 		    }
7581 		}
7582 	      if (info->shared)
7583 		/* When creating a shared object, we must copy these
7584 		   reloc types into the output file as R_MIPS_REL32
7585 		   relocs.  We make room for this reloc in the
7586 		   .rel.dyn reloc section.  */
7587 		mips_elf_allocate_dynamic_relocations (dynobj, 1);
7588 	      else
7589 		{
7590 		  struct mips_elf_link_hash_entry *hmips;
7591 
7592 		  /* We only need to copy this reloc if the symbol is
7593                      defined in a dynamic object.  */
7594 		  hmips = (struct mips_elf_link_hash_entry *) h;
7595 		  ++hmips->possibly_dynamic_relocs;
7596 		}
7597 
7598 	      /* Even though we don't directly need a GOT entry for
7599 		 this symbol, a symbol must have a dynamic symbol
7600 		 table index greater that DT_MIPS_GOTSYM if there are
7601 		 dynamic relocations against it.  */
7602 	      if (h != NULL
7603 		  && !mips_elf_record_global_got_symbol (h, info, g))
7604 		return false;
7605 	    }
7606 
7607 	  if (SGI_COMPAT (dynobj))
7608 	    mips_elf_hash_table (info)->compact_rel_size +=
7609 	      sizeof (Elf32_External_crinfo);
7610 	  break;
7611 
7612 	case R_MIPS_26:
7613 	case R_MIPS_GPREL16:
7614 	case R_MIPS_LITERAL:
7615 	case R_MIPS_GPREL32:
7616 	  if (SGI_COMPAT (dynobj))
7617 	    mips_elf_hash_table (info)->compact_rel_size +=
7618 	      sizeof (Elf32_External_crinfo);
7619 	  break;
7620 
7621 	  /* This relocation describes the C++ object vtable hierarchy.
7622 	     Reconstruct it for later use during GC.  */
7623 	case R_MIPS_GNU_VTINHERIT:
7624 	  if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7625 	    return false;
7626 	  break;
7627 
7628 	  /* This relocation describes which C++ vtable entries are actually
7629 	     used.  Record for later use during GC.  */
7630 	case R_MIPS_GNU_VTENTRY:
7631 	  if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7632 	    return false;
7633 	  break;
7634 
7635 	default:
7636 	  break;
7637 	}
7638 
7639       /* If this reloc is not a 16 bit call, and it has a global
7640          symbol, then we will need the fn_stub if there is one.
7641          References from a stub section do not count. */
7642       if (h != NULL
7643 	  && r_type != R_MIPS16_26
7644 	  && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
7645 		      sizeof FN_STUB - 1) != 0
7646 	  && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
7647 		      sizeof CALL_STUB - 1) != 0
7648 	  && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
7649 		      sizeof CALL_FP_STUB - 1) != 0)
7650 	{
7651 	  struct mips_elf_link_hash_entry *mh;
7652 
7653 	  mh = (struct mips_elf_link_hash_entry *) h;
7654 	  mh->need_fn_stub = true;
7655 	}
7656     }
7657 
7658   return true;
7659 }
7660 
7661 /* Return the section that should be marked against GC for a given
7662    relocation.  */
7663 
7664 asection *
7665 _bfd_mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
7666      bfd *abfd;
7667      struct bfd_link_info *info ATTRIBUTE_UNUSED;
7668      Elf_Internal_Rela *rel;
7669      struct elf_link_hash_entry *h;
7670      Elf_Internal_Sym *sym;
7671 {
7672   /* ??? Do mips16 stub sections need to be handled special?  */
7673 
7674   if (h != NULL)
7675     {
7676       switch (ELF32_R_TYPE (rel->r_info))
7677 	{
7678 	case R_MIPS_GNU_VTINHERIT:
7679 	case R_MIPS_GNU_VTENTRY:
7680 	  break;
7681 
7682 	default:
7683 	  switch (h->root.type)
7684 	    {
7685 	    case bfd_link_hash_defined:
7686 	    case bfd_link_hash_defweak:
7687 	      return h->root.u.def.section;
7688 
7689 	    case bfd_link_hash_common:
7690 	      return h->root.u.c.p->section;
7691 
7692 	    default:
7693 	      break;
7694 	    }
7695 	}
7696     }
7697   else
7698     {
7699       if (!(elf_bad_symtab (abfd)
7700 	    && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7701 	  && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
7702 		&& sym->st_shndx != SHN_COMMON))
7703 	{
7704 	  return bfd_section_from_elf_index (abfd, sym->st_shndx);
7705 	}
7706     }
7707 
7708   return NULL;
7709 }
7710 
7711 /* Update the got entry reference counts for the section being removed.  */
7712 
7713 boolean
7714 _bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
7715      bfd *abfd ATTRIBUTE_UNUSED;
7716      struct bfd_link_info *info ATTRIBUTE_UNUSED;
7717      asection *sec ATTRIBUTE_UNUSED;
7718      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
7719 {
7720 #if 0
7721   Elf_Internal_Shdr *symtab_hdr;
7722   struct elf_link_hash_entry **sym_hashes;
7723   bfd_signed_vma *local_got_refcounts;
7724   const Elf_Internal_Rela *rel, *relend;
7725   unsigned long r_symndx;
7726   struct elf_link_hash_entry *h;
7727 
7728   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7729   sym_hashes = elf_sym_hashes (abfd);
7730   local_got_refcounts = elf_local_got_refcounts (abfd);
7731 
7732   relend = relocs + sec->reloc_count;
7733   for (rel = relocs; rel < relend; rel++)
7734     switch (ELF32_R_TYPE (rel->r_info))
7735       {
7736       case R_MIPS_GOT16:
7737       case R_MIPS_CALL16:
7738       case R_MIPS_CALL_HI16:
7739       case R_MIPS_CALL_LO16:
7740       case R_MIPS_GOT_HI16:
7741       case R_MIPS_GOT_LO16:
7742 	/* ??? It would seem that the existing MIPS code does no sort
7743 	   of reference counting or whatnot on its GOT and PLT entries,
7744 	   so it is not possible to garbage collect them at this time.  */
7745         break;
7746 
7747       default:
7748 	break;
7749       }
7750 #endif
7751 
7752   return true;
7753 }
7754 
7755 
7756 /* Adjust a symbol defined by a dynamic object and referenced by a
7757    regular object.  The current definition is in some section of the
7758    dynamic object, but we're not including those sections.  We have to
7759    change the definition to something the rest of the link can
7760    understand.  */
7761 
7762 boolean
7763 _bfd_mips_elf_adjust_dynamic_symbol (info, h)
7764      struct bfd_link_info *info;
7765      struct elf_link_hash_entry *h;
7766 {
7767   bfd *dynobj;
7768   struct mips_elf_link_hash_entry *hmips;
7769   asection *s;
7770 
7771   dynobj = elf_hash_table (info)->dynobj;
7772 
7773   /* Make sure we know what is going on here.  */
7774   BFD_ASSERT (dynobj != NULL
7775 	      && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
7776 		  || h->weakdef != NULL
7777 		  || ((h->elf_link_hash_flags
7778 		       & ELF_LINK_HASH_DEF_DYNAMIC) != 0
7779 		      && (h->elf_link_hash_flags
7780 			  & ELF_LINK_HASH_REF_REGULAR) != 0
7781 		      && (h->elf_link_hash_flags
7782 			  & ELF_LINK_HASH_DEF_REGULAR) == 0)));
7783 
7784   /* If this symbol is defined in a dynamic object, we need to copy
7785      any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
7786      file.  */
7787   hmips = (struct mips_elf_link_hash_entry *) h;
7788   if (! info->relocateable
7789       && hmips->possibly_dynamic_relocs != 0
7790       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7791     mips_elf_allocate_dynamic_relocations (dynobj,
7792 					   hmips->possibly_dynamic_relocs);
7793 
7794   /* For a function, create a stub, if needed. */
7795   if (h->type == STT_FUNC
7796       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
7797     {
7798       if (! elf_hash_table (info)->dynamic_sections_created)
7799 	return true;
7800 
7801       /* If this symbol is not defined in a regular file, then set
7802 	 the symbol to the stub location.  This is required to make
7803 	 function pointers compare as equal between the normal
7804 	 executable and the shared library.  */
7805       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7806 	{
7807 	  /* We need .stub section.  */
7808 	  s = bfd_get_section_by_name (dynobj,
7809 				       MIPS_ELF_STUB_SECTION_NAME (dynobj));
7810 	  BFD_ASSERT (s != NULL);
7811 
7812 	  h->root.u.def.section = s;
7813 	  h->root.u.def.value = s->_raw_size;
7814 
7815 	  /* XXX Write this stub address somewhere.  */
7816 	  h->plt.offset = s->_raw_size;
7817 
7818 	  /* Make room for this stub code.  */
7819 	  s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7820 
7821 	  /* The last half word of the stub will be filled with the index
7822 	     of this symbol in .dynsym section.  */
7823 	  return true;
7824 	}
7825     }
7826 
7827   /* If this is a weak symbol, and there is a real definition, the
7828      processor independent code will have arranged for us to see the
7829      real definition first, and we can just use the same value.  */
7830   if (h->weakdef != NULL)
7831     {
7832       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
7833 		  || h->weakdef->root.type == bfd_link_hash_defweak);
7834       h->root.u.def.section = h->weakdef->root.u.def.section;
7835       h->root.u.def.value = h->weakdef->root.u.def.value;
7836       return true;
7837     }
7838 
7839   /* This is a reference to a symbol defined by a dynamic object which
7840      is not a function.  */
7841 
7842   return true;
7843 }
7844 
7845 /* This function is called after all the input files have been read,
7846    and the input sections have been assigned to output sections.  We
7847    check for any mips16 stub sections that we can discard.  */
7848 
7849 static boolean mips_elf_check_mips16_stubs
7850   PARAMS ((struct mips_elf_link_hash_entry *, PTR));
7851 
7852 boolean
7853 _bfd_mips_elf_always_size_sections (output_bfd, info)
7854      bfd *output_bfd;
7855      struct bfd_link_info *info;
7856 {
7857   asection *ri;
7858 
7859   /* The .reginfo section has a fixed size.  */
7860   ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7861   if (ri != NULL)
7862     bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
7863 
7864   if (info->relocateable
7865       || ! mips_elf_hash_table (info)->mips16_stubs_seen)
7866     return true;
7867 
7868   mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7869 			       mips_elf_check_mips16_stubs,
7870 			       (PTR) NULL);
7871 
7872   return true;
7873 }
7874 
7875 /* Check the mips16 stubs for a particular symbol, and see if we can
7876    discard them.  */
7877 
7878 /*ARGSUSED*/
7879 static boolean
7880 mips_elf_check_mips16_stubs (h, data)
7881      struct mips_elf_link_hash_entry *h;
7882      PTR data ATTRIBUTE_UNUSED;
7883 {
7884   if (h->fn_stub != NULL
7885       && ! h->need_fn_stub)
7886     {
7887       /* We don't need the fn_stub; the only references to this symbol
7888          are 16 bit calls.  Clobber the size to 0 to prevent it from
7889          being included in the link.  */
7890       h->fn_stub->_raw_size = 0;
7891       h->fn_stub->_cooked_size = 0;
7892       h->fn_stub->flags &= ~ SEC_RELOC;
7893       h->fn_stub->reloc_count = 0;
7894       h->fn_stub->flags |= SEC_EXCLUDE;
7895     }
7896 
7897   if (h->call_stub != NULL
7898       && h->root.other == STO_MIPS16)
7899     {
7900       /* We don't need the call_stub; this is a 16 bit function, so
7901          calls from other 16 bit functions are OK.  Clobber the size
7902          to 0 to prevent it from being included in the link.  */
7903       h->call_stub->_raw_size = 0;
7904       h->call_stub->_cooked_size = 0;
7905       h->call_stub->flags &= ~ SEC_RELOC;
7906       h->call_stub->reloc_count = 0;
7907       h->call_stub->flags |= SEC_EXCLUDE;
7908     }
7909 
7910   if (h->call_fp_stub != NULL
7911       && h->root.other == STO_MIPS16)
7912     {
7913       /* We don't need the call_stub; this is a 16 bit function, so
7914          calls from other 16 bit functions are OK.  Clobber the size
7915          to 0 to prevent it from being included in the link.  */
7916       h->call_fp_stub->_raw_size = 0;
7917       h->call_fp_stub->_cooked_size = 0;
7918       h->call_fp_stub->flags &= ~ SEC_RELOC;
7919       h->call_fp_stub->reloc_count = 0;
7920       h->call_fp_stub->flags |= SEC_EXCLUDE;
7921     }
7922 
7923   return true;
7924 }
7925 
7926 /* Set the sizes of the dynamic sections.  */
7927 
7928 boolean
7929 _bfd_mips_elf_size_dynamic_sections (output_bfd, info)
7930      bfd *output_bfd;
7931      struct bfd_link_info *info;
7932 {
7933   bfd *dynobj;
7934   asection *s;
7935   boolean reltext;
7936   struct mips_got_info *g = NULL;
7937 
7938   dynobj = elf_hash_table (info)->dynobj;
7939   BFD_ASSERT (dynobj != NULL);
7940 
7941   if (elf_hash_table (info)->dynamic_sections_created)
7942     {
7943       /* Set the contents of the .interp section to the interpreter.  */
7944       if (! info->shared)
7945 	{
7946 	  s = bfd_get_section_by_name (dynobj, ".interp");
7947 	  BFD_ASSERT (s != NULL);
7948 	  s->_raw_size
7949 	    = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
7950 	  s->contents
7951 	    = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
7952 	}
7953     }
7954 
7955   /* The check_relocs and adjust_dynamic_symbol entry points have
7956      determined the sizes of the various dynamic sections.  Allocate
7957      memory for them.  */
7958   reltext = false;
7959   for (s = dynobj->sections; s != NULL; s = s->next)
7960     {
7961       const char *name;
7962       boolean strip;
7963 
7964       /* It's OK to base decisions on the section name, because none
7965 	 of the dynobj section names depend upon the input files.  */
7966       name = bfd_get_section_name (dynobj, s);
7967 
7968       if ((s->flags & SEC_LINKER_CREATED) == 0)
7969 	continue;
7970 
7971       strip = false;
7972 
7973       if (strncmp (name, ".rel", 4) == 0)
7974 	{
7975 	  if (s->_raw_size == 0)
7976 	    {
7977 	      /* We only strip the section if the output section name
7978                  has the same name.  Otherwise, there might be several
7979                  input sections for this output section.  FIXME: This
7980                  code is probably not needed these days anyhow, since
7981                  the linker now does not create empty output sections.  */
7982 	      if (s->output_section != NULL
7983 		  && strcmp (name,
7984 			     bfd_get_section_name (s->output_section->owner,
7985 						   s->output_section)) == 0)
7986 		strip = true;
7987 	    }
7988 	  else
7989 	    {
7990 	      const char *outname;
7991 	      asection *target;
7992 
7993 	      /* If this relocation section applies to a read only
7994                  section, then we probably need a DT_TEXTREL entry.
7995                  If the relocation section is .rel.dyn, we always
7996                  assert a DT_TEXTREL entry rather than testing whether
7997                  there exists a relocation to a read only section or
7998                  not.  */
7999 	      outname = bfd_get_section_name (output_bfd,
8000 					      s->output_section);
8001 	      target = bfd_get_section_by_name (output_bfd, outname + 4);
8002 	      if ((target != NULL
8003 		   && (target->flags & SEC_READONLY) != 0
8004 		   && (target->flags & SEC_ALLOC) != 0)
8005 		  || strcmp (outname,
8006 			     MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) == 0)
8007 		reltext = true;
8008 
8009 	      /* We use the reloc_count field as a counter if we need
8010 		 to copy relocs into the output file.  */
8011 	      if (strcmp (name,
8012 			  MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) != 0)
8013 		s->reloc_count = 0;
8014 	    }
8015 	}
8016       else if (strncmp (name, ".got", 4) == 0)
8017 	{
8018 	  int i;
8019  	  bfd_size_type loadable_size = 0;
8020  	  bfd_size_type local_gotno;
8021  	  struct _bfd *sub;
8022 
8023  	  BFD_ASSERT (elf_section_data (s) != NULL);
8024 	  g = (struct mips_got_info *) elf_section_data (s)->tdata;
8025  	  BFD_ASSERT (g != NULL);
8026 
8027  	  /* Calculate the total loadable size of the output.  That
8028  	     will give us the maximum number of GOT_PAGE entries
8029  	     required.  */
8030  	  for (sub = info->input_bfds; sub; sub = sub->link_next)
8031  	    {
8032  	      asection *subsection;
8033 
8034  	      for (subsection = sub->sections;
8035  		   subsection;
8036  		   subsection = subsection->next)
8037  		{
8038  		  if ((subsection->flags & SEC_ALLOC) == 0)
8039  		    continue;
8040  		  loadable_size += (subsection->_raw_size + 0xf) & ~0xf;
8041  		}
8042  	    }
8043  	  loadable_size += MIPS_FUNCTION_STUB_SIZE;
8044 
8045  	  /* Assume there are two loadable segments consisting of
8046  	     contiguous sections.  Is 5 enough?  */
8047  	  local_gotno = (loadable_size >> 16) + 5;
8048 	  if (IRIX_COMPAT (output_bfd) == ict_irix6)
8049 	    /* It's possible we will need GOT_PAGE entries as well as
8050 	       GOT16 entries.  Often, these will be able to share GOT
8051 	       entries, but not always.  */
8052 	    local_gotno *= 2;
8053 
8054  	  g->local_gotno += local_gotno;
8055  	  s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj);
8056 
8057  	  /* There has to be a global GOT entry for every symbol with
8058  	     a dynamic symbol table index of DT_MIPS_GOTSYM or
8059  	     higher.  Therefore, it make sense to put those symbols
8060  	     that need GOT entries at the end of the symbol table.  We
8061  	     do that here.  */
8062  	  if (!mips_elf_sort_hash_table (info, 1))
8063  	    return false;
8064 
8065 	  if (g->global_gotsym != NULL)
8066 	    i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
8067 	  else
8068 	    /* If there are no global symbols, or none requiring
8069 	       relocations, then GLOBAL_GOTSYM will be NULL.  */
8070 	    i = 0;
8071 	  g->global_gotno = i;
8072 	  s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj);
8073 	}
8074       else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
8075 	{
8076 	  /* Irix rld assumes that the function stub isn't at the end
8077 	     of .text section. So put a dummy. XXX  */
8078 	  s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
8079 	}
8080       else if (! info->shared
8081 	       && ! mips_elf_hash_table (info)->use_rld_obj_head
8082 	       && strncmp (name, ".rld_map", 8) == 0)
8083 	{
8084 	  /* We add a room for __rld_map. It will be filled in by the
8085 	     rtld to contain a pointer to the _r_debug structure.  */
8086 	  s->_raw_size += 4;
8087 	}
8088       else if (SGI_COMPAT (output_bfd)
8089 	       && strncmp (name, ".compact_rel", 12) == 0)
8090 	s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
8091       else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (output_bfd))
8092 	       == 0)
8093 	s->_raw_size = (sizeof (Elf32_External_Msym)
8094 			* (elf_hash_table (info)->dynsymcount
8095 			   + bfd_count_sections (output_bfd)));
8096       else if (strncmp (name, ".init", 5) != 0)
8097 	{
8098 	  /* It's not one of our sections, so don't allocate space.  */
8099 	  continue;
8100 	}
8101 
8102       if (strip)
8103 	{
8104 	  _bfd_strip_section_from_output (info, s);
8105 	  continue;
8106 	}
8107 
8108       /* Allocate memory for the section contents.  */
8109       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
8110       if (s->contents == NULL && s->_raw_size != 0)
8111 	{
8112 	  bfd_set_error (bfd_error_no_memory);
8113 	  return false;
8114 	}
8115     }
8116 
8117   if (elf_hash_table (info)->dynamic_sections_created)
8118     {
8119       /* Add some entries to the .dynamic section.  We fill in the
8120 	 values later, in elf_mips_finish_dynamic_sections, but we
8121 	 must add the entries now so that we get the correct size for
8122 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
8123 	 dynamic linker and used by the debugger.  */
8124       if (! info->shared)
8125 	{
8126 	  if (SGI_COMPAT (output_bfd))
8127 	    {
8128 	      /* SGI object has the equivalence of DT_DEBUG in the
8129 		 DT_MIPS_RLD_MAP entry.  */
8130 	      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8131 		return false;
8132 	    }
8133 	  else
8134 	    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8135 	      return false;
8136 	}
8137 
8138       if (reltext)
8139 	{
8140 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8141 	    return false;
8142 	}
8143 
8144       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8145 	return false;
8146 
8147       if (bfd_get_section_by_name (dynobj,
8148 				   MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)))
8149 	{
8150 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8151 	    return false;
8152 
8153 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8154 	    return false;
8155 
8156 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8157 	    return false;
8158 	}
8159 
8160       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0))
8161 	return false;
8162 
8163       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0))
8164 	return false;
8165 
8166       if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
8167 	{
8168 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0))
8169 	    return false;
8170 
8171 	  s = bfd_get_section_by_name (dynobj, ".liblist");
8172 	  BFD_ASSERT (s != NULL);
8173 
8174 	  if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0))
8175 	    return false;
8176 	}
8177 
8178       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8179 	return false;
8180 
8181       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8182 	return false;
8183 
8184 #if 0
8185       /* Time stamps in executable files are a bad idea.  */
8186       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0))
8187 	return false;
8188 #endif
8189 
8190 #if 0 /* FIXME  */
8191       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0))
8192 	return false;
8193 #endif
8194 
8195 #if 0 /* FIXME  */
8196       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0))
8197 	return false;
8198 #endif
8199 
8200       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8201 	return false;
8202 
8203       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8204 	return false;
8205 
8206       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8207 	return false;
8208 
8209       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8210 	return false;
8211 
8212       if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8213 	return false;
8214 
8215       if (IRIX_COMPAT (dynobj) == ict_irix5
8216 	  && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8217 	return false;
8218 
8219       if (IRIX_COMPAT (dynobj) == ict_irix6
8220 	  && (bfd_get_section_by_name
8221 	      (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8222 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8223 	return false;
8224 
8225       if (bfd_get_section_by_name (dynobj,
8226 				   MIPS_ELF_MSYM_SECTION_NAME (dynobj))
8227 	  && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0))
8228 	return false;
8229     }
8230 
8231   return true;
8232 }
8233 
8234 /* If NAME is one of the special IRIX6 symbols defined by the linker,
8235    adjust it appropriately now.  */
8236 
8237 static void
8238 mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
8239      bfd *abfd ATTRIBUTE_UNUSED;
8240      const char *name;
8241      Elf_Internal_Sym *sym;
8242 {
8243   /* The linker script takes care of providing names and values for
8244      these, but we must place them into the right sections.  */
8245   static const char* const text_section_symbols[] = {
8246     "_ftext",
8247     "_etext",
8248     "__dso_displacement",
8249     "__elf_header",
8250     "__program_header_table",
8251     NULL
8252   };
8253 
8254   static const char* const data_section_symbols[] = {
8255     "_fdata",
8256     "_edata",
8257     "_end",
8258     "_fbss",
8259     NULL
8260   };
8261 
8262   const char* const *p;
8263   int i;
8264 
8265   for (i = 0; i < 2; ++i)
8266     for (p = (i == 0) ? text_section_symbols : data_section_symbols;
8267 	 *p;
8268 	 ++p)
8269       if (strcmp (*p, name) == 0)
8270 	{
8271 	  /* All of these symbols are given type STT_SECTION by the
8272 	     IRIX6 linker.  */
8273 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8274 
8275 	  /* The IRIX linker puts these symbols in special sections.  */
8276 	  if (i == 0)
8277 	    sym->st_shndx = SHN_MIPS_TEXT;
8278 	  else
8279 	    sym->st_shndx = SHN_MIPS_DATA;
8280 
8281 	  break;
8282 	}
8283 }
8284 
8285 /* Finish up dynamic symbol handling.  We set the contents of various
8286    dynamic sections here.  */
8287 
8288 boolean
8289 _bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
8290      bfd *output_bfd;
8291      struct bfd_link_info *info;
8292      struct elf_link_hash_entry *h;
8293      Elf_Internal_Sym *sym;
8294 {
8295   bfd *dynobj;
8296   bfd_vma gval;
8297   asection *sgot;
8298   asection *smsym;
8299   struct mips_got_info *g;
8300   const char *name;
8301   struct mips_elf_link_hash_entry *mh;
8302 
8303   dynobj = elf_hash_table (info)->dynobj;
8304   gval = sym->st_value;
8305   mh = (struct mips_elf_link_hash_entry *) h;
8306 
8307   if (h->plt.offset != (bfd_vma) -1)
8308     {
8309       asection *s;
8310       bfd_byte *p;
8311       bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
8312 
8313       /* This symbol has a stub.  Set it up.  */
8314 
8315       BFD_ASSERT (h->dynindx != -1);
8316 
8317       s = bfd_get_section_by_name (dynobj,
8318 				   MIPS_ELF_STUB_SECTION_NAME (dynobj));
8319       BFD_ASSERT (s != NULL);
8320 
8321       /* Fill the stub.  */
8322       p = stub;
8323       bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
8324       p += 4;
8325       bfd_put_32 (output_bfd, STUB_MOVE, p);
8326       p += 4;
8327 
8328       /* FIXME: Can h->dynindex be more than 64K?  */
8329       if (h->dynindx & 0xffff0000)
8330 	return false;
8331 
8332       bfd_put_32 (output_bfd, STUB_JALR, p);
8333       p += 4;
8334       bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
8335 
8336       BFD_ASSERT (h->plt.offset <= s->_raw_size);
8337       memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
8338 
8339       /* Mark the symbol as undefined.  plt.offset != -1 occurs
8340 	 only for the referenced symbol.  */
8341       sym->st_shndx = SHN_UNDEF;
8342 
8343       /* The run-time linker uses the st_value field of the symbol
8344 	 to reset the global offset table entry for this external
8345 	 to its stub address when unlinking a shared object.  */
8346       gval = s->output_section->vma + s->output_offset + h->plt.offset;
8347       sym->st_value = gval;
8348     }
8349 
8350   BFD_ASSERT (h->dynindx != -1);
8351 
8352   sgot = mips_elf_got_section (dynobj);
8353   BFD_ASSERT (sgot != NULL);
8354   BFD_ASSERT (elf_section_data (sgot) != NULL);
8355   g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8356   BFD_ASSERT (g != NULL);
8357 
8358   /* Run through the global symbol table, creating GOT entries for all
8359      the symbols that need them.  */
8360   if (g->global_gotsym != NULL
8361       && h->dynindx >= g->global_gotsym->dynindx)
8362     {
8363       bfd_vma offset;
8364       bfd_vma value;
8365 
8366       if (sym->st_value)
8367 	value = sym->st_value;
8368       else
8369 	/* For an entity defined in a shared object, this will be
8370 	   NULL.  (For functions in shared objects for
8371 	   which we have created stubs, ST_VALUE will be non-NULL.
8372 	   That's because such the functions are now no longer defined
8373 	   in a shared object.)  */
8374 	value = h->root.u.def.value;
8375 
8376       offset = mips_elf_global_got_index (dynobj, h);
8377       MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
8378     }
8379 
8380   /* Create a .msym entry, if appropriate.  */
8381   smsym = bfd_get_section_by_name (dynobj,
8382 				   MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8383   if (smsym)
8384     {
8385       Elf32_Internal_Msym msym;
8386 
8387       msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
8388       /* It is undocumented what the `1' indicates, but IRIX6 uses
8389 	 this value.  */
8390       msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
8391       bfd_mips_elf_swap_msym_out
8392 	(dynobj, &msym,
8393 	 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
8394     }
8395 
8396   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
8397   name = h->root.root.string;
8398   if (strcmp (name, "_DYNAMIC") == 0
8399       || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
8400     sym->st_shndx = SHN_ABS;
8401   else if (strcmp (name, "_DYNAMIC_LINK") == 0)
8402     {
8403       sym->st_shndx = SHN_ABS;
8404       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8405       sym->st_value = 1;
8406     }
8407   else if (SGI_COMPAT (output_bfd))
8408     {
8409       if (strcmp (name, "_gp_disp") == 0)
8410 	{
8411 	  sym->st_shndx = SHN_ABS;
8412 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8413 	  sym->st_value = elf_gp (output_bfd);
8414 	}
8415       else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
8416 	       || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
8417 	{
8418 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8419 	  sym->st_other = STO_PROTECTED;
8420 	  sym->st_value = 0;
8421 	  sym->st_shndx = SHN_MIPS_DATA;
8422 	}
8423       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
8424 	{
8425 	  sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8426 	  sym->st_other = STO_PROTECTED;
8427 	  sym->st_value = mips_elf_hash_table (info)->procedure_count;
8428 	  sym->st_shndx = SHN_ABS;
8429 	}
8430       else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
8431 	{
8432 	  if (h->type == STT_FUNC)
8433 	    sym->st_shndx = SHN_MIPS_TEXT;
8434 	  else if (h->type == STT_OBJECT)
8435 	    sym->st_shndx = SHN_MIPS_DATA;
8436 	}
8437     }
8438 
8439   /* Handle the IRIX6-specific symbols.  */
8440   if (IRIX_COMPAT (output_bfd) == ict_irix6)
8441     mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
8442 
8443   if (SGI_COMPAT (output_bfd)
8444       && ! info->shared)
8445     {
8446       if (! mips_elf_hash_table (info)->use_rld_obj_head
8447 	  && strcmp (name, "__rld_map") == 0)
8448 	{
8449 	  asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
8450 	  BFD_ASSERT (s != NULL);
8451 	  sym->st_value = s->output_section->vma + s->output_offset;
8452 	  bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
8453 	  if (mips_elf_hash_table (info)->rld_value == 0)
8454 	    mips_elf_hash_table (info)->rld_value = sym->st_value;
8455 	}
8456       else if (mips_elf_hash_table (info)->use_rld_obj_head
8457 	       && strcmp (name, "__rld_obj_head") == 0)
8458 	{
8459 	  /* IRIX6 does not use a .rld_map section.  */
8460 	  if (IRIX_COMPAT (output_bfd) == ict_irix5)
8461 	    BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
8462 			!= NULL);
8463 	  mips_elf_hash_table (info)->rld_value = sym->st_value;
8464 	}
8465     }
8466 
8467   /* If this is a mips16 symbol, force the value to be even.  */
8468   if (sym->st_other == STO_MIPS16
8469       && (sym->st_value & 1) != 0)
8470     --sym->st_value;
8471 
8472   return true;
8473 }
8474 
8475 /* Finish up the dynamic sections.  */
8476 
8477 boolean
8478 _bfd_mips_elf_finish_dynamic_sections (output_bfd, info)
8479      bfd *output_bfd;
8480      struct bfd_link_info *info;
8481 {
8482   bfd *dynobj;
8483   asection *sdyn;
8484   asection *sgot;
8485   struct mips_got_info *g;
8486 
8487   dynobj = elf_hash_table (info)->dynobj;
8488 
8489   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8490 
8491   sgot = mips_elf_got_section (dynobj);
8492   if (sgot == NULL)
8493     g = NULL;
8494   else
8495     {
8496       BFD_ASSERT (elf_section_data (sgot) != NULL);
8497       g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8498       BFD_ASSERT (g != NULL);
8499     }
8500 
8501   if (elf_hash_table (info)->dynamic_sections_created)
8502     {
8503       bfd_byte *b;
8504 
8505       BFD_ASSERT (sdyn != NULL);
8506       BFD_ASSERT (g != NULL);
8507 
8508       for (b = sdyn->contents;
8509 	   b < sdyn->contents + sdyn->_raw_size;
8510 	   b += MIPS_ELF_DYN_SIZE (dynobj))
8511 	{
8512 	  Elf_Internal_Dyn dyn;
8513 	  const char *name;
8514 	  size_t elemsize;
8515 	  asection *s;
8516 	  boolean swap_out_p;
8517 
8518 	  /* Read in the current dynamic entry.  */
8519 	  (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
8520 
8521 	  /* Assume that we're going to modify it and write it out.  */
8522 	  swap_out_p = true;
8523 
8524 	  switch (dyn.d_tag)
8525 	    {
8526 	    case DT_RELENT:
8527 	      s = (bfd_get_section_by_name
8528 		   (dynobj,
8529 		    MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)));
8530 	      BFD_ASSERT (s != NULL);
8531 	      dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
8532 	      break;
8533 
8534 	    case DT_STRSZ:
8535 	      /* Rewrite DT_STRSZ.  */
8536 	      dyn.d_un.d_val =
8537 		_bfd_stringtab_size (elf_hash_table (info)->dynstr);
8538 	      break;
8539 
8540 	    case DT_PLTGOT:
8541 	      name = ".got";
8542 	      goto get_vma;
8543 	    case DT_MIPS_CONFLICT:
8544 	      name = ".conflict";
8545 	      goto get_vma;
8546 	    case DT_MIPS_LIBLIST:
8547 	      name = ".liblist";
8548 	    get_vma:
8549 	      s = bfd_get_section_by_name (output_bfd, name);
8550 	      BFD_ASSERT (s != NULL);
8551 	      dyn.d_un.d_ptr = s->vma;
8552 	      break;
8553 
8554 	    case DT_MIPS_RLD_VERSION:
8555 	      dyn.d_un.d_val = 1; /* XXX */
8556 	      break;
8557 
8558 	    case DT_MIPS_FLAGS:
8559 	      dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
8560 	      break;
8561 
8562 	    case DT_MIPS_CONFLICTNO:
8563 	      name = ".conflict";
8564 	      elemsize = sizeof (Elf32_Conflict);
8565 	      goto set_elemno;
8566 
8567 	    case DT_MIPS_LIBLISTNO:
8568 	      name = ".liblist";
8569 	      elemsize = sizeof (Elf32_Lib);
8570 	    set_elemno:
8571 	      s = bfd_get_section_by_name (output_bfd, name);
8572 	      if (s != NULL)
8573 		{
8574 		  if (s->_cooked_size != 0)
8575 		    dyn.d_un.d_val = s->_cooked_size / elemsize;
8576 		  else
8577 		    dyn.d_un.d_val = s->_raw_size / elemsize;
8578 		}
8579 	      else
8580 		    dyn.d_un.d_val = 0;
8581 	      break;
8582 
8583 	    case DT_MIPS_TIME_STAMP:
8584 	      time ((time_t *) &dyn.d_un.d_val);
8585 	      break;
8586 
8587 	    case DT_MIPS_ICHECKSUM:
8588 	      /* XXX FIXME: */
8589 	      swap_out_p = false;
8590 	      break;
8591 
8592 	    case DT_MIPS_IVERSION:
8593 	      /* XXX FIXME: */
8594 	      swap_out_p = false;
8595 	      break;
8596 
8597 	    case DT_MIPS_BASE_ADDRESS:
8598 	      s = output_bfd->sections;
8599 	      BFD_ASSERT (s != NULL);
8600 	      dyn.d_un.d_ptr = s->vma & ~(0xffff);
8601 	      break;
8602 
8603 	    case DT_MIPS_LOCAL_GOTNO:
8604 	      dyn.d_un.d_val = g->local_gotno;
8605 	      break;
8606 
8607 	    case DT_MIPS_UNREFEXTNO:
8608 	      /* The index into the dynamic symbol table which is the
8609 		 entry of the first external symbol that is not
8610 		 referenced within the same object.  */
8611 	      dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
8612 	      break;
8613 
8614 	    case DT_MIPS_GOTSYM:
8615 	      if (g->global_gotsym)
8616 		{
8617 		  dyn.d_un.d_val = g->global_gotsym->dynindx;
8618 		  break;
8619 		}
8620 	      /* In case if we don't have global got symbols we default
8621 		 to setting DT_MIPS_GOTSYM to the same value as
8622 		 DT_MIPS_SYMTABNO, so we just fall through.  */
8623 
8624 	    case DT_MIPS_SYMTABNO:
8625 	      name = ".dynsym";
8626 	      elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
8627 	      s = bfd_get_section_by_name (output_bfd, name);
8628 	      BFD_ASSERT (s != NULL);
8629 
8630 	      if (s->_cooked_size != 0)
8631 		dyn.d_un.d_val = s->_cooked_size / elemsize;
8632 	      else
8633 		dyn.d_un.d_val = s->_raw_size / elemsize;
8634 	      break;
8635 
8636 	    case DT_MIPS_HIPAGENO:
8637 	      dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
8638 	      break;
8639 
8640 	    case DT_MIPS_RLD_MAP:
8641 	      dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
8642 	      break;
8643 
8644 	    case DT_MIPS_OPTIONS:
8645 	      s = (bfd_get_section_by_name
8646 		   (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
8647 	      dyn.d_un.d_ptr = s->vma;
8648 	      break;
8649 
8650 	    case DT_MIPS_MSYM:
8651 	      s = (bfd_get_section_by_name
8652 		   (output_bfd, MIPS_ELF_MSYM_SECTION_NAME (output_bfd)));
8653 	      dyn.d_un.d_ptr = s->vma;
8654 	      break;
8655 
8656 	    default:
8657 	      swap_out_p = false;
8658 	      break;
8659 	    }
8660 
8661 	  if (swap_out_p)
8662 	    (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
8663 	      (dynobj, &dyn, b);
8664 	}
8665     }
8666 
8667   /* The first entry of the global offset table will be filled at
8668      runtime. The second entry will be used by some runtime loaders.
8669      This isn't the case of Irix rld. */
8670   if (sgot != NULL && sgot->_raw_size > 0)
8671     {
8672       MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
8673       MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
8674 			 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
8675     }
8676 
8677   if (sgot != NULL)
8678     elf_section_data (sgot->output_section)->this_hdr.sh_entsize
8679       = MIPS_ELF_GOT_SIZE (output_bfd);
8680 
8681   {
8682     asection *smsym;
8683     asection *s;
8684     Elf32_compact_rel cpt;
8685 
8686     /* ??? The section symbols for the output sections were set up in
8687        _bfd_elf_final_link.  SGI sets the STT_NOTYPE attribute for these
8688        symbols.  Should we do so?  */
8689 
8690     smsym = bfd_get_section_by_name (dynobj,
8691 				     MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8692     if (smsym != NULL)
8693       {
8694 	Elf32_Internal_Msym msym;
8695 
8696 	msym.ms_hash_value = 0;
8697 	msym.ms_info = ELF32_MS_INFO (0, 1);
8698 
8699 	for (s = output_bfd->sections; s != NULL; s = s->next)
8700 	  {
8701 	    long dynindx = elf_section_data (s)->dynindx;
8702 
8703 	    bfd_mips_elf_swap_msym_out
8704 	      (output_bfd, &msym,
8705 	       (((Elf32_External_Msym *) smsym->contents)
8706 		+ dynindx));
8707 	  }
8708       }
8709 
8710     if (SGI_COMPAT (output_bfd))
8711       {
8712 	/* Write .compact_rel section out.  */
8713 	s = bfd_get_section_by_name (dynobj, ".compact_rel");
8714 	if (s != NULL)
8715 	  {
8716 	    cpt.id1 = 1;
8717 	    cpt.num = s->reloc_count;
8718 	    cpt.id2 = 2;
8719 	    cpt.offset = (s->output_section->filepos
8720 			  + sizeof (Elf32_External_compact_rel));
8721 	    cpt.reserved0 = 0;
8722 	    cpt.reserved1 = 0;
8723 	    bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
8724 					    ((Elf32_External_compact_rel *)
8725 					     s->contents));
8726 
8727 	    /* Clean up a dummy stub function entry in .text.  */
8728 	    s = bfd_get_section_by_name (dynobj,
8729 					 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8730 	    if (s != NULL)
8731 	      {
8732 		file_ptr dummy_offset;
8733 
8734 		BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
8735 		dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
8736 		memset (s->contents + dummy_offset, 0,
8737 			MIPS_FUNCTION_STUB_SIZE);
8738 	      }
8739 	  }
8740       }
8741 
8742     /* Clean up a first relocation in .rel.dyn.  */
8743     s = bfd_get_section_by_name (dynobj,
8744 				 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj));
8745     if (s != NULL && s->_raw_size > 0)
8746       memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj));
8747   }
8748 
8749   return true;
8750 }
8751 
8752 /* This is almost identical to bfd_generic_get_... except that some
8753    MIPS relocations need to be handled specially.  Sigh.  */
8754 
8755 static bfd_byte *
8756 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
8757 					   relocateable, symbols)
8758      bfd *abfd;
8759      struct bfd_link_info *link_info;
8760      struct bfd_link_order *link_order;
8761      bfd_byte *data;
8762      boolean relocateable;
8763      asymbol **symbols;
8764 {
8765   /* Get enough memory to hold the stuff */
8766   bfd *input_bfd = link_order->u.indirect.section->owner;
8767   asection *input_section = link_order->u.indirect.section;
8768 
8769   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
8770   arelent **reloc_vector = NULL;
8771   long reloc_count;
8772 
8773   if (reloc_size < 0)
8774     goto error_return;
8775 
8776   reloc_vector = (arelent **) bfd_malloc (reloc_size);
8777   if (reloc_vector == NULL && reloc_size != 0)
8778     goto error_return;
8779 
8780   /* read in the section */
8781   if (!bfd_get_section_contents (input_bfd,
8782 				 input_section,
8783 				 (PTR) data,
8784 				 0,
8785 				 input_section->_raw_size))
8786     goto error_return;
8787 
8788   /* We're not relaxing the section, so just copy the size info */
8789   input_section->_cooked_size = input_section->_raw_size;
8790   input_section->reloc_done = true;
8791 
8792   reloc_count = bfd_canonicalize_reloc (input_bfd,
8793 					input_section,
8794 					reloc_vector,
8795 					symbols);
8796   if (reloc_count < 0)
8797     goto error_return;
8798 
8799   if (reloc_count > 0)
8800     {
8801       arelent **parent;
8802       /* for mips */
8803       int gp_found;
8804       bfd_vma gp = 0x12345678;	/* initialize just to shut gcc up */
8805 
8806       {
8807 	struct bfd_hash_entry *h;
8808 	struct bfd_link_hash_entry *lh;
8809 	/* Skip all this stuff if we aren't mixing formats.  */
8810 	if (abfd && input_bfd
8811 	    && abfd->xvec == input_bfd->xvec)
8812 	  lh = 0;
8813 	else
8814 	  {
8815 	    h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
8816 	    lh = (struct bfd_link_hash_entry *) h;
8817 	  }
8818       lookup:
8819 	if (lh)
8820 	  {
8821 	    switch (lh->type)
8822 	      {
8823 	      case bfd_link_hash_undefined:
8824 	      case bfd_link_hash_undefweak:
8825 	      case bfd_link_hash_common:
8826 		gp_found = 0;
8827 		break;
8828 	      case bfd_link_hash_defined:
8829 	      case bfd_link_hash_defweak:
8830 		gp_found = 1;
8831 		gp = lh->u.def.value;
8832 		break;
8833 	      case bfd_link_hash_indirect:
8834 	      case bfd_link_hash_warning:
8835 		lh = lh->u.i.link;
8836 		/* @@FIXME  ignoring warning for now */
8837 		goto lookup;
8838 	      case bfd_link_hash_new:
8839 	      default:
8840 		abort ();
8841 	      }
8842 	  }
8843 	else
8844 	  gp_found = 0;
8845       }
8846       /* end mips */
8847       for (parent = reloc_vector; *parent != (arelent *) NULL;
8848 	   parent++)
8849 	{
8850 	  char *error_message = (char *) NULL;
8851 	  bfd_reloc_status_type r;
8852 
8853 	  /* Specific to MIPS: Deal with relocation types that require
8854 	     knowing the gp of the output bfd.  */
8855 	  asymbol *sym = *(*parent)->sym_ptr_ptr;
8856 	  if (bfd_is_abs_section (sym->section) && abfd)
8857 	    {
8858 	      /* The special_function wouldn't get called anyways.  */
8859 	    }
8860 	  else if (!gp_found)
8861 	    {
8862 	      /* The gp isn't there; let the special function code
8863 		 fall over on its own.  */
8864 	    }
8865 	  else if ((*parent)->howto->special_function
8866 		   == _bfd_mips_elf_gprel16_reloc)
8867 	    {
8868 	      /* bypass special_function call */
8869 	      r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
8870 				   relocateable, (PTR) data, gp);
8871 	      goto skip_bfd_perform_relocation;
8872 	    }
8873 	  /* end mips specific stuff */
8874 
8875 	  r = bfd_perform_relocation (input_bfd,
8876 				      *parent,
8877 				      (PTR) data,
8878 				      input_section,
8879 				      relocateable ? abfd : (bfd *) NULL,
8880 				      &error_message);
8881 	skip_bfd_perform_relocation:
8882 
8883 	  if (relocateable)
8884 	    {
8885 	      asection *os = input_section->output_section;
8886 
8887 	      /* A partial link, so keep the relocs */
8888 	      os->orelocation[os->reloc_count] = *parent;
8889 	      os->reloc_count++;
8890 	    }
8891 
8892 	  if (r != bfd_reloc_ok)
8893 	    {
8894 	      switch (r)
8895 		{
8896 		case bfd_reloc_undefined:
8897 		  if (!((*link_info->callbacks->undefined_symbol)
8898 			(link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8899 			 input_bfd, input_section, (*parent)->address,
8900 			 true)))
8901 		    goto error_return;
8902 		  break;
8903 		case bfd_reloc_dangerous:
8904 		  BFD_ASSERT (error_message != (char *) NULL);
8905 		  if (!((*link_info->callbacks->reloc_dangerous)
8906 			(link_info, error_message, input_bfd, input_section,
8907 			 (*parent)->address)))
8908 		    goto error_return;
8909 		  break;
8910 		case bfd_reloc_overflow:
8911 		  if (!((*link_info->callbacks->reloc_overflow)
8912 			(link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8913 			 (*parent)->howto->name, (*parent)->addend,
8914 			 input_bfd, input_section, (*parent)->address)))
8915 		    goto error_return;
8916 		  break;
8917 		case bfd_reloc_outofrange:
8918 		default:
8919 		  abort ();
8920 		  break;
8921 		}
8922 
8923 	    }
8924 	}
8925     }
8926   if (reloc_vector != NULL)
8927     free (reloc_vector);
8928   return data;
8929 
8930 error_return:
8931   if (reloc_vector != NULL)
8932     free (reloc_vector);
8933   return NULL;
8934 }
8935 #define bfd_elf32_bfd_get_relocated_section_contents \
8936   elf32_mips_get_relocated_section_contents
8937 
8938 /* ECOFF swapping routines.  These are used when dealing with the
8939    .mdebug section, which is in the ECOFF debugging format.  */
8940 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
8941 {
8942   /* Symbol table magic number.  */
8943   magicSym,
8944   /* Alignment of debugging information.  E.g., 4.  */
8945   4,
8946   /* Sizes of external symbolic information.  */
8947   sizeof (struct hdr_ext),
8948   sizeof (struct dnr_ext),
8949   sizeof (struct pdr_ext),
8950   sizeof (struct sym_ext),
8951   sizeof (struct opt_ext),
8952   sizeof (struct fdr_ext),
8953   sizeof (struct rfd_ext),
8954   sizeof (struct ext_ext),
8955   /* Functions to swap in external symbolic data.  */
8956   ecoff_swap_hdr_in,
8957   ecoff_swap_dnr_in,
8958   ecoff_swap_pdr_in,
8959   ecoff_swap_sym_in,
8960   ecoff_swap_opt_in,
8961   ecoff_swap_fdr_in,
8962   ecoff_swap_rfd_in,
8963   ecoff_swap_ext_in,
8964   _bfd_ecoff_swap_tir_in,
8965   _bfd_ecoff_swap_rndx_in,
8966   /* Functions to swap out external symbolic data.  */
8967   ecoff_swap_hdr_out,
8968   ecoff_swap_dnr_out,
8969   ecoff_swap_pdr_out,
8970   ecoff_swap_sym_out,
8971   ecoff_swap_opt_out,
8972   ecoff_swap_fdr_out,
8973   ecoff_swap_rfd_out,
8974   ecoff_swap_ext_out,
8975   _bfd_ecoff_swap_tir_out,
8976   _bfd_ecoff_swap_rndx_out,
8977   /* Function to read in symbolic data.  */
8978   _bfd_mips_elf_read_ecoff_info
8979 };
8980 
8981 #define TARGET_LITTLE_SYM		bfd_elf32_littlemips_vec
8982 #define TARGET_LITTLE_NAME		"elf32-littlemips"
8983 #define TARGET_BIG_SYM			bfd_elf32_bigmips_vec
8984 #define TARGET_BIG_NAME			"elf32-bigmips"
8985 #define ELF_ARCH			bfd_arch_mips
8986 #define ELF_MACHINE_CODE		EM_MIPS
8987 
8988 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
8989    a value of 0x1000, and we are compatible.  */
8990 #define ELF_MAXPAGESIZE			0x1000
8991 
8992 #define elf_backend_collect		true
8993 #define elf_backend_type_change_ok	true
8994 #define elf_backend_can_gc_sections	true
8995 #define elf_backend_sign_extend_vma	true
8996 #define elf_info_to_howto		mips_info_to_howto_rela
8997 #define elf_info_to_howto_rel		mips_info_to_howto_rel
8998 #define elf_backend_sym_is_global	mips_elf_sym_is_global
8999 #define elf_backend_object_p		_bfd_mips_elf_object_p
9000 #define elf_backend_section_from_shdr	_bfd_mips_elf_section_from_shdr
9001 #define elf_backend_fake_sections	_bfd_mips_elf_fake_sections
9002 #define elf_backend_section_from_bfd_section \
9003 					_bfd_mips_elf_section_from_bfd_section
9004 #define elf_backend_section_processing	_bfd_mips_elf_section_processing
9005 #define elf_backend_symbol_processing	_bfd_mips_elf_symbol_processing
9006 #define elf_backend_additional_program_headers \
9007 					_bfd_mips_elf_additional_program_headers
9008 #define elf_backend_modify_segment_map	_bfd_mips_elf_modify_segment_map
9009 #define elf_backend_final_write_processing \
9010 					_bfd_mips_elf_final_write_processing
9011 #define elf_backend_ecoff_debug_swap	&mips_elf32_ecoff_debug_swap
9012 #define elf_backend_add_symbol_hook	_bfd_mips_elf_add_symbol_hook
9013 #define elf_backend_create_dynamic_sections \
9014 					_bfd_mips_elf_create_dynamic_sections
9015 #define elf_backend_check_relocs	_bfd_mips_elf_check_relocs
9016 #define elf_backend_adjust_dynamic_symbol \
9017 					_bfd_mips_elf_adjust_dynamic_symbol
9018 #define elf_backend_always_size_sections \
9019 					_bfd_mips_elf_always_size_sections
9020 #define elf_backend_size_dynamic_sections \
9021 					_bfd_mips_elf_size_dynamic_sections
9022 #define elf_backend_relocate_section	_bfd_mips_elf_relocate_section
9023 #define elf_backend_link_output_symbol_hook \
9024 					_bfd_mips_elf_link_output_symbol_hook
9025 #define elf_backend_finish_dynamic_symbol \
9026 					_bfd_mips_elf_finish_dynamic_symbol
9027 #define elf_backend_finish_dynamic_sections \
9028 					_bfd_mips_elf_finish_dynamic_sections
9029 #define elf_backend_gc_mark_hook	_bfd_mips_elf_gc_mark_hook
9030 #define elf_backend_gc_sweep_hook	_bfd_mips_elf_gc_sweep_hook
9031 
9032 #define elf_backend_got_header_size	(4*MIPS_RESERVED_GOTNO)
9033 #define elf_backend_plt_header_size	0
9034 
9035 #define bfd_elf32_bfd_is_local_label_name \
9036 					mips_elf_is_local_label_name
9037 #define bfd_elf32_find_nearest_line	_bfd_mips_elf_find_nearest_line
9038 #define bfd_elf32_set_section_contents	_bfd_mips_elf_set_section_contents
9039 #define bfd_elf32_bfd_link_hash_table_create \
9040 					_bfd_mips_elf_link_hash_table_create
9041 #define bfd_elf32_bfd_final_link	_bfd_mips_elf_final_link
9042 #define bfd_elf32_bfd_copy_private_bfd_data \
9043 					_bfd_mips_elf_copy_private_bfd_data
9044 #define bfd_elf32_bfd_merge_private_bfd_data \
9045 					_bfd_mips_elf_merge_private_bfd_data
9046 #define bfd_elf32_bfd_set_private_flags	_bfd_mips_elf_set_private_flags
9047 #define bfd_elf32_bfd_print_private_bfd_data \
9048 					_bfd_mips_elf_print_private_bfd_data
9049 #include "elf32-target.h"
9050