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