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