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